Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 155 lines (137 sloc) 3.751 kb
617f362 @aredridel Initial commit
aredridel authored
1 <?php
2
3 require_once(dirname(__FILE__)."/debug.php");
4 require_once(dirname(__FILE__)."/ob/exit.php");
5
6 $__HTTP_STATUS_CALLED = FALSE;
7
8 function http_status($code, $message) {
9 global $__HTTP_STATUS_CALLED;
10 if($__HTTP_STATUS_CALLED == TRUE) {
11 trigger_error("http_status has already been called with $__HTTP_STATUS_CALLED");
12 } else {
13 $__HTTP_STATUS_CALLED = TRUE;
14 header("HTTP/1.1 $code"); # $message");
15 }
16 }
17
18 function http_302($uri, $message = 'Permanently Moved') {
19 global $_SERVER;
20 if(!@$_SERVER['FCGI_ROLE']) http_status(302, $message);
21 /*
22 if($uri{0} == '/') {
23 $uri = "http://".$_SERVER['HTTP_HOST'].($_SERVER['HTTP_PORT'] != 80 ? ":".$_SERVER['HTTP_PORT'] : "").$uri;
24 }
25 */
26 /*
27 while(ob_get_level()) ob_end_clean();
28 phpinfo();
29 exit();
30 */
31 header("Location: $uri");
32 ob_exit();
33 }
34
35 function http_404($message = 'Not Found') {
36 http_status(404, $message);
37 }
38
39 function http_401($realm = 'default', $auth_type = 'Basic',
40 $opaque = NULL, $message = 'Unauthorized') {
41 http_status(401, $message);
42 header("WWW-Authenticate: $auth_type realm=\"$realm\"" .
43 ($opaque ? ' opaque="'.base64_encode(ldapify($opaque)).'"'
44 :'')
45 );
46 }
47
48 function ldapify($array, $sep = ' ') {
49 /* Warning: function name will change */
50 foreach($array as $key => $val) {
51 $out[] = "$key=\"" . addslashes($val) . '"';
52 }
53 return join($sep, $array);
54 }
55
56 function unldapify($string) {
57 $out = array();
58 preg_match_all('/(\w+)="([^"]*)"/', $string,
59 $matches, PREG_SET_ORDER);
60 foreach($matches as $match) {
61 $out[$match[0]] = $match[1];
62 }
63 return $out;
64 }
65
66 function fs2ht($s) {
67 global $_SERVER;
68
69 if($s{0} != '/') {
70 return $s;
71 } else {
72 if(strpos($s,
73 $b = dirname($_SERVER['SCRIPT_FILENAME']).'/') === 0) {
74 return substr($s, strlen($b) - 1);
75 }
76 $s = preg_replace('#^'.preg_quote($_SERVER['DOCUMENT_ROOT'])."/#",
77 '/', $s);
78 return $s;
79 }
80 }
81
82 function http_403($message = 'Forbidden') {
83 http_status(403, $message);
84 }
85
86 function http_500($message = 'Server Error') {
87 http_status(500, $message);
88 }
89
90 function get($uri, $refer = NULL) {
91 if(function_exists('curl_init')) {
92 debug("getting $uri with curl");
93 $session = curl_init();
94 curl_setopt($session, CURLOPT_URL, $uri);
95 #curl_setopt($session, CURLOPT_NOPROGRESS, 0);
96 curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
97 curl_setopt($session, CURLOPT_TIMEOUT, 30);
98 if($refer) {
99 curl_setopt($session, CURLOPT_REFERER, $refer);
100 }
101 if(defined("HTTP_USERAGENT")) {
102 curl_setopt($session, CURLOPT_USERAGENT, HTTP_USERAGENT);
103 }
104
105 $o = curl_exec($session);
106 if(function_exists('curl_errno')) {
107 # if(curl_errno($session) > 0) {
108 # print(curl_error($session)."\n");
109 # }
110 }
111 curl_close($session);
112 return $o;
113 } else {
114 debug("Getting $uri with external curl");
115 if($f = popen("curl -s ".escapeshellarg($uri), "r")) {
116 while(!feof($f)) {
117 $o .= fread($f, 1024);
118 }
119 pclose($f);
120 }
121 return $o;
122
123 // Obs:
124 debug("getting $uri with file()");
125 $o = file($uri);
126 if(count($o) > 0) {
127 return join('', $file);
128 } else {
129 return '';
130 }
131 }
132 }
133
134 function post($uri, $data) {
135 $o = array();
136 foreach($data as $field => $val) {
137 $o[] = rawurlencode($field).'='.rawurlencode($val);
138 }
139 $o = join('&', $o);
140 $session = curl_init();
141 curl_setopt($session, CURLOPT_URL, $uri);
142 curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
143 curl_setopt($session, CURLOPT_TIMEOUT, 30);
144 if(defined("HTTP_USERAGENT")) {
145 curl_setopt($session, CURLOPT_USERAGENT, HTTP_USERAGENT);
146 }
147 curl_setopt($session, CURLOPT_POST, 1);
148 curl_setopt($session, CURLOPT_POSTFIELDS, $o);
149 $o = curl_exec($session);
150 curl_close($session);
151 return $o;
152 }
153
154 ?>
Something went wrong with that request. Please try again.