File indexing completed on 2022-09-27 22:11:19

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 // Define path to application directory
0024 defined('APPLICATION_PATH')
0025 || define('APPLICATION_PATH', realpath(dirname(__FILE__)));
0026 
0027 
0028 // Define application environment
0029 defined('APPLICATION_ENV')
0030 || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
0031 
0032 defined('IMAGES_UPLOAD_PATH')
0033 || define('IMAGES_UPLOAD_PATH', APPLICATION_PATH . '/img/');
0034 
0035 
0036 // Ensure library/ is on include_path
0037 set_include_path(implode("/", array(
0038     APPLICATION_PATH . '/../library',
0039     get_include_path(),
0040 )));
0041 
0042 $_mime_type = array(
0043     'image/jpeg' => '.jpg',
0044     'image/jpg' => '.jpg',
0045     'image/png' => '.png',
0046     'image/gif' => '.gif',
0047     'application/x-empty' => '.png'
0048 );
0049 
0050 
0051 require_once 'Zend/Loader/Autoloader.php';
0052 
0053 $loader = Zend_Loader_Autoloader::getInstance();
0054 $loader->setFallbackAutoloader(true);
0055 
0056 $log = new Zend_Log();
0057 $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH .'/data/logs/msg_' . date("Y-m-d") . '.log');
0058 $log->addWriter($writer);
0059 
0060 $log->debug('_POST: ' . print_r($_POST, true));
0061 $log->debug('_FILES: ' . print_r($_FILES, true));
0062 
0063 $upload = new Zend_File_Transfer_Adapter_Http();
0064 $upload->addValidator('Count', false, 1)
0065     ->addValidator('IsImage', false)
0066     ->addValidator('Size', false, 10097152)
0067     ->addValidator('FilesSize', false, 10097152);
0068 
0069 //$upload->addValidator('ImageSize', false,
0070 //    array(
0071 //        'minwidth' => 50,
0072 //        'maxwidth' => 2000,
0073 //        'minheight' => 50,
0074 //        'maxheight' => 2000
0075 //    )
0076 //);
0077 
0078 if (false === $upload->isValid()){
0079     $log->err('isValid errors: ' . print_r($upload->getErrors(), true));
0080     $log->info('isValid messages: ' . print_r($upload->getMessages(), true));
0081 
0082     header("HTTP/1.0 500 Server Error");
0083     print implode("\n<br>", $upload->getMessages());
0084     exit(0);
0085 }
0086 
0087 //create buckets
0088 $fileHash = $upload->getHash('sha1').''. md5(rand());
0089 $destBucketPath = substr_replace($fileHash, '/', 1, 0);
0090 $destBucketPath = substr_replace($destBucketPath, '/', 3, 0);
0091 $destBucketPath = substr_replace($destBucketPath, '/', 5, 0);
0092 $destBucketPath = substr_replace($destBucketPath, '/', 7, 0);
0093 $destPath = IMAGES_UPLOAD_PATH . $destBucketPath . $_mime_type[$upload->getMimeType()];
0094 
0095 $dir = dirname($destPath);
0096 if (!file_exists($destPath) and !is_dir($dir)) {
0097     mkdir($dir, 0777, true);
0098 }
0099 
0100 $upload->addFilter('Rename', array('target' => $destPath, 'overwrite' => true));
0101 
0102 if (false === $upload->receive()) {
0103     $log->err('receive errors: ' . print_r($upload->getErrors(), true));
0104     $log->info('receive messages: ' . print_r($upload->getMessages(), true));
0105 
0106     header("HTTP/1.0 500 Server Error");
0107     print implode("\n<br>", $upload->getMessages());
0108     exit(0);
0109 }
0110 
0111 header("HTTP/1.0 200 OK");
0112 print $destBucketPath . $_mime_type[$upload->getMimeType()];