PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Controller/UsersController.php

https://bitbucket.org/fastestsolution/chachar_cz
PHP | 486 lines | 340 code | 92 blank | 54 comment | 52 complexity | a7f4dbfb2d2c300f69cee763c351885b MD5 | raw file
Possible License(s): MIT, Apache-2.0, 0BSD, Unlicense, BSD-3-Clause, JSON
  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\AppController;
  4. use Cake\Core\Configure;
  5. use Cake\Network\Exception\NotFoundException;
  6. use Cake\ORM\TableRegistry;
  7. use Cake\I18n\Time;
  8. class UsersController extends AppController
  9. {
  10. public function initialize(){
  11. parent::initialize();
  12. //$this->Auth->allow("add");
  13. }
  14. public function login(){
  15. if (!$this->request->is('ajax')) {
  16. if (isset($this->request->query['se'])){
  17. $this->Session->write('chatbot_server',$this->request->query['se']);
  18. $this->Session->write('chatbot_msi',$this->request->query['msi']);
  19. }
  20. //$this->viewBuilder()->autoLayout(false);
  21. } else {
  22. $this->Auth->config('authenticate', [
  23. 'Form' => [
  24. 'fields' => ['username' => 'email']
  25. ]
  26. ]);
  27. $this->Auth->constructAuthenticate();
  28. $this->request->data['email'] = $this->request->data['username'];
  29. $this->request->data['password'] = md5($this->request->data['password']);
  30. unset($this->request->data['username']);
  31. $user = $this->Auth->identify();
  32. //pr($user);
  33. if ($user) {
  34. //pr($user);
  35. if ($this->Session->check('chatbot_msi')){
  36. $data = [
  37. 'code'=>$user['id'],
  38. 'project'=>$this->chatBotsProjects[$this->system_id],
  39. 'm_id'=>$this->Session->read('chatbot_msi'),
  40. 'server'=>$this->Session->read('chatbot_server'),
  41. ];
  42. $this->chatbotLogin($data);
  43. die();
  44. }
  45. $this->Auth->setUser($user);
  46. //pr($this->Auth);
  47. setcookie("favorite_list", $user['favorite'], time()+360000,'/');
  48. //pr($user);
  49. //$_COOKIE['favorite_list'] = user
  50. die(json_encode(['r'=>true,'m'=>__('Přihlášení v pořádku'),'redirect'=>'self','user_id'=>$user['id']]));
  51. } else {
  52. die(json_encode(['r'=>false,'m'=>__('Chybné přihlašovací údaje')]));
  53. }
  54. }
  55. }
  56. /**
  57. * chatbot login
  58. */
  59. private function chatbotLogin($data){
  60. $ch = curl_init();
  61. $post = [
  62. 'm_id'=>$data['m_id'],
  63. 'code'=>$data['code'],
  64. ];
  65. $url = 'https://'.$data['server'].'-server.gobots.cz/'.$data['project'].'/events/login';
  66. //pr($url);
  67. curl_setopt($ch, CURLOPT_URL, $url);
  68. curl_setopt($ch, CURLOPT_POST, 1);
  69. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  71. $result = curl_exec ($ch);
  72. //print_r($result);
  73. curl_close ($ch);
  74. $this->Session->delete('chatbot_msi');
  75. $this->Session->delete('chatbot_server');
  76. die(json_encode(['r'=>true,'m'=>__('Přihlášení v pořádku'),'close'=>true]));
  77. }
  78. /**
  79. * chatbot registrace
  80. */
  81. private function chatbotRegistrate($data){
  82. $ch = curl_init();
  83. $post = [
  84. 'm_id'=>$data['m_id'],
  85. 'code'=>$data['code'],
  86. ];
  87. $url = 'https://'.$data['server'].'-server.gobots.cz/'.$data['project'].'/events/register';
  88. //pr($data);
  89. //pr($url);die();
  90. curl_setopt($ch, CURLOPT_URL, $url);
  91. curl_setopt($ch, CURLOPT_POST, 1);
  92. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
  93. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  94. $result = curl_exec ($ch);
  95. //print_r($result);
  96. curl_close ($ch);
  97. $this->Session->delete('chatbot_msi');
  98. $this->Session->delete('chatbot_server');
  99. //die('aa');
  100. die(json_encode(['r'=>true,'m'=>__('Registrace v pořádku'),'close'=>true]));
  101. }
  102. public function logout(){
  103. $this->Auth->logout();
  104. return $this->redirect('/');
  105. }
  106. public function registrace(){
  107. $this->set("no_side", true);
  108. $this->set("layout_class", 'col-sm-12 txt_page');
  109. $this->set("title", __("Registrujte se a získejte 20 kreditů ZDARMA! "));
  110. $this->set("button_title", __('Zaregistrovat se'));
  111. $done_title = __('Váš účet byl zaregistrován');
  112. if (isset($this->request->query['se'])){
  113. $this->Session->write('chatbot_server',$this->request->query['se']);
  114. $this->Session->write('chatbot_msi',$this->request->query['msi']);
  115. }
  116. //$read = $this->Users->find()->where(['id'=>1])->contain('UserAddresses')->toArray();
  117. //pr($read);
  118. $users = $this->Users->newEntity();
  119. if (isset($this->loggedUser)){
  120. //pr($this->loggedUser);die();
  121. $users = $this->Users->find()
  122. ->where(['id'=>$this->loggedUser['id']])
  123. ->first();
  124. if (empty($users)){
  125. return $this->redirect('/');
  126. }
  127. $this->set('userEdit',true);
  128. $this->set("title", __("Editace uživatele - ".$users->first_name.' '.$users->last_name));
  129. $this->set("button_title", __('Upravit údaje'));
  130. $done_title = __('Váš účet byl upraven');
  131. $users->password2 = $users->password;
  132. $users = $this->Users->loadPhonePref($users);
  133. //pr($users);
  134. // load adresa
  135. $this->loadModel('UserAddresses');
  136. $address = $this->UserAddresses->find()
  137. ->where(['shop_client_id'=>$this->loggedUser['id']])
  138. ->select([
  139. ])
  140. ->order('id DESC')
  141. ->toArray();
  142. //pr($address);
  143. $users->user_addresses = $address;
  144. //pr($users);
  145. }
  146. if ($this->request->is("ajax")){
  147. if (empty($this->request->data['password'])){
  148. die(json_encode(['r'=>false,'m'=>'Zadejte Vaše heslo']));
  149. }
  150. // editace hesla
  151. if (isset($this->request->data['password_tmp'])){
  152. if ($this->request->data['password_tmp'] == $this->request->data['password']){
  153. unset($this->request->data['password']);
  154. }
  155. }
  156. if (isset($this->request->data['password'])){
  157. $this->request->data['password'] = md5($this->request->data['password']);
  158. $this->request->data['password2'] = md5($this->request->data['password2']);
  159. }
  160. // convert phone pref
  161. $this->request->data = $this->Users->savePhonePref($this->request->data);
  162. // nastavit odkud se zaregistroval
  163. if (empty($this->request->data['id']) && !isset($this->loggedUser)){
  164. $this->request->data['system_id'] = $this->system_id;
  165. $this->request->data['credits'] = 20;
  166. $this->request->data['reg'] = 1;
  167. }
  168. // validace kroku
  169. if (isset($this->request->data['step'])){
  170. if ($this->request->data['step'] == 1){
  171. $this->Users->patchEntity($users, $this->request->data(),[
  172. 'validate' => 'Step1',
  173. 'associated' => [
  174. 'UserAddresses' => ['validate' => 'onlyCheck']
  175. ]
  176. ]);
  177. //pr($this->request->data);
  178. if (isset($this->request->data['email']))
  179. if ($this->findUserReg($this->request->data['email'])){
  180. die(json_encode(['r'=>false,'m'=>__d('shop','Váš email je již zaregistrován, prosím přihlašte se')]));
  181. }
  182. $this->check_error($users);
  183. die(json_encode(['r'=>true]));
  184. }
  185. if ($this->request->data['step'] == 2){
  186. if (isset($this->loggedUser)){
  187. $this->Users->patchEntity($users, $this->request->data(),[
  188. 'validate' => 'onlyCheck',
  189. 'associated' => [
  190. 'UserAddresses' => ['validate' => 'onlyCheck']
  191. ]
  192. ]);
  193. } else {
  194. $this->Users->patchEntity($users, $this->request->data());
  195. }
  196. $this->check_error($users);
  197. die(json_encode(['r'=>true]));
  198. }
  199. }
  200. if (isset($this->loggedUser)){
  201. // neukladat adresu
  202. if (isset($this->request->data['user_addresses'][0]['city']) && empty($this->request->data['user_addresses'][0]['city'])){
  203. unset($this->request->data['user_addresses']);
  204. }
  205. $this->Users->patchEntity($users, $this->request->data(),[
  206. 'validate' => 'onlyCheck',
  207. 'associated' => [
  208. 'UserAddresses' => ['validate' => 'onlyCheck']
  209. ]
  210. ]);
  211. } else {
  212. $this->Users->patchEntity($users, $this->request->data());
  213. }
  214. //pr($users);die();
  215. $this->check_error($users);
  216. if ($result = $this->Users->save($users)) {
  217. $res = $result->toArray();
  218. //unset($res['password']);
  219. //unset($res['password2']);
  220. $find = $this->Users->getUser($res['id']);
  221. $this->Auth->setUser($find);
  222. //pr($find);
  223. //pr($this->request->data);
  224. if (empty($this->request->data['id']) && !isset($this->loggedUser))
  225. $this->send_email_registrace($find);
  226. if ($this->Session->check('chatbot_msi')){
  227. //pr($find);
  228. $data = [
  229. 'code'=>$find['id'],
  230. 'project'=>$this->chatBotsProjects[$this->system_id],
  231. 'm_id'=>$this->Session->read('chatbot_msi'),
  232. 'server'=>$this->Session->read('chatbot_server'),
  233. ];
  234. $this->chatbotRegistrate($data);
  235. die();
  236. }
  237. if (isset($this->loggedUser)){
  238. die(json_encode(['r'=>true,'m'=>$done_title,'redirect'=>'/']));
  239. } else {
  240. die(json_encode(['r'=>true]));
  241. }
  242. } else {
  243. die(json_encode(['r'=>false,'m'=>__('Chyba uložení')]));
  244. }
  245. }
  246. $this->set(compact("users"));
  247. }
  248. private function findUserReg($email){
  249. $this->loadModel('ShopClients');
  250. $find_email = $this->ShopClients->find()
  251. ->where(['email'=>$email,'reg'=>1])
  252. ->first();
  253. if ($find_email){
  254. return true;
  255. } else {
  256. return false;
  257. }
  258. }
  259. // send email registrace
  260. private function send_email_registrace($data){
  261. $data_email = json_decode(json_encode($data),true);
  262. //pr($data);
  263. $this->loadComponent('Email');
  264. $opt = [
  265. 'to'=>$data['email'],
  266. 'template_id'=>2,
  267. 'data'=>$data_email,
  268. ];
  269. $this->Email->send($opt);
  270. }
  271. public function zapomenuteHeslo(){
  272. $this->set("title", __("Zapomenuté heslo"));
  273. $users = $this->Users->newEntity();
  274. //$this->sendEmail($opt);
  275. if ($this->request->is("ajax")){
  276. $this->Users->patchEntity($users, $this->request->data());
  277. $find = $this->Users->find()
  278. ->where(['email'=>$this->request->data['email']])
  279. ->select([
  280. 'id',
  281. 'email',
  282. ])
  283. ->first();
  284. //pr($this->request->data());die();
  285. //pr($find);
  286. if ($find) {
  287. $this->Users->updateAll(
  288. ['password_send' => new Time(date('Y-m-d'))], // fields
  289. ['id' => $find->id]
  290. );
  291. $hash = date('Y-m-d').$find->email.$find->id;
  292. //pr($hash);die();
  293. // send to email
  294. $this->loadComponent('Email');
  295. $link = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.heslo_reset_link.'/'.sha1($hash);
  296. $data = [
  297. 'Users_link'=>'<a href="'.$link.'">'.$link.'</a>',
  298. ];
  299. //pr($data);die();
  300. $opt = [
  301. 'to'=>$find->email,
  302. 'template_id'=>1,
  303. 'data'=>$data,
  304. ];
  305. $this->Email->send($opt);
  306. die(json_encode(['r'=>true,'m'=>__('Na váš email byly zaslány požadované údaje'),'redirect'=>'self']));
  307. } else {
  308. die(json_encode(['r'=>false,'m'=>__('Zadaný email nebyl nalezen')]));
  309. }
  310. }
  311. $this->set(compact("users"));
  312. }
  313. public function resetHeslo($hash){
  314. $this->set('title',__('Obnovení hesla'));
  315. $find = $this->Users->find()
  316. ->where([
  317. 'SHA1(CONCAT(password_send,email,id)) ='=>$hash,
  318. 'password_send'=>date('Y-m-d')
  319. ])
  320. ->select([
  321. 'id',
  322. 'password_send',
  323. 'email',
  324. ])
  325. ->first()
  326. ;
  327. if (!$find){
  328. $this->set('alert_class','alert-danger');
  329. $this->set('alert_text','<strong>'.__('Pozor').'!</strong> '.__('Špatné ověření emailu, nebo vypršení expirace. Zkuste zaslat heslo znovu.'));
  330. } else {
  331. $this->set('alert_class','alert-success');
  332. $this->set('alert_text','<strong>'.__('V pořádku').'!</strong> '.__('Zadejte Vaše nové heslo.'));
  333. $users = $this->Users->newEntity();
  334. $this->set(compact("users"));
  335. if ($this->request->is("ajax")){
  336. $this->request->data['password'] = md5($this->request->data['password']);
  337. $this->request->data['password2'] = md5($this->request->data['password2']);
  338. $users->id = $find->id;
  339. $this->Users->patchEntity($users, $this->request->data());
  340. $this->check_error($users);
  341. if ($result = $this->Users->save($users)) {
  342. $res = $result->toArray();
  343. //unset($res['password']);
  344. //unset($res['password2']);
  345. $find = $this->Users->getUser($res['id']);
  346. $this->Auth->setUser($find);
  347. die(json_encode(['r'=>true,'m'=>__('Heslo bylo změněno, budete přihlášen(a)'),'redirect'=>'/']));
  348. } else {
  349. die(json_encode(['r'=>false,'m'=>__('Chyba uložení')]));
  350. }
  351. }
  352. }
  353. //pr($find);
  354. //a
  355. //die();
  356. }
  357. // delete adresa
  358. public function deleteAddress($id){
  359. if (!$this->request->is("ajax")){
  360. die('Chyba volani funkce');
  361. }
  362. $this->loadModel('UserAddresses');
  363. $find = $this->UserAddresses->find()
  364. ->where([
  365. 'shop_client_id'=>$this->loggedUser['id'],
  366. 'id'=>$id,
  367. ])
  368. ->select([
  369. 'id',
  370. ])
  371. ->first()
  372. ;
  373. if (!$find){
  374. die(json_encode(['r'=>false,'m'=>__('Adresa nenalezena')]));
  375. }
  376. //pr($find);
  377. if (!$result = $this->UserAddresses->delete($find)){
  378. die(json_encode(['r'=>false,'m'=>__('Chyba vymazání adresy')]));
  379. } else {
  380. die(json_encode(['r'=>true,'m'=>__('Adresa vymazána')]));
  381. }
  382. }
  383. public function saveFavorite(){
  384. if (!isset($this->loggedUser)){
  385. die(json_encode(['r'=>false]));
  386. }
  387. $this->Users->updateAll(
  388. ['favorite' => $_COOKIE['favorite_list']], // fields
  389. ['id' => $this->loggedUser['id']]
  390. );
  391. //pr($_COOKIE['favorite_list']);
  392. die(json_encode(['r'=>true]));
  393. }
  394. }