예제 #1
0
 public function load()
 {
     // Load config
     // Try from cache
     $cachekey = "config";
     if ($this->user) {
         $cachekey .= ":" . $this->user;
     }
     $this->config = Cache::load("config");
     if (!is_array($this->config) || $this->get('cache.active') != 1) {
         // Load config from database
         $sql = new SqlManager();
         if (!is_null($this->user)) {
             $sql->setQuery("SELECT * FROM config WHERE user_id = {{user}}");
             $sql->bindParam("{{user}}", $this->user);
         } else {
             $sql->setQuery("SELECT * FROM config WHERE user_id IS NULL");
         }
         $sql->execute();
         $this->config = array();
         while ($row = $sql->fetch()) {
             $this->config[$row['name']] = $row['value'];
         }
         if (!isset($this->config['cache.active']) || $this->config['cache.active'] != 1) {
             // If cache is deactivated, clear possible cache file
             Cache::clear($cachekey);
         } else {
             // If cache is activeated, save config for later use
             Cache::save($cachekey, $this->config);
         }
     }
 }
예제 #2
0
 public function loadByLogin($username, $password, $groups = array())
 {
     // Load user from database by given login data
     $sql = new SqlManager();
     if (count($groups) > 0) {
         $in = $sql->arrayToInString($groups, true);
         $sql->setQuery("\n\t\t\t\tSELECT user.id, user.password FROM user\n\t\t\t\tJOIN meta ON (meta.name = 'usergroup' AND meta.value IN (" . $in . "))\n\t\t\t\tJOIN assortment ON (assortment.type = 'usergroup' AND assortment.id = meta.value)\n\t\t\t\tWHERE username = '******'\n\t\t\t\t");
     } else {
         $sql->setQuery("SELECT id, password FROM user WHERE username = '******'");
     }
     $sql->bindParam("{{username}}", $username);
     $user = $sql->result();
     if (isset($user['id']) && isset($user['password']) && Crypt::checkHash($password, $user['password'])) {
         $this->load($user['id']);
     }
 }
예제 #3
0
 public static function save($code, $value, $lang = null)
 {
     // Save specific locale value by given key
     if (!$lang) {
         $lang = self::getLanguage();
     }
     // Check if locale already exists
     $sql = new SqlManager();
     $sql->setQuery("\n\t\t\tSELECT code FROM locale \n\t\t\tWHERE code = '{{code}}' \n\t\t\tAND language = {{lang}}\n\t\t\tLIMIT 1");
     $sql->bindParam('{{code}}', $code);
     $sql->bindParam('{{lang}}', $lang, "int");
     $check = $sql->result();
     $loc = array('code' => $sql->escape($code), 'language' => $sql->escape($lang, "int"), 'text' => $sql->escape($value), 'lastchanged' => DateManager::now());
     // Either update database or insert new entry for given locale
     if (!$check['code']) {
         $loc['created'] = DateManager::now();
         $sql->insert("locale", $loc);
     } else {
         $sql->update("locale", $loc);
     }
     // Refresh cache to make sure new locale entry will be used
     $cachekey = "locale:" . $lang;
     Cache::clear($cachekey);
     self::load($lang);
 }
예제 #4
0
 public function applyRules()
 {
     $sql = new SqlManager();
     $sql->setQuery("\n\t\t\tSELECT * FROM rewrite\n\t\t\tWHERE request = '{{url}}'\n\t\t\t");
     $sql->bindParam("{{url}}", $this->request_url);
     $rule = $sql->result();
     if (isset($rule['rewrite'])) {
         $this->target_url = $rule['rewrite'];
         $this->data = $rule;
     }
 }
예제 #5
0
 public static function load($name, $parent = null, $level = 0, $maxlevel = 0)
 {
     $assortment = array();
     $sql = new SqlManager();
     if (is_null($parent)) {
         $sql->setQuery("\n\t\t\t\tSELECT assortment.*, COUNT(*) AS cnt FROM assortment\n\t\t\t\tLEFT JOIN meta ON (meta.name = 'assortment' AND meta.value = assortment.id)\n\t\t\t\tWHERE assortment.name = '{{name}}'\n\t\t\t\t\tAND assortment.parent_id IS NULL\n\t\t\t\tGROUP BY assortment.id\n\t\t\t\tORDER BY assortment.sortkey ASC\n\t\t\t\t");
     } else {
         $sql->setQuery("\n\t\t\t\tSELECT assortment.*, COUNT(*) AS count FROM assortment\n\t\t\t\tLEFT JOIN meta ON (meta.name = 'assortment' AND meta.value = assortment.id)\n\t\t\t\tWHERE assortment.name = '{{name}}'\n\t\t\t\t\tAND assortment.parent_id = {{parent}}\n\t\t\t\tGROUP BY assortment.id\n\t\t\t\tORDER BY assortment.sortkey ASC\n\t\t\t\t");
         $sql->bindParam("{{parent}}", $parent, "int");
     }
     $sql->bindParam("{{name}}", $name);
     $sql->execute();
     $i = 0;
     while ($row = $sql->fetch()) {
         $assortment[$i] = $row;
         if ($level < $maxlevel) {
             $assortment[$i]['_children'] = self::load($name, $row['id'], $level + 1, $maxlevel);
             $assortment[$i]['count'] += $assortment[$i]['_children']['cnt'];
         }
         $i++;
     }
     return $assortment;
 }
예제 #6
0
 public static function save($type, $id, $data)
 {
     $data['object_type'] = $type;
     $data['object_id'] = $id;
     $sql = new SqlManager();
     if (!isset($data['id'])) {
         $sql->insert("version", $data);
     } else {
         $sql->setQuery("\n\t\t\t\tSELECT id FROM version\n\t\t\t\tWHERE id = {{id}}\n\t\t\t\tLIMIT 1\n\t\t\t\t");
         $sql->bindParam("{{id}}", $data['id']);
         $check = $sql->result();
         if (isset($check['id'])) {
             $sql->update("version", $data);
         } else {
             $sql->insert("version", $data);
         }
     }
 }
예제 #7
0
 public function check()
 {
     // Check rights for setup instance
     // Check if neccessary fields are all set
     if (!$this->table) {
         throw new Exception("Table not set!");
         return;
     }
     if (!$this->id) {
         throw new Exception("ID not set!");
         return;
     }
     if (!is_object($this->user)) {
         throw new Exception("User not set!");
         return;
     }
     // Check users access rights to any object
     $cachekey = "access:" . $this->table . ":" . $this->id . ":" . $this->user->getID();
     $this->access = Cache::load($cachekey);
     if (is_null($this->access)) {
         // No cache found, load access rights from database
         $this->access = true;
         $sql = new SqlManager();
         $sql->setQuery("\n\t\t\t\tSELECT * FROM access\n\t\t\t\tWHERE object_table = '{{table}}'\n\t\t\t\t\tAND object_id = {{id}}\n\t\t\t\t");
         $sql->bindParam("{{table}}", $this->table);
         $sql->bindParam("{{id}}", $this->id, "int");
         $sql->execute();
         $rulescnt = 0;
         while ($row = $sql->fetch()) {
             $rulescnt++;
             switch ($row['access_type']) {
                 case "password":
                     // Check somehow if user entered password already
                     $this->access = false;
                     $this->access_data = $row;
                     break;
                 case "usergroup":
                     // Check if user is part of the usergroup
                     if (!in_array($row['access_key'], $this->user->getUserGroups())) {
                         $this->access = false;
                         $this->access_data = $row;
                     }
                     break;
                 default:
                     $this->access = false;
                     break;
             }
             if ($this->access) {
                 // If one of the access settings allows access, it's enough
                 // Stop loop
                 $this->access_data = array();
                 break;
             }
         }
         if ($this->access && count($this->parents) > 0) {
             foreach ($this->parents as $parent) {
                 $check = $this->parents[count($this->parents) - 1];
                 unset($this->parents[count($this->parents) - 1]);
                 $check = new AccessOfficer($this->table, $check, $this->user, $this->parents);
                 $this->access = $check->check();
                 $this->access_data = $check->getAccessData();
             }
         }
         // Save determined access in cahce for later use
         Cache::save($cachekey, $this->access);
     }
     return $this->access;
 }
예제 #8
0
 public static function remove($table, $id, $name)
 {
     // Removes meta dataset(s!!) in database
     // Notice: All datasets with the given name will be removed!
     $sql = new SqlManager();
     $sql->setQuery("\n\t\t\tDELETE FROM meta\n\t\t\tWHERE object_table = '{{table}}'\n\t\t\t\tAND object_id = {{id}}\n\t\t\t\tAND name = '{{name}}'\n\t\t\t");
     $sql->bindParam("{{table}}", $table);
     $sql->bindParam("{{id}}", $id, "int");
     $sql->bindParam("{{name}}", $name);
     $sql->execute();
 }
예제 #9
0
 public function loadElements($parent = null)
 {
     // Get elements to loaded content from the database for given parent id
     // Is used recursiv to load the hierarchical structure of the content elements
     if (!isset($this->id)) {
         throw new Exception("Cannot get content elements! No content set!");
         return;
     }
     // Try from cache first
     $elements = array();
     // No cache found, load from database
     $sql = new SqlManager();
     if ($parent > 0) {
         $sql->setQuery("SELECT * FROM element WHERE object_table = 'content' AND object_id = {{id}} AND parent_id = {{parent}} ORDER BY sortkey");
         $sql->bindParam("{{parent}}", $parent, "int");
     } else {
         $sql->setQuery("SELECT * FROM element WHERE object_table = 'content' AND object_id = {{id}} AND parent_id IS NULL ORDER BY sortkey");
     }
     $sql->bindParam("{{id}}", $this->id, "int");
     $sql->execute();
     while ($element = $sql->fetch()) {
         if (!isset($elements[$element['position']])) {
             $elements[$element['position']] = array();
         }
         $element['parameters'] = unserialize($element['parameters']);
         $index = count($elements[$element['position']]);
         $elements[$element['position']][$index] = $element;
         // Created parent-children array tree
         $elements[$element['position']][$index]['_children'] = $this->loadElements($element['id']);
     }
     return $elements;
 }
예제 #10
0
 public function userRegister($request)
 {
     // Register new user from request
     if (empty($request['username'])) {
         return array("message" => array("type" => "error", "code" => "MandatoryInputMissing"), "missing_fields" => "username");
     }
     $sql = new SqlManager();
     $sql->setQuery("SELECT * FROM user WHERE username = '******'");
     $sql->bindParam("{{username}}", $request['username']);
     $check = $sql->result();
     if (isset($check['id'])) {
         return array("message" => array("type" => "error", "code" => "UsernameUsed"));
     }
     $newuser = new User();
     $id = $newuser->create(array("username" => $request['username']));
     if (!$id) {
         return array("message" => array("type" => "error", "code" => "UnknownError"));
     }
     $newuser->load($id);
     $groups = array();
     if (isset($request['usergroups'])) {
         $groups = split(",", $request['usergroups']);
     }
     foreach ($groups as $group) {
         Meta::save("user", $newuser->getID(), "usergroup", $group);
     }
     $newuser->createAuthCode();
     return array("redirect" => array("url" => "/bestaetige-account", "post" => array("username" => $request['username'])));
 }
예제 #11
0
 public function initPlugins()
 {
     // Init installed and actived plugins
     $sql = new SqlManager();
     $sql->setQuery("SELECT * FROM plugin WHERE active = 1");
     $sql->execute();
     while ($plugin = $sql->fetch()) {
         // Check plugin core file
         $path = __DIR__ . "/" . $this->plugin_root . "/" . strtolower($plugin['name']) . "/" . $plugin['name'] . ".php";
         if (is_file($path)) {
             // ... and include it
             include_once $path;
             // Then check for class and init method and call if possible
             if (class_exists($plugin['name'])) {
                 $instance = new $plugin['name']();
                 if (method_exists($instance, "init")) {
                     $instance->init();
                 } else {
                     throw new Exception("Init method in plugin '{$plugin['name']}' core file not found!");
                 }
             } else {
                 throw new Exception("Class for plugin '{$plugin['name']} not found!");
             }
         } else {
             throw new Exception("Core file for plugin '{$plugin['name']}' not found!");
         }
     }
 }