1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=0);
24
25namespace Ampache\Module\System;
26
27/**
28 * Error class
29 *
30 * This is the basic error class, its better now that we can use php5
31 * hello static functions and variables
32 */
33class AmpError
34{
35    private static $state  = false; // set to one when an error occurs
36     public static $errors = array(); // Errors array key'd array with errors that have occurred
37
38    /**
39     * __destruct
40     * This saves all of the errors that are left into the session
41     */
42    public function __destruct()
43    {
44        foreach (self::$errors as $key => $error) {
45            $_SESSION['errors'][$key] = $error;
46        }
47    } // __destruct
48
49    /**
50     * add
51     * This is a public static function it adds a new error message to the array
52     * It can optionally clobber rather then adding to the error message
53     * @param string $name
54     * @param string $message
55     * @param integer $clobber
56     */
57    public static function add($name, $message, $clobber = 0)
58    {
59        // Make sure its set first
60        if (!isset(AmpError::$errors[$name])) {
61            AmpError::$errors[$name]   = $message;
62            AmpError::$state           = true;
63            $_SESSION['errors'][$name] = $message;
64        } elseif ($clobber) {
65            // They want us to clobber it
66            AmpError::$state           = true;
67            AmpError::$errors[$name]   = $message;
68            $_SESSION['errors'][$name] = $message;
69        } else {
70            // They want us to append the error, add a BR\n and then the message
71            AmpError::$state = true;
72            AmpError::$errors[$name] .= "<br />\n" . $message;
73            $_SESSION['errors'][$name] .= "<br />\n" . $message;
74        }
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    /**
85     * occurred
86     * This returns true / false if an error has occurred anywhere
87     * @return boolean
88     */
89    public static function occurred()
90    {
91        if (self::$state == '1') {
92            return true;
93        }
94
95        return false;
96    } // occurred
97
98    /**
99     * get
100     * This returns an error by name
101     * @param string $name
102     * @return string
103     */
104    public static function get($name)
105    {
106        if (!isset(AmpError::$errors[$name])) {
107            return '';
108        }
109
110        return AmpError::$errors[$name];
111    } // get
112
113    /**
114     * display
115     * This prints the error out with a standard Error class span
116     * Ben Goska: Renamed from print to display, print is reserved
117     * @param string $name
118     * @return string
119     */
120    public static function display($name)
121    {
122        // Be smart about this, if no error don't print
123        if (isset(AmpError::$errors[$name])) {
124            return self::getErrorsFormatted($name);
125        }
126
127        return '';
128    } // display
129
130    public static function getErrorsFormatted(string $name): string
131    {
132        if (isset(AmpError::$errors[$name])) {
133            return '<p class="alert alert-danger">' . T_(AmpError::$errors[$name]) . '</p>';
134        }
135
136        return '';
137    }
138}
139