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

/Controller/AppController.php

https://github.com/Wargo/reddevil
PHP | 184 lines | 82 code | 29 blank | 73 comment | 11 complexity | b9d70add008aa04978b237d1ddbf12e1 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * Application level Controller
  4. *
  5. * This file is application-wide controller file. You can put all
  6. * application-wide controller-related methods here.
  7. *
  8. * PHP 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package app.Controller
  19. * @since CakePHP(tm) v 0.2.9
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('Controller', 'Controller');
  23. /**
  24. * Application Controller
  25. *
  26. * Add your application-wide methods in the class below, your controllers
  27. * will inherit them.
  28. *
  29. * @package app.Controller
  30. * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
  31. */
  32. class AppController extends Controller {
  33. public $helpers = array('Html', 'Form', 'Session', 'Funciones', 'MiAsset.Asset');
  34. public $components = array(
  35. 'Security' => array('csrfUseOnce' => false),
  36. 'Session',
  37. 'Mi.SwissArmy' => array('autoLayout' => true, 'authLoginSessionToken' => false),
  38. 'MiUsers.RememberMe' => array('auth' => array('fields' => array('username' => 'email', 'password' => 'password'))),
  39. 'Auth' => array(
  40. 'authenticate' => array(
  41. 'Form' => array(
  42. 'fields' => array('username' => 'username')
  43. )
  44. ),
  45. 'loginAction' => array(
  46. 'controller' => 'users',
  47. 'action' => 'login',
  48. 'plugin' => false
  49. ),
  50. ),
  51. 'Cookie',
  52. 'RequestHandler'
  53. );
  54. function beforeFilter() {
  55. $this->Auth->allow('*');
  56. parent::beforeFilter();
  57. if (!empty($this->params['admin'])) {
  58. if ($this->Auth->user('group') != 'admin') {
  59. return $this->redirect('/');
  60. }
  61. $this->layout = 'panel';
  62. }
  63. if (isset($this->params->query['nats'])) {
  64. $this->Session->write('NatsCode', $this->params->query['nats']);
  65. }
  66. $this->Cookie->name = 'RedDevilX';
  67. $this->Cookie->time = 3600 * 24; // or '1 hour'
  68. $this->_detectarMovil();
  69. $this->_checkActive();
  70. }
  71. protected function _message($message, $url = false, $value = false, $error = false, $admin=0){
  72. /*
  73. if($this->historico>0){
  74. $entrada['HistoricoEntrada']['ip']=ip2long($_SERVER['REMOTE_ADDR']);
  75. if($this->Session->check('Administrador.id')){
  76. $administrador_id=$this->Session->read('Administrador.id');
  77. $admin=1;
  78. }
  79. else{
  80. $administrador_id=0;
  81. //$admin=0;
  82. }
  83. if($this->Session->check('Usuario.usuario')){
  84. $entrada['HistoricoEntrada']['usuario_id']=$this->Session->read('Usuario.id');
  85. }
  86. $entrada['HistoricoEntrada']['admin']=$admin;
  87. $entrada['HistoricoEntrada']['administrador_id']=$administrador_id;
  88. if(isset($_SERVER['REQUEST_URI'])){
  89. $current = $_SERVER['REQUEST_URI'];
  90. }
  91. else{
  92. $current="";
  93. }
  94. $entrada['HistoricoEntrada']['accion']=$current;
  95. if ($value) {
  96. $mensaje = sprintf($message, $value);
  97. }
  98. else{
  99. $mensaje=$message;
  100. }
  101. $entrada['HistoricoEntrada']['mensaje']=$mensaje;
  102. if($error){
  103. $entrada['HistoricoEntrada']['error']=1;
  104. }
  105. $this->HistoricoEntrada->create();
  106. $this->HistoricoEntrada->save($entrada);
  107. }
  108. */
  109. if ($value) {
  110. $message = sprintf($message, '<strong>' . $value . '</strong>');
  111. }
  112. if ($error) {
  113. $message = '<div class="mensaje error">' . $message . '</div>';
  114. } else {
  115. $message = '<div class="mensaje confirmation">' . $message . '</div>';
  116. }
  117. $this->Session->setFlash($message, '');
  118. if ($url) {
  119. $this->redirect($url);
  120. }
  121. }
  122. /*
  123. * _detectarMovil - Detectar si se accede desde un movil o al subdominio m. y redirigir a la vista de m贸vil si procede
  124. */
  125. protected function _detectarMovil() {
  126. $domain = $_SERVER['SERVER_NAME'];
  127. $userAgent = $_SERVER['HTTP_USER_AGENT'];
  128. //Tipos de dispositivos/browsers m贸viles
  129. $agents = array('Android', 'iPhone', 'BlackBerry', 'Blazer', 'Symbian', 'Dorothy', 'Fennec', 'GoBrowser', 'Windows Phone', 'IEMobile',
  130. 'Maemo Browser', 'MIB/2.2', 'Minimo', 'NetFront', 'Opera Mini', 'Opera Mobi', 'SEMC-Browser', 'Skyfire', 'TeaShark',
  131. 'Teleca', 'uZardWeb', 'Mobile Safari');
  132. $mobileDevice = false;
  133. foreach ($agents as $agent) {
  134. if (strpos($userAgent, $agent) !== false) {
  135. $mobileDevice = true;
  136. break;
  137. }
  138. }
  139. $this->set(compact('mobileDevice'));
  140. }
  141. protected function _checkActive() {
  142. if ($this->Auth->user('id')) {
  143. if (strtotime($this->Auth->user('last_active')) < strtotime("-10 seconds")) {
  144. $this->loadModel('User');
  145. $this->User->id = $this->Auth->user('id');
  146. $now = date('Y-m-d H:i:s');
  147. $this->User->save(array('last_active' => $now));
  148. $this->Session->write('Auth.User.last_active', $now);
  149. }
  150. }
  151. }
  152. }