Figlet.php 6.2 KB

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