public function processRequest()
 {
     if ($this->id) {
         $macro = id(new PhabricatorFileImageMacro())->load($this->id);
         if (!$macro) {
             return new Aphront404Response();
         }
     } else {
         $macro = new PhabricatorFileImageMacro();
     }
     $errors = array();
     $e_name = true;
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($request->isFormPost()) {
         $macro->setName($request->getStr('name'));
         if (!strlen($macro->getName())) {
             $errors[] = 'Macro name is required.';
             $e_name = 'Required';
         } else {
             if (!preg_match('/^[a-z0-9_-]{3,}$/', $macro->getName())) {
                 $errors[] = 'Macro must be at least three characters long and contain ' . 'only lowercase letters, digits, hyphen and underscore.';
                 $e_name = 'Invalid';
             } else {
                 $e_name = null;
             }
         }
         if (!$errors) {
             $file = PhabricatorFile::newFromPHPUpload(idx($_FILES, 'file'), array('name' => $request->getStr('name'), 'authorPHID' => $user->getPHID()));
             $macro->setFilePHID($file->getPHID());
             try {
                 $macro->save();
                 return id(new AphrontRedirectResponse())->setURI('/file/macro/');
             } catch (AphrontQueryDuplicateKeyException $ex) {
                 $errors[] = 'Macro name is not unique!';
                 $e_name = 'Duplicate';
             }
         }
     }
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     } else {
         $error_view = null;
     }
     $form = new AphrontFormView();
     $form->setAction('/file/macro/edit/');
     $form->setUser($request->getUser());
     $form->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($macro->getName())->setCaption('This word or phrase will be replaced with the image.')->setError($e_name))->appendChild(id(new AphrontFormFileControl())->setLabel('File')->setName('file')->setError(true))->appendChild(id(new AphrontFormSubmitControl())->setValue('Save Image Macro')->addCancelButton('/file/macro/'));
     $panel = new AphrontPanelView();
     if ($macro->getID()) {
         $panel->setHeader('Edit Image Macro');
     } else {
         $panel->setHeader('Create Image Macro');
     }
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Edit Image Macro'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $editable = $this->getAccountEditable();
     // There's no sense in showing a change password panel if the user
     // can't change their password
     if (!$editable || !PhabricatorEnv::getEnvConfig('auth.password-auth-enabled')) {
         return new Aphront400Response();
     }
     $errors = array();
     if ($request->isFormPost()) {
         if ($user->comparePassword($request->getStr('old_pw'))) {
             $pass = $request->getStr('new_pw');
             $conf = $request->getStr('conf_pw');
             if ($pass === $conf) {
                 if (strlen($pass)) {
                     $user->setPassword($pass);
                     // This write is unguarded because the CSRF token has already
                     // been checked in the call to $request->isFormPost() and
                     // the CSRF token depends on the password hash, so when it
                     // is changed here the CSRF token check will fail.
                     $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
                     $user->save();
                     unset($unguarded);
                     return id(new AphrontRedirectResponse())->setURI('/settings/page/password/?saved=true');
                 } else {
                     $errors[] = 'Your new password is too short.';
                 }
             } else {
                 $errors[] = 'New password and confirmation do not match.';
             }
         } else {
             $errors[] = 'The old password you entered is incorrect.';
         }
     }
     $notice = null;
     if (!$errors) {
         if ($request->getStr('saved')) {
             $notice = new AphrontErrorView();
             $notice->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
             $notice->setTitle('Changes Saved');
             $notice->appendChild('<p>Your password has been updated.</p>');
         }
     } else {
         $notice = new AphrontErrorView();
         $notice->setTitle('Error Changing Password');
         $notice->setErrors($errors);
     }
     $form = new AphrontFormView();
     $form->setUser($user)->appendChild(id(new AphrontFormPasswordControl())->setLabel('Old Password')->setName('old_pw'));
     $form->appendChild(id(new AphrontFormPasswordControl())->setLabel('New Password')->setName('new_pw'));
     $form->appendChild(id(new AphrontFormPasswordControl())->setLabel('Confirm Password')->setName('conf_pw'));
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Save'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Change Password');
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     return id(new AphrontNullView())->appendChild(array($notice, $panel));
 }
 private function processImportRequest($request)
 {
     $admin = $request->getUser();
     $usernames = $request->getArr('usernames');
     $emails = $request->getArr('email');
     $names = $request->getArr('name');
     $notice_view = new AphrontErrorView();
     $notice_view->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
     $notice_view->setTitle(pht('Import Successful'));
     $notice_view->setErrors(array(pht('Successfully imported users from LDAP')));
     $list = new PHUIObjectItemListView();
     $list->setNoDataString(pht('No users imported?'));
     foreach ($usernames as $username) {
         $user = new PhabricatorUser();
         $user->setUsername($username);
         $user->setRealname($names[$username]);
         $email_obj = id(new PhabricatorUserEmail())->setAddress($emails[$username])->setIsVerified(1);
         try {
             id(new PhabricatorUserEditor())->setActor($admin)->createNewUser($user, $email_obj);
             id(new PhabricatorExternalAccount())->setUserPHID($user->getPHID())->setAccountType('ldap')->setAccountDomain('self')->setAccountID($username)->save();
             $header = pht('Successfully added %s', $username);
             $attribute = null;
             $color = 'green';
         } catch (Exception $ex) {
             $header = pht('Failed to add %s', $username);
             $attribute = $ex->getMessage();
             $color = 'red';
         }
         $item = id(new PHUIObjectItemView())->setHeader($header)->addAttribute($attribute)->setBarColor($color);
         $list->addItem($item);
     }
     return array($notice_view, $list);
 }
 protected function renderErrorPage($title, array $messages)
 {
     $view = new AphrontErrorView();
     $view->setTitle($title);
     $view->setErrors($messages);
     return $this->buildApplicationPage($view, array('title' => $title));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $project = new PhabricatorProject();
     $project->setAuthorPHID($user->getPHID());
     $profile = new PhabricatorProjectProfile();
     $e_name = true;
     $errors = array();
     if ($request->isFormPost()) {
         try {
             $editor = new PhabricatorProjectEditor($project);
             $editor->setUser($user);
             $editor->setName($request->getStr('name'));
             $editor->save();
         } catch (PhabricatorProjectNameCollisionException $ex) {
             $e_name = 'Not Unique';
             $errors[] = $ex->getMessage();
         }
         $project->setStatus(PhabricatorProjectStatus::ONGOING);
         $profile->setBlurb($request->getStr('blurb'));
         if (!$errors) {
             $project->save();
             $profile->setProjectPHID($project->getPHID());
             $profile->save();
             id(new PhabricatorProjectAffiliation())->setUserPHID($user->getPHID())->setProjectPHID($project->getPHID())->setRole('Owner')->setIsOwner(true)->save();
             if ($request->isAjax()) {
                 return id(new AphrontAjaxResponse())->setContent(array('phid' => $project->getPHID(), 'name' => $project->getName()));
             } else {
                 return id(new AphrontRedirectResponse())->setURI('/project/view/' . $project->getID() . '/');
             }
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     }
     if ($request->isAjax()) {
         $form = new AphrontFormLayoutView();
     } else {
         $form = new AphrontFormView();
         $form->setUser($user);
     }
     $form->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($project->getName())->setError($e_name))->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Blurb')->setName('blurb')->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)->setValue($profile->getBlurb()));
     if ($request->isAjax()) {
         if ($error_view) {
             $error_view->setWidth(AphrontErrorView::WIDTH_DIALOG);
         }
         $dialog = id(new AphrontDialogView())->setUser($user)->setWidth(AphrontDialogView::WIDTH_FORM)->setTitle('Create a New Project')->appendChild($error_view)->appendChild($form)->addSubmitButton('Create Project')->addCancelButton('/project/');
         return id(new AphrontDialogResponse())->setDialog($dialog);
     } else {
         $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Create')->addCancelButton('/project/'));
         $panel = new AphrontPanelView();
         $panel->setWidth(AphrontPanelView::WIDTH_FORM)->setHeader('Create a New Project')->appendChild($form);
         return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Create new Project'));
     }
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $resource = new DrydockResource();
     $json = new PhutilJSON();
     $err_attributes = true;
     $err_capabilities = true;
     $json_attributes = $json->encodeFormatted($resource->getAttributes());
     $json_capabilities = $json->encodeFormatted($resource->getCapabilities());
     $errors = array();
     if ($request->isFormPost()) {
         $raw_attributes = $request->getStr('attributes');
         $attributes = json_decode($raw_attributes, true);
         if (!is_array($attributes)) {
             $err_attributes = 'Invalid';
             $errors[] = 'Enter attributes as a valid JSON object.';
             $json_attributes = $raw_attributes;
         } else {
             $resource->setAttributes($attributes);
             $json_attributes = $json->encodeFormatted($attributes);
             $err_attributes = null;
         }
         $raw_capabilities = $request->getStr('capabilities');
         $capabilities = json_decode($raw_capabilities, true);
         if (!is_array($capabilities)) {
             $err_capabilities = 'Invalid';
             $errors[] = 'Enter capabilities as a valid JSON object.';
             $json_capabilities = $raw_capabilities;
         } else {
             $resource->setCapabilities($capabilities);
             $json_capabilities = $json->encodeFormatted($capabilities);
             $err_capabilities = null;
         }
         $resource->setBlueprintClass($request->getStr('blueprint'));
         $resource->setType($resource->getBlueprint()->getType());
         $resource->setOwnerPHID($user->getPHID());
         $resource->setName($request->getStr('name'));
         if (!$errors) {
             $resource->save();
             return id(new AphrontRedirectResponse())->setURI('/drydock/resource/');
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     }
     $blueprints = id(new PhutilSymbolLoader())->setType('class')->setAncestorClass('DrydockBlueprint')->selectAndLoadSymbols();
     $blueprints = ipull($blueprints, 'name', 'name');
     $panel = new AphrontPanelView();
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->setHeader('Allocate Drydock Resource');
     $form = id(new AphrontFormView())->setUser($request->getUser())->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($resource->getName()))->appendChild(id(new AphrontFormSelectControl())->setLabel('Blueprint')->setOptions($blueprints)->setName('blueprint')->setValue($resource->getBlueprintClass()))->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Attributes')->setName('attributes')->setValue($json_attributes)->setError($err_attributes)->setCaption('Specify attributes in JSON.'))->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Capabilities')->setName('capabilities')->setValue($json_capabilities)->setError($err_capabilities)->setCaption('Specify capabilities in JSON.'))->appendChild(id(new AphrontFormSubmitControl())->setValue('Allocate Resource'));
     $panel->appendChild($form);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Allocate Resource'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $e_name = true;
     $e_callsign = true;
     $repository = new PhabricatorRepository();
     $type_map = PhabricatorRepositoryType::getAllRepositoryTypes();
     $errors = array();
     if ($request->isFormPost()) {
         $repository->setName($request->getStr('name'));
         $repository->setCallsign($request->getStr('callsign'));
         $repository->setVersionControlSystem($request->getStr('type'));
         if (!strlen($repository->getName())) {
             $e_name = 'Required';
             $errors[] = 'Repository name is required.';
         } else {
             $e_name = null;
         }
         if (!strlen($repository->getCallsign())) {
             $e_callsign = 'Required';
             $errors[] = 'Callsign is required.';
         } else {
             if (!preg_match('/^[A-Z]+$/', $repository->getCallsign())) {
                 $e_callsign = 'Invalid';
                 $errors[] = 'Callsign must be ALL UPPERCASE LETTERS.';
             } else {
                 $e_callsign = null;
             }
         }
         if (empty($type_map[$repository->getVersionControlSystem()])) {
             $errors[] = 'Invalid version control system.';
         }
         if (!$errors) {
             try {
                 $repository->save();
                 return id(new AphrontRedirectResponse())->setURI('/repository/edit/' . $repository->getID() . '/');
             } catch (AphrontQueryDuplicateKeyException $ex) {
                 $e_callsign = 'Duplicate';
                 $errors[] = 'Callsign must be unique. Another repository already ' . 'uses that callsign.';
             }
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setErrors($errors);
         $error_view->setTitle('Form Errors');
     }
     $form = new AphrontFormView();
     $form->setUser($user)->setAction('/repository/create/')->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($repository->getName())->setError($e_name)->setCaption('Human-readable repository name.'))->appendChild('<p class="aphront-form-instructions">Select a "Callsign" &mdash; a ' . 'short, uppercase string to identify revisions in this repository. If ' . 'you choose "EX", revisions in this repository will be identified ' . 'with the prefix "rEX".</p>')->appendChild(id(new AphrontFormTextControl())->setLabel('Callsign')->setName('callsign')->setValue($repository->getCallsign())->setError($e_callsign)->setCaption('Short, UPPERCASE identifier. Once set, it can not be changed.'))->appendChild(id(new AphrontFormSelectControl())->setLabel('Type')->setName('type')->setOptions($type_map)->setValue($repository->getVersionControlSystem()))->appendChild(id(new AphrontFormSubmitControl())->setValue('Create Repository')->addCancelButton('/repository/'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Create Repository');
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Create Repository'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     if (!PhabricatorEnv::getEnvConfig('auth.password-auth-enabled')) {
         return new Aphront400Response();
     }
     $token = $this->token;
     $email = $request->getStr('email');
     $target_user = id(new PhabricatorUser())->loadOneWhere('email = %s', $email);
     if (!$target_user || !$target_user->validateEmailToken($token)) {
         $view = new AphrontRequestFailureView();
         $view->setHeader('Unable to Login');
         $view->appendChild('<p>The authentication information in the link you clicked is ' . 'invalid or out of date. Make sure you are copy-and-pasting the ' . 'entire link into your browser. You can try again, or request ' . 'a new email.</p>');
         $view->appendChild('<div class="aphront-failure-continue">' . '<a class="button" href="/login/email/">Send Another Email</a>' . '</div>');
         return $this->buildStandardPageResponse($view, array('title' => 'Email Sent'));
     }
     if ($request->getUser()->getPHID() != $target_user->getPHID()) {
         $session_key = $target_user->establishSession('web');
         $request->setCookie('phusr', $target_user->getUsername());
         $request->setCookie('phsid', $session_key);
     }
     $errors = array();
     $e_pass = true;
     $e_confirm = true;
     if ($request->isFormPost()) {
         $e_pass = '******';
         $e_confirm = 'Error';
         $pass = $request->getStr('password');
         $confirm = $request->getStr('confirm');
         if (strlen($pass) < 3) {
             $errors[] = 'That password is ridiculously short.';
         }
         if ($pass !== $confirm) {
             $errors[] = "Passwords do not match.";
         }
         if (!$errors) {
             $target_user->setPassword($pass);
             $target_user->save();
             return id(new AphrontRedirectResponse())->setURI('/');
         }
     }
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Password Reset Failed');
         $error_view->setErrors($errors);
     } else {
         $error_view = null;
     }
     $form = new AphrontFormView();
     $form->setUser($target_user)->setAction('/login/etoken/' . $token . '/')->addHiddenInput('email', $email)->appendChild(id(new AphrontFormPasswordControl())->setLabel('New Password')->setName('password')->setError($e_pass))->appendChild(id(new AphrontFormPasswordControl())->setLabel('Confirm Password')->setName('confirm')->setError($e_confirm))->appendChild(id(new AphrontFormSubmitControl())->setValue('Reset Password')->addCancelButton('/', 'Skip'));
     $panel = new AphrontPanelView();
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->setHeader('Reset Password');
     $panel->appendChild($form);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Create New Account'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $editable = $this->getAccountEditable();
     $e_realname = $editable ? true : null;
     $errors = array();
     if ($request->isFormPost()) {
         if ($editable) {
             $user->setRealName($request->getStr('realname'));
             if (!strlen($user->getRealName())) {
                 $errors[] = 'Real name must be nonempty.';
                 $e_realname = 'Required';
             }
         }
         $new_timezone = $request->getStr('timezone');
         if (in_array($new_timezone, DateTimeZone::listIdentifiers(), true)) {
             $user->setTimezoneIdentifier($new_timezone);
         } else {
             $errors[] = 'The selected timezone is not a valid timezone.';
         }
         if (!$errors) {
             $user->save();
             return id(new AphrontRedirectResponse())->setURI('/settings/page/account/?saved=true');
         }
     }
     $notice = null;
     if (!$errors) {
         if ($request->getStr('saved')) {
             $notice = new AphrontErrorView();
             $notice->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
             $notice->setTitle('Changes Saved');
             $notice->appendChild('<p>Your changes have been saved.</p>');
             $notice = $notice->render();
         }
     } else {
         $notice = new AphrontErrorView();
         $notice->setTitle('Form Errors');
         $notice->setErrors($errors);
         $notice = $notice->render();
     }
     $timezone_ids = DateTimeZone::listIdentifiers();
     $timezone_id_map = array_combine($timezone_ids, $timezone_ids);
     $form = new AphrontFormView();
     $form->setUser($user)->setEncType('multipart/form-data')->appendChild(id(new AphrontFormStaticControl())->setLabel('Username')->setValue($user->getUsername()))->appendChild(id(new AphrontFormTextControl())->setLabel('Real Name')->setName('realname')->setError($e_realname)->setValue($user->getRealName())->setDisabled(!$editable))->appendChild(id(new AphrontFormSelectControl())->setLabel('Timezone')->setName('timezone')->setOptions($timezone_id_map)->setValue($user->getTimezoneIdentifier()))->appendChild(id(new AphrontFormSubmitControl())->setValue('Save'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Account Settings');
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     return id(new AphrontNullView())->appendChild(array($notice, $panel));
 }
예제 #10
0
 public function renderExample()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $sevs = array(AphrontErrorView::SEVERITY_ERROR => 'Error', AphrontErrorView::SEVERITY_WARNING => 'Warning', AphrontErrorView::SEVERITY_NOTICE => 'Notice', AphrontErrorView::SEVERITY_NODATA => 'No Data');
     $views = array();
     foreach ($sevs as $sev => $title) {
         $view = new AphrontErrorView();
         $view->setSeverity($sev);
         $view->setTitle($title);
         $view->appendChild('Several issues were encountered.');
         $view->setErrors(array('Overcooked.', 'Too much salt.', 'Full of sand.'));
         $views[] = $view;
     }
     return $views;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $editable = $this->getAccountEditable();
     $e_email = true;
     $errors = array();
     if ($request->isFormPost()) {
         if (!$editable) {
             return new Aphront400Response();
         }
         $user->setEmail($request->getStr('email'));
         if (!strlen($user->getEmail())) {
             $errors[] = 'You must enter an e-mail address.';
             $e_email = 'Required';
         }
         if (!$errors) {
             $user->save();
             return id(new AphrontRedirectResponse())->setURI('/settings/page/email/?saved=true');
         }
     }
     $notice = null;
     if (!$errors) {
         if ($request->getStr('saved')) {
             $notice = new AphrontErrorView();
             $notice->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
             $notice->setTitle('Changes Saved');
             $notice->appendChild('<p>Your changes have been saved.</p>');
         }
     } else {
         $notice = new AphrontErrorView();
         $notice->setTitle('Form Errors');
         $notice->setErrors($errors);
     }
     $form = new AphrontFormView();
     $form->setUser($user)->appendChild(id(new AphrontFormTextControl())->setLabel('Email')->setName('email')->setDisabled(!$editable)->setCaption('Note: there is no email validation yet; double-check your ' . 'typing.')->setValue($user->getEmail())->setError($e_email));
     if ($editable) {
         $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Save'));
     }
     $panel = new AphrontPanelView();
     $panel->setHeader('Email Settings');
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     return id(new AphrontNullView())->appendChild(array($notice, $panel));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $response_type = $request->getStr('responseType', 'task');
     $order = $request->getStr('order', PhabricatorProjectColumn::DEFAULT_ORDER);
     $can_edit_assign = $this->hasApplicationCapability(ManiphestEditAssignCapability::CAPABILITY);
     $can_edit_policies = $this->hasApplicationCapability(ManiphestEditPoliciesCapability::CAPABILITY);
     $can_edit_priority = $this->hasApplicationCapability(ManiphestEditPriorityCapability::CAPABILITY);
     $can_edit_projects = $this->hasApplicationCapability(ManiphestEditProjectsCapability::CAPABILITY);
     $can_edit_status = $this->hasApplicationCapability(ManiphestEditStatusCapability::CAPABILITY);
     $parent_task = null;
     $template_id = null;
     if ($this->id) {
         $task = id(new ManiphestTaskQuery())->setViewer($user)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->withIDs(array($this->id))->executeOne();
         if (!$task) {
             return new Aphront404Response();
         }
     } else {
         $task = ManiphestTask::initializeNewTask($user);
         // We currently do not allow you to set the task status when creating
         // a new task, although now that statuses are custom it might make
         // sense.
         $can_edit_status = false;
         // These allow task creation with defaults.
         if (!$request->isFormPost()) {
             $task->setTitle($request->getStr('title'));
             if ($can_edit_projects) {
                 $projects = $request->getStr('projects');
                 if ($projects) {
                     $tokens = $request->getStrList('projects');
                     $type_project = PhabricatorProjectProjectPHIDType::TYPECONST;
                     foreach ($tokens as $key => $token) {
                         if (phid_get_type($token) == $type_project) {
                             // If this is formatted like a PHID, leave it as-is.
                             continue;
                         }
                         if (preg_match('/^#/', $token)) {
                             // If this already has a "#", leave it as-is.
                             continue;
                         }
                         // Add a "#" prefix.
                         $tokens[$key] = '#' . $token;
                     }
                     $default_projects = id(new PhabricatorObjectQuery())->setViewer($user)->withNames($tokens)->execute();
                     $default_projects = mpull($default_projects, 'getPHID');
                     if ($default_projects) {
                         $task->attachProjectPHIDs($default_projects);
                     }
                 }
             }
             if ($can_edit_priority) {
                 $priority = $request->getInt('priority');
                 if ($priority !== null) {
                     $priority_map = ManiphestTaskPriority::getTaskPriorityMap();
                     if (isset($priority_map[$priority])) {
                         $task->setPriority($priority);
                     }
                 }
             }
             $task->setDescription($request->getStr('description'));
             if ($can_edit_assign) {
                 $assign = $request->getStr('assign');
                 if (strlen($assign)) {
                     $assign_user = id(new PhabricatorPeopleQuery())->setViewer($user)->withUsernames(array($assign))->executeOne();
                     if (!$assign_user) {
                         $assign_user = id(new PhabricatorPeopleQuery())->setViewer($user)->withPHIDs(array($assign))->executeOne();
                     }
                     if ($assign_user) {
                         $task->setOwnerPHID($assign_user->getPHID());
                     }
                 }
             }
         }
         $template_id = $request->getInt('template');
         // You can only have a parent task if you're creating a new task.
         $parent_id = $request->getInt('parent');
         if ($parent_id) {
             $parent_task = id(new ManiphestTaskQuery())->setViewer($user)->withIDs(array($parent_id))->executeOne();
             if (!$template_id) {
                 $template_id = $parent_id;
             }
         }
     }
     $errors = array();
     $e_title = true;
     $field_list = PhabricatorCustomField::getObjectFields($task, PhabricatorCustomField::ROLE_EDIT);
     $field_list->setViewer($user);
     $field_list->readFieldsFromStorage($task);
     $aux_fields = $field_list->getFields();
     if ($request->isFormPost()) {
         $changes = array();
         $new_title = $request->getStr('title');
         $new_desc = $request->getStr('description');
         $new_status = $request->getStr('status');
         if (!$task->getID()) {
             $workflow = 'create';
         } else {
             $workflow = '';
         }
         $changes[ManiphestTransaction::TYPE_TITLE] = $new_title;
         $changes[ManiphestTransaction::TYPE_DESCRIPTION] = $new_desc;
         if ($can_edit_status) {
             $changes[ManiphestTransaction::TYPE_STATUS] = $new_status;
         } else {
             if (!$task->getID()) {
                 // Create an initial status transaction for the burndown chart.
                 // TODO: We can probably remove this once Facts comes online.
                 $changes[ManiphestTransaction::TYPE_STATUS] = $task->getStatus();
             }
         }
         $owner_tokenizer = $request->getArr('assigned_to');
         $owner_phid = reset($owner_tokenizer);
         if (!strlen($new_title)) {
             $e_title = pht('Required');
             $errors[] = pht('Title is required.');
         }
         $old_values = array();
         foreach ($aux_fields as $aux_arr_key => $aux_field) {
             // TODO: This should be buildFieldTransactionsFromRequest() once we
             // switch to ApplicationTransactions properly.
             $aux_old_value = $aux_field->getOldValueForApplicationTransactions();
             $aux_field->readValueFromRequest($request);
             $aux_new_value = $aux_field->getNewValueForApplicationTransactions();
             // TODO: We're faking a call to the ApplicaitonTransaction validation
             // logic here. We need valid objects to pass, but they aren't used
             // in a meaningful way. For now, build User objects. Once the Maniphest
             // objects exist, this will switch over automatically. This is a big
             // hack but shouldn't be long for this world.
             $placeholder_editor = new PhabricatorUserProfileEditor();
             $field_errors = $aux_field->validateApplicationTransactions($placeholder_editor, PhabricatorTransactions::TYPE_CUSTOMFIELD, array(id(new ManiphestTransaction())->setOldValue($aux_old_value)->setNewValue($aux_new_value)));
             foreach ($field_errors as $error) {
                 $errors[] = $error->getMessage();
             }
             $old_values[$aux_field->getFieldKey()] = $aux_old_value;
         }
         if ($errors) {
             $task->setTitle($new_title);
             $task->setDescription($new_desc);
             $task->setPriority($request->getInt('priority'));
             $task->setOwnerPHID($owner_phid);
             $task->setCCPHIDs($request->getArr('cc'));
             $task->attachProjectPHIDs($request->getArr('projects'));
         } else {
             if ($can_edit_priority) {
                 $changes[ManiphestTransaction::TYPE_PRIORITY] = $request->getInt('priority');
             }
             if ($can_edit_assign) {
                 $changes[ManiphestTransaction::TYPE_OWNER] = $owner_phid;
             }
             $changes[ManiphestTransaction::TYPE_CCS] = $request->getArr('cc');
             if ($can_edit_projects) {
                 $projects = $request->getArr('projects');
                 $changes[ManiphestTransaction::TYPE_PROJECTS] = $projects;
                 $column_phid = $request->getStr('columnPHID');
                 // allow for putting a task in a project column at creation -only-
                 if (!$task->getID() && $column_phid && $projects) {
                     $column = id(new PhabricatorProjectColumnQuery())->setViewer($user)->withProjectPHIDs($projects)->withPHIDs(array($column_phid))->executeOne();
                     if ($column) {
                         $changes[ManiphestTransaction::TYPE_PROJECT_COLUMN] = array('new' => array('projectPHID' => $column->getProjectPHID(), 'columnPHIDs' => array($column_phid)), 'old' => array('projectPHID' => $column->getProjectPHID(), 'columnPHIDs' => array()));
                     }
                 }
             }
             if ($can_edit_policies) {
                 $changes[PhabricatorTransactions::TYPE_VIEW_POLICY] = $request->getStr('viewPolicy');
                 $changes[PhabricatorTransactions::TYPE_EDIT_POLICY] = $request->getStr('editPolicy');
             }
             $template = new ManiphestTransaction();
             $transactions = array();
             foreach ($changes as $type => $value) {
                 $transaction = clone $template;
                 $transaction->setTransactionType($type);
                 if ($type == ManiphestTransaction::TYPE_PROJECT_COLUMN) {
                     $transaction->setNewValue($value['new']);
                     $transaction->setOldValue($value['old']);
                 } else {
                     if ($type == ManiphestTransaction::TYPE_PROJECTS) {
                         // TODO: Gross.
                         $project_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
                         $transaction->setTransactionType(PhabricatorTransactions::TYPE_EDGE)->setMetadataValue('edge:type', $project_type)->setNewValue(array('=' => array_fuse($value)));
                     } else {
                         $transaction->setNewValue($value);
                     }
                 }
                 $transactions[] = $transaction;
             }
             if ($aux_fields) {
                 foreach ($aux_fields as $aux_field) {
                     $transaction = clone $template;
                     $transaction->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD);
                     $aux_key = $aux_field->getFieldKey();
                     $transaction->setMetadataValue('customfield:key', $aux_key);
                     $old = idx($old_values, $aux_key);
                     $new = $aux_field->getNewValueForApplicationTransactions();
                     $transaction->setOldValue($old);
                     $transaction->setNewValue($new);
                     $transactions[] = $transaction;
                 }
             }
             if ($transactions) {
                 $is_new = !$task->getID();
                 $event = new PhabricatorEvent(PhabricatorEventType::TYPE_MANIPHEST_WILLEDITTASK, array('task' => $task, 'new' => $is_new, 'transactions' => $transactions));
                 $event->setUser($user);
                 $event->setAphrontRequest($request);
                 PhutilEventEngine::dispatchEvent($event);
                 $task = $event->getValue('task');
                 $transactions = $event->getValue('transactions');
                 $editor = id(new ManiphestTransactionEditor())->setActor($user)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true)->applyTransactions($task, $transactions);
                 $event = new PhabricatorEvent(PhabricatorEventType::TYPE_MANIPHEST_DIDEDITTASK, array('task' => $task, 'new' => $is_new, 'transactions' => $transactions));
                 $event->setUser($user);
                 $event->setAphrontRequest($request);
                 PhutilEventEngine::dispatchEvent($event);
             }
             if ($parent_task) {
                 // TODO: This should be transactional now.
                 id(new PhabricatorEdgeEditor())->addEdge($parent_task->getPHID(), PhabricatorEdgeConfig::TYPE_TASK_DEPENDS_ON_TASK, $task->getPHID())->save();
                 $workflow = $parent_task->getID();
             }
             if ($request->isAjax()) {
                 switch ($response_type) {
                     case 'card':
                         $owner = null;
                         if ($task->getOwnerPHID()) {
                             $owner = id(new PhabricatorHandleQuery())->setViewer($user)->withPHIDs(array($task->getOwnerPHID()))->executeOne();
                         }
                         $tasks = id(new ProjectBoardTaskCard())->setViewer($user)->setTask($task)->setOwner($owner)->setCanEdit(true)->getItem();
                         $column = id(new PhabricatorProjectColumnQuery())->setViewer($user)->withPHIDs(array($request->getStr('columnPHID')))->executeOne();
                         if (!$column) {
                             return new Aphront404Response();
                         }
                         $positions = id(new PhabricatorProjectColumnPositionQuery())->setViewer($user)->withColumns(array($column))->execute();
                         $task_phids = mpull($positions, 'getObjectPHID');
                         $column_tasks = id(new ManiphestTaskQuery())->setViewer($user)->withPHIDs($task_phids)->execute();
                         if ($order == PhabricatorProjectColumn::ORDER_NATURAL) {
                             // TODO: This is a little bit awkward, because PHP and JS use
                             // slightly different sort order parameters to achieve the same
                             // effect. It would be unify this a bit at some point.
                             $sort_map = array();
                             foreach ($positions as $position) {
                                 $sort_map[$position->getObjectPHID()] = array(-$position->getSequence(), $position->getID());
                             }
                         } else {
                             $sort_map = mpull($column_tasks, 'getPrioritySortVector', 'getPHID');
                         }
                         $data = array('sortMap' => $sort_map);
                         break;
                     case 'task':
                     default:
                         $tasks = $this->renderSingleTask($task);
                         $data = array();
                         break;
                 }
                 return id(new AphrontAjaxResponse())->setContent(array('tasks' => $tasks, 'data' => $data));
             }
             $redirect_uri = '/T' . $task->getID();
             if ($workflow) {
                 $redirect_uri .= '?workflow=' . $workflow;
             }
             return id(new AphrontRedirectResponse())->setURI($redirect_uri);
         }
     } else {
         if (!$task->getID()) {
             $task->setCCPHIDs(array($user->getPHID()));
             if ($template_id) {
                 $template_task = id(new ManiphestTaskQuery())->setViewer($user)->withIDs(array($template_id))->executeOne();
                 if ($template_task) {
                     $cc_phids = array_unique(array_merge($template_task->getCCPHIDs(), array($user->getPHID())));
                     $task->setCCPHIDs($cc_phids);
                     $task->attachProjectPHIDs($template_task->getProjectPHIDs());
                     $task->setOwnerPHID($template_task->getOwnerPHID());
                     $task->setPriority($template_task->getPriority());
                     $task->setViewPolicy($template_task->getViewPolicy());
                     $task->setEditPolicy($template_task->getEditPolicy());
                     $template_fields = PhabricatorCustomField::getObjectFields($template_task, PhabricatorCustomField::ROLE_EDIT);
                     $fields = $template_fields->getFields();
                     foreach ($fields as $key => $field) {
                         if (!$field->shouldCopyWhenCreatingSimilarTask()) {
                             unset($fields[$key]);
                         }
                         if (empty($aux_fields[$key])) {
                             unset($fields[$key]);
                         }
                     }
                     if ($fields) {
                         id(new PhabricatorCustomFieldList($fields))->setViewer($user)->readFieldsFromStorage($template_task);
                         foreach ($fields as $key => $field) {
                             $aux_fields[$key]->setValueFromStorage($field->getValueForStorage());
                         }
                     }
                 }
             }
         }
     }
     $phids = array_merge(array($task->getOwnerPHID()), $task->getCCPHIDs(), $task->getProjectPHIDs());
     if ($parent_task) {
         $phids[] = $parent_task->getPHID();
     }
     $phids = array_filter($phids);
     $phids = array_unique($phids);
     $handles = $this->loadViewerHandles($phids);
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setErrors($errors);
     }
     $priority_map = ManiphestTaskPriority::getTaskPriorityMap();
     if ($task->getOwnerPHID()) {
         $assigned_value = array($handles[$task->getOwnerPHID()]);
     } else {
         $assigned_value = array();
     }
     if ($task->getCCPHIDs()) {
         $cc_value = array_select_keys($handles, $task->getCCPHIDs());
     } else {
         $cc_value = array();
     }
     if ($task->getProjectPHIDs()) {
         $projects_value = array_select_keys($handles, $task->getProjectPHIDs());
     } else {
         $projects_value = array();
     }
     $cancel_id = nonempty($task->getID(), $template_id);
     if ($cancel_id) {
         $cancel_uri = '/T' . $cancel_id;
     } else {
         $cancel_uri = '/maniphest/';
     }
     if ($task->getID()) {
         $button_name = pht('Save Task');
         $header_name = pht('Edit Task');
     } else {
         if ($parent_task) {
             $cancel_uri = '/T' . $parent_task->getID();
             $button_name = pht('Create Task');
             $header_name = pht('Create New Subtask');
         } else {
             $button_name = pht('Create Task');
             $header_name = pht('Create New Task');
         }
     }
     require_celerity_resource('maniphest-task-edit-css');
     $project_tokenizer_id = celerity_generate_unique_node_id();
     $form = new AphrontFormView();
     $form->setUser($user)->addHiddenInput('template', $template_id)->addHiddenInput('responseType', $response_type)->addHiddenInput('order', $order)->addHiddenInput('ungrippable', $request->getStr('ungrippable'))->addHiddenInput('columnPHID', $request->getStr('columnPHID'));
     if ($parent_task) {
         $form->appendChild(id(new AphrontFormStaticControl())->setLabel(pht('Parent Task'))->setValue($handles[$parent_task->getPHID()]->getFullName()))->addHiddenInput('parent', $parent_task->getID());
     }
     $form->appendChild(id(new AphrontFormTextAreaControl())->setLabel(pht('Title'))->setName('title')->setError($e_title)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)->setValue($task->getTitle()));
     if ($can_edit_status) {
         // See T4819.
         $status_map = ManiphestTaskStatus::getTaskStatusMap();
         $dup_status = ManiphestTaskStatus::getDuplicateStatus();
         if ($task->getStatus() != $dup_status) {
             unset($status_map[$dup_status]);
         }
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Status'))->setName('status')->setValue($task->getStatus())->setOptions($status_map));
     }
     $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($task)->execute();
     if ($can_edit_assign) {
         $form->appendChild(id(new AphrontFormTokenizerControl())->setLabel(pht('Assigned To'))->setName('assigned_to')->setValue($assigned_value)->setUser($user)->setDatasource(new PhabricatorPeopleDatasource())->setLimit(1));
     }
     $form->appendChild(id(new AphrontFormTokenizerControl())->setLabel(pht('CC'))->setName('cc')->setValue($cc_value)->setUser($user)->setDatasource(new PhabricatorMetaMTAMailableDatasource()));
     if ($can_edit_priority) {
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Priority'))->setName('priority')->setOptions($priority_map)->setValue($task->getPriority()));
     }
     if ($can_edit_policies) {
         $form->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($task)->setPolicies($policies)->setName('viewPolicy'))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicyObject($task)->setPolicies($policies)->setName('editPolicy'));
     }
     if ($can_edit_projects) {
         $form->appendChild(id(new AphrontFormTokenizerControl())->setLabel(pht('Projects'))->setName('projects')->setValue($projects_value)->setID($project_tokenizer_id)->setCaption(javelin_tag('a', array('href' => '/project/create/', 'mustcapture' => true, 'sigil' => 'project-create'), pht('Create New Project')))->setDatasource(new PhabricatorProjectDatasource()));
     }
     $field_list->appendFieldsToForm($form);
     require_celerity_resource('aphront-error-view-css');
     Javelin::initBehavior('project-create', array('tokenizerID' => $project_tokenizer_id));
     $description_control = new PhabricatorRemarkupControl();
     // "Upsell" creating tasks via email in create flows if the instance is
     // configured for this awesomeness.
     $email_create = PhabricatorEnv::getEnvConfig('metamta.maniphest.public-create-email');
     if (!$task->getID() && $email_create) {
         $email_hint = pht('You can also create tasks by sending an email to: %s', phutil_tag('tt', array(), $email_create));
         $description_control->setCaption($email_hint);
     }
     $description_control->setLabel(pht('Description'))->setName('description')->setID('description-textarea')->setValue($task->getDescription())->setUser($user);
     $form->appendChild($description_control);
     if ($request->isAjax()) {
         $dialog = id(new AphrontDialogView())->setUser($user)->setWidth(AphrontDialogView::WIDTH_FULL)->setTitle($header_name)->appendChild(array($error_view, $form->buildLayoutView()))->addCancelButton($cancel_uri)->addSubmitButton($button_name);
         return id(new AphrontDialogResponse())->setDialog($dialog);
     }
     $form->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($button_name));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($header_name)->setFormErrors($errors)->setForm($form);
     $preview = id(new PHUIRemarkupPreviewPanel())->setHeader(pht('Description Preview'))->setControlID('description-textarea')->setPreviewURI($this->getApplicationURI('task/descriptionpreview/'));
     if ($task->getID()) {
         $page_objects = array($task->getPHID());
     } else {
         $page_objects = array();
     }
     $crumbs = $this->buildApplicationCrumbs();
     if ($task->getID()) {
         $crumbs->addTextCrumb('T' . $task->getID(), '/T' . $task->getID());
     }
     $crumbs->addTextCrumb($header_name);
     return $this->buildApplicationPage(array($crumbs, $form_box, $preview), array('title' => $header_name, 'pageObjects' => $page_objects));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($this->id) {
         $package = id(new PhabricatorOwnersPackage())->load($this->id);
         if (!$package) {
             return new Aphront404Response();
         }
     } else {
         $package = new PhabricatorOwnersPackage();
         $package->setPrimaryOwnerPHID($user->getPHID());
     }
     $e_name = true;
     $e_primary = true;
     $e_owners = true;
     $errors = array();
     if ($request->isFormPost()) {
         $package->setName($request->getStr('name'));
         $package->setDescription($request->getStr('description'));
         $primary = $request->getArr('primary');
         $primary = reset($primary);
         $package->setPrimaryOwnerPHID($primary);
         $owners = $request->getArr('owners');
         if ($primary) {
             array_unshift($owners, $primary);
         }
         $owners = array_unique($owners);
         $paths = $request->getArr('path');
         $repos = $request->getArr('repo');
         $path_refs = array();
         for ($ii = 0; $ii < count($paths); $ii++) {
             if (empty($paths[$ii]) || empty($repos[$ii])) {
                 continue;
             }
             $path_refs[] = array('repositoryPHID' => $repos[$ii], 'path' => $paths[$ii]);
         }
         if (!strlen($package->getName())) {
             $e_name = 'Required';
             $errors[] = 'Package name is required.';
         } else {
             $e_name = null;
         }
         if (!$package->getPrimaryOwnerPHID()) {
             $e_primary = 'Required';
             $errors[] = 'Package must have a primary owner.';
         } else {
             $e_primary = null;
         }
         if (!$owners) {
             $e_owners = 'Required';
             $errors[] = 'Package must have at least one owner.';
         } else {
             $e_owners = null;
         }
         if (!$path_refs) {
             $errors[] = 'Package must include at least one path.';
         }
         if (!$errors) {
             $package->attachUnsavedOwners($owners);
             $package->attachUnsavedPaths($path_refs);
             try {
                 $package->save();
                 return id(new AphrontRedirectResponse())->setURI('/owners/package/' . $package->getID() . '/');
             } catch (AphrontQueryDuplicateKeyException $ex) {
                 $e_name = 'Duplicate';
                 $errors[] = 'Package name must be unique.';
             }
         }
     } else {
         $owners = $package->loadOwners();
         $owners = mpull($owners, 'getUserPHID');
         $paths = $package->loadPaths();
         $path_refs = array();
         foreach ($paths as $path) {
             $path_refs[] = array('repositoryPHID' => $path->getRepositoryPHID(), 'path' => $path->getPath());
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Package Errors');
         $error_view->setErrors($errors);
     }
     $handles = id(new PhabricatorObjectHandleData($owners))->loadHandles();
     $primary = $package->getPrimaryOwnerPHID();
     if ($primary && isset($handles[$primary])) {
         $token_primary_owner = array($primary => $handles[$primary]->getFullName());
     } else {
         $token_primary_owner = array();
     }
     $token_all_owners = array_select_keys($handles, $owners);
     $token_all_owners = mpull($token_all_owners, 'getFullName');
     $title = $package->getID() ? 'Edit Package' : 'New Package';
     $repos = id(new PhabricatorRepository())->loadAll();
     $default_paths = array();
     foreach ($repos as $repo) {
         $default_path = $repo->getDetail('default-owners-path');
         if ($default_path) {
             $default_paths[$repo->getPHID()] = $default_path;
         }
     }
     $repos = mpull($repos, 'getCallsign', 'getPHID');
     $template = new AphrontTypeaheadTemplateView();
     $template = $template->render();
     Javelin::initBehavior('owners-path-editor', array('root' => 'path-editor', 'table' => 'paths', 'add_button' => 'addpath', 'repositories' => $repos, 'input_template' => $template, 'pathRefs' => $path_refs, 'completeURI' => '/diffusion/services/path/complete/', 'validateURI' => '/diffusion/services/path/validate/', 'repositoryDefaultPaths' => $default_paths));
     require_celerity_resource('owners-path-editor-css');
     $cancel_uri = $package->getID() ? '/owners/package/' . $package->getID() . '/' : '/owners/';
     $form = id(new AphrontFormView())->setUser($user)->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($package->getName())->setError($e_name))->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/users/')->setLabel('Primary Owner')->setName('primary')->setLimit(1)->setValue($token_primary_owner)->setError($e_primary))->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/users/')->setLabel('Owners')->setName('owners')->setValue($token_all_owners)->setError($e_owners))->appendChild('<h1>Paths</h1>' . '<div class="aphront-form-inset" id="path-editor">' . '<div style="float: right;">' . javelin_render_tag('a', array('href' => '#', 'class' => 'button green', 'sigil' => 'addpath', 'mustcapture' => true), 'Add New Path') . '</div>' . '<p>Specify the files and directories which comprise this ' . 'package.</p>' . '<div style="clear: both;"></div>' . javelin_render_tag('table', array('class' => 'owners-path-editor-table', 'sigil' => 'paths'), '') . '</div>')->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Description')->setName('description')->setValue($package->getDescription()))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue('Save Package'));
     $panel = new AphrontPanelView();
     $panel->setHeader($title);
     $panel->setWidth(AphrontPanelView::WIDTH_WIDE);
     $panel->appendChild($error_view);
     $panel->appendChild($form);
     return $this->buildStandardPageResponse($panel, array('title' => $title));
 }
 public function processRequest()
 {
     if (!$this->provider->isProviderEnabled()) {
         return new Aphront400Response();
     }
     $current_user = $this->getRequest()->getUser();
     $request = $this->getRequest();
     $ldap_username = $request->getCookie('phusr');
     if ($request->isFormPost()) {
         $ldap_username = $request->getStr('username');
         try {
             $envelope = new PhutilOpaqueEnvelope($request->getStr('password'));
             $this->provider->auth($ldap_username, $envelope);
         } catch (Exception $e) {
             $errors[] = $e->getMessage();
         }
         if (empty($errors)) {
             $ldap_info = $this->retrieveLDAPInfo($this->provider);
             if ($current_user->getPHID()) {
                 if ($ldap_info->getID()) {
                     $existing_ldap = id(new PhabricatorUserLDAPInfo())->loadOneWhere('userID = %d', $current_user->getID());
                     if ($ldap_info->getUserID() != $current_user->getID() || $existing_ldap) {
                         $dialog = new AphrontDialogView();
                         $dialog->setUser($current_user);
                         $dialog->setTitle('Already Linked to Another Account');
                         $dialog->appendChild('<p>The LDAP account you just authorized is already linked to ' . 'another Phabricator account. Before you can link it to a ' . 'different LDAP account, you must unlink the old account.</p>');
                         $dialog->addCancelButton('/settings/page/ldap/');
                         return id(new AphrontDialogResponse())->setDialog($dialog);
                     } else {
                         return id(new AphrontRedirectResponse())->setURI('/settings/page/ldap/');
                     }
                 }
                 if (!$request->isDialogFormPost()) {
                     $dialog = new AphrontDialogView();
                     $dialog->setUser($current_user);
                     $dialog->setTitle('Link LDAP Account');
                     $dialog->appendChild('<p>Link your LDAP account to your Phabricator account?</p>');
                     $dialog->addHiddenInput('username', $request->getStr('username'));
                     $dialog->addHiddenInput('password', $request->getStr('password'));
                     $dialog->addSubmitButton('Link Accounts');
                     $dialog->addCancelButton('/settings/page/ldap/');
                     return id(new AphrontDialogResponse())->setDialog($dialog);
                 }
                 $ldap_info->setUserID($current_user->getID());
                 $this->saveLDAPInfo($ldap_info);
                 return id(new AphrontRedirectResponse())->setURI('/settings/page/ldap/');
             }
             if ($ldap_info->getID()) {
                 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
                 $known_user = id(new PhabricatorUser())->load($ldap_info->getUserID());
                 $session_key = $known_user->establishSession('web');
                 $this->saveLDAPInfo($ldap_info);
                 $request->setCookie('phusr', $known_user->getUsername());
                 $request->setCookie('phsid', $session_key);
                 $uri = new PhutilURI('/login/validate/');
                 $uri->setQueryParams(array('phusr' => $known_user->getUsername()));
                 return id(new AphrontRedirectResponse())->setURI((string) $uri);
             }
             $controller = newv('PhabricatorLDAPRegistrationController', array($this->getRequest()));
             $controller->setLDAPProvider($this->provider);
             $controller->setLDAPInfo($ldap_info);
             return $this->delegateToController($controller);
         }
     }
     $ldap_form = new AphrontFormView();
     $ldap_form->setUser($request->getUser())->setAction('/ldap/login/')->appendChild(id(new AphrontFormTextControl())->setLabel('LDAP username')->setName('username')->setValue($ldap_username))->appendChild(id(new AphrontFormPasswordControl())->setLabel('Password')->setName('password'));
     $ldap_form->appendChild(id(new AphrontFormSubmitControl())->setValue('Login'));
     $panel = new AphrontPanelView();
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild('<h1>LDAP login</h1>');
     $panel->appendChild($ldap_form);
     if (isset($errors) && count($errors) > 0) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Login Failed');
         $error_view->setErrors($errors);
     }
     return $this->buildStandardPageResponse(array(isset($error_view) ? $error_view : null, $panel), array('title' => 'Login'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $editable = $this->getAccountEditable();
     // There's no sense in showing a change password panel if the user
     // can't change their password
     if (!$editable || !PhabricatorEnv::getEnvConfig('auth.password-auth-enabled')) {
         return new Aphront400Response();
     }
     $min_len = PhabricatorEnv::getEnvConfig('account.minimum-password-length');
     $min_len = (int) $min_len;
     // NOTE: To change your password, you need to prove you own the account,
     // either by providing the old password or by carrying a token to
     // the workflow from a password reset email.
     $token = $request->getStr('token');
     if ($token) {
         $valid_token = $user->validateEmailToken($token);
     } else {
         $valid_token = false;
     }
     $e_old = true;
     $e_new = true;
     $e_conf = true;
     $errors = array();
     if ($request->isFormPost()) {
         if (!$valid_token) {
             if (!$user->comparePassword($request->getStr('old_pw'))) {
                 $errors[] = 'The old password you entered is incorrect.';
                 $e_old = 'Invalid';
             }
         }
         $pass = $request->getStr('new_pw');
         $conf = $request->getStr('conf_pw');
         if (strlen($pass) < $min_len) {
             $errors[] = 'Your new password is too short.';
             $e_new = 'Too Short';
         }
         if ($pass !== $conf) {
             $errors[] = 'New password and confirmation do not match.';
             $e_conf = 'Invalid';
         }
         if (!$errors) {
             $user->setPassword($pass);
             // This write is unguarded because the CSRF token has already
             // been checked in the call to $request->isFormPost() and
             // the CSRF token depends on the password hash, so when it
             // is changed here the CSRF token check will fail.
             $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
             $user->save();
             unset($unguarded);
             if ($valid_token) {
                 // If this is a password set/reset, kick the user to the home page
                 // after we update their account.
                 $next = '/';
             } else {
                 $next = '/settings/page/password/?saved=true';
             }
             return id(new AphrontRedirectResponse())->setURI($next);
         }
     }
     $notice = null;
     if (!$errors) {
         if ($request->getStr('saved')) {
             $notice = new AphrontErrorView();
             $notice->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
             $notice->setTitle('Changes Saved');
             $notice->appendChild('<p>Your password has been updated.</p>');
         }
     } else {
         $notice = new AphrontErrorView();
         $notice->setTitle('Error Changing Password');
         $notice->setErrors($errors);
     }
     $len_caption = null;
     if ($min_len) {
         $len_caption = 'Minimum password length: ' . $min_len . ' characters.';
     }
     $form = new AphrontFormView();
     $form->setUser($user)->addHiddenInput('token', $token);
     if (!$valid_token) {
         $form->appendChild(id(new AphrontFormPasswordControl())->setLabel('Old Password')->setError($e_old)->setName('old_pw'));
     }
     $form->appendChild(id(new AphrontFormPasswordControl())->setLabel('New Password')->setError($e_new)->setName('new_pw'));
     $form->appendChild(id(new AphrontFormPasswordControl())->setLabel('Confirm Password')->setCaption($len_caption)->setError($e_conf)->setName('conf_pw'));
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Save'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Change Password');
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     return id(new AphrontNullView())->appendChild(array($notice, $panel));
 }
    public function processRequest()
    {
        $request = $this->getRequest();
        if (!PhabricatorPasswordAuthProvider::getPasswordProvider()) {
            return new Aphront400Response();
        }
        $e_email = true;
        $e_captcha = true;
        $errors = array();
        $is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
        if ($request->isFormPost()) {
            $e_email = null;
            $e_captcha = pht('Again');
            $captcha_ok = AphrontFormRecaptchaControl::processCaptcha($request);
            if (!$captcha_ok) {
                $errors[] = pht('Captcha response is incorrect, try again.');
                $e_captcha = pht('Invalid');
            }
            $email = $request->getStr('email');
            if (!strlen($email)) {
                $errors[] = pht('You must provide an email address.');
                $e_email = pht('Required');
            }
            if (!$errors) {
                // NOTE: Don't validate the email unless the captcha is good; this makes
                // it expensive to fish for valid email addresses while giving the user
                // a better error if they goof their email.
                $target_email = id(new PhabricatorUserEmail())->loadOneWhere('address = %s', $email);
                $target_user = null;
                if ($target_email) {
                    $target_user = id(new PhabricatorUser())->loadOneWhere('phid = %s', $target_email->getUserPHID());
                }
                if (!$target_user) {
                    $errors[] = pht('There is no account associated with that email address.');
                    $e_email = pht('Invalid');
                }
                // If this address is unverified, only send a reset link to it if
                // the account has no verified addresses. This prevents an opportunistic
                // attacker from compromising an account if a user adds an email
                // address but mistypes it and doesn't notice.
                // (For a newly created account, all the addresses may be unverified,
                // which is why we'll send to an unverified address in that case.)
                if ($target_email && !$target_email->getIsVerified()) {
                    $verified_addresses = id(new PhabricatorUserEmail())->loadAllWhere('userPHID = %s AND isVerified = 1', $target_email->getUserPHID());
                    if ($verified_addresses) {
                        $errors[] = pht('That email addess is not verified. You can only send ' . 'password reset links to a verified address.');
                        $e_email = pht('Unverified');
                    }
                }
                if (!$errors) {
                    $engine = new PhabricatorAuthSessionEngine();
                    $uri = $engine->getOneTimeLoginURI($target_user, null, PhabricatorAuthSessionEngine::ONETIME_RESET);
                    if ($is_serious) {
                        $body = <<<EOBODY
You can use this link to reset your Phabricator password:

  {$uri}

EOBODY;
                    } else {
                        $body = <<<EOBODY
Condolences on forgetting your password. You can use this link to reset it:

  {$uri}

After you set a new password, consider writing it down on a sticky note and
attaching it to your monitor so you don't forget again! Choosing a very short,
easy-to-remember password like "cat" or "1234" might also help.

Best Wishes,
Phabricator

EOBODY;
                    }
                    $mail = id(new PhabricatorMetaMTAMail())->setSubject(pht('[Phabricator] Password Reset'))->setForceDelivery(true)->addRawTos(array($target_email->getAddress()))->setBody($body)->saveAndSend();
                    return $this->newDialog()->setTitle(pht('Check Your Email'))->setShortTitle(pht('Email Sent'))->appendParagraph(pht('An email has been sent with a link you can use to login.'))->addCancelButton('/', pht('Done'));
                }
            }
        }
        $error_view = null;
        if ($errors) {
            $error_view = new AphrontErrorView();
            $error_view->setErrors($errors);
        }
        $email_auth = new PHUIFormLayoutView();
        $email_auth->appendChild($error_view);
        $email_auth->setUser($request->getUser())->setFullWidth(true)->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Email'))->setName('email')->setValue($request->getStr('email'))->setError($e_email))->appendChild(id(new AphrontFormRecaptchaControl())->setLabel(pht('Captcha'))->setError($e_captcha));
        $crumbs = $this->buildApplicationCrumbs();
        $crumbs->addTextCrumb(pht('Reset Password'));
        $dialog = new AphrontDialogView();
        $dialog->setUser($request->getUser());
        $dialog->setTitle(pht('Forgot Password / Email Login'));
        $dialog->appendChild($email_auth);
        $dialog->addSubmitButton(pht('Send Email'));
        $dialog->setSubmitURI('/login/email/');
        return $this->buildApplicationPage(array($crumbs, $dialog), array('title' => pht('Forgot Password')));
    }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $profile = id(new PhabricatorUserProfile())->loadOneWhere('userPHID = %s', $user->getPHID());
     if (!$profile) {
         $profile = new PhabricatorUserProfile();
         $profile->setUserPHID($user->getPHID());
     }
     $supported_formats = PhabricatorFile::getTransformableImageFormats();
     $e_image = null;
     $errors = array();
     if ($request->isFormPost()) {
         $profile->setTitle($request->getStr('title'));
         $profile->setBlurb($request->getStr('blurb'));
         if (!empty($_FILES['image'])) {
             $err = idx($_FILES['image'], 'error');
             if ($err != UPLOAD_ERR_NO_FILE) {
                 $file = PhabricatorFile::newFromPHPUpload($_FILES['image'], array('authorPHID' => $user->getPHID()));
                 $okay = $file->isTransformableImage();
                 if ($okay) {
                     $xformer = new PhabricatorImageTransformer();
                     // Generate the large picture for the profile page.
                     $large_xformed = $xformer->executeProfileTransform($file, $width = 280, $min_height = 140, $max_height = 420);
                     $profile->setProfileImagePHID($large_xformed->getPHID());
                     // Generate the small picture for comments, etc.
                     $small_xformed = $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
                     $user->setProfileImagePHID($small_xformed->getPHID());
                 } else {
                     $e_image = 'Not Supported';
                     $errors[] = 'This server only supports these image formats: ' . implode(', ', $supported_formats) . '.';
                 }
             }
         }
         if (!$errors) {
             $user->save();
             $profile->save();
             $response = id(new AphrontRedirectResponse())->setURI('/settings/page/profile/?saved=true');
             return $response;
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     } else {
         if ($request->getStr('saved')) {
             $error_view = new AphrontErrorView();
             $error_view->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
             $error_view->setTitle('Changes Saved');
             $error_view->appendChild('<p>Your changes have been saved.</p>');
             $error_view = $error_view->render();
         }
     }
     $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $user->getProfileImagePHID());
     if ($file) {
         $img_src = $file->getBestURI();
     } else {
         $img_src = null;
     }
     $profile_uri = PhabricatorEnv::getURI('/p/' . $user->getUsername() . '/');
     $form = new AphrontFormView();
     $form->setUser($request->getUser())->setAction('/settings/page/profile/')->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setName('title')->setValue($profile->getTitle())->setCaption('Serious business title.'))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Profile URI')->setValue(phutil_render_tag('a', array('href' => $profile_uri), phutil_escape_html($profile_uri))))->appendChild('<p class="aphront-form-instructions">Write something about yourself! ' . 'Make sure to include <strong>important information</strong> like ' . 'your favorite pokemon and which Starcraft race you play.</p>')->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Blurb')->setName('blurb')->setValue($profile->getBlurb()))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Profile Image')->setValue(phutil_render_tag('img', array('src' => $img_src))))->appendChild(id(new AphrontFormFileControl())->setLabel('Change Image')->setName('image')->setError($e_image)->setCaption('Supported formats: ' . implode(', ', $supported_formats)))->appendChild(id(new AphrontFormSubmitControl())->setValue('Save')->addCancelButton('/p/' . $user->getUsername() . '/'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Edit Profile Details');
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     return id(new AphrontNullView())->appendChild(array($error_view, $panel));
 }
예제 #18
0
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $content_type_map = HeraldContentTypeConfig::getContentTypeMap();
     $rule_type_map = HeraldRuleTypeConfig::getRuleTypeMap();
     if ($this->id) {
         $rule = id(new HeraldRule())->load($this->id);
         if (!$rule) {
             return new Aphront404Response();
         }
         if (!$this->canEditRule($rule, $user)) {
             throw new Exception("You don't own this rule and can't edit it.");
         }
     } else {
         $rule = new HeraldRule();
         $rule->setAuthorPHID($user->getPHID());
         $rule->setMustMatchAll(true);
         $content_type = $request->getStr('content_type');
         if (!isset($content_type_map[$content_type])) {
             $content_type = HeraldContentTypeConfig::CONTENT_TYPE_DIFFERENTIAL;
         }
         $rule->setContentType($content_type);
         $rule_type = $request->getStr('rule_type');
         if (!isset($rule_type_map[$rule_type])) {
             $rule_type = HeraldRuleTypeConfig::RULE_TYPE_GLOBAL;
         }
         $rule->setRuleType($rule_type);
     }
     $local_version = id(new HeraldRule())->getConfigVersion();
     if ($rule->getConfigVersion() > $local_version) {
         throw new Exception("This rule was created with a newer version of Herald. You can not " . "view or edit it in this older version. Upgrade your Phabricator " . "deployment.");
     }
     // Upgrade rule version to our version, since we might add newly-defined
     // conditions, etc.
     $rule->setConfigVersion($local_version);
     $rule_conditions = $rule->loadConditions();
     $rule_actions = $rule->loadActions();
     $rule->attachConditions($rule_conditions);
     $rule->attachActions($rule_actions);
     $e_name = true;
     $errors = array();
     if ($request->isFormPost() && $request->getStr('save')) {
         list($e_name, $errors) = $this->saveRule($rule, $request);
         if (!$errors) {
             $uri = '/herald/view/' . $rule->getContentType() . '/' . $rule->getRuleType() . '/';
             return id(new AphrontRedirectResponse())->setURI($uri);
         }
     }
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     } else {
         $error_view = null;
     }
     $must_match_selector = $this->renderMustMatchSelector($rule);
     $repetition_selector = $this->renderRepetitionSelector($rule);
     $handles = $this->loadHandlesForRule($rule);
     require_celerity_resource('herald-css');
     $content_type_name = $content_type_map[$rule->getContentType()];
     $rule_type_name = $rule_type_map[$rule->getRuleType()];
     $form = id(new AphrontFormView())->setUser($user)->setID('herald-rule-edit-form')->addHiddenInput('content_type', $rule->getContentType())->addHiddenInput('rule_type', $rule->getRuleType())->addHiddenInput('save', 1)->appendChild(javelin_render_tag('input', array('type' => 'hidden', 'name' => 'rule', 'sigil' => 'rule')))->appendChild(id(new AphrontFormTextControl())->setLabel('Rule Name')->setName('name')->setError($e_name)->setValue($rule->getName()));
     $form->appendChild(id(new AphrontFormMarkupControl())->setValue("This <strong>{$rule_type_name}</strong> rule triggers for " . "<strong>{$content_type_name}</strong>."))->appendChild(id(new AphrontFormInsetView())->setTitle('Conditions')->setRightButton(javelin_render_tag('a', array('href' => '#', 'class' => 'button green', 'sigil' => 'create-condition', 'mustcapture' => true), 'Create New Condition'))->setDescription('When ' . $must_match_selector . ' these conditions are met:')->setContent(javelin_render_tag('table', array('sigil' => 'rule-conditions', 'class' => 'herald-condition-table'), '')))->appendChild(id(new AphrontFormInsetView())->setTitle('Action')->setRightButton(javelin_render_tag('a', array('href' => '#', 'class' => 'button green', 'sigil' => 'create-action', 'mustcapture' => true), 'Create New Action'))->setDescription('Take these actions ' . $repetition_selector . ' this rule matches:')->setContent(javelin_render_tag('table', array('sigil' => 'rule-actions', 'class' => 'herald-action-table'), '')))->appendChild(id(new AphrontFormSubmitControl())->setValue('Save Rule')->addCancelButton('/herald/view/' . $rule->getContentType() . '/'));
     $this->setupEditorBehavior($rule, $handles);
     $panel = new AphrontPanelView();
     $panel->setHeader($rule->getID() ? 'Edit Herald Rule' : 'Create Herald Rule');
     $panel->setWidth(AphrontPanelView::WIDTH_WIDE);
     $panel->appendChild($form);
     $nav = $this->renderNav();
     $nav->selectFilter('view/' . $rule->getContentType() . '/' . $rule->getRuleType());
     $nav->appendChild(array($error_view, $panel));
     return $this->buildStandardPageResponse($nav, array('title' => 'Edit Rule'));
 }
 public function processRequest(AphrontRequest $request)
 {
     $user = $request->getUser();
     $edit = $request->getStr('edit');
     $delete = $request->getStr('delete');
     if (!$edit && !$delete) {
         return $this->renderKeyListView($request);
     }
     $id = nonempty($edit, $delete);
     if ($id && is_numeric($id)) {
         // NOTE: Prevent editing/deleting of keys you don't own.
         $key = id(new PhabricatorUserSSHKey())->loadOneWhere('userPHID = %s AND id = %d', $user->getPHID(), (int) $id);
         if (!$key) {
             return new Aphront404Response();
         }
     } else {
         $key = new PhabricatorUserSSHKey();
         $key->setUserPHID($user->getPHID());
     }
     if ($delete) {
         return $this->processDelete($request, $key);
     }
     $e_name = true;
     $e_key = true;
     $errors = array();
     $entire_key = $key->getEntireKey();
     if ($request->isFormPost()) {
         $key->setName($request->getStr('name'));
         $entire_key = $request->getStr('key');
         if (!strlen($entire_key)) {
             $errors[] = 'You must provide an SSH Public Key.';
             $e_key = 'Required';
         } else {
             $parts = str_replace("\n", '', trim($entire_key));
             $parts = preg_split('/\\s+/', $parts);
             if (count($parts) == 2) {
                 $parts[] = '';
                 // Add an empty comment part.
             } else {
                 if (count($parts) == 3) {
                     // This is the expected case.
                 } else {
                     if (preg_match('/private\\s*key/i', $entire_key)) {
                         // Try to give the user a better error message if it looks like
                         // they uploaded a private key.
                         $e_key = 'Invalid';
                         $errors[] = 'Provide your public key, not your private key!';
                     } else {
                         $e_key = 'Invalid';
                         $errors[] = 'Provided public key is not properly formatted.';
                     }
                 }
             }
             if (!$errors) {
                 list($type, $body, $comment) = $parts;
                 if (!preg_match('/^ssh-dsa|ssh-rsa$/', $type)) {
                     $e_key = 'Invalid';
                     $errors[] = 'Public key should be "ssh-dsa" or "ssh-rsa".';
                 } else {
                     $key->setKeyType($type);
                     $key->setKeyBody($body);
                     $key->setKeyHash(md5($body));
                     $key->setKeyComment($comment);
                     $e_key = null;
                 }
             }
         }
         if (!strlen($key->getName())) {
             $errors[] = 'You must name this public key.';
             $e_name = 'Required';
         } else {
             $e_name = null;
         }
         if (!$errors) {
             try {
                 $key->save();
                 return id(new AphrontRedirectResponse())->setURI($this->getPanelURI());
             } catch (AphrontQueryDuplicateKeyException $ex) {
                 $e_key = 'Duplicate';
                 $errors[] = 'This public key is already associated with a user ' . 'account.';
             }
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     }
     $is_new = !$key->getID();
     if ($is_new) {
         $header = 'Add New SSH Public Key';
         $save = 'Add Key';
     } else {
         $header = 'Edit SSH Public Key';
         $save = 'Save Changes';
     }
     $form = id(new AphrontFormView())->setUser($user)->addHiddenInput('edit', $is_new ? 'true' : $key->getID())->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($key->getName())->setError($e_name))->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Public Key')->setName('key')->setValue($entire_key)->setError($e_key))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($this->getPanelURI())->setValue($save));
     $panel = new AphrontPanelView();
     $panel->setHeader($header);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     return id(new AphrontNullView())->appendChild(array($error_view, $panel));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $key = $request->getStr('key');
     if (!$key) {
         $id = nonempty($this->id, $request->getInt('id'));
         if (!$id) {
             return new Aphront404Response();
         }
         $query = id(new ManiphestSavedQuery())->load($id);
         if (!$query) {
             return new Aphront404Response();
         }
         if ($query->getUserPHID() != $user->getPHID()) {
             return new Aphront400Response();
         }
     } else {
         $query = new ManiphestSavedQuery();
         $query->setUserPHID($user->getPHID());
         $query->setQueryKey($key);
         $query->setIsDefault(0);
     }
     $e_name = true;
     $errors = array();
     if ($request->isFormPost()) {
         $e_name = null;
         $query->setName($request->getStr('name'));
         if (!strlen($query->getName())) {
             $e_name = 'Required';
             $errors[] = 'Saved query name is required.';
         }
         if (!$errors) {
             $query->save();
             return id(new AphrontRedirectResponse())->setURI('/maniphest/custom/');
         }
     }
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     } else {
         $error_view = null;
     }
     if ($query->getID()) {
         $header = 'Edit Saved Query';
         $cancel_uri = '/maniphest/custom/';
     } else {
         $header = 'New Saved Query';
         $cancel_uri = '/maniphest/view/custom/?key=' . $key;
     }
     $form = id(new AphrontFormView())->setUser($user)->addHiddenInput('key', $key)->addHiddenInput('id', $query->getID())->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setValue($query->getName())->setName('name')->setError($e_name))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue('Save'));
     $panel = new AphrontPanelView();
     $panel->setHeader($header);
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $nav = $this->buildBaseSideNav();
     // The side nav won't show "Saved Queries..." until you have at least one.
     $nav->selectFilter('saved', 'custom');
     $nav->appendChild($error_view);
     $nav->appendChild($panel);
     return $this->buildStandardPageResponse($nav, array('title' => 'Saved Queries'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $project = id(new PhabricatorProjectQuery())->setViewer($user)->withIDs(array($this->id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
     if (!$project) {
         return new Aphront404Response();
     }
     $profile = $project->loadProfile();
     if (empty($profile)) {
         $profile = new PhabricatorProjectProfile();
     }
     $img_src = $profile->loadProfileImageURI();
     $options = PhabricatorProjectStatus::getStatusMap();
     $supported_formats = PhabricatorFile::getTransformableImageFormats();
     $e_name = true;
     $e_image = null;
     $errors = array();
     if ($request->isFormPost()) {
         try {
             $xactions = array();
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_NAME);
             $xaction->setNewValue($request->getStr('name'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_STATUS);
             $xaction->setNewValue($request->getStr('status'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_CAN_VIEW);
             $xaction->setNewValue($request->getStr('can_view'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_CAN_EDIT);
             $xaction->setNewValue($request->getStr('can_edit'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_CAN_JOIN);
             $xaction->setNewValue($request->getStr('can_join'));
             $xactions[] = $xaction;
             $editor = new PhabricatorProjectEditor($project);
             $editor->setUser($user);
             $editor->applyTransactions($xactions);
         } catch (PhabricatorProjectNameCollisionException $ex) {
             $e_name = 'Not Unique';
             $errors[] = $ex->getMessage();
         }
         $profile->setBlurb($request->getStr('blurb'));
         if (!strlen($project->getName())) {
             $e_name = 'Required';
             $errors[] = 'Project name is required.';
         } else {
             $e_name = null;
         }
         $default_image = $request->getExists('default_image');
         if ($default_image) {
             $profile->setProfileImagePHID(null);
         } else {
             if (!empty($_FILES['image'])) {
                 $err = idx($_FILES['image'], 'error');
                 if ($err != UPLOAD_ERR_NO_FILE) {
                     $file = PhabricatorFile::newFromPHPUpload($_FILES['image'], array('authorPHID' => $user->getPHID()));
                     $okay = $file->isTransformableImage();
                     if ($okay) {
                         $xformer = new PhabricatorImageTransformer();
                         $xformed = $xformer->executeThumbTransform($file, $x = 50, $y = 50);
                         $profile->setProfileImagePHID($xformed->getPHID());
                     } else {
                         $e_image = 'Not Supported';
                         $errors[] = 'This server only supports these image formats: ' . implode(', ', $supported_formats) . '.';
                     }
                 }
             }
         }
         if (!$errors) {
             $project->save();
             $profile->setProjectPHID($project->getPHID());
             $profile->save();
             return id(new AphrontRedirectResponse())->setURI('/project/view/' . $project->getID() . '/');
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     }
     $header_name = 'Edit Project';
     $title = 'Edit Project';
     $action = '/project/edit/' . $project->getID() . '/';
     $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($project)->execute();
     $form = new AphrontFormView();
     $form->setID('project-edit-form')->setUser($user)->setAction($action)->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($project->getName())->setError($e_name))->appendChild(id(new AphrontFormSelectControl())->setLabel('Project Status')->setName('status')->setOptions($options)->setValue($project->getStatus()))->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Blurb')->setName('blurb')->setValue($profile->getBlurb()))->appendChild('<p class="aphront-form-instructions">NOTE: Policy settings are not ' . 'yet fully implemented. Some interfaces still ignore these settings, ' . 'particularly "Visible To".</p>')->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setName('can_view')->setCaption('Members can always view a project.')->setPolicyObject($project)->setPolicies($policies)->setCapability(PhabricatorPolicyCapability::CAN_VIEW))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setName('can_edit')->setPolicyObject($project)->setPolicies($policies)->setCapability(PhabricatorPolicyCapability::CAN_EDIT))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setName('can_join')->setCaption('Users who can edit a project can always join a project.')->setPolicyObject($project)->setPolicies($policies)->setCapability(PhabricatorPolicyCapability::CAN_JOIN))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Profile Image')->setValue(phutil_render_tag('img', array('src' => $img_src))))->appendChild(id(new AphrontFormImageControl())->setLabel('Change Image')->setName('image')->setError($e_image)->setCaption('Supported formats: ' . implode(', ', $supported_formats)))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton('/project/view/' . $project->getID() . '/')->setValue('Save'));
     $panel = new AphrontPanelView();
     $panel->setHeader($header_name);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     $nav = $this->buildLocalNavigation($project);
     $nav->selectFilter('edit');
     $nav->appendChild(array($error_view, $panel));
     return $this->buildStandardPageResponse($nav, array('title' => $title));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $files = array();
     $parent_task = null;
     $template_id = null;
     if ($this->id) {
         $task = id(new ManiphestTask())->load($this->id);
         if (!$task) {
             return new Aphront404Response();
         }
     } else {
         $task = new ManiphestTask();
         $task->setPriority(ManiphestTaskPriority::PRIORITY_TRIAGE);
         $task->setAuthorPHID($user->getPHID());
         // These allow task creation with defaults.
         if (!$request->isFormPost()) {
             $task->setTitle($request->getStr('title'));
             $default_projects = $request->getStr('projects');
             if ($default_projects) {
                 $task->setProjectPHIDs(explode(';', $default_projects));
             }
         }
         $file_phids = $request->getArr('files', array());
         if (!$file_phids) {
             // Allow a single 'file' key instead, mostly since Mac OS X urlencodes
             // square brackets in URLs when passed to 'open', so you can't 'open'
             // a URL like '?files[]=xyz' and have PHP interpret it correctly.
             $phid = $request->getStr('file');
             if ($phid) {
                 $file_phids = array($phid);
             }
         }
         if ($file_phids) {
             $files = id(new PhabricatorFile())->loadAllWhere('phid IN (%Ls)', $file_phids);
         }
         $template_id = $request->getInt('template');
         // You can only have a parent task if you're creating a new task.
         $parent_id = $request->getInt('parent');
         if ($parent_id) {
             $parent_task = id(new ManiphestTask())->load($parent_id);
         }
     }
     $errors = array();
     $e_title = true;
     $extensions = ManiphestTaskExtensions::newExtensions();
     $aux_fields = $extensions->getAuxiliaryFieldSpecifications();
     if ($request->isFormPost()) {
         $changes = array();
         $new_title = $request->getStr('title');
         $new_desc = $request->getStr('description');
         $new_status = $request->getStr('status');
         $workflow = '';
         if ($task->getID()) {
             if ($new_title != $task->getTitle()) {
                 $changes[ManiphestTransactionType::TYPE_TITLE] = $new_title;
             }
             if ($new_desc != $task->getDescription()) {
                 $changes[ManiphestTransactionType::TYPE_DESCRIPTION] = $new_desc;
             }
             if ($new_status != $task->getStatus()) {
                 $changes[ManiphestTransactionType::TYPE_STATUS] = $new_status;
             }
         } else {
             $task->setTitle($new_title);
             $task->setDescription($new_desc);
             $changes[ManiphestTransactionType::TYPE_STATUS] = ManiphestTaskStatus::STATUS_OPEN;
             $workflow = 'create';
         }
         $owner_tokenizer = $request->getArr('assigned_to');
         $owner_phid = reset($owner_tokenizer);
         if (!strlen($new_title)) {
             $e_title = 'Required';
             $errors[] = 'Title is required.';
         }
         foreach ($aux_fields as $aux_field) {
             $aux_field->setValueFromRequest($request);
             if ($aux_field->isRequired() && !strlen($aux_field->getValue())) {
                 $errors[] = $aux_field->getLabel() . ' is required.';
                 $aux_field->setError('Required');
             }
             if (strlen($aux_field->getValue())) {
                 try {
                     $aux_field->validate();
                 } catch (Exception $e) {
                     $errors[] = $e->getMessage();
                     $aux_field->setError('Invalid');
                 }
             }
         }
         if ($errors) {
             $task->setPriority($request->getInt('priority'));
             $task->setOwnerPHID($owner_phid);
             $task->setCCPHIDs($request->getArr('cc'));
             $task->setProjectPHIDs($request->getArr('projects'));
         } else {
             if ($request->getInt('priority') != $task->getPriority()) {
                 $changes[ManiphestTransactionType::TYPE_PRIORITY] = $request->getInt('priority');
             }
             if ($owner_phid != $task->getOwnerPHID()) {
                 $changes[ManiphestTransactionType::TYPE_OWNER] = $owner_phid;
             }
             if ($request->getArr('cc') != $task->getCCPHIDs()) {
                 $changes[ManiphestTransactionType::TYPE_CCS] = $request->getArr('cc');
             }
             $new_proj_arr = $request->getArr('projects');
             $new_proj_arr = array_values($new_proj_arr);
             sort($new_proj_arr);
             $cur_proj_arr = $task->getProjectPHIDs();
             $cur_proj_arr = array_values($cur_proj_arr);
             sort($cur_proj_arr);
             if ($new_proj_arr != $cur_proj_arr) {
                 $changes[ManiphestTransactionType::TYPE_PROJECTS] = $new_proj_arr;
             }
             if ($files) {
                 $file_map = mpull($files, 'getPHID');
                 $file_map = array_fill_keys($file_map, array());
                 $changes[ManiphestTransactionType::TYPE_ATTACH] = array(PhabricatorPHIDConstants::PHID_TYPE_FILE => $file_map);
             }
             $content_source = PhabricatorContentSource::newForSource(PhabricatorContentSource::SOURCE_WEB, array('ip' => $request->getRemoteAddr()));
             $template = new ManiphestTransaction();
             $template->setAuthorPHID($user->getPHID());
             $template->setContentSource($content_source);
             $transactions = array();
             foreach ($changes as $type => $value) {
                 $transaction = clone $template;
                 $transaction->setTransactionType($type);
                 $transaction->setNewValue($value);
                 $transactions[] = $transaction;
             }
             if ($aux_fields) {
                 $task->loadAndAttachAuxiliaryAttributes();
                 foreach ($aux_fields as $aux_field) {
                     $transaction = clone $template;
                     $transaction->setTransactionType(ManiphestTransactionType::TYPE_AUXILIARY);
                     $aux_key = $aux_field->getAuxiliaryKey();
                     $transaction->setMetadataValue('aux:key', $aux_key);
                     $transaction->setNewValue($aux_field->getValueForStorage());
                     $transactions[] = $transaction;
                 }
             }
             if ($transactions) {
                 $is_new = !$task->getID();
                 $event = new PhabricatorEvent(PhabricatorEventType::TYPE_MANIPHEST_WILLEDITTASK, array('task' => $task, 'new' => $is_new, 'transactions' => $transactions));
                 $event->setUser($user);
                 $event->setAphrontRequest($request);
                 PhutilEventEngine::dispatchEvent($event);
                 $task = $event->getValue('task');
                 $transactions = $event->getValue('transactions');
                 $editor = new ManiphestTransactionEditor();
                 $editor->setAuxiliaryFields($aux_fields);
                 $editor->applyTransactions($task, $transactions);
                 $event = new PhabricatorEvent(PhabricatorEventType::TYPE_MANIPHEST_DIDEDITTASK, array('task' => $task, 'new' => $is_new, 'transactions' => $transactions));
                 $event->setUser($user);
                 $event->setAphrontRequest($request);
                 PhutilEventEngine::dispatchEvent($event);
             }
             if ($parent_task) {
                 $type_task = PhabricatorPHIDConstants::PHID_TYPE_TASK;
                 // NOTE: It's safe to simply apply this transaction without doing
                 // cycle detection because we know the new task has no children.
                 $new_value = $parent_task->getAttached();
                 $new_value[$type_task][$task->getPHID()] = array();
                 $parent_xaction = clone $template;
                 $attach_type = ManiphestTransactionType::TYPE_ATTACH;
                 $parent_xaction->setTransactionType($attach_type);
                 $parent_xaction->setNewValue($new_value);
                 $editor = new ManiphestTransactionEditor();
                 $editor->setAuxiliaryFields($aux_fields);
                 $editor->applyTransactions($parent_task, array($parent_xaction));
                 $workflow = $parent_task->getID();
             }
             $redirect_uri = '/T' . $task->getID();
             if ($workflow) {
                 $redirect_uri .= '?workflow=' . $workflow;
             }
             return id(new AphrontRedirectResponse())->setURI($redirect_uri);
         }
     } else {
         if (!$task->getID()) {
             $task->setCCPHIDs(array($user->getPHID()));
             if ($template_id) {
                 $template_task = id(new ManiphestTask())->load($template_id);
                 if ($template_task) {
                     $task->setCCPHIDs($template_task->getCCPHIDs());
                     $task->setProjectPHIDs($template_task->getProjectPHIDs());
                     $task->setOwnerPHID($template_task->getOwnerPHID());
                 }
             }
         }
     }
     $phids = array_merge(array($task->getOwnerPHID()), $task->getCCPHIDs(), $task->getProjectPHIDs());
     if ($parent_task) {
         $phids[] = $parent_task->getPHID();
     }
     $phids = array_filter($phids);
     $phids = array_unique($phids);
     $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles($phids);
     $tvalues = mpull($handles, 'getFullName', 'getPHID');
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setErrors($errors);
         $error_view->setTitle('Form Errors');
     }
     $priority_map = ManiphestTaskPriority::getTaskPriorityMap();
     if ($task->getOwnerPHID()) {
         $assigned_value = array($task->getOwnerPHID() => $handles[$task->getOwnerPHID()]->getFullName());
     } else {
         $assigned_value = array();
     }
     if ($task->getCCPHIDs()) {
         $cc_value = array_select_keys($tvalues, $task->getCCPHIDs());
     } else {
         $cc_value = array();
     }
     if ($task->getProjectPHIDs()) {
         $projects_value = array_select_keys($tvalues, $task->getProjectPHIDs());
     } else {
         $projects_value = array();
     }
     $cancel_id = nonempty($task->getID(), $template_id);
     if ($cancel_id) {
         $cancel_uri = '/T' . $cancel_id;
     } else {
         $cancel_uri = '/maniphest/';
     }
     if ($task->getID()) {
         $button_name = 'Save Task';
         $header_name = 'Edit Task';
     } else {
         if ($parent_task) {
             $cancel_uri = '/T' . $parent_task->getID();
             $button_name = 'Create Task';
             $header_name = 'Create New Subtask';
         } else {
             $button_name = 'Create Task';
             $header_name = 'Create New Task';
         }
     }
     require_celerity_resource('maniphest-task-edit-css');
     $project_tokenizer_id = celerity_generate_unique_node_id();
     $form = new AphrontFormView();
     $form->setUser($user)->setAction($request->getRequestURI()->getPath())->addHiddenInput('template', $template_id);
     if ($parent_task) {
         $form->appendChild(id(new AphrontFormStaticControl())->setLabel('Parent Task')->setValue($handles[$parent_task->getPHID()]->getFullName()))->addHiddenInput('parent', $parent_task->getID());
     }
     $form->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Title')->setName('title')->setError($e_title)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)->setValue($task->getTitle()));
     if ($task->getID()) {
         // Only show this in "edit" mode, not "create" mode, since creating a
         // non-open task is kind of silly and it would just clutter up the
         // "create" interface.
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel('Status')->setName('status')->setValue($task->getStatus())->setOptions(ManiphestTaskStatus::getTaskStatusMap()));
     }
     $form->appendChild(id(new AphrontFormTokenizerControl())->setLabel('Assigned To')->setName('assigned_to')->setValue($assigned_value)->setUser($user)->setDatasource('/typeahead/common/users/')->setLimit(1))->appendChild(id(new AphrontFormTokenizerControl())->setLabel('CC')->setName('cc')->setValue($cc_value)->setUser($user)->setDatasource('/typeahead/common/mailable/'))->appendChild(id(new AphrontFormSelectControl())->setLabel('Priority')->setName('priority')->setOptions($priority_map)->setValue($task->getPriority()))->appendChild(id(new AphrontFormTokenizerControl())->setLabel('Projects')->setName('projects')->setValue($projects_value)->setID($project_tokenizer_id)->setCaption(javelin_render_tag('a', array('href' => '/project/create/', 'mustcapture' => true, 'sigil' => 'project-create'), 'Create New Project'))->setDatasource('/typeahead/common/projects/'));
     if ($aux_fields) {
         if (!$request->isFormPost()) {
             $task->loadAndAttachAuxiliaryAttributes();
             foreach ($aux_fields as $aux_field) {
                 $aux_key = $aux_field->getAuxiliaryKey();
                 $value = $task->getAuxiliaryAttribute($aux_key);
                 $aux_field->setValueFromStorage($value);
             }
         }
         foreach ($aux_fields as $aux_field) {
             if ($aux_field->isRequired() && !$aux_field->getError() && !$aux_field->getValue()) {
                 $aux_field->setError(true);
             }
             $aux_control = $aux_field->renderControl();
             $form->appendChild($aux_control);
         }
     }
     require_celerity_resource('aphront-error-view-css');
     Javelin::initBehavior('maniphest-project-create', array('tokenizerID' => $project_tokenizer_id));
     if ($files) {
         $file_display = array();
         foreach ($files as $file) {
             $file_display[] = phutil_escape_html($file->getName());
         }
         $file_display = implode('<br />', $file_display);
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel('Files')->setValue($file_display));
         foreach ($files as $ii => $file) {
             $form->addHiddenInput('files[' . $ii . ']', $file->getPHID());
         }
     }
     $email_create = PhabricatorEnv::getEnvConfig('metamta.maniphest.public-create-email');
     $email_hint = null;
     if (!$task->getID() && $email_create) {
         $email_hint = 'You can also create tasks by sending an email to: ' . '<tt>' . phutil_escape_html($email_create) . '</tt>';
     }
     $panel_id = celerity_generate_unique_node_id();
     $form->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Description')->setName('description')->setID('description-textarea')->setCaption($email_hint)->setValue($task->getDescription()));
     if (!$task->getID()) {
         $form->appendChild(id(new AphrontFormDragAndDropUploadControl())->setLabel('Attached Files')->setName('files')->setDragAndDropTarget($panel_id)->setActivatedClass('aphront-panel-view-drag-and-drop'));
     }
     $form->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($button_name));
     $panel = new AphrontPanelView();
     $panel->setWidth(AphrontPanelView::WIDTH_FULL);
     $panel->setHeader($header_name);
     $panel->setID($panel_id);
     $panel->appendChild($form);
     $description_preview_panel = '<div class="aphront-panel-preview aphront-panel-preview-full">
     <div class="maniphest-description-preview-header">
       Description Preview
     </div>
     <div id="description-preview">
       <div class="aphront-panel-preview-loading-text">
         Loading preview...
       </div>
     </div>
   </div>';
     Javelin::initBehavior('maniphest-description-preview', array('preview' => 'description-preview', 'textarea' => 'description-textarea', 'uri' => '/maniphest/task/descriptionpreview/'));
     return $this->buildStandardPageResponse(array($error_view, $panel, $description_preview_panel), array('title' => $header_name));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $paste = new PhabricatorPaste();
     $error_view = null;
     $e_text = true;
     $fork = $request->getInt('fork');
     $paste_text = null;
     $paste_parent = null;
     if ($request->isFormPost()) {
         $errors = array();
         $title = $request->getStr('title');
         $paste_language = $request->getStr('language');
         $text = $request->getStr('text');
         if (!strlen($text)) {
             $e_text = 'Required';
             $errors[] = 'The paste may not be blank.';
         } else {
             $e_text = null;
         }
         $parent = id(new PhabricatorPaste())->loadOneWhere('phid = %s', $request->getStr('parent'));
         if ($parent) {
             $paste->setParentPHID($parent->getPHID());
         }
         $paste->setTitle($title);
         if (!$errors) {
             if ($paste_language == 'infer') {
                 // If it's infer, store an empty string. Otherwise, store the
                 // language name. We do this so we can refer to 'infer' elsewhere
                 // in the code (such as default value) while retaining backwards
                 // compatibility with old posts with no language stored.
                 $paste_language = '';
             }
             $paste->setLanguage($paste_language);
             $paste_file = PhabricatorFile::newFromFileData($text, array('name' => $title, 'mime-type' => 'text/plain; charset=utf-8', 'authorPHID' => $user->getPHID()));
             $paste->setFilePHID($paste_file->getPHID());
             $paste->setAuthorPHID($user->getPHID());
             $paste->save();
             return id(new AphrontRedirectResponse())->setURI('/P' . $paste->getID());
         } else {
             $error_view = new AphrontErrorView();
             $error_view->setErrors($errors);
             $error_view->setTitle('A problem has occurred!');
         }
     } else {
         if ($fork) {
             $fork_paste = id(new PhabricatorPaste())->load($fork);
             if ($fork_paste) {
                 $paste->setTitle('Fork of ' . $fork_paste->getID() . ': ' . $fork_paste->getTitle());
                 $fork_file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $fork_paste->getFilePHID());
                 $paste_text = $fork_file->loadFileData();
                 $paste_language = nonempty($fork_paste->getLanguage(), 'infer');
                 $paste_parent = $fork_paste->getPHID();
             }
         } else {
             $paste_language = PhabricatorEnv::getEnvConfig('pygments.dropdown-default');
         }
     }
     $form = new AphrontFormView();
     $available_languages = PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
     asort($available_languages);
     $language_select = id(new AphrontFormSelectControl())->setLabel('Language')->setName('language')->setValue($paste_language)->setOptions($available_languages);
     $form->setUser($user)->setAction($request->getRequestURI()->getPath())->addHiddenInput('parent', $paste_parent)->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setValue($paste->getTitle())->setName('title'))->appendChild($language_select)->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Text')->setError($e_text)->setValue($paste_text)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setName('text'))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton('/paste/')->setValue('Create Paste'));
     $panel = new AphrontPanelView();
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->setHeader('Create a Paste');
     $panel->appendChild($form);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Paste Creation', 'tab' => 'create'));
 }
 public function processRequest()
 {
     $provider = $this->getOAuthProvider();
     $oauth_info = $this->getOAuthInfo();
     $request = $this->getRequest();
     $errors = array();
     $e_username = true;
     $e_email = true;
     $e_realname = true;
     $user = new PhabricatorUser();
     $user->setUsername($provider->retrieveUserAccountName());
     $user->setRealName($provider->retrieveUserRealName());
     $user->setEmail($provider->retrieveUserEmail());
     if ($request->isFormPost()) {
         $user->setUsername($request->getStr('username'));
         $username = $user->getUsername();
         if (!strlen($user->getUsername())) {
             $e_username = '******';
             $errors[] = 'Username is required.';
         } else {
             if (!PhabricatorUser::validateUsername($username)) {
                 $e_username = '******';
                 $errors[] = 'Username must consist of only numbers and letters.';
             } else {
                 $e_username = null;
             }
         }
         if ($user->getEmail() === null) {
             $user->setEmail($request->getStr('email'));
             if (!strlen($user->getEmail())) {
                 $e_email = 'Required';
                 $errors[] = 'Email is required.';
             } else {
                 $e_email = null;
             }
         }
         if (!strlen($user->getRealName())) {
             $user->setRealName($request->getStr('realname'));
             if (!strlen($user->getRealName())) {
                 $e_realname = 'Required';
                 $errors[] = 'Real name is required.';
             } else {
                 $e_realname = null;
             }
         }
         if (!$errors) {
             $image = $provider->retrieveUserProfileImage();
             if ($image) {
                 $file = PhabricatorFile::newFromFileData($image, array('name' => $provider->getProviderKey() . '-profile.jpg', 'authorPHID' => $user->getPHID()));
                 $user->setProfileImagePHID($file->getPHID());
             }
             try {
                 $user->save();
                 $oauth_info->setUserID($user->getID());
                 $oauth_info->save();
                 $session_key = $user->establishSession('web');
                 $request->setCookie('phusr', $user->getUsername());
                 $request->setCookie('phsid', $session_key);
                 return id(new AphrontRedirectResponse())->setURI('/');
             } catch (AphrontQueryDuplicateKeyException $exception) {
                 $same_username = id(new PhabricatorUser())->loadOneWhere('userName = %s', $user->getUserName());
                 $same_email = id(new PhabricatorUser())->loadOneWhere('email = %s', $user->getEmail());
                 if ($same_username) {
                     $e_username = '******';
                     $errors[] = 'That username or email is not unique.';
                 } else {
                     if ($same_email) {
                         $e_email = 'Duplicate';
                         $errors[] = 'That email is not unique.';
                     } else {
                         throw $exception;
                     }
                 }
             }
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Registration Failed');
         $error_view->setErrors($errors);
     }
     // Strip the URI down to the path, because otherwise we'll trigger
     // external CSRF protection (by having a protocol in the form "action")
     // and generate a form with no CSRF token.
     $action_uri = new PhutilURI($provider->getRedirectURI());
     $action_path = $action_uri->getPath();
     $form = new AphrontFormView();
     $form->addHiddenInput('token', $provider->getAccessToken())->addHiddenInput('expires', $oauth_info->getTokenExpires())->addHiddenInput('state', $this->getOAuthState())->setUser($request->getUser())->setAction($action_path)->appendChild(id(new AphrontFormTextControl())->setLabel('Username')->setName('username')->setValue($user->getUsername())->setError($e_username));
     if ($provider->retrieveUserEmail() === null) {
         $form->appendChild(id(new AphrontFormTextControl())->setLabel('Email')->setName('email')->setValue($request->getStr('email'))->setError($e_email));
     }
     if ($provider->retrieveUserRealName() === null) {
         $form->appendChild(id(new AphrontFormTextControl())->setLabel('Real Name')->setName('realname')->setValue($request->getStr('realname'))->setError($e_realname));
     }
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Create Account'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Create New Account');
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Create New Account'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $poll = new PhabricatorSlowvotePoll();
     $poll->setAuthorPHID($user->getPHID());
     $e_question = true;
     $e_response = true;
     $errors = array();
     $responses = $request->getArr('response');
     if ($request->isFormPost()) {
         $poll->setQuestion($request->getStr('question'));
         $poll->setResponseVisibility($request->getInt('response_visibility'));
         $poll->setShuffle($request->getBool('shuffle', false));
         $poll->setMethod($request->getInt('method'));
         if (!strlen($poll->getQuestion())) {
             $e_question = 'Required';
             $errors[] = 'You must ask a poll question.';
         } else {
             $e_question = null;
         }
         $responses = array_filter($responses);
         if (empty($responses)) {
             $errors[] = 'You must offer at least one response.';
             $e_response = 'Required';
         } else {
             $e_response = null;
         }
         if (empty($errors)) {
             $poll->save();
             foreach ($responses as $response) {
                 $option = new PhabricatorSlowvoteOption();
                 $option->setName($response);
                 $option->setPollID($poll->getID());
                 $option->save();
             }
             return id(new AphrontRedirectResponse())->setURI('/V' . $poll->getID());
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     }
     $form = id(new AphrontFormView())->setUser($user)->appendChild('<p class="aphront-form-instructions">Resolve issues and build ' . 'consensus through protracted deliberation.</p>')->appendChild(id(new AphrontFormTextControl())->setLabel('Question')->setName('question')->setValue($poll->getQuestion())->setError($e_question));
     for ($ii = 0; $ii < 10; $ii++) {
         $n = $ii + 1;
         $response = id(new AphrontFormTextControl())->setLabel("Response {$n}")->setName('response[]')->setValue(idx($responses, $ii, ''));
         if ($ii == 0) {
             $response->setError($e_response);
         }
         $form->appendChild($response);
     }
     $poll_type_options = array(PhabricatorSlowvotePoll::METHOD_PLURALITY => 'Plurality (Single Choice)', PhabricatorSlowvotePoll::METHOD_APPROVAL => 'Approval (Multiple Choice)');
     $response_type_options = array(PhabricatorSlowvotePoll::RESPONSES_VISIBLE => 'Allow anyone to see the responses', PhabricatorSlowvotePoll::RESPONSES_VOTERS => 'Require a vote to see the responses', PhabricatorSlowvotePoll::RESPONSES_OWNER => 'Only I can see the responses');
     $form->appendChild(id(new AphrontFormSelectControl())->setLabel('Vote Type')->setName('method')->setValue($poll->getMethod())->setOptions($poll_type_options))->appendChild(id(new AphrontFormSelectControl())->setLabel('Responses')->setName('response_visibility')->setValue($poll->getResponseVisibility())->setOptions($response_type_options))->appendChild(id(new AphrontFormCheckboxControl())->setLabel('Shuffle')->addCheckbox('shuffle', 1, 'Show choices in random order', $poll->getShuffle()))->appendChild(id(new AphrontFormSubmitControl())->setValue('Create Slowvote')->addCancelButton('/vote/'));
     $panel = new AphrontPanelView();
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->setHeader('Create Slowvote');
     $panel->appendChild($form);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Create Slowvote'));
 }
    public function processRequest()
    {
        $request = $this->getRequest();
        if (!PhabricatorEnv::getEnvConfig('auth.password-auth-enabled')) {
            return new Aphront400Response();
        }
        $e_email = true;
        $e_captcha = true;
        $errors = array();
        if ($request->isFormPost()) {
            $e_email = null;
            $e_captcha = 'Again';
            $captcha_ok = AphrontFormRecaptchaControl::processCaptcha($request);
            if (!$captcha_ok) {
                $errors[] = "Captcha response is incorrect, try again.";
                $e_captcha = 'Invalid';
            }
            $email = $request->getStr('email');
            if (!strlen($email)) {
                $errors[] = "You must provide an email address.";
                $e_email = 'Required';
            }
            if (!$errors) {
                // NOTE: Don't validate the email unless the captcha is good; this makes
                // it expensive to fish for valid email addresses while giving the user
                // a better error if they goof their email.
                $target_user = id(new PhabricatorUser())->loadOneWhere('email = %s', $email);
                if (!$target_user) {
                    $errors[] = "There is no account associated with that email address.";
                    $e_email = "Invalid";
                }
                if (!$errors) {
                    $uri = $target_user->getEmailLoginURI();
                    $body = <<<EOBODY
Condolences on forgetting your password. You can use this link to reset it:

  {$uri}

After you set a new password, consider writing it down on a sticky note and
attaching it to your monitor so you don't forget again! Choosing a very short,
easy-to-remember password like "cat" or "1234" might also help.

Best Wishes,
Phabricator

EOBODY;
                    $mail = new PhabricatorMetaMTAMail();
                    $mail->setSubject('[Phabricator] Password Reset');
                    $mail->setFrom($target_user->getPHID());
                    $mail->addTos(array($target_user->getPHID()));
                    $mail->setBody($body);
                    $mail->saveAndSend();
                    $view = new AphrontRequestFailureView();
                    $view->setHeader('Check Your Email');
                    $view->appendChild('<p>An email has been sent with a link you can use to login.</p>');
                    return $this->buildStandardPageResponse($view, array('title' => 'Email Sent'));
                }
            }
        }
        $email_auth = new AphrontFormView();
        $email_auth->setAction('/login/email/')->setUser($request->getUser())->appendChild(id(new AphrontFormTextControl())->setLabel('Email')->setName('email')->setValue($request->getStr('email'))->setError($e_email))->appendChild(id(new AphrontFormRecaptchaControl())->setLabel('Captcha')->setError($e_captcha))->appendChild(id(new AphrontFormSubmitControl())->setValue('Send Email'));
        $error_view = null;
        if ($errors) {
            $error_view = new AphrontErrorView();
            $error_view->setTitle('Login Error');
            $error_view->setErrors($errors);
        }
        $panel = new AphrontPanelView();
        $panel->setWidth(AphrontPanelView::WIDTH_FORM);
        $panel->appendChild('<h1>Forgot Password / Email Login</h1>');
        $panel->appendChild($email_auth);
        return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Create New Account'));
    }
 public function processRequest()
 {
     $provider = $this->getLDAProvider();
     $ldap_info = $this->getLDAPInfo();
     $request = $this->getRequest();
     $errors = array();
     $e_username = true;
     $e_email = true;
     $e_realname = true;
     $user = new PhabricatorUser();
     $user->setUsername();
     $user->setRealname($provider->retrieveUserRealName());
     $new_email = $provider->retrieveUserEmail();
     if ($new_email) {
         // If the user's LDAP provider account has an email address but the
         // email address domain is not allowed by the Phabricator configuration,
         // we just pretend the provider did not supply an address.
         //
         // For instance, if the user uses LDAP Auth and their email address
         // is "*****@*****.**" but Phabricator is configured to require users
         // use "@company.com" addresses, we show a prompt below and tell the user
         // to provide their "@company.com" address. They can still use the LDAP
         // account to login, they just need to associate their account with an
         // allowed address.
         //
         // If the email address is fine, we just use it and don't prompt the user.
         if (!PhabricatorUserEmail::isAllowedAddress($new_email)) {
             $new_email = null;
         }
     }
     $show_email_input = $new_email === null;
     if ($request->isFormPost()) {
         $user->setUsername($request->getStr('username'));
         $username = $user->getUsername();
         if (!strlen($user->getUsername())) {
             $e_username = '******';
             $errors[] = 'Username is required.';
         } else {
             if (!PhabricatorUser::validateUsername($username)) {
                 $e_username = '******';
                 $errors[] = PhabricatorUser::describeValidUsername();
             } else {
                 $e_username = null;
             }
         }
         if (!$new_email) {
             $new_email = trim($request->getStr('email'));
             if (!$new_email) {
                 $e_email = 'Required';
                 $errors[] = 'Email is required.';
             } else {
                 $e_email = null;
             }
         }
         if ($new_email) {
             if (!PhabricatorUserEmail::isAllowedAddress($new_email)) {
                 $e_email = 'Invalid';
                 $errors[] = PhabricatorUserEmail::describeAllowedAddresses();
             }
         }
         if (!strlen($user->getRealName())) {
             $user->setRealName($request->getStr('realname'));
             if (!strlen($user->getRealName())) {
                 $e_realname = 'Required';
                 $errors[] = 'Real name is required.';
             } else {
                 $e_realname = null;
             }
         }
         if (!$errors) {
             try {
                 // NOTE: We don't verify LDAP email addresses by default because
                 // LDAP providers might associate email addresses with accounts that
                 // haven't actually verified they own them. We could selectively
                 // auto-verify some providers that we trust here, but the stakes for
                 // verifying an email address are high because having a corporate
                 // address at a company is sometimes the key to the castle.
                 $email_obj = id(new PhabricatorUserEmail())->setAddress($new_email)->setIsVerified(0);
                 id(new PhabricatorUserEditor())->setActor($user)->createNewUser($user, $email_obj);
                 $ldap_info->setUserID($user->getID());
                 $ldap_info->save();
                 $session_key = $user->establishSession('web');
                 $request->setCookie('phusr', $user->getUsername());
                 $request->setCookie('phsid', $session_key);
                 $email_obj->sendVerificationEmail($user);
                 return id(new AphrontRedirectResponse())->setURI('/');
             } catch (AphrontQueryDuplicateKeyException $exception) {
                 $same_username = id(new PhabricatorUser())->loadOneWhere('userName = %s', $user->getUserName());
                 $same_email = id(new PhabricatorUserEmail())->loadOneWhere('address = %s', $new_email);
                 if ($same_username) {
                     $e_username = '******';
                     $errors[] = 'That username or email is not unique.';
                 } else {
                     if ($same_email) {
                         $e_email = 'Duplicate';
                         $errors[] = 'That email is not unique.';
                     } else {
                         throw $exception;
                     }
                 }
             }
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Registration Failed');
         $error_view->setErrors($errors);
     }
     // Strip the URI down to the path, because otherwise we'll trigger
     // external CSRF protection (by having a protocol in the form "action")
     // and generate a form with no CSRF token.
     $action_uri = new PhutilURI('/ldap/login/');
     $action_path = $action_uri->getPath();
     $form = new AphrontFormView();
     $form->setUser($request->getUser())->setAction($action_path)->appendChild(id(new AphrontFormTextControl())->setLabel('Username')->setName('username')->setValue($user->getUsername())->setError($e_username));
     $form->appendChild(id(new AphrontFormPasswordControl())->setLabel('Password')->setName('password'));
     if ($show_email_input) {
         $form->appendChild(id(new AphrontFormTextControl())->setLabel('Email')->setName('email')->setValue($request->getStr('email'))->setError($e_email));
     }
     if ($provider->retrieveUserRealName() === null) {
         $form->appendChild(id(new AphrontFormTextControl())->setLabel('Real Name')->setName('realname')->setValue($request->getStr('realname'))->setError($e_realname));
     }
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Create Account'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Create New Account');
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Create New Account'));
 }
 private function processTrackingRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $repository = $this->repository;
     $repository_id = $repository->getID();
     $errors = array();
     $e_uri = null;
     $e_path = null;
     $is_git = false;
     $is_svn = false;
     $is_mercurial = false;
     $e_ssh_key = null;
     $e_ssh_keyfile = null;
     $e_branch = null;
     switch ($repository->getVersionControlSystem()) {
         case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
             $is_git = true;
             break;
         case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
             $is_svn = true;
             break;
         case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
             $is_mercurial = true;
             break;
         default:
             throw new Exception("Unsupported VCS!");
     }
     $has_branches = $is_git || $is_mercurial;
     $has_local = $is_git || $is_mercurial;
     $has_branch_filter = $is_git;
     $has_auth_support = $is_svn;
     if ($request->isFormPost()) {
         $tracking = $request->getStr('tracking') == 'enabled' ? true : false;
         $repository->setDetail('tracking-enabled', $tracking);
         $repository->setDetail('remote-uri', $request->getStr('uri'));
         if ($has_local) {
             $repository->setDetail('local-path', $request->getStr('path'));
         }
         if ($has_branch_filter) {
             $branch_filter = $request->getStrList('branch-filter');
             $branch_filter = array_fill_keys($branch_filter, true);
             $repository->setDetail('branch-filter', $branch_filter);
             $close_commits_filter = $request->getStrList('close-commits-filter');
             $close_commits_filter = array_fill_keys($close_commits_filter, true);
             $repository->setDetail('close-commits-filter', $close_commits_filter);
         }
         $repository->setDetail('disable-autoclose', $request->getStr('autoclose') == 'disabled' ? true : false);
         $repository->setDetail('pull-frequency', max(1, $request->getInt('frequency')));
         if ($has_branches) {
             $repository->setDetail('default-branch', $request->getStr('default-branch'));
             if ($is_git) {
                 $branch_name = $repository->getDetail('default-branch');
                 if (strpos($branch_name, '/') !== false) {
                     $e_branch = 'Invalid';
                     $errors[] = "Your branch name should not specify an explicit " . "remote. For instance, use 'master', not " . "'origin/master'.";
                 }
             }
         }
         $repository->setDetail('default-owners-path', $request->getStr('default-owners-path', '/'));
         $repository->setDetail('ssh-login', $request->getStr('ssh-login'));
         $repository->setDetail('ssh-key', $request->getStr('ssh-key'));
         $repository->setDetail('ssh-keyfile', $request->getStr('ssh-keyfile'));
         $repository->setDetail('http-login', $request->getStr('http-login'));
         $repository->setDetail('http-pass', $request->getStr('http-pass'));
         if ($repository->getDetail('ssh-key') && $repository->getDetail('ssh-keyfile')) {
             $errors[] = "Specify only one of 'SSH Private Key' and 'SSH Private Key File', " . "not both.";
             $e_ssh_key = 'Choose Only One';
             $e_ssh_keyfile = 'Choose Only One';
         }
         $repository->setDetail('herald-disabled', $request->getInt('herald-disabled', 0));
         if ($is_svn) {
             $repository->setUUID($request->getStr('uuid'));
             $subpath = ltrim($request->getStr('svn-subpath'), '/');
             if ($subpath) {
                 $subpath = rtrim($subpath, '/') . '/';
             }
             $repository->setDetail('svn-subpath', $subpath);
         }
         $repository->setDetail('detail-parser', $request->getStr('detail-parser', 'PhabricatorRepositoryDefaultCommitMessageDetailParser'));
         if ($tracking) {
             if (!$repository->getDetail('remote-uri')) {
                 $e_uri = 'Required';
                 $errors[] = "Repository URI is required.";
             } else {
                 if ($is_svn && !preg_match('@/$@', $repository->getDetail('remote-uri'))) {
                     $e_uri = 'Invalid';
                     $errors[] = 'Subversion Repository Root must end in a slash ("/").';
                 } else {
                     $e_uri = null;
                 }
             }
             if ($has_local) {
                 if (!$repository->getDetail('local-path')) {
                     $e_path = 'Required';
                     $errors[] = "Local path is required.";
                 } else {
                     $e_path = null;
                 }
             }
         }
         if (!$errors) {
             $repository->save();
             return id(new AphrontRedirectResponse())->setURI('/repository/edit/' . $repository_id . '/tracking/?saved=true');
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setErrors($errors);
         $error_view->setTitle('Form Errors');
     } else {
         if ($request->getStr('saved')) {
             $error_view = new AphrontErrorView();
             $error_view->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
             $error_view->setTitle('Changes Saved');
             $error_view->appendChild('Tracking changes were saved.');
         } else {
             if (!$repository->isTracked()) {
                 $error_view = new AphrontErrorView();
                 $error_view->setSeverity(AphrontErrorView::SEVERITY_WARNING);
                 $error_view->setTitle('Repository Not Tracked');
                 $error_view->appendChild('Tracking is currently "Disabled" for this repository, so it will ' . 'not be imported into Phabricator. You can enable it below.');
             }
         }
     }
     switch ($repository->getVersionControlSystem()) {
         case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
             $is_git = true;
             break;
         case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
             $is_svn = true;
             break;
     }
     $doc_href = PhabricatorEnv::getDoclink('article/Diffusion_User_Guide.html');
     $user_guide_link = phutil_render_tag('a', array('href' => $doc_href), 'Diffusion User Guide');
     $form = new AphrontFormView();
     $form->setUser($user)->setAction('/repository/edit/' . $repository->getID() . '/tracking/')->appendChild('<p class="aphront-form-instructions">Phabricator can track ' . 'repositories, importing commits as they happen and notifying ' . 'Differential, Diffusion, Herald, and other services. To enable ' . 'tracking for a repository, configure it here and then start (or ' . 'restart) the daemons. More information is available in the ' . '<strong>' . $user_guide_link . '</strong>.</p>');
     $form->appendChild(id(new AphrontFormInsetView())->setTitle('Basics')->appendChild(id(new AphrontFormStaticControl())->setLabel('Repository Name')->setValue($repository->getName()))->appendChild(id(new AphrontFormSelectControl())->setName('tracking')->setLabel('Tracking')->setOptions(array('disabled' => 'Disabled', 'enabled' => 'Enabled'))->setValue($repository->isTracked() ? 'enabled' : 'disabled')));
     $inset = new AphrontFormInsetView();
     $inset->setTitle('Remote URI');
     $clone_command = null;
     $fetch_command = null;
     if ($is_git) {
         $clone_command = 'git clone';
         $fetch_command = 'git fetch';
     } else {
         if ($is_mercurial) {
             $clone_command = 'hg clone';
             $fetch_command = 'hg pull';
         }
     }
     $uri_label = 'Repository URI';
     if ($has_local) {
         if ($is_git) {
             $instructions = 'Enter the URI to clone this repository from. It should look like ' . '<tt>git@github.com:example/example.git</tt>, ' . '<tt>ssh://user@host.com/git/example.git</tt>, or ' . '<tt>file:///local/path/to/repo</tt>';
         } else {
             if ($is_mercurial) {
                 $instructions = 'Enter the URI to clone this repository from. It should look ' . 'something like <tt>ssh://user@host.com/hg/example</tt>';
             }
         }
         $inset->appendChild('<p class="aphront-form-instructions">' . $instructions . '</p>');
     } else {
         if ($is_svn) {
             $instructions = 'Enter the <strong>Repository Root</strong> for this SVN repository. ' . 'You can figure this out by running <tt>svn info</tt> and looking at ' . 'the value in the <tt>Repository Root</tt> field. It should be a URI ' . 'and look like <tt>http://svn.example.org/svn/</tt>, ' . '<tt>svn+ssh://svn.example.com/svnroot/</tt>, or ' . '<tt>svn://svn.example.net/svn/</tt>';
             $inset->appendChild('<p class="aphront-form-instructions">' . $instructions . '</p>');
             $uri_label = 'Repository Root';
         }
     }
     $inset->appendChild(id(new AphrontFormTextControl())->setName('uri')->setLabel($uri_label)->setID('remote-uri')->setValue($repository->getDetail('remote-uri'))->setError($e_uri));
     $inset->appendChild('<div class="aphront-form-instructions">' . 'If you want to connect to this repository over SSH, enter the ' . 'username and private key to use. You can leave these fields blank if ' . 'the repository does not use SSH.' . '</div>');
     $inset->appendChild(id(new AphrontFormTextControl())->setName('ssh-login')->setLabel('SSH User')->setValue($repository->getDetail('ssh-login')))->appendChild(id(new AphrontFormTextAreaControl())->setName('ssh-key')->setLabel('SSH Private Key')->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)->setValue($repository->getDetail('ssh-key'))->setError($e_ssh_key)->setCaption('Specify the entire private key, <em>or</em>...'))->appendChild(id(new AphrontFormTextControl())->setName('ssh-keyfile')->setLabel('SSH Private Key File')->setValue($repository->getDetail('ssh-keyfile'))->setError($e_ssh_keyfile)->setCaption('...specify a path on disk where the daemon should ' . 'look for a private key.'));
     if ($has_auth_support) {
         $inset->appendChild('<div class="aphront-form-instructions">' . 'If you want to connect to this repository with a username and ' . 'password, such as over HTTP Basic Auth or SVN with SASL, ' . 'enter the username and password to use. You can leave these ' . 'fields blank if the repository does not use a username and ' . 'password for authentication.' . '</div>')->appendChild(id(new AphrontFormTextControl())->setName('http-login')->setLabel('Username')->setValue($repository->getDetail('http-login')))->appendChild(id(new AphrontFormPasswordControl())->setName('http-pass')->setLabel('Password')->setValue($repository->getDetail('http-pass')));
     }
     $inset->appendChild('<div class="aphront-form-important">' . 'To test your authentication configuration, <strong>save this ' . 'form</strong> and then run this script:' . '<code>' . 'phabricator/ $ ./scripts/repository/test_connection.php ' . phutil_escape_html($repository->getCallsign()) . '</code>' . 'This will verify that your configuration is correct and the ' . 'daemons can connect to the remote repository and pull changes ' . 'from it.' . '</div>');
     $form->appendChild($inset);
     $inset = new AphrontFormInsetView();
     $inset->setTitle('Repository Information');
     if ($has_local) {
         $inset->appendChild('<p class="aphront-form-instructions">Select a path on local disk ' . 'which the daemons should <tt>' . $clone_command . '</tt> the repository ' . 'into. This must be readable and writable by the daemons, and ' . 'readable by the webserver. The daemons will <tt>' . $fetch_command . '</tt> and keep this repository up to date.</p>');
         $inset->appendChild(id(new AphrontFormTextControl())->setName('path')->setLabel('Local Path')->setValue($repository->getDetail('local-path'))->setError($e_path));
     } else {
         if ($is_svn) {
             $inset->appendChild('<p class="aphront-form-instructions">If you only want to parse one ' . 'subpath of the repository, specify it here, relative to the ' . 'repository root (e.g., <tt>trunk/</tt> or <tt>projects/wheel/</tt>). ' . 'If you want to parse multiple subdirectories, create a separate ' . 'Phabricator repository for each one.</p>');
             $inset->appendChild(id(new AphrontFormTextControl())->setName('svn-subpath')->setLabel('Subpath')->setValue($repository->getDetail('svn-subpath'))->setError($e_path));
         }
     }
     if ($has_branch_filter) {
         $branch_filter_str = implode(', ', array_keys($repository->getDetail('branch-filter', array())));
         $inset->appendChild(id(new AphrontFormTextControl())->setName('branch-filter')->setLabel('Track Only')->setValue($branch_filter_str)->setCaption('Optional list of branches to track. Other branches will be ' . 'completely ignored. If left empty, all branches are tracked. ' . 'Example: <tt>master, release</tt>'));
     }
     $inset->appendChild(id(new AphrontFormTextControl())->setName('frequency')->setLabel('Pull Frequency')->setValue($repository->getDetail('pull-frequency', 15))->setCaption('Number of seconds daemon should sleep between requests. Larger ' . 'numbers reduce load but also decrease responsiveness.'));
     $form->appendChild($inset);
     $inset = new AphrontFormInsetView();
     $inset->setTitle('Application Configuration');
     if ($has_branches) {
         $inset->appendChild(id(new AphrontFormTextControl())->setName('default-branch')->setLabel('Default Branch')->setValue($repository->getDefaultBranch())->setError($e_branch)->setCaption('Default branch to show in Diffusion.'));
     }
     $inset->appendChild(id(new AphrontFormSelectControl())->setName('autoclose')->setLabel('Autoclose')->setOptions(array('enabled' => 'Enabled: Automatically Close Pushed Revisions', 'disabled' => 'Disabled: Ignore Pushed Revisions'))->setCaption("Automatically close Differential revisions when associated commits " . "are pushed to this repository.")->setValue($repository->getDetail('disable-autoclose', false) ? 'disabled' : 'enabled'));
     if ($has_branch_filter) {
         $close_commits_filter_str = implode(', ', array_keys($repository->getDetail('close-commits-filter', array())));
         $inset->appendChild(id(new AphrontFormTextControl())->setName('close-commits-filter')->setLabel('Autoclose Branches')->setValue($close_commits_filter_str)->setCaption('Optional list of branches which can trigger autoclose. ' . 'If left empty, all branches trigger autoclose.'));
     }
     $inset->appendChild(id(new AphrontFormTextControl())->setName('default-owners-path')->setLabel('Default Owners Path')->setValue($repository->getDetail('default-owners-path', '/'))->setCaption('Default path in Owners tool.'));
     $inset->appendChild(id(new AphrontFormSelectControl())->setName('herald-disabled')->setLabel('Herald/Feed Enabled')->setValue($repository->getDetail('herald-disabled', 0))->setOptions(array(0 => 'Enabled - Send Email and Publish Stories', 1 => 'Disabled - Do Not Send Email or Publish Stories'))->setCaption('You can disable Herald commit notifications and feed stories ' . 'for this repository. This can be useful when initially importing ' . 'a repository. Feed stories are never published about commits ' . 'that are more than 24 hours old.'));
     $parsers = id(new PhutilSymbolLoader())->setAncestorClass('PhabricatorRepositoryCommitMessageDetailParser')->selectSymbolsWithoutLoading();
     $parsers = ipull($parsers, 'name', 'name');
     $inset->appendChild('<p class="aphront-form-instructions">If you extend the commit ' . 'message format, you can provide a new parser which will extract ' . 'extra information from it when commits are imported. This is an ' . 'advanced feature, and using the default parser will be suitable ' . 'in most cases.</p>')->appendChild(id(new AphrontFormSelectControl())->setName('detail-parser')->setLabel('Detail Parser')->setOptions($parsers)->setValue($repository->getDetail('detail-parser', 'PhabricatorRepositoryDefaultCommitMessageDetailParser')));
     if ($is_svn) {
         $inset->appendChild(id(new AphrontFormTextControl())->setName('uuid')->setLabel('UUID')->setValue($repository->getUUID())->setCaption('Repository UUID from <tt>svn info</tt>.'));
     }
     $form->appendChild($inset);
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Save Configuration'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Repository Tracking');
     $panel->appendChild($form);
     $panel->setWidth(AphrontPanelView::WIDTH_WIDE);
     $nav = $this->sideNav;
     $nav->appendChild($error_view);
     $nav->appendChild($panel);
     return $this->buildStandardPageResponse($nav, array('title' => 'Edit Repository Tracking'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $request = $this->getRequest();
     $object_name = trim($request->getStr('object_name'));
     $e_name = true;
     $errors = array();
     if ($request->isFormPost()) {
         if (!$object_name) {
             $e_name = 'Required';
             $errors[] = 'An object name is required.';
         }
         if (!$errors) {
             $matches = null;
             $object = null;
             if (preg_match('/^D(\\d+)$/', $object_name, $matches)) {
                 $object = id(new DifferentialRevision())->load($matches[1]);
                 if (!$object) {
                     $e_name = 'Invalid';
                     $errors[] = 'No Differential Revision with that ID exists.';
                 }
             } else {
                 if (preg_match('/^r([A-Z]+)(\\w+)$/', $object_name, $matches)) {
                     $repo = id(new PhabricatorRepository())->loadOneWhere('callsign = %s', $matches[1]);
                     if (!$repo) {
                         $e_name = 'Invalid';
                         $errors[] = 'There is no repository with the callsign ' . $matches[1] . '.';
                     }
                     $commit = id(new PhabricatorRepositoryCommit())->loadOneWhere('repositoryID = %d AND commitIdentifier = %s', $repo->getID(), $matches[2]);
                     if (!$commit) {
                         $e_name = 'Invalid';
                         $errors[] = 'There is no commit with that identifier.';
                     }
                     $object = $commit;
                 } else {
                     $e_name = 'Invalid';
                     $errors[] = 'This object name is not recognized.';
                 }
             }
             if (!$errors) {
                 if ($object instanceof DifferentialRevision) {
                     $adapter = new HeraldDifferentialRevisionAdapter($object, $object->loadActiveDiff());
                 } else {
                     if ($object instanceof PhabricatorRepositoryCommit) {
                         $data = id(new PhabricatorRepositoryCommitData())->loadOneWhere('commitID = %d', $object->getID());
                         $adapter = new HeraldCommitAdapter($repo, $object, $data);
                     } else {
                         throw new Exception("Can not build adapter for object!");
                     }
                 }
                 $rules = HeraldRule::loadAllByContentTypeWithFullData($adapter->getHeraldTypeName());
                 $engine = new HeraldEngine();
                 $effects = $engine->applyRules($rules, $adapter);
                 $dry_run = new HeraldDryRunAdapter();
                 $engine->applyEffects($effects, $dry_run);
                 $xscript = $engine->getTranscript();
                 return id(new AphrontRedirectResponse())->setURI('/herald/transcript/' . $xscript->getID() . '/');
             }
         }
     }
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     } else {
         $error_view = null;
     }
     $form = id(new AphrontFormView())->setUser($user)->appendChild('<p class="aphront-form-instructions">Enter an object to test rules ' . 'for, like a Diffusion commit (e.g., <tt>rX123</tt>) or a ' . 'Differential revision (e.g., <tt>D123</tt>). You will be shown the ' . 'results of a dry run on the object.</p>')->appendChild(id(new AphrontFormTextControl())->setLabel('Object Name')->setName('object_name')->setError($e_name)->setValue($object_name))->appendChild(id(new AphrontFormSubmitControl())->setValue('Test Rules'));
     $panel = new AphrontPanelView();
     $panel->setHeader('Test Herald Rules');
     $panel->setWidth(AphrontPanelView::WIDTH_FULL);
     $panel->appendChild($form);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Test Console', 'tab' => 'test'));
 }
 private function processCreateRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $fork = $request->getInt('fork');
     $error_view = null;
     $e_text = true;
     $new_paste = new PhabricatorPaste();
     $new_paste_text = null;
     $new_paste_language = PhabricatorEnv::getEnvConfig('pygments.dropdown-default');
     if ($request->isFormPost()) {
         $errors = array();
         $text = $request->getStr('text');
         if (!strlen($text)) {
             $e_text = 'Required';
             $errors[] = 'The paste may not be blank.';
         } else {
             $e_text = null;
         }
         $parent_phid = $request->getStr('parent');
         if ($parent_phid) {
             $parent = id(new PhabricatorPaste())->loadOneWhere('phid = %s', $parent_phid);
             if ($parent) {
                 $new_paste->setParentPHID($parent->getPHID());
             }
         }
         $title = $request->getStr('title');
         $new_paste->setTitle($title);
         $new_paste_language = $request->getStr('language');
         if (!$errors) {
             if ($new_paste_language == 'infer') {
                 // If it's infer, store an empty string. Otherwise, store the
                 // language name. We do this so we can refer to 'infer' elsewhere
                 // in the code (such as default value) while retaining backwards
                 // compatibility with old posts with no language stored.
                 $new_paste_language = '';
             }
             $new_paste->setLanguage($new_paste_language);
             $new_paste_file = PhabricatorFile::newFromFileData($text, array('name' => $title, 'mime-type' => 'text/plain; charset=utf-8', 'authorPHID' => $user->getPHID()));
             $new_paste->setFilePHID($new_paste_file->getPHID());
             $new_paste->setAuthorPHID($user->getPHID());
             $new_paste->save();
             return id(new AphrontRedirectResponse())->setURI('/P' . $new_paste->getID());
         } else {
             $error_view = new AphrontErrorView();
             $error_view->setErrors($errors);
             $error_view->setTitle('A problem has occurred!');
         }
     } else {
         if ($fork) {
             $fork_paste = id(new PhabricatorPaste())->load($fork);
             if ($fork_paste) {
                 $new_paste->setTitle('Fork of ' . $fork_paste->getID() . ': ' . $fork_paste->getTitle());
                 $fork_file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $fork_paste->getFilePHID());
                 $new_paste_text = $fork_file->loadFileData();
                 $new_paste_language = nonempty($fork_paste->getLanguage(), 'infer');
                 $new_paste->setParentPHID($fork_paste->getPHID());
             }
         }
     }
     $this->setErrorView($error_view);
     $this->setErrorText($e_text);
     $this->setPasteText($new_paste_text);
     $new_paste->setLanguage($new_paste_language);
     $this->setPaste($new_paste);
 }