Passed
Push — master ( 05b645...e6fae1 )
by Andreas
10:21
created

midcom_helper_misc::find_node_by_component()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0218

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 1
dl 0
loc 28
ccs 16
cts 18
cp 0.8889
crap 4.0218
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.helper
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Cocur\Slugify\Slugify;
10
11
/**
12
 * Miscellaneous helper functions
13
 *
14
 * @package midcom.helper
15
 */
16
class midcom_helper_misc
17
{
18 14
    public static function random_string(int $length, string $characters) : string
19
    {
20 14
        if ($length < 1) {
21
            throw new InvalidArgumentException('invalid length');
22
        }
23 14
        $size = strlen($characters) - 1;
24 14
        if ($size < 1) {
25
            throw new InvalidArgumentException('invalid characters');
26
        }
27 14
        $return = '';
28 14
        for ($i = 0; $i < $length; $i++) {
29 14
            $return .= $characters[random_int(0, $size)];
30
        }
31 14
        return $return;
32
    }
33
34 36
    public static function urlize(string $input) : string
35
    {
36 36
        $slugify = new Slugify;
37 36
        return $slugify->slugify($input);
38
    }
39
40
    /**
41
     * Turn midcom config files into PHP arrays
42
     */
43 529
    public static function parse_config($data, string $path) : array
44
    {
45
        try {
46 529
            return eval("return [{$data}\n];");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
47
        } catch (ParseError $e) {
0 ignored issues
show
Unused Code introduced by
catch (\ParseError $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
48
            throw new midcom_error('Failed to parse config data: ' . $e->getMessage() . ' in ' . $path . ' line ' . $e->getLine());
49
        }
50
    }
51
52
    /**
53
     * This helper function searches for a snippet either in the Filesystem
54
     * or in the database and returns its content or code-field, respectively.
55
     *
56
     * Prefix the snippet Path with 'file:' for retrieval of a file relative to
57
     * MIDCOM_ROOT; omit it to get the code field of a Snippet.
58
     *
59
     * Any error (files not found) will return null. If you want to trigger an error,
60
     * look for midcom_helper_misc::get_snippet_content.
61
     *
62
     * @return string       The content of the snippet/file.
63
     */
64 529
    public static function get_snippet_content_graceful(string $path)
65
    {
66 529
        static $cached_snippets = [];
67
68 529
        if (!array_key_exists($path, $cached_snippets)) {
69 63
            $cached_snippets[$path] = null;
70 63
            if (str_starts_with($path, 'file:') || str_starts_with($path, 'conf:')) {
71 63
                $filename = self::resolve_path($path);
72 63
                if (is_readable($filename)) {
73 63
                    $cached_snippets[$path] = file_get_contents($filename);
74
                }
75
            } else {
76 18
                $snippet = new midgard_snippet();
77 18
                if ($snippet->get_by_path($path)) {
78
                    midcom::get()->cache->content->register($snippet->guid);
79
                    $cached_snippets[$path] = $snippet->code;
80
                }
81
            }
82
        }
83
84 529
        return $cached_snippets[$path];
85
    }
86
87 66
    public static function resolve_path(string $path) : string
88
    {
89 66
        if (str_starts_with($path, 'conf:')) {
90 22
            return midcom::get()->config->get('midcom_config_basedir') . '/midcom' . substr($path, 5);
91
        }
92 47
        if (str_starts_with($path, 'file:')) {
93 47
            $filename = MIDCOM_ROOT . substr($path, 5);
94 47
            if (file_exists($filename)) {
95 47
                return $filename;
96
            }
97
            // try in src
98 2
            $filename = preg_replace('/\/lib\/?$/', '/src', MIDCOM_ROOT) . substr($path, 5);
99 2
            if (file_exists($filename)) {
100 2
                return $filename;
101
            }
102
            //If we can't find the file in-tree, we look for out-of-tree components before giving up
103
            $filename = substr($path, 6);
104
            if (preg_match('|.+?/.+?/.+?/|', $filename)) {
105
                $component_name = preg_replace('|(.+?)/(.+?)/(.+?)/.+|', '$1.$2.$3', $filename);
106
                if (midcom::get()->componentloader->is_installed($component_name)) {
107
                    $filename = substr($filename, strlen($component_name));
108
                    return midcom::get()->componentloader->path_to_snippetpath($component_name) . $filename;
109
                }
110
            }
111
        }
112
        return $path;
113
    }
114
115
    /**
116
     * This helper function searches for a snippet either in the Filesystem
117
     * or in the database and returns its content or code-field, respectively.
118
     *
119
     * Prefix the snippet Path with 'file:' for retrieval of a file relative to
120
     * MIDCOM_ROOT; omit it to get the code field of a Snippet.
121
     *
122
     * Any error (files not found) will raise a MidCOM Error. If you want a more
123
     * graceful behavior, look for midcom_helper_misc::get_snippet_content_graceful
124
     */
125 167
    public static function get_snippet_content(string $path) : string
126
    {
127 167
        $data = self::get_snippet_content_graceful($path);
128 167
        if ($data === null) {
0 ignored issues
show
introduced by
The condition $data === null is always false.
Loading history...
129
            throw new midcom_error("Could not load the contents of the snippet {$path}: Snippet does not exist.");
130
        }
131 167
        return $data;
132
    }
133
134
    /**
135
     * Find MIME type image for a document
136
     *
137
     * Used in midcom.helper.imagepopup, midgard.admin.asgard and org.openpsa.documents.
138
     *
139
     * @return string    Path to the icon
140
     */
141
    public static function get_mime_icon(string $mimetype) : string
142
    {
143
        $mime_fspath = MIDCOM_STATIC_ROOT . '/stock-icons/mime';
144
        $mime_urlpath = MIDCOM_STATIC_URL . '/stock-icons/mime';
145
        $mimetype_filename = str_replace('/', '-', $mimetype);
146
        if (!is_readable($mime_fspath)) {
147
            debug_add("Couldn't read directory {$mime_fspath}", MIDCOM_LOG_WARN);
148
        }
149
150
        if ($mimetype_filename == 'application-x-zip-compressed') {
151
            $filename = "gnome-application-zip.png";
152
        } else {
153
            $filename = "gnome-{$mimetype_filename}.png";
154
        }
155
        if (is_readable("{$mime_fspath}/{$filename}")) {
156
            return "{$mime_urlpath}/{$filename}";
157
        }
158
        // Default icon if there is none for the MIME type
159
        return $mime_urlpath . '/gnome-unknown.png';
160
    }
161
162
    /**
163
     * Pretty print file sizes
164
     *
165
     * @param int $size  File size in bytes
166
     */
167 8
    public static function filesize_to_string(int $size) : string
168
    {
169 8
        if ($size >= 1048576) {
170
            // More than a meg
171
            return sprintf("%01.1f", $size / 1048576) . " MB";
172
        }
173 8
        if ($size >= 1024) {
174
            // More than a kilo
175
            return sprintf("%01.1f", $size / 1024) . " KB";
176
        }
177 8
        return $size . " Bytes";
178
    }
179
180
    /**
181
     * Returns the first instance of a given component on the site.
182
     *
183
     * @return array NAP array of the first component instance found
184
     */
185 5
    public static function find_node_by_component(string $component) : ?array
186
    {
187 5
        static $cache = [];
188
189 5
        if (!array_key_exists($component, $cache)) {
190 1
            $cache[$component] = null;
191
192 1
            $nap = new midcom_helper_nav;
193 1
            $node_id = $nap->get_root_node();
194 1
            $root_node = $nap->get_node($node_id);
195
196 1
            if ($root_node[MIDCOM_NAV_COMPONENT] == $component) {
197
                $cache[$component] = $root_node;
198
            } else {
199 1
                $qb = midcom_db_topic::new_query_builder();
200 1
                $qb->add_constraint('component', '=', $component);
201 1
                $qb->add_constraint('name', '<>', '');
202 1
                $qb->add_constraint('up', 'INTREE', $node_id);
203 1
                $qb->set_limit(1);
204 1
                $topics = $qb->execute();
205
206 1
                if (count($topics) === 1) {
207
                    $cache[$component] = $nap->get_node($topics[0]->id);
208
                }
209
            }
210
        }
211
212 5
        return $cache[$component];
213
    }
214
}
215