summaryrefslogtreecommitdiff
path: root/src/application/mystic/forum/Messaging.php
blob: 3530d5eb1052ad1cc7e829eab530c9c9b6706f84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);

namespace mystic\forum;

use mystic\forum\utils\StaticClass;

final class Messaging {
    use StaticClass;

    const ENT_FLAGS = \ENT_COMPAT | \ENT_HTML401 | \ENT_SUBSTITUTE;

    protected static function message(array $items, string $headerText, string $headerColor, string $headerTextColor): void {
        //echo "<HTML><BODY>\n";
        echo "<TABLE cellspacing=2 cellpadding=0 bgcolor=gray><TR><TD>\n";

        echo "<TABLE width=\"100%\" cellspacing=0 cellpadding=4 bgcolor=\"".htmlentities($headerColor, self::ENT_FLAGS)."\"><TR><TD align=center>\n";
        echo "<FONT face=Arial size=3 color=\"".htmlentities($headerTextColor, self::ENT_FLAGS)."\"><B>".htmlentities($headerText, self::ENT_FLAGS)."</B></FONT>\n";
        echo "</TD></TR></TABLE>\n";
        
        echo "</TD></TR><TR><TD>\n";

        echo "<TABLE width=\"100%\" cellspacing=0 cellpadding=4 bgcolor=WHITE><TR><TD align=left><FONT size=2 face=Arial color=BLACK>\n";
        
        foreach ($items as $item) {
            if (is_scalar($item)) {
                echo "<P>" . htmlentities(strval($item), self::ENT_FLAGS) . "</P>\n";
            } elseif (is_array($item)) {
                if (count(array_keys($item)) === 2 && isset($item["___!type"]) && isset($item["___!content"])) {
                    // special item
                    switch ($item["___!type"]) {
                        case "HTML":
                            echo $item["___!content"];
                            break;
                        default:
                            echo "invalid";
                            break;
                    }
                } elseif (array_is_list($item)) {
                    echo "<UL>\n";
                    foreach ($item as $i)
                        echo "<LI>" . htmlentities($i, self::ENT_FLAGS) . "</LI>\n";
                    echo "</UL>\n";
                } else {
                    echo "<TABLE cellspacing=0 cellpadding=2 border=1>\n";
                    foreach ($item as $k => $i) {
                        echo "<TR>\n";
                        echo "<TH align=left>" . htmlentities($k, self::ENT_FLAGS) . "</TH>\n";
                        echo "<TD>";
                        if (is_scalar($i)) {
                            echo htmlentities($i, self::ENT_FLAGS);
                        } else {
                            echo gettype($i);
                        }
                        echo "</TD>";
                        echo "</TR>\n";
                    }
                    echo "</TABLE>\n";
                }
            } else {
                echo gettype($item);
            }
        }

        echo "</FONT></TD></TR></TABLE>\n";

        echo "</TD></TR></TABLE>\n";
        //echo "</BODY></HTML>\n";
    }

    public static function bold(string $contents): array {
        return self::html("<P><B>" . htmlentities($contents, self::ENT_FLAGS) . "</B></P>\n");
    }

    public static function italic(string $contents): array {
        return self::html("<P><I>" . htmlentities($contents, self::ENT_FLAGS) . "</I></P>\n");
    }

    public static function bold_italic(string $contents): array {
        return self::html("<P><B><I>" . htmlentities($contents, self::ENT_FLAGS) . "</I></B></P>\n");
    }

    public static function html(string $contents): array {
        return [
            "___!type" => "HTML",
            "___!content" => $contents,
        ];
    }

    public static function info(string|array $contents): void {
        if (is_string($contents))
            $contents = [$contents];
        self::message($contents, "INFORMATION", "SKYBLUE", "BLACK");
    }

    public static function error(string|array $contents): void {
        if (is_string($contents))
            $contents = [$contents];
        self::message($contents, "ERROR", "RED", "WHITE");
    }
}