Advertisement
Guest User

Untitled

a guest
Dec 26th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 37.85 KB | None | 0 0
  1. <?php
  2.  
  3. class UserController extends AppController
  4. {
  5.  
  6.     public $components = array('Session', 'Captcha', 'API');
  7.  
  8.     function get_captcha()
  9.     {
  10.         $this->autoRender = false;
  11.         App::import('Component', 'Captcha');
  12.  
  13.         //generate random charcters for captcha
  14.         $random = mt_rand(100, 99999);
  15.  
  16.         //save characters in session
  17.         $this->Session->write('captcha_code', $random);
  18.  
  19.         $settings = array(
  20.             'characters' => $random,
  21.             'winHeight' => 50,         // captcha image height
  22.             'winWidth' => 220,           // captcha image width
  23.             'fontSize' => 25,          // captcha image characters fontsize
  24.             'fontPath' => WWW_ROOT . 'tahomabd.ttf',    // captcha image font
  25.             'noiseColor' => '#ccc',
  26.             'bgColor' => '#fff',
  27.             'noiseLevel' => '100',
  28.             'textColor' => '#000'
  29.         );
  30.  
  31.         $img = $this->Captcha->ShowImage($settings);
  32.         echo $img;
  33.     }
  34.  
  35.     function ajax_register()
  36.     {
  37.         $this->autoRender = false;
  38.         $this->response->type('json');
  39.         if ($this->request->is('Post')) { // si la requête est bien un post
  40.             if (!empty($this->request->data['pseudo']) && !empty($this->request->data['password']) && !empty($this->request->data['password_confirmation']) && !empty($this->request->data['email'])) { // si tout les champs sont bien remplis
  41.  
  42.                 // Captcha
  43.                 if ($this->Configuration->getKey('captcha_type') == "2") { // ReCaptcha
  44.  
  45.                     $validCaptcha = $this->Util->isValidReCaptcha($this->request->data['recaptcha'], $this->Util->getIP(), $this->Configuration->getKey('captcha_google_secret'));
  46.  
  47.                 } else {
  48.  
  49.                     $captcha = $this->Session->read('captcha_code');
  50.                     $validCaptcha = (!empty($captcha) && $captcha == $this->request->data['captcha']);
  51.  
  52.                 }
  53.                 //
  54.  
  55.                 if ($validCaptcha) { // on check le captcha déjà
  56.                     $this->loadModel('User');
  57.                     $isValid = $this->User->validRegister($this->request->data, $this->Util);
  58.                     if ($isValid === true) { // on vérifie si y'a aucune erreur
  59.  
  60.                         $eventData = $this->request->data;
  61.                         $eventData['password'] = $this->Util->password($eventData['password'], $eventData['pseudo']);
  62.                         $event = new CakeEvent('beforeRegister', $this, array('data' => $eventData));
  63.                         $this->getEventManager()->dispatch($event);
  64.                         if ($event->isStopped()) {
  65.                             return $event->result;
  66.                         }
  67.  
  68.                         // on enregistre
  69.                         $userSession = $this->User->register($this->request->data, $this->Util);
  70.  
  71.                         // On envoie le mail de confirmation si demandé
  72.                         if ($this->Configuration->getKey('confirm_mail_signup')) {
  73.  
  74.                             $confirmCode = substr(md5(uniqid()), 0, 12);
  75.  
  76.                             $emailMsg = $this->Lang->get('EMAIL__CONTENT_CONFIRM_MAIL', array(
  77.                                 '{LINK}' => Router::url('/user/confirm/', true) . $confirmCode,
  78.                                 '{IP}' => $this->Util->getIP(),
  79.                                 '{USERNAME}' => $this->request->data['pseudo'],
  80.                                 '{DATE}' => $this->Lang->date(date('Y-m-d H:i:s'))
  81.                             ));
  82.  
  83.                             $email = $this->Util->prepareMail(
  84.                                 $this->request->data['email'],
  85.                                 $this->Lang->get('EMAIL__TITLE_CONFIRM_MAIL'),
  86.                                 $emailMsg
  87.                             )->sendMail();
  88.  
  89.                             if ($email) {
  90.  
  91.                                 $this->User->read(null, $this->User->getLastInsertID());
  92.                                 $this->User->set(array('confirmed' => $confirmCode));
  93.                                 $this->User->save();
  94.  
  95.                             }
  96.  
  97.                         }
  98.  
  99.                         if (!$this->Configuration->getKey('confirm_mail_signup_block')) { // si on doit pas bloquer le compte si non confirmé
  100.                             // on prépare la connexion
  101.                             $this->Session->write('user', $userSession);
  102.  
  103.                             $event = new CakeEvent('onLogin', $this, array('user' => $this->User->getAllFromCurrentUser(), 'register' => true));
  104.                             $this->getEventManager()->dispatch($event);
  105.                             if ($event->isStopped()) {
  106.                                 return $event->result;
  107.                             }
  108.                         }
  109.  
  110.                         // on dis que c'est bon
  111.                         $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('USER__REGISTER_SUCCESS'))));
  112.  
  113.                     } else { // si c'est pas bon, on envoie le message d'erreur retourné par l'étape de validation
  114.                         $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get($isValid))));
  115.                     }
  116.                 } else {
  117.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('FORM__INVALID_CAPTCHA'))));
  118.                 }
  119.             } else {
  120.                 $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));
  121.             }
  122.         } else {
  123.             $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__BAD_REQUEST'))));
  124.         }
  125.     }
  126.  
  127.     function ajax_login()
  128.     {
  129.         if (!$this->request->is('post'))
  130.             throw new BadRequestException();
  131.         if (empty($this->request->data['pseudo']) || empty($this->request->data['password']))
  132.             return $this->sendJSON(['statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')]);
  133.         $this->autoRender = false;
  134.         $this->response->type('json');
  135.  
  136.         $confirmEmailIsNeeded = ($this->Configuration->getKey('confirm_mail_signup') && $this->Configuration->getKey('confirm_mail_signup_block'));
  137.         $login = $this->User->login($this->request->data, $confirmEmailIsNeeded, $this);
  138.         if (!isset($login['status']) || $login['status'] !== true)
  139.             return $this->sendJSON(['statut' => false, 'msg' => $this->Lang->get($login, array('{URL_RESEND_EMAIL}' => Router::url(array('action' => 'resend_confirmation'))))]);
  140.  
  141.         $event = new CakeEvent('onLogin', $this, array('user' => $this->User->getAllFromUser($this->request->data['pseudo'])));
  142.         $this->getEventManager()->dispatch($event);
  143.         if ($event->isStopped())
  144.             return $event->result;
  145.  
  146.         if ($this->request->data['remember_me'])
  147.             $this->Cookie->write('remember_me', array('pseudo' => $this->request->data['pseudo'], 'password' => $this->User->getFromUser('password', $this->request->data['pseudo'])), true, '1 week');
  148.         $this->Session->write('user', $login['session']);
  149.  
  150.         $this->sendJSON(['statut' => true, 'msg' => $this->Lang->get('USER__REGISTER_LOGIN')]);
  151.     }
  152.  
  153.     function confirm($code = false)
  154.     {
  155.         $this->autoRender = false;
  156.         if (isset($code)) {
  157.  
  158.             $find = $this->User->find('first', array('conditions' => array('confirmed' => $code)));
  159.  
  160.             if (!empty($find)) {
  161.  
  162.                 $event = new CakeEvent('beforeConfirmAccount', $this, array('user_id' => $find['User']['id']));
  163.                 $this->getEventManager()->dispatch($event);
  164.                 if ($event->isStopped()) {
  165.                     return $event->result;
  166.                 }
  167.  
  168.                 $this->User->read(null, $find['User']['id']);
  169.                 $this->User->set(array('confirmed' => date('Y-m-d H:i:s')));
  170.                 $this->User->save();
  171.  
  172.                 $userSession = $find['User']['id'];
  173.  
  174.                 $this->loadModel('Notification');
  175.                 $this->Notification->setToUser($this->Lang->get('USER__CONFIRM_NOTIFICATION'), $find['User']['id']);
  176.  
  177.                 $this->Session->write('user', $userSession);
  178.  
  179.                 $event = new CakeEvent('onLogin', $this, array('user' => $this->User->getAllFromCurrentUser(), 'confirmAccount' => true));
  180.                 $this->getEventManager()->dispatch($event);
  181.                 if ($event->isStopped()) {
  182.                     return $event->result;
  183.                 }
  184.  
  185.                 $this->redirect(array('action' => 'profile'));
  186.  
  187.             } else {
  188.                 throw new NotFoundException();
  189.             }
  190.  
  191.         } else {
  192.             throw new NotFoundException();
  193.         }
  194.     }
  195.  
  196.     function ajax_lostpasswd()
  197.     {
  198.         $this->layout = null;
  199.         $this->autoRender = false;
  200.         $this->response->type('json');
  201.         if ($this->request->is('ajax')) {
  202.             if (!empty($this->request->data['email'])) {
  203.                 $this->loadModel('User');
  204.                 if (filter_var($this->request->data['email'], FILTER_VALIDATE_EMAIL)) {
  205.                     $search = $this->User->find('first', array('conditions' => array('email' => $this->request->data['email'])));
  206.                     if (!empty($search)) {
  207.                         $this->loadModel('Lostpassword');
  208.                         $key = substr(md5(rand() . date('sihYdm')), 0, 10);
  209.  
  210.                         $to = $this->request->data['email'];
  211.                         $subject = $this->Lang->get('USER__PASSWORD_RESET_LINK');
  212.                         $message = $this->Lang->get('USER__PASSWORD_RESET_EMAIL_CONTENT', array(
  213.                             '{EMAIL}' => $this->request->data['email'],
  214.                             '{PSEUDO}' => $search['User']['pseudo'],
  215.                             '{LINK}' => Router::url('/?resetpasswd_' . $key, true)
  216.                         ));
  217.  
  218.  
  219.                         $event = new CakeEvent('beforeSendResetPassMail', $this, array('user_id' => $search['User']['id'], 'key' => $key));
  220.                         $this->getEventManager()->dispatch($event);
  221.                         if ($event->isStopped()) {
  222.                             return $event->result;
  223.                         }
  224.  
  225.  
  226.                         if ($this->Util->prepareMail($to, $subject, $message)->sendMail()) {
  227.                             $this->Lostpassword->create();
  228.                             $this->Lostpassword->set(array(
  229.                                 'email' => $this->request->data['email'],
  230.                                 'key' => $key
  231.                             ));
  232.                             $this->Lostpassword->save();
  233.                             $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('USER__PASSWORD_FORGOT_EMAIL_SUCCESS'))));
  234.                         } else {
  235.                             $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__INTERNAL_ERROR'))));
  236.                         }
  237.                     } else {
  238.                         $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('USER__ERROR_NOT_FOUND'))));
  239.                     }
  240.                 } else {
  241.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('USER__ERROR_EMAIL_NOT_VALID'))));
  242.                 }
  243.             } else {
  244.                 $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));
  245.             }
  246.         } else {
  247.             $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__BAD_REQUEST'))));
  248.         }
  249.     }
  250.  
  251.     function ajax_resetpasswd()
  252.     {
  253.         $this->autoRender = false;
  254.         $this->response->type('json');
  255.         if ($this->request->is('ajax')) {
  256.             if (!empty($this->request->data['password']) AND !empty($this->request->data['password2']) AND !empty($this->request->data['email']) && !empty($this->request->data['key'])) {
  257.  
  258.                 $reset = $this->User->resetPass($this->request->data, $this);
  259.                 if (isset($reset['status']) && $reset['status'] === true) {
  260.                     $this->Session->write('user', $reset['session']);
  261.  
  262.                     $this->History->set('RESET_PASSWORD', 'user');
  263.  
  264.                     $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('USER__PASSWORD_RESET_SUCCESS'))));
  265.                 } else {
  266.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get($reset))));
  267.                 }
  268.             } else {
  269.                 $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));
  270.             }
  271.         } else {
  272.             $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__BAD_REQUEST'))));
  273.         }
  274.     }
  275.  
  276.     function logout()
  277.     {
  278.         $this->autoRender = false;
  279.  
  280.         $event = new CakeEvent('onLogout', $this, array('session' => $this->Session->read('user')));
  281.         $this->getEventManager()->dispatch($event);
  282.         if ($event->isStopped()) {
  283.             return $event->result;
  284.         }
  285.  
  286.         if ($this->Cookie->read('remember_me')) {
  287.             $this->Cookie->delete('remember_me');
  288.         }
  289.  
  290.         $this->Session->delete('user');
  291.         $this->redirect($this->referer());
  292.     }
  293.  
  294.     function uploadSkin()
  295.     {
  296.         $this->autoRender = false;
  297.         $this->response->type('json');
  298.  
  299.         if ($this->isConnected && $this->API->can_skin()) {
  300.             if ($this->request->is('post')) {
  301.  
  302.                 $skin_max_size = 10000000; // octet
  303.  
  304.                 $this->loadModel('ApiConfiguration');
  305.                 $ApiConfiguration = $this->ApiConfiguration->find('first');
  306.                 $target_config = $ApiConfiguration['ApiConfiguration']['skin_filename'];
  307.  
  308.                 $filename = substr($target_config, (strrpos($target_config, '/') + 1));
  309.                 $filename = str_replace('{PLAYER}', $this->User->getKey('pseudo'), $filename);
  310.                 $filename = str_replace('php', '', $filename);
  311.                 $filename = str_replace('.', '', $filename);
  312.                 $filename = $filename . '.png';
  313.  
  314.                 $target = substr($target_config, 0, (strrpos($target_config, '/') + 1));
  315.                 $target = WWW_ROOT . '/' . $target;
  316.  
  317.                 $width_max = $ApiConfiguration['ApiConfiguration']['skin_width']; // pixel
  318.                 $height_max = $ApiConfiguration['ApiConfiguration']['skin_height']; // pixel
  319.  
  320.                 $isValidImg = $this->Util->isValidImage($this->request, array('png'), $width_max, $height_max, $skin_max_size);
  321.  
  322.                 if (!$isValidImg['status']) {
  323.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $isValidImg['msg'])));
  324.                     return;
  325.                 } else {
  326.                     $infos = $isValidImg['infos'];
  327.                 }
  328.  
  329.                 if (!$this->Util->uploadImage($this->request, $target . $filename)) {
  330.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('FORM__ERROR_WHEN_UPLOAD'))));
  331.                     return;
  332.                 }
  333.  
  334.                 $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('API__UPLOAD_SKIN_SUCCESS'))));
  335.  
  336.             }
  337.  
  338.         } else {
  339.             throw new ForbiddenException();
  340.         }
  341.     }
  342.  
  343.     function uploadCape()
  344.     {
  345.         $this->autoRender = false;
  346.         $this->response->type('json');
  347.  
  348.         if ($this->isConnected && $this->API->can_cape()) {
  349.             if ($this->request->is('post')) {
  350.  
  351.                 $cape_max_size = 10000000; // octet
  352.  
  353.                 $this->loadModel('ApiConfiguration');
  354.                 $ApiConfiguration = $this->ApiConfiguration->find('first');
  355.                 $target_config = $ApiConfiguration['ApiConfiguration']['cape_filename'];
  356.  
  357.                 $filename = substr($target_config, (strrpos($target_config, '/') + 1));
  358.                 $filename = str_replace('{PLAYER}', $this->User->getKey('pseudo'), $filename);
  359.                 $filename = str_replace('php', '', $filename);
  360.                 $filename = str_replace('.', '', $filename);
  361.                 $filename = $filename . '.png';
  362.  
  363.                 $target = substr($target_config, 0, (strrpos($target_config, '/') + 1));
  364.                 $target = WWW_ROOT . '/' . $target;
  365.  
  366.                 $width_max = $ApiConfiguration['ApiConfiguration']['cape_width']; // pixel
  367.                 $height_max = $ApiConfiguration['ApiConfiguration']['cape_height']; // pixel
  368.  
  369.                 $isValidImg = $this->Util->isValidImage($this->request, array('png'), $width_max, $height_max, $cape_max_size);
  370.  
  371.                 if (!$isValidImg['status']) {
  372.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $isValidImg['msg'])));
  373.                     return;
  374.                 } else {
  375.                     $infos = $isValidImg['infos'];
  376.                 }
  377.  
  378.                 if (!$this->Util->uploadImage($this->request, $target . $filename)) {
  379.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('FORM__ERROR_WHEN_UPLOAD'))));
  380.                     return;
  381.                 }
  382.  
  383.                 $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('API__UPLOAD_CAPE_SUCCESS'))));
  384.  
  385.             }
  386.  
  387.         } else {
  388.             throw new ForbiddenException();
  389.         }
  390.     }
  391.  
  392.     function profile()
  393.     {
  394.         if ($this->isConnected) {
  395.  
  396.             $this->loadModel('User');
  397.  
  398.             $this->set('title_for_layout', $this->User->getKey('pseudo'));
  399.             $this->layout = $this->Configuration->getKey('layout');
  400.             if ($this->EyPlugin->isInstalled('eywek.shop.1')) {
  401.                 $this->set('shop_active', true);
  402.             } else {
  403.                 $this->set('shop_active', false);
  404.             }
  405.  
  406.             $available_ranks = array(0 => $this->Lang->get('USER__RANK_MEMBER'), 2 => $this->Lang->get('USER__RANK_MODERATOR'), 3 => $this->Lang->get('USER__RANK_ADMINISTRATOR'), 4 => $this->Lang->get('USER__RANK_ADMINISTRATOR'), 5 => $this->Lang->get('USER__RANK_BANNED'));
  407.             $this->loadModel('Rank');
  408.             $custom_ranks = $this->Rank->find('all');
  409.             foreach ($custom_ranks as $key => $value) {
  410.                 $available_ranks[$value['Rank']['rank_id']] = $value['Rank']['name'];
  411.             }
  412.             $this->set(compact('available_ranks'));
  413.  
  414.             $this->set('can_cape', $this->API->can_cape());
  415.             $this->set('can_skin', $this->API->can_skin());
  416.  
  417.             $this->loadModel('ApiConfiguration');
  418.             $configAPI = $this->ApiConfiguration->find('first');
  419.             $skin_width_max = $configAPI['ApiConfiguration']['skin_width'];
  420.             $skin_height_max = $configAPI['ApiConfiguration']['skin_height'];
  421.             $cape_width_max = $configAPI['ApiConfiguration']['cape_width'];
  422.             $cape_height_max = $configAPI['ApiConfiguration']['cape_height'];
  423.  
  424.             $this->set(compact('skin_width_max', 'skin_height_max', 'cape_width_max', 'cape_height_max'));
  425.  
  426.             $confirmed = $this->User->getKey('confirmed');
  427.             if ($this->Configuration->getKey('confirm_mail_signup') && !empty($confirmed) && date('Y-m-d H:i:s', strtotime($confirmed)) != $confirmed) { // si ca ne correspond pas à une date -> compte non confirmé
  428.                 $this->Session->setFlash($this->Lang->get('USER__MSG_NOT_CONFIRMED_EMAIL', array('{URL_RESEND_EMAIL}' => Router::url(array('action' => 'resend_confirmation')))), 'default.warning');
  429.             }
  430.  
  431.         } else {
  432.             $this->redirect('/');
  433.         }
  434.     }
  435.  
  436.     function resend_confirmation()
  437.     {
  438.         if (!$this->isConnected && !$this->Session->check('email.confirm.user.id'))
  439.             throw new ForbiddenException();
  440.         if ($this->isConnected)
  441.             $user = $this->User->getAllFromCurrentUser();
  442.         else
  443.             $user = $this->User->find('first', array('conditions' => array('id' => $this->Session->read('email.confirm.user.id'))));
  444.         $this->Session->delete('email.confirm.user.id');
  445.         if (!$user || empty($user))
  446.             throw new NotFoundException();
  447.         if (isset($user['User']))
  448.             $user = $user['User'];
  449.         $confirmed = $user['confirmed'];
  450.  
  451.         if (!$this->Configuration->getKey('confirm_mail_signup') || empty($confirmed) || date('Y-m-d H:i:s', strtotime($confirmed)) == $confirmed)
  452.             throw new NotFoundException();
  453.  
  454.         $emailMsg = $this->Lang->get('EMAIL__CONTENT_CONFIRM_MAIL', array(
  455.             '{LINK}' => Router::url('/user/confirm/', true) . $confirmed,
  456.             '{IP}' => $this->Util->getIP(),
  457.             '{USERNAME}' => $user['pseudo'],
  458.             '{DATE}' => $this->Lang->date(date('Y-m-d H:i:s'))
  459.         ));
  460.  
  461.         $email = $this->Util->prepareMail(
  462.             $user['email'],
  463.             $this->Lang->get('EMAIL__TITLE_CONFIRM_MAIL'),
  464.             $emailMsg
  465.         )->sendMail();
  466.  
  467.         if ($email)
  468.             $this->Session->setFlash($this->Lang->get('USER__CONFIRM_EMAIL_RESEND_SUCCESS'), 'default.success');
  469.         else
  470.             $this->Session->setFlash($this->Lang->get('USER__CONFIRM_EMAIL_RESEND_FAIL'), 'default.error');
  471.         if ($this->isConnected)
  472.             $this->redirect(array('action' => 'profile'));
  473.         else
  474.             $this->redirect('/');
  475.     }
  476.  
  477.     function change_pw()
  478.     {
  479.         $this->autoRender = false;
  480.         $this->response->type('json');
  481.         if ($this->isConnected) {
  482.             if ($this->request->is('ajax')) {
  483.                 if (!empty($this->request->data['password']) AND !empty($this->request->data['password_confirmation'])) {
  484.                     $password = $this->Util->password($this->request->data['password'], $this->User->getKey('pseudo'));
  485.                     $password_confirmation = $this->Util->password($this->request->data['password_confirmation'], $this->User->getKey('pseudo'));
  486.                     if ($password == $password_confirmation) {
  487.  
  488.                         $event = new CakeEvent('beforeUpdatePassword', $this, array('user' => $this->User->getAllFromCurrentUser(), 'new_password' => $password));
  489.                         $this->getEventManager()->dispatch($event);
  490.                         if ($event->isStopped()) {
  491.                             return $event->result;
  492.                         }
  493.  
  494.                         $this->User->setKey('password', $password);
  495.                         $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('USER__PASSWORD_UPDATE_SUCCESS'))));
  496.                     } else {
  497.                         $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('USER__ERROR_PASSWORDS_NOT_SAME'))));
  498.                     }
  499.                 } else {
  500.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));
  501.                 }
  502.             } else {
  503.                 $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__BAD_REQUEST'))));
  504.             }
  505.         } else {
  506.             $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('USER__ERROR_MUST_BE_LOGGED'))));
  507.         }
  508.     }
  509.  
  510.     function change_email()
  511.     {
  512.         $this->autoRender = false;
  513.         $this->response->type('json');
  514.         if ($this->isConnected && $this->Permissions->can('EDIT_HIS_EMAIL')) {
  515.             if ($this->request->is('ajax')) {
  516.                 if (!empty($this->request->data['email']) AND !empty($this->request->data['email_confirmation'])) {
  517.                     if ($this->request->data['email'] == $this->request->data['email_confirmation']) {
  518.                         if (filter_var($this->request->data['email'], FILTER_VALIDATE_EMAIL)) {
  519.  
  520.                             $event = new CakeEvent('beforeUpdateEmail', $this, array('user' => $this->User->getAllFromCurrentUser(), 'new_email' => $this->request->data['email']));
  521.                             $this->getEventManager()->dispatch($event);
  522.                             if ($event->isStopped()) {
  523.                                 return $event->result;
  524.                             }
  525.  
  526.                             $this->User->setKey('email', htmlentities($this->request->data['email']));
  527.                             $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('USER__EMAIL_UPDATE_SUCCESS'))));
  528.                         } else {
  529.                             $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('USER__ERROR_EMAIL_NOT_VALID'))));
  530.                         }
  531.                     } else {
  532.                         $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('USER__ERROR_EMAIL_NOT_SAME'))));
  533.                     }
  534.                 } else {
  535.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));
  536.                 }
  537.             } else {
  538.                 $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__BAD_REQUEST'))));
  539.             }
  540.         } else {
  541.             throw new ForbiddenException();
  542.         }
  543.     }
  544.  
  545.     function admin_index()
  546.     {
  547.         if ($this->isConnected AND $this->Permissions->can('MANAGE_USERS')) {
  548.  
  549.             $this->set('title_for_layout', $this->Lang->get('USER__TITLE'));
  550.             $this->layout = 'admin';
  551.  
  552.             $this->set('type', $this->Configuration->getKey('member_page_type'));
  553.  
  554.         } else {
  555.             $this->redirect('/');
  556.         }
  557.     }
  558.  
  559.     function admin_liveSearch($query = false)
  560.     {
  561.         $this->autoRender = false;
  562.         $this->response->type('json');
  563.         if ($this->isConnected AND $this->Permissions->can('MANAGE_USERS')) {
  564.             if ($query != false) {
  565.  
  566.                 $result = $this->User->find('all', array('conditions' => array('pseudo LIKE' => $query . '%')));
  567.  
  568.  
  569.                 foreach ($result as $key => $value) {
  570.  
  571.                     $users[] = array('pseudo' => $value['User']['pseudo'], 'id' => $value['User']['id']);
  572.  
  573.                 }
  574.  
  575.                 $response = (empty($result)) ? array('status' => false) : array('status' => true, 'data' => $users);
  576.                 $this->response->body($response);
  577.  
  578.             } else {
  579.                 $this->response->body(json_encode(array('status' => false)));
  580.             }
  581.         } else {
  582.             $this->response->body(json_encode(array('status' => false)));
  583.         }
  584.     }
  585.  
  586.     public function admin_get_users()
  587.     {
  588.         if ($this->isConnected AND $this->Permissions->can('MANAGE_USERS')) {
  589.             $this->autoRender = false;
  590.             $this->response->type('json');
  591.  
  592.             if ($this->request->is('ajax')) {
  593.  
  594.                 $available_ranks = array(
  595.                     0 => array('label' => 'success', 'name' => $this->Lang->get('USER__RANK_MEMBER')),
  596.                     2 => array('label' => 'warning', 'name' => $this->Lang->get('USER__RANK_MODERATOR')),
  597.                     3 => array('label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')),
  598.                     4 => array('label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')),
  599.                     5 => array('label' => 'primary', 'name' => $this->Lang->get('USER__RANK_BANNED'))
  600.                 );
  601.                 $this->loadModel('Rank');
  602.                 $custom_ranks = $this->Rank->find('all');
  603.                 foreach ($custom_ranks as $key => $value) {
  604.                     $available_ranks[$value['Rank']['rank_id']] = array('label' => 'info', 'name' => $value['Rank']['name']);
  605.                 }
  606.  
  607.                 $this->DataTable = $this->Components->load('DataTable');
  608.                 $this->modelClass = 'User';
  609.                 $this->DataTable->initialize($this);
  610.                 $this->paginate = array(
  611.                     'fields' => array('User.id', 'User.pseudo', 'User.email', 'User.created', 'User.rank'),
  612.                 );
  613.                 $this->DataTable->mDataProp = true;
  614.  
  615.                 $response = $this->DataTable->getResponse();
  616.  
  617.                 $users = $response['aaData'];
  618.                 $data = array();
  619.                 foreach ($users as $key => $value) {
  620.  
  621.                     $username = $value['User']['pseudo'];
  622.                     $date = 'Le ' . $this->Lang->date($value['User']['created']);
  623.  
  624.                     $rank_label = (isset($available_ranks[$value['User']['rank']])) ? $available_ranks[$value['User']['rank']]['label'] : $available_ranks[0]['label'];
  625.                     $rank_name = (isset($available_ranks[$value['User']['rank']])) ? $available_ranks[$value['User']['rank']]['name'] : $available_ranks[0]['name'];
  626.                     $rank = '<span class="label label-' . $rank_label . '">' . $rank_name . '</span>';
  627.  
  628.                     $btns = '<a href="' . Router::url(array('controller' => 'user', 'action' => 'edit/' . $value["User"]["id"], 'admin' => true)) . '" class="btn btn-info">' . $this->Lang->get('GLOBAL__EDIT') . '</a>';
  629.                     $btns .= '&nbsp;<a onClick="confirmDel(\'' . Router::url(array('controller' => 'user', 'action' => 'delete/' . $value["User"]["id"], 'admin' => true)) . '\')" class="btn btn-danger">' . $this->Lang->get('GLOBAL__DELETE') . '</button>';
  630.  
  631.                     $data[] = array(
  632.                         'User' => array(
  633.                             'pseudo' => $username,
  634.                             'email' => $value['User']['email'],
  635.                             'created' => $date,
  636.                             'rank' => $rank
  637.                         ),
  638.                         'actions' => $btns
  639.                     );
  640.  
  641.                 }
  642.  
  643.                 $response['aaData'] = $data;
  644.  
  645.                 $this->response->body(json_encode($response));
  646.  
  647.             }
  648.         }
  649.     }
  650.  
  651.     function admin_edit($search = false)
  652.     {
  653.         if ($this->isConnected AND $this->Permissions->can('MANAGE_USERS')) {
  654.             if ($search != false) {
  655.  
  656.                 $this->layout = 'admin';
  657.                 $this->set('title_for_layout', $this->Lang->get('USER__EDIT_TITLE'));
  658.                 $this->loadModel('User');
  659.                 $find = $this->User->find('all', array('conditions' => $this->User->__makeCondition($search)));
  660.  
  661.                 if (!empty($find)) {
  662.                     $search_user = $find[0]['User'];
  663.                     $this->loadModel('History');
  664.                     $findHistory = $this->History->getLastFromUser($search_user['id']);
  665.                     $search_user['History'] = $this->History->format($findHistory, $this->Lang);
  666.  
  667.                     $options_ranks = array(
  668.                         0 => $this->Lang->get('USER__RANK_MEMBER'),
  669.                         2 => $this->Lang->get('USER__RANK_MODERATOR'),
  670.                         3 => $this->Lang->get('USER__RANK_ADMINISTRATOR'),
  671.                         4 => $this->Lang->get('USER__RANK_SUPER_ADMINISTRATOR'),
  672.                         5 => $this->Lang->get('USER__RANK_BANNED')
  673.                     );
  674.                     $this->loadModel('Rank');
  675.                     $custom_ranks = $this->Rank->find('all');
  676.                     foreach ($custom_ranks as $key => $value) {
  677.                         $options_ranks[$value['Rank']['rank_id']] = $value['Rank']['name'];
  678.                     }
  679.  
  680.                     if ($this->Configuration->getKey('confirm_mail_signup') && !empty($search_user['confirmed']) && date('Y-m-d H:i:s', strtotime($search_user['confirmed'])) != $search_user['confirmed']) {
  681.                         $search_user['confirmed'] = false;
  682.                     } else {
  683.                         $search_user['confirmed'] = true;
  684.                     }
  685.  
  686.                     $this->set(compact('options_ranks'));
  687.  
  688.                     $this->set(compact('search_user'));
  689.                 } else {
  690.                     throw new NotFoundException();
  691.                 }
  692.             } else {
  693.                 throw new NotFoundException();
  694.             }
  695.         } else {
  696.             $this->redirect('/');
  697.         }
  698.     }
  699.  
  700.     function admin_confirm($user_id = false)
  701.     {
  702.         $this->autoRender = false;
  703.         if (isset($user_id) && $this->isConnected AND $this->Permissions->can('MANAGE_USERS')) {
  704.  
  705.             $find = $this->User->find('first', array('conditions' => array('id' => $user_id)));
  706.  
  707.             if (!empty($find)) {
  708.  
  709.                 $event = new CakeEvent('beforeConfirmAccount', $this, array('user_id' => $find['User']['id'], 'manual' => true));
  710.                 $this->getEventManager()->dispatch($event);
  711.                 if ($event->isStopped()) {
  712.                     return $event->result;
  713.                 }
  714.  
  715.                 $this->User->read(null, $find['User']['id']);
  716.                 $this->User->set(array('confirmed' => date('Y-m-d H:i:s')));
  717.                 $this->User->save();
  718.  
  719.                 $userSession = $find['User']['id'];
  720.  
  721.                 $this->redirect(array('action' => 'edit', $user_id));
  722.  
  723.             } else {
  724.                 throw new NotFoundException();
  725.             }
  726.  
  727.         } else {
  728.             throw new NotFoundException();
  729.         }
  730.     }
  731.  
  732.     function admin_edit_ajax()
  733.     {
  734.         $this->autoRender = false;
  735.         $this->response->type('json');
  736.         if ($this->isConnected && $this->Permissions->can('MANAGE_USERS')) {
  737.             if ($this->request->is('post')) {
  738.                 $this->loadModel('User');
  739.                 if (!empty($this->request->data['id']) && !empty($this->request->data['email']) && (!empty($this->request->data['rank']) || $this->request->data['rank'] == 0)) {
  740.  
  741.                     $findUser = $this->User->find('first', array('conditions' => array('id' => intval($this->request->data['id']))));
  742.  
  743.                     if (empty($findUser)) {
  744.                         $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('USER__EDIT_ERROR_UNKNOWN'))));
  745.                         return;
  746.                     }
  747.  
  748.                     if ($findUser['User']['id'] == $this->User->getKey('id') && $this->request->data['rank'] != $this->User->getKey('rank')) {
  749.                         $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('USER__EDIT_ERROR_YOURSELF'))));
  750.                         return;
  751.                     }
  752.  
  753.                     $data = array(
  754.                         'email' => $this->request->data['email'],
  755.                         'rank' => $this->request->data['rank']
  756.                     );
  757.  
  758.                     if (!empty($this->request->data['password'])) {
  759.                         $data['password'] = $this->Util->password($this->request->data['password'], $findUser['User']['pseudo']);
  760.                         $password_updated = true;
  761.                     } else {
  762.                         $password_updated = false;
  763.                     }
  764.  
  765.                     if ($this->EyPlugin->isInstalled('eywek.shop.1')) {
  766.                         $data['money'] = $this->request->data['money'];
  767.                     }
  768.  
  769.                     $event = new CakeEvent('beforeEditUser', $this, array('user_id' => $findUser['User']['id'], 'data' => $data, 'password_updated' => $password_updated));
  770.                     $this->getEventManager()->dispatch($event);
  771.                     if ($event->isStopped()) {
  772.                         return $event->result;
  773.                     }
  774.  
  775.                     $this->User->read(null, $findUser['User']['id']);
  776.                     $this->User->set($data);
  777.                     $this->User->save();
  778.  
  779.                     $this->History->set('EDIT_USER', 'user');
  780.                     $this->Session->setFlash($this->Lang->get('USER__EDIT_SUCCESS'), 'default.success');
  781.                     $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('USER__EDIT_SUCCESS'))));
  782.                 } else {
  783.                     $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS'))));
  784.                 }
  785.             } else {
  786.                 throw new NotFoundException();
  787.             }
  788.         } else {
  789.             throw new ForbiddenException();
  790.         }
  791.     }
  792.  
  793.     function admin_delete($id = false)
  794.     {
  795.         $this->autoRender = false;
  796.         if ($this->isConnected AND $this->Permissions->can('MANAGE_USERS')) {
  797.             if ($id != false) {
  798.                 $this->loadModel('User');
  799.                 $find = $this->User->find('all', array('conditions' => array('id' => $id)));
  800.                 if (!empty($find)) {
  801.  
  802.                     $event = new CakeEvent('beforeDeleteUser', $this, array('user' => $find['User']));
  803.                     $this->getEventManager()->dispatch($event);
  804.                     if ($event->isStopped()) {
  805.                         return $event->result;
  806.                     }
  807.  
  808.                     $this->User->delete($id);
  809.                     $this->History->set('DELETE_USER', 'user');
  810.                     $this->Session->setFlash($this->Lang->get('USER__DELETE_SUCCESS'), 'default.success');
  811.                     $this->redirect(array('controller' => 'user', 'action' => 'index', 'admin' => true));
  812.                 } else {
  813.                     $this->Session->setFlash($this->Lang->get('UNKNONW_ID'), 'default.error');
  814.                     $this->redirect(array('controller' => 'user', 'action' => 'index', 'admin' => true));
  815.                 }
  816.             } else {
  817.                 $this->redirect(array('controller' => 'user', 'action' => 'index', 'admin' => true));
  818.             }
  819.         } else {
  820.             $this->redirect('/');
  821.         }
  822.     }
  823. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement