1<?php
2/**
3 * Setting up the sympa-soap authentication admin plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     David Pepin <sympa-authors AT cru.fr>
7 */
8
9if(!defined('DOKU')) define('DOKU',realpath(dirname(__FILE__).'/../../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU.'lib/plugins/');
11require_once(DOKU_PLUGIN.'admin.php');
12if(!defined('DOKU_CONFIGLANG')) define('DOKU_CONFIGLANG', realpath(dirname(__FILE__).'/../config/lang/'));
13if(!defined('DOKU_SYMPALANG')) define('DOKU_SYMPALANG', realpath(dirname(__FILE__) .'/lang/'));
14
15
16
17
18/**
19 * All DokuWiki plugins to extend the admin function
20 * need to inherit from this class
21 */
22class admin_plugin_sympaauth extends DokuWiki_Admin_Plugin {
23
24
25  /**
26   * Return some info about the plugin
27   *
28   **/
29  function getInfo(){
30
31      return array(
32		   'author' => 'David Pepin',
33		   'email'  => 'sympa-authors AT cru.fr',
34		   'date'   => '2008/02/15',
35		   'version' => '1.3',
36		   'name'   => 'sympaauth',
37		   'desc'   => 'Plugin to grant authentication and access rights from a Soap Sympa server',
38		   'url'    => 'http://www.sympa.org',
39		   );
40  }
41
42
43  /**
44   * handle user request
45   */
46  function handle() {
47    if ($_REQUEST['action_'] == 'install') {
48      $this->install();
49    }
50    elseif ($_REQUEST['action_'] == 'uninstall') {
51      $this->uninstall();
52    }
53  }
54
55
56  /**
57   * Output appropriate html
58   *
59   */
60  function html() {
61    global $ID;
62    // Display the text about this plugin from "sympaauth/lang/$lang/intro.txt"
63    print $this->locale_xhtml('intro');
64
65    // If not installed, it displays the install menu
66    // if installed, it displays the uninstall menu
67    ptln('<form action_="'.wl($ID).'" method="post">');
68    if($this->is_installed()) {
69      if($this->is_used()) {
70	ptln('<p> ' .$this->getLang('is_used'). '</p>');
71      }
72      else {
73	ptln('<p> '.$this->getLang('uninstall') .' </p>');
74	ptln('<p> <input type="hidden" name="action_" value="uninstall" />');
75	ptln('    <input type="submit" name="submit" class="button" value="'.$this->getLang('btn_uninstall').'" /> </p>');
76      }
77    }
78    else {
79      ptln('<p> '.$this->getLang('install') .' </p>');
80      ptln('<p> <input type="hidden" name="action_" value="install" />');
81      ptln('    <input type="submit" name="submit" class="button" value="'.$this->getLang('btn_install').'" /> </p>');
82    }
83    ptln('  <p> <input type="hidden" name="do"     value="admin" />');
84    ptln('      <input type="hidden" name="page"   value="sympaauth" /> </p>');
85    ptln('</form>');
86  }
87
88  /**********************
89   * Check if the plugin
90   * is installed
91   *
92   **********************/
93  function is_installed() {
94    //Check if the file sympa.class.php is in the authenticafion methods directory
95    if (!file_exists(DOKU . '/inc/auth/sympa.class.php')) {
96      return false;
97    }
98
99    //Check if the sympa soap server is rightly set up in the file local.php
100    if (!$local_handle = fopen(DOKU . '/conf/local.php', 'r')) {
101      return false;
102    }
103    while (!feof($local_handle)) {
104      $line = fgets($local_handle, 1000);
105      if(preg_match('!\$conf\[\'plugin\'\]\[\'sympaauth\'\]\[\'sympaSoapService\'\] *= *.*!', $line)) {
106	fclose($local_handle);
107	return true;
108      }
109    }
110    fclose($local_handle);
111    return false;
112  }
113
114
115  /**********************
116   * Check if the plugin
117   * is currently used
118   *
119   **********************/
120  function is_used() {
121    //Check if the authentication mode is the sympa-soap server one
122    if (!$local_handle = fopen(DOKU . '/conf/local.php', 'r')) {
123      return true;
124    }
125    while (!feof($local_handle)) {
126      $line = fgets($local_handle, 1000);
127      if(preg_match('!\$conf\[\'authtype\'\] *= *.*!', $line)) {
128	$value = $line;
129      }
130      if(preg_match('!\$conf\[\'authtype\'\] *= *[\'"]sympa[\'"]!', $value)) return true;
131    }
132    fclose($local_handle);
133    return false;
134  }
135
136
137  /**********************
138   * Install the plugin
139   *
140   **********************/
141  function install() {
142    // Check if the plugin files are already there, and delete it if it's the case
143    if (file_exists(DOKU . '/inc/auth/sympa.class.php')) {
144      if(!unlink(DOKU . '/inc/auth/sympa.class.php')) return false;
145    }
146    // Copy the new ones
147    if (!copy(DOKU .'/lib/plugins/sympaauth/files/sympa.class.php', DOKU .'/inc/auth/sympa.class.php')) return false;
148
149    // Backup
150    if (file_exists(DOKU . '/conf/local.php')) {
151      if (!copy(DOKU . '/conf/local.php', DOKU . '/conf/local.php.bak')) return false;
152    }
153
154    // Open local.php, copy its content to $lines, look for $conf['sympaSoapService'] line(s) delete it (them) and create it
155    // just before loading the protected settings.
156    // Note that $conf['plugin']['sympaauth']['sympaSoapService'] should be before "@include(DOKU_CONF.'local.protected.php');"
157    $i=0;
158    //    if (!(feof($local_handle))) { // if the file is not empty, assuming it has a <?
159    if (file_exists(DOKU .'/conf/local.php') && $local_handle = fopen(DOKU . '/conf/local.php', 'r')) { // if the file exists and is readable
160      if (!(feof($local_handle))) { // and if the file is not empty, assuming it has a <?
161	do {
162	  $lines[$i] = fgets($local_handle, 1000);
163	  if(preg_match('!\$conf\[\'plugin\'\]\[\'sympaauth\'\]\[\[\'sympaSoapService\'\] *= *.*!', $lines[$i])) unset($lines[$i--]);
164	} while (!(preg_match('!local.protected.php!', $lines[$i++]) || feof($local_handle)) );
165	$temp = $lines[--$i];
166	$lines[$i++] = '$conf[\'plugin\'][\'sympaauth\'][\'sympaSoapService\'] = \'\';' . "\n";
167	$lines[$i] = $temp;
168	while (!feof($local_handle)) {
169	  $i++;
170	  $lines[$i] = fgets($local_handle, 1000);
171	  if(preg_match('!\$conf\[\'plugin\'\]\[\'sympaauth\'\]\[\[\'sympaSoapService\'\] *= *.*!', $lines[$i])) unset($line[$i--]);
172	}
173      }
174      else {
175	$lines[$i] = '<?'."\n";
176	$lines[++$i] = '$conf[\'plugin\'][\'sympaauth\'][\'sympaSoapService\'] = \'\';' . "\n";
177      }
178    }
179    else { // if the file does not exist
180      if (!$local_handle = fopen(DOKU . '/conf/local.php', 'w+')) return false;
181      $lines[$i] = '<?'."\n";
182      $lines[++$i] = '$conf[\'plugin\'][\'sympaauth\'][\'sympaSoapService\'] = \'\';' . "\n";
183    }
184    fclose($local_handle);
185
186    // Delete local.php (a backup was created few lines before), then copy $lines to a new local.php. Restore backup if failure.
187    if(!unlink(DOKU . '/conf/local.php')) return false;
188    if (!$local_handle = fopen(DOKU . '/conf/local.php', 'w')) {
189      copy(DOKU . '/conf/local.php.bak', DOKU . '/conf/local.php');
190      return false;
191    }
192    foreach ($lines as $line) {
193      if (!empty($line) && fwrite($local_handle, $line) == FALSE) {
194	fclose($local_handle);
195	unlink(DOKU . '/conf/local.php');
196	copy(DOKU . '/conf/local.php.bak', DOKU . '/conf/local.php');
197	return false;
198      }
199    }
200
201    // Install localization for the config plugin
202    return($this->install_langconf());
203
204    return(true);
205  }
206
207
208  /***********************
209   * Uninstall the plugin
210   *
211   ***********************/
212  function uninstall() {
213     // Check if the plugin files are already there, and delete it if it's the case
214    if (file_exists(DOKU . '/inc/auth/sympa.class.php')) {
215      if(!unlink(DOKU . '/inc/auth/sympa.class.php')) return false;
216    }
217    if (file_exists(DOKU . '/lib/nusoap.php')) {
218      if(!unlink(DOKU . '/lib/nusoap.php')) return false;
219    }
220
221    if (file_exists(DOKU . '/conf/local.php')) {
222      if (!copy(DOKU . '/conf/local.php', DOKU . '/conf/local.php.bak')) return false;
223    }
224
225    // Open local.php, copy its content to $lines, look for $conf['sympaSoapService'] line(s) and delete it (them)
226    if (!$local_handle = fopen(DOKU . '/conf/local.php', 'r')) return false;
227    $i = -1;
228    while (!feof($local_handle)) {
229      $i++;
230      $lines[$i] = fgets($local_handle, 1000);
231      if(preg_match('!\$conf\[\'plugin\'\]\[\'sympaauth\'\]\[\[\'sympaSoapService\'\] *= *.*!', $lines[$i])) unset($lines[$i--]);
232    }
233    fclose($local_handle);
234
235    // Delete local.php after having backed it up, then copy $lines to a new local.php. Restore backup if failure.
236    if(!unlink(DOKU . '/conf/local.php')) return false;
237    if (!$local_handle = fopen(DOKU . '/conf/local.php', 'w+')) {
238      copy(DOKU . '/conf/local.php.bak', DOKU . '/conf/local.php');
239      return false;
240    }
241    foreach ($lines as $line) {
242      if (!empty($line) && fwrite($local_handle, $line) == FALSE) {
243	fclose($local_handle);
244	unlink(DOKU . '/conf/local.php');
245	copy(DOKU . '/conf/local.php.bak', DOKU . '/conf/local.php');
246	return false;
247      }
248    }
249    fclose($local_handle);
250
251    // Uninstall localization for the config plugin
252    return($this->uninstall_langconf());
253  }
254
255
256  /**************************
257   * Install the language
258   * phrases related to this
259   * plugin in the config
260   * plugin
261   *
262   **************************/
263  function install_langconf() {
264    // Read what are the languages available with the config plugin
265    if( !$configlang_h = opendir(DOKU_CONFIGLANG) ) return false;
266    while ($lang = readdir($configlang_h)) {
267      if ($lang != "." && $lang != ".." && is_dir(DOKU_CONFIGLANG.'/'. $lang)) {
268	// Then look for every languages in the sympaauth plugin
269	if(is_dir(DOKU_SYMPALANG.'/'. $lang)) {
270	  if(!$sympa_file = fopen(DOKU_SYMPALANG .'/' .$lang .'/config_lang.php','r')) return false;
271	  $i=0;
272	  // And copy the $lang lines from each... assuming that noone would place a ; at the beginning of a new line :-)
273	  while (!feof($sympa_file)) {
274	    $lines[$i] = fgets($sympa_file, 1000);
275	    if(!preg_match('!\$lang\[\'\w{1,}\'\] *= *.*!', $lines[$i])) unset($lines[$i--]);
276	    $i++;
277	  }
278	  fclose($sympa_file);
279	  // To the associated language file of the config plugin
280	  if(!$config_file= fopen(DOKU_CONFIGLANG .'/'.$lang .'/lang.php','a')) return false;
281	  if(!fwrite($config_file,'/* Parameters for authenticating from a sympa server */'."\n")) return false;
282	  foreach ($lines as $line) {
283	    if(!empty($line) && !fwrite($config_file,$line ."\n")) return false;
284	  }
285	  fclose($config_file);
286	}
287      }
288    }
289    closedir($configlang_h);
290    return(true);
291  }
292
293
294  /**************************
295   * Uninstall the language
296   * phrases from the config
297   * plugin
298   *
299   **************************/
300  function uninstall_langconf() {
301    if( !$configlang_h = opendir(DOKU_CONFIGLANG) ) return false;
302    // Delete every line containing the word "sympa"
303    while (false !== ($lang = readdir($configlang_h)) ) {
304      if ($lang != "." && $lang != ".." && is_dir(DOKU_CONFIGLANG.'/'. $lang)) {
305	if(file_exists(DOKU_CONFIGLANG .'/' .$lang .'/lang.php')){
306	  if(!$sympa_file = fopen(DOKU_CONFIGLANG .'/' .$lang .'/lang.php','r')) return false;
307	  $i = 0;
308	  while (!feof($sympa_file)) {
309	    $lines[$i] = fgets($sympa_file, 1000);
310	    if(preg_match('!sympa!i', $lines[$i])) unset($lines[$i--]);
311	    $i++;
312	  }
313	  fclose($sympa_file);
314
315	  // Delete lang.php after having backed it up, then copy $lines to a new lang.php. Restore backup if failure.
316	  if(!copy(DOKU_CONFIGLANG .'/' .$lang .'/lang.php', DOKU_CONFIGLANG .'/' .$lang .'/lang.php.bak')) return false;
317	  if(!unlink(DOKU_CONFIGLANG .'/' .$lang .'/lang.php')) return false;
318	  if (!$sympa_file = fopen(DOKU_CONFIGLANG .'/' .$lang .'/lang.php', 'w')) {
319	    copy(DOKU . '/conf/local.php.bak', DOKU . '/conf/local.php');
320	    return false;
321	  }
322	  foreach ($lines as $line) {
323	    if (!empty($line) && fwrite($sympa_file, $line) == FALSE) {
324	      copy(DOKU . '/conf/local.php.bak', DOKU . '/conf/local.php');
325	      return false;
326	    }
327	  }
328	  fclose($sympa_file);
329	}
330      }
331    }
332  }
333
334
335}