1<?php
2/**
3 * Setting up the Facebook authentication admin plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Václav Voborník <nomail@vobornik.eu>
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_FBLOGINLANG')) define('DOKU_FBLOGINLANG', 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_fblogin extends DokuWiki_Admin_Plugin {
23
24  //global $conf;
25  /**
26   * Return some info about the plugin
27   *
28   **/
29  function getInfo(){
30
31      return array(
32		   'author' => 'Václav Voborník',
33		   'email'  => 'nomail@vobornik.eu',
34		   'date'   => '2010/11/28',
35		   'name'   => 'fblogin',
36		   'desc'   => 'Plugin to get authentication from Facebook',
37		   );
38  }
39
40
41  /**
42   * handle user request
43   */
44  function handle() {
45    if ($_REQUEST['action_'] == 'install') {
46      $this->install();
47    }
48    elseif ($_REQUEST['action_'] == 'uninstall') {
49      $this->uninstall();
50    }
51  }
52
53
54  /**
55   * Output appropriate html
56   *
57   */
58  function html() {
59    global $ID;
60    global $conf;
61    // Display the text about this plugin from "fblogin/lang/$lang/intro.txt"
62    print $this->locale_xhtml('intro');
63    ptln('<div id="fblofin__manager">');
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="fblogin" /> </p>');
85    ptln('</form>');
86    ptln('</div>');
87  }
88
89  /**********************
90   * Check if the plugin
91   * is installed
92   *
93   **********************/
94  function is_installed() {
95    //Check if the file facebook.class.php is in the authenticafion methods directory
96    if (!file_exists(DOKU . '/inc/auth/facebook.class.php')) {
97      return false;
98    }
99
100
101    return true;
102  }
103
104
105  /**********************
106   * Check if the plugin
107   * is currently used
108   *
109   **********************/
110  function is_used() {
111    //Check if the authentication mode is facebook
112    if (!$local_handle = fopen(DOKU . '/conf/local.php', 'r')) {
113      return true;
114    }
115    while (!feof($local_handle)) {
116      $line = fgets($local_handle, 1000);
117      if(preg_match('!\$conf\[\'authtype\'\] *= *.*!', $line)) {
118	$value = $line;
119      }
120      if(preg_match('!\$conf\[\'authtype\'\] *= *[\'"]facebook[\'"]!', $value)) return true;
121    }
122    fclose($local_handle);
123    return false;
124  }
125
126
127  /**********************
128   * Install the plugin
129   *
130   **********************/
131  function install() {
132    // Check if the plugin files are already there, and delete it if it's the case
133    if (file_exists(DOKU . '/inc/auth/facebook.class.php')) {
134      if(!unlink(DOKU . '/inc/auth/facebook.class.php')) return false;
135    }
136    // Copy the new ones
137    if (!copy(DOKU .'/lib/plugins/fblogin/files/facebook.class.php', DOKU .'/inc/auth/facebook.class.php')) return false;
138
139
140    // Install localization for the config plugin
141    return($this->install_langconf());
142
143    return(true);
144  }
145
146
147  /***********************
148   * Uninstall the plugin
149   *
150   ***********************/
151  function uninstall() {
152     // Check if the plugin files are already there, and delete it if it's the case
153    if (file_exists(DOKU . '/inc/auth/facebook.class.php')) {
154      if(!unlink(DOKU . '/inc/auth/facebook.class.php')) return false;
155    }
156
157    // Uninstall localization for the config plugin
158    return($this->uninstall_langconf());
159  }
160
161
162  /**************************
163   * Install the language
164   * phrases related to this
165   * plugin in the config
166   * plugin
167   *
168   **************************/
169  function install_langconf() {
170    // Read what are the languages available with the config plugin
171    if( !$configlang_h = opendir(DOKU_CONFIGLANG) ) return false;
172    while ($lang = readdir($configlang_h)) {
173      if ($lang != "." && $lang != ".." && is_dir(DOKU_CONFIGLANG.'/'. $lang)) {
174	// Then look for every languages in the fblogin plugin
175	if(is_dir(DOKU_FBLOGINLANG.'/'. $lang)) {
176	  if(!$fblogin_file = fopen(DOKU_FBLOGINLANG .'/' .$lang .'/config_lang.php','r')) return false;
177	  $i=0;
178	  // And copy the $lang lines from each... assuming that noone would place a ; at the beginning of a new line :-)
179	  while (!feof($fblogin_file)) {
180	    $lines[$i] = fgets($fblogin_file, 1000);
181	    if(!preg_match('!\$lang\[\'\w{1,}\'\] *= *.*!', $lines[$i])) unset($lines[$i--]);
182	    $i++;
183	  }
184	  fclose($fblogin_file);
185	  // To the associated language file of the config plugin
186	  if(!$config_file= fopen(DOKU_CONFIGLANG .'/'.$lang .'/lang.php','a')) return false;
187	  if(!fwrite($config_file,'/* Parameters for authenticating from Facebook */'."\n")) return false;
188	  foreach ($lines as $line) {
189	    if(!empty($line) && !fwrite($config_file,$line ."\n")) return false;
190	  }
191	  fclose($config_file);
192	}
193      }
194    }
195    closedir($configlang_h);
196    return(true);
197  }
198
199
200  /**************************
201   * Uninstall the language
202   * phrases from the config
203   * plugin
204   *
205   **************************/
206  function uninstall_langconf() {
207    if( !$configlang_h = opendir(DOKU_CONFIGLANG) ) return false;
208    // Delete every line containing the word "fblogin"
209    while (false !== ($lang = readdir($configlang_h)) ) {
210      if ($lang != "." && $lang != ".." && is_dir(DOKU_CONFIGLANG.'/'. $lang)) {
211	if(!$fblogin_file = fopen(DOKU_CONFIGLANG .'/' .$lang .'/lang.php','r')) return false;
212	$i = 0;
213	while (!feof($fblogin_file)) {
214	  $lines[$i] = fgets($fblogin_file, 1000);
215	  if(preg_match('!fblogin!i', $lines[$i])) unset($lines[$i--]);
216	  $i++;
217	}
218	fclose($fblogin_file);
219
220	// Delete lang.php after having backed it up, then copy $lines to a new lang.php. Restore backup if failure.
221	if(!copy(DOKU_CONFIGLANG .'/' .$lang .'/lang.php', DOKU_CONFIGLANG .'/' .$lang .'/lang.php.bak')) return false;
222	if(!unlink(DOKU_CONFIGLANG .'/' .$lang .'/lang.php')) return false;
223	if (!$fblogin_file = fopen(DOKU_CONFIGLANG .'/' .$lang .'/lang.php', 'w')) {
224	  copy(DOKU . '/conf/local.php.bak', DOKU . '/conf/local.php');
225	  return false;
226	}
227	foreach ($lines as $line) {
228	  if (!empty($line) && fwrite($fblogin_file, $line) == FALSE) {
229	    copy(DOKU . '/conf/local.php.bak', DOKU . '/conf/local.php');
230	    return false;
231	  }
232	}
233	fclose($fblogin_file);
234      }
235    }
236  }
237
238
239}
240