File indexing completed on 2024-06-23 05:51:18

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Backend_Commands_DeleteProductUnsplash implements Local_Queue_CommandInterface
0024 {
0025 
0026     /** @var Zend_Db_Table_Row_Abstract */
0027     protected $product;
0028     /**
0029      * @var Zend_Config
0030      */
0031     private $config;
0032 
0033     public function __construct()
0034     {
0035         $this->config = Zend_Registry::get('config');
0036     }
0037 
0038     public function doCommand()
0039     {
0040         $projectTable = new Default_Model_DbTable_Project();
0041         $sql = 'SELECT `project`.`project_id`, `source_url`, `image_small`, `image_big`
0042                 FROM `project` 
0043                 WHERE `project`.`source_url` LIKE "%unsplash.com%" AND `project`.`status` = 20 AND `project`.`image_small` NOT LIKE "%--%"
0044                 ORDER BY `project`.`project_id`
0045                 ';
0046         $result = $projectTable->getAdapter()->fetchAll($sql);
0047         $count_projects = count($result);
0048         $count_errors = 0;
0049 
0050         if (count($result) == 0) {
0051             return;
0052         }
0053 
0054         foreach ($result as $table_row) {
0055             try {
0056                 $currentPath = $table_row['image_small'];
0057                 $newPath = $this->renameImageOnCdn($currentPath);
0058                 $result = $projectTable->update(array('image_small' => $newPath),"image_small = '" . $table_row['image_small'] . "'");
0059                 Zend_Registry::get('logger')
0060                              ->info(__METHOD__ . ' --> ' . $table_row['project_id'] . ' (' . $currentPath . ' => ' . $newPath . ')');
0061                 $this->renameGalleryImagesOnCdn($table_row['project_id']);
0062             } catch (Exception $e) {
0063                 $count_errors++;
0064                 Zend_Registry::get('logger')->err($e);
0065             }
0066         }
0067 
0068         return array('success' => true, 'total' => $count_projects, 'errors' => $count_errors);
0069     }
0070 
0071     private function renameImageOnCdn($imgPath)
0072     {
0073         $url = $this->config->images->media->delete;
0074         $secret = $this->config->images->media->privateKey;
0075 
0076         $postString = '--' . md5(rand()) . md5(rand());
0077         $url .= '?path=' . urlencode($imgPath) . '&post=' . $postString . '&key=' . $secret;
0078 
0079         $client = new Zend_Http_Client($url);
0080         $response = $client->request('POST');
0081 
0082         if ($response->getStatus() > 200) {
0083             throw new Default_Model_Exception_Image('ERROR: Could not remove images from CD-Server: ' . $url . ' - server response: ' . $response->getBody());
0084         }
0085 
0086         Zend_Registry::get('logger')->info(__METHOD__ . ' - Result fromCN-Server: ' . $response->getBody());
0087 
0088         return $imgPath . $postString;
0089     }
0090 
0091     private function renameGalleryImagesOnCdn($project_id)
0092     {
0093         $galleryTable = new Default_Model_DbTable_ProjectGalleryPicture();
0094         $sql = 'SELECT `picture_src`
0095                 FROM `project_gallery_picture`
0096                 WHERE `project_id` = :project_id
0097                 ';
0098         $result = $galleryTable->getAdapter()->fetchAll($sql, array('project_id' => $project_id));
0099 
0100         if (count($result) == 0) {
0101             return;
0102         }
0103 
0104         foreach ($result as $table_row) {
0105             try {
0106                 $currentPath = $table_row['picture_src'];
0107                 $newImagePath = $this->renameImageOnCdn($currentPath);
0108                 //save renamed images
0109                 $galleryTable->update(array('picture_src' => $newImagePath), "picture_src = '" . $currentPath . "'");
0110                 Zend_Registry::get('logger')
0111                              ->info(__METHOD__ . ' --> ' . $project_id . '(' . $currentPath . '=>' . $newImagePath);
0112             } catch (Exception $e) {
0113                 Zend_Registry::get('logger')->err($e);
0114             }
0115         }
0116     }
0117 
0118 }