Skip to content

Commit

Permalink
Merge changes from master
Browse files Browse the repository at this point in the history
  • Loading branch information
YahnisElsts committed Dec 15, 2014
2 parents a49dc35 + 338f373 commit f20a10f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
12 changes: 10 additions & 2 deletions includes/Wpup/FileCache.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php
/**
* A simple file-based cache.
*
* @internal Data is base64 encoded to avoid unserialization issues ('unserialize(): Error at offset') which
* could be caused by:
* - inconsistent line endings
* - unescaped quotes/slashes etc
* - miscounted unicode characters
*
* @see https://github.com/YahnisElsts/wp-update-server/pull/11
*/
class Wpup_FileCache implements Wpup_Cache {
protected $cacheDirectory;
Expand All @@ -18,7 +26,7 @@ public function __construct($cacheDirectory) {
public function get($key) {
$filename = $this->getCacheFilename($key);
if ( is_file($filename) && is_readable($filename) ) {
$cache = unserialize(file_get_contents($filename));
$cache = unserialize(base64_decode(file_get_contents($filename)));
if ( $cache['expiration_time'] < time() ) {
return null; //Cache expired.
} else {
Expand All @@ -41,7 +49,7 @@ public function set($key, $value, $expiration = 0) {
'expiration_time' => time() + $expiration,
'value' => $value,
);
file_put_contents($this->getCacheFilename($key), serialize($cache));
file_put_contents($this->getCacheFilename($key), base64_encode(serialize($cache)));
}

/**
Expand Down
21 changes: 13 additions & 8 deletions includes/Wpup/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getMetadata() {
*/
public static function fromArchive($filename, $slug = null, Wpup_Cache $cache = null) {
$modified = filemtime($filename);
$cacheKey = 'metadata-' . md5($filename . '|' . filesize($filename) . '|' . $modified);
$cacheKey = 'metadata-b64-' . $slug . '-' . md5($filename . '|' . filesize($filename) . '|' . $modified);
$metadata = null;

//Try the cache first.
Expand Down Expand Up @@ -123,12 +123,14 @@ public static function extractMetadata($zipFilename){
if ( isset($packageInfo['header']) && !empty($packageInfo['header']) ){
$mapping = array(
'Name' => 'name',
'Version' => 'version',
'PluginURI' => 'homepage',
'ThemeURI' => 'homepage',
'Author' => 'author',
'AuthorURI' => 'author_homepage',
'DetailsURI' => 'details_url', //Only for themes.
'Version' => 'version',
'PluginURI' => 'homepage',
'ThemeURI' => 'homepage',
'Author' => 'author',
'AuthorURI' => 'author_homepage',
'DetailsURI' => 'details_url', //Only for themes.
'Depends' => 'depends', // plugin-dependencies plugin
'Provides' => 'provides', // plugin-dependencies plugin
);
foreach($mapping as $headerField => $metaField){
if ( array_key_exists($headerField, $packageInfo['header']) && !empty($packageInfo['header'][$headerField]) ){
Expand All @@ -145,7 +147,10 @@ public static function extractMetadata($zipFilename){
}

if ( !empty($packageInfo['readme']) ){
$mapping = array('requires', 'tested');
$mapping = array(
'requires',
'tested',
);
foreach($mapping as $readmeField){
if ( !empty($packageInfo['readme'][$readmeField]) ){
$meta[$readmeField] = $packageInfo['readme'][$readmeField];
Expand Down
75 changes: 63 additions & 12 deletions includes/Wpup/UpdateServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) {
$serverDirectory = realpath(__DIR__ . '/../..');
}
if ( $serverUrl === null ) {
//Default to the current URL minus the query and "index.php".
$serverUrl = 'http://' . $_SERVER['HTTP_HOST'];
$path = $_SERVER['SCRIPT_NAME'];
if ( basename($path) === 'index.php' ) {
$path = dirname($path) . '/';
}
$serverUrl .= $path;
$serverUrl = self::guessServerUrl();
}

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

/**
* Guess the Server Url based on the current request.
*
* Defaults to the current URL minus the query and "index.php".
*
* @static
*
* @return string Url
*/
public static function guessServerUrl() {
$serverUrl = ( self::isSsl() ? 'https' : 'http' );
$serverUrl .= '://' . $_SERVER['HTTP_HOST'];
$path = $_SERVER['SCRIPT_NAME'];

if ( basename($path) === 'index.php' ) {
$dir = dirname($path);
if ( DIRECTORY_SEPARATOR === '/' ) {
$path = $dir . '/';
} else {
// Fix Windows
$path = str_replace('\\', '/', $dir);
//Make sure there's a trailing slash.
if ( substr($path, -1) !== '/' ) {
$path .= '/';
}
}
}

$serverUrl .= $path;
return $serverUrl;
}

/**
* Determine if ssl is used.
*
* @see WP core - wp-includes/functions.php
*
* @return bool True if SSL, false if not used.
*/
public static function isSsl() {
if ( isset($_SERVER['HTTPS']) ) {
if ( $_SERVER['HTTPS'] == '1' || strtolower($_SERVER['HTTPS']) === 'on' ) {
return true;
}
} elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
return true;
}
return false;
}

/**
* Process an update API request.
*
Expand Down Expand Up @@ -274,9 +318,13 @@ protected function filterLogInfo($columns) {
*/
protected function outputAsJson($response) {
header('Content-Type: application/json');
$output = json_encode($response);
if ( function_exists('wsh_pretty_json') ) {
$output = wsh_pretty_json($output);
$output = '';
if ( defined('JSON_PRETTY_PRINT') ) {
$output = json_encode($response, JSON_PRETTY_PRINT);
} elseif ( function_exists('wsh_pretty_json') ) {
$output = wsh_pretty_json(json_encode($response));
} else {
$output = json_encode($response);
}
echo $output;
}
Expand Down Expand Up @@ -353,10 +401,13 @@ protected function exitWithError($message = '', $httpStatus = 500) {
* You can also set an argument to NULL to remove it.
*
* @param array $args An associative array of query arguments.
* @param string $url The old URL.
* @param string $url The old URL. Optional, defaults to the request url without query arguments.
* @return string New URL.
*/
protected static function addQueryArg($args, $url) {
protected static function addQueryArg($args, $url = null ) {
if ( !isset($url) ) {
$url = self::guessServerUrl();
}
if ( strpos($url, '?') !== false ) {
$parts = explode('?', $url, 2);
$base = $parts[0] . '?';
Expand Down
13 changes: 13 additions & 0 deletions includes/extension-meta/extension-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ public static function getPluginHeaders($fileContents) {
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path',
'Network' => 'Network',
'Depends' => 'Depends',
'Provides' => 'Provides',

//Site Wide Only is deprecated in favor of Network.
'_sitewide' => 'Site Wide Only',
);
Expand All @@ -277,6 +280,16 @@ public static function getPluginHeaders($fileContents) {

//For backward compatibility by default Title is the same as Name.
$headers['Title'] = $headers['Name'];

//"Depends" is a comma-separated list. Convert it to an array.
if ( !empty($headers['Depends']) ){
$headers['Depends'] = array_map('trim', explode(',', $headers['Depends']));
}

//Same for "Provides"
if ( !empty($headers['Provides']) ){
$headers['Provides'] = array_map('trim', explode(',', $headers['Provides']));
}

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

0 comments on commit f20a10f

Please sign in to comment.