public function isValid($value)
 {
     $this->_setValue($value);
     $validZipcode = new Zend_Validate_PostCode('en_US');
     if (!$validZipcode->isValid($value)) {
         $this->_error(self::ZIPCODE);
         return false;
     }
     return true;
 }
 /**
  * Defined by Zend_Validate_Interface
  *
  * Returns true if and only if $value is a valid postalcode
  *
  * @param string $value
  * @param Default_Model_Organization_Address
  * @return boolean
  */
 public function isValid($value, $context = null)
 {
     if (isset($context) && isset($context[$this->getLocaleFieldName()])) {
         $this->setLocale($context[$this->getLocaleFieldName()]);
     }
     return parent::isValid($value);
 }
Beispiel #3
0
 /**
  * @group ZF-9212
  */
 public function testErrorMessageText()
 {
     $this->assertFalse($this->_validator->isValid('hello'));
     $message = $this->_validator->getMessages();
     $this->assertContains('not appear to be an postal code', $message['postcodeNoMatch']);
 }
 /** !!
  ************************************************************************
  * The following methods take the request data, validate it, parse it,
  * and store it if there were no previous errors.
  ************************************************************************
  */
 public function basicProfileSave($request_data)
 {
     global $error_msg;
     $thumb = null;
     $file_name = null;
     $file_mime_type = null;
     $file_size = null;
     $apiDataArray = array('person' => null);
     $this->isError = TRUE;
     if (empty($request_data['first_name'])) {
         $this->message = __('Fields marked with * can not be empty, First name can not be empty.');
     } else {
         if (!empty($request_data['pass']) || !empty($request_data['conpass'])) {
             $set_new_password = true;
             $new_password_ok = false;
             if ($request_data['pass'] != $request_data['conpass']) {
                 $this->message = __('Password and confirm password should match.');
             } else {
                 if (strlen($request_data['pass']) < PA::$password_min_length) {
                     $this->message = sprintf(__('Password should be of %s characters or more.'), PA::$password_min_length);
                 } else {
                     if (strlen($request_data['pass']) > PA::$password_max_length) {
                         $this->message = sprintf(__('Password should be less than %s charcaters.'), PA::$password_max_length);
                     } else {
                         // all is good
                         $new_password_ok = true;
                     }
                 }
             }
         }
     }
     if (isset($request_data['postal_code']) && isset($request_data['postal_code']['value']) && !empty($request_data['postal_code']['value'])) {
         $zipCode = trim($request_data['postal_code']['value']);
         $validator = new Zend_Validate_PostCode("en_US");
         if (!$validator->isValid($zipCode)) {
             $this->message = "The zip code is invalid.";
         } else {
             $request_data['postal_code']['value'] = $zipCode;
             $apiDataArray['person']['zip_code'] = $zipCode;
         }
     }
     if (isset($request_data['about']) && isset($request_data['about']['value']) && !empty($request_data['about']['value'])) {
         $about = trim($request_data['about']['value']);
         $validator = new Zend_Validate_StringLength(array('max' => 500));
         if (!$validator->isValid($about)) {
             $this->message = "The about field is limited to 500 characters. There are " . strlen($about) . " characters in the about field";
         }
     }
     if ($request_data['deletepicture'] == "true") {
         $this->handleDeleteUserPic($request_data);
     }
     /* Parag Jagdale - 10/27/2010: Upload files with Zend library, Resize files and upload to AmazonS3 */
     if (empty($this->message) && !empty($_FILES['userfile']['name'])) {
         $file = null;
         // Recieve uploaded file
         $zendUploadAdapter = new Zend_File_Transfer_Adapter_Http();
         $zendUploadAdapter->setDestination(PA::$upload_path);
         $zendUploadAdapter->addValidator('Size', false, '1MB')->addValidator('Count', false, 1)->addValidator('FilesSize', false, array('min' => '1kB', 'max' => '2MB'))->addValidator('Extension', false, array('jpeg', 'jpg', 'png', 'gif'))->addValidator('ImageSize', false, array('minwidth' => 40, 'maxwidth' => 1024, 'minheight' => 40, 'maxheight' => 768));
         if (!$zendUploadAdapter->isValid()) {
             // Invalid image, so show errors
             $this->message = __("The image you selected as your photo has some errors: <br />" . implode("<br />", $zendUploadAdapter->getMessages()));
         }
         try {
             $zendUploadAdapter->receive();
         } catch (Zend_File_Transfer_Exception $e) {
             $this->message = $e->getMessage();
         }
         if (empty($this->message)) {
             //If there is no error message then try saving to amazon s3
             // save images to amazon s3
             global $app;
             $aws_key = null;
             $aws_secret_key = null;
             $aws_key = $app->configData['configuration']['api_keys']['value']['amazon_aws_key']['value'];
             $aws_secret_key = $app->configData['configuration']['api_keys']['value']['amazon_aws_secret']['value'];
             $amazon_bucket_name = $app->configData['configuration']['api_keys']['value']['amazon_s3_bucket']['value'];
             if (isset($aws_key) && isset($aws_secret_key)) {
                 $s3 = null;
                 $bucketAvailable = false;
                 $bucketCreated = false;
                 $s3 = new Zend_Service_Amazon_S3($aws_key, $aws_secret_key);
                 if (isset($s3)) {
                     $bucketCreated = false;
                     $bucketAvailable = $s3->isBucketAvailable($amazon_bucket_name);
                     if (!$bucketAvailable) {
                         if ($s3->createBucket($amazon_bucket_name)) {
                             // new bucket was created
                             $bucketCreated = true;
                         }
                     } else {
                         $bucketCreated = true;
                     }
                     if ($bucketCreated == true) {
                         // delete old amazon s3 pictures
                         $this->handleDeleteUserPic($request_data);
                         $file_path = $zendUploadAdapter->getFileName('userfile', true);
                         $file_name = $zendUploadAdapter->getFileName('userfile', false);
                         $file_size = $zendUploadAdapter->getFileSize('userfile');
                         $file_name = $this->cleanupFileName($file_name);
                         $file_info = getimagesize($file_path);
                         $file_mime_type = isset($file_info) && isset($file_info['mime']) && !empty($file_info['mime']) ? $file_info['mime'] : null;
                         $apiDataArray['person'] = array('avatar' => array('file_name' => $file_name, 'content_type' => $file_mime_type, 'file_size' => $file_size));
                         // api_call needs to be set to true in order for the User class to update the avatar and avatar_small fields
                         $this->user_info->api_call = true;
                         foreach ($this->_image_sizes as $imageSizeType => $imageDimensions) {
                             // Resize image into thumbnails
                             $imageAsString = null;
                             try {
                                 $thumb = PhpThumbFactory::create($file_path);
                                 if (!isset($this->user_info->core_id) && !empty($this->user_info->core_id)) {
                                     $this->user_info->core_id = 0;
                                 }
                                 $objectPath = $this->buildAmazonS3ObjectURL($imageSizeType, $this->user_info->core_id, $file_name);
                                 if (isset($imageDimensions) && !empty($imageDimensions)) {
                                     // if this is an original size image, the width and height dont need to be set
                                     $thumb->adaptiveResize($imageDimensions['width'], $imageDimensions['height']);
                                 }
                                 $imageAsString = $thumb->getImageAsString();
                                 $amazonURL = "http://s3.amazonaws.com/" . $amazon_bucket_name;
                                 switch ($imageSizeType) {
                                     case "large":
                                         $this->user_info->picture = $objectPath;
                                         $this->user_info->picture_dimensions = $imageDimensions;
                                         break;
                                     case "standard":
                                         $this->user_info->avatar = $objectPath;
                                         $this->user_info->avatar_dimensions = $imageDimensions;
                                         break;
                                     case "medium":
                                         $this->user_info->avatar_small = $objectPath;
                                         $this->user_info->avatar_small_dimensions = $imageDimensions;
                                         break;
                                     default:
                                         break;
                                 }
                             } catch (Exception $e) {
                                 $this->message = $e->getMessage();
                                 break;
                             }
                             if (isset($imageAsString) && !empty($imageAsString)) {
                                 // send object to AmazonS3
                                 $s3->putObject($amazon_bucket_name . $objectPath, $imageAsString, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
                             }
                         }
                     }
                 }
             }
         }
     }
     if (empty($this->message)) {
         // Add first name and last name if they have been changed to the api update data array
         if ($this->user_info->first_name != $request_data['first_name'] || $this->user_info->last_name != $request_data['last_name']) {
             $apiDataArray['person']['name'] = $request_data['first_name'] . ' ' . $request_data['last_name'];
         }
         //If there is no error message then try saving the user information.
         $this->user_info->first_name = $request_data['first_name'];
         $this->user_info->last_name = $request_data['last_name'];
         try {
             if (!empty($request_data['pass'])) {
                 $passwordSaltArray = $this->EncryptPassword($request_data['pass']);
                 if (isset($passwordSaltArray)) {
                     list($encryptedPassword, $salt) = $passwordSaltArray;
                     $this->user_info->password = $encryptedPassword;
                     $apiDataArray['person']['encrypted_password'] = $encryptedPassword;
                     // remove last $ from salt because ruby doesn't like it
                     $salt = rtrim($salt, '$');
                     $apiDataArray['person']['password_salt'] = $salt;
                 } else {
                     $this->message = "Your password could not be changed, please contact the administrator.";
                 }
             }
         } catch (Exception $e) {
             $this->message = $e->getMessage();
         }
     }
     if (empty($this->message)) {
         try {
             $this->user_info->save();
             $dynProf = new DynamicProfile($this->user_info);
             $dynProf->processPOST('basic');
             $dynProf->save('basic', GENERAL);
             $this->message = __('Profile updated successfully.');
             //        $this->redirect2 = PA_ROUTE_EDIT_PROFILE;
             //        $this->queryString = '?type='.$this->profile_type;
             //TODO: change URL after the contributions activity stream URLs have been changed
             $url = CC_APPLICATION_URL . CC_APPLICATION_URL_TO_API . $this->user_info->user_id;
             // Try to send updated data to Core (Ruby)
             $this->sendUserDataToCivicCommons($url, $apiDataArray);
             $this->isError = FALSE;
         } catch (PAException $e) {
             $this->message = $e->message;
         }
     }
     $error_msg = $this->message;
 }
 public function __construct($options = null)
 {
     $this->_messageTemplates[self::INVALID] = trlKwfStatic("Invalid type given. The value should be a string or a integer");
     $this->_messageTemplates[self::NO_MATCH] = trlKwfStatic("'%value%' does not appear to be a postal code");
     parent::__construct($options);
 }