source: branches/MootoolsFileManager-Update/plugins/MootoolsFileManager/mootools-filemanager/Assets/Connector/FMgr4Alias.php @ 1302

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

Updates to http://www.github.com/sleemanj/mootools-filemanager from GerHoblett?

http://www.github.com/GerHoblett/

Changes to said updates by gogo (sleemanj @ github)

Modifications to MootoolsFileManager? to work with those updates, some courtesy of GerHoblett?, some sleemanj

GerHoblett? provided a large diff which accomplished the goal in a quite different way. It has merit, however I have opted for a less-affecting path in so far as Xinha's "way" is concerned, namely, not splitting the config for a single plugin into several calls to backend config passing functions which seemed a little cumbersome.

Instead I take the option of using POST to send backend data around, at the minor expense of an extra round trip when displaying thumbnails (for each one). This could be reduced by checking for thumbnail existence and returning the thumbnail name directly in "onView" rather than the backend request to generate said thumbnail.

Still to do, is to make the preview pane thumbnail also work.


File size: 5.8 KB
Line 
1<?php
2/*
3 * Script: FMgr4Alias.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 & FMgr4Alias 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_once(str_replace('\\', '/', 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                                $p_uri = parent::getParentDir($uri);
74                                $a_name = pathinfo($uri, PATHINFO_BASENAME);
75
76                                // as scandir works with filesystem paths, convert this URI path to a filesystem path:
77                                $p_dir = $this->url_path2file_path($p_uri);
78                                $p_dir = self::enforceTrailingSlash($p_dir);
79
80                                if (!isset($scandir_lookup_arr[$p_dir]))
81                                {
82                                        $scandir_lookup_arr[$p_dir] = array();
83                                }
84                                $scandir_lookup_arr[$p_dir][] = /* 'alias' => */ $a_name;
85                        }
86
87                        $this->scandir_alias_lu_arr = $scandir_lookup_arr;
88                }
89        }
90
91        /**
92         * @return array the FileManager options and settings.
93         */
94        public function getSettings()
95        {
96                return array_merge(array(
97                                'scandir_alias_lu_arr' => $this->scandir_alias_lu_arr
98                ), parent::getSettings());
99        }
100
101
102
103        /**
104         * An augmented scandir() whih 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 = false)
108        {
109                $dir = self::enforceTrailingSlash($dir);
110
111                // collect the real items first:
112                $coll = parent::scandir($dir, $filemask, $see_thumbnail_dir);
113                if ($coll === false)
114                        return $coll;
115
116
117                // make sure we keep the guarantee that the '..' entry, when present, is the very last one, intact:
118                $doubledot = array_pop($coll);
119                if ($doubledot !== null && $doubledot !== '..')
120                {
121                        $coll[] = $doubledot;
122                        $doubledot = null;
123                }
124
125
126                // we must check against thumbnail path again, as it MAY be an alias, itself!
127                $tndir = null;
128                if (!$see_thumbnail_dir)
129                {
130                        $tn_uri = $this->options['thumbnailPath'];
131                        $tnpath = $this->url_path2file_path($tn_uri);
132                        //if (FileManagerUtility::startswith($dir, $tnpath))
133                        //  return false;
134
135                        $tnparent = self::getParentDir($tnpath);
136                        $just_below_thumbnail_dir = FileManagerUtility::startswith($dir, $tnparent);
137
138                        if ($just_below_thumbnail_dir)
139                        {
140                                $tndir = basename(substr($tn_uri, 0, -1));
141                        }
142                }
143
144
145                // now see if we need to add any aliases as elements:
146                if (isset($this->scandir_alias_lu_arr) && !empty($this->scandir_alias_lu_arr[$dir]))
147                {
148                        foreach($this->scandir_alias_lu_arr[$dir] as $a_elem)
149                        {
150                                if (!in_array($a_elem, $coll, true) && $tndir !== $a_elem)
151                                        $coll[] = $a_elem;
152                        }
153                }
154
155                // make sure we keep the guarantee that the '..' entry, when present, is the very last one, intact:
156                if ($doubledot !== null)
157                {
158                        $coll[] = $doubledot;
159                }
160
161                return $coll;
162        }
163
164
165
166
167        /**
168         * Return the filesystem absolute path for the relative or absolute URI path.
169         *
170         * Takes the ['Aliases'] mapping array into account; it is processed from top to bottom a la mod_alias.
171         *
172         * Note: as it uses normalize(), any illegal path will throw an FileManagerException
173         *
174         * Returns a fully normalized filesystem absolute path.
175         */
176        public function url_path2file_path($url_path)
177        {
178                $url_path = $this->rel2abs_url_path($url_path);
179
180                $replaced_some = false;
181                if (is_array($this->options['Aliases']))
182                {
183                        $alias_arr = $this->options['Aliases'];
184
185                        foreach($alias_arr as $a_url => $a_path)
186                        {
187                                // if the uri path matches us (or at least our start), then apply the mapping.
188                                // Make sure to only match entire path elements:
189                                if (FileManagerUtility::startsWith($url_path . '/', $a_url . '/'))
190                                {
191                                        $url_path = $a_path . substr($url_path, strlen($a_url));
192                                        $replaced_some = true;
193                                }
194                        }
195                }
196
197                if (!$replaced_some)
198                {
199                        $url_path = parent::url_path2file_path($url_path);
200                }
201
202                return $url_path;
203        }
204}
205
Note: See TracBrowser for help on using the repository browser.