1<?php
2
3namespace dokuwiki\plugin\gallery\classes;
4
5class NamespaceGallery extends AbstractGallery
6{
7    /** @inheritdoc */
8    public function __construct($ns, $options)
9    {
10        parent::__construct($ns, $options);
11        $this->searchNamespace($ns, $options->recursive, $options->filter);
12    }
13
14    /**
15     * Find the images
16     *
17     * @param string $ns
18     * @param bool $recursive search recursively?
19     * @param string $filter regular expresion to filter image IDs against (without namespace)
20     * @throws \Exception
21     */
22    protected function searchNamespace($ns, $recursive, $filter)
23    {
24        global $conf;
25
26        if (media_exists($ns) && !is_dir(mediaFN($ns))) {
27            // this is a single file, not a namespace
28            if ($this->hasImageExtension($ns)) {
29                $this->images[] = new Image($ns);
30            }
31        } else {
32            search(
33                $this->images,
34                $conf['mediadir'],
35                [$this, 'searchCallback'],
36                [
37                    'depth' => $recursive ? 0 : 1,
38                    'filter' => $filter
39                ],
40                utf8_encodeFN(str_replace(':', '/', $ns))
41            );
42        }
43    }
44
45    /**
46     * Callback for search() to find images
47     */
48    public function searchCallback(&$data, $base, $file, $type, $lvl, $opts)
49    {
50        if ($type == 'd') {
51            if (empty($opts['depth'])) return true; // recurse forever
52            $depth = substr_count($file, '/'); // we can't use level because we start deeper
53            if ($depth >= $opts['depth']) return false; // depth reached
54            return true;
55        }
56
57        $id = pathID($file, true);
58
59        // skip non-valid files
60        if ($id != cleanID($id)) return false;
61        //check ACL for namespace (we have no ACL for mediafiles)
62        if (auth_quickaclcheck(getNS($id) . ':*') < AUTH_READ) return false;
63        // skip non-images
64        if (!$this->hasImageExtension($file)) return false;
65        // skip filtered images
66        if ($opts['filter'] && !preg_match($opts['filter'], noNS($id))) return false;
67
68        // still here, add to result
69        $data[] = new Image($id);
70        return false;
71    }
72}
73