includes/clientside/static/misc.js
changeset 179 36b287f1d85c
parent 176 1bc7e849a264
child 182 c69730750be3
equal deleted inserted replaced
178:4c19952406db 179:36b287f1d85c
   300 
   300 
   301 var ajax_auth_prompt_cache = false;
   301 var ajax_auth_prompt_cache = false;
   302 var ajax_auth_mb_cache = false;
   302 var ajax_auth_mb_cache = false;
   303 var ajax_auth_level_cache = false;
   303 var ajax_auth_level_cache = false;
   304 var ajax_auth_error_string = false;
   304 var ajax_auth_error_string = false;
       
   305 var ajax_auth_show_captcha = false;
       
   306 
       
   307 function ajaxAuthErrorToString($data)
       
   308 {
       
   309   var $errstring = $data.error;
       
   310   // this was literally copied straight from the PHP code.
       
   311   switch($data.error)
       
   312   {
       
   313     case 'key_not_found':
       
   314       $errstring = 'Enano couldn\'t look up the encryption key used to encrypt your password. This most often happens if a cache rotation occurred during your login attempt, or if you refreshed the login page.';
       
   315       break;
       
   316     case 'key_wrong_length':
       
   317       $errstring = 'The encryption key was the wrong length.';
       
   318       break;
       
   319     case 'too_big_for_britches':
       
   320       $errstring = 'You are trying to authenticate at a level that your user account does not permit.';
       
   321       break;
       
   322     case 'invalid_credentials':
       
   323       $errstring = 'You have entered an invalid username or password. Please enter your login details again.';
       
   324       if ( $data.lockout_policy == 'lockout' )
       
   325       {
       
   326         $errstring += ' You have used up '+$data['lockout_fails']+' out of '+$data['lockout_threshold']+' login attempts. After you have used up all '+$data['lockout_threshold']+' login attempts, you will be locked out from logging in for '+$data['lockout_duration']+' minutes.';
       
   327       }
       
   328       else if ( $data.lockout_policy == 'captcha' )
       
   329       {
       
   330         $errstring += ' You have used up '+$data['lockout_fails']+' out of '+$data['lockout_threshold']+' login attempts. After you have used up all '+$data['lockout_threshold']+' login attempts, you will have to enter a visual confirmation code before logging in, effective for '+$data['lockout_duration']+' minutes.';
       
   331       }
       
   332       break;
       
   333     case 'backend_fail':
       
   334       $errstring = 'You entered the right credentials and everything was validated, but for some reason Enano couldn\'t register your session. This is an internal problem with the site and you are encouraged to contact site administration.';
       
   335       break;
       
   336     case 'locked_out':
       
   337       $attempts = parseInt($data['lockout_fails']);
       
   338       if ( $attempts > $data['lockout_threshold'])
       
   339         $attempts = $data['lockout_threshold'];
       
   340       window.console.debug('server time ', $data.server_time, ', last time ', $data['lockout_last_time'], ', duration ', $data['lockout_duration']);
       
   341       $time_rem = $data.lockout_duration - Math.round( ( $data.server_time - $data.lockout_last_time ) / 60 );
       
   342       $s = ( $time_rem == 1 ) ? '' : 's';
       
   343       $errstring = "You have used up all "+$data['lockout_threshold']+" allowed login attempts. Please wait "+$time_rem+" minute"+$s+" before attempting to log in again";
       
   344       if ( $data['lockout_policy'] == 'captcha' )
       
   345         $errstring += ', or enter the visual confirmation code shown above in the appropriate box';
       
   346       $errstring += '.';
       
   347       break;
       
   348   }
       
   349   return $errstring;
       
   350 }
   305 
   351 
   306 function ajaxPromptAdminAuth(call_on_ok, level)
   352 function ajaxPromptAdminAuth(call_on_ok, level)
   307 {
   353 {
   308   if ( typeof(call_on_ok) == 'function' )
   354   if ( typeof(call_on_ok) == 'function' )
   309   {
   355   {
   318       <p><img alt="Please wait..." src="'+scriptPath+'/images/loading-big.gif" /></p> \
   364       <p><img alt="Please wait..." src="'+scriptPath+'/images/loading-big.gif" /></p> \
   319     </div>';
   365     </div>';
   320   var title = ( level > USER_LEVEL_MEMBER ) ? 'You are requesting a sensitive operation.' : 'Please enter your username and password to continue.';
   366   var title = ( level > USER_LEVEL_MEMBER ) ? 'You are requesting a sensitive operation.' : 'Please enter your username and password to continue.';
   321   ajax_auth_mb_cache = new messagebox(MB_OKCANCEL|MB_ICONLOCK, title, loading_win);
   367   ajax_auth_mb_cache = new messagebox(MB_OKCANCEL|MB_ICONLOCK, title, loading_win);
   322   ajax_auth_mb_cache.onbeforeclick['OK'] = ajaxValidateLogin;
   368   ajax_auth_mb_cache.onbeforeclick['OK'] = ajaxValidateLogin;
       
   369   ajax_auth_mb_cache.onbeforeclick['Cancel'] = function()
       
   370   {
       
   371     if ( document.getElementById('autoCaptcha') )
       
   372     {
       
   373       var to = fly_out_top(document.getElementById('autoCaptcha'), false, true);
       
   374       setTimeout(function() {
       
   375           var d = document.getElementById('autoCaptcha');
       
   376           d.parentNode.removeChild(d);
       
   377         }, to);
       
   378     }
       
   379   }
   323   ajaxAuthLoginInnerSetup();
   380   ajaxAuthLoginInnerSetup();
   324 }
   381 }
   325 
   382 
   326 function ajaxAuthLoginInnerSetup()
   383 function ajaxAuthLoginInnerSetup()
   327 {
   384 {
   333         {
   390         {
   334           alert('Invalid JSON response from server: ' + response);
   391           alert('Invalid JSON response from server: ' + response);
   335           return false;
   392           return false;
   336         }
   393         }
   337         response = parseJSON(response);
   394         response = parseJSON(response);
       
   395         var disable_controls = false;
       
   396         if ( response.locked_out && !ajax_auth_error_string )
       
   397         {
       
   398           response.error = 'locked_out';
       
   399           ajax_auth_error_string = ajaxAuthErrorToString(response);
       
   400           if ( response.lockout_policy == 'captcha' )
       
   401           {
       
   402             ajax_auth_show_captcha = response.captcha;
       
   403           }
       
   404           else
       
   405           {
       
   406             disable_controls = true;
       
   407           }
       
   408         }
   338         var level = ajax_auth_level_cache;
   409         var level = ajax_auth_level_cache;
   339         var form_html = '';
   410         var form_html = '';
   340         var shown_error = false;
   411         var shown_error = false;
   341         if ( ajax_auth_error_string )
   412         if ( ajax_auth_error_string )
   342         {
   413         {
   346         }
   417         }
   347         else if ( level > USER_LEVEL_MEMBER )
   418         else if ( level > USER_LEVEL_MEMBER )
   348         {
   419         {
   349           form_html += 'Please re-enter your login details, to verify your identity.<br /><br />';
   420           form_html += 'Please re-enter your login details, to verify your identity.<br /><br />';
   350         }
   421         }
       
   422         if ( ajax_auth_show_captcha )
       
   423          {
       
   424            var captcha_html = ' \
       
   425              <tr> \
       
   426                <td>Code in image:</td> \
       
   427                <td><input type="hidden" id="ajaxlogin_captcha_hash" value="' + ajax_auth_show_captcha + '" /><input type="text" tabindex="3" size="25" id="ajaxlogin_captcha_code" /> \
       
   428              </tr>';
       
   429          }
       
   430          else
       
   431          {
       
   432            var captcha_html = '';
       
   433          }
       
   434          var disableme = ( disable_controls ) ? 'disabled="disabled" ' : '';
   351         form_html += ' \
   435         form_html += ' \
   352           <table border="0" align="center"> \
   436           <table border="0" align="center"> \
   353             <tr> \
   437             <tr> \
   354               <td>Username:</td><td><input tabindex="1" id="ajaxlogin_user" type="text"     size="25" /> \
   438               <td>Username:</td><td><input tabindex="1" id="ajaxlogin_user" type="text"     ' + disableme + 'size="25" /> \
   355             </tr> \
   439             </tr> \
   356             <tr> \
   440             <tr> \
   357               <td>Password:</td><td><input tabindex="2" id="ajaxlogin_pass" type="password" size="25" /> \
   441               <td>Password:</td><td><input tabindex="2" id="ajaxlogin_pass" type="password" ' + disableme + 'size="25" /> \
   358             </tr> \
   442             </tr> \
       
   443             ' + captcha_html + ' \
   359             <tr> \
   444             <tr> \
   360               <td colspan="2" style="text-align: center;"> \
   445               <td colspan="2" style="text-align: center;"> \
   361                 <br /><small>Trouble logging in? Try the <a href="'+makeUrlNS('Special', 'Login/' + title, 'level=' + level)+'">full login form</a>.<br />';
   446                 <br /><small>Trouble logging in? Try the <a href="'+makeUrlNS('Special', 'Login/' + title, 'level=' + level)+'">full login form</a>.<br />';
   362        if ( level <= USER_LEVEL_MEMBER )
   447        if ( level <= USER_LEVEL_MEMBER )
   363        {
   448        {
   381         }
   466         }
   382         else
   467         else
   383         {
   468         {
   384           $('ajaxlogin_user').object.focus();
   469           $('ajaxlogin_user').object.focus();
   385         }
   470         }
   386         $('ajaxlogin_pass').object.onblur = function(e) { if ( !shift ) $('messageBox').object.nextSibling.firstChild.focus(); };
   471         if ( ajax_auth_show_captcha )
   387         $('ajaxlogin_pass').object.onkeypress = function(e) { if ( !e && IE ) return true; if ( e.keyCode == 13 ) $('messageBox').object.nextSibling.firstChild.click(); };
   472         {
       
   473           $('ajaxlogin_captcha_code').object.onblur = function(e) { if ( !shift ) $('messageBox').object.nextSibling.firstChild.focus(); };
       
   474           $('ajaxlogin_captcha_code').object.onkeypress = function(e) { if ( !e && IE ) return true; if ( e.keyCode == 13 ) $('messageBox').object.nextSibling.firstChild.click(); };
       
   475         }
       
   476         else
       
   477         {
       
   478           $('ajaxlogin_pass').object.onblur = function(e) { if ( !shift ) $('messageBox').object.nextSibling.firstChild.focus(); };
       
   479           $('ajaxlogin_pass').object.onkeypress = function(e) { if ( !e && IE ) return true; if ( e.keyCode == 13 ) $('messageBox').object.nextSibling.firstChild.click(); };
       
   480         }
       
   481         if ( disable_controls )
       
   482         {
       
   483           var panel = document.getElementById('messageBoxButtons');
       
   484           panel.firstChild.disabled = true;
       
   485         }
   388         /*
   486         /*
   389         ## This causes the background image to disappear under Fx 2
   487         ## This causes the background image to disappear under Fx 2
   390         if ( shown_error )
   488         if ( shown_error )
   391         {
   489         {
   392           // fade to #FFF4F4
   490           // fade to #FFF4F4
   396                 fader.start();
   494                 fader.start();
   397           }});
   495           }});
   398           fader.start();
   496           fader.start();
   399         }
   497         }
   400         */
   498         */
       
   499         if ( ajax_auth_show_captcha )
       
   500         {
       
   501           ajaxShowCaptcha(ajax_auth_show_captcha);
       
   502           ajax_auth_show_captcha = false;
       
   503         }
   401       }
   504       }
   402     });
   505     });
   403 }
   506 }
   404 
   507 
   405 function ajaxValidateLogin()
   508 function ajaxValidateLogin()
   410     return false;
   513     return false;
   411   username = document.getElementById('ajaxlogin_user').value;
   514   username = document.getElementById('ajaxlogin_user').value;
   412   password = document.getElementById('ajaxlogin_pass').value;
   515   password = document.getElementById('ajaxlogin_pass').value;
   413   auth_enabled = false;
   516   auth_enabled = false;
   414   
   517   
       
   518   if ( document.getElementById('autoCaptcha') )
       
   519   {
       
   520     var to = fly_out_top(document.getElementById('autoCaptcha'), false, true);
       
   521     setTimeout(function() {
       
   522         var d = document.getElementById('autoCaptcha');
       
   523         d.parentNode.removeChild(d);
       
   524       }, to);
       
   525   }
       
   526   
   415   disableJSONExts();
   527   disableJSONExts();
   416   
   528   
   417   //
   529   //
   418   // Encryption test
   530   // Encryption test
   419   //
   531   //
   464     'crypt_key' : crypt_key_md5,
   576     'crypt_key' : crypt_key_md5,
   465     'challenge' : challenge_data,
   577     'challenge' : challenge_data,
   466     'crypt_data' : crypt_data,
   578     'crypt_data' : crypt_data,
   467     'level' : ajax_auth_level_cache
   579     'level' : ajax_auth_level_cache
   468   };
   580   };
       
   581   
       
   582   if ( document.getElementById('ajaxlogin_captcha_hash') )
       
   583   {
       
   584     json_data.captcha_hash = document.getElementById('ajaxlogin_captcha_hash').value;
       
   585     json_data.captcha_code = document.getElementById('ajaxlogin_captcha_code').value;
       
   586   }
   469   
   587   
   470   json_data = toJSONString(json_data);
   588   json_data = toJSONString(json_data);
   471   json_data = encodeURIComponent(json_data);
   589   json_data = encodeURIComponent(json_data);
   472   
   590   
   473   var loading_win = '<div align="center" style="text-align: center;"> \
   591   var loading_win = '<div align="center" style="text-align: center;"> \
   507             {
   625             {
   508               ajaxAuthLoginInnerSetup();
   626               ajaxAuthLoginInnerSetup();
   509             }
   627             }
   510             break;
   628             break;
   511           case 'error':
   629           case 'error':
   512             if ( response.error == 'The username and/or password is incorrect.' )
   630             if ( response.data.error == 'invalid_credentials' || response.data.error == 'locked_out' )
   513             {
   631             {
   514               ajax_auth_error_string = response.error;
   632               ajax_auth_error_string = ajaxAuthErrorToString(response.data);
   515               mb_current_obj.updateContent('');
   633               mb_current_obj.updateContent('');
   516               document.getElementById('messageBox').style.backgroundColor = '#C0C0C0';
   634               document.getElementById('messageBox').style.backgroundColor = '#C0C0C0';
   517               var mb_parent = document.getElementById('messageBox').parentNode;
   635               var mb_parent = document.getElementById('messageBox').parentNode;
   518               new Spry.Effect.Shake(mb_parent, {duration: 1500}).start();
   636               new Spry.Effect.Shake(mb_parent, {duration: 1500}).start();
   519               setTimeout("document.getElementById('messageBox').style.backgroundColor = '#FFF'; ajaxAuthLoginInnerSetup();", 2500);
   637               setTimeout("document.getElementById('messageBox').style.backgroundColor = '#FFF'; ajaxAuthLoginInnerSetup();", 2500);
       
   638               
       
   639               if ( response.data.lockout_policy == 'captcha' && response.data.error == 'locked_out' )
       
   640               {
       
   641                 ajax_auth_show_captcha = response.captcha;
       
   642               }
   520             }
   643             }
   521             else
   644             else
   522             {
   645             {
   523               alert(response.error);
   646               ajax_auth_error_string = ajaxAuthErrorToString(response.data);
   524               ajaxAuthLoginInnerSetup();
   647               ajaxAuthLoginInnerSetup();
   525             }
   648             }
   526             break;
   649             break;
   527           default:
   650           default:
   528             alert(ajax.responseText);
   651             alert(ajax.responseText);