fluxcms /inc/popoon/components/generators/directory.php

Language PHP Lines 254
MD5 Hash 3c96abf790b9c87d210b4269ad2c6308
Repository https://github.com/chregu/fluxcms.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
// +----------------------------------------------------------------------+
// | popoon                                                               |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001-2006 Bitflux GmbH                                 |
// | Copyright (c) 2003 Mike Hommey                                       |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Mike Hommey <mh@glandium.org>                                |
// |         Christian Stocker <chregu@bitflux.ch>                        |
// |         Iván Montes <imontes@imaginocreativa.com>                    |
// +----------------------------------------------------------------------+
//
// $Id$

include_once('popoon/components/generator.php');

/**
* This class returns xml from directory listing
*
* The "dateFormat" parameter should be a suitable format string for strftime()
*
* @author   Mike Hommey <mh@glandium.org>
* @version  $Id$
* @package  popoon
* @note     Reversed sorting using 'directory' is somewhat broken :(
*           include and exclude patterns are a bit limited in my opinion, although I'm not
*           sure how they're handled in cocoon.
*           The only parameter missing from the cocoon implementation is the 'root' one.
*/
class popoon_components_generators_directory extends popoon_components_generator {
    protected $mimeType;
    protected $ns = 'http://apache.org/cocoon/directory/2.0';
    protected $strDateFormat;  // strftime format string
    protected $sortBy;
    protected $sortDir = false;
    protected $sortOrder;
    protected $includePattern = false;
    protected $excludePattern = false;
    
    /**
    * Constructor, does nothing at the moment
    */
    function __construct (&$sitemap) {
        parent::__construct($sitemap);
    }
    
    /**
    * generates an xml-DomDocument out of the xml-file
    *
    * @access public
    * @returns object DomDocument XML-Document
    */
    function DomStart(&$xml) {
        
        $this->mimeType = $this->getParameterDefault('mimeType');
        if ($this->mimeType && !function_exists("mime_content_type")) {
            $this->mimeType = false;
        }
        
        if (! $this->strDateFormat = $this->getParameterDefault('dateFormat')) {
            $this->strDateFormat = '%c';     //use current locale default
        }
        
        $this->includePattern = $this->getParameterDefault('include');
        $this->excludePattern = $this->getParameterDefault('exclude');
        
        //setup some flags to handle the sorting
        $this->sortBy = $this->getParameterDefault('sort');
        if ($this->sortBy === 'date') $this->sortBy = 'lastModified';
        if ($this->sortBy === 'directory') {
            $this->sortBy = 'name';
            $this->sortDir = true;
        }
        $this->sortOrder = (strtolower($this->getParameterDefault('reverse')) === 'true')?-1:1;
        
        
        $src = $this->getAttrib('src');
        $xml = new DOMDocument('1.0');
        $root = $xml->createElementNs($this->ns,'directory');
        $xml->appendChild($root);
        
        if (! $depth = $this->getParameterDefault('depth')) {
            $depth = 1;
        }
        
        if ($this->read_directory($xml,$root,$src, $depth)) {
            $stat = stat($this->sitemap->base_dir.'/'.$src);
            $root->setAttribute('requested','true');
            $root->setAttribute('size',$stat['size']);
            $root->setAttribute('lastModified', $stat['mtime']);
            $root->setAttribute('date', $this->formatDate($stat['mtime']));
            if ($this->sortBy) $root->setAttribute('sort', $this->getParameterDefault('sort'));
            $root->setAttribute('reverse', ($this->sortOrder==1)?'false':'true');
        } else {
            $xml = NULL;
        }
        
        return True;
    }
    
    function read_directory($dom, $parent, $directory, $depth = 1) {
        if (is_dir($directory) && ($dh = @opendir($directory))) {
            while (($file = readdir($dh)) !== false) {
                if (($file == ".") || ($file == "..")) continue;
                
                //check include and exclude patterns
                if ($this->includePattern && !preg_match($this->includePattern, $directory.'/'.$file)) continue;
                if ($this->excludePattern && preg_match($this->excludePattern, $directory.'/'.$file)) continue;
                
                $path = $directory.'/'.$file;
                while ($path && is_link($path)) { //get the real file
                    $path = readlink($path);
                }
                
                if (is_file($path)) {
                    $node = $dom->createElementNs($this->ns, 'file');
                } else if (is_dir($path)) {
                    $node = $dom->createElementNs($this->ns, 'directory');
                } else {
                    continue; //just in case :)
                }
                
                $node->setAttribute('name', $file);
                
                $info = $this->getInfo($path);
                foreach ($info as $k => $v) {
                    $node->setAttribute($k, $v);
                }
                
                //performs the sorting
                $done = false;
                if ($this->sortBy) {
                    foreach ($parent->childNodes as $child) {
                        if (
                        ($this->sortDir && $child->tagName == 'file' && $node->tagName == 'directory') ||
                        (strcmp($child->getAttribute($this->sortBy), $node->getAttribute($this->sortBy)) == $this->sortOrder)
                        ) {
                            $node = $parent->insertBefore($node, $child);
                            $done = true;
                            break;
                        }
                    }
                }
                if (! $done) $parent->appendChild($node);
                
                if (is_dir($path) and ($depth > 1)) {
                    $this->read_directory($dom, $node, $path, $depth - 1);
                }
            } //while
            
            closedir($dh);
            return true;
        } else {
            return false;
        }
    }
    
    function formatDate($timestamp) {
        if ($timestamp) return strftime($this->strDateFormat, $timestamp);
    }
    
    /**
    * gets information about a directory entry
    * Tip: Extend this method to add custom metadata to files like MP3, images ...
    *
    * @access public
    * @returns array
    */
    function getInfo($entry) {
        $info = array();
        
        $stat = stat($entry);
        
        //try to fetch details
        if ($this->mimeType) {
            $mimeType = mime_content_type($directory.'/'.$file);
            if (strpos($mimeType, "image") === 0) {
                $size = getimagesize($directory.'/'.$file);
                $info['imageWidth'] = $size[0];
                $info['imageHeight'] = $size[1];
            }
            $info['mimeType'] = $mimeType;
        }
        
        $info['size'] = $stat['size'];
        $info['lastModified'] = $stat['mtime'];
        $info['date'] = $this->formatDate($stat['mtime']);
        
        return $info;
    }
    
    /* CACHING STUFF */
    
    /**
    * Generate cacheKey
    *
    * Calls the method inherited from 'Component'
    *
    * @param   array  attributes
    * @param   int    last cacheKey
    * @see     generateKeyDefault()
    */
    function generateKey($attribs, $keyBefore){
        return($this->generateKeyDefault($attribs, $keyBefore));
    }
    
    /** Generate validityObject
    *
    * This is common to all "readers", you'll find the same code there.
    * I'm thinking about making a method in the class component named generateValidityFile() or alike
    * instead of having the same code everywhere..
    *
    * @author Hannes Gassert <hannes.gassert@unifr.ch>
    * @see  checkvalidity()
    * @return  array  $validityObject contains the components attributes plus file modification time and time of last access.
    */
    function generateValidity(){
        $validityObject = $this->attribs;
        $src = $this->getAttrib('src');
        $validityObject['filemtime'] = filemtime($src);
        $validityObject['fileatime'] = fileatime($src);
        return($validityObject);
    }
    
    /**
    * Check validity of a validityObject from cache
    *
    * This implements only the most simple form: If there's no fresher version, take that from cache.
    * I guess we'll need some more refined criteria..
    *
    * @return  bool  true if the validityObject indicates that the cached version can be used, false otherwise.
    * @param   object  validityObject
    */
    function checkValidity($validityObject){
        return(isset($validityObject['src'])       &&
        isset($validityObject['filemtime']) &&
        file_exists($validityObject['src']) &&
        ($validityObject['filemtime'] == filemtime($validityObject['src'])));
    }
    
}


?>
Back to Top