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

/includes/CUserManager.php

https://gitlab.com/BGCX261/zion-downloads-css-svn-to-git
PHP | 279 lines | 181 code | 39 blank | 59 comment | 47 complexity | 4dd0ba7c8246323c3c7f6c9d8ce0f52f MD5 | raw file
  1. <?php
  2. /**
  3. * =============================================================================
  4. * Main user handler
  5. *
  6. * @author SteamFriends Development Team
  7. * @version 1.0.0
  8. * @copyright SourceBans (C)2007 SteamFriends.com. All rights reserved.
  9. * @package SourceBans
  10. * @link http://www.sourcebans.net
  11. *
  12. * @version $Id: CUserManager.php 182 2008-12-18 19:12:19Z smithxxl $
  13. * =============================================================================
  14. */
  15. class CUserManager
  16. {
  17. var $aid = -1;
  18. var $admins = array();
  19. /**
  20. * Class constructor
  21. *
  22. * @param $aid the current user's aid
  23. * @param $password the current user's password
  24. * @return noreturn.
  25. */
  26. function CUserManager($aid, $password)
  27. {
  28. if($this->CheckLogin($password, $aid))
  29. {
  30. $this->aid = $aid;
  31. $this->GetUserArray($aid);
  32. }
  33. else
  34. $this->aid = -1;
  35. }
  36. /**
  37. * Gets all user details from the database, saves them into
  38. * the admin array 'cache', and then returns the array
  39. *
  40. * @param $aid the ID of admin to get info for.
  41. * @return array.
  42. */
  43. function GetUserArray($aid=-2)
  44. {
  45. if($aid == -2)
  46. $aid = $this->aid;
  47. // Invalid aid
  48. if($aid < 0 || empty($aid))
  49. return 0;
  50. $aid = (int)$aid;
  51. // We already got the data from the DB, and its saved in the manager
  52. if(isset($this->admins[$aid]) && !empty($this->admins[$aid]))
  53. return $this->admins[$aid];
  54. // Not in the manager, so we need to get them from DB
  55. $res = $GLOBALS['db']->GetRow("SELECT adm.user user, adm.authid authid, adm.password password, adm.gid gid, adm.email email, adm.validate validate, adm.extraflags extraflags,
  56. adm.immunity admimmunity,sg.immunity sgimmunity, adm.srv_password srv_password, adm.srv_group srv_group, adm.srv_flags srv_flags,sg.flags sgflags,
  57. wg.flags wgflags, wg.name wgname, adm.lastvisit lastvisit
  58. FROM " . DB_PREFIX . "_admins AS adm
  59. LEFT JOIN " . DB_PREFIX . "_groups AS wg ON adm.gid = wg.gid
  60. LEFT JOIN " . DB_PREFIX . "_srvgroups AS sg ON adm.srv_group = sg.name
  61. WHERE adm.aid = $aid");
  62. if(!$res)
  63. return 0; // ohnoes some type of db error
  64. $user = array();
  65. //$user['user'] = stripslashes($res[0]);
  66. $user['aid'] = $aid; //immediately obvious
  67. $user['user'] = $res['user'];
  68. $user['authid'] = $res['authid'];
  69. $user['password'] = $res['password'];
  70. $user['gid'] = $res['gid'];
  71. $user['email'] = $res['email'];
  72. $user['validate'] = $res['validate'];
  73. $user['extraflags'] = (intval($res['extraflags']) | intval($res['wgflags']));
  74. if(intval($res['admimmunity']) > intval($res['sgimmunity']))
  75. $user['srv_immunity'] = intval($res['admimmunity']);
  76. else
  77. $user['srv_immunity'] = intval($res['sgimmunity']);
  78. $user['srv_password'] = $res['srv_password'];
  79. $user['srv_groups'] = $res['srv_group'];
  80. $user['srv_flags'] = $res['srv_flags'] . $res['sgflags'];
  81. $user['group_name'] = $res['wgname'];
  82. $user['lastvisit'] = $res['lastvisit'];
  83. $this->admins[$aid] = $user;
  84. return $user;
  85. }
  86. /**
  87. * Will check to see if an admin has any of the flags given
  88. *
  89. * @param $flags The flags to check for.
  90. * @param $aid The user to check flags for.
  91. * @return boolean.
  92. */
  93. function HasAccess($flags, $aid=-2)
  94. {
  95. if($aid == -2)
  96. $aid = $this->aid;
  97. if(empty($flags) || $aid <= 0)
  98. return false;
  99. $aid = (int)$aid;
  100. if(is_numeric($flags))
  101. {
  102. if(!isset($this->admins[$aid]))
  103. $this->GetUserArray($aid);
  104. return ($this->admins[$aid]['extraflags'] & $flags) != 0 ? true : false;
  105. }
  106. else
  107. {
  108. if(!isset($this->admins[$aid]))
  109. $this->GetUserArray($aid);
  110. for($i=0;$i<strlen($this->admins[$aid]['srv_flags']);$i++)
  111. {
  112. for($a=0;$a<strlen($flags);$a++)
  113. {
  114. if(strstr($this->admins[$aid]['srv_flags'][$i], $flags[$a]))
  115. return true;
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Gets a 'property' from the user array eg. 'authid'
  122. *
  123. * @param $aid the ID of admin to get info for.
  124. * @return mixed.
  125. */
  126. function GetProperty($name, $aid=-2)
  127. {
  128. if($aid == -2)
  129. $aid = $this->aid;
  130. if(empty($name) || $aid < 0)
  131. return false;
  132. $aid = (int)$aid;
  133. if(!isset($this->admins[$aid]))
  134. $this->GetUserArray($aid);
  135. return $this->admins[$aid][$name];
  136. }
  137. /**
  138. * Will test the user's login stuff to check if they havnt changed their
  139. * cookies or something along those lines.
  140. *
  141. * @param $password The admins password.
  142. * @param $aid the admins aid
  143. * @return boolean.
  144. */
  145. function CheckLogin($password, $aid)
  146. {
  147. $aid = (int)$aid;
  148. if(empty($password))
  149. return false;
  150. if(!isset($this->admins[$aid]))
  151. $this->GetUserArray($aid);
  152. if($password == $this->admins[$aid]['password'])
  153. {
  154. $GLOBALS['db']->Execute("UPDATE `" . DB_PREFIX . "_admins` SET `lastvisit` = UNIX_TIMESTAMP() WHERE `aid` = '$aid'");
  155. return true;
  156. }
  157. else
  158. return false;
  159. }
  160. function login($aid, $password, $save = true)
  161. {
  162. if($this->CheckLogin($this->encrypt_password($password), $aid))
  163. {
  164. if($save)
  165. {
  166. //Sets cookies
  167. setcookie("aid", $aid, time()+LOGIN_COOKIE_LIFETIME);
  168. setcookie("password", $this->encrypt_password($password), time()+LOGIN_COOKIE_LIFETIME);
  169. setcookie("user", isset($_SESSION['user']['user'])?$_SESSION['user']['user']:null, time()+LOGIN_COOKIE_LIFETIME);
  170. }
  171. else
  172. {
  173. setcookie("aid", $aid);
  174. setcookie("password", $this->encrypt_password($password));
  175. setcookie("user", $_SESSION['user']['user']);
  176. }
  177. return true;
  178. }
  179. else
  180. {
  181. return false;
  182. }
  183. }
  184. /**
  185. * Encrypts a password.
  186. *
  187. * @param $password password to encrypt.
  188. * @return string.
  189. */
  190. function encrypt_password($password, $salt=SB_SALT)
  191. {
  192. return sha1(sha1($salt . $password));
  193. }
  194. function is_logged_in()
  195. {
  196. if($this->aid != -1)
  197. return true;
  198. else
  199. return false;
  200. }
  201. function is_admin($aid=-2)
  202. {
  203. if($aid == -2)
  204. $aid = $this->aid;
  205. if($this->HasAccess(ALL_WEB, $aid))
  206. return true;
  207. else
  208. return false;
  209. }
  210. function GetAid()
  211. {
  212. return $this->aid;
  213. }
  214. function GetAllAdmins()
  215. {
  216. $res = $GLOBALS['db']->GetAll("SELECT aid FROM " . DB_PREFIX . "_admins");
  217. foreach($res AS $admin)
  218. $this->GetUserArray($admin['aid']);
  219. return $this->admins;
  220. }
  221. function GetAdmin($aid=-2)
  222. {
  223. if($aid == -2)
  224. $aid = $this->aid;
  225. if($aid < 0)
  226. return false;
  227. $aid = (int)$aid;
  228. if(!isset($this->admins[$aid]))
  229. $this->GetUserArray($aid);
  230. return $this->admins[$aid];
  231. }
  232. function AddAdmin($name, $steam, $password, $email, $web_group, $web_flags, $srv_group, $srv_flags, $immunity, $srv_password)
  233. {
  234. $add_admin = $GLOBALS['db']->Prepare("INSERT INTO ".DB_PREFIX."_admins(user, authid, password, gid, email, extraflags, immunity, srv_group, srv_flags, srv_password)
  235. VALUES (?,?,?,?,?,?,?,?,?,?)");
  236. $GLOBALS['db']->Execute($add_admin,array($name, $steam, $this->encrypt_password($password), $web_group, $email, $web_flags, $immunity, $srv_group, $srv_flags, $srv_password));
  237. return ($add_admin) ? (int)$GLOBALS['db']->Insert_ID() : -1;
  238. }
  239. }
  240. ?>