Esempio n. 1
0
 public function addFile($fileInfo, $userInfo, $privacy = false, $details = false)
 {
     $config = self::$_registry->get("config");
     $s3 = new Zend_Service_Amazon_S3($config['services']['S3']['key'], $config['services']['S3']['secret']);
     $filenameFilter = new Ml_Filter_FilenameRobot();
     $filenameValidator = new Ml_Validate_Filename();
     if (isset($details['title']) && !empty($details['title'])) {
         $title = $details['title'];
     } else {
         $title = mb_substr(trim($fileInfo['name']), 0, 100);
         /* try to use a good initial title for the file */
         $titleNameposition = mb_strrpos($title, ".");
         $titleSize = mb_strlen($title);
         if ($titleSize > 5 && $titleSize - $titleNameposition <= 5) {
             $tryTitle = mb_substr($title, 0, $titleNameposition);
             if (!empty($tryTitle) && strrpos($tryTitle, ".") < mb_strlen($tryTitle) - 4) {
                 $title = $tryTitle;
             }
         }
     }
     //get the max value of mt_getrandmax() or the max value of the unsigned int type
     $maxRand = mt_getrandmax() < 4294967295.0 ? mt_getrandmax() : 4294967295.0;
     $secret = mt_rand(0, $maxRand);
     $downloadSecret = mt_rand(0, $maxRand);
     $filename = $filenameFilter->filter($fileInfo['name']);
     if (!$filenameValidator->isValid($filename)) {
         $extension = $filenameFilter->filter(strchr($filename, '.'));
         if ($filenameValidator->isValid($extension)) {
             $filename = mt_rand() . $extension;
         } else {
             $filename = mt_rand();
         }
     }
     $this->_dbAdapter->beginTransaction();
     try {
         $this->_dbAdapter->insert("upload_history", array("byUid" => $userInfo['id'], "fileSize" => $fileInfo['size'], "filename" => $fileInfo['name']));
         $uploadId = $this->_dbAdapter->lastInsertId("upload_history");
         if (!$uploadId) {
             throw new Exception("Can not create upload ID.");
         }
         $objectKey = $userInfo['alias'] . "/" . $uploadId . "-" . $downloadSecret . "/" . $filename;
         $put = $s3->putFile($fileInfo['tmp_name'], $config['services']['S3']['sharesBucket'] . "/" . $objectKey, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ, "Content-Type" => Zend_Service_Amazon_S3::getMimeType($objectKey), 'Content-Disposition' => 'attachment;', "x-amz-meta-id" => $uploadId, "x-amz-meta-uid" => $userInfo['id'], "x-amz-meta-username" => $userInfo['alias']));
         if (!$put) {
             throw new Exception("Could not upload to storage service.");
         }
         $getInfo = $s3->getInfo($objectKey);
         //If for error we can't retrieve the md5 from the s3 server...
         if (!$getInfo) {
             $md5 = md5_file($fileInfo['tmp_name']);
         } else {
             $md5 = $getInfo['etag'];
         }
         if (!isset($details['short'])) {
             $details['short'] = '';
         }
         if (!isset($details['description'])) {
             $details['description'] = '';
         }
         $this->_dbTable->insert(array("id" => $uploadId, "byUid" => $userInfo['id'], "secret" => $secret, "download_secret" => $downloadSecret, "privacy" => $privacy, "title" => $title, "filename" => $filename, "short" => $details['short'], "description" => $details['description'], "type" => mb_substr($fileInfo['type'], 0, 50), "fileSize" => $fileInfo['size'], "md5" => $md5));
         if (!$this->_dbAdapter->lastInsertId()) {
             throw new Exception("Could not create insert for new file.");
         }
         $this->_dbAdapter->commit();
         return $uploadId;
     } catch (Exception $e) {
         $this->_dbAdapter->rollBack();
         throw $e;
     }
 }
Esempio n. 2
0
 public function setAvatar($userInfo, $source)
 {
     $registry = Zend_Registry::getInstance();
     $config = $registry->get("config");
     $people = Ml_Model_People::getInstance();
     $s3config = $config['services']['S3'];
     $s3 = new Zend_Service_Amazon_S3($s3config['key'], $s3config['secret']);
     try {
         $im = new Imagick($source);
         $im->setimagecompressionquality(self::$_imageQuality);
         $dim = $im->getimagegeometry();
         if (!$dim) {
             return false;
         }
     } catch (Exception $e) {
         return false;
     }
     $sizesInfo = array();
     $tmpFilenames = array();
     $im->unsharpMaskImage(0, 0.5, 1, 0.05);
     foreach ($this->_sizes as $sizeInfo) {
         $tmpFilenames[$sizeInfo[1]] = tempnam(sys_get_temp_dir(), 'HEADSHOT');
         if ($sizeInfo[0] == "sq") {
             if ($dim['height'] < $dim['width']) {
                 $size = $dim['height'];
             } else {
                 $size = $dim['width'];
             }
             //@todo let the user crop using Javascript, so he/she can set the offsets (default 0,0)
             $im->cropThumbnailImage($sizeInfo[3], $sizeInfo[3]);
         } else {
             if ($dim['width'] < $sizeInfo[3] && $dim['height'] < $sizeInfo[3] && $sizeInfo[2] != 'huge') {
                 copy($source, $tmpFilenames[$sizeInfo[1]]);
             } else {
                 if ($dim['width'] > $dim['height']) {
                     $im->resizeimage($sizeInfo[3], 0, Imagick::FILTER_LANCZOS, 1);
                 } else {
                     $im->resize(0, $sizeInfo[3], Imagick::FILTER_LANCZOS, 1);
                 }
             }
         }
         $im->writeimage($tmpFilenames[$sizeInfo[1]]);
         $imGeometry = $im->getimagegeometry();
         $sizesInfo[$sizeInfo[0]] = array("w" => $imGeometry['width'], "h" => $imGeometry['height']);
     }
     $oldData = unserialize($userInfo['avatarInfo']);
     //get the max value of mt_getrandmax() or the max value of the unsigned int type
     if (mt_getrandmax() < 4294967295.0) {
         $maxRand = mt_getrandmax();
     } else {
         $maxRand = 4294967295.0;
     }
     $newSecret = mt_rand(0, $maxRand);
     if (isset($oldData['secret'])) {
         while ($oldData['secret'] == $newSecret) {
             $newSecret = mt_rand(0, $maxRand);
         }
     }
     foreach ($tmpFilenames as $size => $file) {
         if ($size == '_h') {
             $privacy = Zend_Service_Amazon_S3::S3_ACL_PRIVATE;
         } else {
             $privacy = Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ;
         }
         $picAddr = $s3config['headshotsBucket'] . "/" . $userInfo['id'] . '-' . $newSecret . $size . '.jpg';
         $meta = array(Zend_Service_Amazon_S3::S3_ACL_HEADER => $privacy, "Content-Type" => Zend_Service_Amazon_S3::getMimeType($picAddr), "Cache-Control" => "max-age=37580000, public", "Expires" => "Thu, 10 May 2029 00:00:00 GMT");
         $s3->putFile($file, $picAddr, $meta);
         unlink($file);
     }
     $newAvatarInfo = serialize(array("sizes" => $sizesInfo, "secret" => $newSecret));
     $people->update($userInfo['id'], array("avatarInfo" => $newAvatarInfo));
     //delete the old files
     $this->deleteFiles($userInfo);
     return true;
 }