Exemplo n.º 1
0
 /**
  * Get connection to database
  *
  * @param string $name Connection name
  * @param array $config Connection config (not required)
  *
  * @return \Kalibri\Db\Driver\Base
  */
 public function getConnection($name = null, $config = null)
 {
     // Use default connection name if name is not passed
     $name = $name ?: $this->_defaultConnectName;
     // Check is driver already connected
     if ($this->isConnected($name)) {
         return $this->_connections[$name];
     }
     // Is config not passed and we have stored config
     if (!$config && isset($this->_config['connection'][$name])) {
         $config = $this->_config['connection'][$name];
     } else {
         // Config not passed and not available in config file
         \Kalibri::error()->show("Invalid DB connection name '{$name}'.");
     }
     // Calculate driver name
     $driverName = '\\Kalibri\\Db\\Driver\\' . \ucfirst($config['driver']);
     try {
         $this->_connections[$name] = new $driverName($config);
         \Kalibri::logger()->add(Logger::L_INFO, "Connected to database: driverName={$driverName}, connection={$name}");
     } catch (Exception $e) {
         \Kalibri::error()->showException($e);
     }
     // Return connected driver
     return $this->_connections[$name];
 }
Exemplo n.º 2
0
 public function write()
 {
     // Skip empty log saving
     if (!count($this->_messages)) {
         return false;
     }
     if ((!\file_exists($this->_options['path']) || !\is_dir($this->_options['path']) || !is_writable($this->_options['path'])) && !@mkdir($this->_options['path'], 0777, true)) {
         \Kalibri::error()->show("Can't create log file in '" . $this->_options['path'] . "'");
     }
     if (($fResource = @\fopen($this->_options['path'] . 'k' . \date('Y-m-d') . '.log', 'a')) !== null) {
         \fwrite($fResource, $this->getAsString());
         \fclose($fResource);
     }
     return true;
 }
Exemplo n.º 3
0
 public function register()
 {
     if (\Kalibri::auth()->getProfile()) {
         \Url::redirect(\Kalibri::config()->get('page.after-login'));
     }
     $this->page()->setTitle(tr('Sign up'));
     if (isset($_POST['login'], $_POST['password'], $_POST['re-password'])) {
         $errors = array();
         if (empty($_POST['password'])) {
             $errors[] = tr('Password should not be empty.');
         }
         if ($_POST['password'] !== $_POST['re-password']) {
             $errors[] = tr('Password and Re-password should match.');
         }
         if (strlen($_POST['password']) < \Kalibri::config()->get('auth.min-password-length')) {
             $errors[] = tr('Password should be minimum :min-length letters.', array('min-length' => \Kalibri::config()->get('auth.min-password-length')));
         }
         if (empty($_POST['login'])) {
             $errors[] = tr('Login should not be empty.');
         }
         if (!\Kalibri::auth()->isValidLogin($_POST['login'])) {
             $errors[] = tr('Login should contain latin letters or digits and be from 4 to 15 chars long.');
         }
         if (\Kalibri::auth()->getProfileByLogin($_POST['login'])) {
             $errors[] = tr('Profile with this name already registered.');
         }
         $this->page()->errorMsg = $errors;
         if (!count($errors)) {
             $model = \Kalibri::auth()->getModel();
             //$model->register(
             $model->getEmpty()->setLogin($_POST['login'])->setPassword(\Kalibri::auth()->encryptPassword($_POST['password']))->save();
             //);
             if (\Kalibri::auth()->tryLogin($_POST['login'], $_POST['password'])) {
                 \Url::redirect(\Kalibri::config()->get('page.after-login'));
             } else {
                 \Kalibri::error()->show(tr('Ooops. Something go wrong.'));
             }
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Called on each method request. Will try to find appropriete action for field.
  *
  * @param string $name Method name
  * @param string $arguments Arguments passed to method
  *
  * @return mixed
  */
 public function __call($name, $arguments)
 {
     if (\method_exists($this, $name) && \is_callable(array($this, $name))) {
         return \call_user_func(array(&$this, $name), $arguments);
     }
     $type = '';
     $fieldName = '';
     if (\strpos($name, 'get') === 0 || \strpos($name, 'set') === 0) {
         $type = \substr($name, 0, 3);
         $fieldName = \substr($name, 3);
         $fieldName = \strtolower($fieldName[0]) . substr($fieldName, 1);
     } elseif (\strpos($name, 'is') === 0) {
         $type = 'is';
         $fieldName = \substr($name, 2);
         $fieldName = \strtolower($fieldName[0]) . substr($fieldName, 1);
     }
     if (property_exists($this, $fieldName)) {
         switch ($type) {
             case 'is':
                 return $this->__is($fieldName, current($arguments));
             case 'get':
                 return $this->__get($fieldName);
             case 'set':
                 return $this->__set($fieldName, current($arguments));
         }
     } else {
         \Kalibri::error()->show('Method not available: ' . get_class($this) . '::' . $name);
     }
     return null;
 }
Exemplo n.º 5
0
 /**
  *  Called on each method request. Will try to find appropriete action for field.
  *
  *  @param string $name Method name
  *  @param string $arguments Arguments passed to method
  *
  *  @return mixed
  */
 public function __call($name, $arguments)
 {
     if (\method_exists($this, $name) && \is_callable([$this, $name])) {
         return \call_user_func([&$this, $name], $arguments);
     }
     if (!$this->_withMagic) {
         \Kalibri::error()->show('Method not available: ' . get_class($this) . '::' . $name);
     }
     $type = null;
     $fieldName = null;
     if (strpos($name, 'get') === 0 || strpos($name, 'set') === 0) {
         $type = substr($name, 0, 3);
         $fieldName = substr($name, 3);
         $fieldName = strtolower($fieldName[0]) . substr($fieldName, 1);
     }
     if (property_exists($this, $fieldName)) {
         switch ($type) {
             case 'get':
                 return $this->{$fieldName};
             case 'set':
                 $this->{$fieldName} = current($arguments);
                 $this->registerChanged(Text::camelToUnderscore($fieldName));
                 return $this;
         }
     } else {
         \Kalibri::error()->show('Method not available: ' . get_class($this) . '::' . $name);
     }
     return null;
 }
Exemplo n.º 6
0
 /**
  * Execute controller and action
  */
 public function run()
 {
     try {
         $controllerName = "\\{$this->_baseNamespace}\\App\\Controller\\" . str_replace('/', '\\', $this->_dir . $this->_controller);
         if (\class_exists($controllerName)) {
             $actionName = $this->_action . $this->_options['action-prefix'];
             /** @var $controller \Kalibri\Controller\Page*/
             $controller = new $controllerName();
             // Set global controller instance
             \Kalibri::controller($controller);
             //If exists method '_remap' call it and pass method name and params
             if (\method_exists($controller, '_remap')) {
                 \call_user_func_array([$controller, '_remap'], [$actionName, $this->_params]);
             } elseif (\is_callable("{$controllerName}::{$actionName}")) {
                 \call_user_func_array([$controller, $actionName], $this->_params);
             } else {
                 throw new NotFound();
             }
             // Check is controller already rendered by hand, if not render it
             if (!$controller->isRendered()) {
                 \call_user_func_array([$controller, '_render'], []);
             }
             return $this;
         }
     } catch (NotFound $e) {
         \Kalibri::error()->showPageNotFound();
     } catch (\Exception $e) {
         \Kalibri::error()->showException($e);
     }
     \Kalibri::error()->showPageNotFound();
     return $this;
 }
Exemplo n.º 7
0
 /**
  * Render view
  *
  * @param bool $asString
  *
  * @return mixed(string,bool)
  */
 public function render($asString = false, $path = null)
 {
     $output = null;
     if (!empty($this->_name)) {
         if ($this->isExists($this->_name, $path)) {
             \ob_start();
             \extract(\Kalibri::data()->getData());
             include $location = $this->getLocation(null, $path);
             $output = ob_get_contents();
             K_COMPILE_ROUTES && \Kalibri::compiler()->skip($location);
             @\ob_end_clean();
         } else {
             \Kalibri::error()->show("View with name '{$this->_name}' not found");
         }
     }
     if (!$asString) {
         echo $output;
     }
     \Kalibri::data()->set(self::VAR_CONTENT, $output);
     return $output;
 }
Exemplo n.º 8
0
 /**
  * @access protected
  */
 protected function process()
 {
     $indexPage = \Kalibri::config()->get('entry');
     $indexPage = strlen($indexPage) && $indexPage[0] != '/' ? '/' . $indexPage : $indexPage;
     $uri = str_replace($indexPage, '', $this->uri);
     //Set default uri and clear segments
     $this->uri = '/';
     $this->segments = array();
     if (strlen($uri) === 0) {
         $uri = '/';
     } elseif (isset($uri[0]) && $uri[0] == '?') {
         $uri = substr($uri, 1);
     }
     $uri = $uri == '' ? '/' : $uri;
     //Check is URI valid
     if (!$this->isValidUri($uri)) {
         \Kalibri::error()->show('Invalid URI chars');
     }
     //Remove first slash
     $this->uri = $uri;
     $uri = $uri[0] == '/' && strlen($uri) > 1 ? substr($uri, 1, strlen($uri)) : $uri;
     //if( strpos($uri, '/') !== false )
     //{
     $this->segments = explode('/', $uri);
     //}
 }