public function __construct($config_file = NULL) { if ($config_file == NULL) { $config_file = APP_DIR . '/.env'; } $this->getenv_func = 'getenv'; if (function_exists('apache_getenv')) { $this->getenv_func = 'apache_getenv'; } if (!file_exists($config_file)) { Logger::warning('Unable to find the configuration file (only the global environment variable will be available)!'); return; } $config = file_get_contents($config_file); $lines = explode(PHP_EOL, $config); foreach ($lines as $env) { $env = trim($env); if (!empty($env)) { if (function_exists('apache_setenv')) { $raw = explode('=', $env); if (count($raw) == 2) { apache_setenv($raw[0], $raw[1]); } else { Logger::warning('Invalid environment definition: ' . $env); } continue; } putenv($env); } } unset($lines); unset($config); }
private static function classloader($class_name, $type) { $dirs = \Biome\Biome::getDirs($type); if (empty($dirs)) { return FALSE; } $filename = ''; foreach ($dirs as $d) { if (!file_exists($d)) { continue; } $files = scandir($d); foreach ($files as $f) { if ($f[0] == '.') { continue; } if (strtolower($f) == strtolower($class_name) . '.php') { $filename = $d . '/' . $f; } } } if (file_exists($filename)) { Logger::debug('Load class in ' . $filename); include_once $filename; return TRUE; } return FALSE; }
public static function load($object_name) { if (empty($object_name)) { throw new \Exception('Cannot load an empty object name!'); } if (class_exists($object_name)) { return TRUE; } $dirs = \Biome\Biome::getDirs('models'); if (empty($dirs)) { return FALSE; } $filename = ''; foreach ($dirs as $d) { if (!file_exists($d)) { continue; } $files = scandir($d); foreach ($files as $f) { if ($f[0] == '.') { continue; } if (strtolower($f) == strtolower($object_name) . '.php') { $filename = $d . '/' . $f; } } } if (file_exists($filename)) { Logger::debug('Load object ' . $object_name . ' in ' . $filename); include_once $filename; return TRUE; } return FALSE; }
public function redirect($controller = NULL, $action = NULL, $item = NULL, $module = NULL) { if ($controller === NULL) { $url = \Biome\Biome::getService('request')->headers->get('referer'); } else { $url = \URL::fromRoute($controller, $action, $item, $module); } Logger::info('Redirect to ' . $url); $this->headers->set('Location', $url); $this->setStatusCode(302); return $this; }
public function process($type, $controller_name, $action_name, $method_name, $method_params) { Logger::info('Processing method ' . $method_name); $this->_call_params['http_method_type'] = $type; $this->_call_params['controller_name'] = $controller_name; $this->_call_params['action_name'] = $action_name; $this->_call_params['method_name'] = $method_name; $this->_call_params['method_params'] = $method_params; /** * preRoute */ if (!$this->beforeRoute()) { return $this->response(); } $rendering = $type == 'GET' ? TRUE : FALSE; $this->view = \Biome\Biome::getService('view'); try { $this->view->load($controller_name, $action_name); } catch (\Biome\Core\View\Exception\TemplateNotFoundException $e) { $rendering = FALSE; } $result = call_user_func_array(array($this, $method_name), $method_params); if ($result instanceof Response) { $this->response = $result; } // Ajax Request if ($this->request->isXmlHttpRequest()) { $rendering = FALSE; $partial_rendering = $this->request->get('partial'); if ($partial_rendering) { $this->response->setContentType('application/json'); $this->view->ajaxHandle($partial_rendering); } } if ($rendering && !$this->response->isRedirection()) { // Render view $content = $this->view->render(); if (!empty($content)) { $this->response->setContent($content); } } $this->response = $this->afterRoute($this->response); return $this->response; }
protected static function declareServices() { /* Registering default services. */ /** * Autoload */ Autoload::register(); /** * Biome default logger service. */ if (!Biome::hasService('logger')) { Biome::registerService('logger', function () { return new \Psr\Log\NullLogger(); }); } /** * Biome default request. */ if (!Biome::hasService('request')) { Biome::registerService('request', function () { return Request::createFromGlobals(); }); } /** * Biome default lang service. */ if (!Biome::hasService('lang')) { Biome::registerService('lang', function () { $languages = Biome::getService('request')->getLanguages(); $lang = new \Biome\Core\Lang\XMLLang($languages); return $lang; }); } /** * Biome default view service. */ if (!Biome::hasService('view')) { Biome::registerService('view', function () { $view = new \Biome\Core\View(); $app_name = Biome::getService('lang')->get('app_name'); $view->setTitle($app_name); return $view; }); } /** * Biome default rights service. */ if (!Biome::hasService('rights')) { Biome::registerService('rights', function () { $auth = \Biome\Core\Collection::get('auth'); if ($auth->isAuthenticated()) { $admin_id = 1; // Default value of the Admin role id. if (Biome::hasService('config')) { $admin_id = Biome::getService('config')->get('ADMIN_ROLE_ID', 1); } $roles = $auth->user->roles; foreach ($roles as $role) { /* If Admin. */ if ($role->role_id == $admin_id) { return new \Biome\Core\Rights\FreeRights(); } $rights = AccessRights::loadFromJSON($role->role_rights); } return $rights; } $rights = AccessRights::loadFromArray(array()); $rights->setAttribute('User', 'firstname', TRUE, TRUE)->setAttribute('User', 'lastname', TRUE, TRUE)->setAttribute('User', 'mail', TRUE, TRUE)->setAttribute('User', 'password', TRUE, TRUE)->setRoute('GET', 'index', 'index')->setRoute('GET', 'auth', 'login')->setRoute('POST', 'auth', 'signin')->setRoute('POST', 'auth', 'signup'); return $rights; }); } /** * Biome default route service. */ if (!Biome::hasService('router')) { Biome::registerService('router', function () { $router = new Route(); $router->autoroute(); return $router; }); } /** * Biome default dispatch service. */ if (!Biome::hasService('dispatcher')) { Biome::registerService('dispatcher', function () { return Biome::getService('router')->getDispatcher(); }); } Logger::info('Services registered!'); }
/** * Fetch the object in the database with the specific fields. * If NULL is set, the object will be retrieve with all the * fields without thoses corresponding to the primary keys. */ public function fetch(...$fields) { if ($this->getId() != NULL) { Logger::warning('Trying to fetch an object already fetched!'); return $this; } $result = self::all(); if (!empty($fields)) { foreach ($fields as $f) { $result->filter($f, '=', $this->getRawValue($f)); } } else { $pks = $this->parameters()['primary_key']; if (is_string($pks)) { $pks = array($pks => $pks); } foreach ($this->_structure as $f) { if ($f instanceof PrimaryField) { continue; } if (isset($pks[$f])) { continue; } $result->filter($f, '=', $this->getRawValue($f)); } } $count = count($result); if ($count > 1) { throw new \Exception('Too many results!'); } if ($count == 0) { Logger::notice('Object not fetched in the database!'); return NULL; } $obj = $result->current(); if (!$obj instanceof Models) { throw new \Exception('Internal Error: The QuerySet doesn\'t return an object of instance Models! ' . print_r($obj, true)); } foreach ($this->_structure as $f_name => $field) { $f = $obj->getField($f_name); // Reset the field unset($this->_values['old'][$f_name]); // Set the value from the database if ($f->isReady()) { $this->_values['old'][$f_name] = $obj->getRawValue($f_name); } } return $this; }
/** * Errors handling (validation). */ public function setError($type, $message) { Logger::debug('User error ' . $type . ': ' . $message); $this->_error_list[$type] = $message; return TRUE; }
protected function checkDbError() { if (!empty($this->_instance->error)) { Logger::error('SQL Error (' . $this->_instance->errno . '): ' . $this->_instance->error); switch ($this->_instance->errno) { case 1062: throw new DuplicateException($this->_instance->error); default: throw new \Exception('SQL Error: ' . $this->_instance->error . ' Last Query:(' . $this->_last_query . ')'); } } if ($this->_instance->warning_count != 0) { $message = ''; if ($result = $this->_instance->query("SHOW WARNINGS")) { while ($row = $result->fetch_row()) { $message .= $row[0] . ' (' . $row[1] . '): ' . $row[2] . PHP_EOL; } $result->close(); } Logger::error('SQL Warning: ' . $message); throw new \Exception('SQL Warning: ' . $message . ' Last Query:(' . $this->_last_query . ')'); } return TRUE; }