/** * Return an event * @since Version 3.9.1 * @return \Railpage\Organisations\Organisation * @param int|string $id */ public static function CreateOrganisation($id = false) { $Memcached = AppCore::getMemcached(); $Redis = AppCore::getRedis(); $Registry = Registry::getInstance(); if (!filter_var($id, FILTER_VALIDATE_INT)) { $slugkey = sprintf(Organisation::REGISTRY_KEY, $id); try { $id = $Registry->get($slugkey); } catch (Exception $e) { $Database = (new AppCore())->getDatabaseConnection(); $id = $Database->fetchOne("SELECT organisation_id FROM organisation WHERE organisation_slug = ?", $id); $Registry->set($slugkey, $id); } } $regkey = sprintf(Organisation::REGISTRY_KEY, $id); try { $Organisation = $Registry->get($regkey); } catch (Exception $e) { $cachekey = sprintf(Organisation::CACHE_KEY, $id); if (!self::USE_REDIS || !($Organisation = $Redis->fetch($cachekey))) { $Organisation = new Organisation($id); if (self::USE_REDIS) { $Redis->save($cachekey, $Organisation); } } $Registry->set($regkey, $Organisation); } return $Organisation; }
/** * Get an new instance of a forum thread * @since Version 3.9.1 * @return \Railpage\Forums\Thread * @param int $thread_id */ public static function CreateThread($thread_id) { Debug::LogEvent(__METHOD__ . "(" . $thread_id . ")"); $key = sprintf("railpage:forums.thread=%d", $thread_id); if ($Thread = self::load($key)) { return $Thread; } $Thread = new Thread($thread_id); self::$Registry->set($key, $Thread); return $Thread; }
/** * Create a download * @since Version 3.10.0 * @param int $id * @return \Railpage\Downloads\Download */ public function createDownload($id = null) { $Registry = Registry::GetInstance(); $cachekey = sprintf("railpage:download=%d", intval($id)); try { $Download = $Registry->get($cachekey); } catch (Exception $e) { $Download = new Download($id); $Registry->set($cachekey, $Download); } return $Download; }
/** * @depends test_loadBanControl * @depends test_newUser */ public function test_banUser($BanControl, $User) { $Registry = Registry::getInstance(); $Registry->remove("redis"); $BanControl->unBanUser($User); $this->assertFalse($BanControl->isClientBanned($User->id, "8.8.8.8")); $BanControl->banUser($User->id, "test ban", strtotime("+1 hour"), $User->id); $this->assertFalse($BanControl->isUserBanned()); $BanControl->isUserBanned($User->id); $BanControl->users = NULL; $BanControl->isUserBanned($User); $BanControl->lookupUser($User->id); $BanControl->banUser($User->id, "test ban", 0, $User->id); $BanControl->lookupUser($User->id); }
/** * Return a user * @since Version 3.9.1 * @return \Railpage\Users\User * @param int|string $id */ public static function CreateUser($id = null) { $Redis = AppCore::getRedis(); $Registry = Registry::getInstance(); $regkey = sprintf(User::REGISTRY_KEY, $id); try { $User = $Registry->get($regkey); } catch (Exception $e) { if (!($User = $Redis->fetch(sprintf("railpage:users.user=%d", $id)))) { $User = new User($id); $Redis->save(sprintf("railpage:users.user=%d", $id), $User, 60 * 60 * 2); } $Registry->set($regkey, $User); } return $User; }
/** * Return an instance of EventCategory * @since Version 3.9.1 * @return \Railpage\Events\EventCategory * @param int|string $id */ public static function CreateEventCategory($id = null) { $CacheDriver = AppCore::getRedis(); $Registry = Registry::getInstance(); $regkey = sprintf(EventCategory::REGISTRY_KEY, $id); try { $EventCategory = $Registry->get($regkey); } catch (Exception $e) { $cachekey = sprintf(EventCategory::CACHE_KEY, $id); if (!self::USE_REDIS || !($EventCategory = $CacheDriver->fetch($cachekey))) { $EventCategory = new EventCategory($id); if (self::USE_REDIS) { $CacheDriver->save($cachekey, $EventCategory); } } $Registry->set($regkey, $EventCategory); } return $EventCategory; }
/** * Return a news article * @since Version 3.9.1 * @return \Railpage\News\Article * @param int|string $id */ public static function CreateArticle($id) { $Redis = AppCore::getRedis(); $Memcached = AppCore::getMemcached(); $Registry = Registry::getInstance(); /** * Lookup article slug-to-ID first */ if (!filter_var($id, FILTER_VALIDATE_INT)) { $mckey = sprintf("railpage:news.article_slug=%s", $id); if (!($article_id = $Memcached->fetch($mckey))) { $Database = AppCore::getDatabase(); $article_id = $Database->fetchOne("SELECT sid FROM nuke_stories WHERE slug = ?", $id); } if (!filter_var($article_id, FILTER_VALIDATE_INT)) { throw new Exception("Could not find an article ID matching URL slug " . $id); } $id = $article_id; } /** * We have an integer article ID, so go ahead and load it */ $regkey = sprintf(Article::REGISTRY_KEY, $id); try { $Article = $Registry->get($regkey); } catch (Exception $e) { if ($Article = $Redis->fetch($regkey)) { $Article->Memcached = $Memcached; $Article->Redis = $Redis; $Database = AppCore::getDatabase(); $Article->setDatabaseConnection($Database)->setDatabaseReadOnlyConnection($Database); } else { $Article = new Article($id); $Redis->save($regkey, $Article); } $Registry->set($regkey, $Article); } return $Article; }
/** * Create a photo competition object from an ID or URL slug * @since Version 3.10.0 * @param string|int $id * @return \Railpage\Images\Competition */ public static function CreatePhotoComp($id) { //$Database = AppCore::GetDatabase(); $cacheHandler = AppCore::getRedis(); $Registry = Registry::getInstance(); if (!filter_var($id, FILTER_VALIDATE_INT)) { $lookup = Utility\CompetitionUtility::getIDFromSlug($id); if (!filter_var($lookup, FILTER_VALIDATE_INT)) { throw new Exception("Could not find a competition ID from URL slug " . $id); } $id = $lookup; } $regkey = sprintf(Competition::CACHE_KEY, $id); try { $Competition = $Registry->get($regkey); } catch (Exception $e) { #if (!$Competition = $cacheHandler->fetch($regkey)) { $Competition = new Competition($id); $cacheHandler->save($regkey, $Competition, strtotime("+1 day")); #} $Registry->set($regkey, $Competition); } return $Competition; }
/** * Save an error message * * @since Version 3.10.0 * * @param string|Exception $error * * @return void */ public static function SaveError($error) { if ($error instanceof Exception) { $error = $error->getMessage(); } $Registry = Registry::getInstance(); try { $Error_Handler = $Registry->get("errorlogger"); } catch (Exception $e) { require_once "includes" . DS . "error.class.php"; $Error_Handler = new Error_Handler(); $Registry->set("errorlogger", $Error_Handler); } $Error_Handler->save($error); return; }
/** * Commit changes to database * @since Version 3.2 * @version 3.8.7 * @return boolean */ public function commit() { $timer = Debug::getTimer(); $this->validate(); $data = Utility\LocomotiveUtility::getSubmitData($this); if (!filter_var($this->id, FILTER_VALIDATE_INT)) { $rs = $this->db->insert("loco_unit", $data); $this->id = $this->db->lastInsertId(); $verb = "Insert"; } else { $this->Memcached->delete($this->mckey); $this->Redis->delete($this->mckey); $where = array("loco_id = ?" => $this->id); $verb = "Update"; $rs = $this->db->update("loco_unit", $data, $where); } // Update the registry $Registry = Registry::getInstance(); $regkey = sprintf(self::REGISTRY_KEY, $this->id); $Registry->remove($regkey)->set($regkey, $this); $this->Memcached->delete(sprintf(self::CACHE_KEY_DESC, $this->id)); Debug::logEvent("Zend_DB: commit loco ID " . $this->id, $timer); $this->makeLinks(); return true; }
/** * Commit changes to existing user or create new user * @since Version 3.1 * @version 3.9 * * @param boolean $force Force an update of this user even if certain values (eg a password) are empty * * @return boolean */ public function commit($force = false) { $this->validate($force); Utility\UserUtility::clearCache($this); $data = array(); foreach (Utility\UserUtility::getColumnMapping() as $key => $var) { $data[$key] = $this->{$var}; } if (is_string($data['meta']) && strpos($data['meta'], "\\\\\\\\") !== false) { $data['meta'] = NULL; } $json = ["meta", "user_opts"]; foreach ($json as $key) { $data[$key] = json_encode($data[$key]); } if ($this->RegistrationDate instanceof DateTime) { $data['user_regdate_nice'] = $this->RegistrationDate->format("Y-m-d H:i:s"); } if (filter_var($this->id, FILTER_VALIDATE_INT)) { $this->db->update("nuke_users", $data, array("user_id = ?" => $this->id)); } else { $this->db->insert("nuke_users", $data); $this->id = $this->db->lastInsertId(); $this->guest = false; $this->createUrls(); } $this->id = intval($this->id); // Update the registry $Registry = Registry::getInstance(); $regkey = sprintf(self::REGISTRY_KEY, $this->id); $Registry->remove($regkey); #->set($regkey, $this); return true; }
/** * Return an instance of this object from the cache or whateverzz * @since Version 3.9.1 * @return \Railpage\Place */ public static function Factory($lat = false, $lon = false) { $Memcached = AppCore::getMemcached(); $Redis = AppCore::getRedis(); $Registry = Registry::getInstance(); $regkey = sprintf("railpage.place;lat=%s;lon=%s", $lat, $lon); try { $Place = $Registry->get($regkey); } catch (Exception $e) { $Place = new Place($lat, $lon); $Registry->set($regkey, $Place); } return $Place; }
/** * Get our smarty template engine * @since Version 3.9.1 * @return \Railpage\Template */ public static function getSmarty() { $Registry = Registry::getInstance(); $Config = self::getConfig(); try { $Smarty = $Registry->get("smarty"); } catch (Exception $e) { $Smarty = new Template(); /** * Send the RP config to Smarty */ $Smarty->setRailpageConfig($Config); /** * Smarty options and configuration */ $Smarty->debugging = false; $Smarty->caching = false; $Smarty->cache_lifetime = 120; $Smarty->site_root = RP_SITE_ROOT; $Smarty->use_sub_dirs = true; $Smarty->assign("rp_host", RP_HOST); $Smarty->assign("rand", rand()); $Smarty->assign("rp_version", defined("RP_VERSION") ? RP_VERSION : "0.0.0"); $Smarty->assign("rp_web_root", defined("RP_WEB_ROOT") ? RP_WEB_ROOT : __DIR__); $Smarty->assign("site_name", $Config->SiteName); $Smarty->assign("rp_production", defined("RP_ISPRODUCTION") ? "RP_ISPRODUCTION" : define("RP_ISPRODUCTION", true) ? true : false); $Smarty->assign("ad_header", $Config->AdHeader); $Smarty->assign("rp_map_styles", json_encode($Config->Google->Maps->Styles)); $Smarty->assign("rp_map_icons", json_encode($Config->Google->Maps->Icons)); $Smarty->subtheme = "smooth"; $Smarty->Assign("rp_ui_show_banner_head", true); /** * Set the template compile directory */ $Smarty->setCompileDir(dirname(RP_SITE_ROOT) . DS . "smarty" . DS . "compile"); $Registry->set("smarty", $Smarty); } return $Smarty; }
/** * Build the Forums ACL * @since Version 3.8.7 * @param boolean $force Force an update of the ACL * @todo Finish this shit */ public function buildACL($force = false) { $Registry = Registry::getInstance(); try { $ForumsACL = $Registry->get("forumsacl"); $this->ZendACL = $ForumsACL; return; } catch (Exception $e) { // F**k it } Debug::RecordInstance(__METHOD__); $timer = Debug::getTimer(); $acl = $Registry->get("acl"); if (!$this->User instanceof User) { throw new Exception("A valid user must be set before the ACL can be built"); } $mckey = "railpage.forums.list"; if ($force || !($forums = $this->Memcached->fetch($mckey))) { $query = "SELECT forum_id FROM nuke_bbforums"; $forums = $this->db->fetchAll($query); $this->Memcached->save($mckey, $forums); } $acl_forums = array(); /** * Add all the forums to the ACL */ foreach ($forums as $row) { $acl_forum_name = sprintf("railpage.forums.forum:%d", $row['forum_id']); $acl_forums[$row['forum_id']] = $acl_forum_name; try { $acl->get($acl_forum_name); } catch (Exception $e) { $acl->addResource(new Zend_Acl_Resource($acl_forum_name)); } } /** * Get the forum permissions from the database */ $a_sql = array("auth_view", "auth_read", "auth_post", "auth_reply", "auth_edit", "auth_delete", "auth_sticky", "auth_announce", "auth_vote", "auth_pollcreate"); $auth_fields = array('auth_view', 'auth_read', 'auth_post', 'auth_reply', 'auth_edit', 'auth_delete', 'auth_sticky', 'auth_announce', 'auth_vote', 'auth_pollcreate'); $query = "SELECT forum_id, " . implode(", ", $a_sql) . ", " . self::AUTH_ACL . " AS auth_mod FROM nuke_bbforums"; $db_acl = array(); foreach ($this->db->fetchAll($query) as $row) { $db_acl[$row['forum_id']] = $row; } /** * Get the group permissions for this user */ $query = "SELECT a.* FROM nuke_bbauth_access AS a WHERE a.group_id IN (SELECT group_id FROM nuke_bbuser_group WHERE user_id = ? AND user_pending = 0)"; $gperms = array(); foreach ($this->db->fetchAll($query, $this->User->id) as $perm) { $forum_id = $perm['forum_id']; $group_id = $perm['group_id']; unset($perm['forum_id']); unset($perm['group_id']); $gperms[$forum_id][$group_id] = $perm; } /** * Guest details */ $guestfucknamingthis = [self::AUTH_MOD => $this->User->inGroup(RP_GROUP_MODERATORS), self::AUTH_ADMIN => $this->User->inGroup(RP_GROUP_ADMINS)]; /** * Add the forum permissions to Zend_ACL */ foreach ($db_acl as $forum_id => $permissions) { $allowed = array(); $denied = array(); unset($permissions['forum_id']); $allowed = array_merge($allowed, array_keys($permissions, self::AUTH_ALL)); if (!$this->User->guest) { $allowed = array_merge($allowed, array_keys($permissions, self::AUTH_REG)); } if ($guestfucknamingthis[self::AUTH_MOD]) { $allowed = array_merge($allowed, array_keys($permissions, self::AUTH_MOD)); } if ($guestfucknamingthis[self::AUTH_ADMIN]) { $allowed = array_merge($allowed, array_keys($permissions, self::AUTH_ADMIN)); } $perms_acl = array_keys($permissions, self::AUTH_ACL); if (count($perms_acl)) { if (isset($gperms[$forum_id])) { foreach ($gperms[$forum_id] as $group) { foreach ($group as $gitem => $gval) { $allowed = array_merge($allowed, array_keys($permissions, self::AUTH_REG)); if ($guestfucknamingthis[self::AUTH_MOD]) { $allowed = array_merge($allowed, array_keys($permissions, self::AUTH_MOD)); } if ($guestfucknamingthis[self::AUTH_ADMIN]) { $allowed = array_merge($allowed, array_keys($permissions, self::AUTH_ADMIN)); } } } } } $allowed = array_unique($allowed); #continue; /* foreach ($permissions as $item => $value) { switch ($value) { case self::AUTH_ACL . "zzz" : if (isset($gperms[$forum_id])) { foreach ($gperms[$forum_id] as $group) { foreach ($group as $gitem => $gval) { switch ($gval) { case self::AUTH_REG : $allowed[] = $item; break; case self::AUTH_ACL : // Inception break; case self::AUTH_MOD : if ($this->User->inGroup(RP_GROUP_MODERATORS)) { $allowed[] = $gitem; } break; case self::AUTH_ADMIN : if ($this->User->inGroup(RP_GROUP_ADMINS)) { $allowed[] = $gitem; } break; } } } } break; case self::AUTH_MOD . "zzz": if ($this->User->inGroup(RP_GROUP_MODERATORS)) { $allowed[] = $item; } break; case self::AUTH_ADMIN . "zzz" : if ($this->User->inGroup(RP_GROUP_ADMINS)) { $allowed[] = $item; } break; } } */ foreach ($permissions as $item => $value) { if (!in_array($item, $allowed)) { $denied[] = $item; } } #$allowed = array_unique($allowed); #$denied = array_unique($denied); $acl->allow("forums_viewer", sprintf("railpage.forums.forum:%d", $forum_id), $allowed); $acl->deny("forums_viewer", sprintf("railpage.forums.forum:%d", $forum_id), $denied); } $Registry->set("acl", $acl); $Registry->set("forumsacl", $acl); $this->ZendACL = $acl; Debug::LogEvent(__METHOD__, $timer); return; }
/** * Get impressions on this module over a given date range * * @since Version 3.9.1 * @return array * * @param \DateTime $dateFrom * @param \DateTime $dateTo */ public function getImpressions(DateTime $dateFrom, DateTime $dateTo) { if (!is_object($this->db)) { $Registry = Registry::getInstance(); $this->db = $Registry->get("db"); } $query = "SELECT count(log_id) AS num, DATE_FORMAT(date, '%Y-%m-%d') AS date FROM log_useractivity WHERE module_id = ? AND date >= ? AND date <= ? GROUP BY DATE_FORMAT(date, '%Y-%m-%d')"; $return = array(); $params = [$this->id, $dateFrom->Format("Y-m-d 00:00:00"), $dateTo->Format("Y-m-d 23:59:59")]; foreach ($this->db->fetchAll($query, $params) as $row) { $return[$row['date']] = $row['num']; } return $return; }
/** * Commit changes to the database * @since Version 3.2 * @version 3.8.7 * @return boolean */ public function commit() { $this->validate(); $timer = Debug::getTimer(); $this->flushMemcached(); $data = array("name" => $this->name, "desc" => $this->desc, "introduced" => $this->introduced, "wheel_arrangement_id" => $this->wheel_arrangement_id, "loco_type_id" => $this->type_id, "manufacturer_id" => $this->manufacturer_id, "flickr_tag" => $this->flickr_tag, "flickr_image_id" => $this->flickr_image_id, "length" => $this->length, "weight" => $this->weight, "axle_load" => $this->axle_load, "tractive_effort" => $this->tractive_effort, "model" => $this->model, "download_id" => empty($this->download_id) ? 0 : $this->download_id, "slug" => empty($this->slug) ? "" : $this->slug, "meta" => json_encode(isset($this->meta) && is_array($this->meta) ? $this->meta : array())); if (empty($this->date_added)) { $data['date_added'] = time(); } if (!empty($this->date_added)) { $data['date_modified'] = time(); } if ($this->Asset instanceof Asset) { $data['asset_id'] = $this->Asset->id; } foreach ($data as $key => $val) { if (is_null($val)) { $data[$key] = ""; } } // Update if (filter_var($this->id, FILTER_VALIDATE_INT)) { // Update $where = array("id = ?" => $this->id); $this->db->update("loco_class", $data, $where); $verb = "Update"; } // Insert if (!filter_var($this->id, FILTER_VALIDATE_INT)) { $this->db->insert("loco_class", $data); $this->id = intval($this->db->lastInsertId()); $this->createSlug(); $this->commit(); $this->url = new Url($this->makeClassURL($this->slug)); $this->url->edit = sprintf("%s?mode=class.edit&id=%d", $this->Module->url, $this->id); $this->url->addLoco = sprintf("%s?mode=loco.edit&class_id=%d", $this->Module->url, $this->id); $verb = "Insert"; } // Update the registry $Registry = Registry::getInstance(); $regkey = sprintf(self::REGISTRY_KEY, $this->id); $Registry->set($regkey, $this); Debug::logEvent(__METHOD__ . " :: ID " . $this->id, $timer); $this->Memcached->delete("railpage:loco.class.bytype=all"); return true; }
/** * Return a thing * * @since Version 3.9.1 * @return mixed * * @param string $Object An instance of Locomotive, Class, Livery etc to be created * @param int|string $id */ public static function Create($Object, $id) { $class = sprintf("\\Railpage\\Locos\\%s", $Object); $regkey = sprintf("railpage:locos.%s=%d", strtolower($Object), $id); $Registry = Registry::getInstance(); try { $Object = $Registry->get($regkey); } catch (Exception $e) { $Object = new $class($id); $Registry->set($regkey, $Object); } return $Object; }
/** * Create a country * @since Version 3.10.0 * @param string $country * @param string $region * @return \Railpage\Locations\Region */ public static function CreateCountry($code) { $Memcached = AppCore::getMemcached(); $Redis = AppCore::getRedis(); $Registry = Registry::getInstance(); $key = sprintf(Country::CACHE_KEY, strtolower($code)); try { $Country = $Registry->get($key); } catch (Exception $e) { if ($Country = $Redis->fetch($key)) { $Registry->set($key, $Country); return $Country; } $Country = new Country($code); $Registry->set($key, $Country); $Redis->save($key, $Country, 0); } return $Country; }
public function test_remove() { $Registry = Registry::getInstance(); $Registry->remove(self::KEY); }
/** * Get reputation markers for this post * @since Version 3.9 * @return array * @param boolean $force Force an update, removing whatever is cached in Memcache */ public function getReputationMarkers($force = false) { $mckey = sprintf("railpage:forums.post;id=%d;getreputationmarkers", $this->id); $Registry = Registry::getInstance(); $Memcached = new Memcached(); $Memcached->addServer($Config->Memcached->Host, 11211); #$cacheDriver = new MemcachedCache; #$cacheDriver->setMemcached($Memcached); #$this->Memcached = $cacheDriver; if ($force || !($types = $Memcached->get($mckey))) { $query = "SELECT r.*, u.username FROM nuke_bbposts_reputation AS r LEFT JOIN nuke_users AS u ON r.user_id = u.user_id WHERE r.post_id = ?"; $types = $this->getReputationTypes(); foreach ($this->db->fetchAll($query, $this->id) as $row) { $types[$row['type']]['votes'][] = $row; $types[$row['type']]['count'] = count($types[$row['type']]['votes']); } $Memcached->set($mckey, $types, 0); } return $types; }