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
<?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';
	/**
	 * Fetch metadata information from an addon path
	 * @param string $path Directory the addon is in (without trailing slash)
	 * @return array The metadata fetched from the JSON file, or an empty array otherwise
	 */
	public function fetchFromAddonPath($path)
	{
		$metadataFile = $path . '/' . self::METADATA_FILE_JSON;
		if (!is_file($metadataFile)) {
			return array();
		}
		$content = file_get_contents($metadataFile);
		return $this->getArrayFromJson($content);
	}
	/**
	 * Fetch metadata information from an URL
	 * @param string $url URL linking to a metadata.json file
	 * @return array The metadata fetched from the file
	 */
	public function fetchFromUrl($url, $type='Plugin')
	{
		$contents = qa_retrieve_url($url);
		$metadata = $this->getArrayFromJson($contents);
		// fall back to old metadata format
		if (empty($metadata)) {
			$metadata = qa_addon_metadata($contents, $type);
		}
		return $metadata;
	}
	/**
	 * 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();
	}
}