| 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. */ |
|---|
| 7 | function hideDocumentElement(id) { return mtHide(id) } |
|---|
| 8 | function showDocumentElement(id) { return mtShow(id) } |
|---|
| 9 | function individualArchivesOnLoad() { return mtEntryOnLoad() } |
|---|
| 10 | function writeCommenterGreeting() { return mtShowGreeting() } |
|---|
| 11 | function rememberMe(f) { return mtRememberMe(f) } |
|---|
| 12 | function forgetMe(f) { return mtForgetMe(f) } |
|---|
| 13 | </mt:Ignore> |
|---|
| 14 | |
|---|
| 15 | // The cookie name to use for storing the blog-side comment session cookie. |
|---|
| 16 | var mtCookieName = "<$mt:UserSessionCookieName$>"; |
|---|
| 17 | var mtCookieDomain = "<$mt:UserSessionCookieDomain$>"; |
|---|
| 18 | var mtCookiePath = "<$mt:UserSessionCookiePath$>"; |
|---|
| 19 | var 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> |
|---|
| 27 | function 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> |
|---|
| 38 | function 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> |
|---|
| 48 | function 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> |
|---|
| 66 | function 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> |
|---|
| 79 | function 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">'; |
|---|
| 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">'; |
|---|
| 93 | else if (hours > 1) |
|---|
| 94 | str = '<__trans phrase="[quant,_1,hour,hours] ago" params="2">'.replace(/2/, hours); |
|---|
| 95 | else if (min == 1) |
|---|
| 96 | str = '<__trans phrase="[quant,_1,minute,minutes] ago" params="1">'; |
|---|
| 97 | else |
|---|
| 98 | str = '<__trans phrase="[quant,_1,minute,minutes] ago" params="2">'.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">'; |
|---|
| 105 | else if (days > 1) |
|---|
| 106 | str = '<__trans phrase="[quant,_1,day,days] ago" params="2">'.replace(/2/, days); |
|---|
| 107 | else if (hours == 1) |
|---|
| 108 | str = '<__trans phrase="[quant,_1,hour,hours] ago" params="1">'; |
|---|
| 109 | else |
|---|
| 110 | str = '<__trans phrase="[quant,_1,hour,hours] ago" params="2">'.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> |
|---|
| 120 | function 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 = '<__trans phrase='<a href="[_1]">Edit</a>' params="<$mt:AdminScript$>?__mode=view&_type=entry&id=' + entry_id + '">'; |
|---|
| 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> |
|---|
| 135 | function 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> |
|---|
| 146 | var mtCaptchaVisible = false; |
|---|
| 147 | function 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 | |
|---|
| 178 | var is_preview; |
|---|
| 179 | var 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> |
|---|
| 186 | function 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> |
|---|
| 202 | function mtEscapeJS(s) { |
|---|
| 203 | s = s.replace(/'/g, "'"); |
|---|
| 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> |
|---|
| 213 | function mtUnescapeJS(s) { |
|---|
| 214 | s = s.replace(/'/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> |
|---|
| 223 | function 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> |
|---|
| 247 | function 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> |
|---|
| 277 | function 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> |
|---|
| 300 | var mtFetchedUser = false; |
|---|
| 301 | function 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> |
|---|
| 324 | function 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> |
|---|
| 340 | var mtRequestSubmitted = false; |
|---|
| 341 | function 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 | |
|---|
| 367 | function 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 | |
|---|
| 383 | function 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> |
|---|
| 438 | function 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 | |
|---|
| 444 | mtAttachEvent('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> |
|---|
| 452 | function mtSignIn() { |
|---|
| 453 | var doc_url = document.URL; |
|---|
| 454 | doc_url = doc_url.replace(/#.+/, ''); |
|---|
| 455 | var url = '<$mt:SignInLink$>'; |
|---|
| 456 | url += '&return_url=' + encodeURIComponent(doc_url); |
|---|
| 457 | mtClearUser(); |
|---|
| 458 | location.href = url; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | function mtSignInOnClick(sign_in_element) { |
|---|
| 462 | var el; |
|---|
| 463 | if (sign_in_element) { |
|---|
| 464 | // display throbber |
|---|
| 465 | el = document.getElementById(sign_in_element); |
|---|
| 466 | if (!el) // legacy MT 4.x element id |
|---|
| 467 | el = document.getElementById('comment-form-external-auth'); |
|---|
| 468 | } |
|---|
| 469 | if (el) |
|---|
| 470 | el.innerHTML = '<__trans phrase="Signing in..."> <img src="<$mt:StaticWebPath$>images/indicator.white.gif" height="16" width="16" alt="" />'; |
|---|
| 471 | |
|---|
| 472 | mtClearUser(); // clear any 'anonymous' user cookie to allow sign in |
|---|
| 473 | mtFetchUser('mtSetUserOrLogin'); |
|---|
| 474 | return false; |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | function mtSetUserOrLogin(u) { |
|---|
| 478 | if (u && u.is_authenticated) { |
|---|
| 479 | mtSetUser(u); |
|---|
| 480 | } else { |
|---|
| 481 | // user really isn't logged in; so let's do this! |
|---|
| 482 | mtSignIn(); |
|---|
| 483 | } |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | <mt:Ignore> |
|---|
| 487 | /*** |
|---|
| 488 | * Handles sign out from the web site. |
|---|
| 489 | * First clears any existing user cookie, then direts to the MT comment |
|---|
| 490 | * script to sign the user out. |
|---|
| 491 | */ |
|---|
| 492 | </mt:Ignore> |
|---|
| 493 | function mtSignOut(entry_id) { |
|---|
| 494 | mtClearUser(); |
|---|
| 495 | var url = '<$mt:SignOutLink$>&return_url=' + encodeURIComponent(location.href); |
|---|
| 496 | location.href = url; |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | <mt:Ignore> |
|---|
| 500 | /*** |
|---|
| 501 | * Handles the action of the "Sign out" link. |
|---|
| 502 | */ |
|---|
| 503 | </mt:Ignore> |
|---|
| 504 | function mtSignOutOnClick() { |
|---|
| 505 | mtSignOut(); |
|---|
| 506 | return false; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | <mt:Ignore> |
|---|
| 510 | /*** |
|---|
| 511 | * Handles the display of the greeting message, depending on what kind of |
|---|
| 512 | * user is logged in and blog comment policy. |
|---|
| 513 | */ |
|---|
| 514 | </mt:Ignore> |
|---|
| 515 | function mtShowGreeting() { |
|---|
| 516 | <mt:IfRegistrationAllowed> |
|---|
| 517 | var reg_reqd = <mt:IfRegistrationRequired>true<mt:Else>false</mt:IfRegistrationRequired>; |
|---|
| 518 | |
|---|
| 519 | var cf = document['comments_form']; |
|---|
| 520 | if (!cf) return; |
|---|
| 521 | |
|---|
| 522 | var el = document.getElementById('comment-greeting'); |
|---|
| 523 | if (!el) // legacy MT 4.x element id |
|---|
| 524 | el = document.getElementById('comment-form-external-auth'); |
|---|
| 525 | if (!el) return; |
|---|
| 526 | |
|---|
| 527 | var eid = cf.entry_id; |
|---|
| 528 | var entry_id; |
|---|
| 529 | if (eid) entry_id = eid.value; |
|---|
| 530 | |
|---|
| 531 | var phrase; |
|---|
| 532 | var u = mtGetUser(); |
|---|
| 533 | |
|---|
| 534 | if ( u && u.is_authenticated ) { |
|---|
| 535 | if ( u.is_banned ) { |
|---|
| 536 | 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>">'; |
|---|
| 537 | } else { |
|---|
| 538 | var user_link; |
|---|
| 539 | if ( u.is_author ) { |
|---|
| 540 | user_link = '<a href="<$mt:CGIPath$><$mt:CommentScript$>?__mode=edit_profile&blog_id=<$mt:BlogID$>'; |
|---|
| 541 | if (entry_id) |
|---|
| 542 | user_link += '&entry_id=' + entry_id; |
|---|
| 543 | user_link += '">' + u.name + '</a>'; |
|---|
| 544 | } else { |
|---|
| 545 | // registered user, but not a user with posting rights |
|---|
| 546 | if (u.url) |
|---|
| 547 | user_link = '<a href="' + u.url + '">' + u.name + '</a>'; |
|---|
| 548 | else |
|---|
| 549 | user_link = u.name; |
|---|
| 550 | } |
|---|
| 551 | // TBD: supplement phrase with userpic if one is available. |
|---|
| 552 | phrase = '<__trans phrase="Thanks for signing in, [_1]. ([_2]sign out[_3])" params="' + user_link + '%%<a href="javascript:void(0)" onclick="return mtSignOutOnClick();">%%</a>">'; |
|---|
| 553 | } |
|---|
| 554 | } else { |
|---|
| 555 | if (reg_reqd) { |
|---|
| 556 | phrase = '<__trans phrase="[_1]Sign in[_2] to comment." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick(\'comment-greeting\')">%%</a>">'; |
|---|
| 557 | } else { |
|---|
| 558 | phrase = '<__trans phrase="[_1]Sign in[_2] to comment, or comment anonymously." params="<a href="javascript:void(0)" onclick="return mtSignInOnClick(\'comment-greeting\')">%%</a>">'; |
|---|
| 559 | } |
|---|
| 560 | } |
|---|
| 561 | el.innerHTML = phrase; |
|---|
| 562 | <mt:Else> |
|---|
| 563 | mtShowCaptcha(); |
|---|
| 564 | </mt:IfRegistrationAllowed> |
|---|
| 565 | } |
|---|
| 566 | |
|---|
| 567 | <mt:Ignore> |
|---|
| 568 | /*** |
|---|
| 569 | * Handles the action of the 'Reply' links. |
|---|
| 570 | */ |
|---|
| 571 | </mt:Ignore> |
|---|
| 572 | function mtReplyCommentOnClick(parent_id, author) { |
|---|
| 573 | mtShow('comment-form-reply'); |
|---|
| 574 | |
|---|
| 575 | var checkbox = document.getElementById('comment-reply'); |
|---|
| 576 | var label = document.getElementById('comment-reply-label'); |
|---|
| 577 | var text = document.getElementById('comment-text'); |
|---|
| 578 | |
|---|
| 579 | // Populate label with new values |
|---|
| 580 | var reply_text = '<__trans phrase="Replying to <a href="[_1]" onclick="[_2]">comment from [_3]</a>" params="#comment-'+ parent_id +'%%location.href=this.href; return false%%'+ author +'">'; |
|---|
| 581 | label.innerHTML = reply_text; |
|---|
| 582 | |
|---|
| 583 | checkbox.value = parent_id; |
|---|
| 584 | checkbox.checked = true; |
|---|
| 585 | text.focus(); |
|---|
| 586 | |
|---|
| 587 | mtSetCommentParentID(); |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | <mt:Ignore> |
|---|
| 591 | /*** |
|---|
| 592 | * Sets the parent comment ID when replying to a comment. |
|---|
| 593 | */ |
|---|
| 594 | </mt:Ignore> |
|---|
| 595 | function mtSetCommentParentID() { |
|---|
| 596 | var checkbox = document.getElementById('comment-reply'); |
|---|
| 597 | var parent_id_field = document.getElementById('comment-parent-id'); |
|---|
| 598 | if (!checkbox || !parent_id_field) return; |
|---|
| 599 | |
|---|
| 600 | var pid = 0; |
|---|
| 601 | if (checkbox.checked == true) |
|---|
| 602 | pid = checkbox.value; |
|---|
| 603 | parent_id_field.value = pid; |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | <mt:Ignore> |
|---|
| 607 | /*** |
|---|
| 608 | * Persists a copy of the current user cookie into the browser cookie stash. |
|---|
| 609 | */ |
|---|
| 610 | </mt:Ignore> |
|---|
| 611 | function mtSaveUser(f) { |
|---|
| 612 | // We can't reliably store the user cookie during a preview. |
|---|
| 613 | if (is_preview) return; |
|---|
| 614 | |
|---|
| 615 | var u = mtGetUser(); |
|---|
| 616 | |
|---|
| 617 | if (f && (!u || u.is_anonymous)) { |
|---|
| 618 | if ( !u ) { |
|---|
| 619 | u = {}; |
|---|
| 620 | u.is_authenticated = false; |
|---|
| 621 | u.can_comment = true; |
|---|
| 622 | u.is_author = false; |
|---|
| 623 | u.is_banned = false; |
|---|
| 624 | u.is_anonymous = true; |
|---|
| 625 | u.is_trusted = false; |
|---|
| 626 | } |
|---|
| 627 | if (f.author != undefined) u.name = f.author.value; |
|---|
| 628 | if (f.email != undefined) u.email = f.email.value; |
|---|
| 629 | if (f.url != undefined) u.url = f.url.value; |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | if (!u) return; |
|---|
| 633 | |
|---|
| 634 | var cache_period = mtCookieTimeout * 1000; |
|---|
| 635 | |
|---|
| 636 | // cache anonymous user info for a long period if the |
|---|
| 637 | // user has requested to be remembered |
|---|
| 638 | if (u.is_anonymous && f && f.bakecookie && f.bakecookie.checked) |
|---|
| 639 | cache_period = 365 * 24 * 60 * 60 * 1000; |
|---|
| 640 | |
|---|
| 641 | var now = new Date(); |
|---|
| 642 | mtFixDate(now); |
|---|
| 643 | now.setTime(now.getTime() + cache_period); |
|---|
| 644 | |
|---|
| 645 | var cmtcookie = mtBakeUserCookie(u); |
|---|
| 646 | mtSetCookie(mtCookieName, cmtcookie, now, mtCookiePath, mtCookieDomain, |
|---|
| 647 | location.protocol == 'https:'); |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | <mt:Ignore> |
|---|
| 651 | /*** |
|---|
| 652 | * Clears the blog-side user cookie. |
|---|
| 653 | */ |
|---|
| 654 | </mt:Ignore> |
|---|
| 655 | function mtClearUser() { |
|---|
| 656 | user = null; |
|---|
| 657 | mtDeleteCookie(mtCookieName, mtCookiePath, mtCookieDomain, |
|---|
| 658 | location.protocol == 'https:'); |
|---|
| 659 | } |
|---|
| 660 | |
|---|
| 661 | <mt:Ignore> |
|---|
| 662 | /*** |
|---|
| 663 | * Sets a browser cookie. |
|---|
| 664 | */ |
|---|
| 665 | </mt:Ignore> |
|---|
| 666 | function mtSetCookie(name, value, expires, path, domain, secure) { |
|---|
| 667 | if (domain && domain.match(/^\.?localhost$/)) |
|---|
| 668 | domain = null; |
|---|
| 669 | var curCookie = name + "=" + escape(value) + |
|---|
| 670 | (expires ? "; expires=" + expires.toGMTString() : "") + |
|---|
| 671 | (path ? "; path=" + path : "") + |
|---|
| 672 | (domain ? "; domain=" + domain : "") + |
|---|
| 673 | (secure ? "; secure" : ""); |
|---|
| 674 | document.cookie = curCookie; |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | <mt:Ignore> |
|---|
| 678 | /*** |
|---|
| 679 | * Retrieves a browser cookie. |
|---|
| 680 | */ |
|---|
| 681 | </mt:Ignore> |
|---|
| 682 | function mtGetCookie(name) { |
|---|
| 683 | var prefix = name + '='; |
|---|
| 684 | var c = document.cookie; |
|---|
| 685 | var cookieStartIndex = c.indexOf(prefix); |
|---|
| 686 | if (cookieStartIndex == -1) |
|---|
| 687 | return ''; |
|---|
| 688 | var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length); |
|---|
| 689 | if (cookieEndIndex == -1) |
|---|
| 690 | cookieEndIndex = c.length; |
|---|
| 691 | return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex)); |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | <mt:Ignore> |
|---|
| 695 | /*** |
|---|
| 696 | * Deletes a browser cookie. |
|---|
| 697 | */ |
|---|
| 698 | </mt:Ignore> |
|---|
| 699 | function mtDeleteCookie(name, path, domain, secure) { |
|---|
| 700 | if (mtGetCookie(name)) { |
|---|
| 701 | if (domain && domain.match(/^\.?localhost$/)) |
|---|
| 702 | domain = null; |
|---|
| 703 | document.cookie = name + "=" + |
|---|
| 704 | (path ? "; path=" + path : "") + |
|---|
| 705 | (domain ? "; domain=" + domain : "") + |
|---|
| 706 | (secure ? "; secure" : "") + |
|---|
| 707 | "; expires=Thu, 01-Jan-70 00:00:01 GMT"; |
|---|
| 708 | } |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | function mtFixDate(date) { |
|---|
| 712 | var skew = (new Date(0)).getTime(); |
|---|
| 713 | if (skew > 0) |
|---|
| 714 | date.setTime(date.getTime() - skew); |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | <mt:Ignore> |
|---|
| 718 | /*** |
|---|
| 719 | * Returns a XMLHttpRequest object (for Ajax operations). |
|---|
| 720 | */ |
|---|
| 721 | </mt:Ignore> |
|---|
| 722 | function mtGetXmlHttp() { |
|---|
| 723 | if ( !window.XMLHttpRequest ) { |
|---|
| 724 | window.XMLHttpRequest = function() { |
|---|
| 725 | var types = [ |
|---|
| 726 | "Microsoft.XMLHTTP", |
|---|
| 727 | "MSXML2.XMLHTTP.5.0", |
|---|
| 728 | "MSXML2.XMLHTTP.4.0", |
|---|
| 729 | "MSXML2.XMLHTTP.3.0", |
|---|
| 730 | "MSXML2.XMLHTTP" |
|---|
| 731 | ]; |
|---|
| 732 | |
|---|
| 733 | for ( var i = 0; i < types.length; i++ ) { |
|---|
| 734 | try { |
|---|
| 735 | return new ActiveXObject( types[ i ] ); |
|---|
| 736 | } catch( e ) {} |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | return undefined; |
|---|
| 740 | }; |
|---|
| 741 | } |
|---|
| 742 | if ( window.XMLHttpRequest ) |
|---|
| 743 | return new XMLHttpRequest(); |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | // BEGIN: fast browser onload init |
|---|
| 747 | // Modifications by David Davis, DWD |
|---|
| 748 | // Dean Edwards/Matthias Miller/John Resig |
|---|
| 749 | // http://dean.edwards.name/weblog/2006/06/again/?full#comment5338 |
|---|
| 750 | |
|---|
| 751 | function mtInit() { |
|---|
| 752 | // quit if this function has already been called |
|---|
| 753 | if (arguments.callee.done) return; |
|---|
| 754 | |
|---|
| 755 | // flag this function so we don't do the same thing twice |
|---|
| 756 | arguments.callee.done = true; |
|---|
| 757 | |
|---|
| 758 | // kill the timer |
|---|
| 759 | // DWD - check against window |
|---|
| 760 | if ( window._timer ) clearInterval(window._timer); |
|---|
| 761 | |
|---|
| 762 | // DWD - fire the window onload now, and replace it |
|---|
| 763 | if ( window.onload && ( window.onload !== window.mtInit ) ) { |
|---|
| 764 | window.onload(); |
|---|
| 765 | window.onload = function() {}; |
|---|
| 766 | } |
|---|
| 767 | } |
|---|
| 768 | |
|---|
| 769 | /* for Mozilla/Opera9 */ |
|---|
| 770 | if (document.addEventListener) { |
|---|
| 771 | document.addEventListener("DOMContentLoaded", mtInit, false); |
|---|
| 772 | } |
|---|
| 773 | |
|---|
| 774 | /* for Internet Explorer */ |
|---|
| 775 | /*@cc_on @*/ |
|---|
| 776 | /*@if (@_win32) |
|---|
| 777 | document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>"); |
|---|
| 778 | var script = document.getElementById("__ie_onload"); |
|---|
| 779 | script.onreadystatechange = function() { |
|---|
| 780 | if (this.readyState == "complete") { |
|---|
| 781 | mtInit(); // call the onload handler |
|---|
| 782 | } |
|---|
| 783 | }; |
|---|
| 784 | /*@end @*/ |
|---|
| 785 | |
|---|
| 786 | /* for Safari */ |
|---|
| 787 | if (/WebKit/i.test(navigator.userAgent)) { // sniff |
|---|
| 788 | _timer = setInterval(function() { |
|---|
| 789 | if (/loaded|complete/.test(document.readyState)) { |
|---|
| 790 | mtInit(); // call the onload handler |
|---|
| 791 | } |
|---|
| 792 | }, 10); |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | /* for other browsers */ |
|---|
| 796 | window.onload = mtInit; |
|---|
| 797 | |
|---|
| 798 | // END: fast browser onload init |
|---|
| 799 | |
|---|
| 800 | <mt:IfRegistrationAllowed> |
|---|
| 801 | /*** |
|---|
| 802 | * If request contains a '#_login' or '#_logout' hash, use this to |
|---|
| 803 | * also delete the blog-side user cookie, since we're coming back from |
|---|
| 804 | * a login, logout or edit profile operation. |
|---|
| 805 | */ |
|---|
| 806 | var clearCookie = ( window.location.hash && window.location.hash.match( /^#_log(in|out)/ ) ) ? true : false; |
|---|
| 807 | if (clearCookie) { |
|---|
| 808 | // clear any logged in state |
|---|
| 809 | mtClearUser(); |
|---|
| 810 | if (RegExp.$1 == 'in') |
|---|
| 811 | mtFetchUser(); |
|---|
| 812 | } else { |
|---|
| 813 | <mt:Ignore> |
|---|
| 814 | /*** |
|---|
| 815 | * Uncondition this call to fetch the current user state (if available) |
|---|
| 816 | * from MT upon page load if no user cookie is already present. |
|---|
| 817 | * This is okay if you have a private install, such as an Intranet; |
|---|
| 818 | * not recommended for public web sites! |
|---|
| 819 | */ |
|---|
| 820 | </mt:Ignore> |
|---|
| 821 | if ( is_preview && !user ) |
|---|
| 822 | mtFetchUser(); |
|---|
| 823 | } |
|---|
| 824 | </mt:IfRegistrationAllowed> |
|---|