PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/root/includes/captcha/plugins/phpbb_captcha_sortables_plugin.php

https://github.com/cope/Sortables-CAPTCHA-Plugin
PHP | 1136 lines | 807 code | 155 blank | 174 comment | 70 complexity | 057b70f9c11c6921b0a2d7e1deff5fd4 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. *
  4. * @package VC
  5. * @version $Id: phpbb_captcha_sortables_plugin.php 2009-09-03 Derky $
  6. * extending from: phpbb_captcha_qa_plugin.php 10484 2010-02-08 16:43:39Z bantu $
  7. * @copyright (c) 2006, 2008 phpBB Group
  8. * @copyright (c) 2009 Derky - phpBB3styles.net
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. *
  11. */
  12. /**
  13. * @ignore
  14. */
  15. if (!defined('IN_PHPBB'))
  16. {
  17. exit;
  18. }
  19. /**
  20. * Load extending class
  21. */
  22. if (!class_exists('phpbb_captcha_qa'))
  23. {
  24. include($phpbb_root_path . 'includes/captcha/plugins/phpbb_captcha_qa_plugin.' . $phpEx);
  25. }
  26. /**
  27. * Hack for phpBB 3.0.9 table/index name limitations
  28. * Add a backwards_compatibility boolean to $config
  29. * Due to static API calls, this has to be defined here
  30. */
  31. global $table_prefix, $config;
  32. // If the bc key is not there yet
  33. if (!isset($config['sortables_bc']))
  34. {
  35. global $db;
  36. if (!class_exists('phpbb_db_tools'))
  37. {
  38. include("$phpbb_root_path/includes/db/db_tools.$phpEx");
  39. }
  40. $db_tool = new phpbb_db_tools($db);
  41. // Find out if we need backwards compatibility
  42. ($db_tool->sql_table_exists($table_prefix . 'captcha_sortables_questions')) ? set_config('sortables_bc', 1) : set_config('sortables_bc', 0);
  43. }
  44. // Use the backwards compatible table names? (longer then 30 digits and already created on a phpBB 3.0.8 installation or lower)
  45. if ($config['sortables_bc'])
  46. {
  47. define('CAPTCHA_SORTABLES_QUESTIONS_TABLE', $table_prefix . 'captcha_sortables_questions');
  48. define('CAPTCHA_SORTABLES_ANSWERS_TABLE', $table_prefix . 'captcha_sortables_answers');
  49. define('CAPTCHA_SORTABLES_CONFIRM_TABLE', $table_prefix . 'captcha_sortables_confirm');
  50. }
  51. else // The new shorted table names
  52. {
  53. define('CAPTCHA_SORTABLES_QUESTIONS_TABLE', $table_prefix . 'sortables_questions');
  54. define('CAPTCHA_SORTABLES_ANSWERS_TABLE', $table_prefix . 'sortables_answers');
  55. define('CAPTCHA_SORTABLES_CONFIRM_TABLE', $table_prefix . 'sortables_confirm');
  56. }
  57. /**
  58. * Sortables captcha with extending of the QA captcha class.
  59. *
  60. * @package VC
  61. */
  62. class phpbb_captcha_sortables extends phpbb_captcha_qa
  63. {
  64. var $confirm_id;
  65. var $options_left; // $answer in captcha_qa
  66. var $options_right; //
  67. var $question_ids;
  68. var $answer_ids = false;
  69. var $question_text;
  70. var $question_lang;
  71. var $question_sort;
  72. var $attempts = 0;
  73. var $type;
  74. // dirty trick: 0 is false, but can still encode that the captcha is not yet validated
  75. var $solved = 0;
  76. /**
  77. * @param int $type as per the CAPTCHA API docs, the type
  78. */
  79. function init($type)
  80. {
  81. global $config, $db, $user;
  82. // load our language file
  83. $user->add_lang('mods/captcha_sortables');
  84. // read input
  85. $this->confirm_id = request_var('sortables_confirm_id', '');
  86. $this->type = (int) $type;
  87. $this->question_lang = $user->lang_name;
  88. // we need all defined questions - shouldn't be too many, so we can just grab them
  89. // try the user's lang first
  90. $sql = 'SELECT question_id
  91. FROM ' . CAPTCHA_SORTABLES_QUESTIONS_TABLE . "
  92. WHERE lang_iso = '" . $db->sql_escape($user->lang_name) . "'";
  93. $result = $db->sql_query($sql, 3600);
  94. while ($row = $db->sql_fetchrow($result))
  95. {
  96. $this->question_ids[$row['question_id']] = $row['question_id'];
  97. }
  98. $db->sql_freeresult($result);
  99. // fallback to the board default lang
  100. if (!sizeof($this->question_ids))
  101. {
  102. $this->question_lang = $config['default_lang'];
  103. $sql = 'SELECT question_id
  104. FROM ' . CAPTCHA_SORTABLES_QUESTIONS_TABLE . "
  105. WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
  106. $result = $db->sql_query($sql, 7200);
  107. while ($row = $db->sql_fetchrow($result))
  108. {
  109. $this->question_ids[$row['question_id']] = $row['question_id'];
  110. }
  111. $db->sql_freeresult($result);
  112. }
  113. // okay, if there is a confirm_id, we try to load that confirm's state. If not, we try to find one
  114. if (!$this->load_answer() && (!$this->load_confirm_id() || !$this->load_answer()))
  115. {
  116. // we have no valid confirm ID, better get ready to ask something
  117. $this->select_question();
  118. }
  119. }
  120. /**
  121. * API function
  122. */
  123. function &get_instance()
  124. {
  125. $instance =& new phpbb_captcha_sortables();
  126. return $instance;
  127. }
  128. /**
  129. * See if the captcha has created its tables.
  130. */
  131. function is_installed()
  132. {
  133. global $db, $phpbb_root_path, $phpEx;
  134. if (!class_exists('phpbb_db_tools'))
  135. {
  136. include("$phpbb_root_path/includes/db/db_tools.$phpEx");
  137. }
  138. $db_tool = new phpbb_db_tools($db);
  139. return $db_tool->sql_table_exists(CAPTCHA_SORTABLES_QUESTIONS_TABLE);
  140. }
  141. /**
  142. * API function - for the captcha to be available, it must have installed itself and there has to be at least one question in the board's default lang
  143. */
  144. function is_available()
  145. {
  146. global $config, $db, $phpbb_root_path, $phpEx, $user;
  147. // load language file for pretty display in the ACP dropdown
  148. $user->add_lang('mods/captcha_sortables');
  149. if (!phpbb_captcha_sortables::is_installed())
  150. {
  151. return false;
  152. }
  153. $sql = 'SELECT COUNT(question_id) AS question_count
  154. FROM ' . CAPTCHA_SORTABLES_QUESTIONS_TABLE . "
  155. WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
  156. $result = $db->sql_query($sql);
  157. $question_count = $db->sql_fetchfield('question_count');
  158. $db->sql_freeresult($result);
  159. return ((bool) $question_count);
  160. }
  161. /**
  162. * API function
  163. */
  164. function has_config()
  165. {
  166. return true;
  167. }
  168. /**
  169. * API function
  170. */
  171. function get_name()
  172. {
  173. return 'CAPTCHA_SORTABLES';
  174. }
  175. /**
  176. * API function
  177. */
  178. function get_class_name()
  179. {
  180. return 'phpbb_captcha_sortables';
  181. }
  182. /**
  183. * API function - send the question to the template
  184. */
  185. function get_template()
  186. {
  187. global $template;
  188. if ($this->is_solved())
  189. {
  190. return false;
  191. }
  192. else
  193. {
  194. $template->assign_vars(array(
  195. 'SORTABLES_CONFIRM_QUESTION' => $this->question_text,
  196. 'SORTABLES_CONFIRM_ID' => $this->confirm_id,
  197. 'SORTABLES_NAME_LEFT' => $this->name_left,
  198. 'SORTABLES_NAME_RIGHT' => $this->name_right,
  199. 'SORTABLES_DEFAULT_SORT' => (!$this->question_sort) ? 'LEFT' : 'RIGHT', // 0 = left, 1 = right
  200. 'S_CONFIRM_CODE' => true,
  201. 'S_TYPE' => $this->type,
  202. // Set version numbers here, so jQuery updates don't require a template refresh anymore
  203. 'SORTABLES_JQUERY_VERSION' => '1.8.3',
  204. 'SORTABLES_JQUERYUI_VERSION' => '1.9.2',
  205. ));
  206. return 'captcha_sortables.html';
  207. }
  208. }
  209. /**
  210. * API function - we just display a mockup so that the captcha doesn't need to be installed
  211. */
  212. function get_demo_template()
  213. {
  214. return 'captcha_sortables_acp_demo.html';
  215. }
  216. /**
  217. * API function
  218. */
  219. function get_hidden_fields()
  220. {
  221. $hidden_fields = array();
  222. // this is required - otherwise we would forget about the captcha being already solved
  223. if ($this->solved)
  224. {
  225. $hidden_fields['sortables_options_left'] = $this->options_left;
  226. $hidden_fields['sortables_options_right'] = $this->options_right;
  227. }
  228. $hidden_fields['sortables_confirm_id'] = $this->confirm_id;
  229. return $hidden_fields;
  230. }
  231. /**
  232. * API function, just the same from captcha_qa but with other table names
  233. */
  234. function garbage_collect($type = 0)
  235. {
  236. global $db, $config;
  237. $sql = 'SELECT c.confirm_id
  238. FROM ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . ' c
  239. LEFT JOIN ' . SESSIONS_TABLE . ' s
  240. ON (c.session_id = s.session_id)
  241. WHERE s.session_id IS NULL' .
  242. ((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type);
  243. $result = $db->sql_query($sql);
  244. if ($row = $db->sql_fetchrow($result))
  245. {
  246. $sql_in = array();
  247. do
  248. {
  249. $sql_in[] = (string) $row['confirm_id'];
  250. }
  251. while ($row = $db->sql_fetchrow($result));
  252. if (sizeof($sql_in))
  253. {
  254. $sql = 'DELETE FROM ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . '
  255. WHERE ' . $db->sql_in_set('confirm_id', $sql_in);
  256. $db->sql_query($sql);
  257. }
  258. }
  259. $db->sql_freeresult($result);
  260. }
  261. /**
  262. * API function - we don't drop the tables here, as that would cause the loss of all entered questions.
  263. */
  264. function uninstall()
  265. {
  266. $this->garbage_collect(0);
  267. }
  268. /**
  269. * API function - create the tables needed for sortables captcha
  270. */
  271. function install()
  272. {
  273. global $db, $phpbb_root_path, $phpEx;
  274. if (!class_exists('phpbb_db_tools'))
  275. {
  276. include("$phpbb_root_path/includes/db/db_tools.$phpEx");
  277. }
  278. $db_tool = new phpbb_db_tools($db);
  279. $tables = array(CAPTCHA_SORTABLES_QUESTIONS_TABLE, CAPTCHA_SORTABLES_ANSWERS_TABLE, CAPTCHA_SORTABLES_CONFIRM_TABLE);
  280. $schemas = array(
  281. CAPTCHA_SORTABLES_QUESTIONS_TABLE => array (
  282. 'COLUMNS' => array(
  283. 'question_id' => array('UINT', Null, 'auto_increment'),
  284. 'sort' => array('BOOL', 0),
  285. 'lang_id' => array('UINT', 0),
  286. 'lang_iso' => array('VCHAR:30', ''),
  287. 'question_text' => array('TEXT_UNI', ''),
  288. 'name_left' => array('STEXT_UNI', 0), // Column names
  289. 'name_right' => array('STEXT_UNI', 0),
  290. ),
  291. 'PRIMARY_KEY' => 'question_id',
  292. 'KEYS' => array(
  293. 'iso' => array('INDEX', 'lang_iso'),
  294. ),
  295. ),
  296. CAPTCHA_SORTABLES_ANSWERS_TABLE => array (
  297. 'COLUMNS' => array(
  298. 'answer_id' => array('UINT', Null, 'auto_increment'),
  299. 'question_id' => array('UINT', 0),
  300. 'answer_sort' => array('BOOL', 0),
  301. 'answer_text' => array('STEXT_UNI', ''),
  302. ),
  303. 'PRIMARY_KEY' => 'answer_id',
  304. 'KEYS' => array(
  305. 'aid' => array('INDEX', 'answer_id'),
  306. 'qid' => array('INDEX', 'question_id'),
  307. 'asort' => array('INDEX', 'answer_sort'),
  308. ),
  309. ),
  310. CAPTCHA_SORTABLES_CONFIRM_TABLE => array (
  311. 'COLUMNS' => array(
  312. 'session_id' => array('CHAR:32', ''),
  313. 'confirm_id' => array('CHAR:32', ''),
  314. 'lang_iso' => array('VCHAR:30', ''),
  315. 'question_id' => array('UINT', 0),
  316. 'attempts' => array('UINT', 0),
  317. 'confirm_type' => array('USINT', 0),
  318. ),
  319. 'KEYS' => array(
  320. 'sid' => array('INDEX', 'session_id'),
  321. 'lookup' => array('INDEX', array('confirm_id', 'session_id', 'lang_iso')),
  322. ),
  323. 'PRIMARY_KEY' => 'confirm_id',
  324. ),
  325. );
  326. foreach($schemas as $table => $schema)
  327. {
  328. if (!$db_tool->sql_table_exists($table))
  329. {
  330. $db_tool->sql_create_table($table, $schema);
  331. }
  332. }
  333. }
  334. /**
  335. * API function - see what has to be done to validate
  336. */
  337. function validate()
  338. {
  339. global $config, $db, $user;
  340. $error = '';
  341. if (!sizeof($this->question_ids))
  342. {
  343. return false;
  344. }
  345. if (!$this->confirm_id)
  346. {
  347. $error = $user->lang['CONFIRM_QUESTION_WRONG'];
  348. }
  349. else
  350. {
  351. if ($this->check_answer())
  352. {
  353. // $this->delete_code(); commented out to allow posting.php to repeat the question
  354. $this->solved = true;
  355. }
  356. else
  357. {
  358. $error = $user->lang['CONFIRM_QUESTION_WRONG'];
  359. }
  360. }
  361. if (strlen($error))
  362. {
  363. // okay, incorrect answer. Let's ask a new question.
  364. $this->new_attempt();
  365. $this->solved = false;
  366. return $error;
  367. }
  368. else
  369. {
  370. return false;
  371. }
  372. }
  373. /**
  374. * Select a question
  375. */
  376. function select_question()
  377. {
  378. global $db, $user;
  379. if (!sizeof($this->question_ids))
  380. {
  381. return false;
  382. }
  383. $this->confirm_id = md5(unique_id($user->ip));
  384. $this->question = (int) array_rand($this->question_ids);
  385. $sql = 'INSERT INTO ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . ' ' . $db->sql_build_array('INSERT', array(
  386. 'confirm_id' => (string) $this->confirm_id,
  387. 'session_id' => (string) $user->session_id,
  388. 'lang_iso' => (string) $this->question_lang,
  389. 'confirm_type' => (int) $this->type,
  390. 'question_id' => (int) $this->question,
  391. ));
  392. $db->sql_query($sql);
  393. $this->load_answer();
  394. }
  395. /**
  396. * New Question, if desired.
  397. */
  398. function reselect_question()
  399. {
  400. global $db, $user;
  401. if (!sizeof($this->question_ids))
  402. {
  403. return false;
  404. }
  405. $this->question = (int) array_rand($this->question_ids);
  406. $this->solved = 0;
  407. $sql = 'UPDATE ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . '
  408. SET question_id = ' . (int) $this->question . "
  409. WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
  410. AND session_id = '" . $db->sql_escape($user->session_id) . "'";
  411. $db->sql_query($sql);
  412. $this->load_answer();
  413. }
  414. /**
  415. * Wrong answer, so we increase the attempts and use a different question.
  416. */
  417. function new_attempt()
  418. {
  419. global $db, $user;
  420. // yah, I would prefer a stronger rand, but this should work
  421. $this->question = (int) array_rand($this->question_ids);
  422. $this->solved = 0;
  423. $sql = 'UPDATE ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . '
  424. SET question_id = ' . (int) $this->question . ",
  425. attempts = attempts + 1
  426. WHERE confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
  427. AND session_id = '" . $db->sql_escape($user->session_id) . "'";
  428. $db->sql_query($sql);
  429. $this->load_answer();
  430. }
  431. /**
  432. * See if there is already an entry for the current session.
  433. */
  434. function load_confirm_id()
  435. {
  436. global $db, $user;
  437. $sql = 'SELECT confirm_id
  438. FROM ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . "
  439. WHERE
  440. session_id = '" . $db->sql_escape($user->session_id) . "'
  441. AND lang_iso = '" . $db->sql_escape($this->question_lang) . "'
  442. AND confirm_type = " . $this->type;
  443. $result = $db->sql_query_limit($sql, 1);
  444. $confirm_id = $db->sql_fetchfield('confirm_id');
  445. $db->sql_freeresult($result);
  446. if ($confirm_id)
  447. {
  448. $this->confirm_id = $confirm_id;
  449. return true;
  450. }
  451. return false;
  452. }
  453. /**
  454. * Look up everything we need and populate the instance variables.
  455. */
  456. function load_answer()
  457. {
  458. global $db, $user, $template;
  459. if (!strlen($this->confirm_id) || !sizeof($this->question_ids))
  460. {
  461. return false;
  462. }
  463. $sql = 'SELECT con.question_id, attempts, question_text, sort, name_left, name_right
  464. FROM ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . ' con, ' . CAPTCHA_SORTABLES_QUESTIONS_TABLE . " qes
  465. WHERE con.question_id = qes.question_id
  466. AND confirm_id = '" . $db->sql_escape($this->confirm_id) . "'
  467. AND session_id = '" . $db->sql_escape($user->session_id) . "'
  468. AND qes.lang_iso = '" . $db->sql_escape($this->question_lang) . "'
  469. AND confirm_type = " . $this->type;
  470. $result = $db->sql_query($sql);
  471. $row = $db->sql_fetchrow($result);
  472. $db->sql_freeresult($result);
  473. if ($row)
  474. {
  475. $this->question = $row['question_id'];
  476. $this->attempts = $row['attempts'];
  477. $this->question_sort = $row['sort'];
  478. $this->question_text = $row['question_text'];
  479. $this->name_left = $row['name_left'];
  480. $this->name_right = $row['name_right'];
  481. // Postgres random fix
  482. $order_by = ($db->sql_layer == 'postgres') ? 'ORDER BY RANDOM()' : 'ORDER BY RAND()';
  483. // Let's load the answers
  484. $sql = 'SELECT answer_id, answer_text
  485. FROM ' . CAPTCHA_SORTABLES_ANSWERS_TABLE . "
  486. WHERE question_id = '" . (int) $this->question . "'
  487. $order_by";
  488. $result = $db->sql_query($sql);
  489. $template->destroy_block_vars('options'); // It's running twice, only grab the lastest see topic 1732385
  490. $this->total_options = 0;
  491. while ($row = $db->sql_fetchrow($result))
  492. {
  493. $template->assign_block_vars('options', array(
  494. 'ID' => $row['answer_id'],
  495. 'TEXT' => $row['answer_text'],
  496. ));
  497. $this->total_options++;
  498. }
  499. $db->sql_freeresult($result);
  500. return true;
  501. }
  502. return false;
  503. }
  504. /**
  505. * The actual validation
  506. */
  507. function check_answer()
  508. {
  509. global $db;
  510. // Well how did the user sorted it
  511. $options_left = request_var('sortables_options_left', array(0));
  512. $options_right = request_var('sortables_options_right', array(0));
  513. // Make sure the didn't submitted more options then it should (like trying everything... left/right: options ^ 2 )
  514. if ($this->total_options === sizeof($options_left) + sizeof($options_right))
  515. {
  516. // Let's count how many options the user sorted correctly
  517. $sql = 'SELECT COUNT(*) AS total
  518. FROM ' . CAPTCHA_SORTABLES_ANSWERS_TABLE . '
  519. WHERE question_id = ' . (int) $this->question . '
  520. AND ((answer_sort = 0 AND ' . $db->sql_in_set('answer_id', $options_left, false, true) . ')
  521. OR (answer_sort = 1 AND ' . $db->sql_in_set('answer_id', $options_right, false, true) .'))';
  522. $result = $db->sql_query($sql);
  523. $total_options_good = (int) $db->sql_fetchfield('total');
  524. // Now compare that amount with the total amount of options for this question
  525. if ($this->total_options === $total_options_good)
  526. {
  527. $this->solved = true;
  528. // Remember this for the hidden fields
  529. $this->options_left = $options_left;
  530. $this->options_right = $options_right;
  531. }
  532. $db->sql_freeresult($result);
  533. }
  534. return $this->solved;
  535. }
  536. /**
  537. * API function - clean the entry
  538. */
  539. function delete_code()
  540. {
  541. global $db, $user;
  542. $sql = 'DELETE FROM ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . "
  543. WHERE confirm_id = '" . $db->sql_escape($confirm_id) . "'
  544. AND session_id = '" . $db->sql_escape($user->session_id) . "'
  545. AND confirm_type = " . $this->type;
  546. $db->sql_query($sql);
  547. }
  548. /**
  549. * API function
  550. */
  551. function get_attempt_count()
  552. {
  553. return $this->attempts;
  554. }
  555. /**
  556. * API function
  557. */
  558. function reset()
  559. {
  560. global $db, $user;
  561. $sql = 'DELETE FROM ' . CAPTCHA_SORTABLES_CONFIRM_TABLE . "
  562. WHERE session_id = '" . $db->sql_escape($user->session_id) . "'
  563. AND confirm_type = " . (int) $this->type;
  564. $db->sql_query($sql);
  565. // we leave the class usable by generating a new question
  566. $this->select_question();
  567. }
  568. /**
  569. * API function
  570. */
  571. function is_solved()
  572. {
  573. if (request_var('qa_answer', false) && $this->solved === 0)
  574. {
  575. $this->validate();
  576. }
  577. return (bool) $this->solved;
  578. }
  579. /**
  580. * API function - The ACP backend, this marks the end of the easy methods
  581. */
  582. function acp_page($id, &$module)
  583. {
  584. global $db, $user, $auth, $template;
  585. global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
  586. $user->add_lang('acp/board');
  587. $user->add_lang('mods/captcha_sortables');
  588. if (!$this->is_installed())
  589. {
  590. $this->install();
  591. }
  592. $module->tpl_name = 'captcha_sortables_acp';
  593. $module->page_title = 'ACP_VC_SETTINGS';
  594. $form_key = 'acp_captcha';
  595. add_form_key($form_key);
  596. $submit = request_var('submit', false);
  597. $question_id = request_var('question_id', 0);
  598. $action = request_var('action', '');
  599. // we have two pages, so users might want to navigate from one to the other
  600. $list_url = $module->u_action . "&amp;configure=1&amp;select_captcha=" . $this->get_class_name();
  601. $template->assign_vars(array(
  602. 'U_ACTION' => $module->u_action,
  603. 'QUESTION_ID' => $question_id ,
  604. 'CLASS' => $this->get_class_name(),
  605. ));
  606. // show the list?
  607. if (!$question_id && $action != 'add')
  608. {
  609. $this->acp_question_list($module);
  610. }
  611. else if ($question_id && $action == 'delete')
  612. {
  613. if ($this->get_class_name() !== $config['captcha_plugin'] || !$this->acp_is_last($question_id))
  614. {
  615. if (confirm_box(true))
  616. {
  617. $this->acp_delete_question($question_id);
  618. trigger_error($user->lang['QUESTION_DELETED'] . adm_back_link($list_url));
  619. }
  620. else
  621. {
  622. confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
  623. 'question_id' => $question_id,
  624. 'action' => $action,
  625. 'configure' => 1,
  626. 'select_captcha' => $this->get_class_name(),
  627. ))
  628. );
  629. }
  630. }
  631. else
  632. {
  633. trigger_error($user->lang['QA_LAST_QUESTION'] . adm_back_link($list_url), E_USER_WARNING);
  634. }
  635. }
  636. else
  637. {
  638. // okay, show the editor
  639. $error = false;
  640. $input_question = request_var('question_text', '', true);
  641. $input_name_left = request_var('name_left', '', true);
  642. $input_name_right = request_var('name_right', '', true);
  643. $input_options_left = request_var('options_left', '', true);
  644. $input_options_right = request_var('options_right', '', true);
  645. $input_lang = request_var('lang_iso', '');
  646. $input_sort = request_var('sort', false);
  647. $langs = $this->get_languages();
  648. foreach ($langs as $lang => $entry)
  649. {
  650. $template->assign_block_vars('langs', array(
  651. 'ISO' => $lang,
  652. 'NAME' => $entry['name'],
  653. ));
  654. }
  655. $template->assign_vars(array(
  656. 'U_LIST' => $list_url,
  657. ));
  658. if ($question_id)
  659. {
  660. if ($question = $this->acp_get_question_data($question_id))
  661. {
  662. $options_left = (isset($input_options_left[$lang])) ? $input_options_left[$lang] : implode("\n", $question['options_left']);
  663. $options_right = (isset($input_options_right[$lang])) ? $input_options_right[$lang] : implode("\n", $question['options_right']);
  664. $template->assign_vars(array(
  665. 'QUESTION_TEXT' => ($input_question) ? $input_question : $question['question_text'],
  666. 'LANG_ISO' => ($input_lang) ? $input_lang : $question['lang_iso'],
  667. 'SORT' => (isset($_REQUEST['sort'])) ? $input_sort : $question['sort'],
  668. 'NAME_LEFT' => ($input_name_left) ? $input_name_left : $question['name_left'],
  669. 'NAME_RIGHT' => ($input_name_right) ? $input_name_right : $question['name_right'],
  670. 'OPTIONS_LEFT' => $options_left,
  671. 'OPTIONS_RIGHT' => $options_right,
  672. ));
  673. }
  674. else
  675. {
  676. trigger_error($user->lang['FORM_INVALID'] . adm_back_link($list_url));
  677. }
  678. }
  679. else
  680. {
  681. $template->assign_vars(array(
  682. 'QUESTION_TEXT' => $input_question,
  683. 'LANG_ISO' => $input_lang,
  684. 'SORT' => $input_sort,
  685. 'NAME_LEFT' => $input_name_left,
  686. 'NAME_RIGHT' => $input_name_right,
  687. 'OPTIONS_LEFT' => $input_options_left,
  688. 'OPTIONS_RIGHT' => $input_options_right,
  689. ));
  690. }
  691. if ($submit && check_form_key($form_key))
  692. {
  693. $data = $this->acp_get_question_input();
  694. if (!$this->validate_input($data))
  695. {
  696. $template->assign_vars(array(
  697. 'S_ERROR' => true,
  698. ));
  699. }
  700. else
  701. {
  702. if ($question_id)
  703. {
  704. $this->acp_update_question($data, $question_id);
  705. }
  706. else
  707. {
  708. $this->acp_add_question($data);
  709. }
  710. add_log('admin', 'LOG_CONFIG_VISUAL');
  711. trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($list_url));
  712. }
  713. }
  714. else if ($submit)
  715. {
  716. trigger_error($user->lang['FORM_INVALID'] . adm_back_link($list_url), E_USER_WARNING);
  717. }
  718. }
  719. }
  720. /**
  721. * This handles the list overview
  722. */
  723. function acp_question_list(&$module)
  724. {
  725. global $db, $template;
  726. $sql = 'SELECT *
  727. FROM ' . CAPTCHA_SORTABLES_QUESTIONS_TABLE;
  728. $result = $db->sql_query($sql);
  729. $template->assign_vars(array(
  730. 'S_LIST' => true,
  731. ));
  732. while ($row = $db->sql_fetchrow($result))
  733. {
  734. $url = $module->u_action . "&amp;question_id={$row['question_id']}&amp;configure=1&amp;select_captcha=" . $this->get_class_name() . '&amp;';
  735. $template->assign_block_vars('questions', array(
  736. 'QUESTION_TEXT' => $row['question_text'],
  737. 'QUESTION_ID' => $row['question_id'],
  738. 'QUESTION_LANG' => $row['lang_iso'],
  739. 'U_DELETE' => "{$url}action=delete",
  740. 'U_EDIT' => "{$url}action=edit",
  741. ));
  742. }
  743. $db->sql_freeresult($result);
  744. }
  745. /**
  746. * Grab a question and bring it into a format the editor understands
  747. */
  748. function acp_get_question_data($question_id)
  749. {
  750. global $db;
  751. if ($question_id)
  752. {
  753. $sql = 'SELECT *
  754. FROM ' . CAPTCHA_SORTABLES_QUESTIONS_TABLE . '
  755. WHERE question_id = ' . $question_id;
  756. $result = $db->sql_query($sql);
  757. $question = $db->sql_fetchrow($result);
  758. $db->sql_freeresult($result);
  759. if (!$question)
  760. {
  761. return false;
  762. }
  763. $question['answers'] = array();
  764. $question['options_left'] = array();
  765. $question['options_right'] = array();
  766. $sql = 'SELECT *
  767. FROM ' . CAPTCHA_SORTABLES_ANSWERS_TABLE . '
  768. WHERE question_id = ' . $question_id;
  769. $result = $db->sql_query($sql);
  770. while ($row = $db->sql_fetchrow($result))
  771. {
  772. if (!$row['answer_sort']) // 0 = left column, 1 = right column
  773. {
  774. $question['options_left'][] = $row['answer_text'];
  775. }
  776. else
  777. {
  778. $question['options_right'][] = $row['answer_text'];
  779. }
  780. }
  781. $db->sql_freeresult($result);
  782. return $question;
  783. }
  784. }
  785. /**
  786. * Grab a question from input and bring it into a format the editor understands
  787. */
  788. function acp_get_question_input()
  789. {
  790. $question = array(
  791. 'question_text' => request_var('question_text', '', true),
  792. 'sort' => request_var('sort', false),
  793. 'lang_iso' => request_var('lang_iso', ''),
  794. 'name_left' => request_var('name_left', '', true),
  795. 'name_right' => request_var('name_right', '', true),
  796. 'options_left' => explode("\n", request_var('options_left', '', true)),
  797. 'options_right' => explode("\n", request_var('options_right', '', true)),
  798. );
  799. return $question;
  800. }
  801. /**
  802. * Update a question.
  803. * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
  804. */
  805. function acp_update_question($data, $question_id)
  806. {
  807. global $db, $cache;
  808. // easier to delete all answers than to figure out which to update
  809. $sql = 'DELETE FROM ' . CAPTCHA_SORTABLES_ANSWERS_TABLE . " WHERE question_id = $question_id";
  810. $db->sql_query($sql);
  811. $langs = $this->get_languages();
  812. $question_ary = $data;
  813. $question_ary['lang_id'] = $langs[$question_ary['lang_iso']]['id'];
  814. unset($question_ary['options_left']);
  815. unset($question_ary['options_right']);
  816. $sql = "UPDATE " . CAPTCHA_SORTABLES_QUESTIONS_TABLE . '
  817. SET ' . $db->sql_build_array('UPDATE', $question_ary) . "
  818. WHERE question_id = $question_id";
  819. $db->sql_query($sql);
  820. $this->acp_insert_answers($data, $question_id);
  821. $cache->destroy('sql', CAPTCHA_SORTABLES_QUESTIONS_TABLE);
  822. }
  823. /**
  824. * Insert a question.
  825. * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
  826. */
  827. function acp_add_question($data)
  828. {
  829. global $db, $cache;
  830. $langs = $this->get_languages();
  831. $question_ary = $data;
  832. $question_ary['lang_id'] = $langs[$data['lang_iso']]['id'];
  833. unset($question_ary['options_left']);
  834. unset($question_ary['options_right']);
  835. $sql = 'INSERT INTO ' . CAPTCHA_SORTABLES_QUESTIONS_TABLE . $db->sql_build_array('INSERT', $question_ary);
  836. $db->sql_query($sql);
  837. $question_id = $db->sql_nextid();
  838. $this->acp_insert_answers($data, $question_id);
  839. $cache->destroy('sql', CAPTCHA_SORTABLES_QUESTIONS_TABLE);
  840. }
  841. /**
  842. * Insert the answers.
  843. * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
  844. */
  845. function acp_insert_answers($data, $question_id)
  846. {
  847. global $db, $cache;
  848. foreach ($data['options_left'] as $answer)
  849. {
  850. $answer_ary = array(
  851. 'answer_id' => $this->acp_gen_random_answer_id(),
  852. 'question_id' => $question_id,
  853. 'answer_sort' => 0,
  854. 'answer_text' => $answer,
  855. );
  856. $sql = 'INSERT INTO ' . CAPTCHA_SORTABLES_ANSWERS_TABLE . $db->sql_build_array('INSERT', $answer_ary);
  857. $db->sql_query($sql);
  858. }
  859. foreach ($data['options_right'] as $answer)
  860. {
  861. $answer_ary = array(
  862. 'answer_id' => $this->acp_gen_random_answer_id(),
  863. 'question_id' => $question_id,
  864. 'answer_sort' => 1,
  865. 'answer_text' => $answer,
  866. );
  867. $sql = 'INSERT INTO ' . CAPTCHA_SORTABLES_ANSWERS_TABLE . $db->sql_build_array('INSERT', $answer_ary);
  868. $db->sql_query($sql);
  869. }
  870. $cache->destroy('sql', CAPTCHA_SORTABLES_ANSWERS_TABLE);
  871. }
  872. /**
  873. * Delete a question.
  874. */
  875. function acp_delete_question($question_id)
  876. {
  877. global $db, $cache;
  878. $tables = array(CAPTCHA_SORTABLES_QUESTIONS_TABLE, CAPTCHA_SORTABLES_ANSWERS_TABLE);
  879. foreach ($tables as $table)
  880. {
  881. $sql = "DELETE FROM $table
  882. WHERE question_id = $question_id";
  883. $db->sql_query($sql);
  884. }
  885. $cache->destroy('sql', $tables);
  886. }
  887. /**
  888. * Check if the entered data can be inserted/used
  889. * param mixed $data : an array as created from acp_get_question_input or acp_get_question_data
  890. */
  891. function validate_input($question_data)
  892. {
  893. $langs = $this->get_languages();
  894. if (!isset($question_data['lang_iso']) ||
  895. !isset($question_data['question_text']) ||
  896. !isset($question_data['sort']) ||
  897. !isset($question_data['name_left']) ||
  898. !isset($question_data['name_right']) ||
  899. !isset($question_data['options_left']) ||
  900. !isset($question_data['options_right']))
  901. {
  902. return false;
  903. }
  904. if (!isset($langs[$question_data['lang_iso']]) ||
  905. !$question_data['question_text'] ||
  906. !sizeof($question_data['options_left']) ||
  907. !sizeof($question_data['options_right']))
  908. {
  909. return false;
  910. }
  911. return true;
  912. }
  913. /**
  914. * See if there is a question other than the one we have
  915. */
  916. function acp_is_last($question_id)
  917. {
  918. global $config, $db;
  919. if ($question_id)
  920. {
  921. $sql = 'SELECT question_id
  922. FROM ' . CAPTCHA_SORTABLES_QUESTIONS_TABLE . "
  923. WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'
  924. AND question_id <> " . (int) $question_id;
  925. $result = $db->sql_query_limit($sql, 1);
  926. $question = $db->sql_fetchrow($result);
  927. $db->sql_freeresult($result);
  928. if (!$question)
  929. {
  930. return true;
  931. }
  932. return false;
  933. }
  934. }
  935. /**
  936. * Get all answer_ids (used to check if a random answer_id is not already used)
  937. */
  938. function acp_get_answer_ids()
  939. {
  940. global $db;
  941. // If it's ready set, then stop here
  942. if ($this->answer_ids)
  943. {
  944. return $this->answer_ids;
  945. }
  946. // Get all answer ids
  947. $sql = 'SELECT answer_id
  948. FROM ' . CAPTCHA_SORTABLES_ANSWERS_TABLE;
  949. $result = $db->sql_query($sql);
  950. // Fill it up
  951. while ($row = $db->sql_fetchrow($result))
  952. {
  953. $this->answer_ids[] = $row['answer_id'];
  954. }
  955. $db->sql_freeresult($result);
  956. // When the answers table is empty, add 0 to prevent problems
  957. if (empty($this->answer_ids))
  958. {
  959. $this->answer_ids[] = 0;
  960. }
  961. return $this->answer_ids;
  962. }
  963. /**
  964. * Generate an unique answer_id
  965. */
  966. function acp_gen_random_answer_id()
  967. {
  968. global $db;
  969. // Get the already used ids
  970. $answer_ids = $this->acp_get_answer_ids();
  971. // Randomise
  972. $random = mt_rand(1, 100000);
  973. // If already used, repeat this function recursively
  974. if (in_array($random, $answer_ids))
  975. {
  976. return $this->acp_gen_random_answer_id();
  977. }
  978. return $random;
  979. }
  980. }
  981. ?>