]> dev.renevier.net Git - syp.git/blob - inc/utils.php
cee9d9a2a2d7e26650f144e351a214366f7396fc
[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 gethost() {
8     $host = $_SERVER ["HTTP_HOST"];
9     $colpos = strpos ($host, ':');
10     // some web clients add port informations in Host header
11     if ($colpos !== false) {
12         $host = substr ($host, 0, $colpos);
13     }
14     return $host;
15 }
16
17 function basename_safe ($path) {
18     return end (explode ("/", $path)); 
19 }
20
21 function unquote($gpc_str) {
22    if (!isset ($gpc_str)) {
23        return $gpc_str;
24    }
25    if (get_magic_quotes_gpc ()) {
26         return stripslashes ($gpc_str);
27    } else {
28        return $gpc_str;
29    }
30 }
31
32 function full_url_from_imgpath ($filename) {
33     if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) {
34         return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename);
35     }
36
37     $rel_path = UPLOADDIR . "/" . rawurlencode ($filename);
38
39     return full_url_from_path (UPLOADDIR . "/" . rawurlencode ($filename));
40 }
41
42 function full_url_from_path ($path) {
43     $rel_path = $path;
44
45     while (substr($rel_path, 0, 2) == "./") { // strips ./
46         $rel_path = substr ($rel_path, 2);
47     }
48
49     if ($rel_path [0] == "/") {
50         $path = $rel_path;
51     } else {
52         $script_dir = dirname ($_SERVER ["SCRIPT_NAME"]);
53         while ((substr ($rel_path, 0, 3) == "../") &&
54                 (strlen($script_dir) != 0)) {
55             $rel_path = substr ($rel_path, 3);
56             while (substr($rel_path, 0, 2) == "./") {
57                 $rel_path = substr ($rel_path, 2);
58             }
59              $script_dir = substr ($script_dir, 0, strrpos ($script_dir, "/"));
60         }
61         if ((strlen ($script_dir) == 0) && (substr ($rel_path, 0, 3) == "../")) {
62             return null;
63         }
64         $path = "$script_dir/$rel_path";
65     }
66
67     $host = gethost();
68     $port = $_SERVER ["SERVER_PORT"];
69     if ($_SERVER ["HTTPS"] == "on") {
70         $proto = "https";
71     } else {
72         $proto = "http";
73     }
74
75     if (($port == "80" && $proto == "http") ||
76         ($port == "443" && $proto == "https")) {
77         $port = "";
78     } else {
79         $port = ":$port";
80     }
81
82     return "$proto://$host$port$path";
83 }
84 ?>