source: extensions/netinstall/trunk/lib.files.txt @ 3291

Last change on this file since 3291 was 2869, checked in by patdenice, 15 years ago
  • add multi-language support to auto-install script.
  • Add roma theme to piwigo-loader.
File size: 10.2 KB
Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3# This file is part of Clearbricks.
4# Copyright (c) 2006 Olivier Meunier and contributors. All rights
5# reserved.
6#
7# Clearbricks is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# Clearbricks is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Clearbricks; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20#
21# ***** END LICENSE BLOCK *****
22
23class files
24{
25        # Default
26        public static $dir_mode = null;
27       
28        # Supported MIME types
29        public static $mimeType = array(
30                        'odt'   => 'application/vnd.oasis.opendocument.text',
31                        'odp'   => 'application/vnd.oasis.opendocument.presentation',
32                        'ods'   => 'application/vnd.oasis.opendocument.spreadsheet',
33                       
34                        'sxw'   => 'application/vnd.sun.xml.writer',
35                        'sxc'   => 'application/vnd.sun.xml.calc',
36                        'sxi'   => 'application/vnd.sun.xml.impress',
37                       
38                        'ppt'   => 'application/mspowerpoint',
39                        'doc'   => 'application/msword',
40                        'xls'   => 'application/msexcel',                       
41                        'rtf'   => 'application/rtf',
42                       
43                        'pdf'   => 'application/pdf',
44                        'ps'            => 'application/postscript',
45                        'ai'            => 'application/postscript',
46                        'eps'   => 'application/postscript',
47                       
48                        'bin'   => 'application/octet-stream',
49                        'exe'   => 'application/octet-stream',
50                       
51                        'deb'   => 'application/x-debian-package',
52                        'gz'            => 'application/x-gzip',
53                        'jar'   => 'application/x-java-archive',
54                        'rar'   => 'application/rar',
55                        'rpm'   => 'application/x-redhat-package-manager',
56                        'tar'   => 'application/x-tar',
57                        'tgz'   => 'application/x-gtar',
58                        'zip'   => 'application/zip',
59                       
60                        'aiff'  => 'audio/x-aiff',
61                        'ua'            => 'audio/basic',
62                        'mp3'   => 'audio/mpeg3',
63                        'mid'   => 'audio/x-midi',
64                        'midi'  => 'audio/x-midi',
65                        'ogg'   => 'application/ogg',
66                        'wav'   => 'audio/x-wav',
67                       
68                        'swf'   => 'application/x-shockwave-flash',
69                        'swfl'  => 'application/x-shockwave-flash',
70                       
71                        'bmp'   => 'image/bmp',
72                        'gif'   => 'image/gif',
73                        'jpeg'  => 'image/jpeg',
74                        'jpg'   => 'image/jpeg',
75                        'jpe'   => 'image/jpeg',
76                        'png'   => 'image/png',
77                        'tiff'  => 'image/tiff',
78                        'tif'   => 'image/tiff',
79                        'xbm'   => 'image/x-xbitmap',
80                       
81                        'css'   => 'text/css',
82                        'js'            => 'text/javascript',
83                        'html'  => 'text/html',
84                        'htm'   => 'text/html',
85                        'txt'   => 'text/plain',
86                        'rtf'   => 'text/richtext',
87                        'rtx'   => 'text/richtext',
88                       
89                        'mpg'   => 'video/mpeg',
90                        'mpeg'  => 'video/mpeg',
91                        'mpe'   => 'video/mpeg',
92                        'viv'   => 'video/vnd.vivo',
93                        'vivo'  => 'video/vnd.vivo',
94                        'qt'            => 'video/quicktime',
95                        'mov'   => 'video/quicktime',
96                        'm4v'   => 'video/x-m4v',
97                        'flv'   => 'video/x-flv',
98                        'avi'   => 'video/x-msvideo'
99                );
100       
101        public static function scandir($d,$order=0)
102        {
103                $res = array();
104                $dh = @opendir($d);
105               
106                if ($dh === false) {
107                        throw new Exception(__('Unable to open directory.'));
108                }
109               
110                while (($f = readdir($dh)) !== false) {
111                        $res[] = $f;
112                }
113                closedir($dh);
114               
115                sort($res);
116                if ($order == 1) {
117                        rsort($res);
118                }
119               
120                return $res;
121        }
122       
123        public static function getExtension($f)
124        {
125                $f = explode('.',basename($f));
126               
127                if (count($f) <= 1) { return ''; }
128               
129                return strtolower($f[count($f)-1]);
130        }
131       
132        public static function getMimeType($f)
133        {
134                $ext = self::getExtension($f);
135                $types = self::mimeTypes();
136               
137                if (isset($types[$ext])) {
138                        return $types[$ext];
139                } else {
140                        return 'text/plain';
141                }
142        }
143       
144        public static function mimeTypes()
145        {
146                return self::$mimeType;
147        }
148       
149        public static function registerMimeTypes($tab)
150        {
151                self::$mimeType = array_merge(self::$mimeType, $tab);
152        }
153       
154        public static function isDeletable($f)
155        {
156                if (is_file($f)) {
157                        return is_writable(dirname($f));
158                } elseif (is_dir($f)) {
159                        return (is_writable(dirname($f)) && count(files::scandir($f)) <= 2);
160                }
161        }
162       
163        # Recusive remove (rm -rf)
164        public static function deltree($dir)
165        {
166                $current_dir = opendir($dir);
167                while($entryname = readdir($current_dir))
168                {
169                        if (is_dir($dir.'/'.$entryname) and ($entryname != '.' and $entryname!='..'))
170                        {
171                                if (!files::deltree($dir.'/'.$entryname)) {
172                                        return false;
173                                }
174                        }
175                        elseif ($entryname != '.' and $entryname!='..')
176                        {
177                                if (!@unlink($dir.'/'.$entryname)) {
178                                        return false;
179                                }
180                        }
181                }
182                closedir($current_dir);
183                return @rmdir($dir);
184        }
185       
186        public static function touch($f)
187        {
188                if (is_writable($f)) {
189                        if (function_exists('touch')) {
190                                @touch($f);
191                        } else {
192                                # Very bad hack
193                                @file_put_contents($f,file_get_contents($f));
194                        }
195                }
196        }
197       
198        public static function makeDir($f,$r=false)
199        {
200                if (empty($f)) {
201                        return;
202                }
203               
204                if (DIRECTORY_SEPARATOR == '\\') {
205                        $f = str_replace('/','\\',$f);
206                }
207               
208                if (is_dir($f)) {
209                        return;
210                }
211               
212                if ($r)
213                {
214                        $dir = path::real($f,false);
215                        $dirs = array();
216                       
217                        while (!is_dir($dir)) {
218                                array_unshift($dirs,basename($dir));
219                                $dir = dirname($dir);
220                        }
221                       
222                        foreach ($dirs as $d)
223                        {
224                                $dir .= DIRECTORY_SEPARATOR.$d;
225                                if ($d != '' && !is_dir($dir)) {
226                                        self::makeDir($dir);
227                                }
228                        }
229                }
230                else
231                {
232                        if (@mkdir($f) === false) {
233                                throw new Exception(__('Unable to create directory.'));
234                        }
235                        self::inheritChmod($f);
236                }
237        }
238       
239        public static function inheritChmod($file)
240        {
241                if (!function_exists('fileperms') || !function_exists('chmod')) {
242                        return false;
243                }
244               
245                if (self::$dir_mode != null) {
246                        return @chmod($file,self::$dir_mode);
247                } else {
248                        return @chmod($file,fileperms(dirname($file)));
249                }
250        }
251       
252        public static function putContent($f, $f_content)
253        {
254                if (file_exists($f) && !is_writable($f)) {     
255                        throw new Exception(__('File is not writable.'));
256                }
257               
258                $fp = @fopen($f, 'w');
259               
260                if ($fp === false) {
261                        throw new Exception(__('Unable to open file.'));
262                }
263               
264                fwrite($fp,$f_content,strlen($f_content));
265                fclose($fp);
266                return true;
267        }
268       
269        public static function size($size)
270        {
271                $kb = 1024;
272                $mb = 1024 * $kb;
273                $gb = 1024 * $mb;
274                $tb = 1024 * $gb;
275               
276                if($size < $kb) {
277                        return $size." B";
278                }
279                else if($size < $mb) {
280                        return round($size/$kb,2)." KB";
281                }
282                else if($size < $gb) {
283                        return round($size/$mb,2)." MB";
284                }
285                else if($size < $tb) {
286                        return round($size/$gb,2)." GB";
287                }
288                else {
289                        return round($size/$tb,2)." TB";
290                }
291        }
292       
293        public static function str2bytes($v)
294        {
295                $v = trim($v);
296                $last = strtolower(substr($v,-1,1));
297               
298                switch($last)
299                {
300                        case 'g':
301                                $v *= 1024;
302                        case 'm':
303                                $v *= 1024;
304                        case 'k':
305                                $v *= 1024;
306                }
307               
308                return $v;
309        }
310       
311        public static function uploadStatus($file)
312        {
313                if (!isset($file['error'])) {
314                        throw new Exception(__('Not an uploaded file.'));
315                }
316               
317                switch ($file['error']) {
318                        case UPLOAD_ERR_OK:
319                                return true;
320                        case UPLOAD_ERR_INI_SIZE:
321                        case UPLOAD_ERR_FORM_SIZE:
322                                throw new Exception(__('The uploaded file exceeds the maximum file size allowed.'));
323                                return false;
324                        case UPLOAD_ERR_PARTIAL:
325                                throw new Exception(__('The uploaded file was only partially uploaded.'));
326                                return false;
327                        case UPLOAD_ERR_NO_FILE:
328                                throw new Exception(__('No file was uploaded.'));
329                                return false;
330                        case UPLOAD_ERR_NO_TMP_DIR:
331                                throw new Exception(__('Missing a temporary folder.'));
332                                return false;
333                        case UPLOAD_ERR_CANT_WRITE:
334                                throw new Exception(__('Failed to write file to disk.'));
335                                return false;
336                        default:
337                                return true;
338                }
339        }
340       
341        # Packages generation methods
342        #
343        public static function getDirList($dirName, &$contents = null)
344        {
345                if (!$contents) {
346                        $contents = array('dirs'=> array(),'files' => array());
347                }                       
348               
349                $exclude_list=array('.','..','.svn');
350               
351                if (empty($res)) {
352                        $res = array();
353                }
354               
355                $dirName = preg_replace('|/$|','',$dirName);
356               
357                if (!is_dir($dirName)) {
358                        throw new Exception(sprintf(__('%s is not a directory.'),$dirName));
359                }
360               
361                $contents['dirs'][] = $dirName;
362               
363                $d = @dir($dirName);
364               
365                if ($d === false) {
366                        throw new Exception(__('Unable to open directory.'));
367                }
368               
369                while($entry = $d->read())
370                {
371                        if (!in_array($entry,$exclude_list))
372                        {
373                                if (is_dir($dirName.'/'.$entry))
374                                {
375                                        files::getDirList($dirName.'/'.$entry, $contents);
376                                }
377                                else
378                                {
379                                        $contents['files'][] = $dirName.'/'.$entry;
380                                }
381                        }
382                }
383                $d->close();
384               
385                return $contents;
386        }
387       
388        public static function tidyFileName($n)
389        {
390                $n = text::deaccent($n);
391                $n = preg_replace('/^[.]/u','',$n);
392                return preg_replace('/[^A-Za-z0-9._-]/u','_',$n);
393        }
394}
395
396
397class path
398{
399        public static function real($p,$strict=true)
400        {
401                $os = (DIRECTORY_SEPARATOR == '\\') ? 'win' : 'nix';
402               
403                # Absolute path?
404                if ($os == 'win') {
405                        $_abs = preg_match('/^\w+:/',$p);
406                } else {
407                        $_abs = substr($p,0,1) == '/';
408                }
409               
410                # Standard path form
411                if ($os == 'win') {
412                        $p = str_replace('\\','/',$p);
413                }
414               
415                # Adding root if !$_abs
416                if (!$_abs) {
417                        $p = dirname($_SERVER['SCRIPT_FILENAME']).'/'.$p;
418                }
419               
420                # Clean up
421                $p = preg_replace('|/+|','/',$p);
422               
423                if (strlen($p) > 1) {
424                        $p = preg_replace('|/$|','',$p);
425                }
426               
427                $_start = '';
428                if ($os == 'win') {
429                        list($_start,$p) = explode(':',$p);
430                        $_start .= ':/';
431                } else {
432                        $_start = '/';
433                }
434                $p = substr($p,1);
435               
436                # Go through
437                $P = explode('/',$p);
438                $res = array();
439               
440                for ($i=0;$i<count($P);$i++)
441                {
442                        if ($P[$i] == '.') {
443                                continue;
444                        }
445                       
446                        if ($P[$i] == '..') {
447                                if (count($res) > 0) {
448                                        array_pop($res);
449                                }
450                        } else {
451                                array_push($res,$P[$i]);
452                        }
453                }
454               
455                $p = $_start.implode('/',$res);
456               
457                if ($strict && !@file_exists($p)) {
458                        return false;
459                }
460               
461                return $p;
462        }
463       
464        public static function clean($p)
465        {
466                $p = str_replace('..','',$p);
467                $p = preg_replace('|/{2,}|','/',$p);
468                $p = preg_replace('|/$|','',$p);
469               
470                return $p;
471        }
472       
473        public static function info($f)
474        {
475                $p = pathinfo($f);
476                $res = array();
477               
478                $res['dirname'] = $p['dirname'];
479                $res['basename'] = $p['basename'];
480                $res['extension'] = isset($p['extension']) ? $p['extension'] : '';
481                $res['base'] = preg_replace('/\.'.preg_quote($res['extension'],'/').'$/','',$res['basename']);
482               
483                return $res;
484        }
485       
486        public static function fullFromRoot($p,$root)
487        {
488                if (substr($p,0,1) == '/') {
489                        return $p;
490                }
491               
492                return $root.'/'.$p;
493        }
494}
495?>
Note: See TracBrowser for help on using the repository browser.