PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/php/common/common.php

https://bitbucket.org/emilhdiaz/webics
PHP | 302 lines | 289 code | 13 blank | 0 comment | 5 complexity | 93ecf1231616b90ecc1a2ce63e7dc5d5 MD5 | raw file
  1. <?php
  2. require_once('enum.php');
  3. function executution_environment() {
  4. return defined('STDIN') ? 'cli' : 'cgi';
  5. }
  6. function __autoload( $resource ) {
  7. if( $class = explode('\\', $resource) )
  8. $class = array_pop($class);
  9. include_once("$class.php");
  10. if ( !class_exists($resource, false) && !interface_exists($resource, false) ) {
  11. trigger_error("Unable to load class: $class", E_USER_WARNING);
  12. }
  13. }
  14. function r( array $array ) {
  15. print '<pre>'.print_r($array, true).'</pre>';
  16. }
  17. function v( $object ) {
  18. var_dump($object);
  19. }
  20. function println( $string ) {
  21. print $string."\n";
  22. }
  23. function a( $object ) {
  24. if( is_scalar($object) ) {
  25. return array($object);
  26. } else
  27. if( is_object($object) ) {
  28. return object_to_array($object);
  29. } else
  30. if( is_array($object) ) {
  31. return $object;
  32. } else {
  33. return array();
  34. }
  35. }
  36. function t($conditional, $value) {
  37. return $conditional ? $value : null;
  38. }
  39. function f($conditional, $value) {
  40. return $conditional ? null : $value;
  41. }
  42. function error_handler($errno, $errstr, $errfile, $errline ) {
  43. // print "Error $errno: ".$errstr."\n";
  44. // print "File: ".$errfile."\n";
  45. // print "Line: ".$errline."\n";
  46. // throw new WebicsErrorException($errstr, 0, $errno, $errfile, $errline);
  47. }
  48. function exception_handler(Exception $exception) {
  49. print '<pre>';
  50. print "Exception: ".$exception->getMessage()."\n";
  51. print "File: ".$exception->getFile()."\n";
  52. print "Line: ".$exception->getLine()."\n";
  53. print "Trace:".print_r($exception->getTrace(), true)."\n";
  54. print '</pre>';
  55. }
  56. function assert_handler($file, $line, $message) {
  57. throw new FailedAssertionException($message);
  58. }
  59. function debug( $message, $level = 3 ) {
  60. if( $level == 0 ) print "<br /><b>BENCH:</b> $message <br /><br />\n";
  61. else if( $level <= DEBUG ) print "<b>DEBUG:</b> $message <br />\n";
  62. }
  63. function array_to_object( $array ) {
  64. if( empty($array) ) return false;
  65. $data = new Std();
  66. foreach( $array as $akey => $aval ) {
  67. $data->{$akey} = is_array($aval) ? array_to_object($aval) : $aval;
  68. }
  69. return $data;
  70. }
  71. function object_to_array( $object ) {
  72. if( empty($object) ) return false;
  73. $data = array();
  74. foreach( $object as $akey => $aval ) {
  75. $data[$akey] = is_object($aval) ? object_to_array($aval) : $aval;
  76. }
  77. return $data;
  78. }
  79. function get_types( array $values ) {
  80. $type = '';
  81. foreach( $values as $value ) {
  82. if( is_integer($value) || $value instanceOf Integer )
  83. $type .= 'i';
  84. elseif( is_double($value) || $value instanceOf Float )
  85. $type .= 'd';
  86. elseif( is_string($value) || $value instanceOf String )
  87. $type .= 's';
  88. else
  89. $type .= 'i';
  90. }
  91. return $type;
  92. }
  93. function parse_key_value_pair( $pair ) {
  94. return array_reverse(preg_split('/[=|==|=>]/', $pair));
  95. }
  96. function is_associative( $array ) {
  97. if ( empty($array) ) return false;
  98. for ( $iterator = count($array) - 1; $iterator; $iterator-- ) {
  99. if ( ! array_key_exists($iterator, $array) ) { return true; }
  100. }
  101. return ! array_key_exists(0, $array);
  102. }
  103. function array_prefix_values( $prefix, $array ) {
  104. foreach($array as $key=>$value) {
  105. $array[$key] = $prefix.$value;
  106. }
  107. return $array;
  108. }
  109. function array_postfix_values( $postfix, $array) {
  110. foreach($array as $key=>$value) {
  111. $array[$key] = $value.$postfix;
  112. }
  113. return $array;
  114. }
  115. function array_prefix_keys( $prefix, $array ) {
  116. foreach($array as $key=>$value) {
  117. $array[$prefix.$key] = $value;
  118. unset($array[$key]);
  119. }
  120. return $array;
  121. }
  122. function array_unprefix_keys( $prefix, $array ) {
  123. foreach($array as $key=>$value) {
  124. $array[str_replace($prefix, '', $key)] = $value;
  125. unset($array[$key]);
  126. }
  127. return $array;
  128. }
  129. function reference_to_copy( $var ) {
  130. return unserialize(serialize($var));
  131. }
  132. function array_key_search( $needle, $haystack ) {
  133. return (bool) (in_array(strtolower($needle), array_map('strtolower', array_keys($haystack))));
  134. }
  135. function array_value( $key, $array ) {
  136. $array = array_change_key_case($array, CASE_LOWER);
  137. return in_array(strtolower($key), array_keys($array)) ? $array[strtolower($key)] : null;
  138. }
  139. function array_extract( $array, $keys ) {
  140. $extract = array();
  141. foreach( $keys as $key ) {
  142. $extract[$key] = $array[$key];
  143. }
  144. return $extract;
  145. }
  146. function get_caller_method( $level = 1 ) {
  147. $traces = array_shift(debug_backtrace());
  148. if (isset($traces[$level])) {
  149. return $traces[$level]['function'];
  150. }
  151. return null;
  152. }
  153. function get_caller_class( $level = 1 ) {
  154. $traces = debug_backtrace();
  155. array_shift($traces);
  156. if( is_array($level) ) {
  157. foreach( $traces as $trace ) {
  158. if( !in_array($trace['class'], $level) )
  159. return $trace['class'];
  160. }
  161. }
  162. if (isset($traces[$level])) {
  163. return $traces[$level]['class'];
  164. }
  165. return null;
  166. }
  167. function restrict_method_access( $allowed_classes ) {
  168. if( !in_array(get_caller_class(2), a($allowed_classes)) )
  169. throw new UnknownMethodException(get_caller_method());
  170. }
  171. function createRandomPassword() {
  172. $chars = "abcdefghijkmnopqrstuvwxyz023456789";
  173. srand((double)microtime()*1000000);
  174. $i = 0;
  175. $pass = '' ;
  176. while ($i <= 7) {
  177. $num = rand() % 33;
  178. $tmp = substr($chars, $num, 1);
  179. $pass = $pass . $tmp;
  180. $i++;
  181. }
  182. return $pass;
  183. }
  184. function call( $func, $args ) {
  185. switch(count($args)) {
  186. case 0: return $func(); break;
  187. case 1: return $func($args[0]); break;
  188. case 2: return $func($args[0],$args[1]); break;
  189. case 3: return $func($args[0],$args[1],$args[2]); break;
  190. case 4: return $func($args[0],$args[1],$args[2],$args[3]); break;
  191. case 5: return $func($args[0],$args[1],$args[2],$args[3],$args[4],$args[5]); break;
  192. case 6: return $func($args[0],$args[1],$args[2],$args[3],$args[4],$args[5],$args[6]); break;
  193. case 7: return $func($args[0],$args[1],$args[2],$args[3],$args[4],$args[5],$args[6],$args[7]); break;
  194. case 8: return $func($args[0],$args[1],$args[2],$args[3],$args[4],$args[5],$args[6],$args[7],$args[8]); break;
  195. case 9: return $func($args[0],$args[1],$args[2],$args[3],$args[4],$args[5],$args[6],$args[7],$args[8],$args[9]); break;
  196. }
  197. }
  198. function call_static( $class, $method, $args ) {
  199. switch(count($args)) {
  200. case 0: $class::$method(); break;
  201. case 1: $class::$method($args[0]); break;
  202. case 2: $class::$method($args[0],$args[1]); break;
  203. case 3: $class::$method($args[0],$args[1],$args[2]); break;
  204. case 4: $class::$method($args[0],$args[1],$args[2],$args[3]); break;
  205. case 5: $class::$method($args[0],$args[1],$args[2],$args[3],$args[4],$args[5]); break;
  206. case 6: $class::$method($args[0],$args[1],$args[2],$args[3],$args[4],$args[5],$args[6]); break;
  207. case 7: $class::$method($args[0],$args[1],$args[2],$args[3],$args[4],$args[5],$args[6],$args[7]); break;
  208. case 8: $class::$method($args[0],$args[1],$args[2],$args[3],$args[4],$args[5],$args[6],$args[7],$args[8]); break;
  209. case 9: $class::$method($args[0],$args[1],$args[2],$args[3],$args[4],$args[5],$args[6],$args[7],$args[8],$args[9]); break;
  210. }
  211. }
  212. function runExternal( $cmd, &$code ) {
  213. $descriptorspec = array(
  214. 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
  215. 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
  216. 2 => array("pipe", "w") // stderr is a file to write to
  217. );
  218. $pipes= array();
  219. $process = proc_open($cmd, $descriptorspec, $pipes);
  220. $output= "";
  221. if (!is_resource($process)) return false;
  222. #close child's input imidiately
  223. fclose($pipes[0]);
  224. stream_set_blocking($pipes[1],false);
  225. stream_set_blocking($pipes[2],false);
  226. $todo= array($pipes[1],$pipes[2]);
  227. while( true ) {
  228. $read= array();
  229. if( !feof($pipes[1]) ) $read[]= $pipes[1];
  230. if( !feof($pipes[2]) ) $read[]= $pipes[2];
  231. if (!$read) break;
  232. $ready= stream_select($read, $write=NULL, $ex= NULL, 2);
  233. if ($ready === false) {
  234. break; #should never happen - something died
  235. }
  236. foreach ($read as $r) {
  237. $s= fread($r,1024);
  238. $output.= $s;
  239. }
  240. }
  241. fclose($pipes[1]);
  242. fclose($pipes[2]);
  243. $code= proc_close($process);
  244. return $output;
  245. }
  246. ?>