root/branches/release-39/default_templates/javascript.mtml @ 2490

Revision 2490, 25.1 kB (checked in by bchoate, 18 months ago)

Fixes for double-escaping in javascript strings.

  • Property svn:keywords set to Id Revision
Line 
1<mt:Ignore>
2/* The following functions are here to support legacy MT templates.
3   If you have refreshed your JavaScript template but still use older
4   MT comment templates, you may need to uncomment this block in order
5   for those templates to work properly. Simply remove the wrapping
6   'mt:Ignore' tag to do so. */
7function hideDocumentElement(id) { return mtHide(id) }
8function showDocumentElement(id) { return mtShow(id) }
9function individualArchivesOnLoad() { return mtEntryOnLoad() }
10function writeCommenterGreeting() { return mtShowGreeting() }
11function rememberMe(f) { return mtRememberMe(f) }
12function forgetMe(f) { return mtForgetMe(f) }
13</mt:Ignore>
14
15// The cookie name to use for storing the blog-side comment session cookie.
16var mtCookieName = "<$mt:UserSessionCookieName$>";
17var mtCookieDomain = "<$mt:UserSessionCookieDomain$>";
18var mtCookiePath = "<$mt:UserSessionCookiePath$>";
19var mtCookieTimeout = <$mt:UserSessionCookieTimeout$>;
20
21<mt:Ignore>
22/***
23 * Simple routine for showing a DOM element (applying a CSS display
24 * attribute of 'none').
25 */
26</mt:Ignore>
27function mtHide(id) {
28    var el = (typeof id == "string") ? document.getElementById(id) : id;
29    if (el) el.style.display = 'none';
30}
31
32<mt:Ignore>
33/***
34 * Simple routine for showing a DOM element (applying a CSS display
35 * attribute of 'block').
36 */
37</mt:Ignore>
38function mtShow(id) {
39    var el = (typeof id == "string") ? document.getElementById(id) : id;
40    if (el) el.style.display = 'block';
41}
42
43<mt:Ignore>
44/***
45 * A utility function for assigning/adding handlers to window events.
46 */
47</mt:Ignore>
48function mtAttachEvent(eventName,func) {
49    var onEventName = 'on' + eventName;
50    var old = window[onEventName];
51    if( typeof old != 'function' )
52        window[onEventName] = func;
53    else {
54        window[onEventName] = function( evt ) {
55            old( evt );
56            return func( evt );
57        };
58    }
59}
60
61<mt:Ignore>
62/***
63 * Calls the event named, if there are handlers for it.
64 */
65</mt:Ignore>
66function mtFireEvent(eventName,param) {
67    var fn = window['on' + eventName];
68    if (typeof fn == 'function') return fn(param);
69    return;
70}
71
72<mt:Ignore>
73/***
74 * Displays a relative date.
75 * 'ts' is a Date object, 'fds' is a string of the date which
76 * will be displayed if the given date is older than 1 week.
77 */
78</mt:Ignore>
79function mtRelativeDate(ts, fds) {
80    var now = new Date();
81    var ref = ts;
82    var delta = Math.floor((now.getTime() - ref.getTime()) / 1000);
83
84    var str;
85    if (delta < 60) {
86        str = '<__trans phrase="moments ago" escape="js">';
87    } else if (delta <= 86400) {
88        // less than 1 day
89        var hours = Math.floor(delta / 3600);
90        var min = Math.floor((delta % 3600) / 60);
91        if (hours == 1)
92            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="1" escape="js">';
93        else if (hours > 1)
94            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="2" escape="js">'.replace(/2/, hours);
95        else if (min == 1)
96            str = '<__trans phrase="[quant,_1,minute,minutes] ago" params="1" escape="js">';
97        else
98            str = '<__trans phrase="[quant,_1,minute,minutes] ago" params="2" escape="js">'.replace(/2/, min);
99    } else if (delta <= 604800) {
100        // less than 1 week
101        var days = Math.floor(delta / 86400);
102        var hours = Math.floor((delta % 86400) / 3600);
103        if (days == 1)
104            str = '<__trans phrase="[quant,_1,day,days] ago" params="1" escape="js">';
105        else if (days > 1)
106            str = '<__trans phrase="[quant,_1,day,days] ago" params="2" escape="js">'.replace(/2/, days);
107        else if (hours == 1)
108            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="1" escape="js">';
109        else
110            str = '<__trans phrase="[quant,_1,hour,hours] ago" params="2" escape="js">'.replace(/2/, hours);
111    }
112    return str ? str : fds;
113}
114
115<mt:Ignore>
116/***
117 * Used to display an edit link for the given entry.
118 */
119</mt:Ignore>
120function mtEditLink(entry_id, author_id) {
121    var u = mtGetUser();
122    if (! u) return;
123    if (! entry_id) return;
124    if (! author_id) return;
125    if (u.id != author_id) return;
126    var link = '<a href="<$mt:AdminScript$>?__mode=view&amp;_type=entry&amp;id=' + entry_id + '"><__trans phrase="Edit" escape="js"></a>';
127    document.write(link);
128}
129
130<mt:Ignore>
131/***
132 * Called when an input field on the comment form receives focus.
133 */
134</mt:Ignore>
135function mtCommentFormOnFocus() {
136    // if CAPTCHA is enabled, this causes the captcha image to be
137    // displayed if it hasn't been already.
138    mtShowCaptcha();
139}
140
141<mt:Ignore>
142/***
143 * Displays a captcha field for anonymous commenters.
144 */
145</mt:Ignore>
146var mtCaptchaVisible = false;
147function mtShowCaptcha() {
148    var u = mtGetUser();
149    if ( u && u.is_authenticated ) return;
150    if (mtCaptchaVisible) return;
151    var div = document.getElementById('comments-open-captcha');
152    if (div) {
153        div.innerHTML = '<$mt:CaptchaFields$>';
154        mtCaptchaVisible = true;
155    }
156}
157
158<mt:Ignore>
159/* user object
160    -- saved in user cookie --
161    u.name (display name)
162    u.url (link to home page)
163    u.email (for anonymous only)
164    u.userpic (url for commenter/author)
165    u.profile (link to profile)
166    u.is_trusted (boolean)
167    u.is_author (user has posting rights)
168    u.is_banned (banned status; neither post/comment perms)
169    u.can_post (has permission to post)
170    u.can_comment (has permission to comment)
171
172    -- status fields --
173    u.is_authenticated (boolean)
174    u.is_anonymous (user is anonymous)
175*/
176</mt:Ignore>
177
178var is_preview;
179var user;
180<mt:Ignore>
181/***
182 * Assigns a user object as the actively logged in user; also saves the
183 * user information in a browser cookie.
184 */
185</mt:Ignore>
186function mtSetUser(u) {
187    if (u) {
188        // persist this
189        user = u;
190        mtSaveUser();
191        // sync up user greeting
192        mtFireEvent('usersignin');
193    }
194}
195
196<mt:Ignore>
197/***
198 * Simple function that escapes single quote characters for storing
199 * in a cookie.
200 */
201</mt:Ignore>
202function mtEscapeJS(s) {
203    s = s.replace(/'/g, "&apos;");
204    return s;
205}
206
207<mt:Ignore>
208/***
209 * Simple function that unescapes single quote characters that were
210 * stored in a cookie.
211 */
212</mt:Ignore>
213function mtUnescapeJS(s) {
214    s = s.replace(/&apos;/g, "'");
215    return s;
216}
217
218<mt:Ignore>
219/***
220 * Serializes a user object into a string, suitable for storing as a cookie.
221 */
222</mt:Ignore>
223function mtBakeUserCookie(u) {
224    var str = "";
225    if (u.name) str += "name:'" + mtEscapeJS(u.name) + "';";
226    if (u.url) str += "url:'" + mtEscapeJS(u.url) + "';";
227    if (u.email) str += "email:'" + mtEscapeJS(u.email) + "';";
228    if (u.is_authenticated) str += "is_authenticated:'1';";
229    if (u.profile) str += "profile:'" + mtEscapeJS(u.profile) + "';";
230    if (u.userpic) str += "userpic:'" + mtEscapeJS(u.userpic) + "';";
231    if (u.sid) str += "sid:'" + mtEscapeJS(u.sid) + "';";
232    str += "is_trusted:'" + (u.is_trusted ? "1" : "0") + "';";
233    str += "is_author:'" + (u.is_author ? "1" : "0") + "';";
234    str += "is_banned:'" + (u.is_banned ? "1" : "0") + "';";
235    str += "can_post:'" + (u.can_post ? "1" : "0") + "';";
236    str += "can_comment:'" + (u.can_comment ? "1" : "0") + "';";
237    str = str.replace(/;$/, '');
238    return str;
239}
240
241<mt:Ignore>
242/***
243 * Unserializes a user cookie and returns a user object with the restored
244 * state.
245 */
246</mt:Ignore>
247function mtUnbakeUserCookie(s) {
248    if (!s) return;
249
250    var u = {};
251    var m;
252    while (m = s.match(/^((name|url|email|is_authenticated|profile|userpic|sid|is_trusted|is_author|is_banned|can_post|can_comment):'([^']+?)';?)/)) {
253        s = s.substring(m[1].length);
254        if (m[2].match(/^(is|can)_/)) // boolean fields
255            u[m[2]] = m[3] == '1' ? true : false;
256        else
257            u[m[2]] = mtUnescapeJS(m[3]);
258    }
259    if (u.is_authenticated) {
260        u.is_anonymous = false;
261    } else {
262        u.is_anonymous = true;
263        u.can_post = false;
264        u.is_author = false;
265        u.is_banned = false;
266        u.is_trusted = false;
267    }
268    return u;
269}
270
271<mt:Ignore>
272/***
273 * Retrieves an object of the currently logged in user's state.
274 * If no user is logged in or cookied, this will return null.
275 */
276</mt:Ignore>
277function mtGetUser() {
278    if (!user) {
279        var cookie = mtGetCookie(mtCookieName);
280        if (!cookie) return;
281        user = mtUnbakeUserCookie(cookie);
282        if (! user) {
283            user = {};
284            user.is_anonymous = true;
285            user.can_post = false;
286            user.is_author = false;
287            user.is_banned = false;
288            user.is_trusted = false;
289        }
290    }
291    return user;
292}
293
294<mt:Ignore>
295/***
296 * Issues a request to the MT comment script to retrieve the currently
297 * logged-in user (if any).
298 */
299</mt:Ignore>
300var mtFetchedUser = false;
301function mtFetchUser(cb) {
302    if (!cb) cb = 'mtSetUser';
303    if ( ( cb == 'mtSetUser' ) && mtGetUser() ) {
304        var url = document.URL;
305        url = url.replace(/#.+$/, '');
306        url += '#comments-open';
307        location.href = url;
308    } else {
309        // we aren't using AJAX for this, since we may have to request
310        // from a different domain. JSONP to the rescue.
311        mtFetchedUser = true;
312        var script = document.createElement('script');
313        script.src = '<$mt:CGIPath$><$mt:CommentScript$>?__mode=session_js&blog_id=<$mt:BlogID$>&jsonp=' + cb;
314        (document.getElementsByTagName('head'))[0].appendChild(script);
315    }
316}
317
318<mt:Ignore>
319/***
320 * Called when the 'Remember me' checkbox is changed. If the checkbox
321 * is cleared, the cached user cookie is immediately cleared.
322 */
323</mt:Ignore>
324function mtRememberMeOnClick(b) {
325    if (!b.checked)
326        mtClearUser(b.form);
327    return true;
328}
329
330<mt:Ignore>
331/***
332 * Called when comment form is sent.
333 * Required parameter: Form DOM object of comment form.
334 * If form has a 'bakecookie' member, it will be used to signal
335 * storing the anonymous commenter information to a cookie.
336 * If form has a 'armor' member, it will be used to store
337 * a token that is checked by the comment script.
338 */
339</mt:Ignore>
340var mtRequestSubmitted = false;
341function mtCommentOnSubmit(f) {
342    if (!mtRequestSubmitted) {
343        mtRequestSubmitted = true;
344
345        if (f.armor)
346            f.armor.value = '<$mt:BlogSitePath encode_sha1="1"$>';
347        if (f.bakecookie && f.bakecookie.checked)
348            mtSaveUser(f);
349
350        // disable submit buttons
351        if (f.preview_button) f.preview_button.disabled = true;
352        if (f.post) f.post.disabled = true;
353
354        var u = mtGetUser();
355        if ( !is_preview && ( u && u.is_authenticated ) ) {
356            // validate session; then submit
357            mtFetchedUser = false;
358            mtFetchUser('mtCommentSessionVerify');
359            return false;
360        }
361
362        return true;
363    }
364    return false;
365}
366
367function mtCommentSessionVerify(app_user) {
368    var u = mtGetUser();
369    var f = document['comments_form'];
370    if ( u && app_user && app_user.sid && ( u.sid == app_user.sid ) ) {
371        f.submit();
372    } else {
373        alert('<__trans phrase="Your session has expired. Please sign in again to comment." escape="js">');
374        mtClearUser();
375        mtFireEvent('usersignin');
376<mt:IfRegistrationRequired>
377        mtShow('comments-form');
378        mtHide('comments-open-footer');
379</mt:IfRegistrationRequired>
380    }
381}
382
383function mtUserOnLoad() {
384    var u = mtGetUser();
385
386    // if the user is authenticated, hide the 'anonymous' fields
387    // and any captcha input if already shown
388    if ( document.getElementById('comments-form')) {
389        if ( u && u.is_authenticated ) {
390            mtShow('comments-form');
391            mtHide('comments-open-data');
392            if (mtCaptchaVisible)
393                mtHide('comments-open-captcha');
394        } else {
395<mt:IfRegistrationRequired>
396            mtHide('comments-form');
397</mt:IfRegistrationRequired>
398        }
399        if ( u && u.is_banned )
400            mtHide('comments-form');
401
402        // if we're previewing a comment, make sure the captcha
403        // field is visible
404        if (is_preview)
405            mtShowCaptcha();
406        else
407            mtShowGreeting();
408
409        // populate anonymous comment fields if user is cookied as anonymous
410        var cf = document['comments_form'];
411        if (cf) {
412            if (u && u.is_anonymous) {
413                if (u.email) cf.email.value = u.email;
414                if (u.name) cf.author.value = u.name;
415                if (u.url) cf.url.value = u.url;
416                if (cf.bakecookie)
417                    cf.bakecookie.checked = u.name || u.email;
418            } else {
419                if (u && u.sid && cf.sid)
420                    cf.sid.value = u.sid;
421            }
422            if (cf.post.disabled)
423                cf.post.disabled = false;
424            if (cf.preview_button.disabled)
425                cf.preview_button.disabled = false;
426            mtRequestSubmitted = false;
427        }
428    }
429}
430
431<mt:Ignore>
432/***
433 * Called when an entry archive page is loaded.
434 * This routine controls which elements of the comment form are shown
435 * or hidden, depending on commenter type and blog configuration.
436 */
437</mt:Ignore>
438function mtEntryOnLoad() {
439    <mt:Unless tag="IfPingsAccepted">mtHide('trackbacks-info');</mt:Unless>
440    <mt:Unless tag="IfCommentsAccepted">mtHide('comments-open');</mt:Unless>
441    mtFireEvent('usersignin');
442}
443
444mtAttachEvent('usersignin', mtUserOnLoad);
445
446<mt:Ignore>
447/***
448 * Handles the action of the "Sign in" link. First clears any existing
449 * user cookie, then directs to the MT comment script to sign the user in.
450 */
451</mt:Ignore>
452function mtSignIn() {
453    var doc_url = document.URL;
454    doc_url = doc_url.replace(/#.+/, '');
455    var url = '<$mt:SignInLink$>';
456    if (is_preview) {
457        if ( document['comments_form'] ) {
458            var entry_id = document['comments_form'].entry_id.value;
459            url += '&entry_id=' + entry_id;
460        } else {
461            url += '&return_url=<$MTBlogURL encode_url="1"$>';
462        }
463    } else {
464        url += '&return_url=' + encodeURIComponent(doc_url);
465    }
466    mtClearUser();
467    location.href = url;
468}
469
470function mtSignInOnClick(sign_in_element) {
471    var el;
472    if (sign_in_element) {
473        // display throbber
474        el = document.getElementById(sign_in_element);
475        if (!el)  // legacy MT 4.x element id
476            el = document.getElementById('comment-form-external-auth');
477    }
478    if (el)
479        el.innerHTML = '<__trans phrase="Signing in..." escape="js"> <img src="<$mt:StaticWebPath$>images/indicator.white.gif" height="16" width="16" alt="" />';
480
481    mtClearUser(); // clear any 'anonymous' user cookie to allow sign in
482    mtFetchUser('mtSetUserOrLogin');
483    return false;
484}
485
486function mtSetUserOrLogin(u) {
487    if (u && u.is_authenticated) {
488        mtSetUser(u);
489    } else {
490        // user really isn't logged in; so let's do this!
491        mtSignIn();
492    }
493}
494
495<mt:Ignore>
496/***
497 * Handles sign out from the web site.
498 * First clears any existing user cookie, then direts to the MT comment
499 * script to sign the user out.
500 */
501</mt:Ignore>
502function mtSignOut(entry_id) {
503    mtClearUser();
504    var doc_url = document.URL;
505    doc_url = doc_url.replace(/#.+/, '');
506    var url = '<$mt:SignOutLink$>';
507    if (is_preview) {
508        if ( document['comments_form'] ) {
509            var entry_id = document['comments_form'].entry_id.value;
510            url += '&entry_id=' + entry_id;
511        } else {
512            url += '&return_url=<$MTBlogURL encode_url="1"$>';
513        }
514    } else {
515        url += '&return_url=' + encodeURIComponent(doc_url);
516    }
517    location.href = url;
518}
519
520<mt:Ignore>
521/***
522 * Handles the action of the "Sign out" link.
523 */
524</mt:Ignore>
525function mtSignOutOnClick() {
526    mtSignOut();
527    return false;
528}
529
530<mt:Ignore>
531/***
532 * Handles the display of the greeting message, depending on what kind of
533 * user is logged in and blog comment policy.
534 */
535</mt:Ignore>
536function mtShowGreeting() {
537<mt:IfRegistrationAllowed>
538    var reg_reqd = <mt:IfRegistrationRequired>true<mt:Else>false</mt:IfRegistrationRequired>;
539
540    var cf = document['comments_form'];
541    if (!cf) return;
542
543    var el = document.getElementById('comment-greeting');
544    if (!el)  // legacy MT 4.x element id
545        el = document.getElementById('comment-form-external-auth');
546    if (!el) return;
547
548    var eid = cf.entry_id;
549    var entry_id;
550    if (eid) entry_id = eid.value;
551
552    var phrase;
553    var u = mtGetUser();
554
555    if ( u && u.is_authenticated ) {
556        if ( u.is_banned ) {
557            phrase = '<__trans phrase="You do not have permission to comment on this blog. ([_1]sign out[_2])" params="<a href="javascript:void(0);" onclick="return mtSignOutOnClick();">%%</a>" escape="js">';
558        } else {
559            var user_link;
560            if ( u.is_author ) {
561                user_link = '<a href="<$mt:CGIPath$><$mt:CommentScript$>?__mode=edit_profile&blog_id=<$mt:BlogID$>';
562                if (entry_id)
563                    user_link += '&entry_id=' + entry_id;
564                user_link += '">' + u.name + '</a>';
565            } else {
566                // registered user, but not a user with posting rights
567                if (u.url)
568                    user_link = '<a href="' + u.url + '">' + u.name + '</a>';
569                else
570                    user_link = u.name;
571            }
572            // TBD: supplement phrase with userpic if one is available.
573            phrase = '<__trans phrase="Thanks for signing in, __NAME__. ([_1]sign out[_2])" params="<a href="javascript:void(0)" onclick="return mtSignOutOnClick();">%%</a>" escape="js">';
574            phrase = phrase.replace(/__NAME__/, user_link);
575        }
576    } else {
577        if (reg_reqd) {
578            phrase = '<__trans phrase="[_1]Sign in[_2] to comment." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick('comment-greeting')">%%</a>" escape="js">';
579        } else {
580            phrase = '<__trans phrase="[_1]Sign in[_2] to comment, or comment anonymously." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick('comment-greeting')">%%</a>" escape="js">';
581        }
582    }
583    el.innerHTML = phrase;
584<mt:Else>
585    mtShowCaptcha();
586</mt:IfRegistrationAllowed>
587}
588
589<mt:Ignore>
590/***
591 * Handles the action of the 'Reply' links.
592 */
593</mt:Ignore>
594function mtReplyCommentOnClick(parent_id, author) {
595    mtShow('comment-form-reply');
596
597    var checkbox = document.getElementById('comment-reply');
598    var label = document.getElementById('comment-reply-label');
599    var text = document.getElementById('comment-text');
600
601    // Populate label with new values
602    var reply_text = '<__trans phrase="Replying to <a href="[_1]" onclick="[_2]">comment from [_3]</a>" params="#comment-__PARENT__%%location.href=this.href; return false%%__AUTHOR__" escape="js">';
603    reply_text = reply_text.replace(/__PARENT__/, parent_id);
604    reply_text = reply_text.replace(/__AUTHOR__/, author);
605    label.innerHTML = reply_text;
606
607    checkbox.value = parent_id;
608    checkbox.checked = true;
609    try {
610        // text field may be hidden
611        text.focus();
612    } catch(e) {
613    }
614
615    mtSetCommentParentID();
616}
617
618<mt:Ignore>
619/***
620 * Sets the parent comment ID when replying to a comment.
621 */
622</mt:Ignore>
623function mtSetCommentParentID() {
624    var checkbox = document.getElementById('comment-reply');
625    var parent_id_field = document.getElementById('comment-parent-id');
626    if (!checkbox || !parent_id_field) return;
627
628    var pid = 0;
629    if (checkbox.checked == true)
630        pid = checkbox.value;
631    parent_id_field.value = pid;
632}
633
634<mt:Ignore>
635/***
636 * Persists a copy of the current user cookie into the browser cookie stash.
637 */
638</mt:Ignore>
639function mtSaveUser(f) {
640    // We can't reliably store the user cookie during a preview.
641    if (is_preview) return;
642
643    var u = mtGetUser();
644
645    if (f && (!u || u.is_anonymous)) {
646        if ( !u ) {
647            u = {};
648            u.is_authenticated = false;
649            u.can_comment = true;
650            u.is_author = false;
651            u.is_banned = false;
652            u.is_anonymous = true;
653            u.is_trusted = false;
654        }
655        if (f.author != undefined) u.name = f.author.value;
656        if (f.email != undefined) u.email = f.email.value;
657        if (f.url != undefined) u.url = f.url.value;
658    }
659
660    if (!u) return;
661
662    var cache_period = mtCookieTimeout * 1000;
663
664    // cache anonymous user info for a long period if the
665    // user has requested to be remembered
666    if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
667        cache_period = 365 * 24 * 60 * 60 * 1000;
668
669    var now = new Date();
670    mtFixDate(now);
671    now.setTime(now.getTime() + cache_period);
672
673    var cmtcookie = mtBakeUserCookie(u);
674    mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
675        location.protocol == 'https:');
676}
677
678<mt:Ignore>
679/***
680 * Clears the blog-side user cookie.
681 */
682</mt:Ignore>
683function mtClearUser() {
684    user = null;
685    mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain,
686        location.protocol == 'https:');
687}
688
689<mt:Ignore>
690/***
691 * Sets a browser cookie.
692 */
693</mt:Ignore>
694function mtSetCookie(name, value, expires, path, domain, secure) {
695    if (domain && domain.match(/^\.?localhost$/))
696        domain = null;
697    var curCookie = name + "=" + escape(value) +
698        (expires ? "; expires=" + expires.toGMTString() : "") +
699        (path ? "; path=" + path : "") +
700        (domain ? "; domain=" + domain : "") +
701        (secure ? "; secure" : "");
702    document.cookie = curCookie;
703}
704
705<mt:Ignore>
706/***
707 * Retrieves a browser cookie.
708 */
709</mt:Ignore>
710function mtGetCookie(name) {
711    var prefix = name + '=';
712    var c = document.cookie;
713    var cookieStartIndex = c.indexOf(prefix);
714    if (cookieStartIndex == -1)
715        return '';
716    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
717    if (cookieEndIndex == -1)
718        cookieEndIndex = c.length;
719    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
720}
721
722<mt:Ignore>
723/***
724 * Deletes a browser cookie.
725 */
726</mt:Ignore>
727function mtDeleteCookie(name, path, domain, secure) {
728    if (mtGetCookie(name)) {
729        if (domain && domain.match(/^\.?localhost$/))
730            domain = null;
731        document.cookie = name + "=" +
732            (path ? "; path=" + path : "") +
733            (domain ? "; domain=" + domain : "") +
734            (secure ? "; secure" : "") +
735            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
736    }
737}
738
739function mtFixDate(date) {
740    var skew = (new Date(0)).getTime();
741    if (skew > 0)
742        date.setTime(date.getTime() - skew);
743}
744
745<mt:Ignore>
746/***
747 * Returns a XMLHttpRequest object (for Ajax operations).
748 */
749</mt:Ignore>
750function mtGetXmlHttp() {
751    if ( !window.XMLHttpRequest ) {
752        window.XMLHttpRequest = function() {
753            var types = [
754                "Microsoft.XMLHTTP",
755                "MSXML2.XMLHTTP.5.0",
756                "MSXML2.XMLHTTP.4.0",
757                "MSXML2.XMLHTTP.3.0",
758                "MSXML2.XMLHTTP"
759            ];
760
761            for ( var i = 0; i < types.length; i++ ) {
762                try {
763                    return new ActiveXObject( types[ i ] );
764                } catch( e ) {}
765            }
766
767            return undefined;
768        };
769    }
770    if ( window.XMLHttpRequest )
771        return new XMLHttpRequest();
772}
773
774// BEGIN: fast browser onload init
775// Modifications by David Davis, DWD
776// Dean Edwards/Matthias Miller/John Resig
777// http://dean.edwards.name/weblog/2006/06/again/?full#comment5338
778
779function mtInit() {
780    // quit if this function has already been called
781    if (arguments.callee.done) return;
782
783    // flag this function so we don't do the same thing twice
784    arguments.callee.done = true;
785
786    // kill the timer
787    // DWD - check against window
788    if ( window._timer ) clearInterval(window._timer);
789
790    // DWD - fire the window onload now, and replace it
791    if ( window.onload && ( window.onload !== window.mtInit ) ) {
792        window.onload();
793        window.onload = function() {};
794    }
795}
796
797/* for Mozilla/Opera9 */
798if (document.addEventListener) {
799    document.addEventListener("DOMContentLoaded", mtInit, false);
800}
801
802/* for Internet Explorer */
803/*@cc_on @*/
804/*@if (@_win32)
805document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
806var script = document.getElementById("__ie_onload");
807script.onreadystatechange = function() {
808    if (this.readyState == "complete") {
809        mtInit(); // call the onload handler
810    }
811};
812/*@end @*/
813
814/* for Safari */
815if (/WebKit/i.test(navigator.userAgent)) { // sniff
816    _timer = setInterval(function() {
817        if (/loaded|complete/.test(document.readyState)) {
818            mtInit(); // call the onload handler
819        }
820    }, 10);
821}
822
823/* for other browsers */
824window.onload = mtInit;
825
826// END: fast browser onload init
827
828<mt:IfRegistrationAllowed>
829/***
830 * If request contains a '#_login' or '#_logout' hash, use this to
831 * also delete the blog-side user cookie, since we're coming back from
832 * a login, logout or edit profile operation.
833 */
834var clearCookie = ( window.location.hash && window.location.hash.match( /^#_log(in|out)/ ) ) ? true : false;
835if (clearCookie) {
836    // clear any logged in state
837    mtClearUser();
838    if (RegExp.$1 == 'in')
839        mtFetchUser();
840} else {
841    <mt:Ignore>
842    /***
843     * Uncondition this call to fetch the current user state (if available)
844     * from MT upon page load if no user cookie is already present.
845     * This is okay if you have a private install, such as an Intranet;
846     * not recommended for public web sites!
847     */
848    </mt:Ignore>
849    if ( is_preview && !user )
850        mtFetchUser();
851}
852</mt:IfRegistrationAllowed>
Note: See TracBrowser for help on using the browser.