Source for file User.inc

Documentation is available at User.inc

  1. <?php
  2. ################################################################
  3. # This file is part of the Core module of CompInaBox #
  4. # Copyright 2002. Eric D. Nielsen, All rights reserverd #
  5. # CompInaBox is availible for license under the GPL, see #
  6. # the COPYING file in the root directory of the install for #
  7. # the full terms of the GPL. #
  8. # #
  9. # File: User.inc #
  10. # Author: Eric D. Nielsen #
  11. # Description: Authenticated user class #
  12. # Attributes: #
  13. # Constructor: #
  14. # Methods: #
  15. ################################################################
  16.  
  17.  
  18.  
  19. class User
  20. {
  21. var $db;
  22. var $username;
  23. var $permissions;
  24. var $isSuperUser;
  25.  
  26. function User($db)
  27. {
  28. $this->db=$db;
  29. $this->username = "";
  30. $this->permissions=array();
  31. }
  32.  
  33. function isSiteAdmin()
  34. {
  35. return $this->isSuperUser;
  36. }
  37.  
  38. function getKeys()
  39. {
  40. return $this->permissions;
  41.  
  42. }
  43.  
  44. function getEmail()
  45. {
  46. $query="SELECT email FROM users NATURAL JOIN people_email WHERE username='".$this->username."' AND main_addr = TRUE;";
  47. $result=$this->db->query($query);
  48. list($email)=$result->getRowAt(0);
  49. return $email;
  50. }
  51.  
  52.  
  53. function login($username,$password)
  54. {
  55. $query = "SELECT hashed FROM users WHERE username='$username' AND status_name='ACTIVE';";
  56. $result = $this->db->query($query);
  57. if ($result->numrows()!=1)
  58. return FALSE;
  59. list($ref_pass)=$result->getRowAt(0);
  60. $test_pass = crypt($password,$ref_pass);
  61. if ($test_pass==$ref_pass)
  62. {
  63. $this->private_retrieve($username);
  64. return TRUE;
  65. }
  66. else
  67. return FALSE;
  68. }
  69.  
  70. function retrieve($username)
  71. {
  72. GLOBAL $HTTP_SESSION_VARS,$HTTP_REFERER;
  73. $ref_hash = $HTTP_SESSION_VARS["security_hash"];
  74. $test_hash = md5($username . session_id() . $HTTP_REFERER . $ref_hash);
  75. if ($ref_hash==$test_hash)
  76. {
  77. private_retrieve($username);
  78. return TRUE;
  79. }
  80. else
  81. return FALSE;
  82. }
  83. function getUsername()
  84. {
  85. return $this->username;
  86. }
  87. function isLoggedIn()
  88. {
  89. return $this->username!="";
  90. }
  91.  
  92. function mayAuthorizeKey($key)
  93. {
  94. if ($this->isSiteAdmin()) return TRUE;
  95. $userKeys = $this->getKeys();
  96. if (count($userKeys)==0) return FALSE;
  97. switch($key["Type"])
  98. {
  99. case "Site" : return FALSE;break;
  100. case "Comp" :
  101. foreach ($userKey as $testKey)
  102. {
  103. if ($testKey["Type"]=="Comp" &&
  104. $testKey["Compname"]==$key["Compname"] &&
  105. equalOrGreaterKeyLevel($key["Level"],
  106. $testKey["Level"],"Comp"))
  107. return TRUE;
  108. }
  109. return FALSE;
  110. break;
  111. case "Team" :
  112. foreach ($userKey as $testKey)
  113. {
  114. if ($testKey["Type"]=="Team" &&
  115. $testKey["TeamID"]==$key["TeamID"] &&
  116. equalOrGreaterKeyLevel($testkey["Level"],
  117. $key["Level"],"Team"))
  118. return TRUE;
  119. }
  120. return FALSE;
  121. break;
  122. default: return FALSE;
  123. }
  124. }
  125.  
  126. function getComps()
  127. {
  128. $comps = array();
  129. $isSiteAdmin = $this->isSiteAdmin();
  130. $compList = $this->db->getHostedComps();
  131. $username = $this->getUsername();
  132. foreach ($compList as $aComp)
  133. {
  134. $name = $aComp["Label"];
  135. $compunix = $aComp["Link"];
  136. $tasks = array();
  137. $query = "SELECT * FROM hosted_comps_view WHERE compname='$name' ";
  138. $query .= "AND status_name='OPEN';";
  139. $result = $this->db->query($query);
  140. if ($result->numrows())
  141. $tasks[]="Register";
  142. $query = "SELECT * FROM user_comp_roles ";
  143. $query .= "WHERE username='$username' AND compunix='$compunix';";
  144. $result = $this->db->query($query);
  145. if ($result->numrows() || $isSiteAdmin)
  146. {
  147. $tasks[] = "Reg Admin";
  148. $tasks[] = "Setup";
  149. }
  150. $query = "SELECT * FROM user_team_roles ";
  151. $query .= "WHERE username='$username' AND rolename <>'Affiliation Member';";
  152. $result = $this->db->query($query);
  153. if ($result->numrows() || $isSiteAdmin)
  154. {
  155. $tasks[] = "Affil";
  156. }
  157. if (count($tasks))
  158. $comps[] = array("CompName"=>$name,
  159. "CompUnixname"=>$compunix,
  160. "Tasks"=>$tasks);
  161. }
  162. return $comps;
  163. }
  164.  
  165.  
  166.  
  167.  
  168. function private_retrieve($username)
  169. {
  170. $this->username=$username;
  171. $query = "SELECT siteadmin FROM users WHERE username='$username';";
  172. $result = $this->db->query($query);
  173. if ($result->numrows())
  174. {
  175. list($siteadmin) = $result->getRowAt(0);
  176. $this->permissions=array();
  177. if ($siteadmin=='t')
  178. {
  179. $this->isSuperUser=TRUE;
  180. $this->permissions[]=array("Type"=>"Site",
  181. "Level"=>"Admin");
  182. }
  183. $query = "SELECT compunix,rolename FROM ";
  184. $query .= "user_comp_roles ";
  185. $query .= "WHERE username='{$this->username}' ";
  186. $query .= "ORDER BY compunix, rolename;";
  187. $result = $this->db->query($query);
  188. $numPermissions = $result->numrows();
  189. for ($i=0;$i< $numPermissions;$i++)
  190. {
  191. list($comp,$role) = $result->getRowAt($i);
  192. $this->permissions[]=array("Type"=>"Comp",
  193. "Compname"=>"$comp",
  194. "Level"=>"$role");
  195. }
  196. $query = "SELECT teamid,rolename FROM ";
  197. $query .= "user_team_roles ";
  198. $query .= "WHERE username='{$this->username}' ";
  199. $query .= "ORDER BY teamid, rolename;";
  200. $result = $this->db->query($query);
  201. $numPermissions = $result->numrows();
  202. for ($i=0;$i< $numPermissions;$i++)
  203. {
  204. list($team,$role) = $result->getRowAt($i);
  205. $this->permissions[]=array("Type"=>"Team",
  206. "TeamID"=>"$team",
  207. "Level"=>"$role");
  208. }
  209. return TRUE;
  210. }
  211. else
  212. return FALSE;
  213. }
  214.  
  215. // OBE: Gate/Guard combo handles this now
  216. # function mayView($page,$roleRestrict="")
  217. # {
  218. # // if the page asking for permission isn't a CIB page return false
  219. # if (FALSE===strpos(COMPINABOX_SECURE_URL,$page))
  220. # return FALSE;
  221. # // superuser doesn't need to use permission table
  222. # if ($this->isSuperUser)
  223. # return TRUE;##
  224. #
  225. # // strip the common base url
  226. # $baseLen = strlen(COMPINABOX_SECURE_URL);
  227. # $dispatchString = substr($page,$baseLen);#
  228. #
  229. # // everyone may view the CompInaBox index page
  230. # $lastSlash = strrpos($dispatchString,'/');
  231. # if ($lastSlash==0)
  232. # return TRUE;
  233. #
  234. # $requestPage = substr($dispatchString,$lastSlash);
  235. # $path = substr($dispatchString,0,strlen($dispatchString)-
  236. # strlen($requestPage));#
  237. #
  238. #
  239. # $directories = explode("/",$path);
  240. # // directories[0]=""
  241. # // directories[1]=toolname, if present
  242. # // directories[2]=compname, if present#
  243. #
  244. # // All top level pages are public
  245. # if (count($directories) < 3)
  246. # return TRUE;#
  247. #
  248. # if (isset($this->permissions[$directories[2]]))
  249. # {
  250. # // if the user has this permission, and the calling page hasn't
  251. # // imposed additional restrictions, the user may view the page
  252. # if ($roleRestrict=="")
  253. # return TRUE;
  254. # else
  255. # {
  256. # $perms = $this->permissions[$directories[2]];
  257. # if (count(array_intersect($perms,$roleRestrict)))
  258. # return TRUE;
  259. # else
  260. # return FALSE;
  261. # }
  262. # }
  263. # else
  264. # return FALSE;
  265. # }
  266.  
  267. function mail_username($email)
  268. {
  269. $query = "SELECT username FROM users NATURAL JOIN people_email WHERE email='$email';";
  270. $result = $this->db->query($query);
  271. if ($result->numrows()==1)
  272. {
  273. list($username)=$result->getRowAt(0);
  274. mail($email,"[CompInaBox] Username reminder",
  275. "Someone, hopefully you, requested the username associated with
  276. this email address. The username is $username . If you did not request this
  277. mailing please contact ".COMPINABOX_ADMIN_EMAIL." immediately.",
  278. "From: noreply@".COMPINABOX_DOMAIN."\r\n");
  279. return TRUE;
  280. }
  281. else
  282. return FALSE;
  283. }
  284.  
  285. //:NOTE: siteadmin may NOT request their password in this manner
  286. function reset_and_mail_password($username)
  287. {
  288. $query = "SELECT email FROM users NATURAL JOIN people_email where username='$username' AND siteadmin=FALSE;";
  289. $result = $this->db->query($query);
  290. if ($result->numrows()==1)
  291. {
  292. list($email)=$result->getRowAt(0);
  293. $password="";
  294. #:TODO: SEED the random number generate somewhere....
  295. for ($i=0;$i<8;$i++)
  296. $password.=$this->encode(random(70));
  297. mail($email,"[CompInaBox] Password reset",
  298. "Somebody, hopefully you, requested that the password associated
  299. with your accoun be reset. The new password is $password . Please log in
  300. immediately and change it as this message was sent in the clear. If you did
  301. not request this please contact ".COMPINABOX_ADMIN_EMAIL." immediately.",
  302. "From: noreply@".COMPINABOX_DOMAIN."\r\n");
  303. return $this->private_change_password($password,$username);
  304. }
  305. else
  306. {
  307. return FALSE;
  308. }
  309. }
  310.  
  311. function private_change_password($password,$username="")
  312. {
  313. if ($username=="" && $this->username=="") return FALSE;
  314. if ($username=="") $username=$this->username;
  315. // generate an MD5-style salt
  316. $salt = '$1$'.substr(md5(microtime().getmypid()),0,12);
  317. $hashed = crypt($password,$salt);
  318. $query = "UPDATE users SET hashed='$hashed' WHERE username='$username';";
  319. $this->db->query($query);
  320. return TRUE;
  321. }
  322.  
  323. function createPendingAccount($username,$email,$password,$firstname,$lastname)
  324. {
  325. GLOBAL $CIB_SERVER_HOST_NAME, $CIB_ADMIN_EMAIL, $CIB_SECURE_URL;
  326. GLOBAL $CIB_DOMAIN, $CIB_HOSTNAME;
  327. GLOBAL $CIB_HASH_SECRET;
  328. // generate an MD5-style salt
  329. $salt = '$1$'.substr(md5(microtime().getmypid()),0,12);
  330. $hashed = crypt($password,$salt);
  331. $this->db->startTransaction();
  332. // $query = "SELECT peopleid FROM people where firstname='$firstname' AND lastname='$lastname';";
  333. $query ="SELECT nextval('people_peopleid_seq');";
  334. $result = $this->db->query($query);
  335. list($peopleid)=$result->getRowAt(0);
  336. $query = "INSERT INTO people (peopleid,firstname, lastname) VALUES ($peopleid,'$firstname', '$lastname');";
  337. $result = $this->db->query($query);
  338. $query = "SELECT email FROM people_email WHERE peopleid=$peopleid;";
  339. $result = $this->db->query($query);
  340. if ($result->numrows())
  341. {
  342. $numEmails = $result->numrows();
  343. $matched=FALSE;
  344. for ($i=0;$i < $numEmails;$i++)
  345. {
  346. list($tempEmail) = $result->getRowAt($i);
  347. if ($tempEmail==$email) $matched=TRUE;
  348. if ($matched) break;
  349. }
  350. if (!$matched)
  351. {
  352. $query = "INSERT INTO people_email (peopleid,email,main_addr) VALUES ($peopleid,'$email',FALSE);";
  353. }
  354. }
  355. else
  356. {
  357. $query = "INSERT INTO people_email (peopleid,email,main_addr) VALUES ($peopleid,'$email',TRUE);";
  358. }
  359. $query = $this->db->query($query);
  360. $query = "INSERT INTO users (status_name,username,hashed,peopleid,siteadmin) ";
  361. $query.= " VALUES ('PENDING','$username','$hashed',$peopleid,FALSE);";
  362. $result = $this->db->query($query);
  363.  
  364. $confirmation_hash = MD5($CIB_HASH_SECRET . $username);
  365. $query = "INSERT INTO user_hashes (username, hash) VALUES ";
  366. $query.= "('$username','$confirmation_hash')";
  367. $result = $this->db->query($query);
  368. $mail_to = $email;
  369. $title = "[CompInaBox-$CIB_SERVER_HOST_NAME] New Account";
  370. /* The following block is a HEREDOC, make sure you understand how
  371. they work before you edit it, don't event add spaces/tabes */
  372. $body =<<<END_EMAIL
  373. This Email is sent to you to confirm your new account on the
  374. $CIB_SERVER_HOST_NAME CompInaBox Server. To complete your registration
  375. simply follow the following link (or cut and paste it into a browser).
  376. $CIB_SECURE_URL/accounts/confirm_account.php?hash=$confirmation_hash
  377.  
  378. If you did not request this account, you may safely ignore this
  379. email or you may contact $CIB_ADMIN_EMAIL to report this.
  380.  
  381. -- The $CIB_SERVER_HOST_NAME CompInaBox staff
  382. END_EMAIL;
  383. $headers='From: noreply@'.$CIB_HOSTNAME.$CIB_DOMAIN."\r\n";
  384. mail($mail_to,$title,$body,$headers);
  385. $this->db->commit();
  386. }
  387.  
  388. function activate_user($username)
  389. {
  390. GLOBAL $CIB_DB_NAME, $CIB_AUTH_USER,$CIB_AUTH_PASS;
  391. GLOBAL $CIB_ADMIN_USER,$CIB_ADMIN_PASS;
  392. if (""==$username) return;
  393. // upgrade database connection to an authenticated user
  394. $this->db = new CIB_DB($CIB_DB_NAME,$CIB_AUTH_USER,$CIB_AUTH_PASS);
  395.  
  396. $query = "UPDATE users SET status_name='ACTIVE' WHERE username='$username';";
  397. $this->db->query($query);
  398. $query= "DELETE FROM user_hashes WHERE username='$username';";
  399. $tempDB = new CIB_DB($CIB_DB_NAME,$CIB_ADMIN_USER,$CIB_ADMIN_PASS);
  400. $tempDB->query($query);
  401. return;
  402. }
  403.  
  404. }

Documentation generated on Tue, 25 Apr 2006 13:12:33 -0400 by phpDocumentor 1.3.0RC3