Commit b5aca36a by pupi1985

Added backwards compatible support to handle local metadata.json files in plugins and themes

parent a6277bc8
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
File: qa-include/Q2A/Util/Metadata.php
Description: Some useful metadata handling stuff
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
class Q2A_Util_Metadata
{
const METADATA_FILE_JSON = 'metadata.json';
/**
* Return an array from a JSON string
* @param mixed $json The JSON string to turn into an array
* @return array Always return an array containing the decoded JSON or an empty array in case the
* $json parameter is not a valid JSON string
*/
private function getArrayFromJson($json)
{
$result = json_decode($json, true);
return is_array($result) ? $result : array();
}
/**
* Fetch metadata information from an addon path
* @param string $path Path to the addon
* @return array The metadata fetched from the metadata.json file in the addon path
*/
public function fetchFromAddonPath($path)
{
$metadataFile = $path . self::METADATA_FILE_JSON;
if (is_file($metadataFile)) {
$content = file_get_contents($metadataFile);
return $this->getArrayFromJson($content);
}
return array();
}
}
\ No newline at end of file
......@@ -1006,9 +1006,14 @@
qa_optionfield_make_select($optionfield, $themeoptions, $value, 'Classic');
// limit theme parsing to first 8kB
$contents = file_get_contents(QA_THEME_DIR.$value.'/qa-styles.css', false, NULL, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Theme');
$metadataUtil = new Q2A_Util_Metadata();
$themedirectory = QA_THEME_DIR . $value . '/';
$metadata = $metadataUtil->fetchFromAddonPath($themedirectory);
if (empty($metadata)) {
// limit theme parsing to first 8kB
$contents = file_get_contents($themedirectory . 'qa-styles.css', false, NULL, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Theme');
}
if (strlen(@$metadata['version']))
$namehtml = 'v'.qa_html($metadata['version']);
......
......@@ -100,14 +100,18 @@
$showpluginforms = true;
if (count($pluginfiles)) {
$metadataUtil = new Q2A_Util_Metadata();
foreach ($pluginfiles as $pluginindex => $pluginfile) {
$plugindirectory = dirname($pluginfile).'/';
$hash = qa_admin_plugin_directory_hash($plugindirectory);
$showthisform = $showpluginforms && (qa_get('show') == $hash);
// limit plugin parsing to first 8kB
$contents = file_get_contents($pluginfile, false, NULL, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin');
$metadata = $metadataUtil->fetchFromAddonPath($plugindirectory);
if (empty($metadata)) {
// limit plugin parsing to first 8kB
$contents = file_get_contents($pluginfile, false, NULL, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin');
}
if (isset($metadata['name']) && strlen($metadata['name']))
$namehtml = qa_html($metadata['name']);
......
......@@ -288,12 +288,14 @@
}
/**
* Retrieve metadata information from the $contents of a qa-theme.php or qa-plugin.php file, specified by $type ('Plugin' or 'Theme').
* If $versiononly is true, only min version metadata is parsed.
* Name, Description, Min Q2A & Min PHP are not currently used by themes.
*
* @deprecated Deprecated from 1.7; Q2A_Util_Metadata class and metadata.json files should be used instead
*/
function qa_addon_metadata($contents, $type, $versiononly=false)
/*
Retrieve metadata information from the $contents of a qa-theme.php or qa-plugin.php file, specified by $type ('Plugin' or 'Theme').
If $versiononly is true, only min version metadata is parsed.
Name, Description, Min Q2A & Min PHP are not currently used by themes.
*/
{
$fields = array(
'min_q2a' => 'Minimum Question2Answer Version',
......@@ -334,10 +336,18 @@
$pluginfiles = glob(QA_PLUGIN_DIR.'*/qa-plugin.php');
$metadataUtil = new Q2A_Util_Metadata();
foreach ($pluginfiles as $pluginfile) {
// limit plugin parsing to first 8kB
$contents = file_get_contents($pluginfile, false, NULL, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin', true);
// these variables are utilized in the qa_register_plugin_* functions
$qa_plugin_directory = dirname($pluginfile) . '/';
$qa_plugin_urltoroot = substr($qa_plugin_directory, strlen(QA_BASE_DIR));
$metadata = $metadataUtil->fetchFromAddonPath($qa_plugin_directory);
if (empty($metadata)) {
// limit plugin parsing to first 8kB
$contents = file_get_contents($pluginfile, false, NULL, -1, 8192);
$metadata = qa_addon_metadata($contents, 'Plugin', true);
}
// skip plugin which requires a later version of Q2A
if (isset($metadata['min_q2a']) && qa_qa_version_below($metadata['min_q2a']))
......@@ -346,10 +356,6 @@
if (isset($metadata['min_php']) && qa_php_version_below($metadata['min_php']))
continue;
// these variables are utilized in the qa_register_plugin_* functions
$qa_plugin_directory = dirname($pluginfile).'/';
$qa_plugin_urltoroot = substr($qa_plugin_directory, strlen(QA_BASE_DIR));
require_once $pluginfile;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment