public function handleRequest(AphrontRequest $request)
 {
     $viewer = $this->getViewer();
     $preferences = PhabricatorUserPreferences::loadUserPreferences($viewer);
     $editor = id(new PhabricatorUserPreferencesEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true)->setContinueOnMissingFields(true);
     $key = $request->getStr('key');
     $value = $request->getStr('value');
     $xactions = array();
     $xactions[] = $preferences->newTransaction($key, $value);
     $editor->applyTransactions($preferences, $xactions);
     return id(new AphrontAjaxResponse())->setContent(array());
 }
 private function writeSettings(array $map)
 {
     $request = $this->getRequest();
     $viewer = $this->getViewer();
     $preferences = PhabricatorUserPreferences::loadUserPreferences($viewer);
     $editor = id(new PhabricatorUserPreferencesEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true)->setContinueOnMissingFields(true);
     $xactions = array();
     foreach ($map as $key => $value) {
         $xactions[] = $preferences->newTransaction($key, $value);
     }
     $editor->applyTransactions($preferences, $xactions);
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $this->getViewer();
     // Redirect "/panel/XYZ/" to the viewer's personal settings panel. This
     // was the primary URI before global settings were introduced and allows
     // generation of viewer-agnostic URIs for email.
     $panel = $request->getURIData('panel');
     if ($panel) {
         $panel = phutil_escape_uri($panel);
         $username = $viewer->getUsername();
         $panel_uri = "/user/{$username}/page/{$panel}/";
         $panel_uri = $this->getApplicationURI($panel_uri);
         return id(new AphrontRedirectResponse())->setURI($panel_uri);
     }
     $username = $request->getURIData('username');
     $builtin = $request->getURIData('builtin');
     $key = $request->getURIData('pageKey');
     if ($builtin) {
         $this->builtinKey = $builtin;
         $preferences = id(new PhabricatorUserPreferencesQuery())->setViewer($viewer)->withBuiltinKeys(array($builtin))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$preferences) {
             $preferences = id(new PhabricatorUserPreferences())->attachUser(null)->setBuiltinKey($builtin);
         }
     } else {
         $user = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withUsernames(array($username))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$user) {
             return new Aphront404Response();
         }
         $preferences = PhabricatorUserPreferences::loadUserPreferences($user);
         $this->user = $user;
     }
     if (!$preferences) {
         return new Aphront404Response();
     }
     PhabricatorPolicyFilter::requireCapability($viewer, $preferences, PhabricatorPolicyCapability::CAN_EDIT);
     $this->preferences = $preferences;
     $panels = $this->buildPanels($preferences);
     $nav = $this->renderSideNav($panels);
     $key = $nav->selectFilter($key, head($panels)->getPanelKey());
     $panel = $panels[$key]->setController($this)->setNavigation($nav);
     $response = $panel->processRequest($request);
     if ($response instanceof AphrontResponse || $response instanceof AphrontResponseProducerInterface) {
         return $response;
     }
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($panel->getPanelName());
     $title = $panel->getPanelName();
     $view = id(new PHUITwoColumnView())->setNavigation($nav)->setMainColumn($response);
     return $this->newPage()->setTitle($title)->setCrumbs($crumbs)->appendChild($view);
 }
 private function browseFile()
 {
     $viewer = $this->getViewer();
     $request = $this->getRequest();
     $drequest = $this->getDiffusionRequest();
     $repository = $drequest->getRepository();
     $before = $request->getStr('before');
     if ($before) {
         return $this->buildBeforeResponse($before);
     }
     $path = $drequest->getPath();
     $blame_key = PhabricatorDiffusionBlameSetting::SETTINGKEY;
     $color_key = PhabricatorDiffusionColorSetting::SETTINGKEY;
     $show_blame = $request->getBool('blame', $viewer->getUserSetting($blame_key));
     $show_color = $request->getBool('color', $viewer->getUserSetting($color_key));
     $view = $request->getStr('view');
     if ($request->isFormPost() && $view != 'raw' && $viewer->isLoggedIn()) {
         $preferences = PhabricatorUserPreferences::loadUserPreferences($viewer);
         $editor = id(new PhabricatorUserPreferencesEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true)->setContinueOnMissingFields(true);
         $xactions = array();
         $xactions[] = $preferences->newTransaction($blame_key, $show_blame);
         $xactions[] = $preferences->newTransaction($color_key, $show_color);
         $editor->applyTransactions($preferences, $xactions);
         $uri = $request->getRequestURI()->alter('blame', null)->alter('color', null);
         return id(new AphrontRedirectResponse())->setURI($uri);
     }
     // We need the blame information if blame is on and we're building plain
     // text, or blame is on and this is an Ajax request. If blame is on and
     // this is a colorized request, we don't show blame at first (we ajax it
     // in afterward) so we don't need to query for it.
     $needs_blame = $show_blame && !$show_color || $show_blame && $request->isAjax();
     $params = array('commit' => $drequest->getCommit(), 'path' => $drequest->getPath());
     $byte_limit = null;
     if ($view !== 'raw') {
         $byte_limit = PhabricatorFileStorageEngine::getChunkThreshold();
         $time_limit = 10;
         $params += array('timeout' => $time_limit, 'byteLimit' => $byte_limit);
     }
     $response = $this->callConduitWithDiffusionRequest('diffusion.filecontentquery', $params);
     $hit_byte_limit = $response['tooHuge'];
     $hit_time_limit = $response['tooSlow'];
     $file_phid = $response['filePHID'];
     if ($hit_byte_limit) {
         $corpus = $this->buildErrorCorpus(pht('This file is larger than %s byte(s), and too large to display ' . 'in the web UI.', phutil_format_bytes($byte_limit)));
     } else {
         if ($hit_time_limit) {
             $corpus = $this->buildErrorCorpus(pht('This file took too long to load from the repository (more than ' . '%s second(s)).', new PhutilNumber($time_limit)));
         } else {
             $file = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($file_phid))->executeOne();
             if (!$file) {
                 throw new Exception(pht('Failed to load content file!'));
             }
             if ($view === 'raw') {
                 return $file->getRedirectResponse();
             }
             $data = $file->loadFileData();
             $ref = $this->getGitLFSRef($repository, $data);
             if ($ref) {
                 if ($view == 'git-lfs') {
                     $file = $this->loadGitLFSFile($ref);
                     // Rename the file locally so we generate a better vanity URI for
                     // it. In storage, it just has a name like "lfs-13f9a94c0923...",
                     // since we don't get any hints about possible human-readable names
                     // at upload time.
                     $basename = basename($drequest->getPath());
                     $file->makeEphemeral();
                     $file->setName($basename);
                     return $file->getRedirectResponse();
                 } else {
                     $corpus = $this->buildGitLFSCorpus($ref);
                 }
             } else {
                 if (ArcanistDiffUtils::isHeuristicBinaryFile($data)) {
                     $file_uri = $file->getBestURI();
                     if ($file->isViewableImage()) {
                         $corpus = $this->buildImageCorpus($file_uri);
                     } else {
                         $corpus = $this->buildBinaryCorpus($file_uri, $data);
                     }
                 } else {
                     $this->loadLintMessages();
                     $this->coverage = $drequest->loadCoverage();
                     // Build the content of the file.
                     $corpus = $this->buildCorpus($show_blame, $show_color, $data, $needs_blame, $drequest, $path, $data);
                 }
             }
         }
     }
     if ($request->isAjax()) {
         return id(new AphrontAjaxResponse())->setContent($corpus);
     }
     require_celerity_resource('diffusion-source-css');
     // Render the page.
     $view = $this->buildCurtain($drequest);
     $curtain = $this->enrichCurtain($view, $drequest, $show_blame, $show_color);
     $properties = $this->buildPropertyView($drequest);
     $header = $this->buildHeaderView($drequest);
     $header->setHeaderIcon('fa-file-code-o');
     $content = array();
     $follow = $request->getStr('follow');
     if ($follow) {
         $notice = new PHUIInfoView();
         $notice->setSeverity(PHUIInfoView::SEVERITY_WARNING);
         $notice->setTitle(pht('Unable to Continue'));
         switch ($follow) {
             case 'first':
                 $notice->appendChild(pht('Unable to continue tracing the history of this file because ' . 'this commit is the first commit in the repository.'));
                 break;
             case 'created':
                 $notice->appendChild(pht('Unable to continue tracing the history of this file because ' . 'this commit created the file.'));
                 break;
         }
         $content[] = $notice;
     }
     $renamed = $request->getStr('renamed');
     if ($renamed) {
         $notice = new PHUIInfoView();
         $notice->setSeverity(PHUIInfoView::SEVERITY_NOTICE);
         $notice->setTitle(pht('File Renamed'));
         $notice->appendChild(pht('File history passes through a rename from "%s" to "%s".', $drequest->getPath(), $renamed));
         $content[] = $notice;
     }
     $content[] = $corpus;
     $content[] = $this->buildOpenRevisions();
     $crumbs = $this->buildCrumbs(array('branch' => true, 'path' => true, 'view' => 'browse'));
     $crumbs->setBorder(true);
     $basename = basename($this->getDiffusionRequest()->getPath());
     $view = id(new PHUITwoColumnView())->setHeader($header)->setCurtain($curtain)->setMainColumn(array($content));
     if ($properties) {
         $view->addPropertySection(pht('Details'), $properties);
     }
     $title = array($basename, $repository->getDisplayName());
     return $this->newPage()->setTitle($title)->setCrumbs($crumbs)->appendChild(array($view));
 }
 private function handleProjectRequest(AphrontRequest $request)
 {
     $viewer = $this->getViewer();
     $errors = array();
     $e_project = true;
     if ($request->isFormPost()) {
         $project_phids = $request->getArr('projectPHIDs');
         $project_phid = head($project_phids);
         $project = id(new PhabricatorObjectQuery())->setViewer($viewer)->withPHIDs(array($project_phid))->executeOne();
         if ($project) {
             // Save this project as one of the user's most recently used projects,
             // so we'll show it by default in future menus.
             $favorites_key = PhabricatorPolicyFavoritesSetting::SETTINGKEY;
             $favorites = $viewer->getUserSetting($favorites_key);
             if (!is_array($favorites)) {
                 $favorites = array();
             }
             // Add this, or move it to the end of the list.
             unset($favorites[$project_phid]);
             $favorites[$project_phid] = true;
             $preferences = PhabricatorUserPreferences::loadUserPreferences($viewer);
             $editor = id(new PhabricatorUserPreferencesEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true)->setContinueOnMissingFields(true);
             $xactions = array();
             $xactions[] = $preferences->newTransaction($favorites_key, $favorites);
             $editor->applyTransactions($preferences, $xactions);
             $data = array('phid' => $project->getPHID(), 'info' => array('name' => $project->getName(), 'full' => $project->getName(), 'icon' => $project->getDisplayIconIcon()));
             return id(new AphrontAjaxResponse())->setContent($data);
         } else {
             $errors[] = pht('You must choose a project.');
             $e_project = pht('Required');
         }
     }
     $project_datasource = id(new PhabricatorProjectDatasource())->setParameters(array('policy' => 1));
     $form = id(new AphrontFormView())->setUser($viewer)->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Members Of'))->setName('projectPHIDs')->setLimit(1)->setError($e_project)->setDatasource($project_datasource));
     return $this->newDialog()->setWidth(AphrontDialogView::WIDTH_FORM)->setErrors($errors)->setTitle(pht('Select Project'))->appendForm($form)->addSubmitButton(pht('Done'))->addCancelButton('#');
 }
 private function writeSetting(PhabricatorUser $user, $key, $value)
 {
     $preferences = PhabricatorUserPreferences::loadUserPreferences($user);
     $editor = id(new PhabricatorUserPreferencesEditor())->setActor($user)->setContentSource($this->newContentSource())->setContinueOnNoEffect(true)->setContinueOnMissingFields(true);
     $xactions = array();
     $xactions[] = $preferences->newTransaction($key, $value);
     $editor->applyTransactions($preferences, $xactions);
     return id(new PhabricatorPeopleQuery())->setViewer($user)->withIDs(array($user->getID()))->executeOne();
 }