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