-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupgrademe.php
204 lines (172 loc) · 6.28 KB
/
upgrademe.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
Plugin Name: upgrademe
Description: Allows auto-upgrade for plugins outside of official WordPress repository
Company: Copyright 2011 Meglio. All rights reserved
Version: 0.2
Author: Meglio (Anton Andriyevskyy)
Plugin URI: https://github.com/meglio/wp-upgrademe
*/
if (!class_exists('Upgrademe')) {
class Upgrademe
{
/**
* Stores parsed and validated data returned by unofficial APIs.
* @var array
*/
private static $data;
private static $WP_FILTER_PREFIX = 'wpFilter_';
public static function register()
{
$r = new ReflectionClass(__CLASS__);
$methods = $r->getMethods(ReflectionMethod::IS_PUBLIC);
foreach($methods as $m)
{
/** @var ReflectionMethod $m */
if ($m->isStatic() && strpos($m->getName(), self::$WP_FILTER_PREFIX) === 0) {
add_filter(substr($m->getName(), strlen(self::$WP_FILTER_PREFIX)), array(get_class(), $m->getName()),
10, $m->getNumberOfParameters());
}
}
}
public static function wpFilter_http_response($response, $args, $url)
{
# Control recursion
static $recursion = false;
if ($recursion)
return $response;
if (empty($response) || !is_array($response) || !isset($response['body']))
return $response;
# Guess if it's time to take action
if ($url == 'http://api.wordpress.org/plugins/update-check/1.0/')
$showTime = true;
# Prevent failures if WordPress changes url for updates; we will detect if it still contains "update-check" token
# and called from withing wp_update_plugins() function
elseif (stripos($url, 'update-check') !== false)
{
$showTime = false;
$trace = debug_backtrace(false);
foreach($trace as $t)
# http request made from within wp_update_plugins
if (isset($t['function']) && $t['function'] == 'wp_update_plugins')
{
$showTime = true;
break;
}
unset($trace, $t);
}
else
$showTime = false;
if (!$showTime)
return $response;
# Loop over plugins who provided <pluginName>_upgrademe() function and use returned url to request for up-to-date version signature.
# Collect retrieved (only valid) data into $upgrademe
$plugins = get_plugins();
$upgrademe = array();
foreach($plugins as $file => $info)
{
# Get url if function exists
$slugName = str_replace('-', '_', basename($file, '.php'));
# Request latest version signature from custom url (non-WP plugins repository api) && validate response variables
$recursion = true;
$vars = self::loadPluginData($slugName);
$recursion = false;
if (empty($vars))
continue;
$upgrademe[$file] = $vars;
}
if (!count($upgrademe))
return $response;
$body = $response['body'];
if (!empty($body))
$body = unserialize($body);
if (empty($body))
$body = array();
foreach($upgrademe as $file => $upgradeVars)
{
# Do not override data returned by official WP plugins repository API
if (isset($body[$file]))
continue;
# If new version is different then current one, only then add info
if (!isset($plugins[$file]['Version']) || $plugins[$file]['Version'] == $upgradeVars['new_version'])
continue;
$upgradeInfo = new stdClass();
$upgradeInfo->id = $upgradeVars['id'];
$upgradeInfo->slug = $upgradeVars['slug'];
$upgradeInfo->new_version = $upgradeVars['new_version'];
$upgradeInfo->url = $upgradeVars['url'];
$upgradeInfo->package = $upgradeVars['package'];
$body[$file] = $upgradeInfo;
}
$response['body'] = serialize($body);
return $response;
}
public static function wpFilter_plugins_api($value, $action, $args)
{
// If for some reason value available already, do not change it
if (!empty($value))
return $value;
if ($action != 'plugin_information' || !is_object($args) || !isset($args->slug) || empty($args->slug))
return $value;
$vars = self::loadPluginData($args->slug);
if (empty($vars))
return $value;
return (object)$vars['info'];
}
public static function wpFilter_http_request_args($args, $url)
{
if (strpos($url, 'wp-upgrademe') === false || !is_array($args))
return $args;
$args['sslverify'] = false;
return $args;
}
private static function loadPluginData($slug)
{
if (isset(self::$data[$slug]))
return self::$data[$slug];
$funcName = $slug.'_upgrademe';
if (!function_exists($funcName))
return self::$data[$slug] = null;
$upgradeUrl = filter_var(call_user_func($funcName), FILTER_VALIDATE_URL);
if (empty($upgradeUrl))
return self::$data[$slug] = null;
# Request latest version signature from custom url (non-WP plugins repository api) && validate response variables
$r = wp_remote_post($upgradeUrl, array('method' => 'POST', 'timeout' => 4, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true,
'headers' => array(), 'body' => null, 'cookies' => array(), 'sslverify' => false));
if( is_wp_error($r) || !isset($r['body']) || empty($r['body']))
return self::$data[$slug] = null;
$vars = json_decode($r['body'], true);
if (empty($vars) || !is_array($vars) || count($vars) > 4
|| !isset($vars['new_version']) || !isset($vars['url']) || !isset($vars['package']) || !isset($vars['info']))
return self::$data[$slug] = null;
# 2 147 483 648 - max int32
# 16 777 215 - ffffff = max possible value of 6-letters hex
# 50 000 000 - reasonable offset
# Finally generate ID between 50 000 000 and 66 777 215
$vars['id'] = 50000000 + hexdec(substr(md5($slug), 1, 6));
$vars['slug'] = $slug;
# Sanitize variables of "info"
if (!is_array($vars['info']))
$vars['info'] = array();
$info = array();
foreach($vars['info'] as $key => $val)
{
if (!in_array($key, array('name','slug','version','author','author_profile','contributors','requires','tested',
'compatibility','rating','rating','num_ratings','downloaded','last_updated','added','homepage',
'sections','download_link','tags')))
continue;
$info[$key] = $val;
}
$info['slug'] = $slug;
$info['version'] = $vars['new_version'];
$info['download_link'] = $vars['url'];
$vars['info'] = $info;
return self::$data[$slug] = $vars;
}
}
Upgrademe::register();
function upgrademe_upgrademe()
{
return 'https://raw.github.com/meglio/wp-upgrademe/master/meta.php';
}
} # class_exists()