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