PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/elgg/mod/dokuwiki/lib/dokuwiki/inc/auth/elgg.class.php

https://bitbucket.org/rhizomatik/lorea_production/
PHP | 352 lines | 189 code | 42 blank | 121 comment | 39 complexity | 5b9cde286a77bfb0225fc4490e7a4a19 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Plaintext authentication backend
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Andreas Gohr <andi@splitbrain.org>
  7. * @author Chris Smith <chris@jalakai.co.uk>
  8. */
  9. define('DOKU_AUTH', dirname(__FILE__));
  10. require_once(DOKU_AUTH.'/basic.class.php');
  11. //define('AUTH_USERFILE',DOKU_CONF.'users.auth.php');
  12. class auth_elgg extends auth_basic {
  13. var $users = null;
  14. var $_pattern = array();
  15. /**
  16. * Constructor
  17. *
  18. * Carry out sanity checks to ensure the object is
  19. * able to operate. Set capabilities.
  20. *
  21. * @author Christopher Smith <chris@jalakai.co.uk>
  22. */
  23. function auth_elgg() {
  24. // $this->cando['addUser'] = true;
  25. // $this->cando['delUser'] = true;
  26. // $this->cando['modLogin'] = true;
  27. // $this->cando['modPass'] = true;
  28. // $this->cando['modName'] = true;
  29. // $this->cando['modMail'] = true;
  30. $this->cando['getACL'] = true;
  31. $this->cando['modGroups'] = true;
  32. // $this->cando['getUsers'] = true;
  33. $this->cando['getUserCount'] = true;
  34. }
  35. /**
  36. * Check user+password [required auth function]
  37. *
  38. * Checks if the given user exists and the given
  39. * plaintext password is correct
  40. *
  41. * @author Andreas Gohr <andi@splitbrain.org>
  42. * @return bool
  43. */
  44. function checkPass($user,$pass){
  45. $user = get_user_by_username($user);
  46. if ($user && $user->password == $pass)
  47. return true;
  48. /*if (authenticate($user, $pass)) {
  49. return true;
  50. }*/
  51. return false;
  52. }
  53. /**
  54. * Return user info
  55. *
  56. * Returns info about the given user needs to contain
  57. * at least these fields:
  58. *
  59. * name string full name of the user
  60. * mail string email addres of the user
  61. * grps array list of groups the user is in
  62. *
  63. * @author Andreas Gohr <andi@splitbrain.org>
  64. */
  65. function getUserData($username){
  66. $user = get_user_by_username($username);
  67. //error_log("getUserData:".$username);
  68. if (!$user)
  69. return false;
  70. //error_log("getUserData:".$username);
  71. $page_owner = page_owner_entity();
  72. $grps = array();
  73. if ($page_owner instanceof ElggGroup) {
  74. if ($page_owner->canEdit($user->getGUID())) {
  75. $grps[] = "admin";
  76. $grps[] = "root";
  77. //error_log('operator');
  78. }
  79. elseif ($page_owner->isMember($user)) {
  80. $grps[] = "member";
  81. }
  82. }
  83. elseif ($page_owner instanceof ElggUser) {
  84. if ($page_owner == $user) {
  85. $grps[] = "admin";
  86. }
  87. elseif ($page_owner->isFriendsWith($user->getGUID())) {
  88. $grps[] = "member";
  89. }
  90. }
  91. if ($user->isAdmin()) {
  92. $grps[] = "root";
  93. $grps[] = "admin";
  94. }
  95. $groups = elgg_get_entities_from_relationship(array('relationship' => 'member', 'relationship_guid' => $user->getGUID(), 'inverse_relationship' => FALSE, 'limit'=>0));
  96. foreach($groups as $group) {
  97. $grps[] = $this->cleanUser($group->name);
  98. }
  99. return array('name'=>$user->name, 'mail'=>$user->email, 'grps'=>$grps);
  100. }
  101. /**
  102. * Create a new User
  103. *
  104. * Returns false if the user already exists, null when an error
  105. * occurred and true if everything went well.
  106. *
  107. * The new user will be added to the default group by this
  108. * function if grps are not specified (default behaviour).
  109. *
  110. * @author Andreas Gohr <andi@splitbrain.org>
  111. * @author Chris Smith <chris@jalakai.co.uk>
  112. */
  113. function createUser($user,$pwd,$name,$mail,$grps=null){
  114. global $conf;
  115. // user mustn't already exist
  116. if ($this->getUserData($user) !== false) return false;
  117. $pass = auth_cryptPassword($pwd);
  118. // set default group if no groups specified
  119. if (!is_array($grps)) $grps = array($conf['defaultgroup']);
  120. // prepare user line
  121. $groups = join(',',$grps);
  122. $userline = join(':',array($user,$pass,$name,$mail,$groups))."\n";
  123. if (io_saveFile(AUTH_USERFILE,$userline,true)) {
  124. $this->users[$user] = compact('pass','name','mail','grps');
  125. return $pwd;
  126. }
  127. msg('The '.AUTH_USERFILE.' file is not writable. Please inform the Wiki-Admin',-1);
  128. return null;
  129. }
  130. /**
  131. * Modify user data
  132. *
  133. * @author Chris Smith <chris@jalakai.co.uk>
  134. * @param $user nick of the user to be changed
  135. * @param $changes array of field/value pairs to be changed (password will be clear text)
  136. * @return bool
  137. */
  138. function modifyUser($user, $changes) {
  139. global $conf;
  140. global $ACT;
  141. global $INFO;
  142. // sanity checks, user must already exist and there must be something to change
  143. if (($userinfo = $this->getUserData($user)) === false) return false;
  144. if (!is_array($changes) || !count($changes)) return true;
  145. // update userinfo with new data, remembering to encrypt any password
  146. $newuser = $user;
  147. foreach ($changes as $field => $value) {
  148. if ($field == 'user') {
  149. $newuser = $value;
  150. continue;
  151. }
  152. if ($field == 'pass') $value = auth_cryptPassword($value);
  153. $userinfo[$field] = $value;
  154. }
  155. $groups = join(',',$userinfo['grps']);
  156. $userline = join(':',array($newuser, $userinfo['pass'], $userinfo['name'], $userinfo['mail'], $groups))."\n";
  157. if (!$this->deleteUsers(array($user))) {
  158. msg('Unable to modify user data. Please inform the Wiki-Admin',-1);
  159. return false;
  160. }
  161. if (!io_saveFile(AUTH_USERFILE,$userline,true)) {
  162. msg('There was an error modifying your user data. You should register again.',-1);
  163. // FIXME, user has been deleted but not recreated, should force a logout and redirect to login page
  164. $ACT == 'register';
  165. return false;
  166. }
  167. $this->users[$newuser] = $userinfo;
  168. return true;
  169. }
  170. /**
  171. * Remove one or more users from the list of registered users
  172. *
  173. * @author Christopher Smith <chris@jalakai.co.uk>
  174. * @param array $users array of users to be deleted
  175. * @return int the number of users deleted
  176. */
  177. function deleteUsers($users) {
  178. if (!is_array($users) || empty($users)) return 0;
  179. if ($this->users === null) $this->_loadUserData();
  180. $deleted = array();
  181. foreach ($users as $user) {
  182. if (isset($this->users[$user])) $deleted[] = preg_quote($user,'/');
  183. }
  184. if (empty($deleted)) return 0;
  185. $pattern = '/^('.join('|',$deleted).'):/';
  186. if (io_deleteFromFile(AUTH_USERFILE,$pattern,true)) {
  187. foreach ($deleted as $user) unset($this->users[$user]);
  188. return count($deleted);
  189. }
  190. // problem deleting, reload the user list and count the difference
  191. $count = count($this->users);
  192. $this->_loadUserData();
  193. $count -= count($this->users);
  194. return $count;
  195. }
  196. /**
  197. * Return a count of the number of user which meet $filter criteria
  198. *
  199. * @author Chris Smith <chris@jalakai.co.uk>
  200. */
  201. function getUserCount($filter=array()) {
  202. return get_number_users(true);
  203. }
  204. /**
  205. * Bulk retrieval of user data
  206. *
  207. * @author Chris Smith <chris@jalakai.co.uk>
  208. * @param start index of first user to be returned
  209. * @param limit max number of users to be returned
  210. * @param filter array of field/pattern pairs
  211. * @return array of userinfo (refer getUserData for internal userinfo details)
  212. */
  213. function retrieveUsers($start=0,$limit=0,$filter=array()) {
  214. $entities = elgg_get_entities(array('types'=>'user','limit'=>$limit, 'offset'=>$start));
  215. $allusers = array();
  216. foreach ($entities as $entity) {
  217. $allusers[$entity->username] = getUserData($entity->username);
  218. }
  219. return $allusers;
  220. }
  221. /**
  222. * Only valid pageid's (no namespaces) for usernames
  223. */
  224. function cleanUser($user){
  225. global $conf;
  226. return cleanID(str_replace(':',$conf['sepchar'],$user));
  227. }
  228. function getACL(){
  229. $doku = current_dokuwiki_entity();
  230. elgg_set_ignore_access(true);
  231. $acl = explode("\n" ,$doku->wiki_acl);
  232. //error_log(json_encode($acl));
  233. elgg_set_ignore_access(false);
  234. return $acl;
  235. global $conf;
  236. $acl = array();
  237. $acl[] = "# acl.auth.php";
  238. $acl[] = '# <?php exit()?\>';
  239. $acl[] = "* @ALL 0";
  240. $acl[] = "* @user 1";
  241. $acl[] = "* @member 8";
  242. $acl[] = "* @admin 16";
  243. $acl[] = "* @root 255";
  244. $acl[] = "* @testers_de_la_red_social 8";
  245. return $acl;
  246. }
  247. function setACL($newacl){
  248. $doku = current_dokuwiki_entity();
  249. elgg_set_ignore_access(true);
  250. $doku->wiki_acl = $newacl;
  251. elgg_set_ignore_access(false);
  252. }
  253. /**
  254. * Only valid pageid's (no namespaces) for groupnames
  255. */
  256. function cleanGroup($group){
  257. global $conf;
  258. return cleanID(str_replace(':',$conf['sepchar'],$group));
  259. }
  260. /**
  261. * Load all user data
  262. *
  263. * loads the user file into a datastructure
  264. *
  265. * @author Andreas Gohr <andi@splitbrain.org>
  266. */
  267. function _loadUserData(){
  268. //error_log("getUserData:");
  269. $this->users = array();
  270. if(!@file_exists(AUTH_USERFILE)) return;
  271. $lines = file(AUTH_USERFILE);
  272. foreach($lines as $line){
  273. $line = preg_replace('/#.*$/','',$line); //ignore comments
  274. $line = trim($line);
  275. if(empty($line)) continue;
  276. $row = explode(":",$line,5);
  277. $groups = array_values(array_filter(explode(",",$row[4])));
  278. $this->users[$row[0]]['pass'] = $row[1];
  279. $this->users[$row[0]]['name'] = urldecode($row[2]);
  280. $this->users[$row[0]]['mail'] = $row[3];
  281. $this->users[$row[0]]['grps'] = $groups;
  282. }
  283. }
  284. /**
  285. * return 1 if $user + $info match $filter criteria, 0 otherwise
  286. *
  287. * @author Chris Smith <chris@jalakai.co.uk>
  288. */
  289. function _filter($user, $info) {
  290. // FIXME
  291. foreach ($this->_pattern as $item => $pattern) {
  292. if ($item == 'user') {
  293. if (!preg_match($pattern, $user)) return 0;
  294. } else if ($item == 'grps') {
  295. if (!count(preg_grep($pattern, $info['grps']))) return 0;
  296. } else {
  297. if (!preg_match($pattern, $info[$item])) return 0;
  298. }
  299. }
  300. return 1;
  301. }
  302. function _constructPattern($filter) {
  303. $this->_pattern = array();
  304. foreach ($filter as $item => $pattern) {
  305. // $this->_pattern[$item] = '/'.preg_quote($pattern,"/").'/i'; // don't allow regex characters
  306. $this->_pattern[$item] = '/'.str_replace('/','\/',$pattern).'/i'; // allow regex characters
  307. }
  308. }
  309. }
  310. //Setup VIM: ex: et ts=2 enc=utf-8 :