PageRenderTime 55ms CodeModel.GetById 45ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/core/utils/Utils.php

http://zoop.googlecode.com/
PHP | 340 lines | 194 code | 41 blank | 105 comment | 21 complexity | 0266866e161b40deeebd8211392d87b7 MD5 | raw file
  1<?php
  2/**
  3 * Returns true if the current page was requested with the GET method
  4 *
  5 * @return boolean
  6 */
  7function RequestIsGet()
  8{
  9	return $_SERVER['REQUEST_METHOD'] == 'GET' ? 1 : 0;
 10}
 11
 12/**
 13 * Returns true if the current page was requested with the POST method
 14 *
 15 * @return boolean
 16 */
 17function RequestIsPost()
 18{
 19	return $_SERVER['REQUEST_METHOD'] == 'POST' ? 1 : 0;
 20}
 21
 22/**
 23 * Evaluates the POST variables and creates a standard "year-month-day Hour24:minute:second -7:00" date from a POSTed form
 24 * The fields in the form should be as follows:
 25 * <name>Month, <name>Day, <name>Year
 26 * <name>Hour, <name>Minute, <name>Second
 27 * <name>Meridian (<-- "am" or "pm")
 28 * 
 29 * @param $name Prefix of the POST variables to evaluate
 30 * @return string Date string
 31 */
 32
 33function GetFormDate($name, $src = null)
 34{
 35	if(!$src)
 36		$src = $_POST;
 37	
 38	if(is_array($src[$name]))
 39	{
 40		$year = $src[$name]['Date_Year'];
 41		$month = $src[$name]['Date_Month'];
 42		$day = $src[$name]['Date_Day'];
 43	}
 44	else
 45	{
 46		$name = "{$name}_";
 47		$month = $src[$name . 'Month'];
 48		$day = $src[$name . 'Day'];
 49		$year = $src[$name . 'Year'];
 50	}
 51	
 52	return "$year-$month-$day";
 53}
 54
 55/*
 56there should be separate functions for date and time
 57function GetPostDate($name)
 58{
 59	//echo_r($_POST);
 60	$name = "{$name}_";
 61	$month = $_POST[$name . 'Month'];
 62	$day = $_POST[$name . 'Day'];
 63	$year = $_POST[$name . 'Year'];
 64	$hour = $_POST[$name . 'Hour'];
 65	$minute = $_POST[$name . 'Minute'];
 66	$second = $_POST[$name . 'Second'];
 67	$meridian = $_POST[$name . 'Meridian'];
 68	
 69	$hour = $meridian == 'pm' ? ($hour + 12) : $hour;
 70	
 71	return "$year-$month-$day $hour:$minute:$second -7:00";
 72}
 73*/
 74
 75
 76/**
 77 * print_r the contents of the variable $var along with a full function backtrace to indicate where in the program this is occurring (great for debugging)
 78 *
 79 * @param mixed $var Variable to print
 80 * @param boolean $supressBacktrace True if you wish to suppress the backtrace (default: False)
 81 */
 82function echo_r($var, $supressBacktrace = 0)
 83{
 84	if(!$supressBacktrace)
 85		EchoBacktrace();
 86	echo '<pre>';
 87	print_r($var);
 88	echo '</pre>';
 89}
 90
 91/**
 92 * Redirect the client browser to $url
 93 *
 94 * @param string $url URL to which to send them
 95 */
 96function Redirect($url = NULL)
 97{
 98	if(!$url)
 99		$url = virtual_url;
100	header("location: $url");
101	die();
102}
103
104/**
105 * Redirects the client to a URL relative to the project (index.php/<url>)
106 *
107 * @param string $virtualPath Path inside the project to which to send them
108 */
109function BaseRedirect($virtualPath)
110{
111	Redirect(script_url . '/' . $virtualPath);
112}
113
114
115/**
116 * Echos an HTML-formatted backtrace
117 *
118 * @param unknown_type $value I don't know what this is for
119 */
120function EchoBacktrace($value='')
121{
122	echo FormatBacktraceHtml(debug_backtrace());
123}
124
125
126/**
127 * Generates and prints backtrace information in readable HTML
128 *
129 * @param debug_backtrace() $backtraceInfo The results of a debug_backtrace() function call
130 */
131function FormatBacktraceHtml($backtraceInfo)
132{
133	// debug_print_backtrace();
134	// return;
135	//echo_r($backtraceInfo);
136?>
137<table border="1">
138	<tr>
139		<th>File</th><th>Line</th><th>Function</th>
140	</tr>
141	<?php  foreach($backtraceInfo as $thisRow): 
142		$lineInfo = FormateBacktraceLineHtml($thisRow);
143	?><tr>
144		<td><?php echo $lineInfo['file']; ?></td>
145		<td><?php echo $lineInfo['line']; ?></td>
146		<td><?php echo $lineInfo['function']; ?></td>
147	</tr>
148	<?php endforeach; ?>
149</table>
150<?php
151}
152
153function FormateBacktraceLineHtml($lineInfo)
154{
155	// echo_r($lineInfo);
156	$result = array();
157	$result['file'] = isset($lineInfo['file']) ? $lineInfo['file'] : 'php function';
158	$result['line'] = isset($lineInfo['line']) ? $lineInfo['line'] : 'na';
159	$result['function'] = FormatBacktraceFunctionCellHtml($lineInfo);
160	return $result;
161}
162
163function FormatBacktraceFunctionCellHtml($lineInfo)
164{
165//	echo "here we are<br>";
166//	var_dump($lineInfo);
167//	echo_r($lineInfo);
168	$call = '';
169	$call .= isset($lineInfo['class']) ? ($lineInfo['class'] . $lineInfo['type']) : '';
170	$call .= $lineInfo['function'] . '(';
171	$argStrings = array();
172	
173	if(isset($lineInfo['args']))
174	foreach($lineInfo['args'] as $thisArg)
175	{
176//		echo '<b>arg = ' . $thisArg . '</b><br>';
177//		echo '<b>type = ' . gettype($thisArg) . '</b>';
178//		echo_r($thisArg);
179		switch(gettype($thisArg))
180		{
181			case 'string':
182				$argStrings[] = '"' . $thisArg . '"';
183				break;
184			case 'integer':
185				$argStrings[] = $thisArg;
186				break;
187			case 'array':
188				$argStrings[] = '&lt;array&gt;';
189				break;
190			case 'object':
191				$argStrings[] = '&lt;object&gt;';
192				break;
193			case 'resource':
194				$argStrings[] = 'resource: ' . $thisArg;
195				break;
196			case 'boolean':
197				$argStrings[] = 'boolean: -' . $thisArg . '-';
198				break;
199			case 'NULL':
200				$argStrings[] = 'NULL';
201				break;
202			default:
203				die('unhandled type ' . gettype($thisArg));
204				break;
205		}
206		
207//		echo '<strong>call = ' . $call . '</strong><br>';
208	}
209	
210	$call .= implode(', ', $argStrings);
211	
212	$call .= ')';
213	
214	return $call;
215}
216
217
218
219/**
220 * Given a filename, outputs the contents of the file to the client
221 *
222 * @param string $filename Path and filename of the file to output
223 */
224function EchoStaticFile($filename)
225{
226	$fp = fopen($filename, 'rb');
227	
228	//	send the headers
229	//header("Content-Type: image/png");	//	figure out what should really be done here
230	header("Content-Length: " . filesize($filename));	//	also we want to be able to properly set the cache headers here
231	
232	fpassthru($fp);
233}
234
235
236/**
237 * Returns a list of files in the specified directory, optionally filtered by the values in array $extention
238 *
239 * @param string $path $path Directory path to scan
240 * @param array $params Array of file extensions (without leading ".")
241 * @return array Array of filenames found in the directory
242 */
243function ListDir($path, $params)
244{
245	$entries = array();
246	$d = dir($path);
247	while (false !== ($entry = $d->read()))
248	{
249		$keep = 1;
250		if(isset($params['extentions']))
251		{
252			$keep = 0;
253			$extention = GetFileExtention($entry);
254			
255			if(in_array($extention, $params['extentions']))
256				//echo $extention . "\n";
257				$keep = 1;
258		}
259		
260		if($keep)
261			$entries[] = $entry;
262	}
263	$d->close();
264	
265	return $entries;
266}
267
268
269/**
270 * Return the extension of the given filename
271 *
272 * @param string $filename Filename to process
273 * @return string extension of the filename
274 */
275function GetFileExtention($filename)
276{
277	$parts = explode('.', $filename);
278	return array_pop($parts);
279}
280
281/**
282 * Appends a prefix to a string, if given prefix doesn't already exist
283 *
284 * @param string $string String to analyze
285 * @param string $prefix Prefix to append (if it isn't already there)
286 * @return string Prefixed string
287 */
288function str_prefix($string, $prefix)
289{
290	return substr($string, 0, strlen($prefix)) == $prefix ? 1 : 0;
291}
292
293function StripMagicQuotesFromPost()
294{
295	_StripMagicQuotes($_POST);
296}
297
298function StripMagicQuotesFromGet()
299{
300	_StripMagicQuotes($_GET);
301}
302
303function _StripMagicQuotes(&$cur)
304{
305	foreach($cur as $key => $val)
306	{
307		if(gettype($val) == 'string')
308			$cur[$key] = stripslashes($val);
309		else if(gettype($val) == 'array')
310			_StripMagicQuotes($cur[$key]);
311	}
312}
313
314//	adapted from the excellent phpass security package
315function GetRandomBytes($count, $allowFallback = false)
316{
317	$output = '';
318	if(($fh = fopen('/dev/urandom', 'rb')))
319	{
320		$output = fread($fh, $count);
321		fclose($fh);
322	}
323
324	if (strlen($output) < $count)
325	{
326		if(!$allowFallback)
327			trigger_error('system could not generate enough random data');
328		
329		$output = '';
330		for ($i = 0; $i < $count; $i += 16) {
331			$this->random_state =
332			    md5(microtime() . $this->random_state);
333			$output .=
334			    pack('H*', md5($this->random_state));
335		}
336		$output = substr($output, 0, $count);
337	}
338
339	return $output;
340}