/** * Process bbcode inside a string * @since Version 3.10.0 * @param string|DOMDocument $string The HTML or text block to process * @return DOMDocument */ public static function Process($string, $doBbcode = true) { if (!$doBbcode) { return $string; } $timer = Debug::getTimer(); /** * Pre-process the string before we send it through the BBCode parser */ $string = self::preProcessBBCodeUIDs($string); $parser = new Decoda($string); $parser->addPath(__DIR__ . DIRECTORY_SEPARATOR . 'BbcodeEtc' . DIRECTORY_SEPARATOR); $emoticonConfig = ['path' => '//static.railpage.com.au/images/smiles/', 'extension' => 'gif']; $engine = new DecodaPhpEngine(); $engine->addPath(__DIR__ . DIRECTORY_SEPARATOR . 'BbcodeEtc' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR); $parser->setEngine($engine); $parser->defaults(); $parser->setStrict(false); $parser->setLineBreaks(false); $parser->removeHook('Emoticon'); $parser->addFilter(new RailpageImageFilter()); $string = $parser->parse(); $string = html_entity_decode($string); // Fix: if I set escapeHtml in the Decoda options, it fails to auto linkify links //$string = wpautop($string); Debug::LogEvent(__METHOD__, $timer); return $string; }
/** * Process emoticons inside a string * @since Version 3.10.0 * @param string|DOMDocument $string The HTML or text block to process * @param boolean $doEmoticons Boolean flag for processing or skipping emoticons * @return DOMDocument */ public static function Process($string, $doEmoticons = true) { if (!$doEmoticons) { return $string; } $emojiOne = new EmojioneClient(new EmoticonsRuleset()); $emojiOne->ascii = true; $string = $emojiOne->toImage($string); $attr = "data-sceditor-emoticon"; $timer = Debug::getTimer(); if (is_string($string)) { $string = phpQuery::newDocumentHTML($string); } //phpQuery::selectDocument($doc); // Remove #tinymce and .mceContentBody tags foreach (pq('img') as $e) { if (pq($e)->attr($attr)) { $emoticon = pq($e)->attr($attr); if (strlen($emoticon) > 0) { pq($e)->replaceWith(str_replace('\\"', "", $emoticon)); } } } Debug::LogEvent(__METHOD__, $timer); return $string; }
/** * Convert plaintext URI to HTML links. * * Converts URI, www and ftp, and email addresses. Finishes by fixing links * within links. * * @since 0.71 * * @param string $text Content to convert URIs. * @return string Content with converted URIs. */ public static function Process($text) { $timer = Debug::GetTimer(); $r = ''; $textarr = preg_split('/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE); // split out HTML tags foreach ($textarr as $piece) { if (empty($piece) || $piece[0] == '<' && !preg_match('|^<\\s*[\\w]{1,20}+://|', $piece)) { $r .= $piece; continue; } // Long strings might contain expensive edge cases ... if (10000 < strlen($piece)) { // ... break it up foreach (self::_split_str_by_whitespace($piece, 2100) as $chunk) { // 2100: Extra room for scheme and leading and trailing paretheses if (2101 < strlen($chunk)) { $r .= $chunk; // Too big, no whitespace: bail. } else { $r .= make_clickable($chunk); } } } else { $ret = " {$piece} "; // Pad with whitespace to simplify the regexes $url_clickable = '~ ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation ( # 2: URL [\\w]{1,20}+:// # Scheme and hier-part prefix (?=\\S{1,2000}\\s) # Limit to URLs less than about 2000 characters long [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character [\'.,;:!?)] # Punctuation URL character [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character )* ) (\\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing) ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character. // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times. $ret = preg_replace_callback($url_clickable, 'self::_make_url_clickable_cb', $ret); $ret = preg_replace_callback('#([\\s>])((www|ftp)\\.[\\w\\x80-\\xff\\#$%&~/.\\-;:=,?@\\[\\]+]+)#is', 'self::_make_web_ftp_clickable_cb', $ret); $ret = preg_replace_callback('#([\\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\\.)+[0-9a-z]{2,})#i', 'self::_make_email_clickable_cb', $ret); $ret = substr($ret, 1, -1); // Remove our whitespace padding. $r .= $ret; } } // Cleanup of accidental links within links $r = preg_replace('#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "\$1\$3</a>", $r); Debug::LogEvent(__METHOD__, $timer); return $r; }
/** * Constructor * * @param string|null $country * @param string|bool $region */ public function __construct($country = null, $region = false) { Debug::RecordInstance(); $timer = Debug::GetTimer(); if (is_null($country)) { throw new InvalidArgumentException("No country was specified"); } parent::__construct(); $this->load($country, $region); Debug::LogEvent(__METHOD__, $timer); }
/** * 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; }
/** * Find Railpage objects (loco, class, livery) in this image * * @since Version 3.8.7 * * @param string $namespace * @param boolean $force * * @return \Railpage\Images\Image; * @throws \Exception if $namespace is null or empty */ public function findObjects($namespace = null, $force = false) { if (is_null($namespace)) { throw new Exception("Parameter 1 (namespace) cannot be empty"); } $key = sprintf("railpage:images.image=%d;objects.namespace=%s;lastupdate", $this->id, $namespace); $lastupdate = $this->Memcached->fetch($key); if (!$force && $lastupdate && $lastupdate > strtotime("1 day ago")) { return $this; } /** * Start the debug timer */ $timer = Debug::GetTimer(); switch ($namespace) { case "railpage.locos.loco": if (isset($this->meta['tags'])) { foreach ($this->meta['tags'] as $tag) { if (preg_match("@railpage:class=([0-9]+)@", $tag, $matches)) { Debug::LogEvent(__METHOD__ . " :: #1 Instantating new LocoClass object with ID " . $matches[1] . " "); $LocoClass = LocosFactory::CreateLocoClass($matches[1]); } } foreach ($this->meta['tags'] as $tag) { if (isset($LocoClass) && $LocoClass instanceof LocoClass && preg_match("@railpage:loco=([a-zA-Z0-9]+)@", $tag, $matches)) { Debug::LogEvent(__METHOD__ . " :: #2 Instantating new LocoClass object with class ID " . $LocoClass->id . " and loco number " . $matches[1] . " "); $Loco = LocosFactory::CreateLocomotive(false, $LocoClass->id, $matches[1]); if (filter_var($Loco->id, FILTER_VALIDATE_INT)) { $this->addLink($Loco->namespace, $Loco->id); } } } foreach ($this->db->fetchAll("SELECT id AS class_id, flickr_tag AS class_tag FROM loco_class") as $row) { foreach ($this->meta['tags'] as $tag) { if (stristr($tag, $row['class_tag']) && strlen(str_replace($row['class_tag'] . "-", "", $tag) > 0)) { $loco_num = str_replace($row['class_tag'] . "-", "", $tag); Debug::LogEvent(__METHOD__ . " :: #3 Instantating new LocoClass object with class ID " . $row['class_id'] . " and loco number " . $loco_num . " "); $Loco = LocosFactory::CreateLocomotive(false, $row['class_id'], $loco_num); if (filter_var($Loco->id, FILTER_VALIDATE_INT)) { $this->addLink($Loco->namespace, $Loco->id); if (!$Loco->hasCoverImage()) { $Loco->setCoverImage($this); } if (!$Loco->Class->hasCoverImage()) { $Loco->Class->setCoverImage($this); } } } } } } break; case "railpage.locos.class": if (isset($this->meta['tags'])) { foreach ($this->db->fetchAll("SELECT id AS class_id, flickr_tag AS class_tag FROM loco_class") as $row) { foreach ($this->meta['tags'] as $tag) { if ($tag == $row['class_tag']) { $LocoClass = LocosFactory::CreateLocoClass($row['class_id']); if (filter_var($LocoClass->id, FILTER_VALIDATE_INT)) { $this->addLink($LocoClass->namespace, $LocoClass->id); } } } } foreach ($this->meta['tags'] as $tag) { if (preg_match("@railpage:class=([0-9]+)@", $tag, $matches)) { $LocoClass = LocosFactory::CreateLocoClass($matches[1]); if (filter_var($LocoClass->id, FILTER_VALIDATE_INT)) { $this->addLink($LocoClass->namespace, $LocoClass->id); if (!$LocoClass->hasCoverImage()) { $LocoClass->setCoverImage($this); } } } } } break; case "railpage.locos.liveries.livery": if (isset($this->meta['tags'])) { foreach ($this->meta['tags'] as $tag) { if (preg_match("@railpage:livery=([0-9]+)@", $tag, $matches)) { $Livery = new Livery($matches[1]); if (filter_var($Livery->id, FILTER_VALIDATE_INT)) { $this->addLink($Livery->namespace, $Livery->id); } } } } break; } Debug::LogEvent(__METHOD__ . "(\"" . $namespace . "\")", $timer); $this->Memcached->save($key, time()); return $this; }
/** * Constructor * * @param string $code */ public function __construct($code) { parent::__construct(); $this->code = $code; $this->url = new Url("/locations/" . strtolower($this->code)); $countries = ISO_3166::get_countries(); if (strlen($this->code) == 2) { $this->name = $countries[$code]['name']; } else { foreach ($countries as $cc => $data) { if (strtolower($data['name']) == strtolower($this->code)) { $this->code = $cc; $this->url = new Url("/locations/" . strtolower($this->code)); $this->name = $data['name']; } } } Debug::RecordInstance(); $timer = Debug::GetTimer(); if (!$this->loadFromCache() || empty($this->name)) { $woe = Place::getWOEData(strtoupper($code)); if (isset($woe['places']['place'][0]['name'])) { $woe = $woe['places']['place'][0]; $data = ["point" => new Zend_Db_Expr(sprintf("GeomFromText('POINT(%s %s)')", $woe['centroid']['latitude'], $woe['centroid']['longitude'])), "bb_southwest" => new Zend_Db_Expr(sprintf("GeomFromText('POINT(%s %s)')", $woe['boundingBox']['southWest']['latitude'], $woe['boundingBox']['southWest']['longitude'])), "bb_northeast" => new Zend_Db_Expr(sprintf("GeomFromText('POINT(%s %s)')", $woe['boundingBox']['northEast']['latitude'], $woe['boundingBox']['northEast']['longitude'])), "country_code" => $woe['country attrs']['code'], "country_name" => $woe['name'], "timezone" => isset($woe['timezone']) ? $woe['timezone'] : ""]; $this->db->insert("geoplace", $data); $this->name = $woe['name']; $this->centre = new stdClass(); $this->centre->lat = $woe['centroid']['latitude']; $this->centre->lon = $woe['centroid']['longitude']; $this->boundingBox = new stdClass(); $this->boundingBox->northEast = new stdClass(); $this->boundingBox->northEast->lat = $woe['boundingBox']['northEast']['latitude']; $this->boundingBox->northEast->lon = $woe['boundingBox']['northEast']['longitude']; $this->boundingBox->southWest = new stdClass(); $this->boundingBox->southWest->lat = $woe['boundingBox']['southWest']['latitude']; $this->boundingBox->southWest->lon = $woe['boundingBox']['southWest']['longitude']; } } /** * Fetch the WOE (Where On Earth) data from Yahoo */ Debug::LogEvent(__METHOD__, $timer); }
/** * Constructor * @since Version 3.0.1 * @version 3.0.1 * @param int $forumid * @param object $database */ public function __construct($forumid = false, $getParent = true) { parent::__construct(); $timer = Debug::GetTimer(); $this->Module = new Module("forums"); if (filter_var($forumid, FILTER_VALIDATE_INT)) { $this->load($forumid, $getParent); } elseif ($shortname = filter_var($forumid, FILTER_SANITIZE_STRING)) { if (!is_null($shortname)) { $this->load($shortname, $getParent); } } Debug::LogEvent(__METHOD__, $timer); }
/** * Get the latest news items * @version 3.7.5 * @since Version 3.0 * @return mixed * @param int $number * @param int $offset */ public function latest($number = 5, $offset = 0) { $return = false; $mckey = "railpage:news.latest.count=" . $number . ".offset=" . $offset; $mcexp = strtotime("+5 minutes"); // Store for five minutes $Sphinx = $this->getSphinx(); $query = $Sphinx->select("*")->from("idx_news_article")->orderBy("story_time_unix", "DESC")->where("story_active", "=", 1)->limit($offset, $number); $matches = $query->execute(); /** * Attempt to fetch from Sphinx first */ if (is_array($matches) && count($matches)) { foreach ($matches as $id => $row) { $row['time_relative'] = time2str($row['story_time_unix']); $row['time'] = time2str($row['story_time']); // Match the first sentence $line = explode("\n", str_replace("\r\n", "\n", !empty($row['story_lead']) ? $row['story_lead'] : $row['story_blurb'])); $row['firstline'] = strip_tags($line[0]); $row['hometext'] = wpautop(process_bbcode($row['story_blurb'])); $row['bodytext'] = wpautop(process_bbcode($row['story_body'])); $row['title'] = format_topictitle($row['story_title']); $row['featured_image'] = $row['story_image']; if (empty($row['slug'])) { $row['slug'] = $this->createSlug($row['story_id']); } $row['url'] = $this->makePermaLink($row['story_slug']); $matches[$id] = $row; } return $matches; } /** * Fall back to database query */ if (!($data = $this->Memcached->fetch($mckey))) { $timer = Debug::GetTimer(); $query = "SELECT s.*, t.topicname, t.topicimage, t.topictext, u.user_id AS informant_id, u.user_id, u.username, u.user_avatar \r\n FROM nuke_stories AS s\r\n LEFT JOIN nuke_topics AS t ON s.topic = t.topicid\r\n LEFT JOIN nuke_users AS u ON s.informant = u.username\r\n WHERE s.title != \"\"\r\n AND s.approved = ?\r\n ORDER BY s.time DESC\r\n LIMIT ?, ?"; if ($result = $this->db_readonly->fetchAll($query, array("1", $offset, $number))) { $return = array(); foreach ($result as $row) { if (function_exists("relative_date")) { $row['time_relative'] = relative_date(strtotime($row['time'])); } else { $row['time_relative'] = $row['time']; } // Match the first sentence $line = explode("\n", str_replace("\r\n", "\n", $row['hometext'])); $row['firstline'] = strip_tags($line[0]); $row['hometext'] = format_post($row['hometext']); $row['hometext'] = wpautop($row['hometext']); if (empty($row['slug'])) { $row['slug'] = $this->createSlug($row['sid']); } $row['url'] = $this->makePermaLink($row['slug']); $return[] = $row; } $this->Memcached->save($mckey, $return, $mcexp); Debug::LogEvent(__METHOD__, $timer); } return $return; } }
/** * Update the owners/operators * @since Version 3.9.1 * @param string $type * @return void */ private function reloadOrganisations($type) { if (substr($type, -1) !== "s") { $type .= "s"; } $allowed = ["owners", "operators"]; if (!in_array($type, $allowed)) { throw new InvalidArgumentException("Cannot update owners/operators/organisations: " . $type . " is an invalid organisation type"); } $lookup = ["owners" => 1, "operators" => 2]; $type_id = $lookup[$type]; $var_name = substr($type, 0, -1); $var_name_id = substr($type, 0, -1) . "_id"; $this->{$type} = $this->getOrganisations($type_id); reset($this->{$type}); $array = $this->{$type}; if (isset($array[0]['organisation_id']) && isset($array[0]['organisation_name'])) { $this->{$var_name_id} = $array[0]['organisation_id']; $this->{$var_name} = $array[0]['organisation_name']; Debug::LogEvent(__METHOD__ . "() : Latest " . $var_name . " ID requires updating"); return; } $this->{$var_name_id} = 0; $this->{$var_name} = "Unknown"; return; }
/** * Generate the URL slug * @since Version 3.7.5 * @param int $id * @param string $name * @return string */ public function createSlug($id = false, $name = false) { $timer = Debug::GetTimer(); if (filter_var($id, FILTER_VALIDATE_INT) && !$name) { $name = $this->db->fetchOne("SELECT organisation_name FROM organisation WHERE organisation_id = ?", $id); } elseif (filter_var($id, FILTER_VALIDATE_INT) && is_string($name)) { // Do nothing } elseif (isset($this->name) && !empty($this->name)) { $name = $this->name; $id = $this->id; } else { return false; } $proposal = ContentUtility::generateUrlSlug($name, 200); /** * Check that we haven't used this slug already */ $result = $this->db->fetchAll("SELECT organisation_id FROM organisation WHERE organisation_slug = ? AND organisation_id != ?", array($proposal, $id)); if (count($result)) { $proposal .= count($result); } if (isset($this->slug) || empty($this->slug)) { $this->slug = $proposal; } /** * Add this slug to the database */ $data = array("organisation_slug" => $proposal); $where = array("organisation_id = ?" => $id); $rs = $this->db->update("organisation", $data, $where); Debug::LogEvent(__METHOD__, $timer); /** * Return it */ return $proposal; }
/** * Make a permalink for this location * @since Version 3.7.5 * @return string * @param int $id Optional location ID - inherited by * Railpage\Locations\Location so will attempt to use $this->id if none provided */ public function makePermalink($id = false) { $mckey = $id ? "railpage:locations.permalink.id=" . $id : "railpage:locations.permalink.id=" . $this->id; $timer = Debug::GetTimer(); if (!($string = $this->Memcached->fetch($mckey))) { if ((!isset($this->country) || !isset($this->region) || !isset($this->slug)) && $id) { // Fetch it from the database $query = "SELECT country, region, slug FROM location WHERE id = ?"; $data = $this->db->fetchRow($query, $id); if (empty($data['slug'])) { $Location = new Location($id); $data['slug'] = $Location->slug; } } else { $data['country'] = $this->country; $data['region'] = $this->region; $data['slug'] = $this->slug; } $params = [$this->Module->url, str_replace(" ", "-", $data['country']), str_replace(" ", "-", $data['region']), $data['slug']]; $string = strtolower(vsprintf("%s/%s/%s/%s", $params)); $this->Memcached->save($mckey, $string, strtotime("+1 year")); } Debug::LogEvent(__METHOD__, $timer); return $string; }
/** * Remove an object from Memcache * @since Version 3.7.5 * @param string $key * @return mixed */ function remove($key = false) { if (!$key) { throw new Exception("Cannot remove object from memcache - \$key was not specified"); return false; } $timer = Debug::GetTimer(); $rs = $this->cn->delete($key); Debug::LogEvent(($rs === false ? "FAILED" : "SUCCEEDED") . " delete " . $key, $timer); StatsD::increment("rp.memcached.delete"); return $rs; }
/** * Process and format the article (the/a/an) of a timeline item * @since Version 3.9.1 * @param array $row * @return array */ private function processGrammarArticle($row) { $timer = Debug::GetTimer(); $row['event']['article'] = Grammar::getArticle_OfIn($row); $row['event']['article'] = Grammar::getArticle_AnA($row); if (preg_match("@(date)@Di", $row['event']['object'], $matches) && preg_match("@(edited)@Di", $row['event']['action'], $matches)) { $row['event']['preposition'] = "for"; } $row = Grammar::getArticle_The($row); Debug::LogEvent(__METHOD__, $timer); return $row; }
/** * 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; }
/** * Strip headers from within a block of text * @param string|DOMDocument $string * @return DOMDocument */ public static function removeHeaders($string) { $timer = Debug::getTimer(); if (is_string($string)) { $string = phpQuery::newDocumentHTML($string); } foreach (pq('h1') as $e) { pq($e)->replaceWith("<p><strong>" . pq($e)->text() . "</strong></p>"); } foreach (pq('h2') as $e) { pq($e)->replaceWith("<p><strong>" . pq($e)->text() . "</strong></p>"); } foreach (pq('h3') as $e) { pq($e)->replaceWith("<p><strong>" . pq($e)->text() . "</strong></p>"); } Debug::LogEvent(__METHOD__, $timer); return $string; }
/** * Fetch the users' timeline * @since Version 3.5 * * @param object $dateStart * @param object $dateEnd * * @return array */ public function timeline($dateStart, $dateEnd) { $timer = Debug::GetTimer(); $timezzz = (new Timeline())->setUser($this)->generateTimeline($dateStart, $dateEnd); Debug::LogEvent(__METHOD__, $timer); return $timezzz; #return Timeline::GenerateTimeline($this, $date_start, $date_end); }
/** * Convert a Google Maps link into embedded content * @since Version 3.10.0 * @param \DOMElement $e * @return \DOMElement */ public static function EmbedGoogleMap(DOMElement $e) { $timer = Debug::GetTimer(); $Config = AppCore::GetConfig(); $lookup = pq($e)->attr("href"); // Prevent this from f*****g with links in the middle of sentences if (pq($e)->text() != $lookup) { return $e; } if (!preg_match("#google.com(.au)?/maps/(.*)\\@([0-9\\-.]{8,13}),([0-9\\-.]{8,13})#", $lookup, $matches[0]) && !preg_match("#goo.gl/maps/([a-zA-Z0-9]{10,13})#", $lookup, $matches[1])) { return $e; } $basehtml = '<iframe width="%s" height="%s" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/view?key=%s &zoom=%d&maptype=%s¢er=%s" allowfullscreen> </iframe>'; $params = ["100%", 600, $Config->Google->API_Key, 15, "satellite"]; foreach ($matches as $val) { if (!count($val)) { continue; } // Co-ordinates known, great if (count($val) === 5) { $params[] = $val[3] . "," . $val[4]; continue; } // Co-ordinates not known. Shit. Better look 'em up if (count($val) !== 2) { continue; } $Memcached = AppCore::GetMemcached(); $cachekey = sprintf("google:url.shortner=%s", $val[1]); if (!($return = $Memcached->fetch($cachekey))) { $GuzzleClient = new Client(); $url = sprintf("https://www.googleapis.com/urlshortener/v1/url?shortUrl=%s&key=%s", $lookup, "AIzaSyC1lUe1h-gwmFqj9xDTDYI9HYVTUxNscCA"); $response = $GuzzleClient->get($url); // F****d it if ($response->getStatusCode() != 200) { return $e; } $return = json_decode($response->getBody(), true); $Memcached->save($cachekey, $return); } // Get out if it looks problematic if ($return['status'] != "OK") { return $e; } pq($e)->attr("href", $return['longUrl'])->text($return['longUrl']); return self::EmbedGoogleMap($e); continue; } pq($e)->replaceWith(vsprintf($basehtml, $params)); Debug::LogEvent(__METHOD__, $timer); return $e; }
/** * Check SpamCop for the given IP address * @since Version 3.10.0 * @return boolean * @param string $ip */ public static function spamCop($ip) { if (!filter_var($ip, FILTER_VALIDATE_IP)) { $ip = $_SERVER['REMOTE_ADDR']; } $timer = Debug::GetTimer(); $reversedIp = implode(".", array_reverse(explode(".", $ip))); $host = $reversedIp . ".bl.spamcop.net"; $response = gethostbyname($host); if (stristr($response, "127.0.0")) { return true; } Debug::LogEvent(__METHOD__, $timer); return false; }
/** * Constructor * @since Version 3.0.1 * @version 3.0.1 * @param object $database * @param int $postid */ public function __construct($postid = false) { $post_timer_start = Debug::GetTimer(); parent::__construct(); $this->Module = new Module("forums"); $this->timestamp = time(); $this->mckey = sprintf("railpage:forums;post=%d", $postid); $this->Memcached = AppCore::getMemcached(); if (!($row = $this->Memcached->fetch($this->mckey))) { Debug::LogEvent("Could not find forum post in Redis using cache key " . $this->mckey); if (filter_var($postid, FILTER_VALIDATE_INT)) { $timer = Debug::GetTimer(); $query = "SELECT p.*, t.*, u.username, u.user_avatar FROM nuke_bbposts p, nuke_bbposts_text t, nuke_users AS u WHERE u.user_id = p.poster_id AND p.post_id = ? AND t.post_id = p.post_id LIMIT 1"; $row = $this->db->fetchRow($query, $postid); $rs = $this->Memcached->save($this->mckey, $row, 43200); Debug::LogEvent("Fetch forum post from database", $timer); if (!$rs) { Debug::LogEvent("!! Failed to store forum post in cache provider"); } } elseif (is_string($postid)) { $query = "SELECT p.*, t.*, u.username, u.user_avatar FROM nuke_bbposts p, nuke_bbposts_text t, nuke_users AS u WHERE u.user_id = p.poster_id AND t.url_slug = ? AND t.post_id = p.post_id LIMIT 1"; $row = $this->db->fetchRow($query, $postid); $rs = $this->Memcached->save($this->mckey, $row, 43200); } } if (isset($row) && is_array($row)) { $this->id = $row['post_id']; $this->thread = ForumsFactory::CreateThread($row['topic_id']); $this->uid = $row['poster_id']; $this->username = $row['username']; $this->user_avatar = $row['user_avatar']; $this->timestamp = $row['post_time']; $this->ip = $row['poster_ip']; $this->flag_bbCode = $row['enable_bbcode']; $this->flag_html = $row['enable_html']; $this->flag_smilies = $row['enable_smilies']; $this->flag_signature = $row['enable_sig']; $this->edit_timestamp = $row['post_edit_time']; $this->edit_count = $row['post_edit_count']; $this->reported = $row['post_reported']; $this->herring_count = $row['post_edit_count']; $this->bbcodeuid = $row['bbcode_uid']; $this->subject = $row['post_subject']; $this->text = stripslashes($row['post_text']); $this->old_text = stripslashes($row['post_text']); $this->rating = $row['post_rating']; $this->bbcode_uid = $row['bbcode_uid']; $this->editor_version = $row['editor_version']; $this->url_slug = $row['url_slug']; $this->pinned = isset($row['pinned']) ? (bool) $row['pinned'] : false; if (empty($this->url_slug)) { $this->createSlug(); $this->commit(); } $this->lat = $row['lat']; $this->lon = $row['lon']; $this->Date = new DateTime(); $this->Date->setTimestamp($row['post_time']); $this->Author = UserFactory::CreateUser($row['poster_id']); $this->makeLinks(); } Debug::LogEvent(__METHOD__, $post_timer_start); }