PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_jfusionplugins/dokuwiki/jfusion/action.php

http://jfusion.googlecode.com/
PHP | 278 lines | 195 code | 42 blank | 41 comment | 51 complexity | 51fb438b7d484d0be6b2f65a8a966e4e MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * DokuWiki Plugin jfusion (Action Component)
  4. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  5. * @author JFusion Team <webmaster@jfusion.org>
  6. *
  7. * Adapted from Dokuwiki's own auth routines
  8. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  9. * @author Andreas Gohr <andi@splitbrain.org>
  10. */
  11. // must be run within Dokuwiki
  12. if (!defined('DOKU_INC')) die();
  13. if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
  14. if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
  15. if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  16. require_once DOKU_PLUGIN.'action.php';
  17. class action_plugin_jfusion extends DokuWiki_Action_Plugin {
  18. var $session_save_handler = '';
  19. function register(&$controller) {
  20. $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE', $this, 'jfusion_login');
  21. $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'jfusion_logout');
  22. }
  23. function jfusion_login(&$event, $param) {
  24. //do not use Dokuwiki's standard login method
  25. $event->preventDefault();
  26. $user = & $event->data['user'];
  27. $password = & $event->data['password'];
  28. $sticky = & $event->data['sticky'];
  29. $silent = & $event->data['silent'];
  30. $this->loginDokuwiki($user, $password, $sticky, $silent);
  31. }
  32. function loginDokuwiki($user, $password, $sticky, $silent) {
  33. global $USERINFO, $conf, $lang, $auth;
  34. if (!$auth) return false;
  35. $sticky ? $sticky = true : $sticky = false; //sanity check
  36. if(!empty($user)){
  37. //usual login
  38. if ($auth->checkPass($user,$password)){
  39. // make logininfo globally available
  40. $_SERVER['REMOTE_USER'] = $user;
  41. $pass = PMA_blowfish_encrypt($password,auth_cookiesalt());
  42. $USERINFO = $auth->getUserData($user);
  43. // set cookie
  44. $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass);
  45. $time = $sticky ? (time()+60*60*24*365) : 0; //one year
  46. if (version_compare(PHP_VERSION, '5.2.0', '>')) {
  47. setcookie(DOKU_COOKIE,$cookie,$time,$conf['jfusion']['cookie_path'],$conf['jfusion']['cookie_domain'],($conf['securecookie'] && is_ssl()),true);
  48. }else{
  49. setcookie(DOKU_COOKIE,$cookie,$time,$conf['jfusion']['cookie_path'],$conf['jfusion']['cookie_domain'],($conf['securecookie'] && is_ssl()));
  50. }
  51. // set session
  52. $_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
  53. $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass;
  54. $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid();
  55. $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
  56. $_SESSION[DOKU_COOKIE]['auth']['time'] = time();
  57. if (!empty($conf['jfusion']['joomla'])) {
  58. $this->loginJoomla($user, $password, $sticky);
  59. }
  60. return true;
  61. }else{
  62. //invalid credentials - log off
  63. if(!$silent) msg($lang['badlogin'],-1);
  64. $this->logoutDokuwiki();
  65. return false;
  66. }
  67. }else{
  68. // read cookie information
  69. list($user,$sticky,$pass) = auth_getCookie();
  70. // get session info
  71. $session = $_SESSION[DOKU_COOKIE]['auth'];
  72. if($user && $pass){
  73. // we got a cookie - see if we can trust it
  74. if(isset($session) &&
  75. $auth->useSessionCache($user) &&
  76. ($session['time'] >= time()-$conf['auth_security_timeout']) &&
  77. ($session['user'] == $user) &&
  78. ($session['pass'] == $pass) && //still crypted
  79. ($session['buid'] == auth_browseruid()) ){
  80. // he has session, cookie and browser right - let him in
  81. $_SERVER['REMOTE_USER'] = $user;
  82. $USERINFO = $session['info']; //FIXME move all references to session
  83. return true;
  84. }
  85. // no we don't trust it yet - recheck pass but silent
  86. $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt());
  87. return $this->loginDokuwiki($user,$pass,$sticky,true);
  88. }
  89. }
  90. //just to be sure
  91. $this->logoutDokuwiki(true);
  92. }
  93. function jfusion_logout(&$event, $param) {
  94. global $ACT;
  95. //sanitize $ACT
  96. $ACT = act_clean($ACT);
  97. if ($ACT == 'logout') {
  98. global $ID, $INFO;
  99. //do not use Dokuwiki's standard logout method
  100. $event->preventDefault();
  101. $lockedby = checklock($ID); //page still locked?
  102. if($lockedby == $_SERVER['REMOTE_USER']) {
  103. unlock($ID); //try to unlock
  104. }
  105. $this->logoutDokuwiki();
  106. // rebuild info array
  107. $INFO = pageinfo();
  108. act_redirect($ID,'login');
  109. }
  110. }
  111. function logoutDokuwiki($keepbc = false) {
  112. global $conf, $USERINFO, $auth;
  113. // do the logout stuff
  114. if(isset($_SESSION[DOKU_COOKIE]['auth']['user']))
  115. unset($_SESSION[DOKU_COOKIE]['auth']['user']);
  116. if(isset($_SESSION[DOKU_COOKIE]['auth']['pass']))
  117. unset($_SESSION[DOKU_COOKIE]['auth']['pass']);
  118. if(isset($_SESSION[DOKU_COOKIE]['auth']['info']))
  119. unset($_SESSION[DOKU_COOKIE]['auth']['info']);
  120. if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc']))
  121. unset($_SESSION[DOKU_COOKIE]['bc']);
  122. if(isset($_SERVER['REMOTE_USER']))
  123. unset($_SERVER['REMOTE_USER']);
  124. $USERINFO=null; //FIXME
  125. if (version_compare(PHP_VERSION, '5.2.0', '>')) {
  126. setcookie(DOKU_COOKIE,'',time()-600000,$conf['jfusion']['cookie_path'],$conf['jfusion']['cookie_domain'],($conf['securecookie'] && is_ssl()),true);
  127. }else{
  128. setcookie(DOKU_COOKIE,'',time()-600000,$conf['jfusion']['cookie_path'],$conf['jfusion']['cookie_domain'],($conf['securecookie'] && is_ssl()));
  129. }
  130. if($auth && $auth->canDo('logout')){
  131. $auth->logOff();
  132. }
  133. if (!empty($conf['jfusion']['joomla'])) {
  134. $this->logoutJoomla();
  135. }
  136. }
  137. function startJoomla() {
  138. $this->session_save_handler = ini_get('session.save_handler');
  139. global $conf;
  140. if (!defined('_JEXEC')) {
  141. // trick joomla into thinking we're running through joomla
  142. define('_JEXEC', true);
  143. define('DS', DIRECTORY_SEPARATOR);
  144. define('JPATH_BASE', $conf['jfusion']['joomla_basepath']);
  145. // load joomla libraries
  146. require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
  147. require_once JPATH_LIBRARIES . DS . 'loader.php';
  148. spl_autoload_register('__autoload');
  149. jimport('joomla.base.object');
  150. jimport('joomla.factory');
  151. jimport('joomla.filter.filterinput');
  152. jimport('joomla.error.error');
  153. jimport('joomla.event.dispatcher');
  154. jimport('joomla.event.plugin');
  155. jimport('joomla.plugin.helper');
  156. jimport('joomla.utilities.arrayhelper');
  157. jimport('joomla.environment.uri');
  158. jimport('joomla.environment.request');
  159. jimport('joomla.user.user');
  160. jimport('joomla.html.parameter');
  161. jimport('joomla.version');
  162. // JText cannot be loaded with jimport since it's not in a file called text.php but in methods
  163. JLoader::register('JText', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
  164. JLoader::register('JRoute', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
  165. //load JFusion's libraries
  166. require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_jfusion' . DS . 'models' . DS . 'model.factory.php';
  167. require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_jfusion' . DS . 'models' . DS . 'model.jfusion.php';
  168. } else {
  169. define('IN_JOOMLA', 1);
  170. }
  171. //set the cookie path to the correct setting
  172. if (version_compare(PHP_VERSION, '5.2.0', '>')) {
  173. session_set_cookie_params(0, '/', '', ($conf['securecookie'] && is_ssl()), true);
  174. } else {
  175. session_set_cookie_params(0, '/', '', ($conf['securecookie'] && is_ssl()));
  176. }
  177. $mainframe = & JFactory::getApplication('site');
  178. $GLOBALS['mainframe'] = & $mainframe;
  179. return $mainframe;
  180. }
  181. function stopJoomla() {
  182. global $conf;
  183. //restore Dokuwiki's cookie settings
  184. if (version_compare(PHP_VERSION, '5.2.0', '>')) {
  185. session_set_cookie_params(0, DOKU_REL, '', ($conf['securecookie'] && is_ssl()), true);
  186. } else {
  187. session_set_cookie_params(0, DOKU_REL, '', ($conf['securecookie'] && is_ssl()));
  188. }
  189. ini_set('session.save_handler',$this->session_save_handler);
  190. }
  191. function loginJoomla($username, $password, $sticky) {
  192. global $JFusionActive, $conf;
  193. if (empty($JFusionActive)) {
  194. $mainframe = $this->startJoomla();
  195. //if already in Joomla framelessly, then do nothing as the getBuffer function will handle logins/outs
  196. if (!defined('IN_JOOMLA')) {
  197. //define that the phpBB3 JFusion plugin needs to be excluded
  198. global $JFusionActivePlugin;
  199. $JFusionActivePlugin =(empty($conf['jfusion']['jfusion_plugin_name'])) ? 'dokuwiki' : $conf['jfusion']['jfusion_plugin_name'];
  200. $JFusionActivePlugin = JFusionFactory::getPluginNodeId($plugin_name);
  201. // do the login
  202. $credentials = array('username' => $username, 'password' => $password);
  203. $options = array('entry_url' => JURI::root() . 'index.php?option=com_user&task=login', 'silent' => true);
  204. //detect if the session should be remembered
  205. if (!empty($sticky)) {
  206. $options['remember'] = 1;
  207. } else {
  208. $options['remember'] = 0;
  209. }
  210. $success = $mainframe->login($credentials, $options);
  211. // clean up the joomla session object before continuing
  212. $session = & JFactory::getSession();
  213. $session->close();
  214. }
  215. $this->stopJoomla();
  216. }
  217. }
  218. function logoutJoomla() {
  219. global $JFusionActive, $conf;
  220. if (empty($JFusionActive)) {
  221. //define that the phpBB3 JFusion plugin needs to be excluded
  222. global $JFusionActivePlugin;
  223. $JFusionActivePlugin =(empty($conf['jfusion']['jfusion_plugin_name'])) ? 'dokuwiki' : $conf['jfusion']['jfusion_plugin_name'];
  224. $mainframe = $this->startJoomla();
  225. //if already in Joomla framelessly, then do nothing as the getBuffer function will handle logins/outs
  226. if (!defined('IN_JOOMLA')) {
  227. //logout any joomla users
  228. $mainframe->logout();
  229. //clean up session
  230. $session = & JFactory::getSession();
  231. $session->close();
  232. }
  233. $this->stopJoomla();
  234. }
  235. }
  236. }