/** * Internal function to lookup the saved data for a given component based on its name. * * Will return null if it doesn't exist or an array. * * @param string $componentname The name of the component to lookup * * @return array | null */ public static function _LookupComponentData($componentname) { if (self::$_DBCache === null) { self::$_DBCache = array(); // Try to load the components try { $res = Core\Datamodel\Dataset::Init()->table('component')->select('*')->execute(); } // But since this function is called during the installer, it might fail... that's acceptable. catch (DMI_Exception $e) { return false; } foreach ($res as $r) { $n = strtolower($r['name']); self::$_DBCache[$n] = $r; } } $componentname = strtolower($componentname); return (isset(self::$_DBCache[$componentname])) ? self::$_DBCache[$componentname] : null; }
public function pagemetas_autocompletekeyword(){ $request = $this->getPageRequest(); $view = $this->getView(); $view->mode = View::MODE_AJAX; $view->contenttype = View::CTYPE_JSON; $term = $request->getParameter('term'); $view->record = false; // This is an ajax-only request. if(!$request->isAjax()){ return View::ERROR_BADREQUEST; } $ds = new Core\Datamodel\Dataset(); $ds->table('page_meta'); $ds->uniquerecords = true; $ds->select('meta_value', 'meta_value_title'); $ds->where('meta_key = keyword'); $ds->where('meta_value_title LIKE ' . $term . '%'); // Just in case there are a huge number of records... $stream = new DatasetStream($ds); $view->jsondata = array(); while(($record = $stream->getRecord())){ $view->jsondata[] = array( 'id' => $record['meta_value'], 'label' => $record['meta_value_title'], 'value' => $record['meta_value'], ); } // Does the user have access to search for users? // if so include that search here to! This is for the subject matter tag, or "This x is about person y!" if(\Core\user()->checkAccess('p:/user/search/autocomplete')){ $results = UserModel::Search($term); foreach($results as $r){ /** @var $r \Core\Search\ModelResult */ /** @var UserModel $user */ $user = $r->_model; $view->jsondata[] = array( 'id' => 'u:' . $user->get('id'), 'label' => $user->getDisplayName(), 'value' => 'u:' . $user->get('id'), ); } } }
/** * Method to purge the user activity cron. * * This is useful because on an extremely busy site, this table can grow to several gigs within not much time. */ public static function PurgeUserActivityCron() { $opt = \ConfigHandler::Get('/user/activity/keephistory'); if($opt == 'all' || !$opt){ echo 'Not purging any user activity.' . "\n"; return true; } // Convert the key to a datestring value. $date = new \CoreDateTime(); switch($opt){ case '1-week': $date->modify('-1 week'); break; case '1-month': $date->modify('-1 month'); break; case '2-months': $date->modify('-2 month'); break; case '3-months': $date->modify('-3 month'); break; case '6-months': $date->modify('-6 month'); break; case '12-months': $date->modify('-12 month'); break; case '24-months': $date->modify('-24 month'); break; case '36-months': $date->modify('-36 month'); break; default: echo 'Invalid value for /user/activity/keephistory: [' . $opt . ']'; return false; } // And delete any activity older than this date. echo 'Purging user activity older than ' . $date->getFormatted('r') . "...\n"; $ds = new \Core\Datamodel\Dataset(); $ds->delete()->table('user_activity')->where('datetime < ' . $date->getFormatted('U', \TIME::TIMEZONE_GMT))->execute(); echo 'Removed ' . $ds->num_rows . ' record(s).' . "\n"; return true; }
/** * Internal function to do the multisite check on the model. * If the model supports a site attribute and none requested, then set it to the current site. */ private function _performMultisiteCheck(){ $m = $this->_model; $ref = new ReflectionClass($m); $schema = $ref->getMethod('GetSchema')->invoke(null); $index = $ref->getMethod('GetIndexes')->invoke(null); //$schema = $m::GetSchema(); //$index = $m:: // Is there a site property? If not I don't even care. if( isset($schema['site']) && $schema['site']['type'] == Model::ATT_TYPE_SITE && Core::IsComponentAvailable('multisite') && MultiSiteHelper::IsEnabled() ){ // I want to look it up because if the script actually set the site, then // it evidently wants it for a reason. $siteexact = (sizeof($this->_dataset->getWhereClause()->findByField('site')) > 0); $idexact = false; // The primary check will allow a model to be instantiated with the exact primary key string. $pri = isset($index['primary']) ? $index['primary'] : null; if($pri && !is_array($pri)) $pri = [$pri]; if($pri){ $allids = true; foreach($pri as $k){ if(sizeof($this->_dataset->getWhereClause()->findByField($k)) == 0){ $allids = false; break; } } if($allids) $idexact = true; } if(!($siteexact || $idexact)){ $w = new \Core\Datamodel\DatasetWhereClause(); $w->setSeparator('or'); $w->addWhere('site = ' . MultiSiteHelper::GetCurrentSiteID()); $w->addWhere('site = -1'); $this->_dataset->where($w); //$this->_dataset->where('site = ' . MultiSiteHelper::GetCurrentSiteID()); } } }
/** * DELETE FROM tablename WHERE searchclause * * @access public * @return mixed array parsed delete on success, otherwise Error */ public function parseDelete() { $tree = new Core\Datamodel\Dataset(); $tree->_mode = Core\Datamodel\Dataset::MODE_DELETE; $this->getTok(); if ($this->token == 'from') { // FROM is not required // This is used from the DELETE FROM tablename $this->getTok(); } if($this->token == 'table'){ // Table is allowed in the // TRUNCATE TABLE tablename // syntax. $this->getTok(); } if ($this->token != 'ident') { $this->raiseError('Expected a table name'); } $tree->table($this->lexer->tokText); $this->getTok(); if ($this->token == 'where') { // WHERE is not required $this->getTok(); $clause = $this->parseWhereCondition(); if (false === $clause) { return $clause; } $tree->_where = $clause; } return $tree; }
} catch (Exception $e) { // Yeah... I probably don't care at this stage... but maybe I do... \Core\ErrorManagement\exception_handler($e); } } if(!defined('CDN_TYPE')){ define('CDN_TYPE', 'local'); } if(!defined('CDN_LOCAL_ASSETDIR')){ define('CDN_LOCAL_ASSETDIR', 'files/assets/'); } if(!defined('CDN_LOCAL_PUBLICDIR')){ define('CDN_LOCAL_PUBLICDIR', 'files/public/'); } require_once(ROOT_PDIR . 'core/libs/core/Core.class.php'); require_once(ROOT_PDIR . 'core/libs/core/ComponentHandler.class.php'); require_once(ROOT_PDIR . 'core/helpers/UpdaterHelper.class.php'); require_once(ROOT_PDIR . 'core/libs/core/datamodel/Dataset.php'); $db = DMI::GetSystemDMI(); $ds = new \Core\Datamodel\Dataset(); $ds->delete()->table('component')->execute($db); // And now execute reinstall. exec('"' . ROOT_PDIR . '../utilities/reinstall.php' . '"');
/** * Internal function to parse and handle the dataset in the <upgrade> and <install> tasks. * This is used for installations and upgrades. * * Unlike the other parse functions, this handles a single node at a time. * * @param $node DOMElement * @param $verbose bool * * @throws InstallerException */ private function _parseDatasetNode(DOMElement $node, $verbose = false){ $action = $node->getAttribute('action'); $table = $node->getAttribute('table'); $haswhere = false; $sets = array(); $renames = array(); $ds = new Core\Datamodel\Dataset(); $ds->table($table); foreach($node->getElementsByTagName('datasetset') as $el){ $sets[$el->getAttribute('key')] = $el->nodeValue; } foreach($node->getElementsByTagName('datasetrenamecolumn') as $el){ // <datasetrenamecolumn oldname="ID" newname="id"/> $renames[$el->getAttribute('oldname')] = $el->getAttribute('newname'); } foreach($node->getElementsByTagName('datasetwhere') as $el){ $haswhere = true; $ds->where(trim($el->nodeValue)); } switch($action){ case 'alter': if(sizeof($sets)) throw new InstallerException('Invalid mix of arguments on ' . $action . ' dataset request, datasetset is not supported!'); if($haswhere) throw new InstallerException('Invalid mix of arguments on ' . $action . ' dataset request, datasetwhere is not supported!'); foreach($renames as $k => $v){ // ALTER TABLE `controllers` CHANGE `ID` `id` INT( 11 ) NOT NULL AUTO_INCREMENT $ds->renameColumn($k, $v); } break; case 'update': foreach($sets as $k => $v){ $ds->update($k, $v); } break; case 'insert': foreach($sets as $k => $v){ $ds->insert($k, $v); } break; case 'delete': if(sizeof($sets)) throw new InstallerException('Invalid mix of arguments on ' . $action . ' dataset request'); if(!$haswhere) throw new InstallerException('Cowardly refusing to delete with no where statement'); $ds->delete(); break; default: throw new InstallerException('Invalid action type, '. $action); } // and GO! if($verbose){ CLI::PrintActionStart('Executing dataset ' . $action . ' command on ' . $table); } $ds->execute(); if($ds->num_rows){ CLI::PrintActionStatus(true); return array($action . ' on table ' . $table . ' affected ' . $ds->num_rows . ' records.'); } else{ CLI::PrintActionStatus(false); return false; } }
private function load() { if ($this->_loaded) return; // Load in all the data in the components table. try { $res = Core\Datamodel\Dataset::Init()->table('component')->select('*')->execute(); $this->_dbcache = array(); foreach ($res as $r) { $n = strtolower($r['name']); $this->_dbcache[$n] = $r; } } catch (Exception $e) { //echo '<pre>' . $e->getTraceAsString() . '</pre>'; return; } /* // Add all the libraries from the LibraryHandler. foreach(LibraryHandler::singleton()->librariesLoaded as $l){ $ch->_libraries[$l->getName()] = $l->getVersion(); } // Add any classes that come from libraries. $ch->_classes = array_merge($ch->_classes, LibraryHandler::singleton()->getClassList()); */ // Load every component first. foreach ($this->_componentCache as $n => $c) { $c->load(); // If the component is not in the initial dbcache, it must not be installed. // Keep it in the component cache, but do not try to load it just yet. if (!isset($this->_dbcache[$n])) { //unset($this->_componentCache[$n]); continue; } // Set the data from the loaded cache $c->_versionDB = $this->_dbcache[$n]['version']; $c->enabled = ($this->_dbcache[$n]['enabled']); // First check before anything else is even done.... Did the user disable it? if (!$c->enabled) { //echo "Skipping " . $c->getName() . " because it is disabled<br/>"; unset($this->_componentCache[$n]); continue; } //var_dump($c); // Doesn't contain a valid xml, just remove it. if (!$c->isValid()) { if (DEVELOPMENT_MODE) { echo 'Component ' . $c->getName() . ' appears to be invalid due to:<br/>' . $c->getErrors(); //CAEUtils::AddMessage('Component ' . $c->name . ' appears to be invalid due to:<br/>' . $c->_invalidReason); } unset($this->_componentCache[$n]); } } // If the execution mode is CLI, ensure the CLI tools are installed! if (EXEC_MODE == 'CLI') { $cli_component = $this->getComponent('CLI'); // CLI is bundled with the core. // How do you expect to use the CLI tools if they're not installed? hmm??? //if(!$cli_component) die("Cannot execute anything in CLI mode without the CLI component, please download that.\n"); //if(!$cli_component->isInstalled()) $cli_component->install(); } //echo "Loading..."; // Now that I have a list of components available, copy them into a list of // components that are installed. $list = $this->_componentCache; do { $size = sizeof($list); foreach ($list as $n => $c) { // If it's loaded, register it and remove it from the list! if ($c->isInstalled() && $c->isLoadable() && $c->loadFiles()) { // Allow for on-the-fly package upgrading regardless of DEV mode or not. if ($c->needsUpdated()) { $c->upgrade(); } $this->_registerComponent($c); unset($list[$n]); continue; } // Allow for on-the-fly package upgrading regardless of DEV mode or not. if ($c->isInstalled() && $c->needsUpdated() && $c->isLoadable()) { $c->upgrade(); $c->loadFiles(); $this->_registerComponent($c); unset($list[$n]); continue; } // Allow packages to be auto-installed if in DEV mode. // this should NEVER be enabled on production, due to the GIANT // security risk that it could potentially cause if someone manages // to get a rogue component.xml file on the filesystem. (in theory at least) if (!$c->isInstalled() && DEVELOPMENT_MODE && $c->isLoadable()) { // w00t $c->install(); $c->loadFiles(); $this->_registerComponent($c); unset($list[$n]); continue; } } } while ($size > 0 && ($size != sizeof($list))); // If dev mode is enabled, display a list of components installed but not loadable. if (DEVELOPMENT_MODE) { foreach ($list as $l) { // Ignore anything with the execmode different, those should be minor notices for debugging if anything. if ($l->error & Component_2_1::ERROR_WRONGEXECMODE) continue; $msg = 'Could not load installed component ' . $l->getName() . ' due to requirement failed.<br/>' . $l->getErrors(); echo $msg . '<br/>'; //Core::AddMessage($msg); } } $this->_loaded = true; }