]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/example/Rest_Proxy.php
Release 6.5.0
[Github/sugarcrm.git] / service / example / Rest_Proxy.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 ob_start();
39 $fp =  fopen('proxy.log', 'a');
40 define('PROXY_SERVER',  'http://localhost/service/v2/rest.php');
41 $headers = (function_exists('getallheaders'))?getallheaders(): array();
42 $_headers  = array();
43 foreach($headers as $k=>$v){
44         $_headers[strtolower($k)] = $v;
45 }
46 $url = parse_url(PROXY_SERVER);
47 if(!empty($_headers['referer']))$curl_headers['referer'] = 'Referer: '  . $_headers['referer'];
48 if(!empty($_headers['user-agent']))$curl_headers['user-agent'] = 'User-Agent: ' . $_headers['user-agent'];
49 if(!empty($_headers['accept']))$curl_headers['accept'] = 'Accept: ' . $_headers['accept'];
50 if(!empty($_headers['accept-language']))$curl_headers['accept-Language'] = 'Accept-Language: ' . $_headers['accept-language'];
51 if(!empty($_headers['accept-encoding']))$curl_headers['accept-encoding:'] = 'Accept-Encoding: ' .$_headers['accept-encoding'];
52 if(!empty($_headers['accept-charset']))$curl_headers['accept-charset:'] = 'Accept-Charset: ' .$_headers['accept-charset'];
53
54 // create a new cURL resource
55 $ch = curl_init();
56 // set URL and other appropriate options
57 curl_setopt($ch, CURLOPT_URL, PROXY_SERVER);
58 curl_setopt ($ch, CURLOPT_POST, 1);
59 curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
60 curl_setopt($ch, CURLOPT_HEADER, 1);
61 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
62 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
63 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
64 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0  );
65 $post_data = '';
66 if(!empty($_POST)){
67         foreach($_POST as $k=>$v){
68                 if(get_magic_quotes_gpc())$v = stripslashes($v);
69                 if(!empty($post_data))$post_data .= '&';
70                 $post_data .= "$k=" . $v;
71         }
72 }
73 curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_data);
74 // grab URL and pass it to the browser
75 fwrite($fp, 'client headers:' . var_export($headers, true) . "\n");
76 fwrite($fp, 'starting curl request' . "\n");
77 fwrite($fp, $post_data . "\n");
78 $result = curl_exec($ch);
79 curl_close($ch);
80 fwrite($fp, 'finished curl request' . "\n");
81 fwrite($fp, 'response:' . var_export($result, true) . "\n");
82 //we only handle 1 response no redirects
83 $result = explode("\r\n\r\n", $result, 2);
84 //we neeed to split up the ehaders
85 $result_headers = explode("\r\n", $result[0]);
86 //now echo out the same headers the server passed to us
87 fwrite($fp, "setting headers\n");
88 foreach($result_headers as &$header){
89         if(substr_count($header, 'Set-Cookie:') ==0)
90         header($header);
91 }
92 header('Content-Length: ' . strlen($result[1]));
93 header('Connection: close');
94 // now echo out the body
95 fwrite($fp, "sending body\n");
96 echo $result[1];
97 ob_end_flush();
98 fwrite($fp, "done\n");
99 die();
100 // close cURL resource, and free up system resources
101
102 ?>