/** * Create a user by their username * @since Version 3.9.1 * @return \Railpage\Users\User * @param string $username */ public static function CreateUserFromUsername($username = null) { $id = Utility\UserUtility::getUserId($username); if (!filter_var($id, FILTER_VALIDATE_INT)) { throw new Exception("Could not find user ID from given username"); } return self::CreateUser($id); }
/** * Update the author for a specific image * @since Version 3.10.0 * @param \Railpage\Images\Image $imageObject * @return \Railpage\Images\Image */ public static function updateAuthor(Image $imageObject) { if ($id = UserUtility::findFromFlickrNSID($imageObject->author->id)) { $imageObject->author->railpage_id = $id; $imageObject->author->User = UserFactory::CreateUser($imageObject->author->railpage_id); $imageObject->commit(); } return $imageObject; }
/** * Commit changes to existing user or create new user * @since Version 3.1 * @version 3.9 * * @param boolean $force Force an update of this user even if certain values (eg a password) are empty * * @return boolean */ public function commit($force = false) { $this->validate($force); Utility\UserUtility::clearCache($this); $data = array(); foreach (Utility\UserUtility::getColumnMapping() as $key => $var) { $data[$key] = $this->{$var}; } if (is_string($data['meta']) && strpos($data['meta'], "\\\\\\\\") !== false) { $data['meta'] = NULL; } $json = ["meta", "user_opts"]; foreach ($json as $key) { $data[$key] = json_encode($data[$key]); } if ($this->RegistrationDate instanceof DateTime) { $data['user_regdate_nice'] = $this->RegistrationDate->format("Y-m-d H:i:s"); } if (filter_var($this->id, FILTER_VALIDATE_INT)) { $this->db->update("nuke_users", $data, array("user_id = ?" => $this->id)); } else { $this->db->insert("nuke_users", $data); $this->id = $this->db->lastInsertId(); $this->guest = false; $this->createUrls(); } $this->id = intval($this->id); // Update the registry $Registry = Registry::getInstance(); $regkey = sprintf(self::REGISTRY_KEY, $this->id); $Registry->remove($regkey); #->set($regkey, $this); return true; }
/** * Populate this image with fresh data * * @since Version 3.8.7 * @return $this * * @param boolean $force * @param int $option * * @throws \Exception if the photo cannot be found on the image provider * @todo Split this into utility functions */ public function populate($force = false, $option = null) { if ($force === false && !$this->isStale()) { return $this; } Debug::LogCLI("Fetching data from " . $this->provider . " for image ID " . $this->id . " (photo ID " . $this->photo_id . ")"); /** * Start the debug timer */ if (RP_DEBUG) { global $site_debug; $debug_timer_start = microtime(true); } /** * New and improved populator using image providers */ $Provider = $this->getProvider(); $data = false; try { $data = $Provider->getImage($this->photo_id, $force); } catch (Exception $e) { $expected = array(sprintf("Unable to fetch data from Flickr: Photo \"%s\" not found (invalid ID) (1)", $this->photo_id), "Unable to fetch data from Flickr: Photo not found (1)"); if (in_array($e->getMessage(), $expected)) { $where = ["image_id = ?" => $this->id]; $this->db->delete("image_link", $where); $where = ["id = ?" => $this->id]; $this->db->delete("image", $where); throw new Exception("Photo no longer available from " . $this->provider); } } if ($data) { $this->sizes = $data['sizes']; $this->title = empty($data['title']) ? "Untitled" : $data['title']; $this->description = $data['description']; $this->meta = array("dates" => array("posted" => $data['dates']['uploaded'] instanceof DateTime ? $data['dates']['uploaded']->format("Y-m-d H:i:s") : $data['dates']['uploaded']['date'], "taken" => $data['dates']['taken'] instanceof DateTime ? $data['dates']['taken']->format("Y-m-d H:i:s") : $data['dates']['taken']['date'])); $this->author = new stdClass(); $this->author->username = $data['author']['username']; $this->author->realname = !empty($data['author']['realname']) ? $data['author']['realname'] : $data['author']['username']; $this->author->id = $data['author']['id']; $this->author->url = "https://www.flickr.com/photos/" . $this->author->id; if ($user_id = UserUtility::findFromFlickrNSID($this->author->id)) { $data['author']['railpage_id'] = $user_id; } if (isset($data['author']['railpage_id']) && filter_var($data['author']['railpage_id'], FILTER_VALIDATE_INT)) { $this->author->User = UserFactory::CreateUser($data['author']['railpage_id']); } /** * Load the tags */ if (isset($data['tags']) && is_array($data['tags']) && count($data['tags'])) { foreach ($data['tags'] as $row) { $this->meta['tags'][] = $row['raw']; } } /** * Load the Place object */ if ($option != Images::OPT_NOPLACE && isset($data['location']) && !empty($data['location'])) { try { $this->Place = Place::Factory($data['location']['latitude'], $data['location']['longitude']); } catch (Exception $e) { // Throw it away. Don't care. } } $this->links = new stdClass(); $this->links->provider = isset($data['urls']['url'][0]['_content']) ? $data['urls']['url'][0]['_content'] : $data['urls'][key($data['urls'])]; $this->commit(); $this->cacheGeoData(); return true; } /** * Fetch data in various ways for different photo providers */ switch ($this->provider) { /** * Picasa */ case "picasaweb": if (empty($this->meta) && !is_null(filter_input(INPUT_SERVER, "HTTP_REFERER", FILTER_SANITIZE_URL)) && strpos(filter_input(INPUT_SERVER, "HTTP_REFERER", FILTER_SANITIZE_URL), "picasaweb.google.com")) { $album = preg_replace("@(http|https)://picasaweb.google.com/([a-zA-Z\\-\\.]+)/(.+)@", "\$2", filter_input(INPUT_SERVER, "HTTP_REFERER", FILTER_SANITIZE_URL)); if (is_string($album)) { $update_url = sprintf("https://picasaweb.google.com/data/feed/api/user/%s/photoid/%s?alt=json", $album, $this->photo_id); } } if (isset($update_url)) { $data = file_get_contents($update_url); $json = json_decode($data, true); $this->meta = array("title" => $json['feed']['subtitle']['$t'], "description" => $json['feed']['title']['$t'], "dates" => array("posted" => date("Y-m-d H:i:s", $json['feed']['gphoto$timestamp']['$t'])), "sizes" => array("original" => array("width" => $json['feed']['gphoto$width']['$t'], "height" => $json['feed']['gphoto$height']['$t'], "source" => str_replace(sprintf("/s%d/", $json['feed']['media$group']['media$thumbnail'][0]['width']), sprintf("/s%d/", $json['feed']['gphoto$width']['$t']), $json['feed']['media$group']['media$thumbnail'][0]['url'])), "largest" => array("width" => $json['feed']['gphoto$width']['$t'], "height" => $json['feed']['gphoto$height']['$t'], "source" => str_replace(sprintf("/s%d/", $json['feed']['media$group']['media$thumbnail'][0]['width']), sprintf("/s%d/", $json['feed']['gphoto$width']['$t']), $json['feed']['media$group']['media$thumbnail'][0]['url']))), "photo_id" => $json['feed']['gphoto$id']['$t'], "album_id" => $json['feed']['gphoto$albumid']['$t'], "updateurl" => sprintf("%s?alt=json", $json['feed']['id']['$t'])); foreach ($json['feed']['media$group']['media$thumbnail'] as $size) { if ($size['width'] <= 500 && $size['width'] > 200) { $this->meta['sizes']['small'] = array("width" => $size['width'], "height" => $size['height'], "source" => $size['url']); } if ($size['width'] <= 200) { $this->meta['sizes']['small'] = array("width" => $size['width'], "height" => $size['height'], "source" => $size['url']); } if ($size['width'] <= 1024 && $size['width'] > 500) { $this->meta['sizes']['large'] = array("width" => $size['width'], "height" => $size['height'], "source" => $size['url']); } } foreach ($json['feed']['link'] as $link) { if ($link['rel'] == "alternate" && $link['type'] == "text/html") { $this->meta['source'] = $link['href']; } } if ($option != Images::OPT_NOPLACE && isset($json['feed']['georss$where']['gml$Point']) && is_array($json['feed']['georss$where']['gml$Point'])) { $pos = explode(" ", $json['feed']['georss$where']['gml$Point']['gml$pos']['$t']); $this->Place = Place::Factory($pos[0], $pos[1]); } $this->title = $this->meta['title']; $this->description = $this->meta['description']; $this->author = new stdClass(); $this->author->username = $album; $this->author->id = $album; $this->author->url = sprintf("%s/%s", $json['feed']['generator']['uri'], $album); } $this->sizes = $this->meta['sizes']; $this->commit(); break; /** * Vicsig */ /** * Vicsig */ case "vicsig": if (strpos(filter_input(INPUT_SERVER, "HTTP_REFERER", FILTER_SANITIZE_URL), "vicsig.net/photo")) { $this->meta['source'] = filter_input(INPUT_SERVER, "HTTP_REFERER", FILTER_SANITIZE_STRING); $response = $this->GuzzleClient->get($this->meta['source']); if ($response->getStatusCode() != 200) { throw new Exception(sprintf("Failed to fetch image data from %s: HTTP error %s", $this->provider, $response->getStatusCode())); } /** * Start fetching it */ $data = $response->getBody(); $doc = new DomDocument(); $doc->loadHTML($data); $images = $doc->getElementsByTagName("img"); foreach ($images as $element) { if (!empty($element->getAttribute("src")) && !empty($element->getAttribute("alt"))) { #$image_title = $element->getAttribute("alt"); $this->sizes['original'] = array("source" => $element->getAttribute("src"), "width" => $element->getAttribute("width"), "height" => $element->getAttribute("height")); if (substr($this->sizes['original']['source'], 0, 1) == "/") { $this->sizes['original']['source'] = "http://www.vicsig.net" . $this->sizes['original']['source']; } break; } } $desc = $doc->getElementsByTagName("i"); foreach ($desc as $element) { if (!isset($image_desc)) { $text = trim($element->nodeValue); $text = str_replace("\r\n", "\n", $text); $text = explode("\n", $text); /** * Loop through the exploded text and remove the obvious date/author/etc */ foreach ($text as $k => $line) { // Get the author if (preg_match("@Photo: @i", $line)) { $this->author = new stdClass(); $this->author->realname = str_replace("Photo: ", "", $line); $this->author->url = filter_input(INPUT_SERVER, "HTTP_REFERER", FILTER_SANITIZE_STRING); unset($text[$k]); } // Get the date try { $this->meta['dates']['posted'] = (new DateTime($line))->format("Y-m-d H:i:s"); unset($text[$k]); } catch (Exception $e) { // Throw it away } } /** * Whatever's left must be the photo title and description */ foreach ($text as $k => $line) { if (empty($this->title)) { $this->title = $line; continue; } $this->description .= $line; } $this->links = new stdClass(); $this->links->provider = filter_input(INPUT_SERVER, "HTTP_REFERER", FILTER_SANITIZE_STRING); $this->commit(); } } } break; } /** * End the debug timer */ if (RP_DEBUG) { $site_debug[] = __CLASS__ . "::" . __FUNCTION__ . "() : completed in " . round(microtime(true) - $debug_timer_start, 5) . "s"; } return $this; }