public function processRequest()
 {
     $request = $this->getRequest();
     $viewer = $request->getUser();
     $project = id(new PhabricatorProjectQuery())->setViewer($viewer)->withIDs(array($this->id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
     if (!$project) {
         return new Aphront404Response();
     }
     $edit_uri = $this->getApplicationURI('edit/' . $project->getID() . '/');
     $view_uri = $this->getApplicationURI('view/' . $project->getID() . '/');
     $supported_formats = PhabricatorFile::getTransformableImageFormats();
     $e_file = true;
     $errors = array();
     if ($request->isFormPost()) {
         $phid = $request->getStr('phid');
         $is_default = false;
         if ($phid == PhabricatorPHIDConstants::PHID_VOID) {
             $phid = null;
             $is_default = true;
         } else {
             if ($phid) {
                 $file = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($phid))->executeOne();
             } else {
                 if ($request->getFileExists('picture')) {
                     $file = PhabricatorFile::newFromPHPUpload($_FILES['picture'], array('authorPHID' => $viewer->getPHID(), 'canCDN' => true));
                 } else {
                     $e_file = pht('Required');
                     $errors[] = pht('You must choose a file when uploading a new project picture.');
                 }
             }
         }
         if (!$errors && !$is_default) {
             if (!$file->isTransformableImage()) {
                 $e_file = pht('Not Supported');
                 $errors[] = pht('This server only supports these image formats: %s.', implode(', ', $supported_formats));
             } else {
                 $xformer = new PhabricatorImageTransformer();
                 $xformed = $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
             }
         }
         if (!$errors) {
             if ($is_default) {
                 $new_value = null;
             } else {
                 $new_value = $xformed->getPHID();
             }
             $xactions = array();
             $xactions[] = id(new PhabricatorProjectTransaction())->setTransactionType(PhabricatorProjectTransaction::TYPE_IMAGE)->setNewValue($new_value);
             $editor = id(new PhabricatorProjectTransactionEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnMissingFields(true)->setContinueOnNoEffect(true);
             $editor->applyTransactions($project, $xactions);
             return id(new AphrontRedirectResponse())->setURI($edit_uri);
         }
     }
     $title = pht('Edit Project Picture');
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($project->getName(), $view_uri);
     $crumbs->addTextCrumb(pht('Edit'), $edit_uri);
     $crumbs->addTextCrumb(pht('Picture'));
     $form = id(new PHUIFormLayoutView())->setUser($viewer);
     $default_image = PhabricatorFile::loadBuiltin($viewer, 'project.png');
     $images = array();
     $current = $project->getProfileImagePHID();
     $has_current = false;
     if ($current) {
         $files = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($current))->execute();
         if ($files) {
             $file = head($files);
             if ($file->isTransformableImage()) {
                 $has_current = true;
                 $images[$current] = array('uri' => $file->getBestURI(), 'tip' => pht('Current Picture'));
             }
         }
     }
     $images[PhabricatorPHIDConstants::PHID_VOID] = array('uri' => $default_image->getBestURI(), 'tip' => pht('Default Picture'));
     require_celerity_resource('people-profile-css');
     Javelin::initBehavior('phabricator-tooltips', array());
     $buttons = array();
     foreach ($images as $phid => $spec) {
         $button = javelin_tag('button', array('class' => 'grey profile-image-button', 'sigil' => 'has-tooltip', 'meta' => array('tip' => $spec['tip'], 'size' => 300)), phutil_tag('img', array('height' => 50, 'width' => 50, 'src' => $spec['uri'])));
         $button = array(phutil_tag('input', array('type' => 'hidden', 'name' => 'phid', 'value' => $phid)), $button);
         $button = phabricator_form($viewer, array('class' => 'profile-image-form', 'method' => 'POST'), $button);
         $buttons[] = $button;
     }
     if ($has_current) {
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Current Picture'))->setValue(array_shift($buttons)));
     }
     $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Use Picture'))->setValue($buttons));
     $launch_id = celerity_generate_unique_node_id();
     $input_id = celerity_generate_unique_node_id();
     Javelin::initBehavior('launch-icon-composer', array('launchID' => $launch_id, 'inputID' => $input_id));
     $compose_button = javelin_tag('button', array('class' => 'grey', 'id' => $launch_id, 'sigil' => 'icon-composer'), pht('Choose Icon and Color...'));
     $compose_input = javelin_tag('input', array('type' => 'hidden', 'id' => $input_id, 'name' => 'phid'));
     $compose_form = phabricator_form($viewer, array('class' => 'profile-image-form', 'method' => 'POST'), array($compose_input, $compose_button));
     $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Quick Create'))->setValue($compose_form));
     $upload_form = id(new AphrontFormView())->setUser($viewer)->setEncType('multipart/form-data')->appendChild(id(new AphrontFormFileControl())->setName('picture')->setLabel(pht('Upload Picture'))->setError($e_file)->setCaption(pht('Supported formats: %s', implode(', ', $supported_formats))))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($edit_uri)->setValue(pht('Upload Picture')));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setFormErrors($errors)->setForm($form);
     $upload_box = id(new PHUIObjectBoxView())->setHeaderText(pht('Upload New Picture'))->setForm($upload_form);
     return $this->buildApplicationPage(array($crumbs, $form_box, $upload_box), array('title' => $title));
 }
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
$root = dirname(dirname(dirname(__FILE__)));
require_once $root . '/scripts/__init_script__.php';
echo "Examining users.\n";
foreach (new LiskMigrationIterator(new PhabricatorUser()) as $user) {
    $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $user->getProfileImagePHID());
    if (!$file) {
        echo 'No pic for user ', $user->getUserName(), "\n";
        continue;
    }
    $data = $file->loadFileData();
    $img = imagecreatefromstring($data);
    $sx = imagesx($img);
    $sy = imagesy($img);
    if ($sx != 50 || $sy != 50) {
        echo 'Found one! User ', $user->getUserName(), "\n";
        $xformer = new PhabricatorImageTransformer();
        // Resize OAuth image to a reasonable size
        $small_xformed = $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
        $user->setProfileImagePHID($small_xformed->getPHID());
        $user->save();
        break;
    } else {
        echo '.';
    }
}
echo "\n";
echo "Done.\n";
 public function processRequest(AphrontRequest $request)
 {
     $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'));
         $sex = $request->getStr('sex');
         $sexes = array(PhutilPerson::SEX_MALE, PhutilPerson::SEX_FEMALE);
         if (in_array($sex, $sexes)) {
             $user->setSex($sex);
         } else {
             $user->setSex(null);
         }
         // Checked in runtime.
         $user->setTranslation($request->getStr('translation'));
         $default_image = $request->getExists('default_image');
         if ($default_image) {
             $profile->setProfileImagePHID(null);
             $user->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();
                         // 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($this->getPanelURI('?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();
         }
     }
     $img_src = $user->loadProfileImageURI();
     $profile_uri = PhabricatorEnv::getURI('/p/' . $user->getUsername() . '/');
     $sexes = array(PhutilPerson::SEX_UNKNOWN => 'Unknown', PhutilPerson::SEX_MALE => 'Male', PhutilPerson::SEX_FEMALE => 'Female');
     $translations = array();
     $symbols = id(new PhutilSymbolLoader())->setType('class')->setAncestorClass('PhabricatorTranslation')->setConcreteOnly(true)->selectAndLoadSymbols();
     foreach ($symbols as $symbol) {
         $class = $symbol['name'];
         $translations[$class] = newv($class, array())->getName();
     }
     asort($translations);
     $default = PhabricatorEnv::newObjectFromConfig('translation.provider');
     $translations = array('' => 'Server Default (' . $default->getName() . ')') + $translations;
     $form = new AphrontFormView();
     $form->setUser($request->getUser())->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Title')->setName('title')->setValue($profile->getTitle())->setCaption('Serious business title.'))->appendChild(id(new AphrontFormSelectControl())->setOptions($sexes)->setLabel('Sex')->setName('sex')->setValue($user->getSex()))->appendChild(id(new AphrontFormSelectControl())->setOptions($translations)->setLabel('Translation')->setName('translation')->setValue($user->getTranslation()))->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 AphrontFormImageControl())->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 array($error_view, $panel);
 }
 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));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $project = id(new PhabricatorProject())->load($this->id);
     if (!$project) {
         return new Aphront404Response();
     }
     $profile = $project->loadProfile();
     if (empty($profile)) {
         $profile = new PhabricatorProjectProfile();
     }
     if ($project->getSubprojectPHIDs()) {
         $phids = $project->getSubprojectPHIDs();
         $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
         $subprojects = mpull($handles, 'getFullName', 'getPHID');
     } else {
         $subprojects = array();
     }
     $options = PhabricatorProjectStatus::getStatusMap();
     $affiliations = $project->loadAffiliations();
     $affiliations = mpull($affiliations, null, 'getUserPHID');
     $e_name = true;
     $errors = array();
     $state = null;
     if ($request->isFormPost()) {
         $project->setName($request->getStr('name'));
         $project->setStatus($request->getStr('status'));
         $project->setSubprojectPHIDs($request->getArr('set_subprojects'));
         $profile->setBlurb($request->getStr('blurb'));
         if (!strlen($project->getName())) {
             $e_name = 'Required';
             $errors[] = 'Project name is required.';
         } else {
             $e_name = null;
         }
         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->executeProfileTransform($file, $width = 280, $min_height = 140, $max_height = 420);
                     $profile->setProfileImagePHID($xformed->getPHID());
                 } else {
                     $errors[] = 'Only valid image files (jpg, jpeg, png or gif) ' . 'will be accepted.';
                 }
             }
         }
         $resources = $request->getStr('resources');
         $resources = json_decode($resources, true);
         if (!is_array($resources)) {
             throw new Exception("Project resource information was not correctly encoded in the " . "request.");
         }
         $state = array();
         foreach ($resources as $resource) {
             $user_phid = $resource['phid'];
             if (!$user_phid) {
                 continue;
             }
             if (isset($state[$user_phid])) {
                 // TODO: We should deal with this better -- the user has entered
                 // the same resource more than once.
             }
             $state[$user_phid] = array('phid' => $user_phid, 'status' => $resource['status'], 'role' => $resource['role'], 'owner' => $resource['owner']);
         }
         $all_phids = array_merge(array_keys($state), array_keys($affiliations));
         $all_phids = array_unique($all_phids);
         $delete_affiliations = array();
         $save_affiliations = array();
         foreach ($all_phids as $phid) {
             $old = idx($affiliations, $phid);
             $new = idx($state, $phid);
             if ($old && !$new) {
                 $delete_affiliations[] = $affiliations[$phid];
                 continue;
             }
             if (!$old) {
                 $affil = new PhabricatorProjectAffiliation();
                 $affil->setUserPHID($phid);
             } else {
                 $affil = $old;
             }
             $affil->setRole((string) $new['role']);
             $affil->setStatus((string) $new['status']);
             $affil->setIsOwner((int) $new['owner']);
             $save_affiliations[] = $affil;
         }
         if (!$errors) {
             $project->save();
             $profile->setProjectPHID($project->getPHID());
             $profile->save();
             foreach ($delete_affiliations as $affil) {
                 $affil->delete();
             }
             foreach ($save_affiliations as $save) {
                 $save->setProjectPHID($project->getPHID());
                 $save->save();
             }
             return id(new AphrontRedirectResponse())->setURI('/project/view/' . $project->getID() . '/');
         } else {
             $phids = array_keys($state);
             $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
             foreach ($state as $phid => $info) {
                 $state[$phid]['name'] = $handles[$phid]->getFullName();
             }
         }
     } else {
         $phids = mpull($affiliations, 'getUserPHID');
         $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
         $state = array();
         foreach ($affiliations as $affil) {
             $user_phid = $affil->getUserPHID();
             $state[] = array('phid' => $user_phid, 'name' => $handles[$user_phid]->getFullName(), 'status' => $affil->getStatus(), 'role' => $affil->getRole(), 'owner' => $affil->getIsOwner());
         }
     }
     $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() . '/';
     require_celerity_resource('project-edit-css');
     $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(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/projects/')->setLabel('Subprojects')->setName('set_subprojects')->setValue($subprojects))->appendChild(id(new AphrontFormFileControl())->setLabel('Change Image')->setName('image'))->appendChild('<h1>Resources</h1>' . '<input type="hidden" name="resources" id="resources" />' . '<div class="aphront-form-inset">' . '<div style="float: right;">' . javelin_render_tag('a', array('href' => '#', 'class' => 'button green', 'sigil' => 'add-resource', 'mustcapture' => true), 'Add New Resource') . '</div>' . '<p></p>' . '<div style="clear: both;"></div>' . javelin_render_tag('table', array('sigil' => 'resources', 'class' => 'project-resource-table'), '') . '</div>')->appendChild(id(new AphrontFormSubmitControl())->addCancelButton('/project/view/' . $project->getID() . '/')->setValue('Save'));
     $template = new AphrontTokenizerTemplateView();
     $template = $template->render();
     Javelin::initBehavior('projects-resource-editor', array('root' => 'project-edit-form', 'tokenizerTemplate' => $template, 'tokenizerSource' => '/typeahead/common/users/', 'input' => 'resources', 'state' => array_values($state)));
     $panel = new AphrontPanelView();
     $panel->setHeader($header_name);
     $panel->setWidth(AphrontPanelView::WIDTH_WIDE);
     $panel->appendChild($form);
     return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => $title));
 }
 private function refreshProfileImage(PhabricatorUserOAuthInfo $oauth_info)
 {
     $user = $this->getRequest()->getUser();
     $provider = $this->provider;
     $error = false;
     $userinfo_uri = new PhutilURI($provider->getUserInfoURI());
     $token = $oauth_info->getToken();
     try {
         $userinfo_uri->setQueryParam('access_token', $token);
         $user_data = HTTPSFuture::loadContent($userinfo_uri);
         $provider->setUserData($user_data);
         $provider->setAccessToken($token);
         $image = $provider->retrieveUserProfileImage();
         if ($image) {
             $file = PhabricatorFile::newFromFileData($image, array('name' => $provider->getProviderKey() . '-profile.jpg', 'authorPHID' => $user->getPHID()));
             $xformer = new PhabricatorImageTransformer();
             // Resize OAuth image to a reasonable size
             $small_xformed = $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
             $user->setProfileImagePHID($small_xformed->getPHID());
             $user->save();
         } else {
             $error = 'Unable to retrieve image.';
         }
     } catch (Exception $e) {
         if ($e instanceof PhabricatorOAuthProviderException) {
             $error = sprintf('Unable to retrieve image from %s', $provider->getProviderName());
         } else {
             $error = 'Unable to save image.';
         }
     }
     $notice = new AphrontErrorView();
     if ($error) {
         $notice->setTitle('Error Refreshing Profile Picture')->setErrors(array($error));
     } else {
         $notice->setSeverity(AphrontErrorView::SEVERITY_NOTICE)->setTitle('Successfully Refreshed Profile Picture');
     }
     return $notice;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $viewer = $request->getUser();
     $user = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withIDs(array($this->id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
     if (!$user) {
         return new Aphront404Response();
     }
     $profile_uri = '/p/' . $user->getUsername() . '/';
     $supported_formats = PhabricatorFile::getTransformableImageFormats();
     $e_file = true;
     $errors = array();
     if ($request->isFormPost()) {
         $phid = $request->getStr('phid');
         $is_default = false;
         if ($phid == PhabricatorPHIDConstants::PHID_VOID) {
             $phid = null;
             $is_default = true;
         } else {
             if ($phid) {
                 $file = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($phid))->executeOne();
             } else {
                 if ($request->getFileExists('picture')) {
                     $file = PhabricatorFile::newFromPHPUpload($_FILES['picture'], array('authorPHID' => $viewer->getPHID(), 'canCDN' => true));
                 } else {
                     $e_file = pht('Required');
                     $errors[] = pht('You must choose a file when uploading a new profile picture.');
                 }
             }
         }
         if (!$errors && !$is_default) {
             if (!$file->isTransformableImage()) {
                 $e_file = pht('Not Supported');
                 $errors[] = pht('This server only supports these image formats: %s.', implode(', ', $supported_formats));
             } else {
                 $xformer = new PhabricatorImageTransformer();
                 $xformed = $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
             }
         }
         if (!$errors) {
             if ($is_default) {
                 $user->setProfileImagePHID(null);
             } else {
                 $user->setProfileImagePHID($xformed->getPHID());
                 $xformed->attachToObject($user->getPHID());
             }
             $user->save();
             return id(new AphrontRedirectResponse())->setURI($profile_uri);
         }
     }
     $title = pht('Edit Profile Picture');
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($user->getUsername(), $profile_uri);
     $crumbs->addTextCrumb($title);
     $form = id(new PHUIFormLayoutView())->setUser($viewer);
     $default_image = PhabricatorFile::loadBuiltin($viewer, 'profile.png');
     $images = array();
     $current = $user->getProfileImagePHID();
     $has_current = false;
     if ($current) {
         $files = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($current))->execute();
         if ($files) {
             $file = head($files);
             if ($file->isTransformableImage()) {
                 $has_current = true;
                 $images[$current] = array('uri' => $file->getBestURI(), 'tip' => pht('Current Picture'));
             }
         }
     }
     // Try to add external account images for any associated external accounts.
     $accounts = id(new PhabricatorExternalAccountQuery())->setViewer($viewer)->withUserPHIDs(array($user->getPHID()))->needImages(true)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->execute();
     foreach ($accounts as $account) {
         $file = $account->getProfileImageFile();
         if ($account->getProfileImagePHID() != $file->getPHID()) {
             // This is a default image, just skip it.
             continue;
         }
         $provider = PhabricatorAuthProvider::getEnabledProviderByKey($account->getProviderKey());
         if ($provider) {
             $tip = pht('Picture From %s', $provider->getProviderName());
         } else {
             $tip = pht('Picture From External Account');
         }
         if ($file->isTransformableImage()) {
             $images[$file->getPHID()] = array('uri' => $file->getBestURI(), 'tip' => $tip);
         }
     }
     // Try to add Gravatar images for any email addresses associated with the
     // account.
     if (PhabricatorEnv::getEnvConfig('security.allow-outbound-http')) {
         $emails = id(new PhabricatorUserEmail())->loadAllWhere('userPHID = %s ORDER BY address', $user->getPHID());
         $futures = array();
         foreach ($emails as $email_object) {
             $email = $email_object->getAddress();
             $hash = md5(strtolower(trim($email)));
             $uri = id(new PhutilURI("https://secure.gravatar.com/avatar/{$hash}"))->setQueryParams(array('size' => 200, 'default' => '404', 'rating' => 'x'));
             $futures[$email] = new HTTPSFuture($uri);
         }
         $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
         foreach (Futures($futures) as $email => $future) {
             try {
                 list($body) = $future->resolvex();
                 $file = PhabricatorFile::newFromFileData($body, array('name' => 'profile-gravatar', 'ttl' => 60 * 60 * 4));
                 if ($file->isTransformableImage()) {
                     $images[$file->getPHID()] = array('uri' => $file->getBestURI(), 'tip' => pht('Gravatar for %s', $email));
                 }
             } catch (Exception $ex) {
                 // Just continue.
             }
         }
         unset($unguarded);
     }
     $images[PhabricatorPHIDConstants::PHID_VOID] = array('uri' => $default_image->getBestURI(), 'tip' => pht('Default Picture'));
     require_celerity_resource('people-profile-css');
     Javelin::initBehavior('phabricator-tooltips', array());
     $buttons = array();
     foreach ($images as $phid => $spec) {
         $button = javelin_tag('button', array('class' => 'grey profile-image-button', 'sigil' => 'has-tooltip', 'meta' => array('tip' => $spec['tip'], 'size' => 300)), phutil_tag('img', array('height' => 50, 'width' => 50, 'src' => $spec['uri'])));
         $button = array(phutil_tag('input', array('type' => 'hidden', 'name' => 'phid', 'value' => $phid)), $button);
         $button = phabricator_form($viewer, array('class' => 'profile-image-form', 'method' => 'POST'), $button);
         $buttons[] = $button;
     }
     if ($has_current) {
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Current Picture'))->setValue(array_shift($buttons)));
     }
     $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Use Picture'))->setValue($buttons));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setFormErrors($errors)->setForm($form);
     $upload_form = id(new AphrontFormView())->setUser($viewer)->setEncType('multipart/form-data')->appendChild(id(new AphrontFormFileControl())->setName('picture')->setLabel(pht('Upload Picture'))->setError($e_file)->setCaption(pht('Supported formats: %s', implode(', ', $supported_formats))))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($profile_uri)->setValue(pht('Upload Picture')));
     $upload_box = id(new PHUIObjectBoxView())->setHeaderText(pht('Upload New Picture'))->setForm($upload_form);
     return $this->buildApplicationPage(array($crumbs, $form_box, $upload_box), array('title' => $title));
 }
 private function loadProfilePicture(PhabricatorExternalAccount $account)
 {
     $phid = $account->getProfileImagePHID();
     if (!$phid) {
         return null;
     }
     // NOTE: Use of omnipotent user is okay here because the registering user
     // can not control the field value, and we can't use their user object to
     // do meaningful policy checks anyway since they have not registered yet.
     // Reaching this means the user holds the account secret key and the
     // registration secret key, and thus has permission to view the image.
     $file = id(new PhabricatorFileQuery())->setViewer(PhabricatorUser::getOmnipotentUser())->withPHIDs(array($phid))->executeOne();
     if (!$file) {
         return null;
     }
     try {
         $xformer = new PhabricatorImageTransformer();
         return $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
     } catch (Exception $ex) {
         phlog($ex);
         return null;
     }
 }
 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());
     $new_email = $provider->retrieveUserEmail();
     if ($new_email) {
         // If the user's OAuth 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 Google OAuth and their Google 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 OAuth
         // account to login, they just need to associate their account with an
         // allowed address.
         //
         // If the OAuth 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) {
             $email_ok = PhabricatorUserEmail::isAllowedAddress($new_email);
             if (!$email_ok) {
                 $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) {
             $image = $provider->retrieveUserProfileImage();
             if ($image) {
                 $file = PhabricatorFile::newFromFileData($image, array('name' => $provider->getProviderKey() . '-profile.jpg', 'authorPHID' => $user->getPHID()));
                 $xformer = new PhabricatorImageTransformer();
                 // Resize OAuth image to a reasonable size
                 $small_xformed = $xformer->executeProfileTransform($file, $width = 50, $min_height = 50, $max_height = 50);
                 $user->setProfileImagePHID($small_xformed->getPHID());
             }
             try {
                 // NOTE: We don't verify OAuth email addresses by default because
                 // OAuth 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);
                 $oauth_info->setUserID($user->getID());
                 $oauth_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($provider->getRedirectURI());
     $action_path = $action_uri->getPath();
     $form = new AphrontFormView();
     $form->addHiddenInput('confirm_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 ($show_email_input) {
         $form->appendChild(id(new AphrontFormTextControl())->setLabel('Email')->setName('email')->setValue($request->getStr('email'))->setCaption(PhabricatorUserEmail::describeAllowedAddresses())->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'));
 }