File indexing completed on 2024-12-22 05:33:57

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  **/
0022 
0023 defined('OCS_DEFAULT_TIMEZONE')
0024 || define('OCS_DEFAULT_TIMEZONE', (getenv('OCS_DEFAULT_TIMEZONE') ? getenv('OCS_DEFAULT_TIMEZONE') : 'UTC'));
0025 
0026 date_default_timezone_set(OCS_DEFAULT_TIMEZONE);
0027 
0028 // Define path to application directory
0029 defined('APPLICATION_PATH')
0030 || define('APPLICATION_PATH', realpath(dirname(__FILE__)));
0031 
0032 // Define path to application library
0033 defined('APPLICATION_LIB')
0034 || define('APPLICATION_LIB', realpath(dirname(__FILE__) . '/../../library'));
0035 
0036 // Define application environment
0037 defined('APPLICATION_ENV')
0038 || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
0039 
0040 defined('IMAGES_UPLOAD_PATH')
0041 || define('IMAGES_UPLOAD_PATH', APPLICATION_PATH . '/');
0042 
0043 defined('OCS_IMGCACHE_LOGFILE')
0044 || define('OCS_IMGCACHE_LOGFILE',
0045     (getenv('OCS_IMGCACHE_LOGFILE') ? getenv('OCS_IMGCACHE_LOGFILE') : APPLICATION_PATH . '/../../data/logs/msg_' . date("Y-m-d") . '.log'));
0046 
0047 defined('OCS_IMGCACHE_LOG_MODE')
0048 || define('OCS_IMGCACHE_LOG_MODE', (getenv('OCS_IMGCACHE_LOG_MODE') ? getenv('OCS_IMGCACHE_LOG_MODE') : 'a'));
0049 
0050 // Ensure library/ is on include_path
0051 set_include_path(implode(PATH_SEPARATOR, array(
0052     APPLICATION_LIB,
0053     get_include_path(),
0054 )));
0055 
0056 $_mime_type = array(
0057     'image/jpeg'          => '.jpg',
0058     'image/jpg'           => '.jpg',
0059     'image/png'           => '.png',
0060     'image/gif'           => '.gif',
0061     'application/x-empty' => '.png'
0062 );
0063 
0064 
0065 require_once 'Zend/Loader/Autoloader.php';
0066 
0067 $loader = Zend_Loader_Autoloader::getInstance();
0068 $loader->setFallbackAutoloader(true);
0069 
0070 $log = new Zend_Log();
0071 $writer = new Zend_Log_Writer_Stream(OCS_IMGCACHE_LOGFILE, OCS_IMGCACHE_LOG_MODE);
0072 $log->addWriter($writer);
0073 
0074 $log->debug('_POST: ' . print_r($_POST, true));
0075 $log->debug('_FILES: ' . print_r($_FILES, true));
0076 
0077 $upload = new Zend_File_Transfer_Adapter_Http();
0078 $upload->addValidator('Count', false, 1)
0079        ->addValidator('IsImage', false)
0080        ->addValidator('Size', false, 5097152)
0081        ->addValidator('FilesSize', false, 5097152);
0082 
0083 if (false === $upload->isValid()) {
0084     $log->err('isValid errors: ' . print_r($upload->getErrors(), true));
0085     $log->info('isValid messages: ' . print_r($upload->getMessages(), true));
0086 
0087     header("HTTP/1.0 500 Server Error");
0088     print implode("\n<br>", $upload->getMessages());
0089     exit(0);
0090 }
0091 
0092 //create buckets
0093 $fileHash = $upload->getHash('sha1');
0094 $destBucketPath = substr_replace($fileHash, '/', 1, 0);
0095 $destBucketPath = substr_replace($destBucketPath, '/', 3, 0);
0096 $destBucketPath = substr_replace($destBucketPath, '/', 5, 0);
0097 $destBucketPath = substr_replace($destBucketPath, '/', 7, 0);
0098 $destPath = IMAGES_UPLOAD_PATH . $destBucketPath . $_mime_type[$upload->getMimeType()];
0099 
0100 $dir = dirname($destPath);
0101 if (!file_exists($destPath) and !is_dir($dir)) {
0102     mkdir($dir, 0777, true);
0103 }
0104 
0105 $upload->addFilter('Rename', array('target' => $destPath, 'overwrite' => true));
0106 
0107 if (false === $upload->receive()) {
0108     $log->err('receive errors: ' . print_r($upload->getErrors(), true));
0109     $log->info('receive messages: ' . print_r($upload->getMessages(), true));
0110 
0111     header("HTTP/1.0 500 Server Error");
0112     echo "HTTP/1.0 500 Server Error";
0113     error_log(implode("\n<br>", $upload->getMessages()));
0114 
0115     exit(0);
0116 }
0117 
0118 header("HTTP/1.0 200 OK");
0119 print $destBucketPath . $_mime_type[$upload->getMimeType()];