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; }
/** * Perform the documentation import. * @param $filename Import filename. */ protected function doImport( $filename ) { global $wgUser; $this->mFilename = $filename; $this->mFileHandle = fopen( $this->mFilename, 'rb' ); if ( !$this->mFileHandle ) { throw new MWException( __CLASS__ . ": Failed to open {$this->mFilename}." ); } $this->mDB = wfGetDB( DB_MASTER ); $this->mSource = new ImportStreamSource( $this->mFileHandle ); $this->mImporter = new WikiImporter( $this->mSource ); $this->mUser = User::newFromName( wfMsgForContent( 'wikilog-auto' ), false ); $wgUser = $this->mUser; $this->mComment = wfMsgForContent( 'wikilog-doc-import-comment' ); $this->mTimestamp = wfTimestampNow(); $this->mImporter->setRevisionCallback( array( &$this, 'handleRevision' ) ); $basefn = wfBasename( $this->mFilename ); $this->output( "+ Importing {$basefn}...\n" ); $this->mStatPages = 0; $this->mStatFiles = 0; $this->mImporter->doImport(); $this->output( " + Done. {$this->mStatPages} page(s) and {$this->mStatFiles} file(s) imported.\n" ); fclose( $this->mFileHandle ); }