PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/site/administrator/components/com_jfusion/plugins/dokuwiki/auth/basic.class.php

https://bitbucket.org/manchas/jrobotz
PHP | 392 lines | 124 code | 25 blank | 243 comment | 11 complexity | 3ab194046f0f2969a425a024159fe88e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * @package JFusion_dokuwiki
  4. * @author Chris Smith <chris@jalakai.co.uk>
  5. * @author JFusion development team
  6. * @copyright Copyright (C) 2008 JFusion. All rights reserved.
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. */
  9. // no direct access
  10. defined('_JEXEC' ) or die('Restricted access' );
  11. /**
  12. * Basic authentication class for dokuwiki
  13. * @package JFusion_dokuwiki
  14. */
  15. class doku_auth_basic {
  16. var $success = true;
  17. /**
  18. * Posible things an auth backend module may be able to
  19. * do. The things a backend can do need to be set to true
  20. * in the constructor.
  21. */
  22. var $cando = array (
  23. 'addUser' => false, // can Users be created?
  24. 'delUser' => false, // can Users be deleted?
  25. 'modLogin' => false, // can login names be changed?
  26. 'modPass' => false, // can passwords be changed?
  27. 'modName' => false, // can real names be changed?
  28. 'modMail' => false, // can emails be changed?
  29. 'modGroups' => false, // can groups be changed?
  30. 'getUsers' => false, // can a (filtered) list of users be retrieved?
  31. 'getUserCount'=> false, // can the number of users be retrieved?
  32. 'getGroups' => false, // can a list of available groups be retrieved?
  33. 'external' => false, // does the module do external auth checking?
  34. 'logoff' => false, // has the module some special logoff method?
  35. );
  36. /**
  37. * Encrypts a password using the given method and salt
  38. *
  39. * If the selected method needs a salt and none was given, a random one
  40. * is chosen.
  41. *
  42. * The following methods are understood:
  43. *
  44. * smd5 - Salted MD5 hashing
  45. * md5 - Simple MD5 hashing
  46. * sha1 - SHA1 hashing
  47. * ssha - Salted SHA1 hashing
  48. * crypt - Unix crypt
  49. * mysql - MySQL password (old method)
  50. * my411 - MySQL 4.1.1 password
  51. *
  52. * @author Andreas Gohr <andi@splitbrain.org>
  53. * @return string The crypted password
  54. */
  55. function cryptPassword($clear,$method='',$salt=''){
  56. $share = Dokuwiki::getInstance();
  57. $conf = $share->getConf();
  58. if(empty($method)) $method = $conf['passcrypt'];
  59. //prepare a salt
  60. if(empty($salt)) $salt = md5(uniqid(rand(), true));
  61. switch(strtolower($method)){
  62. case 'smd5':
  63. return crypt($clear,'$1$'.substr($salt,0,8).'$');
  64. case 'md5':
  65. return md5($clear);
  66. case 'sha1':
  67. return sha1($clear);
  68. case 'ssha':
  69. $salt=substr($salt,0,4);
  70. return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt);
  71. case 'crypt':
  72. return crypt($clear,substr($salt,0,2));
  73. case 'mysql':
  74. //from http://www.php.net/mysql comment by <soren at byu dot edu>
  75. $nr=0x50305735;
  76. $nr2=0x12345671;
  77. $add=7;
  78. $charArr = preg_split("//", $clear);
  79. foreach ($charArr as $char) {
  80. if (($char == '') || ($char == ' ') || ($char == '\t')) continue;
  81. $charVal = ord($char);
  82. $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8);
  83. $nr2 += ($nr2 << 8) ^ $nr;
  84. $add += $charVal;
  85. }
  86. return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff));
  87. case 'my411':
  88. return '*'.sha1(pack("H*", sha1($clear)));
  89. default:
  90. JError::raiseWarning(500,"Unsupported crypt method $method");
  91. }
  92. }
  93. /**
  94. * Constructor.
  95. *
  96. * Carry out sanity checks to ensure the object is
  97. * able to operate. Set capabilities in $this->cando
  98. * array here
  99. *
  100. * Set $this->success to false if checks fail
  101. *
  102. * @author Christopher Smith <chris@jalakai.co.uk>
  103. */
  104. function auth_basic() {
  105. // the base class constructor does nothing, derived class
  106. // constructors do the real work
  107. }
  108. /**
  109. * Capability check. [ DO NOT OVERRIDE ]
  110. *
  111. * Checks the capabilities set in the $this->cando array and
  112. * some pseudo capabilities (shortcutting access to multiple
  113. * ones)
  114. *
  115. * ususal capabilities start with lowercase letter
  116. * shortcut capabilities start with uppercase letter
  117. *
  118. * @author Andreas Gohr <andi@splitbrain.org>
  119. * @return bool
  120. */
  121. function canDo($cap) {
  122. switch($cap){
  123. case 'Profile':
  124. // can at least one of the user's properties be changed?
  125. return ( $this->cando['modPass'] ||
  126. $this->cando['modName'] ||
  127. $this->cando['modMail'] );
  128. break;
  129. case 'UserMod':
  130. // can at least anything be changed?
  131. return ( $this->cando['modPass'] ||
  132. $this->cando['modName'] ||
  133. $this->cando['modMail'] ||
  134. $this->cando['modLogin'] ||
  135. $this->cando['modGroups'] ||
  136. $this->cando['modMail'] );
  137. break;
  138. default:
  139. // print a helping message for developers
  140. if(!isset($this->cando[$cap])){
  141. JError::raiseWarning(500,"Check for unknown capability '$cap' - Do you use an outdated Plugin?");
  142. return false;
  143. }
  144. return $this->cando[$cap];
  145. }
  146. }
  147. /**
  148. * Log off the current user [ OPTIONAL ]
  149. *
  150. * Is run in addition to the ususal logoff method. Should
  151. * only be needed when trustExternal is implemented.
  152. *
  153. * @see auth_logoff()
  154. * @author Andreas Gohr
  155. */
  156. function logOff(){
  157. }
  158. /**
  159. * Do all authentication [ OPTIONAL ]
  160. *
  161. * Set $this->cando['external'] = true when implemented
  162. *
  163. * If this function is implemented it will be used to
  164. * authenticate a user - all other DokuWiki internals
  165. * will not be used for authenticating, thus
  166. * implementing the checkPass() function is not needed
  167. * anymore.
  168. *
  169. * The function can be used to authenticate against third
  170. * party cookies or Apache auth mechanisms and replaces
  171. * the auth_login() function
  172. *
  173. * The function will be called with or without a set
  174. * username. If the Username is given it was called
  175. * from the login form and the given credentials might
  176. * need to be checked. If no username was given it
  177. * the function needs to check if the user is logged in
  178. * by other means (cookie, environment).
  179. *
  180. * The function needs to set some globals needed by
  181. * DokuWiki like auth_login() does.
  182. *
  183. * @see auth_login()
  184. * @author Andreas Gohr <andi@splitbrain.org>
  185. *
  186. * @param string $user Username
  187. * @param string $pass Cleartext Password
  188. * @param bool $sticky Cookie should not expire
  189. * @return bool true on successful auth
  190. */
  191. function trustExternal($user,$pass,$sticky=false){
  192. # // some example:
  193. #
  194. # global $USERINFO;
  195. # global $conf;
  196. # $sticky ? $sticky = true : $sticky = false; //sanity check
  197. #
  198. # // do the checking here
  199. #
  200. # // set the globals if authed
  201. # $USERINFO['name'] = 'FIXME';
  202. # $USERINFO['mail'] = 'FIXME';
  203. # $USERINFO['grps'] = array('FIXME');
  204. # $_SERVER['REMOTE_USER'] = $user;
  205. # $_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
  206. # $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass;
  207. # $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
  208. # return true;
  209. }
  210. /**
  211. * Check user+password [ MUST BE OVERRIDDEN ]
  212. *
  213. * Checks if the given user exists and the given
  214. * plaintext password is correct
  215. *
  216. * May be ommited if trustExternal is used.
  217. *
  218. * @author Andreas Gohr <andi@splitbrain.org>
  219. * @return bool
  220. */
  221. function checkPass($user,$pass){
  222. JError::raiseWarning(500,"no valid authorisation system in use");
  223. return false;
  224. }
  225. /**
  226. * Return user info [ MUST BE OVERRIDDEN ]
  227. *
  228. * Returns info about the given user needs to contain
  229. * at least these fields:
  230. *
  231. * name string full name of the user
  232. * mail string email addres of the user
  233. * grps array list of groups the user is in
  234. *
  235. * @author Andreas Gohr <andi@splitbrain.org>
  236. * @return array containing user data or false
  237. */
  238. function getUserData($user) {
  239. if(!$this->cando['external']) JError::raiseWarning(500,"no valid authorisation system in use");
  240. return false;
  241. }
  242. /**
  243. * Create a new User [implement only where required/possible]
  244. *
  245. * Returns false if the user already exists, null when an error
  246. * occurred and true if everything went well.
  247. *
  248. * The new user HAS TO be added to the default group by this
  249. * function!
  250. *
  251. * Set addUser capability when implemented
  252. *
  253. * @author Andreas Gohr <andi@splitbrain.org>
  254. */
  255. function createUser($user,$pass,$name,$mail,$grps=null){
  256. JError::raiseWarning(500,"authorisation method does not allow creation of new users");
  257. return null;
  258. }
  259. /**
  260. * Modify user data [implement only where required/possible]
  261. *
  262. * Set the mod* capabilities according to the implemented features
  263. *
  264. * @author Chris Smith <chris@jalakai.co.uk>
  265. * @param $user nick of the user to be changed
  266. * @param $changes array of field/value pairs to be changed (password will be clear text)
  267. * @return bool
  268. */
  269. function modifyUser($user, $changes) {
  270. JError::raiseWarning(500,"authorisation method does not allow modifying of user data");
  271. return false;
  272. }
  273. /**
  274. * Delete one or more users [implement only where required/possible]
  275. *
  276. * Set delUser capability when implemented
  277. *
  278. * @author Chris Smith <chris@jalakai.co.uk>
  279. * @param array $users
  280. * @return int number of users deleted
  281. */
  282. function deleteUsers($users) {
  283. JError::raiseWarning(500,"authorisation method does not allow deleting of users");
  284. return false;
  285. }
  286. /**
  287. * Return a count of the number of user which meet $filter criteria
  288. * [should be implemented whenever retrieveUsers is implemented]
  289. *
  290. * Set getUserCount capability when implemented
  291. *
  292. * @author Chris Smith <chris@jalakai.co.uk>
  293. */
  294. function getUserCount($filter=array()) {
  295. JError::raiseWarning(500,"authorisation method does not provide user counts");
  296. return 0;
  297. }
  298. /**
  299. * Bulk retrieval of user data [implement only where required/possible]
  300. *
  301. * Set getUsers capability when implemented
  302. *
  303. * @author Chris Smith <chris@jalakai.co.uk>
  304. * @param start index of first user to be returned
  305. * @param limit max number of users to be returned
  306. * @param filter array of field/pattern pairs, null for no filter
  307. * @return array of userinfo (refer getUserData for internal userinfo details)
  308. */
  309. function retrieveUsers($start=0,$limit=-1,$filter=null) {
  310. JError::raiseWarning(500,"authorisation method does not provide user counts");
  311. return array();
  312. }
  313. /**
  314. * Define a group [implement only where required/possible]
  315. *
  316. * Set addGroup capability when implemented
  317. *
  318. * @author Chris Smith <chris@jalakai.co.uk>
  319. * @return bool
  320. */
  321. function addGroup($group) {
  322. JError::raiseWarning(500,"authorisation method does not support independent group creation");
  323. return false;
  324. }
  325. /**
  326. * Retrieve groups [implement only where required/possible]
  327. *
  328. * Set getGroups capability when implemented
  329. *
  330. * @author Chris Smith <chris@jalakai.co.uk>
  331. * @return array
  332. */
  333. function retrieveGroups($start=0,$limit=0) {
  334. JError::raiseWarning(500,"authorisation method does not support group list retrieval");
  335. return array();
  336. }
  337. /**
  338. * Check Session Cache validity [implement only where required/possible]
  339. *
  340. * DokuWiki caches user info in the user's session for the timespan defined
  341. * in $conf['securitytimeout'].
  342. *
  343. * This makes sure slow authentication backends do not slow down DokuWiki.
  344. * This also means that changes to the user database will not be reflected
  345. * on currently logged in users.
  346. *
  347. * To accommodate for this, the user manager plugin will touch a reference
  348. * file whenever a change is submitted. This function compares the filetime
  349. * of this reference file with the time stored in the session.
  350. *
  351. * This reference file mechanism does not reflect changes done directly in
  352. * the backend's database through other means than the user manager plugin.
  353. *
  354. * Fast backends might want to return always false, to force rechecks on
  355. * each page load. Others might want to use their own checking here. If
  356. * unsure, do not override.
  357. *
  358. * @param string $user - The username
  359. * @author Andreas Gohr <andi@splitbrain.org>
  360. * @return bool
  361. */
  362. function useSessionCache($user){
  363. global $conf;
  364. return ($_SESSION[DOKU_COOKIE]['auth']['time'] >= @filemtime($conf['cachedir'].'/sessionpurge'));
  365. }
  366. }
  367. //Setup VIM: ex: et ts=2 enc=utf-8 :