PageRenderTime 95ms CodeModel.GetById 34ms RepoModel.GetById 7ms app.codeStats 0ms

/app/models/UserModel.php

https://bitbucket.org/andrey4korop/iwobox.com
PHP | 333 lines | 214 code | 57 blank | 62 comment | 34 complexity | 194ba294ee7e4e7bd2b0e95c7547522f MD5 | raw file
Possible License(s): LGPL-2.1, MIT, Apache-2.0, MPL-2.0-no-copyleft-exception, BitTorrent-1.0
  1. <?php
  2. /**
  3. * User Model
  4. *
  5. * @version 1.0
  6. * @author Onelab <hello@onelab.co>
  7. *
  8. */
  9. class UserModel extends DataEntry
  10. {
  11. /**
  12. * Extend parents constructor and select entry
  13. * @param mixed $uniqid Value of the unique identifier
  14. */
  15. public function __construct($uniqid=0)
  16. {
  17. parent::__construct();
  18. $this->select($uniqid);
  19. }
  20. /**
  21. * Select entry with uniqid
  22. * @param int|string $uniqid Value of the any unique field
  23. * @return self
  24. */
  25. public function select($uniqid)
  26. {
  27. if (is_int($uniqid) || ctype_digit($uniqid)) {
  28. $col = $uniqid > 0 ? "id" : null;
  29. } else if (filter_var($uniqid, FILTER_VALIDATE_EMAIL)) {
  30. $col = "email";
  31. } else {
  32. $col = "username";
  33. }
  34. if ($col) {
  35. $query = DB::table(TABLE_PREFIX.TABLE_USERS)
  36. ->where($col, "=", $uniqid)
  37. ->limit(1)
  38. ->select("*");
  39. if ($query->count() == 1) {
  40. $resp = $query->get();
  41. $r = $resp[0];
  42. foreach ($r as $field => $value)
  43. $this->set($field, $value);
  44. $this->is_available = true;
  45. } else {
  46. $this->data = array();
  47. $this->is_available = false;
  48. }
  49. }
  50. return $this;
  51. }
  52. /**
  53. * Extend default values
  54. * @return self
  55. */
  56. public function extendDefaults()
  57. {
  58. $defaults = array(
  59. "account_type" => "member",
  60. "email" => uniqid()."@thepostcode.co",
  61. "username" => "user_".uniqid(),
  62. "password" => uniqid(),
  63. "firstname" => "",
  64. "lastname" => "",
  65. "package_id" => "0",
  66. "package_subscription" => "0",
  67. "settings" => "{}",
  68. "preferences" => "{}",
  69. "is_active" => "0",
  70. "expire_date" => date("Y-m-d H:i:s"),
  71. "date" => date("Y-m-d H:i:s"),
  72. "data" => '{}',
  73. );
  74. foreach ($defaults as $field => $value) {
  75. if (is_null($this->get($field)))
  76. $this->set($field, $value);
  77. }
  78. }
  79. /**
  80. * Insert Data as new entry
  81. */
  82. public function insert()
  83. {
  84. if ($this->isAvailable())
  85. return false;
  86. $this->extendDefaults();
  87. $id = DB::table(TABLE_PREFIX.TABLE_USERS)
  88. ->insert(array(
  89. "id" => null,
  90. "account_type" => $this->get("account_type"),
  91. "email" => $this->get("email"),
  92. "username" => $this->get("username"),
  93. "password" => $this->get("password"),
  94. "firstname" => $this->get("firstname"),
  95. "lastname" => $this->get("lastname"),
  96. "package_id" => $this->get("package_id"),
  97. "package_subscription" => $this->get("package_subscription"),
  98. "settings" => $this->get("settings"),
  99. "preferences" => $this->get("preferences"),
  100. "is_active" => $this->get("is_active"),
  101. "expire_date" => $this->get("expire_date"),
  102. "date" => $this->get("date"),
  103. "data" => $this->get("data"),
  104. ));
  105. $this->set("id", $id);
  106. $this->markAsAvailable();
  107. return $this->get("id");
  108. }
  109. /**
  110. * Update selected entry with Data
  111. */
  112. public function update()
  113. {
  114. if (!$this->isAvailable())
  115. return false;
  116. $this->extendDefaults();
  117. $id = DB::table(TABLE_PREFIX.TABLE_USERS)
  118. ->where("id", "=", $this->get("id"))
  119. ->update(array(
  120. "account_type" => $this->get("account_type"),
  121. "email" => $this->get("email"),
  122. "username" => $this->get("username"),
  123. "password" => $this->get("password"),
  124. "firstname" => $this->get("firstname"),
  125. "lastname" => $this->get("lastname"),
  126. "package_id" => $this->get("package_id"),
  127. "package_subscription" => $this->get("package_subscription"),
  128. "settings" => $this->get("settings"),
  129. "preferences" => $this->get("preferences"),
  130. "is_active" => $this->get("is_active"),
  131. "expire_date" => $this->get("expire_date"),
  132. "date" => $this->get("date"),
  133. "data" => $this->get("data"),
  134. ));
  135. return $this;
  136. }
  137. /**
  138. * Remove selected entry from database
  139. */
  140. public function delete()
  141. {
  142. if(!$this->isAvailable())
  143. return false;
  144. DB::table(TABLE_PREFIX.TABLE_USERS)->where("id", "=", $this->get("id"))->delete();
  145. $this->is_available = false;
  146. return true;
  147. }
  148. /**
  149. * Check if account has administrative privilages
  150. * @return boolean
  151. */
  152. public function isAdmin()
  153. {
  154. if ($this->isAvailable() && in_array($this->get("account_type"), array("developer", "admin"))) {
  155. return true;
  156. }
  157. return false;
  158. }
  159. /**
  160. * Checks if this user can edit another user's data
  161. *
  162. * @param UserModel $User Another user
  163. * @return boolean
  164. */
  165. public function canEdit(UserModel $User)
  166. {
  167. if ($this->isAvailable()) {
  168. if ($this->get("account_type") == "developer" || $this->get("id") == $User->get("id")) {
  169. return true;
  170. }
  171. if (
  172. $this->get("account_type") == "admin" &&
  173. (
  174. in_array($User->get("account_type"), array("member", "admin")) ||
  175. !$User->isAvailable() // New User
  176. )
  177. ) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. /**
  184. * Check if user is expired
  185. * @return boolean true on expired
  186. */
  187. public function isExpired()
  188. {
  189. if ($this->isAvailable()) {
  190. $ed = new DateTime($this->get("expire_date"));
  191. $now = new DateTime();
  192. if ($ed > $now) {
  193. return false;
  194. }
  195. }
  196. return true;
  197. }
  198. /**
  199. * get date-time format preference
  200. * @return null|string
  201. */
  202. public function getDateTimeFormat()
  203. {
  204. if (!$this->isAvailable()) {
  205. return null;
  206. }
  207. $date_format = $this->get("preferences.dateformat");
  208. $time_format = $this->get("preferences.timeformat") == "24"
  209. ? "H:i" : "h:i A";
  210. return $date_format . " " . $time_format;
  211. }
  212. /**
  213. * Check if user's (primary) email is verified or not
  214. * @return boolean
  215. */
  216. public function isEmailVerified()
  217. {
  218. if (!$this->isAvailable()) {
  219. return false;
  220. }
  221. if ($this->get("data.email_verification_hash")) {
  222. return false;
  223. }
  224. return true;
  225. }
  226. /**
  227. * Send verification email to the user
  228. * @param boolean $force_new Create a new hash if it's true
  229. * @return [bool]
  230. */
  231. public function sendVerificationEmail($force_new = false)
  232. {
  233. if (!$this->isAvailable()) {
  234. return false;
  235. }
  236. $hash = $this->get("data.email_verification_hash");
  237. if (!$hash || $force_new) {
  238. $hash = sha1(uniqid(readableRandomString(10), true));
  239. }
  240. // Get site settings
  241. $site_settings = \Controller::model("GeneralData", "settings");
  242. // Send mail
  243. $mail = new \Email;
  244. $mail->addAddress($this->get("email"));
  245. $mail->Subject = __("{site_name} Account Activation", [
  246. "{site_name}" => $site_settings->get("data.site_name")
  247. ]);
  248. $body = "<p>" . __("Hi %s", htmlchars($this->get("firstname"))) . ", </p>"
  249. . "<p>" . __("Please verify the email address {email} belongs to you. To do so, simply click the button below.", ["{email}" => "<strong>" . $this->get("email") . "</strong>"])
  250. . "<div style='margin-top: 30px; font-size: 14px; color: #9b9b9b'>"
  251. . "<a style='display: inline-block; background-color: #3b7cff; color: #fff; font-size: 14px; line-height: 24px; text-decoration: none; padding: 6px 12px; border-radius: 4px;' href='".APPURL."/verification/email/".$this->get("id").".".$hash."'>".__("Verify Email")."</a>"
  252. . "</div>";
  253. $mail->sendmail($body);
  254. // Save (new) hash
  255. $this->set("data.email_verification_hash", $hash)
  256. ->save();
  257. return true;
  258. }
  259. /**
  260. * Set the user's (primary) email address as verified
  261. */
  262. public function setEmailAsVerified()
  263. {
  264. if (!$this->isAvailable()) {
  265. return false;
  266. }
  267. $data = json_decode($this->get("data"));
  268. if (isset($data->email_verification_hash)) {
  269. unset($data->email_verification_hash);
  270. $this->set("data", json_encode($data))
  271. ->update();
  272. }
  273. return true;
  274. }
  275. }
  276. ?>