]> dev.renevier.net Git - syp.git/blob - inc/utils.php
improve wizard (hand editing of settings.php is no more needed)
[syp.git] / inc / utils.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4
5 // using include because that file may be sourced even if config file has not
6 // been created.
7 @include_once ("inc/settings.php");
8
9 function getthumbsdir () {
10     if (THUMBSDIR) {
11         return rtrim (THUMBSDIR, "/");
12     } else {
13         $uploaddir = rtrim (UPLOADDIR, "/");
14         return $uploaddir . "/" . "_thumbs";
15     }
16 }
17
18 function gethost () {
19     $host = $_SERVER ["HTTP_HOST"];
20     $colpos = strpos ($host, ':');
21     // some web clients add port informations in Host header
22     if ($colpos !== false) {
23         $host = substr ($host, 0, $colpos);
24     }
25     return $host;
26 }
27
28 function ext_safe ($path) {
29     $basename = basename_safe ($path);
30     return end (explode (".", $basename)); 
31 }
32
33 function basename_safe ($path) {
34     return end (explode ("/", $path)); 
35 }
36
37 function unquote($gpc_str) {
38    if (!isset ($gpc_str)) {
39        return $gpc_str;
40    }
41    if (get_magic_quotes_gpc ()) {
42         return stripslashes ($gpc_str);
43    } else {
44        return $gpc_str;
45    }
46 }
47
48 function thumb_url_from_imgpath ($filename) {
49     if (defined ("THUMBSDIRURL") && (strlen (THUMBSDIRURL) != 0)) {
50         return rtrim (THUMBSDIRURL, '/') . "/mini_" . rawurlencode ($filename);
51     }
52     return full_url_from_path (getthumbsdir () . "/mini_" . rawurlencode ($filename));
53 }
54
55 function image_url_from_imgpath ($filename) {
56     if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) {
57         return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename);
58     }
59
60     return full_url_from_path (UPLOADDIR . "/" . rawurlencode ($filename));
61 }
62
63 function full_url_from_path ($path) {
64     $rel_path = $path;
65
66     while (substr($rel_path, 0, 2) == "./") { // strips ./
67         $rel_path = substr ($rel_path, 2);
68     }
69
70     if ($rel_path [0] == "/") {
71         $path = $rel_path;
72     } else {
73         $script_dir = dirname ($_SERVER ["SCRIPT_NAME"]);
74         while ((substr ($rel_path, 0, 3) == "../") &&
75                 (strlen($script_dir) != 0)) {
76             $rel_path = substr ($rel_path, 3);
77             while (substr($rel_path, 0, 2) == "./") {
78                 $rel_path = substr ($rel_path, 2);
79             }
80              $script_dir = substr ($script_dir, 0, strrpos ($script_dir, "/"));
81         }
82         if ((strlen ($script_dir) == 0) && (substr ($rel_path, 0, 3) == "../")) {
83             return null;
84         }
85         $path = "$script_dir/$rel_path";
86     }
87
88     $host = gethost();
89     $port = $_SERVER ["SERVER_PORT"];
90     if ($_SERVER ["HTTPS"] == "on") {
91         $proto = "https";
92     } else {
93         $proto = "http";
94     }
95
96     if (($port == "80" && $proto == "http") ||
97         ($port == "443" && $proto == "https")) {
98         $port = "";
99     } else {
100         $port = ":$port";
101     }
102
103     return "$proto://$host$port$path";
104 }
105
106 function create_thumbnail_or_copy ($filename, $destfile) { 
107     try {   
108         $thumbnail_ok = create_thumbnail ($filename, $destfile);
109     } catch (Exception $e) {
110         $thumbnail_ok = false;
111     }
112     if (!$thumbnail_ok) {
113         if (!copy ($filename, $destfile)) {
114             return false; 
115         }
116     }   
117     return true;
118 }
119
120 function create_thumbnail ($filename, $destfile) {
121     if (!function_exists ("imagecreatefromjpeg")
122         || !function_exists ("imagecreatefrompng")) {
123         return false;
124     }
125     $ext = strtolower (ext_safe ($filename));
126     if ($ext == "jpg" || $ext == "jpeg") {
127         $image = imagecreatefromjpeg ($filename);
128     } else if ($ext == "png") {
129         $image = imagecreatefrompng ($filename);
130     } else {
131         return false;
132     }
133
134     if ($image === false) {
135         return false;
136     }
137
138     if (defined (THUMBSMAXSIZE) && (THUMBSMAXSIZE > 0)) {
139         $thumbsmaxsize = THUMBSMAXSIZE;
140     } else {
141         $thumbsmaxsize = 400; // default value;
142     }
143
144     $width = imageSX ($image);
145     $height = imageSY ($image);
146     if (($width  <= $thumbsmaxsize) || ($height <= $thumbsmaxsize)) {
147         return false;
148     }
149
150     if ($width > $height) {
151         $thumb_width = $thumbsmaxsize;
152         $thumb_height = $height * ($thumbsmaxsize / $width);
153     } else if ($width  < $height) {
154         $thumb_width = $width * ($thumbsmaxsize / $height);
155         $thumb_height = $thumbsmaxsize;
156     } else if ($width  == $height) {
157         $thumb_width = $thumbsmaxsize;
158         $thumb_height = $thumbsmaxsize;
159     }
160
161     $thumb_image = ImageCreateTrueColor ($thumb_width, $thumb_height);
162     if ($thumb_image === false) {
163         return false;
164     }
165     if (!imagecopyresampled ($thumb_image, $image, 0, 0, 0, 0,
166                         $thumb_width, $thumb_height, $width, $height)) {
167         return false;
168     }
169
170     if ($ext == "jpg" || $ext == "jpeg") {
171         if (!imagejpeg ($thumb_image, $destfile, 100)) {
172             return false;
173         }
174     } else if ($ext == "png") {
175         if (!imagepng ($thumb_image, $destfile)) {
176             return false;
177         }
178     }
179
180     imagedestroy ($image); 
181     imagedestroy ($thumb_image); 
182
183     return true;
184 }
185 ?>