File indexing completed on 2024-12-08 05:17:48

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 ini_set("memory_limit", "80M");
0024 
0025 # prevent creation of new directories
0026 $is_locked = false;
0027 
0028 
0029 # figure out requested path and actual physical file paths
0030 $orig_dir = dirname(__FILE__);
0031 $path = $_GET['dir'];
0032 $tokens = explode("/", $path);
0033 $file = "/" . implode('/', array_slice($tokens, 3));
0034 $orig_file = $orig_dir . $file;
0035 
0036 
0037 if (!file_exists($orig_file)) {
0038     header("Status: 404 Not Found");
0039     echo "<br/>PATH=$path<br/>ORIGFILE=$orig_file";
0040     return 0;
0041 }
0042 
0043 
0044 # check if new directory would need to be created
0045 $save_path = "$orig_dir/cache/$tokens[2]$file";
0046 $save_dir = dirname($save_path);
0047 
0048 if (!file_exists($save_dir) && $is_locked) {
0049     header("Status: 403 Forbidden");
0050     echo "Directory creation is forbidden.";
0051     return 0;
0052 }
0053 
0054 # check for a valid image file
0055 if(!getimagesize($orig_file)) {
0056     header("Status: 404 Not Found");
0057     echo "<br/>PATH=$path<br/>ORIGFILE=$orig_file";
0058     return 0;
0059 }
0060 
0061 # parse out the requested image dimensions and resize mode
0062 $x_pos = strpos($tokens[2], 'x');
0063 $dash_pos = strpos($tokens[2], '-');
0064 $target_width = substr($tokens[2], 0, $x_pos);
0065 $target_height = substr($tokens[2], $x_pos + 1, $dash_pos - $x_pos - 1);
0066 $mode = substr($tokens[2], $dash_pos + 1);
0067 
0068 $new_width = $target_width;
0069 $new_height = $target_height;
0070 
0071 $image = new Imagick($orig_file);
0072 list($orig_width, $orig_height, $type, $attr) = getimagesize($orig_file);
0073 
0074 # preserve aspect ratio, fitting image to specified box
0075 if ($mode == "0") {
0076     $new_height = $orig_height * $new_width / $orig_width;
0077     if ($new_height > $target_height) {
0078         $new_width = $orig_width * $target_height / $orig_height;
0079         $new_height = $target_height;
0080     }
0081 } # zoom and crop to exactly fit specified box
0082 else {
0083     if ($mode == "2") {
0084         // crop to get desired aspect ratio
0085         $desired_aspect = $target_width / $target_height;
0086         $orig_aspect = $orig_width / $orig_height;
0087 
0088         if ($desired_aspect > $orig_aspect) {
0089             $trim = $orig_height - ($orig_width / $desired_aspect);
0090             $image->cropImage($orig_width, $orig_height - $trim, 0, $trim / 2);
0091             error_log("HEIGHT TRIM $trim");
0092         } else {
0093             $trim = $orig_width - ($orig_height * $desired_aspect);
0094             $image->cropImage($orig_width - $trim, $orig_height, $trim / 2, 0);
0095         }
0096     }
0097 }
0098 
0099 # mode 3 (stretch to fit) is automatic fall-through as image will be blindly resized
0100 # in following code to specified box
0101 //bugfix: $new_width and $new_height have to be > 0
0102 if($new_width == 0 && $new_height > 0) 
0103     $new_width = $new_height;
0104 if($new_height == 0 && $new_width > 0) 
0105     $new_height = $new_width;
0106 
0107 $image->resizeImage($new_width, $new_height, imagick::FILTER_LANCZOS, 1);
0108 
0109 # save and return the resized image file
0110 if (!file_exists($save_dir)) {
0111     mkdir($save_dir, 0777, true);
0112 }
0113 
0114 $image->writeImage($save_path);
0115 
0116 
0117 // Parse Info / Get Extension
0118 $fsize = filesize($save_path);
0119 $path_parts = pathinfo($save_path);
0120 $ext = strtolower($path_parts["extension"]);
0121 
0122 // Determine Content Type
0123 switch ($ext) {
0124     case "pdf":
0125         $ctype = "application/pdf";
0126         break;
0127     case "exe":
0128         $ctype = "application/octet-stream";
0129         break;
0130     case "zip":
0131         $ctype = "application/zip";
0132         break;
0133     case "doc":
0134         $ctype = "application/msword";
0135         break;
0136     case "xls":
0137         $ctype = "application/vnd.ms-excel";
0138         break;
0139     case "ppt":
0140         $ctype = "application/vnd.ms-powerpoint";
0141         break;
0142     case "gif":
0143         $ctype = "image/gif";
0144         break;
0145     case "png":
0146         $ctype = "image/png";
0147         break;
0148     case "jpeg":
0149     case "jpg":
0150         $ctype = "image/jpg";
0151         break;
0152     default:
0153         $ctype = "text/html";
0154 }
0155 
0156 header("Content-Type: $ctype");
0157 ob_clean();
0158 flush();
0159 
0160 echo file_get_contents($save_path);
0161 
0162 return true;