/** * Handle image upload * * Returns array with uploaded files details or error details */ public function uploadImage($uploadFieldName = self::DEFAULT_FILE_FIELD_NAME, $destFileName = null, $forceOverwrite = false) { global $IP, $wgRequest, $wgUser; wfProfileIn(__METHOD__); $ret = false; // check whether upload is enabled (RT #53714) if (!WikiaPhotoGalleryHelper::isUploadAllowed()) { $ret = array('error' => true, 'message' => wfMsg('uploaddisabled')); wfProfileOut(__METHOD__); return $ret; } $imageName = stripslashes(!empty($destFileName) ? $destFileName : $wgRequest->getFileName($uploadFieldName)); // validate name and content of uploaded photo $nameValidation = $this->checkImageName($imageName, $uploadFieldName); if ($nameValidation == UploadBase::SUCCESS) { // get path to uploaded image $imagePath = $wgRequest->getFileTempName($uploadFieldName); // check if image with this name is already uploaded if ($this->imageExists($imageName) && !$forceOverwrite) { // upload as temporary file $this->log(__METHOD__, "image '{$imageName}' already exists!"); $tempName = $this->tempFileName($wgUser); $title = Title::makeTitle(NS_FILE, $tempName); $localRepo = RepoGroup::singleton()->getLocalRepo(); $file = new FakeLocalFile($title, $localRepo); $file->upload($wgRequest->getFileTempName($uploadFieldName), '', ''); // store uploaded image in GarbageCollector (image will be removed if not used) $tempId = $this->tempFileStoreInfo($tempName); // generate thumbnail (to fit 200x200 box) of temporary file $width = min(WikiaPhotoGalleryHelper::thumbnailMaxWidth, $file->width); $height = min(WikiaPhotoGalleryHelper::thumbnailMaxHeight, $file->height); $thumbnail = $file->transform(array('height' => $height, 'width' => $width)); // split uploaded file name into name + extension (foo-bar.png => foo-bar + png) list($fileName, $extensionsName) = UploadBase::splitExtensions($imageName); $extensionName = !empty($extensionsName) ? end($extensionsName) : ''; $this->log(__METHOD__, 'upload successful'); $ret = array('conflict' => true, 'name' => $imageName, 'nameParts' => array($fileName, $extensionName), 'tempId' => $tempId, 'size' => array('height' => $file->height, 'width' => $file->width), 'thumbnail' => array('height' => $thumbnail->height, 'url' => $thumbnail->url, 'width' => $thumbnail->width)); } else { // use regular MW upload $this->log(__METHOD__, "image '{$imageName}' is new one - uploading as MW file"); $this->log(__METHOD__, "uploading '{$imagePath}' as File:{$imageName}"); // create title and file objects for MW image to create $imageTitle = Title::newFromText($imageName, NS_FILE); $imageFile = new LocalFile($imageTitle, RepoGroup::singleton()->getLocalRepo()); // perform upload $result = $imageFile->upload($imagePath, '', ''); $this->log(__METHOD__, !empty($result->ok) ? 'upload successful' : 'upload failed'); $ret = array('success' => !empty($result->ok), 'name' => $imageName, 'size' => array('height' => !empty($result->ok) ? $imageFile->getHeight() : 0, 'width' => !empty($result->ok) ? $imageFile->getWidth() : 0)); } } else { $reason = $nameValidation; $this->log(__METHOD__, "upload failed - file name is not valid (error #{$reason})"); $ret = array('error' => true, 'reason' => $reason, 'message' => $this->translateError($reason)); } wfProfileOut(__METHOD__); return $ret; }
public function saveSettings($settings, $cityId = null) { global $wgCityId, $wgUser; $cityId = empty($cityId) ? $wgCityId : $cityId; // Verify wordmark length ( CONN-116 ) if (!empty($settings['wordmark-text'])) { $settings['wordmark-text'] = trim($settings['wordmark-text']); } if (empty($settings['wordmark-text'])) { // Do not save wordmark if its empty. unset($settings['wordmark-text']); } else { if (mb_strlen($settings['wordmark-text']) > 50) { $settings['wordmark-text'] = mb_substr($settings['wordmark-text'], 0, 50); } } if (isset($settings['favicon-image-name']) && strpos($settings['favicon-image-name'], 'Temp_file_') === 0) { $temp_file = new LocalFile(Title::newFromText($settings['favicon-image-name'], 6), RepoGroup::singleton()->getLocalRepo()); $file = new LocalFile(Title::newFromText(self::FaviconImageName, 6), RepoGroup::singleton()->getLocalRepo()); $file->upload($temp_file->getPath(), '', ''); $temp_file->delete(''); Wikia::invalidateFavicon(); $settings['favicon-image-url'] = $file->getURL(); $settings['favicon-image-name'] = $file->getName(); $file->repo->forceMaster(); $history = $file->getHistory(1); if (count($history) == 1) { $oldFaviconFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName()); } } if (isset($settings['wordmark-image-name']) && strpos($settings['wordmark-image-name'], 'Temp_file_') === 0) { $temp_file = new LocalFile(Title::newFromText($settings['wordmark-image-name'], 6), RepoGroup::singleton()->getLocalRepo()); $file = new LocalFile(Title::newFromText(self::WordmarkImageName, 6), RepoGroup::singleton()->getLocalRepo()); $file->upload($temp_file->getPath(), '', ''); $temp_file->delete(''); $settings['wordmark-image-url'] = $file->getURL(); $settings['wordmark-image-name'] = $file->getName(); $file->repo->forceMaster(); $history = $file->getHistory(1); if (count($history) == 1) { $oldFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName()); } } if (isset($settings['background-image-name']) && strpos($settings['background-image-name'], 'Temp_file_') === 0) { $temp_file = new LocalFile(Title::newFromText($settings['background-image-name'], 6), RepoGroup::singleton()->getLocalRepo()); $file = new LocalFile(Title::newFromText(self::BackgroundImageName, 6), RepoGroup::singleton()->getLocalRepo()); $file->upload($temp_file->getPath(), '', ''); $temp_file->delete(''); $settings['background-image'] = $file->getURL(); $settings['background-image-name'] = $file->getName(); $settings['background-image-width'] = $file->getWidth(); $settings['background-image-height'] = $file->getHeight(); $imageServing = new ImageServing(null, 120, array("w" => "120", "h" => "65")); $settings['user-background-image'] = $file->getURL(); $settings['user-background-image-thumb'] = wfReplaceImageServer($file->getThumbUrl($imageServing->getCut($file->getWidth(), $file->getHeight(), "origin") . "-" . $file->getName())); $file->repo->forceMaster(); $history = $file->getHistory(1); if (count($history) == 1) { $oldBackgroundFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName()); } } $reason = wfMsg('themedesigner-reason', $wgUser->getName()); // update history if (!empty($GLOBALS[self::WikiFactoryHistory])) { $history = $GLOBALS[self::WikiFactoryHistory]; $lastItem = end($history); $revisionId = intval($lastItem['revision']) + 1; } else { $history = array(); $revisionId = 1; } // #140758 - Jakub // validation // default color values foreach (ThemeDesignerHelper::getColorVars() as $sColorVar => $sDefaultValue) { if (!isset($settings[$sColorVar]) || !ThemeDesignerHelper::isValidColor($settings[$sColorVar])) { $settings[$sColorVar] = $sDefaultValue; } } // update WF variable with current theme settings WikiFactory::setVarByName(self::WikiFactorySettings, $cityId, $settings, $reason); // add entry $history[] = array('settings' => $settings, 'author' => $wgUser->getName(), 'timestamp' => wfTimestampNow(), 'revision' => $revisionId); // limit history size to last 10 changes $history = array_slice($history, -self::HistoryItemsLimit); if (count($history) > 1) { for ($i = 0; $i < count($history) - 1; $i++) { if (isset($oldFaviconFile) && isset($history[$i]['settings']['favicon-image-name'])) { if ($history[$i]['settings']['favicon-image-name'] == self::FaviconImageName) { $history[$i]['settings']['favicon-image-name'] = $oldFaviconFile['name']; $history[$i]['settings']['favicon-image-url'] = $oldFaviconFile['url']; } } if (isset($oldFile) && isset($history[$i]['settings']['wordmark-image-name'])) { if ($history[$i]['settings']['wordmark-image-name'] == self::WordmarkImageName) { $history[$i]['settings']['wordmark-image-name'] = $oldFile['name']; $history[$i]['settings']['wordmark-image-url'] = $oldFile['url']; } } if (isset($oldBackgroundFile) && isset($history[$i]['settings']['background-image-name'])) { if ($history[$i]['settings']['background-image-name'] == self::BackgroundImageName) { $history[$i]['settings']['background-image-name'] = $oldBackgroundFile['name']; } } } } WikiFactory::setVarByName(self::WikiFactoryHistory, $cityId, $history, $reason); }
/** * Accept a request to upload an image either via POST data (user upload) * or via flickr or google / wikimedia.org search. * * @param $src string with value 'upload', 'flickr' or 'wiki' * @return html outputs image details page */ private function uploadImage($src) { global $wgRequest, $wgUser, $IP, $wgOut; $error = ''; $debugInfo = array(); if ($src == 'upload') { $tempname = self::createTempFilename(); $tempUser = self::getTempFileUser(); $file = new LocalFile(Title::newFromText($tempname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo()); $name = $wgRequest->getFileName('wpUploadFile'); $comment = ''; $file->upload($wgRequest->getFileTempName('wpUploadFile'), $comment, '', 0, false, false, $tempUser); $filesize = $file->getSize(); if (!$filesize) { $error = wfMsg('eiu-upload-error'); } } elseif ($src == 'flickr' || $src == 'wiki') { $sourceName = $src == 'flickr' ? 'Flickr' : 'Mediawiki Commons'; $tempname = self::createTempFilename(); $file = new LocalFile(Title::newFromText($tempname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo()); $details = (array) json_decode($wgRequest->getVal('img-details')); $name = $details['name']; // scrape the file using curl $filename = '/tmp/tmp-curl-' . mt_rand(0, 100000000) . '.jpg'; $remoteFile = strlen($details['url_l']) ? $details['url_l'] : $details['url']; $ch = curl_init($remoteFile); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $fp = fopen($filename, 'w'); curl_setopt($ch, CURLOPT_FILE, $fp); $ret = curl_exec($ch); $err = curl_error($ch); curl_close($ch); fclose($fp); if ($err) { $debugInfo['curl'] = $err; } $filesize = @filesize($filename); if ($filesize) { if ($src == 'flickr' || preg_match('@^http://[^/]*flickr@', $details['url'])) { require_once $IP . '/extensions/3rdparty/phpFlickr-2.3.1/phpFlickr.php'; $flickr = new phpFlickr(WH_FLICKR_API_KEY); $photo = $flickr->photos_getInfo($details['photoid']); $err = $flickr->getErrorMsg(); if ($err) { $debugInfo['flickrAPI'] = $err; } $license = $photo['license']; $username = $photo['owner']['username']; $comment = '{{flickr' . intval($license) . '|' . wfEscapeWikiText($details['photoid']) . '|' . wfEscapeWikiText($details['ownerid']) . '|' . wfEscapeWikiText($username) . '}}'; } else { $comment = self::getWPLicenseTag($details['url']); } // finish initializing the $file obj $tempUser = self::getTempFileUser(); $status = $file->upload($filename, '', '', 0, false, false, $tempUser); if (!$status->ok) { $error = wfMsg('eiu-upload-error'); } } else { $error = wfMsg('eiu-download-error', $sourceName); } } if ($error) { $html = EasyTemplate::html('eiu_file_error.tmpl.php', array('error' => $error)); $wgOut->addHTML($html); error_log("file from {$src} error msgs: " . print_r($debugInfo, true)); } else { $mwname = $tempname; $props = array('src' => $src, 'name' => $name, 'mwname' => $mwname, 'is_image' => $file->getMediaType() == 'BITMAP' || $file->getMediaType() == 'DRAWING', 'width' => $file->getWidth(), 'height' => $file->getHeight(), 'upload_file' => $file, 'image_comment' => $comment, 'license' => $wgUser->getOption('image_license'), 'file' => $file); $html = EasyTemplate::html('eiu_image_details.tmpl.php', $props); $wgOut->addHTML($html); } }