• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cypht-org / cypht / 29328457375

14 Jul 2026 11:18AM UTC coverage: 87.259% (-0.02%) from 87.28%
29328457375

push

travis-ci

web-flow
Merge pull request #2031 from IrAlfred/fix-phpunit-warnings-php84

fix(unit): fix PHPUnit warnings and PHP 8.4 deprecations in test suite

58 of 60 new or added lines in 3 files covered. (96.67%)

5794 of 6640 relevant lines covered (87.26%)

11.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.39
/lib/format.php
1
<?php
2

3
/**
4
 * Format output
5
 * @package framework
6
 * @subpackage format
7
 */
8

9
/**
10
 * Base class for output formatting. Currently JSON and HTML5 formats are
11
 * supported. To add support for a new format this class must be extended
12
 * and the content method needs to be overridden.
13
 * @abstract
14
 */
15
abstract class HM_Format {
16

17
    protected $config;
18

19
    /**
20
     * Init
21
     * @param object $config site config object
22
     */
23
    public function __construct($config) {
8✔
24
        $this->config = $config;
8✔
25
    }
26

27
    /**
28
     * Return combined output from all modules. Must be overridden by specific
29
     * output classes
30
     * @param array $allowed_output allowed fields for JSON responses
31
     * @return mixed combined output
32
     */
33
    abstract protected function content($input, $allowed_output);
34
}
35

36
/**
37
 * Handles JSON formatted results for AJAX requests
38
 */
39
class Hm_Format_JSON extends HM_Format {
40

41
    /**
42
     * Run modules and merge + filter the result array
43
     * @param array $output data from the handler modules
44
     * @param array $allowed_output allowed fields for JSON responses
45
     * @return string encoded data to be sent to the browser
46
     */
47
    public function content($output, $allowed_output) {
1✔
48
        $output['router_user_msgs'] = Hm_Msgs::getRaw();
1✔
49
        $output = $this->filter_all_output($output, $allowed_output);
1✔
50
        if ($this->config->get('encrypt_ajax_requests', false)) {
1✔
51
            $output = ['payload' => Hm_Crypt_Base::ciphertext(json_encode($output, JSON_FORCE_OBJECT), Hm_Request_Key::generate())];
1✔
52
        }
53
        return json_encode($output, JSON_FORCE_OBJECT);
1✔
54
    }
55

56
    /**
57
     * Filter data against module set white lists before sending it to the browser
58
     * @param array $data output module data to filter
59
     * @param array $allowed set of white list filters
60
     * @return array filtered data
61
     */
62
    public function filter_all_output($data, $allowed) {
2✔
63
        foreach ($data as $name => $value) {
2✔
64
            if (!array_key_exists($name, $allowed)) {
2✔
65
                unset($data[$name]);
1✔
66
                continue;
1✔
67
            }
68
            $new_value = $this->filter_output($name, $value, $allowed);
2✔
69
            if ($new_value === NULL) {
2✔
70
                unset($data[$name]);
1✔
71
                continue;
1✔
72
            }
73
            $data[$name] = $new_value;
2✔
74
        }
75
        return $data;
2✔
76
    }
77

78
    /**
79
     * Filter a single output value
80
     * @param string $name
81
     * @param mixed $value
82
     * @param array $allowed
83
     * @return mixed
84
     */
85
    private function filter_output($name, $value, $allowed) {
2✔
86
        if ($allowed[$name][1]) {
2✔
87
            $new_value = filter_var($value, $allowed[$name][0], $allowed[$name][1]);
1✔
88
        } else {
89
            $new_value = filter_var($value, $allowed[$name][0]);
2✔
90
        }
91
        if ($new_value === false && $allowed[$name] != FILTER_VALIDATE_BOOLEAN) {
2✔
92
            return NULL;
1✔
93
        }
94
        return $new_value;
2✔
95
    }
96
}
97

98
/**
99
 * Handles HTML5 formatted results for normal HTTP requests
100
 */
101
class Hm_Format_HTML5 extends HM_Format {
102

103
    /**
104
     * Collect and return content from modules for HTTP requests
105
     * @param array $output data from the output modules
106
     * @param array $allowed_output allowed fields for JSON responses
107
     * @return string HTML5 content
108
     */
109
    public function content($output, $allowed_output) {
7✔
110
        if (array_key_exists('router_module_list', $output)) {
7✔
111
            unset($output['router_module_list']);
7✔
112
        }
113
        if (array_is_list($output)) {
7✔
NEW
114
            return implode('', $output);
×
115
        }
116
        return implode('', array_filter($output, 'is_string'));
7✔
117
    }
118
}
119

120
/**
121
 * binary safe wrapper around json encode/decode using base64
122
 */
123
class Hm_Transform {
124

125
    /**
126
     * Convert an array to a string
127
     * @param mixed $data data to be transformed to a string
128
     * @param string $encoding encoding to use for values
129
     * @return string on success, false on failure
130
     */
131
    public static function stringify($data, $encoding = 'base64_encode') {
20✔
132
        if (is_string($data)) {
20✔
133
            return $data;
4✔
134
        }
135
        if (!is_array($data)) {
17✔
136
            return (string) $data;
1✔
137
        }
138
        return @json_encode(self::hm_encode($data, $encoding));
17✔
139

140
    }
141

142
    /**
143
     * @param string $data
144
     * @return array|false
145
     */
146
    public static function convert($data) {
16✔
147
        if (mb_substr($data, 0, 2) === 'a:') {
16✔
148
            return @unserialize($data);
1✔
149
        } elseif (mb_substr($data, 0, 1) === '{' || mb_substr($data, 0, 1) === '[') {
16✔
150
            return @json_decode($data, true);
12✔
151
        }
152
        return false;
5✔
153
    }
154

155
    /**
156
     * Convert a stringified array back to an array
157
     * @param string|false $data data to be transformed from a string
158
     * @param string $encoding encoding to use for values
159
     * @param boolean $return return original string if true
160
     * @return mixed array on success, false or original string on failure
161
     */
162
    public static function unstringify($data, $encoding = 'base64_decode', $return = false) {
15✔
163
        if (!is_string($data) || !trim($data)) {
15✔
164
            return false;
8✔
165
        }
166
        $result = self::convert($data);
9✔
167
        if (is_array($result)) {
9✔
168
            return self::hm_encode($result, $encoding);
7✔
169
        }
170
        if ($return) {
3✔
171
            return $data;
3✔
172
        }
173
        return false;
1✔
174
    }
175

176
    /**
177
     * Recursively encode values in an array
178
     * @param array $data data to encode values for
179
     * @param string $encoding the type of encoding to use
180
     * @return array
181
     */
182
    public static function hm_encode($data, $encoding) {
18✔
183
        $result = [];
18✔
184
        foreach ($data as $name => $val) {
18✔
185
            if (is_array($val)) {
8✔
186
                $result[$name] = self::hm_encode($val, $encoding);
1✔
187
            } else {
188
                if (is_string($val)) {
8✔
189
                    $result[$name] = $encoding($val);
8✔
190
                } else {
191
                    $result[$name] = $val;
1✔
192
                }
193
            }
194
        }
195
        return $result;
18✔
196
    }
197
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc