PageRenderTime 41ms CodeModel.GetById 16ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/attach_mod/includes/functions_filetypes.php

http://torrentpier2.googlecode.com/
PHP | 402 lines | 287 code | 74 blank | 41 comment | 98 complexity | 267fad396cc5629f0da5c12e4381a3cd MD5 | raw file
  1<?php
  2
  3/**
  4* All Attachment Functions needed to determine Special Files/Dimensions
  5*/
  6
  7/**
  8* Read Long Int (4 Bytes) from File
  9*/
 10function read_longint($fp)
 11{
 12	$data = fread($fp, 4);
 13
 14	$value = ord($data[0]) + (ord($data[1])<<8)+(ord($data[2])<<16)+(ord($data[3])<<24);
 15	if ($value >= 4294967294)
 16	{
 17		$value -= 4294967296;
 18	}
 19
 20	return $value;
 21}
 22
 23/**
 24* Read Word (2 Bytes) from File - Note: It's an Intel Word
 25*/
 26function read_word($fp)
 27{
 28	$data = fread($fp, 2);
 29
 30	$value = ord($data[1]) * 256 + ord($data[0]);
 31
 32	return $value;
 33}
 34
 35/**
 36* Read Byte
 37*/
 38function read_byte($fp)
 39{
 40	$data = fread($fp, 1);
 41
 42	$value = ord($data);
 43
 44	return $value;
 45}
 46
 47/**
 48* Get Image Dimensions
 49*/
 50function image_getdimension($file)
 51{
 52
 53	$size = @getimagesize($file);
 54
 55	if ($size[0] != 0 || $size[1] != 0)
 56	{
 57		return $size;
 58	}
 59
 60	// Try to get the Dimension manually, depending on the mimetype
 61	$fp = @fopen($file, 'rb');
 62	if (!$fp)
 63	{
 64		return $size;
 65	}
 66
 67	$error = FALSE;
 68
 69	// BMP - IMAGE
 70
 71	$tmp_str = fread($fp, 2);
 72	if ($tmp_str == 'BM')
 73	{
 74		$length = read_longint($fp);
 75
 76		if ($length <= 6)
 77		{
 78			$error = true;
 79		}
 80
 81		if (!$error)
 82		{
 83			$i = read_longint($fp);
 84			if ( $i != 0)
 85			{
 86				$error = true;
 87			}
 88		}
 89
 90		if (!$error)
 91		{
 92			$i = read_longint($fp);
 93
 94			if ($i != 0x3E && $i != 0x76 && $i != 0x436 && $i != 0x36)
 95			{
 96				$error = true;
 97			}
 98		}
 99
100		if (!$error)
101		{
102			$tmp_str = fread($fp, 4);
103			$width = read_longint($fp);
104			$height = read_longint($fp);
105
106			if ($width > 3000 || $height > 3000)
107			{
108				$error = true;
109			}
110		}
111	}
112	else
113	{
114		$error = true;
115	}
116
117	if (!$error)
118	{
119		fclose($fp);
120		return array(
121			$width,
122			$height,
123			6
124		);
125	}
126
127	$error = false;
128	fclose($fp);
129
130	// GIF - IMAGE
131
132	$fp = @fopen($file, 'rb');
133
134	$tmp_str = fread($fp, 3);
135
136	if ($tmp_str == 'GIF')
137	{
138		$tmp_str = fread($fp, 3);
139		$width = read_word($fp);
140		$height = read_word($fp);
141
142		$info_byte = fread($fp, 1);
143		$info_byte = ord($info_byte);
144		if (($info_byte & 0x80) != 0x80 && ($info_byte & 0x80) != 0)
145		{
146			$error = true;
147		}
148
149		if (!$error)
150		{
151			if (($info_byte & 8) != 0)
152			{
153				$error = true;
154			}
155
156		}
157	}
158	else
159	{
160		$error = true;
161	}
162
163	if (!$error)
164	{
165		fclose($fp);
166		return array(
167			$width,
168			$height,
169			1
170		);
171	}
172
173	$error = false;
174	fclose($fp);
175
176	// JPG - IMAGE
177	$fp = @fopen($file, 'rb');
178
179	$tmp_str = fread($fp, 4);
180	$w1 = read_word($fp);
181
182	if (intval($w1) < 16)
183	{
184		$error = true;
185	}
186
187	if (!$error)
188	{
189		$tmp_str = fread($fp, 4);
190		if ($tmp_str == 'JFIF')
191		{
192			$o_byte = fread($fp, 1);
193			if (intval($o_byte) != 0)
194			{
195				$error = true;
196			}
197
198			if (!$error)
199			{
200				$str = fread($fp, 2);
201				$b = read_byte($fp);
202
203				if ($b != 0 && $b != 1 && $b != 2)
204				{
205					$error = true;
206				}
207			}
208
209			if (!$error)
210			{
211				$width = read_word($fp);
212				$height = read_word($fp);
213
214				if ($width <= 0 || $height <= 0)
215				{
216					$error = true;
217				}
218			}
219		}
220	}
221	else
222	{
223		$error = true;
224	}
225
226	if (!$error)
227	{
228		fclose($fp);
229		return array(
230			$width,
231			$height,
232			2
233		);
234	}
235
236	$error = false;
237	fclose($fp);
238
239	// PCX - IMAGE
240
241	$fp = @fopen($file, 'rb');
242
243	$tmp_str = fread($fp, 3);
244
245	if ((ord($tmp_str[0]) == 10) && (ord($tmp_str[1]) == 0 || ord($tmp_str[1]) == 2 || ord($tmp_str[1]) == 3 || ord($tmp_str[1]) == 4 || ord($tmp_str[1]) == 5) && (ord($tmp_str[2]) == 1))
246	{
247		$b = fread($fp, 1);
248
249		if (ord($b) != 1 && ord($b) != 2 && ord($b) != 4 && ord($b) != 8 && ord($b) != 24)
250		{
251			$error = true;
252		}
253
254		if (!$error)
255		{
256			$xmin = read_word($fp);
257			$ymin = read_word($fp);
258			$xmax = read_word($fp);
259			$ymax = read_word($fp);
260			$tmp_str = fread($fp, 52);
261
262			$b = fread($fp, 1);
263			if ($b != 0)
264			{
265				$error = true;
266			}
267		}
268
269		if (!$error)
270		{
271			$width = $xmax - $xmin + 1;
272			$height = $ymax - $ymin + 1;
273		}
274	}
275	else
276	{
277		$error = true;
278	}
279
280	if (!$error)
281	{
282		fclose($fp);
283		return array(
284			$width,
285			$height,
286			7
287		);
288	}
289
290	fclose($fp);
291
292	return $size;
293}
294
295/**
296* Flash MX Support
297* Routines and Methods are from PhpAdsNew (www.sourceforge.net/projects/phpadsnew)
298*/
299
300/**
301*/
302define('swf_tag_compressed', chr(0x43).chr(0x57).chr(0x53));
303define('swf_tag_identify', chr(0x46).chr(0x57).chr(0x53));
304
305/**
306* Get flash bits
307*/
308function swf_bits($buffer, $pos, $count)
309{
310	$result = 0;
311
312	for ($loop = $pos; $loop < $pos + $count; $loop++)
313	{
314		$result = $result + ((((ord($buffer[(int)($loop / 8)])) >> (7 - ($loop % 8))) & 0x01) << ($count - ($loop - $pos) - 1));
315	}
316
317	return $result;
318}
319
320/**
321* decompress flash contents
322*/
323function swf_decompress($buffer)
324{
325	if ((function_exists('gzuncompress')) && (substr($buffer, 0, 3) == swf_tag_compressed) && (ord(substr($buffer, 3, 1)) >= 6) )
326	{
327		// Only decompress relevant Informations
328		$output  = 'F';
329		$output .= substr ($buffer, 1, 7);
330		$output .= gzuncompress (substr ($buffer, 8));
331
332		return $output;
333	}
334	else
335	{
336		return $buffer;
337	}
338}
339
340/**
341* Get flash dimension
342*/
343function swf_getdimension($file)
344{
345	$size = @getimagesize($file);
346
347	if ($size[0] != 0 || $size[1] != 0)
348	{
349		return $size;
350	}
351
352	// Try to get the Dimension manually
353	$fp = @fopen($file, 'rb');
354	if (!$fp)
355	{
356		return $size;
357	}
358
359	$error = false;
360
361	// SWF - FLASH FILE
362	$fp = @fopen($file, 'rb');
363
364	// Decompress if file is a Flash MX compressed file
365	$buffer = fread($fp, 1024);
366
367	if (substr($buffer, 0, 3) == swf_tag_identify || substr($buffer, 0, 3) == swf_tag_compressed)
368	{
369		if (substr($buffer, 0, 3) == swf_tag_compressed)
370		{
371			fclose($fp);
372			$fp = @fopen($file, 'rb');
373			$buffer = fread($fp, filesize($file));
374			$buffer = swf_decompress($buffer);
375		}
376
377		// Get size of rect structure
378		$bits = swf_bits ($buffer, 64, 5);
379
380		// Get rect
381		$width  = (int)(swf_bits ($buffer, 69 + $bits, $bits) - swf_bits ($buffer, 69, $bits)) / 20;
382		$height = (int)(swf_bits ($buffer, 69 + (3 * $bits), $bits) - swf_bits ($buffer, 69 + (2 * $bits), $bits)) / 20;
383	}
384	else
385	{
386		$error = true;
387	}
388
389	if (!$error)
390	{
391		fclose($fp);
392		return array(
393			$width,
394			$height,
395			2
396		);
397	}
398
399	fclose($fp);
400
401	return $size;
402}