/** * Constructor * @praam int $category_id */ public function __construct($category_id = false, $recurse = true) { parent::__construct(); if (filter_var($category_id, FILTER_VALIDATE_INT)) { $this->id = $category_id; } elseif (is_string($category_id)) { $this->id = $this->db->fetchOne("SELECT cid FROM nuke_links_categories WHERE slug = ?", $category_id); } if (filter_var($this->id, FILTER_VALIDATE_INT)) { $this->mckey = sprintf("railpage.links.category=%d", $this->id); if (!($row = getMemcacheObject($this->mckey))) { $row = $this->db->fetchRow("SELECT * FROM nuke_links_categories WHERE cid = ?", $this->id); if (!empty($row)) { setMemcacheObject($this->mckey, $row); } } $this->name = $row['title']; $this->desc = $row['cdescription']; $this->parent_id = $row['parentid']; $this->slug = $row['slug']; $this->url = $this->makePermalink($this->id); if ($this->parent_id > 0 && $recurse) { $this->parent = new Category($this->parent_id); } } }
/** * Constructor * @since Version 3.9 * @param int $id */ public function __construct($id = false) { parent::__construct(); if (is_string($id)) { $query = "SELECT id FROM timetable_points WHERE name = ? OR slug = ?"; if ($tmpid = $this->db->fetchOne($query, array($id, $id))) { $id = $tmpid; } else { $this->name = $id; $this->commit(); } } if (filter_var($id, FILTER_VALIDATE_INT)) { $this->mckey = sprintf("railpage:timetable.point=%d", $id); if (!($row = getMemcacheObject($this->mckey))) { $query = "SELECT * FROM timetable_points WHERE id = ?"; $row = $this->db->fetchRow($query, $id); setMemcacheObject($this->mckey, $row); } } if (isset($row) && count($row)) { $this->id = $row['id']; $this->lat = $row['lat']; $this->lon = $row['lon']; $this->name = $row['name']; $this->slug = isset($row['slug']) ? $row['slug'] : ""; if (!isset($row['slug']) || empty($row['slug']) || substr($row['slug'], -1, 1) == "1") { $this->createSlug(); $this->commit(); } $this->url = new Url(sprintf("%s/p/%s", $this->Module->url, $this->slug)); $this->url->edit = sprintf("%s?mode=point.edit", $this->url->url); } }
/** * Constructor * @since Version 3.8.6 * @param int|string $id */ public function __construct($id = false) { parent::__construct(); if ($id) { $mckey = "railpage:fwlink=" . md5($id); if ($row = getMemcacheObject($mckey)) { $this->id = $row['id']; $this->url = $row['url']; $this->title = $row['title']; } else { if (filter_var($id, FILTER_VALIDATE_INT)) { $query = "SELECT * FROM fwlink WHERE id = ?"; } else { $query = "SELECT * FROM fwlink WHERE url = ?"; } if ($row = $this->db->fetchRow($query, $id)) { $this->id = $row['id']; $this->url = $row['url']; $this->title = $row['title']; setMemcacheObject($mckey, $row, strtotime("+1 month")); } } if (!empty($this->url)) { $this->url_canonical = sprintf("http://%s%s", RP_HOST, $this->url); $this->url_short = sprintf("http://%s/fwlink?id=%d", RP_HOST, $this->id); } } }
/** * Constructor * @since Version 3.8.7 * @param int $id */ public function __construct($id = false) { parent::__construct(); $this->Module = new Module("glossary"); if (filter_var($id, FILTER_VALIDATE_INT)) { $this->id = $id; $this->mckey = sprintf("%s.entry=%d", $this->Module->namespace, $this->id); deleteMemcacheObject($this->mckey); if ($row = getMemcacheObject($this->mckey)) { } else { $query = "SELECT type, short, full, example, date, author FROM glossary WHERE id = ?"; $row = $this->db->fetchRow($query, $this->id); setMemcacheObject($this->mckey, $row); } if (isset($row) && is_array($row)) { $this->name = $row['short']; $this->text = $row['full']; $this->example = $row['example']; $this->Type = new Type($row['type']); $this->url = sprintf("%s?mode=entry&id=%d", $this->Module->url, $this->id); if ($row['date'] == "0000-00-00 00:00:00") { $this->Date = new DateTime(); $this->commit(); } else { $this->Date = new DateTime($row['date']); } $this->Author = new User($row['author']); } } }
/** * Get filters from the database * @since Version 3.8.7 * @return $this */ public function getFilters() { if (!$this->User instanceof User) { throw new Exception("Cannot get filters for news feed because no valid user was specified"); } if (!($row = getMemcacheObject(sprintf("rp:news.feed=%d", $this->User->id)))) { if ($row = $this->db->fetchRow("SELECT keywords, topics FROM news_feed WHERE user_id = ?", $this->User->id)) { setMemcacheObject(sprintf("rp:news.feed=%d", $this->User->id), $row); } } if (isset($row) && is_array($row)) { $this->filter_words = json_decode($row['keywords'], true); $this->filter_topics = json_decode($row['topics'], true); } return $this; }
/** * Constructor * @param string $slug */ public function __construct($slug) { global $ZendDB, $ZendDB_ReadOnly; $mckey = "railpage:news.article_slug=" . $slug; if ($story_id = getMemcacheObject($mckey)) { parent::__construct($story_id); } else { $story_id = $ZendDB_ReadOnly->fetchOne("SELECT sid FROM nuke_stories WHERE slug = ?", $slug); if (filter_var($story_id, FILTER_VALIDATE_INT)) { setMemcacheObject($mckey, $story_id, strtotime("+6 months")); parent::__construct($story_id); } else { throw new \Exception("Could not find a story matching URL slug " . $slug); return false; } } }
/** * Constructor * @since Version 3.0.1 * @version 3.8.7 * @param string $topic_alias */ public function __construct($topic_alias = false) { parent::__construct(); if (!$topic_alias) { return false; } if ($this->db instanceof \sql_db) { $query = "SELECT * FROM nuke_topics WHERE topicname = '" . $this->db->real_escape_string($topic_alias) . "'"; if ($rs = $this->db->query($query)) { if ($rs->num_rows == 1) { $row = $rs->fetch_assoc(); $this->id = $row['topicid']; $this->alias = $topic_alias; $this->title = $row['topictext']; $this->image = $row['topicimage']; } } else { trigger_error(__CLASS__ . ": Could not retrieve topic alias " . $topic_alias); trigger_error($this->db->error); trigger_error($query); return false; } } else { $mckey = "railpage:news.topic_slug=" . $topic_alias; if ($row = getMemcacheObject($mckey)) { $this->id = $row['topicid']; $this->alias = $topic_alias; $this->title = $row['topictext']; $this->image = $row['topicimage']; } else { $query = "SELECT * FROM nuke_topics WHERE topicname = ?"; if ($row = $this->db_readonly->fetchRow($query, $topic_alias)) { $this->id = $row['topicid']; $this->alias = $topic_alias; $this->title = $row['topictext']; $this->image = $row['topicimage']; setMemcacheObject($mckey, $row, strtotime("+6 months")); } } } }
/** * List all online registered users * @since Version 3.0 * @version 3.9 * @return mixed */ public function onlineUsers() { $mckey = "railpage:onlineusers"; if ($return = getMemcacheObject($mckey)) { foreach ($return as $id => $row) { if (isset($row['last_session_ip'])) { return $return; } } } $return = array(); $query = "SELECT user_id, user_level, username, last_session_ip FROM nuke_users WHERE user_session_time > " . (time() - 60 * 10) . " ORDER BY username ASC"; if ($result = $this->db->fetchAll($query)) { foreach ($result as $row) { $row['bot'] = false; $row['guest'] = false; $return[$row['user_id']] = $row; } setMemcacheObject($mckey, $return, strtotime("+1 minute")); } return $return; }
/** * Get liveries carried by this loco * Based on tagged Flickr photos * @since Version 3.2 * @param object $f * @return array|boolean */ public function getLiveries($f = false) { if (is_object($f)) { $mckey = "railpage:locos.liveries.loco_id=" . $this->id; if ($result = getMemcacheObject($mckey)) { return $result; } else { // Get photos of this loco, including tags $result = $f->groups_pools_getPhotos(RP_FLICKR_GROUPID, $this->flickr_tag, NULL, NULL, "tags"); $tags = array(); if (isset($result['photos']['photo'])) { foreach ($result['photos']['photo'] as $photo) { $rawtags = explode(" ", $photo['tags']); foreach ($rawtags as $tag) { if (preg_match("@railpage:livery=([0-9]+)@", $tag, $matches)) { if (!in_array($matches[1], $tags)) { $tags[] = $matches[1]; } } } } } if ($memcache) { $memcache->delete($mckey); $memcache->set($mckey, $tags, strtotime("+1 day")); // Expire in one day } } if (count($tags)) { return $tags; } else { return false; } } else { $query = "SELECT l.livery_id AS id, l.livery AS name, l.photo_id FROM loco_livery AS l LEFT JOIN loco_unit_livery AS ul ON l.livery_id = ul.livery_id WHERE ul.ignored = ? AND ul.loco_id = ? GROUP BY l.livery_id ORDER BY l.livery"; $return = array(); foreach ($this->db->fetchAll($query, array("0", $this->id)) as $row) { $Livery = new Livery($row['id']); $row = array("id" => $Livery->id, "name" => $Livery->name, "photo" => array("id" => $Livery->photo_id, "provider" => "flickr")); $return[] = $row; } return $return; } }
/** * Get stories from topic * @version 3.0 * @since Version 3.0 * @return mixed * @param int $page * @param int $limit * @param boolean $total */ public function stories($page = 0, $limit = 25, $total = true) { $return = false; $mckey = "railpage:topic_id=" . $this->id . ".stories.page=" . $page . ".limit=" . $limit . ".total=" . (int) $total; $mcexp = strtotime("+1 hour"); if (!($return = getMemcacheObject($mckey))) { // Get it from Sphinx $Sphinx = $this->getSphinx(); $query = $Sphinx->select("*")->from("idx_news_article")->orderBy("story_time_unix", "DESC")->limit($page * $limit, $limit)->where("topic_id", "=", $this->id); $matches = $query->execute(); $meta = $Sphinx->query("SHOW META"); $meta = $meta->execute(); if (is_array($matches) && count($matches)) { $return = array("total" => $meta[1]['Value'], "children" => array(), "page" => $page, "perpage" => $limit, "topic_id" => $this->id); foreach ($matches as $id => $row) { $row['time_relative'] = time2str($row['story_time_unix']); $row['time'] = $row['story_time']; $row['title'] = format_topictitle($row['story_title']); // Match the first sentence $line = explode("\n", $row['story_blurb']); $row['firstline'] = preg_replace('/([^?!.]*.).*/', '\\1', strip_tags($line[0])); if (empty($row['story_slug'])) { $row['slug'] = $this->createSlug($row['story_id']); } $row['url'] = $this->makePermaLink($row['story_slug']); $row['hometext'] = $row['story_blurb']; $row['bodytext'] = $row['story_body']; $row['featured_image'] = $row['story_image']; $row['informant'] = $row['username']; $return['children'][$id] = $row; } } } if (!isset($return) || $return === false || !is_array($return)) { $query = "SELECT SQL_CALC_FOUND_ROWS s.*, t.topicname, t.topicimage, t.topictext, u.user_id AS informant_id FROM nuke_stories AS s LEFT JOIN nuke_topics AS t ON s.topic = t.topicid LEFT JOIN nuke_users AS u ON s.informant = u.username WHERE s.topic = ? AND s.approved = ? ORDER BY s.time DESC LIMIT ?, ?"; $return = array(); $return['total'] = 0; $return['children'] = array(); $return['page'] = $page; $return['perpage'] = $limit; $return['topic_id'] = $this->id; if ($result = $this->db_readonly->fetchAll($query, array($this->id, "1", $page * $limit, $limit))) { $return['total'] = $this->db_readonly->fetchOne("SELECT FOUND_ROWS() AS total"); foreach ($result as $row) { if (function_exists("relative_date")) { $row['time_relative'] = relative_date(strtotime($row['time'])); } else { $row['time_relative'] = $row['time']; } $row['title'] = format_topictitle($row['title']); // Match the first sentence $line = explode("\n", $row['hometext']); $row['firstline'] = preg_replace('/([^?!.]*.).*/', '\\1', strip_tags($line[0])); if (empty($row['slug'])) { $row['slug'] = $this->createSlug($row['sid']); } $row['url'] = $this->makePermaLink($row['slug']); $return['children'][] = $row; } } setMemcacheObject($mckey, $return, $mcexp); } return $return; }
/** * Fetch a page * @since Version 3.5 */ public function fetch() { if (!$this->id) { throw new Exception("Cannot fetch page - no page ID given"); return false; } if (!($row = getMemcacheObject($this->mckey))) { $query = "SELECT * FROM nuke_pages WHERE pid = ?"; $row = $this->db->fetchRow($query, $this->id); foreach ($row as $key => $val) { $row[$key] = stripslashes($val); } setMemcacheObject($this->mckey, $row, strtotime("+1 month")); } $this->title = $row['title']; $this->subtitle = $row['subtitle']; $this->active = $row['active']; $this->header = $row['page_header']; $this->body = $row['text']; $this->footer = $row['page_footer']; $this->date = new DateTime($row['date']); $this->hits = $row['counter']; $this->langauge = isset($row['language']) ? $row['language'] : NULL; $this->permalink = $row['shortname']; $this->url = new Url(sprintf("/static-%s.htm", $this->permalink)); $this->url->edit = sprintf("/admin/pages/edit/%d", $this->id); return true; }
/** * Constructor * @since Version 3.8.7 * @param int $id */ public function __construct($id = NULL) { parent::__construct(); $this->Module = new Module("images"); if (filter_var($id, FILTER_VALIDATE_INT)) { /** * Record this in the debug log */ debug_recordInstance(__CLASS__); $this->mckey = sprintf("railpage:image=%d", $id); if (!($row = getMemcacheObject($this->mckey))) { $query = "SELECT i.id, i.provider, i.photo_id, i.modified, i.meta, i.lat, i.lon FROM image AS i WHERE i.id = ?"; $row = $this->db->fetchRow($query, $id); $row['meta'] = json_decode($row['meta'], true); setMemcacheObject($this->mckey, $row, strtotime("+24 hours")); } $this->id = $id; $this->provider = $row['provider']; $this->photo_id = $row['photo_id']; $this->Date = new DateTime($row['modified']); $this->title = !empty($row['meta']['title']) ? format_topictitle($row['meta']['title']) : "Untitled"; $this->description = $row['meta']['description']; $this->sizes = $row['meta']['sizes']; $this->links = $row['meta']['links']; $this->meta = $row['meta']['data']; $this->url = new Url("/image?id=" . $this->id); if ($this->provider == "rpoldgallery") { $GalleryImage = new \Railpage\Gallery\Image($this->photo_id); $this->url->source = $GalleryImage->url->url; if (empty($this->meta['source'])) { $this->meta['source'] = $this->url->source; } } /** * Normalize some sizes */ if (count($this->sizes)) { if (!isset($this->sizes['thumb'])) { foreach ($this->sizes as $size) { if ($size['width'] >= 280 && $size['height'] >= 150) { $this->sizes['thumb'] = $size; break; } } } if (!isset($this->sizes['small'])) { foreach ($this->sizes as $size) { if ($size['width'] >= 500 && $size['height'] >= 281) { $this->sizes['small'] = $size; break; } } } $width = 0; foreach ($this->sizes as $size) { if ($size['width'] > $width) { $this->sizes['largest'] = $size; $width = $size['width']; } } foreach ($this->sizes as $size) { if ($size['width'] >= 1920) { $this->sizes['fullscreen'] = $size; break; } } foreach ($this->sizes as $size) { if ($size['width'] > 1024 && $size['width'] <= 1920) { $this->sizes['larger'] = $size; break; } } foreach ($this->sizes as $size) { if ($size['width'] == 1024) { $this->sizes['large'] = $size; break; } } foreach ($this->sizes as $size) { if ($size['width'] == 800) { $this->sizes['medium'] = $size; break; } } } if (isset($row['meta']['author'])) { $this->author = json_decode(json_encode($row['meta']['author'])); if (isset($this->author->railpage_id)) { $this->author->User = new User($this->author->railpage_id); } } else { $this->populate(true); } if (round($row['lat'], 3) != "0.000" && round($row['lon'], 3) != "0.000") { try { $this->Place = new Place($row['lat'], $row['lon']); } catch (Exception $e) { // Throw it away. Don't care. } } /** * Set the source URL */ if (isset($this->meta['source'])) { $this->source = $this->meta['source']; } else { switch ($this->provider) { case "flickr": $this->source = "https://flic.kr/p/" . base58_encode($this->photo_id); } } $this->getJSON(); } }
/** * Constructor * @since Version 3.9 */ public function __construct($id = false, $provider = false) { parent::__construct(); if ($id != false && $provider == "artc") { $query = "SELECT id FROM timetable_trains WHERE train_number LIKE ? AND provider = ?"; $id = $this->db->fetchOne($query, array("%" . $id . "%", $provider)); } if (filter_var($id, FILTER_VALIDATE_INT)) { $this->mckey = sprintf("railpage:timetable.train=%d", $id); if (!($row = getMemcacheObject($this->mckey))) { $query = "SELECT * FROM timetable_trains WHERE id = ?"; $row = $this->db->fetchRow($query, $id); setMemcacheObject($this->mckey, $row); } } if (isset($row)) { $this->id = $row['id']; $this->number = $row['train_number']; $this->meta = json_decode($row['meta'], true); $this->provider = $row['provider']; $this->commodity = $row['commodity']; if ($row['operator_id'] > 0) { $this->Organisation = new Organisation($row['operator_id']); } $this->url = new Url(sprintf("%s/t/%s/%s", $this->Module->url, $this->provider, strtolower($this->number))); $this->url->edit = sprintf("%s?mode=edit", $this->url->url); } }
/** * * Get links within a category * */ public function getLinks($category_id = false, $sort = "title", $direction = "ASC") { if (!$this->db || !$category_id) { return false; } $mckey = "railpage:links.category_id=" . $category_id . ".sort=" . $sort . ".direction=" . $direction; if ($return = getMemcacheObject($mckey)) { return $return; } elseif ($this->db instanceof \sql_db) { $return = false; $query = "SELECT * FROM nuke_links_links WHERE link_approved = 1 AND cid = " . $this->db->real_escape_string($category_id) . " ORDER BY " . $this->db->real_escape_string($sort) . " " . strtoupper(strtolower($this->db->real_escape_string($direction))); if ($rs = $this->db->query($query)) { $return = array(); while ($row = $rs->fetch_assoc()) { if (stripos($row['url'], "http") === false) { $row['url'] = "http://" . $row['url']; } $return[] = $row; } setMemcacheObject($mckey, $return, strtotime("+24 hours")); } else { trigger_error("Links: Could not fetch links for category " . $category_id); trigger_error($this->db->error); trigger_error($query); } return $return; } else { $query = "SELECT * FROM nuke_links_links WHERE link_approved = 1 AND cid = ? ORDER BY " . $sort . " " . $direction; $params = array($category_id); $return = array(); foreach ($this->db->fetchAll($query, $params) as $row) { if (stripos($row['url'], "http") === false) { $row['url'] = "http://" . $row['url']; } $return[] = $row; } setMemcacheObject($mckey, $return, strtotime("+24 hours")); return $return; } }
/** * 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; if ($string = getMemcacheObject($mckey)) { return $string; } else { 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; } #$string = "/locations/" . strtolower(str_replace(" ", "-", $data['country'])) . "/" . strtolower(str_replace(" ", "-", $data['region'])) . "/" . $data['slug']; $string = strtolower(sprintf("%s/%s/%s/%s", $this->Module->url, str_replace(" ", "-", $data['country']), str_replace(" ", "-", $data['region']), $data['slug'])); setMemcacheObject($mckey, $string, strtotime("+1 year")); } return $string; }
/** * Constructor * @since Version 3.8.7 * @param int $id The ID of the event * @uses \Railpage\Events\EventCategory The category which this event is filed under * @uses \Railpage\Organisations\Organisation The organisation associated with this event * @uses \Railpage\Place The geographic place (latitude & longitude) of this event */ public function __construct($id = false) { parent::__construct(); /** * Record this in the debug log */ if (function_exists("debug_recordInstance")) { debug_recordInstance(__CLASS__); } if (RP_DEBUG) { global $site_debug; $debug_timer_start = microtime(true); } $this->Module = new Module("events"); $this->namespace = $this->Module->namespace; if (filter_var($id, FILTER_VALIDATE_INT)) { $query = "SELECT * FROM event WHERE id = ?"; $this->mckey = sprintf("railpage:events.event=%d", $id); } elseif (is_string($id) && strlen($id) > 2) { $query = "SELECT * FROM event WHERE slug = ?"; $this->mckey = getMemcacheObject(sprintf("railpage:events.event=%s", $id)); } if (empty($this->mckey) || $this->mckey === false || !($row = getMemcacheObject($this->mckey))) { if (isset($query)) { $row = $this->db->fetchRow($query, $id); $this->mckey = sprintf("railpage:events.event=%d", $row['id']); setMemcacheObject($this->mckey, $row); setMemcacheObject(sprintf("railpage:events.event=%s", $row['slug']), $row['id']); } } if (isset($row) && is_array($row)) { $this->id = $row['id']; $this->title = $row['title']; $this->desc = $row['description']; $this->meta = json_decode($row['meta'], true); $this->slug = $row['slug']; $this->flickr_tag = "railpage:event=" . $this->id; if (filter_var($row['category_id'], FILTER_VALIDATE_INT)) { $this->Category = new EventCategory($row['category_id']); } if (filter_var($row['organisation_id'], FILTER_VALIDATE_INT)) { $this->Organisation = new Organisation($row['organisation_id']); } if (round($row['lat'], 3) != "0.000" && round($row['lon'], 3) != "0.000") { $this->Place = new Place($row['lat'], $row['lon']); } $this->createUrls(); $this->Templates = new stdClass(); $this->Templates->view = sprintf("%s/event.tpl", $this->Module->Paths->html); } if (RP_DEBUG) { $site_debug[] = __CLASS__ . "::" . __METHOD__ . " completed in " . round(microtime(true) - $debug_timer_start, 5) . "s"; } }
/** * Get WOE (Where On Earth) data from Yahoo's GeoPlanet API * * Ported from [master]/includes/functions.php * @since Version 3.8.7 * @param string $lookup * @return array */ public static function getWOEData($lookup = false) { if ($lookup === false) { return false; } $return = array(); $mckey = "railpage:woe=" . $lookup; #deleteMemcacheObject($mckey); if (!($return = getMemcacheObject($mckey))) { global $RailpageConfig; if (preg_match("@[a-zA-Z]+@", $lookup) || strpos($lookup, ",")) { $lookup = sprintf("places.q('%s')", $lookup); } else { $lookup = sprintf("place/%s", $lookup); } $url = sprintf("http://where.yahooapis.com/v1/%s?lang=en&appid=%s&format=json", $lookup, $RailpageConfig->Yahoo->ApplicationID); /** * Load PEAR HTTP/Request2 */ #require_once("HTTP/Request2.php"); /** * Fetch from Yahoo's GeoPlanet API */ $request = new HTTP_Request2($url, HTTP_Request2::METHOD_GET); $request->setConfig(array("ssl_verify_peer" => false)); $response = $request->send(); switch ($response->getStatus()) { case 200: $return = json_decode($response->getBody(), true); break; case 503: throw new Exception("Your call to Yahoo Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you."); break; case 403: throw new Exception("Your call to Yahoo Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit."); break; case 400: throw new Exception("Your call to Yahoo Web Services failed and returned an HTTP status of 400. That means: Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML/JSON response."); break; default: throw new Exception("Your call to Yahoo Web Services returned an unexpected HTTP status of: " . $response->getStatus()); } $return['url'] = $url; /* // Zend\Http\Client code messes with the URL and tries to encode the brackets. This breaks Yahoo's API, because they're too shit to urldecode() anything $client = new Zend\Http\Client; $client->setAdapter("Zend\Http\Client\Adapter\Curl"); $client->setUri($url); $client->setEncType(Zend\Http\Client::ENC_URLENCODED); $result = $client->send(); #if ($result->isSuccess()) { $return = json_decode($result->getBody(), true); $return['url'] = $url; #} */ } if ($return !== false) { setMemcacheObject($mckey, $return, strtotime("+24 hours")); } return $return; }
/** * Get JSON object for a news article * @since Version 3.8.7 * @param int $article_id * @return string */ public static function getArticleJSON($article_id) { $key = sprintf("json:railpage.news.article=%d", $article_id); #deleteMemcacheObject($key); if (!($json = getMemcacheObject($key))) { $Article = new Article($article_id); $json = $Article->makeJSON(); } return $json; }
/** * 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) { if (!$this->validate($force)) { // Get out early return false; } if (!empty($this->mckey) && getMemcacheObject($this->mckey)) { removeMemcacheObject($this->mckey); } $dataArray = array(); $dataArray['provider'] = $this->provider; $dataArray['meta'] = json_encode($this->meta); $dataArray['user_opts'] = json_encode($this->preferences); $dataArray['username'] = $this->username; $dataArray['user_active'] = $this->active; $dataArray['user_regdate'] = $this->regdate; $dataArray['user_level'] = $this->level; $dataArray['user_posts'] = $this->posts; $dataArray['user_style'] = $this->style; $dataArray['theme'] = $this->theme; $dataArray['user_lang'] = $this->lang; $dataArray['user_dateformat'] = $this->date_format; $dataArray['user_rank'] = $this->rank_id; $dataArray['user_from'] = $this->location; $dataArray['user_occ'] = $this->occupation; $dataArray['user_interests'] = $this->interests; $dataArray['name'] = $this->real_name; $dataArray['timezone'] = $this->timezone; $dataArray['user_website'] = $this->website; $dataArray['user_allow_viewonline'] = $this->hide; $dataArray['uWheat'] = $this->wheat; $dataArray['uChaff'] = $this->chaff; $dataArray['api_key'] = $this->api_key; $dataArray['api_secret'] = $this->api_secret; $dataArray['user_report_optout'] = $this->report_optout; $dataArray['user_warnlevel'] = $this->warning_level; $dataArray['disallow_mod_warn'] = $this->warning_exempt; $dataArray['user_group_cp'] = $this->group_cp; $dataArray['user_group_list_cp'] = $this->group_list_cp; $dataArray['user_active_cp'] = $this->active_cp; $dataArray['user_forum_postsperpage'] = $this->items_per_page; $dataArray['user_avatar'] = $this->avatar; $dataArray['user_avatar_gravatar'] = $this->avatar_gravatar; $dataArray['user_avatar_type'] = $this->avatar_type; $dataArray['user_avatar_width'] = $this->avatar_width; $dataArray['user_avatar_height'] = $this->avatar_height; $dataArray['user_new_privmsg'] = $this->privmsg_new; $dataArray['user_unread_privmsg'] = $this->privmsg_unread; $dataArray['user_last_privmsg'] = $this->privmsg_last_id; $dataArray['user_viewemail'] = $this->email_show; $dataArray['storynum'] = $this->news_submissions; $dataArray['user_notify'] = $this->notify; $dataArray['user_notify_pm'] = $this->notify_privmsg; $dataArray['user_email'] = $this->contact_email; $dataArray['femail'] = $this->contact_email_public; $dataArray['user_icq'] = $this->contact_icq; $dataArray['user_aim'] = $this->contact_aim; $dataArray['user_yim'] = $this->contact_yim; $dataArray['user_msnm'] = $this->contact_msn; $dataArray['user_sig'] = $this->signature; $dataArray['user_attachsig'] = $this->signature_attach; $dataArray['user_showsigs'] = $this->signature_showall; $dataArray['user_sig_bbcode_uid'] = $this->signature_bbcode_uid; $dataArray['user_password'] = $this->password; $dataArray['user_password_bcrypt'] = $this->password_bcrypt; $dataArray['user_lastvisit'] = $this->lastvisit; $dataArray['user_session_time'] = $this->session_time; $dataArray['user_session_page'] = $this->session_page; $dataArray['user_current_visit'] = $this->session_current; $dataArray['user_last_visit'] = $this->session_last; $dataArray['last_session_ip'] = $this->session_ip; $dataArray['last_session_cslh'] = $this->session_cslh; $dataArray['last_session_ignore'] = $this->session_mu_ignore; $dataArray['user_enablerte'] = $this->enable_rte; $dataArray['user_enableglossary'] = $this->enable_glossary; $dataArray['user_allowhtml'] = $this->enable_html; $dataArray['user_allowbbcode'] = $this->enable_bbcode; $dataArray['user_allowsmile'] = $this->enable_emoticons; $dataArray['user_allowavatar'] = $this->enable_avatar; $dataArray['user_allow_pm'] = $this->enable_privmsg; $dataArray['user_popup_pm'] = $this->enable_privmsg_popup; $dataArray['flickr_oauth_token'] = $this->flickr_oauth_token; $dataArray['flickr_oauth_token_secret'] = $this->flickr_oauth_secret; $dataArray['flickr_nsid'] = $this->flickr_nsid; $dataArray['flickr_username'] = $this->flickr_username; $dataArray['user_actkey'] = $this->act_key; $dataArray['oauth_consumer_id'] = $this->oauth_id; $dataArray['sidebar_type'] = $this->sidebar_type; $dataArray['user_enableautologin'] = $this->enable_autologin; $dataArray['user_enablessl'] = $this->ssl; $dataArray['facebook_user_id'] = $this->facebook_user_id; $dataArray['reported_to_sfs'] = $this->reported_to_sfs; if ($this->db instanceof \sql_db) { // Escape values for SQL foreach ($dataArray as $key => $val) { $dataArray[$key] = $this->db->real_escape_string($val); } if ($this->id) { // Update existing user $where = array(); $where['user_id'] = $this->db->real_escape_string($this->id); $query = $this->db->buildQuery($dataArray, "nuke_users", $where); } else { // Create a new user $query = $this->db->buildQuery($dataArray, "nuke_users"); } try { if ($this->db->query($query)) { if (!$this->id) { $this->id = $this->db->insert_id; } $return = true; } else { throw new \Exception($this->db->error); } } catch (Exception $e) { global $Error; $Error->save($e); $return = false; throw new \Exception($e->getMessage()); } return $return; } else { if ($this->id) { $this->db->update("nuke_users", $dataArray, array("user_id = ?" => $this->id)); } else { $this->db->insert("nuke_users", $dataArray); $this->id = $this->db->lastInsertId(); $this->createUrls(); } return true; } }
/** * 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) { /** * I hate using Globals... */ global $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 = getMemcacheObject($mckey))) { $query = "SELECT forum_id FROM nuke_bbforums"; $forums = $this->db->fetchAll($query); setMemcacheObject($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; } /** * Add the forum permissions to Zend_ACL */ foreach ($db_acl as $forum_id => $permissions) { $allowed = array(); $denied = array(); foreach ($permissions as $item => $value) { switch ($value) { case self::AUTH_ALL: $allowed[] = $item; break; case self::AUTH_REG: if (!$this->User->guest) { $allowed[] = $item; } break; case self::AUTH_ACL: 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: if ($this->User->inGroup(RP_GROUP_MODERATORS)) { $allowed[] = $item; } break; case self::AUTH_ADMIN: 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); } $this->ZendACL = $acl; }
/** * Get photo info and sizes * @since Version 3.5 * @param int $photo_id * @return array */ public function getPhotoLegacy($photo_id = false) { if (!$photo_id) { throw new Exception("Cannot fetch photo info and sizes - no photo ID given"); return false; } $mckey = "railpage:railcam.photo.id=" . $photo_id; #deleteMemcacheObject($mckey); if ($return = getMemcacheObject($mckey)) { $return['photo']['time_relative'] = relative_date($return['photo']['dateuploaded']); return $return; } else { $use_rezzza = false; if ($use_rezzza) { $metadata = new Metadata(RP_FLICKR_API_KEY, RP_FLICKR_API_SECRET); $metadata->setOauthAccess($this->flickr_oauth_token, $this->flickr_oauth_secret); $factory = new ApiFactory($metadata, new GuzzleAdapter()); $photo_info = $factory->call('flickr.photos.getInfo', array('photo_id' => $photo_id)); if ($photo_info) { $photo_sizes = $factory->call('flickr.photos.getSizes', array('photo_id' => $photo_id)); } /** * do stuff! */ if ($photo_info && $photo_sizes) { $return = array(); /** * Photo info */ foreach ($photo_info->photo->attributes() as $a => $b) { $return['photo'][$a] = $b->__toString(); } foreach ($photo_info->photo->children() as $element) { foreach ($element->attributes() as $a => $b) { $return['photo'][$element->getName()][$a] = $b->__toString(); } foreach ($element->children() as $child) { foreach ($child->attributes() as $a => $b) { $return['photo'][$element->getName()][$child->getName()][$a] = $b->__toString(); } foreach ($child->children() as $blah) { $return['photo'][$element->getName()][$child->getName()][$blah->getName()][$a] = $b->__toString(); foreach ($blah->attributes() as $a => $b) { $return['photo'][$element->getName()][$child->getName()][$blah->getName()][$a] = $b->__toString(); } } } } /** * Photo sizes */ $i = 0; foreach ($photo_sizes->sizes->size as $key => $element) { foreach ($element->attributes() as $a => $b) { $return['photo']['sizes'][$i][$a] = $b->__toString(); } $i++; } } return $return; } $f = new flickr_railpage(RP_FLICKR_API_KEY); $f->oauth_token = $this->flickr_oauth_token; $f->oauth_secret = $this->flickr_oauth_secret; $f->cache = false; $return = array(); if ($return = $f->photos_getInfo($photo_id)) { $return['photo']['sizes'] = $f->photos_getSizes($photo_id); $return['photo']['time_relative'] = relative_date($return['photo']['dateuploaded']); setMemcacheObject($mckey, $return, strtotime("+2 hours")); } return $return; } }
/** * Fetch the message * @since Version 3.3 * @version 3.3 * @return boolean */ public function fetch() { if (empty($this->id)) { throw new Exception("Cannot fetch PM - no message ID provided"); return false; } $this->mckey = "railpage:messsages.message_id=" . $this->id; if (!($row = getMemcacheObject($this->mckey))) { if ($this->db instanceof \sql_db) { $query = "SELECT pm.*, pmt.*, ufrom.user_id AS user_id_from, ufrom.username AS username_from, ufrom.user_avatar AS from_user_avatar, ufrom.user_allow_viewonline AS from_user_viewonline, uto.user_id AS user_id_to, uto.username AS username_to, uto.user_allow_viewonline AS to_user_viewonline\r\n\t\t\t\t\t\t\t\tFROM nuke_bbprivmsgs AS pm\r\n\t\t\t\t\t\t\t\tLEFT JOIN nuke_bbprivmsgs_text AS pmt ON pm.privmsgs_id = pmt.privmsgs_text_id\r\n\t\t\t\t\t\t\t\tLEFT JOIN nuke_users AS ufrom ON pm.privmsgs_from_userid = ufrom.user_id\r\n\t\t\t\t\t\t\t\tLEFT JOIN nuke_users AS uto ON pm.privmsgs_to_userid = uto.user_id\r\n\t\t\t\t\t\t\t\tWHERE pm.privmsgs_id = " . $this->db->real_escape_string($this->id); if ($rs = $this->db->query($query)) { if ($rs->num_rows != 1) { throw new Exception("Cannot fetch PM - no PM found!"); return false; } $row = $rs->fetch_assoc(); $this->setCache($this->mckey, $row); } } else { $query = "SELECT pm.*, pmt.*, ufrom.user_id AS user_id_from, ufrom.username AS username_from, ufrom.user_avatar AS from_user_avatar, ufrom.user_allow_viewonline AS from_user_viewonline, uto.user_id AS user_id_to, uto.username AS username_to, uto.user_allow_viewonline AS to_user_viewonline\r\n\t\t\t\t\t\t\t\tFROM nuke_bbprivmsgs AS pm\r\n\t\t\t\t\t\t\t\tLEFT JOIN nuke_bbprivmsgs_text AS pmt ON pm.privmsgs_id = pmt.privmsgs_text_id\r\n\t\t\t\t\t\t\t\tLEFT JOIN nuke_users AS ufrom ON pm.privmsgs_from_userid = ufrom.user_id\r\n\t\t\t\t\t\t\t\tLEFT JOIN nuke_users AS uto ON pm.privmsgs_to_userid = uto.user_id\r\n\t\t\t\t\t\t\t\tWHERE pm.privmsgs_id = ?"; $row = $this->db->fetchRow($query, $this->id); $this->setCache($this->mckey, $row); } } if (isset($row) && count($row)) { // Nasty way of doing it, but it should work for now. // Remember to extend BOTH open AND close when adding another bbcode to match $bbcode_preg_open = "@\\[(img|url|list|b|i|u)\\:([a-zA-Z0-9]+)\\]@"; $bbcode_preg_close = "@\\[/(img|url|list|b|i|u)\\:([a-zA-Z0-9]+)\\]@"; $this->id = $row['privmsgs_id']; $this->date = $row['privmsgs_date']; $this->subject = $row['privmsgs_subject']; $this->body = trim($row['privmsgs_text']); $this->bbcode_uid = $row['privmsgs_bbcode_uid']; $this->type = $row['privmsgs_type']; $this->enable_bbcode = $row['privmsgs_enable_bbcode']; $this->enable_html = $row['privmsgs_enable_html']; $this->enable_smilies = $row['privmsgs_enable_smilies']; $this->enable_signature = $row['privmsgs_attach_sig']; $this->object_id = $row['object_id']; $this->hide_from = $row['hide_from']; $this->hide_to = $row['hide_to']; $this->setRecipient(new User($row['privmsgs_to_userid'])); $this->setAuthor(new User($row['privmsgs_from_userid'])); #$this->Author = new User($row['privmsgs_from_userid']); #$this->from_user_id = $row['privmsgs_from_userid']; #$this->from_username = $row['username_from']; #$this->from_user_avatar = $row['from_user_avatar']; #$this->from_user_viewonline = $row['from_user_viewonline']; #$this->Recipient = new User($row['privmsgs_to_userid']); #$this->to_user_id = $row['privmsgs_to_userid']; #$this->to_username = $row['username_to']; #$this->to_user_viewonline = $row['to_user_viewonline']; } }
/** * Check if a user is a member of this group * @since Version 3.7.5 * @paran int $user_id * @return boolean */ public function userInGroup($user_id = false) { if (!$user_id) { return false; } $mckey = "railpage:group=" . $this->id . ".user_id=" . $user_id; if (RP_DEBUG) { global $site_debug; $debug_timer_start = microtime(true); } if (getMemcacheObject($mckey) == "yes") { return true; } else { $query = "SELECT user_id FROM nuke_bbuser_group WHERE group_id = '" . $this->db->real_escape_string($this->id) . "' AND user_id = '" . $this->db->real_escape_string($user_id) . "'"; if ($rs = $this->db->query($query)) { if ($rs->num_rows == 1) { if (RP_DEBUG) { $site_debug[] = "Railpage: Find user ID " . $user_id . " in group ID " . $this->id . " completed in " . number_format(microtime(true) - $debug_timer_start, 8) . "s"; } setMemcacheObject($mckey, "yes", strtotime("+1 day")); return true; } } if (RP_DEBUG) { $site_debug[] = "Railpage: Find user ID " . $user_id . " in group ID " . $this->id . " completed in " . number_format(microtime(true) - $debug_timer_start, 8) . "s"; } setMemcacheObject($mckey, "no", strtotime("+1 day")); return false; } }
/** * Fetch an object from Memcache * @deprecated 3.8.7 Calls to this method should be replaced with getMemcacheObject() with the same parameter(s) * @since Version 3.7.5 * @param string|boolean $key * @return mixed */ protected function getCache($key = false) { if (!$this->useCache) { return false; } return getMemcacheObject($key); }
/** * Get photo info and sizes * @since Version 3.5 * @param int $photo_id * @return array */ public function getPhoto($photo_id = false) { if (!$photo_id) { throw new Exception("Cannot fetch photo info and sizes - no photo ID given"); return false; } $mckey = "railpage:railcam.photo.id=" . $photo_id; deleteMemcacheObject($mckey); if ($return = getMemcacheObject($mckey)) { $return['photo']['time_relative'] = relative_date($return['photo']['dateuploaded']); return $return; } else { $f = new \flickr_railpage(RP_FLICKR_API_KEY); $f->oauth_token = $this->flickr_oauth_token; $f->oauth_secret = $this->flickr_oauth_secret; $f->cache = false; $return = array(); if ($return = $f->photos_getInfo($photo_id)) { $return['photo']['sizes'] = $f->photos_getSizes($photo_id); $return['photo']['time_relative'] = relative_date($return['photo']['dateuploaded']); setMemcacheObject($mckey, $return, strtotime("+2 hours")); } return $return; } }
/** * Constructor * @since Version 3.8.7 * @param int|string $id */ public function __construct($id = false) { parent::__construct(); $this->Module = new Module("Gallery"); if (filter_var($id, FILTER_VALIDATE_INT)) { $this->id = $id; $this->mckey = sprintf("railpage:gallery.album.image=%d", $this->id); if (!($row = getMemcacheObject($this->mckey))) { $query = "SELECT * FROM gallery_mig_image WHERE id = ?"; $row = $this->db->fetchRow($query, $id); setMemcacheObject($this->mckey, $row, strtotime("+1 year")); } $this->title = $row['title']; $this->DateTaken = new DateTime($row['date_taken']); $this->DateUploaded = new DateTime($row['date_uploaded']); $this->path = $row['path']; $this->url = new Url(sprintf("%s?image=%d", $this->Module->url, $this->id)); $this->meta = json_decode($row['meta'], true); $this->hidden = isset($row['hidden']) ? $row['hidden'] : false; $this->sizes = array("original" => array("source" => sprintf("//static.railpage.com.au/albums/%s", $this->path), "width" => $this->meta['image']['raw_width'], "height" => $this->meta['image']['raw_height'])); if (isset($this->meta['image']['resizedName'])) { $this->sizes['medium'] = array("source" => sprintf("//static.railpage.com.au/albums/%s/%s.%s", dirname($this->path), $this->meta['image']['resizedName'], $this->meta['image']['type']), "width" => $this->meta['image']['width'], "height" => $this->meta['image']['height']); } if (isset($this->meta['thumbnail'])) { $this->sizes['thumb'] = array("source" => sprintf("//static.railpage.com.au/albums/%s/%s.%s", dirname($this->path), $this->meta['thumbnail']['name'], $this->meta['thumbnail']['type']), "width" => $this->meta['thumbnail']['width'], "height" => $this->meta['thumbnail']['height']); } $this->owner = $row['owner']; if (filter_var($row['owner'], FILTER_VALIDATE_INT)) { try { $this->Owner = new User($row['owner']); } catch (Exception $e) { // Don't care } } } }
/** * Get all child albums as an array * @since Version 3.8.7 * @return array */ public function getAlbums($page, $limit = 25) { if (!($return = getMemcacheObject(sprintf("railpage:gallery.old.album=%d.subalbums.page=%d.perpage=%d", $this->id, $page, $limit)))) { $Sphinx = $this->getSphinx(); $query = $Sphinx->select("*")->from("idx_gallery_album")->where("parent_id", "=", $this->id)->orderBy("album_title", "ASC")->limit(($page - 1) * $limit, $limit); $matches = $query->execute(); $meta = $Sphinx->query("SHOW META"); $meta = $meta->execute(); foreach ($matches as $id => $row) { $row['album_meta'] = json_decode($row['album_meta'], true); $matches[$id] = $row; } $return = array("total" => $meta[1]['Value'], "page" => $page, "perpage" => $limit, "albums" => $matches); setMemcacheObject(sprintf("railpage:gallery.old.album=%d.subalbums.page=%d.perpage=%d", $this->id, $page, $limit), $return); } return $return; }