2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
5 require_once ("inc/settings.php");
7 function getthumbsdir () {
9 return rtrim (THUMBSDIR, "/");
11 $uploaddir = rtrim (UPLOADDIR, "/");
12 return $uploaddir . "/" . "_thumbs";
17 $host = $_SERVER ["HTTP_HOST"];
18 $colpos = strpos ($host, ':');
19 // some web clients add port informations in Host header
20 if ($colpos !== false) {
21 $host = substr ($host, 0, $colpos);
26 function ext_safe ($path) {
27 $basename = basename_safe ($path);
28 return end (explode (".", $basename));
31 function basename_safe ($path) {
32 return end (explode ("/", $path));
35 function unquote($gpc_str) {
36 if (!isset ($gpc_str)) {
39 if (get_magic_quotes_gpc ()) {
40 return stripslashes ($gpc_str);
46 function thumb_url_from_imgpath ($filename) {
47 if (defined ("THUMBSDIRURL") && (strlen (THUMBSDIRURL) != 0)) {
48 return rtrim (THUMBSDIRURL, '/') . "/mini_" . rawurlencode ($filename);
50 return full_url_from_path (getthumbsdir () . "/mini_" . rawurlencode ($filename));
53 function image_url_from_imgpath ($filename) {
54 if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) {
55 return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename);
58 return full_url_from_path (UPLOADDIR . "/" . rawurlencode ($filename));
61 function full_url_from_path ($path) {
64 while (substr($rel_path, 0, 2) == "./") { // strips ./
65 $rel_path = substr ($rel_path, 2);
68 if ($rel_path [0] == "/") {
71 $script_dir = dirname ($_SERVER ["SCRIPT_NAME"]);
72 while ((substr ($rel_path, 0, 3) == "../") &&
73 (strlen($script_dir) != 0)) {
74 $rel_path = substr ($rel_path, 3);
75 while (substr($rel_path, 0, 2) == "./") {
76 $rel_path = substr ($rel_path, 2);
78 $script_dir = substr ($script_dir, 0, strrpos ($script_dir, "/"));
80 if ((strlen ($script_dir) == 0) && (substr ($rel_path, 0, 3) == "../")) {
83 $path = "$script_dir/$rel_path";
87 $port = $_SERVER ["SERVER_PORT"];
88 if ($_SERVER ["HTTPS"] == "on") {
94 if (($port == "80" && $proto == "http") ||
95 ($port == "443" && $proto == "https")) {
101 return "$proto://$host$port$path";
104 function create_thumbnail_or_copy ($filename, $destfile) {
106 $thumbnail_ok = create_thumbnail ($filename, $destfile);
107 } catch (Exception $e) {
108 $thumbnail_ok = false;
110 if (!$thumbnail_ok) {
111 if (!copy ($filename, $destfile)) {
118 function create_thumbnail ($filename, $destfile) {
119 if (!function_exists ("imagecreatefromjpeg")
120 || !function_exists ("imagecreatefrompng")) {
123 $ext = strtolower (ext_safe ($filename));
124 if ($ext == "jpg" || $ext == "jpeg") {
125 $image = imagecreatefromjpeg ($filename);
126 } else if ($ext == "png") {
127 $image = imagecreatefrompng ($filename);
132 if ($image === false) {
136 if (defined (THUMBSMAXSIZE) && (THUMBSMAXSIZE > 0)) {
137 $thumbsmaxsize = THUMBSMAXSIZE;
139 $thumbsmaxsize = 400; // default value;
142 $width = imageSX ($image);
143 $height = imageSY ($image);
144 if (($width <= $thumbsmaxsize) || ($height <= $thumbsmaxsize)) {
148 if ($width > $height) {
149 $thumb_width = $thumbsmaxsize;
150 $thumb_height = $height * ($thumbsmaxsize / $width);
151 } else if ($width < $height) {
152 $thumb_width = $width * ($thumbsmaxsize / $height);
153 $thumb_height = $thumbsmaxsize;
154 } else if ($width == $height) {
155 $thumb_width = $thumbsmaxsize;
156 $thumb_height = $thumbsmaxsize;
159 $thumb_image = ImageCreateTrueColor ($thumb_width, $thumb_height);
160 if ($thumb_image === false) {
163 if (!imagecopyresampled ($thumb_image, $image, 0, 0, 0, 0,
164 $thumb_width, $thumb_height, $width, $height)) {
168 if ($ext == "jpg" || $ext == "jpeg") {
169 if (!imagejpeg ($thumb_image, $destfile, 100)) {
172 } else if ($ext == "png") {
173 if (!imagepng ($thumb_image, $destfile)) {
178 imagedestroy ($image);
179 imagedestroy ($thumb_image);
184 function safe_create_dir ($dirname) {
185 if (is_dir ($dirname)) {
188 if (file_exists ($dirname)) {
189 par_error ($dirname . ": " . trans ('exist but is not a directory'));
191 if (!mkdir ($dirname)) {
192 par_error ($dirname . ": " . trans ('could not create directory'));
194 par_success ($dirname . ": " . trans ('directory created'));
198 function safe_create_writable_dir ($dirname) {
199 safe_create_dir ($dirname);
200 if (!is_writeable ($dirname) || !is_executable ($dirname)) {
201 par_error ($dirname . ": " . trans ('could not write in directory'));
205 function delete_from_ftp ($file) {
206 $ftp_conn = ftp_connect (FTPSERVER);
207 $login = ftp_login ($ftp_conn, FTPUSER, FTPPASS);
208 if ((!$ftp_conn) || (!$login)) {
211 $target = sprintf ("%s/%s", FTPROOTDIR, $file);
212 $deleted = ftp_delete ($ftp_conn, $target);
217 ftp_close ($ftp_conn);
220 function send_to_ftp ($file) {
221 $ftp_conn = ftp_connect (FTPSERVER);
222 $login = ftp_login ($ftp_conn, FTPUSER, FTPPASS);
223 if ((!$ftp_conn) || (!$login)) {
226 $dest = sprintf ("%s/%s", FTPROOTDIR, $file);
227 $upload = ftp_put ($ftp_conn, $dest, $file, FTP_BINARY);
232 ftp_close ($ftp_conn);
236 exit ("\n</body></html>");
238 function par_success ($message) {
239 printf ("<p class=\"success center\">%s</p>", $message);
241 function par_error ($message) {
242 printf ("<p class=\"error center\">%s</p>", $message);
245 function par_warn ($message) {
246 printf ("<p class=\"warn center\">%s</p>", $message);