/**
  * @brief Validates file upload (whenever it's regular upload or by-url upload) and sets status and errorMsg
  *
  * @param integer $errorNo variable being checked
  *
  * @return String
  * @author Andrzej 'nAndy' Ɓukaszewski
  */
 private function validateUpload($errorNo)
 {
     switch ($errorNo) {
         case UPLOAD_ERR_NO_FILE:
             return wfMessage('user-identity-box-avatar-error-nofile')->escaped();
             break;
         case UPLOAD_ERR_CANT_WRITE:
             return wfMessage('user-identity-box-avatar-error-cantwrite')->escaped();
             break;
         case UPLOAD_ERR_FORM_SIZE:
             return wfMessage('user-identity-box-avatar-error-size', (int) (self::AVATAR_MAX_SIZE / 1024))->escaped();
             break;
         case UPLOAD_ERR_EXTENSION:
             return wfMessage('userprofilepage-avatar-error-type', $this->wg->Lang->listToText(ImageOperationsHelper::getAllowedMime()))->escaped();
             break;
         case ImageOperationsHelper::UPLOAD_ERR_RESOLUTION:
             return wfMessage('userprofilepage-avatar-error-resolution')->escaped();
             break;
         default:
             return wfMessage('user-identity-box-avatar-error')->escaped();
     }
 }
Example #2
0
 /**
  * Given the filename of the temporary image, post-process the image to be the right size, format, etc.
  *
  * Returns an error code if there is an error or UPLOAD_ERR_OK if there were no errors.
  *
  * @param String $sTmpFile -- the full path to the temporary image file (will be deleted after processing).
  * @param $errorNo -- optional initial error-code state.
  * @param $errorMsg -- optional string containing details on what went wrong if there is an UPLOAD_ERR_EXTENSION.
  */
 private function postProcessImageInternal($sTmpFile, &$errorNo = UPLOAD_ERR_OK, &$errorMsg = '')
 {
     wfProfileIn(__METHOD__);
     $aImgInfo = getimagesize($sTmpFile);
     /**
      * check if mimetype is allowed
      */
     $aAllowMime = array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg');
     if (!in_array($aImgInfo['mime'], $aAllowMime)) {
         global $wgLang;
         // This seems to be the most appropriate error message to describe that the image type is invalid.
         // Available error codes; http://php.net/manual/en/features.file-upload.errors.php
         $errorNo = UPLOAD_ERR_EXTENSION;
         $errorMsg = wfMsg('blog-avatar-error-type', $aImgInfo['mime'], $wgLang->listToText($aAllowMime));
         //				Wikia::log( __METHOD__, 'mime', $errorMsg);
         wfProfileOut(__METHOD__);
         return $errorNo;
     }
     switch ($aImgInfo['mime']) {
         case 'image/gif':
             $oImgOrig = @imagecreatefromgif($sTmpFile);
             break;
         case 'image/pjpeg':
         case 'image/jpeg':
         case 'image/jpg':
             $oImgOrig = @imagecreatefromjpeg($sTmpFile);
             break;
         case 'image/x-png':
         case 'image/png':
             $oImgOrig = @imagecreatefrompng($sTmpFile);
             break;
     }
     $aOrigSize = array('width' => $aImgInfo[0], 'height' => $aImgInfo[1]);
     /**
      * generate new image to png format
      */
     $addedAvatars = array();
     $sFilePath = $this->getFullPath();
     $ioh = new ImageOperationsHelper();
     $oImg = $ioh->postProcess($oImgOrig, $aOrigSize);
     /**
      * save to new file ... but create folder for it first
      */
     if (!is_dir(dirname($sFilePath)) && !wfMkdirParents(dirname($sFilePath))) {
         wfDebugLog("avatar", __METHOD__ . sprintf(": Cannot create directory %s", dirname($sFilePath)), true);
         wfProfileOut(__METHOD__);
         return UPLOAD_ERR_CANT_WRITE;
     }
     if (!imagepng($oImg, $sFilePath)) {
         wfDebugLog("avatar", __METHOD__ . ": Cannot save png Avatar: {$sFilePath}", true);
         $errorNo = UPLOAD_ERR_CANT_WRITE;
     } else {
         /* remove tmp file */
         imagedestroy($oImg);
         $sUserText = $this->mUser->getName();
         $mUserPage = Title::newFromText($sUserText, NS_USER);
         $oLogPage = new LogPage(AVATAR_LOG_NAME);
         $oLogPage->addEntry('avatar_chn', $mUserPage, '');
         unlink($sTmpFile);
         /**
          * notify image replication system
          */
         global $wgEnableUploadInfoExt;
         if ($wgEnableUploadInfoExt) {
             UploadInfo::log($mUserPage, $sFilePath, $this->getLocalPath());
         }
         // remove generated thumbnails
         $this->purgeThumbnails();
         $errorNo = UPLOAD_ERR_OK;
     }
     return $errorNo;
 }