PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/mews/captcha/src/Captcha.php

https://bitbucket.org/kiruthiga208/expertplus_enc
PHP | 450 lines | 212 code | 67 blank | 171 comment | 13 complexity | 522a3dbb49e92fbf3950ebc44659a7de MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, MIT
  1. <?php
  2. namespace Mews\Captcha;
  3. /**
  4. * Laravel 5 Captcha package
  5. *
  6. * @copyright Copyright (c) 2015 MeWebStudio
  7. * @version 2.x
  8. * @author Muharrem ERİN
  9. * @contact me@mewebstudio.com
  10. * @web http://www.mewebstudio.com
  11. * @date 2015-04-03
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. */
  14. use Exception;
  15. use Illuminate\Config\Repository;
  16. use Illuminate\Hashing\BcryptHasher as Hasher;
  17. use Illuminate\Filesystem\Filesystem;
  18. use Illuminate\Support\Str;
  19. use Intervention\Image\ImageManager;
  20. use Illuminate\Session\Store as Session;
  21. /**
  22. * Class Captcha
  23. * @package Mews\Captcha
  24. */
  25. class Captcha
  26. {
  27. /**
  28. * @var Filesystem
  29. */
  30. protected $files;
  31. /**
  32. * @var Repository
  33. */
  34. protected $config;
  35. /**
  36. * @var ImageManager
  37. */
  38. protected $imageManager;
  39. /**
  40. * @var Session
  41. */
  42. protected $session;
  43. /**
  44. * @var Hasher
  45. */
  46. protected $hasher;
  47. /**
  48. * @var Str
  49. */
  50. protected $str;
  51. /**
  52. * @var ImageManager->canvas
  53. */
  54. protected $canvas;
  55. /**
  56. * @var ImageManager->image
  57. */
  58. protected $image;
  59. /**
  60. * @var array
  61. */
  62. protected $backgrounds = [];
  63. /**
  64. * @var array
  65. */
  66. protected $fonts = [];
  67. /**
  68. * @var array
  69. */
  70. protected $fontColors = [];
  71. /**
  72. * @var int
  73. */
  74. protected $length = 5;
  75. /**
  76. * @var int
  77. */
  78. protected $width = 120;
  79. /**
  80. * @var int
  81. */
  82. protected $height = 36;
  83. /**
  84. * @var int
  85. */
  86. protected $angle = 15;
  87. /**
  88. * @var int
  89. */
  90. protected $lines = 3;
  91. /**
  92. * @var string
  93. */
  94. protected $characters;
  95. /**
  96. * @var string
  97. */
  98. protected $text;
  99. /**
  100. * @var int
  101. */
  102. protected $contrast = 0;
  103. /**
  104. * @var int
  105. */
  106. protected $quality = 90;
  107. /**
  108. * @var int
  109. */
  110. protected $sharpen = 0;
  111. /**
  112. * @var int
  113. */
  114. protected $blur = 0;
  115. /**
  116. * @var bool
  117. */
  118. protected $bgImage = true;
  119. /**
  120. * @var string
  121. */
  122. protected $bgColor = '#ffffff';
  123. /**
  124. * @var bool
  125. */
  126. protected $invert = false;
  127. /**
  128. * @var bool
  129. */
  130. protected $sensitive = false;
  131. /**
  132. * Constructor
  133. *
  134. * @param Filesystem $files
  135. * @param Repository $config
  136. * @param ImageManager $imageManager
  137. * @param Session $session
  138. * @param Hasher $hasher
  139. * @param Str $str
  140. * @throws Exception
  141. * @internal param Validator $validator
  142. */
  143. public function __construct(
  144. Filesystem $files,
  145. Repository $config,
  146. ImageManager $imageManager,
  147. Session $session,
  148. Hasher $hasher,
  149. Str $str
  150. )
  151. {
  152. $this->files = $files;
  153. $this->config = $config;
  154. $this->imageManager = $imageManager;
  155. $this->session = $session;
  156. $this->hasher = $hasher;
  157. $this->str = $str;
  158. $this->characters = config('captcha.characters','2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ');
  159. }
  160. /**
  161. * @param string $config
  162. * @return void
  163. */
  164. protected function configure($config)
  165. {
  166. if ($this->config->has('captcha.' . $config))
  167. {
  168. foreach($this->config->get('captcha.' . $config) as $key => $val)
  169. {
  170. $this->{$key} = $val;
  171. }
  172. }
  173. }
  174. /**
  175. * Create captcha image
  176. *
  177. * @param string $config
  178. * @return ImageManager->response
  179. */
  180. public function create($config = 'default')
  181. {
  182. $this->backgrounds = $this->files->files(__DIR__ . '/../assets/backgrounds');
  183. $this->fonts = $this->files->files(__DIR__ . '/../assets/fonts');
  184. if (app()->version() >= 5.5){
  185. $this->fonts = array_map(function($file) {
  186. return $file->getPathName();
  187. }, $this->fonts);
  188. }
  189. $this->fonts = array_values($this->fonts); //reset fonts array index
  190. $this->configure($config);
  191. $this->text = $this->generate();
  192. $this->canvas = $this->imageManager->canvas(
  193. $this->width,
  194. $this->height,
  195. $this->bgColor
  196. );
  197. if ($this->bgImage)
  198. {
  199. $this->image = $this->imageManager->make($this->background())->resize(
  200. $this->width,
  201. $this->height
  202. );
  203. $this->canvas->insert($this->image);
  204. }
  205. else
  206. {
  207. $this->image = $this->canvas;
  208. }
  209. if ($this->contrast != 0)
  210. {
  211. $this->image->contrast($this->contrast);
  212. }
  213. $this->text();
  214. $this->lines();
  215. if ($this->sharpen)
  216. {
  217. $this->image->sharpen($this->sharpen);
  218. }
  219. if ($this->invert)
  220. {
  221. $this->image->invert($this->invert);
  222. }
  223. if ($this->blur)
  224. {
  225. $this->image->blur($this->blur);
  226. }
  227. return $this->image->response('png', $this->quality);
  228. }
  229. /**
  230. * Image backgrounds
  231. *
  232. * @return string
  233. */
  234. protected function background()
  235. {
  236. return $this->backgrounds[rand(0, count($this->backgrounds) - 1)];
  237. }
  238. /**
  239. * Generate captcha text
  240. *
  241. * @return string
  242. */
  243. protected function generate()
  244. {
  245. $characters = str_split($this->characters);
  246. $bag = '';
  247. for($i = 0; $i < $this->length; $i++)
  248. {
  249. $bag .= $characters[rand(0, count($characters) - 1)];
  250. }
  251. $this->session->put('captcha', [
  252. 'sensitive' => $this->sensitive,
  253. 'key' => $this->hasher->make($this->sensitive ? $bag : $this->str->lower($bag))
  254. ]);
  255. return $bag;
  256. }
  257. /**
  258. * Writing captcha text
  259. */
  260. protected function text()
  261. {
  262. $marginTop = $this->image->height() / $this->length;
  263. $i = 0;
  264. foreach(str_split($this->text) as $char)
  265. {
  266. $marginLeft = ($i * $this->image->width() / $this->length);
  267. $this->image->text($char, $marginLeft, $marginTop, function($font) {
  268. $font->file($this->font());
  269. $font->size($this->fontSize());
  270. $font->color($this->fontColor());
  271. $font->align('left');
  272. $font->valign('top');
  273. $font->angle($this->angle());
  274. });
  275. $i++;
  276. }
  277. }
  278. /**
  279. * Image fonts
  280. *
  281. * @return string
  282. */
  283. protected function font()
  284. {
  285. return $this->fonts[rand(0, count($this->fonts) - 1)];
  286. }
  287. /**
  288. * Random font size
  289. *
  290. * @return integer
  291. */
  292. protected function fontSize()
  293. {
  294. return rand($this->image->height() - 10, $this->image->height());
  295. }
  296. /**
  297. * Random font color
  298. *
  299. * @return array
  300. */
  301. protected function fontColor()
  302. {
  303. if ( ! empty($this->fontColors))
  304. {
  305. $color = $this->fontColors[rand(0, count($this->fontColors) - 1)];
  306. }
  307. else
  308. {
  309. $color = [rand(0, 255), rand(0, 255), rand(0, 255)];
  310. }
  311. return $color;
  312. }
  313. /**
  314. * Angle
  315. *
  316. * @return int
  317. */
  318. protected function angle()
  319. {
  320. return rand((-1 * $this->angle), $this->angle);
  321. }
  322. /**
  323. * Random image lines
  324. *
  325. * @return \Intervention\Image\Image
  326. */
  327. protected function lines()
  328. {
  329. for($i = 0; $i <= $this->lines; $i++)
  330. {
  331. $this->image->line(
  332. rand(0, $this->image->width()) + $i * rand(0, $this->image->height()),
  333. rand(0, $this->image->height()),
  334. rand(0, $this->image->width()),
  335. rand(0, $this->image->height()),
  336. function ($draw) {
  337. $draw->color($this->fontColor());
  338. }
  339. );
  340. }
  341. return $this->image;
  342. }
  343. /**
  344. * Captcha check
  345. *
  346. * @param $value
  347. * @return bool
  348. */
  349. public function check($value)
  350. {
  351. if ( ! $this->session->has('captcha'))
  352. {
  353. return false;
  354. }
  355. $key = $this->session->get('captcha.key');
  356. if ( ! $this->session->get('captcha.sensitive'))
  357. {
  358. $value = $this->str->lower($value);
  359. }
  360. $this->session->remove('captcha');
  361. return $this->hasher->check($value, $key);
  362. }
  363. /**
  364. * Generate captcha image source
  365. *
  366. * @param null $config
  367. * @return string
  368. */
  369. public function src($config = null)
  370. {
  371. return url('captcha' . ($config ? '/' . $config : '/default')) . '?' . $this->str->random(8);
  372. }
  373. /**
  374. * Generate captcha image html tag
  375. *
  376. * @param null $config
  377. * @return string
  378. */
  379. public function img($config = null)
  380. {
  381. return '<img src="' . $this->src($config) . '" alt="captcha">';
  382. }
  383. }