Subversion Repositories Applications.gtt

Rev

Rev 60 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
60 jpm 1
<?php
2
/*
3
 * This work is hereby released into the Public Domain.
4
 * To view a copy of the public domain dedication,
5
 * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
6
 * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
7
 *
8
 */
9
 
10
require_once dirname(__FILE__)."/Image.class.php";
11
 
12
/**
13
 * AntiSpam
14
 * String printed on the images are case insensitive.
15
 *
16
 * @package Artichow
17
 */
18
class awAntiSpam extends awImage {
19
 
20
	/**
21
	 * Anti-spam string
22
	 *
23
	 * @var string
24
	 */
25
	protected $string;
26
 
27
	/**
28
	 * Noise intensity
29
	 *
30
	 * @var int
31
	 */
32
	protected $noise = 0;
33
 
34
	/**
35
	 * Construct a new awAntiSpam image
36
	 *
37
	 * @param string $string A string to display
38
	 */
39
	public function __construct($string = '') {
40
 
41
		parent::__construct();
42
 
43
		$this->string = (string)$string;
44
 
45
	}
46
 
47
	/**
48
	 * Create a random string
49
	 *
50
	 * @param int $length String length
51
	 * @return string String created
52
	 */
53
	public function setRand($length) {
54
 
55
		$length = (int)$length;
56
 
57
		$this->string = '';
58
 
59
		$letters = 'aAbBCDeEFgGhHJKLmMnNpPqQRsStTuVwWXYZz2345679';
60
		$number = strlen($letters);
61
 
62
		for($i = 0; $i < $length; $i++) {
63
			$this->string .= $letters{mt_rand(0, $number - 1)};
64
		}
65
 
66
		return $this->string;
67
 
68
	}
69
 
70
	/**
71
	 * Set noise on image
72
	 *
73
	 * @param int $nois Noise intensity (from 0 to 10)
74
	 */
75
	public function setNoise($noise) {
76
		if($noise < 0) {
77
			$noise = 0;
78
		}
79
		if($noise > 10) {
80
			$noise = 10;
81
		}
82
		$this->noise = (int)$noise;
83
	}
84
 
85
	/**
86
	 * Save string value in session
87
	 * You can use check() to verify the value later
88
	 *
89
	 * @param string $qName A name that identify the anti-spam image
90
	 */
91
	public function save($qName) {
92
		$this->session();
93
		$session = 'artichow_'.(string)$qName;
94
		$_SESSION[$session] = $this->string;
95
	}
96
 
97
	/**
98
	 * Verify user entry
99
	 *
100
	 * @param string $qName A name that identify the anti-spam image
101
	 * @param string $value User-defined value
102
	 * @param bool $case TRUE for case insensitive check, FALSE for case sensitive check ? (default to TRUE)
103
	 * @return bool TRUE if the value is correct, FALSE otherwise
104
	 */
105
	public function check($qName, $value, $case = TRUE) {
106
 
107
		$this->session();
108
 
109
		$session = 'artichow_'.(string)$qName;
110
 
111
		return (
112
			array_key_exists($session, $_SESSION) === TRUE and
113
			$case ?
114
				(strtolower($_SESSION[$session]) === strtolower((string)$value)) :
115
				($_SESSION[$session] === (string)$value)
116
		);
117
 
118
	}
119
 
120
	/**
121
	 * Draw image
122
	 */
123
	public function draw() {
124
 
125
		$fonts = array(
126
			'Tuffy',
127
			'TuffyBold',
128
			'TuffyItalic',
129
			'TuffyBoldItalic'
130
		);
131
 
132
		$sizes = array(12, 12.5, 13, 13.5, 14, 15, 16, 17, 18, 19);
133
 
134
		$widths = array();
135
		$heights = array();
136
		$texts = array();
137
 
138
		// Set up a temporary driver to allow font size calculations...
139
		$this->setSize(10, 10);
140
		$driver = $this->getDriver();
141
 
142
		for($i = 0; $i < strlen($this->string); $i++) {
143
 
144
			$fontKey = array_rand($fonts);
145
			$sizeKey = array_rand($sizes);
146
 
147
			$font = new awTTFFont(
148
				$fonts[$fontKey], $sizes[$sizeKey]
149
			);
150
 
151
			$text = new awText(
152
				$this->string{$i},
153
				$font,
154
				NULL,
155
				mt_rand(-15, 15)
156
			);
157
 
158
			$widths[] = $driver->getTextWidth($text);
159
			$heights[] = $driver->getTextHeight($text);
160
			$texts[] = $text;
161
 
162
		}
163
 
164
		// ... and get rid of it.
165
		$this->driver = NULL;
166
 
167
		$width = array_sum($widths);
168
		$height = array_max($heights);
169
 
170
		$totalWidth = $width + 10 + count($texts) * 10;
171
		$totalHeight = $height + 20;
172
 
173
		$this->setSize($totalWidth, $totalHeight);
174
 
175
		$this->create();
176
 
177
		for($i = 0; $i < strlen($this->string); $i++) {
178
 
179
			$this->driver->string(
180
				$texts[$i],
181
				new awPoint(
182
					5 + array_sum(array_slice($widths, 0, $i)) + $widths[$i] / 2 + $i * 10,
183
					10 + ($height - $heights[$i]) / 2
184
				)
185
			);
186
 
187
		}
188
 
189
		$this->drawNoise($totalWidth, $totalHeight);
190
 
191
		$this->send();
192
 
193
	}
194
 
195
	protected function drawNoise($width, $height) {
196
 
197
		$points = $this->noise * 30;
198
		$color = new awColor(0, 0, 0);
199
 
200
		for($i = 0; $i < $points; $i++) {
201
			$this->driver->point(
202
				$color,
203
				new awPoint(
204
					mt_rand(0, $width),
205
					mt_rand(0, $height)
206
				)
207
			);
208
		}
209
 
210
	}
211
 
212
	protected function session() {
213
 
214
		// Start session if needed
215
		if(!session_id()) {
216
			session_start();
217
		}
218
 
219
	}
220
 
221
}
222
 
223
registerClass('AntiSpam');
224
?>