/** * Upload an icon for a work * * @param KCommandContext A command context object * @return void */ public function uploadIcon(KCommandContext $context) { $icon = KRequest::get('files.icon', 'raw'); if (!$icon['name']) { return; } //Prepare MediaHelper JLoader::register('MediaHelper', JPATH_ROOT . '/components/com_media/helpers/media.php'); // is it an image if (!MediaHelper::isImage($icon['name'])) { JError::raiseWarning(21, sprintf(JText::_("%s failed to upload because it's not an image."), $icon['name'])); return; } // are we allowed to upload this filetype if (!MediaHelper::canUpload($icon, $error)) { JError::raiseWarning(21, sprintf(JText::_("%s failed to upload because %s"), $icon['name'], lcfirst($error))); return; } $slug = $this->getService('koowa:filter.slug'); $path = 'images/com_portfolio/work/' . $slug->sanitize($context->data->title) . '/icon/'; $ext = JFile::getExt($icon['name']); $name = JFile::makeSafe($slug->sanitize($context->data->title) . '.' . $ext); JFile::upload($icon['tmp_name'], JPATH_ROOT . '/' . $path . $name); $context->data->icon = $path . $name; }
/** * Upload the users avatar * * @param KCommandContext A command context object * @return void */ public function uploadAvatar(KCommandContext $context) { $avatar = KRequest::get('files.avatar', 'raw'); if (!$avatar['name']) { return; } //Prepare MediaHelper JLoader::register('MediaHelper', JPATH_ROOT . '/components/com_media/helpers/media.php'); // is it an image if (!MediaHelper::isImage($avatar['name'])) { JError::raiseWarning(21, sprintf(JText::_("%s failed to upload because it's not an image."), $avatar['name'])); return; } // are we allowed to upload this filetype if (!MediaHelper::canUpload($avatar, $error)) { JError::raiseWarning(21, sprintf(JText::_("%s failed to upload because %s"), $avatar['name'], lcfirst($error))); return; } // @todo put in some max file size checks $path = 'images/com_portfolio/avatars/' . $context->data->user_id . '/'; $ext = JFile::getExt($avatar['name']); $name = JFile::makeSafe($this->getService('koowa:filter.slug')->sanitize($context->data->title) . '.' . $ext); JFile::upload($avatar['tmp_name'], JPATH_ROOT . '/' . $path . $name); $context->data->avatar = $path . $name; }
/** * Returns true if it's an image * * @return boolean True if image, false if not */ public function isImage() { if (!isset($this->_is_image)) { //Dirty hack as MediaHelper::isImage mistakenly thinks jpeg files aren't images $this->_is_image = MediaHelper::isImage(str_replace('.jpeg', '.jpg', $this->name)); } return $this->_is_image; }
public function setAvatar(KCommandContext $context) { //@TODO we shouldn't clear all cache, only the cache for this user if (JFolder::exists(JPATH_ROOT . '/cache/com_ninjaboard/avatars')) { JFolder::delete(JPATH_ROOT . '/cache/com_ninjaboard/avatars'); } //If nothing is uploaded, don't execute if (!KRequest::get('files.avatar.name', 'raw')) { return; } //Prepare MediaHelper JLoader::register('MediaHelper', JPATH_ROOT . '/components/com_media/helpers/media.php'); $person = KFactory::tmp('admin::com.ninjaboard.model.people')->id($context->result->id)->getItem(); $error = null; $errors = array(); $identifier = $this->getIdentifier(); $name = $identifier->type . '_' . $identifier->package; $relative = '/media/' . $name . '/images/avatars/' . $person->id . '/'; $absolute = JPATH_ROOT . $relative; $attachments = array(); $avatar = KRequest::get('files.avatar', 'raw'); //if we are a bmp we cant upload it if (strtolower(JFile::getExt($avatar['name'])) == 'bmp') { JError::raiseWarning(21, sprintf(JText::_('%s failed to upload because this file type is not supported'), $avatar['name'])); return $this; } if (!MediaHelper::canUpload($avatar, $error)) { $message = JText::_("%s failed to upload because %s"); JError::raiseWarning(21, sprintf($message, $avatar['name'], lcfirst($error))); return $this; } if (!MediaHelper::isImage($avatar['name'])) { $message = JText::_("%s failed to upload because it's not an image."); JError::raiseWarning(21, sprintf($message, $avatar['name'])); return $this; } $this->params = KFactory::get('admin::com.ninjaboard.model.settings')->getParams(); $params = $this->params['avatar_settings']; $maxSize = (int) $params['upload_size_limit']; if ($maxSize > 0 && (int) $avatar['size'] > $maxSize) { $message = JText::_("%s failed uploading because it's too large."); JError::raiseWarning(21, sprintf($message, $avatar['name'])); return $this; } $upload = JFile::makeSafe(uniqid(time())) . '.' . JFile::getExt($avatar['name']); JFile::upload($avatar['tmp_name'], $absolute . $upload); $person->avatar = $relative . $upload; $person->avatar_on = gmdate('Y-m-d H:i:s'); $person->save(); return $this; }
/** * Method for uploading files on save * * @param KCommandContext A command context object * @return void */ public function _afterSave(KCommandContext $context) { //Prepare MediaHelper JLoader::register('MediaHelper', JPATH_ROOT . '/components/com_media/helpers/media.php'); $item = $this->getModel()->getItem(); KRequest::set('files.icon', null); foreach (KRequest::get('files', 'raw') as $key => $file) { if ($file['error'] != UPLOAD_ERR_OK || !$file) { continue; } // are we allowed to upload this filetype if (!MediaHelper::canUpload($file, $error)) { JError::raiseWarning(21, sprintf(JText::_("%s failed to upload because %s"), $file['name'], lcfirst($error))); return; } $slug = $this->getService('koowa:filter.slug'); $ext = JFile::getExt($file['name']); $name = $slug->sanitize(JFile::stripExt($file['name'])) . '-' . time() . '.' . $ext; $name = JFile::makeSafe($name); $path = 'images/com_portfolio/work/' . $slug->sanitize($context->data->title) . '/'; // if this is an image, check we are allowed to upload it if (strpos($key, 'image') === false) { $path .= 'files/'; $row = $this->getService('com://admin/portfolio.database.row.file'); } else { if (!MediaHelper::isImage($file['name'])) { JError::raiseWarning(21, sprintf(JText::_("%s failed to upload because it's not an image."), $file['name'])); return; } $path .= 'images/'; $row = $this->getService('com://admin/portfolio.database.row.image'); $this->generateThumb($file, JPATH_ROOT . '/' . $path . 'thumb-' . $name); } JFile::upload($file['tmp_name'], JPATH_ROOT . '/' . $path . $name); $row->setData(array('directory' => $path, 'filename' => $name, 'work_id' => $item->id))->save(); } }
/** * Helper method for uploading a file * * @author Stian Didriksen <*****@*****.**> * @param array $config Configuration array * ->name Where to find the file object in $_FILES * ->to Where the file upload destination * ->rename If given a string, that will be the new name, false to keep the current name * ->randomize Wether to create a random name for the uploaded file or not * ->image Set to true if an additional image validation is needed * ->root The root of the move operation, change this if you need to go up the root * @return array Result of the operation */ protected function _upload(array $config) { $config = new KConfig($config); $identifier = $this->getIdentifier(); $package = $identifier->package; $folder = KInflector::pluralize($identifier->name); $config->append(array('name' => 'image', 'to' => '/images/stories/com_' . $package . '/' . $folder . '/', 'rename' => false, 'randomize' => false, 'image' => false, 'root' => JPATH_ROOT)); //Prepare MediaHelper JLoader::register('MediaHelper', JPATH_ROOT . '/components/com_media/helpers/media.php'); $error = null; $file = KRequest::get('files.' . $config->name, 'raw'); if (!MediaHelper::canUpload($file, $error)) { $message = JText::_("%s failed to upload because %s"); JError::raiseWarning(21, sprintf($message, $file['name'], lcfirst($error))); return array(); } if ($config->image && !MediaHelper::isImage($file['name'])) { $message = JText::_("%s failed to upload because it's not an image."); JError::raiseWarning(21, sprintf($message, $file['name'])); return array(); } $name = $config->rename ? $config->rename : $file['name']; $upload = JFile::makeSafe($config->randomize ? uniqid(time()) . '.' . JFile::getExt($name) : $name); $relative = $config->to . $upload; $absolute = $config->root . $relative; JFile::upload($file['tmp_name'], $absolute); return array('filename' => $upload, 'filepath' => array('relative' => $relative, 'absolute' => $absolute)); }