PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/datamodel/user.php

#
PHP | 398 lines | 303 code | 49 blank | 46 comment | 7 complexity | 2d69ddbb85e42c9c42f2390abaf48f15 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. Copyright (C) 2009-2010 Fabio Mattei <burattino@gmail.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once(STARTPATH.UTILSPATH.'password.php');
  16. require_once(STARTPATH.UTILSPATH.'imagefiles.php');
  17. require_once(STARTPATH.FILTERPATH.'userfilterremote.php');
  18. class User {
  19. const NEW_USER = -1;
  20. private $id = self::NEW_USER;
  21. private $name;
  22. private $username;
  23. private $password;
  24. private $body;
  25. private $role;
  26. private $toshow;
  27. private $email;
  28. private $msn;
  29. private $skype;
  30. private $created;
  31. private $updated;
  32. const INSERT_SQL = 'insert into users (id, name, username, password, body, role, toshow, email, msn, skype, created, updated) values (@#@, @?@, @?@, @?@, @?@, @?@, @#@, @?@, @?@, @?@, now(), now())';
  33. const UPDATE_SQL = 'update users set name = @?@, body = @?@, role = @?@, toshow = @#@, email = @?@, msn = @?@, skype = @?@, updated=now() where id = @#@';
  34. const UPDATE_SQL_PASSWORD = 'update users set password = @?@, updated=now() where id = @#@';
  35. const UPDATE_SQL_USERNAME = 'update users set username = @?@, updated=now() where id = @#@';
  36. const DELETE_SQL = 'delete from users where id = @#@';
  37. const SELECT_MAX_ID = 'select max(id) as maxid from users ';
  38. const SELECT_BY_ID = 'select * from users where id = @#@ ';
  39. const SELECT_BY_NAME = 'select * from users where name like @?@ order by name';
  40. const SELECT_BY_USERNAME = 'select * from users where username = @?@ LIMIT 1 ';
  41. const SELECT_BY_USERNAME_AND_EMAIL = 'select * from users where username = @?@ AND email = @?@ order by name ';
  42. const SELECT_ALL = 'select * from users order by name ';
  43. const SELECT_ALL_TO_SHOW = 'select * from users WHERE toshow = 1 order by name ';
  44. const SELECT_USR_PSW = 'select * from users WHERE username like @?@ AND password like @?@ ';
  45. const SELECT_BY_ID_ORD = 'select id from users order by id DESC';
  46. const SELECT_ARTICLES = 'select AR.* from articles as AR, users_articles as UA where AR.id = UA.article_id AND UA.user_id = @#@ order by AR.id DESC';
  47. const SELECT_COMMENTSARTICLES = 'select CM.* from comments as CM, articles as AR, users_articles as UA where AR.id = CM.article_id AND AR.id = UA.article_id AND UA.user_id = @#@ order by CM.id DESC';
  48. public function __construct($id=self::NEW_USER, $name='', $username='', $password='', $body='', $role='', $toshow='', $email='', $msn='', $skype='', $created='', $updated='') {
  49. $this->filter = UserFilterRemote::getInstance();
  50. $this->id = $id;
  51. $this->name = $name;
  52. $this->username = $username;
  53. $this->password = $password;
  54. $this->body = $body;
  55. $this->role = $role;
  56. $this->toshow = $toshow;
  57. $this->email = $email;
  58. $this->msn = $msn;
  59. $this->skype = $skype;
  60. $this->created = $created;
  61. $this->updated = $updated;
  62. }
  63. /**
  64. * Return a user from the query
  65. *
  66. * @return User
  67. */
  68. public static function findOne($SQL, $array_str, $array_int) {
  69. $tables = array("users" => TBPREFIX."users");
  70. try {
  71. $rs = DB::getInstance()->execute(
  72. $SQL,
  73. $array_str,
  74. $array_int,
  75. $tables);
  76. if ($row = mysql_fetch_array($rs)) {
  77. $ret = new User($row['id'], $row['name'], $row['username'], $row['password'], $row['body'], $row['role'], $row['toshow'], $row['email'], $row['msn'], $row['skype'], $row['created'], $row['updated']);
  78. } else {
  79. $ret = new User();
  80. }
  81. } catch (Exception $e) {
  82. $ret = new User();
  83. echo 'Caught exception: ', $e->getMessage(), "\n";
  84. }
  85. return $ret;
  86. }
  87. public static function findMany($SQL, $array_str, $array_int) {
  88. $tables = array("users" => TBPREFIX."users");
  89. $ret = array();
  90. try {
  91. $rs = DB::getInstance()->execute(
  92. $SQL,
  93. $array_str,
  94. $array_int,
  95. $tables);
  96. while ($row = mysql_fetch_array($rs)) {
  97. $ret[] = new User($row['id'], $row['name'], $row['username'], $row['password'], $row['body'], $row['role'], $row['toshow'], $row['email'], $row['msn'], $row['skype'], $row['created'], $row['updated']);
  98. }
  99. } catch (Exception $e) {
  100. $ret[] = new User();
  101. echo 'Caught exception: ', $e->getMessage(), "\n";
  102. }
  103. return $ret;
  104. }
  105. /**
  106. * Return a user from the query searching by id
  107. *
  108. * @return User
  109. */
  110. public static function findById($id) {
  111. return USER::findOne(self::SELECT_BY_ID, array(), array($id));
  112. }
  113. /**
  114. * Return a user from the query searching by name
  115. *
  116. * @return User
  117. */
  118. public static function findByName($name) {
  119. return USER::findMany(self::SELECT_BY_NAME, array("%$name%"), array());
  120. }
  121. /**
  122. * Return a user from the query searching by username
  123. *
  124. * @return User
  125. */
  126. public static function findByUserName($username) {
  127. return USER::findOne(self::SELECT_BY_USERNAME, array("$username"), array());
  128. }
  129. /**
  130. * Return a user from the searching by Username and Email
  131. *
  132. * @return User
  133. */
  134. public static function findByUsernameAndEmail($username, $email) {
  135. return USER::findOne(self::SELECT_BY_USERNAME_AND_EMAIL, array("$username", "$email"), array());
  136. }
  137. /**
  138. * Return all user in the database
  139. *
  140. * @return Array(User)
  141. */
  142. public static function findAll() {
  143. return USER::findMany(self::SELECT_ALL, array(), array());
  144. }
  145. public static function findAllToShow() {
  146. return USER::findMany(self::SELECT_ALL_TO_SHOW, array(), array());
  147. }
  148. public static function checkUsrPsw($usr, $psw) {
  149. return USER::findOne(self::SELECT_USR_PSW, array("$usr", md5($psw)), array());
  150. }
  151. public function articles() {
  152. $tables = array('articles' => TBPREFIX.'articles',
  153. 'users_articles' => TBPREFIX.'users_articles');
  154. return ARTICLE::findManyAndSpecifyTables(self::SELECT_ARTICLES, array(), array($this->id), $tables);
  155. }
  156. public function articlescomments() {
  157. $tables = array('comments' => TBPREFIX.'comments',
  158. 'articles' => TBPREFIX.'articles',
  159. 'users_articles' => TBPREFIX.'users_articles');
  160. return COMMENT::findManyAndSpecifyTables(self::SELECT_COMMENTSARTICLES, array(), array($this->id), $tables);
  161. }
  162. public function getMaxId() {
  163. $tables = array("users" => TBPREFIX."users");
  164. try {
  165. $rs = DB::getInstance()->execute(
  166. self::SELECT_MAX_ID,
  167. array(),
  168. array(),
  169. $tables);
  170. $row = mysql_fetch_array($rs);
  171. $maxId = $row['maxid'];
  172. } catch (Exception $e) {
  173. $maxId = 0;
  174. echo 'Caught exception: ', $e->getMessage(), "\n";
  175. }
  176. return $maxId;
  177. }
  178. public function save() {
  179. if ($this->id == self::NEW_USER) {
  180. $this->insert();
  181. } else {
  182. $this->update();
  183. }
  184. }
  185. public function delete() {
  186. $tables = array("users" => TBPREFIX."users");
  187. try {
  188. DB::getInstance()->execute(
  189. self::DELETE_SQL,
  190. array(),
  191. array($this->id),
  192. $tables);
  193. $this->id = self::NEW_USER;
  194. $this->name = '';
  195. $this->username = '';
  196. $this->password = '';
  197. $this->body = '';
  198. $this->role = '';
  199. $this->toshow = '';
  200. $this->email = '';
  201. $this->msn = '';
  202. $this->skype = '';
  203. $this->created = '';
  204. $this->updated = '';
  205. } catch (Exception $e) {
  206. echo 'Caught exception: ', $e->getMessage(), "\n";
  207. }
  208. }
  209. protected function insert() {
  210. $this->id = $this->getMaxId()+1;
  211. $tables = array("users" => TBPREFIX."users");
  212. $psw = md5($this->password);
  213. try {
  214. DB::getInstance()->execute(
  215. self::INSERT_SQL,
  216. array($this->name, $this->username, $psw, $this->body, $this->role, $this->email, $this->msn, $this->skype),
  217. array($this->id, $this->toshow),
  218. $tables);
  219. } catch (Exception $e) {
  220. echo 'Caught exception: ', $e->getMessage(), "\n";
  221. }
  222. }
  223. protected function update() {
  224. $tables = array("users" => TBPREFIX."users");
  225. try {
  226. DB::getInstance()->execute(
  227. self::UPDATE_SQL,
  228. array($this->name, $this->body, $this->role, $this->email, $this->msn, $this->skype),
  229. array($this->toshow, $this->id),
  230. $tables);
  231. } catch (Exception $e) {
  232. echo 'Caught exception: ', $e->getMessage(), "\n";
  233. }
  234. }
  235. public function updatePassword($NewPsw, $OldPsw) {
  236. if (md5($OldPsw)==$this->password) {
  237. $tables = array("users" => TBPREFIX."users");
  238. $this->password = md5($NewPsw);
  239. try {
  240. DB::getInstance()->execute(
  241. self::UPDATE_SQL_PASSWORD,
  242. array($this->password),
  243. array($this->id),
  244. $tables);
  245. } catch (Exception $e) {
  246. echo 'Caught exception: ', $e->getMessage(), "\n";
  247. }
  248. }
  249. }
  250. public function updateUsername($NewUserName) {
  251. $tables = array("users" => TBPREFIX."users");
  252. $this->username = $NewUserName;
  253. try {
  254. DB::getInstance()->execute(
  255. self::UPDATE_SQL_USERNAME,
  256. array($this->username),
  257. array($this->id),
  258. $tables);
  259. } catch (Exception $e) {
  260. echo 'Caught exception: ', $e->getMessage(), "\n";
  261. }
  262. }
  263. public function setNewRandomPassword() {
  264. $newPassword = Password::generatePassword();
  265. $tables = array("users" => TBPREFIX."users");
  266. $this->password = md5($newPassword);
  267. try {
  268. DB::getInstance()->execute(
  269. self::UPDATE_SQL_PASSWORD,
  270. array($this->password),
  271. array($this->id),
  272. $tables);
  273. } catch (Exception $e) {
  274. echo 'Caught exception: ', $e->getMessage(), "\n";
  275. }
  276. return $newPassword;
  277. }
  278. public function getId() {
  279. return $this->id;
  280. }
  281. public function getName() {
  282. return $this->name;
  283. }
  284. public function setName($name) {
  285. $this->name = $name;
  286. }
  287. public function getUsername() {
  288. return $this->username;
  289. }
  290. public function setUsername($username) {
  291. $this->username = $username;
  292. }
  293. public function getPassword() {
  294. return $this->password;
  295. }
  296. public function setPassword($password) {
  297. $this->password = $password;
  298. }
  299. public function getBody() {
  300. $out = $this->filter->executeFiltersBody($this->body);
  301. return $out;
  302. }
  303. public function getUnfilteredBody() {
  304. return $this->body;
  305. }
  306. public function setBody($body) {
  307. $this->body = $body;
  308. }
  309. public function getRole() {
  310. return $this->role;
  311. }
  312. public function setRole($role) {
  313. $this->role = $role;
  314. }
  315. public function getToshow() {
  316. return $this->toshow;
  317. }
  318. public function setToshow($toshow) {
  319. $this->toshow = $toshow;
  320. }
  321. public function getEmail() {
  322. return $this->email;
  323. }
  324. public function setEmail($email) {
  325. $this->email = $email;
  326. }
  327. public function getMsn() {
  328. return $this->msn;
  329. }
  330. public function setMsn($msn) {
  331. $this->msn = $msn;
  332. }
  333. public function getSkype() {
  334. return $this->skype;
  335. }
  336. public function setSkype($skype) {
  337. $this->skype = $skype;
  338. }
  339. public function getCreated() {
  340. return $this->created;
  341. }
  342. public function getUpdated() {
  343. return $this->updated;
  344. }
  345. }
  346. ?>