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