~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/util.js

  • Committer: dcoles
  • Date: 2008-03-09 02:34:46 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:671
forum: Now uses a unique secret generated at './setup config' time for shared secret

setup.py: Generate and write forum_secret to config.py and config.php
w/p/p/i/session.php: Use forum_secret from config.php and some code cleaning
l/c/forumutil.py: Use config.py to get forum_secret to make HMAC 

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
/* Expects the following variables to have been declared by JavaScript in
26
26
 * the HTML generated by the server:
27
27
 * - root_dir
28
 
 * - public_host
29
28
 * - username
30
29
 */
31
30
 
311
310
    if (("path" in obj) && obj.path != null)
312
311
    {
313
312
        var path = urlencode_path(obj.path.toString());
314
 
        if (url.length > 0 && path.length > 0 && path.charAt(0) != "/")
 
313
        if (url.length > 0 && path.length > 0 && path[0] != "/")
315
314
            path = "/" + path;
316
315
        url += path;
317
316
    }
426
425
    {
427
426
        arg = arguments[i];
428
427
        if (arg.length == 0) continue;
429
 
        if (arg.charAt(0) == '/')
 
428
        if (arg[0] == '/')
430
429
            path = arg;
431
430
        else
432
431
        {
433
 
            if (path.length > 0 && path.charAt(path.length-1) != '/')
 
432
            if (path.length > 0 && path[path.length-1] != '/')
434
433
                path += '/';
435
434
            path += arg;
436
435
        }
491
490
function pathlist_to_path(pathlist)
492
491
{
493
492
    ret = path_join.apply(null, pathlist);
494
 
    if (ret.charAt(ret.length-1) != '/')
 
493
    if (ret[ret.length-1] != '/')
495
494
        ret += '/';
496
495
    return ret;
497
496
}
512
511
    return make_path(path_join.apply(null, arguments));
513
512
}
514
513
 
515
 
/** Generates an absolute URL to a public application
516
 
 */
517
 
function public_app_path(app /*,...*/)
518
 
{
519
 
    return location.protocol + "//" + public_host
520
 
        + make_path(path_join.apply(null, arguments));
521
 
}
522
 
 
523
514
/** Given a path, gets the "basename" (the last path segment).
524
515
 */
525
516
function path_basename(path)
603
594
    }
604
595
}
605
596
 
606
 
/** Creates a random string of length length,
607
 
 * consisting of alphanumeric characters.
608
 
 */
609
 
var rand_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"
610
 
               + "abcdefghiklmnopqrstuvwxyz";
611
 
function random_string(length)
612
 
{
613
 
    var str = Array(length);
614
 
    var v;
615
 
    for (var i=0; i<length; i++)
616
 
    {
617
 
        v = Math.floor(Math.random() * rand_chars.length);
618
 
        str[i] = rand_chars.charAt(v);
619
 
    }
620
 
    return str.join('');
621
 
}
622
 
 
623
 
/** Makes an XMLHttpRequest call to the server.
 
597
/** Makes an asynchronous XMLHttpRequest call to the server.
624
598
 * Sends the XMLHttpRequest object containing the completed response to a
625
599
 * specified callback function.
626
600
 *
627
601
 * \param callback A callback function. Will be called when the response is
628
602
 *      complete. Passed 1 parameter, an XMLHttpRequest object containing the
629
 
 *      completed response. If callback is null this is a syncronous request 
630
 
 *      otherwise this is an asynchronous request.
 
603
 *      completed response.
631
604
 * \param app IVLE app to call (such as "fileservice").
632
605
 * \param path URL path to make the request to, within the application.
633
606
 * \param args Argument object, as described in parse_url and friends.
645
618
    /* A random string, for multipart/form-data
646
619
     * (This is not checked against anywhere else, it is solely defined and
647
620
     * used within this function) */
648
 
    var boundary = random_string(20);
 
621
    var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
649
622
    var xhr = new_xmlhttprequest();
650
 
    var asyncronous = callback != null;
651
 
    if (asyncronous)
652
 
    {
653
 
        xhr.onreadystatechange = function()
 
623
    xhr.onreadystatechange = function()
 
624
        {
 
625
            if (xhr.readyState == 4)
654
626
            {
655
 
                if (xhr.readyState == 4)
656
 
                {
657
 
                    callback(xhr);
658
 
                }
 
627
                callback(xhr);
659
628
            }
660
 
    }
 
629
        }
661
630
    if (method == "GET")
662
631
    {
663
632
        /* GET sends the args in the URL */
664
633
        url = build_url({"path": path, "args": args});
665
634
        /* open's 3rd argument = true -> asynchronous */
666
 
        xhr.open(method, url, asyncronous);
 
635
        xhr.open(method, url, true);
667
636
        xhr.send(null);
668
637
    }
669
638
    else
670
639
    {
671
640
        /* POST sends the args in application/x-www-form-urlencoded */
672
641
        url = encodeURI(path);
673
 
        xhr.open(method, url, asyncronous);
 
642
        xhr.open(method, url, true);
674
643
        var message;
675
644
        if (content_type == "multipart/form-data")
676
645
        {
677
646
            xhr.setRequestHeader("Content-Type",
678
 
                "multipart/form-data; boundary=" + boundary);
 
647
                "multipart/form-data, boundary=" + boundary);
679
648
            message = make_multipart_formdata(args, boundary);
680
649
        }
681
650
        else
683
652
            xhr.setRequestHeader("Content-Type", content_type);
684
653
            message = make_query_string(args);
685
654
        }
 
655
        xhr.setRequestHeader("Content-Length", message.length);
686
656
        xhr.send(message);
687
657
    }
688
 
    /* Only return the XHR for syncronous requests */
689
 
    if (!asyncronous)
690
 
    { 
691
 
        return xhr;
692
 
    }
693
658
}
694
659
 
695
 
/** Attempts to JSON decodes a response object
696
 
 * If a non-200 response or the JSON decode fails then returns null
697
 
 */
698
 
function decode_response(response)
699
 
{
700
 
    if (response.status == 200)
701
 
    {
702
 
        try
703
 
        {
704
 
            var responseText = response.responseText;
705
 
            return JSON.parse(responseText);
706
 
        }
707
 
        catch (e)
708
 
        {
709
 
            // Pass
710
 
        }
711
 
     }
712
 
    
713
 
     return null;
714
 
}