add extension checking to config
[Pman.Core] / Config.php
1 <?php
2
3 // default framework settings for release
4
5 class Pman_Core_Config {
6     
7     
8     var $memory_limit = 0;
9     
10     var $defaults = array(  ); // override... 
11     
12     
13     // note if other extended 'config's require more, then you porbably need to include these first.
14     var $required_extensions = array(
15         'json',        
16         'curl',
17         'gd',
18         'mbstring'
19     );
20     
21     function init($ff, $cfg)
22     {
23       
24         $cfg = $this->overlayDefaults($cfg);
25         
26         if (!empty($this->memory_limit)) {
27             $mem = ini_get('memory_limit');
28             if (php_sapi_name() != 'cli' && $this->to_bytes($mem) < $this->to_bytes($this->memory_limit)) {
29                 die("increase the memory limit settings to 2048M or more");
30             }
31         
32         }
33         $this->verifyExtensions();
34         
35         return $cfg;
36     }
37     
38     function to_bytes($val) {
39         $val = trim($val);
40         $last = strtolower($val[strlen($val)-1]);
41         switch($last) {
42             // The 'G' modifier is available since PHP 5.1.0
43             case 'g':
44                 $val = substr($val, 0, -1);
45                 $val *= 1024;
46             case 'm':
47                 $val = substr($val, 0, -1);
48                 $val *= 1024;
49                 $val = substr($val, 0, -1);
50             case 'k':
51                 $val *= 1024;
52         }
53     
54         return $val;
55     }
56     function overlayDefaults($cfg)
57     {
58         if (isset($this->defaults['disable']) && is_array($this->defaults['disable']) ) {
59             $this->defaults['disable'] = implode(',', $this->defaults['disable']);
60         }
61         foreach($this->defaults as $k=>$v) {
62             if (is_array($v)) {
63                 
64                 if (!isset($cfg[$k])) {
65                     $cfg[$k] = $v;
66                     continue;
67                 }
68                 
69                 foreach($v as $kk=>$vv) {
70                     if (isset($cfg[$k][$kk])) {
71                         continue;
72                     }
73                     
74                     $cfg[$k][$kk] = $vv;
75                 }
76             }
77             
78             if (!isset($cfg[$k])) {
79                 $cfg[$k] = $v;
80             }
81         }
82         
83         return $cfg;
84     }
85     function verifyExtensions()
86     {
87         $error = array();
88         
89         foreach ($this->required_extensions as $e){
90             
91             if(empty($e) || extension_loaded($e)) {
92                 continue;
93             }
94             
95             $error[] = "Error: Please install php extension: {$e}";
96         }
97         
98         if(empty($error)){
99            return true; 
100         }
101         die(implode('\n', $error));
102     }
103
104 }