admin-plugins.php 6.81 KB
Newer Older
Scott committed
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
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-page-admin-plugins.php
	Description: Controller for admin page listing plugins and showing their options


	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

Scott committed
28
	require_once QA_INCLUDE_DIR.'app/admin.php';
Scott committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51


//	Check admin privileges

	if (!qa_admin_check_privileges($qa_content))
		return $qa_content;


//	Map modules with options to their containing plugins

	$pluginoptionmodules = array();

	$tables = qa_db_list_tables();
	$moduletypes = qa_list_module_types();

	foreach ($moduletypes as $type) {
		$modules = qa_list_modules($type);

		foreach ($modules as $name) {
			$module = qa_load_module($type, $name);

			if (method_exists($module, 'admin_form')) {
				$info = qa_get_module_info($type, $name);
52 53
				$dir = rtrim($info['directory'], '/');
				$pluginoptionmodules[$dir][] = array(
Scott committed
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
					'type' => $type,
					'name' => $name,
				);
			}
		}
	}


//	Prepare content for theme

	$qa_content = qa_content_prepare();

	$qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/plugins_title');

	$qa_content['error'] = qa_admin_page_error();

	$qa_content['script_rel'][] = 'qa-content/qa-admin.js?'.QA_VERSION;

	$pluginfiles = glob(QA_PLUGIN_DIR.'*/qa-plugin.php');

	foreach ($moduletypes as $type) {
		$modules = qa_load_modules_with($type, 'init_queries');

		foreach ($modules as $name => $module) {
			$queries = $module->init_queries($tables);

			if (!empty($queries)) {
				if (qa_is_http_post())
					qa_redirect('install');

				else {
					$qa_content['error'] = strtr(qa_lang_html('admin/module_x_database_init'), array(
						'^1' => qa_html($name),
						'^2' => qa_html($type),
						'^3' => '<a href="'.qa_path_html('install').'">',
						'^4' => '</a>',
					));
				}
			}
		}
	}

	if ( qa_is_http_post() && !qa_check_form_security_code('admin/plugins', qa_post_text('qa_form_security_code')) ) {
		$qa_content['error'] = qa_lang_html('misc/form_security_reload');
		$showpluginforms = false;
	}
	else
		$showpluginforms = true;

103
	if (!empty($pluginfiles)) {
104
		$metadataUtil = new Q2A_Util_Metadata();
105
		$sortedPluginFiles = array();
106

107
		foreach ($pluginfiles as $pluginFile) {
108
			$metadata = $metadataUtil->fetchFromAddonPath(dirname($pluginFile));
109 110
			if (empty($metadata)) {
				// limit plugin parsing to first 8kB
111
				$contents = file_get_contents($pluginFile, false, null, -1, 8192);
112 113
				$metadata = qa_addon_metadata($contents, 'Plugin');
			}
114 115 116 117 118 119
			$metadata['name'] = isset($metadata['name']) && !empty($metadata['name'])
				? qa_html($metadata['name'])
				: qa_lang_html('admin/unnamed_plugin')
			;
			$sortedPluginFiles[$pluginFile] = $metadata;
		}
120

121
		qa_sort_by($sortedPluginFiles, 'name');
122

123 124 125
		$pluginIndex = -1;
		foreach ($sortedPluginFiles as $pluginFile => $metadata) {
			$pluginIndex++;
126
			$plugindirectory = dirname($pluginFile);
127 128
			$hash = qa_admin_plugin_directory_hash($plugindirectory);
			$showthisform = $showpluginforms && (qa_get('show') == $hash);
Scott committed
129

130
			$namehtml = $metadata['name'];
Scott committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

			if (isset($metadata['uri']) && strlen($metadata['uri']))
				$namehtml = '<a href="'.qa_html($metadata['uri']).'">'.$namehtml.'</a>';

			$namehtml = '<b>'.$namehtml.'</b>';

			$metaver = isset($metadata['version']) && strlen($metadata['version']);
			if ($metaver)
				$namehtml .= ' v'.qa_html($metadata['version']);

			if (isset($metadata['author']) && strlen($metadata['author'])) {
				$authorhtml = qa_html($metadata['author']);

				if (isset($metadata['author_uri']) && strlen($metadata['author_uri']))
					$authorhtml = '<a href="'.qa_html($metadata['author_uri']).'">'.$authorhtml.'</a>';

				$authorhtml = qa_lang_html_sub('main/by_x', $authorhtml);

			}
			else
				$authorhtml = '';

Scott committed
153
			if ($metaver && isset($metadata['update_uri']) && strlen($metadata['update_uri'])) {
Scott committed
154 155 156 157 158
				$elementid = 'version_check_'.md5($plugindirectory);

				$updatehtml = '(<span id="'.$elementid.'">...</span>)';

				$qa_content['script_onloads'][] = array(
Scott committed
159
					"qa_version_check(".qa_js($metadata['update_uri']).", ".qa_js($metadata['version'], true).", ".qa_js($elementid).");"
Scott committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
				);

			}
			else
				$updatehtml = '';

			if (isset($metadata['description']))
				$deschtml = qa_html($metadata['description']);
			else
				$deschtml = '';

			if (isset($pluginoptionmodules[$plugindirectory]) && !$showthisform)
				$deschtml .= (strlen($deschtml) ? ' - ' : '').'<a href="'.
					qa_admin_plugin_options_path($plugindirectory).'">'.qa_lang_html('admin/options').'</a>';

			$pluginhtml = $namehtml.' '.$authorhtml.' '.$updatehtml.'<br>'.$deschtml.(strlen($deschtml) ? '<br>' : '').
176
				'<small style="color:#666">'.qa_html($plugindirectory).'/</small>';
Scott committed
177 178 179 180 181 182 183 184 185

			if (qa_qa_version_below(@$metadata['min_q2a']))
				$pluginhtml = '<strike style="color:#999">'.$pluginhtml.'</strike><br><span style="color:#f00">'.
					qa_lang_html_sub('admin/requires_q2a_version', qa_html($metadata['min_q2a'])).'</span>';

			elseif (qa_php_version_below(@$metadata['min_php']))
				$pluginhtml = '<strike style="color:#999">'.$pluginhtml.'</strike><br><span style="color:#f00">'.
					qa_lang_html_sub('admin/requires_php_version', qa_html($metadata['min_php'])).'</span>';

186
			$qa_content['form_plugin_'.$pluginIndex] = array(
Scott committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
				'tags' => 'id="'.qa_html($hash).'"',
				'style' => 'tall',
				'fields' => array(
					array(
						'type' => 'custom',
						'html' => $pluginhtml,
					)
				),
			);

			if ($showthisform && isset($pluginoptionmodules[$plugindirectory]))
				foreach ($pluginoptionmodules[$plugindirectory] as $pluginoptionmodule) {
					$type = $pluginoptionmodule['type'];
					$name = $pluginoptionmodule['name'];

					$module = qa_load_module($type, $name);

					$form = $module->admin_form($qa_content);

					if (!isset($form['tags']))
						$form['tags'] = 'method="post" action="'.qa_admin_plugin_options_path($plugindirectory).'"';

					if (!isset($form['style']))
						$form['style'] = 'tall';

					$form['boxed'] = true;

					$form['hidden']['qa_form_security_code'] = qa_get_form_security_code('admin/plugins');

					$qa_content['form_plugin_options'] = $form;
				}

		}
	}

	$qa_content['navigation']['sub'] = qa_admin_sub_navigation();


	return $qa_content;


/*
	Omit PHP closing tag to help avoid accidental output
Gideon Greenspan committed
230
*/