SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/CaptchaController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Controllers;
  15.  
  16. use Sprout\Helpers\Security;
  17. use Sprout\Helpers\Session;
  18. use Sprout\Helpers\View;
  19.  
  20.  
  21. /**
  22.  * Used for generating CAPTCHA images and explanatory text
  23.  */
  24. class CaptchaController extends Controller
  25. {
  26.  
  27. /**
  28.   * Render a CAPTCHA image
  29.   * See https://en.wikipedia.org/wiki/CAPTCHA for information about CAPTCHAs
  30.   * @param int $num Number used to differentiate between multiple CAPTCHA codes held in session
  31.   * @return void Outputs image content after setting the appropriate content-type header
  32.   */
  33. public function image($num)
  34. {
  35. Session::instance();
  36.  
  37. $num = (int) $num;
  38.  
  39. $captcha_code = Security::randStr(mt_rand(8,10), 'QWERTYUOPASDFGHJKLZXCVBNMqwertyupasdfghjkzxcvbnm');
  40.  
  41. $_SESSION['captcha'][$num] = $captcha_code;
  42.  
  43.  
  44. $width = 200;
  45. $height = 50;
  46.  
  47. $my_image = imagecreatetruecolor($width, $height);
  48.  
  49. imagefill($my_image, 0, 0, 0x000000);
  50.  
  51. // add noise
  52. for ($c = 0; $c < 10; $c++) {
  53. $x = mt_rand(-20, $width + 20);
  54. $y = mt_rand(-20, $height + 20);
  55. $x2 = mt_rand(-20, $width + 20);
  56. $y2 = mt_rand(-20, $height + 20);
  57. imageline($my_image, $x, $y, $x2, $y2, 0x333333);
  58. }
  59.  
  60. // Background text
  61. for ($i = 0; $i < 4; $i++) {
  62. $x = mt_rand(5, 100);
  63. $y = mt_rand(10, 40);
  64. $angle = mt_rand(-30, 30);
  65. imagettftext ($my_image, 12, $angle, $x, $y, 0x777777, DOCROOT . 'media/fonts/DejaVuSans.ttf', Security::randStr(10));
  66. }
  67.  
  68. // Real text
  69. $x = mt_rand(15, 35);
  70. $y = mt_rand(30, 35);
  71. $angle = mt_rand(-10, 10);
  72. imagettftext ($my_image, 14, $angle, $x, $y, 0xFFFFFF, DOCROOT . 'media/fonts/DejaVuSans.ttf', $captcha_code);
  73.  
  74.  
  75. header('Content-type: image/jpeg');
  76. imagejpeg($my_image);
  77.  
  78. imagedestroy($my_image);
  79. }
  80.  
  81.  
  82.  
  83. /**
  84.   * Output info about CAPTCHAs; should be displayed in a popup
  85.   * @return void Outputs HTML directly
  86.   */
  87. public function about()
  88. {
  89. $text = '<p>A captcha is a puzzle which is easy for humans to solve, but hard for computers to solve.</p>';
  90. $text .= '<p>They are designed to stop spam-bots from attacking the website, because the computer program
  91. which the spam-bot is running cannot solve the captcha.</p>';
  92. $text .= '<p>When entering the captcha, letter case is not important.</p>';
  93. $text .= '<p>If you cannot read the captcha, you can generate a new one by clicking on the "Refresh" icon.</p>';
  94.  
  95. $page_view = new View('skin/popup');
  96. $page_view->page_title = 'What is a captcha?';
  97. $page_view->main_content = $text;
  98. echo $page_view->render();
  99. }
  100.  
  101. }
  102.