]> dev.renevier.net Git - syp.git/blob - inc/utils.php
fixes: upgrade did not run correctly when gd extension is not present
[syp.git] / inc / utils.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5 require_once ("inc/settings.php");
6
7 function getthumbsdir () {
8     if (THUMBSDIR) {
9         return rtrim (THUMBSDIR, "/");
10     } else {
11         $uploaddir = rtrim (UPLOADDIR, "/");
12         return $uploaddir . "/" . "_thumbs";
13     }
14 }
15
16 function gethost () {
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);
22     }
23     return $host;
24 }
25
26 function ext_safe ($path) {
27     $basename = basename_safe ($path);
28     return end (explode (".", $basename)); 
29 }
30
31 function basename_safe ($path) {
32     return end (explode ("/", $path)); 
33 }
34
35 function unquote($gpc_str) {
36    if (!isset ($gpc_str)) {
37        return $gpc_str;
38    }
39    if (get_magic_quotes_gpc ()) {
40         return stripslashes ($gpc_str);
41    } else {
42        return $gpc_str;
43    }
44 }
45
46 function thumb_url_from_imgpath ($filename) {
47     if (defined ("THUMBSDIRURL") && (strlen (THUMBSDIRURL) != 0)) {
48         return rtrim (THUMBSDIRURL, '/') . "/_mini" . rawurlencode ($filename);
49     }
50     return full_url_from_path (getthumbsdir () . "/mini_" . rawurlencode ($filename));
51 }
52
53 function image_url_from_imgpath ($filename) {
54     if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) {
55         return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename);
56     }
57
58     return full_url_from_path (UPLOADDIR . "/" . rawurlencode ($filename));
59 }
60
61 function full_url_from_path ($path) {
62     $rel_path = $path;
63
64     while (substr($rel_path, 0, 2) == "./") { // strips ./
65         $rel_path = substr ($rel_path, 2);
66     }
67
68     if ($rel_path [0] == "/") {
69         $path = $rel_path;
70     } else {
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);
77             }
78              $script_dir = substr ($script_dir, 0, strrpos ($script_dir, "/"));
79         }
80         if ((strlen ($script_dir) == 0) && (substr ($rel_path, 0, 3) == "../")) {
81             return null;
82         }
83         $path = "$script_dir/$rel_path";
84     }
85
86     $host = gethost();
87     $port = $_SERVER ["SERVER_PORT"];
88     if ($_SERVER ["HTTPS"] == "on") {
89         $proto = "https";
90     } else {
91         $proto = "http";
92     }
93
94     if (($port == "80" && $proto == "http") ||
95         ($port == "443" && $proto == "https")) {
96         $port = "";
97     } else {
98         $port = ":$port";
99     }
100
101     return "$proto://$host$port$path";
102 }
103
104 function create_thumbnail_or_copy ($filename, $destfile) { 
105     try {   
106         $thumbnail_ok = create_thumbnail ($filename, $destfile);
107     } catch (Exception $e) {
108         $thumbnail_ok = false;
109     }
110     if (!$thumbnail_ok) {
111         if (!copy ($filename, $destfile)) {
112             return false; 
113         }
114     }   
115     return true;
116 }
117
118 function create_thumbnail ($filename, $destfile) {
119     if (!function_exists ("imagecreatefromjpeg")
120         || !function_exists ("imagecreatefrompng")) {
121         return false;
122     }
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);
128     } else {
129         return false;
130     }
131
132     if ($image === false) {
133         return false;
134     }
135
136     if (defined (THUMBSMAXSIZE) && (THUMBSMAXSIZE > 0)) {
137         $thumbsmaxsize = THUMBSMAXSIZE;
138     } else {
139         $thumbsmaxsize = 400; // default value;
140     }
141
142     $width = imageSX ($image);
143     $height = imageSY ($image);
144     if (($width  <= $thumbsmaxsize) || ($height <= $thumbsmaxsize)) {
145         return false;
146     }
147
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;
157     }
158
159     $thumb_image = ImageCreateTrueColor ($thumb_width, $thumb_height);
160     if ($thumb_image === false) {
161         return false;
162     }
163     if (!imagecopyresampled ($thumb_image, $image, 0, 0, 0, 0,
164                         $thumb_width, $thumb_height, $width, $height)) {
165         return false;
166     }
167
168     if ($ext == "jpg" || $ext == "jpeg") {
169         if (!imagejpeg ($thumb_image, $destfile, 100)) {
170             return false;
171         }
172     } else if ($ext == "png") {
173         if (!imagepng ($thumb_image, $destfile)) {
174             return false;
175         }
176     }
177
178     imagedestroy ($image); 
179     imagedestroy ($thumb_image); 
180
181     return true;
182 }
183
184 function safe_create_dir ($dirname) {
185     if (is_dir ($dirname)) {
186         return;
187     }
188     if (file_exists ($dirname)) {
189         par_error ($dirname . ": " . trans ('exist but is not a directory'));
190     }
191     if (!mkdir ($dirname)) {
192         par_error ($dirname . ": " . trans ('could not create directory'));
193     } else {
194         par_success ($dirname . ": " . trans ('directory created'));
195     }
196 }
197
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'));
202     }
203 }
204
205 function leave () {
206     exit ("\n</body></html>");
207 }
208 function par_success ($message) {
209     printf ("<p class=\"success center\">%s</p>", $message);
210 }
211 function par_error ($message) {
212     printf ("<p class=\"error center\">%s</p>", $message);
213     leave ();
214 }
215 function par_warn ($message) {
216     printf ("<p class=\"warn center\">%s</p>", $message);
217 }
218 ?>