public function processRequest()
 {
     $request = $this->getRequest();
     $chrono_key = $request->getStr('chronoKey');
     $user = $request->getUser();
     if ($request->isDialogFormPost()) {
         $table = new PhabricatorFeedStoryNotification();
         queryfx($table->establishConnection('w'), 'UPDATE %T SET hasViewed = 1 ' . 'WHERE userPHID = %s AND hasViewed = 0 and chronologicalKey <= %s', $table->getTableName(), $user->getPHID(), $chrono_key);
         return id(new AphrontReloadResponse())->setURI('/notification/');
     }
     $dialog = new AphrontDialogView();
     $dialog->setUser($user);
     $dialog->addCancelButton('/notification/');
     if ($chrono_key) {
         $dialog->setTitle(pht('Really mark all notifications as read?'));
         $dialog->addHiddenInput('chronoKey', $chrono_key);
         $is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
         if ($is_serious) {
             $dialog->appendChild(pht('All unread notifications will be marked as read. You can not ' . 'undo this action.'));
         } else {
             $dialog->appendChild(pht("You can't ignore your problems forever, you know."));
         }
         $dialog->addSubmitButton(pht('Mark All Read'));
     } else {
         $dialog->setTitle(pht('No notifications to mark as read.'));
         $dialog->appendChild(pht('You have no unread notifications.'));
     }
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     if (!$request->isFormPost()) {
         return new Aphront400Response();
     }
     $revision_id = $request->getInt('revision_id');
     $revision = id(new DifferentialRevision())->load($revision_id);
     if (!$revision) {
         return new Aphront400Response();
     }
     $comment = $request->getStr('comment');
     $action = $request->getStr('action');
     $reviewers = $request->getArr('reviewers');
     $ccs = $request->getArr('ccs');
     $editor = new DifferentialCommentEditor($revision, $request->getUser()->getPHID(), $action);
     $content_source = PhabricatorContentSource::newForSource(PhabricatorContentSource::SOURCE_WEB, array('ip' => $request->getRemoteAddr()));
     try {
         $editor->setMessage($comment)->setContentSource($content_source)->setAttachInlineComments(true)->setAddedReviewers($reviewers)->setAddedCCs($ccs)->save();
     } catch (DifferentialActionHasNoEffectException $no_effect) {
         $has_inlines = id(new DifferentialInlineComment())->loadAllWhere('authorPHID = %s AND revisionID = %d AND commentID IS NULL', $request->getUser()->getPHID(), $revision->getID());
         $dialog = new AphrontDialogView();
         $dialog->setUser($request->getUser());
         $dialog->addCancelButton('/D' . $revision_id);
         $dialog->addHiddenInput('revision_id', $revision_id);
         $dialog->addHiddenInput('action', 'none');
         $dialog->addHiddenInput('reviewers', $reviewers);
         $dialog->addHiddenInput('ccs', $ccs);
         $dialog->addHiddenInput('comment', $comment);
         $dialog->setTitle('Action Has No Effect');
         $dialog->appendChild('<p>' . phutil_escape_html($no_effect->getMessage()) . '</p>');
         if (strlen($comment) || $has_inlines) {
             $dialog->addSubmitButton('Post as Comment');
             $dialog->appendChild('<br />');
             $dialog->appendChild('<p>Do you want to post your feedback anyway, as a normal ' . 'comment?</p>');
         }
         return id(new AphrontDialogResponse())->setDialog($dialog);
     }
     // TODO: Diff change detection?
     $draft = id(new PhabricatorDraft())->loadOneWhere('authorPHID = %s AND draftKey = %s', $request->getUser()->getPHID(), 'differential-comment-' . $revision->getID());
     if ($draft) {
         $draft->delete();
     }
     return id(new AphrontRedirectResponse())->setURI('/D' . $revision->getID());
 }
 public function buildDialog()
 {
     $user = $this->user;
     $filter_id = celerity_generate_unique_node_id();
     $query_id = celerity_generate_unique_node_id();
     $results_id = celerity_generate_unique_node_id();
     $current_id = celerity_generate_unique_node_id();
     $search_id = celerity_generate_unique_node_id();
     $form_id = celerity_generate_unique_node_id();
     require_celerity_resource('phabricator-object-selector-css');
     $options = array();
     foreach ($this->filters as $key => $label) {
         $options[] = phutil_tag('option', array('value' => $key, 'selected' => $key == $this->selectedFilter ? 'selected' : null), $label);
     }
     $instructions = null;
     if ($this->instructions) {
         $instructions = phutil_tag('p', array('class' => 'phabricator-object-selector-instructions'), $this->instructions);
     }
     $search_box = phabricator_form($user, array('method' => 'POST', 'action' => $this->submitURI, 'id' => $search_id), phutil_tag('table', array('class' => 'phabricator-object-selector-search'), phutil_tag('tr', array(), array(phutil_tag('td', array('class' => 'phabricator-object-selector-search-filter'), phutil_tag('select', array('id' => $filter_id), $options)), phutil_tag('td', array('class' => 'phabricator-object-selector-search-text'), phutil_tag('input', array('id' => $query_id, 'type' => 'text')))))));
     $result_box = phutil_tag('div', array('class' => 'phabricator-object-selector-results', 'id' => $results_id), '');
     $attached_box = phutil_tag_div('phabricator-object-selector-current', phutil_tag_div('phabricator-object-selector-currently-attached', array(phutil_tag_div('phabricator-object-selector-header', $this->header), phutil_tag('div', array('id' => $current_id)), $instructions)));
     $dialog = new AphrontDialogView();
     $dialog->setUser($this->user)->setTitle($this->title)->setClass('phabricator-object-selector-dialog')->appendChild($search_box)->appendChild($result_box)->appendChild($attached_box)->setRenderDialogAsDiv()->setFormID($form_id)->addSubmitButton($this->buttonText);
     if ($this->cancelURI) {
         $dialog->addCancelButton($this->cancelURI);
     }
     $handle_views = array();
     foreach ($this->handles as $handle) {
         $phid = $handle->getPHID();
         $view = new PhabricatorHandleObjectSelectorDataView($handle);
         $handle_views[$phid] = $view->renderData();
     }
     $dialog->addHiddenInput('phids', implode(';', array_keys($this->handles)));
     Javelin::initBehavior('phabricator-object-selector', array('filter' => $filter_id, 'query' => $query_id, 'search' => $search_id, 'results' => $results_id, 'current' => $current_id, 'form' => $form_id, 'exclude' => $this->excluded, 'uri' => $this->searchURI, 'handles' => $handle_views));
     return $dialog;
 }
 public function processRequest()
 {
     $current_user = $this->getRequest()->getUser();
     $provider = $this->provider;
     if (!$provider->isProviderEnabled()) {
         return new Aphront400Response();
     }
     $provider_name = $provider->getProviderName();
     $provider_key = $provider->getProviderKey();
     $request = $this->getRequest();
     if ($request->getStr('error')) {
         $error_view = id(new PhabricatorOAuthFailureView())->setRequest($request);
         return $this->buildErrorResponse($error_view);
     }
     $error_response = $this->retrieveAccessToken($provider);
     if ($error_response) {
         return $error_response;
     }
     $userinfo_uri = new PhutilURI($provider->getUserInfoURI());
     $userinfo_uri->setQueryParams(array('access_token' => $this->accessToken));
     $user_json = @file_get_contents($userinfo_uri);
     $user_data = json_decode($user_json, true);
     $provider->setUserData($user_data);
     $provider->setAccessToken($this->accessToken);
     $user_id = $provider->retrieveUserID();
     $provider_key = $provider->getProviderKey();
     $oauth_info = $this->retrieveOAuthInfo($provider);
     if ($current_user->getPHID()) {
         if ($oauth_info->getID()) {
             if ($oauth_info->getUserID() != $current_user->getID()) {
                 $dialog = new AphrontDialogView();
                 $dialog->setUser($current_user);
                 $dialog->setTitle('Already Linked to Another Account');
                 $dialog->appendChild('<p>The ' . $provider_name . ' account you just authorized ' . 'is already linked to another Phabricator account. Before you can ' . 'associate your ' . $provider_name . ' account with this Phabriactor ' . 'account, you must unlink it from the Phabricator account it is ' . 'currently linked to.</p>');
                 $dialog->addCancelButton('/settings/page/' . $provider_key . '/');
                 return id(new AphrontDialogResponse())->setDialog($dialog);
             } else {
                 return id(new AphrontRedirectResponse())->setURI('/settings/page/' . $provider_key . '/');
             }
         }
         $existing_oauth = id(new PhabricatorUserOAuthInfo())->loadOneWhere('userID = %d AND oauthProvider = %s', $current_user->getID(), $provider_key);
         if ($existing_oauth) {
             $dialog = new AphrontDialogView();
             $dialog->setUser($current_user);
             $dialog->setTitle('Already Linked to an Account From This Provider');
             $dialog->appendChild('<p>The account you are logged in with is already linked to a ' . $provider_name . ' account. Before you can link it to a different ' . $provider_name . ' account, you must unlink the old account.</p>');
             $dialog->addCancelButton('/settings/page/' . $provider_key . '/');
             return id(new AphrontDialogResponse())->setDialog($dialog);
         }
         if (!$request->isDialogFormPost()) {
             $dialog = new AphrontDialogView();
             $dialog->setUser($current_user);
             $dialog->setTitle('Link ' . $provider_name . ' Account');
             $dialog->appendChild('<p>Link your ' . $provider_name . ' account to your Phabricator ' . 'account?</p>');
             $dialog->addHiddenInput('token', $provider->getAccessToken());
             $dialog->addHiddenInput('expires', $oauth_info->getTokenExpires());
             $dialog->addHiddenInput('state', $this->oauthState);
             $dialog->addSubmitButton('Link Accounts');
             $dialog->addCancelButton('/settings/page/' . $provider_key . '/');
             return id(new AphrontDialogResponse())->setDialog($dialog);
         }
         $oauth_info->setUserID($current_user->getID());
         $this->saveOAuthInfo($oauth_info);
         return id(new AphrontRedirectResponse())->setURI('/settings/page/' . $provider_key . '/');
     }
     $next_uri = $request->getCookie('next_uri', '/');
     // Login with known auth.
     if ($oauth_info->getID()) {
         $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
         $known_user = id(new PhabricatorUser())->load($oauth_info->getUserID());
         $request->getApplicationConfiguration()->willAuthenticateUserWithOAuth($known_user, $oauth_info, $provider);
         $session_key = $known_user->establishSession('web');
         $this->saveOAuthInfo($oauth_info);
         $request->setCookie('phusr', $known_user->getUsername());
         $request->setCookie('phsid', $session_key);
         $request->clearCookie('next_uri');
         return id(new AphrontRedirectResponse())->setURI($next_uri);
     }
     $oauth_email = $provider->retrieveUserEmail();
     if ($oauth_email) {
         $known_email = id(new PhabricatorUser())->loadOneWhere('email = %s', $oauth_email);
         if ($known_email) {
             $dialog = new AphrontDialogView();
             $dialog->setUser($current_user);
             $dialog->setTitle('Already Linked to Another Account');
             $dialog->appendChild('<p>The ' . $provider_name . ' account you just authorized has an ' . 'email address which is already in use by another Phabricator ' . 'account. To link the accounts, log in to your Phabricator ' . 'account and then go to Settings.</p>');
             $dialog->addCancelButton('/login/');
             return id(new AphrontDialogResponse())->setDialog($dialog);
         }
     }
     if (!$provider->isProviderRegistrationEnabled()) {
         $dialog = new AphrontDialogView();
         $dialog->setUser($current_user);
         $dialog->setTitle('No Account Registration With ' . $provider_name);
         $dialog->appendChild('<p>You can not register a new account using ' . $provider_name . '; ' . 'you can only use your ' . $provider_name . ' account to log into an ' . 'existing Phabricator account which you have registered through ' . 'other means.</p>');
         $dialog->addCancelButton('/login/');
         return id(new AphrontDialogResponse())->setDialog($dialog);
     }
     $class = PhabricatorEnv::getEnvConfig('controller.oauth-registration');
     PhutilSymbolLoader::loadClass($class);
     $controller = newv($class, array($this->getRequest()));
     $controller->setOAuthProvider($provider);
     $controller->setOAuthInfo($oauth_info);
     $controller->setOAuthState($this->oauthState);
     return $this->delegateToController($controller);
 }
 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();
     $this->readRequestParameters();
     switch ($this->getOperation()) {
         case 'delete':
             $inline = $this->loadCommentForEdit($this->getCommentID());
             if ($request->isFormPost()) {
                 $inline->delete();
                 return $this->buildEmptyResponse();
             }
             $dialog = new AphrontDialogView();
             $dialog->setUser($user);
             $dialog->setSubmitURI($request->getRequestURI());
             $dialog->setTitle('Really delete this comment?');
             $dialog->addHiddenInput('id', $this->getCommentID());
             $dialog->addHiddenInput('op', 'delete');
             $dialog->appendChild('<p>Delete this inline comment?</p>');
             $dialog->addCancelButton('#');
             $dialog->addSubmitButton('Delete');
             return id(new AphrontDialogResponse())->setDialog($dialog);
         case 'edit':
             $inline = $this->loadCommentForEdit($this->getCommentID());
             $text = $this->getCommentText();
             if ($request->isFormPost()) {
                 if (strlen($text)) {
                     $inline->setContent($text);
                     $inline->save();
                     return $this->buildRenderedCommentResponse($inline, $this->getIsOnRight());
                 } else {
                     $inline->delete();
                     return $this->buildEmptyResponse();
                 }
             }
             $edit_dialog = $this->buildEditDialog();
             $edit_dialog->setTitle('Edit Inline Comment');
             $edit_dialog->addHiddenInput('id', $this->getCommentID());
             $edit_dialog->addHiddenInput('op', 'edit');
             $edit_dialog->appendChild($this->renderTextArea(nonempty($text, $inline->getContent())));
             return id(new AphrontAjaxResponse())->setContent($edit_dialog->render());
         case 'create':
             $text = $this->getCommentText();
             if (!$request->isFormPost() || !strlen($text)) {
                 return $this->buildEmptyResponse();
             }
             $inline = $this->createComment()->setChangesetID($this->getChangesetID())->setAuthorPHID($user->getPHID())->setLineNumber($this->getLineNumber())->setLineLength($this->getLineLength())->setIsNewFile($this->getIsNewFile())->setContent($text)->save();
             return $this->buildRenderedCommentResponse($inline, $this->getIsOnRight());
         case 'reply':
         default:
             $edit_dialog = $this->buildEditDialog();
             if ($this->getOperation() == 'reply') {
                 $inline = $this->loadComment($this->getCommentID());
                 $edit_dialog->setTitle('Reply to Inline Comment');
                 $changeset = $inline->getChangesetID();
                 $is_new = $inline->getIsNewFile();
                 $number = $inline->getLineNumber();
                 $length = $inline->getLineLength();
             } else {
                 $edit_dialog->setTitle('New Inline Comment');
                 $changeset = $this->getChangesetID();
                 $is_new = $this->getIsNewFile();
                 $number = $this->getLineNumber();
                 $length = $this->getLineLength();
             }
             $edit_dialog->addHiddenInput('op', 'create');
             $edit_dialog->addHiddenInput('changeset', $changeset);
             $edit_dialog->addHiddenInput('is_new', $is_new);
             $edit_dialog->addHiddenInput('number', $number);
             $edit_dialog->addHiddenInput('length', $length);
             $text_area = $this->renderTextArea($this->getCommentText());
             $edit_dialog->appendChild($text_area);
             return id(new AphrontAjaxResponse())->setContent($edit_dialog->render());
     }
 }
 public function buildDialog()
 {
     $user = $this->user;
     $filter_id = celerity_generate_unique_node_id();
     $query_id = celerity_generate_unique_node_id();
     $results_id = celerity_generate_unique_node_id();
     $current_id = celerity_generate_unique_node_id();
     $search_id = celerity_generate_unique_node_id();
     $form_id = celerity_generate_unique_node_id();
     require_celerity_resource('phabricator-object-selector-css');
     $options = array();
     foreach ($this->filters as $key => $label) {
         $options[] = phutil_render_tag('option', array('value' => $key, 'selected' => $key == $this->selectedFilter ? 'selected' : null), $label);
     }
     $options = implode("\n", $options);
     $instructions = null;
     if ($this->instructions) {
         $instructions = '<p class="phabricator-object-selector-instructions">' . $this->instructions . '</p>';
     }
     $search_box = phabricator_render_form($user, array('method' => 'POST', 'action' => $this->submitURI, 'id' => $search_id), '<table class="phabricator-object-selector-search">
     <tr>
       <td class="phabricator-object-selector-search-filter">
         <select id="' . $filter_id . '">' . $options . '</select>
       </td>
       <td class="phabricator-object-selector-search-text">
         <input type="text" id="' . $query_id . '" />
       </td>
     </tr>
   </table>');
     $result_box = '<div class="phabricator-object-selector-results" id="' . $results_id . '">' . '</div>';
     $attached_box = '<div class="phabricator-object-selector-current">' . '<div class="phabricator-object-selector-currently-attached">' . '<div class="phabricator-object-selector-header">' . phutil_escape_html($this->header) . '</div>' . '<div id="' . $current_id . '">' . '</div>' . $instructions . '</div>' . '</div>';
     $dialog = new AphrontDialogView();
     $dialog->setUser($this->user)->setTitle($this->title)->setClass('phabricator-object-selector-dialog')->appendChild($search_box)->appendChild($result_box)->appendChild($attached_box)->setRenderDialogAsDiv()->setFormID($form_id)->addSubmitButton($this->buttonText);
     if ($this->cancelURI) {
         $dialog->addCancelButton($this->cancelURI);
     }
     $handle_views = array();
     foreach ($this->handles as $handle) {
         $phid = $handle->getPHID();
         $view = new PhabricatorHandleObjectSelectorDataView($handle);
         $handle_views[$phid] = $view->renderData();
     }
     $dialog->addHiddenInput('phids', implode(';', array_keys($this->handles)));
     Javelin::initBehavior('phabricator-object-selector', array('filter' => $filter_id, 'query' => $query_id, 'search' => $search_id, 'results' => $results_id, 'current' => $current_id, 'form' => $form_id, 'exclude' => $this->excluded, 'uri' => $this->searchURI, 'handles' => $handle_views));
     return $dialog;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $changeset = $request->getInt('changeset');
     $is_new = $request->getBool('is_new');
     $on_right = $request->getBool('on_right');
     $number = $request->getInt('number');
     $length = $request->getInt('length');
     $text = $request->getStr('text');
     $op = $request->getStr('op');
     $inline_id = $request->getInt('id');
     $user = $request->getUser();
     $submit_uri = '/differential/comment/inline/edit/' . $this->revisionID . '/';
     $edit_dialog = new AphrontDialogView();
     $edit_dialog->setUser($user);
     $edit_dialog->setSubmitURI($submit_uri);
     $edit_dialog->addHiddenInput('on_right', $on_right);
     $edit_dialog->addSubmitButton();
     $edit_dialog->addCancelButton('#');
     switch ($op) {
         case 'delete':
             $inline = $this->loadInlineCommentForEditing($inline_id);
             if ($request->isFormPost()) {
                 $inline->delete();
                 return $this->buildEmptyResponse();
             }
             $edit_dialog->setTitle('Really delete this comment?');
             $edit_dialog->addHiddenInput('id', $inline_id);
             $edit_dialog->addHiddenInput('op', 'delete');
             $edit_dialog->appendChild('<p>Delete this inline comment?</p>');
             return id(new AphrontDialogResponse())->setDialog($edit_dialog);
         case 'edit':
             $inline = $this->loadInlineCommentForEditing($inline_id);
             if ($request->isFormPost()) {
                 if (strlen($text)) {
                     $inline->setContent($text);
                     $inline->setCache(null);
                     $inline->save();
                     return $this->buildRenderedCommentResponse($inline, $on_right);
                 } else {
                     $inline->delete();
                     return $this->buildEmptyResponse();
                 }
             }
             $edit_dialog->setTitle('Edit Inline Comment');
             $edit_dialog->addHiddenInput('id', $inline_id);
             $edit_dialog->addHiddenInput('op', 'edit');
             $edit_dialog->appendChild($this->renderTextArea(nonempty($text, $inline->getContent())));
             return id(new AphrontDialogResponse())->setDialog($edit_dialog);
         case 'create':
             if (!$request->isFormPost() || !strlen($text)) {
                 return $this->buildEmptyResponse();
             }
             // Verify revision and changeset correspond to actual objects.
             $revision_obj = id(new DifferentialRevision())->load($this->revisionID);
             $changeset_obj = id(new DifferentialChangeset())->load($changeset);
             if (!$revision_obj || !$changeset_obj) {
                 throw new Exception("Invalid revision ID or changeset ID!");
             }
             $inline = id(new DifferentialInlineComment())->setRevisionID($this->revisionID)->setChangesetID($changeset)->setCommentID(null)->setAuthorPHID($user->getPHID())->setLineNumber($number)->setLineLength($length)->setIsNewFile($on_right)->setContent($text)->save();
             return $this->buildRenderedCommentResponse($inline, $on_right);
         case 'reply':
         default:
             if ($op == 'reply') {
                 $inline = $this->loadInlineComment($inline_id);
                 // Override defaults.
                 $changeset = $inline->getChangesetID();
                 $is_new = $inline->getIsNewFile();
                 $number = $inline->getLineNumber();
                 $length = $inline->getLineLength();
                 $edit_dialog->setTitle('Reply to Inline Comment');
             } else {
                 $edit_dialog->setTitle('New Inline Comment');
             }
             $edit_dialog->addHiddenInput('op', 'create');
             $edit_dialog->addHiddenInput('changeset', $changeset);
             $edit_dialog->addHiddenInput('is_new', $is_new);
             $edit_dialog->addHiddenInput('number', $number);
             $edit_dialog->addHiddenInput('length', $length);
             $edit_dialog->appendChild($this->renderTextArea($text));
             return id(new AphrontDialogResponse())->setDialog($edit_dialog);
     }
 }
 public function processRequest()
 {
     $current_user = $this->getRequest()->getUser();
     $provider = $this->provider;
     if (!$provider->isProviderEnabled()) {
         return new Aphront400Response();
     }
     $provider_name = $provider->getProviderName();
     $provider_key = $provider->getProviderKey();
     $request = $this->getRequest();
     if ($request->getStr('error')) {
         $error_view = id(new PhabricatorOAuthFailureView())->setRequest($request);
         return $this->buildErrorResponse($error_view);
     }
     $error_response = $this->retrieveAccessToken($provider);
     if ($error_response) {
         return $error_response;
     }
     $userinfo_uri = new PhutilURI($provider->getUserInfoURI());
     $userinfo_uri->setQueryParam('access_token', $this->accessToken);
     $userinfo_uri = (string) $userinfo_uri;
     try {
         $user_data = HTTPSFuture::loadContent($userinfo_uri);
         if ($user_data === false) {
             throw new PhabricatorOAuthProviderException("Request to '{$userinfo_uri}' failed!");
         }
         $provider->setUserData($user_data);
     } catch (PhabricatorOAuthProviderException $e) {
         return $this->buildErrorResponse(new PhabricatorOAuthFailureView(), $e);
     }
     $provider->setAccessToken($this->accessToken);
     $user_id = $provider->retrieveUserID();
     $provider_key = $provider->getProviderKey();
     $oauth_info = $this->retrieveOAuthInfo($provider);
     if ($current_user->getPHID()) {
         if ($oauth_info->getID()) {
             if ($oauth_info->getUserID() != $current_user->getID()) {
                 $dialog = new AphrontDialogView();
                 $dialog->setUser($current_user);
                 $dialog->setTitle('Already Linked to Another Account');
                 $dialog->appendChild(hsprintf('<p>The %s account you just authorized is already linked to ' . 'another Phabricator account. Before you can associate your %s ' . 'account with this Phabriactor account, you must unlink it from ' . 'the Phabricator account it is currently linked to.</p>', $provider_name, $provider_name));
                 $dialog->addCancelButton('/settings/page/' . $provider_key . '/');
                 return id(new AphrontDialogResponse())->setDialog($dialog);
             } else {
                 $this->saveOAuthInfo($oauth_info);
                 // Refresh token.
                 return id(new AphrontRedirectResponse())->setURI('/settings/page/' . $provider_key . '/');
             }
         }
         $existing_oauth = id(new PhabricatorUserOAuthInfo())->loadOneWhere('userID = %d AND oauthProvider = %s', $current_user->getID(), $provider_key);
         if ($existing_oauth) {
             $dialog = new AphrontDialogView();
             $dialog->setUser($current_user);
             $dialog->setTitle('Already Linked to an Account From This Provider');
             $dialog->appendChild(hsprintf('<p>The account you are logged in with is already linked to a %s ' . 'account. Before you can link it to a different %s account, you ' . 'must unlink the old account.</p>', $provider_name, $provider_name));
             $dialog->addCancelButton('/settings/page/' . $provider_key . '/');
             return id(new AphrontDialogResponse())->setDialog($dialog);
         }
         if (!$request->isDialogFormPost()) {
             $dialog = new AphrontDialogView();
             $dialog->setUser($current_user);
             $dialog->setTitle('Link ' . $provider_name . ' Account');
             $dialog->appendChild(hsprintf('<p>Link your %s account to your Phabricator account?</p>', $provider_name));
             $dialog->addHiddenInput('confirm_token', $provider->getAccessToken());
             $dialog->addHiddenInput('expires', $oauth_info->getTokenExpires());
             $dialog->addHiddenInput('state', $this->oauthState);
             $dialog->addHiddenInput('scope', $oauth_info->getTokenScope());
             $dialog->addSubmitButton('Link Accounts');
             $dialog->addCancelButton('/settings/page/' . $provider_key . '/');
             return id(new AphrontDialogResponse())->setDialog($dialog);
         }
         $oauth_info->setUserID($current_user->getID());
         $this->saveOAuthInfo($oauth_info);
         return id(new AphrontRedirectResponse())->setURI('/settings/page/' . $provider_key . '/');
     }
     // Login with known auth.
     if ($oauth_info->getID()) {
         $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
         $known_user = id(new PhabricatorUser())->load($oauth_info->getUserID());
         $request->getApplicationConfiguration()->willAuthenticateUserWithOAuth($known_user, $oauth_info, $provider);
         $session_key = $known_user->establishSession('web');
         $this->saveOAuthInfo($oauth_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);
     }
     $oauth_email = $provider->retrieveUserEmail();
     if ($oauth_email) {
         $known_email = id(new PhabricatorUserEmail())->loadOneWhere('address = %s', $oauth_email);
         if ($known_email) {
             $dialog = new AphrontDialogView();
             $dialog->setUser($current_user);
             $dialog->setTitle('Already Linked to Another Account');
             $dialog->appendChild(hsprintf('<p>The %s account you just authorized has an email address which ' . 'is already in use by another Phabricator account. To link the ' . 'accounts, log in to your Phabricator account and then go to ' . 'Settings.</p>', $provider_name));
             $user = id(new PhabricatorUser())->loadOneWhere('phid = %s', $known_email->getUserPHID());
             $oauth_infos = id(new PhabricatorUserOAuthInfo())->loadAllWhere('userID = %d', $user->getID());
             if ($oauth_infos) {
                 $providers = array();
                 foreach ($oauth_infos as $info) {
                     $provider = $info->getOAuthProvider();
                     $providers[] = PhabricatorOAuthProvider::newProvider($provider)->getProviderName();
                 }
                 $dialog->appendChild(hsprintf('<p>The account is associated with: %s.</p>', implode(', ', $providers)));
             }
             $dialog->addCancelButton('/login/');
             return id(new AphrontDialogResponse())->setDialog($dialog);
         }
     }
     if (!$provider->isProviderRegistrationEnabled()) {
         $dialog = new AphrontDialogView();
         $dialog->setUser($current_user);
         $dialog->setTitle('No Account Registration With ' . $provider_name);
         $dialog->appendChild(hsprintf('<p>You can not register a new account using %s; you can only use ' . 'your %s account to log into an existing Phabricator account which ' . 'you have registered through other means.</p>', $provider_name, $provider_name));
         $dialog->addCancelButton('/login/');
         return id(new AphrontDialogResponse())->setDialog($dialog);
     }
     $controller = PhabricatorEnv::newObjectFromConfig('controller.oauth-registration', array($this->getRequest()));
     $controller->setOAuthProvider($provider);
     $controller->setOAuthInfo($oauth_info);
     $controller->setOAuthState($this->oauthState);
     return $this->delegateToController($controller);
 }