1: <?php
2: 3: 4: 5: 6: 7:
8:
9: 10: 11: 12:
13: function print_debug($info)
14: {
15: echo "<pre>";
16: foreach(func_get_args() as $arg)
17: {
18: print_r($arg);
19: echo "\n";
20: }
21:
22: die();
23: }
24: function print_brief_debug($info, $limit = 1)
25: {
26: echo "<pre>";
27: print_array($info, $limit);
28:
29:
30: }
31: function print_array($array,$depth=1,$indentation=0){
32: if (is_array($array)){
33: echo "Array(\n";
34: foreach ($array as $key=>$value){
35: if(is_array($value)){
36: if($depth){
37: echo "max depth reached.";
38: }
39: else{
40: for($i=0;$i<$indentation;$i++){
41: echo " ";
42: }
43: echo $key."=Array(";
44: print_array($value,$depth-1,$indentation+1);
45: for($i=0;$i<$indentation;$i++){
46: echo " ";
47: }
48: echo ");";
49: }
50: }
51: else{
52: for($i=0;$i<$indentation;$i++){
53: echo " ";
54: }
55: echo $key."=>".$value."\n";
56: }
57: }
58: echo ");\n";
59: }
60: elseif(is_object($array)){
61: echo get_class($array) . "\n";
62: } else{
63: var_dump($array);
64: }
65: }
66: function fix_date($date)
67: {
68: if($date != "")
69: {
70: if(preg_match("#([0-9]{2})/([0-9]{2})/([0-9]{4})#", $date, $matches))
71: {
72: return $matches[3] . "-" . $matches[2] . "-" . $matches[1];
73: }
74: $date = str_replace("/", "-", $date);
75: return date("Y-m-d", strtotime($date));
76: }
77: return $date;
78: }
79:
80: function fix_decimal($number)
81: {
82: if(preg_match("#[0-9,.]+#", $number, $matches))
83: {
84: $number = str_replace(".", "", $number);
85: $number = str_replace(",", ".", $number);
86: }
87: return $number;
88: }
89:
90: function fix_date_format($date, $format = "d-m-Y")
91: {
92: $res = date_create_from_format($format, $date);
93: if($res)
94: return $res->format("Y-m-d");
95: return $date;
96: }
97:
98: function reArrayFiles(&$file_post) {
99:
100: $file_ary = array();
101: $file_count = count($file_post['name']);
102: $file_keys = array_keys($file_post);
103:
104: for ($i=0; $i<$file_count; $i++) {
105: foreach ($file_keys as $key) {
106: $file_ary[$i][$key] = $file_post[$key][$i];
107: }
108: }
109:
110: return $file_ary;
111: }
112: function reArrayPost($array) {
113: $keys = array();
114: $result = array();
115: foreach ($array as $key => $value)
116: {
117: $keys[] = $key;
118: }
119: foreach ($keys as $key)
120: {
121: foreach ($array[$key] as $k => $v)
122: {
123: if(!isset($result[$k])) $result[$k] = array();
124: $result[$k][$key] = $v;
125: }
126: }
127: return $result;
128: }
129:
130: function file_get_php_classes($filepath) {
131: $php_code = file_get_contents($filepath);
132: $classes = get_php_classes($php_code);
133: return $classes;
134: }
135:
136: function get_php_classes($php_code) {
137: $classes = array();
138: $namespace = "";
139: $namespaceBool = false;
140: $tokens = token_get_all($php_code);
141: $count = count($tokens);
142: for ($i = 0; $i < $count; $i++) {
143: if ( $i >= 2 && $tokens[$i - 2][0] == T_CLASS
144: && $tokens[$i - 1][0] == T_WHITESPACE
145: && $tokens[$i][0] == T_STRING) {
146:
147: $class_name = $namespace . $tokens[$i][1];
148: $classes[] = $class_name;
149: }
150:
151: if ( $tokens[$i][0] == T_NAMESPACE
152:
153:
154: ) {
155: $namespaceBool = true;
156: }
157: if($tokens[$i] == ';')
158: {
159: $namespaceBool = false;
160: }
161: if($namespaceBool && $tokens[$i][0] == T_STRING)
162: {
163: $namespace .= $tokens[$i][1] . "\\";
164: }
165: }
166: return $classes;
167: }
168:
169: function array_merge_recursive_ex(array & $array1, array & $array2)
170: {
171: $merged = $array1;
172:
173: foreach ($array2 as $key => & $value)
174: {
175: if (is_array($value) && isset($merged[$key]) && is_array($merged[$key]))
176: {
177: $merged[$key] = array_merge_recursive_ex($merged[$key], $value);
178: } else if (is_numeric($key))
179: {
180: if (!in_array($value, $merged))
181: $merged[] = $value;
182: } else
183: $merged[$key] = $value;
184: }
185:
186: return $merged;
187: }
188:
189: 190: 191: 192: 193: 194: 195: 196: 197:
198: function instance_method($name, &$cache = array(), $instanceParams = array())
199: {
200: if(!$cache) $cache = array();
201: if(strpos($name, "->") !== FALSE)
202: {
203: $split = explode("->", $name);
204: $instance = null;
205: if(!isset($cache[$split[0]]))
206: {
207: $rf = new ReflectionClass($split[0]);
208: $instance = $rf->getConstructor()?$rf->newInstanceArgs($instanceParams):$rf->newInstance();
209:
210: $cache[$split[0]] = $instance;
211: }
212: $instance = $cache[$split[0]];
213: return array($instance, $split[1]);
214: }
215: if(strpos($name, "::") !== FALSE)
216: {
217: $split = explode("::", $name);
218: return $split[0] . "::" . $split[1];
219: }
220: return $name;
221: }
222:
223: function today($format = "Y-m-d")
224: {
225: return date($format);
226: }
227:
228: function now($format = "Y-m-d H:i:s")
229: {
230: return date($format);
231: }
232:
233: function e($text)
234: {
235: return htmlentities($text);
236: }
237:
238: function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
239: {
240: $str = '';
241: $max = mb_strlen($keyspace, '8bit') - 1;
242: for ($i = 0; $i < $length; ++$i) {
243: $str .= $keyspace[mt_rand(0, $max)];
244: }
245: return $str;
246: }
247:
248: function is_module_enabled($module)
249: {
250: return \GLFramework\Module\ModuleManager::exists($module);
251: }
252:
253: if (!function_exists('http_response_code')) {
254: function http_response_code($code = NULL) {
255:
256: if ($code !== NULL) {
257:
258: switch ($code) {
259: case 100: $text = 'Continue'; break;
260: case 101: $text = 'Switching Protocols'; break;
261: case 200: $text = 'OK'; break;
262: case 201: $text = 'Created'; break;
263: case 202: $text = 'Accepted'; break;
264: case 203: $text = 'Non-Authoritative Information'; break;
265: case 204: $text = 'No Content'; break;
266: case 205: $text = 'Reset Content'; break;
267: case 206: $text = 'Partial Content'; break;
268: case 300: $text = 'Multiple Choices'; break;
269: case 301: $text = 'Moved Permanently'; break;
270: case 302: $text = 'Moved Temporarily'; break;
271: case 303: $text = 'See Other'; break;
272: case 304: $text = 'Not Modified'; break;
273: case 305: $text = 'Use Proxy'; break;
274: case 400: $text = 'Bad Request'; break;
275: case 401: $text = 'Unauthorized'; break;
276: case 402: $text = 'Payment Required'; break;
277: case 403: $text = 'Forbidden'; break;
278: case 404: $text = 'Not Found'; break;
279: case 405: $text = 'Method Not Allowed'; break;
280: case 406: $text = 'Not Acceptable'; break;
281: case 407: $text = 'Proxy Authentication Required'; break;
282: case 408: $text = 'Request Time-out'; break;
283: case 409: $text = 'Conflict'; break;
284: case 410: $text = 'Gone'; break;
285: case 411: $text = 'Length Required'; break;
286: case 412: $text = 'Precondition Failed'; break;
287: case 413: $text = 'Request Entity Too Large'; break;
288: case 414: $text = 'Request-URI Too Large'; break;
289: case 415: $text = 'Unsupported Media Type'; break;
290: case 500: $text = 'Internal Server Error'; break;
291: case 501: $text = 'Not Implemented'; break;
292: case 502: $text = 'Bad Gateway'; break;
293: case 503: $text = 'Service Unavailable'; break;
294: case 504: $text = 'Gateway Time-out'; break;
295: case 505: $text = 'HTTP Version not supported'; break;
296: default:
297: exit('Unknown http status code "' . htmlentities($code) . '"');
298: break;
299: }
300:
301: $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
302:
303: header($protocol . ' ' . $code . ' ' . $text);
304:
305: $GLOBALS['http_response_code'] = $code;
306:
307: } else {
308:
309: $code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200);
310:
311: }
312:
313: return $code;
314:
315: }
316: }
317:
318: function post($url, $fields, $header = array())
319: {
320: $fields_string = "";
321: foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
322: rtrim($fields_string, '&');
323:
324: $ch = curl_init();
325:
326:
327: curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
328: curl_setopt($ch,CURLOPT_URL, $url);
329: curl_setopt($ch,CURLOPT_POST, count($fields));
330: curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
331: curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
332:
333:
334: $result = curl_exec($ch);
335:
336:
337: curl_close($ch);
338: return $result;
339: }
340:
341: function get($url, $header = array())
342: {
343:
344: $ch = curl_init();
345:
346:
347: curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
348: curl_setopt($ch,CURLOPT_URL, $url);
349: curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
350:
351:
352: $result = curl_exec($ch);
353:
354:
355: curl_close($ch);
356: return $result;
357: }
358:
359: function fix_url($url)
360: {
361: if(strpos($url, "http") === FALSE)
362: {
363: $url = "http://" . $_SERVER['HTTP_HOST'] . $url;
364: }
365: return $url;
366: }