Changeset View
Changeset View
Standalone View
Standalone View
src/applications/macro/controller/PhabricatorMacroEditController.php
| Show All 27 Lines | if ($this->id) { | ||||
| $macro = new PhabricatorFileImageMacro(); | $macro = new PhabricatorFileImageMacro(); | ||||
| $macro->setAuthorPHID($user->getPHID()); | $macro->setAuthorPHID($user->getPHID()); | ||||
| } | } | ||||
| $errors = array(); | $errors = array(); | ||||
| $e_name = true; | $e_name = true; | ||||
| $e_file = null; | $e_file = null; | ||||
| $file = null; | $file = null; | ||||
| $can_fetch = PhabricatorEnv::getEnvConfig('security.allow-outbound-http'); | |||||
| if ($request->isFormPost()) { | if ($request->isFormPost()) { | ||||
| $original = clone $macro; | $original = clone $macro; | ||||
| $new_name = null; | $new_name = null; | ||||
| if ($request->getBool('name_form') || !$macro->getID()) { | if ($request->getBool('name_form') || !$macro->getID()) { | ||||
| $new_name = $request->getStr('name'); | $new_name = $request->getStr('name'); | ||||
| $macro->setName($new_name); | $macro->setName($new_name); | ||||
| if (!strlen($macro->getName())) { | if (!strlen($macro->getName())) { | ||||
| $errors[] = pht('Macro name is required.'); | $errors[] = pht('Macro name is required.'); | ||||
| $e_name = pht('Required'); | $e_name = pht('Required'); | ||||
| } else if (!preg_match('/^[a-z0-9:_-]{3,}\z/', $macro->getName())) { | } else if (!preg_match('/^[a-z0-9:_-]{3,}\z/', $macro->getName())) { | ||||
| $errors[] = pht( | $errors[] = pht( | ||||
| 'Macro must be at least three characters long and contain only '. | 'Macro must be at least three characters long and contain only '. | ||||
| 'lowercase letters, digits, hyphens, colons and underscores.'); | 'lowercase letters, digits, hyphens, colons and underscores.'); | ||||
| $e_name = pht('Invalid'); | $e_name = pht('Invalid'); | ||||
| } else { | } else { | ||||
| $e_name = null; | $e_name = null; | ||||
| } | } | ||||
| } | } | ||||
| $uri = $request->getStr('url'); | |||||
| $engine = new PhabricatorDestructionEngine(); | |||||
| $file = null; | $file = null; | ||||
| if ($request->getFileExists('file')) { | if ($request->getFileExists('file')) { | ||||
| $file = PhabricatorFile::newFromPHPUpload( | $file = PhabricatorFile::newFromPHPUpload( | ||||
| $_FILES['file'], | $_FILES['file'], | ||||
| array( | array( | ||||
| 'name' => $request->getStr('name'), | 'name' => $request->getStr('name'), | ||||
| 'authorPHID' => $user->getPHID(), | 'authorPHID' => $user->getPHID(), | ||||
| 'isExplicitUpload' => true, | 'isExplicitUpload' => true, | ||||
| 'canCDN' => true, | 'canCDN' => true, | ||||
| )); | )); | ||||
| } else if ($request->getStr('url') && $can_fetch) { | } else if ($uri) { | ||||
| try { | try { | ||||
| $file = PhabricatorFile::newFromFileDownload( | $file = PhabricatorFile::newFromFileDownload( | ||||
epriestley: Note that `newFromFileDownload()` does the URI check for us. | |||||
| $request->getStr('url'), | $uri, | ||||
| array( | array( | ||||
| 'name' => $request->getStr('name'), | 'name' => $request->getStr('name'), | ||||
| 'authorPHID' => $user->getPHID(), | 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, | ||||
| 'isExplicitUpload' => true, | 'isExplicitUpload' => true, | ||||
| 'canCDN' => true, | 'canCDN' => true, | ||||
| )); | )); | ||||
| if (!$file->isViewableInBrowser()) { | |||||
| $mime_type = $file->getMimeType(); | |||||
| $engine->destroyObject($file); | |||||
| $file = null; | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'The URI "%s" does not correspond to a valid image file, got '. | |||||
| 'a file with MIME type "%s". You must specify the URI of a '. | |||||
| 'valid image file.', | |||||
| $uri, | |||||
| $mime_type)); | |||||
| } else { | |||||
| $file | |||||
| ->setAuthorPHID($user->getPHID()) | |||||
| ->save(); | |||||
| } | |||||
| } catch (HTTPFutureHTTPResponseStatus $status) { | |||||
| $errors[] = pht( | |||||
| 'The URI "%s" could not be loaded, got %s error.', | |||||
| $uri, | |||||
| $status->getStatusCode()); | |||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| $errors[] = pht('Could not fetch URL: %s', $ex->getMessage()); | $errors[] = $ex->getMessage(); | ||||
| } | } | ||||
| } else if ($request->getStr('phid')) { | } else if ($request->getStr('phid')) { | ||||
| $file = id(new PhabricatorFileQuery()) | $file = id(new PhabricatorFileQuery()) | ||||
| ->setViewer($user) | ->setViewer($user) | ||||
| ->withPHIDs(array($request->getStr('phid'))) | ->withPHIDs(array($request->getStr('phid'))) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | if (!$macro->getID()) { | ||||
| ->setLabel(pht('Selected File')) | ->setLabel(pht('Selected File')) | ||||
| ->setValue($current_file_view)); | ->setValue($current_file_view)); | ||||
| $other_label = pht('Change File'); | $other_label = pht('Change File'); | ||||
| } else { | } else { | ||||
| $other_label = pht('File'); | $other_label = pht('File'); | ||||
| } | } | ||||
| if ($can_fetch) { | |||||
| $form->appendChild( | $form->appendChild( | ||||
| id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
| ->setLabel(pht('URL')) | ->setLabel(pht('URL')) | ||||
| ->setName('url') | ->setName('url') | ||||
| ->setValue($request->getStr('url')) | ->setValue($request->getStr('url')) | ||||
| ->setError($request->getFileExists('file') ? false : $e_file)); | ->setError($request->getFileExists('file') ? false : $e_file)); | ||||
| } | |||||
| $form->appendChild( | $form->appendChild( | ||||
| id(new AphrontFormFileControl()) | id(new AphrontFormFileControl()) | ||||
| ->setLabel($other_label) | ->setLabel($other_label) | ||||
| ->setName('file') | ->setName('file') | ||||
| ->setError($request->getStr('url') ? false : $e_file)); | ->setError($request->getStr('url') ? false : $e_file)); | ||||
| } | } | ||||
| Show All 27 Lines | public function processRequest() { | ||||
| $crumbs->addTextCrumb($crumb, $request->getRequestURI()); | $crumbs->addTextCrumb($crumb, $request->getRequestURI()); | ||||
| $upload = null; | $upload = null; | ||||
| if ($macro->getID()) { | if ($macro->getID()) { | ||||
| $upload_form = id(new AphrontFormView()) | $upload_form = id(new AphrontFormView()) | ||||
| ->setEncType('multipart/form-data') | ->setEncType('multipart/form-data') | ||||
| ->setUser($request->getUser()); | ->setUser($request->getUser()); | ||||
| if ($can_fetch) { | |||||
| $upload_form->appendChild( | $upload_form->appendChild( | ||||
| id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
| ->setLabel(pht('URL')) | ->setLabel(pht('URL')) | ||||
| ->setName('url') | ->setName('url') | ||||
| ->setValue($request->getStr('url'))); | ->setValue($request->getStr('url'))); | ||||
| } | |||||
| $upload_form | $upload_form | ||||
| ->appendChild( | ->appendChild( | ||||
| id(new AphrontFormFileControl()) | id(new AphrontFormFileControl()) | ||||
| ->setLabel(pht('File')) | ->setLabel(pht('File')) | ||||
| ->setName('file')) | ->setName('file')) | ||||
| ->appendChild( | ->appendChild( | ||||
| id(new AphrontFormSubmitControl()) | id(new AphrontFormSubmitControl()) | ||||
| Show All 24 Lines | |||||
Note that newFromFileDownload() does the URI check for us.