[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/captcha/plugins/ -> captcha_abstract.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\captcha\plugins;
  15  
  16  /**
  17  * This class holds the code shared by the two default 3.0.x CAPTCHAs.
  18  */
  19  abstract class captcha_abstract
  20  {
  21      var $confirm_id;
  22      var $confirm_code;
  23      var $code;
  24      var $seed;
  25      var $attempts = 0;
  26      var $type;
  27      var $solved = 0;
  28      var $captcha_vars = false;
  29  
  30      /**
  31      * @var string name of the service.
  32      */
  33      protected $service_name;
  34  
  35  	function init($type)
  36      {
  37          global $config, $db, $user;
  38  
  39          // read input
  40          $this->confirm_id = request_var('confirm_id', '');
  41          $this->confirm_code = request_var('confirm_code', '');
  42          $refresh = request_var('refresh_vc', false) && $config['confirm_refresh'];
  43  
  44          $this->type = (int) $type;
  45  
  46          if (!strlen($this->confirm_id) || !$this->load_code())
  47          {
  48              // we have no confirm ID, better get ready to display something
  49              $this->generate_code();
  50          }
  51          else if ($refresh)
  52          {
  53              $this->regenerate_code();
  54          }
  55      }
  56  
  57  	function execute_demo()
  58      {
  59          global $user;
  60  
  61          $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
  62          $this->seed = hexdec(substr(unique_id(), 4, 10));
  63  
  64          // compute $seed % 0x7fffffff
  65          $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
  66  
  67          $generator = $this->get_generator_class();
  68          $captcha = new $generator();
  69          define('IMAGE_OUTPUT', 1);
  70          $captcha->execute($this->code, $this->seed);
  71      }
  72  
  73  	function execute()
  74      {
  75          if (empty($this->code))
  76          {
  77              if (!$this->load_code())
  78              {
  79                  // invalid request, bail out
  80                  return false;
  81              }
  82          }
  83          $generator = $this->get_generator_class();
  84          $captcha = new $generator();
  85          define('IMAGE_OUTPUT', 1);
  86          $captcha->execute($this->code, $this->seed);
  87      }
  88  
  89  	function get_template()
  90      {
  91          global $config, $user, $template, $phpEx, $phpbb_root_path;
  92  
  93          if ($this->is_solved())
  94          {
  95              return false;
  96          }
  97          else
  98          {
  99              $link = append_sid($phpbb_root_path . 'ucp.' . $phpEx,  'mode=confirm&amp;confirm_id=' . $this->confirm_id . '&amp;type=' . $this->type);
 100              $contact_link = phpbb_get_board_contact_link($config, $phpbb_root_path, $phpEx);
 101              $explain = $user->lang(($this->type != CONFIRM_POST) ? 'CONFIRM_EXPLAIN' : 'POST_CONFIRM_EXPLAIN', '<a href="' . $contact_link . '">', '</a>');
 102  
 103              $template->assign_vars(array(
 104                  'CONFIRM_IMAGE_LINK'        => $link,
 105                  'CONFIRM_IMAGE'                => '<img src="' . $link . '" />',
 106                  'CONFIRM_IMG'                => '<img src="' . $link . '" />',
 107                  'CONFIRM_ID'                => $this->confirm_id,
 108                  'S_CONFIRM_CODE'            => true,
 109                  'S_TYPE'                    => $this->type,
 110                  'S_CONFIRM_REFRESH'            => ($config['enable_confirm'] && $config['confirm_refresh'] && $this->type == CONFIRM_REG) ? true : false,
 111                  'L_CONFIRM_EXPLAIN'            => $explain,
 112              ));
 113  
 114              return 'captcha_default.html';
 115          }
 116      }
 117  
 118  	function get_demo_template($id)
 119      {
 120          global $config, $user, $template, $phpbb_admin_path, $phpEx;
 121  
 122          $variables = '';
 123  
 124          if (is_array($this->captcha_vars))
 125          {
 126              foreach ($this->captcha_vars as $captcha_var => $template_var)
 127              {
 128                  $variables .= '&amp;' . rawurlencode($captcha_var) . '=' . request_var($captcha_var, (int) $config[$captcha_var]);
 129              }
 130          }
 131  
 132          // acp_captcha has a delivery function; let's use it
 133          $template->assign_vars(array(
 134              'CONFIRM_IMAGE'        => append_sid($phpbb_admin_path . 'index.' . $phpEx, 'captcha_demo=1&amp;mode=visual&amp;i=' . $id . '&amp;select_captcha=' . $this->get_service_name()) . $variables,
 135              'CONFIRM_ID'        => $this->confirm_id,
 136          ));
 137  
 138          return 'captcha_default_acp_demo.html';
 139      }
 140  
 141  	function get_hidden_fields()
 142      {
 143          $hidden_fields = array();
 144  
 145          // this is required for posting.php - otherwise we would forget about the captcha being already solved
 146          if ($this->solved)
 147          {
 148              $hidden_fields['confirm_code'] = $this->confirm_code;
 149          }
 150          $hidden_fields['confirm_id'] = $this->confirm_id;
 151          return $hidden_fields;
 152      }
 153  
 154  	function garbage_collect($type)
 155      {
 156          global $db, $config;
 157  
 158          $sql = 'SELECT DISTINCT c.session_id
 159              FROM ' . CONFIRM_TABLE . ' c
 160              LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
 161              WHERE s.session_id IS NULL' .
 162                  ((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
 163          $result = $db->sql_query($sql);
 164  
 165          if ($row = $db->sql_fetchrow($result))
 166          {
 167              $sql_in = array();
 168              do
 169              {
 170                  $sql_in[] = (string) $row['session_id'];
 171              }
 172              while ($row = $db->sql_fetchrow($result));
 173  
 174              if (sizeof($sql_in))
 175              {
 176                  $sql = 'DELETE FROM ' . CONFIRM_TABLE . '
 177                      WHERE ' . $db->sql_in_set('session_id', $sql_in);
 178                  $db->sql_query($sql);
 179              }
 180          }
 181          $db->sql_freeresult($result);
 182      }
 183  
 184  	function uninstall()
 185      {
 186          $this->garbage_collect(0);
 187      }
 188  
 189  	function install()
 190      {
 191          return;
 192      }
 193  
 194  	function validate()
 195      {
 196          global $config, $db, $user;
 197  
 198          if (empty($user->lang))
 199          {
 200              $user->setup();
 201          }
 202  
 203          $error = '';
 204          if (!$this->confirm_id)
 205          {
 206              $error = $user->lang['CONFIRM_CODE_WRONG'];
 207          }
 208          else
 209          {
 210              if ($this->check_code())
 211              {
 212                  $this->solved = true;
 213              }
 214              else
 215              {
 216                  $error = $user->lang['CONFIRM_CODE_WRONG'];
 217              }
 218          }
 219  
 220          if (strlen($error))
 221          {
 222              // okay, incorrect answer. Let's ask a new question.
 223              $this->new_attempt();
 224              return $error;
 225          }
 226          else
 227          {
 228              return false;
 229          }
 230      }
 231  
 232      /**
 233      * The old way to generate code, suitable for GD and non-GD. Resets the internal state.
 234      */
 235  	function generate_code()
 236      {
 237          global $db, $user;
 238  
 239          $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
 240          $this->confirm_id = md5(unique_id($user->ip));
 241          $this->seed = hexdec(substr(unique_id(), 4, 10));
 242          $this->solved = 0;
 243          // compute $seed % 0x7fffffff
 244          $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
 245  
 246          $sql = 'INSERT INTO ' . CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array(
 247                  'confirm_id'    => (string) $this->confirm_id,
 248                  'session_id'    => (string) $user->session_id,
 249                  'confirm_type'    => (int) $this->type,
 250                  'code'            => (string) $this->code,
 251                  'seed'            => (int) $this->seed)
 252          );
 253          $db->sql_query($sql);
 254      }
 255  
 256      /**
 257      * New Question, if desired.
 258      */
 259  	function regenerate_code()
 260      {
 261          global $db, $user;
 262  
 263          $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
 264          $this->seed = hexdec(substr(unique_id(), 4, 10));
 265          $this->solved = 0;
 266          // compute $seed % 0x7fffffff
 267          $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
 268  
 269          $sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
 270                  'code'            => (string) $this->code,
 271                  'seed'            => (int) $this->seed)) . '
 272                  WHERE
 273                  confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
 274                      AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
 275          $db->sql_query($sql);
 276      }
 277  
 278      /**
 279      * New Question, if desired.
 280      */
 281  	function new_attempt()
 282      {
 283          global $db, $user;
 284  
 285          $this->code = gen_rand_string_friendly(mt_rand(CAPTCHA_MIN_CHARS, CAPTCHA_MAX_CHARS));
 286          $this->seed = hexdec(substr(unique_id(), 4, 10));
 287          $this->solved = 0;
 288          // compute $seed % 0x7fffffff
 289          $this->seed -= 0x7fffffff * floor($this->seed / 0x7fffffff);
 290  
 291          $sql = 'UPDATE ' . CONFIRM_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
 292                  'code'            => (string) $this->code,
 293                  'seed'            => (int) $this->seed)) . '
 294                  , attempts = attempts + 1
 295                  WHERE
 296                  confirm_id = \'' . $db->sql_escape($this->confirm_id) . '\'
 297                      AND session_id = \'' . $db->sql_escape($user->session_id) . '\'';
 298          $db->sql_query($sql);
 299      }
 300  
 301      /**
 302      * Look up everything we need for painting&checking.
 303      */
 304  	function load_code()
 305      {
 306          global $db, $user;
 307  
 308          $sql = 'SELECT code, seed, attempts
 309              FROM ' . CONFIRM_TABLE . "
 310              WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
 311                  AND session_id = '" . $db->sql_escape($user->session_id) . "'
 312                  AND confirm_type = " . $this->type;
 313          $result = $db->sql_query($sql);
 314          $row = $db->sql_fetchrow($result);
 315          $db->sql_freeresult($result);
 316  
 317          if ($row)
 318          {
 319              $this->code = $row['code'];
 320              $this->seed = $row['seed'];
 321              $this->attempts = $row['attempts'];
 322              return true;
 323          }
 324  
 325          return false;
 326      }
 327  
 328  	function check_code()
 329      {
 330          return (strcasecmp($this->code, $this->confirm_code) === 0);
 331      }
 332  
 333  	function get_attempt_count()
 334      {
 335          return $this->attempts;
 336      }
 337  
 338  	function reset()
 339      {
 340          global $db, $user;
 341  
 342          $sql = 'DELETE FROM ' . CONFIRM_TABLE . "
 343              WHERE session_id = '" . $db->sql_escape($user->session_id) . "'
 344                  AND confirm_type = " . (int) $this->type;
 345          $db->sql_query($sql);
 346  
 347          // we leave the class usable by generating a new question
 348          $this->generate_code();
 349      }
 350  
 351  	function is_solved()
 352      {
 353          if (request_var('confirm_code', false) && $this->solved === 0)
 354          {
 355              $this->validate();
 356          }
 357          return (bool) $this->solved;
 358      }
 359  
 360      /**
 361      *  API function
 362      */
 363  	function has_config()
 364      {
 365          return false;
 366      }
 367  
 368      /**
 369      * @return string the name of the service corresponding to the plugin
 370      */
 371  	function get_service_name()
 372      {
 373          return $this->service_name;
 374      }
 375  
 376      /**
 377      * Set the name of the plugin
 378      *
 379      * @param string $name
 380      */
 381  	public function set_name($name)
 382      {
 383          $this->service_name = $name;
 384      }
 385  
 386      /**
 387      * @return string the name of the class used to generate the captcha
 388      */
 389      abstract function get_generator_class();
 390  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1