[ Index ]
 

Code source de Cr@wltr@ck 2.2.1

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/graphs/artichow/php4/ -> AntiSpam.class.php (source)

   1  <?php
   2  /*
   3   * This work is hereby released into the Public Domain.
   4   * To view a copy of the public domain dedication,
   5   * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
   6   * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
   7   *
   8   */
   9  
  10  require_once dirname(__FILE__)."/Image.class.php";
  11  
  12  /**
  13   * AntiSpam
  14   * String printed on the images are case insensitive.
  15   *
  16   * @package Artichow
  17   */
  18  class awAntiSpam extends awImage {
  19  
  20      /**
  21       * Anti-spam string
  22       *
  23       * @var string
  24       */
  25      var $string;
  26  
  27      /**
  28       * Noise intensity
  29       *
  30       * @var int
  31       */
  32      var $noise = 0;
  33           
  34      /**
  35       * Construct a new awAntiSpam image
  36       *
  37       * @param string $string A string to display
  38       */
  39  	 function awAntiSpam($string = '') {
  40      
  41          parent::awImage();
  42          
  43          $this->string = (string)$string;
  44      
  45      }
  46      
  47      /**
  48       * Create a random string
  49       *
  50       * @param int $length String length
  51       * @return string String created
  52       */
  53  	 function setRand($length) {
  54      
  55          $length = (int)$length;
  56      
  57          $this->string = '';
  58          
  59          $letters = 'aAbBCDeEFgGhHJKLmMnNpPqQRsStTuVwWXYZz2345679';
  60          $number = strlen($letters);
  61      
  62          for($i = 0; $i < $length; $i++) {
  63              $this->string .= $letters{mt_rand(0, $number - 1)};
  64          }
  65          
  66          return $this->string;
  67          
  68      }
  69      
  70      /**
  71       * Set noise on image
  72       *
  73       * @param int $nois Noise intensity (from 0 to 10)
  74       */
  75  	 function setNoise($noise) {
  76          if($noise < 0) {
  77              $noise = 0;
  78          }
  79          if($noise > 10) {
  80              $noise = 10;
  81          }
  82          $this->noise = (int)$noise;
  83      }
  84      
  85      /**
  86       * Save string value in session
  87       * You can use check() to verify the value later
  88       *
  89       * @param string $qName A name that identify the anti-spam image
  90       */
  91  	 function save($qName) {
  92          $this->session();
  93          $session = 'artichow_'.(string)$qName;
  94          $_SESSION[$session] = $this->string;
  95      }
  96      
  97      /**
  98       * Verify user entry
  99       *
 100       * @param string $qName A name that identify the anti-spam image
 101       * @param string $value User-defined value
 102       * @param bool $case TRUE for case insensitive check, FALSE for case sensitive check ? (default to TRUE)
 103       * @return bool TRUE if the value is correct, FALSE otherwise
 104       */
 105  	 function check($qName, $value, $case = TRUE) {
 106      
 107          $this->session();
 108      
 109          $session = 'artichow_'.(string)$qName;
 110          
 111          return (
 112              array_key_exists($session, $_SESSION) === TRUE and
 113              $case ?
 114                  (strtolower($_SESSION[$session]) === strtolower((string)$value)) :
 115                  ($_SESSION[$session] === (string)$value)
 116          );
 117      
 118      }
 119      
 120      /**
 121       * Draw image
 122       */
 123  	 function draw() {
 124      
 125          $fonts = array(
 126              ARTICHOW_FONT.DIRECTORY_SEPARATOR.'Tuffy.ttf',
 127              ARTICHOW_FONT.DIRECTORY_SEPARATOR.'TuffyBold.ttf',
 128              ARTICHOW_FONT.DIRECTORY_SEPARATOR.'TuffyItalic.ttf',
 129              ARTICHOW_FONT.DIRECTORY_SEPARATOR.'TuffyBoldItalic.ttf'
 130          );
 131          
 132          $sizes = array(12, 12.5, 13, 13.5, 14, 15, 16, 17, 18, 19);
 133          
 134          $widths = array();
 135          $heights = array();
 136          $texts = array();
 137          
 138          for($i = 0; $i < strlen($this->string); $i++) {
 139          
 140              $fontKey = array_rand($fonts);
 141              $sizeKey = array_rand($sizes);
 142          
 143              $font = new awTTFFont(
 144                  $fonts[$fontKey], $sizes[$sizeKey]
 145              );
 146              
 147              $text = new awText(
 148                  $this->string{$i},
 149                  $font,
 150                  NULL,
 151                  mt_rand(-15, 15)
 152              );
 153              
 154              $widths[] = $font->getTextWidth($text);
 155              $heights[] = $font->getTextHeight($text);
 156              $texts[] = $text;
 157          
 158          }
 159          
 160          $width = array_sum($widths);
 161          $height = array_max($heights);
 162          
 163          $totalWidth = $width + 10 + count($texts) * 10;
 164          $totalHeight = $height + 20;
 165          
 166          $this->setSize($totalWidth, $totalHeight);
 167      
 168          $this->create();
 169          
 170          for($i = 0; $i < strlen($this->string); $i++) {
 171          
 172              $this->drawer->string(
 173                  $texts[$i],
 174                  new awPoint(
 175                      5 + array_sum(array_slice($widths, 0, $i)) + $widths[$i] / 2 + $i * 10,
 176                      10 + ($height - $heights[$i]) / 2
 177                  )
 178              );
 179          
 180          }
 181          
 182          $this->drawNoise($totalWidth, $totalHeight);
 183          
 184          $this->send();
 185          
 186      }
 187      
 188  	 function drawNoise($width, $height) {
 189      
 190          $points = $this->noise * 30;
 191          $color = new awColor(0, 0, 0);
 192          
 193          for($i = 0; $i < $points; $i++) {
 194              $this->drawer->point(
 195                  $color,
 196                  new awPoint(
 197                      mt_rand(0, $width),
 198                      mt_rand(0, $height)
 199                  )
 200              );
 201          }
 202      
 203      }
 204      
 205  	 function session() {
 206      
 207          // Start session if needed
 208          if(!session_id()) {
 209              session_start();
 210          }
 211          
 212      }
 213  
 214  }
 215  
 216  registerClass('AntiSpam');
 217  ?>


Généré le : Thu Sep 6 14:14:11 2007 par Balluche grâce à PHPXref 0.7