FileDownload.php 2.8KB

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