Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.63 KB | None | 0 0
  1. <?php
  2. if(!defined('INITIALIZED'))
  3. exit;
  4.  
  5. class Account extends ObjectData
  6. {
  7. const LOADTYPE_ID = 'id';
  8. const LOADTYPE_NAME = 'name';
  9. const LOADTYPE_MAIL = 'email';
  10. public static $table = 'accounts';
  11. public $data = array('name' => null, 'password' => null, 'salt' => null, 'premdays' => null, 'lastday' => null, 'email' => null, 'key' => null, 'group_id' => null, 'create_ip' => null, 'create_date' => null, 'premium_points' => null, 'page_access' => null, 'location' => null, 'rlname' => null, 'email_new' => null, 'email_new_time' => null, 'email_code' => null, 'next_email' => null, 'last_post' => null, 'flag' => null);
  12. public static $fields = array('id', 'name', 'password', 'salt', 'premdays', 'lastday', 'email', 'key', 'group_id', 'create_ip', 'create_date', 'premium_points', 'page_access', 'location', 'rlname', 'email_new', 'email_new_time', 'email_code', 'next_email', 'last_post', 'flag');
  13. public $players;
  14. public $playerRanks;
  15. public $guildAccess;
  16. public $bans;
  17.  
  18. public function __construct($search_text = null, $search_by = self::LOADTYPE_ID)
  19. {
  20. if($search_text != null)
  21. $this->load($search_text, $search_by);
  22. }
  23.  
  24. public function load($search_text, $search_by = self::LOADTYPE_ID)
  25. {
  26. if(in_array($search_by, self::$fields))
  27. $search_string = $this->getDatabaseHandler()->fieldName($search_by) . ' = ' . $this->getDatabaseHandler()->quote($search_text);
  28. else
  29. new Error_Critic('', 'Wrong Account search_by type.');
  30. $fieldsArray = array();
  31. foreach(self::$fields as $fieldName)
  32. $fieldsArray[$fieldName] = $this->getDatabaseHandler()->fieldName($fieldName);
  33. $this->data = $this->getDatabaseHandler()->query('SELECT ' . implode(', ', $fieldsArray) . ' FROM ' . $this->getDatabaseHandler()->tableName(self::$table) . ' WHERE ' . $search_string)->fetch();
  34. }
  35.  
  36. public function loadById($id)
  37. {
  38. $this->load($id, 'id');
  39. }
  40.  
  41. public function loadByName($name)
  42. {
  43. $this->load($name, 'name');
  44. }
  45.  
  46. public function loadByEmail($mail)
  47. {
  48. $this->load($mail, 'email');
  49. }
  50.  
  51. public function save($forceInsert = false)
  52. {
  53. if(!isset($this->data['id']) || $forceInsert)
  54. {
  55. $keys = array();
  56. $values = array();
  57. foreach(self::$fields as $key)
  58. if($key != 'id')
  59. {
  60. $keys[] = $this->getDatabaseHandler()->fieldName($key);
  61. $values[] = $this->getDatabaseHandler()->quote($this->data[$key]);
  62. }
  63. $this->getDatabaseHandler()->query('INSERT INTO ' . $this->getDatabaseHandler()->tableName(self::$table) . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $values) . ')');
  64. $this->setID($this->getDatabaseHandler()->lastInsertId());
  65. }
  66. else
  67. {
  68. $updates = array();
  69. foreach(self::$fields as $key)
  70. if($key != 'id')
  71. $updates[] = $this->getDatabaseHandler()->fieldName($key) . ' = ' . $this->getDatabaseHandler()->quote($this->data[$key]);
  72. $this->getDatabaseHandler()->query('UPDATE ' . $this->getDatabaseHandler()->tableName(self::$table) . ' SET ' . implode(', ', $updates) . ' WHERE ' . $this->getDatabaseHandler()->fieldName('id') . ' = ' . $this->getDatabaseHandler()->quote($this->data['id']));
  73. }
  74. }
  75.  
  76. public function getPlayers($forceReload = false)
  77. {
  78. if(!isset($this->players) || $forceReload)
  79. {
  80. $this->players = new DatabaseList('Player');
  81. $this->players->setFilter(new SQL_Filter(new SQL_Field('account_id'), SQL_Filter::EQUAL, $this->getID()));
  82. $this->players->addOrder(new SQL_Order(new SQL_Field('name')));
  83. }
  84. return $this->players;
  85. }
  86.  
  87. public function getGuildRanks($forceReload = false)
  88. {
  89. if(!isset($this->playerRanks) || $forceReload)
  90. {
  91. $this->playerRanks = new DatabaseList('AccountGuildRank');
  92. $filterAccount = new SQL_Filter(new SQL_Field('account_id', 'players'), SQL_Filter::EQUAL, $this->getID());
  93. $filterPlayer = new SQL_Filter(new SQL_Field('rank_id', 'players'), SQL_Filter::EQUAL, new SQL_Field('id', 'guild_ranks'));
  94. $filterGuild = new SQL_Filter(new SQL_Field('guild_id', 'guild_ranks'), SQL_Filter::EQUAL, new SQL_Field('id', 'guilds'));
  95. $filter = new SQL_Filter($filterAccount, SQL_Filter::CRITERIUM_AND, $filterPlayer);
  96. $filter = new SQL_Filter($filter, SQL_Filter::CRITERIUM_AND, $filterGuild);
  97. $this->playerRanks->setFilter($filter);
  98. }
  99. return $this->playerRanks;
  100. }
  101.  
  102. public function loadGuildAccess($forceReload = false)
  103. {
  104. if(!isset($this->guildAccess) || $forceReload)
  105. {
  106. $this->guildAccess = array();
  107. foreach($this->getGuildRanks($forceReload) as $rank)
  108. if($rank->getOwnerID() == $rank->getPlayerID())
  109. $this->guildAccess[$rank->getGuildID()] = Guild::LEVEL_OWNER;
  110. elseif(!isset($this->guildAccess[$rank->getGuildID()]) || $rank->getLevel() > $this->guildAccess[$rank->getGuildID()])
  111. $this->guildAccess[$rank->getGuildID()] = $rank->getLevel();
  112. }
  113. }
  114.  
  115. public function isInGuild($guildId, $forceReload = false)
  116. {
  117. $this->loadGuildAccess($forceReload);
  118. return isset($this->guildAccess[$guildId]);
  119. }
  120.  
  121. public function getGuildLevel($guildId, $forceReload = false)
  122. {
  123. $this->loadGuildAccess($forceReload);
  124. if(isset($this->guildAccess[$guildId]))
  125. return $this->guildAccess[$guildId];
  126. else
  127. return 0;
  128. }
  129.  
  130. public function unban()
  131. {
  132. $bans = new DatabaseList('Ban');
  133. $filterType = new SQL_Filter(new SQL_Field('type'), SQL_Filter::EQUAL, Ban::TYPE_ACCOUNT);
  134. $filterValue = new SQL_Filter(new SQL_Field('value'), SQL_Filter::EQUAL, $this->data['id']);
  135. $filterActive = new SQL_Filter(new SQL_Field('active'), SQL_Filter::EQUAL, 1);
  136. $filter = new SQL_Filter($filterType, SQL_Filter::CRITERIUM_AND, $filterValue);
  137. $filter = new SQL_Filter($filter, SQL_Filter::CRITERIUM_AND, $filterActive);
  138. $bans->setFilter($filter);
  139. foreach($bans as $ban)
  140. {
  141. $ban->setActive(0);
  142. $ban->save();
  143. }
  144. }
  145.  
  146. public function loadBans($forceReload = false)
  147. {
  148. if(!isset($this->bans) || $forceReload)
  149. {
  150. $this->bans = new DatabaseList('Ban');
  151. $filterType = new SQL_Filter(new SQL_Field('type'), SQL_Filter::EQUAL, Ban::TYPE_ACCOUNT);
  152. $filterValue = new SQL_Filter(new SQL_Field('value'), SQL_Filter::EQUAL, $this->data['id']);
  153. $filterActive = new SQL_Filter(new SQL_Field('active'), SQL_Filter::EQUAL, 1);
  154. $filter = new SQL_Filter($filterType, SQL_Filter::CRITERIUM_AND, $filterValue);
  155. $filter = new SQL_Filter($filter, SQL_Filter::CRITERIUM_AND, $filterActive);
  156. $this->bans->setFilter($filter);
  157. }
  158. }
  159.  
  160. public function isBanned($forceReload = false)
  161. {
  162. $this->loadBans($forceReload);
  163. $isBanned = false;
  164. foreach($this->bans as $ban)
  165. {
  166. if($ban->getExpires() <= 0 || $ban->getExpires() > time())
  167. $isBanned = true;
  168. }
  169. return $isBanned;
  170. }
  171.  
  172. public function getBanTime($forceReload = false)
  173. {
  174. $this->loadBans($forceReload);
  175. $lastExpires = 0;
  176. foreach($bans as $ban)
  177. {
  178. if($ban->getExpires() <= 0)
  179. {
  180. $lastExpires = 0;
  181. break;
  182. }
  183. if($ban->getExpires() > time() && $ban->getExpires() > $lastExpires)
  184. $lastExpires = $ban->getExpires();
  185. }
  186. return $lastExpires;
  187. }
  188.  
  189. public function delete()
  190. {
  191. $this->getDatabaseHandler()->query('DELETE FROM ' . $this->getDatabaseHandler()->tableName(self::$table) . ' WHERE ' . $this->getDatabaseHandler()->fieldName('id') . ' = ' . $this->getDatabaseHandler()->quote($this->data['id']));
  192.  
  193. unset($this->data['id']);
  194. }
  195.  
  196. public function setID($value){$this->data['id'] = $value;}
  197. public function getID(){return $this->data['id'];}
  198. public function setName($value){$this->data['name'] = $value;}
  199. public function getName(){return $this->data['name'];}
  200. public function setPassword($value)
  201. {
  202. $this->data['salt'] = md5(microtime(true));
  203. $this->data['password'] = Website::encryptPassword($value, $this);
  204. }
  205. public function getPassword(){return $this->data['password'];}
  206. public function setSalt($value){$this->data['salt'] = $value;}
  207. public function getSalt(){return $this->data['salt'];}
  208. public function setPremDays($value){$this->data['premdays'] = $value;}
  209. public function getPremDays(){return $this->data['premdays'] - (date("z", time()) + (365 * (date("Y", time()) - date("Y", $this->data['lastday']))) - date("z", $this->data['lastday']));}
  210. public function setLastDay($value){$this->data['lastday'] = $value;}
  211. public function getLastDay(){return $this->data['lastday'];}
  212. public function setMail($value){$this->data['email'] = $value;}
  213. public function getMail(){return $this->data['email'];}
  214. public function setKey($value){$this->data['key'] = $value;}
  215. public function getKey(){return $this->data['key'];}
  216. public function setGroupID($value){$this->data['group_id'] = $value;}
  217. public function getGroupID(){return $this->data['group_id'];}
  218. /*
  219. * Custom AAC fields
  220. * create_ip , INT, default 0
  221. * create_date , INT, default 0
  222. * premium_points , INT, default 0
  223. * page_access, INT, default 0
  224. * location, VARCHAR(255), default ''
  225. * rlname, VARCHAR(255), default ''
  226. */
  227. public function setCreateIP($value){$this->data['create_ip'] = $value;}
  228. public function getCreateIP(){return $this->data['create_ip'];}
  229. public function setCreateDate($value){$this->data['create_date'] = $value;}
  230. public function getCreateDate(){return $this->data['create_date'];}
  231. public function setPremiumPoints($value){$this->data['premium_points'] = $value;}
  232. public function getPremiumPoints(){return $this->data['premium_points'];}
  233. public function setPageAccess($value){$this->data['page_access'] = $value;}
  234. public function getPageAccess(){return $this->data['page_access'];}
  235.  
  236. public function setLocation($value){$this->data['location'] = $value;}
  237. public function getLocation(){return $this->data['location'];}
  238. public function setRLName($value){$this->data['rlname'] = $value;}
  239. public function getRLName(){return $this->data['rlname'];}
  240. public function setFlag($value){$this->data['flag'] = $value;}
  241. public function getFlag(){return $this->data['flag'];}
  242. /*
  243. * for compability with old scripts
  244. */
  245. public function getGroup(){return $this->getGroupID();}
  246. public function setGroup($value){$this->setGroupID($value);}
  247. public function getEMail(){return $this->getMail();}
  248. public function setEMail($value){$this->setMail($value);}
  249. public function getPlayersList(){return $this->getPlayers();}
  250. public function getGuildAccess($guildID){return $this->getGuildLevel($guildID);}
  251.  
  252. public function isValidPassword($password)
  253. {
  254. return (strtoupper($this->data['password']) == strtoupper(Website::encryptPassword($password, $this)));
  255. }
  256.  
  257. public function find($name){$this->loadByName($name);}
  258. public function findByEmail($email){$this->loadByEmail($email);}
  259. public function isPremium(){return ($this->getPremDays() > 0);}
  260. public function getLastLogin(){return $this->getLastDay();}
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement