Example #1
0
 /**
  * Get a associative array of this photo data in a standardised format
  * @since Version 3.9
  * @return array
  */
 public function getArray()
 {
     return array("id" => $this->id, "title" => $this->title, "description" => $this->description, "provider" => $this->getProviderName(), "url" => $this->url->getURLs(), "sizes" => Images::NormaliseSizes($this->sizes), "dates" => $this->dates);
 }
Example #2
0
 /**
  * Load the locomotive object
  * @since Version 3.2
  * @version 3.2
  * @return boolean
  */
 public function fetch()
 {
     if (!$this->id) {
         throw new Exception("Cannot load loco object - loco ID not provided");
         return false;
     }
     $this->mckey = "railpage:locos.loco_id=" . $this->id;
     #deleteMemcacheObject($this->mckey);
     if ($row = $this->getCache($this->mckey)) {
         // Do nothing
     } elseif ($this->db instanceof \sql_db) {
         $query = "SELECT l.*, s.name AS loco_status, ow.operator_name AS owner_name, op.operator_name AS operator_name\r\n\t\t\t\t\t\t\tFROM loco_unit AS l\r\n\t\t\t\t\t\t\tLEFT JOIN loco_status AS s ON l.loco_status_id = s.id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS ow ON ow.operator_id = l.owner_id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS op ON op.operator_id = l.operator_id\r\n\t\t\t\t\t\t\tWHERE l.loco_id = " . $this->id;
         if ($rs = $this->db->query($query)) {
             $row = $rs->fetch_assoc();
             $this->setCache($this->mckey, $row, strtotime("+24 hours"));
         }
     } else {
         if (RP_DEBUG) {
             global $site_debug;
             $debug_timer_start = microtime(true);
         }
         $query = "SELECT l.*, s.name AS loco_status, ow.operator_name AS owner_name, op.operator_name AS operator_name\r\n\t\t\t\t\t\t\tFROM loco_unit AS l\r\n\t\t\t\t\t\t\tLEFT JOIN loco_status AS s ON l.loco_status_id = s.id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS ow ON ow.operator_id = l.owner_id\r\n\t\t\t\t\t\t\tLEFT JOIN operators AS op ON op.operator_id = l.operator_id\r\n\t\t\t\t\t\t\tWHERE l.loco_id = ?";
         $row = $this->db->fetchRow($query, $this->id);
         if (RP_DEBUG) {
             if ($row === false) {
                 $site_debug[] = "Zend_DB: FAILED select loco ID " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s";
             } else {
                 $site_debug[] = "Zend_DB: SUCCESS select loco ID " . $this->id . " in " . round(microtime(true) - $debug_timer_start, 5) . "s";
             }
         }
         $this->setCache($this->mckey, $row, strtotime("+24 hours"));
     }
     if (isset($row) && is_array($row)) {
         $this->number = stripslashes($row['loco_num']);
         $this->name = stripslashes($row['loco_name']);
         $this->gauge = stripslashes($row['loco_gauge']);
         $this->gauge_id = $row['loco_gauge_id'];
         $this->status_id = $row['loco_status_id'];
         $this->status = $row['loco_status'];
         $this->class_id = $row['class_id'];
         $this->owner_id = $row['owner_id'];
         $this->owner = $row['owner_name'];
         $this->operator_id = $row['operator_id'];
         $this->operator = $row['operator_name'];
         $this->entered_service = $row['entered_service'];
         $this->withdrawal_date = $row['withdrawn'];
         $this->date_added = $row['date_added'];
         $this->date_modified = $row['date_modified'];
         $this->builders_num = $row['builders_number'];
         $this->photo_id = intval($row['photo_id']);
         $this->manufacturer_id = $row['manufacturer_id'];
         $this->Class = new LocoClass($this->class_id);
         $this->class =& $this->Class;
         $this->flickr_tag = trim(str_replace(" ", "", $this->Class->flickr_tag . "-" . $this->number));
         $this->gauge_formatted = format_gauge($this->gauge);
         $this->url = new Url(strtolower($this->makeLocoURL($this->Class->slug, $this->number)));
         $this->url->edit = sprintf("%s?mode=loco.edit&id=%d", $this->Module->url, $this->id);
         $this->url->sightings = sprintf("%s/sightings", $this->url->url);
         /**
          * Set the meta data
          */
         if (isset($row['meta'])) {
             $this->meta = json_decode($row['meta'], true);
         } else {
             $this->meta = array();
         }
         // Fetch the gauge data
         if ($this->gauge = getMemcacheObject(sprintf("railpage:locos.gauge_id=%d", $row['loco_gauge_id']))) {
             // Do nothing
         } elseif ($this->db instanceof \sql_db) {
             $query = "SELECT * FROM loco_gauge WHERE gauge_id = '" . $this->db->real_escape_string($row['loco_gauge_id']) . "'";
             if ($rs = $this->db->query($query)) {
                 $this->gauge = $rs->fetch_assoc();
                 $this->setCache("rp-locos-gauge-" . $row['loco_gauge_id'], $this->gauge);
             }
         } else {
             $query = "SELECT * FROM loco_gauge WHERE gauge_id = ?";
             $this->gauge = $this->db->fetchRow($query, $row['loco_gauge_id']);
             $this->setCache("railpage:locos.gauge_id=" . $row['loco_gauge_id'], $this->gauge, strtotime("+2 months"));
         }
         /**
          * If an asset ID exists and is greater than 0, create the asset object
          */
         if (isset($row['asset_id']) && $row['asset_id'] > 0) {
             try {
                 $this->Asset = new \Railpage\Assets\Asset($row['asset_id']);
             } catch (Exception $e) {
                 global $Error;
                 $Error->save($e);
             }
         }
         /**
          * Try to load the Image object
          */
         if (filter_var($row['photo_id'], FILTER_VALIDATE_INT)) {
             $Images = new \Railpage\Images\Images();
             $this->Image = $Images->findImage("flickr", $row['photo_id']);
             $this->Image->addLink($this->namespace, $this->id);
         }
         /**
          * Get all owners of this locomotive
          */
         try {
             $this->owners = $this->getOrganisations(1);
             if (!empty($this->owner_id) && empty($this->owners)) {
                 $this->addOrganisation($this->owner_id, 1);
                 // Re-fetch the owners
                 $this->owners = $this->getOrganisations(1);
             }
             reset($this->owners);
             if (isset($this->owners[0]['organisation_id']) && isset($this->owners[0]['organisation_name'])) {
                 $this->owner_id = $this->owners[0]['organisation_id'];
                 $this->owner = $this->owners[0]['organisation_name'];
             } else {
                 $this->owner_id = 0;
                 $this->owner = "Unknown";
             }
         } catch (Exception $e) {
             global $Error;
             $Error->save($e);
         }
         /**
          * Get all operators of this locomotive
          */
         try {
             $this->operators = $this->getOrganisations(2);
             if (!empty($this->operator_id) && empty($this->operators)) {
                 $this->addOrganisation($this->operator_id, 2);
                 // Re-fetch the operators
                 $this->operators = $this->getOrganisations(2);
             }
             reset($this->operators);
             if (isset($this->operators[0]['organisation_id']) && isset($this->operators[0]['organisation_name'])) {
                 $this->operator_id = $this->operators[0]['organisation_id'];
                 $this->operator = $this->operators[0]['organisation_name'];
             } else {
                 $this->operator_id = 0;
                 $this->operator = "Unknown";
             }
         } catch (Exception $e) {
             global $Error;
             $Error->save($e);
         }
         /**
          * Get the average rating of the data of this loco
          */
         try {
             $this->rating = $this->getRating();
         } catch (Exception $e) {
             // Do nothing for now
         }
         /**
          * Get the manufacturer
          */
         if (empty($this->manufacturer_id)) {
             $this->manufacturer_id = $this->Class->manufacturer_id;
             $this->manufacturer = $this->Class->manufacturer;
         } else {
             try {
                 $builders = $this->listManufacturers();
                 if (count($builders['manufacturers'])) {
                     $this->manufacturer = $builders['manufacturers'][$this->manufacturer_id]['manufacturer_name'];
                 }
             } catch (Exception $e) {
                 // I hate globals, but I don't want to throw an exception here...
                 global $Error;
                 $Error->save($e);
             }
         }
         /**
          * Update the latest owner/operator stored in this row
          */
         $owners = $this->getOrganisations(1, 1);
         $operators = $this->getOrganisations(2, 1);
         if (count($owners) && intval(trim($this->owner_id)) != intval(trim($owners[0]['operator_id']))) {
             if (RP_DEBUG) {
                 global $site_debug;
                 $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : committing changes to owner for loco ID " . $this->id;
                 $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : Current owner_id: " . $this->owner_id . ", Proposed owner_id: " . $owners[0]['operator_id'];
             }
             $this->owner = $owners[0]['organisation_name'];
             $this->owner_id = $owners[0]['operator_id'];
             $this->commit();
         }
         if (count($operators) && intval(trim($this->operator_id)) != intval(trim($operators[0]['operator_id']))) {
             if (RP_DEBUG) {
                 global $site_debug;
                 $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : committing changes to operator for loco ID " . $this->id;
                 $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : Current operator_id: " . $this->operator_id . ", Proposed operator_id: " . $owners[0]['operator_id'];
             }
             $this->operator = $operators[0]['organisation_name'];
             $this->operator_id = $operators[0]['operator_id'];
             $this->commit();
         }
         /**
          * Build the shortcut URL for this object
          */
         try {
             $this->fwlink = new \Railpage\fwlink($this->url);
             if (empty($this->fwlink->url)) {
                 $this->fwlink->url = $this->url;
                 $this->fwlink->title = $this->number . " - " . $this->Class->name;
                 $this->fwlink->commit();
             }
         } catch (Exception $e) {
             global $Error;
             $Error->save($e);
         }
         /**
          * Populate the list of liveries
          */
         foreach ($this->db->fetchAll("SELECT lu.livery_id FROM loco_unit_livery AS lu LEFT JOIN loco_livery AS li ON lu.livery_id = li.livery_id WHERE lu.loco_id = ? ORDER BY li.livery", $this->id) as $row) {
             $Livery = new Livery($row['livery_id']);
             $livery = array("id" => $Livery->id, "name" => $Livery->name, "tag" => $Livery->tag, "country" => array("code" => $Livery->country), "region" => array("code" => $Livery->region));
             if ($Livery->Image instanceof \Railpage\Images\Image) {
                 $livery['image'] = array("id" => $Livery->Image->id, "title" => $Livery->Image->title, "description" => $Livery->Image->description, "provider" => $Livery->Image->provider, "photo_id" => $Livery->Image->photo_id, "sizes" => $Livery->Image->sizes);
             }
             $this->liveries[] = $livery;
         }
         /**
          * Set the StatsD namespaces
          */
         $this->StatsD->target->view = sprintf("%s.%d.view", $this->namespace, $this->id);
         $this->StatsD->target->edit = sprintf("%s.%d.view", $this->namespace, $this->id);
     } else {
         throw new Exception("No data found for Loco ID " . $this->id);
         return false;
     }
 }
Example #3
0
 /**
  * Get thumbnails of images from an array of image IDs
  * @since Version 3.9.1
  * @param array $ids
  * @return array
  */
 public static function GetThumbnails($ids)
 {
     $Memcached = AppCore::getMemcached();
     $Database = (new AppCore())->getDatabaseConnection();
     if (is_array($ids)) {
         $ids = implode(",", $ids);
     }
     $cachekey = md5($ids) . ".v2";
     if (!($return = $Memcached->fetch($cachekey))) {
         $query = "SELECT id, meta, title FROM image WHERE id IN (" . $ids . ")";
         $return = array();
         #echo $query;
         foreach ($Database->fetchAll($query) as $row) {
             $meta = json_decode($row['meta'], true);
             $meta['sizes'] = Images::normaliseSizes($meta['sizes']);
             $return[] = array("id" => $row['id'], "url" => sprintf("/photos/%d", $row['id']), "thumbnail" => $meta['sizes']['thumb']['source'], "html" => sprintf("<a href='%s' class='thumbnail' style='background-image:url(%s);'></a>", sprintf("/photos/%d", $row['id']), $meta['sizes']['thumb']['source']));
         }
         $Memcached->save($cachekey, $return);
     }
     return $return;
 }
Example #4
0
 /**
  * Generate the JSON data string
  *
  * @return $this
  */
 public function getJSON()
 {
     if (isset($this->author)) {
         $author = clone $this->author;
         if (isset($author->User) && $author->User instanceof User) {
             $author->User = $author->User->getArray();
         }
     }
     $data = array("id" => $this->id, "title" => $this->title, "description" => $this->description, "score" => $this->getScore(), "provider" => array("name" => $this->provider, "photo_id" => $this->photo_id), "sizes" => Images::NormaliseSizes($this->sizes), "srcset" => implode(", ", Utility\ImageUtility::generateSrcSet($this)), "author" => isset($author) ? $author : false, "url" => $this->url instanceof Url ? $this->url->getURLs() : array(), "dates" => array());
     $times = ["posted", "taken"];
     #printArray($this->meta['dates']);die;
     foreach ($times as $time) {
         if (isset($this->meta['dates'][$time])) {
             $Date = filter_var($this->meta['dates'][$time], FILTER_VALIDATE_INT) ? new DateTime("@" . $this->meta['dates'][$time]) : new DateTime($this->meta['dates'][$time]);
             $data['dates'][$time] = array("absolute" => $Date->format("Y-m-d H:i:s"), "nice" => $Date->Format("d H:i:s") == "01 00:00:00" ? $Date->Format("F Y") : $Date->format("F j, Y, g:i a"), "relative" => ContentUtility::RelativeTime($Date));
         }
     }
     if ($this->Place instanceof Place) {
         $data['place'] = array("url" => $this->Place->url, "lat" => $this->Place->lat, "lon" => $this->Place->lon, "name" => $this->Place->name, "country" => array("code" => $this->Place->Country->code, "name" => $this->Place->Country->name, "url" => $this->Place->Country->url));
     }
     $this->json = json_encode($data);
     return $this;
 }
Example #5
0
 /**
  * Get the cover photo for this locomotive
  * @since Version 3.8.7
  * @return array
  * @todo Set the AssetProvider (requires creating AssetProvider)
  */
 public function getCoverImage()
 {
     /**
      * Image stored in meta data
      */
     if (isset($this->meta['coverimage'])) {
         $Image = new Image($this->meta['coverimage']['id']);
         return array("type" => "image", "provider" => $Image->provider, "title" => $Image->title, "author" => array("id" => $Image->author->id, "username" => $Image->author->username, "realname" => isset($Image->author->realname) ? $Image->author->realname : $Image->author->username, "url" => $Image->author->url), "image" => array("id" => $Image->id), "sizes" => $Image->sizes, "url" => $Image->url->getURLs());
     }
     /**
      * Asset
      */
     if ($this->Asset instanceof Asset) {
         return array("type" => "asset", "provider" => "", "title" => $Asset->meta['title'], "author" => array("id" => "", "username" => "", "realname" => "", "url" => ""), "sizes" => array("large" => array("source" => $Asset->meta['image']), "original" => array("source" => $Asset->meta['original'])), "url" => array("url" => $Asset['meta']['image']));
     }
     /**
      * Ordinary Flickr image
      */
     if (filter_var($this->photo_id, FILTER_VALIDATE_INT) && $this->photo_id > 0) {
         $Images = new Images();
         $Image = $Images->findImage("flickr", $this->photo_id);
         return array("type" => "image", "provider" => $Image->provider, "title" => $Image->title, "author" => array("id" => $Image->author->id, "username" => $Image->author->username, "realname" => isset($Image->author->realname) ? $Image->author->realname : $Image->author->username, "url" => $Image->author->url), "image" => array("id" => $Image->id), "sizes" => $Image->sizes, "url" => $Image->url->getURLs());
     }
     /**
      * No cover image!
      */
     return false;
 }
Example #6
0
 /**
  * Load this livery object
  * @since Version 3.2
  * @return boolean
  */
 public function fetch()
 {
     if (!filter_var($this->id, FILTER_VALIDATE_INT)) {
         throw new Exception("Cannot fetch Livery object - no ID given");
         return false;
     }
     $query = "SELECT * FROM loco_livery WHERE livery_id = ?";
     $row = $this->db->fetchRow($query, $this->id);
     if (isset($row) && is_array($row)) {
         $this->name = $row['livery'];
         $this->introduced = $row['introduced'];
         $this->withdrawn = $row['withdrawn'];
         $this->superseded_by_id = $row['superseded_by'] > 0 ? $row['superseded_by'] : NULL;
         $this->supersedes_id = $row['supersedes'] > 0 ? $row['supersedes'] : NULL;
         $this->photo_id = $row['photo_id'];
         $this->region = $row['region'];
         $this->country = $row['country'];
         $this->tag = sprintf("railpage:livery=%d", $this->id);
         $this->url = "/flickr?tag=" . $this->tag;
         if (filter_var($this->photo_id, FILTER_VALIDATE_INT)) {
             $Images = new Images();
             $this->Image = $Images->findImage("flickr", $this->photo_id);
         }
         if ($this->recurse && $this->superseded_by_id > 0) {
             $Superseded_by = new Livery($this->superseded_by_id, false);
             $this->superseded_by = $Superseded_by->name;
         }
         if ($this->recurse && $this->supersedes_id > 0) {
             $Supersedes = new Livery($this->supersedes_id, false);
             $this->supersedes = $Supersedes->name;
         }
         return true;
     } else {
         throw new Exception("Cannot fetch Livery object - no livery for ID " . $this->id . " was found");
         return false;
     }
 }
Example #7
0
 /**
  * Find and set the hero image
  * @since Version 3.10.0
  * @return \Railpage\Newsletters\Weekly
  */
 private function getHeroImage()
 {
     $query = "SELECT f.image_id, i.meta FROM image_flags AS f LEFT JOIN image AS i ON f.image_id = i.id WHERE f.screened_pick = 1";
     $ids = [];
     foreach ($this->db->fetchAll($query) as $row) {
         $row['meta'] = json_decode($row['meta'], true);
         $sizes = Images::normaliseSizes($row['meta']['sizes']);
         if ($sizes['medium']['height'] > $sizes['medium']['width']) {
             continue;
         }
         $ids[] = $row['image_id'];
     }
     $image_id = $ids[array_rand($ids)];
     if (filter_var($image_id, FILTER_VALIDATE_INT)) {
         Debug::LogCLI("Creating instance of Image for the hero photo");
         $this->HeroImage = ImageFactory::CreateImage($image_id);
         $this->Newsletter->setHeroImage($this->HeroImage);
     }
     return $this;
 }
Example #8
0
 /**
  * Get the image from the provider
  * @since Version 3.9.1
  * @param int $id The ID of the image from the provider
  * @return array
  */
 public function getImage($id, $force = 0)
 {
     $params = array("ImageKey" => $id);
     $force = (bool) $force;
     $response = $this->send("smugmug.images.getInfo", $params);
     if ($response['stat'] == "ok") {
         // List of image sizes to look for
         $sizes = array("TinyURL", "ThumbURL", "SmallURL", "MediumURL", "LargeURL", "XLargeURL", "X2LargeURL", "X3LargeURL", "OriginalURL");
         // List of image links to look for
         $links = array("URL", "LightboxURL");
         /**
          * Start assembling the photo data
          */
         $this->photo = array("provider" => self::PROVIDER_NAME, "id" => $id, "dates" => array("taken" => new DateTime($response['Image']['LastUpdated']), "updated" => new DateTime($response['Image']['LastUpdated']), "uploaded" => new DateTime($response['Image']['LastUpdated'])), "author" => array(), "title" => $response['Image']['Caption'], "description" => $response['Image']['FileName'], "tags" => $response['Image']['Keywords'], "sizes" => array(), "urls" => array());
         /**
          * Grab all the image sizes
          */
         foreach ($sizes as $size) {
             if (isset($response['Image'][$size]) && !empty($response['Image'][$size])) {
                 $dimensions = getimagesize($response['Image'][$size]);
                 ini_set("max_execution_time", 3200);
                 $this->photo['sizes'][$size] = array("source" => $response['Image'][$size], "width" => $dimensions[0], "height" => $dimensions[1]);
             }
         }
         $this->photo['sizes'] = Images::normaliseSizes($this->photo['sizes']);
         /**
          * Grab all the image links
          */
         foreach ($links as $link) {
             if (isset($response['Image'][$link]) && !empty($response['Image'][$link])) {
                 $this->photo['urls'][$link] = $response['Image'][$link];
             }
         }
         /**
          * Grab the image owner
          */
         if (preg_match("#(http|https)://([a-zA-Z0-9\\-_]+).smugmug#", $response['Image']['URL'], $matches)) {
             $nickname = $matches[2];
             if ($response = $this->send("smugmug.users.getInfo", array("NickName" => $nickname))) {
                 if ($response['stat'] == "ok") {
                     $this->photo['author'] = array("id" => $nickname, "username" => $response['User']['NickName'], "realname" => $response['User']['Name'], "url" => $response['User']['URL']);
                 }
             }
         }
         return $this->photo;
     }
 }
Example #9
0
 /**
  * Get locomotive class timeline
  * @since Version 3.8.7
  * @return array
  */
 public function getTimeline()
 {
     $query = "SELECT d.*, lu.loco_num, ld.loco_date_text FROM loco_unit_date AS d INNER JOIN loco_date_type AS ld ON ld.loco_date_id = d.loco_date_id INNER JOIN loco_unit AS lu ON lu.loco_id = d.loco_unit_id WHERE lu.class_id = ? ORDER BY timestamp ASC";
     $return = array("timeline" => array("headline" => $this->name . " timeline", "type" => "default", "text" => NULL, "asset" => array("media" => NULL, "credit" => NULL, "caption" => NULL), "date" => array()));
     foreach ($this->db->fetchAll($query, $this->id) as $row) {
         if ($row['timestamp'] == "0000-00-00") {
             $row['timestamp'] = date("Y-m-d", $row['date']);
         }
         $row['meta'] = json_decode($row['meta'], true);
         $data = array("startDate" => str_replace("-", ",", $row['timestamp']), "endDate" => str_replace("-", ",", $row['timestamp']), "headline" => $row['loco_num'] . " - " . $row['loco_date_text'], "text" => $row['text'], "asset" => array("media" => NULL, "thumbnail" => NULL, "credit" => NULL, "caption" => NULL), "meta" => array("date_id" => $row['date_id']));
         /**
          * Location
          */
         if (isset($row['meta']['position']['lat']) && isset($row['meta']['position']['lon'])) {
             try {
                 $imageObject = new \Railpage\Images\MapImage($row['meta']['position']['lat'], $row['meta']['position']['lon']);
                 $data['asset']['media'] = $imageObject->sizes['thumb']['source'];
                 $data['asset']['thumbnail'] = $imageObject->sizes['thumb']['source'];
                 $data['asset']['caption'] = "<a href='/place?lat=" . $imageObject->Place->lat . "&lon=" . $imageObject->Place->lon . "'>" . $imageObject->Place->name . ", " . $imageObject->Place->Country->name . "</a>";
             } catch (Exception $e) {
                 // Throw it away. Throw. It. Away. NOW!
             }
         }
         /**
          * Liveries
          */
         if (isset($row['meta']['livery']['id'])) {
             try {
                 $Images = new \Railpage\Images\Images();
                 $imageObject = $Images->findLocoImage($row['loco_unit_id'], $row['meta']['livery']['id']);
                 if ($imageObject instanceof \Railpage\Images\Image) {
                     $data['asset']['media'] = $imageObject->sizes['thumb']['source'];
                     $data['asset']['thumbnail'] = $imageObject->sizes['thumb']['source'];
                     $data['asset']['caption'] = "<a href='/image?id=" . $imageObject->id . "'>" . $imageObject->title . "</a>";
                     $data['asset']['credit'] = $imageObject->author->username;
                 }
             } catch (Exception $e) {
                 // Throw it away. Throw. It. Away. NOW!
             }
         }
         $return['timeline']['date'][] = $data;
     }
     return $return;
 }
Example #10
0
 /**
  * Find a suitable cover photo
  * @since Version 3.10.0
  * @param string|object $searchQuery
  * @return string
  */
 public static function GuessCoverPhoto($searchQuery)
 {
     $defaultPhoto = "https://static.railpage.com.au/i/logo-fb.jpg";
     $cachekey = sprintf("railpage:coverphoto=%s", md5($searchQuery));
     $Memcached = AppCore::getMemcached();
     #if ($image = $Memcached->fetch($cachekey)) {
     #   return $image;
     #}
     $SphinxQL = AppCore::getSphinx();
     if (!is_string($searchQuery)) {
         return $defaultPhoto;
     }
     preg_match_all('/([a-zA-Z]|\\xC3[\\x80-\\x96\\x98-\\xB6\\xB8-\\xBF]|\\xC5[\\x92\\x93\\xA0\\xA1\\xB8\\xBD\\xBE]){4,}/', $searchQuery, $match_arr);
     $word_arr = $match_arr[0];
     $words = implode(" || ", $word_arr);
     $SphinxQL->select()->from("idx_images")->match(array("title", "description"), $words, true);
     $rs = $SphinxQL->execute();
     if (!count($rs)) {
         return $defaultPhoto;
     }
     $photo = $rs[0];
     $photo['meta'] = json_decode($photo['meta'], true);
     $photo['sizes'] = Images::NormaliseSizes($photo['meta']['sizes']);
     foreach ($photo['sizes'] as $size) {
         if ($size['width'] > 400 && $size['height'] > 300) {
             $Memcached->save($cachekey, $size['source'], 0);
             return $size['source'];
         }
     }
 }
Example #11
0
 /**
  * Get skipped images
  * @since Version 3.10.0
  * @param int $page
  * @param int $itemsPerPage
  * @return array
  */
 public function getSkippedImages($page = 1, $itemsPerPage = 25)
 {
     $query = "SELECT SQL_CALC_FOUND_ROWS i.*\r\n            FROM image_flags_skip AS f\r\n            LEFT JOIN image AS i ON f.image_id = i.id\r\n            GROUP BY f.image_id\r\n            ORDER BY f.date DESC\r\n            LIMIT ?, ?";
     $params = [($page - 1) * $itemsPerPage, $itemsPerPage];
     $return = array("total" => 0, "page" => $page, "items_per_page" => $itemsPerPage, "photos" => array());
     foreach ($this->db->fetchAll($query, $params) as $row) {
         $row['meta'] = json_decode($row['meta'], true);
         $row['meta']['sizes'] = Images::normaliseSizes($row['meta']['sizes']);
         $return['photos'][] = $row;
     }
     $return['total'] = $this->db->fetchOne("SELECT FOUND_ROWS() AS total");
     return $return;
 }
Example #12
0
 /**
  * Get previous photos
  * @since Version 3.10.0
  * @return array
  * @param int $page
  * @param int $itemsPerPage
  */
 public function getPreviousPhotos($page = 1, $itemsPerPage = 25)
 {
     $Date = self::getStartOfWeek(new DateTime());
     $query = "SELECT SQL_CALC_FOUND_ROWS i.*, u.username, iw.added_by AS user_id FROM image_weekly AS iw\r\n                    LEFT JOIN image AS i ON iw.image_id = i.id\r\n                    LEFT JOIN nuke_users AS u ON u.user_id = iw.added_by\r\n                    WHERE iw.datefrom < ? LIMIT ?, ?";
     $params = [$Date->format("Y-m-d"), ($page - 1) * $itemsPerPage, $itemsPerPage];
     $result = $this->db->fetchAll($query, $params);
     $return = ["total" => 0, "page" => $page, "items_per_page" => $itemsPerPage, "photos" => []];
     $return['total'] = $this->db->fetchOne("SELECT FOUND_ROWS() AS total");
     foreach ($result as $row) {
         $row['meta'] = json_decode($row['meta'], true);
         $row['meta']['sizes'] = Images::normaliseSizes($row['meta']['sizes']);
         $row['url'] = Utility\Url::CreateFromImageID($row['id'])->getURLs();
         $return['photos'][] = $row;
     }
     return $return;
 }
Example #13
0
 /**
  * Get photos from the Sphinx search API within or adjacent to this place
  * @since Version 3.9.1
  * @param int $num
  * @return array
  */
 public function getPhotosFromSphinx($num = 10)
 {
     $Sphinx = AppCore::getSphinxAPI();
     $Sphinx->SetGeoAnchor("lat", "lon", deg2rad($this->lat), deg2rad($this->lon));
     $Sphinx->SetFilterRange("@geodist", 0, 5000);
     // 1km radius
     $Sphinx->SetSortMode(SPH_SORT_EXTENDED, '@geodist ASC');
     $result = $Sphinx->query("", "idx_images");
     $return = array("stat" => "ok");
     if (!$result) {
         $return['stat'] = "err";
         $return['message'] = $Sphinx->getLastError();
         return $return;
     }
     if (empty($result['matches'])) {
         return $return;
     }
     foreach ($result['matches'] as $row) {
         $meta = json_decode($row['attrs']['meta'], true);
         $return['photos'][] = array("id" => $row['attrs']['image_id'], "provider" => $row['attrs']['provider'], "photo_id" => $row['attrs']['photo_id'], "url" => $row['attrs']['url'], "distance" => round($row['attrs']['@geodist']), "lat" => rad2deg($row['attrs']['lat']), "lon" => rad2deg($row['attrs']['lon']), "title" => empty(trim($row['attrs']['title'])) ? "Untitled" : $row['attrs']['title'], "description" => $row['attrs']['description'], "sizes" => Images::normaliseSizes($meta['sizes']));
     }
     $return['photos'] = array_slice($return['photos'], 0, $num);
     return $return;
 }
Example #14
0
 /**
  * Get the top 5 photos viewed today/weekly/overall
  * @since Version 3.10.0
  * @param string $lookup
  * @param int $num
  * @return array
  */
 public function getMostViewedPhotos($lookup = self::HITS_WEEKLY, $num = 5)
 {
     $allowed = [self::HITS_DAILY => "hits_today", self::HITS_WEEKLY => "hits_weekly", self::HITS_OVERALL => "hits_overall"];
     if (!in_array($lookup, array_keys($allowed))) {
         throw new InvalidArgumentException("Parameter supplied for lookup type is invalid");
     }
     $query = "SELECT i.*,\r\n            u.username,\r\n            f.*\r\n            FROM image AS i\r\n            LEFT JOIN image_flags AS f ON f.image_id = i.id\r\n            LEFT JOIN nuke_users AS u ON i.user_id = u.user_id\r\n            ORDER BY " . $allowed[$lookup] . " DESC \r\n            LIMIT 0, ?";
     $result = $this->db->fetchAll($query, $num);
     foreach ($result as $key => $val) {
         $result[$key]['meta'] = json_decode($val['meta'], true);
         $result[$key]['meta']['author']['url'] = UserUrlUtility::MakeURLs($val);
         $result[$key]['meta']['sizes'] = Images::normaliseSizes($result[$key]['meta']['sizes']);
         if ($result[$key]['meta']['author']['url'] instanceof Url) {
             $result[$key]['meta']['author']['url'] = $result[$key]['meta']['author']['url']->getURLs();
         }
     }
     return $result;
 }