PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/controller/cron/verify.php

https://github.com/alugo/Goteo
PHP | 133 lines | 66 code | 23 blank | 44 comment | 17 complexity | 780c8f914caf2a7527fd40ec4c550da0 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. * Copyright (C) 2012 Platoniq y Fundación Fuentes Abiertas (see README for details)
  4. * This file is part of Goteo.
  5. *
  6. * Goteo is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Goteo is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
  18. *
  19. */
  20. namespace Goteo\Controller\Cron {
  21. use Goteo\Model,
  22. Goteo\Core\Redirection,
  23. Goteo\Core\Error;
  24. class Verify {
  25. public static function process ($debug = false) {
  26. // eliminamos ACL innecesario
  27. $sql = "DELETE FROM `acl`
  28. WHERE id > 1000
  29. AND role_id = 'user'
  30. AND user_id != '*'
  31. AND (url LIKE '%project/edit%' OR url LIKE '%project/delete%')
  32. AND DATE_FORMAT(from_unixtime(unix_timestamp(now()) - unix_timestamp(`timestamp`)), '%j') > 30
  33. ";
  34. // echo $sql . '<br />';
  35. $query = Model\Project::query($sql);
  36. $count = $query->rowCount();
  37. if ($debug) echo "Eliminados $count registros de ACL antiguo.<br />";
  38. // eliminamos feed antiguo
  39. $sql1 = "DELETE
  40. FROM `feed`
  41. WHERE type != 'goteo'
  42. AND DATE_FORMAT(from_unixtime(unix_timestamp(now()) - unix_timestamp(`datetime`)), '%j') > 30
  43. AND (url NOT LIKE '%updates%' OR url IS NULL)
  44. ";
  45. // echo $sql . '<br />';
  46. $query1 = Model\Project::query($sql1);
  47. $count1 = $query1->rowCount();
  48. if ($debug) echo "Eliminados $count1 registros de feed.<br />";
  49. // eliminamos mail antiguo
  50. $sql2 = "DELETE
  51. FROM `mail`
  52. WHERE (template != 33 OR template IS NULL)
  53. AND DATE_FORMAT(from_unixtime(unix_timestamp(now()) - unix_timestamp(`date`)), '%j') > 30
  54. ";
  55. // echo $sql2 . '<br />';
  56. $query2 = Model\Project::query($sql2);
  57. $count2 = $query2->rowCount();
  58. if ($debug) echo "Eliminados $count2 registros de mail.<br />";
  59. // eliminamos registros de imágenes cuyo archivo no esté en el directorio de imágenes
  60. // busco aportes incompletos con codigo de autorización
  61. $sql5 = "SELECT * FROM invest WHERE status = -1 AND transaction IS NOT NULL";
  62. $query5 = Model\Project::query($sql5);
  63. foreach ($query5->fetchAll(\PDO::FETCH_OBJ) as $row) {
  64. @mail(\GOTEO_FAIL_MAIL,
  65. 'Aporte Incompleto con numero de autorización. En ' . SITE_URL,
  66. 'Aporte Incompleto con numero de autorización: <pre>' . print_r($row, 1). '</pre>');
  67. }
  68. // eliminamos aportes incompletos
  69. /*
  70. $sql4 = "DELETE
  71. FROM `invest`
  72. WHERE status = -1
  73. AND DATE_FORMAT(from_unixtime(unix_timestamp(now()) - unix_timestamp(`datetime`)), '%j') > 120
  74. ";
  75. //echo $sql4 . '<br />';
  76. $query4 = Model\Project::query($sql4);
  77. $count4 = $query4->rowCount();
  78. // -- eliminamos registros relativos a aportes no existentes
  79. Model\Project::query("DELETE FROM `invest_address` WHERE invest NOT IN (SELECT id FROM `invest`)");
  80. Model\Project::query("DELETE FROM `invest_detail` WHERE invest NOT IN (SELECT id FROM `invest`)");
  81. Model\Project::query("DELETE FROM `invest_reward` WHERE invest NOT IN (SELECT id FROM `invest`)");
  82. echo "Eliminados $count4 aportes incompletos y sus registros (recompensa, dirección, detalles) relacionados.<br />";
  83. */
  84. if ($debug) echo "<hr /> Iniciamos caducidad de tokens<br/>";
  85. // eliminamos los tokens que tengan más de 4 días
  86. $sql5 = "SELECT id, token FROM user WHERE token IS NOT NULL AND token != '' AND token LIKE '%¬%'";
  87. $query5 = Model\Project::query($sql5);
  88. foreach ($query5->fetchAll(\PDO::FETCH_OBJ) as $row) {
  89. $parts = explode('¬', $row->token);
  90. $datepart = strtotime($parts[2]);
  91. $today = date('Y-m-d');
  92. $datedif = strtotime($today) - $datepart;
  93. $days = round($datedif / 86400);
  94. if ($days > 4 || !isset($parts[2])) {
  95. if ($debug) echo "User: $row->id ; Token: $row->token ; Datepart: $parts[2] => $datepart ; Compare: $today => $datedif ; Days: $days ; ";
  96. if (Model\Project::query("UPDATE user SET token = '' WHERE id = ?", array($row->id))) {
  97. if ($debug) echo "Token borrado.";
  98. } else {
  99. if ($debug) echo "Fallo al borrar Token!!!";
  100. }
  101. if ($debug) echo "<br />";
  102. }
  103. }
  104. if ($debug) echo "<br />";
  105. echo 'Verify Listo!';
  106. return;
  107. }
  108. }
  109. }