root/trunk/Vanilla/templates/vanilla+authentication/javascript.mtml @ 910

Revision 910, 14.4 kB (checked in by bsmith, 16 months ago)

adding userpics

Line 
1<$mt:var name="user-auth" value="signin-content"$>
2<mt:Ignore>
3    Set show_userpic to 1 to enable userpics in the signin line
4    - Requires Community Solution
5    - Note: This will update username to link to Community Solution "edit profile" script. This script assumes that you are using profiles and thus links to "view profile" screen. If the template set is not using profiles then the Community Solution "Profile Edit" Global System Template should be updated.
6</mt:Ignore>
7<$mt:var name="show_userpic" value="1"$>
8
9/* section *****************************************************************/
10
11// The cookie name to use for storing the blog-side comment session cookie.
12var mtCookieName = "<$mt:UserSessionCookieName$>";
13var mtCookieDomain = "<$mt:UserSessionCookieDomain$>";
14var mtCookiePath = "<$mt:UserSessionCookiePath$>";
15var mtCookieTimeout = <$mt:UserSessionCookieTimeout$>;
16var is_preview;
17var user;
18
19<mt:Ignore>
20/***
21 * Issues a request to the MT comment script to retrieve the currently
22 * logged-in user (if any).
23 */
24</mt:Ignore>
25var mtFetchedUser = false;
26<mt:IfBlog>
27function mtFetchUser(cb) {
28    if (!cb) cb = 'mtSetUser';
29    if ( ( cb == 'mtSetUser' ) && mtGetUser() ) {
30        var url = document.URL;
31        url = url.replace(/#.+$/, '');
32        url += '#comments-open';
33        location.href = url;
34    } else {
35        // we aren't using AJAX for this, since we may have to request
36        // from a different domain. JSONP to the rescue.
37        mtFetchedUser = true;
38        var script = document.createElement('script');
39        var ts = new Date().getTime();
40        script.src = '<$mt:CGIPath$><$mt:CommentScript$>?__mode=session_js&blog_id=<$mt:BlogID$>&jsonp=' + cb + '&ts=' + ts;
41        (document.getElementsByTagName('head'))[0].appendChild(script);
42    }
43}
44</mt:IfBlog>
45
46<mt:Ignore>
47/***
48 * A routine that displays various phrases based upon users authenticated status
49 */
50</mt:Ignore>
51
52// <a href="#">Signin</a>
53// You are signed in as <a href="#">UserName</a> (<a href="#">Sign out</a>)
54// You do not have permission to sign in to this blog
55
56function mtUpdateSignInWidget(u) {
57    var el = document.getElementById('<$mt:var name="user-auth"$>');
58    var content = '';
59    var doit = 1;
60    if (!el) return;
61    if (u) {
62        if (u && u.is_authenticated) {
63            user = u;
64            mtSaveUser();
65        } else {
66            // user really isn't logged in; so let's do this!
67            return mtSignIn();
68        }
69    } else {
70        u = mtGetUser();
71    }
72    if (u && u.name) {
73<mt:If name="show_userpic">
74        if (u.userpic)
75            content += '<img src="' + u.userpic + '" width="50" height="50" />';
76</mt:If>
77        var url;
78        if (u.is_authenticated) {
79            if (u.is_author) {
80<mt:If name="show_userpic">
81                url = '<$mt:CGIPath$><$mt:CommunityScript$>?__mode=edit&blog_id=<$mt:BlogID$>';
82                url += '&return_to=' + encodeURIComponent(document.URL);
83<mt:Else>
84                url = '<$mt:CGIPath$><$mt:CommentScript$>?__mode=edit_profile&blog_id=<$mt:BlogID$>';
85                url += '&static=' + encodeURIComponent( location.href );
86</mt:If>
87            } else {
88                url = u.url;
89            }
90        } else if (u.url) {
91            url = u.url;
92        } else {
93            url = null;
94        }
95        content += '<__trans phrase="You are signed in as " escape="js">';
96        if (url)
97            content += '<a href="' + url + '">' + u.name + '</a>';
98        else
99            content += u.name;
100        content += '.  (<a href="javascript:void(0)" onclick="return mtSignOutOnClick()"><__trans phrase="sign out" escape="js"></a>)';
101    } else if (u && u.is_banned) {
102        content = '<__trans phrase="You do not have permission to sign in to this blog." escape="js">';
103    } else {
104    //    content = '<a href="javascript:void(0)" onclick="return mtSignInOnClick(\'<$mt:var name="user-auth"$>\')"><__trans phrase="Sign In" escape="js"></a>';
105        doit = 0;
106    }
107    if (doit) {
108        el.innerHTML = content;
109    }
110}
111
112<mt:Ignore>
113/***
114 * Retrieves an object of the currently logged in user's state.
115 * If no user is logged in or cookied, this will return null.
116 */
117</mt:Ignore>
118function mtGetUser() {
119    if (!user) {
120        var cookie = mtGetCookie(mtCookieName);
121        if (!cookie) return;
122        user = mtUnbakeUserCookie(cookie);
123        if (! user) {
124            user = {};
125            user.is_anonymous = true;
126            user.can_post = false;
127            user.is_author = false;
128            user.is_banned = false;
129            user.is_trusted = false;
130        }
131    }
132    return user;
133}
134
135<mt:Ignore>
136/***
137 * Assigns a user object as the actively logged in user; also saves the
138 * user information in a browser cookie.
139 */
140</mt:Ignore>
141function mtSetUser(u) {
142    if (u) {
143        // persist this
144        user = u;
145        mtSaveUser();
146        // sync up user greeting
147        mtFireEvent('usersignin');
148    }
149}
150
151<mt:Ignore>
152/***
153 * Persists a copy of the current user cookie into the browser cookie stash.
154 */
155</mt:Ignore>
156function mtSaveUser(f) {
157    // We can't reliably store the user cookie during a preview.
158    if (is_preview) return;
159
160    var u = mtGetUser();
161
162    if (f && (!u || u.is_anonymous)) {
163        if ( !u ) {
164            u = {};
165            u.is_authenticated = false;
166            u.can_comment = true;
167            u.is_author = false;
168            u.is_banned = false;
169            u.is_anonymous = true;
170            u.is_trusted = false;
171        }
172        if (f.author != undefined) u.name = f.author.value;
173        if (f.email != undefined) u.email = f.email.value;
174        if (f.url != undefined) u.url = f.url.value;
175    }
176
177    if (!u) return;
178
179    var cache_period = mtCookieTimeout * 1000;
180
181    // cache anonymous user info for a long period if the
182    // user has requested to be remembered
183    if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
184        cache_period = 365 * 24 * 60 * 60 * 1000;
185
186    var now = new Date();
187    mtFixDate(now);
188    now.setTime(now.getTime() + cache_period);
189
190    var cmtcookie = mtBakeUserCookie(u);
191    mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
192        location.protocol == 'https:');
193}
194
195<mt:Ignore>
196/***
197 * Handles the action of the "Sign in" link. First clears any existing
198 * user cookie, then directs to the MT comment script to sign the user in.
199 */
200</mt:Ignore>
201function mtSignIn() {
202    var doc_url = document.URL;
203    doc_url = doc_url.replace(/#.+/, '');
204    var url = '<$mt:SignInLink$>';
205    if (is_preview) {
206        if ( document['comments_form'] ) {
207            var entry_id = document['comments_form'].entry_id.value;
208            url += '&entry_id=' + entry_id;
209        } else {
210            url += '&return_url=<$mt:BlogURL encode_url="1"$>';
211        }
212    } else {
213        url += '&return_url=' + encodeURIComponent(doc_url);
214    }
215    mtClearUser();
216    location.href = url;
217}
218
219function mtSignInOnClick(sign_in_element) {
220    var el;
221    if (sign_in_element) {
222        // display throbber
223        el = document.getElementById(sign_in_element);
224        if (!el)  // legacy MT 4.x element id
225            el = document.getElementById('comment-form-external-auth');
226    }
227    if (el)
228        el.innerHTML = '<__trans phrase="Signing in..." escape="js"> <span class="status-indicator">&nbsp;</span>';
229
230    mtClearUser(); // clear any 'anonymous' user cookie to allow sign in
231    mtFetchUser('mtSetUserOrLogin');
232    return false;
233}
234
235function mtSetUserOrLogin(u) {
236    if (u && u.is_authenticated) {
237        mtSetUser(u);
238    } else {
239        // user really isn't logged in; so let's do this!
240        mtSignIn();
241    }
242}
243
244<mt:Ignore>
245/***
246 * Handles sign out from the web site.
247 * First clears any existing user cookie, then direts to the MT comment
248 * script to sign the user out.
249 */
250</mt:Ignore>
251function mtSignOut(entry_id) {
252    mtClearUser();
253    var doc_url = document.URL;
254    doc_url = doc_url.replace(/#.+/, '');
255    var url = '<$mt:SignOutLink$>';
256    if (is_preview) {
257        if ( document['comments_form'] ) {
258            var entry_id = document['comments_form'].entry_id.value;
259            url += '&entry_id=' + entry_id;
260        } else {
261            url += '&return_url=<$mt:BlogURL encode_url="1"$>';
262        }
263    } else {
264        url += '&return_url=' + encodeURIComponent(doc_url);
265    }
266    location.href = url;
267}
268
269<mt:Ignore>
270/***
271 * Handles the action of the "Sign out" link.
272 */
273</mt:Ignore>
274function mtSignOutOnClick() {
275    mtSignOut();
276    return false;
277}
278
279
280
281/* Cookie *****************************************************************/
282
283<mt:Ignore>
284/***
285 * Persists a copy of the current user cookie into the browser cookie stash.
286 */
287</mt:Ignore>
288function mtSaveUser(f) {
289    // We can't reliably store the user cookie during a preview.
290    if (is_preview) return;
291
292    var u = mtGetUser();
293
294    if (f && (!u || u.is_anonymous)) {
295        if ( !u ) {
296            u = {};
297            u.is_authenticated = false;
298            u.can_comment = true;
299            u.is_author = false;
300            u.is_banned = false;
301            u.is_anonymous = true;
302            u.is_trusted = false;
303        }
304        if (f.author != undefined) u.name = f.author.value;
305        if (f.email != undefined) u.email = f.email.value;
306        if (f.url != undefined) u.url = f.url.value;
307    }
308
309    if (!u) return;
310
311    var cache_period = mtCookieTimeout * 1000;
312
313    // cache anonymous user info for a long period if the
314    // user has requested to be remembered
315    if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked)
316        cache_period = 365 * 24 * 60 * 60 * 1000;
317
318    var now = new Date();
319    mtFixDate(now);
320    now.setTime(now.getTime() + cache_period);
321
322    var cmtcookie = mtBakeUserCookie(u);
323    mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain,
324        location.protocol == 'https:');
325}
326
327<mt:Ignore>
328/***
329 * Clears the blog-side user cookie.
330 */
331</mt:Ignore>
332function mtClearUser() {
333    user = null;
334    mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain,
335        location.protocol == 'https:');
336}
337
338<mt:Ignore>
339/***
340 * Sets a browser cookie.
341 */
342</mt:Ignore>
343function mtSetCookie(name, value, expires, path, domain, secure) {
344    if (domain && domain.match(/^\.?localhost$/))
345        domain = null;
346    var curCookie = name + "=" + escape(value) +
347        (expires ? "; expires=" + expires.toGMTString() : "") +
348        (path ? "; path=" + path : "") +
349        (domain ? "; domain=" + domain : "") +
350        (secure ? "; secure" : "");
351    document.cookie = curCookie;
352}
353
354<mt:Ignore>
355/***
356 * Retrieves a browser cookie.
357 */
358</mt:Ignore>
359function mtGetCookie(name) {
360    var prefix = name + '=';
361    var c = document.cookie;
362    var cookieStartIndex = c.indexOf(prefix);
363    if (cookieStartIndex == -1)
364        return '';
365    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
366    if (cookieEndIndex == -1)
367        cookieEndIndex = c.length;
368    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
369}
370
371<mt:Ignore>
372/***
373 * Deletes a browser cookie.
374 */
375</mt:Ignore>
376function mtDeleteCookie(name, path, domain, secure) {
377    if (mtGetCookie(name)) {
378        if (domain && domain.match(/^\.?localhost$/))
379            domain = null;
380        document.cookie = name + "=" +
381            (path ? "; path=" + path : "") +
382            (domain ? "; domain=" + domain : "") +
383            (secure ? "; secure" : "") +
384            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
385    }
386}
387
388<mt:Ignore>
389/***
390 * Serializes a user object into a string, suitable for storing as a cookie.
391 */
392</mt:Ignore>
393function mtBakeUserCookie(u) {
394    var str = "";
395    if (u.name) str += "name:'" + mtEscapeJS(u.name) + "';";
396    if (u.url) str += "url:'" + mtEscapeJS(u.url) + "';";
397    if (u.email) str += "email:'" + mtEscapeJS(u.email) + "';";
398    if (u.auth_type) str += "auth_type:'" + u.auth_type + "';";
399    if (u.is_authenticated) str += "is_authenticated:'1';";
400    if (u.profile) str += "profile:'" + mtEscapeJS(u.profile) + "';";
401    if (u.userpic) str += "userpic:'" + mtEscapeJS(u.userpic) + "';";
402    if (u.sid) str += "sid:'" + mtEscapeJS(u.sid) + "';";
403    str += "is_trusted:'" + (u.is_trusted ? "1" : "0") + "';";
404    str += "is_author:'" + (u.is_author ? "1" : "0") + "';";
405    str += "is_banned:'" + (u.is_banned ? "1" : "0") + "';";
406    str += "can_post:'" + (u.can_post ? "1" : "0") + "';";
407    str += "can_comment:'" + (u.can_comment ? "1" : "0") + "';";
408    str = str.replace(/;$/, '');
409    return str;
410}
411
412<mt:Ignore>
413/***
414 * Unserializes a user cookie and returns a user object with the restored
415 * state.
416 */
417</mt:Ignore>
418function mtUnbakeUserCookie(s) {
419    if (!s) return;
420
421    var u = {};
422    var m;
423    while (m = s.match(/^((name|url|email|auth_type|is_authenticated|profile|userpic|sid|is_trusted|is_author|is_banned|can_post|can_comment):'([^']+?)';?)/)) {
424        s = s.substring(m[1].length);
425        if (m[2].match(/^(is|can)_/)) // boolean fields
426            u[m[2]] = m[3] == '1' ? true : false;
427        else
428            u[m[2]] = mtUnescapeJS(m[3]);
429    }
430    if (u.is_authenticated) {
431        u.is_anonymous = false;
432    } else {
433        u.is_anonymous = true;
434        u.can_post = false;
435        u.is_author = false;
436        u.is_banned = false;
437        u.is_trusted = false;
438    }
439    return u;
440}
441
442/* Utility Functions *****************************************************************/
443
444<mt:Ignore>
445/***
446 * Calls the event named, if there are handlers for it.
447 */
448</mt:Ignore>
449function mtFireEvent(eventName,param) {
450    var fn = window['on' + eventName];
451    if (typeof fn == 'function') return fn(param);
452    return;
453}
454
455function mtFixDate(date) {
456    var skew = (new Date(0)).getTime();
457    if (skew > 0)
458        date.setTime(date.getTime() - skew);
459}
460
461<mt:Ignore>
462/***
463 * Simple function that escapes single quote characters for storing
464 * in a cookie.
465 */
466</mt:Ignore>
467function mtEscapeJS(s) {
468    s = s.replace(/'/g, "&apos;");
469    return s;
470}
471
472<mt:Ignore>
473/***
474 * Simple function that unescapes single quote characters that were
475 * stored in a cookie.
476 */
477</mt:Ignore>
478function mtUnescapeJS(s) {
479    s = s.replace(/&apos;/g, "'");
480    return s;
481}
482
483<mt:Ignore>
484/***
485 * A utility function for assigning/adding handlers to window events.
486 */
487</mt:Ignore>
488function mtAttachEvent(eventName,func) {
489    var onEventName = 'on' + eventName;
490    var old = window[onEventName];
491    if( typeof old != 'function' )
492        window[onEventName] = func;
493    else {
494        window[onEventName] = function( evt ) {
495            old( evt );
496            return func( evt );
497        };
498    }
499}
500
501/* section *****************************************************************/
Note: See TracBrowser for help on using the browser.