Guest User

Untitled

a guest
Dec 11th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. <?php
  2.  
  3. class User extends CActiveRecord
  4. {
  5. public $newPassword;
  6. public $encryption_key;
  7.  
  8. private $_public_display;
  9.  
  10. ###############################
  11. ### Yii Generated Functions ###
  12. ###############################
  13.  
  14. public static function model($className=__CLASS__)
  15. {
  16. return parent::model($className);
  17. }
  18.  
  19. public function tableName()
  20. {
  21. return 'tbl_user';
  22. }
  23.  
  24. public function beforeSave() {
  25. if(!empty($this->newPassword)) {
  26. $this->password = hash('sha256', $this->newPassword);
  27. }
  28.  
  29. return true;
  30. }
  31.  
  32. public function rules()
  33. {
  34. return array(
  35. array('username, newPassword', 'length', 'max'=>100),
  36. array('newPassword, encryption_key', 'safe'),
  37. array('email', 'email'),
  38. array('id, username, password, email, time_created, number_of_invites, activation_code, activated', 'safe', 'on'=>'search'),
  39. );
  40. }
  41.  
  42. public function relations()
  43. {
  44. return array(
  45. 'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
  46. 'bookmarks' => array(self::HAS_MANY, 'Bookmark', 'user_id'),
  47. 'badges' => array(self::HAS_MANY, 'UserBadge', 'user_id'),
  48. 'social' => array(self::HAS_ONE, 'SocialNames', 'user_id'),
  49. );
  50. }
  51.  
  52. public function attributeLabels()
  53. {
  54. return array(
  55. 'id' => 'ID',
  56. 'username' => 'Username',
  57. 'newPassword' => 'New Password',
  58. 'email' => 'Email',
  59. 'time_created' => 'Time Created',
  60. 'number_of_invites' => 'Number Of Invites',
  61. 'activation_code' => 'Activation Code',
  62. 'activated' => 'Activated',
  63. 'new_secretStash' => 'New Secret Stash Password',
  64. 'new_secretStashC' => 'Confirm Secret Stash Password',
  65. 'IV' => 'Encryption IV',
  66. 'encryption_key' => 'Bookmark Key',
  67. 'invite_code' => 'Invitation Code',
  68. );
  69. }
  70.  
  71. public function search()
  72. {
  73.  
  74. $criteria=new CDbCriteria;
  75.  
  76. $criteria->compare('id',$this->id);
  77. $criteria->compare('username',$this->username,true);
  78. $criteria->compare('password',$this->password,true);
  79. $criteria->compare('email',$this->email,true);
  80. $criteria->compare('time_created',$this->time_created,true);
  81. $criteria->compare('number_of_invites',$this->number_of_invites);
  82. $criteria->compare('activation_code',$this->activation_code,true);
  83. $criteria->compare('activated',$this->activated);
  84.  
  85. return new CActiveDataProvider(get_class($this), array(
  86. 'criteria'=>$criteria,
  87. ));
  88. }
  89.  
  90. ############################
  91. ### User Model Functions ###
  92. ############################
  93.  
  94. /*
  95. * Reset the password.
  96. */
  97. public function resetPassword() {
  98. $forgotten_password = new ForgottenPassword();
  99. $forgotten_password->user_id = $this->id;
  100. $forgotten_password->new_password = substr(md5(time()), 15);
  101. $forgotten_password->code = substr(md5(time() * 60), 15);
  102. $forgotten_password->save();
  103.  
  104. $this->sendPasswordEmail($forgotten_password);
  105. }
  106.  
  107. /*
  108. * Send the actual new password to the e-mail.
  109. */
  110. public function sendNewPasswordEmail($forgotten_password) {
  111. $user = $this;
  112. $username = $user->username;
  113. $email = $user->email;
  114.  
  115. $user->password = hash('sha256', $forgotten_password->new_password);
  116. $user->save();
  117.  
  118. $template = <<<HTML
  119. Dear {username},
  120.  
  121. Below is your new password for use at {app}. We recommend you change this after logging in for the first time.
  122.  
  123. <table>
  124. <tr>
  125. <td><strong>Username:</strong></td>
  126. <td>{username}</td>
  127. </tr>
  128.  
  129. <tr>
  130. <td><strong>New Password:</strong></td>
  131. <td>{password}</td>
  132. </tr>
  133. </table><br /><br />
  134.  
  135. Thank you,<br />
  136. The {app} team.
  137. HTML;
  138.  
  139. $template = str_replace("{app}", Yii::app()->name, $template);
  140. $template = str_replace("{username}", $username, $template);
  141. $template = str_replace("{password}", $forgotten_password->new_password, $template);
  142.  
  143. $forgotten_password->delete();
  144.  
  145. try {
  146. Postmark::compose()
  147. ->addTo($email, $username)
  148. ->subject(Yii::app()->name . ' - New Password')
  149. ->messageHtml($template)
  150. ->send();
  151. } catch(Exception $ex) {
  152.  
  153. }
  154. }
  155.  
  156. /*
  157. * Send password reset e-mail.
  158. */
  159. public function sendPasswordEmail($forgotten_password) {
  160. $user = $this;
  161. $username = $user->username;
  162. $email = $user->email;
  163. $reset_password_url = Yii::app()->params->siteUrl . "user/forgotPassword?code=" . $forgotten_password->code;
  164.  
  165. $template = <<<HTML
  166. Dear {username},
  167.  
  168. You have requested a new password at {app}. Please click the link below to have your new password emailed to you.
  169.  
  170. <table>
  171. <tr>
  172. <td><strong>Username:</strong></td>
  173. <td>{username}</td>
  174. </tr>
  175.  
  176. <tr>
  177. <td><strong>Reset Link:</strong></td>
  178. <td>{link}</td>
  179. </tr>
  180. </table><br /><br />
  181.  
  182. Thank you,<br />
  183. The {app} team.
  184. HTML;
  185.  
  186. $template = str_replace("{app}", Yii::app()->name, $template);
  187. $template = str_replace("{username}", $username, $template);
  188. $template = str_replace("{link}", $reset_password_url, $template);
  189.  
  190. try {
  191. Postmark::compose()
  192. ->addTo($email, $username)
  193. ->subject(Yii::app()->name . ' - Forgotten Password')
  194. ->messageHtml($template)
  195. ->send();
  196. } catch(Exception $ex) {
  197.  
  198. }
  199. }
  200.  
  201. /*
  202. * Sends activation e-mail.
  203. */
  204. public function sendActivationEmail() {
  205. $user = $this;
  206. $username = $user->username;
  207. $email = $user->email;
  208. $activation_code = $user->activation_code;
  209. $activation_url = Yii::app()->params->siteUrl . 'user/activate?code=' . $activation_code;
  210.  
  211. $template = <<<HTML
  212. Thank you {username} for registering at {app}!<br /><br />
  213. Before we enable all of the features that {app} has to offer, we need you to first confirm your email address for us.<br /><br />
  214.  
  215. This e-mail is <strong>VERY</strong> important. If you have chosen the "Grazely Managed" Bookmark Key, this e-mail (activation code) is the <strong><u>ONLY</u></strong> way to recover it.<br /><br />
  216.  
  217. <table>
  218. <tr>
  219. <td><strong>Username:</strong></td>
  220. <td>{username}</td>
  221. </tr>
  222.  
  223. <tr>
  224. <td><strong>Activation Code:</strong></td>
  225. <td>{activation_code}</td>
  226. </tr>
  227.  
  228. <tr>
  229. <td><strong>Activation Link:</strong></td>
  230. <td>{activation_url}</td>
  231. </tr>
  232. </table><br /><br />
  233.  
  234. Thank you,<br />
  235. The {app} team.
  236. HTML;
  237.  
  238. $template = str_replace("{app}", Yii::app()->name, $template);
  239. $template = str_replace("{username}", $username, $template);
  240. $template = str_replace("{activation_code}", $activation_code, $template);
  241. $template = str_replace("{activation_url}", $activation_url, $template);
  242.  
  243.  
  244. try {
  245. Postmark::compose()
  246. ->addTo($email, $username)
  247. ->subject(Yii::app()->name . ' - User Activation')
  248. ->messageHtml($template)
  249. ->send();
  250. } catch(Exception $ex) {
  251.  
  252. }
  253. }
  254.  
  255. /*
  256. * Determines a user's public display name.
  257. */
  258. public function publicDisplay() {
  259.  
  260. if(!empty($this->_public_display)) {
  261. return $this->_public_display;
  262. }
  263.  
  264. $first_name = $this->profile->first_name;
  265. $last_name = $this->profile->last_name;
  266.  
  267. if(!empty($first_name) && !empty($last_name)) {
  268. $this->_public_display = $first_name . ' ' . $last_name;
  269. return $this->_public_display;
  270. }
  271.  
  272. if(!empty($first_name)) {
  273. $this->_public_display = ($first_name != $this->username) ? $first_name . ' (' . $this->username . ')' : $first_name;
  274. return $this->_public_display;
  275. }
  276.  
  277. $this->_public_display = $this->username;
  278.  
  279. return $this->username;
  280. }
  281.  
  282. public function generateBookmarklet() {
  283. $template = <<<HTML
  284. <a id="bookmarklet_link" href="javascript:(function(){ function get_text%28%29 {var text %3D %27%27%3Bif%28window.getSelection%28%29%29 {text %3D window.getSelection%28%29%3B} else if%28document.getSelection%29 {text %3D document.getSelection%28%29%3B} else if%28document.selection%29 {text %3D document.selection.createRange%28%29.text%3B} else {return%3B}if%28text %3D%3D %27%27%29 {return false%3B} else {return text%3B}}var text %3D get_text%28%29%3Bif%28text %3D%3D false%29 {text %3D %27%27%3B}var api_key %3D %27{api_key}%27%3Bvar title %3D document.title%3Bvar url %3D window.location.href%3Bvar %24%24%24ftw11425%3Dwindow.open%28%27{site_url}bookmark/widget%3Fapi_key%3D%27+api_key+%27%26title%3D%27+title+%27%26url%3D%27+url+%27%26text%3D%27 + text%2C%27grazley%27%2C%27width%3D520%2Cheight%3D600%2Cscrollbars%3Dyes%2Ctoolbar%3Dno%2Cstatus%3Dno%2Clocation%3Dno%2Cmenubar%3Dno%2Cresizable%3Dyes%27%29%3B%24%24%24ftw11425.focus%28%29})();">
  285. Grazely!
  286. </a>
  287. HTML;
  288.  
  289. $template = str_replace('{api_key}', APIKey::model()->findByAttributes(array("user_id" => Yii::app()->user->id))->api_key, $template);
  290. $template = str_replace('{site_url}', Yii::app()->params->siteUrl, $template);
  291.  
  292. return $template;
  293. }
  294.  
  295. /*
  296. * Determines if a user has access to encryption.
  297. */
  298. public function hasEncryption() {
  299. if(!empty(Yii::app()->user->encryption_key) && empty(Yii::app()->user->encryption_disabled)) {
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. }
  305.  
  306. /*
  307. * Returns the model of the current user.
  308. */
  309. public function currentUser() {
  310. return $this->findByPk(Yii::app()->user->id);
  311. }
  312.  
  313. public function giveBadge($badge_id) {
  314. $user_badge = new UserBadge();
  315.  
  316. $user_badge->user_id = $this->id;
  317.  
  318. $user_badge->badge_id = $badge_id;
  319.  
  320. $user_badge->save();
  321. }
  322.  
  323. public function hasBadge($badge_id) {
  324. $user_badge = UserBadge::model()->findByAttributes(array('badge_id' => $badge_id, 'user_id' => $this->id));
  325.  
  326. if($user_badge == null) {
  327. return false;
  328. } else {
  329. return true;
  330. }
  331. }
  332. }
Add Comment
Please, Sign In to add comment