/var/www/hkosl.com/alliancealliance/uploader/lib/helper_file.php


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
<?php

/** This file is part of KCFinder project
  *
  *      @desc File helper class
  *   @package KCFinder
  *   @version 3.12
  *    @author Pavel Tzonkov <sunhater@sunhater.com>
  * @copyright 2010-2014 KCFinder Project
  *   @license http://opensource.org/licenses/GPL-3.0 GPLv3
  *   @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  *      @link http://kcfinder.sunhater.com
  */

namespace kcfinder;

class 
file {

    static 
$MIME = array(
        
'ai'    => 'application/postscript',
        
'aif'   => 'audio/x-aiff',
        
'aifc'  => 'audio/x-aiff',
        
'aiff'  => 'audio/x-aiff',
        
'avi'   => 'video/x-msvideo',
        
'bin'   => 'application/macbinary',
        
'bmp'   => 'image/bmp',
        
'cpt'   => 'application/mac-compactpro',
        
'css'   => 'text/css',
        
'csv'   => 'text/x-comma-separated-values',
        
'dcr'   => 'application/x-director',
        
'dir'   => 'application/x-director',
        
'doc'   => 'application/msword',
        
'docx'  => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        
'dvi'   => 'application/x-dvi',
        
'dxr'   => 'application/x-director',
        
'eml'   => 'message/rfc822',
        
'eps'   => 'application/postscript',
        
'flv'   => 'video/x-flv',
        
'gif'   => 'image/gif',
        
'gtar'  => 'application/x-gtar',
        
'gz'    => 'application/x-gzip',
        
'hqx'   => 'application/mac-binhex40',
        
'htm'   => 'text/html',
        
'html'  => 'text/html',
        
'jpe'   => 'image/jpeg',
        
'jpeg'  => 'image/jpeg',
        
'jpg'   => 'image/jpeg',
        
'js'    => 'application/x-javascript',
        
'log'   => 'text/plain',
        
'mid'   => 'audio/midi',
        
'midi'  => 'audio/midi',
        
'mif'   => 'application/vnd.mif',
        
'mov'   => 'video/quicktime',
        
'movie' => 'video/x-sgi-movie',
        
'mp2'   => 'audio/mpeg',
        
'mp3'   => 'audio/mpeg',
        
'mp4'   => 'video/mpeg',
        
'mpe'   => 'video/mpeg',
        
'mpeg'  => 'video/mpeg',
        
'mpg'   => 'video/mpeg',
        
'mpga'  => 'audio/mpeg',
        
'oda'   => 'application/oda',
        
'pdf'   => 'application/pdf',
        
'php'   => 'application/x-httpd-php',
        
'php3'  => 'application/x-httpd-php',
        
'php4'  => 'application/x-httpd-php',
        
'phps'  => 'application/x-httpd-php-source',
        
'phtml' => 'application/x-httpd-php',
        
'png'   => 'image/png',
        
'ppt'   => 'application/powerpoint',
        
'ps'    => 'application/postscript',
        
'psd'   => 'application/x-photoshop',
        
'qt'    => 'video/quicktime',
        
'ra'    => 'audio/x-realaudio',
        
'ram'   => 'audio/x-pn-realaudio',
        
'rm'    => 'audio/x-pn-realaudio',
        
'rpm'   => 'audio/x-pn-realaudio-plugin',
        
'rtf'   => 'text/rtf',
        
'rtx'   => 'text/richtext',
        
'rv'    => 'video/vnd.rn-realvideo',
        
'shtml' => 'text/html',
        
'sit'   => 'application/x-stuffit',
        
'smi'   => 'application/smil',
        
'smil'  => 'application/smil',
        
'swf'   => 'application/x-shockwave-flash',
        
'tar'   => 'application/x-tar',
        
'tgz'   => 'application/x-tar',
        
'text'  => 'text/plain',
        
'tif'   => 'image/tiff',
        
'tiff'  => 'image/tiff',
        
'txt'   => 'text/plain',
        
'wav'   => 'audio/x-wav',
        
'wbxml' => 'application/wbxml',
        
'wmlc'  => 'application/wmlc',
        
'word'  => 'application/msword',
        
'xht'   => 'application/xhtml+xml',
        
'xhtml' => 'application/xhtml+xml',
        
'xl'    => 'application/excel',
        
'xls'   => 'application/excel',
        
'xlsx'  => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        
'xml'   => 'text/xml',
        
'xsl'   => 'text/xml',
        
'zip'   => 'application/x-zip'
    
);

/** Checks if the given file is really writable. The standard PHP function
  * is_writable() does not work properly on Windows servers.
  * @param string $filename
  * @return bool */

    
static function isWritable($filename) {
        
$filename path::normalize($filename);
        if (!
is_file($filename) || (false === ($fp = @fopen($filename'a+'))))
            return 
false;
        
fclose($fp);
        return 
true;
    }

/** Get the extension from filename
  * @param string $filename
  * @param bool $toLower
  * @return string */

    
static function getExtension($filename$toLower=true) {
        return 
preg_match('/^.*\.([^\.]*)$/s'$filename$patt)
            ? (
$toLower strtolower($patt[1]) : $patt[1]) : "";
    }

/** Get MIME type of the given filename. If Fileinfo PHP extension is
  * available the MIME type will be fetched by the file's content. The
  * second parameter is optional and defines the magic file path. If you
  * skip it, the default one will be loaded.
  * If Fileinfo PHP extension is not available the MIME type will be fetched
  * by filename extension regarding $MIME property. If the file extension
  * does not exist there, returned type will be application/octet-stream
  * @param string $filename
  * @param string $magic
  * @return string */

    
static function getMimeType($filename$magic=null) {
        if (
class_exists("finfo")) {
            
$finfo = new \finfo(FILEINFO_MIME$magic);
            if (
$finfo) {
                
$mime $finfo->file($filename);
                
$mime substr($mime0strrpos($mime";"));
                return 
$mime;
            }
        }
        
$ext self::getExtension($filenametrue);
        return isset(
self::$MIME[$ext]) ? self::$MIME[$ext] : "application/octet-stream";
    }

/** Get inexistant filename based on the given filename. If you skip $dir
  * parameter the directory will be fetched from $filename and returned
  * value will be full filename path. The third parameter is optional and
  * defines the template, the filename will be renamed to. Default template
  * is {name}({sufix}){ext}. Examples:
  *
  *   file::getInexistantFilename("/my/directory/myfile.txt");
  *   If myfile.txt does not exist - returns the same path to the file
  *   otherwise returns "/my/directory/myfile(1).txt"
  *
  *   file::getInexistantFilename("myfile.txt", "/my/directory");
  *   returns "myfile.txt" or "myfile(1).txt" or "myfile(2).txt" etc...
  *
  *   file::getInexistantFilename("myfile.txt", "/dir", "{name}[{sufix}]{ext}");
  *   returns "myfile.txt" or "myfile[1].txt" or "myfile[2].txt" etc...
  *
  * @param string $filename
  * @param string $dir
  * @param string $tpl
  * @return string */

    
static function getInexistantFilename($filename$dir=null$tpl=null) {
        if (
$tpl === null)  $tpl "{name}({sufix}){ext}";
        
$fullPath = ($dir === null);
        if (
$fullPath)
            
$dir path::normalize(dirname($filename));
        else {
            
$fdir dirname($filename);
            
$dir strlen($fdir)
                ? 
path::normalize("$dir/$fdir")
                : 
path::normalize($dir);
        }
        
$filename basename($filename);
        
$ext self::getExtension($filenamefalse);
        
$name strlen($ext) ? substr($filename0, -strlen($ext) - 1) : $filename;
        
$tpl str_replace('{name}'$name$tpl);
        
$tpl str_replace('{ext}', (strlen($ext) ? ".$ext""), $tpl);
        
$i 1$file "$dir/$filename";
        while (
file_exists($file))
            
$file "$dir/" str_replace('{sufix}'$i++, $tpl);

        return 
$fullPath
            
$file
            
: (strlen($fdir)
                ? 
"$fdir/" basename($file)
                : 
basename($file));
    }

/** Normalize given filename. Accented characters becomes non-accented and
  * removes any other special characters. Usable for non-unicode filesystems
  * @param $filename
  * @return string */

    
static function normalizeFilename($filename) {
        
$string htmlentities($filenameENT_QUOTES'UTF-8');
        if (
strpos($string'&') !== false)
            
$filename html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i''$1'$string), ENT_QUOTES'UTF-8');
        
$filename trim(preg_replace('~[^0-9a-z\.\- ]~i'"_"$filename));
        return 
$filename;
    }

}

?>