includes/captcha/engine_default.php
changeset 401 6ae6e387a0e3
child 411 d1a95497b68f
equal deleted inserted replaced
400:7eef739a5b81 401:6ae6e387a0e3
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.1.1 (Caoineag alpha 1)
       
     6  * Copyright (C) 2006-2007 Dan Fuhry
       
     7  * captcha.php - visual confirmation system used during registration
       
     8  *
       
     9  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    14  *
       
    15  * This file contains code written by Paul Sohier (www.paulscripts.nl). The CAPTCHA code was ported from the phpBB Better
       
    16  * Captcha mod, and has been released under the GPLv2 by the original author.
       
    17  */
       
    18 
       
    19 /**
       
    20  * The default CAPTCHA engine. Generates medium-strength captchas with very good performance.
       
    21  * @package Enano
       
    22  * @subpackage User management
       
    23  * @copyright 2007-2008 Dan Fuhry
       
    24  * @copyright Paul Sohier
       
    25  */
       
    26  
       
    27 class captcha_engine_default extends captcha_base
       
    28 {
       
    29   function make_image()
       
    30   {
       
    31     $code =& strtoupper($this->get_code());
       
    32     
       
    33     /**
       
    34       * The next part is orginnaly written by ted from mastercode.nl and modified for use in Enano.
       
    35       **/
       
    36     header("content-type:image/png");
       
    37     header('Cache-control: no-cache, no-store');
       
    38     $breedte = 320;
       
    39     $hoogte = 60;
       
    40     $img = imagecreatetruecolor($breedte,$hoogte);
       
    41     $achtergrond = imagecolorallocate($img, $this->color("bg"), $this->color("bg"), $this->color("bg"));
       
    42     
       
    43     imagefilledrectangle($img, 0, 0, $breedte-1, $hoogte-1, $achtergrond);
       
    44     for($g = 0;$g < 30; $g++)
       
    45     {
       
    46       $t = $this->dss_rand();
       
    47       $t = $t[0];
       
    48           
       
    49       $ypos = rand(0,$hoogte);
       
    50       $xpos = rand(0,$breedte);
       
    51           
       
    52       $kleur = imagecolorallocate($img, $this->color("bgtekst"), $this->color("bgtekst"), $this->color("bgtekst"));
       
    53           
       
    54       imagettftext($img, $this->size(), $this->move(), $xpos, $ypos, $kleur, $this->font(), $t);
       
    55     } 			
       
    56     $stukje = $breedte / (strlen($code) + 3);
       
    57     
       
    58     for($j = 0;$j < strlen($code); $j++)
       
    59     {
       
    60       
       
    61       
       
    62       $tek = $code[$j];
       
    63       $ypos = rand(33,43);
       
    64       $xpos = $stukje * ($j+1);
       
    65           
       
    66       $kleur2 = imagecolorallocate($img, $this->color("tekst"), $this->color("tekst"), $this->color("tekst"));
       
    67       
       
    68       imagettftext($img, $this->size(), $this->move(), $xpos, $ypos, $kleur2, $this->font() , $tek);
       
    69     }
       
    70       
       
    71     imagepng($img);
       
    72   }
       
    73   
       
    74   /**
       
    75     * Some functions :)
       
    76     * Also orginally written by mastercode.nl
       
    77     **/
       
    78   /**
       
    79     * Function to create a random color
       
    80     * @param $type string Mode for the color
       
    81     * @return int
       
    82     **/
       
    83   function color($type)
       
    84   {
       
    85     switch($type)
       
    86     {
       
    87       case "bg": 
       
    88         $kleur = rand(224,255); 
       
    89       break;
       
    90       case "tekst": 
       
    91         $kleur = rand(0,127); 
       
    92       break;
       
    93       case "bgtekst": 
       
    94         $kleur = rand(200,224); 
       
    95       break;
       
    96       default: 
       
    97         $kleur = rand(0,255); 
       
    98       break;
       
    99     }
       
   100     return $kleur;
       
   101   }
       
   102   /**
       
   103     * Function to ranom the size
       
   104     * @return int
       
   105     **/
       
   106   function size()
       
   107   {
       
   108     $grootte = rand(14,30);
       
   109     return $grootte;
       
   110   }
       
   111   /**
       
   112     * Function to random the posistion
       
   113     * @return int
       
   114     **/
       
   115   function move()
       
   116   {
       
   117     $draai = rand(-25,25);
       
   118     return $draai;
       
   119   }
       
   120   
       
   121   /**
       
   122     * Function to return a ttf file from fonts map
       
   123     * @return string
       
   124     **/
       
   125   function font()
       
   126   {
       
   127     $f = @opendir(ENANO_ROOT . '/includes/captcha/fonts/');
       
   128     if(!$f) die('Can\'t open includes/captcha/fonts/ for reading');
       
   129     $ar = array();
       
   130     while(($file = @readdir($f)) !== false)
       
   131     {
       
   132       if(!in_array($file, array('.','..')) && strstr($file, '.ttf'))
       
   133       {
       
   134         $ar[] = $file;
       
   135       }
       
   136     }
       
   137     if(count($ar))
       
   138     {
       
   139       shuffle($ar);
       
   140       $i = rand(0,(count($ar) - 1));
       
   141       return ENANO_ROOT . '/includes/captcha/fonts/' . $ar[$i];
       
   142     }
       
   143   }
       
   144   function dss_rand()
       
   145   {
       
   146     $val = microtime() .  mt_rand();
       
   147     $val = md5($val . 'a');
       
   148     return substr($val, 4, 16);
       
   149   }
       
   150 }