Commit 0822235b5f688d5177f97b6a8dc2909df606e0ff
- Diff rendering mode:
- inline
- side by side
Belokan.php
(21 / 20)
|   | |||
| 11 | 11 | class Belokan { | |
| 12 | 12 | ||
| 13 | 13 | private | |
| 14 | $_config, | ||
| 15 | $_request; | ||
| 14 | $_config; | ||
| 16 | 15 | ||
| 17 | 16 | public function __construct ($config_file=null) | |
| 18 | 17 | { | |
| 19 | spl_autoload_register(array($this, 'autoloader')); | ||
| 18 | spl_autoload_register('Belokan::autoloader'); | ||
| 20 | 19 | $this->_config = Belokan_Config::getInstance(); | |
| 21 | 20 | if ($config_file != null) $this->_config->loadFile($config_file); | |
| 22 | $this->_request = Belokan_Request::getInstance(); | ||
| 23 | 21 | } | |
| 24 | 22 | ||
| 25 | 23 | public function run () | |
| 26 | 24 | { | |
| 27 | 25 | $logBelokan = $this->_config->get('log', 'belokan'); | |
| 28 | 26 | $log = Belokan_Log::getInstance(); | |
| 29 | $ctrl = $this->_request->getController(); | ||
| 30 | $action = $this->_request->getAction(); | ||
| 27 | $req = Belokan_Request::getInstance(); | ||
| 28 | $ctrl = $req->getController(); | ||
| 29 | $action = $req->getAction(); | ||
| 31 | 30 | $ctrlClass = $ctrl.'Controller'; | |
| 32 | 31 | $actionMethod = $action.'Action'; | |
| 33 | 32 | $RESTCtrl = false; | |
| 34 | |||
| 33 | |||
| 35 | 34 | if (!class_exists($ctrlClass)) { | |
| 36 | 35 | if ($logBelokan) | |
| 37 | $log->add('Belokan', '['.date('Y-m-d H:i').'] Request undefined controller ('.$ctrl.'). HTTP 404 error generated.'); | ||
| 36 | $log->add('Belokan', 'Request unknown controller ('.$ctrl.'). HTTP 404 error generated.'); | ||
| 38 | 37 | $actionMethod = 'e404'; | |
| 39 | 38 | $ctrlInstance = new HttpErrorsController('HttpErrors', 'e404'); | |
| 40 | 39 | } else { | |
| 41 | 40 | $ctrlInstance = new $ctrlClass($ctrl, $action); | |
| 42 | 41 | $RESTCtrl = $ctrlInstance instanceof Belokan_REST_Controller; | |
| 43 | |||
| 44 | if (!$RESTCtrl && !method_exists($ctrlClass, $actionMethod)) { | ||
| 42 | |||
| 43 | if (0 && !$RESTCtrl && !method_exists($ctrlClass, $actionMethod)) { | ||
| 45 | 44 | if ($logBelokan) | |
| 46 | $log->add('Belokan', '['.date('Y-m-d H:i').'] Request undefined action ('.$action.') for controller '.$ctrl.'. HTTP 404 error generated.'); | ||
| 45 | $log->add('Belokan', 'Request unknown action ('.$action.') for controller '.$ctrl.'. HTTP 404 error generated.'); | ||
| 47 | 46 | $actionMethod = 'e404'; | |
| 48 | 47 | $ctrlInstance = new HttpErrorsController('HttpErrors', 'e404'); | |
| 49 | 48 | } | |
| … | … | ||
| 50 | 50 | ||
| 51 | 51 | $ctrlInstance->_pre_action(); | |
| 52 | 52 | if ($RESTCtrl) | |
| 53 | call_user_func(array($ctrlInstance, strtolower($this->_request->getMethod())), $action); | ||
| 53 | call_user_func(array($ctrlInstance, strtolower($req->getMethod())), $action); | ||
| 54 | 54 | else | |
| 55 | 55 | call_user_func(array($ctrlInstance, $actionMethod)); | |
| 56 | 56 | $ctrlInstance->_post_action(); | |
| … | … | ||
| 64 | 64 | ||
| 65 | 65 | public static function autoloader ($class_name) | |
| 66 | 66 | { | |
| 67 | static $config = null; | ||
| 68 | if ($config == null) | ||
| 69 | $config = Belokan_Config::getInstance(); | ||
| 70 | |||
| 67 | 71 | if (substr($class_name, 0, 8) == 'Belokan_') | |
| 68 | 72 | $classPath = dirname(__FILE__).str_replace('_', '/', substr($class_name, 7)).'.php'; | |
| 69 | 73 | else if (substr($class_name, -10) == 'Controller') | |
| 70 | $classPath = Belokan_Config::getInstance()->get('dir', 'controllers').substr($class_name, 0, -10).'.php'; | ||
| 74 | $classPath = $config->get('dir', 'controllers').substr($class_name, 0, -10).'.php'; | ||
| 71 | 75 | else { | |
| 72 | $classPath = Belokan_Config::getInstance()->get('dir', 'models').str_replace('_', '/', $class_name).'.php'; | ||
| 73 | if (!file_exists($classPath) && Belokan_Config::getInstance()->exists('dir', 'lib')) { | ||
| 74 | $classPath = Belokan_Config::getInstance()->get('dir', 'lib').str_replace('_', '/', $class_name).'.php'; | ||
| 75 | } | ||
| 76 | $classPath = $config->get('dir', 'models').str_replace('_', '/', $class_name).'.php'; | ||
| 77 | if (!file_exists($classPath) && $config->exists('dir', 'lib')) | ||
| 78 | $classPath = $config->get('dir', 'lib').str_replace('_', '/', $class_name).'.php'; | ||
| 76 | 79 | } | |
| 77 | 80 | ||
| 78 | 81 | if (file_exists($classPath)) { | |
| 79 | 82 | require_once $classPath; | |
| 80 | 83 | return true; | |
| 81 | 84 | } else { | |
| 82 | if (Belokan_Config::getInstance()->get('log', 'belokan')) { | ||
| 83 | Belokan_Log::getInstance()->add('Belokan', '['.date('Y-m-d H:i').'] ERROR: Trying to load an inexistant class ('.$class_name.').'); | ||
| 84 | } | ||
| 85 | if ($config->get('log', 'belokan')) | ||
| 86 | Belokan_Log::getInstance()->add('Belokan', 'ERROR: Trying to load an inexistant class ('.$class_name.').'); | ||
| 85 | 87 | return false; | |
| 86 | 88 | } | |
| 87 | 89 | } |
Cache.php
(7 / 10)
|   | |||
| 16 | 16 | $_updateValue, | |
| 17 | 17 | $_buffer; | |
| 18 | 18 | ||
| 19 | public function __construct ($key, $cond_type='time', $cond_value='3600') | ||
| 19 | public function __construct ($key=null, $cond_type='time', $cond_value='3600') | ||
| 20 | 20 | { | |
| 21 | 21 | $config = Belokan_Config::getInstance(); | |
| 22 | 22 | ||
| 23 | if ($key === null) $key = md5($_SERVER['REQUEST_URI']); | ||
| 23 | if ($key == null) $key = md5($_SERVER['REQUEST_URI']); | ||
| 24 | 24 | ||
| 25 | 25 | if ($config->exists('dir', 'cache')) | |
| 26 | 26 | $this->_file = $config->get('dir', 'cache').$key.'.cache'; | |
| 27 | 27 | else if ($config->get('log', 'belokan')) { | |
| 28 | echo $err = 'ERROR: No cache directory specified in the configuration.'; | ||
| 29 | Belokan_Log::getInstance()->add('Belokan', '['.date('Y-m-d H:i').']'.$err); | ||
| 28 | Belokan_Log::getInstance()->add('Belokan', 'ERROR: No cache directory specified in the configuration.'); | ||
| 30 | 29 | exit; | |
| 31 | 30 | } | |
| 32 | 31 | ||
| … | … | ||
| 39 | 39 | { | |
| 40 | 40 | if (!file_exists($this->_file)) return false; | |
| 41 | 41 | if ($this->_updateType == 'forced') return false; | |
| 42 | if ($this->_updateType == 'time' && | ||
| 43 | (time() - filemtime($this->_file)) | ||
| 44 | > $this->_updateValue) return false; | ||
| 45 | if ($this->_updateType == 'date' && | ||
| 46 | date($this->_updateValue, filemtime($this->_file)) | ||
| 47 | != date($this->_updateValue)) return false; | ||
| 42 | if ($this->_updateType == 'time' && (time() - filemtime($this->_file)) > $this->_updateValue) | ||
| 43 | return false; | ||
| 44 | if ($this->_updateType == 'date' && date($this->_updateValue, filemtime($this->_file)) != date($this->_updateValue)) | ||
| 45 | return false; | ||
| 48 | 46 | ||
| 49 | 47 | readfile($this->_file); | |
| 50 | 48 | return true; |
Config.php
(5 / 8)
|   | |||
| 16 | 16 | ||
| 17 | 17 | public static function getInstance () | |
| 18 | 18 | { | |
| 19 | if (null === self::$_instance) | ||
| 19 | if (self::$_instance === null) | ||
| 20 | 20 | self::$_instance = new self; | |
| 21 | 21 | return self::$_instance; | |
| 22 | 22 | } | |
| … | … | ||
| 29 | 29 | 'models' => '../models/', | |
| 30 | 30 | 'views' => '../views/', | |
| 31 | 31 | 'langs' => '../langs/', | |
| 32 | 'logs' => '../log/' | ||
| 32 | 'logs' => '../logs/' | ||
| 33 | 33 | ); | |
| 34 | 34 | $this->_config['log'] = array( | |
| 35 | 35 | 'autosave' => true, | |
| 36 | 36 | 'belokan' => true | |
| 37 | 37 | ); | |
| 38 | $this->_config['cache'] = array( | ||
| 39 | 'enabled' => false | ||
| 40 | ); | ||
| 41 | 38 | $this->_config['format'] = array( | |
| 42 | 39 | 'enabled' => true, | |
| 43 | 40 | 'default' => 'html' | |
| … | … | ||
| 48 | 48 | { | |
| 49 | 49 | if (!file_exists($config_file)) { | |
| 50 | 50 | if (Belokan_Config::getInstance()->get('log', 'belokan')) | |
| 51 | Belokan_Log::getInstance()->add('Belokan', '['.date('Y-m-d H:i').'] ERROR: configuration file not found ('.$config_file.').'); | ||
| 51 | Belokan_Log::getInstance()->add('Belokan', 'ERROR: configuration file not found ('.$config_file.').'); | ||
| 52 | 52 | return false; | |
| 53 | 53 | } | |
| 54 | 54 | $userConfig = include $config_file; | |
| … | … | ||
| 66 | 66 | { | |
| 67 | 67 | if (!$this->exists($group, $key)) return null; | |
| 68 | 68 | if ($key != null) return $this->_config[$group][$key]; | |
| 69 | else return $this->_config[$group]; | ||
| 69 | return $this->_config[$group]; | ||
| 70 | 70 | } | |
| 71 | 71 | ||
| 72 | 72 | public function exists ($group, $key=null) | |
| 73 | 73 | { | |
| 74 | 74 | if ($key != null) return isset($this->_config[$group][$key]); | |
| 75 | else return isset($this->_config[$group]); | ||
| 75 | return isset($this->_config[$group]); | ||
| 76 | 76 | } | |
| 77 | 77 | ||
| 78 | 78 | private function merge ($user_config) |
Controller.php
(6 / 6)
|   | |||
| 17 | 17 | $_layout, | |
| 18 | 18 | $_controller, | |
| 19 | 19 | $_action, | |
| 20 | $_datas; | ||
| 20 | $_data; | ||
| 21 | 21 | ||
| 22 | 22 | public function __construct ($controller, $action) | |
| 23 | 23 | { | |
| … | … | ||
| 25 | 25 | $this->_layout = 'default'; | |
| 26 | 26 | $this->_controller = strtolower($controller); | |
| 27 | 27 | $this->_action = strtolower($action); | |
| 28 | $this->_datas = array(); | ||
| 28 | $this->_data = array(); | ||
| 29 | 29 | } | |
| 30 | 30 | ||
| 31 | 31 | public function __get ($var) | |
| 32 | 32 | { | |
| 33 | if (isset($this->_datas[$var])) return $this->_datas[$var]; | ||
| 33 | if (isset($this->_data[$var])) return $this->_data[$var]; | ||
| 34 | 34 | else return null; | |
| 35 | 35 | } | |
| 36 | 36 | ||
| 37 | 37 | public function __set ($var, $value) | |
| 38 | 38 | { | |
| 39 | return $this->_datas[$var] = $value; | ||
| 39 | return $this->_data[$var] = $value; | ||
| 40 | 40 | } | |
| 41 | 41 | ||
| 42 | 42 | public function __isset ($var) | |
| 43 | 43 | { | |
| 44 | return isset($this->_datas[$var]); | ||
| 44 | return isset($this->_data[$var]); | ||
| 45 | 45 | } | |
| 46 | 46 | ||
| 47 | 47 | public function __unset ($var) | |
| 48 | 48 | { | |
| 49 | unset($this->_datas[$var]); | ||
| 49 | unset($this->_data[$var]); | ||
| 50 | 50 | } | |
| 51 | 51 | ||
| 52 | 52 | public function setLayout ($layout_name) |
Db/MySQL.php
(21 / 28)
|   | |||
| 18 | 18 | ||
| 19 | 19 | public function connect () | |
| 20 | 20 | { | |
| 21 | if ($this->_persistent) $this->_connection = mysql_pconnect($this->_host, | ||
| 22 | $this->_user, | ||
| 23 | $this->_pass); | ||
| 24 | else $this->_connection = mysql_connect($this->_host, | ||
| 25 | $this->_user, | ||
| 26 | $this->_pass); | ||
| 21 | if ($this->_persistent) | ||
| 22 | $this->_connection = mysql_pconnect($this->_host, $this->_user, $this->_pass); | ||
| 23 | else | ||
| 24 | $this->_connection = mysql_connect($this->_host, $this->_user, $this->_pass); | ||
| 27 | 25 | mysql_select_db($this->_base, $this->_connection); | |
| 28 | 26 | } | |
| 29 | 27 | ||
| … | … | ||
| 51 | 51 | ||
| 52 | 52 | public function fetchRows ($fetch_object=false) | |
| 53 | 53 | { | |
| 54 | if ($this->_results != null) { | ||
| 55 | $this->_rows = array(); | ||
| 56 | if ($fetch_object) | ||
| 57 | while ($row = mysql_fetch_object($this->_results)) | ||
| 58 | $this->_rows[] = $row; | ||
| 59 | else | ||
| 60 | while ($row = mysql_fetch_array($this->_results)) | ||
| 61 | $this->_rows[] = $row; | ||
| 62 | return $this->_rows; | ||
| 63 | } else return array(); | ||
| 54 | if ($this->_results == null) return null; | ||
| 55 | $this->_rows = array(); | ||
| 56 | if ($fetch_object) | ||
| 57 | while ($row = mysql_fetch_object($this->_results)) | ||
| 58 | $this->_rows[] = $row; | ||
| 59 | else | ||
| 60 | while ($row = mysql_fetch_array($this->_results)) | ||
| 61 | $this->_rows[] = $row; | ||
| 62 | return $this->_rows; | ||
| 64 | 63 | } | |
| 65 | 64 | ||
| 66 | 65 | public function fetchRow ($fetch_object=false) | |
| 67 | 66 | { | |
| 68 | if ($this->_results != null) { | ||
| 69 | if ($fetch_object) return mysql_fetch_object($this->_results); | ||
| 70 | else return mysql_fetch_array($this->_results); | ||
| 71 | } else return null; | ||
| 67 | if ($this->_results == null) return null; | ||
| 68 | if ($fetch_object) return mysql_fetch_object($this->_results); | ||
| 69 | return mysql_fetch_array($this->_results); | ||
| 72 | 70 | } | |
| 73 | 71 | ||
| 74 | 72 | public function numberOfRows () | |
| 75 | 73 | { | |
| 76 | if (strtolower(substr($this->_query, 0, 6)) == 'select' && | ||
| 77 | $this->_results != null) | ||
| 74 | if (strtolower(substr($this->_query, 0, 6)) == 'select' && $this->_results != null) | ||
| 78 | 75 | return mysql_num_rows($this->_results); | |
| 79 | 76 | else if ($this->_results === true) | |
| 80 | 77 | return $this->affectedRows(); | |
| 81 | else return null; | ||
| 78 | return null; | ||
| 82 | 79 | } | |
| 83 | 80 | ||
| 84 | 81 | public function affectedRows () | |
| 85 | 82 | { | |
| 86 | if (in_array(strtolower(substr($this->_query, 0, 7)), | ||
| 87 | array('insert ', 'update ', 'replace', 'delete '))) | ||
| 83 | if (in_array(strtolower(substr($this->_query, 0, 7)), array('insert ', 'update ', 'replace', 'delete '))) | ||
| 88 | 84 | return mysql_affected_rows($this->_connection); | |
| 89 | else return null; | ||
| 85 | return null; | ||
| 90 | 86 | } | |
| 91 | 87 | ||
| 92 | 88 | public function freeResults () | |
| 93 | 89 | { | |
| 94 | if (strtolower(substr($this->_query, 0, 6)) == 'select' && | ||
| 95 | $this->_results != null) | ||
| 90 | if (strtolower(substr($this->_query, 0, 6)) == 'select' && $this->_results != null) | ||
| 96 | 91 | mysql_free_result($this->_results); | |
| 97 | 92 | } | |
| 98 | 93 |
Db/SQLite.php
(16 / 20)
|   | |||
| 50 | 50 | ||
| 51 | 51 | public function fetchRows ($fetch_object=false) | |
| 52 | 52 | { | |
| 53 | if ($this->_results != null) { | ||
| 54 | $this->_rows = array(); | ||
| 55 | if ($fetch_object) | ||
| 56 | while ($row = sqlite_fetch_object($this->_results)) | ||
| 57 | $this->_rows[] = $row; | ||
| 58 | else | ||
| 59 | while ($row = sqlite_fetch_array($this->_results)) | ||
| 60 | $this->_rows[] = $row; | ||
| 61 | return $this->_rows; | ||
| 62 | } else return null; | ||
| 53 | if ($this->_results == null) return null; | ||
| 54 | $this->_rows = array(); | ||
| 55 | if ($fetch_object) | ||
| 56 | while ($row = sqlite_fetch_object($this->_results)) | ||
| 57 | $this->_rows[] = $row; | ||
| 58 | else | ||
| 59 | while ($row = sqlite_fetch_array($this->_results)) | ||
| 60 | $this->_rows[] = $row; | ||
| 61 | return $this->_rows; | ||
| 63 | 62 | } | |
| 64 | 63 | ||
| 65 | 64 | public function fetchRow ($fetch_object=false) | |
| 66 | 65 | { | |
| 67 | if ($this->_results != null) { | ||
| 68 | if ($fetch_object) return sqlite_fetch_object($this->_results); | ||
| 69 | else return sqlite_fetch_array($this->_results); | ||
| 70 | } else return null; | ||
| 66 | if ($this->_results == null) return null; | ||
| 67 | if ($fetch_object) return sqlite_fetch_object($this->_results); | ||
| 68 | return sqlite_fetch_array($this->_results); | ||
| 71 | 69 | } | |
| 72 | 70 | ||
| 73 | 71 | public function numberOfRows () | |
| 74 | 72 | { | |
| 75 | if (strtolower(substr($this->_query, 0, 6)) == 'select' && | ||
| 76 | $this->_results != null) | ||
| 73 | if (strtolower(substr($this->_query, 0, 6)) == 'select' && $this->_results != null) | ||
| 77 | 74 | return sqlite_num_rows($this->_results); | |
| 78 | 75 | else if ($this->_results === true) | |
| 79 | 76 | return $this->affectedRows(); | |
| 80 | else return null; | ||
| 77 | return null; | ||
| 81 | 78 | } | |
| 82 | 79 | ||
| 83 | 80 | public function affectedRows () | |
| 84 | 81 | { | |
| 85 | if (in_array(strtolower(substr($this->_query, 0, 7)), | ||
| 86 | array('insert ', 'update ', 'replace', 'delete '))) | ||
| 82 | if (in_array(strtolower(substr($this->_query, 0, 7)), array('insert ', 'update ', 'replace', 'delete '))) | ||
| 87 | 83 | return sqlite_changes($this->_connection); | |
| 88 | else return null; | ||
| 84 | return null; | ||
| 89 | 85 | } | |
| 90 | 86 | ||
| 91 | 87 | } |
Local.php
(12 / 13)
|   | |||
| 13 | 13 | static private | |
| 14 | 14 | $_instance, | |
| 15 | 15 | $_langs, | |
| 16 | $_datas, | ||
| 16 | $_data, | ||
| 17 | 17 | $_lang; | |
| 18 | 18 | ||
| 19 | 19 | public static function getInstance () | |
| 20 | 20 | { | |
| 21 | if (null === self::$_instance) | ||
| 21 | if (self::$_instance === null) | ||
| 22 | 22 | self::$_instance = new self; | |
| 23 | 23 | return self::$_instance; | |
| 24 | 24 | } | |
| … | … | ||
| 26 | 26 | public function __construct () | |
| 27 | 27 | { | |
| 28 | 28 | $this->_langs = array(); | |
| 29 | $this->_datas = array(); | ||
| 29 | $this->_data = array(); | ||
| 30 | 30 | $this->_lang = Belokan_Config::getInstance()->get('lang', 'default'); | |
| 31 | 31 | } | |
| 32 | 32 | ||
| … | … | ||
| 36 | 36 | ||
| 37 | 37 | if (strtolower($sourceType) == 'file') { | |
| 38 | 38 | if (file_exists($source)) | |
| 39 | $this->_datas[$lang] = include $source; | ||
| 39 | $this->_data[$lang] = include $source; | ||
| 40 | 40 | else { | |
| 41 | 41 | $source = Belokan_Config::getInstance()->get('dir', 'langs').$source; | |
| 42 | 42 | if (file_exists($source)) | |
| 43 | $this->_datas[$lang] = include $source; | ||
| 43 | $this->_data[$lang] = include $source; | ||
| 44 | 44 | else | |
| 45 | $log->add('Belokan', '['.date('Y-m-d H:i').'] Source file for language '.$lang.' not found ('.$source.').'); | ||
| 45 | $log->add('Belokan', 'Source file for language '.$lang.' not found ('.$source.').'); | ||
| 46 | 46 | } | |
| 47 | 47 | } else if (strtolower($sourceType) == 'mysql') { | |
| 48 | 48 | $base = new Belokan_Db_MySQL($source); | |
| … | … | ||
| 50 | 50 | $rows = $base->getRawResults(); | |
| 51 | 51 | if (mysql_num_rows($rows) > 0) | |
| 52 | 52 | while ($row = mysql_fetch_array($rows)) | |
| 53 | $this->_datas[$lang][$row['key']] = $row['value']; | ||
| 53 | $this->_data[$lang][$row['key']] = $row['value']; | ||
| 54 | 54 | else | |
| 55 | $log->add('Belokan', '['.date('Y-m-d H:i').'] Retrieve an empty rowset for language '.$lang.' from the MySQL database.'); | ||
| 55 | $log->add('Belokan', 'Retrieve an empty rowset for language '.$lang.' from the MySQL database.'); | ||
| 56 | 56 | } else if (strtolower($sourceType) == 'sqlite') { | |
| 57 | 57 | $base = new Belokan_Db_SQLite($source); | |
| 58 | 58 | $base->query('SELECT * FROM lang_'.$lang.';')->execute(); | |
| 59 | 59 | $rows = $base->getRawResults(); | |
| 60 | 60 | if (sqlite_num_rows($rows)) | |
| 61 | 61 | while ($row = sqlite_fetch_array($rows)) | |
| 62 | $this->_datas[$lang][$row['key']] = $row['value']; | ||
| 62 | $this->_data[$lang][$row['key']] = $row['value']; | ||
| 63 | 63 | else | |
| 64 | $log->add('Belokan', '['.date('Y-m-d H:i').'] Retrieve an empty rowset for language '.$lang.' from the SQLite database.'); | ||
| 64 | $log->add('Belokan', 'Retrieve an empty rowset for language '.$lang.' from the SQLite database.'); | ||
| 65 | 65 | } | |
| 66 | 66 | } | |
| 67 | 67 | ||
| … | … | ||
| 72 | 72 | ||
| 73 | 73 | public function get ($str) | |
| 74 | 74 | { | |
| 75 | if ($this->_lang == null || !isset($this->_datas[$this->_lang][$str])) | ||
| 75 | if ($this->_lang == null || !isset($this->_data[$this->_lang][$str])) | ||
| 76 | 76 | return $str; | |
| 77 | else | ||
| 78 | return $this->_datas[$this->_lang][$str]; | ||
| 77 | return $this->_data[$this->_lang][$str]; | ||
| 79 | 78 | } | |
| 80 | 79 | ||
| 81 | 80 | } |
Log.php
(10 / 6)
|   | |||
| 11 | 11 | class Belokan_Log { | |
| 12 | 12 | ||
| 13 | 13 | private static | |
| 14 | $_instance, | ||
| 14 | $_instance; | ||
| 15 | |||
| 16 | private | ||
| 15 | 17 | $_stacks, | |
| 16 | 18 | $_logs; | |
| 17 | 19 | ||
| 18 | 20 | public static function getInstance () | |
| 19 | 21 | { | |
| 20 | if (null === self::$_instance) | ||
| 22 | if (self::$_instance === null) | ||
| 21 | 23 | self::$_instance = new self; | |
| 22 | 24 | return self::$_instance; | |
| 23 | 25 | } | |
| … | … | ||
| 38 | 38 | public function add ($log, $str) | |
| 39 | 39 | { | |
| 40 | 40 | if (!in_array($log, $this->_logs)) $this->_logs[] = $log; | |
| 41 | $this->_stacks[$log][] = $str; | ||
| 41 | $this->_stacks[$log][] = '['.date('Y-m-d H:i').']'.$str; | ||
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | 44 | public function get ($log) | |
| … | … | ||
| 54 | 54 | foreach ($this->_logs as $log) { | |
| 55 | 55 | if (count($this->_stacks[$log]) == 0) continue; | |
| 56 | 56 | $tmp = ''; | |
| 57 | foreach ($this->_stacks[$log] as $elem) $tmp .= $elem."\n"; | ||
| 57 | foreach ($this->_stacks[$log] as $msg) $tmp .= $msg."\n"; | ||
| 58 | 58 | $logFile = fopen(Belokan_Config::getInstance()->get('dir', 'logs').$log.'.log', 'a'); | |
| 59 | fwrite($logFile, $tmp); | ||
| 60 | fclose($logFile); | ||
| 59 | if ($logFile !== false) { | ||
| 60 | fwrite($logFile, $tmp); | ||
| 61 | fclose($logFile); | ||
| 62 | } | ||
| 61 | 63 | } | |
| 62 | 64 | ||
| 63 | 65 | $this->_stacks = array(); |
Plugins.php
(6 / 6)
|   | |||
| 29 | 29 | if (file_exists($pluginFile)) require_once $pluginFile; | |
| 30 | 30 | } | |
| 31 | 31 | ||
| 32 | if (class_exists($plugin.'Plugin')) { | ||
| 33 | $plugin = $plugin.'Plugin'; | ||
| 34 | $this->_plugins[$plugin_name] = new $plugin; | ||
| 35 | $this->_loaded[] = $plugin_name; | ||
| 36 | } else return false; | ||
| 32 | if (!class_exists($plugin.'Plugin')) return false; | ||
| 37 | 33 | ||
| 34 | $plugin = $plugin.'Plugin'; | ||
| 35 | $this->_plugins[$plugin_name] = new $plugin; | ||
| 36 | $this->_loaded[] = $plugin_name; | ||
| 37 | |||
| 38 | 38 | return $this->_plugins[$plugin_name]; | |
| 39 | 39 | } | |
| 40 | 40 | ||
| … | … | ||
| 51 | 51 | public function get ($plugin_name) | |
| 52 | 52 | { | |
| 53 | 53 | if ($this->isLoaded($plugin_name)) return $this->_plugins[$plugin_name]; | |
| 54 | else return null; | ||
| 54 | return null; | ||
| 55 | 55 | } | |
| 56 | 56 | ||
| 57 | 57 | } |
Request.php
(3 / 5)
|   | |||
| 24 | 24 | ||
| 25 | 25 | public static function getInstance () | |
| 26 | 26 | { | |
| 27 | if (null === self::$_instance) { | ||
| 27 | if (self::$_instance === null) | ||
| 28 | 28 | self::$_instance = new self; | |
| 29 | } | ||
| 30 | 29 | return self::$_instance; | |
| 31 | 30 | } | |
| 32 | 31 | ||
| … | … | ||
| 157 | 157 | { | |
| 158 | 158 | if ($this->_xhr === null) | |
| 159 | 159 | $this->_xhr = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && | |
| 160 | $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); | ||
| 160 | $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); | ||
| 161 | 161 | return $this->_xhr; | |
| 162 | 162 | } | |
| 163 | 163 | ||
| 164 | 164 | public function isReload () | |
| 165 | 165 | { | |
| 166 | 166 | if ($this->_reload === null) | |
| 167 | $this->_reload = (isset($_SERVER['HTTP_CACHE_CONTROL']) && | ||
| 168 | strpos($_SERVER['HTTP_CACHE_CONTROL'], 'max-age=0') !== false) || | ||
| 167 | $this->_reload = (isset($_SERVER['HTTP_CACHE_CONTROL']) && strpos($_SERVER['HTTP_CACHE_CONTROL'], 'max-age=0') !== false) || | ||
| 169 | 168 | (isset($_SERVER['HTTP_PRAGMA']) && strpos($_SERVER['HTTP_PRAGMA'], 'no-cache')); | |
| 170 | 169 | return $this->_reload; | |
| 171 | 170 | } |
View.php
(28 / 47)
|   | |||
| 12 | 12 | ||
| 13 | 13 | private | |
| 14 | 14 | $_plugins, | |
| 15 | $_datas, | ||
| 15 | $_data, | ||
| 16 | 16 | $_logBelokan, | |
| 17 | 17 | $_log, | |
| 18 | 18 | $_viewDir, | |
| … | … | ||
| 22 | 22 | public function __construct () | |
| 23 | 23 | { | |
| 24 | 24 | $this->_plugins = null; | |
| 25 | $this->_datas = array(); | ||
| 25 | $this->_data = array(); | ||
| 26 | 26 | $config = Belokan_Config::getInstance(); | |
| 27 | 27 | $this->_logBelokan = $config->get('log', 'belokan'); | |
| 28 | 28 | $this->_log = Belokan_Log::getInstance(); | |
| … | … | ||
| 33 | 33 | ||
| 34 | 34 | public function __get ($var) | |
| 35 | 35 | { | |
| 36 | if (isset($this->_datas[$var])) { | ||
| 37 | return $this->_datas[$var]; | ||
| 38 | } else { | ||
| 39 | return null; | ||
| 40 | } | ||
| 36 | if (isset($this->_data[$var])) return $this->_data[$var]; | ||
| 37 | return null; | ||
| 41 | 38 | } | |
| 42 | 39 | ||
| 43 | 40 | public function __set ($var, $value) | |
| 44 | 41 | { | |
| 45 | return $this->_datas[$var] = $value; | ||
| 42 | return $this->_data[$var] = $value; | ||
| 46 | 43 | } | |
| 47 | 44 | ||
| 48 | 45 | public function __isset ($var) | |
| 49 | 46 | { | |
| 50 | return isset($this->_datas[$var]); | ||
| 47 | return isset($this->_data[$var]); | ||
| 51 | 48 | } | |
| 52 | 49 | ||
| 53 | 50 | public function __unset ($var) | |
| 54 | 51 | { | |
| 55 | unset($this->_datas[$var]); | ||
| 52 | unset($this->_data[$var]); | ||
| 56 | 53 | } | |
| 57 | 54 | ||
| 58 | 55 | public function __call ($plugin_name, $args) | |
| 59 | 56 | { | |
| 60 | if ($this->_plugins == null) { | ||
| 57 | if ($this->_plugins == null) | ||
| 61 | 58 | $this->_plugins = new Belokan_Plugins; | |
| 62 | } | ||
| 63 | 59 | if (!$this->_plugins->isLoaded($plugin_name)) { | |
| 64 | 60 | if ($this->_plugins->load($plugin_name) === false) { | |
| 65 | if (Belokan_Config::getInstance()->get('log', 'belokan')) { | ||
| 66 | Belokan_Log::getInstance()->add('Belokan', '['.date('Y-m-d H:i').'] ERROR: Failed to load plugin ('.$plugin_name.').'); | ||
| 67 | } | ||
| 61 | if (Belokan_Config::getInstance()->get('log', 'belokan')) | ||
| 62 | Belokan_Log::getInstance()->add('Belokan', 'ERROR: Failed to load plugin ('.$plugin_name.').'); | ||
| 68 | 63 | return null; | |
| 69 | 64 | } | |
| 70 | 65 | } | |
| 71 | 66 | $plugin = $this->_plugins->get($plugin_name); | |
| 72 | 67 | $defaultMethod = $plugin_name; | |
| 73 | if (method_exists($plugin, $defaultMethod)) { | ||
| 68 | if (method_exists($plugin, $defaultMethod)) | ||
| 74 | 69 | return call_user_func_array(array($plugin, $defaultMethod), $args); | |
| 75 | } else { | ||
| 76 | return $plugin; | ||
| 77 | } | ||
| 70 | return $plugin; | ||
| 78 | 71 | } | |
| 79 | 72 | ||
| 80 | 73 | public function renderLayoutBefore ($layout) | |
| 81 | 74 | { | |
| 82 | if ($layout != null) { | ||
| 83 | $layoutTop = $this->_layoutDir.$layout.'/before.p'.$this->_format; | ||
| 84 | if (file_exists($layoutTop)) { | ||
| 85 | include $layoutTop; | ||
| 86 | } else { | ||
| 87 | if ($this->_logBelokan) { | ||
| 88 | $this->_log->add('Belokan', '['.date('Y-m-d H:i').'] ERROR: Layout before file ('.$layoutTop.') not found.'); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 75 | if ($layout == null) return; | ||
| 76 | $layoutTop = $this->_layoutDir.$layout.'/before.p'.$this->_format; | ||
| 77 | if (file_exists($layoutTop)) | ||
| 78 | include $layoutTop; | ||
| 79 | else if ($this->_logBelokan) | ||
| 80 | $this->_log->add('Belokan', 'ERROR: Layout before file ('.$layoutTop.') not found.'); | ||
| 92 | 81 | } | |
| 93 | 82 | ||
| 94 | 83 | public function renderView ($view) { | |
| 95 | 84 | $viewFile = $this->_viewsDir.$view.'.p'.$this->_format; | |
| 96 | if (file_exists($viewFile)) { | ||
| 85 | if (file_exists($viewFile)) | ||
| 97 | 86 | include $viewFile; | |
| 98 | } else { | ||
| 99 | if ($this->_logBelokan) { | ||
| 100 | $this->_log->add('Belokan', '['.date('Y-m-d H:i').'] ERROR: View file ('.$viewFile.') not found.'); | ||
| 101 | } | ||
| 102 | } | ||
| 87 | else if ($this->_logBelokan) | ||
| 88 | $this->_log->add('Belokan', 'ERROR: View file ('.$viewFile.') not found.'); | ||
| 103 | 89 | } | |
| 104 | 90 | ||
| 105 | 91 | public function renderLayoutAfter ($layout) { | |
| 106 | if ($layout != null) { | ||
| 107 | $layoutBottom = $this->_layoutDir.$layout.'/after.p'.$this->_format; | ||
| 108 | if (file_exists($layoutBottom)) { | ||
| 109 | include $layoutBottom; | ||
| 110 | } else { | ||
| 111 | if ($this->_logBelokan) { | ||
| 112 | $this->_log->add('Belokan', '['.date('Y-m-d H:i').'] ERROR: Layout after file ('.$layoutBottom.') not found.'); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 92 | if ($layout == null) return; | ||
| 93 | $layoutBottom = $this->_layoutDir.$layout.'/after.p'.$this->_format; | ||
| 94 | if (file_exists($layoutBottom)) | ||
| 95 | include $layoutBottom; | ||
| 96 | else if ($this->_logBelokan) | ||
| 97 | $this->_log->add('Belokan', 'ERROR: Layout after file ('.$layoutBottom.') not found.'); | ||
| 116 | 98 | } | |
| 117 | 99 | ||
| 118 | 100 | public function loadPlugin ($plugin_name, $plugin=null, $plugin_file=null) | |
| 119 | 101 | { | |
| 120 | if ($this->_plugins == null) { | ||
| 102 | if ($this->_plugins == null) | ||
| 121 | 103 | $this->_plugins = new Belokan_Plugins; | |
| 122 | } | ||
| 123 | 104 | return $this->_plugins->load($plugin_name, $plugin, $plugin_file); | |
| 124 | 105 | } | |
| 125 | 106 |

