Figlet.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Require Figlet class for rendering the text.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Text
  8. * @package Text_CAPTCHA
  9. * @author Aaron Wormus <wormus@php.net>
  10. * @author Christian Wenz <wenz@php.net>
  11. * @author Michael Cramer <michael@bigmichi1.de>
  12. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  13. * @link http://pear.php.net/package/Text_CAPTCHA
  14. */
  15. require_once 'Text/CAPTCHA/Driver/Base.php';
  16. require_once 'Text/Figlet.php';
  17. /**
  18. * Text_CAPTCHA_Driver_Figlet - Text_CAPTCHA driver Figlet based CAPTCHAs
  19. *
  20. * @category Text
  21. * @package Text_CAPTCHA
  22. * @author Aaron Wormus <wormus@php.net>
  23. * @author Christian Wenz <wenz@php.net>
  24. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  25. * @link http://pear.php.net/package/Text_CAPTCHA
  26. * @todo define an obfuscation algorithm
  27. */
  28. class Text_CAPTCHA_Driver_Figlet extends Text_CAPTCHA_Driver_Base
  29. {
  30. /**
  31. * Text_Password options.
  32. *
  33. * @var array
  34. */
  35. private $_textPasswordOptions;
  36. /**
  37. * Width of CAPTCHA
  38. *
  39. * @var int
  40. */
  41. private $_width;
  42. /**
  43. * Length of CAPTCHA
  44. *
  45. * @var int
  46. */
  47. private $_length;
  48. /**
  49. * Figlet font
  50. *
  51. * @var string
  52. */
  53. private $_font;
  54. /**
  55. * Figlet font
  56. *
  57. * @var array
  58. */
  59. private $_style = array();
  60. /**
  61. * Output Format
  62. *
  63. * @var string
  64. */
  65. private $_output;
  66. /**
  67. * init function
  68. *
  69. * Initializes the new Text_CAPTCHA_Driver_Figlet object and creates a GD image
  70. *
  71. * @param array $options CAPTCHA options
  72. *
  73. * @return void
  74. * @throws Text_CAPTCHA_Exception when no options are given
  75. */
  76. public function initDriver($options = array())
  77. {
  78. if (!empty($options['output'])) {
  79. $this->_output = (string)$options['output'];
  80. } else {
  81. $this->_output = 'html';
  82. }
  83. if (isset($options['width']) && $options['width']) {
  84. $this->_width = (int)$options['width'];
  85. } else {
  86. $this->_width = 200;
  87. }
  88. if (!empty($options['length'])) {
  89. $this->_length = $options['length'];
  90. } else {
  91. $this->_length = 6;
  92. }
  93. if (!isset($options['phrase']) || empty($options['phrase'])) {
  94. $phraseOptions = (isset($options['phraseOptions'])
  95. && is_array($options['phraseOptions']))
  96. ? $options['phraseOptions'] : array();
  97. $this->_textPasswordOptions = $phraseOptions;
  98. } else {
  99. $this->setPhrase($options['phrase']);
  100. }
  101. if (!empty($options['style'])
  102. && is_array($options['style'])
  103. ) {
  104. $this->_style = $options['style'];
  105. }
  106. if (empty($this->_style['padding'])) {
  107. $this->_style['padding'] = '5px';
  108. }
  109. if (!empty($options['font_file'])) {
  110. if (is_array($options['font_file'])) {
  111. $arr = $options['font_file'];
  112. $this->_font = $arr[array_rand($arr)];
  113. } else {
  114. $this->_font = $options['font_file'];
  115. }
  116. }
  117. }
  118. /**
  119. * Create the passphrase.
  120. *
  121. * @return string
  122. */
  123. public function createPhrase()
  124. {
  125. $options = $this->_textPasswordOptions;
  126. $textPassword = new Text_Password();
  127. if (!is_array($options) || count($options) === 0) {
  128. $this->setPhrase($textPassword->create($this->_length));
  129. } else {
  130. if (count($options) === 1) {
  131. $this->setPhrase($textPassword->create($this->_length, $options[0]));
  132. } else {
  133. $this->setPhrase(
  134. $textPassword->create($this->_length, $options[0], $options[1])
  135. );
  136. }
  137. }
  138. }
  139. /**
  140. * Create CAPTCHA image.
  141. *
  142. * This method creates a CAPTCHA image.
  143. *
  144. * @return void on error
  145. * @throws Text_CAPTCHA_Exception when loading font fails
  146. */
  147. public function createCAPTCHA()
  148. {
  149. $pear = new PEAR();
  150. $figlet = new Text_Figlet();
  151. if ($pear->isError($figlet->loadFont($this->_font))) {
  152. throw new Text_CAPTCHA_Exception('Error loading Text_Figlet font');
  153. }
  154. $outputString = $figlet->lineEcho($this->getPhrase());
  155. switch ($this->_output) {
  156. case 'text':
  157. $this->setCaptcha($outputString);
  158. break;
  159. case 'html':
  160. $this->setCaptcha($this->_getCAPTCHAAsHTML($outputString));
  161. break;
  162. case 'javascript':
  163. $this->setCaptcha($this->_getCAPTCHAAsJavascript($outputString));
  164. break;
  165. default:
  166. throw new Text_CAPTCHA_Exception('Invalid output option given');
  167. }
  168. }
  169. /**
  170. * Return CAPTCHA as HTML.
  171. *
  172. * This method returns the CAPTCHA as HTML.
  173. *
  174. * @param string $figletOutput output string from Figlet.
  175. *
  176. * @return string HTML Figlet image or PEAR error
  177. */
  178. private function _getCAPTCHAAsHTML($figletOutput)
  179. {
  180. $charWidth = strpos($figletOutput, "\n");
  181. $data = str_replace("\n", '<br />', $figletOutput);
  182. $textSize = ($this->_width / $charWidth) * 1.4;
  183. $cssOutput = "";
  184. foreach ($this->_style as $key => $value) {
  185. $cssOutput .= "$key: $value;";
  186. }
  187. $htmlOutput = '<div style="font-family: courier;
  188. font-size: ' . $textSize . 'px;
  189. width:' . $this->_width . 'px;
  190. text-align:center;">';
  191. $htmlOutput .= '<div style="' . $cssOutput . 'margin:0px;">
  192. <pre style="padding: 0px; margin: 0px;">' . $data . '</pre></div></div>';
  193. return $htmlOutput;
  194. }
  195. /**
  196. * Return CAPTCHA as Javascript version of HTML.
  197. *
  198. * This method returns the CAPTCHA as a Javascript string.
  199. * I'm not exactly sure what the point of doing this would be.
  200. *
  201. * @param string $figletOutput output string from Figlet.
  202. *
  203. * @return string javascript string or PEAR error
  204. */
  205. private function _getCAPTCHAAsJavascript($figletOutput)
  206. {
  207. $obfusData = rawurlencode($figletOutput);
  208. $javascript = "<script language=\"javascript\">
  209. document.write(unescape(\"$obfusData\" ) );
  210. </script>";
  211. return $javascript;
  212. }
  213. }