qa-page-admin-plugins.php 6.66 KB
Newer Older
Gideon Greenspan committed
1
<?php
Scott Vivian committed
2

Gideon Greenspan committed
3 4 5 6 7
/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-page-admin-plugins.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	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;
	}

	require_once QA_INCLUDE_DIR.'qa-app-admin.php';

Scott Vivian committed
34

Gideon Greenspan committed
35 36 37 38
//	Check admin privileges

	if (!qa_admin_check_privileges($qa_content))
		return $qa_content;
Scott Vivian committed
39 40


Gideon Greenspan committed
41
//	Map modules with options to their containing plugins
Scott Vivian committed
42

Scott Vivian committed
43
	$pluginoptionmodules = array();
Scott Vivian committed
44

Scott Vivian committed
45 46
	$tables = qa_db_list_tables_lc();
	$moduletypes = qa_list_module_types();
Scott Vivian committed
47

Gideon Greenspan committed
48
	foreach ($moduletypes as $type) {
Scott Vivian committed
49
		$modules = qa_list_modules($type);
Scott Vivian committed
50

Gideon Greenspan committed
51
		foreach ($modules as $name) {
Scott Vivian committed
52
			$module = qa_load_module($type, $name);
Scott Vivian committed
53

Gideon Greenspan committed
54
			if (method_exists($module, 'admin_form')) {
Scott Vivian committed
55 56
				$info = qa_get_module_info($type, $name);
				$pluginoptionmodules[$info['directory']][] = array(
Gideon Greenspan committed
57 58 59
					'type' => $type,
					'name' => $name,
				);
Gideon Greenspan committed
60 61 62 63 64 65
			}
		}
	}


//	Prepare content for theme
Scott Vivian committed
66

Scott Vivian committed
67
	$qa_content = qa_content_prepare();
Gideon Greenspan committed
68

Scott Vivian committed
69
	$qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/plugins_title');
Scott Vivian committed
70

Scott Vivian committed
71
	$qa_content['error'] = qa_admin_page_error();
Scott Vivian committed
72

Scott Vivian committed
73
	$qa_content['script_rel'][] = 'qa-content/qa-admin.js?'.QA_VERSION;
Gideon Greenspan committed
74

Scott Vivian committed
75
	$pluginfiles = glob(QA_PLUGIN_DIR.'*/qa-plugin.php');
Scott Vivian committed
76

Gideon Greenspan committed
77
	foreach ($moduletypes as $type) {
Scott Vivian committed
78
		$modules = qa_load_modules_with($type, 'init_queries');
Gideon Greenspan committed
79

Gideon Greenspan committed
80
		foreach ($modules as $name => $module) {
Scott Vivian committed
81
			$queries = $module->init_queries($tables);
Scott Vivian committed
82

Gideon Greenspan committed
83 84 85
			if (!empty($queries)) {
				if (qa_is_http_post())
					qa_redirect('install');
Scott Vivian committed
86

Scott Vivian committed
87 88
				else {
					$qa_content['error'] = strtr(qa_lang_html('admin/module_x_database_init'), array(
Gideon Greenspan committed
89 90
						'^1' => qa_html($name),
						'^2' => qa_html($type),
Gideon Greenspan committed
91 92
						'^3' => '<a href="'.qa_path_html('install').'">',
						'^4' => '</a>',
Gideon Greenspan committed
93
					));
Scott Vivian committed
94
				}
Gideon Greenspan committed
95 96 97
			}
		}
	}
Scott Vivian committed
98

Gideon Greenspan committed
99
	if ( qa_is_http_post() && !qa_check_form_security_code('admin/plugins', qa_post_text('qa_form_security_code')) ) {
Scott Vivian committed
100 101 102 103 104
		$qa_content['error'] = qa_lang_html('misc/form_security_reload');
		$showpluginforms = false;
	}
	else
		$showpluginforms = true;
Gideon Greenspan committed
105 106 107

	if (count($pluginfiles)) {
		foreach ($pluginfiles as $pluginindex => $pluginfile) {
Scott Vivian committed
108 109 110
			$plugindirectory = dirname($pluginfile).'/';
			$hash = qa_admin_plugin_directory_hash($plugindirectory);
			$showthisform = $showpluginforms && (qa_get('show') == $hash);
Scott Vivian committed
111

112 113 114
			// limit plugin parsing to first 8kB
			$contents = file_get_contents($pluginfile, false, NULL, -1, 8192);
			$metadata = qa_addon_metadata($contents, 'Plugin');
Scott Vivian committed
115

116
			if (isset($metadata['name']) && strlen($metadata['name']))
Scott Vivian committed
117
				$namehtml = qa_html($metadata['name']);
Gideon Greenspan committed
118
			else
Scott Vivian committed
119
				$namehtml = qa_lang_html('admin/unnamed_plugin');
Scott Vivian committed
120

121
			if (isset($metadata['uri']) && strlen($metadata['uri']))
Scott Vivian committed
122
				$namehtml = '<a href="'.qa_html($metadata['uri']).'">'.$namehtml.'</a>';
Scott Vivian committed
123

Scott Vivian committed
124
			$namehtml = '<b>'.$namehtml.'</b>';
Scott Vivian committed
125

126 127
			$metaver = isset($metadata['version']) && strlen($metadata['version']);
			if ($metaver)
Scott Vivian committed
128
				$namehtml .= ' v'.qa_html($metadata['version']);
Scott Vivian committed
129

130
			if (isset($metadata['author']) && strlen($metadata['author'])) {
Scott Vivian committed
131
				$authorhtml = qa_html($metadata['author']);
Scott Vivian committed
132

133
				if (isset($metadata['author_uri']) && strlen($metadata['author_uri']))
Scott Vivian committed
134
					$authorhtml = '<a href="'.qa_html($metadata['author_uri']).'">'.$authorhtml.'</a>';
Scott Vivian committed
135

Scott Vivian committed
136
				$authorhtml = qa_lang_html_sub('main/by_x', $authorhtml);
Scott Vivian committed
137

Scott Vivian committed
138 139 140
			}
			else
				$authorhtml = '';
Scott Vivian committed
141

142
			if ($metaver && isset($metadata['update']) && strlen($metadata['update'])) {
Scott Vivian committed
143
				$elementid = 'version_check_'.md5($plugindirectory);
Scott Vivian committed
144

Scott Vivian committed
145
				$updatehtml = '(<span id="'.$elementid.'">...</span>)';
Scott Vivian committed
146

Scott Vivian committed
147
				$qa_content['script_onloads'][] = array(
148
					"qa_version_check(".qa_js($metadata['update']).", 'Plugin', ".qa_js($metadata['version'], true).", ".qa_js($elementid).");"
Gideon Greenspan committed
149 150
				);

Scott Vivian committed
151 152 153
			}
			else
				$updatehtml = '';
Scott Vivian committed
154

155
			if (isset($metadata['description']))
Scott Vivian committed
156
				$deschtml = qa_html($metadata['description']);
Gideon Greenspan committed
157
			else
Scott Vivian committed
158
				$deschtml = '';
Scott Vivian committed
159

Gideon Greenspan committed
160
			if (isset($pluginoptionmodules[$plugindirectory]) && !$showthisform)
Scott Vivian committed
161
				$deschtml .= (strlen($deschtml) ? ' - ' : '').'<a href="'.
Gideon Greenspan committed
162
					qa_admin_plugin_options_path($plugindirectory).'">'.qa_lang_html('admin/options').'</a>';
Scott Vivian committed
163

Scott Vivian committed
164
			$pluginhtml = $namehtml.' '.$authorhtml.' '.$updatehtml.'<br>'.$deschtml.(strlen($deschtml) ? '<br>' : '').
Gideon Greenspan committed
165
				'<small style="color:#666">'.qa_html($plugindirectory).'</small>';
Scott Vivian committed
166

Gideon Greenspan committed
167
			if (qa_qa_version_below(@$metadata['min_q2a']))
Scott Vivian committed
168
				$pluginhtml = '<strike style="color:#999">'.$pluginhtml.'</strike><br><span style="color:#f00">'.
Gideon Greenspan committed
169
					qa_lang_html_sub('admin/requires_q2a_version', qa_html($metadata['min_q2a'])).'</span>';
Scott Vivian committed
170

Gideon Greenspan committed
171
			elseif (qa_php_version_below(@$metadata['min_php']))
Scott Vivian committed
172
				$pluginhtml = '<strike style="color:#999">'.$pluginhtml.'</strike><br><span style="color:#f00">'.
Gideon Greenspan committed
173
					qa_lang_html_sub('admin/requires_php_version', qa_html($metadata['min_php'])).'</span>';
Scott Vivian committed
174

Scott Vivian committed
175
			$qa_content['form_plugin_'.$pluginindex] = array(
Gideon Greenspan committed
176
				'tags' => 'id="'.qa_html($hash).'"',
Gideon Greenspan committed
177 178 179 180 181 182 183
				'style' => 'tall',
				'fields' => array(
					array(
						'type' => 'custom',
						'html' => $pluginhtml,
					)
				),
Gideon Greenspan committed
184
			);
Scott Vivian committed
185

Gideon Greenspan committed
186 187
			if ($showthisform && isset($pluginoptionmodules[$plugindirectory]))
				foreach ($pluginoptionmodules[$plugindirectory] as $pluginoptionmodule) {
Scott Vivian committed
188 189
					$type = $pluginoptionmodule['type'];
					$name = $pluginoptionmodule['name'];
Scott Vivian committed
190

Scott Vivian committed
191
					$module = qa_load_module($type, $name);
Scott Vivian committed
192

Scott Vivian committed
193
					$form = $module->admin_form($qa_content);
Gideon Greenspan committed
194 195

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

Gideon Greenspan committed
198
					if (!isset($form['style']))
Scott Vivian committed
199
						$form['style'] = 'tall';
Scott Vivian committed
200

Scott Vivian committed
201
					$form['boxed'] = true;
Scott Vivian committed
202

Scott Vivian committed
203
					$form['hidden']['qa_form_security_code'] = qa_get_form_security_code('admin/plugins');
Scott Vivian committed
204

Scott Vivian committed
205
					$qa_content['form_plugin_options'] = $form;
Gideon Greenspan committed
206
				}
Gideon Greenspan committed
207 208 209

		}
	}
Scott Vivian committed
210

Scott Vivian committed
211
	$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Gideon Greenspan committed
212

Scott Vivian committed
213

Gideon Greenspan committed
214
	return $qa_content;
Scott Vivian committed
215

Gideon Greenspan committed
216 217 218 219

/*
	Omit PHP closing tag to help avoid accidental output
*/