1<?php
2
3/*****************************************************************
4 * SearchPattern plugin for dokuwiki / Administration plugin
5 * Author : Matthieu Rioteau (matthieu<dot>rioteau<at>skf<dot>com)
6 * Creation : 2009-12-15
7 * Releases list :
8 * - ver 0.1 : 2009-12-23 : Initial release
9 * - ver 0.2 : 2010-07-05 : Add capability to search only inside certain pages/namespaces
10 * - ver 2013-06-16       : Add <br> to display a option block (leo); add new option 'dispheadl' (leo)
11 *****************************************************************/
12
13// must be run within Dokuwiki
14if(!defined('DOKU_INC')) die();
15
16if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17require_once(DOKU_PLUGIN.'admin.php');
18
19/**
20* All DokuWiki plugins need to extend the admin function
21* Need to inherit from this class
22*/
23class admin_plugin_searchpattern extends DokuWiki_Admin_Plugin {
24
25
26	/**
27	* Return some info about the plugin
28	*/
29	function getInfo(){
30		return array(
31				'author' => 'Matthieu Rioteau',
32				'email'  => 'matthieu<dot>rioteau<at>skf<dot>com',
33				'date'   => '2010-07-05',
34				'name'   => 'SearchPattern plugin / Admin / ver. 0.2',
35				'desc'   => 'Admin SearchPattern plugin behavior settings' ,
36				'url'    => 'http://wiki.splitbrain.org/plugin:searchpattern',
37				);
38	}
39
40	/**
41	* Return sort order for position in admin menu
42	*/
43	function getMenuSort() {
44		return 250;	//random number, don't know what to put here
45	}
46
47	/**
48	* Return prompt for admin menu
49	*/
50	function getMenuText($language) {
51		return $this->getLang('admin_menu_text');
52	}
53
54	/**
55	* Handle user request / triggered when the "save" button is pressed
56	*/
57	function handle() {
58		if(isset($_POST['do']) && isset($_POST['page'])){	//if we are able to identify the current action
59			if($_POST['do']=='admin' && $_POST['page']=='searchpattern'){	//if the action is the one we expect (saving parameters of searchpattern)
60				$saveconf = $this->saveConf($def_values);	//run the configuration save function
61				msg($this->getLang($saveconf).(($saveconf == 'save_conf_warn')?$def_values:''),$this->getLang($saveconf.'_lvl'));	//display a status message
62			}
63		}
64	}
65
66	/**
67	* Output appropriate html inside the admin page
68	*/
69	function html() {
70		global $lang;
71		echo '<h1>'.$this->getLang('main_admin_title').'</h1>';	//display main title
72		echo '<form action="" method="post">';	//create form
73		echo '<input type="hidden" name="do" value="admin" />';	//for check when save
74		echo '<input type="hidden" name="page" value="searchpattern" />';	//for check when save
75		$form_fields = $this->getAuthorized();	//get the authorized parameters and their possible values
76		foreach($form_fields as $auth_param=>$auth_values){	//for each parameter
77			echo '<h4>'.$this->getLang($auth_param.'_admin_title').'</h4>';	//display a subtitle
78			foreach($auth_values as $auth_value){	//for each possible value of the parameter
79				echo '<input type="radio" name="'.$auth_param.'" value="'.$auth_value.'" '.((($this->getConf($auth_param))==$auth_value)?'checked ':'').'/>&nbsp;'.$this->getLang($auth_value).'<br />';	//diaply a radio button
80			}
81			echo '<br />';
82		}
83		echo '<br /><input type="submit" value="'.$lang['btn_save'].'" class="button" />';	//display the save button
84		echo '</form>';
85	}
86
87	/**
88	*	Return the authorized parameters for the plugin and their respective authorized values
89	*/
90	function getAuthorized()
91	{
92		return array(
93					'ndqerr'=>array('nocatch','error','warning','nowarn'),
94					'regerr'=>array('nocatch','error'),
95					'option'=>array('disp','nodisp'),
96					'badopt'=>array('nocatch','error','warning','nowarn'),
97					'ignopt'=>array('warning','nowarn'),
98					'dispheadl'=>array('disp','nodisp')
99					);
100	}
101
102	/**
103	* Save the configuration in the appropriate file and return a status
104	* Function parameter will be the name(s) of the parameter(s) that has/have been set to default value(s)
105	*/
106	function saveConf(&$default_values)
107	{
108		$conf_file = '<?php'."\n\n".'//This file is autogenerated. You can change configuration from admin menu.'."\n".'//For more information about possible options, please consult website.'."\n\n";	//create the string that will be flushed in the conf file
109			$default_values = '';	//store parameter(s) set to default
110		$auth_params = $this->getAuthorized();	//get authorized parameters
111		foreach($auth_params as $auth_param => $auth_values)	//for each authorized parameter
112		{
113			$value_is_ok = false;	//indicates if the new value is set AND correct
114			if(isset($_POST[$auth_param])){	//if a new value is set
115				foreach($auth_values as $auth_value)	//check if the new value is authorized
116				{
117					if($_POST[$auth_param] == $auth_value)	//if yes
118					{
119						$value_is_ok = true;	//value is ok
120					}
121				}
122			}
123			if($value_is_ok){	//if value is of
124				$conf_file .= '$conf[\''.$auth_param.'\'] = \''.$_POST[$auth_param].'\';'."\n";	//add it to the configuration
125			}
126			else{	//if not ok
127				$conf_file .= '$conf[\''.$auth_param.'\'] = \''.$auth_params[$auth_param][0].'\';'."\n";	//set the default value inside the configuration
128				$default_values .= ' : '.$auth_param;	//and store the parameter name
129			}
130		}
131		$conf_file .= "\n\n".'?>';	//end configuration string
132		if($file_handle = fopen(realpath(dirname(__FILE__)).'/conf/default.php','w')){	//try to open the conf file
133			if(fwrite($file_handle,$conf_file)){	//and try to write the new configuration
134				if($default_values == ''){	//if all parameters were properly defined
135					fclose($file_handle);	//close the file to flush
136					return 'save_conf_ok';	//return
137				}
138				else{	//if some parameters were not/bad defined
139					fclose($file_handle);	//close file to flush
140					return 'save_conf_warn';	//return
141				}
142			}
143			else{	//if it wasn't possible to write in the file
144				fclose($file_handle);	//close file
145				return 'save_conf_nowr';	//return
146			}
147		}
148		else{	//if it wasn't possible to open the file
149			return 'save_conf_noop';	//return
150		}
151	}
152}
153
154?>
155