captcha.install 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the CAPTCHA module.
  5. */
  6. /**
  7. * Implementation of hook_schema().
  8. */
  9. function captcha_schema() {
  10. // Table for positions and types of the challenges.
  11. $schema['captcha_points'] = array(
  12. 'description' => 'This table describes which challenges should be added to which forms.',
  13. 'export' => array(
  14. 'key' => 'form_id',
  15. 'identifier' => 'captcha',
  16. 'default hook' => 'captcha_default_points', // Function hook name.
  17. 'status' => 'mark_status',
  18. 'api' => array(
  19. 'owner' => 'captcha',
  20. 'api' => 'captcha', // Base name for api include files.
  21. 'minimum_version' => 1,
  22. 'current_version' => 1,
  23. ),
  24. ),
  25. 'fields' => array(
  26. 'form_id' => array(
  27. 'description' => 'The form_id of the form to add a CAPTCHA to.',
  28. 'type' => 'varchar',
  29. 'length' => 128,
  30. 'not null' => TRUE,
  31. 'default' => '',
  32. ),
  33. 'module' => array(
  34. 'description' => 'The module that provides the challenge.',
  35. 'type' => 'varchar',
  36. 'length' => 64,
  37. ),
  38. 'captcha_type' => array(
  39. 'description' => 'The challenge type to use.',
  40. 'type' => 'varchar',
  41. 'length' => 64,
  42. ),
  43. ),
  44. 'primary key' => array('form_id'),
  45. );
  46. // Table for the CAPTCHA sessions.
  47. $schema['captcha_sessions'] = array(
  48. 'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
  49. 'fields' => array(
  50. 'csid' => array(
  51. 'description' => 'CAPTCHA session ID.',
  52. 'type' => 'serial',
  53. 'not null' => TRUE,
  54. ),
  55. 'token' => array(
  56. 'description' => 'One time CAPTCHA token.',
  57. 'type' => 'varchar',
  58. 'length' => 64,
  59. 'not null' => FALSE,
  60. ),
  61. 'uid' => array(
  62. 'description' => "User's {users}.uid.",
  63. 'type' => 'int',
  64. 'not null' => TRUE,
  65. 'default' => 0,
  66. ),
  67. 'sid' => array(
  68. 'description' => "Session ID of the user.",
  69. 'type' => 'varchar',
  70. 'length' => 128,
  71. 'not null' => TRUE,
  72. 'default' => '',
  73. ),
  74. 'ip_address' => array(
  75. 'description' => 'IP address of the visitor.',
  76. 'type' => 'varchar',
  77. 'length' => 128,
  78. 'not null' => FALSE,
  79. ),
  80. 'timestamp' => array(
  81. 'description' => 'A Unix timestamp indicating when the challenge was generated.',
  82. 'type' => 'int',
  83. 'not null' => TRUE,
  84. 'default' => 0,
  85. ),
  86. 'form_id' => array(
  87. 'description' => 'The form_id of the form where the CAPTCHA is added to.',
  88. 'type' => 'varchar',
  89. 'length' => 128,
  90. 'not null' => TRUE,
  91. ),
  92. 'solution' => array(
  93. 'description' => 'Solution of the challenge.',
  94. 'type' => 'varchar',
  95. 'length' => 128,
  96. 'not null' => TRUE,
  97. 'default' => '',
  98. ),
  99. 'status' => array(
  100. 'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
  101. 'type' => 'int',
  102. 'not null' => TRUE,
  103. 'default' => 0,
  104. ),
  105. 'attempts' => array(
  106. 'description' => 'The number of attempts.',
  107. 'type' => 'int',
  108. 'not null' => TRUE,
  109. 'default' => 0,
  110. ),
  111. ),
  112. 'primary key' => array('csid'),
  113. 'indexes' => array(
  114. 'csid_ip' => array('csid', 'ip_address'),
  115. ),
  116. );
  117. return $schema;
  118. }
  119. /**
  120. * Implements of hook_requirements().
  121. */
  122. function captcha_requirements($phase) {
  123. $requirements = array();
  124. $t = get_t();
  125. if ($phase == 'runtime' && variable_get('captcha_enable_stats', FALSE)) {
  126. // Show the wrong response counter in the status report.
  127. $requirements['captcha_wrong_response_counter'] = array(
  128. 'title' => $t('CAPTCHA'),
  129. 'value' => format_plural(
  130. variable_get('captcha_wrong_response_counter', 0),
  131. 'Already 1 blocked form submission',
  132. 'Already @count blocked form submissions'
  133. ),
  134. 'severity' => REQUIREMENT_INFO,
  135. );
  136. }
  137. return $requirements;
  138. }
  139. /**
  140. * Implements of hook_install().
  141. */
  142. function captcha_install() {
  143. $t = get_t();
  144. // Be friendly to your users: what to do after install?
  145. drupal_set_message($t('You can now <a href="!captcha_admin">configure the CAPTCHA module</a> for your site.',
  146. array('!captcha_admin' => url('admin/config/people/captcha'))), 'status');
  147. // Explain to users that page caching may be disabled.
  148. if (variable_get('cache', 0) != 0) {
  149. drupal_set_message($t('Note that the CAPTCHA module disables <a href="!performance_admin">page caching</a> of pages that include a CAPTCHA challenge.',
  150. array('!performance_admin' => url('admin/config/development/performance'))), 'warning');
  151. }
  152. }
  153. /**
  154. * Implements of hook_uninstall().
  155. */
  156. function captcha_uninstall() {
  157. drupal_uninstall_schema('captcha');
  158. db_query("DELETE FROM {variable} WHERE name LIKE 'captcha_%'");
  159. cache_clear_all('variables', 'cache');
  160. }
  161. /**
  162. * Implements of hook_update_N().
  163. */
  164. function captcha_update_6200() {
  165. $items = array();
  166. // Table for the CAPTCHA sessions.
  167. $schema['captcha_sessions'] = array(
  168. 'description' => 'Stores the data about CAPTCHA sessions (solution, IP address, timestamp, ...).',
  169. 'fields' => array(
  170. 'csid' => array(
  171. 'description' => 'CAPTCHA session ID.',
  172. 'type' => 'serial',
  173. 'not null' => TRUE,
  174. ),
  175. 'uid' => array(
  176. 'description' => "User's {users}.uid.",
  177. 'type' => 'int',
  178. 'not null' => TRUE,
  179. 'default' => 0,
  180. ),
  181. 'sid' => array(
  182. 'description' => "Session ID of the user.",
  183. 'type' => 'varchar',
  184. 'length' => 64,
  185. 'not null' => TRUE,
  186. 'default' => '',
  187. ),
  188. 'ip_address' => array(
  189. 'description' => 'IP address of the visitor.',
  190. 'type' => 'varchar',
  191. 'length' => 128,
  192. 'not null' => FALSE,
  193. ),
  194. 'timestamp' => array(
  195. 'description' => 'A Unix timestamp indicating when the challenge was generated.',
  196. 'type' => 'int',
  197. 'not null' => TRUE,
  198. 'default' => 0,
  199. ),
  200. 'form_id' => array(
  201. 'description' => 'The form_id of the form where the CAPTCHA is added to.',
  202. 'type' => 'varchar',
  203. 'length' => 128,
  204. 'not null' => TRUE,
  205. ),
  206. 'solution' => array(
  207. 'description' => 'Solution of the challenge.',
  208. 'type' => 'varchar',
  209. 'length' => 128,
  210. 'not null' => TRUE,
  211. 'default' => '',
  212. ),
  213. 'status' => array(
  214. 'description' => 'Status of the CAPTCHA session (unsolved, solved, ...)',
  215. 'type' => 'int',
  216. 'not null' => TRUE,
  217. 'default' => 0,
  218. ),
  219. 'attempts' => array(
  220. 'description' => 'The number of attempts.',
  221. 'type' => 'int',
  222. 'not null' => TRUE,
  223. 'default' => 0,
  224. ),
  225. ),
  226. 'primary key' => array('csid'),
  227. 'indexes' => array(
  228. 'csid_ip' => array('csid', 'ip_address'),
  229. ),
  230. );
  231. db_create_table($items, 'captcha_sessions', $schema['captcha_sessions']);
  232. return $items;
  233. }
  234. /**
  235. * Implements of hook_update_N().
  236. *
  237. * Change the captcha points with the old text CAPTCHA, which was
  238. * removed from the 6.x-2.x branch, to the simple math CAPTCHA.
  239. */
  240. function captcha_update_6201() {
  241. $items = array();
  242. $items[] = update_sql("UPDATE {captcha_points} SET module = 'captcha', type = 'Math' WHERE module = 'text_captcha' AND type = 'Text';");
  243. return $items;
  244. }
  245. /**
  246. * Implements of hook_update_N().
  247. *
  248. * Add a CAPTCHA token column to captcha_sessions table.
  249. */
  250. function captcha_update_6202() {
  251. $ret = array();
  252. db_add_column($ret, 'captcha_sessions', 'token', 'varchar(64)');
  253. return $ret;
  254. }
  255. /**
  256. * Implements of hook_update_N().
  257. *
  258. * Rename the type field to captcha_type in captcha_points.
  259. */
  260. function captcha_update_6203() {
  261. $ret = array();
  262. db_change_field($ret, 'captcha_points', 'type', 'captcha_type', array('type' => 'varchar', 'length' => 64));
  263. return $ret;
  264. }
  265. /**
  266. * Migrate form configuration for changed form ids in Drupal 7.
  267. */
  268. function captcha_update_7000() {
  269. // 'user_register' became 'user_register_form'.
  270. db_update('captcha_points')
  271. ->fields(array('form_id' => 'user_register_form'))
  272. ->condition('form_id', 'user_register')
  273. ->execute();
  274. // 'contact_mail_page' became 'contact_site_form'.
  275. db_update('captcha_points')
  276. ->fields(array('form_id' => 'contact_site_form'))
  277. ->condition('form_id', 'contact_mail_page')
  278. ->execute();
  279. // 'contact_mail_user' became 'contact_personal_form'.
  280. db_update('captcha_points')
  281. ->fields(array('form_id' => 'contact_personal_form'))
  282. ->condition('form_id', 'contact_mail_user')
  283. ->execute();
  284. // The D6-style comment_form form_id is split per node type
  285. // in D7: comment_node_{type}_form, e.g. comment_node_page_form.
  286. // Get the current settings for 'comment_form'.
  287. $captcha_point = db_query(
  288. "SELECT * FROM {captcha_points} WHERE form_id = :comment_form_id",
  289. array(':comment_form_id' => 'comment_form')
  290. )->fetchObject();
  291. if ($captcha_point !== FALSE) {
  292. // Create entries for D7-style node form IDs.
  293. $module = $captcha_point->module;
  294. $captcha_type = $captcha_point->captcha_type;
  295. foreach (node_type_get_names() as $type => $name) {
  296. $form_id = 'comment_node_' . $type . '_form';
  297. db_insert('captcha_points')
  298. ->fields(array(
  299. 'form_id' => $form_id,
  300. 'module' => $module,
  301. 'captcha_type' => $captcha_type,
  302. ))
  303. ->execute();
  304. }
  305. // Delete outdated entry.
  306. db_delete('captcha_points')
  307. ->condition('form_id', 'comment_form')
  308. ->execute();
  309. }
  310. }
  311. /**
  312. * Increase the Session Id field size.
  313. */
  314. function captcha_update_7001() {
  315. $schema = captcha_schema();
  316. db_change_field('captcha_sessions', 'sid', 'sid', array(
  317. 'description' => "Session ID of the user.",
  318. 'type' => 'varchar',
  319. 'length' => 128,
  320. 'not null' => TRUE,
  321. 'default' => '',
  322. ));
  323. }