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

/controller/admin.php

https://bitbucket.org/lxa478/qcrt
PHP | 320 lines | 258 code | 55 blank | 7 comment | 31 complexity | 3c643121a0808fbf859e1ea5326f7c03 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class admin{
  3. function beforeRoute(){
  4. //Check Authentication
  5. if (!F3::get('SESSION.admin') && F3::get('PARAMS[0]')!='/admin/login' && F3::get('PARAMS[0]')!='/admin/forgot'){
  6. F3::reroute('/admin/login');
  7. }
  8. if($_SERVER['HTTP_HOST'] != 'www.qcrt.com'){
  9. //F3::reroute('http://www.qcrt.com'.$_SERVER['REQUEST_URI']);
  10. }
  11. }
  12. public function login(){
  13. if($_POST){
  14. $username = $_POST['username'];
  15. $password = $_POST['password'];
  16. $sites = new Axon('site');
  17. $sites->load('active="1" AND subdomain="'.F3::get('subdomain').'"');
  18. F3::set('AUTH', array('table'=>'admin','id'=>'username','pw'=>'password'));
  19. $auth = Auth::sql($username, sha1($password));
  20. if ($auth && $auth->active=='1' && !$sites->dry()) {
  21. F3::set('SESSION.site', $sites->id);
  22. F3::set('SESSION.subdomain', $sites->subdomain);
  23. F3::set('SESSION.admin', $auth->id);
  24. return;
  25. } else {
  26. //User is not authenticated - send error
  27. header('HTTP/1.1 420 Invalid Login Credentials');
  28. return;
  29. }
  30. }else{
  31. F3::set('header','html/public/header.html');
  32. F3::set('content','html/admin/login.html');
  33. F3::set('footer','html/public/footer.html');
  34. F3::set('js','js/admin/login.js');
  35. F3::set('html_title','Administrator Sign In');
  36. echo Template::serve('html/layout.html');
  37. }
  38. }
  39. public function logout(){
  40. if (F3::get('SESSION.admin')){
  41. F3::set('SESSION.admin', null);
  42. }
  43. F3::reroute(F3::get('URL_BASE'));
  44. }
  45. public function forgot(){
  46. if($_POST){
  47. $email = $_POST['email'];
  48. $admin = new Axon('admin');
  49. $admin->load('email="'.$email.'"');
  50. if ($admin->email) {
  51. $arr = str_split('abcdefghkABCDEFGHK23456789'); // get all the characters into an array
  52. shuffle($arr); // randomize the array
  53. $arr = array_slice($arr, 0, 6); // get the first six (random) characters out
  54. $tempPw = implode('', $arr); // smush them back into a string
  55. $admin->password = sha1($tempPw);
  56. $mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
  57. $mail->set('from','<support@qcrt.com>');
  58. $mail->set('reply-to', 'support@qcrt.com');
  59. $mail->set('x-mailer', 'PHP/' . phpversion());
  60. $mail->set('to', $admin->email);
  61. $mail->set('subject','Your Username and Temporary Password');
  62. $mail->send("Here is your username and temporary password:\n\nUsername: ".$admin->username."\nPassword: ".$tempPw."\n\nPlease log in and update your password.");
  63. $admin->save();
  64. return;
  65. } else {
  66. //User is not authenticated - send error
  67. header('HTTP/1.1 420 Email Not Found');
  68. return;
  69. }
  70. }else{
  71. F3::set('header','html/public/header.html');
  72. F3::set('content','html/admin/forgot.html');
  73. F3::set('footer','html/public/footer.html');
  74. F3::set('js','js/admin/forgot.js');
  75. F3::set('html_title','Forgot your username or password?');
  76. echo Template::serve('html/layout.html');
  77. }
  78. }
  79. public function home(){
  80. F3::set('menu','home');
  81. F3::set('header','html/admin/header.html');
  82. $admin = new Axon('admin');
  83. $admin->load('id="'.F3::get('SESSION.admin').'"');
  84. F3::set('admin', $admin);
  85. F3::set('header','html/admin/header.html');
  86. F3::set('content','html/admin/home.html');
  87. F3::set('footer','html/admin/footer.html');
  88. F3::set('js','js/admin/home.js');
  89. F3::set('html_title','Admin Page');
  90. echo Template::serve('html/admin/layout.html');
  91. }
  92. public function account(){
  93. $admin = new Axon('admin');
  94. $admin->load('id="'.F3::get('SESSION.admin').'"');
  95. F3::set('menu','account');
  96. F3::set('admin', $admin);
  97. F3::set('header','html/admin/header.html');
  98. F3::set('content','html/admin/account.html');
  99. F3::set('footer','html/admin/footer.html');
  100. F3::set('js','js/admin/account.js');
  101. F3::set('html_title','Admin Page');
  102. echo Template::serve('html/admin/layout.html');
  103. }
  104. public function users(){
  105. $users = new Axon('user');
  106. $ausers = $users->afind('site_id="'.F3::get('SESSION.site').'" OR site_id=0');
  107. F3::set('users', $ausers);
  108. F3::set('menu','users');
  109. F3::set('header','html/admin/header.html');
  110. F3::set('content','html/admin/users.html');
  111. F3::set('footer','html/admin/footer.html');
  112. F3::set('js','js/admin/users.js');
  113. F3::set('html_title','Admin Page');
  114. echo Template::serve('html/admin/layout.html');
  115. }
  116. public function change_status(){
  117. if(isset($_POST['user_id']) && $_POST['user_id']!=''){
  118. $user = new Axon('user');
  119. $user->load('id="'.$_POST['user_id'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
  120. $user->active = $_POST['status'];
  121. $user->save();
  122. }
  123. return;
  124. }
  125. public function edit_user(){
  126. if($_POST){
  127. $user = new Axon('user');
  128. $user->load('id="'.$_POST['user_id'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
  129. $user->firstname = $_POST['firstname'];
  130. $user->lastname = $_POST['lastname'];
  131. $user->email = $_POST['email'];
  132. $user->active = isset($_POST['status']) ? 1 : 0;
  133. $user->site_id = isset($_POST['is_global']) ? $_POST['is_global'] : F3::get('SESSION.site');
  134. if(isset($_POST['auto_password'])){
  135. $arr = str_split('abcdefghkABCDEFGHK23456789'); // get all the characters into an array
  136. shuffle($arr); // randomize the array
  137. $arr = array_slice($arr, 0, 6); // get the first six (random) characters out
  138. $tempPw = implode('', $arr); // smush them back into a string
  139. $user->password = sha1($tempPw);
  140. //Send Temporary Password In Email
  141. $mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
  142. $mail->set('from','<support@qcrt.com>');
  143. $mail->set('reply-to', 'support@qcrt.com');
  144. $mail->set('x-mailer', 'PHP/' . phpversion());
  145. $mail->set('to', $_POST['email']);
  146. $mail->set('subject','Your Temporary Password');
  147. $mail->send("Here is your temporary password:\n\Email: ".$_POST['email']."\nPassword: ".$tempPw."\n\nPlease log in and update your password.");
  148. }else if($_POST['password']!='' && $_POST['password_confirm']!='' && $_POST['password']==$_POST['password_confirm']){
  149. $user->password = sha1($_POST['password']);
  150. }
  151. $user->save();
  152. F3::reroute('/admin/users');
  153. return;
  154. }else{
  155. $user = new Axon('user');
  156. $user->load('id="'.F3::get('PARAMS["id"]').'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
  157. if($user->dry()){
  158. F3::reroute('/admin/users');
  159. }else{
  160. F3::set('user', $user);
  161. F3::set('menu','users');
  162. F3::set('header','html/admin/header.html');
  163. F3::set('content','html/admin/users/edit.html');
  164. F3::set('footer','html/admin/footer.html');
  165. F3::set('js','js/admin/users/edit.js');
  166. F3::set('html_title','Edit User Page');
  167. echo Template::serve('html/admin/layout.html');
  168. }
  169. }
  170. }
  171. public function new_user(){
  172. if($_POST){
  173. $user = new Axon('user');
  174. $user->firstname = $_POST['firstname'];
  175. $user->lastname = $_POST['lastname'];
  176. $user->email = $_POST['email'];
  177. $user->active = 1;
  178. $user->admin_id = F3::get('SESSION.admin');
  179. $user->site_id = isset($_POST['is_global']) ? $_POST['is_global'] : F3::get('SESSION.site');
  180. if(isset($_POST['auto_password'])){
  181. $arr = str_split('abcdefghkABCDEFGHK23456789'); // get all the characters into an array
  182. shuffle($arr); // randomize the array
  183. $arr = array_slice($arr, 0, 6); // get the first six (random) characters out
  184. $tempPw = implode('', $arr); // smush them back into a string
  185. $user->password = sha1($tempPw);
  186. //Send Temporary Password In Email
  187. $mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
  188. $mail->set('from','<support@qcrt.com>');
  189. $mail->set('reply-to', 'support@qcrt.com');
  190. $mail->set('x-mailer', 'PHP/' . phpversion());
  191. $mail->set('to', $_POST['email']);
  192. $mail->set('subject','Your Temporary Password');
  193. $mail->send("Here is your temporary password:\n\Email: ".$_POST['email']."\nPassword: ".$tempPw."\n\nPlease log in and update your password.");
  194. }else{
  195. $user->password = sha1($_POST['password']);
  196. }
  197. if(isset($_POST['welcome_email'])){
  198. //Send Welcome Email
  199. $mail=new SMTP('mail.directedgemedia.com',465,'SSL','jake@directedgemedia.com','myers478');
  200. $mail->set('from','<support@qcrt.com>');
  201. $mail->set('reply-to', 'support@qcrt.com');
  202. $mail->set('x-mailer', 'PHP/' . phpversion());
  203. $mail->set('to', $_POST['email']);
  204. $mail->set('subject','Welcome to QCRT');
  205. $mail->send("Welcome email from QCRT.com");
  206. }
  207. $user->save();
  208. F3::reroute('/admin/users');
  209. return;
  210. }else{
  211. F3::set('menu','users');
  212. F3::set('header','html/admin/header.html');
  213. F3::set('content','html/admin/users/new.html');
  214. F3::set('footer','html/admin/footer.html');
  215. F3::set('js','js/admin/users/new.js');
  216. F3::set('html_title','New User Page');
  217. echo Template::serve('html/admin/layout.html');
  218. }
  219. }
  220. public function delete_user(){
  221. if(isset($_POST['user_id']) && $_POST['user_id']!=''){
  222. $user = new Axon('user');
  223. $user->load('id="'.$_POST['user_id'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
  224. $user->erase();
  225. echo $_POST['user_id'];
  226. }
  227. }
  228. public function checkNewUserEmail(){
  229. $user = new Axon('user');
  230. $user->load('email="'.$_POST['email'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
  231. if($user->dry()){
  232. echo "true";
  233. }else{
  234. echo "false";
  235. }
  236. }
  237. public function checkEditUserEmail(){
  238. $user = new Axon('user');
  239. $user->load('email="'.$_POST['email'].'" AND (site_id="'.F3::get('SESSION.site').'" OR site_id=0)');
  240. if($user->dry()){
  241. echo "true";
  242. }else{
  243. if($user->id==$_POST['user_id']){
  244. echo "true";
  245. }else{
  246. echo "false";
  247. }
  248. }
  249. }
  250. public function update(){
  251. $admin = new Axon('admin');
  252. $admin->load('id="'.F3::get('SESSION.admin').'"');
  253. $admin->email = $_POST['email'];
  254. $admin->username = $_POST['username'];
  255. $admin->firstname = $_POST['firstname'];
  256. $admin->lastname = $_POST['lastname'];
  257. if($_POST['password']!=''){
  258. $admin->password = sha1($_POST['password']);
  259. }
  260. $admin->save();
  261. return;
  262. }
  263. function afterRoute(){}
  264. }
  265. ?>