]> dev.renevier.net Git - syp.git/blob - inc/utils.php
12c85c673dfd321a9b1d1a955103aa93f5895d2c
[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 ($filename, $destfile) {
105     $ext = strtolower (ext_safe ($filename));
106     if ($ext == "jpg" || $ext == "jpeg") {
107         $image = imagecreatefromjpeg ($filename);
108     } else if ($ext == "png") {
109         $image = imagecreatefrompng ($filename);
110     } else {
111         return false;
112     }
113
114     if ($image === false) {
115         return false;
116     }
117
118     if (defined (THUMBSMAXSIZE) && (THUMBSMAXSIZE > 0)) {
119         $thumbsmaxsize = THUMBSMAXSIZE;
120     } else {
121         $thumbsmaxsize = 400; // default value;
122     }
123
124     $width = imageSX ($image);
125     $height = imageSY ($image);
126     if (($width  <= $thumbsmaxsize) || ($height <= $thumbsmaxsize)) {
127         return false;
128     }
129
130     if ($width > $height) {
131         $thumb_width = $thumbsmaxsize;
132         $thumb_height = $height * ($thumbsmaxsize / $width);
133     } else if ($width  < $height) {
134         $thumb_width = $width * ($thumbsmaxsize / $height);
135         $thumb_height = $thumbsmaxsize;
136     } else if ($width  == $height) {
137         $thumb_width = $thumbsmaxsize;
138         $thumb_height = $thumbsmaxsize;
139     }
140
141     $thumb_image = ImageCreateTrueColor ($thumb_width, $thumb_height);
142     if ($thumb_image === false) {
143         return false;
144     }
145     if (!imagecopyresampled ($thumb_image, $image, 0, 0, 0, 0,
146                         $thumb_width, $thumb_height, $width, $height)) {
147         return false;
148     }
149
150     if ($ext == "jpg" || $ext == "jpeg") {
151         if (!imagejpeg ($thumb_image, $destfile, 100)) {
152             return false;
153         }
154     } else if ($ext == "png") {
155         if (!imagepng ($thumb_image, $destfile)) {
156             return false;
157         }
158     }
159
160     imagedestroy ($image); 
161     imagedestroy ($thumb_image); 
162
163     return true;
164 }
165
166 function safe_create_dir ($dirname) {
167     if (is_dir ($dirname)) {
168         return;
169     }
170     if (file_exists ($dirname)) {
171         par_error ($dirname . ": " . trans ('exist but is not a directory'));
172     }
173     if (!mkdir ($dirname)) {
174         par_error ($dirname . ": " . trans ('could not create directory'));
175     } else {
176         par_success ($dirname . ": " . trans ('directory created'));
177     }
178 }
179
180 function safe_create_writable_dir ($dirname) {
181     safe_create_dir ($dirname);
182     if (!is_writeable ($dirname) || !is_executable ($dirname)) {
183         par_error ($dirname . ": " . trans ('could not write in directory'));
184     }
185 }
186
187 function leave () {
188     exit ("\n</body></html>");
189 }
190 function par_success ($message) {
191     printf ("<p class=\"success center\">%s</p>", $message);
192 }
193 function par_error ($message) {
194     printf ("<p class=\"error center\">%s</p>", $message);
195     leave ();
196 }
197 function par_warn ($message) {
198     printf ("<p class=\"warn center\">%s</p>", $message);
199 }
200 ?>