PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/controller/user.php

https://bitbucket.org/lxa478/qcrt
PHP | 192 lines | 154 code | 34 blank | 4 comment | 29 complexity | 3a062d40c83075afbf8877df9d847b10 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class user{
  3. function beforeRoute(){
  4. //Check Authentication
  5. if (!F3::get('SESSION.user') && F3::get('PARAMS[0]')!='/user/login' && F3::get('PARAMS[0]')!='/user/forgot'){
  6. F3::reroute('/user/login');
  7. }
  8. if(F3::get('SESSION.user') && F3::get('PARAMS[0]')!='/user/login' && F3::get('PARAMS[0]')!='/user/forgot'){
  9. $user = new Axon('user');
  10. $user->load('id="'.F3::get('SESSION.user').'"');
  11. if($user->active != 1){
  12. $this->logout();
  13. }
  14. }
  15. }
  16. public function home(){
  17. F3::set('menu','home');
  18. F3::set('header','html/user/header.html');
  19. F3::set('content','html/user/home.html');
  20. F3::set('footer','html/user/footer.html');
  21. F3::set('js','js/user/home.js');
  22. F3::set('html_title','User Home Page');
  23. $stores = new Axon('store');
  24. $astores = $stores->afind('active=1 AND site_id="'.F3::get('SESSION.site').'"');
  25. F3::set('stores', $astores);
  26. if(F3::get('PARAMS["store_id"]')){
  27. $my_store = new Axon('store');
  28. $my_store->load('id="'.F3::get('PARAMS["store_id"]').'" AND site_id="'.F3::get('SESSION.site').'"');
  29. if($my_store->dry() || $my_store->active!=1){
  30. F3::reroute('/user');
  31. }else{
  32. F3::set('store', $my_store);
  33. $window = new Axon('window');
  34. F3::set('windows', $window->afind('store_id="'.F3::get('PARAMS["store_id"]').'" AND type="window" AND active="1"'));
  35. $view = new Axon('window');
  36. F3::set('views', $view->afind('store_id="'.F3::get('PARAMS["store_id"]').'" AND type="view" AND active="1"'));
  37. $door = new Axon('door');
  38. $door->afind('store_id="'.F3::get('PARAMS["store_id"]').'"');
  39. F3::set('doors', $door->afind('store_id="'.F3::get('PARAMS["store_id"]').'" AND active="1"'));
  40. }
  41. }
  42. //Get Thumbnail Function
  43. F3::set('img_thumb', function($key){
  44. $path_parts = pathinfo($key);
  45. return $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension'];
  46. });
  47. echo Template::serve('html/user/layout.html');
  48. }
  49. public function account(){
  50. if($_POST){
  51. $user = new Axon('user');
  52. $user->load('id="'.F3::get('SESSION.user').'"');
  53. $user->email = $_POST['email'];
  54. $user->firstname = $_POST['firstname'];
  55. $user->lastname = $_POST['lastname'];
  56. if($_POST['password']!=''){
  57. $user->password = sha1($_POST['password']);
  58. }
  59. $user->save();
  60. }else{
  61. $user = new Axon('user');
  62. $user->load('id="'.F3::get('SESSION.user').'"');
  63. F3::set('user', $user);
  64. F3::set('menu','account');
  65. F3::set('header','html/user/header.html');
  66. F3::set('content','html/user/account.html');
  67. F3::set('footer','html/user/footer.html');
  68. F3::set('js','js/user/account.js');
  69. F3::set('html_title','User Account Page');
  70. echo Template::serve('html/user/layout.html');
  71. }
  72. }
  73. public function checkUserEmail(){
  74. $user = new Axon('user');
  75. $user->load('email="'.$_POST['email'].'"');
  76. if($user->dry()){
  77. echo "true";
  78. }else{
  79. if($user->id==$_POST['user_id']){
  80. echo "true";
  81. }else{
  82. echo "false";
  83. }
  84. }
  85. }
  86. public function login(){
  87. if($_POST){
  88. $email = $_POST['email'];
  89. $password = $_POST['password'];
  90. F3::set('AUTH', array('table'=>'user','id'=>'email','pw'=>'password'));
  91. $auth = Auth::sql($email, sha1($password));
  92. $sites = new Axon('site');
  93. $sites->load('active="1" AND subdomain="'.F3::get('subdomain').'"');
  94. if ($auth && $auth->active == 1 && ($auth->site_id == $sites->id || $auth->site_id == 0)) {
  95. F3::set('SESSION.token', $auth->user_token);
  96. F3::set('SESSION.user', $auth->id);
  97. F3::set('SESSION.site', $sites->id);
  98. return;
  99. } else {
  100. //User is not authenticated - send error
  101. header('HTTP/1.1 420 Invalid Login Credentials');
  102. return;
  103. }
  104. }else{
  105. F3::set('header','html/public/header.html');
  106. F3::set('content','html/user/login.html');
  107. F3::set('footer','html/public/footer.html');
  108. F3::set('js','js/user/login.js');
  109. F3::set('html_title','User Sign In');
  110. echo Template::serve('html/layout.html');
  111. }
  112. }
  113. public function logout(){
  114. if (F3::get('SESSION.user')){
  115. F3::set('SESSION.user', null);
  116. }
  117. if (F3::get('SESSION.token')){
  118. F3::set('SESSION.token', null);
  119. }
  120. F3::reroute('/');
  121. }
  122. public function forgot(){
  123. if($_POST){
  124. $email = $_POST['email'];
  125. $user = new Axon('user');
  126. $user->load('email="'.$email.'"');
  127. if ($user->email) {
  128. $arr = str_split('abcdefghkABCDEFGHK23456789'); // get all the characters into an array
  129. shuffle($arr); // randomize the array
  130. $arr = array_slice($arr, 0, 6); // get the first six (random) characters out
  131. $tempPw = implode('', $arr); // smush them back into a string
  132. $user->password = sha1($tempPw);
  133. $mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
  134. $mail->set('from','<support@qcrt.com>');
  135. $mail->set('reply-to', 'support@qcrt.com');
  136. $mail->set('x-mailer', 'PHP/' . phpversion());
  137. $mail->set('to', $admin->email);
  138. $mail->set('subject','Your Temporary Password');
  139. $mail->send("Here is your temporary password: ".$tempPw."\n\nPlease log in and update your password.");
  140. $user->save();
  141. return;
  142. } else {
  143. //User is not authenticated - send error
  144. header('HTTP/1.1 420 Email Not Found');
  145. return;
  146. }
  147. }else{
  148. F3::set('header','html/public/header.html');
  149. F3::set('content','html/user/forgot.html');
  150. F3::set('footer','html/public/footer.html');
  151. F3::set('js','js/user/forgot.js');
  152. F3::set('html_title','Forgot your password?');
  153. echo Template::serve('html/layout.html');
  154. }
  155. }
  156. function afterRoute(){}
  157. }
  158. ?>