includes/captcha.php
changeset 1227 bdac73ed481e
parent 1081 745200a9cc2a
equal deleted inserted replaced
1226:de56132c008d 1227:bdac73ed481e
    19  * @copyright 2008 Dan Fuhry
    19  * @copyright 2008 Dan Fuhry
    20  */
    20  */
    21  
    21  
    22 class captcha_base
    22 class captcha_base
    23 {
    23 {
    24   
    24 	
    25   /**
    25 	/**
    26    * Our session ID
    26  	* Our session ID
    27    * @var string
    27  	* @var string
    28    */
    28  	*/
    29   
    29 	
    30   private $session_id;
    30 	private $session_id;
    31   
    31 	
    32   /**
    32 	/**
    33    * Our saved session data
    33  	* Our saved session data
    34    * @var array
    34  	* @var array
    35    */
    35  	*/
    36   
    36 	
    37   private $session_data;
    37 	private $session_data;
    38   
    38 	
    39   /**
    39 	/**
    40    * The confirmation code we're generating.
    40  	* The confirmation code we're generating.
    41    * @var string
    41  	* @var string
    42    */
    42  	*/
    43   
    43 	
    44   private $code = '';
    44 	private $code = '';
    45   
    45 	
    46   /**
    46 	/**
    47    * Numerical ID (primary key) for our session
    47  	* Numerical ID (primary key) for our session
    48    * @var int
    48  	* @var int
    49    */
    49  	*/
    50   
    50 	
    51   private $id = 0;
    51 	private $id = 0;
    52   
    52 	
    53   /**
    53 	/**
    54    * Constructor.
    54  	* Constructor.
    55    * @param string Session ID for captcha
    55  	* @param string Session ID for captcha
    56    */
    56  	*/
    57   
    57 	
    58   function __construct($session_id, $row = false)
    58 	function __construct($session_id, $row = false)
    59   {
    59 	{
    60     global $db, $session, $paths, $template, $plugins; // Common objects
    60 		global $db, $session, $paths, $template, $plugins; // Common objects
    61     if ( !preg_match('/^[a-f0-9]{32}([a-z0-9]{8})?$/', $session_id) )
    61 		if ( !preg_match('/^[a-f0-9]{32}([a-z0-9]{8})?$/', $session_id) )
    62     {
    62 		{
    63       throw new Exception('Invalid session ID');
    63 			throw new Exception('Invalid session ID');
    64     }
    64 		}
    65     $this->session_id = $session_id;
    65 		$this->session_id = $session_id;
    66     // If we weren't supplied with session info, retreive it
    66 		// If we weren't supplied with session info, retreive it
    67     if ( !is_array($row) )
    67 		if ( !is_array($row) )
    68     {
    68 		{
    69       $q = $db->sql_query('SELECT code_id, code, session_data FROM ' . table_prefix . "captcha WHERE session_id = '$session_id';");
    69 			$q = $db->sql_query('SELECT code_id, code, session_data FROM ' . table_prefix . "captcha WHERE session_id = '$session_id';");
    70       if ( !$q )
    70 			if ( !$q )
    71         $db->_die();
    71 				$db->_die();
    72       $row = $db->fetchrow();
    72 			$row = $db->fetchrow();
    73       $row['code_id'] = intval($row['code_id']);
    73 			$row['code_id'] = intval($row['code_id']);
    74       $db->free_result();
    74 			$db->free_result();
    75     }
    75 		}
    76     if ( !isset($row['code']) || !isset($row['session_data']) || !is_int(@$row['code_id']) )
    76 		if ( !isset($row['code']) || !isset($row['session_data']) || !is_int(@$row['code_id']) )
    77     {
    77 		{
    78       throw new Exception('Row doesn\'t contain what we need (code and session_data)');
    78 			throw new Exception('Row doesn\'t contain what we need (code and session_data)');
    79     }
    79 		}
    80     $this->session_data = ( is_array($x = @unserialize($row['session_data'])) ) ? $x : array();
    80 		$this->session_data = ( is_array($x = @unserialize($row['session_data'])) ) ? $x : array();
    81     $this->code = $row['code'];
    81 		$this->code = $row['code'];
    82     $this->id = $row['code_id'];
    82 		$this->id = $row['code_id'];
    83     
    83 		
    84     // run any custom init functions
    84 		// run any custom init functions
    85     if ( method_exists($this, 'construct_hook') )
    85 		if ( method_exists($this, 'construct_hook') )
    86       $this->construct_hook();
    86 			$this->construct_hook();
    87   }
    87 	}
    88   
    88 	
    89   /**
    89 	/**
    90    * Retrieves a key from the session data set
    90  	* Retrieves a key from the session data set
    91    * @param int|string Key to fetch
    91  	* @param int|string Key to fetch
    92    * @param mixed Default value for key
    92  	* @param mixed Default value for key
    93    * @return mixed
    93  	* @return mixed
    94    */
    94  	*/
    95    
    95  	
    96   function session_fetch($key, $default = false)
    96 	function session_fetch($key, $default = false)
    97   {
    97 	{
    98     return ( isset($this->session_data[$key]) ) ? $this->session_data[$key] : $default;
    98 		return ( isset($this->session_data[$key]) ) ? $this->session_data[$key] : $default;
    99   }
    99 	}
   100   
   100 	
   101   /**
   101 	/**
   102    * Stores a value in the session's data set. Change must be committed using $captcha->session_commit()
   102  	* Stores a value in the session's data set. Change must be committed using $captcha->session_commit()
   103    * @param int|string Name of key
   103  	* @param int|string Name of key
   104    * @param mixed Value - can be an array, string, int, or double, but probably not objects :-)
   104  	* @param mixed Value - can be an array, string, int, or double, but probably not objects :-)
   105    */
   105  	*/
   106   
   106 	
   107   function session_store($key, $value)
   107 	function session_store($key, $value)
   108   {
   108 	{
   109     $this->session_data[$key] = $value;
   109 		$this->session_data[$key] = $value;
   110   }
   110 	}
   111   
   111 	
   112   /**
   112 	/**
   113    * Commits changes to the session data set to the database.
   113  	* Commits changes to the session data set to the database.
   114    */
   114  	*/
   115   
   115 	
   116   function session_commit()
   116 	function session_commit()
   117   {
   117 	{
   118     global $db, $session, $paths, $template, $plugins; // Common objects
   118 		global $db, $session, $paths, $template, $plugins; // Common objects
   119     $session_data = serialize($this->session_data);
   119 		$session_data = serialize($this->session_data);
   120     $session_data = $db->escape($session_data);
   120 		$session_data = $db->escape($session_data);
   121     $code = $db->escape($this->code);
   121 		$code = $db->escape($this->code);
   122     
   122 		
   123     $q = $db->sql_query('UPDATE ' . table_prefix . "captcha SET code = '$code', session_data = '$session_data' WHERE code_id = {$this->id};");
   123 		$q = $db->sql_query('UPDATE ' . table_prefix . "captcha SET code = '$code', session_data = '$session_data' WHERE code_id = {$this->id};");
   124     if ( !$q )
   124 		if ( !$q )
   125       $db->_die();
   125 			$db->_die();
   126   }
   126 	}
   127   
   127 	
   128   /**
   128 	/**
   129    * Changes the confirmation code
   129  	* Changes the confirmation code
   130    * @param string New string
   130  	* @param string New string
   131    */
   131  	*/
   132   
   132 	
   133   function set_code($code)
   133 	function set_code($code)
   134   {
   134 	{
   135     if ( !is_string($code) )
   135 		if ( !is_string($code) )
   136       return false;
   136 			return false;
   137     
   137 		
   138     $this->code = $code;
   138 		$this->code = $code;
   139   }
   139 	}
   140   
   140 	
   141   /**
   141 	/**
   142    * Returns the confirmation code
   142  	* Returns the confirmation code
   143    * @return string
   143  	* @return string
   144    */
   144  	*/
   145   
   145 	
   146   function get_code()
   146 	function get_code()
   147   {
   147 	{
   148     return $this->code;
   148 		return $this->code;
   149   }
   149 	}
   150   
   150 	
   151 }
   151 }
   152 
   152 
   153 /**
   153 /**
   154  * Returns a new captcha object
   154  * Returns a new captcha object
   155  * @param string Session ID
   155  * @param string Session ID
   157  * @param array Optional row to send to the captcha engine
   157  * @param array Optional row to send to the captcha engine
   158  */
   158  */
   159 
   159 
   160 function captcha_object($session_id, $engine = false, $row = false)
   160 function captcha_object($session_id, $engine = false, $row = false)
   161 {
   161 {
   162   static $singletons = array();
   162 	static $singletons = array();
   163   if ( !$engine )
   163 	if ( !$engine )
   164   {
   164 	{
   165     $engine = getConfig('captcha_engine');
   165 		$engine = getConfig('captcha_engine');
   166     if ( !$engine )
   166 		if ( !$engine )
   167     {
   167 		{
   168       $engine = 'freecap';
   168 			$engine = 'freecap';
   169     }
   169 		}
   170   }
   170 	}
   171   if( !extension_loaded("gd") || !function_exists("gd_info") || !function_exists('imagettftext') || !function_exists('imagepng') || !function_exists('imagecreatefromjpeg') )
   171 	if( !extension_loaded("gd") || !function_exists("gd_info") || !function_exists('imagettftext') || !function_exists('imagepng') || !function_exists('imagecreatefromjpeg') )
   172   {
   172 	{
   173     $engine = 'failsafe';
   173 		$engine = 'failsafe';
   174   }
   174 	}
   175   if ( !class_exists("captcha_engine_$engine") )
   175 	if ( !class_exists("captcha_engine_$engine") )
   176   {
   176 	{
   177     require_once ENANO_ROOT . "/includes/captcha/engine_{$engine}.php";
   177 		require_once ENANO_ROOT . "/includes/captcha/engine_{$engine}.php";
   178   }
   178 	}
   179   if ( !class_exists("captcha_engine_$engine") )
   179 	if ( !class_exists("captcha_engine_$engine") )
   180   {
   180 	{
   181     throw new Exception("Expected but couldn't find class for captcha engine: captcha_engine_$engine");
   181 		throw new Exception("Expected but couldn't find class for captcha engine: captcha_engine_$engine");
   182   }
   182 	}
   183   $class = "captcha_engine_$engine";
   183 	$class = "captcha_engine_$engine";
   184   return new $class($session_id, $row);
   184 	return new $class($session_id, $row);
   185 }
   185 }
   186 
   186 
   187 ?>
   187 ?>