Backport of fixing vertical alignment on date picker icon
[fa-stable.git] / includes / remote_url.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 /*
14         Read content of remote url via http.
15         Does not require curl php extension nor allow_url_fopen=1.
16 */
17 function url_get_contents($url, $timeout=10)
18 {
19         // get the host name and url path
20         $parsedUrl = parse_url($url);
21
22         if (@$parsedUrl['scheme'] == 'file')
23                 return file_get_contents($parsedUrl['path']);
24
25         $host = $parsedUrl['host'];
26
27         if (isset($parsedUrl['path'])) {
28                 $path = $parsedUrl['path'];
29         } else {
30                 // the url is pointing to the host like http://www.mysite.com
31                 $path = '/';
32         }
33
34         if (isset($parsedUrl['query'])) {
35                 $path .= '?' . $parsedUrl['query'];
36         }
37
38         if (isset($parsedUrl['port'])) {
39                 $port = $parsedUrl['port'];
40         } else {
41                 // most sites use port 80
42                 $port = '80';
43         }
44
45         $response = '';
46
47         // connect to the remote server
48         $fp = @fsockopen($host, $port, $errno, $errstr, $timeout );
49         if( !$fp ) {
50                 return null;
51         } else {
52                 // send the necessary headers to get the file
53                 fputs($fp, "GET $path HTTP/1.0\r\n" .
54                         "Host: $host\r\n".
55                         (isset($parsedUrl['pass']) ? "Authorization: Basic ".
56                                 base64_encode($parsedUrl['user'].':'.$parsedUrl['pass'])."\r\n" : '').
57                         "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3\r\n" .
58                         "Accept: */*\r\n" .
59                         "Accept-Language: en-us,en;q=0.5\r\n" .
60                         "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" .
61                         "Keep-Alive: 300\r\n" .
62                         "Connection: keep-alive\r\n" .
63                         "Referer: http://$host\r\n\r\n");
64
65                 // retrieve the response from the remote server
66                 $unblocked=0;
67
68                 while ($line = fread($fp, 4096)) {
69                         $response .= $line;
70                         if ($host=='localhost' && !$unblocked++)
71                                 stream_set_blocking($fp, 0); // just after connection switch to nonblocking mode
72                         usleep(10);
73                 }
74                 fclose( $fp );
75
76                 if (!strpos($response, "200 OK\r\n"))
77                         return null;
78                 // strip the headers
79                 $pos = strpos($response, "\r\n\r\n");
80
81                 $response = substr($response, $pos + 4);
82         }
83
84         // return the file content
85         return $response;
86 }
87
88 function url_copy($from, $to, $timeout=10)
89 {
90         $f = fopen($to, 'wb');
91         if (!$f || !fwrite($f, url_get_contents($from, $timeout)))
92                 return false;
93         fclose($f);
94         return true;
95 }