imageFilename = $row['ContentPostImageFilename']; parent::__construct($row); } /************************************************************************** * CRUD Functions **************************************************************************/ /** * Stores a new post * * @param int $userID The ID of the user that created this post * @param int $languageID The language this post is in * @param string $text The thought the user is sharing * @return int The ID of the newly created post */ public static function create($userID, $languageID, $text) { $newPostID = parent::create($userID, $languageID, $text); // Do nothing else - All the information for a content post is contained // in the Post. return $newPostID; } /** * Permanently removes this post AND all associated reposts * * WARNING: Since all reposts depend on the content post, calling this * function will delete them too. */ public function delete() { $relatedPosts = $this->getRelatedPosts() ; foreach ($relatedPosts as $post) { $post->delete(); } if ($this->imageFilename) { // Remove Local Image $localPath = $this->getLocalImagePath($this->imageFilename); if (is_file($localPath)) { unlink($localPath); } // Remove from AWS $amazonRoute = $this->getAmazonRoute($this->imageFilename); AWSCloudDelete(self::AWSBUCKET, $amazonRoute); } parent::delete(); } private function getLocalImagePath($filename) { return IMAGEROOT . self::IMAGESUBPATH . $filename; } private function getAmazonRoute($filename) { return '/images/' . self::IMAGESUBPATH . $filename; } /************************************************************************** * Getters \ Setters \ Checkers **************************************************************************/ public function saveImageFile($filesArrayElement) { // Determine Filename and Path $extension = self::getImageExtension($filesArrayElement['tmp_name']); $filename = $this->id . "." .$extension; $localPath = $this->getLocalImagePath($filename); // Save Locally move_uploaded_file($filesArrayElement['tmp_name'], $localPath); // Upload to AWS $amazonRoute = $this->getAmazonRoute($filename); AWSCloud(self::AWSBUCKET, $amazonRoute, $localPath); safe_dbwrite(" UPDATE tig.ProfilePosts SET ContentPostImageFilename = '" . mysqlescape($filename) . "' WHERE ID = $this->id "); } public static function checkImageFile($filesArrayElement) { // Validate File Properties $uploadValidator = new \TIG\Common\UploadValidator(); $uploadValidator->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); $uploadError = $uploadValidator->validate($filesArrayElement); switch ($uploadError) { case \TIG\Common\UploadValidator::MISSING: return self::ERROR_MISSING; case \TIG\Common\UploadValidator::EXTENSIONINVALID: return self::ERROR_FORMATINVALID; } // Validate Image Properties $imageValidator = new \TIG\Common\ImageValidator(); $validImageTypes = array( IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF ); $imageValidator->setValidImageTypes($validImageTypes); $imageError = $imageValidator->validate($filesArrayElement['tmp_name']); switch ($imageError) { case \TIG\Common\ImageValidator::UNREADABLE: return self::ERROR_MISSING; case \TIG\Common\ImageValidator::TYPEINVALID: return self::ERROR_FORMATINVALID; } return false; } public function getImageUrl() { if (!$this->imageFilename) { return; } return "http://acdn.tigurl.org/images/" . self::IMAGESUBPATH . $this->imageFilename; } /************************************************************************** * Helpers **************************************************************************/ private static function getImageExtension($file) { $imageType = exif_imagetype($file); // Is it an image? if ($imageType === false) { throw new \InvalidArgumentException('File is not an image.'); } switch ($imageType) { case IMAGETYPE_GIF: return 'gif'; case IMAGETYPE_JPEG: return 'jpg'; case IMAGETYPE_PNG: return 'png'; default: throw new \Exception('Unacceptable image type: ' . $imageType); } } /************************************************************************** * Required by Post **************************************************************************/ public function getHtmlizedText() { return htmlspecialchars($this->text); } public function isRepostableByUser($userID) { if (!filter_var($userID, FILTER_VALIDATE_INT)) { throw new \InvalidArgumentException("Invalid user ID: $userID"); } // Users cannot repost their own post if ($this->userID == $userID) { return false; } return true; } public function repost($userID, $languageID, $text) { return ContentRepost::create($userID, $languageID, $text, $this->id, $this->id); } public function getRelatedPosts() { return ContentRepost::readByContentPostID($this->id); } }