/** * 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; }
/** * log starter images to upload_log table * * @access private * @author Piotr Molski (moli) * */ private function addStarterImagesToUploadLog() { global $wgEnableUploadInfoExt; wfProfileIn(__METHOD__); if (empty($wgEnableUploadInfoExt)) { wfProfileOut(__METHOD__); return; } $files = array("Wiki.png", "Favicon.ico"); $dbr = wfGetDB(DB_SLAVE); $oRes = $dbr->select(array('page'), array('page_title'), array('page_namespace' => NS_IMAGE), __METHOD__); while ($oRow = $dbr->fetchObject($oRes)) { if (!in_array($oRow->page_title, $files)) { $files[] = $oRow->page_title; } } $dbr->freeResult($oRes); $loop = 0; foreach ($files as $image) { $oTitle = Title::newFromText($image, NS_IMAGE); $file = wfLocalFile($oTitle); if (is_object($file)) { # add to upload_log UploadInfo::log($oTitle, $file->getPath(), $file->getRel(), "", "u"); $loop++; } } Wikia::log(__METHOD__, "info", "added {$loop} images to upload_log table"); wfProfileOut(__METHOD__); }
function uploadAvatar($oMasthead, $mUser, $nypath, $city_id) { global $wgTmpDirectory, $UNLINK_OLD, $TEST; $result = false; $filename = wfBasename($nypath); $sFilePath = $oMasthead->getFullPath(); $user_id = $mUser->getId(); $__path = str_replace($filename, "", $nypath); $__files = $__path . "/*_" . $user_id . "_*"; if ( $TEST == 1 ) { saveDataInDB($user_id, $city_id, $__files, $sFilePath); return true; } if( !isset( $wgTmpDirectory ) || !is_dir( $wgTmpDirectory ) ) { $wgTmpDirectory = '/tmp'; } $sTmpFile = sprintf("%s/avatars/%s", $wgTmpDirectory, $filename); if ( copy( $nypath, $sTmpFile ) ) { $iFileSize = filesize($sTmpFile); if ( empty( $iFileSize ) ) { __log("Empty file {$input} reported size {$iFileSize}"); return $result; } __log("Temp file set to {$sTmpFile}"); if ( file_exists( $sTmpFile ) ) { $aImgInfo = getimagesize($sTmpFile); $aAllowMime = array( 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg' ); if (!in_array($aImgInfo['mime'], $aAllowMime)) { __log("Invalid mime type " . $aImgInfo['mime'] . ", allowed: " . implode(',', $aAllowMime)); return $result; } 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(); /** * calculate new image size - should be 100 x 100 */ $iImgW = AVATAR_DEFAULT_WIDTH; $iImgH = AVATAR_DEFAULT_HEIGHT; /* WIDTH > HEIGHT */ if ( $aOrigSize['width'] > $aOrigSize['height'] ) { $iImgH = $iImgW * ( $aOrigSize['height'] / $aOrigSize['width'] ); } /* HEIGHT > WIDTH */ if ( $aOrigSize['width'] < $aOrigSize['height'] ) { $iImgW = $iImgH * ( $aOrigSize['width'] / $aOrigSize['height'] ); } /* empty image with thumb size on white background */ $oImg = @imagecreatetruecolor($iImgW, $iImgH); $white = imagecolorallocate($oImg, 255, 255, 255); imagefill($oImg, 0, 0, $white); imagecopyresampled( $oImg, $oImgOrig, floor ( ( AVATAR_DEFAULT_WIDTH - $iImgW ) / 2 ) /*dx*/, floor ( ( AVATAR_DEFAULT_HEIGHT - $iImgH ) / 2 ) /*dy*/, 0 /*sx*/, 0 /*sy*/, $iImgW /*dw*/, $iImgH /*dh*/, $aOrigSize['width']/*sw*/, $aOrigSize['height']/*sh*/ ); /** * save to new file ... but create folder for it first */ if ( !is_dir( dirname( $sFilePath ) ) && !wfMkdirParents( dirname( $sFilePath ) ) ) { __log( sprintf("Cannot create directory %s", dirname( $sFilePath ) ) ); return $result; } if ( !imagepng( $oImg, $sFilePath ) ) { __log( sprintf("Cannot save png Avatar: %s", $sFilePath ) ); return $result; } /* remove tmp file */ imagedestroy($oImg); $sUserText = $mUser->getName(); $mUserPage = Title::newFromText( $sUserText, NS_USER ); if ( $ADD_TO_USER_LOG == 1 ) { $oLogPage = new LogPage( AVATAR_LOG_NAME ); $oLogPage->addEntry( 'avatar_chn', $mUserPage, ''); } unlink($sTmpFile); /** * notify image replication system */ $newAvatarPath = $oMasthead->getLocalPath(); __log(sprintf("New avatar path -> %s", $newAvatarPath)); global $wgEnableUploadInfoExt; if( $wgEnableUploadInfoExt ) { UploadInfo::log( $mUserPage, $sFilePath, $newAvatarPath ); } $errorNo = UPLOAD_ERR_OK; if ( !empty($newAvatarPath) ) { /* set user option */ __log(sprintf("Set %s = %s in user preferences", AVATAR_USER_OPTION_NAME, $newAvatarPath)); $mUser->setOption( AVATAR_USER_OPTION_NAME, $newAvatarPath ); $mUser->saveSettings(); } if ( $UNLINK_OLD ) { $files = glob($__files); if ( $files ) { foreach ( $files as $file ) { __log("Unlink $file"); unlink($file); } } } saveDataInDB($user_id, $city_id, $__files, $sFilePath); $result = true; } else { __log(sprintf("File %s doesn't exist", $sTmpFile )); return $result; } } else { __log("Cannot copy $nypath to $sTmpFile"); return $result; } return $result; }