-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimonade.php
2670 lines (2397 loc) · 74.1 KB
/
limonade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
# ============================================================================ #
/**
* L I M O N A D E
*
* a PHP micro framework.
*
* For more informations: {@link https://github.com/sofadesign/limonade}
*
* @author Fabrice Luraine
* @copyright Copyright (c) 2009 Fabrice Luraine
* @license http://opensource.org/licenses/mit-license.php The MIT License
* @package limonade
*/
# ----------------------------------------------------------------------- #
# Copyright (c) 2009 Fabrice Luraine #
# #
# Permission is hereby granted, free of charge, to any person #
# obtaining a copy of this software and associated documentation #
# files (the "Software"), to deal in the Software without #
# restriction, including without limitation the rights to use, #
# copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following #
# conditions: #
# #
# The above copyright notice and this permission notice shall be #
# included in all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, #
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES #
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT #
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR #
# OTHER DEALINGS IN THE SOFTWARE. #
# ============================================================================ #
# ============================================================================ #
# 0. PREPARE #
# ============================================================================ #
## CONSTANTS __________________________________________________________________
/**
* Limonade version
*/
define('LIMONADE', '0.5.0');
define('LIM_NAME', 'Un grand cru qui sait se faire attendre');
define('LIM_START_MICROTIME', microtime(true));
define('LIM_SESSION_NAME', 'LIMONADE'.str_replace('.','x',LIMONADE));
define('LIM_SESSION_FLASH_KEY', '_lim_flash_messages');
if(function_exists('memory_get_usage'))
define('LIM_START_MEMORY', memory_get_usage());
define('E_LIM_HTTP', 32768);
define('E_LIM_PHP', 65536);
define('E_LIM_DEPRECATED', 35000);
define('NOT_FOUND', 404);
define('SERVER_ERROR', 500);
define('ENV_PRODUCTION', 10);
define('ENV_DEVELOPMENT', 100);
define('X-SENDFILE', 10);
define('X-LIGHTTPD-SEND-FILE', 20);
# for PHP 5.3.0 <
if(!defined('E_DEPRECATED')) define('E_DEPRECATED', 8192);
if(!defined('E_USER_DEPRECATED')) define('E_USER_DEPRECATED', 16384);
# for PHP 5.2.0 <
if (!defined('E_RECOVERABLE_ERROR')) define('E_RECOVERABLE_ERROR', 4096);
## SETTING BASIC SECURITY _____________________________________________________
# A. Unsets all global variables set from a superglobal array
/**
* @access private
* @return void
*/
function unregister_globals()
{
$args = func_get_args();
foreach($args as $k => $v)
if(array_key_exists($k, $GLOBALS)) unset($GLOBALS[$k]);
}
if(ini_get('register_globals'))
{
unregister_globals( '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER',
'_ENV', '_FILES');
ini_set('register_globals', 0);
}
# B. removing magic quotes
/**
* @access private
* @param string $array
* @return array
*/
function remove_magic_quotes($array)
{
foreach ($array as $k => $v)
$array[$k] = is_array($v) ? remove_magic_quotes($v) : stripslashes($v);
return $array;
}
if (get_magic_quotes_gpc())
{
$_GET = remove_magic_quotes($_GET);
$_POST = remove_magic_quotes($_POST);
$_COOKIE = remove_magic_quotes($_COOKIE);
ini_set('magic_quotes_gpc', 0);
}
if(function_exists('set_magic_quotes_runtime') && get_magic_quotes_runtime()) set_magic_quotes_runtime(false);
# C. Disable error display
# by default, no error reporting; it will be switched on later in run().
# ini_set('display_errors', 1); must be called explicitly in app file
# if you want to show errors before running app
ini_set('display_errors', 0);
## SETTING INTERNAL ROUTES _____________________________________________________
dispatch(array("/_lim_css/*.css", array('_lim_css_filename')), 'render_limonade_css');
/**
* Internal controller that responds to route /_lim_css/*.css
*
* @access private
* @return string
*/
function render_limonade_css()
{
option('views_dir', file_path(option('limonade_public_dir'), 'css'));
$fpath = file_path(params('_lim_css_filename').".css");
return css($fpath, null); // with no layout
}
dispatch(array("/_lim_public/**", array('_lim_public_file')), 'render_limonade_file');
/**
* Internal controller that responds to route /_lim_public/**
*
* @access private
* @return void
*/
function render_limonade_file()
{
$fpath = file_path(option('limonade_public_dir'), params('_lim_public_file'));
return render_file($fpath, true);
}
# # #
# ============================================================================ #
# 1. BASE #
# ============================================================================ #
## ABSTRACTS ___________________________________________________________________
# Abstract methods that might be redefined by user:
#
# - function configure(){}
# - function initialize(){}
# - function autoload_controller($callback){}
# - function before($route){}
# - function after($output, $route){}
# - function not_found($errno, $errstr, $errfile=null, $errline=null){}
# - function server_error($errno, $errstr, $errfile=null, $errline=null){}
# - function route_missing($request_method, $request_uri){}
# - function before_exit(){}
# - function before_render($content_or_func, $layout, $locals, $view_path){}
# - function autorender($route){}
# - function before_sending_header($header){}
#
# See abstract.php for more details.
## MAIN PUBLIC FUNCTIONS _______________________________________________________
/**
* Set and returns options values
*
* If multiple values are provided, set $name option with an array of those values.
* If there is only one value, set $name option with the provided $values
*
* @param string $name
* @param mixed $values,...
* @return mixed option value for $name if $name argument is provided, else return all options
*/
function option($name = null, $values = null)
{
static $options = array();
$args = func_get_args();
$name = array_shift($args);
if(is_null($name)) return $options;
if(!empty($args))
{
$options[$name] = count($args) > 1 ? $args : $args[0];
}
if(array_key_exists($name, $options)) return $options[$name];
return;
}
/**
* Set and returns params
*
* Depending on provided arguments:
*
* * Reset params if first argument is null
*
* * If first argument is an array, merge it with current params
*
* * If there is a second argument $value, set param $name (first argument) with $value
* <code>
* params('name', 'Doe') // set 'name' => 'Doe'
* </code>
* * If there is more than 2 arguments, set param $name (first argument) value with
* an array of next arguments
* <code>
* params('months', 'jan', 'feb', 'mar') // set 'month' => array('months', 'jan', 'feb', 'mar')
* </code>
*
* @param mixed $name_or_array_or_null could be null || array of params || name of a param (optional)
* @param mixed $value,... for the $name param (optional)
* @return mixed all params, or one if a first argument $name is provided
*/
function params($name_or_array_or_null = null, $value = null)
{
static $params = array();
$args = func_get_args();
if(func_num_args() > 0)
{
$name = array_shift($args);
if(is_null($name))
{
# Reset params
$params = array();
return $params;
}
if(is_array($name))
{
$params = array_merge($params, $name);
return $params;
}
$nargs = count($args);
if($nargs > 0)
{
$value = $nargs > 1 ? $args : $args[0];
$params[$name] = $value;
}
return array_key_exists($name,$params) ? $params[$name] : null;
}
return $params;
}
/**
* Set and returns template variables
*
* If multiple values are provided, set $name variable with an array of those values.
* If there is only one value, set $name variable with the provided $values
*
* @param string $name
* @param mixed $values,...
* @return mixed variable value for $name if $name argument is provided, else return all variables
*/
function set($name = null, $values = null)
{
static $vars = array();
$args = func_get_args();
$name = array_shift($args);
if(is_null($name)) return $vars;
if(!empty($args))
{
$vars[$name] = count($args) > 1 ? $args : $args[0];
}
if(array_key_exists($name, $vars)) return $vars[$name];
return $vars;
}
/**
* Sets a template variable with a value or a default value if value is empty
*
* @param string $name
* @param string $value
* @param string $default
* @return mixed setted value
*/
function set_or_default($name, $value, $default)
{
return set($name, value_or_default($value, $default));
}
/**
* Running application
*
* @param string $env
* @return void
*/
function run($env = null)
{
if(is_null($env)) $env = env();
# 0. Set default configuration
$root_dir = dirname(app_file());
$lim_dir = dirname(__FILE__);
$base_path = dirname(file_path($env['SERVER']['SCRIPT_NAME']));
$base_file = basename($env['SERVER']['SCRIPT_NAME']);
$base_uri = file_path($base_path, (($base_file == 'index.php') ? '?' : $base_file.'?'));
option('root_dir', $root_dir);
option('limonade_dir', file_path($lim_dir));
option('limonade_views_dir', file_path($lim_dir, 'limonade', 'views'));
option('limonade_public_dir',file_path($lim_dir, 'limonade', 'public'));
option('public_dir', file_path($root_dir, 'public'));
option('views_dir', file_path($root_dir, 'views'));
option('controllers_dir', file_path($root_dir, 'controllers'));
option('lib_dir', file_path($root_dir, 'lib'));
option('error_views_dir', option('limonade_views_dir'));
option('base_path', $base_path);
option('base_uri', $base_uri); // set it manually if you use url_rewriting
option('env', ENV_PRODUCTION);
option('debug', true);
option('session', LIM_SESSION_NAME); // true, false or the name of your session
option('encoding', 'utf-8');
option('signature', LIM_NAME); // X-Limonade header value or false to hide it
option('gzip', false);
option('x-sendfile', 0); // 0: disabled,
// X-SENDFILE: for Apache and Lighttpd v. >= 1.5,
// X-LIGHTTPD-SEND-FILE: for Apache and Lighttpd v. < 1.5
# 1. Set handlers
# 1.1 Set error handling
ini_set('display_errors', 1);
set_error_handler('error_handler_dispatcher', E_ALL ^ E_NOTICE);
# 1.2 Register shutdown function
register_shutdown_function('stop_and_exit');
# 2. Set user configuration
call_if_exists('configure');
# 2.1 Set gzip compression if defined
if(is_bool(option('gzip')) && option('gzip'))
{
ini_set('zlib.output_compression', '1');
}
# 2.2 Set X-Limonade header
if($signature = option('signature')) send_header("X-Limonade: $signature");
# 3. Loading libs
require_once_dir(option('lib_dir'));
fallbacks_for_not_implemented_functions();
# 4. Starting session
if(!defined('SID') && option('session'))
{
if(!is_bool(option('session'))) session_name(option('session'));
if(!session_start()) trigger_error("An error occured while trying to start the session", E_USER_WARNING);
}
# 5. Set some default methods if needed
if(!function_exists('after'))
{
function after($output)
{
return $output;
}
}
if(!function_exists('route_missing'))
{
function route_missing($request_method, $request_uri)
{
halt(NOT_FOUND, "($request_method) $request_uri");
}
}
call_if_exists('initialize');
# 6. Check request
if($rm = request_method($env))
{
if(request_is_head($env)) ob_start(); // then no output
if(!request_method_is_allowed($rm))
halt(HTTP_NOT_IMPLEMENTED, "The requested method <code>'$rm'</code> is not implemented");
# 6.1 Check matching route
if($route = route_find($rm, request_uri($env)))
{
params($route['params']);
# 6.2 Load controllers dir
if(!function_exists('autoload_controller'))
{
function autoload_controller($callback)
{
require_once_dir(option('controllers_dir'));
}
}
autoload_controller($route['callback']);
if(is_callable($route['callback']))
{
# 6.3 Call before function
call_if_exists('before', $route);
# 6.4 Call matching controller function and output result
$output = call_user_func_array($route['callback'], array_values($route['params']));
if(is_null($output)) $output = call_if_exists('autorender', $route);
echo after(error_notices_render() . $output, $route);
}
else halt(SERVER_ERROR, "Routing error: undefined function '{$route['callback']}'", $route);
}
else route_missing($rm, request_uri($env));
}
else halt(HTTP_NOT_IMPLEMENTED, "The requested method <code>'$rm'</code> is not implemented");
}
/**
* Stop and exit limonade application
*
* @access private
* @param boolean exit or not
* @return void
*/
function stop_and_exit($exit = true)
{
call_if_exists('before_exit', $exit);
$headers = headers_list();
if(request_is_head())
{
ob_end_clean();
} else {
$flash_sweep = true;
foreach($headers as $header)
{
// If a Content-Type header exists, flash_sweep only if is text/html
// Else if there's no Content-Type header, flash_sweep by default
if(stripos($header, 'Content-Type:') === 0)
{
$flash_sweep = stripos($header, 'Content-Type: text/html') === 0;
break;
}
}
if($flash_sweep) flash_sweep();
}
if(defined('SID')) session_write_close();
if($exit) exit;
}
/**
* Returns limonade environment variables:
*
* 'SERVER', 'FILES', 'REQUEST', 'SESSION', 'ENV', 'COOKIE',
* 'GET', 'POST', 'PUT', 'DELETE'
*
* If a null argument is passed, reset and rebuild environment
*
* @param null @reset reset and rebuild environment
* @return array
*/
function env($reset = null)
{
static $env = array();
if(func_num_args() > 0)
{
$args = func_get_args();
if(is_null($args[0])) $env = array();
}
if(empty($env))
{
if(empty($GLOBALS['_SERVER']))
{
// Fixing empty $GLOBALS['_SERVER'] bug
// http://sofadesign.lighthouseapp.com/projects/29612-limonade/tickets/29-env-is-empty
$GLOBALS['_SERVER'] =& $_SERVER;
$GLOBALS['_FILES'] =& $_FILES;
$GLOBALS['_REQUEST'] =& $_REQUEST;
$GLOBALS['_SESSION'] =& $_SESSION;
$GLOBALS['_ENV'] =& $_ENV;
$GLOBALS['_COOKIE'] =& $_COOKIE;
}
$glo_names = array('SERVER', 'FILES', 'REQUEST', 'SESSION', 'ENV', 'COOKIE');
$vars = array_merge($glo_names, request_methods());
foreach($vars as $var)
{
$varname = "_$var";
if(!array_key_exists($varname, $GLOBALS)) $GLOBALS[$varname] = array();
$env[$var] =& $GLOBALS[$varname];
}
$method = request_method($env);
if($method == 'PUT' || $method == 'DELETE')
{
$varname = "_$method";
if(array_key_exists('_method', $_POST) && $_POST['_method'] == $method)
{
foreach($_POST as $k => $v)
{
if($k == "_method") continue;
$GLOBALS[$varname][$k] = $v;
}
}
else
{
parse_str(file_get_contents('php://input'), $GLOBALS[$varname]);
}
}
}
return $env;
}
/**
* Returns application root file path
*
* @return string
*/
function app_file()
{
static $file;
if(empty($file))
{
$debug_backtrace = debug_backtrace();
$stacktrace = array_pop($debug_backtrace);
$file = $stacktrace['file'];
}
return file_path($file);
}
# # #
# ============================================================================ #
# 2. ERROR #
# ============================================================================ #
/**
* Associate a function with error code(s) and return all associations
*
* @param string $errno
* @param string $function
* @return array
*/
function error($errno = null, $function = null)
{
static $errors = array();
if(func_num_args() > 0)
{
$errors[] = array('errno'=>$errno, 'function'=> $function);
}
return $errors;
}
/**
* Raise an error, passing a given error number and an optional message,
* then exit.
* Error number should be a HTTP status code or a php user error (E_USER...)
* $errno and $msg arguments can be passsed in any order
* If no arguments are passed, default $errno is SERVER_ERROR (500)
*
* @param int,string $errno Error number or message string
* @param string,string $msg Message string or error number
* @param mixed $debug_args extra data provided for debugging
* @return void
*/
function halt($errno = SERVER_ERROR, $msg = '', $debug_args = null)
{
$args = func_get_args();
$error = array_shift($args);
# switch $errno and $msg args
# TODO cleanup / refactoring
if(is_string($errno))
{
$msg = $errno;
$oldmsg = array_shift($args);
$errno = empty($oldmsg) ? SERVER_ERROR : $oldmsg;
}
else if(!empty($args)) $msg = array_shift($args);
if(empty($msg) && $errno == NOT_FOUND) $msg = request_uri();
if(empty($msg)) $msg = "";
if(!empty($args)) $debug_args = $args;
set('_lim_err_debug_args', $debug_args);
error_handler_dispatcher($errno, $msg, null, null);
}
/**
* Internal error handler dispatcher
* Find and call matching error handler and exit
* If no match found, call default error handler
*
* @access private
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param string $errline
* @return void
*/
function error_handler_dispatcher($errno, $errstr, $errfile, $errline)
{
$back_trace = debug_backtrace();
while($trace = array_shift($back_trace))
{
if($trace['function'] == 'halt')
{
$errfile = $trace['file'];
$errline = $trace['line'];
break;
}
}
# Notices and warning won't halt execution
if(error_wont_halt_app($errno))
{
error_notice($errno, $errstr, $errfile, $errline);
return;
}
else
{
# Other errors will stop application
static $handlers = array();
if(empty($handlers))
{
error(E_LIM_PHP, 'error_default_handler');
$handlers = error();
}
$is_http_err = http_response_status_is_valid($errno);
while($handler = array_shift($handlers))
{
$e = is_array($handler['errno']) ? $handler['errno'] : array($handler['errno']);
while($ee = array_shift($e))
{
if($ee == $errno || $ee == E_LIM_PHP || ($ee == E_LIM_HTTP && $is_http_err))
{
echo call_if_exists($handler['function'], $errno, $errstr, $errfile, $errline);
exit;
}
}
}
}
}
/**
* Default error handler
*
* @param string $errno
* @param string $errstr
* @param string $errfile
* @param string $errline
* @return string error output
*/
function error_default_handler($errno, $errstr, $errfile, $errline)
{
$is_http_err = http_response_status_is_valid($errno);
$http_error_code = $is_http_err ? $errno : SERVER_ERROR;
status($http_error_code);
return $http_error_code == NOT_FOUND ?
error_not_found_output($errno, $errstr, $errfile, $errline) :
error_server_error_output($errno, $errstr, $errfile, $errline);
}
/**
* Returns not found error output
*
* @access private
* @param string $msg
* @return string
*/
function error_not_found_output($errno, $errstr, $errfile, $errline)
{
if(!function_exists('not_found'))
{
/**
* Default not found error output
*
* @param string $errno
* @param string $errstr
* @param string $errfile
* @param string $errline
* @return string
*/
function not_found($errno, $errstr, $errfile=null, $errline=null)
{
option('views_dir', option('error_views_dir'));
$msg = h(rawurldecode($errstr));
return html("<h1>Page not found:</h1><p><code>{$msg}</code></p>", error_layout());
}
}
return not_found($errno, $errstr, $errfile, $errline);
}
/**
* Returns server error output
*
* @access private
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param string $errline
* @return string
*/
function error_server_error_output($errno, $errstr, $errfile, $errline)
{
if(!function_exists('server_error'))
{
/**
* Default server error output
*
* @param string $errno
* @param string $errstr
* @param string $errfile
* @param string $errline
* @return string
*/
function server_error($errno, $errstr, $errfile=null, $errline=null)
{
$is_http_error = http_response_status_is_valid($errno);
$args = compact('errno', 'errstr', 'errfile', 'errline', 'is_http_error');
option('views_dir', option('limonade_views_dir'));
$html = render('error.html.php', null, $args);
option('views_dir', option('error_views_dir'));
return html($html, error_layout(), $args);
}
}
return server_error($errno, $errstr, $errfile, $errline);
}
/**
* Set and returns error output layout
*
* @param string $layout
* @return string
*/
function error_layout($layout = false)
{
static $o_layout = 'default_layout.php';
if($layout !== false)
{
option('error_views_dir', option('views_dir'));
$o_layout = $layout;
}
return $o_layout;
}
/**
* Set a notice if arguments are provided
* Returns all stored notices.
* If $errno argument is null, reset the notices array
*
* @access private
* @param string, null $str
* @return array
*/
function error_notice($errno = false, $errstr = null, $errfile = null, $errline = null)
{
static $notices = array();
if($errno) $notices[] = compact('errno', 'errstr', 'errfile', 'errline');
else if(is_null($errno)) $notices = array();
return $notices;
}
/**
* Returns notices output rendering and reset notices
*
* @return string
*/
function error_notices_render()
{
if(option('debug') && option('env') > ENV_PRODUCTION)
{
$notices = error_notice();
error_notice(null); // reset notices
$c_view_dir = option('views_dir'); // keep for restore after render
option('views_dir', option('limonade_views_dir'));
$o = render('_notices.html.php', null, array('notices' => $notices));
option('views_dir', $c_view_dir); // restore current views dir
return $o;
}
}
/**
* Checks if an error is will halt application execution.
* Notices and warnings will not.
*
* @access private
* @param string $num error code number
* @return boolean
*/
function error_wont_halt_app($num)
{
return $num == E_NOTICE ||
$num == E_WARNING ||
$num == E_CORE_WARNING ||
$num == E_COMPILE_WARNING ||
$num == E_USER_WARNING ||
$num == E_USER_NOTICE ||
$num == E_DEPRECATED ||
$num == E_USER_DEPRECATED ||
$num == E_LIM_DEPRECATED;
}
/**
* return error code name for a given code num, or return all errors names
*
* @param string $num
* @return mixed
*/
function error_type($num = null)
{
$types = array (
E_ERROR => 'ERROR',
E_WARNING => 'WARNING',
E_PARSE => 'PARSING ERROR',
E_NOTICE => 'NOTICE',
E_CORE_ERROR => 'CORE ERROR',
E_CORE_WARNING => 'CORE WARNING',
E_COMPILE_ERROR => 'COMPILE ERROR',
E_COMPILE_WARNING => 'COMPILE WARNING',
E_USER_ERROR => 'USER ERROR',
E_USER_WARNING => 'USER WARNING',
E_USER_NOTICE => 'USER NOTICE',
E_STRICT => 'STRICT NOTICE',
E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR',
E_DEPRECATED => 'DEPRECATED WARNING',
E_USER_DEPRECATED => 'USER DEPRECATED WARNING',
E_LIM_DEPRECATED => 'LIMONADE DEPRECATED WARNING'
);
return is_null($num) ? $types : $types[$num];
}
/**
* Returns http response status for a given error number
*
* @param string $errno
* @return int
*/
function error_http_status($errno)
{
$code = http_response_status_is_valid($errno) ? $errno : SERVER_ERROR;
return http_response_status($code);
}
# # #
# ============================================================================ #
# 3. REQUEST #
# ============================================================================ #
/**
* Returns current request method for a given environment or current one
*
* @param string $env
* @return string
*/
function request_method($env = null)
{
if(is_null($env)) $env = env();
$m = array_key_exists('REQUEST_METHOD', $env['SERVER']) ? $env['SERVER']['REQUEST_METHOD'] : null;
if($m == "POST" && array_key_exists('_method', $env['POST']))
$m = strtoupper($env['POST']['_method']);
if(!in_array(strtoupper($m), request_methods()))
{
trigger_error("'$m' request method is unknown or unavailable.", E_USER_WARNING);
$m = false;
}
return $m;
}
/**
* Checks if a request method or current one is allowed
*
* @param string $m
* @return bool
*/
function request_method_is_allowed($m = null)
{
if(is_null($m)) $m = request_method();
return in_array(strtoupper($m), request_methods());
}
/**
* Checks if request method is GET
*
* @param string $env
* @return bool
*/
function request_is_get($env = null)
{
return request_method($env) == "GET";
}
/**
* Checks if request method is POST
*
* @param string $env
* @return bool
*/
function request_is_post($env = null)
{
return request_method($env) == "POST";
}
/**
* Checks if request method is PUT
*
* @param string $env
* @return bool
*/
function request_is_put($env = null)
{
return request_method($env) == "PUT";
}
/**
* Checks if request method is DELETE
*
* @param string $env
* @return bool
*/
function request_is_delete($env = null)
{
return request_method($env) == "DELETE";
}
/**
* Checks if request method is HEAD
*
* @param string $env
* @return bool
*/
function request_is_head($env = null)
{
return request_method($env) == "HEAD";
}
/**
* Returns allowed request methods
*
* @return array
*/
function request_methods()
{
return array("GET","POST","PUT","DELETE", "HEAD");
}
/**
* Returns current request uri (the path that will be compared with routes)
*
* (Inspired from codeigniter URI::_fetch_uri_string method)
*
* @return string
*/
function request_uri($env = null)
{
static $uri = null;
if(is_null($env))