PageRenderTime 59ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/h-source/Application/Models/UsersModel.php

https://gitlab.com/pravi/h-node
PHP | 295 lines | 224 code | 47 blank | 24 comment | 18 complexity | 489dd660fa753289dfdd9f755209d05f MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. <?php
  2. // h-source, a web software to build a community of people that want to share their hardware information.
  3. // Copyright (C) 2010 Antonio Gallo (h-source-copyright.txt)
  4. //
  5. // This file is part of h-source
  6. //
  7. // h-source is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation, either version 3 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // h-source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with h-source. If not, see <http://www.gnu.org/licenses/>.
  19. if (!defined('EG')) die('Direct access not allowed!');
  20. class UsersModel extends Model_Map
  21. {
  22. public static $usersList = array();
  23. public function __construct()
  24. {
  25. $this->_tables='regusers,reggroups,regusers_groups';
  26. $this->_idFields='id_user,id_group';
  27. $this->_where=array(
  28. 'id_group' => 'reggroups',
  29. 'id_user' => 'regusers',
  30. 'name' => 'reggroups',
  31. 'confirmation_token'=> 'regusers',
  32. 'has_confirmed' => 'regusers',
  33. 'deleted' => 'regusers',
  34. 'forgot_token' => 'regusers'
  35. );
  36. $this->_popupItemNames = array(
  37. 'has_confirmed'=>'has_confirmed',
  38. 'deleted'=>'deleted',
  39. 'id_group'=>'name',
  40. );
  41. $this->_popupLabels = array(
  42. 'has_confirmed'=>'HAS CONFIRMED?',
  43. 'deleted'=>'DELETED?',
  44. 'id_group'=>'GROUP'
  45. );
  46. $this->orderBy = 'regusers.id_user desc';
  47. parent::__construct();
  48. $this->deleteNotRegistered();
  49. }
  50. public function pUpdate($id)
  51. {
  52. return parent::update($id);
  53. }
  54. public function deleteNotRegistered()
  55. {
  56. $limit = time() - Account::$confirmTime;
  57. $this->db->del('regusers','has_confirmed = 1 and deleted = "no" and creation_time < '.$limit);
  58. }
  59. public function getUser($id_user = 0)
  60. {
  61. $clean['id_user'] = (int)$id_user;
  62. if (array_key_exists($clean['id_user'],self::$usersList))
  63. {
  64. return self::$usersList[$clean['id_user']];
  65. }
  66. else
  67. {
  68. $user = $this->db->select('regusers','username,has_confirmed','id_user='.$clean['id_user']);
  69. if (count($user) > 0)
  70. {
  71. $fuser = (strcmp($user[0]['regusers']['has_confirmed'],0) === 0) ? $user[0]['regusers']['username'] : "__".$user[0]['regusers']['username'];
  72. self::$usersList[$clean['id_user']] = $fuser;
  73. return $fuser;
  74. }
  75. else
  76. {
  77. return "<i>__anonymous__</i>";
  78. }
  79. }
  80. }
  81. public function getLinkToUser($user)
  82. {
  83. if (strstr($user,'__'))
  84. {
  85. return $user;
  86. // return str_replace('__',null,$user);
  87. }
  88. else
  89. {
  90. return "<a href='".Url::getRoot()."meet/user/".Lang::$current."/$user'>$user</a>";
  91. }
  92. }
  93. public function getLinkToUserFromId($id_user = 0)
  94. {
  95. $clean['id_user'] = (int)$id_user;
  96. return $this->getLinkToUser($this->getUser($clean['id_user']));
  97. }
  98. //check if the user exists
  99. public function userExists($user)
  100. {
  101. $clean['user'] = ctype_alnum($user) ? sanitizeAll($user) : '';
  102. if (strcmp($clean['user'],'') !== 0)
  103. {
  104. $res = $this->where(array("username"=>$clean['user'],"has_confirmed"=>"0","deleted"=>"no"))->send();
  105. // $res = $this->db->select('regusers','has_confirmed','username="'.$clean['user'].'" and has_confirmed=0 and deleted="no"');
  106. if (count($res) > 0)
  107. {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. //get the user id from the username
  114. public function getUserId($username = '')
  115. {
  116. $clean['username'] = ctype_alnum($username) ? sanitizeAll($username) : '';
  117. $users = $this->select('id_user')->where(array('username'=>$clean['username'],'has_confirmed'=>0,'deleted'=>'no'))->send();
  118. if (count($users) > 0)
  119. {
  120. return $users[0]['regusers']['id_user'];
  121. }
  122. else
  123. {
  124. return 0;
  125. }
  126. }
  127. public function isBlocked($idUser)
  128. {
  129. $clean['id_user'] = (int)$idUser;
  130. $res = $this->select('blocked')->where(array('id_user'=>$clean['id_user'],'has_confirmed'=>0,'deleted'=>'no'))->toList('blocked')->send();
  131. if (count($res) > 0)
  132. {
  133. return strcmp($res[0],'yes') === 0 ? true : false;
  134. }
  135. return true;
  136. }
  137. public function insert()
  138. {
  139. //create the token
  140. $confirmation_token = md5(randString(20));
  141. $this->values['confirmation_token'] = $confirmation_token;
  142. //has_confirmed flag
  143. $this->values['has_confirmed'] = 1;
  144. $this->values['creation_time'] = time();
  145. //random ID
  146. $randomId = md5(randString(5).uniqid(mt_rand(),true));
  147. $this->values["temp_field"] = $randomId;
  148. if (isset($_POST['captcha']))
  149. {
  150. if ( strcmp($_SESSION['captchaString'],$_POST['captcha']) === 0 )
  151. {
  152. parent::insert();
  153. if ($this->queryResult)
  154. {
  155. $resId = $this->db->select("regusers","id_user","temp_field='$randomId'");
  156. $clean['id_user'] = $resId[0]['regusers']['id_user'];
  157. $this->db->update("regusers",'temp_field',array(''),'id_user='.$clean['id_user']);
  158. $result = Account::confirm($this->values['username'],$this->values['e_mail'],$clean['id_user'],$confirmation_token);
  159. if ($result)
  160. {
  161. $_SESSION['status'] = 'sent';
  162. }
  163. else
  164. {
  165. $_SESSION['status'] = 'regerror';
  166. }
  167. $hed = new HeaderObj(DOMAIN_NAME);
  168. $hed->redirect('users/notice/'.Lang::$current);
  169. }
  170. }
  171. else
  172. {
  173. $this->result = false;
  174. $this->queryResult = false;
  175. $this->notice = "<div class='alert'>Wrong captcha code...</div>\n";
  176. }
  177. }
  178. }
  179. public function close($id_user)
  180. {
  181. $clean['id_user'] = (int)$id_user;
  182. $this->values = array(
  183. 'has_confirmed' => 1,
  184. 'deleted' => 'yes',
  185. 'e_mail' => ''
  186. );
  187. if ($this->update($clean['id_user']))
  188. {
  189. $_SESSION['status'] = 'deleted';
  190. $profile = new ProfileModel();
  191. $res = $profile->db->select('profile','id_prof','created_by='.$clean['id_user']);
  192. if (count($res) > 0)
  193. {
  194. $clean['id_prof'] = (int)$res[0]['profile']['id_prof'];
  195. $profile->values = array(
  196. 'real_name' => '',
  197. 'where_you_are' => '',
  198. 'birth_date' => '',
  199. 'fav_distro' => '',
  200. 'projects' => '',
  201. 'description' => ''
  202. );
  203. $profile->update($clean['id_prof']);
  204. }
  205. }
  206. }
  207. public function forgot($username)
  208. {
  209. $clean['username'] = ctype_alnum($username) ? sanitizeAll($username) : '';
  210. if (isset($_POST['captcha']))
  211. {
  212. if ( strcmp($_SESSION['captchaString'],$_POST['captcha']) === 0 )
  213. {
  214. $res = $this->db->select('regusers','e_mail,id_user','username="'.$clean['username'].'" and has_confirmed = 0 and deleted = "no"');
  215. if (count($res) > 0)
  216. {
  217. $e_mail = $res[0]['regusers']['e_mail'];
  218. $id_user = (int)$res[0]['regusers']['id_user'];
  219. $forgot_token = md5(randString(20));
  220. $forgot_time = time();
  221. $updateArray = array($forgot_token, $forgot_time);
  222. $this->db->update('regusers','forgot_token,forgot_time',$updateArray,'username="'.$clean['username'].'"');
  223. $result = Account::sendnew($clean['username'],$e_mail,$id_user,$forgot_token);
  224. if ($result)
  225. {
  226. $_SESSION['status'] = 'sent_new';
  227. }
  228. else
  229. {
  230. $_SESSION['status'] = 'sent_new_error';
  231. }
  232. $hed = new HeaderObj(DOMAIN_NAME);
  233. $hed->redirect('users/notice/'.Lang::$current,1);
  234. }
  235. else
  236. {
  237. $this->notice = "<div class='alert'>the user does not exist</div>\n";
  238. }
  239. }
  240. else
  241. {
  242. $this->result = false;
  243. $this->queryResult = false;
  244. $this->notice = "<div class='alert'>Wrong captcha code...</div>\n";
  245. }
  246. }
  247. }
  248. }