PageRenderTime 483ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/core/common.php

https://github.com/alugo/Goteo
PHP | 118 lines | 60 code | 15 blank | 43 comment | 3 complexity | 018f126e8bfd1c9ddb8253070cb0d4ac 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 {
  21. require_once("core/registry.php");
  22. class GoteoLogLevel {
  23. const DEBUG = 10;
  24. const INFO = 20;
  25. const NOTICE = 25;
  26. const WARNING = 30;
  27. const ERROR = 40;
  28. const CRITICAL = 50;
  29. };
  30. class TinyLogger {
  31. var $level_cutoff;
  32. public function __construct($cutoff = GoteLogLevel::DEBUG) {
  33. $this->level_cutoff = $cutoff;
  34. }
  35. public function log($msg, $level) {
  36. if($level >= $this->level_cutoff) {
  37. error_log($msg);
  38. }
  39. }
  40. }
  41. $logger = new TinyLogger(GoteoLogLevel::DEBUG);
  42. Goteo\Core\Registry::set('log', $logger);
  43. /**
  44. * Traza informaci贸n sobre el recurso especificado de forma legible.
  45. *
  46. * @param type mixed $resource Recurso
  47. */
  48. function trace ($resource = null) {
  49. echo '<pre>' . print_r($resource, 1) . '</pre>';
  50. }
  51. /**
  52. * Vuelca informaci贸n sobre el recurso especificado de forma detallada.
  53. *
  54. * @param type mixed $resource Recurso
  55. */
  56. function dump ($resource = null) {
  57. echo '<pre>' . var_dump($resource) . '</pre>';
  58. }
  59. function _log($msg, $level = GoteoLogLevel::DEBUG) {
  60. Goteo\Core\Registry::get('log')->log($msg, $level);
  61. }
  62. /**
  63. * Genera un mktime (UNIX_TIMESTAMP) a partir de una fecha (DATE/DATETIME/TIMESTAMP)
  64. * @param $str
  65. */
  66. function date2time ($str) {
  67. list($date, $time) = explode(' ', $str);
  68. list($year, $month, $day) = explode('-', $date);
  69. list($hour, $minute, $second) = explode(':', $time);
  70. $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
  71. return $timestamp;
  72. }
  73. /**
  74. * Checkea si todos los indices del array son vacios
  75. * @param array $mixed
  76. * @return boolean
  77. */
  78. function array_empty($mixed) {
  79. if (is_array($mixed)) {
  80. foreach ($mixed as $value) {
  81. if (!array_empty($value)) {
  82. return false;
  83. }
  84. }
  85. }
  86. elseif (!empty($mixed)) {
  87. return false;
  88. }
  89. return true;
  90. }
  91. /**
  92. * Numberformat para importes
  93. */
  94. function amount_format($amount, $decs = 0) {
  95. return number_format($amount, $decs, ',', '.');
  96. }
  97. /*
  98. * Verifica si una cadena es sha1
  99. */
  100. function is_sha1($str) {
  101. return (bool) preg_match('/^[0-9a-f]{40}$/i', $str);
  102. }
  103. }