]> dev.renevier.net Git - syp.git/blob - inc/utils.php
real api and clearer separation between server and client
[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 unquote($gpc_str) {
8    if (!isset ($gpc_str)) {
9        return $gpc_str;
10    }
11    if (get_magic_quotes_gpc ()) {
12         return stripslashes ($gpc_str);
13    } else {
14        return $gpc_str;
15    }
16 }
17
18 function full_url_from_filename ($filename) {
19     if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) {
20         return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename);
21     }
22
23     $rel_path = UPLOADDIR . "/" . rawurlencode ($filename);
24
25     while (substr($rel_path, 0, 2) == "./") { // strips ./
26         $rel_path = substr ($rel_path, 2);
27     }
28
29     if ($rel_path [0] == "/") {
30         $path = $rel_path;
31     } else {
32         $script_dir = dirname ($_SERVER ["SCRIPT_NAME"]);
33         while ((substr ($rel_path, 0, 3) == "../") &&
34                 (strlen($script_dir) != 0)) {
35             $rel_path = substr ($rel_path, 3);
36             while (substr($rel_path, 0, 2) == "./") {
37                 $rel_path = substr ($rel_path, 2);
38             }
39              $script_dir = substr ($script_dir, 0, strrpos ($script_dir, "/"));
40         }
41         if ((strlen ($script_dir) == 0) && (substr ($rel_path, 0, 3) == "../")) {
42             return null;
43         }
44         $path = "$script_dir/$rel_path";
45     }
46
47     $host = $_SERVER ["HTTP_HOST"];
48     $port = $_SERVER ["SERVER_PORT"];
49     if ($_SERVER ["HTTPS"] == "on") {
50         $proto = "https";
51     } else {
52         $proto = "http";
53     }
54
55     if (($port == "80" && $proto == "http") ||
56         ($port == "443" && $proto == "https")) {
57         $port = "";
58     } else {
59         $port = ":$port";
60     }
61
62     return "$proto://$host$port$path";
63 }
64
65 ?>