-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
171 lines (142 loc) · 4.16 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Core functionality of TS-Custom-Updates plugin, API end-site.
* Based on: http://konstruktors.com/blog/wordpress/2538-automatic-updates-for-plugins-and-themes-hosted-outside-wordpress-extend/
*
* @author Vino Rodrigues
* @package TS-Custom-Update-Server
* @since TS-Automatic-Theme-Plugin-Update 0.1
*
* WARNING: This code is not very forgiving on errors. Error handling is minimal and non-descript.
*/
define( 'LOADER', 'jsonloader' );
global $request_data;
global $packages;
require_once(LOADER.'.php');
/**
* get var
*
* @global type $request_data
* @param string $name
* @return string or false
*/
function get_request($name) {
global $request_data;
if (is_a($request_data, 'StdClass') && isset($request_data->$name))
return $request_data->$name;
if (isset($_REQUEST[$name]))
return $_REQUEST[$name];
return false; // no luck
}
if (!function_exists('http_response_code')) :
function http_response_code($code) {
header(':', true, $code);
header("HTTP/1.0 $code", true, $code);
header("Status: $code", true, $code);
}
endif;
if (false && file_exists('whitelist.php')) // TODO : remove false
include_once('whitelist.php');
// Force refresh
header('CacheControl: no-cache, must-revalidate');
header('Expires: Tue, 1 Jan 1980 00:00:00 GMT');
header('Pragma: no-cache');
header('Content-Type: application/json');
// Get json raw data sent
$request_data = json_decode(file_get_contents("php://input"));
if (isset($whitelist)) :
$api_key = get_request('api-key');
if ( !$api_key || !in_array($api_key, $whitelist) ) :
// http_response_code(403);
print json_encode(array('error'=>'Access denied', 'reason'=>'Not in whitelist'));
die();
endif;
endif;
// get packages from loader
$packages = function_exists('get_packages') ? get_packages() : array();
// Get user agent
// $user_agent = $_SERVER['HTTP_USER_AGENT'];
// Get action
$action = strtolower( get_request('action') );
if ($action == 'list_all') :
print json_encode($packages);
die();
endif;
// Get data
$data = get_request('data');
if (is_a($data, 'StdClass'))
$data = object2array($data);
// TODO : check data is array or string...
function get_latest_version($slug) {
global $packages;
$latest = -1;
if ( ! isset($packages[$slug] ) ) return $latest;
foreach ($packages[$slug]['versions'] as $ver => $data) :
if (version_compare($latest, $ver, '<'))
$latest = $ver;
endforeach;
return $latest;
}
function get_version_data($slug, $ver) {
global $packages;
if ( isset( $packages[$slug] ) )
return $packages[$slug]['versions'][$ver];
else
return array();
}
switch ($action) :
case 'check' :
$checklist = array();
if (isset($data['plugins']))
foreach ( $data['plugins'] as $slug => $ver )
$checklist[$slug] = $ver;
if (isset($data['themes']))
foreach ( $data['themes'] as $slug => $ver )
$checklist[$slug] = $ver;
$response = array();
foreach ( $checklist as $slug => $v_sent ) :
$v_stored = get_latest_version($slug);
if ( version_compare($v_sent, $v_stored, '<') ) :
$latest_package = get_version_data($slug, $v_stored);
$response[$slug] = array(
'new_version' => $v_stored,
'slug' => $slug,
'package' => isset($latest_package['download_link']) ? $latest_package['download_link'] : '',
);
if ( isset( $latest_package['url'] ) )
$response[$slug]['url'] = $latest_package['url']; // Info URL
endif;
endforeach;
if ( count($response) > 0 )
print json_encode( array( 'new' => $response ) );
else
print json_encode( array( 'ok' => 'no updates' ) );
die();
break;
case 'plugin_information' :
if (isset($data['get'])):
$slug = $data['get'];
$latest_vesion = get_latest_version($slug);
$latest_package = get_version_data($slug, $latest_vesion);
if (empty($latest_package)) :
print json_encode( array(
'error' => 'not found',
'reason' => 'no package for ' . $slug,
) );
else :
print json_encode( array(
'info' => $latest_package,
) );
endif;
die();
endif;
// let it error out 'casue the request is invalid
break;
endswitch;
/// go away!
http_response_code(403);
print json_encode(array(
'error' => 'Access denied',
'reason' => 'No direct access',
) );
/* eof */