/** * Set the avatar for specific application. Caller must have a database table * that is named after the appType. E.g, users should have jos_community_users * * @param appType Application type. ( users , groups ) * @param path The relative path to the avatars. * @param type The type of Image, thumb or avatar. * **/ function setImage($id, $path, $type = 'thumb') { CError::assert($id, '', '!empty', __FILE__, __LINE__); CError::assert($path, '', '!empty', __FILE__, __LINE__); $db =& $this->getDBO(); // Fix the back quotes $path = JString::str_ireplace('\\', '/', $path); $type = JString::strtolower($type); // Test if the record exists. $query = 'SELECT ' . $db->nameQuote($type) . ' FROM ' . $db->nameQuote('#__community_users') . 'WHERE ' . $db->nameQuote('userid') . '=' . $db->Quote($id); $db->setQuery($query); $oldFile = $db->loadResult(); if ($db->getErrorNum()) { JError::raiseError(500, $db->stderr()); } if (!$oldFile) { $query = 'UPDATE ' . $db->nameQuote('#__community_users') . ' ' . 'SET ' . $db->nameQuote($type) . '=' . $db->Quote($path) . ' ' . 'WHERE ' . $db->nameQuote('userid') . '=' . $db->Quote($id); $db->setQuery($query); $db->query($query); if ($db->getErrorNum()) { JError::raiseError(500, $db->stderr()); } } else { $query = 'UPDATE ' . $db->nameQuote('#__community_users') . ' ' . 'SET ' . $db->nameQuote($type) . '=' . $db->Quote($path) . ' ' . 'WHERE ' . $db->nameQuote('userid') . '=' . $db->Quote($id); $db->setQuery($query); $db->query($query); if ($db->getErrorNum()) { JError::raiseError(500, $db->stderr()); } // If old file is default_thumb or default, we should not remove it. // Need proper way to test it if (!Jstring::stristr($oldFile, 'components/com_community/assets/default.jpg') && !Jstring::stristr($oldFile, 'components/com_community/assets/default_thumb.jpg')) { // File exists, try to remove old files first. $oldFile = JString::str_ireplace('/', DS, $oldFile); JFile::delete($oldFile); } } }
function uploadAndSetImage($file, $id, $what) { $mainframe =& JFactory::getApplication(); CFactory::load('helpers', 'image'); $config = CFactory::getConfig(); $uploadLimit = (double) $config->get('maxuploadsize'); $uploadLimit = $uploadLimit * 1024 * 1024; // @rule: Limit image size based on the maximum upload allowed. if (filesize($file['tmp_name']) > $uploadLimit) { $mainframe->enqueueMessage(XiptText::_('IMAGE_FILE_SIZE_EXCEEDED'), 'error'); $mainframe->redirect(CRoute::_('index.php?option=com_xipt&view=profiletypes&task=edit&id=' . $id, false)); } if (!cValidImage($file['tmp_name'])) { $mainframe->enqueueMessage(XiptText::_('IMAGE_FILE_NOT_SUPPORTED'), 'error'); } else { switch ($what) { case 'avatar': $imageMaxWidth = AVATAR_WIDTH; $thumbWidth = AVATAR_WIDTH_THUMB; $thumbHeight = AVATAR_HEIGHT_THUMB; $imgPrefix = 'avatar_'; break; case 'watermark': $imageMaxWidth = WATERMARK_WIDTH; $thumbWidth = WATERMARK_WIDTH_THUMB; $thumbHeight = WATERMARK_HEIGHT_THUMB; $imgPrefix = 'watermark_'; break; } $storage = PROFILETYPE_AVATAR_STORAGE_PATH; $storageImage = $storage . DS . $imgPrefix . $id . cImageTypeToExt($file['type']); $storageThumbnail = $storage . DS . $imgPrefix . $id . '_thumb' . cImageTypeToExt($file['type']); $image = PROFILETYPE_AVATAR_STORAGE_REFERENCE_PATH . DS . $imgPrefix . $id . cImageTypeToExt($file['type']); //$thumbnail = PROFILETYPE_AVATAR_STORAGE_REFERENCE_PATH . $imgPrefix . $id.'_thumb' . cImageTypeToExt( $file['type'] ); //here check if folder exist or not. if not then create it. if (JFolder::exists($storage) == false) { JFolder::create($storage); } // Only resize when the width exceeds the max. if (!cImageResizePropotional($file['tmp_name'], $storageImage, $file['type'], $imageMaxWidth)) { $mainframe->enqueueMessage(XiptText::sprintf('COM_XIPT_ERROR_MOVING_UPLOADED_FILE', $storageImage), 'error'); } // Generate thumbnail if (!cImageCreateThumb($file['tmp_name'], $storageThumbnail, $file['type'], $thumbWidth, $thumbHeight)) { $mainframe->enqueueMessage(XiptText::sprintf('COM_XIPT_ERROR_MOVING_UPLOADED_FILE', $storageThumbnail), 'error'); } $oldFile = XiptLibProfiletypes::getProfiletypeData($id, $what); // If old file is default_thumb or default, we should not remove it. if (!Jstring::stristr($oldFile, DEFAULT_AVATAR) && !Jstring::stristr($oldFile, DEFAULT_AVATAR_THUMB) && $oldFile != $image && $oldFile != '') { // File exists, try to remove old files first. $oldFile = JString::str_ireplace('/', DS, $oldFile); //only delete when required if (JFile::exists($oldFile)) { JFile::delete($oldFile); } } //here due to extension mismatch we can break the functionality of avatar if ($what === 'avatar') { /* No need to update thumb here , script will update both avatar and thumb */ //$newThumb = XiptHelperImage::getThumbAvatarFromFull($newAvatar); $oldAvatar = XiptLibProfiletypes::getProfiletypeData($id, 'avatar'); $allUsers = XiptLibProfiletypes::getAllUsers($id); if ($allUsers) { $filter[] = 'avatar'; $newData['avatar'] = $image; $oldData['avatar'] = $oldAvatar; foreach ($allUsers as $userid) { XiptLibProfiletypes::updateUserProfiletypeFilteredData($userid, $filter, $oldData, $newData); } } } //now update profiletype with new avatar or watermark if (!XiptFactory::getInstance('profiletypes', 'model')->save(array($what => XiptHelperUtils::getUrlpathFromFilePath($image)), $id)) { XiptError::raiseError(__CLASS__ . '.' . __LINE__, XiptText::_("ERROR_IN_DATABASE")); } } }