public function getFile($name = null) { // Check if post_max_size is exceeded. if (empty($_FILES) && empty($_POST)) { return FD::exception('COM_EASYSOCIAL_EXCEPTION_UPLOAD_POST_SIZE'); } // Get the file if (empty($name)) { $name = $this->name; } $file = JRequest::getVar($name, '', 'FILES'); // Check for invalid file object if (empty($file)) { return FD::exception('COM_EASYSOCIAL_EXCEPTION_UPLOAD_NO_OBJECT'); } // If there's an error in this file if ($file['error']) { return FD::exception($file, SOCIAL_EXCEPTION_UPLOAD); } // Check if file exceeds max upload filesize $maxsize = FD::math()->convertBytes($this->maxsize); if ($maxsize > 0 && $file['size'] > $maxsize) { return FD::exception(JText::sprintf('COM_EASYSOCIAL_EXCEPTION_UPLOAD_MAX_SIZE', FD::math()->convertUnits($maxsize, 'B', 'MB', false, true))); } // Return file return $file; }
/** * Removes a file from a group. * * @since 1.2 * @access public * @return mixed True if success, exception if false. */ public function removeFile() { // Check if the user has access to delete files from this group if (!$this->group->isMember()) { return FD::exception(JText::_('COM_EASYSOCIAL_EXPLORER_NO_ACCESS_TO_DELETE')); } // Get the file id $ids = JRequest::getInt('id'); $ids = FD::makeArray($ids); foreach ($ids as $id) { $file = FD::table('File'); $file->load($id); if (!$id || !$file->id) { return FD::exception(JText::_('COM_EASYSOCIAL_EXPLORER_INVALID_FILE_ID_PROVIDED')); } $state = $file->delete(); if (!$state) { return FD::exception(JText::_($file->getError())); } } return true; }
/** * Service Hook for explorer * * @since 1.3 * @access public * @param string * @return */ public function hook() { // Check for request forgeries FD::checkToken(); // Require the user to be logged in FD::requireLogin(); // Get the event object $uid = $this->input->get('uid', 0, 'int'); $type = $this->input->get('type', '', 'cmd'); // Load up the explorer library $explorer = FD::explorer($uid, $type); // Determine if the viewer can really view items if (!$explorer->hook('canViewItem')) { return $this->view->call(__FUNCTION__); } // Get the hook $hook = $this->input->get('hook', '', 'cmd'); // Get the result $result = $explorer->hook($hook); $exception = FD::exception('Folder retrieval successful', SOCIAL_MSG_SUCCESS); return $this->view->call(__FUNCTION__, $exception, $result); }
public function explorer() { $ajax = FD::ajax(); // Note: This is a just a sample code to mock requests. $hook = JRequest::getCmd('hook'); switch ($hook) { case 'getFolders': $start = JRequest::getInt('start'); $limit = JRequest::getInt('limit'); $folders = $this->folders($start, $limit); // // Sample code to use // $explorer = FD::explorer( 1 , SOCIAL_TYPE_GROUP ); // $result = $explorer->hook( $hook ); // Take all files and build file map $files = $this->files(0, 1500); $map = array(); foreach ($files as $file) { $map[] = $file->id; } $folders[0]->map = $map; $ajax->resolve($folders); break; case 'addFolder': $name = JRequest::getCmd('name'); if (empty($name)) { $exception = FD::exception('Invalid name provided'); $ajax->reject($exception->toArray()); } else { $data = array('id' => mt_rand(10, 999), 'name' => $name, 'count' => 0, 'data' => (object) array(), 'settings' => (object) array()); $ajax->resolve($data); } break; case 'removeFolder': // Mock error removal by adding error=1 $error = JRequest::getBool('error', false); if ($error) { $exception = FD::exception('Unable to remove folder.'); $ajax->reject($exception); } else { $exception = FD::exception('Remove successful!', SOCIAL_MSG_SUCCESS); $ajax->resolve($exception); } break; case 'getFiles': $id = JRequest::getInt('id'); $start = JRequest::getInt('start'); $limit = JRequest::getInt('limit'); switch ($id) { // 1500 files case 0: $files = $this->files($start, $limit); $ajax->resolve($files); break; // 0 files // 0 files case 1: $ajax->resolve(array()); break; // 1 files // 1 files case 2: $files = $this->smallfiles($start, $limit); $ajax->resolve($files); break; } break; case 'addFile': // Define uploader options $options = array('name' => 'file', 'maxsize' => '32M'); // Get uploaded file $file = FD::uploader($options)->getFile(); // If there was an error getting uploaded file, stop. if ($file instanceof SocialException) { $ajax->reject($file->toArray()); } // Get filename $name = $file['name']; $data = array('id' => mt_rand(1501, 2000), 'name' => $name, 'folder' => 0, 'count' => 0, 'data' => (object) array(), 'settings' => (object) array()); $ajax->resolve($data); break; case 'removeFile': // Mock error removal by adding error=1 $error = JRequest::getBool('error', false); if ($error) { $exception = FD::exception('Unable to remove file.'); $ajax->reject($exception); } else { $exception = FD::exception('Remove successful!', SOCIAL_MSG_SUCCESS); $ajax->resolve($exception); } break; default: $exception = FD::exception('Invalid hook provided.'); $ajax->reject($exception->toArray()); break; } $ajax->send(); }
/** * Allows caller to upload files * * @since 1.2 * @access public * @param string * @return */ public function addFile($title = null) { if (!$this->hasWriteAccess()) { return FD::exception(JText::_('COM_EASYSOCIAL_EXPLORER_NO_ACCESS_TO_UPLOAD')); } // Ensure that the storage path really exists on the site FD::makeFolder($this->storagePath); // Get the maximum size allowed from the child $max = $this->getMaxSize(); // Define uploader options $options = array('name' => 'file', 'maxsize' => $max); // Get uploaded file from $_FILE $file = FD::uploader($options)->getFile(); // If there was an error getting uploaded file, stop. if ($file instanceof SocialException) { return $file; } // Get filename $name = $file['name']; // Get the folder to store this item to. $collectionId = JRequest::getInt('id', 0); $table = FD::table('File'); $table->name = $name; $table->collection_id = $collectionId; $table->hits = 0; $table->hash = md5('tmp'); $table->uid = $this->uid; $table->type = $this->type; $table->created = JFactory::getDate()->toSql(); $table->user_id = FD::user()->id; $table->size = filesize($file['tmp_name']); $table->mime = $file['type']; $table->state = SOCIAL_STATE_PUBLISHED; $table->storage = SOCIAL_STORAGE_JOOMLA; // Try to store the data on the database. $table->store(); // Now we need to really upload the file. $state = $table->storeWithFile($file); // Format the data now $result = $this->format(array($table)); return $result[0]; }