linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file AntiSpam.class.php

Documentation is available at AntiSpam.class.php

  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 linea21.externals
  17.  * @subpackage artichow
  18.  */
  19. class awAntiSpam extends awImage {
  20.  
  21.   /**
  22.    * Anti-spam string
  23.    *
  24.    * @var string 
  25.    */
  26.   protected $string;
  27.  
  28.   /**
  29.    * Noise intensity
  30.    *
  31.    * @var int 
  32.    */
  33.   protected $noise = 0;
  34.    
  35.   /**
  36.    * Construct a new awAntiSpam image
  37.    *
  38.    * @param string $string A string to display
  39.    */
  40.   public function __construct($string ''{
  41.  
  42.     parent::__construct();
  43.  
  44.     $this->string = (string)$string;
  45.  
  46.   }
  47.  
  48.   /**
  49.    * Create a random string
  50.    *
  51.    * @param int $length String length
  52.    * @return string String created
  53.    */
  54.   public function setRand($length{
  55.  
  56.     $length = (int)$length;
  57.  
  58.     $this->string = '';
  59.  
  60.     $letters 'aAbBCDeEFgGhHJKLmMnNpPqQRsStTuVwWXYZz2345679';
  61.     $number strlen($letters);
  62.  
  63.     for($i 0$i $length$i++{
  64.       $this->string .= $letters{mt_rand(0$number 1)};
  65.     }
  66.  
  67.     return $this->string;
  68.  
  69.   }
  70.  
  71.   /**
  72.    * Set noise on image
  73.    *
  74.    * @param int $nois Noise intensity (from 0 to 10)
  75.    */
  76.   public function setNoise($noise{
  77.     if($noise 0{
  78.       $noise 0;
  79.     }
  80.     if($noise 10{
  81.       $noise 10;
  82.     }
  83.     $this->noise = (int)$noise;
  84.   }
  85.  
  86.   /**
  87.    * Save string value in session
  88.    * You can use check() to verify the value later
  89.    *
  90.    * @param string $qName A name that identify the anti-spam image
  91.    */
  92.   public function save($qName{
  93.     $this->session();
  94.     $session 'artichow_'.(string)$qName;
  95.     $_SESSION[$session$this->string;
  96.   }
  97.  
  98.   /**
  99.    * Verify user entry
  100.    *
  101.    * @param string $qName A name that identify the anti-spam image
  102.    * @param string $value User-defined value
  103.    * @param bool $case TRUE for case insensitive check, FALSE for case sensitive check ? (default to TRUE)
  104.    * @return bool TRUE if the value is correct, FALSE otherwise
  105.    */
  106.   public function check($qName$value$case TRUE{
  107.  
  108.     $this->session();
  109.  
  110.     $session 'artichow_'.(string)$qName;
  111.  
  112.     return (
  113.     array_key_exists($session$_SESSION=== TRUE and
  114.     $case ?
  115.     (strtolower($_SESSION[$session]=== strtolower((string)$value)) :
  116.     ($_SESSION[$session=== (string)$value)
  117.     );
  118.  
  119.   }
  120.  
  121.   /**
  122.    * Draw image
  123.    */
  124.   public function draw({
  125.  
  126.     $fonts array(
  127.             'Tuffy',
  128.             'TuffyBold',
  129.             'TuffyItalic',
  130.             'TuffyBoldItalic'
  131.             );
  132.  
  133.             $sizes array(1212.51313.5141516171819);
  134.  
  135.             $widths array();
  136.             $heights array();
  137.             $texts array();
  138.  
  139.             // Set up a temporary driver to allow font size calculations...
  140.             $this->setSize(1010);
  141.             $driver $this->getDriver();
  142.  
  143.             for($i 0$i strlen($this->string)$i++{
  144.  
  145.               $fontKey array_rand($fonts);
  146.               $sizeKey array_rand($sizes);
  147.  
  148.               $font new awTTFFont(
  149.               $fonts[$fontKey]$sizes[$sizeKey]
  150.               );
  151.                   
  152.               $text new awText(
  153.               $this->string{$i},
  154.               $font,
  155.               NULL,
  156.               mt_rand(-1515)
  157.               );
  158.                   
  159.               $widths[$driver->getTextWidth($text);
  160.               $heights[$driver->getTextHeight($text);
  161.               $texts[$text;
  162.  
  163.             }
  164.  
  165.             // ... and get rid of it.
  166.             $this->driver = NULL;
  167.  
  168.             $width array_sum($widths);
  169.             $height array_max($heights);
  170.  
  171.             $totalWidth $width 10 count($texts10;
  172.             $totalHeight $height 20;
  173.  
  174.             $this->setSize($totalWidth$totalHeight);
  175.  
  176.             $this->create();
  177.  
  178.             for($i 0$i strlen($this->string)$i++{
  179.  
  180.               $this->driver->string(
  181.               $texts[$i],
  182.               new awPoint(
  183.                     array_sum(array_slice($widths0$i)) $widths[$i$i 10,
  184.                     10 ($height $heights[$i]2
  185.                     )
  186.                     );
  187.  
  188.             }
  189.  
  190.             $this->drawNoise($totalWidth$totalHeight);
  191.  
  192.             $this->send();
  193.  
  194.   }
  195.  
  196.   protected function drawNoise($width$height{
  197.  
  198.     $points $this->noise 30;
  199.     $color new awColor(000);
  200.  
  201.     for($i 0$i $points$i++{
  202.       $this->driver->point(
  203.       $color,
  204.       new awPoint(
  205.       mt_rand(0$width),
  206.       mt_rand(0$height)
  207.       )
  208.       );
  209.     }
  210.  
  211.   }
  212.  
  213.   protected function session({
  214.  
  215.     // Start session if needed
  216.     if(!session_id()) {
  217.       session_start();
  218.     }
  219.  
  220.   }
  221.  
  222. }
  223.  
  224. registerClass('AntiSpam');
  225. ?>

Documentation generated on Thu, 03 May 2012 15:02:02 +0200 by phpDocumentor 1.4.1