-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproxy.php
95 lines (73 loc) · 3.64 KB
/
proxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?
/**
* @file proxy.php
* fetches data from memcache if no params are specified
*
* $_GET['q'] and $_GET['url'] take precedence over default values (although url will probably be dropped 'cos it's really unused and allows this script to be used for other purposes)
*
* $_GET['q'] will be received from the FTM client
*/
// include config file loader
require_once "lib/yaml_loader.php";
// Change these configuration options if needed, see above descriptions for info.
$enable_jsonp = false;
$enable_native = true;
$valid_url_regex = '/.*/';
// memcache
$memcache_host = ($config['memcache_host']) ? $config['memcache_host'] : 'localhost';
$memcache_port = ($config['memcache_port']) ? $config['memcache_port'] : '11211' ;
// search
$search['results_per_page'] = ($config['search_results_per_page']) ? $config['search_results_per_page'] : 100;
// search keyword
$search['keyword'] = (isset($_GET['q'])) ? urlencode($_GET['q']) : (($config['search_default_keyword']) ? urlencode($config['search_default_keyword']) : 'firefox');
// ############################### fetch the response data ##################################
// get results from memcache if no url or keyword is given
if ( !isset($_GET['url']) && !isset($_GET['q']) && !isset($_GET['screen_names']) ) {
// connect to memcache
$memcache = new Memcache;
$memcache->connect($memcache_host, $memcache_port) or die ("Could not connect");
// fetch data
$search_data = array($memcache->get('search_data'));
$timeline_data = array($memcache->get('timeline_data'));
$default_data = array($memcache->get('default_data'));
// build the contents json
$contents = json_encode(array_merge(
array('search_results' => reset($search_data)),
array('timeline' => reset($timeline_data)),
array('special_bubbles' => reset($default_data)->special_bubbles),
array('display' => reset($default_data)->display), array('keywords' => reset($default_data)->keywords),
array('version' => reset($default_data)->fftm_version)) );
// set status to ERROR if there are no contents, 200 OK otherwise
$status = array( 'http_code' => ($contents == "false") ? 'ERROR' : 200 );
// proxy the contents of the parameter url
} else if ( isset($_GET['screen_names']) ) {
// load Epi
include_once('lib/twitteroauth/EpiCurl.php');
include_once('lib/twitteroauth/EpiOAuth.php');
include_once('lib/twitteroauth/EpiTwitter.php');
include_once('lib/twitteroauth/secret.php');
// instantiate twitter object
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $user_token, $user_secret_token);
// make the request
$request = $twitterObj->get('/users/lookup.json', array('screen_name' => $_GET['screen_names']));
// set the contents
$contents = json_encode($request->response);
// set the HTTP Auth
$status['http_code'] = $request->code;
}
// ############################### build the response data ##################################
// Split header text into an array.
$header_text = preg_split( '/[\r\n]+/', $header );
// $data will be serialized into JSON data.
$data = array();
// Propagate all cURL request / response info to the JSON data object.
$data['status']['http_code'] = $status['http_code'];
// Set the JSON data object contents, decoding it from JSON if possible.
$decoded_json = json_decode( $contents );
$data['contents'] = $decoded_json ? $decoded_json : $contents;
// Generate appropriate content-type header.
$is_xhr = (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) ? strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' : '';
header( 'Content-type: application/' . ( $is_xhr ? 'json' : 'x-javascript' ) );
// Generate JSON/JSONP string
print json_encode( $data );
?>