source: branches/MootoolsFileManager-Update/plugins/MootoolsFileManager/mootools-filemanager/Assets/Connector/FileManagerWithAliasSupport.php @ 1309

Last change on this file since 1309 was 1309, checked in by gogo, 8 years ago

Part 2: Add new mootools-filemanager

File size: 7.0 KB
Line 
1<?php
2/*
3 * Script: FileManagerWithAliasSupport.php
4 *   MooTools FileManager - Backend for the FileManager Script (with Alias path mapping support)
5 *
6 * Authors:
7 *  - Christoph Pojer (http://cpojer.net) (author)
8 *  - James Ehly (http://www.devtrench.com)
9 *  - Fabian Vogelsteller (http://frozeman.de)
10 *  - Ger Hobbelt (http://hebbut.net)
11 *
12 * License:
13 *   MIT-style license.
14 *
15 * Copyright:
16 *   FileManager Copyright (c) 2009-2011 [Christoph Pojer](http://cpojer.net)
17 *   Backend: FileManager & FileManagerWithAliasSupport Copyright (c) 2011 [Ger Hobbelt](http://hobbelt.com)
18 *
19 * Dependencies:
20 *   - Tooling.php
21 *   - Image.class.php
22 *   - getId3 Library
23 *   - FileManager.php
24 */
25
26require(strtr(dirname(__FILE__), '\\', '/') . '/FileManager.php');
27
28
29/**
30 * Derived class for FileManager which is capable of handling Aliases as served through Apache's mod_alias or
31 * mod_vhost_alias, PROVIDED you have set up the Alias translation table in the constructor: you must pass this table in the
32 * $options array as a mapping array in the constructor.
33 *
34 * Options:
35 *   -(all of the options of the FileManager class)
36 *   -Aliases: (associative array of strings, where the 'key' URI path is to be transformed to the 'value' physical filesystem path.
37 *
38 * See Demos/FM-common.php::mkNewFileManager() for an example of an 'Aliases' path mapping set.
39 */
40class FileManagerWithAliasSupport extends FileManager
41{
42        protected $scandir_alias_lu_arr;
43
44        public function __construct($options)
45        {
46                $this->scandir_alias_lu_arr = null;
47
48                $options = array_merge(array(
49                        'Aliases' => null             // default is an empty Alias list.
50                ), (is_array($options) ? $options : array()));
51
52                parent::__construct($options);
53
54                /*
55                 * Now process the Aliases array:
56                 * it works as-is for transforming URI to FILE path, but we need
57                 * to munch the list for scandir() to have fast access at the same info:
58                 *
59                 * here the goal is for scandir() to show the aliases as (fake) directory
60                 * entries, hence we need to collect the aliases per parent directory:
61                 */
62                if (is_array($this->options['Aliases']))
63                {
64                        $alias_arr = $this->options['Aliases'];
65
66                        // collect the set of aliases per parent directory: we need a fully set up options['directory'] for this now
67                        $scandir_lookup_arr = array();
68
69                        // NOTE: we can use any of the url2file_path methods here as those only use the raw [Aliases] array
70
71                        foreach($alias_arr as $uri => $file)
72                        {
73                                $isdir = !is_file($file);
74
75                                $p_uri = parent::getParentDir($uri);
76                                $a_name = basename($uri);
77
78                                // as scandir works with filesystem paths, convert this URI path to a filesystem path:
79                                $p_dir = $this->url_path2file_path($p_uri);
80                                $p_dir = self::enforceTrailingSlash($p_dir);
81
82                                if (!isset($scandir_lookup_arr[$p_dir]))
83                                {
84                                        $scandir_lookup_arr[$p_dir] = array(array(), array());
85                                }
86                                $scandir_lookup_arr[$p_dir][!$isdir][] = /* 'alias' => */ $a_name;
87                        }
88
89                        $this->scandir_alias_lu_arr = $scandir_lookup_arr;
90                }
91        }
92
93        /**
94         * @return array the FileManager options and settings.
95         */
96        public function getSettings()
97        {
98                return array_merge(array(
99                        'scandir_alias_lu_arr' => $this->scandir_alias_lu_arr
100                ), parent::getSettings());
101        }
102
103        /**
104         * An augmented scandir() which will ensure any Aliases are included in the relevant
105         * directory scans; this makes the Aliases behave very similarly to actual directories.
106         */
107        public function scandir($dir, $filemask, $see_thumbnail_dir, $glob_flags_or, $glob_flags_and)
108        {
109                $dir = self::enforceTrailingSlash($dir);
110
111                // collect the real items first:
112                $coll = parent::scandir($dir, $filemask, $see_thumbnail_dir, $glob_flags_or, $glob_flags_and);
113                if ($coll === false)
114                        return $coll;
115
116                $flags = GLOB_NODOTS | GLOB_NOHIDDEN | GLOB_NOSORT;
117                $flags &= $glob_flags_and;
118                $flags |= $glob_flags_or;
119
120                // make sure we keep the guarantee that the '..' entry, when present, is the very last one, intact:
121                $doubledot = array_pop($coll['dirs']);
122                if ($doubledot !== null && $doubledot !== '..')
123                {
124                        $coll['dirs'][] = $doubledot;
125                        $doubledot = null;
126                }
127
128
129                // we must check against thumbnail path again, as it MAY be an alias, itself!
130                $tndir = null;
131                if (!$see_thumbnail_dir)
132                {
133                        $tn_uri = $this->options['thumbnailPath'];
134                        $tnpath = $this->url_path2file_path($tn_uri);
135                        //if (FileManagerUtility::startswith($dir, $tnpath))
136                        //  return false;
137
138                        $tnparent = self::getParentDir($tnpath);
139                        $just_below_thumbnail_dir = FileManagerUtility::startswith($dir, $tnparent);
140
141                        if ($just_below_thumbnail_dir)
142                        {
143                                $tndir = basename(substr($tn_uri, 0, -1));
144                        }
145                }
146
147
148                // now see if we need to add any aliases as elements:
149                if (isset($this->scandir_alias_lu_arr) && !empty($this->scandir_alias_lu_arr[$dir]))
150                {
151                        $a_base = $this->scandir_alias_lu_arr[$dir];
152                        $d = $coll['dirs'];
153                        $f = $coll['files'];
154                        foreach($a_base[false] as $a_elem)
155                        {
156                                if (!in_array($a_elem, $d, true) && $tndir !== $a_elem
157                                        && (!($flags & GLOB_NOHIDDEN) || $a_elem[0] != '.') )
158                                {
159                                        //$coll['special_indir_mappings'][1][] = array_push($coll['dirs'], $a_elem) - 1;
160                                        $coll['dirs'][] = $a_elem;
161                                }
162                        }
163                        foreach($a_base[true] as $a_elem)
164                        {
165                                if (!in_array($a_elem, $f, true)
166                                        && (!($flags & GLOB_NOHIDDEN) || $a_elem[0] != '.') )
167                                {
168                                        //$coll['special_indir_mappings'][0][] = array_push($coll['files'], $a_elem) - 1;
169                                        $coll['files'][] = $a_elem;
170                                }
171                        }
172                }
173
174                // make sure we keep the guarantee that the '..' entry, when present, is the very last one, intact:
175                if ($doubledot !== null)
176                {
177                        $coll['dirs'][] = $doubledot;
178                }
179
180                return $coll;
181        }
182
183
184
185
186        /**
187         * Return the filesystem absolute path for the relative or absolute URI path.
188         *
189         * Takes the ['Aliases'] mapping array into account; it is processed from top to bottom a la mod_alias.
190         *
191         * Note: as it uses normalize(), any illegal path will throw an FileManagerException
192         *
193         * Returns a fully normalized filesystem absolute path.
194         */
195        public function url_path2file_path($url_path)
196        {
197                $url_path = $this->rel2abs_url_path($url_path);
198
199                $replaced_some = false;
200                if (is_array($this->options['Aliases']))
201                {
202                        $alias_arr = $this->options['Aliases'];
203
204                        foreach($alias_arr as $a_url => $a_path)
205                        {
206                                // if the uri path matches us (or at least our start), then apply the mapping.
207                                // Make sure to only match entire path elements:
208                                if (FileManagerUtility::startsWith($url_path . '/', $a_url . '/'))
209                                {
210                                        $url_path = $a_path . substr($url_path, strlen($a_url));
211                                        $replaced_some = true;
212                                }
213                        }
214                }
215
216                if (!$replaced_some)
217                {
218                        $url_path = parent::url_path2file_path($url_path);
219                }
220
221                return $url_path;
222        }
223
224        /**
225         * Return the filesystem absolute path for the relative URI path or absolute LEGAL URI path.
226         *
227         * Note: as it uses normalize(), any illegal path will throw an FileManagerException
228         *
229         * Returns a fully normalized filesystem absolute path.
230         */
231        public function legal_url_path2file_path($url_path)
232        {
233                $url_path = $this->rel2abs_legal_url_path($url_path);
234
235                $url_path = substr($this->options['directory'], 0, -1) . $url_path;
236
237                $path = $this->url_path2file_path($url_path);
238
239                return $path;
240        }
241}
242
Note: See TracBrowser for help on using the repository browser.