forked from getkirby-v1/toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kirby.php
3399 lines (2890 loc) · 92.8 KB
/
kirby.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
/**
* Kirby -- A stripped down and easy to use toolkit for PHP
*
* @version 0.94
* @author Bastian Allgeier <[email protected]>
* @link http://toolkit.getkirby.com
* @copyright Copyright 2009-2012 Bastian Allgeier
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Kirby
*/
c::set('version', 0.94);
c::set('language', 'en');
c::set('charset', 'utf-8');
c::set('root', dirname(__FILE__));
/**
* Redirects the user to a new URL
*
* @param string $url The URL to redirect to
* @param boolean $code The HTTP status code, which should be sent (301, 302 or 303)
* @package Kirby
*/
function go($url=false, $code=false) {
if(empty($url)) $url = c::get('url', '/');
// send an appropriate header
if($code) {
switch($code) {
case 301:
header('HTTP/1.1 301 Moved Permanently');
break;
case 302:
header('HTTP/1.1 302 Found');
break;
case 303:
header('HTTP/1.1 303 See Other');
break;
}
}
// send to new page
header('Location:' . $url);
exit();
}
/**
* Returns the status from a Kirby response
*
* @param array $response The Kirby response array
* @return string "error" or "success"
* @package Kirby
*/
function status($response) {
return a::get($response, 'status');
}
/**
* Returns the message from a Kirby response
*
* @param array $response The Kirby response array
* @return string The message
* @package Kirby
*/
function msg($response) {
return a::get($response, 'msg');
}
/**
* Checks if a Kirby response is an error response or not.
*
* @param array $response The Kirby response array
* @return boolean Returns true if the response is an error, returns false if no error occurred
* @package Kirby
*/
function error($response) {
return (status($response) == 'error') ? true : false;
}
/**
* Checks if a Kirby response is a success response.
*
* @param array $response The Kirby response array
* @return boolean Returns true if the response is a success, returns false if an error occurred
* @package Kirby
*/
function success($response) {
return !error($response);
}
/**
* Loads additional PHP files
*
* You can set the root directory with c::set('root', 'my/root');
* By default the same directory in which the kirby toolkit file is located will be used.
*
* @param args A list filenames as individual arguments
* @return array Returns a Kirby response array. On error the response array includes the unloadable files (errors).
* @package Kirby
*/
function load() {
$root = c::get('root');
$files = func_get_args();
$errors = array();
foreach((array)$files AS $f) {
$file = $root . '/' . $f . '.php';
if(file_exists($file)) {
include_once($file);
} else {
$errors[] = $file;
}
}
if(!empty($errors)) return array(
'status' => 'error',
'msg' => 'some files could not be loaded',
'errors' => $errors
);
return array(
'status' => 'success',
'msg' => 'all files have been loaded'
);
}
/**
*
* Array
*
* This class is supposed to simplify array handling
* and make it more consistent.
*
* @package Kirby
*/
class a {
/**
* Gets an element of an array by key
*
* @param array $array The source array
* @param mixed $key The key to look for
* @param mixed $default Optional default value, which should be returned if no element has been found
* @return mixed
*/
static function get($array, $key, $default=null) {
return (isset($array[ $key ])) ? $array[ $key ] : $default;
}
/**
* Gets all elements for an array of key
*
* @param array $array The source array
* @keys array $keys An array of keys to fetch
* @return array An array of keys and matching values
*/
static function getall($array, $keys) {
$result = array();
foreach($keys as $key) $result[$key] = $array[$key];
return $result;
}
/**
* Removes an element from an array
*
* @param array $array The source array
* @param mixed $search The value or key to look for
* @param boolean $key Pass true to search for an key, pass false to search for an value.
* @return array The result array without the removed element
*/
static function remove($array, $search, $key=true) {
if($key) {
unset($array[$search]);
} else {
$found_all = false;
while(!$found_all) {
$index = array_search($search, $array);
if($index !== false) {
unset($array[$index]);
} else {
$found_all = true;
}
}
}
return $array;
}
/**
* Injects an element into an array
*
* @param array $array The source array
* @param int $position The position, where to inject the element
* @param mixed $element The element, which should be injected
* @return array The result array including the new element
*/
static function inject($array, $position, $element='placeholder') {
$start = array_slice($array, 0, $position);
$end = array_slice($array, $position);
return array_merge($start, (array)$element, $end);
}
/**
* Shows an entire array or object in a human readable way
* This is perfect for debugging
*
* @param array $array The source array
* @param boolean $echo By default the result will be echoed instantly. You can switch that off here.
* @return mixed If echo is false, this will return the generated array output.
*/
static function show($array, $echo=true) {
$output = '<pre>';
$output .= htmlspecialchars(print_r($array, true));
$output .= '</pre>';
if($echo==true) echo $output;
return $output;
}
/**
* Converts an array to a JSON string
* It's basically a shortcut for json_encode()
*
* @param array $array The source array
* @return string The JSON string
*/
static function json($array) {
return @json_encode( (array)$array );
}
/**
* Converts an array to a XML string
*
* @param array $array The source array
* @param string $tag The name of the root element
* @param boolean $head Include the xml declaration head or not
* @param string $charset The charset, which should be used for the header
* @param int $level The indendation level
* @return string The XML string
*/
static function xml($array, $tag='root', $head=true, $charset='utf-8', $tab=' ', $level=0) {
$result = ($level==0 && $head) ? '<?xml version="1.0" encoding="' . $charset . '"?>' . "\n" : '';
$nlevel = ($level+1);
$result .= str_repeat($tab, $level) . '<' . $tag . '>' . "\n";
foreach($array AS $key => $value) {
$key = str::lower($key);
if(is_array($value)) {
$mtags = false;
foreach($value AS $key2 => $value2) {
if(is_array($value2)) {
$result .= self::xml($value2, $key, $head, $charset, $tab, $nlevel);
} else if(trim($value2) != '') {
$value2 = (htmlspecialchars($value2) != $value2) ? '<![CDATA[' . $value2 . ']]>' : $value2;
$result .= str_repeat($tab, $nlevel) . '<' . $key . '>' . $value2 . '</' . $key . '>' . "\n";
}
$mtags = true;
}
if(!$mtags && count($value) > 0) {
$result .= self::xml($value, $key, $head, $charset, $tab, $nlevel);
}
} else if(trim($value) != '') {
$value = (htmlspecialchars($value) != $value) ? '<![CDATA[' . $value . ']]>' : $value;
$result .= str_repeat($tab, $nlevel) . '<' . $key . '>' . $value . '</' . $key . '>' . "\n";
}
}
return $result . str_repeat($tab, $level) . '</' . $tag . '>' . "\n";
}
/**
* Extracts a single column from an array
*
* @param array $array The source array
* @param string $key The key name of the column to extract
* @return array The result array with all values from that column.
*/
static function extract($array, $key) {
$output = array();
foreach($array AS $a) if(isset($a[$key])) $output[] = $a[ $key ];
return $output;
}
/**
* Shuffles an array and keeps the keys
*
* @param array $array The source array
* @return array The shuffled result array
*/
static function shuffle($array) {
$keys = array_keys($array);
shuffle($keys);
return array_merge(array_flip($keys), $array);
}
/**
* Returns the first element of an array
*
* I always have to lookup the names of that function
* so I decided to make this shortcut which is
* easier to remember.
*
* @param array $array The source array
* @return mixed The first element
*/
static function first($array) {
return array_shift($array);
}
/**
* Returns the last element of an array
*
* I always have to lookup the names of that function
* so I decided to make this shortcut which is
* easier to remember.
*
* @param array $array The source array
* @return mixed The last element
*/
static function last($array) {
return array_pop($array);
}
/**
* Search for elements in an array by regular expression
*
* @param array $array The source array
* @param string $search The regular expression
* @return array The array of results
*/
static function search($array, $search) {
return preg_grep('#' . preg_quote($search) . '#i' , $array);
}
/**
* Checks if an array contains a certain string
*
* @param array $array The source array
* @param string $search The string to search for
* @return boolean true: the array contains the string, false: it doesn't
*/
static function contains($array, $search) {
$search = self::search($array, $search);
return (empty($search)) ? false : true;
}
/**
* Fills an array up with additional elements to certain amount.
*
* @param array $array The source array
* @param int $limit The number of elements the array should contain after filling it up.
* @param mixed $fill The element, which should be used to fill the array
* @return array The filled-up result array
*/
static function fill($array, $limit, $fill='placeholder') {
if(count($array) < $limit) {
$diff = $limit-count($array);
for($x=0; $x<$diff; $x++) $array[] = $fill;
}
return $array;
}
/**
* Checks for missing elements in an array
*
* This is very handy to check for missing
* user values in a request for example.
*
* @param array $array The source array
* @param array $required An array of required keys
* @return array An array of missing fields. If this is empty, nothing is missing.
*/
static function missing($array, $required=array()) {
$missing = array();
foreach($required AS $r) {
if(empty($array[$r])) $missing[] = $r;
}
return $missing;
}
/**
* Sorts a multi-dimensional array by a certain column
*
* @param array $array The source array
* @param string $field The name of the column
* @param string $direction desc (descending) or asc (ascending)
* @param const $method A PHP sort method flag.
* @return array The sorted array
*/
static function sort($array, $field, $direction='desc', $method=SORT_REGULAR) {
$direction = (strtolower($direction) == 'desc') ? SORT_DESC : SORT_ASC;
$helper = array();
foreach($array as $key => $row) {
$helper[$key] = (is_object($row)) ? (method_exists($row, $field)) ? str::lower($row->$field()) : str::lower($row->$field) : str::lower($row[$field]);
}
array_multisort($helper, $direction, $method, $array);
return $array;
}
}
/**
*
* Browser
*
* Browser sniffing is bad - I know!
* But sometimes this class is very helpful to
* react on certain browsers and build browser-specific
* css selectors for example. It's up to you to use it.
*
* @package Kirby
*/
class browser {
/**
* The entire user agent string
*
* @var string
*/
static public $ua = false;
/**
* The readable name of the browser
* For example: "ie"
*
* @var string
*/
static public $name = false;
/**
* The readable browser engine name
* For example: "webkit"
*
* @var string
*/
static public $engine = false;
/**
* The browser version number
* For example: "3.6"
*
* @var string
*/
static public $version = false;
/**
* The platform name
* For example: "mac"
*
* @var string
*/
static public $platform = false;
/**
* True or false if it is a mobile device or not
*
* @var boolean
*/
static public $mobile = false;
/**
* True or false if it is an iOS device or not
*
* @var boolean
*/
static public $ios = false;
/**
* True or false if it is an iPhone or not
*
* @var boolean
*/
static public $iphone = false;
/**
* Returns the name of the browser
*
* @param string $ua The user agent string
* @return string The browser name
*/
static function name($ua=null) {
self::detect($ua);
return self::$name;
}
/**
* Returns the browser engine
*
* @param string $ua The user agent string
* @return string The browser engine
*/
static function engine($ua=null) {
self::detect($ua);
return self::$engine;
}
/**
* Returns the browser version
*
* @param string $ua The user agent string
* @return string The browser version
*/
static function version($ua=null) {
self::detect($ua);
return self::$version;
}
/**
* Returns the platform
*
* @param string $ua The user agent string
* @return string The platform name
*/
static function platform($ua=null) {
self::detect($ua);
return self::$platform;
}
/**
* Checks if the user agent string is from a mobile device
*
* @param string $ua The user agent string
* @return boolean True: mobile device, false: not a mobile device
*/
static function mobile($ua=null) {
self::detect($ua);
return self::$mobile;
}
/**
* Checks if the user agent string is from an iPhone
*
* @param string $ua The user agent string
* @return boolean True: iPhone, false: not an iPhone
*/
static function iphone($ua=null) {
self::detect($ua);
return self::$iphone;
}
/**
* Checks if the user agent string is from an iOS device
*
* @param string $ua The user agent string
* @return boolean True: iOS device, false: not an iOS device
*/
static function ios($ua=null) {
self::detect($ua);
return self::$ios;
}
/**
* Returns a browser-specific css selector string
*
* @param string $ua The user agent string
* @param boolean $array True: return an array, false: return a string
* @return mixed
*/
static function css($ua=null, $array=false) {
self::detect($ua);
$css[] = self::$engine;
$css[] = self::$name;
if(self::$version) $css[] = self::$name . str_replace('.', '_', self::$version);
$css[] = self::$platform;
return ($array) ? $css : implode(' ', $css);
}
/**
* The core detection method, which parses the user agent string
*
* @todo add new browser versions
* @param string $ua The user agent string
* @return array An array with all parsed info
*/
static function detect($ua=null) {
$ua = ($ua) ? str::lower($ua) : str::lower(server::get('http_user_agent'));
// don't do the detection twice
if(self::$ua == $ua) return array(
'name' => self::$name,
'engine' => self::$engine,
'version' => self::$version,
'platform' => self::$platform,
'agent' => self::$ua,
'mobile' => self::$mobile,
'iphone' => self::$iphone,
'ios' => self::$ios,
);
self::$ua = $ua;
self::$name = false;
self::$engine = false;
self::$version = false;
self::$platform = false;
// browser
if(!preg_match('/opera|webtv/i', self::$ua) && preg_match('/msie\s(\d)/', self::$ua, $array)) {
self::$version = $array[1];
self::$name = 'ie';
self::$engine = 'trident';
} else if(strstr(self::$ua, 'firefox/3.6')) {
self::$version = 3.6;
self::$name = 'fx';
self::$engine = 'gecko';
} else if (strstr(self::$ua, 'firefox/3.5')) {
self::$version = 3.5;
self::$name = 'fx';
self::$engine = 'gecko';
} else if(preg_match('/firefox\/(\d+)/i', self::$ua, $array)) {
self::$version = $array[1];
self::$name = 'fx';
self::$engine = 'gecko';
} else if(preg_match('/opera(\s|\/)(\d+)/', self::$ua, $array)) {
self::$engine = 'presto';
self::$name = 'opera';
self::$version = $array[2];
} else if(strstr(self::$ua, 'konqueror')) {
self::$name = 'konqueror';
self::$engine = 'webkit';
} else if(strstr(self::$ua, 'iron')) {
self::$name = 'iron';
self::$engine = 'webkit';
} else if(strstr(self::$ua, 'chrome')) {
self::$name = 'chrome';
self::$engine = 'webkit';
if(preg_match('/chrome\/(\d+)/i', self::$ua, $array)) { self::$version = $array[1]; }
} else if(strstr(self::$ua, 'applewebkit/')) {
self::$name = 'safari';
self::$engine = 'webkit';
if(preg_match('/version\/(\d+)/i', self::$ua, $array)) { self::$version = $array[1]; }
} else if(strstr(self::$ua, 'mozilla/')) {
self::$engine = 'gecko';
self::$name = 'fx';
}
// platform
if(strstr(self::$ua, 'j2me')) {
self::$platform = 'mobile';
} else if(strstr(self::$ua, 'iphone')) {
self::$platform = 'iphone';
} else if(strstr(self::$ua, 'ipod')) {
self::$platform = 'ipod';
} else if(strstr(self::$ua, 'ipad')) {
self::$platform = 'ipad';
} else if(strstr(self::$ua, 'mac')) {
self::$platform = 'mac';
} else if(strstr(self::$ua, 'darwin')) {
self::$platform = 'mac';
} else if(strstr(self::$ua, 'webtv')) {
self::$platform = 'webtv';
} else if(strstr(self::$ua, 'win')) {
self::$platform = 'win';
} else if(strstr(self::$ua, 'freebsd')) {
self::$platform = 'freebsd';
} else if(strstr(self::$ua, 'x11') || strstr(self::$ua, 'linux')) {
self::$platform = 'linux';
}
self::$mobile = (self::$platform == 'mobile') ? true : false;
self::$iphone = (in_array(self::$platform, array('ipod', 'iphone'))) ? true : false;
self::$ios = (in_array(self::$platform, array('ipod', 'iphone', 'ipad'))) ? true : false;
return array(
'name' => self::$name,
'engine' => self::$engine,
'version' => self::$version,
'platform' => self::$platform,
'agent' => self::$ua,
'mobile' => self::$mobile,
'iphone' => self::$iphone,
'ios' => self::$ios,
);
}
}
/**
*
* Config
*
* This is the core class to handle
* configuration values/constants.
*
* @package Kirby
*/
class c {
/**
* The static config array
* It contains all config values
*
* @var array
*/
private static $config = array();
/**
* Gets a config value by key
*
* @param string $key The key to look for. Pass false to get the entire config array
* @param mixed $default The default value, which will be returned if the key has not been found
* @return mixed The found config value
*/
static function get($key=null, $default=null) {
if(empty($key)) return self::$config;
return a::get(self::$config, $key, $default);
}
/**
* Sets a config value by key
*
* @param string $key The key to define
* @param mixed $value The value for the passed key
*/
static function set($key, $value=null) {
if(is_array($key)) {
// set all new values
self::$config = array_merge(self::$config, $key);
} else {
self::$config[$key] = $value;
}
}
/**
* Loads an additional config file
* Returns the entire configuration array
*
* @param string $file The path to the config file
* @return array The entire config array
*/
static function load($file) {
if(file_exists($file)) require_once($file);
return c::get();
}
}
/**
*
* Content
*
* This class handles output buffering,
* content loading and setting content type headers.
*
* @package Kirby
*/
class content {
/**
* Starts the output buffer
*
*/
static function start() {
ob_start();
}
/**
* Stops the output buffer
* and flush the content or return it.
*
* @param boolean $return Pass true to return the content instead of flushing it
* @return mixed
*/
static function end($return=false) {
if($return) {
$content = ob_get_contents();
ob_end_clean();
return $content;
}
ob_end_flush();
}
/**
* Loads content from a passed file
*
* @param string $file The path to the file
* @param boolean $return True: return the content of the file, false: echo the content
* @return mixed
*/
static function load($file, $return=true) {
self::start();
require_once($file);
$content = self::end(true);
if($return) return $content;
echo $content;
}
/**
* Simplifies setting content type headers
*
* @param string $ctype The shortcut for the content type. See the keys of the $ctypes array for all available shortcuts
* @param string $charset The charset definition for the content type header. Default is "utf-8"
*/
static function type() {
$args = func_get_args();
// shortcuts for content types
$ctypes = array(
'html' => 'text/html',
'css' => 'text/css',
'js' => 'text/javascript',
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'json' => 'application/json'
);
$ctype = a::get($args, 0, c::get('content_type', 'text/html'));
$ctype = a::get($ctypes, $ctype, $ctype);
$charset = a::get($args, 1, c::get('charset', 'utf-8'));
header('Content-type: ' . $ctype . '; charset=' . $charset);
}
}
/**
*
* Cookie
*
* This class makes cookie handling easy
*
* @package Kirby
*/
class cookie {
/**
* Set a new cookie
*
* @param string $key The name of the cookie
* @param string $value The cookie content
* @param int $expires The number of seconds until the cookie expires
* @param string $domain The domain to set this cookie for.
* @return boolean true: the cookie has been created, false: cookie creation failed
*/
static function set($key, $value, $expires=3600, $domain='/') {
if(is_array($value)) $value = a::json($value);
$_COOKIE[$key] = $value;
return @setcookie($key, $value, time()+$expires, $domain);
}
/**
* Get a cookie value
*
* @param string $key The name of the cookie
* @param string $default The default value, which should be returned if the cookie has not been found
* @return mixed The found value
*/
static function get($key, $default=null) {
return a::get($_COOKIE, $key, $default);
}
/**
* Remove a cookie
*
* @param string $key The name of the cookie
* @param string $domain The domain of the cookie
* @return mixed true: the cookie has been removed, false: the cookie could not be removed
*/
static function remove($key, $domain='/') {
$_COOKIE[$key] = false;
return @setcookie($key, false, time()-3600, $domain);
}
}
/**
*
* Database
*
* Database handling sucks - not with this class :)
*
* Configure your database connection like this:
*
* <code>
* c::set('db.host', 'localhost');
* c::set('db.user', 'root');
* c::set('db.password', '');
* c::set('db.name', 'mydb');
* c::set('db.prefix', '');
* </code>
*
* @package Kirby
*/
class db {
/**
* Traces all db queries
*
* @var array
*/
public static $trace = array();
/**
* The connection resource
*
* @var mixed
*/
private static $connection = false;
/**
* The selected database
*
* @var string
*/
private static $database = false;
/**
* The used charset
* Default is "utf8"
*
* @var string
*/
private static $charset = false;
/**
* The last used query
*
* @var string
*/
private static $last_query = false;
/**
* The number of affected rows
* for the last query
*
* @var int
*/
private static $affected = 0;
/**
* The core connection method
* Tries to connect to the server
* Selects the database and sets the charset
*
* It will only connect once and return
* that same connection for all following queries
*
* @return mixed
*/
static function connect() {
$connection = self::connection();
$args = func_get_args();
$host = a::get($args, 0, c::get('db.host', 'localhost'));
$user = a::get($args, 1, c::get('db.user', 'root'));
$password = a::get($args, 2, c::get('db.password'));
$database = a::get($args, 3, c::get('db.name'));
$charset = a::get($args, 4, c::get('db.charset', 'utf8'));
// don't connect again if it's already done
$connection = (!$connection) ? @mysql_connect($host, $user, $password) : $connection;
// react on connection failures
if(!$connection) return self::error(l::get('db.errors.connect', 'Database connection failed'), true);
self::$connection = $connection;
// select the database
$database = self::database($database);
if(error($database)) return $database;