Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.Http.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Set of usefull HTTP functions
9  *
10  * @author Anakeen 2000
11  * @version $Id: Lib.Http.php,v 1.38 2008/11/28 12:48:06 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 
19 function Redirect(&$action, $appname, $actionname, $otherurl = "", $httpparamredirect = false)
20 {
21  global $_SERVER, $_GET; // use only with HTTP
22  if ($_SERVER['HTTP_HOST'] == "") {
23  print "\n--Redirect $appname $actionname--\n";
24  return;
25  }
26 
27  if ($appname == "") {
28  $location = ".";
29  } else {
30  if ($otherurl == "") {
31  if (in_array($appname, array(
32  "CORE",
33  "APPMNG",
34  "ACCESS",
35  "AUTHENT"
36  ))) $baseurl = $action->GetParam("CORE_BASEURL");
37  else $baseurl = '?';
38  } else $baseurl = $otherurl;
39  $location = $baseurl . "app=" . $appname . "&action=" . $actionname;
40  // $location .= "&session=".$action->session->id;
41 
42  }
43 
44  $action->log->debug("Redirect : $location");
45 
46  if ($httpparamredirect) {
47  //add ZONE_ARGS
48  global $ZONE_ARGS;
49  if (is_array($ZONE_ARGS)) foreach ($ZONE_ARGS as $k => $v) $location.= "&$k=$v";
50  }
51  global $SQLDEBUG;
52  if ($SQLDEBUG) {
53  global $ticainit, $tic1, $trace;
54  global $TSQLDELAY, $SQLDELAY;
55  $trace["__url"] = $trace["url"];
56  $trace["__init"] = $trace["init"];
57  unset($trace["url"]);
58  unset($trace["init"]);
59  $deb = gettimeofday();
60  $tic4 = $deb["sec"] + $deb["usec"] / 1000000;
61  $trace["__app"] = sprintf("%.03fs", $tic4 - $ticainit);
62  $trace["__memory"] = sprintf("%dkb", round(memory_get_usage() / 1024));
63  $trace["__queries"] = sprintf("%.03fs #%d", $SQLDELAY, count($TSQLDELAY));
64  $trace["__server all"] = sprintf("%.03fs", $tic4 - $tic1);
65  $action->register("trace", $trace);
66  }
67  if (($_GET["viewext"] == "yes") || ($_POST["viewext"] == "yes")) {
68  $location = str_replace("FDL_CARD", "VIEWEXTDOC", $location);
69  if (preg_match("/action=GENERIC_EDIT/", $location)) {
70  $location = str_replace("GENERIC_EDIT", "EDITEXTDOC", $location);
71  $location = str_replace("app=GENERIC", "app=FDL", $location);
72  }
73  }
74  Header("Location: $location");
75  exit;
76 }
77 
79 {
80  global $_SERVER;
81 
82  if ($_SERVER["HTTP_REFERER"] != "") {
83  Header("Location: " . $_SERVER["HTTP_REFERER"]); // return to sender
84  exit;
85  }
86  $referer = GetHttpVars("http_referer");
87  if ($referer != "") {
88  Header("Location: " . $referer); // return to sender
89  exit;
90  }
91 
92  $action->exitError(_("no referer url found"));
93  exit;
94 }
95 function GetHttpVars($name, $def = "")
96 {
97 
98  global $_GET, $_POST, $ZONE_ARGS;
99  // it's necessary to strip slashes because HTTP add slashes automatically
100  if (isset($ZONE_ARGS[$name])) return ($ZONE_ARGS[$name]); // try zone args first : it is set be Layout::execute for a zone
101  if (isset($_GET[$name])) return stripslashes($_GET[$name]);
102  if (isset($_POST[$name])) {
103  if (is_array($_POST[$name])) return array_map(create_function('$v', 'return is_scalar($v)?stripslashes($v):$v;'), $_POST[$name]);
104  else return stripslashes($_POST[$name]);
105  }
106  return ($def);
107 }
108 
109 function GetHttpCookie($name, $def = "")
110 {
111 
112  global $_COOKIE;
113  if (isset($_COOKIE[$name])) return $_COOKIE[$name];
114  return ($def);
115 }
116 
117 function SetHttpVar($name, $def)
118 {
119 
120  global $ZONE_ARGS;
121  if ($def == "") unset($ZONE_ARGS[$name]);
122  else $ZONE_ARGS[$name] = $def;
123 }
124 
125 function GetMimeType($ext)
126 {
127  $mimes = file("/etc/mime.types");
128  while (list($k, $v) = each($mimes)) {
129  if (substr($v, 0, 1) == "#") continue;
130  $tab = preg_split("/\s+/", $v);
131  if ((isset($tab[1])) && ($tab[1] == $ext)) return ($tab[0]);
132  }
133  return ("text/any");
134 }
135 
136 function GetExt($mime_type)
137 {
138  $mimes = file("/etc/mime.types");
139  while (list($k, $v) = each($mimes)) {
140  if (substr($v, 0, 1) == "#") continue;
141  $tab = preg_split("/\s+/", $v);
142  if ((isset($tab[0])) && ($tab[0] == $mime_type)) {
143  if (isset($tab[1])) {
144  return ($tab[1]);
145  } else {
146  return ("");
147  }
148  }
149  }
150  return ("");
151 }
152 
153 function Http_Download($src, $ext, $name, $add_ext = TRUE, $mime_type = "")
154 {
155  if ($mime_type == '') $mime_type = GetMimeType($ext);
156  if ($add_ext) $name = $name . "." . $ext;
157  header("Cache-control: private"); // for IE : don't know why !!
158  header('Content-Length: ' . strlen($src));
159  header("Pragma: "); // HTTP 1.0
160  header("Content-Disposition: form-data;filename=\"$name\"");
161  header("Content-type: " . $mime_type);
162  echo $src;
163 }
164 
165 function Http_DownloadFile($filename, $name, $mime_type = '', $inline = false, $cache = true, $deleteafter = false)
166 {
167  if (!file_exists($filename)) {
168  printf(_("file not found : %s") , $filename);
169  return;
170  }
171  // $name=urlencode($name);
172  // $name=htmlentities( $name , ENT_QUOTES , "UTF-8" );
173  if (seems_utf8($name)) $name = utf8_decode($name);
174  $name = str_replace('"', '\\"', $name);
175  if (!$inline) header("Content-Disposition: attachment;filename=\"$name\"");
176  else header("Content-Disposition: inline;filename=\"$name\"");
177 
178  if ($cache) {
179  $duration = 24 * 3600;
180  header("Cache-Control: private, max-age=$duration"); // use cache client (one hour) for speed optimsation
181  header("Expires: " . gmdate("D, d M Y H:i:s T\n", time() + $duration)); // for mozilla
182 
183  } else {
184  header("Cache-Control: private");
185  }
186  header("Pragma: "); // HTTP 1.0
187  if ($inline && substr($mime_type, 0, 4) == "text" && substr($mime_type, 0, 9) != "text/html" && substr($mime_type, 0, 8) != "text/xml") $mime_type = preg_replace("_text/([^;]*)_", "text/plain", $mime_type);
188 
189  header("Content-type: " . $mime_type);
190  header("Content-Transfer-Encoding: binary");
191  header("Content-Length: " . filesize($filename));
192  ob_clean();
193  flush();
194  if (file_exists($filename)) {
195  readfile($filename);
196  if ($deleteafter) unlink($filename);
197  exit;
198  } else {
199  printf(_("file not found : %s") , $filename);
200  }
201 }
202 
204 { // just to debug
205  global $_GET, $_POST, $ZONE_ARGS;
206  print "<PRE>";
207  if (isset($ZONE_ARGS)) print_r($ZONE_ARGS);
208  if (isset($_GET)) print_r($_GET);
209  if (isset($_POST)) print_r($_POST);
210  print "</PRE>";
211 }
212 
213 function glue_url($parsed)
214 {
215  if (!is_array($parsed)) return false;
216  $uri = $parsed['scheme'] ? $parsed['scheme'] . ':' . ((strtolower($parsed['scheme']) == 'mailto') ? '' : '//') : '';
217  $uri.= $parsed['user'] ? $parsed['user'] . ($parsed['pass'] ? ':' . $parsed['pass'] : '') . '@' : '';
218  $uri.= $parsed['host'] ? $parsed['host'] : '';
219  $uri.= $parsed['port'] ? ':' . $parsed['port'] : '';
220  $uri.= $parsed['path'] ? $parsed['path'] : '';
221  $uri.= $parsed['query'] ? '?' . $parsed['query'] : '';
222  $uri.= $parsed['fragment'] ? '#' . $parsed['fragment'] : '';
223  return $uri;
224 }
225 /**
226  * set in cache one hour
227  */
228 function setHeaderCache($mime = "text/css")
229 {
230  ini_set('session.cache_limiter', 'none');
231  $duration = 24 * 3600;
232  header("Cache-Control: private, max-age=$duration"); // use cache client (one hour) for speed optimsation
233  header("Expires: " . gmdate("D, d M Y H:i:s T\n", time() + $duration)); // for mozilla
234  header("Pragma: none"); // HTTP 1.0
235  header("Content-type: $mime");
236 }
237 ?>
← centre documentaire © anakeen - published under CC License - Dynacase