Example #1
0
 public function isValid($data)
 {
     $valid = parent::isValid($data);
     // Custom valid
     if ($valid) {
         // Check auth
         try {
             $testService = new Zend_Service_Amazon_S3($data['accessKey'], $data['secretKey'], $data['region']);
             $buckets = $testService->getBuckets();
         } catch (Exception $e) {
             $this->addError('Please double check your access keys.');
             return false;
         }
         // Check bucket
         try {
             if (!in_array($data['bucket'], $buckets)) {
                 if (!$testService->createBucket($data['bucket'], $data['region'])) {
                     throw new Exception('Could not create or find bucket');
                 }
             }
         } catch (Exception $e) {
             $this->addError('Bucket name is already taken and could not be created.');
             return false;
         }
     }
     return $valid;
 }
Example #2
0
 public function gc()
 {
     $config = self::$_registry->get("config");
     $s3config = $config['services']['S3'];
     $s3 = new Zend_Service_Amazon_S3($s3config['key'], $s3config['secret']);
     $select = $this->_dbTable->select();
     $select->order("timestamp ASC")->limit(50);
     $delShares = $this->_dbAdapter->fetchAll($select);
     $deletedShares = array();
     foreach ($delShares as $delShare) {
         $objectKey = $delShare['alias'] . "/" . $delShare['share'] . "-" . $delShare['download_secret'] . "/" . $delShare['filename'];
         $object = $s3config['sharesBucket'] . "/" . $objectKey;
         if ($s3->removeObject($object)) {
             $deletedShares[] = $delShare['id'];
         }
     }
     if (!empty($deletedShares)) {
         $querystring = "DELETE from " . $this->_dbAdapter->quoteTableAs($this->_dbTable->getTableName()) . " WHERE ";
         do {
             $line = $this->_dbAdapter->quoteInto("id = ?", current($deletedShares));
             $querystring .= $line;
             next($deletedShares);
             if (current($deletedShares)) {
                 $querystring .= " OR ";
             }
         } while (current($deletedShares));
         $this->_dbAdapter->query($querystring);
     }
     $removedNum = count($deletedShares);
     return $removedNum;
 }
Example #3
0
 private function _sendS3($tmpfile, $name)
 {
     $config = new Zend_Config_Ini('../application/configs/amazon.ini', 's3');
     $s3 = new Zend_Service_Amazon_S3($config->access_key, $config->secret_key);
     $bytes = file_get_contents($tmpfile);
     return $s3->putObject($config->imagebucket . "/" . $name . ".jpg", $bytes, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
 }
 public function syncDirectory()
 {
     $accessKey = Mage::getStoreConfig('amazonsync/general/access_key_id');
     $secretKey = Mage::getStoreConfig('amazonsync/general/secret_access_key');
     $bucket = Mage::getStoreConfig('amazonsync/general/s3_bucket_path');
     $prefix = Mage::getStoreConfig('amazonsync/general/prefix');
     $magentoDir = Mage::getStoreConfig('amazonsync/general/magento_directory');
     $magentoDir = Mage::getBaseDir() . "/" . $magentoDir;
     //Return if S3 settings are empty
     if (!($accessKey && $secretKey && $bucket)) {
         return;
     }
     //Prepare meta data for uploading. All uploaded images are public
     $meta = array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ);
     $s3 = new Zend_Service_Amazon_S3($accessKey, $secretKey);
     //Build prefix for all files on S3
     if ($prefix) {
         $cdnPrefix = $bucket . '/' . $prefix;
     } else {
         $cdnPrefix = $bucket;
     }
     $allFiles = array();
     if (is_dir($magentoDir)) {
         $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($magentoDir), RecursiveIteratorIterator::SELF_FIRST);
         $dir = null;
         foreach ($objects as $name => $object) {
             //Get name of the file and create its key on S3
             if (!($object->getFileName() == '.' || $object->getFileName() == '..')) {
                 if (is_dir($object)) {
                     continue;
                 }
                 $fileName = str_replace($magentoDir, '', $name);
                 $cdnPath = $cdnPrefix . $fileName;
                 //Full path to uploaded file
                 $file = $name;
                 //Upload original file
                 $allFiles[] = $cdnPath;
                 if (!$s3->putFile($file, $cdnPath, $meta)) {
                     $msg = 'Can\'t upload original image (' . $file . ') to S3 with ' . $cdnPath . ' key';
                     throw new Mage_Core_Exception($msg);
                 }
             }
         }
         //Remove Not Matched Files
         foreach ($s3->getObjectsByBucket($bucket) as $object) {
             $object = $bucket . '/' . $object;
             if (!in_array($object, $allFiles)) {
                 $s3->removeObject($object);
             }
         }
         return 'All files uploaded to S3 with ' . $bucket . ' bucket';
     } else {
         $msg = $magentoDir . ' directory does not exist';
         throw new Mage_Core_Exception($msg);
     }
 }
Example #5
0
 public function tearDown()
 {
     unset($this->_amazon->debug);
     $this->_amazon->cleanBucket($this->_bucket);
     $this->_amazon->removeBucket($this->_bucket);
     sleep(1);
 }
Example #6
0
 /**
  * Returns data array of stream variables
  *
  * @return array
  */
 public function stream_stat()
 {
     if (!$this->_objectName) {
         return false;
     }
     $stat = array();
     $stat['dev'] = 0;
     $stat['ino'] = 0;
     $stat['mode'] = 0;
     $stat['nlink'] = 0;
     $stat['uid'] = 0;
     $stat['gid'] = 0;
     $stat['rdev'] = 0;
     $stat['size'] = 0;
     $stat['atime'] = 0;
     $stat['mtime'] = 0;
     $stat['ctime'] = 0;
     $stat['blksize'] = 0;
     $stat['blocks'] = 0;
     $info = $this->_s3->getInfo($this->_objectName);
     if (!empty($info)) {
         $stat['size'] = $info['size'];
         $stat['atime'] = time();
         $stat['mtime'] = $info['mtime'];
     }
     return $stat;
 }
Example #7
0
 /**
  * Returns data array of stream variables
  *
  * @return array
  */
 public function stream_stat()
 {
     if (!$this->_objectName) {
         return false;
     }
     $stat = array();
     $stat['dev'] = 0;
     $stat['ino'] = 0;
     $stat['mode'] = 0777;
     $stat['nlink'] = 0;
     $stat['uid'] = 0;
     $stat['gid'] = 0;
     $stat['rdev'] = 0;
     $stat['size'] = 0;
     $stat['atime'] = 0;
     $stat['mtime'] = 0;
     $stat['ctime'] = 0;
     $stat['blksize'] = 0;
     $stat['blocks'] = 0;
     if (($slash = strchr($this->_objectName, '/')) === false || $slash == strlen($this->_objectName) - 1) {
         /* bucket */
         $stat['mode'] |= 040000;
     } else {
         $stat['mode'] |= 0100000;
     }
     $info = $this->_s3->getInfo($this->_objectName);
     if (!empty($info)) {
         $stat['size'] = $info['size'];
         $stat['atime'] = time();
         $stat['mtime'] = $info['mtime'];
     }
     return $stat;
 }
 /**
  *
  * @param <type> $method
  * @param <type> $path
  * @param <type> $params
  * @param <type> $headers
  * @param <type> $data
  * @return <type> 
  */
 public function _makeRequest($method, $path = '', $params = null, $headers = array(), $data = null)
 {
     if ('PUT' == $method && $this->defaultAcl) {
         $headers = array_merge(array(self::S3_ACL_HEADER => $this->defaultAcl), $headers);
     }
     return parent::_makeRequest($method, $path, $params, $headers, $data);
 }
Example #9
0
 /**
  * Sets up this test case
  *
  * @return void
  */
 public function setUp()
 {
     Zend_Service_Amazon_S3::setKeys(constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ACCESSKEYID'), constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_SECRETKEYID'));
     if (!stream_wrapper_register('s3', 'Zend_Service_Amazon_S3')) {
         $this->fail('Unable to register s3:// streams wrapper');
     }
 }
Example #10
0
 /**
  * Get a key/value array of metadata for the given path.
  *
  * @param  string $path
  * @param  array $options
  * @return array
  */
 public function fetchMetadata($path, $options = array())
 {
     try {
         return $this->_s3->getInfo($this->_getFullPath($path, $options));
     } catch (Zend_Service_Amazon_S3_Exception $e) {
         throw new Zend_Cloud_StorageService_Exception('Error on fetch: ' . $e->getMessage(), $e->getCode(), $e);
     }
 }
Example #11
0
 public function indexAction()
 {
     $s3 = new Zend_Service_Amazon_S3('AKIAJ5HTOSBB7ITPA6VQ', 'n8ZjV8xz/k/FxBGhrVduYlSXVFFmep7aZJ/NOsoj');
     $this->view->buckets = $s3->getBuckets();
     $bucketName = 'vaultman';
     $ret = $s3->getObjectsByBucket($bucketName);
     $this->view->objects = $ret;
     $this->view->form = new Mybase_Form_Files();
     $formData = $this->getRequest()->getPost();
     if ($this->_request->isPost()) {
         $s3->registerStreamWrapper("s3");
         //file_put_contents("s3://".$bucketName."/".$_FILES['img']['name'], fopen($_FILES['img']['tmp_name'], 'r'));
         $adapter = new Zend_File_Transfer_Adapter_Http();
         $adapter->setDestination("s3://" . $bucketName . "/");
         //$adapter->receive();
     }
 }
Example #12
0
 public function tearDown()
 {
     if (!constant('TESTS_ZEND_SERVICE_AMAZON_ONLINE_ENABLED')) {
         return;
     }
     unset($this->_amazon->debug);
     $this->_amazon->cleanBucket($this->_bucket);
     $this->_amazon->removeBucket($this->_bucket);
 }
function s3_put_file($access_key_id, $secret_access_key, $bucket_path, $file_path, $mime)
{
    $result = FALSE;
    if ($access_key_id && $secret_access_key && $bucket_path && $file_path) {
        try {
            $s3 = new Zend_Service_Amazon_S3($access_key_id, $secret_access_key);
            $meta = array();
            $meta[Zend_Service_Amazon_S3::S3_ACL_HEADER] = Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ;
            if ($mime) {
                $meta[Zend_Service_Amazon_S3::S3_CONTENT_TYPE_HEADER] = $mime;
            }
            if ($s3->putFileStream($file_path, $bucket_path, $meta)) {
                $result = TRUE;
            }
        } catch (Exception $ex) {
            //print $ex->getMessage();
        }
    }
    return $result;
}
Example #14
0
 protected function _initApi()
 {
     if (!$this->_apiInitialized) {
         @ini_set('max_execution_time', self::TIMEOUT);
         @set_time_limit(self::TIMEOUT);
         if (!$this->_api) {
             $this->_api = new Garp_Service_Amazon_S3($this->_config['apikey'], $this->_config['secret']);
         }
         $this->_api->getHttpClient()->setConfig(array('timeout' => self::TIMEOUT, 'keepalive' => $this->_config['keepalive']));
         $this->_apiInitialized = true;
     }
 }
Example #15
0
 public function __construct($accessKey = null, $secretKey = null, $region = null)
 {
     parent::__construct($accessKey, $secretKey, $region);
     $this->_region = $region;
     switch ($region) {
         case 'us-east-1':
             // default endpoint is correct for this region
             break;
         default:
             $this->setEndpoint(sprintf('http://s3-%s.amazonaws.com', $region));
     }
 }
Example #16
0
 /**
  * Get a URI for a "stored" file.
  *
  * @see http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html#RESTAuthenticationQueryStringAuth
  * @param string $path
  * @return string URI
  */
 public function getUri($path)
 {
     $endpoint = $this->_s3->getEndpoint();
     $object = urlencode($this->_getObjectName($path));
     $uri = "{$endpoint}/{$object}";
     if ($expiration = $this->_getExpiration()) {
         $date = new Zend_Date();
         $date->add($expiration, Zend_Date::MINUTE);
         $accessKeyId = $this->_options[self::AWS_KEY_OPTION];
         $secretKey = $this->_options[self::AWS_SECRET_KEY_OPTION];
         $expires = $date->getTimestamp();
         $stringToSign = "GET\n\n\n{$expires}\n/{$object}";
         $signature = base64_encode(Zend_Crypt_Hmac::compute($secretKey, 'sha1', utf8_encode($stringToSign), Zend_Crypt_Hmac::BINARY));
         $query['AWSAccessKeyId'] = $accessKeyId;
         $query['Expires'] = $expires;
         $query['Signature'] = $signature;
         $queryString = http_build_query($query);
         $uri .= "?{$queryString}";
     }
     return $uri;
 }
Example #17
0
 /**
  * Set the logging mechanism
  *
  * @param  Zend_Log $logger
  * @return void
  */
 public static function setLogger(Zend_Log $logger)
 {
     self::$logger = $logger;
 }
Example #18
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;
 }
Example #19
0
 public function removeFile($path)
 {
     // Should we add bucket here?
     $path = $this->_bucket . '/' . $path;
     $this->_internalService->removeObject($path);
 }
Example #20
0
 /**
  * Tears down this test case
  *
  * @return void
  */
 public function tearDown()
 {
     if (!$this->_config) {
         return;
     }
     // Delete the bucket here
     $s3 = new Zend_Service_Amazon_S3($this->_config->get(Zend_Cloud_StorageService_Adapter_S3::AWS_ACCESS_KEY), $this->_config->get(Zend_Cloud_StorageService_Adapter_S3::AWS_SECRET_KEY));
     $s3->removeBucket($this->_config->get(Zend_Cloud_StorageService_Adapter_S3::BUCKET_NAME));
     parent::tearDown();
 }
 /**
  * Upload placeholders to CDN
  *
  * @return null
  */
 public function uploadAction()
 {
     $website = $this->getRequest()->getParam('website');
     $website = Mage::app()->getWebsite($website);
     if (!$website->getId()) {
         return $this->_back('No website parameter', self::ERROR, $website);
     }
     $store = $website->getDefaultStore();
     $accessKey = $store->getConfig(MVentory_CDN_Model_Config::ACCESS_KEY);
     $secretKey = $store->getConfig(MVentory_CDN_Model_Config::SECRET_KEY);
     $bucket = $store->getConfig(MVentory_CDN_Model_Config::BUCKET);
     $prefix = $store->getConfig(MVentory_CDN_Model_Config::PREFIX);
     $dimensions = $store->getConfig(MVentory_CDN_Model_Config::DIMENSIONS);
     Mage::log($dimensions);
     if (!($accessKey && $secretKey && $bucket && $prefix)) {
         return $this->_back('CDN settings are not specified', self::ERROR, $website);
     }
     unset($path);
     $config = Mage::getSingleton('catalog/product_media_config');
     $destSubdirs = array('image', 'small_image', 'thumbnail');
     $placeholders = array();
     $appEmulation = Mage::getModel('core/app_emulation');
     $env = $appEmulation->startEnvironmentEmulation($store->getId());
     foreach ($destSubdirs as $destSubdir) {
         $placeholder = Mage::getModel('catalog/product_image')->setDestinationSubdir($destSubdir)->setBaseFile(null)->getBaseFile();
         $basename = basename($placeholder);
         $result = copy($placeholder, $config->getMediaPath($basename));
         if ($result !== true) {
             return $this->_back('Error on copy ' . $placeholder . ' to media folder', self::ERROR, $website);
         }
         $placeholders[] = '/' . $basename;
     }
     $appEmulation->stopEnvironmentEmulation($env);
     unset($store);
     unset($appEmulation);
     $s3 = new Zend_Service_Amazon_S3($accessKey, $secretKey);
     $cdnPrefix = $bucket . '/' . $prefix . '/';
     $dimensions = str_replace(', ', ',', $dimensions);
     $dimensions = explode(',', $dimensions);
     $meta = array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ);
     foreach ($placeholders as $fileName) {
         $cdnPath = $cdnPrefix . 'full' . $fileName;
         $file = $config->getMediaPath($fileName);
         try {
             $s3->putFile($file, $cdnPath, $meta);
         } catch (Exception $e) {
             return $this->_back($e->getMessage(), self::ERROR, $website);
         }
         if (!count($dimensions)) {
             continue;
         }
         foreach ($dimensions as $dimension) {
             $newCdnPath = $cdnPrefix . $dimension . $fileName;
             $productImage = Mage::getModel('catalog/product_image');
             $destinationSubdir = '';
             foreach ($destSubdirs as $destSubdir) {
                 $newFile = $productImage->setDestinationSubdir($destSubdir)->setSize($dimension)->setBaseFile($fileName)->getNewFile();
                 if (file_exists($newFile)) {
                     $destinationSubdir = $destSubdir;
                     break;
                 }
             }
             if ($destinationSubdir == '') {
                 try {
                     $newFile = $productImage->setDestinationSubdir($destinationSubdir)->setSize($dimension)->setBaseFile($fileName)->resize()->saveFile()->getNewFile();
                 } catch (Exception $e) {
                     return $this->_back($e->getMessage(), self::ERROR, $website);
                 }
             }
             try {
                 $s3->putFile($newFile, $newCdnPath, $meta);
             } catch (Exception $e) {
                 return $this->_back($e->getMessage(), self::ERROR, $website);
             }
         }
     }
     return $this->_back('Successfully uploaded all placeholders', self::SUCCESS, $website);
 }
Example #22
0
 public function setMeta($userInfo, $shareInfo, $metaData, $errorHandle = false)
 {
     $config = self::$_registry->get("config");
     if ($userInfo['id'] != $shareInfo['byUid']) {
         throw new Exception("User is not the owner of the share.");
     }
     $changeData = array();
     if ($errorHandle) {
         foreach (self::$_editableMetadata as $what) {
             if (empty($errorHandle[$what]) && $metaData[$what] != $shareInfo[$what]) {
                 $changeData[$what] = $metaData[$what];
             }
         }
     } else {
         $changeData = $metaData;
     }
     if (empty($changeData)) {
         return false;
     }
     if (isset($changeData['filename'])) {
         $s3 = new Zend_Service_Amazon_S3($config['services']['S3']['key'], $config['services']['S3']['secret']);
         $bucketPlusObjectKeyPrefix = $config['services']['S3']['sharesBucket'] . "/" . $userInfo['alias'] . "/" . $shareInfo['id'] . "-" . $shareInfo['download_secret'] . "/";
         $source = $bucketPlusObjectKeyPrefix . $shareInfo['filename'];
         $destination = $bucketPlusObjectKeyPrefix . $changeData['filename'];
         $meta = array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ, "x-amz-copy-source" => $source, "x-amz-metadata-directive" => "COPY");
         $request = $s3->_makeRequest("PUT", $destination, null, $meta);
         if ($request->getStatus() == 200) {
             $filenameChanged = true;
         }
     }
     if (isset($filenameChanged) && $filenameChanged) {
         $removeFiles = Ml_Model_RemoveFiles::getInstance();
         $removeFiles->addFileGc(array("share" => $shareInfo['id'], "byUid" => $shareInfo['byUid'], "download_secret" => $shareInfo['download_secret'], "filename" => $shareInfo['filename'], "alias" => $userInfo['alias']));
         //Using delete from the S3 Zend class here doesn't work because of a bug
         //is not working for some reason after the _makeRequest or other things I tried to COPY...
     } else {
         unset($changeData['filename']);
     }
     if (empty($changeData)) {
         return false;
     }
     if (isset($changeData['description'])) {
         $purifier = Ml_Model_HtmlPurifier::getInstance();
         $changeData['description_filtered'] = $purifier->purify($changeData['description']);
     }
     $date = new Zend_Date();
     $changeData['lastChange'] = $date->get("yyyy-MM-dd HH:mm:ss");
     $this->_dbTable->update($changeData, $this->_dbAdapter->quoteInto("id = ?", $shareInfo['id']));
     return array_merge($shareInfo, $changeData);
 }
Example #23
0
 public function upload($observer)
 {
     $product = $observer->getEvent()->getProduct();
     //There's nothing to process because we're using images
     //from original product in duplicate
     if ($product->getIsDuplicate() || $product->getData('mventory_update_duplicate')) {
         return;
     }
     $images = $observer->getEvent()->getImages();
     //Use product helper from MVentory_API if it's installed and is activated
     //The helper is used to get correct store for the product when MVentory_API
     //extension is used
     //Change current store if product's store is different for correct
     //file name of images
     if (Mage::helper('core')->isModuleEnabled('MVentory_API')) {
         $store = Mage::helper('mventory/product')->getWebsite($product)->getDefaultStore();
         $changeStore = $store->getId() != Mage::app()->getStore()->getId();
     } else {
         $store = Mage::app()->getStore();
         $changeStore = false;
     }
     //Get settings for S3
     $accessKey = $store->getConfig(MVentory_CDN_Model_Config::ACCESS_KEY);
     $secretKey = $store->getConfig(MVentory_CDN_Model_Config::SECRET_KEY);
     $bucket = $store->getConfig(MVentory_CDN_Model_Config::BUCKET);
     $prefix = $store->getConfig(MVentory_CDN_Model_Config::PREFIX);
     $dimensions = $store->getConfig(MVentory_CDN_Model_Config::DIMENSIONS);
     $cacheTime = (int) $store->getConfig(MVentory_CDN_Model_Config::CACHE_TIME);
     //Return if S3 settings are empty
     if (!($accessKey && $secretKey && $bucket && $prefix)) {
         return;
     }
     //Build prefix for all files on S3
     $cdnPrefix = $bucket . '/' . $prefix . '/';
     //Parse dimension. Split string to pairs of width and height
     $dimensions = str_replace(', ', ',', $dimensions);
     $dimensions = explode(',', $dimensions);
     //Prepare meta data for uploading. All uploaded images are public
     $meta = array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ);
     if ($cacheTime > 0) {
         $meta[MVentory_CDN_Model_Config::AMAZON_CACHE_CONTROL] = 'max-age=' . $cacheTime;
     }
     if ($changeStore) {
         $emu = Mage::getModel('core/app_emulation');
         $origEnv = $emu->startEnvironmentEmulation($store);
     }
     $config = Mage::getSingleton('catalog/product_media_config');
     $s3 = new Zend_Service_Amazon_S3($accessKey, $secretKey);
     foreach ($images['images'] as &$image) {
         //Process new images only
         if (isset($image['value_id'])) {
             continue;
         }
         //Get name of the image and create its key on S3
         $fileName = $image['file'];
         $cdnPath = $cdnPrefix . 'full' . $fileName;
         //Full path to uploaded image
         $file = $config->getMediaPath($fileName);
         //Check if object with the key exists
         if ($s3->isObjectAvailable($cdnPath)) {
             $position = strrpos($fileName, '.');
             //Split file name and extension
             $name = substr($fileName, 0, $position);
             $ext = substr($fileName, $position);
             //Search key
             $_key = $prefix . '/full' . $name . '_';
             //Get all objects which is started with the search key
             $keys = $s3->getObjectsByBucket($bucket, array('prefix' => $_key));
             $index = 1;
             //If there're objects which names begin with the search key then...
             if (count($keys)) {
                 $extLength = strlen($ext);
                 $_keys = array();
                 //... store object names without extension as indeces of the array
                 //for fast searching
                 foreach ($keys as $key) {
                     $_keys[substr($key, 0, -$extLength)] = true;
                 }
                 //Find next unused object name
                 while (isset($_keys[$_key . $index])) {
                     ++$index;
                 }
                 unset($_keys);
             }
             //Build new name and path with selected index
             $fileName = $name . '_' . $index . $ext;
             $cdnPath = $cdnPrefix . 'full' . $fileName;
             //Get new name for uploaded file
             $_file = $config->getMediaPath($fileName);
             //Rename file uploaded to Magento
             rename($file, $_file);
             //Update values of media attribute in the product after renaming
             //uploaded image if the image was marked as 'image', 'small_image'
             //or 'thumbnail' in the product
             foreach ($product->getMediaAttributes() as $mediaAttribute) {
                 $code = $mediaAttribute->getAttributeCode();
                 if ($product->getData($code) == $image['file']) {
                     $product->setData($code, $fileName);
                 }
             }
             //Save its new name in Magento
             $image['file'] = $fileName;
             $file = $_file;
             unset($_file);
         }
         //Upload original image
         if (!$s3->putFile($file, $cdnPath, $meta)) {
             $msg = 'Can\'t upload original image (' . $file . ') to S3 with ' . $cdnPath . ' key';
             if ($changeStore) {
                 $emu->stopEnvironmentEmulation($origEnv);
             }
             throw new Mage_Core_Exception($msg);
         }
         //Go to next newly uploaded image if image dimensions for resizing
         //were not set
         if (!count($dimensions)) {
             continue;
         }
         //For every dimension...
         foreach ($dimensions as $dimension) {
             //... resize original image and get path to resized image
             $newFile = Mage::getModel('catalog/product_image')->setDestinationSubdir('image')->setSize($dimension)->setKeepFrame(false)->setConstrainOnly(true)->setBaseFile($fileName)->resize()->saveFile()->getNewFile();
             //Build S3 path for the resized image
             $newCdnPath = $cdnPrefix . $dimension . $fileName;
             //Upload resized images
             if (!$s3->putFile($newFile, $newCdnPath, $meta)) {
                 $msg = 'Can\'t upload resized (' . $dimension . ') image (' . $file . ') to S3 with ' . $cdnPath . ' key';
                 if ($changeStore) {
                     $emu->stopEnvironmentEmulation($origEnv);
                 }
                 throw new Mage_Core_Exception($msg);
             }
         }
     }
     if ($changeStore) {
         $emu->stopEnvironmentEmulation($origEnv);
     }
 }
            if ($wp_blog_tables = $db->fetchCol('SHOW TABLES FROM `' . WORDPRESS_MULTISITE_DB_NAME . '` LIKE "' . $wp_table_prefix . '_' . $wp_blog_id . '%"')) {
                $file_name = 'wordpress_' . $wp_blog_domain . '.sql' . (GZIP_DUMP_FILES ? '.gz' : '');
                printLog('Dumping database tables for the wordpress site ' . $wp_blog_domain);
                mysqldump(WORDPRESS_MULTISITE_DB_NAME, $file_name, $wp_blog_tables);
                $dumps[] = $file_name;
            }
        }
        if ($wp_main_tables = $db->fetchCol('SHOW TABLES FROM `' . WORDPRESS_MULTISITE_DB_NAME . '` WHERE Tables_in_' . WORDPRESS_MULTISITE_DB_NAME . ' REGEXP "' . $wp_table_prefix . '_[a-z]"')) {
            $file_name = 'wordpress.sql' . (GZIP_DUMP_FILES ? '.gz' : '');
            printLog('Dumping database tables for the wordpress core');
            mysqldump(WORDPRESS_MULTISITE_DB_NAME, $file_name, $wp_main_tables);
            $dumps[] = $file_name;
        }
    }
    if (UPLOAD_TO_AWS) {
        $s3 = new Zend_Service_Amazon_S3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
        foreach ($dumps as $dump) {
            $file_contents = file_get_contents(DUMPS_PATH . '/' . $dump);
            printLog('Uploading ' . $dump . ' to the Amazon S3 storage');
            $upload_succesful = $s3->putObject(AWS_BUCKET_NAME . '/' . $timestamp . '/' . $dump, $file_contents);
            if (DELETE_AFTER_UPLOAD && $upload_succesful === true) {
                printLog('Deleting ' . $dump . ' from the local file system');
                unlink(DUMPS_PATH . '/' . $dump);
            }
        }
        if (DELETE_AFTER_UPLOAD) {
            rmdir(DUMPS_PATH);
        }
    }
} catch (Exception $e) {
    printLog($e->getMessage());
 /**
  * Removes the object specified by the file name and user id
  * @param unknown_type $user_id
  * @param unknown_type $file_name
  * @return boolean
  */
 private function removeAmazonS3Object($user_id, $file_name)
 {
     // TODO: make this asynchronous later
     global $app;
     $objectURL = null;
     $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;
         $s3 = new Zend_Service_Amazon_S3($aws_key, $aws_secret_key);
         $bucketAvailable = $s3->isBucketAvailable($amazon_bucket_name);
         if ($bucketAvailable) {
             // bucket is available so try to delete the object
             try {
                 foreach ($this->_image_sizes as $imageSizeType => $imageDimensions) {
                     $objectPath = $this->buildAmazonS3ObjectURL($imageSizeType, $user_id, $file_name);
                     $objectPath = $amazon_bucket_name . $objectPath;
                     $s3->removeObject($objectPath);
                 }
                 return true;
             } catch (Exception $e) {
                 // ignore this error - the extra amazons3 object in the bucket
                 // will not harm anything. It will just be an unclean directory
                 // take care of cleaning asynchronously by deleting orphan objects
                 // that do not appear in the user's picture/avatar urls
                 //$this->message = $e->getMessage();
             }
         } else {
             // no bucket is available
             return false;
         }
     }
     return false;
 }
Example #26
0
 /**
  * Move the dump file to a Amazon S3 bucket
  *
  * @param string $filename
  * @return void
  */
 protected function moveDumpToS3($filename)
 {
     $s3Config = $this->config->s3;
     $this->validateS3Settings($s3Config);
     $s3File = $s3Config->aws_bucket . '/' . $filename;
     $this->writer->line('Copy ' . $filename . ' -> Amazon S3: ' . $s3File);
     $s3 = new Zend_Service_Amazon_S3($s3Config->aws_key, $s3Config->aws_secret_key);
     // use https for uploading
     $s3->setEndpoint('https://' . Zend_Service_Amazon_S3::S3_ENDPOINT);
     $s3->putObject($s3Config->aws_bucket . '/' . $filename, file_get_contents($filename));
     $s3->getObject($s3Config->aws_bucket . '/' . $filename);
 }
 $cdnPrefix = $bucket . '/' . $prefix . '/';
 $dimensions = str_replace(', ', ',', $dimensions);
 $dimensions = explode(',', $dimensions);
 $meta = array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ);
 $config = Mage::getSingleton('catalog/product_media_config');
 $destSubdirs = array('image', 'small_image', 'thumbnail');
 foreach ($destSubdirs as $destSubdir) {
     $placeholder = Mage::getModel('catalog/product_image')->setDestinationSubdir($destSubdir)->setBaseFile(null)->getBaseFile();
     $result = copy($placeholder, $config->getMediaPath(basename($placeholder)));
     if ($result === true) {
         $images[] = '/' . basename($placeholder);
     } else {
         Mage::log('Error on copy ' . $placeholder . ' to media folder', null, 's3.log');
     }
 }
 $s3 = new Zend_Service_Amazon_S3($accessKey, $secretKey);
 $imageNumber = 1;
 foreach ($images as $fileName) {
     Mage::log('Processing image ' . $imageNumber++ . ' of ' . $totalImages, null, 's3.log');
     $cdnPath = $cdnPrefix . 'full' . $fileName;
     $file = $config->getMediaPath($fileName);
     if (!$s3->isObjectAvailable($cdnPath)) {
         Mage::log('Trying to upload original file ' . $file . ' as ' . $cdnPath, null, 's3.log');
         try {
             $s3->putFile($file, $cdnPath, $meta);
         } catch (Exception $e) {
             Mage::log($e->getMessage(), null, 's3.log');
             continue;
         }
     } else {
         Mage::log('File ' . $file . ' has been already uploaded', null, 's3.log');