예제 #1
0
파일: helper.php 프로젝트: pguilford/vcomcc
 public static function uploadAvatar($profile, $isFromBackend = false)
 {
     jimport('joomla.utilities.error');
     jimport('joomla.filesystem.file');
     jimport('joomla.filesystem.folder');
     $my = JFactory::getUser();
     $mainframe = JFactory::getApplication();
     $config = DiscussHelper::getConfig();
     $avatar_config_path = $config->get('main_avatarpath');
     $avatar_config_path = rtrim($avatar_config_path, '/');
     $avatar_config_path = JString::str_ireplace('/', DIRECTORY_SEPARATOR, $avatar_config_path);
     $upload_path = JPATH_ROOT . '/' . $avatar_config_path;
     $rel_upload_path = $avatar_config_path;
     $err = null;
     $file = JRequest::getVar('Filedata', '', 'files', 'array');
     // Check whether the upload folder exist or not. if not create it.
     if (!JFolder::exists($upload_path)) {
         if (!JFolder::create($upload_path)) {
             // Redirect
             if (!$isFromBackend) {
                 DiscussHelper::setMessageQueue(JText::_('COM_EASYDISCUSS_FAILED_TO_CREATE_UPLOAD_FOLDER'), 'error');
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=profile', false));
             } else {
                 // From backend
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=users', false), JText::_('COM_EASYDISCUSS_FAILED_TO_CREATE_UPLOAD_FOLDER'), 'error');
             }
             return;
         }
     }
     // Makesafe on the file
     $date = DiscussHelper::getDate();
     $file_ext = DiscussImageHelper::getFileExtention($file['name']);
     $file['name'] = $my->id . '_' . JFile::makeSafe(md5($file['name'] . $date->toMySQL())) . '.' . strtolower($file_ext);
     if (isset($file['name'])) {
         $target_file_path = $upload_path;
         $relative_target_file = $rel_upload_path . '/' . $file['name'];
         $target_file = JPath::clean($target_file_path . '/' . JFile::makeSafe($file['name']));
         $original = JPath::clean($target_file_path . '/' . 'original_' . JFile::makeSafe($file['name']));
         $isNew = false;
         require_once DISCUSS_HELPERS . '/image.php';
         require_once DISCUSS_CLASSES . '/simpleimage.php';
         if (!DiscussImageHelper::canUpload($file, $err)) {
             if (!$isFromBackend) {
                 DiscussHelper::setMessageQueue(JText::_($err), 'error');
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=profile&layout=edit', false));
             } else {
                 // From backend
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=users', false), JText::_($err), 'error');
             }
             return;
         }
         if (0 != (int) $file['error']) {
             if (!$isFromBackend) {
                 DiscussHelper::setMessageQueue($file['error'], 'error');
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=profile&layout=edit', false));
             } else {
                 //from backend
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=users', false), $file['error'], 'error');
             }
             return;
         }
         //rename the file 1st.
         $oldAvatar = $profile->avatar;
         $tempAvatar = '';
         if ($oldAvatar != 'default.png') {
             $session = JFactory::getSession();
             $sessionId = $session->getToken();
             $fileExt = JFile::getExt(JPath::clean($target_file_path . '/' . $oldAvatar));
             $tempAvatar = JPath::clean($target_file_path . '/' . $sessionId . '.' . $fileExt);
             // Test if old original file exists.
             if (JFile::exists($target_file_path . '/original_' . $oldAvatar)) {
                 JFile::delete($target_file_path . '/original_' . $oldAvatar);
             }
             JFile::move($target_file_path . '/' . $oldAvatar, $tempAvatar);
         } else {
             $isNew = true;
         }
         if (JFile::exists($target_file)) {
             if ($oldAvatar != 'default.png') {
                 //rename back to the previous one.
                 JFile::move($tempAvatar, $target_file_path . '/' . $oldAvatar);
             }
             if (!$isFromBackend) {
                 DiscussHelper::setMessageQueue(JText::sprintf('COM_EASYDISCUSS_FILE_ALREADY_EXISTS', $relative_target_file), 'error');
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=profile', false));
             } else {
                 //from backend
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=users', false), JText::sprintf('COM_EASYDISCUSS_FILE_ALREADY_EXISTS', $relative_target_file), 'error');
             }
             return;
         }
         if (JFolder::exists($target_file)) {
             if ($oldAvatar != 'default.png') {
                 //rename back to the previous one.
                 JFile::move($tempAvatar, $target_file_path . '/' . $oldAvatar);
             }
             if (!$isFromBackend) {
                 DiscussHelper::setMessageQueue(JText::sprintf('COM_EASYDISCUSS_FILE_ALREADY_EXISTS', $relative_target_file), 'error');
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=profile', false));
             } else {
                 //from backend
                 $mainframe->redirect(DiscussRouter::_('index.php?option=com_easydiscuss&view=users', false), JText::sprintf('COM_EASYDISCUSS_FILE_ALREADY_EXISTS', $relative_target_file), 'error');
             }
             return;
         }
         $configImageWidth = $config->get('layout_avatarwidth', 160);
         $configImageHeight = $config->get('layout_avatarheight', 160);
         $originalImageWidth = $config->get('layout_originalavatarwidth', 400);
         $originalImageHeight = $config->get('layout_originalavatarheight', 400);
         // Copy the original image files over
         $image = new SimpleImage();
         $image->load($file['tmp_name']);
         //$image->resizeToFill( $originalImageWidth , $originalImageHeight );
         // By Kevin Lankhorst
         $image->resizeOriginal($originalImageWidth, $originalImageHeight, $configImageWidth, $configImageHeight);
         $image->save($original, $image->image_type);
         unset($image);
         $image = new SimpleImage();
         $image->load($file['tmp_name']);
         $image->resizeToFill($configImageWidth, $configImageHeight);
         $image->save($target_file, $image->image_type);
         //now we update the user avatar. If needed, we remove the old avatar.
         if ($oldAvatar != 'default.png') {
             if (JFile::exists($tempAvatar)) {
                 JFile::delete($tempAvatar);
             }
         }
         return JFile::makeSafe($file['name']);
     } else {
         return 'default.png';
     }
 }