/lib/class/error.class.php

https://gitlab.com/x33n/ampache · PHP · 145 lines · 60 code · 21 blank · 64 comment · 8 complexity · 6aafc15349a4d1715684d0694cf26aac MD5 · raw file

  1. <?php
  2. /* vim:set softtabstop=4 shiftwidth=4 expandtab: */
  3. /**
  4. *
  5. * LICENSE: GNU General Public License, version 2 (GPLv2)
  6. * Copyright 2001 - 2015 Ampache.org
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License v2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANT ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. /**
  23. * Error class
  24. *
  25. * This is the baic error class, its better now that we can use php5
  26. * hello static functions and variables
  27. *
  28. */
  29. class Error
  30. {
  31. private static $state = false; // set to one when an error occurs
  32. private static $errors = array(); // Errors array key'd array with errors that have occured
  33. /**
  34. * __constructor
  35. * This does nothing... amazing isn't it!
  36. */
  37. private function __construct()
  38. {
  39. // Rien a faire
  40. } // __construct
  41. /**
  42. * __destruct
  43. * This saves all of the errors that are left into the session
  44. */
  45. public function __destruct()
  46. {
  47. foreach (self::$errors as $key=>$error) {
  48. $_SESSION['errors'][$key] = $error;
  49. }
  50. } // __destruct
  51. /**
  52. * add
  53. * This is a public static function it adds a new error message to the array
  54. * It can optionally clobber rather then adding to the error message
  55. */
  56. public static function add($name,$message,$clobber=0)
  57. {
  58. // Make sure its set first
  59. if (!isset(Error::$errors[$name])) {
  60. Error::$errors[$name] = $message;
  61. Error::$state = true;
  62. $_SESSION['errors'][$name] = $message;
  63. }
  64. // They want us to clobber it
  65. elseif ($clobber) {
  66. Error::$state = true;
  67. Error::$errors[$name] = $message;
  68. $_SESSION['errors'][$name] = $message;
  69. }
  70. // They want us to append the error, add a BR\n and then the message
  71. else {
  72. Error::$state = true;
  73. Error::$errors[$name] .= "<br />\n" . $message;
  74. $_SESSION['errors'][$name] .= "<br />\n" . $message;
  75. }
  76. // If on SSE worker, output the error directly.
  77. if (defined('SSE_OUTPUT')) {
  78. echo "data: display_sse_error('" . addslashes($message) . "')\n\n";
  79. ob_flush();
  80. flush();
  81. }
  82. } // add
  83. /**
  84. * occurred
  85. * This returns true / false if an error has occured anywhere
  86. */
  87. public static function occurred()
  88. {
  89. if (self::$state == '1') { return true; }
  90. return false;
  91. } // occurred
  92. /**
  93. * get
  94. * This returns an error by name
  95. */
  96. public static function get($name)
  97. {
  98. if (!isset(Error::$errors[$name])) { return ''; }
  99. return Error::$errors[$name];
  100. } // get
  101. /**
  102. * display
  103. * This prints the error out with a standard Error class span
  104. * Ben Goska: Renamed from print to display, print is reserved
  105. */
  106. public static function display($name)
  107. {
  108. // Be smart about this, if no error don't print
  109. if (!isset(Error::$errors[$name])) { return ''; }
  110. echo '<p class="alert alert-danger">' . T_(Error::$errors[$name]) . '</p>';
  111. } // display
  112. /**
  113. * auto_init
  114. * This loads the errors from the session back into Ampache
  115. */
  116. public static function auto_init()
  117. {
  118. if (!is_array($_SESSION['errors'])) { return false; }
  119. // Re-insert them
  120. foreach ($_SESSION['errors'] as $key=>$error) {
  121. self::add($key,$error);
  122. }
  123. } // auto_init
  124. } // Error