19 require_once(BASE.
"wcmf/lib/core/class.WCMFException.php");
20 require_once(BASE.
"wcmf/lib/util/class.Message.php");
21 if (file_exists(BASE.
"wcmf/lib/util/class.InifileParser.php"))
22 require_once(BASE.
"wcmf/lib/util/class.InifileParser.php");
52 function uploadFile($mediaFile, $destName, $mimeType=null, $maxSize=-1, $override=
true)
54 $this->_errorMsg =
'';
56 if (is_uploaded_file($mediaFile[
'tmp_name']))
58 if ($mimeType == null || in_array($mediaFile[
'type'], $mimeType))
60 if ($mediaFile[
'size'] <= $maxSize || $maxSize == -1)
62 if ($override ==
false && file_exists($destName))
64 $pieces = split(
'\.', basename($destName));
65 $extension = array_pop($pieces);
66 $name = join(
'.', $pieces);
67 $destName = dirname($destName).
"/".$name.uniqid(rand()).
".".$extension;
69 $result = move_uploaded_file($mediaFile[
'tmp_name'], $destName);
70 if ($result ===
false)
72 $this->_errorMsg =
Message::get(
"Failed to move %1% to %2%.", array($mediaFile[
'tmp_name'], $destName));
73 $this->_errorMsg .=
"\n";
75 chmod($destName, 0644);
76 $filename = basename($destName);
80 $this->_errorMsg =
Message::get(
"File too big: %1%. Allowed size: %2% bytes.", array($mediaFile[
'name'], $maxSize));
81 $this->_errorMsg .=
"\n";
86 $this->_errorMsg .=
Message::get(
"File '%1%' has wrong mime type: %2%. Allowed types: %3%.",
87 array($mediaFile[
'name'], $mediaFile[
'type'], join(
", ", $mimeTypes)));
88 $this->_errorMsg .=
"\n";
93 $this->_errorMsg =
Message::get(
"Possible file upload attack: filename %1%.", array($mediaFile[
'name']));
94 if (!file_exists(BASE.
"wcmf/lib/util/class.InifileParser.php"))
97 if(($maxFileSize = $parser->getValue(
'maxFileSize',
'htmlform')) !==
false)
99 $this->_errorMsg .=
Message::get(
"A possible reason is that the file size is too big (maximum allowed: %1% bytes).", array($maxFileSize));
100 $this->_errorMsg .=
"\n";
113 fputs($fp, utf8_encode($str));
123 function getFiles($directory, $pattern=
'/./', $prependDirectoryName=
false, $recursive=
false)
126 if (strrpos($directory,
'/') != strlen($directory)-1)
128 if (is_dir($directory))
131 $d = dir($directory);
133 while(
false !== ($file = $d->read()))
135 if($file !=
'.' && $file !=
'..')
137 if ($recursive && is_dir($directory.$file))
139 $files =
FileUtil::getFiles($directory.$file, $pattern, $prependDirectoryName, $recursive);
140 $result = array_merge($result, $files);
142 else if(is_file($directory.$file) && preg_match($pattern, $file))
144 $sortkey = filectime($directory.$file).
',';
145 if ($prependDirectoryName)
146 $file = $directory.$file;
148 $result[$sortkey] = $file;
155 $this->_errorMsg =
Message::get(
"The directory '%1%' does not exist.", array($directory));
157 return array_values($result);
167 function getDirectories($directory, $pattern=
'/./', $prependDirectoryName=
false, $recursive=
false)
170 if (strrpos($directory,
'/') != strlen($directory)-1)
172 if (is_dir($directory))
175 $d = dir($directory);
177 while(
false !== ($file = $d->read()))
179 if($file !=
'.' && $file !=
'..')
181 if (is_dir($directory.$file))
186 $result = array_merge($result, $dirs);
188 if(preg_match($pattern, $file))
190 if ($prependDirectoryName)
191 $file = $directory.$file;
192 array_push($result, $file);
200 $this->_errorMsg =
Message::get(
"The directory '%1%' does not exist.", array($directory));
210 if (is_file($source))
212 $perms = fileperms($source);
213 return copy($source, $dest) && chmod($dest, $perms);
215 else if (is_dir($source))
231 $dir = opendir($source);
232 while ($file = readdir($dir))
234 if ($file ==
"." || $file ==
"..")
246 $folder_list = split(
"/", $dirname);
247 $len =
sizeof($folder_list);
248 for( $i=0; $i<$len; $i++ )
250 $tmp .= $folder_list[$i] .
'/';
262 foreach ($files as $file)
265 foreach ($dirs as $dir)
277 $path1 = str_replace(
'\\',
'/', $path1);
278 $path2 = str_replace(
'\\',
'/', $path2);
281 $path1 = trim($path1,
'/');
282 $path2 = trim($path2,
'/');
283 while (substr_count($path1,
'//')) $path1 = str_replace(
'//',
'/', $path1);
284 while (substr_count($path2,
'//')) $path2 = str_replace(
'//',
'/', $path2);
287 $arr1 = explode(
'/', $path1);
288 if ($arr1 == array(
'')) $arr1 = array();
289 $arr2 = explode(
'/', $path2);
290 if ($arr2 == array(
'')) $arr2 = array();
291 $size1 = count($arr1);
292 $size2 = count($arr2);
296 for($i=0; $i<min($size1,$size2); $i++)
298 if ($arr1[$i] == $arr2[$i])
continue;
299 else $path =
'../'.$path.$arr2[$i].
'/';
302 for ($i = $size2; $i < $size1; $i++)
304 else if ($size2 > $size1)
305 for ($i = $size1; $i < $size2; $i++)
306 $path .= $arr2[$i].
'/';
318 $special_chars = array (
"#",
"$",
"%",
"^",
"&",
"*",
"!",
"~",
"‘",
"\"",
"’",
"'",
"=",
"?",
"/",
"[",
"]",
"(",
")",
"|",
"<",
">",
";",
"\\",
",",
".");
319 $name = preg_replace(
"/^[.]*/",
"",$name);
320 $name = preg_replace(
"/[.]*$/",
"",$name);
322 $lastdotpos=strrpos($name,
".");
324 $name = str_replace($special_chars,
"", $name);
325 $name = str_replace(
' ',
'_',$name);
328 if ($lastdotpos !==
false)
330 if ($lastdotpos < (strlen($name) - 1))
331 $afterdot = substr($name, $lastdotpos);
333 $extensionlen = strlen($afterdot);
335 if ($lastdotpos < (50 - $extensionlen) )
336 $beforedot = substr($name, 0, $lastdotpos);
338 $beforedot = substr($name, 0, (50 - $extensionlen));
341 $beforedot = substr($name,0,50);
344 $name = $beforedot .
"." . $afterdot;
getRelativePath($path1, $path2)
get($message, $parameters=null, $domain='', $lang='')
uploadFile($mediaFile, $destName, $mimeType=null, $maxSize=-1, $override=true)
throwEx($message, $file='', $line='')
getFiles($directory, $pattern='/./', $prependDirectoryName=false, $recursive=false)
copyRecDir($source, $dest)
getDirectories($directory, $pattern='/./', $prependDirectoryName=false, $recursive=false)
FileUtil provides basic support for file functionality like HTTP file upload.