FileDownload.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class FileDownload extends CI_Controller
  4. {
  5. //TODO: Gestionar descarga de archivos por carpeta dia/hora
  6. protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
  7. protected $rutaDiariaFormat = FCPATH . "DOWNLOADS/DAILY/format/";
  8. protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
  9. protected $enlaceAtom = "";
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. $this->load->helper('file');
  14. $this->load->helper('xml');
  15. }
  16. public function index()
  17. {
  18. $this->downloadProcess();
  19. }
  20. private function downloadProcess()
  21. {
  22. $fileName = basename(URL_DIARIA);
  23. $this->downloadFile($fileName, URL_DIARIA, $this->rutaDiaria);
  24. $archivoAtom = $this->getFile($this->rutaDiaria . $fileName, $fileName);
  25. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  26. //while ($nuevoAtomAttrNext !== "") {
  27. $aux = 0;
  28. while ($aux <= 3) {
  29. $fileName = basename($nuevoAtomAttrNext);
  30. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiaria);
  31. $archivoAtom = $this->getFile($this->rutaDiaria . $fileName, $fileName);
  32. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  33. $aux++;
  34. }
  35. }
  36. private function getFile($url = URL_DIARIA, $fileName)
  37. {
  38. $feed = implode(file($url));
  39. $xml = simplexml_load_string($feed);
  40. $json = json_encode($xml);
  41. $array = json_decode($json, true);
  42. $this->formatXml($feed, $fileName);
  43. return $array;
  44. }
  45. private function formatXml($xml, $fileName)
  46. {
  47. $openingFormat = $this->regExIniciosEsquema($xml);
  48. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  49. file_put_contents($this->rutaDiariaFormat . $fileName, $closureFormat);
  50. }
  51. private function readNextAttribute($archivo)
  52. {
  53. $enlace = "";
  54. foreach ($archivo['link'] as $row) {
  55. $attrNext = $row['@attributes']['rel'];
  56. $attrHref = $row['@attributes']['href'];
  57. if ($attrNext === "next") {
  58. $enlace = $attrHref;
  59. }
  60. }
  61. return $enlace;
  62. }
  63. private function downloadFile($fileName, $url, $ruta)
  64. {
  65. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. }
  71. private function regExIniciosEsquema($xml)
  72. {
  73. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  74. $result = preg_replace($pattern, "<", $xml);
  75. return $result;
  76. }
  77. private function regExFinalesEsquema($xml)
  78. {
  79. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  80. $result = preg_replace($pattern, "</", $xml);
  81. return $result;
  82. }
  83. }