private function execute3rdPartyVideo() { if (empty($this->mParams['videoId'])) { $this->dieUsageMsg('The videoId parameter must be set'); } $duplicate = $this->getVideoDuplicate($this->mParams['provider'], $this->mParams['videoId']); if ($duplicate) { return array('title' => $duplicate->getTitle()->getText()); } else { $uploader = new VideoFileUploader(); $title = $uploader->getUniqueTitle(wfStripIllegalFilenameChars($this->mParams['title'])); $uploader->setProvider($this->mParams['provider']); $uploader->setVideoId($this->mParams['videoId']); $uploader->setTargetTitle($title->getBaseText()); $uploader->upload($title); return array('title' => $title->getText()); } }
private function execute3rdPartyVideo() { if (empty($this->mParams['videoId'])) { $this->dieUsageMsg('The videoId parameter must be set'); } $duplicate = $this->getVideoDuplicate($this->mParams['provider'], $this->mParams['videoId']); if ($duplicate) { return array('title' => $duplicate->getTitle()->getText()); } else { $uploader = new VideoFileUploader(); $title = $uploader->getUniqueTitle(wfStripIllegalFilenameChars($this->mParams['title'])); // https://wikia-inc.atlassian.net/browse/VE-1819 if (!$title) { WikiaLogger::instance()->debug('ApiAddMediaPermanent', array('title' => $this->mParams['title'])); } $uploader->setProvider($this->mParams['provider']); $uploader->setVideoId($this->mParams['videoId']); $uploader->setTargetTitle($title->getBaseText()); $uploader->upload($title); return array('title' => $title->getText()); } }
/** * Returns the title of the file to be uploaded. Sets mTitleError in case * the name was illegal. * * @return Title The title of the file or null in case the name was illegal */ public function getTitle() { if ($this->mTitle !== false) { return $this->mTitle; } /* Assume that if a user specified File:Something.jpg, this is an error * and that the namespace prefix needs to be stripped of. */ $title = Title::newFromText($this->mDesiredDestName); if ($title && $title->getNamespace() == NS_FILE) { $this->mFilteredName = $title->getDBkey(); } else { $this->mFilteredName = $this->mDesiredDestName; } # oi_archive_name is max 255 bytes, which include a timestamp and an # exclamation mark, so restrict file name to 240 bytes. if (strlen($this->mFilteredName) > 240) { $this->mTitleError = self::FILENAME_TOO_LONG; $this->mTitle = null; return $this->mTitle; } /** * Chop off any directories in the given filename. Then * filter out illegal characters, and try to make a legible name * out of it. We'll strip some silently that Title would die on. */ $this->mFilteredName = wfStripIllegalFilenameChars($this->mFilteredName); /* Normalize to title form before we do any further processing */ $nt = Title::makeTitleSafe(NS_FILE, $this->mFilteredName); if (is_null($nt)) { $this->mTitleError = self::ILLEGAL_FILENAME; $this->mTitle = null; return $this->mTitle; } $this->mFilteredName = $nt->getDBkey(); /** * We'll want to blacklist against *any* 'extension', and use * only the final one for the whitelist. */ list($partname, $ext) = $this->splitExtensions($this->mFilteredName); if (count($ext)) { $this->mFinalExtension = trim($ext[count($ext) - 1]); } else { $this->mFinalExtension = ''; # No extension, try guessing one $magic = MimeMagic::singleton(); $mime = $magic->guessMimeType($this->mTempPath); if ($mime !== 'unknown/unknown') { # Get a space separated list of extensions $extList = $magic->getExtensionsForType($mime); if ($extList) { # Set the extension to the canonical extension $this->mFinalExtension = strtok($extList, ' '); # Fix up the other variables $this->mFilteredName .= ".{$this->mFinalExtension}"; $nt = Title::makeTitleSafe(NS_FILE, $this->mFilteredName); $ext = array($this->mFinalExtension); } } } /* Don't allow users to override the blacklist (check file extension) */ global $wgCheckFileExtensions, $wgStrictFileExtensions; global $wgFileExtensions, $wgFileBlacklist; $blackListedExtensions = $this->checkFileExtensionList($ext, $wgFileBlacklist); if ($this->mFinalExtension == '') { $this->mTitleError = self::FILETYPE_MISSING; $this->mTitle = null; return $this->mTitle; } elseif ($blackListedExtensions || $wgCheckFileExtensions && $wgStrictFileExtensions && !$this->checkFileExtension($this->mFinalExtension, $wgFileExtensions)) { $this->mBlackListedExtensions = $blackListedExtensions; $this->mTitleError = self::FILETYPE_BADTYPE; $this->mTitle = null; return $this->mTitle; } // Windows may be broken with special characters, see bug XXX if (wfIsWindows() && !preg_match('/^[\\x0-\\x7f]*$/', $nt->getText())) { $this->mTitleError = self::WINDOWS_NONASCII_FILENAME; $this->mTitle = null; return $this->mTitle; } # If there was more than one "extension", reassemble the base # filename to prevent bogus complaints about length if (count($ext) > 1) { for ($i = 0; $i < count($ext) - 1; $i++) { $partname .= '.' . $ext[$i]; } } if (strlen($partname) < 1) { $this->mTitleError = self::MIN_LENGTH_PARTNAME; $this->mTitle = null; return $this->mTitle; } $this->mTitle = $nt; return $this->mTitle; }
/** * Check if the requested move target is a valid file move target * @param Title $nt Target title * @return array List of errors */ protected function validateFileMoveOperation($nt) { global $wgUser; $errors = array(); // wfFindFile( $nt ) / wfLocalFile( $nt ) is not allowed until below $file = wfLocalFile($this); if ($file->exists()) { if ($nt->getText() != wfStripIllegalFilenameChars($nt->getText())) { $errors[] = array('imageinvalidfilename'); } if (!File::checkExtensionCompatibility($file, $nt->getDBkey())) { $errors[] = array('imagetypemismatch'); } } if ($nt->getNamespace() != NS_FILE) { $errors[] = array('imagenocrossnamespace'); // From here we want to do checks on a file object, so if we can't // create one, we must return. return $errors; } // wfFindFile( $nt ) / wfLocalFile( $nt ) is allowed below here $destFile = wfLocalFile($nt); if (!$wgUser->isAllowed('reupload-shared') && !$destFile->exists() && wfFindFile($nt)) { $errors[] = array('file-exists-sharedrepo'); } return $errors; }
/** * Returns the title of the file to be uploaded. Sets mTitleError in case * the name was illegal. * * @return Title The title of the file or null in case the name was illegal */ public function getTitle() { if ($this->mTitle !== false) { return $this->mTitle; } /** * Chop off any directories in the given filename. Then * filter out illegal characters, and try to make a legible name * out of it. We'll strip some silently that Title would die on. */ $this->mFilteredName = wfStripIllegalFilenameChars($this->mDesiredDestName); /* Normalize to title form before we do any further processing */ $nt = Title::makeTitleSafe(NS_FILE, $this->mFilteredName); if (is_null($nt)) { $this->mTitleError = self::ILLEGAL_FILENAME; return $this->mTitle = null; } $this->mFilteredName = $nt->getDBkey(); /** * We'll want to blacklist against *any* 'extension', and use * only the final one for the whitelist. */ list($partname, $ext) = $this->splitExtensions($this->mFilteredName); if (count($ext)) { $this->mFinalExtension = trim($ext[count($ext) - 1]); } else { $this->mFinalExtension = ''; } /* Don't allow users to override the blacklist (check file extension) */ global $wgCheckFileExtensions, $wgStrictFileExtensions; global $wgFileExtensions, $wgFileBlacklist; if ($this->mFinalExtension == '') { $this->mTitleError = self::FILETYPE_MISSING; return $this->mTitle = null; } elseif ($this->checkFileExtensionList($ext, $wgFileBlacklist) || $wgCheckFileExtensions && $wgStrictFileExtensions && !$this->checkFileExtension($this->mFinalExtension, $wgFileExtensions)) { $this->mTitleError = self::FILETYPE_BADTYPE; return $this->mTitle = null; } # If there was more than one "extension", reassemble the base # filename to prevent bogus complaints about length if (count($ext) > 1) { for ($i = 0; $i < count($ext) - 1; $i++) { $partname .= '.' . $ext[$i]; } } if (strlen($partname) < 1) { $this->mTitleError = self::MIN_LENGTH_PARTNAME; return $this->mTitle = null; } return $this->mTitle = $nt; }
/** * Sanity checks for when a file is being moved * * @return Status */ protected function isValidFileMove() { $status = new Status(); $file = wfLocalFile($this->oldTitle); $file->load(File::READ_LATEST); if ($file->exists()) { if ($this->newTitle->getText() != wfStripIllegalFilenameChars($this->newTitle->getText())) { $status->fatal('imageinvalidfilename'); } if (!File::checkExtensionCompatibility($file, $this->newTitle->getDBkey())) { $status->fatal('imagetypemismatch'); } } if (!$this->newTitle->inNamespace(NS_FILE)) { $status->fatal('imagenocrossnamespace'); } return $status; }
/** * Check whether a given move operation would be valid. * Returns true if ok, or a getUserPermissionsErrors()-like array otherwise * * @param $nt \type{Title} the new title * @param $auth \type{\bool} indicates whether $wgUser's permissions * should be checked * @param $reason \type{\string} is the log summary of the move, used for spam checking * @return \type{\mixed} True on success, getUserPermissionsErrors()-like array on failure */ public function isValidMoveOperation(&$nt, $auth = true, $reason = '') { global $wgUser; $errors = array(); if (!$nt) { // Normally we'd add this to $errors, but we'll get // lots of syntax errors if $nt is not an object return array(array('badtitletext')); } if ($this->equals($nt)) { $errors[] = array('selfmove'); } if (!$this->isMovable()) { $errors[] = array('immobile-source-namespace', $this->getNsText()); } if ($nt->getInterwiki() != '') { $errors[] = array('immobile-target-namespace-iw'); } if (!$nt->isMovable()) { $errors[] = array('immobile-target-namespace', $nt->getNsText()); } $oldid = $this->getArticleID(); $newid = $nt->getArticleID(); if (strlen($nt->getDBkey()) < 1) { $errors[] = array('articleexists'); } if ($this->getDBkey() == '' || !$oldid || $nt->getDBkey() == '') { $errors[] = array('badarticleerror'); } // Image-specific checks if ($this->getNamespace() == NS_FILE) { if ($nt->getNamespace() != NS_FILE) { $errors[] = array('imagenocrossnamespace'); } $file = wfLocalFile($this); if ($file->exists()) { if ($nt->getText() != wfStripIllegalFilenameChars($nt->getText())) { $errors[] = array('imageinvalidfilename'); } if (!File::checkExtensionCompatibility($file, $nt->getDBkey())) { $errors[] = array('imagetypemismatch'); } } $destfile = wfLocalFile($nt); if (!$wgUser->isAllowed('reupload-shared') && !$destfile->exists() && wfFindFile($nt)) { $errors[] = array('file-exists-sharedrepo'); } } if ($nt->getNamespace() == NS_FILE && $this->getNamespace() != NS_FILE) { $errors[] = array('nonfile-cannot-move-to-file'); } if ($auth) { $errors = wfMergeErrorArrays($errors, $this->getUserPermissionsErrors('move', $wgUser), $this->getUserPermissionsErrors('edit', $wgUser), $nt->getUserPermissionsErrors('move-target', $wgUser), $nt->getUserPermissionsErrors('edit', $wgUser)); } $match = EditPage::matchSummarySpamRegex($reason); if ($match !== false) { // This is kind of lame, won't display nice $errors[] = array('spamprotectiontext'); } $err = null; if (!wfRunHooks('AbortMove', array($this, $nt, $wgUser, &$err, $reason))) { $errors[] = array('hookaborted', $err); } # The move is allowed only if (1) the target doesn't exist, or # (2) the target is a redirect to the source, and has no history # (so we can undo bad moves right after they're done). if (0 != $newid) { # Target exists; check for validity if (!$this->isValidMoveTarget($nt)) { $errors[] = array('articleexists'); } } else { $tp = $nt->getTitleProtection(); $right = $tp['pt_create_perm'] == 'sysop' ? 'protect' : $tp['pt_create_perm']; if ($tp and !$wgUser->isAllowed($right)) { $errors[] = array('cantmove-titleprotected'); } } if (empty($errors)) { return true; } return $errors; }
private function createCardPage($gatherer) { $title = Title::newFromText($this->name); $info = array(); $i = 0; foreach ($gatherer->sets as $set => $stuff) { $image = wfStripIllegalFilenameChars(str_replace(':', '', $this->name)) . ' ' . $gatherer->rels[$set]['abbr'] . '.jpg'; if (isset($gatherer->borders[$set]) && $gatherer->borders[$set]) { $border = $gatherer->rels[$set]['border']; $info['image' . ++$i] = "{{Border|[[Image:{$image}]]|{$border}}}"; } else { $info['image' . ++$i] = "[[Image:{$image}]]"; } foreach ($stuff as $rarity => $meh) { switch ($rarity) { case 'R': $rarity = 'Rare'; break; case 'U': $rarity = 'Uncommon'; break; case 'C': $rarity = 'Common'; break; case 'S': if ($set == 'Time Spiral "Timeshifted"') { $rarity = 'Timeshifted'; $set = 'Time Spiral'; } else { $rarity = 'Special'; } break; case 'M': $rarity = 'Mythic Rare'; break; case 'L': $rarity = 'Land'; break; } $set = wfStripIllegalFilenameChars(str_replace(':', '', $set)); $info['p/r' . $i] = "{{Rarity|{$set}|{$rarity}}}"; } } for ($i = 1; $i <= 15; $i++) { if (!isset($info['image' . $i])) { $info['image' . $i] = ''; } if (!isset($info['p/r' . $i])) { $info['p/r' . $i] = ''; } } $article = new WikiPage($title); $article->doEdit(wfMsgForContentNoTrans('gatherer-cardpage', array($gatherer->info['name'], $gatherer->info['type'], $gatherer->info['cost'], $gatherer->info['cmc'], $gatherer->info['rules'], $gatherer->info['flavor'], $gatherer->info['p/t'], $gatherer->info['planeswalker'], $info['image1'], $info['p/r1'], $info['image2'], $info['p/r2'], $info['image3'], $info['p/r3'], $info['image4'], $info['p/r4'], $info['image5'], $info['p/r5'], $info['image6'], $info['p/r6'], $info['image7'], $info['p/r7'], $info['image8'], $info['p/r8'], $info['image9'], $info['p/r9'], $info['image10'], $info['p/r10'], $info['image11'], $info['p/r11'], $info['image12'], $info['p/r12'], $info['image13'], $info['p/r13'], $info['image14'], $info['p/r14'], $info['image15'], $info['p/r15'])), wfMsg('gatherer-cardpage-com')); }
/** * Really do the upload * Checks are made in SpecialUpload::execute() * * @param array $resultDetails contains result-specific dict of additional values * * @access private */ function internalProcessUpload(&$resultDetails) { global $wgUser; if (!wfRunHooks('UploadForm:BeforeProcessing', array(&$this))) { wfDebug("Hook 'UploadForm:BeforeProcessing' broke processing the file.\n"); return self::BEFORE_PROCESSING; } /** * If there was no filename or a zero size given, give up quick. */ if (trim($this->mSrcName) == '' || empty($this->mFileSize)) { return self::EMPTY_FILE; } /* Check for curl error */ if ($this->mCurlError) { return self::BEFORE_PROCESSING; } /** * Chop off any directories in the given filename. Then * filter out illegal characters, and try to make a legible name * out of it. We'll strip some silently that Title would die on. */ if ($this->mDesiredDestName) { $basename = $this->mDesiredDestName; } else { $basename = $this->mSrcName; } $filtered = wfStripIllegalFilenameChars($basename); /* Normalize to title form before we do any further processing */ $nt = Title::makeTitleSafe(NS_FILE, $filtered); if (is_null($nt)) { $resultDetails = array('filtered' => $filtered); return self::ILLEGAL_FILENAME; } $filtered = $nt->getDBkey(); /** * We'll want to blacklist against *any* 'extension', and use * only the final one for the whitelist. */ list($partname, $ext) = $this->splitExtensions($filtered); if (count($ext)) { $finalExt = $ext[count($ext) - 1]; } else { $finalExt = ''; } # If there was more than one "extension", reassemble the base # filename to prevent bogus complaints about length if (count($ext) > 1) { for ($i = 0; $i < count($ext) - 1; $i++) { $partname .= '.' . $ext[$i]; } } if (strlen($partname) < 1) { return self::MIN_LENGTH_PARTNAME; } $this->mLocalFile = wfLocalFile($nt); $this->mDestName = $this->mLocalFile->getName(); /** * If the image is protected, non-sysop users won't be able * to modify it by uploading a new revision. */ $permErrors = $nt->getUserPermissionsErrors('edit', $wgUser); $permErrorsUpload = $nt->getUserPermissionsErrors('upload', $wgUser); $permErrorsCreate = $nt->exists() ? array() : $nt->getUserPermissionsErrors('create', $wgUser); if ($permErrors || $permErrorsUpload || $permErrorsCreate) { // merge all the problems into one list, avoiding duplicates $permErrors = array_merge($permErrors, wfArrayDiff2($permErrorsUpload, $permErrors)); $permErrors = array_merge($permErrors, wfArrayDiff2($permErrorsCreate, $permErrors)); $resultDetails = array('permissionserrors' => $permErrors); return self::PROTECTED_PAGE; } /** * In some cases we may forbid overwriting of existing files. */ $overwrite = $this->checkOverwrite($this->mDestName); if ($overwrite !== true) { $resultDetails = array('overwrite' => $overwrite); return self::OVERWRITE_EXISTING_FILE; } /* Don't allow users to override the blacklist (check file extension) */ global $wgCheckFileExtensions, $wgStrictFileExtensions; global $wgFileExtensions, $wgFileBlacklist; if ($finalExt == '') { return self::FILETYPE_MISSING; } elseif ($this->checkFileExtensionList($ext, $wgFileBlacklist) || $wgCheckFileExtensions && $wgStrictFileExtensions && !$this->checkFileExtension($finalExt, $wgFileExtensions)) { $resultDetails = array('finalExt' => $finalExt); return self::FILETYPE_BADTYPE; } /** * Look at the contents of the file; if we can recognize the * type but it's corrupt or data of the wrong type, we should * probably not accept it. */ if (!$this->mStashed) { $this->mFileProps = File::getPropsFromPath($this->mTempPath, $finalExt); $this->checkMacBinary(); $veri = $this->verify($this->mTempPath, $finalExt); if ($veri !== true) { //it's a wiki error... $resultDetails = array('veri' => $veri); return self::VERIFICATION_ERROR; } /** * Provide an opportunity for extensions to add further checks */ $error = ''; if (!wfRunHooks('UploadVerification', array($this->mDestName, $this->mTempPath, &$error))) { $resultDetails = array('error' => $error); return self::UPLOAD_VERIFICATION_ERROR; } } /** * Check for non-fatal conditions */ if (!$this->mIgnoreWarning) { $warning = ''; $comparableName = str_replace(' ', '_', $basename); global $wgCapitalLinks, $wgContLang; if ($wgCapitalLinks) { $comparableName = $wgContLang->ucfirst($comparableName); } if ($comparableName !== $filtered) { $warning .= '<li>' . wfMsgHtml('badfilename', htmlspecialchars($this->mDestName)) . '</li>'; } global $wgCheckFileExtensions; if ($wgCheckFileExtensions) { if (!$this->checkFileExtension($finalExt, $wgFileExtensions)) { global $wgLang; $warning .= '<li>' . wfMsgExt('filetype-unwanted-type', array('parseinline'), htmlspecialchars($finalExt), $wgLang->commaList($wgFileExtensions), $wgLang->formatNum(count($wgFileExtensions))) . '</li>'; } } global $wgUploadSizeWarning; if ($wgUploadSizeWarning && $this->mFileSize > $wgUploadSizeWarning) { $skin = $wgUser->getSkin(); $wsize = $skin->formatSize($wgUploadSizeWarning); $asize = $skin->formatSize($this->mFileSize); $warning .= '<li>' . wfMsgHtml('large-file', $wsize, $asize) . '</li>'; } if ($this->mFileSize == 0) { $warning .= '<li>' . wfMsgHtml('emptyfile') . '</li>'; } if (!$this->mDestWarningAck) { $warning .= self::getExistsWarning($this->mLocalFile); } $warning .= $this->getDupeWarning($this->mTempPath, $finalExt, $nt); if ($warning != '') { /** * Stash the file in a temporary location; the user can choose * to let it through and we'll complete the upload then. */ $resultDetails = array('warning' => $warning); return self::UPLOAD_WARNING; } } /** * Try actually saving the thing... * It will show an error form on failure. */ if (!$this->mForReUpload) { $pageText = self::getInitialPageText($this->mComment, $this->mLicense, $this->mCopyrightStatus, $this->mCopyrightSource); } $status = $this->mLocalFile->upload($this->mTempPath, $this->mComment, $pageText, File::DELETE_SOURCE, $this->mFileProps); if (!$status->isGood()) { $resultDetails = array('internal' => $status->getWikiText()); return self::INTERNAL_ERROR; } else { if ($this->mWatchthis) { global $wgUser; $wgUser->addWatch($this->mLocalFile->getTitle()); } // Success, redirect to description page $img = null; // @todo: added to avoid passing a ref to null - should this be defined somewhere? wfRunHooks('UploadComplete', array(&$this)); return self::SUCCESS; } }