qa-page-admin-plugins.php 6.37 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

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

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

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

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

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


//	Prepare content for theme
Scott Vivian committed
66

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

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

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

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

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

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

Gideon Greenspan committed
80 81
		foreach ($modules as $name => $module) {
			$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

Gideon Greenspan committed
87 88 89 90
				else
					$qa_content['error']=strtr(qa_lang_html('admin/module_x_database_init'), array(
						'^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 94 95 96
					));
			}
		}
	}
Scott Vivian committed
97

Gideon Greenspan committed
98 99 100 101 102 103 104 105
	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;

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

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

Gideon Greenspan committed
114 115 116 117
			if (strlen(@$metadata['name']))
				$namehtml=qa_html($metadata['name']);
			else
				$namehtml=qa_lang_html('admin/unnamed_plugin');
Scott Vivian committed
118

Gideon Greenspan committed
119
			if (strlen(@$metadata['uri']))
Gideon Greenspan committed
120
				$namehtml='<a href="'.qa_html($metadata['uri']).'">'.$namehtml.'</a>';
Scott Vivian committed
121

Gideon Greenspan committed
122
			$namehtml='<b>'.$namehtml.'</b>';
Scott Vivian committed
123

Gideon Greenspan committed
124 125
			if (strlen(@$metadata['version']))
				$namehtml.=' v'.qa_html($metadata['version']);
Scott Vivian committed
126

Gideon Greenspan committed
127 128
			if (strlen(@$metadata['author'])) {
				$authorhtml=qa_html($metadata['author']);
Scott Vivian committed
129

Gideon Greenspan committed
130
				if (strlen(@$metadata['author_uri']))
Gideon Greenspan committed
131
					$authorhtml='<a href="'.qa_html($metadata['author_uri']).'">'.$authorhtml.'</a>';
Scott Vivian committed
132

Gideon Greenspan committed
133
				$authorhtml=qa_lang_html_sub('main/by_x', $authorhtml);
Scott Vivian committed
134

Gideon Greenspan committed
135 136
			} else
				$authorhtml='';
Scott Vivian committed
137

Gideon Greenspan committed
138 139
			if (strlen(@$metadata['version']) && strlen(@$metadata['update'])) {
				$elementid='version_check_'.md5($plugindirectory);
Scott Vivian committed
140

Gideon Greenspan committed
141
				$updatehtml='(<span id="'.$elementid.'">...</span>)';
Scott Vivian committed
142

Gideon Greenspan committed
143
				$qa_content['script_onloads'][]=array(
144
					"qa_version_check(".qa_js($metadata['update']).", 'Plugin', ".qa_js($metadata['version'], true).", ".qa_js($elementid).");"
Gideon Greenspan committed
145 146 147 148
				);

			} else
				$updatehtml='';
Scott Vivian committed
149

Gideon Greenspan committed
150 151 152 153
			if (strlen(@$metadata['description']))
				$deschtml=qa_html($metadata['description']);
			else
				$deschtml='';
Scott Vivian committed
154

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

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

Gideon Greenspan committed
162
			if (qa_qa_version_below(@$metadata['min_q2a']))
Gideon Greenspan committed
163 164
				$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>';
Scott Vivian committed
165

Gideon Greenspan committed
166
			elseif (qa_php_version_below(@$metadata['min_php']))
Gideon Greenspan committed
167 168
				$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>';
Scott Vivian committed
169

Gideon Greenspan committed
170
			$qa_content['form_plugin_'.$pluginindex]=array(
Gideon Greenspan committed
171
				'tags' => 'id="'.qa_html($hash).'"',
Gideon Greenspan committed
172 173 174 175 176 177 178
				'style' => 'tall',
				'fields' => array(
					array(
						'type' => 'custom',
						'html' => $pluginhtml,
					)
				),
Gideon Greenspan committed
179
			);
Scott Vivian committed
180

Gideon Greenspan committed
181 182 183 184
			if ($showthisform && isset($pluginoptionmodules[$plugindirectory]))
				foreach ($pluginoptionmodules[$plugindirectory] as $pluginoptionmodule) {
					$type=$pluginoptionmodule['type'];
					$name=$pluginoptionmodule['name'];
Scott Vivian committed
185

Gideon Greenspan committed
186
					$module=qa_load_module($type, $name);
Scott Vivian committed
187

Gideon Greenspan committed
188 189 190
					$form=$module->admin_form($qa_content);

					if (!isset($form['tags']))
Gideon Greenspan committed
191
						$form['tags']='method="post" action="'.qa_admin_plugin_options_path($plugindirectory).'"';
Scott Vivian committed
192

Gideon Greenspan committed
193 194
					if (!isset($form['style']))
						$form['style']='tall';
Scott Vivian committed
195

Gideon Greenspan committed
196
					$form['boxed']=true;
Scott Vivian committed
197

Gideon Greenspan committed
198
					$form['hidden']['qa_form_security_code']=qa_get_form_security_code('admin/plugins');
Scott Vivian committed
199

Gideon Greenspan committed
200
					$qa_content['form_plugin_options']=$form;
Gideon Greenspan committed
201
				}
Gideon Greenspan committed
202 203 204

		}
	}
Scott Vivian committed
205

Gideon Greenspan committed
206 207
	$qa_content['navigation']['sub']=qa_admin_sub_navigation();

Scott Vivian committed
208

Gideon Greenspan committed
209
	return $qa_content;
Scott Vivian committed
210

Gideon Greenspan committed
211 212 213 214

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