/** * checks if a 'user_id' owns 'id' of a 'table' meaning that he should * be able e.g. edit and delete this row. * @param string $table able name * @param string $id primary id of table * @param type $user_id the users account id * @return array|false $row if row was found else false */ public static function ownID($table, $id, $user_id) { $row = q::setSelect($table)->filter('id =', $id)->condition('AND')->filter('user_id = ', $user_id)->fetchSingle(); if (empty($row)) { return false; } return $row; }
/** * get the table * @param string $table * @param int $from * @param int $limit * @return string $html */ public function getTable($table, $from, $limit = 100) { $total = q::numRows('account')->fetch(); $p = new pagination($total); $rows = q::select($table)->limit($p->from, $limit)->fetch(); $str = "<table border =1><tr>"; $str .= $this->getTableHeaders($table); $str .= $this->getTableRows($rows); $str .= "</tr></table>"; $str .= $p->getPagerHTML(); return $str; }
/** * Get all jobs in a queue, which needs to be executed. Only name * if required * @param string $name * @param mixed $uniqueid if null then only the above param 'name' will be used * @param mixed $done null | 1 | 0 * @return array $rows */ public function getQueueRows($name, $uniqueid = null, $done = 0) { q::select($this->queue)->filter('name =', $name); if (!is_null($uniqueid)) { q::condition('AND'); q::filter('uniqueid =', $uniqueid); } if (is_int($done)) { q::condition('AND'); q::filter('done =', $done); } $rows = q::fetch(); return $rows; }
protected function uploadImage($url) { // Array ( [name] => Angus_cattle_18.jpg [type] => image/jpeg [tmp_name] => /tmp/php5lPQZT [error] => 0 [size] => 52162 ) $ary = []; $name = file::getFilename($url) . "." . file::getExtension($url); $ary['name'] = $name; $ary['abstract'] = file::getFilename($url); $ary['type'] = file::getMime($url); $ary['tmp_name'] = $url; $ary['error'] = 0; $ary['size'] = 0; $i = new \modules\image\uploadBlob(); $res = $i->insertFileDirect($ary, $this->reference, $this->parentId, $this->userId); if ($res) { $id = q::lastInsertId(); $row = $i->getSingleFileInfo($id); return $i->getFullWebPath($row); } else { log::error("Could not upload image: {$name}"); return false; } }
/** * method for loading menus. All Main menu entries is generated from * database, while all module or submodule menus are generated from * files (menu.inc). * */ public function loadMenus() { $num = uri::getInstance()->numFragments(); // always a module menu in web mode self::$menu['main'] = q::select('menus')->filter('parent =', 0)->condition('AND')->filter('admin_only =', 0)->order('weight')->fetch(); // admin item are special if (session::isAdmin()) { self::$menu['admin'] = q::select('menus')->filter('admin_only =', 1)->condition('OR')->filter('section !=', '')->order('weight')->fetch(); } self::setMainMenuTitles(); // if status is set we don't load module menus. Must be 404 or 403. // we then return empty array. module loader will know what to do when // including correct error pages. No menus from normal main module // should then be loaded. if (!empty(moduleloader::$status)) { return array(); } // get base menu from file. Base menu is always loaded if found. // we decide this from num fragments in uri. $module = uri::getInstance()->fragment(0); // if no module it must be 'frontpage_module' set // in configuration if (!$module) { $module = conf::getMainIni('frontpage_module'); $menu = self::getBaseModuleMenu($module); self::$menu['module'] = array_merge(self::$menu['module'], $menu); return; } // main module, e.g content if ($num >= 2) { $menu = self::getBaseModuleMenu($module); self::$menu['module'] = array_merge(self::$menu['module'], $menu); } // sub module e.g. content/article if ($num > 2) { $sub = uri::getInstance()->fragment(0) . '/' . uri::getInstance()->fragment(1); self::$menu['sub'] = self::getSubMenu($sub); } }
/** * Run the system */ public function run() { // Register an autoloader for loading modules from mopdules dir $m = new modules(); $m->autoloadRegister(); // define HTML constants common::defineConstants(); // define global constants - based on base path conf::defineCommon(); // set include paths conf::setIncludePath(); // load config file conf::load(); if (conf::getMainIni('debug')) { log::enableDebug(); } // set public file folder in file class file::$basePath = conf::getFullFilesPath(); // utf-8 ini_set('default_charset', 'UTF-8'); // load config/config.ini // check if there exists a shared ini file // shared ini is used if we want to enable settings between hosts // which share same code base. // e.g. when updating all sites, it is a good idea to set the following flag // site_update = 1 // this flag will send correct 503 headers, when we are updating our site. // if site is being updaing we send temporarily headers // and display an error message if (conf::getMainIni('site_update')) { http::temporarilyUnavailable(); } // set a unified server_name if not set in config file. $server_name = conf::getMainIni('server_name'); if (!$server_name) { conf::setMainIni('server_name', $_SERVER['SERVER_NAME']); } // redirect to uniform server name is set in config.ini // e.g. www.testsite.com => testsite.com $server_redirect = conf::getMainIni('server_redirect'); if (isset($server_redirect)) { http::redirectHeaders($server_redirect); } // redirect to https is set in config.ini // force anything into ssl mode $server_force_ssl = conf::getMainIni('server_force_ssl'); if (isset($server_force_ssl)) { http::sslHeaders(); } // catch all output ob_start(); // Create a db connection $db_conn = array('url' => conf::getMainIni('url'), 'username' => conf::getMainIni('username'), 'password' => conf::getMainIni('password'), 'db_init' => conf::getMainIni('db_init')); // Other options // db_dont_persist = 0 // dont_die = 0 // Set to one and the connection don't die because of // e.g. no database etc. This will return NO_DB_CONN as string //$url = conf::getMainIni('url'); connect::connect($db_conn); // init module loader. $ml = new moduleloader(); // initiate uri uri::getInstance(); // runlevel 1: merge db config $ml->runLevel(1); // select all db settings and merge them with ini file settings $db_Settings = []; if (moduleloader::moduleExists('settings')) { $db_settings = q::select('settings')->filter('id =', 1)->fetchSingle(); } // merge db settings with config/config.ini settings // db settings override ini file settings conf::$vars['coscms_main'] = array_merge(conf::$vars['coscms_main'], $db_settings); // run level 2: set locales $ml->runLevel(2); // set locales intl::setLocale(); // set default timezone intl::setTimezone(); // runlevel 3 - init session $ml->runLevel(3); // start session session::initSession(); // Se if user is logged in with SESSION if (!session::isUser()) { // If not logged in check system cookie // This will start the session, if an appropiate cookie exists session::checkSystemCookie(); } // Check account $res = session::checkAccount(); if (!$res) { // Redirect to main page if user is not allowed // With current SESSION or COOKIE http::locationHeader('/'); } // set account timezone if enabled - can only be done after session // as user needs to be logged in intl::setAccountTimezone(); // run level 4 - load language $ml->runLevel(4); // load all language files $l = new lang(); $base = conf::pathBase(); $htdocs = conf::pathHtdocs(); $l->setDirsInsideDir("{$base}/modules/"); $l->setDirsInsideDir("{$htdocs}/templates/"); $l->setSingleDir("{$base}/vendor/diversen/simple-php-classes"); $l->setSingleDir("{$base}/vendor/diversen/simple-pager"); $l->loadLanguage(conf::getMainIni('lang')); // runlevel 5 $ml->runLevel(5); // load routes if any dispatch::setDbRoutes(); // check db routes or load defaults $db_route = dispatch::getMatchRoutes(); if (!$db_route) { $ml->setModuleInfo(); $ml->initModule(); } else { dispatch::includeModule($db_route['method']); } // After module has been loaded. // You can e.g. override module ini settings $ml->runLevel(6); // Init layout. Sets template name // load correct CSS. St menus if any. Etc. $layout = new layout(); // we first load menus here so we can se what happened when we // init our module. In case of a 404 not found error we don't want // to load module menus $layout->loadMenus(); // init blocks $layout->initBlocks(); // if any matching route was found we check for a method or function if ($db_route) { $str = dispatch::call($db_route['method']); } else { // or we use default module parsing $str = $ml->getParsedModule(); } // set view vars $vars['content'] = $str; // run level 7 $ml->runLevel(7); // echo module content echo $str = \mainTemplate::view($vars); conf::$vars['final_output'] = ob_get_contents(); ob_end_clean(); // Last divine intervention // e.g. Dom or Tidy $ml->runLevel(8); echo conf::$vars['final_output']; }
/** * sets db routes */ public static function setDbRoutes() { $routes = q::setSelect('system_route')->fetch(); if (empty($routes)) { conf::$vars['coscms_main']['routes'] = array(); } foreach ($routes as $route) { conf::$vars['coscms_main']['routes'][$route['route']] = unserialize($route['value']); } }
/** * * Method checks an account based on session user_id. It checks: * a) if an account is locked * b) if the current user_id does not correspond to an account. * * In both cases all sessions are killed. * Method is run at boot. In diversen\boot * * @return void */ public static function checkAccount() { $user_id = session::getUserId(); if ($user_id) { $a = q::select('account')->filter('id =', $user_id)->fetchSingle(); // user may have been deleted if (empty($a)) { self::killSessionAll($user_id); return false; } if ($a['locked'] == 1) { self::killSessionAll($user_id); return false; } } return true; }
public function displayAll($rows) { $str = table::tableBegin(array('class' => 'uk-table uk-table-hover uk-table-striped uk-table-condensed')); foreach ($rows as $row) { $str .= table::trBegin(); $str .= table::td($row['username'], array('class' => 'uk-width-3-10')); $str .= table::td($row['email']); // Comment $dancer = q::select('dancer')->filter('user_id= ', $row['id'])->fetchSingle(); if (!empty($dancer)) { $str .= table::td(html::specialEncode($dancer['comment'])); } else { $str .= table::td(''); } $str .= table::trEnd(); } $str .= table::tableEnd(); echo $str; }
/** * clears system_cache table * @return int */ public static function db() { $res = q::delete('system_cache')->filter('1 =', 1)->exec(); return $res; }
/** * Get all dancers for dropdown */ public function getDancersForDropdown() { $q = <<<EOF SELECT * FROM `account` WHERE id NOT IN (SELECT user_a FROM pair UNION SELECT user_b FROM pair) AND `admin` = 0 ORDER by username; EOF; return q::query($q)->fetch(); }
/** * delete a string from cache * @param string $module * @param int $id * @return boolean $res db result */ private static function deleteDb($module, $id) { $id = self::generateId($module, $id); $row = q::select(self::$table)->filter('id =', $id)->fetchSingle(); if (!empty($row)) { return q::delete(self::$table)->filter('id =', $id)->exec(); } }