PageRenderTime 1058ms CodeModel.GetById 29ms RepoModel.GetById 2ms app.codeStats 1ms

/vidcamara/controller/camaras.controller.php

https://gitlab.com/talueses/SIPVE
PHP | 436 lines | 236 code | 72 blank | 128 comment | 46 complexity | 8fc2773c60916ec11d38c47ca0d4889a MD5 | raw file
  1. <?php
  2. /*******************************************************************************\
  3. * @copyright
  4. *
  5. * === SIPve ===
  6. * Sistema Integrado de Protección con capacidades de Videovigilancia
  7. * Control de Acceso y Carnetización para el resguardo físico de instalaciones.
  8. *
  9. * Copyright (C) 2012 Fundación Centro Nacional de Innovación Tecnológica, Cenit.
  10. * Dirección de Investigación, Desarrollo e Innovación.
  11. * Gilda Ramos.
  12. * José Medina.
  13. * Héctor Reverón.
  14. * David Concepción.
  15. * Ronald Delgado.
  16. * Jenner Fuentes.
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation, either VERSION 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. * Para mas información visite
  32. * @link http://repositorio.softwarelibre.gob.ve/ - RNA
  33. * @link http://sourceforge.net/projects/sipve/ - SourceForge
  34. * @link https://gitlab.com/talueses/SIPVE - Gitlab Repositorio.
  35. *
  36. \*******************************************************************************/
  37. ?>
  38. <?php
  39. include_once '../../inicio/controller/controller.php';
  40. /*
  41. * Autor David Concepcion 16-09-2010 CENIT
  42. * Clase Controller{}
  43. * => control de datos
  44. * Metodos set...() y get..()
  45. * => parametros de metodos
  46. * Metodos sipFtp(), sipSsh(), escribirConfiguracion
  47. * => ejecucion de comandos remotos
  48. * Metodos es...()
  49. * => validacion de datos
  50. * Metodos busca_valor() y make_combo()
  51. * => listas dinamicas
  52. *
  53. * Modificado por David Concepcion 21-09-2010 CENIT
  54. * escribirConfiguracion()
  55. * => IP de servidor de aplicacion por parametros de archivos .conf
  56. *
  57. */
  58. class ControllerLocal extends Controller {
  59. var $idservidor = null;
  60. var $idcamara = null;
  61. var $dataServer = null;
  62. var $serverRemoto = null;
  63. var $usuarioRemoto = null;
  64. var $passwordRemoto = null;
  65. var $archivoSubir = null;
  66. var $comandoRemoto = null;
  67. var $conf = null;
  68. var $contenido = null;
  69. var $fileName = null;
  70. public function setIdservidor($idservidor){
  71. $this->idservidor = $idservidor;
  72. }
  73. public function getIdservidor(){
  74. return $this->idservidor;
  75. }
  76. public function setIdcamara($idcamara){
  77. $this->idcamara = $idcamara;
  78. }
  79. public function getIdcamara(){
  80. return $this->idcamara;
  81. }
  82. public function setDataServer($dataServer){
  83. $this->dataServer = $dataServer;
  84. }
  85. public function getDataServer(){
  86. return $this->dataServer;
  87. }
  88. public function setServerRemoto($serverRemoto){
  89. $this->serverRemoto = $serverRemoto;
  90. }
  91. public function getServerRemoto(){
  92. return $this->serverRemoto;
  93. }
  94. public function setUsuarioRemoto($usuarioRemoto){
  95. $this->usuarioRemoto = $usuarioRemoto;
  96. }
  97. public function getUsuarioRemoto(){
  98. return $this->usuarioRemoto;
  99. }
  100. public function setPasswordRemoto($passwordRemoto){
  101. $this->passwordRemoto = $passwordRemoto;
  102. }
  103. public function getPasswordRemoto(){
  104. return $this->passwordRemoto;
  105. }
  106. public function setArchivoSubir($archivoSubir){
  107. $this->archivoSubir = $archivoSubir;
  108. }
  109. public function getArchivoSubir(){
  110. return $this->archivoSubir;
  111. }
  112. public function setComandoRemoto($comandoRemoto){
  113. $this->comandoRemoto = $comandoRemoto;
  114. }
  115. public function getComandoRemoto(){
  116. return $this->comandoRemoto;
  117. }
  118. public function setConf($conf){
  119. $this->conf = $conf;
  120. }
  121. public function getConf(){
  122. return $this->conf;
  123. }
  124. public function setContenido($contenido){
  125. $this->contenido = $contenido;
  126. }
  127. public function getContenido(){
  128. return $this->contenido;
  129. }
  130. public function setFileName($fileName){
  131. $this->fileName = $fileName;
  132. }
  133. public function getFileName(){
  134. return $this->fileName;
  135. }
  136. /*
  137. * Envia archivos al servidor mediante conexion ssh.
  138. * Debe estar instalado el paquete ssh
  139. */
  140. function sipFtp(){
  141. $server_remoto = $this->getServerRemoto();
  142. $usuario_remoto = $this->getUsuarioRemoto();
  143. $password_remoto = $this->getPasswordRemoto();
  144. $archivo_a_subir = $this->getArchivoSubir();
  145. if (!$server_remoto || !$usuario_remoto || !$password_remoto || !$archivo_a_subir){
  146. return false;
  147. }
  148. $script_folder = "../shell_script/";
  149. $script = "sip_ftp.py";
  150. if (!file_exists($script_folder.$script))
  151. return false;
  152. //return "El script $script_folder$script, para copia remota de archivos, no existe en el servidor...";
  153. if (!file_exists($archivo_a_subir))
  154. return false;
  155. //return "El archivo a subir: $archivos_a_subir no existe en el servidor...";
  156. $directorio_remoto = "/home/$usuario_remoto/motion";
  157. $command = $script_folder.$script;
  158. $parametros =" -s $server_remoto -d $directorio_remoto -u $usuario_remoto -p $password_remoto -f $archivo_a_subir";
  159. //echo $command.$parametros."<br>";
  160. $output = shell_exec($command.$parametros);
  161. return $output;
  162. }
  163. /*
  164. * Ejecuta comandos en el servidor mediante conexion ssh
  165. * Debe estar instalado el paquete ssh
  166. */
  167. function sipSsh(){
  168. $server_remoto = $this->getServerRemoto();
  169. $usuario_remoto = $this->getUsuarioRemoto();
  170. $password_remoto = $this->getPasswordRemoto();
  171. $comando_remoto = $this->getComandoRemoto();
  172. if (!$server_remoto || !$usuario_remoto || !$password_remoto || !$comando_remoto){
  173. return false;
  174. }
  175. $script_folder = "../shell_script/";
  176. $script = "sip_ssh.py";
  177. if (!file_exists($script_folder.$script))
  178. return false;
  179. //"El script $script_folder$script, para la ejecucion remota de comandos, no existe en el servidor...";
  180. //EJEMPLO: ./sip_ssh.py -s 150.186.193.179 -u simulab-5 -p cenit -c '/etc/init.d/motion stop'
  181. //$comando_remoto = "/etc/init.d/motion stop";
  182. //$comando_remoto = "\"motion -c ./motion/motion.conf\"";
  183. $command = $script_folder.$script;
  184. $parametros =" -s $server_remoto -u $usuario_remoto -p $password_remoto -c \"$comando_remoto\"";
  185. //echo $command.$parametros."<br>";
  186. $output = shell_exec($command.$parametros);
  187. return $output;
  188. }
  189. /*
  190. * Escribe en servidor de aplicacion el archivo de configuracion con datos guardados en bases de datos
  191. */
  192. function escribirConfiguracion(){
  193. $conf = $this->getConf();
  194. $contenido = $this->getContenido();
  195. $fileName = $this->getFileName();
  196. if (!$conf || !$contenido || !$fileName){
  197. return false;
  198. }
  199. foreach ($conf as $key => $value) {
  200. //echo $key . "--> ".$value."<br>";
  201. $contenido = str_replace("{".$key."}", $value, $contenido );
  202. }
  203. $contenido = str_replace("{SERVER_ADDR}", $_SERVER["SERVER_ADDR"], $contenido ); // IP ADDRESS APACHE SERVER
  204. $exito = file_put_contents('../motion/'.$fileName.'.conf', $contenido);
  205. return $exito;
  206. }
  207. /**
  208. * Eliminar archivo de configuracion con datos guardados en bases de datos
  209. */
  210. function eliminarConfiguracion(){
  211. $fileName = $this->getFileName();
  212. unlink('../motion/'.$fileName.'.conf');
  213. }
  214. /**
  215. * Formatea la hora militar a normal en formato srting 1800 => 06:00 p.m.
  216. * @param int(4) $hora hora a traducir
  217. * @param string $tipo tipo de horario: 12Hrs (a.m. o p.m.), 24Hrs (18:00)
  218. * @return string Devuelve la hora en formato hh:00 aa
  219. */
  220. static function formatHorarioToView($hora,$tipo){
  221. if (strlen($hora) == 3){
  222. $horas = substr($hora, 0,1);
  223. $minutos = substr($hora, 1,2);
  224. }
  225. if (strlen($hora) == 4 || strlen($hora) == 6){
  226. $horas = substr($hora, 0,2);
  227. $minutos = substr($hora, 2,2);
  228. }
  229. if (strlen($hora) == 6){
  230. $segundos = ":".substr($hora, 4,2);
  231. }
  232. if ($tipo=="24"){
  233. $hora = $horas.":".$minutos.$segundos;
  234. }
  235. if ($tipo=="12"){
  236. $am_pm = "AM";
  237. if ($horas>12){
  238. $horas -= 12;
  239. if ($horas < 10){
  240. $horas = "0".$horas;
  241. }
  242. $am_pm = "PM";
  243. }
  244. $hora = $horas.":".$minutos.$segundos.$am_pm;
  245. }
  246. return $hora;
  247. }
  248. /**
  249. * Evalua el choque de horas entre dos horarios
  250. * @param int(4) $limiteInferiorAChequear Hora desde a comparar
  251. * @param int(4) $limiteSuperiorAChequear Hora hasta a comparar
  252. * @param int(4) $limiteInferiorExistente Hora desde con que se va acomparar
  253. * @param int(4) $limiteSuperiorExistente Hora hasta con que se va acomparar
  254. * @return boolean Devuelve true si hay choque de horario
  255. */
  256. static function choqueHorario ($limiteInferiorAChequear,$limiteSuperiorAChequear,$limiteInferiorExistente,$limiteSuperiorExistente){
  257. //------------------------------
  258. // Validamos que sean correctos
  259. //------------------------------
  260. if ($limiteInferiorAChequear > $limiteSuperiorAChequear) {
  261. //echo "1";
  262. return true;
  263. }
  264. if ($limiteInferiorExistente > $limiteSuperiorExistente) {
  265. //echo "2";
  266. return true;
  267. }
  268. //---------------------------------------------------------------
  269. // Verificamos primero los extremos
  270. //---------------------------------------------------------------
  271. if ($limiteSuperiorAChequear <= $limiteInferiorExistente){
  272. //echo "caso extremo 1";
  273. return false;
  274. }
  275. if ($limiteInferiorAChequear >= $limiteSuperiorExistente){
  276. //echo "caso extremo 2";
  277. return false;
  278. }
  279. //---------------------------------------------------------------
  280. // Dejamos fijo el limite inferior a chequear y variamos el limite
  281. // superior a chequear.
  282. //---------------------------------------------------------------
  283. if ($limiteInferiorAChequear > $limiteInferiorExistente &&
  284. $limiteSuperiorAChequear == $limiteInferiorExistente){
  285. //echo "limite inferior caso 1";
  286. return false;
  287. }
  288. if ($limiteInferiorAChequear <= $limiteInferiorExistente &&
  289. $limiteSuperiorAChequear > $limiteInferiorExistente){
  290. //echo "limite inferior caso 2";
  291. return true;
  292. }
  293. if ($limiteInferiorAChequear <= $limiteInferiorExistente &&
  294. $limiteSuperiorAChequear == $limiteSuperiorExistente){
  295. //echo "limite inferior caso 3";
  296. return true;
  297. }
  298. if ($limiteInferiorAChequear <= $limiteInferiorExistente &&
  299. $limiteSuperiorAChequear > $limiteSuperiorExistente){
  300. //echo "limite inferior caso 4";
  301. return true;
  302. }
  303. //---------------------------------------------------------------
  304. // Dejamos fijo el limite superior fijo y variamos el limite
  305. // inferior a chequear.
  306. //---------------------------------------------------------------
  307. if ($limiteSuperiorAChequear > $limiteSuperiorExistente &&
  308. $limiteInferiorAChequear <= $limiteInferiorExistente){
  309. //echo "limite superior caso 1";
  310. return true;
  311. }
  312. if ($limiteSuperiorAChequear >= $limiteSuperiorExistente &&
  313. $limiteInferiorAChequear > $limiteInferiorExistente){
  314. //echo "limite superior caso 2";
  315. return true;
  316. }
  317. if ($limiteSuperiorAChequear >= $limiteSuperiorExistente &&
  318. $limiteInferiorAChequear == $limiteSuperiorExistente){
  319. // echo "limite superior caso 3";
  320. return false;
  321. }
  322. if ($limiteSuperiorAChequear >= $limiteSuperiorExistente &&
  323. $limiteInferiorAChequear > $limiteSuperiorExistente){
  324. //echo "limite superior caso 4";
  325. return false;
  326. }
  327. // Otros casos: limite inferior dentro del intervalo
  328. if ($limiteInferiorAChequear >= $limiteInferiorExistente &&
  329. $limiteSuperiorAChequear >$limiteInferiorExistente){
  330. //echo "14";
  331. return true;
  332. }
  333. if ($limiteInferiorAChequear == $limiteSuperiorExistente &&
  334. $limiteSuperiorAChequear >$limiteSuperiorExistente){
  335. //echo "15";
  336. return false;
  337. }
  338. // Otros casos: limite superior dentro del intervalo
  339. if ($limiteSuperiorAChequear < $limiteSuperiorExistente &&
  340. $limiteInferiorAChequear < $limiteInferiorExistente){
  341. //echo "16";
  342. return true;
  343. }
  344. if ($limiteSuperiorAChequear == $limiteInferiorExistente &&
  345. $limiteInferiorAChequear < $limiteInferiorExistente){
  346. //echo "17";
  347. return true;
  348. }else{
  349. // si hay otro caso no contemplado, regreso que hay choque
  350. //echo "18";
  351. return true;
  352. }
  353. }
  354. function formatHorarioToModel($hora){
  355. $horas = substr($hora,0,2);
  356. $minutos = substr($hora,3,2);
  357. /*$meridian = substr($hora,5,2);
  358. if ($meridian=="PM"){
  359. $horas += 12;
  360. }*/
  361. return $horas.$minutos;
  362. }
  363. }
  364. ?>