| [ Index ] |
|
Code source de Dotclear 2.0-beta6 |
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 23 class files 24 { 25 public static function scandir($d,$order=0) 26 { 27 $res = array(); 28 $dh = @opendir($d); 29 30 if ($dh === false) { 31 throw new Exception(__('Unable to open directory.')); 32 } 33 34 while (($f = readdir($dh)) !== false) { 35 $res[] = $f; 36 } 37 closedir($dh); 38 39 sort($res); 40 if ($order == 1) { 41 rsort($res); 42 } 43 44 return $res; 45 } 46 47 public static function getExtension($f) 48 { 49 $f = explode('.',basename($f)); 50 51 if (count($f) <= 1) { return ''; } 52 53 return strtolower($f[count($f)-1]); 54 } 55 56 public static function getMimeType($f) 57 { 58 $ext = self::getExtension($f); 59 $types = self::mimeTypes(); 60 61 if (isset($types[$ext])) { 62 return $types[$ext]; 63 } else { 64 return 'text/plain'; 65 } 66 } 67 68 public static function mimeTypes() 69 { 70 return array( 71 'odt' => 'application/vnd.oasis.opendocument.text', 72 'odp' => 'application/vnd.oasis.opendocument.presentation', 73 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 74 75 'sxw' => 'application/vnd.sun.xml.writer', 76 'sxc' => 'application/vnd.sun.xml.calc', 77 'sxi' => 'application/vnd.sun.xml.impress', 78 79 'ppt' => 'application/mspowerpoint', 80 'doc' => 'application/msword', 81 'xls' => 'application/msexcel', 82 'rtf' => 'application/rtf', 83 84 'pdf' => 'application/pdf', 85 'ps' => 'application/postscript', 86 'ai' => 'application/postscript', 87 'eps' => 'application/postscript', 88 89 'bin' => 'application/octet-stream', 90 'exe' => 'application/octet-stream', 91 92 'deb' => 'application/x-debian-package', 93 'gz' => 'application/x-gzip', 94 'jar' => 'application/x-java-archive', 95 'rar' => 'application/rar', 96 'rpm' => 'application/x-redhat-package-manager', 97 'tar' => 'application/x-tar', 98 'tgz' => 'application/x-gtar', 99 'zip' => 'application/zip', 100 101 'aiff' => 'audio/x-aiff', 102 'ua' => 'audio/basic', 103 'mp3' => 'audio/mpeg3', 104 'mid' => 'audio/x-midi', 105 'midi' => 'audio/x-midi', 106 'ogg' => 'application/ogg', 107 'wav' => 'audio/x-wav', 108 109 'swf' => 'application/x-shockwave-flash', 110 'swfl' => 'application/x-shockwave-flash', 111 112 'bmp' => 'image/bmp', 113 'gif' => 'image/gif', 114 'jpeg' => 'image/jpeg', 115 'jpg' => 'image/jpeg', 116 'jpe' => 'image/jpeg', 117 'png' => 'image/png', 118 'tiff' => 'image/tiff', 119 'tif' => 'image/tiff', 120 'xbm' => 'image/x-xbitmap', 121 122 'css' => 'text/css', 123 'js' => 'text/javascript', 124 'html' => 'text/html', 125 'htm' => 'text/html', 126 'txt' => 'text/plain', 127 'rtf' => 'text/richtext', 128 'rtx' => 'text/richtext', 129 130 'mpg' => 'video/mpeg', 131 'mpeg' => 'video/mpeg', 132 'mpe' => 'video/mpeg', 133 'viv' => 'video/vnd.vivo', 134 'vivo' => 'video/vnd.vivo', 135 'qt' => 'video/quicktime', 136 'mov' => 'video/quicktime', 137 'avi' => 'video/x-msvideo' 138 ); 139 } 140 141 public static function isDeletable($f) 142 { 143 if (is_file($f)) { 144 return is_writable(dirname($f)); 145 } elseif (is_dir($f)) { 146 return (is_writable(dirname($f)) && count(files::scandir($f)) <= 2); 147 } 148 } 149 150 # Recusive remove (rm -rf) 151 public static function deltree($dir) 152 { 153 $current_dir = opendir($dir); 154 while($entryname = readdir($current_dir)) 155 { 156 if (is_dir($dir.'/'.$entryname) and ($entryname != '.' and $entryname!='..')) 157 { 158 if (!files::deltree($dir.'/'.$entryname)) { 159 return false; 160 } 161 } 162 elseif ($entryname != '.' and $entryname!='..') 163 { 164 if (!@unlink($dir.'/'.$entryname)) { 165 return false; 166 } 167 } 168 } 169 closedir($current_dir); 170 return @rmdir($dir); 171 } 172 173 public static function touch($f) 174 { 175 if (is_writable($f)) { 176 if (function_exists('touch')) { 177 @touch($f); 178 } else { 179 # Very bad hack 180 @file_put_contents($f,file_get_contents($f)); 181 } 182 } 183 } 184 185 public static function makeDir($f,$r=false) 186 { 187 if (empty($f)) { 188 return; 189 } 190 191 if (DIRECTORY_SEPARATOR == '\\') { 192 $f = str_replace('/','\\',$f); 193 } 194 195 if (is_dir($f)) { 196 return; 197 } 198 199 if ($r) 200 { 201 $dir = path::real($f,false); 202 $dirs = array(); 203 204 while (!is_dir($dir)) { 205 array_unshift($dirs,basename($dir)); 206 $dir = dirname($dir); 207 } 208 209 foreach ($dirs as $d) 210 { 211 $dir .= DIRECTORY_SEPARATOR.$d; 212 if ($d != '' && !is_dir($dir)) { 213 self::makeDir($dir); 214 } 215 } 216 } 217 else 218 { 219 if (@mkdir($f) === false) { 220 throw new Exception(__('Unable to create directory.')); 221 } 222 chmod($f,fileperms(dirname($f))); 223 } 224 } 225 226 public static function putContent($f, $f_content) 227 { 228 if (!is_writable($f)) { 229 throw new Exception(__('File is not writable.')); 230 } 231 232 $fp = @fopen($f, 'w'); 233 234 if ($fp === false) { 235 throw new Exception(__('Unable to open file.')); 236 } 237 238 fwrite($fp,$f_content,strlen($f_content)); 239 fclose($fp); 240 return true; 241 } 242 243 public static function size($size) 244 { 245 $kb = 1024; 246 $mb = 1024 * $kb; 247 $gb = 1024 * $mb; 248 $tb = 1024 * $gb; 249 250 if($size < $kb) { 251 return $size." B"; 252 } 253 else if($size < $mb) { 254 return round($size/$kb,2)." KB"; 255 } 256 else if($size < $gb) { 257 return round($size/$mb,2)." MB"; 258 } 259 else if($size < $tb) { 260 return round($size/$gb,2)." GB"; 261 } 262 else { 263 return round($size/$tb,2)." TB"; 264 } 265 } 266 267 public static function str2bytes($v) 268 { 269 $v = trim($v); 270 $last = strtolower(substr($v,-1,1)); 271 272 switch($last) 273 { 274 case 'g': 275 $v *= 1024; 276 case 'm': 277 $v *= 1024; 278 case 'k': 279 $v *= 1024; 280 } 281 282 return $v; 283 } 284 285 public static function uploadStatus($file) 286 { 287 if (!isset($file['error'])) { 288 throw new Exception(__('Not an uploaded file.')); 289 } 290 291 switch ($file['error']) { 292 case UPLOAD_ERR_OK: 293 return true; 294 case UPLOAD_ERR_INI_SIZE: 295 case UPLOAD_ERR_FORM_SIZE: 296 throw new Exception(__('The uploaded file exceeds the maximum file size allowed.')); 297 return false; 298 case UPLOAD_ERR_PARTIAL: 299 throw new Exception(__('The uploaded file was only partially uploaded.')); 300 return false; 301 case UPLOAD_ERR_NO_FILE: 302 throw new Exception(__('No file was uploaded.')); 303 return false; 304 case UPLOAD_ERR_NO_TMP_DIR: 305 throw new Exception(__('Missing a temporary folder.')); 306 return false; 307 case UPLOAD_ERR_CANT_WRITE: 308 throw new Exception(__('Failed to write file to disk.')); 309 return false; 310 default: 311 return true; 312 } 313 } 314 315 # Packages generation methods 316 # 317 public static function getDirList($dirName, &$contents = null) 318 { 319 if (!$contents) { 320 $contents = array('dirs'=> array(),'files' => array()); 321 } 322 323 $exclude_list=array('.','..','.svn'); 324 325 if (empty($res)) { 326 $res = array(); 327 } 328 329 $dirName = preg_replace('|/$|','',$dirName); 330 331 if (!is_dir($dirName)) { 332 throw new Exception(sprintf(__('%s is not a directory.'),$dirName)); 333 } 334 335 $contents['dirs'][] = $dirName; 336 337 $d = @dir($dirName); 338 339 if ($d === false) { 340 throw new Exception(__('Unable to open directory.')); 341 } 342 343 while($entry = $d->read()) 344 { 345 if (!in_array($entry,$exclude_list)) 346 { 347 if (is_dir($dirName.'/'.$entry)) 348 { 349 files::getDirList($dirName.'/'.$entry, $contents); 350 } 351 else 352 { 353 $contents['files'][] = $dirName.'/'.$entry; 354 } 355 } 356 } 357 $d->close(); 358 359 return $contents; 360 } 361 362 public static function makePackage($name,$dir,$remove_path='',$gzip=true) 363 { 364 if ($gzip && !function_exists('gzcompress')) { 365 throw new Exception(__('No compression functions')); 366 } 367 368 if (($filelist = files::getDirList($dir)) === false) { 369 throw new Exception(__('Unable to list directory.')); 370 } 371 372 $res = array ('name' => $name, 'dirs' => array(), 'files' => array()); 373 374 foreach ($filelist['dirs'] as $v) { 375 $res['dirs'][] = preg_replace('/^'.preg_quote($remove_path,'/').'/','',$v); 376 } 377 378 foreach ($filelist['files'] as $v) { 379 $f_content = base64_encode(file_get_contents($v)); 380 $v = preg_replace('/^'.preg_quote($remove_path,'/').'/','',$v); 381 $res['files'][$v] = $f_content; 382 } 383 384 $res = serialize($res); 385 386 if ($gzip) { 387 $res = gzencode($res); 388 } 389 390 return $res; 391 } 392 393 public static function installPackage($package,$with_remove=true) 394 { 395 $dest_path = dirname($package); 396 397 if (($content = @implode('',@gzfile($package))) === false) { 398 if ($with_remove) { 399 unlink($package); 400 } 401 throw new Exception(__('Cannot open file.')); 402 } 403 404 if (($list = @unserialize($content)) === false) { 405 if ($with_remove) { 406 unlink($package); 407 } 408 throw new Exception(__('Package not valid.')); 409 } 410 unset($content); 411 412 if (is_dir($dest_path.'/'.$list['name'])) { 413 unlink($package); 414 throw new Exception(__('This package is already installed. Delete it before.')); 415 } 416 417 foreach ($list['dirs'] as $d) { 418 mkdir($dest_path.'/'.$d,fileperms($dest_path)); 419 chmod($dest_path.'/'.$d,fileperms($dest_path)); 420 } 421 422 foreach ($list['files'] as $f => $v) { 423 $v = base64_decode($v); 424 $fp = fopen($dest_path.'/'.$f,'w'); 425 fwrite($fp,$v,strlen($v)); 426 fclose($fp); 427 chmod($dest_path.'/'.$f,fileperms($dest_path) & ~0111); 428 } 429 430 unlink($package); 431 432 return true; 433 } 434 435 public static function tidyFileName($n) 436 { 437 $n = preg_replace('/^[.]/u','',$n); 438 return preg_replace('/[^A-Za-z0-9._-]/u','_',$n); 439 } 440 } 441 442 443 class path 444 { 445 public static function real($p,$strict=true) 446 { 447 $os = (DIRECTORY_SEPARATOR == '\\') ? 'win' : 'nix'; 448 449 # Absolute path? 450 if ($os == 'win') { 451 $_abs = preg_match('/^\w+:/',$p); 452 } else { 453 $_abs = substr($p,0,1) == '/'; 454 } 455 456 # Standard path form 457 if ($os == 'win') { 458 $p = str_replace('\\','/',$p); 459 } 460 461 # Adding root if !$_abs 462 if (!$_abs) { 463 $p = dirname($_SERVER['SCRIPT_FILENAME']).'/'.$p; 464 } 465 466 # Clean up 467 $p = preg_replace('|/+|','/',$p); 468 469 if (strlen($p) > 1) { 470 $p = preg_replace('|/$|','',$p); 471 } 472 473 $_start = ''; 474 if ($os == 'win') { 475 list($_start,$p) = explode(':',$p); 476 $_start .= ':/'; 477 } else { 478 $_start = '/'; 479 } 480 $p = substr($p,1); 481 482 # Go through 483 $P = explode('/',$p); 484 $res = array(); 485 486 for ($i=0;$i<count($P);$i++) 487 { 488 if ($P[$i] == '.') { 489 continue; 490 } 491 492 if ($P[$i] == '..') { 493 if (count($res) > 0) { 494 array_pop($res); 495 } 496 } else { 497 array_push($res,$P[$i]); 498 } 499 } 500 501 $p = $_start.implode('/',$res); 502 503 if ($strict && !@file_exists($p)) { 504 return false; 505 } 506 507 return $p; 508 } 509 510 public static function clean($p) 511 { 512 $p = str_replace('..','',$p); 513 $p = preg_replace('|/{2,}|','/',$p); 514 $p = preg_replace('|/$|','',$p); 515 516 return $p; 517 } 518 519 public static function info($f) 520 { 521 $p = pathinfo($f); 522 $res = array(); 523 524 $res['dirname'] = $p['dirname']; 525 $res['basename'] = $p['basename']; 526 $res['extension'] = isset($p['extension']) ? $p['extension'] : ''; 527 $res['base'] = preg_replace('/\.'.preg_quote($res['extension'],'/').'$/','',$res['basename']); 528 529 return $res; 530 } 531 532 public static function fullFromRoot($p,$root) 533 { 534 if (substr($p,0,1) == '/') { 535 return $p; 536 } 537 538 return $root.'/'.$p; 539 } 540 } 541 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |