PageRenderTime 26ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/captcha_helper.php

https://github.com/coderkid/No-CMS
PHP | 228 lines | 130 code | 26 blank | 72 comment | 15 complexity | e76d958f727a544f522206def9fcbceb MD5 | raw file
  1<?php
  2/**
  3 * CodeIgniter
  4 *
  5 * An open source application development framework for PHP 5.2.4 or newer
  6 *
  7 * NOTICE OF LICENSE
  8 *
  9 * Licensed under the Open Software License version 3.0
 10 *
 11 * This source file is subject to the Open Software License (OSL 3.0) that is
 12 * bundled with this package in the files license.txt / license.rst.  It is
 13 * also available through the world wide web at this URL:
 14 * http://opensource.org/licenses/OSL-3.0
 15 * If you did not receive a copy of the license and are unable to obtain it
 16 * through the world wide web, please send an email to
 17 * licensing@ellislab.com so we can send you a copy immediately.
 18 *
 19 * @package		CodeIgniter
 20 * @author		EllisLab Dev Team
 21 * @copyright	Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
 22 * @license		http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 23 * @link		http://codeigniter.com
 24 * @since		Version 1.0
 25 * @filesource
 26 */
 27defined('BASEPATH') OR exit('No direct script access allowed');
 28
 29/**
 30 * CodeIgniter CAPTCHA Helper
 31 *
 32 * @package		CodeIgniter
 33 * @subpackage	Helpers
 34 * @category	Helpers
 35 * @author		EllisLab Dev Team
 36 * @link		http://codeigniter.com/user_guide/helpers/captcha_helper.html
 37 */
 38
 39// ------------------------------------------------------------------------
 40
 41if ( ! function_exists('create_captcha'))
 42{
 43	/**
 44	 * Create CAPTCHA
 45	 *
 46	 * @param	array	$data		data for the CAPTCHA
 47	 * @param	string	$img_path	path to create the image in
 48	 * @param	string	$img_url	URL to the CAPTCHA image folder
 49	 * @param	string	$font_path	server path to font
 50	 * @return	string
 51	 */
 52	function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
 53	{
 54		$defaults = array(
 55			'word'		=> '',
 56			'img_path'	=> '',
 57			'img_url'	=> '',
 58			'img_width'	=> '150',
 59			'img_height'	=> '30',
 60			'font_path'	=> '',
 61			'expiration'	=> 7200,
 62			'word_length'	=> 8,
 63			'pool'		=> '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
 64			'colors'	=> array(
 65				'background'	=> array(255,255,255),
 66				'border'	=> array(153,102,102),
 67				'text'		=> array(204,153,153),
 68				'grid'		=> array(255,182,182)
 69			)
 70		);
 71
 72		foreach ($defaults as $key => $val)
 73		{
 74			if ( ! is_array($data) && empty($$key))
 75			{
 76				$$key = $val;
 77			}
 78			else
 79			{
 80				$$key = isset($data[$key]) ? $data[$key] : $val;
 81			}
 82		}
 83
 84		if ($img_path === '' OR $img_url === ''
 85			OR ! @is_dir($img_path) OR ! is_writeable($img_path)
 86			OR ! extension_loaded('gd'))
 87		{
 88			return FALSE;
 89		}
 90
 91		// -----------------------------------
 92		// Remove old images
 93		// -----------------------------------
 94
 95		$now = microtime(TRUE);
 96
 97		$current_dir = @opendir($img_path);
 98		while ($filename = @readdir($current_dir))
 99		{
100			if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now)
101			{
102				@unlink($img_path.$filename);
103			}
104		}
105
106		@closedir($current_dir);
107
108		// -----------------------------------
109		// Do we have a "word" yet?
110		// -----------------------------------
111
112		if (empty($word))
113		{
114			$word = '';
115			for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++)
116			{
117				$word .= $pool[mt_rand(0, $mt_rand_max)];
118			}
119		}
120		elseif ( ! is_string($word))
121		{
122			$word = (string) $word;
123		}
124
125		// -----------------------------------
126		// Determine angle and position
127		// -----------------------------------
128		$length	= strlen($word);
129		$angle	= ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
130		$x_axis	= mt_rand(6, (360/$length)-16);
131		$y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
132
133		// Create image
134		// PHP.net recommends imagecreatetruecolor(), but it isn't always available
135		$im = function_exists('imagecreatetruecolor')
136			? imagecreatetruecolor($img_width, $img_height)
137			: imagecreate($img_width, $img_height);
138
139		// -----------------------------------
140		//  Assign colors
141		// ----------------------------------
142
143		is_array($colors) OR $colors = $defaults['colors'];
144
145		foreach (array_keys($defaults['colors']) as $key)
146		{
147			// Check for a possible missing value
148			is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key];
149			$colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
150		}
151
152		// Create the rectangle
153		ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);
154
155		// -----------------------------------
156		//  Create the spiral pattern
157		// -----------------------------------
158		$theta		= 1;
159		$thetac		= 7;
160		$radius		= 16;
161		$circles	= 20;
162		$points		= 32;
163
164		for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
165		{
166			$theta += $thetac;
167			$rad = $radius * ($i / $points);
168			$x = ($rad * cos($theta)) + $x_axis;
169			$y = ($rad * sin($theta)) + $y_axis;
170			$theta += $thetac;
171			$rad1 = $radius * (($i + 1) / $points);
172			$x1 = ($rad1 * cos($theta)) + $x_axis;
173			$y1 = ($rad1 * sin($theta)) + $y_axis;
174			imageline($im, $x, $y, $x1, $y1, $colors['grid']);
175			$theta -= $thetac;
176		}
177
178		// -----------------------------------
179		//  Write the text
180		// -----------------------------------
181
182		$use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext'));
183		if ($use_font === FALSE)
184		{
185			$font_size = 5;
186			$x = mt_rand(0, $img_width / ($length / 3));
187			$y = 0;
188		}
189		else
190		{
191			$font_size = 16;
192			$x = mt_rand(0, $img_width / ($length / 1.5));
193			$y = $font_size + 2;
194		}
195
196		for ($i = 0; $i < $length; $i++)
197		{
198			if ($use_font === FALSE)
199			{
200				$y = mt_rand(0 , $img_height / 2);
201				imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']);
202				$x += ($font_size * 2);
203			}
204			else
205			{
206				$y = mt_rand($img_height / 2, $img_height - 3);
207				imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
208				$x += $font_size;
209			}
210		}
211
212		// Create the border
213		imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);
214
215		// -----------------------------------
216		//  Generate the image
217		// -----------------------------------
218		$img_filename = $now.'.jpg';
219		ImageJPEG($im, $img_path.$img_filename);
220		$img = '<img src="'.$img_url.$img_filename.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';
221		ImageDestroy($im);
222
223		return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);
224	}
225}
226
227/* End of file captcha_helper.php */
228/* Location: ./system/helpers/captcha_helper.php */