public function buildResponseString()
 {
     if ($this->shouldStopForDebugging()) {
         $request = $this->getRequest();
         $viewer = $request->getUser();
         $view = new PhabricatorStandardPageView();
         $view->setRequest($this->getRequest());
         $view->setApplicationName(pht('Debug'));
         $view->setTitle(pht('Stopped on Redirect'));
         $dialog = new AphrontDialogView();
         $dialog->setUser($viewer);
         $dialog->setTitle(pht('Stopped on Redirect'));
         $dialog->appendParagraph(pht('You were stopped here because %s is set in your configuration.', phutil_tag('tt', array(), 'debug.stop-on-redirect')));
         $dialog->appendParagraph(pht('You are being redirected to: %s', phutil_tag('tt', array(), $this->getURI())));
         $dialog->addCancelButton($this->getURI(), pht('Continue'));
         $dialog->appendChild(phutil_tag('br'));
         $dialog->appendChild(id(new AphrontStackTraceView())->setUser($viewer)->setTrace($this->stackWhenCreated));
         $dialog->setIsStandalone(true);
         $dialog->setWidth(AphrontDialogView::WIDTH_FULL);
         $box = id(new PHUIBoxView())->addMargin(PHUI::MARGIN_LARGE)->appendChild($dialog);
         $view->appendChild($box);
         return $view->render();
     }
     return '';
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     $id = $request->getURIData('id');
     $action = $request->getURIData('action');
     $capabilities = array(PhabricatorPolicyCapability::CAN_VIEW);
     $process_action = false;
     switch ($action) {
         case 'join':
             $capabilities[] = PhabricatorPolicyCapability::CAN_JOIN;
             $process_action = $request->isFormPost();
             break;
         case 'leave':
             $process_action = $request->isDialogFormPost();
             break;
         default:
             return new Aphront404Response();
     }
     $project = id(new PhabricatorProjectQuery())->setViewer($viewer)->withIDs(array($id))->needMembers(true)->requireCapabilities($capabilities)->executeOne();
     if (!$project) {
         return new Aphront404Response();
     }
     $project_uri = $this->getApplicationURI('profile/' . $project->getID() . '/');
     if ($process_action) {
         $edge_action = null;
         switch ($action) {
             case 'join':
                 $edge_action = '+';
                 break;
             case 'leave':
                 $edge_action = '-';
                 break;
         }
         $type_member = PhabricatorProjectProjectHasMemberEdgeType::EDGECONST;
         $member_spec = array($edge_action => array($viewer->getPHID() => $viewer->getPHID()));
         $xactions = array();
         $xactions[] = id(new PhabricatorProjectTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDGE)->setMetadataValue('edge:type', $type_member)->setNewValue($member_spec);
         $editor = id(new PhabricatorProjectTransactionEditor($project))->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true)->setContinueOnMissingFields(true)->applyTransactions($project, $xactions);
         return id(new AphrontRedirectResponse())->setURI($project_uri);
     }
     $dialog = null;
     switch ($action) {
         case 'leave':
             $dialog = new AphrontDialogView();
             $dialog->setUser($viewer);
             if ($this->userCannotLeave($project)) {
                 $dialog->setTitle(pht('You can not leave this project.'));
                 $body = pht('The membership is locked for this project.');
             } else {
                 $dialog->setTitle(pht('Really leave project?'));
                 $body = pht('Your tremendous contributions to this project will be sorely ' . 'missed. Are you sure you want to leave?');
                 $dialog->addSubmitButton(pht('Leave Project'));
             }
             $dialog->appendParagraph($body);
             $dialog->addCancelButton($project_uri);
             break;
         default:
             return new Aphront404Response();
     }
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $viewer = $request->getUser();
     $parents = $this->loadParentFragments($this->dblob);
     if ($parents === null) {
         return new Aphront404Response();
     }
     $fragment = idx($parents, count($parents) - 1, null);
     if ($this->snapshot !== null) {
         $snapshot = id(new PhragmentSnapshotQuery())->setViewer($viewer)->withPrimaryFragmentPHIDs(array($fragment->getPHID()))->withNames(array($this->snapshot))->executeOne();
         if ($snapshot === null) {
             return new Aphront404Response();
         }
         $cache = id(new PhragmentSnapshotChildQuery())->setViewer($viewer)->needFragmentVersions(true)->withSnapshotPHIDs(array($snapshot->getPHID()))->execute();
         $this->snapshotCache = mpull($cache, 'getFragmentVersion', 'getFragmentPHID');
     }
     $temp = new TempFile();
     $zip = null;
     try {
         $zip = new ZipArchive();
     } catch (Exception $e) {
         $dialog = new AphrontDialogView();
         $dialog->setUser($viewer);
         $inst = pht('This system does not have the ZIP PHP extension installed. This ' . 'is required to download ZIPs from Phragment.');
         $dialog->setTitle(pht('ZIP Extension Not Installed'));
         $dialog->appendParagraph($inst);
         $dialog->addCancelButton('/phragment/browse/' . $this->dblob);
         return id(new AphrontDialogResponse())->setDialog($dialog);
     }
     if (!$zip->open((string) $temp, ZipArchive::CREATE)) {
         throw new Exception('Unable to create ZIP archive!');
     }
     $mappings = $this->getFragmentMappings($fragment, $fragment->getPath());
     $phids = array();
     foreach ($mappings as $path => $file_phid) {
         $phids[] = $file_phid;
     }
     $files = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs($phids)->execute();
     $files = mpull($files, null, 'getPHID');
     foreach ($mappings as $path => $file_phid) {
         if (!isset($files[$file_phid])) {
             // The path is most likely pointing to a deleted fragment, which
             // hence no longer has a file associated with it.
             unset($mappings[$path]);
             continue;
         }
         $mappings[$path] = $files[$file_phid];
     }
     foreach ($mappings as $path => $file) {
         if ($file !== null) {
             $zip->addFromString($path, $file->loadFileData());
         }
     }
     $zip->close();
     $zip_name = $fragment->getName();
     if (substr($zip_name, -4) !== '.zip') {
         $zip_name .= '.zip';
     }
     $data = Filesystem::readFile((string) $temp);
     $file = PhabricatorFile::buildFromFileDataOrHash($data, array('name' => $zip_name, 'ttl' => time() + 60 * 60 * 24));
     $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
     $file->attachToObject($fragment->getPHID());
     unset($unguarded);
     $return = $fragment->getURI();
     if ($request->getExists('return')) {
         $return = $request->getStr('return');
     }
     return id(new AphrontRedirectResponse())->setIsExternal(true)->setURI($file->getDownloadURI($return));
 }
 private function appendStrengthInformation(AphrontDialogView $dialog, PhabricatorPolicyInterface $object, PhabricatorPolicy $policy, $capability)
 {
     $viewer = $this->getViewer();
     $default_policy = PhabricatorPolicyQuery::getDefaultPolicyForObject($viewer, $object, $capability);
     if (!$default_policy) {
         return;
     }
     if ($default_policy->getPHID() == $policy->getPHID()) {
         return;
     }
     if ($default_policy->isStrongerThan($policy)) {
         $info = pht('This object has a less restrictive policy ("%s") than the default ' . 'policy for similar objects (which is "%s").', $policy->getShortName(), $default_policy->getShortName());
     } else {
         if ($policy->isStrongerThan($default_policy)) {
             $info = pht('This object has a more restrictive policy ("%s") than the default ' . 'policy for similar objects (which is "%s").', $policy->getShortName(), $default_policy->getShortName());
         } else {
             $info = pht('This object has a different policy ("%s") than the default policy ' . 'for similar objects (which is "%s").', $policy->getShortName(), $default_policy->getShortName());
         }
     }
     $dialog->appendParagraph($info);
 }