Skip to content

Commit f20a10f

Browse files
committed
Merge changes from master
2 parents a49dc35 + 338f373 commit f20a10f

File tree

4 files changed

+99
-22
lines changed

4 files changed

+99
-22
lines changed

includes/Wpup/FileCache.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<?php
22
/**
33
* A simple file-based cache.
4+
*
5+
* @internal Data is base64 encoded to avoid unserialization issues ('unserialize(): Error at offset') which
6+
* could be caused by:
7+
* - inconsistent line endings
8+
* - unescaped quotes/slashes etc
9+
* - miscounted unicode characters
10+
*
11+
* @see https://github.com/YahnisElsts/wp-update-server/pull/11
412
*/
513
class Wpup_FileCache implements Wpup_Cache {
614
protected $cacheDirectory;
@@ -18,7 +26,7 @@ public function __construct($cacheDirectory) {
1826
public function get($key) {
1927
$filename = $this->getCacheFilename($key);
2028
if ( is_file($filename) && is_readable($filename) ) {
21-
$cache = unserialize(file_get_contents($filename));
29+
$cache = unserialize(base64_decode(file_get_contents($filename)));
2230
if ( $cache['expiration_time'] < time() ) {
2331
return null; //Cache expired.
2432
} else {
@@ -41,7 +49,7 @@ public function set($key, $value, $expiration = 0) {
4149
'expiration_time' => time() + $expiration,
4250
'value' => $value,
4351
);
44-
file_put_contents($this->getCacheFilename($key), serialize($cache));
52+
file_put_contents($this->getCacheFilename($key), base64_encode(serialize($cache)));
4553
}
4654

4755
/**

includes/Wpup/Package.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getMetadata() {
7575
*/
7676
public static function fromArchive($filename, $slug = null, Wpup_Cache $cache = null) {
7777
$modified = filemtime($filename);
78-
$cacheKey = 'metadata-' . md5($filename . '|' . filesize($filename) . '|' . $modified);
78+
$cacheKey = 'metadata-b64-' . $slug . '-' . md5($filename . '|' . filesize($filename) . '|' . $modified);
7979
$metadata = null;
8080

8181
//Try the cache first.
@@ -123,12 +123,14 @@ public static function extractMetadata($zipFilename){
123123
if ( isset($packageInfo['header']) && !empty($packageInfo['header']) ){
124124
$mapping = array(
125125
'Name' => 'name',
126-
'Version' => 'version',
127-
'PluginURI' => 'homepage',
128-
'ThemeURI' => 'homepage',
129-
'Author' => 'author',
130-
'AuthorURI' => 'author_homepage',
131-
'DetailsURI' => 'details_url', //Only for themes.
126+
'Version' => 'version',
127+
'PluginURI' => 'homepage',
128+
'ThemeURI' => 'homepage',
129+
'Author' => 'author',
130+
'AuthorURI' => 'author_homepage',
131+
'DetailsURI' => 'details_url', //Only for themes.
132+
'Depends' => 'depends', // plugin-dependencies plugin
133+
'Provides' => 'provides', // plugin-dependencies plugin
132134
);
133135
foreach($mapping as $headerField => $metaField){
134136
if ( array_key_exists($headerField, $packageInfo['header']) && !empty($packageInfo['header'][$headerField]) ){
@@ -145,7 +147,10 @@ public static function extractMetadata($zipFilename){
145147
}
146148

147149
if ( !empty($packageInfo['readme']) ){
148-
$mapping = array('requires', 'tested');
150+
$mapping = array(
151+
'requires',
152+
'tested',
153+
);
149154
foreach($mapping as $readmeField){
150155
if ( !empty($packageInfo['readme'][$readmeField]) ){
151156
$meta[$readmeField] = $packageInfo['readme'][$readmeField];

includes/Wpup/UpdateServer.php

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) {
1111
$serverDirectory = realpath(__DIR__ . '/../..');
1212
}
1313
if ( $serverUrl === null ) {
14-
//Default to the current URL minus the query and "index.php".
15-
$serverUrl = 'http://' . $_SERVER['HTTP_HOST'];
16-
$path = $_SERVER['SCRIPT_NAME'];
17-
if ( basename($path) === 'index.php' ) {
18-
$path = dirname($path) . '/';
19-
}
20-
$serverUrl .= $path;
14+
$serverUrl = self::guessServerUrl();
2115
}
2216

2317
$this->serverUrl = $serverUrl;
@@ -26,6 +20,56 @@ public function __construct($serverUrl = null, $serverDirectory = null) {
2620
$this->cache = new Wpup_FileCache($serverDirectory . '/cache');
2721
}
2822

23+
/**
24+
* Guess the Server Url based on the current request.
25+
*
26+
* Defaults to the current URL minus the query and "index.php".
27+
*
28+
* @static
29+
*
30+
* @return string Url
31+
*/
32+
public static function guessServerUrl() {
33+
$serverUrl = ( self::isSsl() ? 'https' : 'http' );
34+
$serverUrl .= '://' . $_SERVER['HTTP_HOST'];
35+
$path = $_SERVER['SCRIPT_NAME'];
36+
37+
if ( basename($path) === 'index.php' ) {
38+
$dir = dirname($path);
39+
if ( DIRECTORY_SEPARATOR === '/' ) {
40+
$path = $dir . '/';
41+
} else {
42+
// Fix Windows
43+
$path = str_replace('\\', '/', $dir);
44+
//Make sure there's a trailing slash.
45+
if ( substr($path, -1) !== '/' ) {
46+
$path .= '/';
47+
}
48+
}
49+
}
50+
51+
$serverUrl .= $path;
52+
return $serverUrl;
53+
}
54+
55+
/**
56+
* Determine if ssl is used.
57+
*
58+
* @see WP core - wp-includes/functions.php
59+
*
60+
* @return bool True if SSL, false if not used.
61+
*/
62+
public static function isSsl() {
63+
if ( isset($_SERVER['HTTPS']) ) {
64+
if ( $_SERVER['HTTPS'] == '1' || strtolower($_SERVER['HTTPS']) === 'on' ) {
65+
return true;
66+
}
67+
} elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
68+
return true;
69+
}
70+
return false;
71+
}
72+
2973
/**
3074
* Process an update API request.
3175
*
@@ -274,9 +318,13 @@ protected function filterLogInfo($columns) {
274318
*/
275319
protected function outputAsJson($response) {
276320
header('Content-Type: application/json');
277-
$output = json_encode($response);
278-
if ( function_exists('wsh_pretty_json') ) {
279-
$output = wsh_pretty_json($output);
321+
$output = '';
322+
if ( defined('JSON_PRETTY_PRINT') ) {
323+
$output = json_encode($response, JSON_PRETTY_PRINT);
324+
} elseif ( function_exists('wsh_pretty_json') ) {
325+
$output = wsh_pretty_json(json_encode($response));
326+
} else {
327+
$output = json_encode($response);
280328
}
281329
echo $output;
282330
}
@@ -353,10 +401,13 @@ protected function exitWithError($message = '', $httpStatus = 500) {
353401
* You can also set an argument to NULL to remove it.
354402
*
355403
* @param array $args An associative array of query arguments.
356-
* @param string $url The old URL.
404+
* @param string $url The old URL. Optional, defaults to the request url without query arguments.
357405
* @return string New URL.
358406
*/
359-
protected static function addQueryArg($args, $url) {
407+
protected static function addQueryArg($args, $url = null ) {
408+
if ( !isset($url) ) {
409+
$url = self::guessServerUrl();
410+
}
360411
if ( strpos($url, '?') !== false ) {
361412
$parts = explode('?', $url, 2);
362413
$base = $parts[0] . '?';

includes/extension-meta/extension-meta.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ public static function getPluginHeaders($fileContents) {
262262
'TextDomain' => 'Text Domain',
263263
'DomainPath' => 'Domain Path',
264264
'Network' => 'Network',
265+
'Depends' => 'Depends',
266+
'Provides' => 'Provides',
267+
265268
//Site Wide Only is deprecated in favor of Network.
266269
'_sitewide' => 'Site Wide Only',
267270
);
@@ -277,6 +280,16 @@ public static function getPluginHeaders($fileContents) {
277280

278281
//For backward compatibility by default Title is the same as Name.
279282
$headers['Title'] = $headers['Name'];
283+
284+
//"Depends" is a comma-separated list. Convert it to an array.
285+
if ( !empty($headers['Depends']) ){
286+
$headers['Depends'] = array_map('trim', explode(',', $headers['Depends']));
287+
}
288+
289+
//Same for "Provides"
290+
if ( !empty($headers['Provides']) ){
291+
$headers['Provides'] = array_map('trim', explode(',', $headers['Provides']));
292+
}
280293

281294
//If it doesn't have a name, it's probably not a plugin.
282295
if ( empty($headers['Name']) ){

0 commit comments

Comments
 (0)