PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/sys/plugins/classes/user.class.php

https://bitbucket.org/DESURE/dcms
PHP | 217 lines | 147 code | 28 blank | 42 comment | 20 complexity | 69a13c679d378cc7d1181233f1a6d10e MD5 | raw file
  1. <?php
  2. // ????? ??? ?????? ? ?????????????
  3. class user extends plugins {
  4. protected $_update = array();
  5. protected $_data = array();
  6. function __construct($id_or_arrayToCache) {
  7. if (is_array($id_or_arrayToCache)) {
  8. $this->_usersFromCache($id_or_arrayToCache);
  9. $this->_guest_init();
  10. } else {
  11. $this->_user_init($id_or_arrayToCache);
  12. }
  13. }
  14. /**
  15. * ????????? ?????? ????? ?????????? ????????????? ? ????????? ?? ? ???
  16. * @staticvar array $cache
  17. * @param type $get_users_by_id
  18. * @return type
  19. */
  20. protected function _usersFromCache($get_users_by_id) {
  21. static $cache = array(); // ??? ?????????????
  22. $get_users_by_id = array_unique((array) $get_users_by_id);
  23. $users_from_mysql = array(); // ????????????, ??????? ????? ????????????? ?? ???? (??? ? ????)
  24. $users_return = array(); // ????????????, ??????? ????? ??????????
  25. foreach ($get_users_by_id AS $id_user) {
  26. if (array_key_exists($id_user, $cache))
  27. $users_return[$id_user] = $cache[$id_user];
  28. else
  29. $users_from_mysql[] = $id_user;
  30. }
  31. if ($users_from_mysql) {
  32. $q = mysql_query("SELECT * FROM `users` WHERE `id` IN (" . implode(',', $users_from_mysql) . ")");
  33. while ($user_data = mysql_fetch_assoc($q)) {
  34. $id_user = $user_data['id'];
  35. $users_return[$id_user] = $cache[$id_user] = $user_data;
  36. }
  37. }
  38. return $users_return;
  39. }
  40. /**
  41. * ????????????? ?????? ????????????????? ???????????? (?????)
  42. */
  43. protected function _guest_init() {
  44. $this->_update = array();
  45. $this->_data = array();
  46. $this->_data ['id'] = false;
  47. $this->_data ['sex'] = 1;
  48. $this->_data ['group'] = 0;
  49. }
  50. /**
  51. * ????????????? ?????? ????????????
  52. * @global type $dcms
  53. * @staticvar array $cache
  54. * @param type $id
  55. * @return boolean
  56. */
  57. protected function _user_init($id) {
  58. $this->_guest_init();
  59. if ($id === 0) {
  60. global $dcms;
  61. // ??? ????????? ???????????
  62. $this->_data ['id'] = 0;
  63. $this->_data ['login'] = '[' . $dcms->system_nick . ']';
  64. $this->_data ['group'] = 6;
  65. $this->_data ['description'] = __('????????? ???. ?????? ??? ???????????.');
  66. return true;
  67. }
  68. $users = $this->_usersFromCache($id);
  69. if (array_key_exists($id, $users))
  70. $this->_data = $users[$id];
  71. }
  72. /**
  73. * ???????? ???? ????????????
  74. * @staticvar array $is_ban
  75. * @return boolean
  76. */
  77. protected function _is_ban() {
  78. static $is_ban = array();
  79. if (!isset($is_ban [$this->_data ['id']])) {
  80. $is_ban [$this->_data ['id']] = mysql_result(mysql_query("SELECT COUNT(*) FROM `ban` WHERE `id_user` = '{$this->_data['id']}' AND `time_start` < '" . TIME . "' AND (`time_end` is NULL OR `time_end` > '" . TIME . "')"), 0);
  81. }
  82. return !empty($is_ban [$this->_data ['id']]);
  83. }
  84. /**
  85. * ???????? ??????? (?????? ?????????) ???? ????????????
  86. * @staticvar array $is_ban_full
  87. * @return boolean
  88. */
  89. protected function _is_ban_full() {
  90. static $is_ban_full = array();
  91. if (!isset($is_ban_full [$this->_data ['id']])) {
  92. $is_ban_full [$this->_data ['id']] = mysql_result(mysql_query("SELECT COUNT(*) FROM `ban` WHERE `id_user` = '{$this->_data['id']}' AND `access_view` = '0' AND `time_start` < '" . TIME . "' AND (`time_end` is NULL OR `time_end` > '" . TIME . "')"), 0);
  93. }
  94. return !empty($is_ban_full [$this->_data ['id']]);
  95. }
  96. /**
  97. * ?????????, ????????? ?? ???????????? ?????? ? ???????
  98. * @staticvar array $online
  99. * @param integer $id_user
  100. * @return boolean
  101. */
  102. protected function _is_online($id_user) {
  103. static $online = false;
  104. if ($online === false) {
  105. $online = array();
  106. $q = mysql_query("SELECT `id_user` FROM `users_online`");
  107. while ($on = mysql_fetch_assoc($q)) {
  108. $online[$on ['id_user']] = true;
  109. }
  110. }
  111. return isset($online[$id_user]);
  112. }
  113. /**
  114. * ???????? ?? ??????????? ?????? ?????????
  115. * @global dcms $dcms
  116. * @return boolean
  117. */
  118. protected function _is_writeable() {
  119. if ($this->_is_ban())
  120. return false;
  121. global $dcms;
  122. if (!$dcms->user_write_limit_hour) {
  123. // ??????????? ?? ???????????
  124. return true;
  125. } elseif ($this->_data['group'] >= 2) {
  126. // ???????????? ?????? ? ?????? ?????????????
  127. return true;
  128. } elseif ($this->_data['reg_date'] < TIME - $dcms->user_write_limit_hour * 3600) {
  129. // ???????????? ????????? ???????????
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. function __get($n) {
  136. global $dcms;
  137. switch ($n) {
  138. case 'language' :
  139. return empty($this->_data ['language']) ? $dcms->language : $this->_data ['language'];
  140. case 'is_writeable' :
  141. return $this->_is_writeable();
  142. case 'is_ban' :
  143. return $this->_is_ban();
  144. case 'is_ban_full' :
  145. return $this->_is_ban_full();
  146. case 'online' :
  147. return (bool) (@$this->_data ['last_visit'] > TIME - SESSION_LIFE_TIME);
  148. case 'group_name' :
  149. return groups::name($this->_data ['group']);
  150. case 'items_per_page' :
  151. return !empty($this->_data ['items_per_page_' . $dcms->browser_type]) ? $this->_data ['items_per_page_' . $dcms->browser_type] : $dcms->items_per_page;
  152. case 'theme' :
  153. return @$this->_data ['theme_' . $dcms->browser_type];
  154. case 'nick' :
  155. return @$this->nick();
  156. default :
  157. return !isset($this->_data [$n]) ? false : $this->_data [$n];
  158. }
  159. }
  160. function __set($n, $v) {
  161. if (empty($this->_data ['id']))
  162. return false;
  163. global $dcms;
  164. switch ($n) {
  165. case 'theme' :
  166. $n .= '_' . $dcms->browser_type;
  167. break;
  168. case 'items_per_page' :
  169. $n .= '_' . $dcms->browser_type;
  170. break;
  171. }
  172. if (isset($this->_data [$n])) {
  173. $this->_data [$n] = $v;
  174. $this->_update [$n] = $v;
  175. } else {
  176. trigger_error("???? $n ?? ??????????");
  177. }
  178. }
  179. function __destruct() {
  180. if ($this->_update) {
  181. $sql = array();
  182. foreach ($this->_update as $key => $value) {
  183. $sql [] = "`" . my_esc($key) . "` = '" . my_esc($value) . "'";
  184. }
  185. mysql_query("UPDATE `users` SET " . implode(', ', $sql) . " WHERE `id` = '" . $this->_data ['id'] . "' LIMIT 1");
  186. }
  187. }
  188. }
  189. ?>