public static function generateMacro($viewer, $macro_name, $upper_text, $lower_text)
 {
     $macro = id(new PhabricatorMacroQuery())->setViewer($viewer)->withNames(array($macro_name))->needFiles(true)->executeOne();
     if (!$macro) {
         return false;
     }
     $file = $macro->getFile();
     $upper_text = strtoupper($upper_text);
     $lower_text = strtoupper($lower_text);
     $mixed_text = md5($upper_text) . ':' . md5($lower_text);
     $hash = 'meme' . hash('sha256', $mixed_text);
     $xform = id(new PhabricatorTransformedFile())->loadOneWhere('originalphid=%s and transform=%s', $file->getPHID(), $hash);
     if ($xform) {
         $memefile = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs(array($xform->getTransformedPHID()))->executeOne();
         if ($memefile) {
             return $memefile->getBestURI();
         }
     }
     $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
     $transformers = new PhabricatorImageTransformer();
     $newfile = $transformers->executeMemeTransform($file, $upper_text, $lower_text);
     $xfile = new PhabricatorTransformedFile();
     $xfile->setOriginalPHID($file->getPHID());
     $xfile->setTransformedPHID($newfile->getPHID());
     $xfile->setTransform($hash);
     $xfile->save();
     return $newfile->getBestURI();
 }
 private function composeImage($color, $icon_data)
 {
     $icon_img = imagecreatefromstring($icon_data);
     $map = CelerityResourceTransformer::getCSSVariableMap();
     $color_string = idx($map, $color, '#ff00ff');
     $color_const = hexdec(trim($color_string, '#'));
     $canvas = imagecreatetruecolor(50, 50);
     imagefill($canvas, 0, 0, $color_const);
     imagecopy($canvas, $icon_img, 0, 0, 0, 0, 50, 50);
     return PhabricatorImageTransformer::saveImageDataInAnyFormat($canvas, 'image/png');
 }
Пример #3
0
 protected function renderInput()
 {
     $file = $this->getValue();
     if ($file === null) {
         return phutil_tag('img', array('src' => PhabricatorUser::getDefaultProfileImageURI()), '');
     }
     $c_id = celerity_generate_unique_node_id();
     $metadata = $file->getMetadata();
     $scale = PhabricatorImageTransformer::getScaleForCrop($file, $this->getWidth(), $this->getHeight());
     Javelin::initBehavior('aphront-crop', array('cropBoxID' => $c_id, 'width' => $this->getWidth(), 'height' => $this->getHeight(), 'scale' => $scale, 'imageH' => $metadata[PhabricatorFile::METADATA_IMAGE_HEIGHT], 'imageW' => $metadata[PhabricatorFile::METADATA_IMAGE_WIDTH]));
     return javelin_tag('div', array('id' => $c_id, 'sigil' => 'crop-box', 'mustcapture' => true, 'class' => 'crop-box'), array(javelin_tag('img', array('src' => $file->getBestURI(), 'class' => 'crop-image', 'sigil' => 'crop-image'), ''), javelin_tag('input', array('type' => 'hidden', 'name' => 'image_x', 'sigil' => 'crop-x'), ''), javelin_tag('input', array('type' => 'hidden', 'name' => 'image_y', 'sigil' => 'crop-y'), '')));
 }
 protected function applyCropAndScale($dst_w, $dst_h, $src_x, $src_y, $src_w, $src_h, $use_w, $use_h, $scale_up)
 {
     // Figure out the effective destination width, height, and offsets.
     $cpy_w = min($dst_w, $use_w);
     $cpy_h = min($dst_h, $use_h);
     // If we aren't scaling up, and are copying a very small source image,
     // we're just going to center it in the destination image.
     if (!$scale_up) {
         $cpy_w = min($cpy_w, $src_w);
         $cpy_h = min($cpy_h, $src_h);
     }
     $off_x = ($dst_w - $cpy_w) / 2;
     $off_y = ($dst_h - $cpy_h) / 2;
     if ($this->shouldUseImagemagick()) {
         $argv = array();
         $argv[] = '-coalesce';
         $argv[] = '-shave';
         $argv[] = $src_x . 'x' . $src_y;
         $argv[] = '-resize';
         if ($scale_up) {
             $argv[] = $dst_w . 'x' . $dst_h;
         } else {
             $argv[] = $dst_w . 'x' . $dst_h . '>';
         }
         $argv[] = '-bordercolor';
         $argv[] = 'rgba(255, 255, 255, 0)';
         $argv[] = '-border';
         $argv[] = $off_x . 'x' . $off_y;
         return $this->applyImagemagick($argv);
     }
     $src = $this->getImage();
     $dst = $this->newEmptyImage($dst_w, $dst_h);
     $trap = new PhutilErrorTrap();
     $ok = @imagecopyresampled($dst, $src, $off_x, $off_y, $src_x, $src_y, $cpy_w, $cpy_h, $src_w, $src_h);
     $errors = $trap->getErrorsAsString();
     $trap->destroy();
     if ($ok === false) {
         throw new Exception(pht('Failed to imagecopyresampled() image: %s', $errors));
     }
     $data = PhabricatorImageTransformer::saveImageDataInAnyFormat($dst, $this->file->getMimeType());
     return $this->newFileFromData($data);
 }
 private function renderThumbnail(PholioImage $image)
 {
     $thumbfile = $image->getFile();
     if ($image->getFile()->isViewableImage()) {
         $dimensions = PhabricatorImageTransformer::getPreviewDimensions($thumbfile, 100);
     } else {
         // If this is a PDF or a text file or something, we'll end up using a
         // generic thumbnail which is always sized correctly.
         $dimensions = array('sdx' => 100, 'sdy' => 100);
     }
     $tag = phutil_tag('img', array('width' => $dimensions['sdx'], 'height' => $dimensions['sdy'], 'src' => $thumbfile->getPreview100URI(), 'class' => 'pholio-mock-thumb-grid-image', 'style' => 'top: ' . floor((100 - $dimensions['sdy']) / 2) . 'px'));
     $classes = array('pholio-mock-thumb-grid-item');
     if ($image->getIsObsolete()) {
         $classes[] = 'pholio-mock-thumb-grid-item-obsolete';
     }
     $inline_count = null;
     if ($image->getInlineComments()) {
         $inline_count[] = phutil_tag('span', array('class' => 'pholio-mock-thumb-grid-comment-count'), pht('%s', new PhutilNumber(count($image->getInlineComments()))));
     }
     return javelin_tag('a', array('sigil' => 'mock-thumbnail', 'class' => implode(' ', $classes), 'href' => '#', 'meta' => array('imageID' => $image->getID())), array($tag, $inline_count));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $project = id(new PhabricatorProjectQuery())->setViewer($user)->withIDs(array($this->id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
     if (!$project) {
         return new Aphront404Response();
     }
     $profile = $project->loadProfile();
     if (empty($profile)) {
         $profile = new PhabricatorProjectProfile();
     }
     $img_src = $profile->loadProfileImageURI();
     $options = PhabricatorProjectStatus::getStatusMap();
     $supported_formats = PhabricatorFile::getTransformableImageFormats();
     $e_name = true;
     $e_image = null;
     $errors = array();
     if ($request->isFormPost()) {
         try {
             $xactions = array();
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_NAME);
             $xaction->setNewValue($request->getStr('name'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_STATUS);
             $xaction->setNewValue($request->getStr('status'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_CAN_VIEW);
             $xaction->setNewValue($request->getStr('can_view'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_CAN_EDIT);
             $xaction->setNewValue($request->getStr('can_edit'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_CAN_JOIN);
             $xaction->setNewValue($request->getStr('can_join'));
             $xactions[] = $xaction;
             $editor = new PhabricatorProjectEditor($project);
             $editor->setUser($user);
             $editor->applyTransactions($xactions);
         } catch (PhabricatorProjectNameCollisionException $ex) {
             $e_name = 'Not Unique';
             $errors[] = $ex->getMessage();
         }
         $profile->setBlurb($request->getStr('blurb'));
         if (!strlen($project->getName())) {
             $e_name = 'Required';
             $errors[] = 'Project name is required.';
         } else {
             $e_name = null;
         }
         $default_image = $request->getExists('default_image');
         if ($default_image) {
             $profile->setProfileImagePHID(null);
         } else {
             if (!empty($_FILES['image'])) {
                 $err = idx($_FILES['image'], 'error');
                 if ($err != UPLOAD_ERR_NO_FILE) {
                     $file = PhabricatorFile::newFromPHPUpload($_FILES['image'], array('authorPHID' => $user->getPHID()));
                     $okay = $file->isTransformableImage();
                     if ($okay) {
                         $xformer = new PhabricatorImageTransformer();
                         $xformed = $xformer->executeThumbTransform($file, $x = 50, $y = 50);
                         $profile->setProfileImagePHID($xformed->getPHID());
                     } else {
                         $e_image = 'Not Supported';
                         $errors[] = 'This server only supports these image formats: ' . implode(', ', $supported_formats) . '.';
                     }
                 }
             }
         }
         if (!$errors) {
             $project->save();
             $profile->setProjectPHID($project->getPHID());
             $profile->save();
             return id(new AphrontRedirectResponse())->setURI('/project/view/' . $project->getID() . '/');
         }
     }
     $error_view = null;
     if ($errors) {
         $error_view = new AphrontErrorView();
         $error_view->setTitle('Form Errors');
         $error_view->setErrors($errors);
     }
     $header_name = 'Edit Project';
     $title = 'Edit Project';
     $action = '/project/edit/' . $project->getID() . '/';
     $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($project)->execute();
     $form = new AphrontFormView();
     $form->setID('project-edit-form')->setUser($user)->setAction($action)->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($project->getName())->setError($e_name))->appendChild(id(new AphrontFormSelectControl())->setLabel('Project Status')->setName('status')->setOptions($options)->setValue($project->getStatus()))->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Blurb')->setName('blurb')->setValue($profile->getBlurb()))->appendChild('<p class="aphront-form-instructions">NOTE: Policy settings are not ' . 'yet fully implemented. Some interfaces still ignore these settings, ' . 'particularly "Visible To".</p>')->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setName('can_view')->setCaption('Members can always view a project.')->setPolicyObject($project)->setPolicies($policies)->setCapability(PhabricatorPolicyCapability::CAN_VIEW))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setName('can_edit')->setPolicyObject($project)->setPolicies($policies)->setCapability(PhabricatorPolicyCapability::CAN_EDIT))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setName('can_join')->setCaption('Users who can edit a project can always join a project.')->setPolicyObject($project)->setPolicies($policies)->setCapability(PhabricatorPolicyCapability::CAN_JOIN))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Profile Image')->setValue(phutil_render_tag('img', array('src' => $img_src))))->appendChild(id(new AphrontFormImageControl())->setLabel('Change Image')->setName('image')->setError($e_image)->setCaption('Supported formats: ' . implode(', ', $supported_formats)))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton('/project/view/' . $project->getID() . '/')->setValue('Save'));
     $panel = new AphrontPanelView();
     $panel->setHeader($header_name);
     $panel->setWidth(AphrontPanelView::WIDTH_FORM);
     $panel->appendChild($form);
     $nav = $this->buildLocalNavigation($project);
     $nav->selectFilter('edit');
     $nav->appendChild(array($error_view, $panel));
     return $this->buildStandardPageResponse($nav, array('title' => $title));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $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 handleRequest(AphrontRequest $request)
 {
     $user = $request->getUser();
     $conpherence_id = $request->getURIData('id');
     if (!$conpherence_id) {
         return new Aphront404Response();
     }
     $need_participants = false;
     $needed_capabilities = array(PhabricatorPolicyCapability::CAN_VIEW);
     $action = $request->getStr('action', ConpherenceUpdateActions::METADATA);
     switch ($action) {
         case ConpherenceUpdateActions::REMOVE_PERSON:
             $person_phid = $request->getStr('remove_person');
             if ($person_phid != $user->getPHID()) {
                 $needed_capabilities[] = PhabricatorPolicyCapability::CAN_EDIT;
             }
             break;
         case ConpherenceUpdateActions::ADD_PERSON:
         case ConpherenceUpdateActions::METADATA:
             $needed_capabilities[] = PhabricatorPolicyCapability::CAN_EDIT;
             break;
         case ConpherenceUpdateActions::JOIN_ROOM:
             $needed_capabilities[] = PhabricatorPolicyCapability::CAN_JOIN;
             break;
         case ConpherenceUpdateActions::NOTIFICATIONS:
             $need_participants = true;
             break;
         case ConpherenceUpdateActions::LOAD:
             break;
     }
     $conpherence = id(new ConpherenceThreadQuery())->setViewer($user)->withIDs(array($conpherence_id))->needFilePHIDs(true)->needOrigPics(true)->needCropPics(true)->needParticipants($need_participants)->requireCapabilities($needed_capabilities)->executeOne();
     $latest_transaction_id = null;
     $response_mode = $request->isAjax() ? 'ajax' : 'redirect';
     $error_view = null;
     $e_file = array();
     $errors = array();
     $delete_draft = false;
     $xactions = array();
     if ($request->isFormPost() || $action == ConpherenceUpdateActions::LOAD) {
         $editor = id(new ConpherenceEditor())->setContinueOnNoEffect($request->isContinueRequest())->setContentSourceFromRequest($request)->setActor($user);
         switch ($action) {
             case ConpherenceUpdateActions::DRAFT:
                 $draft = PhabricatorDraft::newFromUserAndKey($user, $conpherence->getPHID());
                 $draft->setDraft($request->getStr('text'));
                 $draft->replaceOrDelete();
                 return new AphrontAjaxResponse();
             case ConpherenceUpdateActions::JOIN_ROOM:
                 $xactions[] = id(new ConpherenceTransaction())->setTransactionType(ConpherenceTransaction::TYPE_PARTICIPANTS)->setNewValue(array('+' => array($user->getPHID())));
                 $delete_draft = true;
                 $message = $request->getStr('text');
                 if ($message) {
                     $message_xactions = $editor->generateTransactionsFromText($user, $conpherence, $message);
                     $xactions = array_merge($xactions, $message_xactions);
                 }
                 // for now, just redirect back to the conpherence so everything
                 // will work okay...!
                 $response_mode = 'redirect';
                 break;
             case ConpherenceUpdateActions::MESSAGE:
                 $message = $request->getStr('text');
                 if (strlen($message)) {
                     $xactions = $editor->generateTransactionsFromText($user, $conpherence, $message);
                     $delete_draft = true;
                 } else {
                     $action = ConpherenceUpdateActions::LOAD;
                     $updated = false;
                     $response_mode = 'ajax';
                 }
                 break;
             case ConpherenceUpdateActions::ADD_PERSON:
                 $person_phids = $request->getArr('add_person');
                 if (!empty($person_phids)) {
                     $xactions[] = id(new ConpherenceTransaction())->setTransactionType(ConpherenceTransaction::TYPE_PARTICIPANTS)->setNewValue(array('+' => $person_phids));
                 }
                 break;
             case ConpherenceUpdateActions::REMOVE_PERSON:
                 if (!$request->isContinueRequest()) {
                     // do nothing; we'll display a confirmation dialogue instead
                     break;
                 }
                 $person_phid = $request->getStr('remove_person');
                 if ($person_phid && $person_phid == $user->getPHID()) {
                     $xactions[] = id(new ConpherenceTransaction())->setTransactionType(ConpherenceTransaction::TYPE_PARTICIPANTS)->setNewValue(array('-' => array($person_phid)));
                     $response_mode = 'go-home';
                 }
                 break;
             case ConpherenceUpdateActions::NOTIFICATIONS:
                 $notifications = $request->getStr('notifications');
                 $participant = $conpherence->getParticipantIfExists($user->getPHID());
                 if (!$participant) {
                     return id(new Aphront404Response());
                 }
                 $participant->setSettings(array('notifications' => $notifications));
                 $participant->save();
                 $result = pht('Updated notification settings to "%s".', ConpherenceSettings::getHumanString($notifications));
                 return id(new AphrontAjaxResponse())->setContent($result);
                 break;
             case ConpherenceUpdateActions::METADATA:
                 $top = $request->getInt('image_y');
                 $left = $request->getInt('image_x');
                 $file_id = $request->getInt('file_id');
                 $title = $request->getStr('title');
                 if ($file_id) {
                     $orig_file = id(new PhabricatorFileQuery())->setViewer($user)->withIDs(array($file_id))->executeOne();
                     $xactions[] = id(new ConpherenceTransaction())->setTransactionType(ConpherenceTransaction::TYPE_PICTURE)->setNewValue($orig_file);
                     $okay = $orig_file->isTransformableImage();
                     if ($okay) {
                         $xformer = new PhabricatorImageTransformer();
                         $crop_file = $xformer->executeConpherenceTransform($orig_file, 0, 0, ConpherenceImageData::CROP_WIDTH, ConpherenceImageData::CROP_HEIGHT);
                         $xactions[] = id(new ConpherenceTransaction())->setTransactionType(ConpherenceTransaction::TYPE_PICTURE_CROP)->setNewValue($crop_file->getPHID());
                     }
                     $response_mode = 'redirect';
                 }
                 // all other metadata updates are continue requests
                 if (!$request->isContinueRequest()) {
                     break;
                 }
                 if ($top !== null || $left !== null) {
                     $file = $conpherence->getImage(ConpherenceImageData::SIZE_ORIG);
                     $xformer = new PhabricatorImageTransformer();
                     $xformed = $xformer->executeConpherenceTransform($file, $top, $left, ConpherenceImageData::CROP_WIDTH, ConpherenceImageData::CROP_HEIGHT);
                     $image_phid = $xformed->getPHID();
                     $xactions[] = id(new ConpherenceTransaction())->setTransactionType(ConpherenceTransaction::TYPE_PICTURE_CROP)->setNewValue($image_phid);
                 }
                 $title = $request->getStr('title');
                 $xactions[] = id(new ConpherenceTransaction())->setTransactionType(ConpherenceTransaction::TYPE_TITLE)->setNewValue($title);
                 $xactions[] = id(new ConpherenceTransaction())->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)->setNewValue($request->getStr('viewPolicy'));
                 $xactions[] = id(new ConpherenceTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)->setNewValue($request->getStr('editPolicy'));
                 $xactions[] = id(new ConpherenceTransaction())->setTransactionType(PhabricatorTransactions::TYPE_JOIN_POLICY)->setNewValue($request->getStr('joinPolicy'));
                 if (!$request->getExists('force_ajax')) {
                     $response_mode = 'redirect';
                 }
                 break;
             case ConpherenceUpdateActions::LOAD:
                 $updated = false;
                 $response_mode = 'ajax';
                 break;
             default:
                 throw new Exception(pht('Unknown action: %s', $action));
                 break;
         }
         if ($xactions) {
             try {
                 $xactions = $editor->applyTransactions($conpherence, $xactions);
                 if ($delete_draft) {
                     $draft = PhabricatorDraft::newFromUserAndKey($user, $conpherence->getPHID());
                     $draft->delete();
                 }
             } catch (PhabricatorApplicationTransactionNoEffectException $ex) {
                 return id(new PhabricatorApplicationTransactionNoEffectResponse())->setCancelURI($this->getApplicationURI($conpherence_id . '/'))->setException($ex);
             }
             // xactions had no effect...!
             if (empty($xactions)) {
                 $errors[] = pht('That was a non-update. Try cancel.');
             }
         }
         if ($xactions || $action == ConpherenceUpdateActions::LOAD) {
             switch ($response_mode) {
                 case 'ajax':
                     $latest_transaction_id = $request->getInt('latest_transaction_id');
                     $content = $this->loadAndRenderUpdates($action, $conpherence_id, $latest_transaction_id);
                     return id(new AphrontAjaxResponse())->setContent($content);
                     break;
                 case 'go-home':
                     $content = array('href' => $this->getApplicationURI());
                     return id(new AphrontAjaxResponse())->setContent($content);
                     break;
                 case 'redirect':
                 default:
                     return id(new AphrontRedirectResponse())->setURI('/' . $conpherence->getMonogram());
                     break;
             }
         }
     }
     if ($errors) {
         $error_view = id(new PHUIInfoView())->setErrors($errors);
     }
     switch ($action) {
         case ConpherenceUpdateActions::ADD_PERSON:
             $dialogue = $this->renderAddPersonDialogue($conpherence);
             break;
         case ConpherenceUpdateActions::REMOVE_PERSON:
             $dialogue = $this->renderRemovePersonDialogue($conpherence);
             break;
         case ConpherenceUpdateActions::METADATA:
         default:
             $dialogue = $this->renderMetadataDialogue($conpherence, $error_view);
             break;
     }
     return id(new AphrontDialogResponse())->setDialog($dialogue->setUser($user)->setWidth(AphrontDialogView::WIDTH_FORM)->setSubmitURI($this->getApplicationURI('update/' . $conpherence_id . '/'))->addSubmitButton()->addCancelButton($this->getApplicationURI($conpherence->getID() . '/')));
 }
 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;
     }
 }
 private function executeThumbTransform(PhabricatorFile $file, $x, $y)
 {
     $xformer = new PhabricatorImageTransformer();
     return $xformer->executeThumbTransform($file, $x, $y);
 }
 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();
     $user = $request->getUser();
     $project = id(new PhabricatorProject())->load($this->id);
     if (!$project) {
         return new Aphront404Response();
     }
     $profile = $project->loadProfile();
     if (empty($profile)) {
         $profile = new PhabricatorProjectProfile();
     }
     $img_src = $profile->loadProfileImageURI();
     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');
     $supported_formats = PhabricatorFile::getTransformableImageFormats();
     $e_name = true;
     $e_image = null;
     $errors = array();
     $state = null;
     if ($request->isFormPost()) {
         try {
             $xactions = array();
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_NAME);
             $xaction->setNewValue($request->getStr('name'));
             $xactions[] = $xaction;
             $xaction = new PhabricatorProjectTransaction();
             $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_STATUS);
             $xaction->setNewValue($request->getStr('status'));
             $xactions[] = $xaction;
             $editor = new PhabricatorProjectEditor($project);
             $editor->setUser($user);
             $editor->applyTransactions($xactions);
         } catch (PhabricatorProjectNameCollisionException $ex) {
             $e_name = 'Not Unique';
             $errors[] = $ex->getMessage();
         }
         $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;
         }
         $default_image = $request->getExists('default_image');
         if ($default_image) {
             $profile->setProfileImagePHID(null);
         } else {
             if (!empty($_FILES['image'])) {
                 $err = idx($_FILES['image'], 'error');
                 if ($err != UPLOAD_ERR_NO_FILE) {
                     $file = PhabricatorFile::newFromPHPUpload($_FILES['image'], array('authorPHID' => $user->getPHID()));
                     $okay = $file->isTransformableImage();
                     if ($okay) {
                         $xformer = new PhabricatorImageTransformer();
                         $xformed = $xformer->executeThumbTransform($file, $x = 50, $y = 50);
                         $profile->setProfileImagePHID($xformed->getPHID());
                     } else {
                         $e_image = 'Not Supported';
                         $errors[] = 'This server only supports these image formats: ' . implode(', ', $supported_formats) . '.';
                     }
                 }
             }
         }
         $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, '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->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(), '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 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 AphrontFormInsetView())->setTitle('Resources')->setRightButton(javelin_render_tag('a', array('href' => '#', 'class' => 'button green', 'sigil' => 'add-resource', 'mustcapture' => true), 'Add New Resource'))->appendChild(phutil_render_tag('input', array('type' => 'hidden', 'name' => 'resources', 'id' => 'resources')))->setContent(javelin_render_tag('table', array('sigil' => 'resources', 'class' => 'project-resource-table'), '')))->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 composeImage($color, $icon)
 {
     $color_map = self::getAllColors();
     $color = idx($color_map, $color);
     if (!$color) {
         $fallback = 'backdrop';
         $color = idx($color_map, $fallback);
         if (!$color) {
             throw new Exception(pht('Fallback compose color ("%s") does not exist!', $fallback));
         }
     }
     $color_hex = idx($color, 'color');
     $color_const = hexdec(trim($color_hex, '#'));
     $icon_map = self::getAllIcons();
     $icon = idx($icon_map, $icon);
     if (!$icon) {
         $fallback = 'fa-umbrella';
         $icon = idx($icon_map, $fallback);
         if (!$icon) {
             throw new Exception(pht('Fallback compose icon ("%s") does not exist!', $fallback));
         }
     }
     $path = idx($icon, 'path');
     $data = Filesystem::readFile($path);
     $icon_img = imagecreatefromstring($data);
     $canvas = imagecreatetruecolor(200, 200);
     imagefill($canvas, 0, 0, $color_const);
     imagecopy($canvas, $icon_img, 0, 0, 0, 0, 200, 200);
     return PhabricatorImageTransformer::saveImageDataInAnyFormat($canvas, 'image/png');
 }
 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 renderImageFile(PhabricatorFile $file, PhabricatorObjectHandle $handle, array $options)
 {
     require_celerity_resource('lightbox-attachment-css');
     $attrs = array();
     $image_class = null;
     $use_size = true;
     if (!$options['size']) {
         $width = $this->parseDimension($options['width']);
         $height = $this->parseDimension($options['height']);
         if ($width || $height) {
             $use_size = false;
             $attrs += array('src' => $file->getBestURI(), 'width' => $width, 'height' => $height);
         }
     }
     if ($use_size) {
         switch ((string) $options['size']) {
             case 'full':
                 $attrs += array('src' => $file->getBestURI());
                 $image_class = 'phabricator-remarkup-embed-image-full';
                 break;
             case 'thumb':
             default:
                 $attrs['src'] = $file->getPreview220URI();
                 $dimensions = PhabricatorImageTransformer::getPreviewDimensions($file, 220);
                 $attrs['width'] = $dimensions['sdx'];
                 $attrs['height'] = $dimensions['sdy'];
                 $image_class = 'phabricator-remarkup-embed-image';
                 break;
         }
     }
     if (isset($options['alt'])) {
         $attrs['alt'] = $options['alt'];
     }
     $img = phutil_tag('img', $attrs);
     $embed = javelin_tag('a', array('href' => $file->getBestURI(), 'class' => $image_class, 'sigil' => 'lightboxable', 'meta' => array('phid' => $file->getPHID(), 'uri' => $file->getBestURI(), 'dUri' => $file->getDownloadURI(), 'viewable' => true)), $img);
     switch ($options['layout']) {
         case 'right':
         case 'center':
         case 'inline':
         case 'left':
             $layout_class = 'phabricator-remarkup-embed-layout-' . $options['layout'];
             break;
         default:
             $layout_class = 'phabricator-remarkup-embed-layout-left';
             break;
     }
     if ($options['float']) {
         switch ($options['layout']) {
             case 'center':
             case 'inline':
                 break;
             case 'right':
                 $layout_class .= ' phabricator-remarkup-embed-float-right';
                 break;
             case 'left':
             default:
                 $layout_class .= ' phabricator-remarkup-embed-float-left';
                 break;
         }
     }
     return phutil_tag('div', array('class' => $layout_class), $embed);
 }
Пример #16
0
 public function generate($x, $y)
 {
     $image = imagecreatetruecolor($x, $y);
     $this->draw($image, $x, $y);
     return PhabricatorImageTransformer::saveImageDataInAnyFormat($image, 'image/jpeg');
 }
 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));
 }
 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));
 }
 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);
 }
Пример #20
0
 * 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()
 {
     $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'));
 }