/** * Uploads the given file to a temporary location on the site. * * @since 1.0 * @access public * @param string * @return string The path to the uploaded item. */ public function upload($file, $hash, $userId) { // Check if file exists on the server if (!isset($file['tmp_name']) || empty($file)) { $this->setError(JText::_('COM_EASYSOCIAL_UPLOADER_FILE_NOT_FOUND')); return false; } // Lets figure out the storage path. $config = FD::config(); // Test if the folder exists for this upload type. $path = JPATH_ROOT . '/' . FD::cleanPath($config->get('uploader.storage.container')); if (!FD::makeFolder($path)) { $this->setError(JText::sprintf('COM_EASYSOCIAL_UPLOADER_UNABLE_TO_CREATE_DESTINATION_FOLDER', $path)); return false; } // Let's finalize the storage path. $storage = $path . '/' . $userId; if (!FD::makeFolder($storage)) { $this->setError(JText::sprintf('COM_EASYSOCIAL_UPLOADER_UNABLE_TO_CREATE_DESTINATION_FOLDER', $storage)); return false; } // Once the script reaches here, we assume everything is good now. // Copy the files over. jimport('joomla.filesystem.file'); $absolutePath = $storage . '/' . $hash; if (!JFile::copy($file['tmp_name'], $absolutePath)) { $this->setError(JText::sprintf('COM_EASYSOCIAL_UPLOADER_UNABLE_TO_COPY_TO_DESTINATION_FOLDER', $absolutePath)); return false; } return $absolutePath; }
/** * Retrieves the uri for this image * * @since 1.3 * @access public * @param string * @return */ public function getUrl() { $config = FD::config(); // Get the container location $container = FD::cleanPath($config->get('links.cache.location')); // Relative path to the item $relativePath = $this->getRelativePath(); // Default base url $url = rtrim(JURI::root(), '/') . $relativePath; // Get the storage type for cached images for links if ($this->storage != 'joomla') { $storage = FD::storage($this->storage); $url = $storage->getPermalink($relativePath); } return $url; }
/** * Deletes a list of default avatars given the unique id and type. * * @since 1.0 * @access public * @param string * @return */ public function deleteDefaultAvatars($uid, $type = SOCIAL_TYPE_PROFILES) { $avatars = $this->getDefaultAvatars($uid, $type); if (!$avatars) { return; } jimport('joomla.filesystem.folder'); $config = FD::config(); // Build the path to the default avatars. // Get the default avatars storage location. $path = JPATH_ROOT . '/' . FD::cleanPath($config->get('avatars.storage.container')) . '/' . FD::cleanPath($config->get('avatars.storage.default')); $path = $path . '/' . FD::cleanPath($config->get('avatars.storage.defaults.' . $type)); if (!JFolder::exists($path)) { $this->setError(JText::_('Default avatars path does not exist.')); return false; } $state = JFolder::delete($path); return $state; }
/** * Stores a given image link into the local cache * * @since 1.2.11 * @access public * @param string * @return */ public function cache($imageLink) { // Check if settings is enabled if (!$this->config->get('links.cache.images')) { return false; } // Generate a unique name for this file $name = md5($imageLink) . '.png'; // Get the storage path $storageFolder = FD::cleanPath($this->config->get('links.cache.location')); $storage = JPATH_ROOT . '/' . $storageFolder . '/' . $name; $storageURI = rtrim(JURI::root(), '/') . '/' . $storageFolder . '/' . $name; $exists = JFile::exists($storage); // If the file is already cached, skip this if ($exists) { return $storageURI; } // Crawl the image now. $connector = FD::get('Connector'); $connector->addUrl($imageLink); $connector->connect(); // Get the result and parse them. $contents = $connector->getResult($imageLink); // Store the file to a temporary directory first $tmpFile = SOCIAL_TMP . '/' . $name; JFile::write($tmpFile, $contents); // Load the image now $image = FD::image(); $image->load($tmpFile); // Ensure that image is valid if (!$image->isValid()) { JFile::delete($tmpFile); return false; } // Delete the temporary file. JFile::delete($tmpFile); // Unset the image now since we don't want to use asido to resize unset($image); // Store the file now into our cache storage. JFile::write($storage, $contents); return $storageURI; }
/** * Gets the storage path for photos folder * * @since 1.0 * @access public * @param string * @return */ public function getStoragePath($uid, $type, $createFolders = true) { // Construct destination path $config = FD::config(); // Get initial storage path $storage = JPATH_ROOT; // Append it with the container path $storage = $storage . '/' . FD::cleanPath($config->get('avatars.storage.container')); // Ensure that the folder exists if ($createFolders) { FD::makeFolder($storage); } // Append it with the type $storage = $storage . '/' . FD::cleanPath($config->get('avatars.storage.' . $type)); // Ensure that the folder exists if ($createFolders) { FD::makeFolder($storage); } // Construct the last segment which contains the uid. $storage = $storage . '/' . $uid; // Ensure that the path exists. if ($createFolders) { FD::makeFolder($storage); } return $storage; }
/** * Get's the uri to an avatar. * * @since 1.0 * @access public * @param bool Determine if the absolute uri should be returned. */ public function getSource($size = SOCIAL_AVATAR_MEDIUM, $absolute = true) { $config = FD::config(); // If avatar_id is not empty, means this is this from the default avatars if (!empty($this->avatar_id)) { $default = FD::table('defaultavatar'); $default->load($this->avatar_id); return $default->getSource($size, $absolute); } // If the avatar size that is being requested is invalid, return default avatar. if (!isset($this->{$size}) || empty($this->{$size})) { return false; } // @TODO: Configurable storage path. $avatarLocation = FD::cleanPath($config->get('avatars.storage.container')); $typesLocation = FD::cleanPath($config->get('avatars.storage.' . $this->type)); // Build absolute path to the file. $path = JPATH_ROOT . '/' . $avatarLocation . '/' . $typesLocation . '/' . $this->uid . '/' . $this->{$size}; // Detect if avatar exists. if (!JFile::exists($path)) { $default = rtrim(JURI::root(), '/') . $config->get('avatars.default.user.' . $size); return $default; } // Build the uri path for the avatar. $uri = $avatarLocation . '/' . $typesLocation . '/' . $this->uid . '/' . $this->{$size}; if ($absolute) { $uri = rtrim(JURI::root(), '/') . '/' . $uri; } return $uri; }
/** * Synchronizes photos from local storage to remote storage. * * @since 1.2 * @access public * @param string * @return */ public function syncPhotos() { $storageType = $this->getStorageType(); // If site is configured to storage in joomla, we don't need to do anything if ($storageType == 'joomla') { return JText::_('Current photos storage is set to local.'); } // Load up the storage library $storage = FD::storage($storageType); // Get the number of files to process at a time $limit = $this->getUploadLimit(); // Get a list of photos that failed during the transfer $exclusion = $this->getFailedObjects('photos'); // Get a list of files to be synchronized over. $model = FD::model('Photos'); $options = array('pagination' => $limit, 'storage' => SOCIAL_STORAGE_JOOMLA, 'ordering' => 'created', 'sort' => 'asc', 'exclusion' => $exclusion); // Get a list of photos to sync to amazon $photos = $model->getPhotos($options); $total = 0; if (!$photos) { return JText::_('No photos to upload to Amazon S3 right now.'); } // Get list of allowed photos $allowed = array('thumbnail', 'large', 'square', 'featured', 'medium', 'original', 'stock'); foreach ($photos as $photo) { // Load the album $album = FD::table('Album'); $album->load($photo->album_id); // If the album no longer exists, skip this if (!$album->id) { continue; } // Get the base path for the album $basePath = $photo->getStoragePath($album); $states = array(); // Now we need to get all the available files for this photo $metas = $model->getMeta($photo->id, SOCIAL_PHOTOS_META_PATH); // Go through each meta foreach ($metas as $meta) { // To prevent some faulty data, we need to manually reconstruct the path here. $absolutePath = $meta->value; $file = basename($absolutePath); $container = FD::cleanPath($this->config->get('photos.storage.container')); // Reconstruct the path to the source file $source = JPATH_ROOT . '/' . $container . '/' . $album->id . '/' . $photo->id . '/' . $file; // To prevent faulty data, manually reconstruct the path here. $dest = $container . '/' . $album->id . '/' . $photo->id . '/' . $file; $dest = ltrim($dest, '/'); // We only want to upload certain files if (in_array($meta->property, $allowed)) { // Upload the file to the remote storage now $state = $storage->push($photo->title . $photo->getExtension(), $source, $dest); // Delete the source file if successfull and configured to do so. if ($state && $this->deleteable()) { JFile::delete($source); } $states[] = $state; } } $success = !in_array(false, $states); // If there are no errors, we want to update the storage for the photo if ($success) { $photo->storage = $storageType; $state = $photo->store(); // if photo storage successfully updated to amazon, we need to update the cached object in stream_item. // Find and update the object from stream_item. $stream = FD::table('StreamItem'); $options = array('context_type' => SOCIAL_TYPE_PHOTO, 'context_id' => $photo->id); $exists = $stream->load($options); if ($exists) { $stream->params = FD::json()->encode($photo); $stream->store(); } $total += 1; } // Add this to the storage logs $this->log($photo->id, 'photos', $success); } if ($total > 0) { return JText::sprintf('%1s photos uploaded to remote storage', $total); } return JText::sprintf('No photos to upload to remote storage'); }
/** * Logics to copy default avatars from a profile. * * @since 1.1 * @access public * @author Sam Teh <*****@*****.**> */ public function copyAvatar($targetProfileId) { $avatarsModel = FD::model('Avatars'); $defaultAvatars = $avatarsModel->getDefaultAvatars($targetProfileId); if ($defaultAvatars) { foreach ($defaultAvatars as $avatar) { unset($avatar->id); $tbl = FD::table('DefaultAvatar'); $tbl->bind($avatar); $tbl->uid = $this->id; $tbl->type = SOCIAL_TYPE_PROFILES; $tbl->created = FD::date()->toMySQL(); $tbl->store(); } // lets copy the image files. $config = FD::config(); // Get the avatars storage path. $avatarsPath = FD::cleanPath($config->get('avatars.storage.container')); // Get the defaults storage path. $defaultsPath = FD::cleanPath($config->get('avatars.storage.default')); // Get the types storage path. $typesPath = FD::cleanPath($config->get('avatars.storage.defaults.' . SOCIAL_TYPE_PROFILES)); // Get the id storage path $sourceId = FD::cleanPath($this->uid); // Let's construct the final path. $sourcePath = JPATH_ROOT . '/' . $avatarsPath . '/' . $defaultsPath . '/' . $typesPath . '/' . $targetProfileId; $targetPath = JPATH_ROOT . '/' . $avatarsPath . '/' . $defaultsPath . '/' . $typesPath . '/' . $this->id; if (JFolder::exists($targetPath)) { return; } // now we are save to copy. if (JFolder::exists($sourcePath)) { JFolder::copy($sourcePath, $targetPath); } } }
/** * Returns the file path * * @since 1.0 * @access public * @param string * @return */ public function getStoragePath($relative = false) { // Lets figure out the storage path. $config = FD::config(); $path = ''; if (!$relative) { $path = JPATH_ROOT; } $path .= '/' . FD::cleanPath($config->get('files.storage.container')); $typePath = $path . '/' . FD::cleanPath($config->get('files.storage.' . $this->type . '.container')); // Let's finalize the storage path. $storage = $typePath . '/' . $this->uid; return $storage; }
/** * Stores a given image link into the local cache * * @since 1.2.11 * @access public * @param string * @return */ public function cache($imageLink) { // Check if settings is enabled if (!$this->config->get('links.cache.images')) { return false; } // Try to load any existing cached image from the db $linkImage = FD::table('LinkImage'); $exists = $linkImage->load(array('source_url' => $imageLink)); // If this already exists, skip this altogether if ($exists) { return $linkImage->internal_url; } // Generate a unique name for this file $fileName = md5($imageLink) . '.png'; // Get the storage path $container = FD::cleanPath($this->config->get('links.cache.location')); $storage = JPATH_ROOT . '/' . $container . '/' . $fileName; // Check if the file already exists $exists = JFile::exists($storage); // If the file is already cached, delete it if ($exists) { JFile::delete($storage); } // Crawl the image now. $connector = FD::get('Connector'); $connector->addUrl($imageLink); $connector->connect(); // Get the result and parse them. $contents = $connector->getResult($imageLink); // Store the file to a temporary directory first $tmpFile = SOCIAL_TMP . '/' . $fileName; JFile::write($tmpFile, $contents); // Load the image now $image = FD::image(); $image->load($tmpFile); // Ensure that image is valid if (!$image->isValid()) { JFile::delete($tmpFile); return false; } // Delete the temporary file. JFile::delete($tmpFile); // Unset the image now since we don't want to use asido to resize unset($image); // Store the file now into our cache storage. JFile::write($storage, $contents); $linkImage->source_url = $imageLink; $linkImage->internal_url = $fileName; $linkImage->store(); return $fileName; }
/** * Removes phsyical avatar file from the system. * * @since 1.0 * @access private * @param string The size to remove. * @return bool True on success, false otherwise. * * @author Mark Lee <*****@*****.**> */ private function removeImage($size = SOCIAL_AVATAR_SMALL) { jimport('joomla.filesystem.file'); if (!empty($this->{$size})) { // Build the path to the image item. $config = FD::config(); $path = JPATH_ROOT; $path = $path . '/' . FD::cleanPath($config->get('avatars.storage.container')); $path = $path . '/' . FD::cleanPath($config->get('avatars.storage.default')); $path = $path . '/' . FD::cleanPath($config->get('avatars.storage.defaults.' . $this->type)); $path = $path . '/' . FD::cleanPath($this->uid); $path = $path . '/' . $this->{$size}; if (!JFile::exists($path)) { // dump( $path ); $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_AVATARS_FILE_NOT_FOUND')); return false; } return JFile::delete($path); } return false; }
public function saveCover(&$post, $uid, $type) { $coverData = !empty($post[$this->inputName]) ? $post[$this->inputName] : ''; unset($post[$this->inputName]); if (empty($coverData)) { return true; } $coverData = FD::makeObject($coverData); // Get the cover table. $cover = FD::table('Cover'); // Try to load existing cover $state = $cover->load(array('uid' => $uid, 'type' => $type)); // If no existing cover, then we set the uid and type. if (!$state) { $cover->uid = $uid; $cover->type = $type; } // If both data does not exist, then we don't proceed to store the data. if (empty($coverData->data) && empty($coverData->position)) { return true; } if (!empty($coverData->data)) { if ($coverData->data === 'delete') { $cover->delete(); return true; } $coverObj = FD::makeObject($coverData->data); // Get the cover album. $album = SocialFieldsUserCoverHelper::getDefaultAlbum($uid, $type); // Create the photo object. $photo = SocialFieldsUserCoverHelper::createPhotoObject($uid, $type, $album->id, $coverObj->stock->title, false); // If there are no cover set for this album, set it as cover. if (empty($album->cover_id)) { $album->cover_id = $photo->id; $album->store(); } // Construct the path to where the photo is temporarily uploaded. // $tmpPath = SocialFieldsUserCoverHelper::getPath($this->inputName); $tmpPath = dirname($coverObj->stock->path); // Get the supposed path of where the cover should be // Instead of doing SocialPhotos::getStoragePath, I copied the logic from there but only to create the folders up until albumId section. // We do not want JPATH_ROOT to be included in the $storage variable $storage = '/' . FD::cleanPath(FD::config()->get('photos.storage.container')); FD::makeFolder(JPATH_ROOT . $storage); $storage .= '/' . $album->id; FD::makeFolder(JPATH_ROOT . $storage); $storage .= '/' . $photo->id; FD::makeFolder(JPATH_ROOT . $storage); // Copy the photo from the temporary path to the storage folder. $state = JFolder::copy($tmpPath, JPATH_ROOT . $storage, '', true); // If cannot copy out the photo, then don't proceed if ($state !== true) { $this->setError(JText::_('PLG_FIELDS_COVER_ERROR_UNABLE_TO_MOVE_FILE')); return false; } // Create the photo meta for each available sizes. foreach ($coverObj as $key => $value) { SocialFieldsUserCoverHelper::createPhotoMeta($photo, $key, $storage . '/' . $value->file); } // Set the uploaded photo as cover for this user. $cover->setPhotoAsCover($photo->id); } // Set the position of the cover if available. if (!empty($coverData->position)) { $position = FD::makeObject($coverData->position); if (isset($position->x)) { $cover->x = $position->x; } if (isset($position->y)) { $cover->y = $position->y; } } // Store the cover object $cover->store(); // And we're done. return true; }
/** * Retrieves the permalink to the image given the size * * @since 1.0 * @access public * @param string * @return */ public function getSource($type = 'thumbnail') { static $paths = array(); $config = FD::config(); // Load the paths for this photo $model = FD::model('Photos'); $metas = $model->getMeta($this->id, SOCIAL_PHOTOS_META_PATH); $obj = new stdClass(); $path = FD::cleanPath($config->get('photos.storage.container')); $allowed = array('thumbnail', 'large', 'original', 'square', 'featured', 'medium'); foreach ($metas as $meta) { $relative = $path . '/' . $this->album_id . '/' . $this->id . '/' . basename($meta->value); if ($this->storage != SOCIAL_STORAGE_JOOMLA && in_array($meta->property, $allowed)) { $storage = FD::storage($this->storage); $url = $storage->getPermalink($relative); } else { $url = rtrim(JURI::root(), '/') . '/' . $relative; } $obj->{$meta->property} = $url; } $paths[$this->id] = $obj; if (!isset($paths[$this->id]->{$type})) { $paths[$this->id]->{$type} = false; } return $paths[$this->id]->{$type}; }
/** * Responsible to store the uploaded images. * * @since 1.0 * @access public * @param null * * @author Mark Lee <*****@*****.**> */ public function upload($file) { // Get config object. $config = FD::config(); // Do not proceed if image doesn't exist. if (empty($file) || !isset($file['tmp_name'])) { $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_AVATARS_FILE_UNAVAILABLE')); return false; } // Get the default avatars storage location. $avatarsPath = JPATH_ROOT . '/' . FD::cleanPath($config->get('avatars.storage.container')); // Test if the avatars path folder exists. If it doesn't we need to create it. if (!FD::makeFolder($avatarsPath)) { $this->setError(JText::_('Errors when creating default container for avatar')); return false; } // Get the default avatars storage location for this type. $typePath = $config->get('avatars.storage.' . $this->type); $storagePath = $avatarsPath . '/' . FD::cleanPath($typePath); // Ensure storage path exists. if (!FD::makeFolder($storagePath)) { $this->setError(JText::_('Errors when creating path for avatar')); return false; } // Get the profile id and construct the final path. $idPath = FD::cleanPath($this->uid); $storagePath = $storagePath . '/' . $idPath; // Ensure storage path exists. if (!FD::makeFolder($storagePath)) { $this->setError(JText::_('Errors when creating default path for avatar')); return false; } // Get the image library to perform some checks. $image = FD::get('Image'); $image->load($file['tmp_name']); // Test if the image is really a valid image. if (!$image->isValid()) { $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_AVATARS_FILE_NOT_IMAGE')); return false; } // Process avatar storage. $avatar = FD::get('Avatar', $image); // Let's create the avatar. $sizes = $avatar->create($storagePath); if ($sizes === false) { $this->setError(JText::_('Sorry, there was some errors when creating the avatars.')); return false; } // Delete previous files. $this->deleteFile($storagePath); // Assign the values back. foreach ($sizes as $size => $url) { $this->{$size} = $url; } return true; }
/** * Retrieves the user's avatar location * * @access public * @param string $size The avatar size to retrieve for. * @return string The current user's username. * * @author Mark Lee <*****@*****.**> */ public function getAvatar($size = SOCIAL_AVATAR_MEDIUM) { $config = FD::config(); // If avatar id is being set, we need to get the avatar source if ($this->defaultAvatar) { $default = $this->defaultAvatar->getSource($size); return $default; } // If the avatar size that is being requested is invalid, return default avatar. $default = $this->getDefaultAvatar($size); if (!$this->avatars[$size] || empty($this->avatars[$size])) { return $default; } // Get the path to the avatar storage. $avatarLocation = FD::cleanPath($config->get('avatars.storage.container')); $usersAvatarLocation = FD::cleanPath($config->get('avatars.storage.user')); // Build the path now. $path = $avatarLocation . '/' . $usersAvatarLocation . '/' . $this->id . '/' . $this->avatars[$size]; if ($this->avatarStorage == SOCIAL_STORAGE_JOOMLA) { // Build final storage path. $absolutePath = JPATH_ROOT . '/' . $path; // Detect if this file really exists. if (!JFile::exists($absolutePath)) { return $default; } $uri = rtrim(JURI::root(), '/') . '/' . $path; } else { $storage = FD::storage($this->avatarStorage); $uri = $storage->getPermalink($path); } return $uri; }
public function addPhotoAlbum($log_usr = 0, $album_id) { $my = FD::user(); // Load up the configuration $config = FD::config(); // Load the album table $album = FD::table('Album'); $album->load($album_id); // Check if the album id provided is valid if (!$album->id || !$album->id) { return "album not valid"; } // Get the uid and the type $uid = $album->uid; $type = $album->type; // Load the photo library $lib = FD::photo($uid, $type); // Set uploader options $options = array('name' => 'file', 'maxsize' => $lib->getUploadFileSizeLimit()); // Get uploaded file $file = FD::uploader($options)->getFile(); // If there was an error getting uploaded file, stop. if ($file instanceof SocialException) { return false; } // Load the image object $image = FD::image(); $image->load($file['tmp_name'], $file['name']); // Detect if this is a really valid image file. if (!$image->isValid()) { return false; } // Bind the photo data now $photo = FD::table('Photo'); $photo->uid = $uid; $photo->type = $type; $photo->user_id = $album->uid; $photo->album_id = $album->id; $photo->title = $file['name']; $photo->caption = ''; $photo->ordering = 0; $photo->state = SOCIAL_STATE_PUBLISHED; // Set the creation date alias $photo->assigned_date = FD::date()->toMySQL(); // Cleanup photo title. $photo->cleanupTitle(); // Trigger rules that should occur before a photo is stored $photo->beforeStore($file, $image); // Try to store the photo. $state = $photo->store(); if (!$state) { return false; } // If album doesn't have a cover, set the current photo as the cover. //~ if (!$album->hasCover()) { //~ $album->cover_id = $photo->id; //~ //~ // Store the album //~ $album->store(); //~ } // Get the photos library $photoLib = FD::get('Photos', $image); // Get the storage path for this photo $storageContainer = FD::cleanPath($config->get('photos.storage.container')); $storage = $photoLib->getStoragePath($album->id, $photo->id); $paths = $photoLib->create($storage); // We need to calculate the total size used in each photo (including all the variants) $totalSize = 0; // Create metadata about the photos if ($paths) { foreach ($paths as $type => $fileName) { $meta = FD::table('PhotoMeta'); $meta->photo_id = $photo->id; $meta->group = SOCIAL_PHOTOS_META_PATH; $meta->property = $type; // do not store the container path as this path might changed from time to time $tmpStorage = str_replace('/' . $storageContainer . '/', '/', $storage); $meta->value = $tmpStorage . '/' . $fileName; $meta->store(); // We need to store the photos dimension here list($width, $height, $imageType, $attr) = getimagesize(JPATH_ROOT . $storage . '/' . $fileName); // Set the photo size $totalSize += filesize(JPATH_ROOT . $storage . '/' . $fileName); // Set the photo dimensions $meta = FD::table('PhotoMeta'); $meta->photo_id = $photo->id; $meta->group = SOCIAL_PHOTOS_META_WIDTH; $meta->property = $type; $meta->value = $width; $meta->store(); $meta = FD::table('PhotoMeta'); $meta->photo_id = $photo->id; $meta->group = SOCIAL_PHOTOS_META_HEIGHT; $meta->property = $type; $meta->value = $height; $meta->store(); } } // Set the total photo size $photo->total_size = $totalSize; $photo->store(); // After storing the photo, trigger rules that should occur after a photo is stored $photo->afterStore($file, $image); // Determine if we should create a stream item for this upload $createStream = JRequest::getBool('createStream'); // Add Stream when a new photo is uploaded if ($createStream) { $photo->addPhotosStream('create'); } if ($isAvatar) { return $photo; } // After storing the photo, trigger rules that should occur after a photo is stored return $photo; }
/** * Allows caller to upload photos * * @since 1.0 * @access public */ public function upload($isAvatar = false) { // Check for request forgeries FD::checkToken(); // Only registered users should be allowed to upload photos FD::requireLogin(); // Get the current view $view = $this->getCurrentView(); // Get current user. $my = FD::user(); // Load up the configuration $config = FD::config(); // Check if the photos is enabled if (!$config->get('photos.enabled')) { $view->setMessage(JText::_('COM_EASYSOCIAL_ALBUMS_PHOTOS_DISABLED'), SOCIAL_MSG_ERROR); return $view->call(__FUNCTION__); } // Load the album table $albumId = JRequest::getInt('albumId'); $album = FD::table('Album'); $album->load($albumId); // Check if the album id provided is valid if (!$albumId || !$album->id) { $view->setMessage(JText::_('COM_EASYSOCIAL_PHOTOS_INVALID_ALBUM_ID_PROVIDED'), SOCIAL_MSG_ERROR); return $view->call(__FUNCTION__); } // Get the uid and the type $uid = $album->uid; $type = $album->type; // Load the photo library $lib = FD::photo($uid, $type); // Check if the upload is for profile pictures if (!$isAvatar) { // Check if the person exceeded the upload limit if ($lib->exceededUploadLimit()) { $view->setMessage($lib->getErrorMessage('upload.exceeded'), SOCIAL_MSG_ERROR); return $view->call(__FUNCTION__); } // Check if the person exceeded the upload limit if ($lib->exceededDiskStorage()) { $view->setMessage($lib->getErrorMessage(), SOCIAL_MSG_ERROR); return $view->call(__FUNCTION__); } // Check if the person exceeded their daily upload limit if ($lib->exceededDailyUploadLimit()) { $view->setMessage($lib->getErrorMessage('upload.daily.exceeded'), SOCIAL_MSG_ERROR); return $view->call(__FUNCTION__); } } // Set uploader options $options = array('name' => 'file', 'maxsize' => $lib->getUploadFileSizeLimit()); // Get uploaded file $file = FD::uploader($options)->getFile(); // If there was an error getting uploaded file, stop. if ($file instanceof SocialException) { $view->setMessage($file); return $view->call(__FUNCTION__); } // Load the image object $image = FD::image(); $image->load($file['tmp_name'], $file['name']); // Detect if this is a really valid image file. if (!$image->isValid()) { $view->setMessage(JText::_('COM_EASYSOCIAL_PHOTOS_INVALID_FILE_PROVIDED'), SOCIAL_MSG_ERROR); return $view->call(__FUNCTION__); } // Bind the photo data now $photo = FD::table('Photo'); $photo->uid = $uid; $photo->type = $type; $photo->user_id = $my->id; $photo->album_id = $album->id; $photo->title = $file['name']; $photo->caption = ''; $photo->ordering = 0; $photo->state = SOCIAL_STATE_PUBLISHED; // Set the creation date alias $photo->assigned_date = FD::date()->toMySQL(); // Cleanup photo title. $photo->cleanupTitle(); // Trigger rules that should occur before a photo is stored $photo->beforeStore($file, $image); // Try to store the photo. $state = $photo->store(); if (!$state) { $view->setMessage(JText::_('COM_EASYSOCIAL_PHOTOS_UPLOAD_ERROR_STORING_DB'), SOCIAL_MSG_ERROR); return $view->call(__FUNCTION__); } // If album doesn't have a cover, set the current photo as the cover. if (!$album->hasCover()) { $album->cover_id = $photo->id; // Store the album $album->store(); } // Get the photos library $photoLib = FD::get('Photos', $image); // Get the storage path for this photo $storageContainer = FD::cleanPath($config->get('photos.storage.container')); $storage = $photoLib->getStoragePath($album->id, $photo->id); $paths = $photoLib->create($storage); // We need to calculate the total size used in each photo (including all the variants) $totalSize = 0; // Create metadata about the photos if ($paths) { foreach ($paths as $type => $fileName) { $meta = FD::table('PhotoMeta'); $meta->photo_id = $photo->id; $meta->group = SOCIAL_PHOTOS_META_PATH; $meta->property = $type; // do not store the container path as this path might changed from time to time $tmpStorage = str_replace('/' . $storageContainer . '/', '/', $storage); $meta->value = $tmpStorage . '/' . $fileName; $meta->store(); // We need to store the photos dimension here list($width, $height, $imageType, $attr) = getimagesize(JPATH_ROOT . $storage . '/' . $fileName); // Set the photo size $totalSize += filesize(JPATH_ROOT . $storage . '/' . $fileName); // Set the photo dimensions $meta = FD::table('PhotoMeta'); $meta->photo_id = $photo->id; $meta->group = SOCIAL_PHOTOS_META_WIDTH; $meta->property = $type; $meta->value = $width; $meta->store(); $meta = FD::table('PhotoMeta'); $meta->photo_id = $photo->id; $meta->group = SOCIAL_PHOTOS_META_HEIGHT; $meta->property = $type; $meta->value = $height; $meta->store(); } } // Set the total photo size $photo->total_size = $totalSize; $photo->store(); // After storing the photo, trigger rules that should occur after a photo is stored $photo->afterStore($file, $image); // Determine if we should create a stream item for this upload $createStream = JRequest::getBool('createStream'); // Add Stream when a new photo is uploaded if ($createStream) { $photo->addPhotosStream('create'); } // Assign badge to user $photo->assignBadge('photos.create', $my->id); if ($isAvatar) { return $photo; } return $view->call(__FUNCTION__, $photo, $paths); }
/** * Gets the storage path for photos folder * * @since 1.0 * @access public * @param string * @return */ public static function getStoragePath($albumId, $photoId, $createFolders = true) { // Get destination folder path. $config = FD::config(); $container = FD::cleanPath($config->get('photos.storage.container')); $storage = JPATH_ROOT . '/' . $container; // Test if the storage folder exists if ($createFolders) { FD::makeFolder($storage); } // Set the storage path to the album $storage = $storage . '/' . $albumId; // If it doesn't exist, create it. if ($createFolders) { FD::makeFolder($storage); } // Create a new folder for the photo $storage = $storage . '/' . $photoId; if ($createFolders) { FD::makeFolder($storage); } // Re-generate the storage path since we do not want to store the JPATH_ROOT $storage = '/' . $container . '/' . $albumId . '/' . $photoId; return $storage; }
/** * Responsible to store the uploaded images. * * @since 1.0 * @access public * @param null * * @author Mark Lee <*****@*****.**> */ public function upload($files) { // Get config object. $config = FD::config(); // Do not proceed if image doesn't exist. if (empty($files) || !isset($files['file'])) { $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_COVERS_FILE_UNAVAILABLE')); return false; } // Get the single file input since the $files is an array. $file = $files['file']; // Get the default avatars storage location. $coversPath = JPATH_ROOT . '/' . FD::cleanPath($config->get('covers.storage.container')); // Test if the avatars path folder exists. If it doesn't we need to create it. if (!FD::makeFolder($coversPath)) { FD::logError(__FILE__, __LINE__, 'DEFAULT_COVERS: Unable to create the path ' . $coversPath); $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_COVERS_UNABLE_TO_CREATE_CONTAINER_FOLDER')); return false; } // Get the defaults avatar path. $defaultsPath = $coversPath . '/' . FD::cleanPath($config->get('covers.storage.default')); // Ensure that the defaults path exist if (!FD::makeFolder($defaultsPath)) { FD::logError(__FILE__, __LINE__, 'DEFAULT_COVERS: Unable to create the path ' . $defaultsPath); $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_COVERS_UNABLE_TO_CREATE_DEFAULT_FOLDER')); return false; } // Get the default avatars storage location for this type. $typePath = $config->get('covers.storage.defaults.' . $this->type); $storagePath = $defaultsPath . '/' . FD::cleanPath($typePath); // Ensure storage path exists. if (!FD::makeFolder($storagePath)) { FD::logError(__FILE__, __LINE__, 'DEFAULT_COVERS: Unable to create the path ' . $storagePath); $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_COVERS_UNABLE_TO_CREATE_DEFAULT_FOLDER')); return false; } // Get the profile id and construct the final path. $idPath = FD::cleanPath($this->uid); $storagePath = $storagePath . '/' . $idPath; // Ensure storage path exists. if (!FD::makeFolder($storagePath)) { FD::logError(__FILE__, __LINE__, 'DEFAULT_COVERS: Unable to create the path ' . $storagePath); $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_COVERS_UNABLE_TO_CREATE_DEFAULT_FOLDER')); return false; } // Get the image library to perform some checks. $image = FD::get('Image'); $image->load($file['tmp_name']); // Test if the image is really a valid image. if (!$image->isValid()) { FD::logError(__FILE__, __LINE__, 'DEFAULT_COVERS: Image uploaded ' . $file['name'] . ' is invalid'); $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_AVATARS_FILE_NOT_IMAGE')); return false; } // Process covers storage. $cover = FD::get('Cover', $image); // Try to create the covers. $sizes = $cover->create($storagePath); // Test if the server returned an error. if ($sizes === false) { FD::logError(__FILE__, __LINE__, 'DEFAULT_COVERS: Error creating covers at ' . $storagePath); $this->setError(JText::_('Sorry, there was some errors when creating the covers.')); return false; } // Assign the values back. foreach ($sizes as $size => $url) { $this->{$size} = $url; } return true; }
/** * Retrieves the user's avatar location * * @author Mark Lee <*****@*****.**> * @since 1.2 * @access public * @param string $size The avatar size to retrieve for. * @return string The avatar uri. */ public function getAvatar($size = SOCIAL_AVATAR_MEDIUM) { $config = FD::config(); // If the avatar size that is being requested is invalid, return default avatar. $default = $this->getDefaultAvatar($size); if (!$this->avatars[$size] || empty($this->avatars[$size])) { // Check if parent exist and call the parent. if ($this->hasParent()) { return $this->getParent()->getAvatar($size); } return $default; } // Get the path to the avatar storage. $container = FD::cleanPath($config->get('avatars.storage.container')); $location = FD::cleanPath($config->get('avatars.storage.' . $this->cluster_type)); // Build the path now. $path = $container . '/' . $location . '/' . $this->id . '/' . $this->avatars[$size]; if ($this->avatarStorage == SOCIAL_STORAGE_JOOMLA) { // Build final storage path. $absolutePath = JPATH_ROOT . '/' . $path; // Detect if this file really exists. if (!JFile::exists($absolutePath)) { return $default; } $uri = rtrim(JURI::root(), '/') . '/' . $path; } else { $storage = FD::storage($this->avatarStorage); $uri = $storage->getPermalink($path); } return $uri; }
/** * Override parent's delete method * * @since 1.0 * @access public * @param string * @return */ public function delete($pk = null) { /* * we need to delete the photos 1st and then only do ao parent::delete to avoid album getting onfindersave issue. */ // Delete the photos from the site first. $photosModel = FD::model('photos'); $photosModel->deleteAlbumPhotos($this->id); // @points: photos.albums.remove // Deduct points for the author for deleting an album $points = FD::points(); $points->assign('photos.albums.remove', 'com_easysocial', $this->uid); // Now, try to delete the folder that houses this photo. $config = FD::config(); $storage = JPATH_ROOT . '/' . FD::cleanPath($config->get('photos.storage.container')); $storage = $storage . '/' . $this->id; jimport('joomla.filesystem.folder'); $exists = JFolder::exists($storage); // Test if the folder really exists first before deleting it. if ($exists) { $state = JFolder::delete($storage); if (!$state) { FD::logError(__FILE__, __LINE__, 'ALBUMS: Unable to delete the photos folder ' . $storage); } } // Delete the record from the database first. $state = parent::delete(); // Delete likes related to the album $likes = FD::get('Likes'); if (!$likes->delete($this->id, SOCIAL_TYPE_ALBUM, 'create')) { FD::logError(__FILE__, __LINE__, 'ALBUMS: Unable to delete the likes for the album ' . $this->id); } // Delete comments related to the album $comments = FD::comments($this->id, SOCIAL_TYPE_ALBUM, 'create', SOCIAL_APPS_GROUP_USER); if (!$comments->delete()) { FD::logError(__FILE__, __LINE__, 'ALBUMS: Unable to delete the comments for the album ' . $this->id); } return $state; }