captcha.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // This file contains javascript associated with the captcha visual verification stuffs.
  2. function smfCaptcha(imageURL, uniqueID, useLibrary, letterCount)
  3. {
  4. // By default the letter count is five.
  5. if (!letterCount)
  6. letterCount = 5;
  7. uniqueID = uniqueID ? '_' + uniqueID : '';
  8. autoCreate();
  9. // Automatically get the captcha event handlers in place and the like.
  10. function autoCreate()
  11. {
  12. // Is there anything to cycle images with - if so attach the refresh image function?
  13. var cycleHandle = document.getElementById('visual_verification' + uniqueID + '_refresh');
  14. if (cycleHandle)
  15. {
  16. createEventListener(cycleHandle);
  17. cycleHandle.addEventListener('click', refreshImages, false);
  18. }
  19. // Maybe a voice is here to spread light?
  20. var soundHandle = document.getElementById('visual_verification' + uniqueID + '_sound');
  21. if (soundHandle)
  22. {
  23. createEventListener(soundHandle);
  24. soundHandle.addEventListener('click', playSound, false);
  25. }
  26. }
  27. // Change the images.
  28. function refreshImages()
  29. {
  30. // Make sure we are using a new rand code.
  31. var new_url = new String(imageURL);
  32. new_url = new_url.substr(0, new_url.indexOf("rand=") + 5);
  33. // Quick and dirty way of converting decimal to hex
  34. var hexstr = "0123456789abcdef";
  35. for(var i=0; i < 32; i++)
  36. new_url = new_url + hexstr.substr(Math.floor(Math.random() * 16), 1);
  37. if (useLibrary && document.getElementById("verification_image" + uniqueID))
  38. {
  39. document.getElementById("verification_image" + uniqueID).src = new_url;
  40. }
  41. else if (document.getElementById("verification_image" + uniqueID))
  42. {
  43. for (i = 1; i <= letterCount; i++)
  44. if (document.getElementById("verification_image" + uniqueID + "_" + i))
  45. document.getElementById("verification_image" + uniqueID + "_" + i).src = new_url + ";letter=" + i;
  46. }
  47. return false;
  48. }
  49. // Request a sound... play it Mr Soundman...
  50. function playSound(ev)
  51. {
  52. if (!ev)
  53. ev = window.event;
  54. popupFailed = reqWin(imageURL + ";sound", 400, 120);
  55. // Don't follow the link if the popup worked, which it would have done!
  56. if (!popupFailed)
  57. {
  58. if (is_ie && ev.cancelBubble)
  59. ev.cancelBubble = true;
  60. else if (ev.stopPropagation)
  61. {
  62. ev.stopPropagation();
  63. ev.preventDefault();
  64. }
  65. }
  66. return popupFailed;
  67. }
  68. }