elFinderVolumeDriver.class.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Base class for elFinder volume. |
| 5 | * Provide 2 layers: |
| 6 | * 1. Public API (commands) |
| 7 | * 2. abstract fs API |
| 8 | * All abstract methods begin with "_" |
| 9 | * |
| 10 | * @author Dmitry (dio) Levashov |
| 11 | * @author Troex Nevelin |
| 12 | * @author Alexey Sukhotin |
| 13 | * @method netmountPrepare(array $options) |
| 14 | * @method postNetmount(array $options) |
| 15 | */ |
| 16 | abstract class elFinderVolumeDriver |
| 17 | { |
| 18 | |
| 19 | /** |
| 20 | * Net mount key |
| 21 | * |
| 22 | * @var string |
| 23 | **/ |
| 24 | public $netMountKey = ''; |
| 25 | |
| 26 | /** |
| 27 | * Request args |
| 28 | * $_POST or $_GET values |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | protected $ARGS = array(); |
| 33 | |
| 34 | /** |
| 35 | * Driver id |
| 36 | * Must be started from letter and contains [a-z0-9] |
| 37 | * Used as part of volume id |
| 38 | * |
| 39 | * @var string |
| 40 | **/ |
| 41 | protected $driverId = 'a'; |
| 42 | |
| 43 | /** |
| 44 | * Volume id - used as prefix for files hashes |
| 45 | * |
| 46 | * @var string |
| 47 | **/ |
| 48 | protected $id = ''; |
| 49 | |
| 50 | /** |
| 51 | * Flag - volume "mounted" and available |
| 52 | * |
| 53 | * @var bool |
| 54 | **/ |
| 55 | protected $mounted = false; |
| 56 | |
| 57 | /** |
| 58 | * Root directory path |
| 59 | * |
| 60 | * @var string |
| 61 | **/ |
| 62 | protected $root = ''; |
| 63 | |
| 64 | /** |
| 65 | * Root basename | alias |
| 66 | * |
| 67 | * @var string |
| 68 | **/ |
| 69 | protected $rootName = ''; |
| 70 | |
| 71 | /** |
| 72 | * Default directory to open |
| 73 | * |
| 74 | * @var string |
| 75 | **/ |
| 76 | protected $startPath = ''; |
| 77 | |
| 78 | /** |
| 79 | * Base URL |
| 80 | * |
| 81 | * @var string |
| 82 | **/ |
| 83 | protected $URL = ''; |
| 84 | |
| 85 | /** |
| 86 | * Path to temporary directory |
| 87 | * |
| 88 | * @var string |
| 89 | */ |
| 90 | protected $tmp; |
| 91 | |
| 92 | /** |
| 93 | * A file save destination path when a temporary content URL is required |
| 94 | * on a network volume or the like |
| 95 | * If not specified, it tries to use "Connector Path/../files/.tmb". |
| 96 | * |
| 97 | * @var string |
| 98 | */ |
| 99 | protected $tmpLinkPath = ''; |
| 100 | |
| 101 | /** |
| 102 | * A file save destination URL when a temporary content URL is required |
| 103 | * on a network volume or the like |
| 104 | * If not specified, it tries to use "Connector URL/../files/.tmb". |
| 105 | * |
| 106 | * @var string |
| 107 | */ |
| 108 | protected $tmpLinkUrl = ''; |
| 109 | |
| 110 | /** |
| 111 | * Thumbnails dir path |
| 112 | * |
| 113 | * @var string |
| 114 | **/ |
| 115 | protected $tmbPath = ''; |
| 116 | |
| 117 | /** |
| 118 | * Is thumbnails dir writable |
| 119 | * |
| 120 | * @var bool |
| 121 | **/ |
| 122 | protected $tmbPathWritable = false; |
| 123 | |
| 124 | /** |
| 125 | * Thumbnails base URL |
| 126 | * |
| 127 | * @var string |
| 128 | **/ |
| 129 | protected $tmbURL = ''; |
| 130 | |
| 131 | /** |
| 132 | * Thumbnails size in px |
| 133 | * |
| 134 | * @var int |
| 135 | **/ |
| 136 | protected $tmbSize = 48; |
| 137 | |
| 138 | /** |
| 139 | * Image manipulation lib name |
| 140 | * auto|imagick|gd|convert |
| 141 | * |
| 142 | * @var string |
| 143 | **/ |
| 144 | protected $imgLib = 'auto'; |
| 145 | |
| 146 | /** |
| 147 | * Video to Image converter |
| 148 | * |
| 149 | * @var array |
| 150 | */ |
| 151 | protected $imgConverter = array(); |
| 152 | |
| 153 | /** |
| 154 | * Library to crypt files name |
| 155 | * |
| 156 | * @var string |
| 157 | **/ |
| 158 | protected $cryptLib = ''; |
| 159 | |
| 160 | /** |
| 161 | * Archivers config |
| 162 | * |
| 163 | * @var array |
| 164 | **/ |
| 165 | protected $archivers = array( |
| 166 | 'create' => array(), |
| 167 | 'extract' => array() |
| 168 | ); |
| 169 | |
| 170 | /** |
| 171 | * Static var of $this->options['maxArcFilesSize'] |
| 172 | * |
| 173 | * @var int|string |
| 174 | */ |
| 175 | protected static $maxArcFilesSize; |
| 176 | |
| 177 | /** |
| 178 | * Server character encoding |
| 179 | * |
| 180 | * @var string or null |
| 181 | **/ |
| 182 | protected $encoding = null; |
| 183 | |
| 184 | /** |
| 185 | * How many subdirs levels return for tree |
| 186 | * |
| 187 | * @var int |
| 188 | **/ |
| 189 | protected $treeDeep = 1; |
| 190 | |
| 191 | /** |
| 192 | * Errors from last failed action |
| 193 | * |
| 194 | * @var array |
| 195 | **/ |
| 196 | protected $error = array(); |
| 197 | |
| 198 | /** |
| 199 | * Today 24:00 timestamp |
| 200 | * |
| 201 | * @var int |
| 202 | **/ |
| 203 | protected $today = 0; |
| 204 | |
| 205 | /** |
| 206 | * Yesterday 24:00 timestamp |
| 207 | * |
| 208 | * @var int |
| 209 | **/ |
| 210 | protected $yesterday = 0; |
| 211 | |
| 212 | /** |
| 213 | * Force make dirctory on extract |
| 214 | * |
| 215 | * @var int |
| 216 | **/ |
| 217 | protected $extractToNewdir = 'auto'; |
| 218 | |
| 219 | /** |
| 220 | * Object configuration |
| 221 | * |
| 222 | * @var array |
| 223 | **/ |
| 224 | protected $options = array( |
| 225 | // Driver ID (Prefix of volume ID), Normally, the value specified for each volume driver is used. |
| 226 | 'driverId' => '', |
| 227 | // Id (Suffix of volume ID), Normally, the number incremented according to the specified number of volumes is used. |
| 228 | 'id' => '', |
| 229 | // revision id of root directory that uses for caching control of root stat |
| 230 | 'rootRev' => '', |
| 231 | // driver type it uses volume root's CSS class name. e.g. 'group' -> Adds 'elfinder-group' to CSS class name. |
| 232 | 'type' => '', |
| 233 | // root directory path |
| 234 | 'path' => '', |
| 235 | // Folder hash value on elFinder to be the parent of this volume |
| 236 | 'phash' => '', |
| 237 | // Folder hash value on elFinder to trash bin of this volume, it require 'copyJoin' to true |
| 238 | 'trashHash' => '', |
| 239 | // open this path on initial request instead of root path |
| 240 | 'startPath' => '', |
| 241 | // how many subdirs levels return per request |
| 242 | 'treeDeep' => 1, |
| 243 | // root url, not set to URL via the connector. If you want to hide the file URL, do not set this value. (replacement for old "fileURL" option) |
| 244 | 'URL' => '', |
| 245 | // enable onetime URL to a file - (true, false, 'auto' (true if a temporary directory is available) or callable (A function that return onetime URL)) |
| 246 | 'onetimeUrl' => 'auto', |
| 247 | // directory link url to own manager url with folder hash (`true`, `false`, `'hide'`(No show) or default `'auto'`: URL is empty then `true` else `false`) |
| 248 | 'dirUrlOwn' => 'auto', |
| 249 | // directory separator. required by client to show paths correctly |
| 250 | 'separator' => DIRECTORY_SEPARATOR, |
| 251 | // Use '/' as directory separator when the path hash encode/decode on the Windows server too |
| 252 | 'winHashFix' => false, |
| 253 | // Server character encoding (default is '': UTF-8) |
| 254 | 'encoding' => '', |
| 255 | // for convert character encoding (default is '': Not change locale) |
| 256 | 'locale' => '', |
| 257 | // URL of volume icon image |
| 258 | 'icon' => '', |
| 259 | // CSS Class of volume root in tree |
| 260 | 'rootCssClass' => '', |
| 261 | // Items to disable session caching |
| 262 | 'noSessionCache' => array(), |
| 263 | // enable i18n folder name that convert name to elFinderInstance.messages['folder_'+name] |
| 264 | 'i18nFolderName' => false, |
| 265 | // Search timeout (sec) |
| 266 | 'searchTimeout' => 30, |
| 267 | // Search exclusion directory regex pattern (require demiliter e.g. '#/path/to/exclude_directory#i') |
| 268 | 'searchExDirReg' => '', |
| 269 | // library to crypt/uncrypt files names (not implemented) |
| 270 | 'cryptLib' => '', |
| 271 | // how to detect files mimetypes. (auto/internal/finfo/mime_content_type) |
| 272 | 'mimeDetect' => 'auto', |
| 273 | // mime.types file path (for mimeDetect==internal) |
| 274 | 'mimefile' => '', |
| 275 | // Static extension/MIME of general server side scripts to security issues |
| 276 | 'staticMimeMap' => array( |
| 277 | 'php:*' => 'text/x-php', |
| 278 | 'pht:*' => 'text/x-php', |
| 279 | 'php3:*' => 'text/x-php', |
| 280 | 'php4:*' => 'text/x-php', |
| 281 | 'php5:*' => 'text/x-php', |
| 282 | 'php7:*' => 'text/x-php', |
| 283 | 'php8:*' => 'text/x-php', |
| 284 | 'php9:*' => 'text/x-php', |
| 285 | 'phtml:*' => 'text/x-php', |
| 286 | 'phar:*' => 'text/x-php', |
| 287 | 'cgi:*' => 'text/x-httpd-cgi', |
| 288 | 'pl:*' => 'text/x-perl', |
| 289 | 'asp:*' => 'text/x-asap', |
| 290 | 'aspx:*' => 'text/x-asap', |
| 291 | 'py:*' => 'text/x-python', |
| 292 | 'rb:*' => 'text/x-ruby', |
| 293 | 'jsp:*' => 'text/x-jsp' |
| 294 | ), |
| 295 | // mime type normalize map : Array '[ext]:[detected mime type]' => '[normalized mime]' |
| 296 | 'mimeMap' => array( |
| 297 | 'md:application/x-genesis-rom' => 'text/x-markdown', |
| 298 | 'md:text/plain' => 'text/x-markdown', |
| 299 | 'markdown:text/plain' => 'text/x-markdown', |
| 300 | 'css:text/x-asm' => 'text/css', |
| 301 | 'css:text/plain' => 'text/css', |
| 302 | 'csv:text/plain' => 'text/csv', |
| 303 | 'java:text/x-c' => 'text/x-java-source', |
| 304 | 'json:text/plain' => 'application/json', |
| 305 | 'sql:text/plain' => 'text/x-sql', |
| 306 | 'rtf:text/rtf' => 'application/rtf', |
| 307 | 'rtfd:text/rtfd' => 'application/rtfd', |
| 308 | 'ico:image/vnd.microsoft.icon' => 'image/x-icon', |
| 309 | 'svg:text/plain' => 'image/svg+xml', |
| 310 | 'pxd:application/octet-stream' => 'image/x-pixlr-data', |
| 311 | 'dng:image/tiff' => 'image/x-adobe-dng', |
| 312 | 'sketch:application/zip' => 'image/x-sketch', |
| 313 | 'sketch:application/octet-stream' => 'image/x-sketch', |
| 314 | 'xcf:application/octet-stream' => 'image/x-xcf', |
| 315 | 'amr:application/octet-stream' => 'audio/amr', |
| 316 | 'm4a:video/mp4' => 'audio/mp4', |
| 317 | 'oga:application/ogg' => 'audio/ogg', |
| 318 | 'ogv:application/ogg' => 'video/ogg', |
| 319 | 'zip:application/x-zip' => 'application/zip', |
| 320 | 'm3u8:text/plain' => 'application/x-mpegurl', |
| 321 | 'mpd:text/plain' => 'application/dash+xml', |
| 322 | 'mpd:text/xml' => 'application/dash+xml', |
| 323 | 'mpd:application/xml' => 'application/dash+xml', |
| 324 | '*:application/x-dosexec' => 'application/x-executable', |
| 325 | 'doc:application/vnd.ms-office' => 'application/msword', |
| 326 | 'xls:application/vnd.ms-office' => 'application/vnd.ms-excel', |
| 327 | 'ppt:application/vnd.ms-office' => 'application/vnd.ms-powerpoint', |
| 328 | 'yml:text/plain' => 'text/x-yaml', |
| 329 | 'ai:application/pdf' => 'application/postscript', |
| 330 | 'cgm:text/plain' => 'image/cgm', |
| 331 | 'dxf:text/plain' => 'image/vnd.dxf', |
| 332 | 'dds:application/octet-stream' => 'image/vnd-ms.dds', |
| 333 | 'hpgl:text/plain' => 'application/vnd.hp-hpgl', |
| 334 | 'igs:text/plain' => 'model/iges', |
| 335 | 'iges:text/plain' => 'model/iges', |
| 336 | 'plt:application/octet-stream' => 'application/plt', |
| 337 | 'plt:text/plain' => 'application/plt', |
| 338 | 'sat:text/plain' => 'application/sat', |
| 339 | 'step:text/plain' => 'application/step', |
| 340 | 'stp:text/plain' => 'application/step' |
| 341 | ), |
| 342 | // An option to add MimeMap to the `mimeMap` option |
| 343 | // Array '[ext]:[detected mime type]' => '[normalized mime]' |
| 344 | 'additionalMimeMap' => array(), |
| 345 | // MIME-Type of filetype detected as unknown |
| 346 | 'mimeTypeUnknown' => 'application/octet-stream', |
| 347 | // MIME regex of send HTTP header "Content-Disposition: inline" or allow preview in quicklook |
| 348 | // '.' is allow inline of all of MIME types |
| 349 | // '$^' is not allow inline of all of MIME types |
| 350 | 'dispInlineRegex' => '^(?:(?:video|audio)|image/(?!.+\+xml)|application/(?:ogg|x-mpegURL|dash\+xml)|(?:text/plain|application/pdf)$)', |
| 351 | // temporary content URL's base path |
| 352 | 'tmpLinkPath' => '', |
| 353 | // temporary content URL's base URL |
| 354 | 'tmpLinkUrl' => '', |
| 355 | // directory for thumbnails |
| 356 | 'tmbPath' => '.tmb', |
| 357 | // mode to create thumbnails dir |
| 358 | 'tmbPathMode' => 0777, |
| 359 | // thumbnails dir URL. Set it if store thumbnails outside root directory |
| 360 | 'tmbURL' => '', |
| 361 | // thumbnails size (px) |
| 362 | 'tmbSize' => 48, |
| 363 | // thumbnails crop (true - crop, false - scale image to fit thumbnail size) |
| 364 | 'tmbCrop' => true, |
| 365 | // thumbnail URL require custom data as the GET query |
| 366 | 'tmbReqCustomData' => false, |
| 367 | // thumbnails background color (hex #rrggbb or 'transparent') |
| 368 | 'tmbBgColor' => 'transparent', |
| 369 | // image rotate fallback background color (hex #rrggbb) |
| 370 | 'bgColorFb' => '#ffffff', |
| 371 | // image manipulations library (imagick|gd|convert|auto|none, none - Does not check the image library at all.) |
| 372 | 'imgLib' => 'auto', |
| 373 | // Fallback self image to thumbnail (nothing imgLib) |
| 374 | 'tmbFbSelf' => true, |
| 375 | // Video to Image converters ['TYPE or MIME' => ['func' => function($file){ /* Converts $file to Image */ return true; }, 'maxlen' => (int)TransferLength]] |
| 376 | 'imgConverter' => array(), |
| 377 | // Max length of transfer to image converter |
| 378 | 'tmbVideoConvLen' => 10000000, |
| 379 | // Captre point seccond |
| 380 | 'tmbVideoConvSec' => 6, |
| 381 | // Life time (hour) for thumbnail garbage collection ("0" means no GC) |
| 382 | 'tmbGcMaxlifeHour' => 0, |
| 383 | // Percentage of garbage collection executed for thumbnail creation command ("1" means "1%") |
| 384 | 'tmbGcPercentage' => 1, |
| 385 | // Resource path of fallback icon images defailt: php/resouces |
| 386 | 'resourcePath' => '', |
| 387 | // Jpeg image saveing quality |
| 388 | 'jpgQuality' => 100, |
| 389 | // Save as progressive JPEG on image editing |
| 390 | 'jpgProgressive' => true, |
| 391 | // enable to get substitute image with command `dim` |
| 392 | 'substituteImg' => true, |
| 393 | // on paste file - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext |
| 394 | 'copyOverwrite' => true, |
| 395 | // if true - join new and old directories content on paste |
| 396 | 'copyJoin' => true, |
| 397 | // on upload - if true - old file will be replaced with new one, if false new file get name - original_name-number.ext |
| 398 | 'uploadOverwrite' => true, |
| 399 | // mimetypes allowed to upload |
| 400 | 'uploadAllow' => array(), |
| 401 | // mimetypes not allowed to upload |
| 402 | 'uploadDeny' => array(), |
| 403 | // order to process uploadAllow and uploadDeny options |
| 404 | 'uploadOrder' => array('deny', 'allow'), |
| 405 | // maximum upload file size. NOTE - this is size for every uploaded files |
| 406 | 'uploadMaxSize' => 0, |
| 407 | // Maximum number of folders that can be created at one time. (0: unlimited) |
| 408 | 'uploadMaxMkdirs' => 0, |
| 409 | // maximum number of chunked upload connection. `-1` to disable chunked upload |
| 410 | 'uploadMaxConn' => 3, |
| 411 | // maximum get file size. NOTE - Maximum value is 50% of PHP memory_limit |
| 412 | 'getMaxSize' => 0, |
| 413 | // files dates format |
| 414 | 'dateFormat' => 'j M Y H:i', |
| 415 | // files time format |
| 416 | 'timeFormat' => 'H:i', |
| 417 | // if true - every folder will be check for children folders, -1 - every folder will be check asynchronously, false - all folders will be marked as having subfolders |
| 418 | 'checkSubfolders' => true, // true, false or -1 |
| 419 | // allow to copy from this volume to other ones? |
| 420 | 'copyFrom' => true, |
| 421 | // allow to copy from other volumes to this one? |
| 422 | 'copyTo' => true, |
| 423 | // cmd duplicate suffix format e.g. '_%s_' to without spaces |
| 424 | 'duplicateSuffix' => ' %s ', |
| 425 | // unique name numbar format e.g. '(%d)' to (1), (2)... |
| 426 | 'uniqueNumFormat' => '%d', |
| 427 | // list of commands disabled on this root |
| 428 | 'disabled' => array(), |
| 429 | // enable file owner, group & mode info, `false` to inactivate "chmod" command. |
| 430 | 'statOwner' => false, |
| 431 | // allow exec chmod of read-only files |
| 432 | 'allowChmodReadOnly' => false, |
| 433 | // regexp or function name to validate new file name |
| 434 | 'acceptedName' => '/^[^\.].*/', // Notice: overwritten it in some volume drivers contractor |
| 435 | // regexp or function name to validate new directory name |
| 436 | 'acceptedDirname' => '', // used `acceptedName` if empty value |
| 437 | // function/class method to control files permissions |
| 438 | 'accessControl' => null, |
| 439 | // some data required by access control |
| 440 | 'accessControlData' => null, |
| 441 | // root stat that return without asking the system when mounted and not the current volume. Query to the system with false. array|false |
| 442 | 'rapidRootStat' => array( |
| 443 | 'read' => true, |
| 444 | 'write' => true, |
| 445 | 'locked' => false, |
| 446 | 'hidden' => false, |
| 447 | 'size' => 0, // Unknown |
| 448 | 'ts' => 0, // Unknown |
| 449 | 'dirs' => -1, // Check on demand for subdirectories |
| 450 | 'mime' => 'directory' |
| 451 | ), |
| 452 | // default permissions. |
| 453 | 'defaults' => array( |
| 454 | 'read' => true, |
| 455 | 'write' => true, |
| 456 | 'locked' => false, |
| 457 | 'hidden' => false |
| 458 | ), |
| 459 | // files attributes |
| 460 | 'attributes' => array(), |
| 461 | // max allowed archive files size (0 - no limit) |
| 462 | 'maxArcFilesSize' => '2G', |
| 463 | // Allowed archive's mimetypes to create. Leave empty for all available types. |
| 464 | 'archiveMimes' => array(), |
| 465 | // Manual config for archivers. See example below. Leave empty for auto detect |
| 466 | 'archivers' => array(), |
| 467 | // Use Archive function for remote volume |
| 468 | 'useRemoteArchive' => false, |
| 469 | // plugin settings |
| 470 | 'plugin' => array(), |
| 471 | // Is support parent directory time stamp update on add|remove|rename item |
| 472 | // Default `null` is auto detection that is LocalFileSystem, FTP or Dropbox are `true` |
| 473 | 'syncChkAsTs' => null, |
| 474 | // Long pooling sync checker function for syncChkAsTs is true |
| 475 | // Calls with args (TARGET DIRCTORY PATH, STAND-BY(sec), OLD TIMESTAMP, VOLUME DRIVER INSTANCE, ELFINDER INSTANCE) |
| 476 | // This function must return the following values. Changed: New Timestamp or Same: Old Timestamp or Error: false |
| 477 | // Default `null` is try use elFinderVolumeLocalFileSystem::localFileSystemInotify() on LocalFileSystem driver |
| 478 | // another driver use elFinder stat() checker |
| 479 | 'syncCheckFunc' => null, |
| 480 | // Long polling sync stand-by time (sec) |
| 481 | 'plStandby' => 30, |
| 482 | // Sleep time (sec) for elFinder stat() checker (syncChkAsTs is true) |
| 483 | 'tsPlSleep' => 10, |
| 484 | // Sleep time (sec) for elFinder ls() checker (syncChkAsTs is false) |
| 485 | 'lsPlSleep' => 30, |
| 486 | // Client side sync interval minimum (ms) |
| 487 | // Default `null` is auto set to ('tsPlSleep' or 'lsPlSleep') * 1000 |
| 488 | // `0` to disable auto sync |
| 489 | 'syncMinMs' => null, |
| 490 | // required to fix bug on macos |
| 491 | // However, we recommend to use the Normalizer plugin instead this option |
| 492 | 'utf8fix' => false, |
| 493 | // й ё Й Ё Ø � |
| 494 | |
| 495 | 'utf8patterns' => array("\u0438\u0306", "\u0435\u0308", "\u0418\u0306", "\u0415\u0308", "\u00d8A", "\u030a"), |
| 496 | 'utf8replace' => array("\u0439", "\u0451", "\u0419", "\u0401", "\u00d8", "\u00c5"), |
| 497 | // cache control HTTP headers for commands `file` and `get` |
| 498 | 'cacheHeaders' => array( |
| 499 | 'Cache-Control: max-age=3600', |
| 500 | 'Expires:', |
| 501 | 'Pragma:' |
| 502 | ), |
| 503 | // Header to use to accelerate sending local files to clients (e.g. 'X-Sendfile', 'X-Accel-Redirect') |
| 504 | 'xsendfile' => '', |
| 505 | // Root path to xsendfile target. Probably, this is required for 'X-Accel-Redirect' on Nginx. |
| 506 | 'xsendfilePath' => '' |
| 507 | ); |
| 508 | |
| 509 | /** |
| 510 | * Defaults permissions |
| 511 | * |
| 512 | * @var array |
| 513 | **/ |
| 514 | protected $defaults = array( |
| 515 | 'read' => true, |
| 516 | 'write' => true, |
| 517 | 'locked' => false, |
| 518 | 'hidden' => false |
| 519 | ); |
| 520 | |
| 521 | /** |
| 522 | * Access control function/class |
| 523 | * |
| 524 | * @var mixed |
| 525 | **/ |
| 526 | protected $attributes = array(); |
| 527 | |
| 528 | /** |
| 529 | * Access control function/class |
| 530 | * |
| 531 | * @var mixed |
| 532 | **/ |
| 533 | protected $access = null; |
| 534 | |
| 535 | /** |
| 536 | * Mime types allowed to upload |
| 537 | * |
| 538 | * @var array |
| 539 | **/ |
| 540 | protected $uploadAllow = array(); |
| 541 | |
| 542 | /** |
| 543 | * Mime types denied to upload |
| 544 | * |
| 545 | * @var array |
| 546 | **/ |
| 547 | protected $uploadDeny = array(); |
| 548 | |
| 549 | /** |
| 550 | * Order to validate uploadAllow and uploadDeny |
| 551 | * |
| 552 | * @var array |
| 553 | **/ |
| 554 | protected $uploadOrder = array(); |
| 555 | |
| 556 | /** |
| 557 | * Maximum allowed upload file size. |
| 558 | * Set as number or string with unit - "10M", "500K", "1G" |
| 559 | * |
| 560 | * @var int|string |
| 561 | **/ |
| 562 | protected $uploadMaxSize = 0; |
| 563 | |
| 564 | /** |
| 565 | * Run time setting of overwrite items on upload |
| 566 | * |
| 567 | * @var string |
| 568 | */ |
| 569 | protected $uploadOverwrite = true; |
| 570 | |
| 571 | /** |
| 572 | * Maximum allowed get file size. |
| 573 | * Set as number or string with unit - "10M", "500K", "1G" |
| 574 | * |
| 575 | * @var int|string |
| 576 | **/ |
| 577 | protected $getMaxSize = -1; |
| 578 | |
| 579 | /** |
| 580 | * Mimetype detect method |
| 581 | * |
| 582 | * @var string |
| 583 | **/ |
| 584 | protected $mimeDetect = 'auto'; |
| 585 | |
| 586 | /** |
| 587 | * Flag - mimetypes from externail file was loaded |
| 588 | * |
| 589 | * @var bool |
| 590 | **/ |
| 591 | private static $mimetypesLoaded = false; |
| 592 | |
| 593 | /** |
| 594 | * Finfo resource for mimeDetect == 'finfo' |
| 595 | * |
| 596 | * @var resource |
| 597 | **/ |
| 598 | protected $finfo = null; |
| 599 | |
| 600 | /** |
| 601 | * List of disabled client's commands |
| 602 | * |
| 603 | * @var array |
| 604 | **/ |
| 605 | protected $disabled = array(); |
| 606 | |
| 607 | /** |
| 608 | * overwrite extensions/mimetypes to mime.types |
| 609 | * |
| 610 | * @var array |
| 611 | **/ |
| 612 | protected static $mimetypes = array( |
| 613 | // applications |
| 614 | 'exe' => 'application/x-executable', |
| 615 | 'jar' => 'application/x-jar', |
| 616 | 'rpm' => 'application/x-rpm', |
| 617 | // archives |
| 618 | 'gz' => 'application/x-gzip', |
| 619 | 'tgz' => 'application/x-gzip', |
| 620 | 'tbz' => 'application/x-bzip2', |
| 621 | 'rar' => 'application/x-rar', |
| 622 | // texts |
| 623 | 'php' => 'text/x-php', |
| 624 | 'js' => 'text/javascript', |
| 625 | 'rtfd' => 'application/rtfd', |
| 626 | 'py' => 'text/x-python', |
| 627 | 'rb' => 'text/x-ruby', |
| 628 | 'sh' => 'text/x-shellscript', |
| 629 | 'pl' => 'text/x-perl', |
| 630 | 'xml' => 'text/xml', |
| 631 | 'c' => 'text/x-csrc', |
| 632 | 'h' => 'text/x-chdr', |
| 633 | 'cpp' => 'text/x-c++src', |
| 634 | 'hpp' => 'text/x-c++hdr', |
| 635 | 'md' => 'text/x-markdown', |
| 636 | 'markdown' => 'text/x-markdown', |
| 637 | 'yml' => 'text/x-yaml', |
| 638 | // images |
| 639 | 'bmp' => 'image/x-ms-bmp', |
| 640 | 'tga' => 'image/x-targa', |
| 641 | 'xbm' => 'image/xbm', |
| 642 | 'pxm' => 'image/pxm', |
| 643 | //audio |
| 644 | 'wav' => 'audio/wav', |
| 645 | // video |
| 646 | 'dv' => 'video/x-dv', |
| 647 | 'wm' => 'video/x-ms-wmv', |
| 648 | 'ogm' => 'video/ogg', |
| 649 | 'm2ts' => 'video/mp2t', |
| 650 | 'mts' => 'video/mp2t', |
| 651 | 'ts' => 'video/mp2t', |
| 652 | 'm3u8' => 'application/x-mpegurl', |
| 653 | 'mpd' => 'application/dash+xml' |
| 654 | ); |
| 655 | |
| 656 | /** |
| 657 | * Directory separator - required by client |
| 658 | * |
| 659 | * @var string |
| 660 | **/ |
| 661 | protected $separator = DIRECTORY_SEPARATOR; |
| 662 | |
| 663 | /** |
| 664 | * Directory separator for decode/encode hash |
| 665 | * |
| 666 | * @var string |
| 667 | **/ |
| 668 | protected $separatorForHash = ''; |
| 669 | |
| 670 | /** |
| 671 | * System Root path (Unix like: '/', Windows: '\', 'C:\' or 'D:\'...) |
| 672 | * |
| 673 | * @var string |
| 674 | **/ |
| 675 | protected $systemRoot = DIRECTORY_SEPARATOR; |
| 676 | |
| 677 | /** |
| 678 | * Mimetypes allowed to display |
| 679 | * |
| 680 | * @var array |
| 681 | **/ |
| 682 | protected $onlyMimes = array(); |
| 683 | |
| 684 | /** |
| 685 | * Store files moved or overwrited files info |
| 686 | * |
| 687 | * @var array |
| 688 | **/ |
| 689 | protected $removed = array(); |
| 690 | |
| 691 | /** |
| 692 | * Store files added files info |
| 693 | * |
| 694 | * @var array |
| 695 | **/ |
| 696 | protected $added = array(); |
| 697 | |
| 698 | /** |
| 699 | * Cache storage |
| 700 | * |
| 701 | * @var array |
| 702 | **/ |
| 703 | protected $cache = array(); |
| 704 | |
| 705 | /** |
| 706 | * Cache by folders |
| 707 | * |
| 708 | * @var array |
| 709 | **/ |
| 710 | protected $dirsCache = array(); |
| 711 | |
| 712 | /** |
| 713 | * You should use `$this->sessionCache['subdirs']` instead |
| 714 | * |
| 715 | * @var array |
| 716 | * @deprecated |
| 717 | */ |
| 718 | protected $subdirsCache = array(); |
| 719 | |
| 720 | /** |
| 721 | * This volume session cache |
| 722 | * |
| 723 | * @var array |
| 724 | */ |
| 725 | protected $sessionCache; |
| 726 | |
| 727 | /** |
| 728 | * Session caching item list |
| 729 | * |
| 730 | * @var array |
| 731 | */ |
| 732 | protected $sessionCaching = array('rootstat' => true, 'subdirs' => true); |
| 733 | |
| 734 | /** |
| 735 | * elFinder session wrapper object |
| 736 | * |
| 737 | * @var elFinderSessionInterface |
| 738 | */ |
| 739 | protected $session; |
| 740 | |
| 741 | /** |
| 742 | * Search start time |
| 743 | * |
| 744 | * @var int |
| 745 | */ |
| 746 | protected $searchStart; |
| 747 | |
| 748 | /** |
| 749 | * Current query word on doSearch |
| 750 | * |
| 751 | * @var array |
| 752 | **/ |
| 753 | protected $doSearchCurrentQuery = array(); |
| 754 | |
| 755 | /** |
| 756 | * Is root modified (for clear root stat cache) |
| 757 | * |
| 758 | * @var bool |
| 759 | */ |
| 760 | protected $rootModified = false; |
| 761 | |
| 762 | /** |
| 763 | * Is disable of command `url` |
| 764 | * |
| 765 | * @var string |
| 766 | */ |
| 767 | protected $disabledGetUrl = false; |
| 768 | |
| 769 | /** |
| 770 | * Accepted filename validator |
| 771 | * |
| 772 | * @var string | callable |
| 773 | */ |
| 774 | protected $nameValidator; |
| 775 | |
| 776 | /** |
| 777 | * Accepted dirname validator |
| 778 | * |
| 779 | * @var string | callable |
| 780 | */ |
| 781 | protected $dirnameValidator; |
| 782 | |
| 783 | /** |
| 784 | * This request require online state |
| 785 | * |
| 786 | * @var boolean |
| 787 | */ |
| 788 | protected $needOnline; |
| 789 | |
| 790 | /*********************************************************************/ |
| 791 | /* INITIALIZATION */ |
| 792 | /*********************************************************************/ |
| 793 | |
| 794 | /** |
| 795 | * Sets the need online. |
| 796 | * |
| 797 | * @param boolean $state The state |
| 798 | */ |
| 799 | public function setNeedOnline($state = null) |
| 800 | { |
| 801 | if ($state !== null) { |
| 802 | $this->needOnline = (bool)$state; |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | $need = false; |
| 807 | $arg = $this->ARGS; |
| 808 | $id = $this->id; |
| 809 | |
| 810 | $target = !empty($arg['target'])? $arg['target'] : (!empty($arg['dst'])? $arg['dst'] : ''); |
| 811 | $targets = !empty($arg['targets'])? $arg['targets'] : array(); |
| 812 | if (!is_array($targets)) { |
| 813 | $targets = array($targets); |
| 814 | } |
| 815 | |
| 816 | if ($target && strpos($target, $id) === 0) { |
| 817 | $need = true; |
| 818 | } else if ($targets) { |
| 819 | foreach($targets as $t) { |
| 820 | if ($t && strpos($t, $id) === 0) { |
| 821 | $need = true; |
| 822 | break; |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | $this->needOnline = $need; |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Prepare driver before mount volume. |
| 832 | * Return true if volume is ready. |
| 833 | * |
| 834 | * @return bool |
| 835 | * @author Dmitry (dio) Levashov |
| 836 | **/ |
| 837 | protected function init() |
| 838 | { |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Configure after successfull mount. |
| 844 | * By default set thumbnails path and image manipulation library. |
| 845 | * |
| 846 | * @return void |
| 847 | * @throws elFinderAbortException |
| 848 | * @author Dmitry (dio) Levashov |
| 849 | */ |
| 850 | protected function configure() |
| 851 | { |
| 852 | // set thumbnails path |
| 853 | $path = $this->options['tmbPath']; |
| 854 | if ($path) { |
| 855 | if (!file_exists($path)) { |
| 856 | if (mkdir($path)) { |
| 857 | chmod($path, $this->options['tmbPathMode']); |
| 858 | } else { |
| 859 | $path = ''; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | if (is_dir($path) && is_readable($path)) { |
| 864 | $this->tmbPath = $path; |
| 865 | $this->tmbPathWritable = is_writable($path); |
| 866 | } |
| 867 | } |
| 868 | // set resouce path |
| 869 | if (!is_dir($this->options['resourcePath'])) { |
| 870 | $this->options['resourcePath'] = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'resources'; |
| 871 | } |
| 872 | |
| 873 | // set image manipulation library |
| 874 | $type = preg_match('/^(imagick|gd|convert|auto|none)$/i', $this->options['imgLib']) |
| 875 | ? strtolower($this->options['imgLib']) |
| 876 | : 'auto'; |
| 877 | |
| 878 | if ($type === 'none') { |
| 879 | $this->imgLib = ''; |
| 880 | } else { |
| 881 | if (($type === 'imagick' || $type === 'auto') && extension_loaded('imagick')) { |
| 882 | $this->imgLib = 'imagick'; |
| 883 | } else if (($type === 'gd' || $type === 'auto') && function_exists('gd_info')) { |
| 884 | $this->imgLib = 'gd'; |
| 885 | } else { |
| 886 | $convertCache = 'imgLibConvert'; |
| 887 | if (($convertCmd = $this->session->get($convertCache, false)) !== false) { |
| 888 | $this->imgLib = $convertCmd; |
| 889 | } else { |
| 890 | $this->imgLib = ($this->procExec(ELFINDER_CONVERT_PATH . ' -version') === 0) ? 'convert' : ''; |
| 891 | $this->session->set($convertCache, $this->imgLib); |
| 892 | } |
| 893 | } |
| 894 | if ($type !== 'auto' && $this->imgLib === '') { |
| 895 | // fallback |
| 896 | $this->imgLib = extension_loaded('imagick') ? 'imagick' : (function_exists('gd_info') ? 'gd' : ''); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // check video to img converter |
| 901 | if (!empty($this->options['imgConverter']) && is_array($this->options['imgConverter'])) { |
| 902 | foreach ($this->options['imgConverter'] as $_type => $_converter) { |
| 903 | if (isset($_converter['func'])) { |
| 904 | $this->imgConverter[strtolower($_type)] = $_converter; |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | if (!isset($this->imgConverter['video'])) { |
| 909 | $videoLibCache = 'videoLib'; |
| 910 | if (($videoLibCmd = $this->session->get($videoLibCache, false)) === false) { |
| 911 | $videoLibCmd = ($this->procExec(ELFINDER_FFMPEG_PATH . ' -version') === 0) ? 'ffmpeg' : ''; |
| 912 | $this->session->set($videoLibCache, $videoLibCmd); |
| 913 | } |
| 914 | if ($videoLibCmd) { |
| 915 | $this->imgConverter['video'] = array( |
| 916 | 'func' => array($this, $videoLibCmd . 'ToImg'), |
| 917 | 'maxlen' => $this->options['tmbVideoConvLen'] |
| 918 | ); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // check onetimeUrl |
| 923 | if (strtolower($this->options['onetimeUrl']) === 'auto') { |
| 924 | $this->options['onetimeUrl'] = elFinder::getStaticVar('commonTempPath')? true : false; |
| 925 | } |
| 926 | |
| 927 | // check archivers |
| 928 | if (empty($this->archivers['create'])) { |
| 929 | $this->disabled[] = 'archive'; |
| 930 | } |
| 931 | if (empty($this->archivers['extract'])) { |
| 932 | $this->disabled[] = 'extract'; |
| 933 | } |
| 934 | $_arc = $this->getArchivers(); |
| 935 | if (empty($_arc['create'])) { |
| 936 | $this->disabled[] = 'zipdl'; |
| 937 | } |
| 938 | |
| 939 | if ($this->options['maxArcFilesSize']) { |
| 940 | $this->options['maxArcFilesSize'] = elFinder::getIniBytes('', $this->options['maxArcFilesSize']); |
| 941 | } |
| 942 | self::$maxArcFilesSize = $this->options['maxArcFilesSize']; |
| 943 | |
| 944 | // check 'statOwner' for command `chmod` |
| 945 | if (empty($this->options['statOwner'])) { |
| 946 | $this->disabled[] = 'chmod'; |
| 947 | } |
| 948 | |
| 949 | // check 'mimeMap' |
| 950 | if (!is_array($this->options['mimeMap'])) { |
| 951 | $this->options['mimeMap'] = array(); |
| 952 | } |
| 953 | if (empty($this->options['staticMimeMap']) && is_array($this->options['staticMineMap']) && $this->options['staticMineMap']) { |
| 954 | $this->options['staticMimeMap'] = $this->options['staticMineMap']; |
| 955 | } |
| 956 | if (is_array($this->options['staticMimeMap']) && $this->options['staticMimeMap']) { |
| 957 | $this->options['mimeMap'] = array_merge($this->options['mimeMap'], $this->options['staticMimeMap']); |
| 958 | } |
| 959 | if (is_array($this->options['additionalMimeMap']) && $this->options['additionalMimeMap']) { |
| 960 | $this->options['mimeMap'] = array_merge($this->options['mimeMap'], $this->options['additionalMimeMap']); |
| 961 | } |
| 962 | |
| 963 | // check 'url' in disabled commands |
| 964 | if (in_array('url', $this->disabled)) { |
| 965 | $this->disabledGetUrl = true; |
| 966 | } |
| 967 | |
| 968 | // set run time setting uploadOverwrite |
| 969 | $this->uploadOverwrite = $this->options['uploadOverwrite']; |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * @deprecated |
| 974 | */ |
| 975 | protected function sessionRestart() |
| 976 | { |
| 977 | $this->sessionCache = $this->session->start()->get($this->id, array()); |
| 978 | return true; |
| 979 | } |
| 980 | |
| 981 | /*********************************************************************/ |
| 982 | /* PUBLIC API */ |
| 983 | /*********************************************************************/ |
| 984 | |
| 985 | /** |
| 986 | * Return driver id. Used as a part of volume id. |
| 987 | * |
| 988 | * @return string |
| 989 | * @author Dmitry (dio) Levashov |
| 990 | **/ |
| 991 | public function driverId() |
| 992 | { |
| 993 | return $this->driverId; |
| 994 | } |
| 995 | |
| 996 | /** |
| 997 | * Return volume id |
| 998 | * |
| 999 | * @return string |
| 1000 | * @author Dmitry (dio) Levashov |
| 1001 | **/ |
| 1002 | public function id() |
| 1003 | { |
| 1004 | return $this->id; |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Assign elFinder session wrapper object |
| 1009 | * |
| 1010 | * @param $session elFinderSessionInterface |
| 1011 | */ |
| 1012 | public function setSession($session) |
| 1013 | { |
| 1014 | $this->session = $session; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * Get elFinder sesson wrapper object |
| 1019 | * |
| 1020 | * @return object The session object |
| 1021 | */ |
| 1022 | public function getSession() |
| 1023 | { |
| 1024 | return $this->session; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * Save session cache data |
| 1029 | * Calls this function before umount this volume on elFinder::exec() |
| 1030 | * |
| 1031 | * @return void |
| 1032 | */ |
| 1033 | public function saveSessionCache() |
| 1034 | { |
| 1035 | $this->session->set($this->id, $this->sessionCache); |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Return debug info for client |
| 1040 | * |
| 1041 | * @return array |
| 1042 | * @author Dmitry (dio) Levashov |
| 1043 | **/ |
| 1044 | public function debug() |
| 1045 | { |
| 1046 | return array( |
| 1047 | 'id' => $this->id(), |
| 1048 | 'name' => strtolower(substr(get_class($this), strlen('elfinderdriver'))), |
| 1049 | 'mimeDetect' => $this->mimeDetect, |
| 1050 | 'imgLib' => $this->imgLib |
| 1051 | ); |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * chmod a file or folder |
| 1056 | * |
| 1057 | * @param string $hash file or folder hash to chmod |
| 1058 | * @param string $mode octal string representing new permissions |
| 1059 | * |
| 1060 | * @return array|false |
| 1061 | * @author David Bartle |
| 1062 | **/ |
| 1063 | public function chmod($hash, $mode) |
| 1064 | { |
| 1065 | if ($this->commandDisabled('chmod')) { |
| 1066 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 1067 | } |
| 1068 | |
| 1069 | if (!($file = $this->file($hash))) { |
| 1070 | return $this->setError(elFinder::ERROR_FILE_NOT_FOUND); |
| 1071 | } |
| 1072 | |
| 1073 | if (!$this->options['allowChmodReadOnly']) { |
| 1074 | if (!$this->attr($this->decode($hash), 'write', null, ($file['mime'] === 'directory'))) { |
| 1075 | return $this->setError(elFinder::ERROR_PERM_DENIED, $file['name']); |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | $path = $this->decode($hash); |
| 1080 | $write = $file['write']; |
| 1081 | |
| 1082 | if ($this->convEncOut(!$this->_chmod($this->convEncIn($path), $mode))) { |
| 1083 | return $this->setError(elFinder::ERROR_PERM_DENIED, $file['name']); |
| 1084 | } |
| 1085 | |
| 1086 | $this->clearstatcache(); |
| 1087 | if ($path == $this->root) { |
| 1088 | $this->rootModified = true; |
| 1089 | } |
| 1090 | |
| 1091 | if ($file = $this->stat($path)) { |
| 1092 | $files = array($file); |
| 1093 | if ($file['mime'] === 'directory' && $write !== $file['write']) { |
| 1094 | foreach ($this->getScandir($path) as $stat) { |
| 1095 | if ($this->mimeAccepted($stat['mime'])) { |
| 1096 | $files[] = $stat; |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | return $files; |
| 1101 | } else { |
| 1102 | return $this->setError(elFinder::ERROR_FILE_NOT_FOUND); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * stat a file or folder for elFinder cmd exec |
| 1108 | * |
| 1109 | * @param string $hash file or folder hash to chmod |
| 1110 | * |
| 1111 | * @return array |
| 1112 | * @author Naoki Sawada |
| 1113 | **/ |
| 1114 | public function fstat($hash) |
| 1115 | { |
| 1116 | $path = $this->decode($hash); |
| 1117 | return $this->stat($path); |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * Clear PHP stat cache & all of inner stat caches |
| 1122 | */ |
| 1123 | public function clearstatcache() |
| 1124 | { |
| 1125 | clearstatcache(); |
| 1126 | $this->clearcache(); |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * Clear inner stat caches for target hash |
| 1131 | * |
| 1132 | * @param string $hash |
| 1133 | */ |
| 1134 | public function clearcaches($hash = null) |
| 1135 | { |
| 1136 | if ($hash === null) { |
| 1137 | $this->clearcache(); |
| 1138 | } else { |
| 1139 | $path = $this->decode($hash); |
| 1140 | unset($this->cache[$path], $this->dirsCache[$path]); |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | /** |
| 1145 | * "Mount" volume. |
| 1146 | * Return true if volume available for read or write, |
| 1147 | * false - otherwise |
| 1148 | * |
| 1149 | * @param array $opts |
| 1150 | * |
| 1151 | * @return bool |
| 1152 | * @throws elFinderAbortException |
| 1153 | * @author Dmitry (dio) Levashov |
| 1154 | * @author Alexey Sukhotin |
| 1155 | */ |
| 1156 | public function mount(array $opts) |
| 1157 | { |
| 1158 | $this->options = array_merge($this->options, $opts); |
| 1159 | |
| 1160 | if (!isset($this->options['path']) || $this->options['path'] === '') { |
| 1161 | return $this->setError('Path undefined.'); |
| 1162 | } |
| 1163 | |
| 1164 | if (!$this->session) { |
| 1165 | return $this->setError('Session wrapper dose not set. Need to `$volume->setSession(elFinderSessionInterface);` before mount.'); |
| 1166 | } |
| 1167 | if (!($this->session instanceof elFinderSessionInterface)) { |
| 1168 | return $this->setError('Session wrapper instance must be "elFinderSessionInterface".'); |
| 1169 | } |
| 1170 | |
| 1171 | // set driverId |
| 1172 | if (!empty($this->options['driverId'])) { |
| 1173 | $this->driverId = $this->options['driverId']; |
| 1174 | } |
| 1175 | |
| 1176 | $this->id = $this->driverId . (!empty($this->options['id']) ? $this->options['id'] : elFinder::$volumesCnt++) . '_'; |
| 1177 | $this->root = $this->normpathCE($this->options['path']); |
| 1178 | $this->separator = isset($this->options['separator']) ? $this->options['separator'] : DIRECTORY_SEPARATOR; |
| 1179 | if (!empty($this->options['winHashFix'])) { |
| 1180 | $this->separatorForHash = ($this->separator !== '/') ? '/' : ''; |
| 1181 | } |
| 1182 | $this->systemRoot = isset($this->options['systemRoot']) ? $this->options['systemRoot'] : $this->separator; |
| 1183 | |
| 1184 | // set ARGS |
| 1185 | $this->ARGS = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET; |
| 1186 | |
| 1187 | $argInit = !empty($this->ARGS['init']); |
| 1188 | |
| 1189 | // set $this->needOnline |
| 1190 | if (!is_bool($this->needOnline)) { |
| 1191 | $this->setNeedOnline(); |
| 1192 | } |
| 1193 | |
| 1194 | // session cache |
| 1195 | if ($argInit) { |
| 1196 | $this->session->set($this->id, array()); |
| 1197 | } |
| 1198 | $this->sessionCache = $this->session->get($this->id, array()); |
| 1199 | |
| 1200 | // default file attribute |
| 1201 | $this->defaults = array( |
| 1202 | 'read' => isset($this->options['defaults']['read']) ? !!$this->options['defaults']['read'] : true, |
| 1203 | 'write' => isset($this->options['defaults']['write']) ? !!$this->options['defaults']['write'] : true, |
| 1204 | 'locked' => isset($this->options['defaults']['locked']) ? !!$this->options['defaults']['locked'] : false, |
| 1205 | 'hidden' => isset($this->options['defaults']['hidden']) ? !!$this->options['defaults']['hidden'] : false |
| 1206 | ); |
| 1207 | |
| 1208 | // root attributes |
| 1209 | $this->attributes[] = array( |
| 1210 | 'pattern' => '~^' . preg_quote($this->separator) . '$~', |
| 1211 | 'locked' => true, |
| 1212 | 'hidden' => false |
| 1213 | ); |
| 1214 | // set files attributes |
| 1215 | if (!empty($this->options['attributes']) && is_array($this->options['attributes'])) { |
| 1216 | |
| 1217 | foreach ($this->options['attributes'] as $a) { |
| 1218 | // attributes must contain pattern and at least one rule |
| 1219 | if (!empty($a['pattern']) || (is_array($a) && count($a) > 1)) { |
| 1220 | $this->attributes[] = $a; |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | if (!empty($this->options['accessControl']) && is_callable($this->options['accessControl'])) { |
| 1226 | $this->access = $this->options['accessControl']; |
| 1227 | } |
| 1228 | |
| 1229 | $this->today = mktime(0, 0, 0, date('m'), date('d'), date('Y')); |
| 1230 | $this->yesterday = $this->today - 86400; |
| 1231 | |
| 1232 | if (!$this->init()) { |
| 1233 | return false; |
| 1234 | } |
| 1235 | |
| 1236 | // set server encoding |
| 1237 | if (!empty($this->options['encoding']) && strtoupper($this->options['encoding']) !== 'UTF-8') { |
| 1238 | $this->encoding = $this->options['encoding']; |
| 1239 | } else { |
| 1240 | $this->encoding = null; |
| 1241 | } |
| 1242 | |
| 1243 | // check some options is arrays |
| 1244 | $this->uploadAllow = isset($this->options['uploadAllow']) && is_array($this->options['uploadAllow']) |
| 1245 | ? $this->options['uploadAllow'] |
| 1246 | : array(); |
| 1247 | |
| 1248 | $this->uploadDeny = isset($this->options['uploadDeny']) && is_array($this->options['uploadDeny']) |
| 1249 | ? $this->options['uploadDeny'] |
| 1250 | : array(); |
| 1251 | |
| 1252 | $this->options['uiCmdMap'] = (isset($this->options['uiCmdMap']) && is_array($this->options['uiCmdMap'])) |
| 1253 | ? $this->options['uiCmdMap'] |
| 1254 | : array(); |
| 1255 | |
| 1256 | if (is_string($this->options['uploadOrder'])) { // telephat_mode on, compatibility with 1.x |
| 1257 | $parts = explode(',', isset($this->options['uploadOrder']) ? $this->options['uploadOrder'] : 'deny,allow'); |
| 1258 | $this->uploadOrder = array(trim($parts[0]), trim($parts[1])); |
| 1259 | } else { // telephat_mode off |
| 1260 | $this->uploadOrder = !empty($this->options['uploadOrder']) ? $this->options['uploadOrder'] : array('deny', 'allow'); |
| 1261 | } |
| 1262 | |
| 1263 | if (!empty($this->options['uploadMaxSize'])) { |
| 1264 | $this->uploadMaxSize = elFinder::getIniBytes('', $this->options['uploadMaxSize']); |
| 1265 | } |
| 1266 | // Set maximum to PHP_INT_MAX |
| 1267 | if (!defined('PHP_INT_MAX')) { |
| 1268 | define('PHP_INT_MAX', 2147483647); |
| 1269 | } |
| 1270 | if ($this->uploadMaxSize < 1 || $this->uploadMaxSize > PHP_INT_MAX) { |
| 1271 | $this->uploadMaxSize = PHP_INT_MAX; |
| 1272 | } |
| 1273 | |
| 1274 | // Set to get maximum size to 50% of memory_limit |
| 1275 | $memLimit = elFinder::getIniBytes('memory_limit') / 2; |
| 1276 | if ($memLimit > 0) { |
| 1277 | $this->getMaxSize = empty($this->options['getMaxSize']) ? $memLimit : min($memLimit, elFinder::getIniBytes('', $this->options['getMaxSize'])); |
| 1278 | } else { |
| 1279 | $this->getMaxSize = -1; |
| 1280 | } |
| 1281 | |
| 1282 | $this->disabled = isset($this->options['disabled']) && is_array($this->options['disabled']) |
| 1283 | ? array_values(array_diff($this->options['disabled'], array('open'))) // 'open' is required |
| 1284 | : array(); |
| 1285 | |
| 1286 | $this->cryptLib = $this->options['cryptLib']; |
| 1287 | $this->mimeDetect = $this->options['mimeDetect']; |
| 1288 | |
| 1289 | // find available mimetype detect method |
| 1290 | $regexp = '/text\/x\-(php|c\+\+)/'; |
| 1291 | $auto_types = array(); |
| 1292 | |
| 1293 | if (class_exists('finfo', false)) { |
| 1294 | $tmpFileInfo = explode(';', finfo_file(finfo_open(FILEINFO_MIME), __FILE__)); |
| 1295 | if ($tmpFileInfo && preg_match($regexp, array_shift($tmpFileInfo))) { |
| 1296 | $auto_types[] = 'finfo'; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | if (function_exists('mime_content_type')) { |
| 1301 | $_mimetypes = explode(';', mime_content_type(__FILE__)); |
| 1302 | if (preg_match($regexp, array_shift($_mimetypes))) { |
| 1303 | $auto_types[] = 'mime_content_type'; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | $auto_types[] = 'internal'; |
| 1308 | |
| 1309 | $type = strtolower($this->options['mimeDetect']); |
| 1310 | if (!in_array($type, $auto_types)) { |
| 1311 | $type = 'auto'; |
| 1312 | } |
| 1313 | |
| 1314 | if ($type == 'auto') { |
| 1315 | $type = array_shift($auto_types); |
| 1316 | } |
| 1317 | |
| 1318 | $this->mimeDetect = $type; |
| 1319 | |
| 1320 | if ($this->mimeDetect == 'finfo') { |
| 1321 | $this->finfo = finfo_open(FILEINFO_MIME); |
| 1322 | } else if ($this->mimeDetect == 'internal' && !elFinderVolumeDriver::$mimetypesLoaded) { |
| 1323 | // load mimes from external file for mimeDetect == 'internal' |
| 1324 | // based on Alexey Sukhotin idea and patch: http://elrte.org/redmine/issues/163 |
| 1325 | // file must be in file directory or in parent one |
| 1326 | elFinderVolumeDriver::loadMimeTypes(!empty($this->options['mimefile']) ? $this->options['mimefile'] : ''); |
| 1327 | } |
| 1328 | $this->rootName = empty($this->options['alias']) ? $this->basenameCE($this->root) : $this->options['alias']; |
| 1329 | |
| 1330 | // This get's triggered if $this->root == '/' and alias is empty. |
| 1331 | // Maybe modify _basename instead? |
| 1332 | if ($this->rootName === '') $this->rootName = $this->separator; |
| 1333 | |
| 1334 | $this->_checkArchivers(); |
| 1335 | |
| 1336 | $root = $this->stat($this->root); |
| 1337 | |
| 1338 | if (!$root) { |
| 1339 | return $this->setError('Root folder does not exist.'); |
| 1340 | } |
| 1341 | if (!$root['read'] && !$root['write']) { |
| 1342 | return $this->setError('Root folder has not read and write permissions.'); |
| 1343 | } |
| 1344 | |
| 1345 | if ($root['read']) { |
| 1346 | if ($argInit) { |
| 1347 | // check startPath - path to open by default instead of root |
| 1348 | $startPath = $this->options['startPath'] ? $this->normpathCE($this->options['startPath']) : ''; |
| 1349 | if ($startPath) { |
| 1350 | $start = $this->stat($startPath); |
| 1351 | if (!empty($start) |
| 1352 | && $start['mime'] == 'directory' |
| 1353 | && $start['read'] |
| 1354 | && empty($start['hidden']) |
| 1355 | && $this->inpathCE($startPath, $this->root)) { |
| 1356 | $this->startPath = $startPath; |
| 1357 | if (substr($this->startPath, -1, 1) == $this->options['separator']) { |
| 1358 | $this->startPath = substr($this->startPath, 0, -1); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | } else { |
| 1364 | $this->options['URL'] = ''; |
| 1365 | $this->options['tmbURL'] = ''; |
| 1366 | $this->options['tmbPath'] = ''; |
| 1367 | // read only volume |
| 1368 | array_unshift($this->attributes, array( |
| 1369 | 'pattern' => '/.*/', |
| 1370 | 'read' => false |
| 1371 | )); |
| 1372 | } |
| 1373 | $this->treeDeep = $this->options['treeDeep'] > 0 ? (int)$this->options['treeDeep'] : 1; |
| 1374 | $this->tmbSize = $this->options['tmbSize'] > 0 ? (int)$this->options['tmbSize'] : 48; |
| 1375 | $this->URL = $this->options['URL']; |
| 1376 | if ($this->URL && preg_match("|[^/?&=]$|", $this->URL)) { |
| 1377 | $this->URL .= '/'; |
| 1378 | } |
| 1379 | |
| 1380 | $dirUrlOwn = strtolower($this->options['dirUrlOwn']); |
| 1381 | if ($dirUrlOwn === 'auto') { |
| 1382 | $this->options['dirUrlOwn'] = $this->URL ? false : true; |
| 1383 | } else if ($dirUrlOwn === 'hide') { |
| 1384 | $this->options['dirUrlOwn'] = 'hide'; |
| 1385 | } else { |
| 1386 | $this->options['dirUrlOwn'] = (bool)$this->options['dirUrlOwn']; |
| 1387 | } |
| 1388 | |
| 1389 | $this->tmbURL = !empty($this->options['tmbURL']) ? $this->options['tmbURL'] : ''; |
| 1390 | if ($this->tmbURL && $this->tmbURL !== 'self' && preg_match("|[^/?&=]$|", $this->tmbURL)) { |
| 1391 | $this->tmbURL .= '/'; |
| 1392 | } |
| 1393 | |
| 1394 | $this->nameValidator = !empty($this->options['acceptedName']) && (is_string($this->options['acceptedName']) || is_callable($this->options['acceptedName'])) |
| 1395 | ? $this->options['acceptedName'] |
| 1396 | : ''; |
| 1397 | |
| 1398 | $this->dirnameValidator = !empty($this->options['acceptedDirname']) && (is_callable($this->options['acceptedDirname']) || (is_string($this->options['acceptedDirname']) && preg_match($this->options['acceptedDirname'], '') !== false)) |
| 1399 | ? $this->options['acceptedDirname'] |
| 1400 | : $this->nameValidator; |
| 1401 | |
| 1402 | // enabling archivers['create'] with options['useRemoteArchive'] |
| 1403 | if ($this->options['useRemoteArchive'] && empty($this->archivers['create']) && $this->getTempPath()) { |
| 1404 | $_archivers = $this->getArchivers(); |
| 1405 | $this->archivers['create'] = $_archivers['create']; |
| 1406 | } |
| 1407 | |
| 1408 | // manual control archive types to create |
| 1409 | if (!empty($this->options['archiveMimes']) && is_array($this->options['archiveMimes'])) { |
| 1410 | foreach ($this->archivers['create'] as $mime => $v) { |
| 1411 | if (!in_array($mime, $this->options['archiveMimes'])) { |
| 1412 | unset($this->archivers['create'][$mime]); |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | // manualy add archivers |
| 1418 | if (!empty($this->options['archivers']['create']) && is_array($this->options['archivers']['create'])) { |
| 1419 | foreach ($this->options['archivers']['create'] as $mime => $conf) { |
| 1420 | if (strpos($mime, 'application/') === 0 |
| 1421 | && !empty($conf['cmd']) |
| 1422 | && isset($conf['argc']) |
| 1423 | && !empty($conf['ext']) |
| 1424 | && !isset($this->archivers['create'][$mime])) { |
| 1425 | $this->archivers['create'][$mime] = $conf; |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | if (!empty($this->options['archivers']['extract']) && is_array($this->options['archivers']['extract'])) { |
| 1431 | foreach ($this->options['archivers']['extract'] as $mime => $conf) { |
| 1432 | if (strpos($mime, 'application/') === 0 |
| 1433 | && !empty($conf['cmd']) |
| 1434 | && isset($conf['argc']) |
| 1435 | && !empty($conf['ext']) |
| 1436 | && !isset($this->archivers['extract'][$mime])) { |
| 1437 | $this->archivers['extract'][$mime] = $conf; |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | if (!empty($this->options['noSessionCache']) && is_array($this->options['noSessionCache'])) { |
| 1443 | foreach ($this->options['noSessionCache'] as $_key) { |
| 1444 | $this->sessionCaching[$_key] = false; |
| 1445 | unset($this->sessionCache[$_key]); |
| 1446 | } |
| 1447 | } |
| 1448 | if ($this->sessionCaching['subdirs']) { |
| 1449 | if (!isset($this->sessionCache['subdirs'])) { |
| 1450 | $this->sessionCache['subdirs'] = array(); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | $this->configure(); |
| 1456 | |
| 1457 | // Normalize disabled (array_merge`for type array of JSON) |
| 1458 | $this->disabled = array_values(array_unique($this->disabled)); |
| 1459 | |
| 1460 | // fix sync interval |
| 1461 | if ($this->options['syncMinMs'] !== 0) { |
| 1462 | $this->options['syncMinMs'] = max($this->options[$this->options['syncChkAsTs'] ? 'tsPlSleep' : 'lsPlSleep'] * 1000, intval($this->options['syncMinMs'])); |
| 1463 | } |
| 1464 | |
| 1465 | // ` copyJoin` is required for the trash function |
| 1466 | if ($this->options['trashHash'] && empty($this->options['copyJoin'])) { |
| 1467 | $this->options['trashHash'] = ''; |
| 1468 | } |
| 1469 | |
| 1470 | // set tmpLinkPath |
| 1471 | if (elFinder::$tmpLinkPath && !$this->options['tmpLinkPath']) { |
| 1472 | if (is_writeable(elFinder::$tmpLinkPath)) { |
| 1473 | $this->options['tmpLinkPath'] = elFinder::$tmpLinkPath; |
| 1474 | } else { |
| 1475 | elFinder::$tmpLinkPath = ''; |
| 1476 | } |
| 1477 | } |
| 1478 | if ($this->options['tmpLinkPath'] && is_writable($this->options['tmpLinkPath'])) { |
| 1479 | $this->tmpLinkPath = realpath($this->options['tmpLinkPath']); |
| 1480 | } else if (!$this->tmpLinkPath && $this->tmbURL && $this->tmbPath) { |
| 1481 | $this->tmpLinkPath = $this->tmbPath; |
| 1482 | $this->options['tmpLinkUrl'] = $this->tmbURL; |
| 1483 | } else if (!$this->options['URL'] && is_writable('../files/.tmb')) { |
| 1484 | $this->tmpLinkPath = realpath('../files/.tmb'); |
| 1485 | $this->options['tmpLinkUrl'] = ''; |
| 1486 | if (!elFinder::$tmpLinkPath) { |
| 1487 | elFinder::$tmpLinkPath = $this->tmpLinkPath; |
| 1488 | elFinder::$tmpLinkUrl = ''; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | // set tmpLinkUrl |
| 1493 | if (elFinder::$tmpLinkUrl && !$this->options['tmpLinkUrl']) { |
| 1494 | $this->options['tmpLinkUrl'] = elFinder::$tmpLinkUrl; |
| 1495 | } |
| 1496 | if ($this->options['tmpLinkUrl']) { |
| 1497 | $this->tmpLinkUrl = $this->options['tmpLinkUrl']; |
| 1498 | } |
| 1499 | if ($this->tmpLinkPath && !$this->tmpLinkUrl) { |
| 1500 | $cur = realpath('./'); |
| 1501 | $i = 0; |
| 1502 | while ($cur !== $this->systemRoot && strpos($this->tmpLinkPath, $cur) !== 0) { |
| 1503 | $i++; |
| 1504 | $cur = dirname($cur); |
| 1505 | } |
| 1506 | list($req) = explode('?', $_SERVER['REQUEST_URI']); |
| 1507 | $reqs = explode('/', dirname($req)); |
| 1508 | $uri = join('/', array_slice($reqs, 0, count($reqs) - 1)) . substr($this->tmpLinkPath, strlen($cur)); |
| 1509 | $https = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'); |
| 1510 | $this->tmpLinkUrl = ($https ? 'https://' : 'http://') |
| 1511 | . $_SERVER['SERVER_NAME'] // host |
| 1512 | . (((!$https && $_SERVER['SERVER_PORT'] == 80) || ($https && $_SERVER['SERVER_PORT'] == 443)) ? '' : (':' . $_SERVER['SERVER_PORT'])) // port |
| 1513 | . $uri; |
| 1514 | if (!elFinder::$tmpLinkUrl) { |
| 1515 | elFinder::$tmpLinkUrl = $this->tmpLinkUrl; |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | // remove last '/' |
| 1520 | if ($this->tmpLinkPath) { |
| 1521 | $this->tmpLinkPath = rtrim($this->tmpLinkPath, '/'); |
| 1522 | } |
| 1523 | if ($this->tmpLinkUrl) { |
| 1524 | $this->tmpLinkUrl = rtrim($this->tmpLinkUrl, '/'); |
| 1525 | } |
| 1526 | |
| 1527 | // to update options cache |
| 1528 | if (isset($this->sessionCache['rootstat'])) { |
| 1529 | unset($this->sessionCache['rootstat'][$this->getRootstatCachekey()]); |
| 1530 | } |
| 1531 | $this->updateCache($this->root, $root); |
| 1532 | |
| 1533 | return $this->mounted = true; |
| 1534 | } |
| 1535 | |
| 1536 | /** |
| 1537 | * Some "unmount" stuffs - may be required by virtual fs |
| 1538 | * |
| 1539 | * @return void |
| 1540 | * @author Dmitry (dio) Levashov |
| 1541 | **/ |
| 1542 | public function umount() |
| 1543 | { |
| 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * Remove session cache of this volume |
| 1548 | */ |
| 1549 | public function clearSessionCache() |
| 1550 | { |
| 1551 | $this->sessionCache = array(); |
| 1552 | } |
| 1553 | |
| 1554 | /** |
| 1555 | * Return error message from last failed action |
| 1556 | * |
| 1557 | * @return array |
| 1558 | * @author Dmitry (dio) Levashov |
| 1559 | **/ |
| 1560 | public function error() |
| 1561 | { |
| 1562 | return $this->error; |
| 1563 | } |
| 1564 | |
| 1565 | /** |
| 1566 | * Return is uploadable that given file name |
| 1567 | * |
| 1568 | * @param string $name file name |
| 1569 | * @param bool $allowUnknown |
| 1570 | * |
| 1571 | * @return bool |
| 1572 | * @author Naoki Sawada |
| 1573 | **/ |
| 1574 | public function isUploadableByName($name, $allowUnknown = false) |
| 1575 | { |
| 1576 | $mimeByName = $this->mimetype($name, true); |
| 1577 | return (($allowUnknown && $mimeByName === 'unknown') || $this->allowPutMime($mimeByName)); |
| 1578 | } |
| 1579 | |
| 1580 | /** |
| 1581 | * Return Extention/MIME Table (elFinderVolumeDriver::$mimetypes) |
| 1582 | * |
| 1583 | * @return array |
| 1584 | * @author Naoki Sawada |
| 1585 | */ |
| 1586 | public function getMimeTable() |
| 1587 | { |
| 1588 | // load mime.types |
| 1589 | if (!elFinderVolumeDriver::$mimetypesLoaded) { |
| 1590 | elFinderVolumeDriver::loadMimeTypes(); |
| 1591 | } |
| 1592 | return elFinderVolumeDriver::$mimetypes; |
| 1593 | } |
| 1594 | |
| 1595 | /** |
| 1596 | * Return file extention detected by MIME type |
| 1597 | * |
| 1598 | * @param string $mime MIME type |
| 1599 | * @param string $suffix Additional suffix |
| 1600 | * |
| 1601 | * @return string |
| 1602 | * @author Naoki Sawada |
| 1603 | */ |
| 1604 | public function getExtentionByMime($mime, $suffix = '') |
| 1605 | { |
| 1606 | static $extTable = null; |
| 1607 | |
| 1608 | if (is_null($extTable)) { |
| 1609 | $extTable = array_flip(array_unique($this->getMimeTable())); |
| 1610 | foreach ($this->options['mimeMap'] as $pair => $_mime) { |
| 1611 | list($ext) = explode(':', $pair); |
| 1612 | if ($ext !== '*' && !isset($extTable[$_mime])) { |
| 1613 | $extTable[$_mime] = $ext; |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | if ($mime && isset($extTable[$mime])) { |
| 1619 | return $suffix ? ($extTable[$mime] . $suffix) : $extTable[$mime]; |
| 1620 | } |
| 1621 | return ''; |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * Set mimetypes allowed to display to client |
| 1626 | * |
| 1627 | * @param array $mimes |
| 1628 | * |
| 1629 | * @return void |
| 1630 | * @author Dmitry (dio) Levashov |
| 1631 | **/ |
| 1632 | public function setMimesFilter($mimes) |
| 1633 | { |
| 1634 | if (is_array($mimes)) { |
| 1635 | $this->onlyMimes = $mimes; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | /** |
| 1640 | * Return root folder hash |
| 1641 | * |
| 1642 | * @return string |
| 1643 | * @author Dmitry (dio) Levashov |
| 1644 | **/ |
| 1645 | public function root() |
| 1646 | { |
| 1647 | return $this->encode($this->root); |
| 1648 | } |
| 1649 | |
| 1650 | /** |
| 1651 | * Return root path |
| 1652 | * |
| 1653 | * @return string |
| 1654 | * @author Naoki Sawada |
| 1655 | **/ |
| 1656 | public function getRootPath() |
| 1657 | { |
| 1658 | return $this->root; |
| 1659 | } |
| 1660 | |
| 1661 | /** |
| 1662 | * Return target path hash |
| 1663 | * |
| 1664 | * @param string $path |
| 1665 | * @param string $name |
| 1666 | * |
| 1667 | * @author Naoki Sawada |
| 1668 | * @return string |
| 1669 | */ |
| 1670 | public function getHash($path, $name = '') |
| 1671 | { |
| 1672 | if ($name !== '') { |
| 1673 | $path = $this->joinPathCE($path, $name); |
| 1674 | } |
| 1675 | return $this->encode($path); |
| 1676 | } |
| 1677 | |
| 1678 | /** |
| 1679 | * Return decoded path of target hash |
| 1680 | * This method do not check the stat of target |
| 1681 | * Use method `realpath()` to do check of the stat of target |
| 1682 | * |
| 1683 | * @param string $hash |
| 1684 | * |
| 1685 | * @author Naoki Sawada |
| 1686 | * @return string |
| 1687 | */ |
| 1688 | public function getPath($hash) |
| 1689 | { |
| 1690 | return $this->decode($hash); |
| 1691 | } |
| 1692 | |
| 1693 | /** |
| 1694 | * Return root or startPath hash |
| 1695 | * |
| 1696 | * @return string |
| 1697 | * @author Dmitry (dio) Levashov |
| 1698 | **/ |
| 1699 | public function defaultPath() |
| 1700 | { |
| 1701 | return $this->encode($this->startPath ? $this->startPath : $this->root); |
| 1702 | } |
| 1703 | |
| 1704 | /** |
| 1705 | * Return volume options required by client: |
| 1706 | * |
| 1707 | * @param $hash |
| 1708 | * |
| 1709 | * @return array |
| 1710 | * @author Dmitry (dio) Levashov |
| 1711 | */ |
| 1712 | public function options($hash) |
| 1713 | { |
| 1714 | $create = $createext = array(); |
| 1715 | if (isset($this->archivers['create']) && is_array($this->archivers['create'])) { |
| 1716 | foreach ($this->archivers['create'] as $m => $v) { |
| 1717 | $create[] = $m; |
| 1718 | $createext[$m] = $v['ext']; |
| 1719 | } |
| 1720 | } |
| 1721 | $opts = array( |
| 1722 | 'path' => $hash ? $this->path($hash) : '', |
| 1723 | 'url' => $this->URL, |
| 1724 | 'tmbUrl' => (!$this->imgLib && $this->options['tmbFbSelf']) ? 'self' : $this->tmbURL, |
| 1725 | 'disabled' => $this->disabled, |
| 1726 | 'separator' => $this->separator, |
| 1727 | 'copyOverwrite' => intval($this->options['copyOverwrite']), |
| 1728 | 'uploadOverwrite' => intval($this->options['uploadOverwrite']), |
| 1729 | 'uploadMaxSize' => intval($this->uploadMaxSize), |
| 1730 | 'uploadMaxConn' => intval($this->options['uploadMaxConn']), |
| 1731 | 'uploadMime' => array( |
| 1732 | 'firstOrder' => isset($this->uploadOrder[0]) ? $this->uploadOrder[0] : 'deny', |
| 1733 | 'allow' => $this->uploadAllow, |
| 1734 | 'deny' => $this->uploadDeny |
| 1735 | ), |
| 1736 | 'dispInlineRegex' => $this->options['dispInlineRegex'], |
| 1737 | 'jpgQuality' => intval($this->options['jpgQuality']), |
| 1738 | 'archivers' => array( |
| 1739 | 'create' => $create, |
| 1740 | 'extract' => isset($this->archivers['extract']) && is_array($this->archivers['extract']) ? array_keys($this->archivers['extract']) : array(), |
| 1741 | 'createext' => $createext |
| 1742 | ), |
| 1743 | 'uiCmdMap' => (isset($this->options['uiCmdMap']) && is_array($this->options['uiCmdMap'])) ? $this->options['uiCmdMap'] : array(), |
| 1744 | 'syncChkAsTs' => intval($this->options['syncChkAsTs']), |
| 1745 | 'syncMinMs' => intval($this->options['syncMinMs']), |
| 1746 | 'i18nFolderName' => intval($this->options['i18nFolderName']), |
| 1747 | 'tmbCrop' => intval($this->options['tmbCrop']), |
| 1748 | 'tmbReqCustomData' => (bool)$this->options['tmbReqCustomData'], |
| 1749 | 'substituteImg' => (bool)$this->options['substituteImg'], |
| 1750 | 'onetimeUrl' => (bool)$this->options['onetimeUrl'], |
| 1751 | ); |
| 1752 | if (!empty($this->options['trashHash'])) { |
| 1753 | $opts['trashHash'] = $this->options['trashHash']; |
| 1754 | } |
| 1755 | if ($hash === null) { |
| 1756 | // call from getRootStatExtra() |
| 1757 | if (!empty($this->options['icon'])) { |
| 1758 | $opts['icon'] = $this->options['icon']; |
| 1759 | } |
| 1760 | if (!empty($this->options['rootCssClass'])) { |
| 1761 | $opts['csscls'] = $this->options['rootCssClass']; |
| 1762 | } |
| 1763 | if (isset($this->options['netkey'])) { |
| 1764 | $opts['netkey'] = $this->options['netkey']; |
| 1765 | } |
| 1766 | } |
| 1767 | return $opts; |
| 1768 | } |
| 1769 | |
| 1770 | /** |
| 1771 | * Get option value of this volume |
| 1772 | * |
| 1773 | * @param string $name target option name |
| 1774 | * |
| 1775 | * @return NULL|mixed target option value |
| 1776 | * @author Naoki Sawada |
| 1777 | */ |
| 1778 | public function getOption($name) |
| 1779 | { |
| 1780 | return isset($this->options[$name]) ? $this->options[$name] : null; |
| 1781 | } |
| 1782 | |
| 1783 | /** |
| 1784 | * Get plugin values of this options |
| 1785 | * |
| 1786 | * @param string $name Plugin name |
| 1787 | * |
| 1788 | * @return NULL|array Plugin values |
| 1789 | * @author Naoki Sawada |
| 1790 | */ |
| 1791 | public function getOptionsPlugin($name = '') |
| 1792 | { |
| 1793 | if ($name) { |
| 1794 | return isset($this->options['plugin'][$name]) ? $this->options['plugin'][$name] : array(); |
| 1795 | } else { |
| 1796 | return $this->options['plugin']; |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /** |
| 1801 | * Return true if command disabled in options |
| 1802 | * |
| 1803 | * @param string $cmd command name |
| 1804 | * |
| 1805 | * @return bool |
| 1806 | * @author Dmitry (dio) Levashov |
| 1807 | **/ |
| 1808 | public function commandDisabled($cmd) |
| 1809 | { |
| 1810 | return in_array($cmd, $this->disabled); |
| 1811 | } |
| 1812 | |
| 1813 | /** |
| 1814 | * Return true if mime is required mimes list |
| 1815 | * |
| 1816 | * @param string $mime mime type to check |
| 1817 | * @param array $mimes allowed mime types list or not set to use client mimes list |
| 1818 | * @param bool|null $empty what to return on empty list |
| 1819 | * |
| 1820 | * @return bool|null |
| 1821 | * @author Dmitry (dio) Levashov |
| 1822 | * @author Troex Nevelin |
| 1823 | **/ |
| 1824 | public function mimeAccepted($mime, $mimes = null, $empty = true) |
| 1825 | { |
| 1826 | $mimes = is_array($mimes) ? $mimes : $this->onlyMimes; |
| 1827 | if (empty($mimes)) { |
| 1828 | return $empty; |
| 1829 | } |
| 1830 | return $mime == 'directory' |
| 1831 | || in_array('all', $mimes) |
| 1832 | || in_array('All', $mimes) |
| 1833 | || in_array($mime, $mimes) |
| 1834 | || in_array(substr($mime, 0, strpos($mime, '/')), $mimes); |
| 1835 | } |
| 1836 | |
| 1837 | /** |
| 1838 | * Return true if voume is readable. |
| 1839 | * |
| 1840 | * @return bool |
| 1841 | * @author Dmitry (dio) Levashov |
| 1842 | **/ |
| 1843 | public function isReadable() |
| 1844 | { |
| 1845 | $stat = $this->stat($this->root); |
| 1846 | return $stat['read']; |
| 1847 | } |
| 1848 | |
| 1849 | /** |
| 1850 | * Return true if copy from this volume allowed |
| 1851 | * |
| 1852 | * @return bool |
| 1853 | * @author Dmitry (dio) Levashov |
| 1854 | **/ |
| 1855 | public function copyFromAllowed() |
| 1856 | { |
| 1857 | return !!$this->options['copyFrom']; |
| 1858 | } |
| 1859 | |
| 1860 | /** |
| 1861 | * Return file path related to root with convert encoging |
| 1862 | * |
| 1863 | * @param string $hash file hash |
| 1864 | * |
| 1865 | * @return string |
| 1866 | * @author Dmitry (dio) Levashov |
| 1867 | **/ |
| 1868 | public function path($hash) |
| 1869 | { |
| 1870 | return $this->convEncOut($this->_path($this->convEncIn($this->decode($hash)))); |
| 1871 | } |
| 1872 | |
| 1873 | /** |
| 1874 | * Return file real path if file exists |
| 1875 | * |
| 1876 | * @param string $hash file hash |
| 1877 | * |
| 1878 | * @return string | false |
| 1879 | * @author Dmitry (dio) Levashov |
| 1880 | **/ |
| 1881 | public function realpath($hash) |
| 1882 | { |
| 1883 | $path = $this->decode($hash); |
| 1884 | return $this->stat($path) ? $path : false; |
| 1885 | } |
| 1886 | |
| 1887 | /** |
| 1888 | * Return list of moved/overwrited files |
| 1889 | * |
| 1890 | * @return array |
| 1891 | * @author Dmitry (dio) Levashov |
| 1892 | **/ |
| 1893 | public function removed() |
| 1894 | { |
| 1895 | if ($this->removed) { |
| 1896 | $unsetSubdir = isset($this->sessionCache['subdirs']) ? true : false; |
| 1897 | foreach ($this->removed as $item) { |
| 1898 | if ($item['mime'] === 'directory') { |
| 1899 | $path = $this->decode($item['hash']); |
| 1900 | if ($unsetSubdir) { |
| 1901 | unset($this->sessionCache['subdirs'][$path]); |
| 1902 | } |
| 1903 | if ($item['phash'] !== '') { |
| 1904 | $parent = $this->decode($item['phash']); |
| 1905 | unset($this->cache[$parent]); |
| 1906 | if ($this->root === $parent) { |
| 1907 | $this->sessionCache['rootstat'] = array(); |
| 1908 | } |
| 1909 | if ($unsetSubdir) { |
| 1910 | unset($this->sessionCache['subdirs'][$parent]); |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | } |
| 1915 | $this->removed = array_values($this->removed); |
| 1916 | } |
| 1917 | return $this->removed; |
| 1918 | } |
| 1919 | |
| 1920 | /** |
| 1921 | * Return list of added files |
| 1922 | * |
| 1923 | * @deprecated |
| 1924 | * @return array |
| 1925 | * @author Naoki Sawada |
| 1926 | **/ |
| 1927 | public function added() |
| 1928 | { |
| 1929 | return $this->added; |
| 1930 | } |
| 1931 | |
| 1932 | /** |
| 1933 | * Clean removed files list |
| 1934 | * |
| 1935 | * @return void |
| 1936 | * @author Dmitry (dio) Levashov |
| 1937 | **/ |
| 1938 | public function resetRemoved() |
| 1939 | { |
| 1940 | $this->resetResultStat(); |
| 1941 | } |
| 1942 | |
| 1943 | /** |
| 1944 | * Clean added/removed files list |
| 1945 | * |
| 1946 | * @return void |
| 1947 | **/ |
| 1948 | public function resetResultStat() |
| 1949 | { |
| 1950 | $this->removed = array(); |
| 1951 | $this->added = array(); |
| 1952 | } |
| 1953 | |
| 1954 | /** |
| 1955 | * Return file/dir hash or first founded child hash with required attr == $val |
| 1956 | * |
| 1957 | * @param string $hash file hash |
| 1958 | * @param string $attr attribute name |
| 1959 | * @param bool $val attribute value |
| 1960 | * |
| 1961 | * @return string|false |
| 1962 | * @author Dmitry (dio) Levashov |
| 1963 | **/ |
| 1964 | public function closest($hash, $attr, $val) |
| 1965 | { |
| 1966 | return ($path = $this->closestByAttr($this->decode($hash), $attr, $val)) ? $this->encode($path) : false; |
| 1967 | } |
| 1968 | |
| 1969 | /** |
| 1970 | * Return file info or false on error |
| 1971 | * |
| 1972 | * @param string $hash file hash |
| 1973 | * |
| 1974 | * @return array|false |
| 1975 | * @internal param bool $realpath add realpath field to file info |
| 1976 | * @author Dmitry (dio) Levashov |
| 1977 | */ |
| 1978 | public function file($hash) |
| 1979 | { |
| 1980 | $file = $this->stat($this->decode($hash)); |
| 1981 | |
| 1982 | return ($file) ? $file : $this->setError(elFinder::ERROR_FILE_NOT_FOUND); |
| 1983 | } |
| 1984 | |
| 1985 | /** |
| 1986 | * Return folder info |
| 1987 | * |
| 1988 | * @param string $hash folder hash |
| 1989 | * @param bool $resolveLink |
| 1990 | * |
| 1991 | * @return array|false |
| 1992 | * @internal param bool $hidden return hidden file info |
| 1993 | * @author Dmitry (dio) Levashov |
| 1994 | */ |
| 1995 | public function dir($hash, $resolveLink = false) |
| 1996 | { |
| 1997 | if (($dir = $this->file($hash)) == false) { |
| 1998 | return $this->setError(elFinder::ERROR_DIR_NOT_FOUND); |
| 1999 | } |
| 2000 | |
| 2001 | if ($resolveLink && !empty($dir['thash'])) { |
| 2002 | $dir = $this->file($dir['thash']); |
| 2003 | } |
| 2004 | |
| 2005 | return $dir && $dir['mime'] == 'directory' && empty($dir['hidden']) |
| 2006 | ? $dir |
| 2007 | : $this->setError(elFinder::ERROR_NOT_DIR); |
| 2008 | } |
| 2009 | |
| 2010 | /** |
| 2011 | * Return directory content or false on error |
| 2012 | * |
| 2013 | * @param string $hash file hash |
| 2014 | * |
| 2015 | * @return array|false |
| 2016 | * @author Dmitry (dio) Levashov |
| 2017 | **/ |
| 2018 | public function scandir($hash) |
| 2019 | { |
| 2020 | if (($dir = $this->dir($hash)) == false) { |
| 2021 | return false; |
| 2022 | } |
| 2023 | |
| 2024 | $path = $this->decode($hash); |
| 2025 | if ($res = $dir['read'] |
| 2026 | ? $this->getScandir($path) |
| 2027 | : $this->setError(elFinder::ERROR_PERM_DENIED)) { |
| 2028 | |
| 2029 | $dirs = null; |
| 2030 | if ($this->sessionCaching['subdirs'] && isset($this->sessionCache['subdirs'][$path])) { |
| 2031 | $dirs = $this->sessionCache['subdirs'][$path]; |
| 2032 | } |
| 2033 | if ($dirs !== null || (isset($dir['dirs']) && $dir['dirs'] != 1)) { |
| 2034 | $_dir = $dir; |
| 2035 | if ($dirs || $this->subdirs($hash)) { |
| 2036 | $dir['dirs'] = 1; |
| 2037 | } else { |
| 2038 | unset($dir['dirs']); |
| 2039 | } |
| 2040 | if ($dir !== $_dir) { |
| 2041 | $this->updateCache($path, $dir); |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | return $res; |
| 2047 | } |
| 2048 | |
| 2049 | /** |
| 2050 | * Return dir files names list |
| 2051 | * |
| 2052 | * @param string $hash file hash |
| 2053 | * @param null $intersect |
| 2054 | * |
| 2055 | * @return array|false |
| 2056 | * @author Dmitry (dio) Levashov |
| 2057 | */ |
| 2058 | public function ls($hash, $intersect = null) |
| 2059 | { |
| 2060 | if (($dir = $this->dir($hash)) == false || !$dir['read']) { |
| 2061 | return false; |
| 2062 | } |
| 2063 | |
| 2064 | $list = array(); |
| 2065 | $path = $this->decode($hash); |
| 2066 | |
| 2067 | $check = array(); |
| 2068 | if ($intersect) { |
| 2069 | $check = array_flip($intersect); |
| 2070 | } |
| 2071 | |
| 2072 | foreach ($this->getScandir($path) as $stat) { |
| 2073 | if (empty($stat['hidden']) && (!$check || isset($check[$stat['name']])) && $this->mimeAccepted($stat['mime'])) { |
| 2074 | $list[$stat['hash']] = $stat['name']; |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | return $list; |
| 2079 | } |
| 2080 | |
| 2081 | /** |
| 2082 | * Return subfolders for required folder or false on error |
| 2083 | * |
| 2084 | * @param string $hash folder hash or empty string to get tree from root folder |
| 2085 | * @param int $deep subdir deep |
| 2086 | * @param string $exclude dir hash which subfolders must be exluded from result, required to not get stat twice on cwd subfolders |
| 2087 | * |
| 2088 | * @return array|false |
| 2089 | * @author Dmitry (dio) Levashov |
| 2090 | **/ |
| 2091 | public function tree($hash = '', $deep = 0, $exclude = '') |
| 2092 | { |
| 2093 | $path = $hash ? $this->decode($hash) : $this->root; |
| 2094 | |
| 2095 | if (($dir = $this->stat($path)) == false || $dir['mime'] != 'directory') { |
| 2096 | return false; |
| 2097 | } |
| 2098 | |
| 2099 | $dirs = $this->gettree($path, $deep > 0 ? $deep - 1 : $this->treeDeep - 1, $exclude ? $this->decode($exclude) : null); |
| 2100 | array_unshift($dirs, $dir); |
| 2101 | return $dirs; |
| 2102 | } |
| 2103 | |
| 2104 | /** |
| 2105 | * Return part of dirs tree from required dir up to root dir |
| 2106 | * |
| 2107 | * @param string $hash directory hash |
| 2108 | * @param bool|null $lineal only lineal parents |
| 2109 | * |
| 2110 | * @return array|false |
| 2111 | * @throws elFinderAbortException |
| 2112 | * @author Dmitry (dio) Levashov |
| 2113 | */ |
| 2114 | public function parents($hash, $lineal = false) |
| 2115 | { |
| 2116 | if (($current = $this->dir($hash)) == false) { |
| 2117 | return false; |
| 2118 | } |
| 2119 | |
| 2120 | $args = func_get_args(); |
| 2121 | // checks 3rd param `$until` (elFinder >= 2.1.24) |
| 2122 | $until = ''; |
| 2123 | if (isset($args[2])) { |
| 2124 | $until = $args[2]; |
| 2125 | } |
| 2126 | |
| 2127 | $path = $this->decode($hash); |
| 2128 | $tree = array(); |
| 2129 | |
| 2130 | while ($path && $path != $this->root) { |
| 2131 | elFinder::checkAborted(); |
| 2132 | $path = $this->dirnameCE($path); |
| 2133 | if (!($stat = $this->stat($path)) || !empty($stat['hidden']) || !$stat['read']) { |
| 2134 | return false; |
| 2135 | } |
| 2136 | |
| 2137 | array_unshift($tree, $stat); |
| 2138 | if (!$lineal) { |
| 2139 | foreach ($this->gettree($path, 0) as $dir) { |
| 2140 | elFinder::checkAborted(); |
| 2141 | if (!isset($tree[$dir['hash']])) { |
| 2142 | $tree[$dir['hash']] = $dir; |
| 2143 | } |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | if ($until && $until === $this->encode($path)) { |
| 2148 | break; |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | return $tree ? array_values($tree) : array($current); |
| 2153 | } |
| 2154 | |
| 2155 | /** |
| 2156 | * Create thumbnail for required file and return its name or false on failed |
| 2157 | * |
| 2158 | * @param $hash |
| 2159 | * |
| 2160 | * @return false|string |
| 2161 | * @throws ImagickException |
| 2162 | * @throws elFinderAbortException |
| 2163 | * @author Dmitry (dio) Levashov |
| 2164 | */ |
| 2165 | public function tmb($hash) |
| 2166 | { |
| 2167 | $path = $this->decode($hash); |
| 2168 | $stat = $this->stat($path); |
| 2169 | |
| 2170 | if (isset($stat['tmb'])) { |
| 2171 | $res = $stat['tmb'] == "1" ? $this->createTmb($path, $stat) : $stat['tmb']; |
| 2172 | if (!$res) { |
| 2173 | list($type) = explode('/', $stat['mime']); |
| 2174 | $fallback = $this->options['resourcePath'] . DIRECTORY_SEPARATOR . strtolower($type) . '.png'; |
| 2175 | if (is_file($fallback)) { |
| 2176 | $res = $this->tmbname($stat); |
| 2177 | if (!copy($fallback, $this->tmbPath . DIRECTORY_SEPARATOR . $res)) { |
| 2178 | $res = false; |
| 2179 | } |
| 2180 | } |
| 2181 | } |
| 2182 | // tmb garbage collection |
| 2183 | if ($res && $this->options['tmbGcMaxlifeHour'] && $this->options['tmbGcPercentage'] > 0) { |
| 2184 | $rand = mt_rand(1, 10000); |
| 2185 | if ($rand <= $this->options['tmbGcPercentage'] * 100) { |
| 2186 | register_shutdown_function(array('elFinder', 'GlobGC'), $this->tmbPath . DIRECTORY_SEPARATOR . '*.png', $this->options['tmbGcMaxlifeHour'] * 3600); |
| 2187 | } |
| 2188 | } |
| 2189 | return $res; |
| 2190 | } |
| 2191 | return false; |
| 2192 | } |
| 2193 | |
| 2194 | /** |
| 2195 | * Return file size / total directory size |
| 2196 | * |
| 2197 | * @param string file hash |
| 2198 | * |
| 2199 | * @return array |
| 2200 | * @throws elFinderAbortException |
| 2201 | * @author Dmitry (dio) Levashov |
| 2202 | */ |
| 2203 | public function size($hash) |
| 2204 | { |
| 2205 | return $this->countSize($this->decode($hash)); |
| 2206 | } |
| 2207 | |
| 2208 | /** |
| 2209 | * Open file for reading and return file pointer |
| 2210 | * |
| 2211 | * @param string file hash |
| 2212 | * |
| 2213 | * @return Resource|false |
| 2214 | * @author Dmitry (dio) Levashov |
| 2215 | **/ |
| 2216 | public function open($hash) |
| 2217 | { |
| 2218 | if (($file = $this->file($hash)) == false |
| 2219 | || $file['mime'] == 'directory') { |
| 2220 | return false; |
| 2221 | } |
| 2222 | // check extra option for network stream pointer |
| 2223 | if (func_num_args() > 1) { |
| 2224 | $opts = func_get_arg(1); |
| 2225 | } else { |
| 2226 | $opts = array(); |
| 2227 | } |
| 2228 | return $this->fopenCE($this->decode($hash), 'rb', $opts); |
| 2229 | } |
| 2230 | |
| 2231 | /** |
| 2232 | * Close file pointer |
| 2233 | * |
| 2234 | * @param Resource $fp file pointer |
| 2235 | * @param string $hash file hash |
| 2236 | * |
| 2237 | * @return void |
| 2238 | * @author Dmitry (dio) Levashov |
| 2239 | **/ |
| 2240 | public function close($fp, $hash) |
| 2241 | { |
| 2242 | $this->fcloseCE($fp, $this->decode($hash)); |
| 2243 | } |
| 2244 | |
| 2245 | /** |
| 2246 | * Create directory and return dir info |
| 2247 | * |
| 2248 | * @param string $dsthash destination directory hash |
| 2249 | * @param string $name directory name |
| 2250 | * |
| 2251 | * @return array|false |
| 2252 | * @author Dmitry (dio) Levashov |
| 2253 | **/ |
| 2254 | public function mkdir($dsthash, $name) |
| 2255 | { |
| 2256 | if ($this->commandDisabled('mkdir')) { |
| 2257 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2258 | } |
| 2259 | |
| 2260 | if (!$this->nameAccepted($name, true)) { |
| 2261 | return $this->setError(elFinder::ERROR_INVALID_DIRNAME); |
| 2262 | } |
| 2263 | |
| 2264 | if (($dir = $this->dir($dsthash)) == false) { |
| 2265 | return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dsthash); |
| 2266 | } |
| 2267 | |
| 2268 | $path = $this->decode($dsthash); |
| 2269 | |
| 2270 | if (!$dir['write'] || !$this->allowCreate($path, $name, true)) { |
| 2271 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2272 | } |
| 2273 | |
| 2274 | $dst = $this->joinPathCE($path, $name); |
| 2275 | $stat = $this->isNameExists($dst); |
| 2276 | if (!empty($stat)) { |
| 2277 | return $this->setError(elFinder::ERROR_EXISTS, $name); |
| 2278 | } |
| 2279 | $this->clearcache(); |
| 2280 | |
| 2281 | $mkpath = $this->convEncOut($this->_mkdir($this->convEncIn($path), $this->convEncIn($name))); |
| 2282 | if ($mkpath) { |
| 2283 | $this->clearstatcache(); |
| 2284 | $this->updateSubdirsCache($path, true); |
| 2285 | $this->updateSubdirsCache($mkpath, false); |
| 2286 | } |
| 2287 | |
| 2288 | return $mkpath ? $this->stat($mkpath) : false; |
| 2289 | } |
| 2290 | |
| 2291 | /** |
| 2292 | * Create empty file and return its info |
| 2293 | * |
| 2294 | * @param string $dst destination directory |
| 2295 | * @param string $name file name |
| 2296 | * |
| 2297 | * @return array|false |
| 2298 | * @author Dmitry (dio) Levashov |
| 2299 | **/ |
| 2300 | public function mkfile($dst, $name) |
| 2301 | { |
| 2302 | if ($this->commandDisabled('mkfile')) { |
| 2303 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2304 | } |
| 2305 | |
| 2306 | if (!$this->nameAccepted($name, false)) { |
| 2307 | return $this->setError(elFinder::ERROR_INVALID_NAME); |
| 2308 | } |
| 2309 | |
| 2310 | $mimeByName = $this->mimetype($name, true); |
| 2311 | if ($mimeByName && !$this->allowPutMime($mimeByName)) { |
| 2312 | return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, $name); |
| 2313 | } |
| 2314 | |
| 2315 | if (($dir = $this->dir($dst)) == false) { |
| 2316 | return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dst); |
| 2317 | } |
| 2318 | |
| 2319 | $path = $this->decode($dst); |
| 2320 | |
| 2321 | if (!$dir['write'] || !$this->allowCreate($path, $name, false)) { |
| 2322 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2323 | } |
| 2324 | |
| 2325 | if ($this->isNameExists($this->joinPathCE($path, $name))) { |
| 2326 | return $this->setError(elFinder::ERROR_EXISTS, $name); |
| 2327 | } |
| 2328 | |
| 2329 | $this->clearcache(); |
| 2330 | $res = false; |
| 2331 | if ($path = $this->convEncOut($this->_mkfile($this->convEncIn($path), $this->convEncIn($name)))) { |
| 2332 | $this->clearstatcache(); |
| 2333 | $res = $this->stat($path); |
| 2334 | } |
| 2335 | return $res; |
| 2336 | } |
| 2337 | |
| 2338 | /** |
| 2339 | * Rename file and return file info |
| 2340 | * |
| 2341 | * @param string $hash file hash |
| 2342 | * @param string $name new file name |
| 2343 | * |
| 2344 | * @return array|false |
| 2345 | * @throws elFinderAbortException |
| 2346 | * @author Dmitry (dio) Levashov |
| 2347 | */ |
| 2348 | public function rename($hash, $name) |
| 2349 | { |
| 2350 | if ($this->commandDisabled('rename')) { |
| 2351 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2352 | } |
| 2353 | |
| 2354 | if (!($file = $this->file($hash))) { |
| 2355 | return $this->setError(elFinder::ERROR_FILE_NOT_FOUND); |
| 2356 | } |
| 2357 | |
| 2358 | if ($name === $file['name']) { |
| 2359 | return $file; |
| 2360 | } |
| 2361 | |
| 2362 | if (!empty($this->options['netkey']) && !empty($file['isroot'])) { |
| 2363 | // change alias of netmount root |
| 2364 | $rootKey = $this->getRootstatCachekey(); |
| 2365 | // delete old cache data |
| 2366 | if ($this->sessionCaching['rootstat']) { |
| 2367 | unset($this->sessionCaching['rootstat'][$rootKey]); |
| 2368 | } |
| 2369 | if (elFinder::$instance->updateNetVolumeOption($this->options['netkey'], 'alias', $name)) { |
| 2370 | $this->clearcache(); |
| 2371 | $this->rootName = $this->options['alias'] = $name; |
| 2372 | return $this->stat($this->root); |
| 2373 | } else { |
| 2374 | return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, $name); |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | if (!empty($file['locked'])) { |
| 2379 | return $this->setError(elFinder::ERROR_LOCKED, $file['name']); |
| 2380 | } |
| 2381 | |
| 2382 | $isDir = ($file['mime'] === 'directory'); |
| 2383 | |
| 2384 | if (!$this->nameAccepted($name, $isDir)) { |
| 2385 | return $this->setError($isDir ? elFinder::ERROR_INVALID_DIRNAME : elFinder::ERROR_INVALID_NAME); |
| 2386 | } |
| 2387 | |
| 2388 | if (!$isDir) { |
| 2389 | $mimeByName = $this->mimetype($name, true); |
| 2390 | if ($mimeByName && !$this->allowPutMime($mimeByName)) { |
| 2391 | return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, $name); |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | $path = $this->decode($hash); |
| 2396 | $dir = $this->dirnameCE($path); |
| 2397 | $stat = $this->isNameExists($this->joinPathCE($dir, $name)); |
| 2398 | if ($stat) { |
| 2399 | return $this->setError(elFinder::ERROR_EXISTS, $name); |
| 2400 | } |
| 2401 | |
| 2402 | if (!$this->allowCreate($dir, $name, ($file['mime'] === 'directory'))) { |
| 2403 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2404 | } |
| 2405 | |
| 2406 | $this->rmTmb($file); // remove old name tmbs, we cannot do this after dir move |
| 2407 | |
| 2408 | |
| 2409 | if ($path = $this->convEncOut($this->_move($this->convEncIn($path), $this->convEncIn($dir), $this->convEncIn($name)))) { |
| 2410 | $this->clearcache(); |
| 2411 | return $this->stat($path); |
| 2412 | } |
| 2413 | return false; |
| 2414 | } |
| 2415 | |
| 2416 | /** |
| 2417 | * Create file copy with suffix "copy number" and return its info |
| 2418 | * |
| 2419 | * @param string $hash file hash |
| 2420 | * @param string $suffix suffix to add to file name |
| 2421 | * |
| 2422 | * @return array|false |
| 2423 | * @throws elFinderAbortException |
| 2424 | * @author Dmitry (dio) Levashov |
| 2425 | */ |
| 2426 | public function duplicate($hash, $suffix = 'copy') |
| 2427 | { |
| 2428 | if ($this->commandDisabled('duplicate')) { |
| 2429 | return $this->setError(elFinder::ERROR_COPY, '#' . $hash, elFinder::ERROR_PERM_DENIED); |
| 2430 | } |
| 2431 | |
| 2432 | if (($file = $this->file($hash)) == false) { |
| 2433 | return $this->setError(elFinder::ERROR_COPY, elFinder::ERROR_FILE_NOT_FOUND); |
| 2434 | } |
| 2435 | |
| 2436 | $path = $this->decode($hash); |
| 2437 | $dir = $this->dirnameCE($path); |
| 2438 | $name = $this->uniqueName($dir, $file['name'], sprintf($this->options['duplicateSuffix'], $suffix)); |
| 2439 | |
| 2440 | if (!$this->allowCreate($dir, $name, ($file['mime'] === 'directory'))) { |
| 2441 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2442 | } |
| 2443 | |
| 2444 | return ($path = $this->copy($path, $dir, $name)) == false |
| 2445 | ? false |
| 2446 | : $this->stat($path); |
| 2447 | } |
| 2448 | |
| 2449 | /** |
| 2450 | * Save uploaded file. |
| 2451 | * On success return array with new file stat and with removed file hash (if existed file was replaced) |
| 2452 | * |
| 2453 | * @param Resource $fp file pointer |
| 2454 | * @param string $dst destination folder hash |
| 2455 | * @param $name |
| 2456 | * @param string $tmpname file tmp name - required to detect mime type |
| 2457 | * @param array $hashes exists files hash array with filename as key |
| 2458 | * |
| 2459 | * @return array|false |
| 2460 | * @throws elFinderAbortException |
| 2461 | * @internal param string $src file name |
| 2462 | * @author Dmitry (dio) Levashov |
| 2463 | */ |
| 2464 | public function upload($fp, $dst, $name, $tmpname, $hashes = array()) |
| 2465 | { |
| 2466 | if ($this->commandDisabled('upload')) { |
| 2467 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2468 | } |
| 2469 | |
| 2470 | if (($dir = $this->dir($dst)) == false) { |
| 2471 | return $this->setError(elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dst); |
| 2472 | } |
| 2473 | |
| 2474 | if (empty($dir['write'])) { |
| 2475 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2476 | } |
| 2477 | |
| 2478 | if (!$this->nameAccepted($name, false)) { |
| 2479 | return $this->setError(elFinder::ERROR_INVALID_NAME); |
| 2480 | } |
| 2481 | |
| 2482 | $mimeByName = ''; |
| 2483 | if ($this->mimeDetect === 'internal') { |
| 2484 | $mime = $this->mimetype($tmpname, $name); |
| 2485 | } else { |
| 2486 | $mime = $this->mimetype($tmpname, $name); |
| 2487 | $mimeByName = $this->mimetype($name, true); |
| 2488 | if ($mime === 'unknown') { |
| 2489 | $mime = $mimeByName; |
| 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | if (!$this->allowPutMime($mime) || ($mimeByName && !$this->allowPutMime($mimeByName))) { |
| 2494 | // Log upload blocked event for debugging purposes |
| 2495 | error_log( |
| 2496 | 'elFinder upload blocked. ' |
| 2497 | . 'mime=' . $mime |
| 2498 | . ', mimeByName=' . $mimeByName |
| 2499 | . ', uploadAllow=' . json_encode($this->uploadAllow) |
| 2500 | . ', uploadDeny=' . json_encode($this->uploadDeny) |
| 2501 | ); |
| 2502 | |
| 2503 | return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME, '(' . $mime . ')'); |
| 2504 | } |
| 2505 | |
| 2506 | $tmpsize = (int)sprintf('%u', filesize($tmpname)); |
| 2507 | if ($this->uploadMaxSize > 0 && $tmpsize > $this->uploadMaxSize) { |
| 2508 | return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE); |
| 2509 | } |
| 2510 | |
| 2511 | $dstpath = $this->decode($dst); |
| 2512 | if (isset($hashes[$name])) { |
| 2513 | $test = $this->decode($hashes[$name]); |
| 2514 | $file = $this->stat($test); |
| 2515 | } else { |
| 2516 | $test = $this->joinPathCE($dstpath, $name); |
| 2517 | $file = $this->isNameExists($test); |
| 2518 | } |
| 2519 | |
| 2520 | $this->clearcache(); |
| 2521 | |
| 2522 | if ($file && $file['name'] === $name) { // file exists and check filename for item ID based filesystem |
| 2523 | if ($this->uploadOverwrite) { |
| 2524 | if (!$file['write']) { |
| 2525 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2526 | } elseif ($file['mime'] == 'directory') { |
| 2527 | return $this->setError(elFinder::ERROR_NOT_REPLACE, $name); |
| 2528 | } |
| 2529 | $this->remove($test); |
| 2530 | } else { |
| 2531 | $name = $this->uniqueName($dstpath, $name, '-', false); |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | $stat = array( |
| 2536 | 'mime' => $mime, |
| 2537 | 'width' => 0, |
| 2538 | 'height' => 0, |
| 2539 | 'size' => $tmpsize); |
| 2540 | |
| 2541 | // $w = $h = 0; |
| 2542 | if (strpos($mime, 'image') === 0 && ($s = getimagesize($tmpname))) { |
| 2543 | $stat['width'] = $s[0]; |
| 2544 | $stat['height'] = $s[1]; |
| 2545 | } |
| 2546 | // $this->clearcache(); |
| 2547 | if (($path = $this->saveCE($fp, $dstpath, $name, $stat)) == false) { |
| 2548 | return false; |
| 2549 | } |
| 2550 | |
| 2551 | $stat = $this->stat($path); |
| 2552 | // Try get URL |
| 2553 | if (empty($stat['url']) && ($url = $this->getContentUrl($stat['hash']))) { |
| 2554 | $stat['url'] = $url; |
| 2555 | } |
| 2556 | |
| 2557 | return $stat; |
| 2558 | } |
| 2559 | |
| 2560 | /** |
| 2561 | * Paste files |
| 2562 | * |
| 2563 | * @param Object $volume source volume |
| 2564 | * @param $src |
| 2565 | * @param string $dst destination dir hash |
| 2566 | * @param bool $rmSrc remove source after copy? |
| 2567 | * @param array $hashes |
| 2568 | * |
| 2569 | * @return array|false |
| 2570 | * @throws elFinderAbortException |
| 2571 | * @internal param string $source file hash |
| 2572 | * @author Dmitry (dio) Levashov |
| 2573 | */ |
| 2574 | public function paste($volume, $src, $dst, $rmSrc = false, $hashes = array()) |
| 2575 | { |
| 2576 | $err = $rmSrc ? elFinder::ERROR_MOVE : elFinder::ERROR_COPY; |
| 2577 | |
| 2578 | if ($this->commandDisabled('paste')) { |
| 2579 | return $this->setError($err, '#' . $src, elFinder::ERROR_PERM_DENIED); |
| 2580 | } |
| 2581 | |
| 2582 | if (($file = $volume->file($src, $rmSrc)) == false) { |
| 2583 | return $this->setError($err, '#' . $src, elFinder::ERROR_FILE_NOT_FOUND); |
| 2584 | } |
| 2585 | |
| 2586 | $name = $file['name']; |
| 2587 | $errpath = $volume->path($file['hash']); |
| 2588 | |
| 2589 | if (($dir = $this->dir($dst)) == false) { |
| 2590 | return $this->setError($err, $errpath, elFinder::ERROR_TRGDIR_NOT_FOUND, '#' . $dst); |
| 2591 | } |
| 2592 | |
| 2593 | if (!$dir['write'] || !$file['read']) { |
| 2594 | return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED); |
| 2595 | } |
| 2596 | |
| 2597 | $destination = $this->decode($dst); |
| 2598 | |
| 2599 | if (($test = $volume->closest($src, $rmSrc ? 'locked' : 'read', $rmSrc))) { |
| 2600 | return $rmSrc |
| 2601 | ? $this->setError($err, $errpath, elFinder::ERROR_LOCKED, $volume->path($test)) |
| 2602 | : $this->setError($err, $errpath, empty($file['thash']) ? elFinder::ERROR_PERM_DENIED : elFinder::ERROR_MKOUTLINK); |
| 2603 | } |
| 2604 | |
| 2605 | if (isset($hashes[$name])) { |
| 2606 | $test = $this->decode($hashes[$name]); |
| 2607 | $stat = $this->stat($test); |
| 2608 | } else { |
| 2609 | $test = $this->joinPathCE($destination, $name); |
| 2610 | $stat = $this->isNameExists($test); |
| 2611 | } |
| 2612 | $this->clearcache(); |
| 2613 | $dstDirExists = false; |
| 2614 | if ($stat && $stat['name'] === $name) { // file exists and check filename for item ID based filesystem |
| 2615 | if ($this->options['copyOverwrite']) { |
| 2616 | // do not replace file with dir or dir with file |
| 2617 | if (!$this->isSameType($file['mime'], $stat['mime'])) { |
| 2618 | return $this->setError(elFinder::ERROR_NOT_REPLACE, $this->path($stat['hash'])); |
| 2619 | } |
| 2620 | // existed file is not writable |
| 2621 | if (empty($stat['write'])) { |
| 2622 | return $this->setError($err, $errpath, elFinder::ERROR_PERM_DENIED); |
| 2623 | } |
| 2624 | if ($this->options['copyJoin']) { |
| 2625 | if (!empty($stat['locked'])) { |
| 2626 | return $this->setError(elFinder::ERROR_LOCKED, $this->path($stat['hash'])); |
| 2627 | } |
| 2628 | } else { |
| 2629 | // existed file locked or has locked child |
| 2630 | if (($locked = $this->closestByAttr($test, 'locked', true))) { |
| 2631 | $stat = $this->stat($locked); |
| 2632 | return $this->setError(elFinder::ERROR_LOCKED, $this->path($stat['hash'])); |
| 2633 | } |
| 2634 | } |
| 2635 | // target is entity file of alias |
| 2636 | if ($volume === $this && ((isset($file['target']) && $test == $file['target']) || $test == $this->decode($src))) { |
| 2637 | return $this->setError(elFinder::ERROR_REPLACE, $errpath); |
| 2638 | } |
| 2639 | // remove existed file |
| 2640 | if (!$this->options['copyJoin'] || $stat['mime'] !== 'directory') { |
| 2641 | if (!$this->remove($test)) { |
| 2642 | return $this->setError(elFinder::ERROR_REPLACE, $this->path($stat['hash'])); |
| 2643 | } |
| 2644 | } else if ($stat['mime'] === 'directory') { |
| 2645 | $dstDirExists = true; |
| 2646 | } |
| 2647 | } else { |
| 2648 | $name = $this->uniqueName($destination, $name, ' ', false); |
| 2649 | } |
| 2650 | } |
| 2651 | |
| 2652 | // copy/move inside current volume |
| 2653 | if ($volume === $this) { // changing == operand to === fixes issue #1285 - Paul Canning 24/03/2016 |
| 2654 | $source = $this->decode($src); |
| 2655 | // do not copy into itself |
| 2656 | if ($this->inpathCE($destination, $source)) { |
| 2657 | return $this->setError(elFinder::ERROR_COPY_ITSELF, $errpath); |
| 2658 | } |
| 2659 | $rmDir = false; |
| 2660 | if ($rmSrc) { |
| 2661 | if ($dstDirExists) { |
| 2662 | $rmDir = true; |
| 2663 | $method = 'copy'; |
| 2664 | } else { |
| 2665 | $method = 'move'; |
| 2666 | } |
| 2667 | } else { |
| 2668 | $method = 'copy'; |
| 2669 | } |
| 2670 | $this->clearcache(); |
| 2671 | if ($res = ($path = $this->$method($source, $destination, $name)) ? $this->stat($path) : false) { |
| 2672 | if ($rmDir) { |
| 2673 | $this->remove($source); |
| 2674 | } |
| 2675 | } else { |
| 2676 | return false; |
| 2677 | } |
| 2678 | } else { |
| 2679 | // copy/move from another volume |
| 2680 | if (!$this->options['copyTo'] || !$volume->copyFromAllowed()) { |
| 2681 | return $this->setError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED); |
| 2682 | } |
| 2683 | |
| 2684 | $this->error = array(); |
| 2685 | if (($path = $this->copyFrom($volume, $src, $destination, $name)) == false) { |
| 2686 | return false; |
| 2687 | } |
| 2688 | |
| 2689 | if ($rmSrc && !$this->error()) { |
| 2690 | if (!$volume->rm($src)) { |
| 2691 | if ($volume->file($src)) { |
| 2692 | $this->addError(elFinder::ERROR_RM_SRC); |
| 2693 | } else { |
| 2694 | $this->removed[] = $file; |
| 2695 | } |
| 2696 | } |
| 2697 | } |
| 2698 | $res = $this->stat($path); |
| 2699 | } |
| 2700 | return $res; |
| 2701 | } |
| 2702 | |
| 2703 | /** |
| 2704 | * Return path info array to archive of target items |
| 2705 | * |
| 2706 | * @param array $hashes |
| 2707 | * |
| 2708 | * @return array|false |
| 2709 | * @throws Exception |
| 2710 | * @author Naoki Sawada |
| 2711 | */ |
| 2712 | public function zipdl($hashes) |
| 2713 | { |
| 2714 | if ($this->commandDisabled('zipdl')) { |
| 2715 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2716 | } |
| 2717 | |
| 2718 | $archivers = $this->getArchivers(); |
| 2719 | $cmd = null; |
| 2720 | if (!$archivers || empty($archivers['create'])) { |
| 2721 | return false; |
| 2722 | } |
| 2723 | $archivers = $archivers['create']; |
| 2724 | if (!$archivers) { |
| 2725 | return false; |
| 2726 | } |
| 2727 | $file = $mime = ''; |
| 2728 | foreach (array('zip', 'tgz') as $ext) { |
| 2729 | $mime = $this->mimetype('file.' . $ext, true); |
| 2730 | if (isset($archivers[$mime])) { |
| 2731 | $cmd = $archivers[$mime]; |
| 2732 | break; |
| 2733 | } |
| 2734 | } |
| 2735 | if (!$cmd) { |
| 2736 | $cmd = array_shift($archivers); |
| 2737 | if (!empty($ext)) { |
| 2738 | $mime = $this->mimetype('file.' . $ext, true); |
| 2739 | } |
| 2740 | } |
| 2741 | $ext = $cmd['ext']; |
| 2742 | $res = false; |
| 2743 | $mixed = false; |
| 2744 | $hashes = array_values($hashes); |
| 2745 | $dirname = dirname(str_replace($this->separator, DIRECTORY_SEPARATOR, $this->path($hashes[0]))); |
| 2746 | $cnt = count($hashes); |
| 2747 | if ($cnt > 1) { |
| 2748 | for ($i = 1; $i < $cnt; $i++) { |
| 2749 | if ($dirname !== dirname(str_replace($this->separator, DIRECTORY_SEPARATOR, $this->path($hashes[$i])))) { |
| 2750 | $mixed = true; |
| 2751 | break; |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | if ($mixed || $this->root == $this->dirnameCE($this->decode($hashes[0]))) { |
| 2756 | $prefix = $this->rootName; |
| 2757 | } else { |
| 2758 | $prefix = basename($dirname); |
| 2759 | } |
| 2760 | if ($dir = $this->getItemsInHand($hashes)) { |
| 2761 | $tmppre = (substr(PHP_OS, 0, 3) === 'WIN') ? 'zd-' : 'elfzdl-'; |
| 2762 | $pdir = dirname($dir); |
| 2763 | // garbage collection (expire 2h) |
| 2764 | register_shutdown_function(array('elFinder', 'GlobGC'), $pdir . DIRECTORY_SEPARATOR . $tmppre . '*', 7200); |
| 2765 | $files = self::localScandir($dir); |
| 2766 | if ($files && ($arc = tempnam($dir, $tmppre))) { |
| 2767 | unlink($arc); |
| 2768 | $arc = $arc . '.' . $ext; |
| 2769 | $name = basename($arc); |
| 2770 | if ($arc = $this->makeArchive($dir, $files, $name, $cmd)) { |
| 2771 | $file = tempnam($pdir, $tmppre); |
| 2772 | unlink($file); |
| 2773 | $res = rename($arc, $file); |
| 2774 | $this->rmdirRecursive($dir); |
| 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | return $res ? array('path' => $file, 'ext' => $ext, 'mime' => $mime, 'prefix' => $prefix) : false; |
| 2779 | } |
| 2780 | |
| 2781 | /** |
| 2782 | * Return file contents |
| 2783 | * |
| 2784 | * @param string $hash file hash |
| 2785 | * |
| 2786 | * @return string|false |
| 2787 | * @author Dmitry (dio) Levashov |
| 2788 | **/ |
| 2789 | public function getContents($hash) |
| 2790 | { |
| 2791 | $file = $this->file($hash); |
| 2792 | |
| 2793 | if (!$file) { |
| 2794 | return $this->setError(elFinder::ERROR_FILE_NOT_FOUND); |
| 2795 | } |
| 2796 | |
| 2797 | if ($file['mime'] == 'directory') { |
| 2798 | return $this->setError(elFinder::ERROR_NOT_FILE); |
| 2799 | } |
| 2800 | |
| 2801 | if (!$file['read']) { |
| 2802 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2803 | } |
| 2804 | |
| 2805 | if ($this->getMaxSize > 0 && $file['size'] > $this->getMaxSize) { |
| 2806 | return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE); |
| 2807 | } |
| 2808 | |
| 2809 | return $file['size'] ? $this->_getContents($this->convEncIn($this->decode($hash), true)) : ''; |
| 2810 | } |
| 2811 | |
| 2812 | /** |
| 2813 | * Put content in text file and return file info. |
| 2814 | * |
| 2815 | * @param string $hash file hash |
| 2816 | * @param string $content new file content |
| 2817 | * |
| 2818 | * @return array|false |
| 2819 | * @author Dmitry (dio) Levashov |
| 2820 | **/ |
| 2821 | public function putContents($hash, $content) |
| 2822 | { |
| 2823 | if ($this->commandDisabled('edit')) { |
| 2824 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2825 | } |
| 2826 | |
| 2827 | $path = $this->decode($hash); |
| 2828 | |
| 2829 | if (!($file = $this->file($hash))) { |
| 2830 | return $this->setError(elFinder::ERROR_FILE_NOT_FOUND); |
| 2831 | } |
| 2832 | |
| 2833 | if (!$file['write']) { |
| 2834 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2835 | } |
| 2836 | |
| 2837 | // check data cheme |
| 2838 | if (preg_match('~^\0data:(.+?/.+?);base64,~', $content, $m)) { |
| 2839 | $dMime = $m[1]; |
| 2840 | if ($file['size'] > 0 && $dMime !== $file['mime']) { |
| 2841 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2842 | } |
| 2843 | $content = base64_decode(substr($content, strlen($m[0]))); |
| 2844 | } |
| 2845 | |
| 2846 | // check MIME |
| 2847 | $name = $this->basenameCE($path); |
| 2848 | $mime = ''; |
| 2849 | $mimeByName = $this->mimetype($name, true); |
| 2850 | if ($this->mimeDetect !== 'internal') { |
| 2851 | if ($tp = $this->tmpfile()) { |
| 2852 | fwrite($tp, $content); |
| 2853 | $info = stream_get_meta_data($tp); |
| 2854 | $filepath = $info['uri']; |
| 2855 | $mime = $this->mimetype($filepath, $name); |
| 2856 | fclose($tp); |
| 2857 | } |
| 2858 | } |
| 2859 | if (!$this->allowPutMime($mimeByName) || ($mime && !$this->allowPutMime($mime))) { |
| 2860 | return $this->setError(elFinder::ERROR_UPLOAD_FILE_MIME); |
| 2861 | } |
| 2862 | |
| 2863 | $this->clearcache(); |
| 2864 | $res = false; |
| 2865 | if ($this->convEncOut($this->_filePutContents($this->convEncIn($path), $content))) { |
| 2866 | $this->rmTmb($file); |
| 2867 | $this->clearstatcache(); |
| 2868 | $res = $this->stat($path); |
| 2869 | } |
| 2870 | return $res; |
| 2871 | } |
| 2872 | |
| 2873 | /** |
| 2874 | * Extract files from archive |
| 2875 | * |
| 2876 | * @param string $hash archive hash |
| 2877 | * @param null $makedir |
| 2878 | * |
| 2879 | * @return array|bool |
| 2880 | * @author Dmitry (dio) Levashov, |
| 2881 | * @author Alexey Sukhotin |
| 2882 | */ |
| 2883 | public function extract($hash, $makedir = null) |
| 2884 | { |
| 2885 | if ($this->commandDisabled('extract')) { |
| 2886 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2887 | } |
| 2888 | |
| 2889 | if (($file = $this->file($hash)) == false) { |
| 2890 | return $this->setError(elFinder::ERROR_FILE_NOT_FOUND); |
| 2891 | } |
| 2892 | |
| 2893 | $archiver = isset($this->archivers['extract'][$file['mime']]) |
| 2894 | ? $this->archivers['extract'][$file['mime']] |
| 2895 | : array(); |
| 2896 | |
| 2897 | if (!$archiver) { |
| 2898 | return $this->setError(elFinder::ERROR_NOT_ARCHIVE); |
| 2899 | } |
| 2900 | |
| 2901 | $path = $this->decode($hash); |
| 2902 | $parent = $this->stat($this->dirnameCE($path)); |
| 2903 | |
| 2904 | if (!$file['read'] || !$parent['write']) { |
| 2905 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2906 | } |
| 2907 | $this->clearcache(); |
| 2908 | $this->extractToNewdir = is_null($makedir) ? 'auto' : (bool)$makedir; |
| 2909 | |
| 2910 | if ($path = $this->convEncOut($this->_extract($this->convEncIn($path), $archiver))) { |
| 2911 | if (is_array($path)) { |
| 2912 | foreach ($path as $_k => $_p) { |
| 2913 | $path[$_k] = $this->stat($_p); |
| 2914 | } |
| 2915 | } else { |
| 2916 | $path = $this->stat($path); |
| 2917 | } |
| 2918 | return $path; |
| 2919 | } else { |
| 2920 | return false; |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | /** |
| 2925 | * Add files to archive |
| 2926 | * |
| 2927 | * @param $hashes |
| 2928 | * @param $mime |
| 2929 | * @param string $name |
| 2930 | * |
| 2931 | * @return array|bool |
| 2932 | * @throws Exception |
| 2933 | */ |
| 2934 | public function archive($hashes, $mime, $name = '') |
| 2935 | { |
| 2936 | if ($this->commandDisabled('archive')) { |
| 2937 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2938 | } |
| 2939 | |
| 2940 | if ($name !== '' && !$this->nameAccepted($name, false)) { |
| 2941 | return $this->setError(elFinder::ERROR_INVALID_NAME); |
| 2942 | } |
| 2943 | |
| 2944 | $archiver = isset($this->archivers['create'][$mime]) |
| 2945 | ? $this->archivers['create'][$mime] |
| 2946 | : array(); |
| 2947 | |
| 2948 | if (!$archiver) { |
| 2949 | return $this->setError(elFinder::ERROR_ARCHIVE_TYPE); |
| 2950 | } |
| 2951 | |
| 2952 | $files = array(); |
| 2953 | $useRemoteArchive = !empty($this->options['useRemoteArchive']); |
| 2954 | |
| 2955 | $dir = ''; |
| 2956 | foreach ($hashes as $hash) { |
| 2957 | if (($file = $this->file($hash)) == false) { |
| 2958 | return $this->setError(elFinder::ERROR_FILE_NOT_FOUND, '#' . $hash); |
| 2959 | } |
| 2960 | if (!$file['read']) { |
| 2961 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2962 | } |
| 2963 | $path = $this->decode($hash); |
| 2964 | if ($dir === '') { |
| 2965 | $dir = $this->dirnameCE($path); |
| 2966 | $stat = $this->stat($dir); |
| 2967 | if (!$stat['write']) { |
| 2968 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | $files[] = $useRemoteArchive ? $hash : $this->basenameCE($path); |
| 2973 | } |
| 2974 | |
| 2975 | if ($name === '') { |
| 2976 | $name = count($files) == 1 ? $files[0] : 'Archive'; |
| 2977 | } else { |
| 2978 | $name = str_replace(array('/', '\\'), '_', preg_replace('/\.' . preg_quote($archiver['ext'], '/') . '$/i', '', $name)); |
| 2979 | } |
| 2980 | $name .= '.' . $archiver['ext']; |
| 2981 | $name = $this->uniqueName($dir, $name, ''); |
| 2982 | $this->clearcache(); |
| 2983 | if ($useRemoteArchive) { |
| 2984 | return ($path = $this->remoteArchive($files, $name, $archiver)) ? $this->stat($path) : false; |
| 2985 | } else { |
| 2986 | return ($path = $this->convEncOut($this->_archive($this->convEncIn($dir), $this->convEncIn($files), $this->convEncIn($name), $archiver))) ? $this->stat($path) : false; |
| 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | /** |
| 2991 | * Create an archive from remote items |
| 2992 | * |
| 2993 | * @param array $hashes files hashes list |
| 2994 | * @param string $name archive name |
| 2995 | * @param array $arc archiver options |
| 2996 | * |
| 2997 | * @return string|boolean path of created archive |
| 2998 | * @throws Exception |
| 2999 | */ |
| 3000 | protected function remoteArchive($hashes, $name, $arc) |
| 3001 | { |
| 3002 | $resPath = false; |
| 3003 | $file0 = $this->file($hashes[0]); |
| 3004 | if ($file0 && ($dir = $this->getItemsInHand($hashes))) { |
| 3005 | $files = self::localScandir($dir); |
| 3006 | if ($files) { |
| 3007 | if ($arc = $this->makeArchive($dir, $files, $name, $arc)) { |
| 3008 | if ($fp = fopen($arc, 'rb')) { |
| 3009 | $fstat = stat($arc); |
| 3010 | $stat = array( |
| 3011 | 'size' => $fstat['size'], |
| 3012 | 'ts' => $fstat['mtime'], |
| 3013 | 'mime' => $this->mimetype($arc, $name) |
| 3014 | ); |
| 3015 | $path = $this->decode($file0['phash']); |
| 3016 | $resPath = $this->saveCE($fp, $path, $name, $stat); |
| 3017 | fclose($fp); |
| 3018 | } |
| 3019 | } |
| 3020 | } |
| 3021 | $this->rmdirRecursive($dir); |
| 3022 | } |
| 3023 | return $resPath; |
| 3024 | } |
| 3025 | |
| 3026 | /** |
| 3027 | * Resize image |
| 3028 | * |
| 3029 | * @param string $hash image file |
| 3030 | * @param int $width new width |
| 3031 | * @param int $height new height |
| 3032 | * @param int $x X start poistion for crop |
| 3033 | * @param int $y Y start poistion for crop |
| 3034 | * @param string $mode action how to mainpulate image |
| 3035 | * @param string $bg background color |
| 3036 | * @param int $degree rotete degree |
| 3037 | * @param int $jpgQuality JEPG quality (1-100) |
| 3038 | * |
| 3039 | * @return array|false |
| 3040 | * @throws ImagickException |
| 3041 | * @throws elFinderAbortException |
| 3042 | * @author Dmitry (dio) Levashov |
| 3043 | * @author Alexey Sukhotin |
| 3044 | * @author nao-pon |
| 3045 | * @author Troex Nevelin |
| 3046 | */ |
| 3047 | public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0, $jpgQuality = null) |
| 3048 | { |
| 3049 | if ($this->commandDisabled('resize')) { |
| 3050 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 3051 | } |
| 3052 | |
| 3053 | if ($mode === 'rotate' && $degree == 0) { |
| 3054 | return array('losslessRotate' => ($this->procExec(ELFINDER_EXIFTRAN_PATH . ' -h') === 0 || $this->procExec(ELFINDER_JPEGTRAN_PATH . ' -version') === 0)); |
| 3055 | } |
| 3056 | |
| 3057 | if (($file = $this->file($hash)) == false) { |
| 3058 | return $this->setError(elFinder::ERROR_FILE_NOT_FOUND); |
| 3059 | } |
| 3060 | |
| 3061 | if (!$file['write'] || !$file['read']) { |
| 3062 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 3063 | } |
| 3064 | |
| 3065 | $path = $this->decode($hash); |
| 3066 | |
| 3067 | $work_path = $this->getWorkFile($this->encoding ? $this->convEncIn($path, true) : $path); |
| 3068 | |
| 3069 | if (!$work_path || !is_writable($work_path)) { |
| 3070 | if ($work_path && $path !== $work_path && is_file($work_path)) { |
| 3071 | unlink($work_path); |
| 3072 | } |
| 3073 | return $this->setError(elFinder::ERROR_PERM_DENIED); |
| 3074 | } |
| 3075 | |
| 3076 | if ($this->imgLib !== 'imagick' && $this->imgLib !== 'convert') { |
| 3077 | if (elFinder::isAnimationGif($work_path)) { |
| 3078 | return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE); |
| 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | if (elFinder::isAnimationPng($work_path)) { |
| 3083 | return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE); |
| 3084 | } |
| 3085 | |
| 3086 | switch ($mode) { |
| 3087 | |
| 3088 | case 'propresize': |
| 3089 | $result = $this->imgResize($work_path, $width, $height, true, true, null, $jpgQuality); |
| 3090 | break; |
| 3091 | |
| 3092 | case 'crop': |
| 3093 | $result = $this->imgCrop($work_path, $width, $height, $x, $y, null, $jpgQuality); |
| 3094 | break; |
| 3095 | |
| 3096 | case 'fitsquare': |
| 3097 | $result = $this->imgSquareFit($work_path, $width, $height, 'center', 'middle', ($bg ? $bg : $this->options['tmbBgColor']), null, $jpgQuality); |
| 3098 | break; |
| 3099 | |
| 3100 | case 'rotate': |
| 3101 | $result = $this->imgRotate($work_path, $degree, ($bg ? $bg : $this->options['bgColorFb']), null, $jpgQuality); |
| 3102 | break; |
| 3103 | |
| 3104 | default: |
| 3105 | $result = $this->imgResize($work_path, $width, $height, false, true, null, $jpgQuality); |
| 3106 | break; |
| 3107 | } |
| 3108 | |
| 3109 | $ret = false; |
| 3110 | if ($result) { |
| 3111 | $this->rmTmb($file); |
| 3112 | $this->clearstatcache(); |
| 3113 | $fstat = stat($work_path); |
| 3114 | $imgsize = getimagesize($work_path); |
| 3115 | if ($path !== $work_path) { |
| 3116 | $file['size'] = $fstat['size']; |
| 3117 | $file['ts'] = $fstat['mtime']; |
| 3118 | if ($imgsize) { |
| 3119 | $file['width'] = $imgsize[0]; |
| 3120 | $file['height'] = $imgsize[1]; |
| 3121 | } |
| 3122 | if ($fp = fopen($work_path, 'rb')) { |
| 3123 | $ret = $this->saveCE($fp, $this->dirnameCE($path), $this->basenameCE($path), $file); |
| 3124 | fclose($fp); |
| 3125 | } |
| 3126 | } else { |
| 3127 | $ret = true; |
| 3128 | } |
| 3129 | if ($ret) { |
| 3130 | $this->clearcache(); |
| 3131 | $ret = $this->stat($path); |
| 3132 | if ($imgsize) { |
| 3133 | $ret['width'] = $imgsize[0]; |
| 3134 | $ret['height'] = $imgsize[1]; |
| 3135 | } |
| 3136 | } |
| 3137 | } |
| 3138 | if ($path !== $work_path) { |
| 3139 | is_file($work_path) && unlink($work_path); |
| 3140 | } |
| 3141 | |
| 3142 | return $ret; |
| 3143 | } |
| 3144 | |
| 3145 | /** |
| 3146 | * Remove file/dir |
| 3147 | * |
| 3148 | * @param string $hash file hash |
| 3149 | * |
| 3150 | * @return bool |
| 3151 | * @throws elFinderAbortException |
| 3152 | * @author Dmitry (dio) Levashov |
| 3153 | */ |
| 3154 | public function rm($hash) |
| 3155 | { |
| 3156 | return $this->commandDisabled('rm') |
| 3157 | ? $this->setError(elFinder::ERROR_PERM_DENIED) |
| 3158 | : $this->remove($this->decode($hash)); |
| 3159 | } |
| 3160 | |
| 3161 | /** |
| 3162 | * Search files |
| 3163 | * |
| 3164 | * @param string $q search string |
| 3165 | * @param array $mimes |
| 3166 | * @param null $hash |
| 3167 | * |
| 3168 | * @return array |
| 3169 | * @throws elFinderAbortException |
| 3170 | * @author Dmitry (dio) Levashov |
| 3171 | */ |
| 3172 | public function search($q, $mimes, $hash = null) |
| 3173 | { |
| 3174 | $res = array(); |
| 3175 | $matchMethod = null; |
| 3176 | $args = func_get_args(); |
| 3177 | if (!empty($args[3])) { |
| 3178 | $matchMethod = 'searchMatch' . $args[3]; |
| 3179 | if (!is_callable(array($this, $matchMethod))) { |
| 3180 | return array(); |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | $dir = null; |
| 3185 | if ($hash) { |
| 3186 | $dir = $this->decode($hash); |
| 3187 | $stat = $this->stat($dir); |
| 3188 | if (!$stat || $stat['mime'] !== 'directory' || !$stat['read']) { |
| 3189 | $q = ''; |
| 3190 | } |
| 3191 | } |
| 3192 | if ($mimes && $this->onlyMimes) { |
| 3193 | $mimes = array_intersect($mimes, $this->onlyMimes); |
| 3194 | if (!$mimes) { |
| 3195 | $q = ''; |
| 3196 | } |
| 3197 | } |
| 3198 | $this->searchStart = time(); |
| 3199 | |
| 3200 | $qs = preg_split('/"([^"]+)"| +/', $q, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
| 3201 | $query = $excludes = array(); |
| 3202 | foreach ($qs as $_q) { |
| 3203 | $_q = trim($_q); |
| 3204 | if ($_q !== '') { |
| 3205 | if ($_q[0] === '-') { |
| 3206 | if (isset($_q[1])) { |
| 3207 | $excludes[] = substr($_q, 1); |
| 3208 | } |
| 3209 | } else { |
| 3210 | $query[] = $_q; |
| 3211 | } |
| 3212 | } |
| 3213 | } |
| 3214 | if (!$query) { |
| 3215 | $q = ''; |
| 3216 | } else { |
| 3217 | $q = join(' ', $query); |
| 3218 | $this->doSearchCurrentQuery = array( |
| 3219 | 'q' => $q, |
| 3220 | 'excludes' => $excludes, |
| 3221 | 'matchMethod' => $matchMethod |
| 3222 | ); |
| 3223 | } |
| 3224 | |
| 3225 | if ($q === '' || $this->commandDisabled('search')) { |
| 3226 | return $res; |
| 3227 | } |
| 3228 | |
| 3229 | // valided regex $this->options['searchExDirReg'] |
| 3230 | if ($this->options['searchExDirReg']) { |
| 3231 | if (false === preg_match($this->options['searchExDirReg'], '')) { |
| 3232 | $this->options['searchExDirReg'] = ''; |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | // check the leaf root too |
| 3237 | if (!$mimes && (is_null($dir) || $dir == $this->root)) { |
| 3238 | $rootStat = $this->stat($this->root); |
| 3239 | if (!empty($rootStat['phash'])) { |
| 3240 | if ($this->stripos($rootStat['name'], $q) !== false) { |
| 3241 | $res = array($rootStat); |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | return array_merge($res, $this->doSearch(is_null($dir) ? $this->root : $dir, $q, $mimes)); |
| 3247 | } |
| 3248 | |
| 3249 | /** |
| 3250 | * Return image dimensions |
| 3251 | * |
| 3252 | * @param string $hash file hash |
| 3253 | * |
| 3254 | * @return array|string |
| 3255 | * @author Dmitry (dio) Levashov |
| 3256 | **/ |
| 3257 | public function dimensions($hash) |
| 3258 | { |
| 3259 | if (($file = $this->file($hash)) == false) { |
| 3260 | return false; |
| 3261 | } |
| 3262 | // Throw additional parameters for some drivers |
| 3263 | if (func_num_args() > 1) { |
| 3264 | $args = func_get_arg(1); |
| 3265 | } else { |
| 3266 | $args = array(); |
| 3267 | } |
| 3268 | return $this->convEncOut($this->_dimensions($this->convEncIn($this->decode($hash)), $file['mime'], $args)); |
| 3269 | } |
| 3270 | |
| 3271 | /** |
| 3272 | * Return has subdirs |
| 3273 | * |
| 3274 | * @param string $hash file hash |
| 3275 | * |
| 3276 | * @return bool |
| 3277 | * @author Naoki Sawada |
| 3278 | **/ |
| 3279 | public function subdirs($hash) |
| 3280 | { |
| 3281 | return (bool)$this->subdirsCE($this->decode($hash)); |
| 3282 | } |
| 3283 | |
| 3284 | /** |
| 3285 | * Return content URL (for netmout volume driver) |
| 3286 | * If file.url == 1 requests from JavaScript client with XHR |
| 3287 | * |
| 3288 | * @param string $hash file hash |
| 3289 | * @param array $options options array |
| 3290 | * |
| 3291 | * @return boolean|string |
| 3292 | * @author Naoki Sawada |
| 3293 | */ |
| 3294 | public function getContentUrl($hash, $options = array()) |
| 3295 | { |
| 3296 | if (($file = $this->file($hash)) === false) { |
| 3297 | return false; |
| 3298 | } |
| 3299 | if (!empty($options['onetime']) && $this->options['onetimeUrl']) { |
| 3300 | if (is_callable($this->options['onetimeUrl'])) { |
| 3301 | return call_user_func_array($this->options['onetimeUrl'], array($file, $options, $this)); |
| 3302 | } else { |
| 3303 | $ret = false; |
| 3304 | if ($tmpdir = elFinder::getStaticVar('commonTempPath')) { |
| 3305 | if ($source = $this->open($hash)) { |
| 3306 | if ($_dat = tempnam($tmpdir, 'ELF')) { |
| 3307 | $token = md5($_dat . session_id()); |
| 3308 | $dat = $tmpdir . DIRECTORY_SEPARATOR . 'ELF' . $token; |
| 3309 | if (rename($_dat, $dat)) { |
| 3310 | $info = stream_get_meta_data($source); |
| 3311 | if (!empty($info['uri'])) { |
| 3312 | $tmp = $info['uri']; |
| 3313 | } else { |
| 3314 | $tmp = tempnam($tmpdir, 'ELF'); |
| 3315 | if ($dest = fopen($tmp, 'wb')) { |
| 3316 | if (!stream_copy_to_stream($source, $dest)) { |
| 3317 | $tmp = false; |
| 3318 | } |
| 3319 | fclose($dest); |
| 3320 | } |
| 3321 | } |
| 3322 | $this->close($source, $hash); |
| 3323 | if ($tmp) { |
| 3324 | $info = array( |
| 3325 | 'file' => base64_encode($tmp), |
| 3326 | 'name' => $file['name'], |
| 3327 | 'mime' => $file['mime'], |
| 3328 | 'ts' => $file['ts'] |
| 3329 | ); |
| 3330 | if (file_put_contents($dat, json_encode($info))) { |
| 3331 | $conUrl = elFinder::getConnectorUrl(); |
| 3332 | $ret = $conUrl . (strpos($conUrl, '?') !== false? '&' : '?') . 'cmd=file&onetime=1&target=' . $token; |
| 3333 | |
| 3334 | } |
| 3335 | } |
| 3336 | if (!$ret) { |
| 3337 | unlink($dat); |
| 3338 | } |
| 3339 | } else { |
| 3340 | unlink($_dat); |
| 3341 | } |
| 3342 | } |
| 3343 | } |
| 3344 | } |
| 3345 | return $ret; |
| 3346 | } |
| 3347 | } |
| 3348 | if (empty($file['url']) && $this->URL) { |
| 3349 | $path = str_replace($this->separator, '/', substr($this->decode($hash), strlen(rtrim($this->root, '/' . $this->separator)) + 1)); |
| 3350 | if ($this->encoding) { |
| 3351 | $path = $this->convEncIn($path, true); |
| 3352 | } |
| 3353 | $path = str_replace('%2F', '/', rawurlencode($path)); |
| 3354 | return $this->_buildFileUrl($path); |
| 3355 | } else { |
| 3356 | $ret = false; |
| 3357 | if (!empty($file['url']) && $file['url'] != 1) { |
| 3358 | return $file['url']; |
| 3359 | } else if (!empty($options['temporary']) && ($tempInfo = $this->getTempLinkInfo('temp_' . md5($hash . session_id())))) { |
| 3360 | if (is_readable($tempInfo['path'])) { |
| 3361 | touch($tempInfo['path']); |
| 3362 | $ret = $tempInfo['url'] . '?' . rawurlencode($file['name']); |
| 3363 | } else if ($source = $this->open($hash)) { |
| 3364 | if ($dest = fopen($tempInfo['path'], 'wb')) { |
| 3365 | if (stream_copy_to_stream($source, $dest)) { |
| 3366 | $ret = $tempInfo['url'] . '?' . rawurlencode($file['name']); |
| 3367 | } |
| 3368 | fclose($dest); |
| 3369 | } |
| 3370 | $this->close($source, $hash); |
| 3371 | } |
| 3372 | } |
| 3373 | return $ret; |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | /** |
| 3378 | * Get temporary contents link infomation |
| 3379 | * |
| 3380 | * @param string $name |
| 3381 | * |
| 3382 | * @return boolean|array |
| 3383 | * @author Naoki Sawada |
| 3384 | */ |
| 3385 | public function getTempLinkInfo($name = null) |
| 3386 | { |
| 3387 | if ($this->tmpLinkPath) { |
| 3388 | if (!$name) { |
| 3389 | $name = 'temp_' . md5($_SERVER['REMOTE_ADDR'] . (string)microtime(true)); |
| 3390 | } else if (substr($name, 0, 5) !== 'temp_') { |
| 3391 | $name = 'temp_' . $name; |
| 3392 | } |
| 3393 | register_shutdown_function(array('elFinder', 'GlobGC'), $this->tmpLinkPath . DIRECTORY_SEPARATOR . 'temp_*', elFinder::$tmpLinkLifeTime); |
| 3394 | return array( |
| 3395 | 'path' => $path = $this->tmpLinkPath . DIRECTORY_SEPARATOR . $name, |
| 3396 | 'url' => $this->tmpLinkUrl . '/' . rawurlencode($name) |
| 3397 | ); |
| 3398 | } |
| 3399 | return false; |
| 3400 | } |
| 3401 | |
| 3402 | /** |
| 3403 | * Get URL of substitute image by request args `substitute` or 4th argument $maxSize |
| 3404 | * |
| 3405 | * @param string $target Target hash |
| 3406 | * @param array $srcSize Size info array [width, height] |
| 3407 | * @param resource $srcfp Source file file pointer |
| 3408 | * @param integer $maxSize Maximum pixel of substitute image |
| 3409 | * |
| 3410 | * @return boolean |
| 3411 | * @throws ImagickException |
| 3412 | * @throws elFinderAbortException |
| 3413 | */ |
| 3414 | public function getSubstituteImgLink($target, $srcSize, $srcfp = null, $maxSize = null) |
| 3415 | { |
| 3416 | $url = false; |
| 3417 | $file = $this->file($target); |
| 3418 | $force = !in_array($file['mime'], array('image/jpeg', 'image/png', 'image/gif')); |
| 3419 | if (!$maxSize) { |
| 3420 | $args = elFinder::$currentArgs; |
| 3421 | if (!empty($args['substitute'])) { |
| 3422 | $maxSize = $args['substitute']; |
| 3423 | } |
| 3424 | } |
| 3425 | if ($maxSize && $srcSize[0] && $srcSize[1]) { |
| 3426 | if ($this->getOption('substituteImg')) { |
| 3427 | $maxSize = intval($maxSize); |
| 3428 | $zoom = min(($maxSize / $srcSize[0]), ($maxSize / $srcSize[1])); |
| 3429 | if ($force || $zoom < 1) { |
| 3430 | $width = round($srcSize[0] * $zoom); |
| 3431 | $height = round($srcSize[1] * $zoom); |
| 3432 | $jpgQuality = 50; |
| 3433 | $preserveExif = false; |
| 3434 | $unenlarge = true; |
| 3435 | $checkAnimated = true; |
| 3436 | $destformat = $file['mime'] === 'image/jpeg'? null : 'png'; |
| 3437 | if (!$srcfp) { |
| 3438 | elFinder::checkAborted(); |
| 3439 | $srcfp = $this->open($target); |
| 3440 | } |
| 3441 | if ($srcfp && ($tempLink = $this->getTempLinkInfo())) { |
| 3442 | elFinder::checkAborted(); |
| 3443 | $dest = fopen($tempLink['path'], 'wb'); |
| 3444 | if ($dest && stream_copy_to_stream($srcfp, $dest)) { |
| 3445 | fclose($dest); |
| 3446 | if ($this->imageUtil('resize', $tempLink['path'], compact('width', 'height', 'jpgQuality', 'preserveExif', 'unenlarge', 'checkAnimated', 'destformat'))) { |
| 3447 | $url = $tempLink['url']; |
| 3448 | // set expire to 1 min left |
| 3449 | touch($tempLink['path'], time() - elFinder::$tmpLinkLifeTime + 60); |
| 3450 | } else { |
| 3451 | unlink($tempLink['path']); |
| 3452 | } |
| 3453 | } |
| 3454 | $this->close($srcfp, $target); |
| 3455 | } |
| 3456 | } |
| 3457 | } |
| 3458 | } |
| 3459 | |
| 3460 | return $url; |
| 3461 | } |
| 3462 | |
| 3463 | /** |
| 3464 | * Return temp path |
| 3465 | * |
| 3466 | * @return string |
| 3467 | * @author Naoki Sawada |
| 3468 | */ |
| 3469 | public function getTempPath() |
| 3470 | { |
| 3471 | $tempPath = null; |
| 3472 | if (isset($this->tmpPath) && $this->tmpPath && is_writable($this->tmpPath)) { |
| 3473 | $tempPath = $this->tmpPath; |
| 3474 | } else if (isset($this->tmp) && $this->tmp && is_writable($this->tmp)) { |
| 3475 | $tempPath = $this->tmp; |
| 3476 | } else if (elFinder::getStaticVar('commonTempPath') && is_writable(elFinder::getStaticVar('commonTempPath'))) { |
| 3477 | $tempPath = elFinder::getStaticVar('commonTempPath'); |
| 3478 | } else if (function_exists('sys_get_temp_dir')) { |
| 3479 | $tempPath = sys_get_temp_dir(); |
| 3480 | } |
| 3481 | |
| 3482 | if ($tempPath && DIRECTORY_SEPARATOR !== '/') { |
| 3483 | $tempPath = str_replace('/', DIRECTORY_SEPARATOR, $tempPath); |
| 3484 | } |
| 3485 | return $tempPath; |
| 3486 | } |
| 3487 | |
| 3488 | /** |
| 3489 | * (Make &) Get upload taget dirctory hash |
| 3490 | * |
| 3491 | * @param string $baseTargetHash |
| 3492 | * @param string $path |
| 3493 | * @param array $result |
| 3494 | * |
| 3495 | * @return boolean|string |
| 3496 | * @author Naoki Sawada |
| 3497 | */ |
| 3498 | public function getUploadTaget($baseTargetHash, $path, & $result) |
| 3499 | { |
| 3500 | $base = $this->decode($baseTargetHash); |
| 3501 | $targetHash = $baseTargetHash; |
| 3502 | $path = ltrim($path, $this->separator); |
| 3503 | $dirs = explode($this->separator, $path); |
| 3504 | array_pop($dirs); |
| 3505 | foreach ($dirs as $dir) { |
| 3506 | $targetPath = $this->joinPathCE($base, $dir); |
| 3507 | if (!$_realpath = $this->realpath($this->encode($targetPath))) { |
| 3508 | if ($stat = $this->mkdir($targetHash, $dir)) { |
| 3509 | $result['added'][] = $stat; |
| 3510 | $targetHash = $stat['hash']; |
| 3511 | $base = $this->decode($targetHash); |
| 3512 | } else { |
| 3513 | return false; |
| 3514 | } |
| 3515 | } else { |
| 3516 | $targetHash = $this->encode($_realpath); |
| 3517 | if ($this->dir($targetHash)) { |
| 3518 | $base = $this->decode($targetHash); |
| 3519 | } else { |
| 3520 | return false; |
| 3521 | } |
| 3522 | } |
| 3523 | } |
| 3524 | return $targetHash; |
| 3525 | } |
| 3526 | |
| 3527 | /** |
| 3528 | * Return this uploadMaxSize value |
| 3529 | * |
| 3530 | * @return integer |
| 3531 | * @author Naoki Sawada |
| 3532 | */ |
| 3533 | public function getUploadMaxSize() |
| 3534 | { |
| 3535 | return $this->uploadMaxSize; |
| 3536 | } |
| 3537 | |
| 3538 | public function setUploadOverwrite($var) |
| 3539 | { |
| 3540 | $this->uploadOverwrite = (bool)$var; |
| 3541 | } |
| 3542 | |
| 3543 | /** |
| 3544 | * Image file utility |
| 3545 | * |
| 3546 | * @param string $mode 'resize', 'rotate', 'propresize', 'crop', 'fitsquare' |
| 3547 | * @param string $src Image file local path |
| 3548 | * @param array $options excute options |
| 3549 | * |
| 3550 | * @return bool |
| 3551 | * @throws ImagickException |
| 3552 | * @throws elFinderAbortException |
| 3553 | * @author Naoki Sawada |
| 3554 | */ |
| 3555 | public function imageUtil($mode, $src, $options = array()) |
| 3556 | { |
| 3557 | if (!isset($options['jpgQuality'])) { |
| 3558 | $options['jpgQuality'] = intval($this->options['jpgQuality']); |
| 3559 | } |
| 3560 | if (!isset($options['bgcolor'])) { |
| 3561 | $options['bgcolor'] = '#ffffff'; |
| 3562 | } |
| 3563 | if (!isset($options['bgColorFb'])) { |
| 3564 | $options['bgColorFb'] = $this->options['bgColorFb']; |
| 3565 | } |
| 3566 | $destformat = !empty($options['destformat'])? $options['destformat'] : null; |
| 3567 | |
| 3568 | // check 'width' ,'height' |
| 3569 | if (in_array($mode, array('resize', 'propresize', 'crop', 'fitsquare'))) { |
| 3570 | if (empty($options['width']) || empty($options['height'])) { |
| 3571 | return false; |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | if (!empty($options['checkAnimated'])) { |
| 3576 | if ($this->imgLib !== 'imagick' && $this->imgLib !== 'convert') { |
| 3577 | if (elFinder::isAnimationGif($src)) { |
| 3578 | return false; |
| 3579 | } |
| 3580 | } |
| 3581 | if (elFinder::isAnimationPng($src)) { |
| 3582 | return false; |
| 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | switch ($mode) { |
| 3587 | case 'rotate': |
| 3588 | if (empty($options['degree'])) { |
| 3589 | return true; |
| 3590 | } |
| 3591 | return (bool)$this->imgRotate($src, $options['degree'], $options['bgColorFb'], $destformat, $options['jpgQuality']); |
| 3592 | |
| 3593 | case 'resize': |
| 3594 | return (bool)$this->imgResize($src, $options['width'], $options['height'], false, true, $destformat, $options['jpgQuality'], $options); |
| 3595 | |
| 3596 | case 'propresize': |
| 3597 | return (bool)$this->imgResize($src, $options['width'], $options['height'], true, true, $destformat, $options['jpgQuality'], $options); |
| 3598 | |
| 3599 | case 'crop': |
| 3600 | if (isset($options['x']) && isset($options['y'])) { |
| 3601 | return (bool)$this->imgCrop($src, $options['width'], $options['height'], $options['x'], $options['y'], $destformat, $options['jpgQuality']); |
| 3602 | } |
| 3603 | break; |
| 3604 | |
| 3605 | case 'fitsquare': |
| 3606 | return (bool)$this->imgSquareFit($src, $options['width'], $options['height'], 'center', 'middle', $options['bgcolor'], $destformat, $options['jpgQuality']); |
| 3607 | |
| 3608 | } |
| 3609 | return false; |
| 3610 | } |
| 3611 | |
| 3612 | /** |
| 3613 | * Convert Video To Image by ffmpeg |
| 3614 | * |
| 3615 | * @param string $file video source file path |
| 3616 | * @param array $stat file stat array |
| 3617 | * @param object $self volume driver object |
| 3618 | * @param int $ss start seconds |
| 3619 | * |
| 3620 | * @return bool |
| 3621 | * @throws elFinderAbortException |
| 3622 | * @author Naoki Sawada |
| 3623 | */ |
| 3624 | public function ffmpegToImg($file, $stat, $self, $ss = null) |
| 3625 | { |
| 3626 | $name = basename($file); |
| 3627 | $path = dirname($file); |
| 3628 | $tmp = $path . DIRECTORY_SEPARATOR . md5($name); |
| 3629 | // register auto delete on shutdown |
| 3630 | $GLOBALS['elFinderTempFiles'][$tmp] = true; |
| 3631 | if (rename($file, $tmp)) { |
| 3632 | if ($ss === null) { |
| 3633 | // specific start time by file name (xxx^[sec].[extention] - video^3.mp4) |
| 3634 | if (preg_match('/\^(\d+(?:\.\d+)?)\.[^.]+$/', $stat['name'], $_m)) { |
| 3635 | $ss = $_m[1]; |
| 3636 | } else { |
| 3637 | $ss = $this->options['tmbVideoConvSec']; |
| 3638 | } |
| 3639 | } |
| 3640 | $cmd = sprintf(ELFINDER_FFMPEG_PATH . ' -i %s -ss 00:00:%.3f -vframes 1 -f image2 -- %s', escapeshellarg($tmp), $ss, escapeshellarg($file)); |
| 3641 | $r = ($this->procExec($cmd) === 0); |
| 3642 | clearstatcache(); |
| 3643 | if ($r && $ss > 0 && !file_exists($file)) { |
| 3644 | // Retry by half of $ss |
| 3645 | $ss = max(intval($ss / 2), 0); |
| 3646 | rename($tmp, $file); |
| 3647 | $r = $this->ffmpegToImg($file, $stat, $self, $ss); |
| 3648 | } else { |
| 3649 | unlink($tmp); |
| 3650 | } |
| 3651 | return $r; |
| 3652 | } |
| 3653 | return false; |
| 3654 | } |
| 3655 | |
| 3656 | /** |
| 3657 | * Creates a temporary file and return file pointer |
| 3658 | * |
| 3659 | * @return resource|boolean |
| 3660 | */ |
| 3661 | public function tmpfile() |
| 3662 | { |
| 3663 | if ($tmp = $this->getTempFile()) { |
| 3664 | return fopen($tmp, 'wb'); |
| 3665 | } |
| 3666 | return false; |
| 3667 | } |
| 3668 | |
| 3669 | /** |
| 3670 | * Save error message |
| 3671 | * |
| 3672 | * @param array error |
| 3673 | * |
| 3674 | * @return boolean false |
| 3675 | * @author Naoki Sawada |
| 3676 | **/ |
| 3677 | protected function setError() |
| 3678 | { |
| 3679 | $this->error = array(); |
| 3680 | $this->addError(func_get_args()); |
| 3681 | return false; |
| 3682 | } |
| 3683 | |
| 3684 | /** |
| 3685 | * Add error message |
| 3686 | * |
| 3687 | * @param array error |
| 3688 | * |
| 3689 | * @return false |
| 3690 | * @author Dmitry(dio) Levashov |
| 3691 | **/ |
| 3692 | protected function addError() |
| 3693 | { |
| 3694 | foreach (func_get_args() as $err) { |
| 3695 | if (is_array($err)) { |
| 3696 | foreach($err as $er) { |
| 3697 | $this->addError($er); |
| 3698 | } |
| 3699 | } else { |
| 3700 | $this->error[] = (string)$err; |
| 3701 | } |
| 3702 | } |
| 3703 | return false; |
| 3704 | } |
| 3705 | |
| 3706 | /*********************************************************************/ |
| 3707 | /* FS API */ |
| 3708 | /*********************************************************************/ |
| 3709 | |
| 3710 | /***************** server encoding support *******************/ |
| 3711 | |
| 3712 | /** |
| 3713 | * Return parent directory path (with convert encoding) |
| 3714 | * |
| 3715 | * @param string $path file path |
| 3716 | * |
| 3717 | * @return string |
| 3718 | * @author Naoki Sawada |
| 3719 | **/ |
| 3720 | protected function dirnameCE($path) |
| 3721 | { |
| 3722 | $dirname = (!$this->encoding) ? $this->_dirname($path) : $this->convEncOut($this->_dirname($this->convEncIn($path))); |
| 3723 | // check to infinite loop prevention |
| 3724 | return ($dirname != $path) ? $dirname : ''; |
| 3725 | } |
| 3726 | |
| 3727 | /** |
| 3728 | * Return file name (with convert encoding) |
| 3729 | * |
| 3730 | * @param string $path file path |
| 3731 | * |
| 3732 | * @return string |
| 3733 | * @author Naoki Sawada |
| 3734 | **/ |
| 3735 | protected function basenameCE($path) |
| 3736 | { |
| 3737 | return (!$this->encoding) ? $this->_basename($path) : $this->convEncOut($this->_basename($this->convEncIn($path))); |
| 3738 | } |
| 3739 | |
| 3740 | /** |
| 3741 | * Join dir name and file name and return full path. (with convert encoding) |
| 3742 | * Some drivers (db) use int as path - so we give to concat path to driver itself |
| 3743 | * |
| 3744 | * @param string $dir dir path |
| 3745 | * @param string $name file name |
| 3746 | * |
| 3747 | * @return string |
| 3748 | * @author Naoki Sawada |
| 3749 | **/ |
| 3750 | protected function joinPathCE($dir, $name) |
| 3751 | { |
| 3752 | return (!$this->encoding) ? $this->_joinPath($dir, $name) : $this->convEncOut($this->_joinPath($this->convEncIn($dir), $this->convEncIn($name))); |
| 3753 | } |
| 3754 | |
| 3755 | /** |
| 3756 | * Return normalized path (with convert encoding) |
| 3757 | * |
| 3758 | * @param string $path file path |
| 3759 | * |
| 3760 | * @return string |
| 3761 | * @author Naoki Sawada |
| 3762 | **/ |
| 3763 | protected function normpathCE($path) |
| 3764 | { |
| 3765 | return (!$this->encoding) ? $this->_normpath($path) : $this->convEncOut($this->_normpath($this->convEncIn($path))); |
| 3766 | } |
| 3767 | |
| 3768 | /** |
| 3769 | * Return file path related to root dir (with convert encoding) |
| 3770 | * |
| 3771 | * @param string $path file path |
| 3772 | * |
| 3773 | * @return string |
| 3774 | * @author Naoki Sawada |
| 3775 | **/ |
| 3776 | protected function relpathCE($path) |
| 3777 | { |
| 3778 | return (!$this->encoding) ? $this->_relpath($path) : $this->convEncOut($this->_relpath($this->convEncIn($path))); |
| 3779 | } |
| 3780 | |
| 3781 | /** |
| 3782 | * Convert path related to root dir into real path (with convert encoding) |
| 3783 | * |
| 3784 | * @param string $path rel file path |
| 3785 | * |
| 3786 | * @return string |
| 3787 | * @author Naoki Sawada |
| 3788 | **/ |
| 3789 | protected function abspathCE($path) |
| 3790 | { |
| 3791 | return (!$this->encoding) ? $this->_abspath($path) : $this->convEncOut($this->_abspath($this->convEncIn($path))); |
| 3792 | } |
| 3793 | |
| 3794 | /** |
| 3795 | * Return true if $path is children of $parent (with convert encoding) |
| 3796 | * |
| 3797 | * @param string $path path to check |
| 3798 | * @param string $parent parent path |
| 3799 | * |
| 3800 | * @return bool |
| 3801 | * @author Naoki Sawada |
| 3802 | **/ |
| 3803 | protected function inpathCE($path, $parent) |
| 3804 | { |
| 3805 | return (!$this->encoding) ? $this->_inpath($path, $parent) : $this->convEncOut($this->_inpath($this->convEncIn($path), $this->convEncIn($parent))); |
| 3806 | } |
| 3807 | |
| 3808 | /** |
| 3809 | * Open file and return file pointer (with convert encoding) |
| 3810 | * |
| 3811 | * @param string $path file path |
| 3812 | * @param string $mode |
| 3813 | * |
| 3814 | * @return false|resource |
| 3815 | * @internal param bool $write open file for writing |
| 3816 | * @author Naoki Sawada |
| 3817 | */ |
| 3818 | protected function fopenCE($path, $mode = 'rb') |
| 3819 | { |
| 3820 | // check extra option for network stream pointer |
| 3821 | if (func_num_args() > 2) { |
| 3822 | $opts = func_get_arg(2); |
| 3823 | } else { |
| 3824 | $opts = array(); |
| 3825 | } |
| 3826 | return (!$this->encoding) ? $this->_fopen($path, $mode, $opts) : $this->convEncOut($this->_fopen($this->convEncIn($path), $mode, $opts)); |
| 3827 | } |
| 3828 | |
| 3829 | /** |
| 3830 | * Close opened file (with convert encoding) |
| 3831 | * |
| 3832 | * @param resource $fp file pointer |
| 3833 | * @param string $path file path |
| 3834 | * |
| 3835 | * @return bool |
| 3836 | * @author Naoki Sawada |
| 3837 | **/ |
| 3838 | protected function fcloseCE($fp, $path = '') |
| 3839 | { |
| 3840 | return (!$this->encoding) ? $this->_fclose($fp, $path) : $this->convEncOut($this->_fclose($fp, $this->convEncIn($path))); |
| 3841 | } |
| 3842 | |
| 3843 | /** |
| 3844 | * Create new file and write into it from file pointer. (with convert encoding) |
| 3845 | * Return new file path or false on error. |
| 3846 | * |
| 3847 | * @param resource $fp file pointer |
| 3848 | * @param string $dir target dir path |
| 3849 | * @param string $name file name |
| 3850 | * @param array $stat file stat (required by some virtual fs) |
| 3851 | * |
| 3852 | * @return bool|string |
| 3853 | * @author Naoki Sawada |
| 3854 | **/ |
| 3855 | protected function saveCE($fp, $dir, $name, $stat) |
| 3856 | { |
| 3857 | $res = (!$this->encoding) ? $this->_save($fp, $dir, $name, $stat) : $this->convEncOut($this->_save($fp, $this->convEncIn($dir), $this->convEncIn($name), $this->convEncIn($stat))); |
| 3858 | if ($res !== false) { |
| 3859 | $this->clearstatcache(); |
| 3860 | } |
| 3861 | return $res; |
| 3862 | } |
| 3863 | |
| 3864 | /** |
| 3865 | * Return true if path is dir and has at least one childs directory (with convert encoding) |
| 3866 | * |
| 3867 | * @param string $path dir path |
| 3868 | * |
| 3869 | * @return bool |
| 3870 | * @author Naoki Sawada |
| 3871 | **/ |
| 3872 | protected function subdirsCE($path) |
| 3873 | { |
| 3874 | if ($this->sessionCaching['subdirs']) { |
| 3875 | if (isset($this->sessionCache['subdirs'][$path]) && !$this->isMyReload()) { |
| 3876 | return $this->sessionCache['subdirs'][$path]; |
| 3877 | } |
| 3878 | } |
| 3879 | $hasdir = (bool)((!$this->encoding) ? $this->_subdirs($path) : $this->convEncOut($this->_subdirs($this->convEncIn($path)))); |
| 3880 | $this->updateSubdirsCache($path, $hasdir); |
| 3881 | return $hasdir; |
| 3882 | } |
| 3883 | |
| 3884 | /** |
| 3885 | * Return files list in directory (with convert encoding) |
| 3886 | * |
| 3887 | * @param string $path dir path |
| 3888 | * |
| 3889 | * @return array |
| 3890 | * @author Naoki Sawada |
| 3891 | **/ |
| 3892 | protected function scandirCE($path) |
| 3893 | { |
| 3894 | return (!$this->encoding) ? $this->_scandir($path) : $this->convEncOut($this->_scandir($this->convEncIn($path))); |
| 3895 | } |
| 3896 | |
| 3897 | /** |
| 3898 | * Create symlink (with convert encoding) |
| 3899 | * |
| 3900 | * @param string $source file to link to |
| 3901 | * @param string $targetDir folder to create link in |
| 3902 | * @param string $name symlink name |
| 3903 | * |
| 3904 | * @return bool |
| 3905 | * @author Naoki Sawada |
| 3906 | **/ |
| 3907 | protected function symlinkCE($source, $targetDir, $name) |
| 3908 | { |
| 3909 | return (!$this->encoding) ? $this->_symlink($source, $targetDir, $name) : $this->convEncOut($this->_symlink($this->convEncIn($source), $this->convEncIn($targetDir), $this->convEncIn($name))); |
| 3910 | } |
| 3911 | |
| 3912 | /***************** paths *******************/ |
| 3913 | |
| 3914 | /** |
| 3915 | * Encode path into hash |
| 3916 | * |
| 3917 | * @param string file path |
| 3918 | * |
| 3919 | * @return string |
| 3920 | * @author Dmitry (dio) Levashov |
| 3921 | * @author Troex Nevelin |
| 3922 | **/ |
| 3923 | protected function encode($path) |
| 3924 | { |
| 3925 | if ($path !== '') { |
| 3926 | |
| 3927 | // cut ROOT from $path for security reason, even if hacker decodes the path he will not know the root |
| 3928 | $p = $this->relpathCE($path); |
| 3929 | // if reqesting root dir $path will be empty, then assign '/' as we cannot leave it blank for crypt |
| 3930 | if ($p === '') { |
| 3931 | $p = $this->separator; |
| 3932 | } |
| 3933 | // change separator |
| 3934 | if ($this->separatorForHash) { |
| 3935 | $p = str_replace($this->separator, $this->separatorForHash, $p); |
| 3936 | } |
| 3937 | // TODO crypt path and return hash |
| 3938 | $hash = $this->crypt($p); |
| 3939 | // hash is used as id in HTML that means it must contain vaild chars |
| 3940 | // make base64 html safe and append prefix in begining |
| 3941 | $hash = strtr(base64_encode($hash), '+/=', '-_.'); |
| 3942 | // remove dots '.' at the end, before it was '=' in base64 |
| 3943 | $hash = rtrim($hash, '.'); |
| 3944 | // append volume id to make hash unique |
| 3945 | return $this->id . $hash; |
| 3946 | } |
| 3947 | //TODO: Add return statement here |
| 3948 | } |
| 3949 | |
| 3950 | /** |
| 3951 | * Decode path from hash |
| 3952 | * |
| 3953 | * @param string file hash |
| 3954 | * |
| 3955 | * @return string |
| 3956 | * @author Dmitry (dio) Levashov |
| 3957 | * @author Troex Nevelin |
| 3958 | **/ |
| 3959 | protected function decode($hash) |
| 3960 | { |
| 3961 | if (strpos($hash, $this->id) === 0) { |
| 3962 | // cut volume id after it was prepended in encode |
| 3963 | $h = substr($hash, strlen($this->id)); |
| 3964 | // replace HTML safe base64 to normal |
| 3965 | $h = base64_decode(strtr($h, '-_.', '+/=')); |
| 3966 | // TODO uncrypt hash and return path |
| 3967 | $path = $this->uncrypt($h); |
| 3968 | // change separator |
| 3969 | if ($this->separatorForHash) { |
| 3970 | $path = str_replace($this->separatorForHash, $this->separator, $path); |
| 3971 | } |
| 3972 | // append ROOT to path after it was cut in encode |
| 3973 | return $this->abspathCE($path);//$this->root.($path === $this->separator ? '' : $this->separator.$path); |
| 3974 | } |
| 3975 | return ''; |
| 3976 | } |
| 3977 | |
| 3978 | /** |
| 3979 | * Return crypted path |
| 3980 | * Not implemented |
| 3981 | * |
| 3982 | * @param string path |
| 3983 | * |
| 3984 | * @return mixed |
| 3985 | * @author Dmitry (dio) Levashov |
| 3986 | **/ |
| 3987 | protected function crypt($path) |
| 3988 | { |
| 3989 | return $path; |
| 3990 | } |
| 3991 | |
| 3992 | /** |
| 3993 | * Return uncrypted path |
| 3994 | * Not implemented |
| 3995 | * |
| 3996 | * @param mixed hash |
| 3997 | * |
| 3998 | * @return mixed |
| 3999 | * @author Dmitry (dio) Levashov |
| 4000 | **/ |
| 4001 | protected function uncrypt($hash) |
| 4002 | { |
| 4003 | return $hash; |
| 4004 | } |
| 4005 | |
| 4006 | /** |
| 4007 | * Validate file name based on $this->options['acceptedName'] regexp or function |
| 4008 | * |
| 4009 | * @param string $name file name |
| 4010 | * @param bool $isDir |
| 4011 | * |
| 4012 | * @return bool |
| 4013 | * @author Dmitry (dio) Levashov |
| 4014 | */ |
| 4015 | protected function nameAccepted($name, $isDir = false) |
| 4016 | { |
| 4017 | if (json_encode($name) === false) { |
| 4018 | return false; |
| 4019 | } |
| 4020 | $nameValidator = $isDir ? $this->dirnameValidator : $this->nameValidator; |
| 4021 | if ($nameValidator) { |
| 4022 | if (is_callable($nameValidator)) { |
| 4023 | $res = call_user_func($nameValidator, $name); |
| 4024 | return $res; |
| 4025 | } |
| 4026 | if (preg_match($nameValidator, '') !== false) { |
| 4027 | return preg_match($nameValidator, $name); |
| 4028 | } |
| 4029 | } |
| 4030 | return true; |
| 4031 | } |
| 4032 | |
| 4033 | /** |
| 4034 | * Return session rootstat cache key |
| 4035 | * |
| 4036 | * @return string |
| 4037 | */ |
| 4038 | protected function getRootstatCachekey() |
| 4039 | { |
| 4040 | return md5($this->root . (isset($this->options['alias']) ? $this->options['alias'] : '')); |
| 4041 | } |
| 4042 | |
| 4043 | /** |
| 4044 | * Return new unique name based on file name and suffix |
| 4045 | * |
| 4046 | * @param $dir |
| 4047 | * @param $name |
| 4048 | * @param string $suffix suffix append to name |
| 4049 | * @param bool $checkNum |
| 4050 | * @param int $start |
| 4051 | * |
| 4052 | * @return string |
| 4053 | * @internal param string $path file path |
| 4054 | * @author Dmitry (dio) Levashov |
| 4055 | */ |
| 4056 | public function uniqueName($dir, $name, $suffix = ' copy', $checkNum = true, $start = 1) |
| 4057 | { |
| 4058 | static $lasts = null; |
| 4059 | |
| 4060 | if ($lasts === null) { |
| 4061 | $lasts = array(); |
| 4062 | } |
| 4063 | |
| 4064 | $ext = ''; |
| 4065 | |
| 4066 | $splits = elFinder::splitFileExtention($name); |
| 4067 | if ($splits[1]) { |
| 4068 | $ext = '.' . $splits[1]; |
| 4069 | $name = $splits[0]; |
| 4070 | } |
| 4071 | |
| 4072 | if ($checkNum && preg_match('/(' . preg_quote($suffix, '/') . ')(\d*)$/i', $name, $m)) { |
| 4073 | $i = (int)$m[2]; |
| 4074 | $name = substr($name, 0, strlen($name) - strlen($m[2])); |
| 4075 | } else { |
| 4076 | $i = $start; |
| 4077 | $name .= $suffix; |
| 4078 | } |
| 4079 | $max = $i + 100000; |
| 4080 | |
| 4081 | if (isset($lasts[$name])) { |
| 4082 | $i = max($i, $lasts[$name]); |
| 4083 | } |
| 4084 | |
| 4085 | while ($i <= $max) { |
| 4086 | $n = $name . ($i > 0 ? sprintf($this->options['uniqueNumFormat'], $i) : '') . $ext; |
| 4087 | |
| 4088 | if (!$this->isNameExists($this->joinPathCE($dir, $n))) { |
| 4089 | $this->clearcache(); |
| 4090 | $lasts[$name] = ++$i; |
| 4091 | return $n; |
| 4092 | } |
| 4093 | $i++; |
| 4094 | } |
| 4095 | return $name . md5($dir) . $ext; |
| 4096 | } |
| 4097 | |
| 4098 | /** |
| 4099 | * Converts character encoding from UTF-8 to server's one |
| 4100 | * |
| 4101 | * @param mixed $var target string or array var |
| 4102 | * @param bool $restoreLocale do retore global locale, default is false |
| 4103 | * @param string $unknown replaces character for unknown |
| 4104 | * |
| 4105 | * @return mixed |
| 4106 | * @author Naoki Sawada |
| 4107 | */ |
| 4108 | public function convEncIn($var = null, $restoreLocale = false, $unknown = '_') |
| 4109 | { |
| 4110 | return (!$this->encoding) ? $var : $this->convEnc($var, 'UTF-8', $this->encoding, $this->options['locale'], $restoreLocale, $unknown); |
| 4111 | } |
| 4112 | |
| 4113 | /** |
| 4114 | * Converts character encoding from server's one to UTF-8 |
| 4115 | * |
| 4116 | * @param mixed $var target string or array var |
| 4117 | * @param bool $restoreLocale do retore global locale, default is true |
| 4118 | * @param string $unknown replaces character for unknown |
| 4119 | * |
| 4120 | * @return mixed |
| 4121 | * @author Naoki Sawada |
| 4122 | */ |
| 4123 | public function convEncOut($var = null, $restoreLocale = true, $unknown = '_') |
| 4124 | { |
| 4125 | return (!$this->encoding) ? $var : $this->convEnc($var, $this->encoding, 'UTF-8', $this->options['locale'], $restoreLocale, $unknown); |
| 4126 | } |
| 4127 | |
| 4128 | /** |
| 4129 | * Converts character encoding (base function) |
| 4130 | * |
| 4131 | * @param mixed $var target string or array var |
| 4132 | * @param string $from from character encoding |
| 4133 | * @param string $to to character encoding |
| 4134 | * @param string $locale local locale |
| 4135 | * @param $restoreLocale |
| 4136 | * @param string $unknown replaces character for unknown |
| 4137 | * |
| 4138 | * @return mixed |
| 4139 | */ |
| 4140 | protected function convEnc($var, $from, $to, $locale, $restoreLocale, $unknown = '_') |
| 4141 | { |
| 4142 | if (strtoupper($from) !== strtoupper($to)) { |
| 4143 | if ($locale) { |
| 4144 | setlocale(LC_ALL, $locale); |
| 4145 | } |
| 4146 | if (is_array($var)) { |
| 4147 | $_ret = array(); |
| 4148 | foreach ($var as $_k => $_v) { |
| 4149 | $_ret[$_k] = $this->convEnc($_v, $from, $to, '', false, $unknown = '_'); |
| 4150 | } |
| 4151 | $var = $_ret; |
| 4152 | } else { |
| 4153 | $_var = false; |
| 4154 | if (is_string($var)) { |
| 4155 | $_var = $var; |
| 4156 | $errlev = error_reporting(); |
| 4157 | error_reporting($errlev ^ E_NOTICE); |
| 4158 | if (false !== ($_var = iconv($from, $to . '//TRANSLIT', $_var))) { |
| 4159 | $_var = str_replace('?', $unknown, $_var); |
| 4160 | } |
| 4161 | error_reporting($errlev); |
| 4162 | } |
| 4163 | if ($_var !== false) { |
| 4164 | $var = $_var; |
| 4165 | } |
| 4166 | } |
| 4167 | if ($restoreLocale) { |
| 4168 | setlocale(LC_ALL, elFinder::$locale); |
| 4169 | } |
| 4170 | } |
| 4171 | return $var; |
| 4172 | } |
| 4173 | |
| 4174 | /** |
| 4175 | * Normalize MIME-Type by options['mimeMap'] |
| 4176 | * |
| 4177 | * @param string $type MIME-Type |
| 4178 | * @param string $name Filename |
| 4179 | * @param string $ext File extention without first dot (optional) |
| 4180 | * |
| 4181 | * @return string Normalized MIME-Type |
| 4182 | */ |
| 4183 | public function mimeTypeNormalize($type, $name, $ext = '') |
| 4184 | { |
| 4185 | if ($ext === '') { |
| 4186 | $ext = (false === $pos = strrpos($name, '.')) ? '' : substr($name, $pos + 1); |
| 4187 | } |
| 4188 | $_checkKey = strtolower($ext . ':' . $type); |
| 4189 | if ($type === '') { |
| 4190 | $_keylen = strlen($_checkKey); |
| 4191 | foreach ($this->options['mimeMap'] as $_key => $_type) { |
| 4192 | if (substr($_key, 0, $_keylen) === $_checkKey) { |
| 4193 | $type = $_type; |
| 4194 | break; |
| 4195 | } |
| 4196 | } |
| 4197 | } else if (isset($this->options['mimeMap'][$_checkKey])) { |
| 4198 | $type = $this->options['mimeMap'][$_checkKey]; |
| 4199 | } else { |
| 4200 | $_checkKey = strtolower($ext . ':*'); |
| 4201 | if (isset($this->options['mimeMap'][$_checkKey])) { |
| 4202 | $type = $this->options['mimeMap'][$_checkKey]; |
| 4203 | } else { |
| 4204 | $_checkKey = strtolower('*:' . $type); |
| 4205 | if (isset($this->options['mimeMap'][$_checkKey])) { |
| 4206 | $type = $this->options['mimeMap'][$_checkKey]; |
| 4207 | } |
| 4208 | } |
| 4209 | } |
| 4210 | return $type; |
| 4211 | } |
| 4212 | |
| 4213 | /*********************** util mainly for inheritance class *********************/ |
| 4214 | |
| 4215 | /** |
| 4216 | * Get temporary filename. Tempfile will be removed when after script execution finishes or exit() is called. |
| 4217 | * When needing the unique file to a path, give $path to parameter. |
| 4218 | * |
| 4219 | * @param string $path for get unique file to a path |
| 4220 | * |
| 4221 | * @return string|false |
| 4222 | * @author Naoki Sawada |
| 4223 | */ |
| 4224 | protected function getTempFile($path = '') |
| 4225 | { |
| 4226 | static $cache = array(); |
| 4227 | |
| 4228 | $key = ''; |
| 4229 | if ($path !== '') { |
| 4230 | $key = $this->id . '#' . $path; |
| 4231 | if (isset($cache[$key])) { |
| 4232 | return $cache[$key]; |
| 4233 | } |
| 4234 | } |
| 4235 | |
| 4236 | if ($tmpdir = $this->getTempPath()) { |
| 4237 | $name = tempnam($tmpdir, 'ELF'); |
| 4238 | if ($key) { |
| 4239 | $cache[$key] = $name; |
| 4240 | } |
| 4241 | // register auto delete on shutdown |
| 4242 | $GLOBALS['elFinderTempFiles'][$name] = true; |
| 4243 | return $name; |
| 4244 | } |
| 4245 | |
| 4246 | return false; |
| 4247 | } |
| 4248 | |
| 4249 | /** |
| 4250 | * File path of local server side work file path |
| 4251 | * |
| 4252 | * @param string $path path need convert encoding to server encoding |
| 4253 | * |
| 4254 | * @return string |
| 4255 | * @author Naoki Sawada |
| 4256 | */ |
| 4257 | protected function getWorkFile($path) |
| 4258 | { |
| 4259 | if ($wfp = $this->tmpfile()) { |
| 4260 | if ($fp = $this->_fopen($path)) { |
| 4261 | while (!feof($fp)) { |
| 4262 | fwrite($wfp, fread($fp, 8192)); |
| 4263 | } |
| 4264 | $info = stream_get_meta_data($wfp); |
| 4265 | fclose($wfp); |
| 4266 | if ($info && !empty($info['uri'])) { |
| 4267 | return $info['uri']; |
| 4268 | } |
| 4269 | } |
| 4270 | } |
| 4271 | return false; |
| 4272 | } |
| 4273 | |
| 4274 | /** |
| 4275 | * Get image size array with `dimensions` |
| 4276 | * |
| 4277 | * @param string $path path need convert encoding to server encoding |
| 4278 | * @param string $mime file mime type |
| 4279 | * |
| 4280 | * @return array|false |
| 4281 | * @throws ImagickException |
| 4282 | * @throws elFinderAbortException |
| 4283 | */ |
| 4284 | public function getImageSize($path, $mime = '') |
| 4285 | { |
| 4286 | $size = false; |
| 4287 | if ($mime === '' || strtolower(substr($mime, 0, 5)) === 'image') { |
| 4288 | if ($work = $this->getWorkFile($path)) { |
| 4289 | if ($size = getimagesize($work)) { |
| 4290 | $size['dimensions'] = $size[0] . 'x' . $size[1]; |
| 4291 | $srcfp = fopen($work, 'rb'); |
| 4292 | $cArgs = elFinder::$currentArgs; |
| 4293 | if (!empty($cArgs['target']) && $subImgLink = $this->getSubstituteImgLink($cArgs['target'], $size, $srcfp)) { |
| 4294 | $size['url'] = $subImgLink; |
| 4295 | } |
| 4296 | } |
| 4297 | } |
| 4298 | is_file($work) && unlink($work); |
| 4299 | } |
| 4300 | return $size; |
| 4301 | } |
| 4302 | |
| 4303 | /** |
| 4304 | * Delete dirctory trees |
| 4305 | * |
| 4306 | * @param string $localpath path need convert encoding to server encoding |
| 4307 | * |
| 4308 | * @return boolean |
| 4309 | * @throws elFinderAbortException |
| 4310 | * @author Naoki Sawada |
| 4311 | */ |
| 4312 | protected function delTree($localpath) |
| 4313 | { |
| 4314 | foreach ($this->_scandir($localpath) as $p) { |
| 4315 | elFinder::checkAborted(); |
| 4316 | $stat = $this->stat($this->convEncOut($p)); |
| 4317 | $this->convEncIn(); |
| 4318 | ($stat['mime'] === 'directory') ? $this->delTree($p) : $this->_unlink($p); |
| 4319 | } |
| 4320 | $res = $this->_rmdir($localpath); |
| 4321 | $res && $this->clearstatcache(); |
| 4322 | return $res; |
| 4323 | } |
| 4324 | |
| 4325 | /** |
| 4326 | * Copy items to a new temporary directory on the local server |
| 4327 | * |
| 4328 | * @param array $hashes target hashes |
| 4329 | * @param string $dir destination directory (for recurcive) |
| 4330 | * @param string $canLink it can use link() (for recurcive) |
| 4331 | * |
| 4332 | * @return string|false saved path name |
| 4333 | * @throws elFinderAbortException |
| 4334 | * @author Naoki Sawada |
| 4335 | */ |
| 4336 | protected function getItemsInHand($hashes, $dir = null, $canLink = null) |
| 4337 | { |
| 4338 | static $banChrs = null; |
| 4339 | static $totalSize = 0; |
| 4340 | |
| 4341 | if (is_null($banChrs)) { |
| 4342 | $banChrs = DIRECTORY_SEPARATOR !== '/'? array('\\', '/', ':', '*', '?', '"', '<', '>', '|') : array('\\', '/'); |
| 4343 | } |
| 4344 | |
| 4345 | if (is_null($dir)) { |
| 4346 | $totalSize = 0; |
| 4347 | if (!$tmpDir = $this->getTempPath()) { |
| 4348 | return false; |
| 4349 | } |
| 4350 | $dir = tempnam($tmpDir, 'elf'); |
| 4351 | if (!unlink($dir) || !mkdir($dir, 0700, true)) { |
| 4352 | return false; |
| 4353 | } |
| 4354 | register_shutdown_function(array($this, 'rmdirRecursive'), $dir); |
| 4355 | } |
| 4356 | if (is_null($canLink)) { |
| 4357 | $canLink = ($this instanceof elFinderVolumeLocalFileSystem); |
| 4358 | } |
| 4359 | elFinder::checkAborted(); |
| 4360 | $res = true; |
| 4361 | $files = array(); |
| 4362 | foreach ($hashes as $hash) { |
| 4363 | if (($file = $this->file($hash)) == false) { |
| 4364 | continue; |
| 4365 | } |
| 4366 | if (!$file['read']) { |
| 4367 | continue; |
| 4368 | } |
| 4369 | |
| 4370 | $name = $file['name']; |
| 4371 | // remove ctrl characters |
| 4372 | $name = preg_replace('/[[:cntrl:]]+/', '', $name); |
| 4373 | // replace ban characters |
| 4374 | $name = str_replace($banChrs, '_', $name); |
| 4375 | |
| 4376 | // for call from search results |
| 4377 | if (isset($files[$name])) { |
| 4378 | $name = preg_replace('/^(.*?)(\..*)?$/', '$1_' . $files[$name]++ . '$2', $name); |
| 4379 | } else { |
| 4380 | $files[$name] = 1; |
| 4381 | } |
| 4382 | $target = $dir . DIRECTORY_SEPARATOR . $name; |
| 4383 | |
| 4384 | if ($file['mime'] === 'directory') { |
| 4385 | $chashes = array(); |
| 4386 | $_files = $this->scandir($hash); |
| 4387 | foreach ($_files as $_file) { |
| 4388 | if ($file['read']) { |
| 4389 | $chashes[] = $_file['hash']; |
| 4390 | } |
| 4391 | } |
| 4392 | if (($res = mkdir($target, 0700, true)) && $chashes) { |
| 4393 | $res = $this->getItemsInHand($chashes, $target, $canLink); |
| 4394 | } |
| 4395 | if (!$res) { |
| 4396 | break; |
| 4397 | } |
| 4398 | !empty($file['ts']) && touch($target, $file['ts']); |
| 4399 | } else { |
| 4400 | $path = $this->decode($hash); |
| 4401 | if (!$canLink || !($canLink = $this->localFileSystemSymlink($path, $target))) { |
| 4402 | if (file_exists($target)) { |
| 4403 | unlink($target); |
| 4404 | } |
| 4405 | if ($fp = $this->fopenCE($path)) { |
| 4406 | if ($tfp = fopen($target, 'wb')) { |
| 4407 | $totalSize += stream_copy_to_stream($fp, $tfp); |
| 4408 | fclose($tfp); |
| 4409 | } |
| 4410 | !empty($file['ts']) && touch($target, $file['ts']); |
| 4411 | $this->fcloseCE($fp, $path); |
| 4412 | } |
| 4413 | } else { |
| 4414 | $totalSize += filesize($path); |
| 4415 | } |
| 4416 | if ($this->options['maxArcFilesSize'] > 0 && $this->options['maxArcFilesSize'] < $totalSize) { |
| 4417 | $res = $this->setError(elFinder::ERROR_ARC_MAXSIZE); |
| 4418 | } |
| 4419 | } |
| 4420 | } |
| 4421 | return $res ? $dir : false; |
| 4422 | } |
| 4423 | |
| 4424 | /*********************** file stat *********************/ |
| 4425 | |
| 4426 | /** |
| 4427 | * Check file attribute |
| 4428 | * |
| 4429 | * @param string $path file path |
| 4430 | * @param string $name attribute name (read|write|locked|hidden) |
| 4431 | * @param bool $val attribute value returned by file system |
| 4432 | * @param bool $isDir path is directory (true: directory, false: file) |
| 4433 | * |
| 4434 | * @return bool |
| 4435 | * @author Dmitry (dio) Levashov |
| 4436 | **/ |
| 4437 | protected function attr($path, $name, $val = null, $isDir = null) |
| 4438 | { |
| 4439 | if (!isset($this->defaults[$name])) { |
| 4440 | return false; |
| 4441 | } |
| 4442 | |
| 4443 | $relpath = $this->relpathCE($path); |
| 4444 | if ($this->separator !== '/') { |
| 4445 | $relpath = str_replace($this->separator, '/', $relpath); |
| 4446 | } |
| 4447 | $relpath = '/' . $relpath; |
| 4448 | |
| 4449 | $perm = null; |
| 4450 | |
| 4451 | if ($this->access) { |
| 4452 | $perm = call_user_func($this->access, $name, $path, $this->options['accessControlData'], $this, $isDir, $relpath); |
| 4453 | if ($perm !== null) { |
| 4454 | return !!$perm; |
| 4455 | } |
| 4456 | } |
| 4457 | |
| 4458 | foreach ($this->attributes as $attrs) { |
| 4459 | if (isset($attrs[$name]) && isset($attrs['pattern']) && preg_match($attrs['pattern'], $relpath)) { |
| 4460 | $perm = $attrs[$name]; |
| 4461 | break; |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | return $perm === null ? (is_null($val) ? $this->defaults[$name] : $val) : !!$perm; |
| 4466 | } |
| 4467 | |
| 4468 | /** |
| 4469 | * Return true if file with given name can be created in given folder. |
| 4470 | * |
| 4471 | * @param string $dir parent dir path |
| 4472 | * @param string $name new file name |
| 4473 | * @param null $isDir |
| 4474 | * |
| 4475 | * @return bool |
| 4476 | * @author Dmitry (dio) Levashov |
| 4477 | */ |
| 4478 | protected function allowCreate($dir, $name, $isDir = null) |
| 4479 | { |
| 4480 | return $this->attr($this->joinPathCE($dir, $name), 'write', true, $isDir); |
| 4481 | } |
| 4482 | |
| 4483 | /** |
| 4484 | * Return true if file MIME type can save with check uploadOrder config. |
| 4485 | * |
| 4486 | * @param string $mime |
| 4487 | * |
| 4488 | * @return boolean |
| 4489 | */ |
| 4490 | protected function allowPutMime($mime) |
| 4491 | { |
| 4492 | // logic based on http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order |
| 4493 | $allow = $this->mimeAccepted($mime, $this->uploadAllow, null); |
| 4494 | $deny = $this->mimeAccepted($mime, $this->uploadDeny, null); |
| 4495 | if (strtolower($this->uploadOrder[0]) == 'allow') { // array('allow', 'deny'), default is to 'deny' |
| 4496 | $res = false; // default is deny |
| 4497 | if (!$deny && ($allow === true)) { // match only allow |
| 4498 | $res = true; |
| 4499 | }// else (both match | no match | match only deny) { deny } |
| 4500 | } else { // array('deny', 'allow'), default is to 'allow' - this is the default rule |
| 4501 | $res = true; // default is allow |
| 4502 | if (($deny === true) && !$allow) { // match only deny |
| 4503 | $res = false; |
| 4504 | } // else (both match | no match | match only allow) { allow } |
| 4505 | } |
| 4506 | return $res; |
| 4507 | } |
| 4508 | |
| 4509 | /** |
| 4510 | * Return fileinfo |
| 4511 | * |
| 4512 | * @param string $path file cache |
| 4513 | * |
| 4514 | * @return array|bool |
| 4515 | * @author Dmitry (dio) Levashov |
| 4516 | **/ |
| 4517 | protected function stat($path) |
| 4518 | { |
| 4519 | if ($path === false || is_null($path)) { |
| 4520 | return false; |
| 4521 | } |
| 4522 | $is_root = ($path == $this->root); |
| 4523 | if ($is_root) { |
| 4524 | $rootKey = $this->getRootstatCachekey(); |
| 4525 | if ($this->sessionCaching['rootstat'] && !isset($this->sessionCache['rootstat'])) { |
| 4526 | $this->sessionCache['rootstat'] = array(); |
| 4527 | } |
| 4528 | if (!isset($this->cache[$path]) && !$this->isMyReload()) { |
| 4529 | // need $path as key for netmount/netunmount |
| 4530 | if ($this->sessionCaching['rootstat'] && isset($this->sessionCache['rootstat'][$rootKey])) { |
| 4531 | if ($ret = $this->sessionCache['rootstat'][$rootKey]) { |
| 4532 | if ($this->options['rootRev'] === $ret['rootRev']) { |
| 4533 | if (isset($this->options['phash'])) { |
| 4534 | $ret['isroot'] = 1; |
| 4535 | $ret['phash'] = $this->options['phash']; |
| 4536 | } |
| 4537 | return $ret; |
| 4538 | } |
| 4539 | } |
| 4540 | } |
| 4541 | } |
| 4542 | } |
| 4543 | $rootSessCache = false; |
| 4544 | if (isset($this->cache[$path])) { |
| 4545 | $ret = $this->cache[$path]; |
| 4546 | } else { |
| 4547 | if ($is_root && !empty($this->options['rapidRootStat']) && is_array($this->options['rapidRootStat']) && !$this->needOnline) { |
| 4548 | $ret = $this->updateCache($path, $this->options['rapidRootStat'], true); |
| 4549 | } else { |
| 4550 | $ret = $this->updateCache($path, $this->convEncOut($this->_stat($this->convEncIn($path))), true); |
| 4551 | if ($is_root && !empty($rootKey) && $this->sessionCaching['rootstat']) { |
| 4552 | $rootSessCache = true; |
| 4553 | } |
| 4554 | } |
| 4555 | } |
| 4556 | if ($is_root) { |
| 4557 | if ($ret) { |
| 4558 | $this->rootModified = false; |
| 4559 | if ($rootSessCache) { |
| 4560 | $this->sessionCache['rootstat'][$rootKey] = $ret; |
| 4561 | } |
| 4562 | if (isset($this->options['phash'])) { |
| 4563 | $ret['isroot'] = 1; |
| 4564 | $ret['phash'] = $this->options['phash']; |
| 4565 | } |
| 4566 | } else if (!empty($rootKey) && $this->sessionCaching['rootstat']) { |
| 4567 | unset($this->sessionCache['rootstat'][$rootKey]); |
| 4568 | } |
| 4569 | } |
| 4570 | return $ret; |
| 4571 | } |
| 4572 | |
| 4573 | /** |
| 4574 | * Get root stat extra key values |
| 4575 | * |
| 4576 | * @return array stat extras |
| 4577 | * @author Naoki Sawada |
| 4578 | */ |
| 4579 | protected function getRootStatExtra() |
| 4580 | { |
| 4581 | $stat = array(); |
| 4582 | if ($this->rootName) { |
| 4583 | $stat['name'] = $this->rootName; |
| 4584 | } |
| 4585 | $stat['rootRev'] = $this->options['rootRev']; |
| 4586 | $stat['options'] = $this->options(null); |
| 4587 | return $stat; |
| 4588 | } |
| 4589 | |
| 4590 | /** |
| 4591 | * Return fileinfo based on filename |
| 4592 | * For item ID based path file system |
| 4593 | * Please override if needed on each drivers |
| 4594 | * |
| 4595 | * @param string $path file cache |
| 4596 | * |
| 4597 | * @return array |
| 4598 | */ |
| 4599 | protected function isNameExists($path) |
| 4600 | { |
| 4601 | return $this->stat($path); |
| 4602 | } |
| 4603 | |
| 4604 | /** |
| 4605 | * Put file stat in cache and return it |
| 4606 | * |
| 4607 | * @param string $path file path |
| 4608 | * @param array $stat file stat |
| 4609 | * |
| 4610 | * @return array |
| 4611 | * @author Dmitry (dio) Levashov |
| 4612 | **/ |
| 4613 | protected function updateCache($path, $stat) |
| 4614 | { |
| 4615 | if (empty($stat) || !is_array($stat)) { |
| 4616 | return $this->cache[$path] = array(); |
| 4617 | } |
| 4618 | |
| 4619 | if (func_num_args() > 2) { |
| 4620 | $fromStat = func_get_arg(2); |
| 4621 | } else { |
| 4622 | $fromStat = false; |
| 4623 | } |
| 4624 | |
| 4625 | $stat['hash'] = $this->encode($path); |
| 4626 | |
| 4627 | $root = $path == $this->root; |
| 4628 | $parent = ''; |
| 4629 | |
| 4630 | if ($root) { |
| 4631 | $stat = array_merge($stat, $this->getRootStatExtra()); |
| 4632 | } else { |
| 4633 | if (!isset($stat['name']) || $stat['name'] === '') { |
| 4634 | $stat['name'] = $this->basenameCE($path); |
| 4635 | } |
| 4636 | if (empty($stat['phash'])) { |
| 4637 | $parent = $this->dirnameCE($path); |
| 4638 | $stat['phash'] = $this->encode($parent); |
| 4639 | } else { |
| 4640 | $parent = $this->decode($stat['phash']); |
| 4641 | } |
| 4642 | } |
| 4643 | |
| 4644 | // name check |
| 4645 | if (isset($stat['name']) && !$jeName = json_encode($stat['name'])) { |
| 4646 | return $this->cache[$path] = array(); |
| 4647 | } |
| 4648 | // fix name if required |
| 4649 | if ($this->options['utf8fix'] && $this->options['utf8patterns'] && $this->options['utf8replace']) { |
| 4650 | $stat['name'] = json_decode(str_replace($this->options['utf8patterns'], $this->options['utf8replace'], $jeName)); |
| 4651 | } |
| 4652 | |
| 4653 | if (!isset($stat['size'])) { |
| 4654 | $stat['size'] = 'unknown'; |
| 4655 | } |
| 4656 | |
| 4657 | $mime = isset($stat['mime']) ? $stat['mime'] : ''; |
| 4658 | if ($isDir = ($mime === 'directory')) { |
| 4659 | $stat['volumeid'] = $this->id; |
| 4660 | } else { |
| 4661 | if (empty($stat['mime']) || $stat['size'] == 0) { |
| 4662 | $stat['mime'] = $this->mimetype($stat['name'], true, $stat['size'], $mime); |
| 4663 | } else { |
| 4664 | $stat['mime'] = $this->mimeTypeNormalize($stat['mime'], $stat['name']); |
| 4665 | } |
| 4666 | } |
| 4667 | |
| 4668 | $stat['read'] = intval($this->attr($path, 'read', isset($stat['read']) ? !!$stat['read'] : null, $isDir)); |
| 4669 | $stat['write'] = intval($this->attr($path, 'write', isset($stat['write']) ? !!$stat['write'] : null, $isDir)); |
| 4670 | if ($root) { |
| 4671 | $stat['locked'] = 1; |
| 4672 | if ($this->options['type'] !== '') { |
| 4673 | $stat['type'] = $this->options['type']; |
| 4674 | } |
| 4675 | } else { |
| 4676 | // lock when parent directory is not writable |
| 4677 | if (!isset($stat['locked'])) { |
| 4678 | $pstat = $this->stat($parent); |
| 4679 | if (isset($pstat['write']) && !$pstat['write']) { |
| 4680 | $stat['locked'] = true; |
| 4681 | } |
| 4682 | } |
| 4683 | if ($this->attr($path, 'locked', isset($stat['locked']) ? !!$stat['locked'] : null, $isDir)) { |
| 4684 | $stat['locked'] = 1; |
| 4685 | } else { |
| 4686 | unset($stat['locked']); |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | if ($root) { |
| 4691 | unset($stat['hidden']); |
| 4692 | } elseif ($this->attr($path, 'hidden', isset($stat['hidden']) ? !!$stat['hidden'] : null, $isDir) |
| 4693 | || !$this->mimeAccepted($stat['mime'])) { |
| 4694 | $stat['hidden'] = 1; |
| 4695 | } else { |
| 4696 | unset($stat['hidden']); |
| 4697 | } |
| 4698 | |
| 4699 | if ($stat['read'] && empty($stat['hidden'])) { |
| 4700 | |
| 4701 | if ($isDir) { |
| 4702 | // caching parent's subdirs |
| 4703 | if ($parent) { |
| 4704 | $this->updateSubdirsCache($parent, true); |
| 4705 | } |
| 4706 | // for dir - check for subdirs |
| 4707 | if ($this->options['checkSubfolders']) { |
| 4708 | if (!isset($stat['dirs']) && intval($this->options['checkSubfolders']) === -1) { |
| 4709 | $stat['dirs'] = -1; |
| 4710 | } |
| 4711 | if (isset($stat['dirs'])) { |
| 4712 | if ($stat['dirs']) { |
| 4713 | if ($stat['dirs'] == -1) { |
| 4714 | $stat['dirs'] = ($this->sessionCaching['subdirs'] && isset($this->sessionCache['subdirs'][$path])) ? (int)$this->sessionCache['subdirs'][$path] : -1; |
| 4715 | } else { |
| 4716 | $stat['dirs'] = 1; |
| 4717 | } |
| 4718 | } else { |
| 4719 | unset($stat['dirs']); |
| 4720 | } |
| 4721 | } elseif (!empty($stat['alias']) && !empty($stat['target'])) { |
| 4722 | $stat['dirs'] = isset($this->cache[$stat['target']]) |
| 4723 | ? intval(isset($this->cache[$stat['target']]['dirs'])) |
| 4724 | : $this->subdirsCE($stat['target']); |
| 4725 | |
| 4726 | } elseif ($this->subdirsCE($path)) { |
| 4727 | $stat['dirs'] = 1; |
| 4728 | } |
| 4729 | } else { |
| 4730 | $stat['dirs'] = 1; |
| 4731 | } |
| 4732 | if ($this->options['dirUrlOwn'] === true) { |
| 4733 | // Set `null` to use the client option `commandsOptions.info.nullUrlDirLinkSelf = true` |
| 4734 | $stat['url'] = null; |
| 4735 | } else if ($this->options['dirUrlOwn'] === 'hide') { |
| 4736 | // to hide link in info dialog of the elFinder client |
| 4737 | $stat['url'] = ''; |
| 4738 | } |
| 4739 | } else { |
| 4740 | // for files - check for thumbnails |
| 4741 | $p = isset($stat['target']) ? $stat['target'] : $path; |
| 4742 | if ($this->tmbURL && !isset($stat['tmb']) && $this->canCreateTmb($p, $stat)) { |
| 4743 | $tmb = $this->gettmb($p, $stat); |
| 4744 | $stat['tmb'] = $tmb ? $tmb : 1; |
| 4745 | } |
| 4746 | |
| 4747 | } |
| 4748 | if (!isset($stat['url']) && $this->URL && $this->encoding) { |
| 4749 | $_path = $this->_getRelativePath($path); |
| 4750 | $_path = str_replace($this->separator, '/', $_path); |
| 4751 | $_path = (substr(PHP_OS, 0, 3) === 'WIN') ? $_path : $this->convEncIn($_path, true); |
| 4752 | $_path = str_replace('%2F', '/', rawurlencode($_path)); |
| 4753 | $stat['url'] = $this->_buildFileUrl($_path); |
| 4754 | } |
| 4755 | } else { |
| 4756 | if ($isDir) { |
| 4757 | unset($stat['dirs']); |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | if (!empty($stat['alias']) && !empty($stat['target'])) { |
| 4762 | $stat['thash'] = $this->encode($stat['target']); |
| 4763 | //$this->cache[$stat['target']] = $stat; |
| 4764 | unset($stat['target']); |
| 4765 | } |
| 4766 | |
| 4767 | $this->cache[$path] = $stat; |
| 4768 | |
| 4769 | if (!$fromStat && $root && $this->sessionCaching['rootstat']) { |
| 4770 | // to update session cache |
| 4771 | $this->stat($path); |
| 4772 | } |
| 4773 | |
| 4774 | return $stat; |
| 4775 | } |
| 4776 | |
| 4777 | /** |
| 4778 | * Get stat for folder content and put in cache |
| 4779 | * |
| 4780 | * @param string $path |
| 4781 | * |
| 4782 | * @return void |
| 4783 | * @author Dmitry (dio) Levashov |
| 4784 | **/ |
| 4785 | protected function cacheDir($path) |
| 4786 | { |
| 4787 | $this->dirsCache[$path] = array(); |
| 4788 | $hasDir = false; |
| 4789 | |
| 4790 | foreach ($this->scandirCE($path) as $p) { |
| 4791 | if (($stat = $this->stat($p)) && empty($stat['hidden'])) { |
| 4792 | if (!$hasDir && $stat['mime'] === 'directory') { |
| 4793 | $hasDir = true; |
| 4794 | } |
| 4795 | $this->dirsCache[$path][] = $p; |
| 4796 | } |
| 4797 | } |
| 4798 | |
| 4799 | $this->updateSubdirsCache($path, $hasDir); |
| 4800 | } |
| 4801 | |
| 4802 | /** |
| 4803 | * Clean cache |
| 4804 | * |
| 4805 | * @return void |
| 4806 | * @author Dmitry (dio) Levashov |
| 4807 | **/ |
| 4808 | protected function clearcache() |
| 4809 | { |
| 4810 | $this->cache = $this->dirsCache = array(); |
| 4811 | } |
| 4812 | |
| 4813 | /** |
| 4814 | * Return file mimetype |
| 4815 | * |
| 4816 | * @param string $path file path |
| 4817 | * @param string|bool $name |
| 4818 | * @param integer $size |
| 4819 | * @param string $mime was notified from the volume driver |
| 4820 | * |
| 4821 | * @return string |
| 4822 | * @author Dmitry (dio) Levashov |
| 4823 | */ |
| 4824 | protected function mimetype($path, $name = '', $size = null, $mime = null) |
| 4825 | { |
| 4826 | $type = ''; |
| 4827 | $nameCheck = false; |
| 4828 | |
| 4829 | if ($name === '') { |
| 4830 | $name = $path; |
| 4831 | } else if ($name === true) { |
| 4832 | $name = $path; |
| 4833 | $nameCheck = true; |
| 4834 | } |
| 4835 | if (!$this instanceof elFinderVolumeLocalFileSystem) { |
| 4836 | $nameCheck = true; |
| 4837 | } |
| 4838 | $ext = (false === $pos = strrpos($name, '.')) ? '' : strtolower(substr($name, $pos + 1)); |
| 4839 | if (!$nameCheck && $size === null) { |
| 4840 | $size = file_exists($path) ? filesize($path) : -1; |
| 4841 | } |
| 4842 | if (!$nameCheck && is_readable($path) && $size > 0) { |
| 4843 | // detecting by contents |
| 4844 | if ($this->mimeDetect === 'finfo') { |
| 4845 | $type = finfo_file($this->finfo, $path); |
| 4846 | } else if ($this->mimeDetect === 'mime_content_type') { |
| 4847 | $type = mime_content_type($path); |
| 4848 | } |
| 4849 | if ($type) { |
| 4850 | $type = explode(';', $type); |
| 4851 | $type = trim($type[0]); |
| 4852 | if ($ext && preg_match('~^application/(?:octet-stream|(?:x-)?zip|xml)$~', $type)) { |
| 4853 | // load default MIME table file "mime.types" |
| 4854 | if (!elFinderVolumeDriver::$mimetypesLoaded) { |
| 4855 | elFinderVolumeDriver::loadMimeTypes(); |
| 4856 | } |
| 4857 | if (isset(elFinderVolumeDriver::$mimetypes[$ext])) { |
| 4858 | $type = elFinderVolumeDriver::$mimetypes[$ext]; |
| 4859 | } |
| 4860 | } else if ($ext === 'js' && preg_match('~^text/~', $type)) { |
| 4861 | $type = 'text/javascript'; |
| 4862 | } |
| 4863 | } |
| 4864 | } |
| 4865 | if (!$type) { |
| 4866 | // detecting by filename |
| 4867 | $type = elFinderVolumeDriver::mimetypeInternalDetect($name); |
| 4868 | if ($type === 'unknown') { |
| 4869 | if ($mime) { |
| 4870 | $type = $mime; |
| 4871 | } else { |
| 4872 | $type = ($size == 0) ? 'text/plain' : $this->options['mimeTypeUnknown']; |
| 4873 | } |
| 4874 | } |
| 4875 | } |
| 4876 | |
| 4877 | // mime type normalization |
| 4878 | $type = $this->mimeTypeNormalize($type, $name, $ext); |
| 4879 | |
| 4880 | return $type; |
| 4881 | } |
| 4882 | |
| 4883 | /** |
| 4884 | * Load file of mime.types |
| 4885 | * |
| 4886 | * @param string $mimeTypesFile The mime types file |
| 4887 | */ |
| 4888 | static protected function loadMimeTypes($mimeTypesFile = '') |
| 4889 | { |
| 4890 | if (!elFinderVolumeDriver::$mimetypesLoaded) { |
| 4891 | elFinderVolumeDriver::$mimetypesLoaded = true; |
| 4892 | $file = false; |
| 4893 | if (!empty($mimeTypesFile) && file_exists($mimeTypesFile)) { |
| 4894 | $file = $mimeTypesFile; |
| 4895 | } elseif (elFinder::$defaultMimefile && file_exists(elFinder::$defaultMimefile)) { |
| 4896 | $file = elFinder::$defaultMimefile; |
| 4897 | } elseif (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mime.types')) { |
| 4898 | $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mime.types'; |
| 4899 | } elseif (file_exists(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'mime.types')) { |
| 4900 | $file = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'mime.types'; |
| 4901 | } |
| 4902 | |
| 4903 | if ($file && file_exists($file)) { |
| 4904 | $mimecf = file($file); |
| 4905 | |
| 4906 | foreach ($mimecf as $line_num => $line) { |
| 4907 | if (!preg_match('/^\s*#/', $line)) { |
| 4908 | $mime = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY); |
| 4909 | for ($i = 1, $size = count($mime); $i < $size; $i++) { |
| 4910 | if (!isset(self::$mimetypes[$mime[$i]])) { |
| 4911 | self::$mimetypes[$mime[$i]] = $mime[0]; |
| 4912 | } |
| 4913 | } |
| 4914 | } |
| 4915 | } |
| 4916 | } |
| 4917 | } |
| 4918 | } |
| 4919 | |
| 4920 | /** |
| 4921 | * Detect file mimetype using "internal" method or Loading mime.types with $path = '' |
| 4922 | * |
| 4923 | * @param string $path file path |
| 4924 | * |
| 4925 | * @return string |
| 4926 | * @author Dmitry (dio) Levashov |
| 4927 | **/ |
| 4928 | static protected function mimetypeInternalDetect($path = '') |
| 4929 | { |
| 4930 | // load default MIME table file "mime.types" |
| 4931 | if (!elFinderVolumeDriver::$mimetypesLoaded) { |
| 4932 | elFinderVolumeDriver::loadMimeTypes(); |
| 4933 | } |
| 4934 | $ext = ''; |
| 4935 | if ($path) { |
| 4936 | $pinfo = pathinfo($path); |
| 4937 | $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : ''; |
| 4938 | } |
| 4939 | $res = ($ext && isset(elFinderVolumeDriver::$mimetypes[$ext])) ? elFinderVolumeDriver::$mimetypes[$ext] : 'unknown'; |
| 4940 | // Recursive check if MIME type is unknown with multiple extensions |
| 4941 | if ($res === 'unknown' && strpos($pinfo['filename'], '.')) { |
| 4942 | return elFinderVolumeDriver::mimetypeInternalDetect($pinfo['filename']); |
| 4943 | } else { |
| 4944 | return $res; |
| 4945 | } |
| 4946 | } |
| 4947 | |
| 4948 | /** |
| 4949 | * Return file/total directory size infomation |
| 4950 | * |
| 4951 | * @param string $path file path |
| 4952 | * |
| 4953 | * @return array |
| 4954 | * @throws elFinderAbortException |
| 4955 | * @author Dmitry (dio) Levashov |
| 4956 | */ |
| 4957 | protected function countSize($path) |
| 4958 | { |
| 4959 | |
| 4960 | elFinder::checkAborted(); |
| 4961 | |
| 4962 | $result = array('size' => 0, 'files' => 0, 'dirs' => 0); |
| 4963 | $stat = $this->stat($path); |
| 4964 | |
| 4965 | if (empty($stat) || !$stat['read'] || !empty($stat['hidden'])) { |
| 4966 | $result['size'] = 'unknown'; |
| 4967 | return $result; |
| 4968 | } |
| 4969 | |
| 4970 | if ($stat['mime'] !== 'directory') { |
| 4971 | $result['size'] = intval($stat['size']); |
| 4972 | $result['files'] = 1; |
| 4973 | return $result; |
| 4974 | } |
| 4975 | |
| 4976 | $result['dirs'] = 1; |
| 4977 | $subdirs = $this->options['checkSubfolders']; |
| 4978 | $this->options['checkSubfolders'] = true; |
| 4979 | foreach ($this->getScandir($path) as $stat) { |
| 4980 | if ($isDir = ($stat['mime'] === 'directory' && $stat['read'])) { |
| 4981 | ++$result['dirs']; |
| 4982 | } else { |
| 4983 | ++$result['files']; |
| 4984 | } |
| 4985 | $res = $isDir |
| 4986 | ? $this->countSize($this->decode($stat['hash'])) |
| 4987 | : (isset($stat['size']) ? array('size' => intval($stat['size'])) : array()); |
| 4988 | if (!empty($res['size']) && is_numeric($res['size'])) { |
| 4989 | $result['size'] += $res['size']; |
| 4990 | } |
| 4991 | if (!empty($res['files']) && is_numeric($res['files'])) { |
| 4992 | $result['files'] += $res['files']; |
| 4993 | } |
| 4994 | if (!empty($res['dirs']) && is_numeric($res['dirs'])) { |
| 4995 | $result['dirs'] += $res['dirs']; |
| 4996 | --$result['dirs']; |
| 4997 | } |
| 4998 | } |
| 4999 | $this->options['checkSubfolders'] = $subdirs; |
| 5000 | return $result; |
| 5001 | } |
| 5002 | |
| 5003 | /** |
| 5004 | * Return true if all mimes is directory or files |
| 5005 | * |
| 5006 | * @param string $mime1 mimetype |
| 5007 | * @param string $mime2 mimetype |
| 5008 | * |
| 5009 | * @return bool |
| 5010 | * @author Dmitry (dio) Levashov |
| 5011 | **/ |
| 5012 | protected function isSameType($mime1, $mime2) |
| 5013 | { |
| 5014 | return ($mime1 == 'directory' && $mime1 == $mime2) || ($mime1 != 'directory' && $mime2 != 'directory'); |
| 5015 | } |
| 5016 | |
| 5017 | /** |
| 5018 | * If file has required attr == $val - return file path, |
| 5019 | * If dir has child with has required attr == $val - return child path |
| 5020 | * |
| 5021 | * @param string $path file path |
| 5022 | * @param string $attr attribute name |
| 5023 | * @param bool $val attribute value |
| 5024 | * |
| 5025 | * @return string|false |
| 5026 | * @author Dmitry (dio) Levashov |
| 5027 | **/ |
| 5028 | protected function closestByAttr($path, $attr, $val) |
| 5029 | { |
| 5030 | $stat = $this->stat($path); |
| 5031 | |
| 5032 | if (empty($stat)) { |
| 5033 | return false; |
| 5034 | } |
| 5035 | |
| 5036 | $v = isset($stat[$attr]) ? $stat[$attr] : false; |
| 5037 | |
| 5038 | if ($v == $val) { |
| 5039 | return $path; |
| 5040 | } |
| 5041 | |
| 5042 | return $stat['mime'] == 'directory' |
| 5043 | ? $this->childsByAttr($path, $attr, $val) |
| 5044 | : false; |
| 5045 | } |
| 5046 | |
| 5047 | /** |
| 5048 | * Return first found children with required attr == $val |
| 5049 | * |
| 5050 | * @param string $path file path |
| 5051 | * @param string $attr attribute name |
| 5052 | * @param bool $val attribute value |
| 5053 | * |
| 5054 | * @return string|false |
| 5055 | * @author Dmitry (dio) Levashov |
| 5056 | **/ |
| 5057 | protected function childsByAttr($path, $attr, $val) |
| 5058 | { |
| 5059 | foreach ($this->scandirCE($path) as $p) { |
| 5060 | if (($_p = $this->closestByAttr($p, $attr, $val)) != false) { |
| 5061 | return $_p; |
| 5062 | } |
| 5063 | } |
| 5064 | return false; |
| 5065 | } |
| 5066 | |
| 5067 | protected function isMyReload($target = '', $ARGtarget = '') |
| 5068 | { |
| 5069 | if ($this->rootModified || (!empty($this->ARGS['cmd']) && $this->ARGS['cmd'] === 'parents')) { |
| 5070 | return true; |
| 5071 | } |
| 5072 | if (!empty($this->ARGS['reload'])) { |
| 5073 | if ($ARGtarget === '') { |
| 5074 | $ARGtarget = isset($this->ARGS['target']) ? $this->ARGS['target'] |
| 5075 | : ((isset($this->ARGS['targets']) && is_array($this->ARGS['targets']) && count($this->ARGS['targets']) === 1) ? |
| 5076 | $this->ARGS['targets'][0] : ''); |
| 5077 | } |
| 5078 | if ($ARGtarget !== '') { |
| 5079 | $ARGtarget = strval($ARGtarget); |
| 5080 | if ($target === '') { |
| 5081 | return (strpos($ARGtarget, $this->id) === 0); |
| 5082 | } else { |
| 5083 | $target = strval($target); |
| 5084 | return ($target === $ARGtarget); |
| 5085 | } |
| 5086 | } |
| 5087 | } |
| 5088 | return false; |
| 5089 | } |
| 5090 | |
| 5091 | /** |
| 5092 | * Update subdirs cache data |
| 5093 | * |
| 5094 | * @param string $path |
| 5095 | * @param bool $subdirs |
| 5096 | * |
| 5097 | * @return void |
| 5098 | */ |
| 5099 | protected function updateSubdirsCache($path, $subdirs) |
| 5100 | { |
| 5101 | if (isset($this->cache[$path])) { |
| 5102 | if ($subdirs) { |
| 5103 | $this->cache[$path]['dirs'] = 1; |
| 5104 | } else { |
| 5105 | unset($this->cache[$path]['dirs']); |
| 5106 | } |
| 5107 | } |
| 5108 | if ($this->sessionCaching['subdirs']) { |
| 5109 | $this->sessionCache['subdirs'][$path] = $subdirs; |
| 5110 | } |
| 5111 | if ($this->sessionCaching['rootstat'] && $path == $this->root) { |
| 5112 | unset($this->sessionCache['rootstat'][$this->getRootstatCachekey()]); |
| 5113 | } |
| 5114 | } |
| 5115 | |
| 5116 | /***************** get content *******************/ |
| 5117 | |
| 5118 | /** |
| 5119 | * Return required dir's files info. |
| 5120 | * If onlyMimes is set - return only dirs and files of required mimes |
| 5121 | * |
| 5122 | * @param string $path dir path |
| 5123 | * |
| 5124 | * @return array |
| 5125 | * @author Dmitry (dio) Levashov |
| 5126 | **/ |
| 5127 | protected function getScandir($path) |
| 5128 | { |
| 5129 | $files = array(); |
| 5130 | |
| 5131 | !isset($this->dirsCache[$path]) && $this->cacheDir($path); |
| 5132 | |
| 5133 | foreach ($this->dirsCache[$path] as $p) { |
| 5134 | if (($stat = $this->stat($p)) && empty($stat['hidden'])) { |
| 5135 | $files[] = $stat; |
| 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | return $files; |
| 5140 | } |
| 5141 | |
| 5142 | |
| 5143 | /** |
| 5144 | * Return subdirs tree |
| 5145 | * |
| 5146 | * @param string $path parent dir path |
| 5147 | * @param int $deep tree deep |
| 5148 | * @param string $exclude |
| 5149 | * |
| 5150 | * @return array |
| 5151 | * @author Dmitry (dio) Levashov |
| 5152 | */ |
| 5153 | protected function gettree($path, $deep, $exclude = '') |
| 5154 | { |
| 5155 | $dirs = array(); |
| 5156 | |
| 5157 | !isset($this->dirsCache[$path]) && $this->cacheDir($path); |
| 5158 | |
| 5159 | foreach ($this->dirsCache[$path] as $p) { |
| 5160 | $stat = $this->stat($p); |
| 5161 | |
| 5162 | if ($stat && empty($stat['hidden']) && $p != $exclude && $stat['mime'] == 'directory') { |
| 5163 | $dirs[] = $stat; |
| 5164 | if ($deep > 0 && !empty($stat['dirs'])) { |
| 5165 | $dirs = array_merge($dirs, $this->gettree($p, $deep - 1)); |
| 5166 | } |
| 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | return $dirs; |
| 5171 | } |
| 5172 | |
| 5173 | /** |
| 5174 | * Recursive files search |
| 5175 | * |
| 5176 | * @param string $path dir path |
| 5177 | * @param string $q search string |
| 5178 | * @param array $mimes |
| 5179 | * |
| 5180 | * @return array |
| 5181 | * @throws elFinderAbortException |
| 5182 | * @author Dmitry (dio) Levashov |
| 5183 | */ |
| 5184 | protected function doSearch($path, $q, $mimes) |
| 5185 | { |
| 5186 | $result = array(); |
| 5187 | $matchMethod = empty($this->doSearchCurrentQuery['matchMethod']) ? 'searchMatchName' : $this->doSearchCurrentQuery['matchMethod']; |
| 5188 | $timeout = $this->options['searchTimeout'] ? $this->searchStart + $this->options['searchTimeout'] : 0; |
| 5189 | if ($timeout && $timeout < time()) { |
| 5190 | $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($path))); |
| 5191 | return $result; |
| 5192 | } |
| 5193 | |
| 5194 | foreach ($this->scandirCE($path) as $p) { |
| 5195 | elFinder::extendTimeLimit($this->options['searchTimeout'] + 30); |
| 5196 | |
| 5197 | if ($timeout && ($this->error || $timeout < time())) { |
| 5198 | !$this->error && $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($path))); |
| 5199 | break; |
| 5200 | } |
| 5201 | |
| 5202 | |
| 5203 | $stat = $this->stat($p); |
| 5204 | |
| 5205 | if (!$stat) { // invalid links |
| 5206 | continue; |
| 5207 | } |
| 5208 | |
| 5209 | if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'], $mimes)) { |
| 5210 | continue; |
| 5211 | } |
| 5212 | |
| 5213 | $name = $stat['name']; |
| 5214 | |
| 5215 | if ($this->doSearchCurrentQuery['excludes']) { |
| 5216 | foreach ($this->doSearchCurrentQuery['excludes'] as $exclude) { |
| 5217 | if ($this->stripos($name, $exclude) !== false) { |
| 5218 | continue 2; |
| 5219 | } |
| 5220 | } |
| 5221 | } |
| 5222 | |
| 5223 | if ((!$mimes || $stat['mime'] !== 'directory') && $this->$matchMethod($name, $q, $p) !== false) { |
| 5224 | $stat['path'] = $this->path($stat['hash']); |
| 5225 | if ($this->URL && !isset($stat['url'])) { |
| 5226 | $_path = $this->_getRelativePath($p); |
| 5227 | $path = str_replace($this->separator, '/', $_path); |
| 5228 | if ($this->encoding) { |
| 5229 | $path = str_replace('%2F', '/', rawurlencode($this->convEncIn($path, true))); |
| 5230 | } else { |
| 5231 | $path = str_replace('%2F', '/', rawurlencode($path)); |
| 5232 | } |
| 5233 | $stat['url'] = $this->_buildFileUrl($path); |
| 5234 | } |
| 5235 | |
| 5236 | $result[] = $stat; |
| 5237 | } |
| 5238 | if ($stat['mime'] == 'directory' && $stat['read'] && !isset($stat['alias'])) { |
| 5239 | if (!$this->options['searchExDirReg'] || !preg_match($this->options['searchExDirReg'], $p)) { |
| 5240 | $result = array_merge($result, $this->doSearch($p, $q, $mimes)); |
| 5241 | } |
| 5242 | } |
| 5243 | } |
| 5244 | |
| 5245 | return $result; |
| 5246 | } |
| 5247 | |
| 5248 | /********************** manuipulations ******************/ |
| 5249 | |
| 5250 | /** |
| 5251 | * Copy file/recursive copy dir only in current volume. |
| 5252 | * Return new file path or false. |
| 5253 | * |
| 5254 | * @param string $src source path |
| 5255 | * @param string $dst destination dir path |
| 5256 | * @param string $name new file name (optionaly) |
| 5257 | * |
| 5258 | * @return string|false |
| 5259 | * @throws elFinderAbortException |
| 5260 | * @author Dmitry (dio) Levashov |
| 5261 | */ |
| 5262 | protected function copy($src, $dst, $name) |
| 5263 | { |
| 5264 | |
| 5265 | elFinder::checkAborted(); |
| 5266 | |
| 5267 | $srcStat = $this->stat($src); |
| 5268 | |
| 5269 | if (!empty($srcStat['thash'])) { |
| 5270 | $target = $this->decode($srcStat['thash']); |
| 5271 | if (!$this->inpathCE($target, $this->root)) { |
| 5272 | return $this->setError(elFinder::ERROR_COPY, $this->path($srcStat['hash']), elFinder::ERROR_MKOUTLINK); |
| 5273 | } |
| 5274 | $stat = $this->stat($target); |
| 5275 | $this->clearcache(); |
| 5276 | return $stat && $this->symlinkCE($target, $dst, $name) |
| 5277 | ? $this->joinPathCE($dst, $name) |
| 5278 | : $this->setError(elFinder::ERROR_COPY, $this->path($srcStat['hash'])); |
| 5279 | } |
| 5280 | |
| 5281 | if ($srcStat['mime'] === 'directory') { |
| 5282 | $testStat = $this->isNameExists($this->joinPathCE($dst, $name)); |
| 5283 | $this->clearcache(); |
| 5284 | |
| 5285 | if (($testStat && $testStat['mime'] !== 'directory') || (!$testStat && !$testStat = $this->mkdir($this->encode($dst), $name))) { |
| 5286 | return $this->setError(elFinder::ERROR_COPY, $this->path($srcStat['hash'])); |
| 5287 | } |
| 5288 | |
| 5289 | $dst = $this->decode($testStat['hash']); |
| 5290 | |
| 5291 | // start time |
| 5292 | $stime = microtime(true); |
| 5293 | foreach ($this->getScandir($src) as $stat) { |
| 5294 | if (empty($stat['hidden'])) { |
| 5295 | // current time |
| 5296 | $ctime = microtime(true); |
| 5297 | if (($ctime - $stime) > 2) { |
| 5298 | $stime = $ctime; |
| 5299 | elFinder::checkAborted(); |
| 5300 | } |
| 5301 | $name = $stat['name']; |
| 5302 | $_src = $this->decode($stat['hash']); |
| 5303 | if (!$this->copy($_src, $dst, $name)) { |
| 5304 | $this->remove($dst, true); // fall back |
| 5305 | return $this->setError($this->error, elFinder::ERROR_COPY, $this->_path($src)); |
| 5306 | } |
| 5307 | } |
| 5308 | } |
| 5309 | |
| 5310 | $this->added[] = $testStat; |
| 5311 | |
| 5312 | return $dst; |
| 5313 | } |
| 5314 | |
| 5315 | if ($this->options['copyJoin']) { |
| 5316 | $test = $this->joinPathCE($dst, $name); |
| 5317 | if ($this->isNameExists($test)) { |
| 5318 | $this->remove($test); |
| 5319 | } |
| 5320 | } |
| 5321 | if ($res = $this->convEncOut($this->_copy($this->convEncIn($src), $this->convEncIn($dst), $this->convEncIn($name)))) { |
| 5322 | $path = is_string($res) ? $res : $this->joinPathCE($dst, $name); |
| 5323 | $this->clearstatcache(); |
| 5324 | $this->added[] = $this->stat($path); |
| 5325 | return $path; |
| 5326 | } |
| 5327 | |
| 5328 | return $this->setError(elFinder::ERROR_COPY, $this->path($srcStat['hash'])); |
| 5329 | } |
| 5330 | |
| 5331 | /** |
| 5332 | * Move file |
| 5333 | * Return new file path or false. |
| 5334 | * |
| 5335 | * @param string $src source path |
| 5336 | * @param string $dst destination dir path |
| 5337 | * @param string $name new file name |
| 5338 | * |
| 5339 | * @return string|false |
| 5340 | * @throws elFinderAbortException |
| 5341 | * @author Dmitry (dio) Levashov |
| 5342 | */ |
| 5343 | protected function move($src, $dst, $name) |
| 5344 | { |
| 5345 | $stat = $this->stat($src); |
| 5346 | $stat['realpath'] = $src; |
| 5347 | $this->rmTmb($stat); // can not do rmTmb() after _move() |
| 5348 | $this->clearcache(); |
| 5349 | |
| 5350 | $res = $this->convEncOut($this->_move($this->convEncIn($src), $this->convEncIn($dst), $this->convEncIn($name))); |
| 5351 | // if moving it didn't work try to copy / delete |
| 5352 | if (!$res) { |
| 5353 | if ($this->copy($src, $dst, $name)) { |
| 5354 | $res = $this->remove($src); |
| 5355 | } |
| 5356 | } |
| 5357 | |
| 5358 | if ($res) { |
| 5359 | $this->clearstatcache(); |
| 5360 | if ($stat['mime'] === 'directory') { |
| 5361 | $this->updateSubdirsCache($dst, true); |
| 5362 | } |
| 5363 | $path = is_string($res) ? $res : $this->joinPathCE($dst, $name); |
| 5364 | $this->added[] = $this->stat($path); |
| 5365 | $this->removed[] = $stat; |
| 5366 | return $path; |
| 5367 | } |
| 5368 | |
| 5369 | return $this->setError(elFinder::ERROR_MOVE, $this->path($stat['hash'])); |
| 5370 | } |
| 5371 | |
| 5372 | /** |
| 5373 | * Copy file from another volume. |
| 5374 | * Return new file path or false. |
| 5375 | * |
| 5376 | * @param Object $volume source volume |
| 5377 | * @param string $src source file hash |
| 5378 | * @param string $destination destination dir path |
| 5379 | * @param string $name file name |
| 5380 | * |
| 5381 | * @return string|false |
| 5382 | * @throws elFinderAbortException |
| 5383 | * @author Dmitry (dio) Levashov |
| 5384 | */ |
| 5385 | protected function copyFrom($volume, $src, $destination, $name) |
| 5386 | { |
| 5387 | |
| 5388 | elFinder::checkAborted(); |
| 5389 | |
| 5390 | if (($source = $volume->file($src)) == false) { |
| 5391 | return $this->addError(elFinder::ERROR_COPY, '#' . $src, $volume->error()); |
| 5392 | } |
| 5393 | |
| 5394 | $srcIsDir = ($source['mime'] === 'directory'); |
| 5395 | |
| 5396 | $errpath = $volume->path($source['hash']); |
| 5397 | |
| 5398 | $errors = array(); |
| 5399 | try { |
| 5400 | $thash = $this->encode($destination); |
| 5401 | elFinder::$instance->trigger('paste.copyfrom', array(&$thash, &$name, '', elFinder::$instance, $this), $errors); |
| 5402 | } catch (elFinderTriggerException $e) { |
| 5403 | return $this->addError(elFinder::ERROR_COPY, $name, $errors); |
| 5404 | } |
| 5405 | |
| 5406 | if (!$this->nameAccepted($name, $srcIsDir)) { |
| 5407 | return $this->addError(elFinder::ERROR_COPY, $name, $srcIsDir ? elFinder::ERROR_INVALID_DIRNAME : elFinder::ERROR_INVALID_NAME); |
| 5408 | } |
| 5409 | |
| 5410 | if (!$this->allowCreate($destination, $name, $srcIsDir)) { |
| 5411 | return $this->addError(elFinder::ERROR_COPY, $name, elFinder::ERROR_PERM_DENIED); |
| 5412 | } |
| 5413 | |
| 5414 | if (!$source['read']) { |
| 5415 | return $this->addError(elFinder::ERROR_COPY, $errpath, elFinder::ERROR_PERM_DENIED); |
| 5416 | } |
| 5417 | |
| 5418 | if ($srcIsDir) { |
| 5419 | $test = $this->isNameExists($this->joinPathCE($destination, $name)); |
| 5420 | $this->clearcache(); |
| 5421 | |
| 5422 | if (($test && $test['mime'] != 'directory') || (!$test && !$test = $this->mkdir($this->encode($destination), $name))) { |
| 5423 | return $this->addError(elFinder::ERROR_COPY, $errpath); |
| 5424 | } |
| 5425 | |
| 5426 | //$path = $this->joinPathCE($destination, $name); |
| 5427 | $path = $this->decode($test['hash']); |
| 5428 | |
| 5429 | foreach ($volume->scandir($src) as $entr) { |
| 5430 | $this->copyFrom($volume, $entr['hash'], $path, $entr['name']); |
| 5431 | } |
| 5432 | |
| 5433 | $this->added[] = $test; |
| 5434 | } else { |
| 5435 | // size check |
| 5436 | if (!isset($source['size']) || $source['size'] > $this->uploadMaxSize) { |
| 5437 | return $this->setError(elFinder::ERROR_UPLOAD_FILE_SIZE); |
| 5438 | } |
| 5439 | |
| 5440 | // MIME check |
| 5441 | $mimeByName = $this->mimetype($source['name'], true); |
| 5442 | if ($source['mime'] === $mimeByName) { |
| 5443 | $mimeByName = ''; |
| 5444 | } |
| 5445 | if (!$this->allowPutMime($source['mime']) || ($mimeByName && !$this->allowPutMime($mimeByName))) { |
| 5446 | return $this->addError(elFinder::ERROR_UPLOAD_FILE_MIME, $errpath); |
| 5447 | } |
| 5448 | |
| 5449 | if (strpos($source['mime'], 'image') === 0 && ($dim = $volume->dimensions($src))) { |
| 5450 | if (is_array($dim)) { |
| 5451 | $dim = isset($dim['dim']) ? $dim['dim'] : null; |
| 5452 | } |
| 5453 | if ($dim) { |
| 5454 | $s = explode('x', $dim); |
| 5455 | $source['width'] = $s[0]; |
| 5456 | $source['height'] = $s[1]; |
| 5457 | } |
| 5458 | } |
| 5459 | |
| 5460 | if (($fp = $volume->open($src)) == false |
| 5461 | || ($path = $this->saveCE($fp, $destination, $name, $source)) == false) { |
| 5462 | $fp && $volume->close($fp, $src); |
| 5463 | return $this->addError(elFinder::ERROR_COPY, $errpath); |
| 5464 | } |
| 5465 | $volume->close($fp, $src); |
| 5466 | |
| 5467 | $this->added[] = $this->stat($path);; |
| 5468 | } |
| 5469 | |
| 5470 | return $path; |
| 5471 | } |
| 5472 | |
| 5473 | /** |
| 5474 | * Remove file/ recursive remove dir |
| 5475 | * |
| 5476 | * @param string $path file path |
| 5477 | * @param bool $force try to remove even if file locked |
| 5478 | * |
| 5479 | * @return bool |
| 5480 | * @throws elFinderAbortException |
| 5481 | * @author Dmitry (dio) Levashov |
| 5482 | */ |
| 5483 | protected function remove($path, $force = false) |
| 5484 | { |
| 5485 | $stat = $this->stat($path); |
| 5486 | |
| 5487 | if (empty($stat)) { |
| 5488 | return $this->setError(elFinder::ERROR_RM, $this->relpathCE($path), elFinder::ERROR_FILE_NOT_FOUND); |
| 5489 | } |
| 5490 | |
| 5491 | $stat['realpath'] = $path; |
| 5492 | $this->rmTmb($stat); |
| 5493 | $this->clearcache(); |
| 5494 | |
| 5495 | if (!$force && !empty($stat['locked'])) { |
| 5496 | return $this->setError(elFinder::ERROR_LOCKED, $this->path($stat['hash'])); |
| 5497 | } |
| 5498 | |
| 5499 | // Symlink to a directory is reported as mime "directory"; unlink the link only (Filester/elFinder fix). |
| 5500 | $localPath = $this->convEncIn($path); |
| 5501 | if (is_link($localPath)) { |
| 5502 | if ($this->convEncOut(!$this->_unlink($localPath))) { |
| 5503 | return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash'])); |
| 5504 | } |
| 5505 | $this->clearstatcache(); |
| 5506 | $this->removed[] = $stat; |
| 5507 | return true; |
| 5508 | } |
| 5509 | |
| 5510 | if ($stat['mime'] == 'directory' && empty($stat['thash'])) { |
| 5511 | $ret = $this->delTree($this->convEncIn($path)); |
| 5512 | $this->convEncOut(); |
| 5513 | if (!$ret) { |
| 5514 | return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash'])); |
| 5515 | } |
| 5516 | } else { |
| 5517 | if ($this->convEncOut(!$this->_unlink($this->convEncIn($path)))) { |
| 5518 | return $this->setError(elFinder::ERROR_RM, $this->path($stat['hash'])); |
| 5519 | } |
| 5520 | $this->clearstatcache(); |
| 5521 | } |
| 5522 | |
| 5523 | $this->removed[] = $stat; |
| 5524 | return true; |
| 5525 | } |
| 5526 | |
| 5527 | |
| 5528 | /************************* thumbnails **************************/ |
| 5529 | |
| 5530 | /** |
| 5531 | * Return thumbnail file name for required file |
| 5532 | * |
| 5533 | * @param array $stat file stat |
| 5534 | * |
| 5535 | * @return string |
| 5536 | * @author Dmitry (dio) Levashov |
| 5537 | **/ |
| 5538 | protected function tmbname($stat) |
| 5539 | { |
| 5540 | $name = $stat['hash'] . (isset($stat['ts']) ? $stat['ts'] : '') . '.png'; |
| 5541 | if (strlen($name) > 255) { |
| 5542 | $name = $this->id . md5($stat['hash']) . $stat['ts'] . '.png'; |
| 5543 | } |
| 5544 | return $name; |
| 5545 | } |
| 5546 | |
| 5547 | /** |
| 5548 | * Return thumnbnail name if exists |
| 5549 | * |
| 5550 | * @param string $path file path |
| 5551 | * @param array $stat file stat |
| 5552 | * |
| 5553 | * @return string|false |
| 5554 | * @author Dmitry (dio) Levashov |
| 5555 | **/ |
| 5556 | protected function gettmb($path, $stat) |
| 5557 | { |
| 5558 | if ($this->tmbURL && $this->tmbPath) { |
| 5559 | // file itself thumnbnail |
| 5560 | if (strpos($path, $this->tmbPath) === 0) { |
| 5561 | return basename($path); |
| 5562 | } |
| 5563 | |
| 5564 | $name = $this->tmbname($stat); |
| 5565 | $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name; |
| 5566 | if (file_exists($tmb)) { |
| 5567 | if ($this->options['tmbGcMaxlifeHour'] && $this->options['tmbGcPercentage'] > 0) { |
| 5568 | touch($tmb); |
| 5569 | } |
| 5570 | return $name; |
| 5571 | } |
| 5572 | } |
| 5573 | return false; |
| 5574 | } |
| 5575 | |
| 5576 | /** |
| 5577 | * Return true if thumnbnail for required file can be created |
| 5578 | * |
| 5579 | * @param string $path thumnbnail path |
| 5580 | * @param array $stat file stat |
| 5581 | * @param bool $checkTmbPath |
| 5582 | * |
| 5583 | * @return string|bool |
| 5584 | * @author Dmitry (dio) Levashov |
| 5585 | **/ |
| 5586 | protected function canCreateTmb($path, $stat, $checkTmbPath = true) |
| 5587 | { |
| 5588 | static $gdMimes = null; |
| 5589 | static $imgmgPS = null; |
| 5590 | if ($gdMimes === null) { |
| 5591 | $_mimes = array('image/jpeg', 'image/png', 'image/gif', 'image/x-ms-bmp'); |
| 5592 | if (function_exists('imagecreatefromwebp')) { |
| 5593 | $_mimes[] = 'image/webp'; |
| 5594 | } |
| 5595 | $gdMimes = array_flip($_mimes); |
| 5596 | $imgmgPS = array_flip(array('application/postscript', 'application/pdf')); |
| 5597 | } |
| 5598 | if ((!$checkTmbPath || $this->tmbPathWritable) |
| 5599 | && (!$this->tmbPath || strpos($path, $this->tmbPath) === false) // do not create thumnbnail for thumnbnail |
| 5600 | ) { |
| 5601 | $mime = strtolower($stat['mime']); |
| 5602 | list($type) = explode('/', $mime); |
| 5603 | if (!empty($this->imgConverter)) { |
| 5604 | if (isset($this->imgConverter[$mime])) { |
| 5605 | return true; |
| 5606 | } |
| 5607 | if (isset($this->imgConverter[$type])) { |
| 5608 | return true; |
| 5609 | } |
| 5610 | } |
| 5611 | return $this->imgLib |
| 5612 | && ( |
| 5613 | ($type === 'image' && ($this->imgLib === 'gd' ? isset($gdMimes[$stat['mime']]) : true)) |
| 5614 | || |
| 5615 | (ELFINDER_IMAGEMAGICK_PS && isset($imgmgPS[$stat['mime']]) && $this->imgLib !== 'gd') |
| 5616 | ); |
| 5617 | } |
| 5618 | return false; |
| 5619 | } |
| 5620 | |
| 5621 | /** |
| 5622 | * Return true if required file can be resized. |
| 5623 | * By default - the same as canCreateTmb |
| 5624 | * |
| 5625 | * @param string $path thumnbnail path |
| 5626 | * @param array $stat file stat |
| 5627 | * |
| 5628 | * @return string|bool |
| 5629 | * @author Dmitry (dio) Levashov |
| 5630 | **/ |
| 5631 | protected function canResize($path, $stat) |
| 5632 | { |
| 5633 | return $this->canCreateTmb($path, $stat, false); |
| 5634 | } |
| 5635 | |
| 5636 | /** |
| 5637 | * Create thumnbnail and return it's URL on success |
| 5638 | * |
| 5639 | * @param string $path file path |
| 5640 | * @param $stat |
| 5641 | * |
| 5642 | * @return false|string |
| 5643 | * @internal param string $mime file mime type |
| 5644 | * @throws elFinderAbortException |
| 5645 | * @throws ImagickException |
| 5646 | * @author Dmitry (dio) Levashov |
| 5647 | */ |
| 5648 | protected function createTmb($path, $stat) |
| 5649 | { |
| 5650 | if (!$stat || !$this->canCreateTmb($path, $stat)) { |
| 5651 | return false; |
| 5652 | } |
| 5653 | |
| 5654 | $name = $this->tmbname($stat); |
| 5655 | $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . $name; |
| 5656 | |
| 5657 | $maxlength = -1; |
| 5658 | $imgConverter = null; |
| 5659 | |
| 5660 | // check imgConverter |
| 5661 | $mime = strtolower($stat['mime']); |
| 5662 | list($type) = explode('/', $mime); |
| 5663 | if (isset($this->imgConverter[$mime])) { |
| 5664 | $imgConverter = $this->imgConverter[$mime]['func']; |
| 5665 | if (!empty($this->imgConverter[$mime]['maxlen'])) { |
| 5666 | $maxlength = intval($this->imgConverter[$mime]['maxlen']); |
| 5667 | } |
| 5668 | } else if (isset($this->imgConverter[$type])) { |
| 5669 | $imgConverter = $this->imgConverter[$type]['func']; |
| 5670 | if (!empty($this->imgConverter[$type]['maxlen'])) { |
| 5671 | $maxlength = intval($this->imgConverter[$type]['maxlen']); |
| 5672 | } |
| 5673 | } |
| 5674 | if ($imgConverter && !is_callable($imgConverter)) { |
| 5675 | return false; |
| 5676 | } |
| 5677 | |
| 5678 | // copy image into tmbPath so some drivers does not store files on local fs |
| 5679 | if (($src = $this->fopenCE($path, 'rb')) == false) { |
| 5680 | return false; |
| 5681 | } |
| 5682 | |
| 5683 | if (($trg = fopen($tmb, 'wb')) == false) { |
| 5684 | $this->fcloseCE($src, $path); |
| 5685 | return false; |
| 5686 | } |
| 5687 | |
| 5688 | stream_copy_to_stream($src, $trg, $maxlength); |
| 5689 | |
| 5690 | $this->fcloseCE($src, $path); |
| 5691 | fclose($trg); |
| 5692 | |
| 5693 | // call imgConverter |
| 5694 | if ($imgConverter) { |
| 5695 | if (!call_user_func_array($imgConverter, array($tmb, $stat, $this))) { |
| 5696 | file_exists($tmb) && unlink($tmb); |
| 5697 | return false; |
| 5698 | } |
| 5699 | } |
| 5700 | |
| 5701 | $result = false; |
| 5702 | |
| 5703 | $tmbSize = $this->tmbSize; |
| 5704 | |
| 5705 | if ($this->imgLib === 'imagick') { |
| 5706 | try { |
| 5707 | $imagickTest = new imagick($tmb . '[0]'); |
| 5708 | $imagickTest->clear(); |
| 5709 | $imagickTest = true; |
| 5710 | } catch (Exception $e) { |
| 5711 | $imagickTest = false; |
| 5712 | } |
| 5713 | } |
| 5714 | |
| 5715 | if (($this->imgLib === 'imagick' && !$imagickTest) || ($s = getimagesize($tmb)) === false) { |
| 5716 | if ($this->imgLib === 'imagick') { |
| 5717 | $bgcolor = $this->options['tmbBgColor']; |
| 5718 | if ($bgcolor === 'transparent') { |
| 5719 | $bgcolor = 'rgba(255, 255, 255, 0.0)'; |
| 5720 | } |
| 5721 | try { |
| 5722 | $imagick = new imagick(); |
| 5723 | $imagick->setBackgroundColor(new ImagickPixel($bgcolor)); |
| 5724 | $imagick->readImage($this->getExtentionByMime($stat['mime'], ':') . $tmb . '[0]'); |
| 5725 | try { |
| 5726 | $imagick->trimImage(0); |
| 5727 | } catch (Exception $e) { |
| 5728 | } |
| 5729 | $imagick->setImageFormat('png'); |
| 5730 | $imagick->writeImage($tmb); |
| 5731 | $imagick->clear(); |
| 5732 | if (($s = getimagesize($tmb)) !== false) { |
| 5733 | $result = true; |
| 5734 | } |
| 5735 | } catch (Exception $e) { |
| 5736 | } |
| 5737 | } else if ($this->imgLib === 'convert') { |
| 5738 | $convParams = $this->imageMagickConvertPrepare($tmb, 'png', 100, array(), $stat['mime']); |
| 5739 | $cmd = sprintf('%s -colorspace sRGB -trim -- %s %s', ELFINDER_CONVERT_PATH, $convParams['quotedPath'], $convParams['quotedDstPath']); |
| 5740 | $result = false; |
| 5741 | if ($this->procExec($cmd) === 0) { |
| 5742 | if (($s = getimagesize($tmb)) !== false) { |
| 5743 | $result = true; |
| 5744 | } |
| 5745 | } |
| 5746 | } |
| 5747 | if (!$result) { |
| 5748 | // fallback imgLib to GD |
| 5749 | if (function_exists('gd_info') && ($s = getimagesize($tmb))) { |
| 5750 | $this->imgLib = 'gd'; |
| 5751 | } else { |
| 5752 | file_exists($tmb) && unlink($tmb); |
| 5753 | return false; |
| 5754 | } |
| 5755 | } |
| 5756 | } |
| 5757 | |
| 5758 | /* If image smaller or equal thumbnail size - just fitting to thumbnail square */ |
| 5759 | if ($s[0] <= $tmbSize && $s[1] <= $tmbSize) { |
| 5760 | $result = $this->imgSquareFit($tmb, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png'); |
| 5761 | } else { |
| 5762 | |
| 5763 | if ($this->options['tmbCrop']) { |
| 5764 | |
| 5765 | $result = $tmb; |
| 5766 | /* Resize and crop if image bigger than thumbnail */ |
| 5767 | if (!(($s[0] > $tmbSize && $s[1] <= $tmbSize) || ($s[0] <= $tmbSize && $s[1] > $tmbSize)) || ($s[0] > $tmbSize && $s[1] > $tmbSize)) { |
| 5768 | $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, false, 'png'); |
| 5769 | } |
| 5770 | |
| 5771 | if ($result && ($s = getimagesize($tmb)) != false) { |
| 5772 | $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize) / 2) : 0; |
| 5773 | $y = $s[1] > $tmbSize ? intval(($s[1] - $tmbSize) / 2) : 0; |
| 5774 | $result = $this->imgCrop($result, $tmbSize, $tmbSize, $x, $y, 'png'); |
| 5775 | } else { |
| 5776 | $result = false; |
| 5777 | } |
| 5778 | |
| 5779 | } else { |
| 5780 | $result = $this->imgResize($tmb, $tmbSize, $tmbSize, true, true, 'png'); |
| 5781 | } |
| 5782 | |
| 5783 | if ($result) { |
| 5784 | if ($s = getimagesize($tmb)) { |
| 5785 | if ($s[0] !== $tmbSize || $s[1] !== $tmbSize) { |
| 5786 | $result = $this->imgSquareFit($result, $tmbSize, $tmbSize, 'center', 'middle', $this->options['tmbBgColor'], 'png'); |
| 5787 | } |
| 5788 | } |
| 5789 | } |
| 5790 | } |
| 5791 | |
| 5792 | if (!$result) { |
| 5793 | unlink($tmb); |
| 5794 | return false; |
| 5795 | } |
| 5796 | |
| 5797 | return $name; |
| 5798 | } |
| 5799 | |
| 5800 | /** |
| 5801 | * Resize image |
| 5802 | * |
| 5803 | * @param string $path image file |
| 5804 | * @param int $width new width |
| 5805 | * @param int $height new height |
| 5806 | * @param bool $keepProportions crop image |
| 5807 | * @param bool $resizeByBiggerSide resize image based on bigger side if true |
| 5808 | * @param string $destformat image destination format |
| 5809 | * @param int $jpgQuality JEPG quality (1-100) |
| 5810 | * @param array $options Other extra options |
| 5811 | * |
| 5812 | * @return string|false |
| 5813 | * @throws elFinderAbortException |
| 5814 | * @author Dmitry (dio) Levashov |
| 5815 | * @author Alexey Sukhotin |
| 5816 | */ |
| 5817 | protected function imgResize($path, $width, $height, $keepProportions = false, $resizeByBiggerSide = true, $destformat = null, $jpgQuality = null, $options = array()) |
| 5818 | { |
| 5819 | if (($s = getimagesize($path)) == false) { |
| 5820 | return false; |
| 5821 | } |
| 5822 | |
| 5823 | if (!$jpgQuality) { |
| 5824 | $jpgQuality = $this->options['jpgQuality']; |
| 5825 | } |
| 5826 | |
| 5827 | list($orig_w, $orig_h) = array($s[0], $s[1]); |
| 5828 | list($size_w, $size_h) = array($width, $height); |
| 5829 | |
| 5830 | if (empty($options['unenlarge']) || $orig_w > $size_w || $orig_h > $size_h) { |
| 5831 | if ($keepProportions == true) { |
| 5832 | /* Resizing by biggest side */ |
| 5833 | if ($resizeByBiggerSide) { |
| 5834 | if ($orig_w > $orig_h) { |
| 5835 | $size_h = round($orig_h * $width / $orig_w); |
| 5836 | $size_w = $width; |
| 5837 | } else { |
| 5838 | $size_w = round($orig_w * $height / $orig_h); |
| 5839 | $size_h = $height; |
| 5840 | } |
| 5841 | } else { |
| 5842 | if ($orig_w > $orig_h) { |
| 5843 | $size_w = round($orig_w * $height / $orig_h); |
| 5844 | $size_h = $height; |
| 5845 | } else { |
| 5846 | $size_h = round($orig_h * $width / $orig_w); |
| 5847 | $size_w = $width; |
| 5848 | } |
| 5849 | } |
| 5850 | } |
| 5851 | } else { |
| 5852 | $size_w = $orig_w; |
| 5853 | $size_h = $orig_h; |
| 5854 | } |
| 5855 | |
| 5856 | elFinder::extendTimeLimit(300); |
| 5857 | switch ($this->imgLib) { |
| 5858 | case 'imagick': |
| 5859 | |
| 5860 | try { |
| 5861 | $img = new imagick($path); |
| 5862 | } catch (Exception $e) { |
| 5863 | return false; |
| 5864 | } |
| 5865 | |
| 5866 | // Imagick::FILTER_BOX faster than FILTER_LANCZOS so use for createTmb |
| 5867 | // resize bench: http://app-mgng.rhcloud.com/9 |
| 5868 | // resize sample: http://www.dylanbeattie.net/magick/filters/result.html |
| 5869 | $filter = ($destformat === 'png' /* createTmb */) ? Imagick::FILTER_BOX : Imagick::FILTER_LANCZOS; |
| 5870 | |
| 5871 | $ani = ($img->getNumberImages() > 1); |
| 5872 | if ($ani && is_null($destformat)) { |
| 5873 | $img = $img->coalesceImages(); |
| 5874 | do { |
| 5875 | $img->resizeImage($size_w, $size_h, $filter, 1); |
| 5876 | } while ($img->nextImage()); |
| 5877 | $img->optimizeImageLayers(); |
| 5878 | $result = $img->writeImages($path, true); |
| 5879 | } else { |
| 5880 | if ($ani) { |
| 5881 | $img->setFirstIterator(); |
| 5882 | } |
| 5883 | if (strtoupper($img->getImageFormat()) === 'JPEG') { |
| 5884 | $img->setImageCompression(imagick::COMPRESSION_JPEG); |
| 5885 | $img->setImageCompressionQuality($jpgQuality); |
| 5886 | if (isset($options['preserveExif']) && !$options['preserveExif']) { |
| 5887 | try { |
| 5888 | $orientation = $img->getImageOrientation(); |
| 5889 | } catch (ImagickException $e) { |
| 5890 | $orientation = 0; |
| 5891 | } |
| 5892 | $img->stripImage(); |
| 5893 | if ($orientation) { |
| 5894 | $img->setImageOrientation($orientation); |
| 5895 | } |
| 5896 | } |
| 5897 | if ($this->options['jpgProgressive']) { |
| 5898 | $img->setInterlaceScheme(Imagick::INTERLACE_PLANE); |
| 5899 | } |
| 5900 | } |
| 5901 | $img->resizeImage($size_w, $size_h, $filter, true); |
| 5902 | if ($destformat) { |
| 5903 | $result = $this->imagickImage($img, $path, $destformat, $jpgQuality); |
| 5904 | } else { |
| 5905 | $result = $img->writeImage($path); |
| 5906 | } |
| 5907 | } |
| 5908 | |
| 5909 | $img->clear(); |
| 5910 | |
| 5911 | return $result ? $path : false; |
| 5912 | |
| 5913 | break; |
| 5914 | |
| 5915 | case 'convert': |
| 5916 | extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s)); |
| 5917 | /** |
| 5918 | * @var string $ani |
| 5919 | * @var string $index |
| 5920 | * @var string $coalesce |
| 5921 | * @var string $deconstruct |
| 5922 | * @var string $jpgQuality |
| 5923 | * @var string $quotedPath |
| 5924 | * @var string $quotedDstPath |
| 5925 | * @var string $interlace |
| 5926 | */ |
| 5927 | $filter = ($destformat === 'png' /* createTmb */) ? '-filter Box' : '-filter Lanczos'; |
| 5928 | $strip = (isset($options['preserveExif']) && !$options['preserveExif']) ? ' -strip' : ''; |
| 5929 | $cmd = sprintf('%s %s%s%s%s%s %s -geometry %dx%d! %s %s', ELFINDER_CONVERT_PATH, $quotedPath, $coalesce, $jpgQuality, $strip, $interlace, $filter, $size_w, $size_h, $deconstruct, $quotedDstPath); |
| 5930 | |
| 5931 | $result = false; |
| 5932 | if ($this->procExec($cmd) === 0) { |
| 5933 | $result = true; |
| 5934 | } |
| 5935 | return $result ? $path : false; |
| 5936 | |
| 5937 | break; |
| 5938 | |
| 5939 | case 'gd': |
| 5940 | elFinder::expandMemoryForGD(array($s, array($size_w, $size_h))); |
| 5941 | $img = $this->gdImageCreate($path, $s['mime']); |
| 5942 | |
| 5943 | if ($img && false != ($tmp = imagecreatetruecolor($size_w, $size_h))) { |
| 5944 | |
| 5945 | $bgNum = false; |
| 5946 | if ($s[2] === IMAGETYPE_GIF && (!$destformat || $destformat === 'gif')) { |
| 5947 | $bgIdx = imagecolortransparent($img); |
| 5948 | if ($bgIdx !== -1) { |
| 5949 | $c = imagecolorsforindex($img, $bgIdx); |
| 5950 | $bgNum = imagecolorallocate($tmp, $c['red'], $c['green'], $c['blue']); |
| 5951 | imagefill($tmp, 0, 0, $bgNum); |
| 5952 | imagecolortransparent($tmp, $bgNum); |
| 5953 | } |
| 5954 | } |
| 5955 | if ($bgNum === false) { |
| 5956 | $this->gdImageBackground($tmp, 'transparent'); |
| 5957 | } |
| 5958 | |
| 5959 | if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) { |
| 5960 | return false; |
| 5961 | } |
| 5962 | |
| 5963 | $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality); |
| 5964 | |
| 5965 | imagedestroy($img); |
| 5966 | imagedestroy($tmp); |
| 5967 | |
| 5968 | return $result ? $path : false; |
| 5969 | |
| 5970 | } |
| 5971 | break; |
| 5972 | } |
| 5973 | |
| 5974 | return false; |
| 5975 | } |
| 5976 | |
| 5977 | /** |
| 5978 | * Crop image |
| 5979 | * |
| 5980 | * @param string $path image file |
| 5981 | * @param int $width crop width |
| 5982 | * @param int $height crop height |
| 5983 | * @param bool $x crop left offset |
| 5984 | * @param bool $y crop top offset |
| 5985 | * @param string $destformat image destination format |
| 5986 | * @param int $jpgQuality JEPG quality (1-100) |
| 5987 | * |
| 5988 | * @return string|false |
| 5989 | * @throws elFinderAbortException |
| 5990 | * @author Dmitry (dio) Levashov |
| 5991 | * @author Alexey Sukhotin |
| 5992 | */ |
| 5993 | protected function imgCrop($path, $width, $height, $x, $y, $destformat = null, $jpgQuality = null) |
| 5994 | { |
| 5995 | if (($s = getimagesize($path)) == false) { |
| 5996 | return false; |
| 5997 | } |
| 5998 | |
| 5999 | if (!$jpgQuality) { |
| 6000 | $jpgQuality = $this->options['jpgQuality']; |
| 6001 | } |
| 6002 | |
| 6003 | elFinder::extendTimeLimit(300); |
| 6004 | switch ($this->imgLib) { |
| 6005 | case 'imagick': |
| 6006 | |
| 6007 | try { |
| 6008 | $img = new imagick($path); |
| 6009 | } catch (Exception $e) { |
| 6010 | return false; |
| 6011 | } |
| 6012 | |
| 6013 | $ani = ($img->getNumberImages() > 1); |
| 6014 | if ($ani && is_null($destformat)) { |
| 6015 | $img = $img->coalesceImages(); |
| 6016 | do { |
| 6017 | $img->setImagePage($s[0], $s[1], 0, 0); |
| 6018 | $img->cropImage($width, $height, $x, $y); |
| 6019 | $img->setImagePage($width, $height, 0, 0); |
| 6020 | } while ($img->nextImage()); |
| 6021 | $img->optimizeImageLayers(); |
| 6022 | $result = $img->writeImages($path, true); |
| 6023 | } else { |
| 6024 | if ($ani) { |
| 6025 | $img->setFirstIterator(); |
| 6026 | } |
| 6027 | $img->setImagePage($s[0], $s[1], 0, 0); |
| 6028 | $img->cropImage($width, $height, $x, $y); |
| 6029 | $img->setImagePage($width, $height, 0, 0); |
| 6030 | $result = $this->imagickImage($img, $path, $destformat, $jpgQuality); |
| 6031 | } |
| 6032 | |
| 6033 | $img->clear(); |
| 6034 | |
| 6035 | return $result ? $path : false; |
| 6036 | |
| 6037 | break; |
| 6038 | |
| 6039 | case 'convert': |
| 6040 | extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s)); |
| 6041 | /** |
| 6042 | * @var string $ani |
| 6043 | * @var string $index |
| 6044 | * @var string $coalesce |
| 6045 | * @var string $deconstruct |
| 6046 | * @var string $jpgQuality |
| 6047 | * @var string $quotedPath |
| 6048 | * @var string $quotedDstPath |
| 6049 | * @var string $interlace |
| 6050 | */ |
| 6051 | $cmd = sprintf('%s %s%s%s%s -crop %dx%d+%d+%d%s %s', ELFINDER_CONVERT_PATH, $quotedPath, $coalesce, $jpgQuality, $interlace, $width, $height, $x, $y, $deconstruct, $quotedDstPath); |
| 6052 | |
| 6053 | $result = false; |
| 6054 | if ($this->procExec($cmd) === 0) { |
| 6055 | $result = true; |
| 6056 | } |
| 6057 | return $result ? $path : false; |
| 6058 | |
| 6059 | break; |
| 6060 | |
| 6061 | case 'gd': |
| 6062 | elFinder::expandMemoryForGD(array($s, array($width, $height))); |
| 6063 | $img = $this->gdImageCreate($path, $s['mime']); |
| 6064 | |
| 6065 | if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) { |
| 6066 | |
| 6067 | $bgNum = false; |
| 6068 | if ($s[2] === IMAGETYPE_GIF && (!$destformat || $destformat === 'gif')) { |
| 6069 | $bgIdx = imagecolortransparent($img); |
| 6070 | if ($bgIdx !== -1) { |
| 6071 | $c = imagecolorsforindex($img, $bgIdx); |
| 6072 | $bgNum = imagecolorallocate($tmp, $c['red'], $c['green'], $c['blue']); |
| 6073 | imagefill($tmp, 0, 0, $bgNum); |
| 6074 | imagecolortransparent($tmp, $bgNum); |
| 6075 | } |
| 6076 | } |
| 6077 | if ($bgNum === false) { |
| 6078 | $this->gdImageBackground($tmp, 'transparent'); |
| 6079 | } |
| 6080 | |
| 6081 | $size_w = $width; |
| 6082 | $size_h = $height; |
| 6083 | |
| 6084 | if ($s[0] < $width || $s[1] < $height) { |
| 6085 | $size_w = $s[0]; |
| 6086 | $size_h = $s[1]; |
| 6087 | } |
| 6088 | |
| 6089 | if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) { |
| 6090 | return false; |
| 6091 | } |
| 6092 | |
| 6093 | $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality); |
| 6094 | |
| 6095 | imagedestroy($img); |
| 6096 | imagedestroy($tmp); |
| 6097 | |
| 6098 | return $result ? $path : false; |
| 6099 | |
| 6100 | } |
| 6101 | break; |
| 6102 | } |
| 6103 | |
| 6104 | return false; |
| 6105 | } |
| 6106 | |
| 6107 | /** |
| 6108 | * Put image to square |
| 6109 | * |
| 6110 | * @param string $path image file |
| 6111 | * @param int $width square width |
| 6112 | * @param int $height square height |
| 6113 | * @param int|string $align reserved |
| 6114 | * @param int|string $valign reserved |
| 6115 | * @param string $bgcolor square background color in #rrggbb format |
| 6116 | * @param string $destformat image destination format |
| 6117 | * @param int $jpgQuality JEPG quality (1-100) |
| 6118 | * |
| 6119 | * @return false|string |
| 6120 | * @throws ImagickException |
| 6121 | * @throws elFinderAbortException |
| 6122 | * @author Dmitry (dio) Levashov |
| 6123 | * @author Alexey Sukhotin |
| 6124 | */ |
| 6125 | protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null, $jpgQuality = null) |
| 6126 | { |
| 6127 | if (($s = getimagesize($path)) == false) { |
| 6128 | return false; |
| 6129 | } |
| 6130 | |
| 6131 | $result = false; |
| 6132 | |
| 6133 | /* Coordinates for image over square aligning */ |
| 6134 | $y = ceil(abs($height - $s[1]) / 2); |
| 6135 | $x = ceil(abs($width - $s[0]) / 2); |
| 6136 | |
| 6137 | if (!$jpgQuality) { |
| 6138 | $jpgQuality = $this->options['jpgQuality']; |
| 6139 | } |
| 6140 | |
| 6141 | elFinder::extendTimeLimit(300); |
| 6142 | switch ($this->imgLib) { |
| 6143 | case 'imagick': |
| 6144 | try { |
| 6145 | $img = new imagick($path); |
| 6146 | } catch (Exception $e) { |
| 6147 | return false; |
| 6148 | } |
| 6149 | |
| 6150 | if ($bgcolor === 'transparent') { |
| 6151 | $bgcolor = 'rgba(255, 255, 255, 0.0)'; |
| 6152 | } |
| 6153 | $ani = ($img->getNumberImages() > 1); |
| 6154 | if ($ani && is_null($destformat)) { |
| 6155 | $img1 = new Imagick(); |
| 6156 | $img1->setFormat('gif'); |
| 6157 | $img = $img->coalesceImages(); |
| 6158 | do { |
| 6159 | $gif = new Imagick(); |
| 6160 | $gif->newImage($width, $height, new ImagickPixel($bgcolor)); |
| 6161 | $gif->setImageColorspace($img->getImageColorspace()); |
| 6162 | $gif->setImageFormat('gif'); |
| 6163 | $gif->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y); |
| 6164 | $gif->setImageDelay($img->getImageDelay()); |
| 6165 | $gif->setImageIterations($img->getImageIterations()); |
| 6166 | $img1->addImage($gif); |
| 6167 | $gif->clear(); |
| 6168 | } while ($img->nextImage()); |
| 6169 | $img1->optimizeImageLayers(); |
| 6170 | $result = $img1->writeImages($path, true); |
| 6171 | } else { |
| 6172 | if ($ani) { |
| 6173 | $img->setFirstIterator(); |
| 6174 | } |
| 6175 | $img1 = new Imagick(); |
| 6176 | $img1->newImage($width, $height, new ImagickPixel($bgcolor)); |
| 6177 | $img1->setImageColorspace($img->getImageColorspace()); |
| 6178 | $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y); |
| 6179 | $result = $this->imagickImage($img1, $path, $destformat, $jpgQuality); |
| 6180 | } |
| 6181 | |
| 6182 | $img1->clear(); |
| 6183 | $img->clear(); |
| 6184 | return $result ? $path : false; |
| 6185 | |
| 6186 | break; |
| 6187 | |
| 6188 | case 'convert': |
| 6189 | extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s)); |
| 6190 | /** |
| 6191 | * @var string $ani |
| 6192 | * @var string $index |
| 6193 | * @var string $coalesce |
| 6194 | * @var string $deconstruct |
| 6195 | * @var string $jpgQuality |
| 6196 | * @var string $quotedPath |
| 6197 | * @var string $quotedDstPath |
| 6198 | * @var string $interlace |
| 6199 | */ |
| 6200 | if ($bgcolor === 'transparent') { |
| 6201 | $bgcolor = 'rgba(255, 255, 255, 0.0)'; |
| 6202 | } |
| 6203 | $bgArg = escapeshellarg('xc:' . $bgcolor); |
| 6204 | $cmd = sprintf( |
| 6205 | '%s -size %dx%d %s png:- | convert%s%s%s png:- %s -geometry +%d+%d -compose over -composite%s %s', |
| 6206 | ELFINDER_CONVERT_PATH, |
| 6207 | $width, |
| 6208 | $height, |
| 6209 | $bgArg, |
| 6210 | $coalesce, |
| 6211 | $jpgQuality, |
| 6212 | $interlace, |
| 6213 | $quotedPath, |
| 6214 | $x, |
| 6215 | $y, |
| 6216 | $deconstruct, |
| 6217 | $quotedDstPath |
| 6218 | ); |
| 6219 | |
| 6220 | $result = false; |
| 6221 | if ($this->procExec($cmd) === 0) { |
| 6222 | $result = true; |
| 6223 | } |
| 6224 | return $result ? $path : false; |
| 6225 | |
| 6226 | break; |
| 6227 | |
| 6228 | case 'gd': |
| 6229 | elFinder::expandMemoryForGD(array($s, array($width, $height))); |
| 6230 | $img = $this->gdImageCreate($path, $s['mime']); |
| 6231 | |
| 6232 | if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) { |
| 6233 | |
| 6234 | $this->gdImageBackground($tmp, $bgcolor); |
| 6235 | if ($bgcolor === 'transparent' && ($destformat === 'png' || $s[2] === IMAGETYPE_PNG)) { |
| 6236 | $bgNum = imagecolorallocatealpha($tmp, 255, 255, 255, 127); |
| 6237 | imagefill($tmp, 0, 0, $bgNum); |
| 6238 | } |
| 6239 | |
| 6240 | if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) { |
| 6241 | return false; |
| 6242 | } |
| 6243 | |
| 6244 | $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality); |
| 6245 | |
| 6246 | imagedestroy($img); |
| 6247 | imagedestroy($tmp); |
| 6248 | |
| 6249 | return $result ? $path : false; |
| 6250 | } |
| 6251 | break; |
| 6252 | } |
| 6253 | |
| 6254 | return false; |
| 6255 | } |
| 6256 | |
| 6257 | /** |
| 6258 | * Rotate image |
| 6259 | * |
| 6260 | * @param string $path image file |
| 6261 | * @param int $degree rotete degrees |
| 6262 | * @param string $bgcolor square background color in #rrggbb format |
| 6263 | * @param string $destformat image destination format |
| 6264 | * @param int $jpgQuality JEPG quality (1-100) |
| 6265 | * |
| 6266 | * @return string|false |
| 6267 | * @throws elFinderAbortException |
| 6268 | * @author nao-pon |
| 6269 | * @author Troex Nevelin |
| 6270 | */ |
| 6271 | protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat = null, $jpgQuality = null) |
| 6272 | { |
| 6273 | if (($s = getimagesize($path)) == false || $degree % 360 === 0) { |
| 6274 | return false; |
| 6275 | } |
| 6276 | |
| 6277 | $result = false; |
| 6278 | |
| 6279 | // try lossless rotate |
| 6280 | if ($degree % 90 === 0 && in_array($s[2], array(IMAGETYPE_JPEG, IMAGETYPE_JPEG2000))) { |
| 6281 | $count = ($degree / 90) % 4; |
| 6282 | $exiftran = array( |
| 6283 | 1 => '-9', |
| 6284 | 2 => '-1', |
| 6285 | 3 => '-2' |
| 6286 | ); |
| 6287 | $jpegtran = array( |
| 6288 | 1 => '90', |
| 6289 | 2 => '180', |
| 6290 | 3 => '270' |
| 6291 | ); |
| 6292 | $quotedPath = escapeshellarg($path); |
| 6293 | $cmds = array(); |
| 6294 | if ($this->procExec(ELFINDER_EXIFTRAN_PATH . ' -h') === 0) { |
| 6295 | $cmds[] = ELFINDER_EXIFTRAN_PATH . ' -i ' . $exiftran[$count] . ' -- ' . $quotedPath; |
| 6296 | } |
| 6297 | if ($this->procExec(ELFINDER_JPEGTRAN_PATH . ' -version') === 0) { |
| 6298 | $cmds[] = ELFINDER_JPEGTRAN_PATH . ' -rotate ' . $jpegtran[$count] . ' -copy all -outfile ' . $quotedPath . ' -- ' . $quotedPath; |
| 6299 | } |
| 6300 | foreach ($cmds as $cmd) { |
| 6301 | if ($this->procExec($cmd) === 0) { |
| 6302 | $result = true; |
| 6303 | break; |
| 6304 | } |
| 6305 | } |
| 6306 | if ($result) { |
| 6307 | return $path; |
| 6308 | } |
| 6309 | } |
| 6310 | |
| 6311 | if (!$jpgQuality) { |
| 6312 | $jpgQuality = $this->options['jpgQuality']; |
| 6313 | } |
| 6314 | |
| 6315 | elFinder::extendTimeLimit(300); |
| 6316 | switch ($this->imgLib) { |
| 6317 | case 'imagick': |
| 6318 | try { |
| 6319 | $img = new imagick($path); |
| 6320 | } catch (Exception $e) { |
| 6321 | return false; |
| 6322 | } |
| 6323 | |
| 6324 | if ($s[2] === IMAGETYPE_GIF || $s[2] === IMAGETYPE_PNG) { |
| 6325 | $bgcolor = 'rgba(255, 255, 255, 0.0)'; |
| 6326 | } |
| 6327 | if ($img->getNumberImages() > 1) { |
| 6328 | $img = $img->coalesceImages(); |
| 6329 | do { |
| 6330 | $img->rotateImage(new ImagickPixel($bgcolor), $degree); |
| 6331 | } while ($img->nextImage()); |
| 6332 | $img->optimizeImageLayers(); |
| 6333 | $result = $img->writeImages($path, true); |
| 6334 | } else { |
| 6335 | $img->rotateImage(new ImagickPixel($bgcolor), $degree); |
| 6336 | $result = $this->imagickImage($img, $path, $destformat, $jpgQuality); |
| 6337 | } |
| 6338 | $img->clear(); |
| 6339 | return $result ? $path : false; |
| 6340 | |
| 6341 | break; |
| 6342 | |
| 6343 | case 'convert': |
| 6344 | extract($this->imageMagickConvertPrepare($path, $destformat, $jpgQuality, $s)); |
| 6345 | /** |
| 6346 | * @var string $ani |
| 6347 | * @var string $index |
| 6348 | * @var string $coalesce |
| 6349 | * @var string $deconstruct |
| 6350 | * @var string $jpgQuality |
| 6351 | * @var string $quotedPath |
| 6352 | * @var string $quotedDstPath |
| 6353 | * @var string $interlace |
| 6354 | */ |
| 6355 | if ($s[2] === IMAGETYPE_GIF || $s[2] === IMAGETYPE_PNG) { |
| 6356 | $bgcolor = 'rgba(255, 255, 255, 0.0)'; |
| 6357 | } |
| 6358 | $bgArg = escapeshellarg($bgcolor); |
| 6359 | $cmd = sprintf( |
| 6360 | '%s%s%s%s -background %s -rotate %d%s -- %s %s', |
| 6361 | ELFINDER_CONVERT_PATH, |
| 6362 | $coalesce, |
| 6363 | $jpgQuality, |
| 6364 | $interlace, |
| 6365 | $bgArg, |
| 6366 | $degree, |
| 6367 | $deconstruct, |
| 6368 | $quotedPath, |
| 6369 | $quotedDstPath |
| 6370 | ); |
| 6371 | |
| 6372 | $result = false; |
| 6373 | if ($this->procExec($cmd) === 0) { |
| 6374 | $result = true; |
| 6375 | } |
| 6376 | return $result ? $path : false; |
| 6377 | |
| 6378 | break; |
| 6379 | |
| 6380 | case 'gd': |
| 6381 | elFinder::expandMemoryForGD(array($s, $s)); |
| 6382 | $img = $this->gdImageCreate($path, $s['mime']); |
| 6383 | |
| 6384 | $degree = 360 - $degree; |
| 6385 | |
| 6386 | $bgNum = -1; |
| 6387 | $bgIdx = false; |
| 6388 | if ($s[2] === IMAGETYPE_GIF) { |
| 6389 | $bgIdx = imagecolortransparent($img); |
| 6390 | if ($bgIdx !== -1) { |
| 6391 | $c = imagecolorsforindex($img, $bgIdx); |
| 6392 | $w = imagesx($img); |
| 6393 | $h = imagesy($img); |
| 6394 | $newImg = imagecreatetruecolor($w, $h); |
| 6395 | imagepalettecopy($newImg, $img); |
| 6396 | $bgNum = imagecolorallocate($newImg, $c['red'], $c['green'], $c['blue']); |
| 6397 | imagefill($newImg, 0, 0, $bgNum); |
| 6398 | imagecolortransparent($newImg, $bgNum); |
| 6399 | imagecopy($newImg, $img, 0, 0, 0, 0, $w, $h); |
| 6400 | imagedestroy($img); |
| 6401 | $img = $newImg; |
| 6402 | $newImg = null; |
| 6403 | } |
| 6404 | } else if ($s[2] === IMAGETYPE_PNG) { |
| 6405 | $bgNum = imagecolorallocatealpha($img, 255, 255, 255, 127); |
| 6406 | } |
| 6407 | if ($bgNum === -1) { |
| 6408 | list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x"); |
| 6409 | $bgNum = imagecolorallocate($img, $r, $g, $b); |
| 6410 | } |
| 6411 | |
| 6412 | $tmp = imageRotate($img, $degree, $bgNum); |
| 6413 | if ($bgIdx !== -1) { |
| 6414 | imagecolortransparent($tmp, $bgNum); |
| 6415 | } |
| 6416 | |
| 6417 | $result = $this->gdImage($tmp, $path, $destformat, $s['mime'], $jpgQuality); |
| 6418 | |
| 6419 | imageDestroy($img); |
| 6420 | imageDestroy($tmp); |
| 6421 | |
| 6422 | return $result ? $path : false; |
| 6423 | |
| 6424 | break; |
| 6425 | } |
| 6426 | |
| 6427 | return false; |
| 6428 | } |
| 6429 | |
| 6430 | /** |
| 6431 | * Execute shell command |
| 6432 | * |
| 6433 | * @param string $command command line |
| 6434 | * @param string $output stdout strings |
| 6435 | * @param int $return_var process exit code |
| 6436 | * @param string $error_output stderr strings |
| 6437 | * |
| 6438 | * @return int exit code |
| 6439 | * @throws elFinderAbortException |
| 6440 | * @author Alexey Sukhotin |
| 6441 | */ |
| 6442 | protected function procExec($command, &$output = '', &$return_var = -1, &$error_output = '', $cwd = null) |
| 6443 | { |
| 6444 | return elFinder::procExec($command, $output, $return_var, $error_output); |
| 6445 | } |
| 6446 | |
| 6447 | /** |
| 6448 | * Remove thumbnail, also remove recursively if stat is directory |
| 6449 | * |
| 6450 | * @param array $stat file stat |
| 6451 | * |
| 6452 | * @return void |
| 6453 | * @throws elFinderAbortException |
| 6454 | * @author Dmitry (dio) Levashov |
| 6455 | * @author Naoki Sawada |
| 6456 | * @author Troex Nevelin |
| 6457 | */ |
| 6458 | protected function rmTmb($stat) |
| 6459 | { |
| 6460 | if ($this->tmbPathWritable) { |
| 6461 | if ($stat['mime'] === 'directory') { |
| 6462 | foreach ($this->scandirCE($this->decode($stat['hash'])) as $p) { |
| 6463 | elFinder::extendTimeLimit(30); |
| 6464 | $name = $this->basenameCE($p); |
| 6465 | $name != '.' && $name != '..' && $this->rmTmb($this->stat($p)); |
| 6466 | } |
| 6467 | } else if (!empty($stat['tmb']) && $stat['tmb'] != "1") { |
| 6468 | $tmb = $this->tmbPath . DIRECTORY_SEPARATOR . rawurldecode($stat['tmb']); |
| 6469 | file_exists($tmb) && unlink($tmb); |
| 6470 | clearstatcache(); |
| 6471 | } |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | /** |
| 6476 | * Create an gd image according to the specified mime type |
| 6477 | * |
| 6478 | * @param string $path image file |
| 6479 | * @param string $mime |
| 6480 | * |
| 6481 | * @return resource|false GD image resource identifier |
| 6482 | */ |
| 6483 | protected function gdImageCreate($path, $mime) |
| 6484 | { |
| 6485 | switch ($mime) { |
| 6486 | case 'image/jpeg': |
| 6487 | return imagecreatefromjpeg($path); |
| 6488 | |
| 6489 | case 'image/png': |
| 6490 | return imagecreatefrompng($path); |
| 6491 | |
| 6492 | case 'image/gif': |
| 6493 | return imagecreatefromgif($path); |
| 6494 | |
| 6495 | case 'image/x-ms-bmp': |
| 6496 | if (!function_exists('imagecreatefrombmp')) { |
| 6497 | include_once dirname(__FILE__) . '/libs/GdBmp.php'; |
| 6498 | } |
| 6499 | return imagecreatefrombmp($path); |
| 6500 | |
| 6501 | case 'image/xbm': |
| 6502 | return imagecreatefromxbm($path); |
| 6503 | |
| 6504 | case 'image/xpm': |
| 6505 | return imagecreatefromxpm($path); |
| 6506 | |
| 6507 | case 'image/webp': |
| 6508 | return imagecreatefromwebp($path); |
| 6509 | } |
| 6510 | return false; |
| 6511 | } |
| 6512 | |
| 6513 | /** |
| 6514 | * Output gd image to file |
| 6515 | * |
| 6516 | * @param resource $image gd image resource |
| 6517 | * @param string $filename The path to save the file to. |
| 6518 | * @param string $destformat The Image type to use for $filename |
| 6519 | * @param string $mime The original image mime type |
| 6520 | * @param int $jpgQuality JEPG quality (1-100) |
| 6521 | * |
| 6522 | * @return bool |
| 6523 | */ |
| 6524 | protected function gdImage($image, $filename, $destformat, $mime, $jpgQuality = null) |
| 6525 | { |
| 6526 | |
| 6527 | if (!$jpgQuality) { |
| 6528 | $jpgQuality = $this->options['jpgQuality']; |
| 6529 | } |
| 6530 | if ($destformat) { |
| 6531 | switch ($destformat) { |
| 6532 | case 'jpg': |
| 6533 | $mime = 'image/jpeg'; |
| 6534 | break; |
| 6535 | case 'gif': |
| 6536 | $mime = 'image/gif'; |
| 6537 | break; |
| 6538 | case 'png': |
| 6539 | default: |
| 6540 | $mime = 'image/png'; |
| 6541 | break; |
| 6542 | } |
| 6543 | } |
| 6544 | switch ($mime) { |
| 6545 | case 'image/gif': |
| 6546 | return imagegif($image, $filename); |
| 6547 | case 'image/jpeg': |
| 6548 | if ($this->options['jpgProgressive']) { |
| 6549 | imageinterlace($image, true); |
| 6550 | } |
| 6551 | return imagejpeg($image, $filename, $jpgQuality); |
| 6552 | case 'image/wbmp': |
| 6553 | return imagewbmp($image, $filename); |
| 6554 | case 'image/png': |
| 6555 | default: |
| 6556 | return imagepng($image, $filename); |
| 6557 | } |
| 6558 | } |
| 6559 | |
| 6560 | /** |
| 6561 | * Output imagick image to file |
| 6562 | * |
| 6563 | * @param imagick $img imagick image resource |
| 6564 | * @param string $filename The path to save the file to. |
| 6565 | * @param string $destformat The Image type to use for $filename |
| 6566 | * @param int $jpgQuality JEPG quality (1-100) |
| 6567 | * |
| 6568 | * @return bool |
| 6569 | */ |
| 6570 | protected function imagickImage($img, $filename, $destformat, $jpgQuality = null) |
| 6571 | { |
| 6572 | |
| 6573 | if (!$jpgQuality) { |
| 6574 | $jpgQuality = $this->options['jpgQuality']; |
| 6575 | } |
| 6576 | |
| 6577 | try { |
| 6578 | if ($destformat) { |
| 6579 | if ($destformat === 'gif') { |
| 6580 | $img->setImageFormat('gif'); |
| 6581 | } else if ($destformat === 'png') { |
| 6582 | $img->setImageFormat('png'); |
| 6583 | } else if ($destformat === 'jpg') { |
| 6584 | $img->setImageFormat('jpeg'); |
| 6585 | } |
| 6586 | } |
| 6587 | if (strtoupper($img->getImageFormat()) === 'JPEG') { |
| 6588 | $img->setImageCompression(imagick::COMPRESSION_JPEG); |
| 6589 | $img->setImageCompressionQuality($jpgQuality); |
| 6590 | if ($this->options['jpgProgressive']) { |
| 6591 | $img->setInterlaceScheme(Imagick::INTERLACE_PLANE); |
| 6592 | } |
| 6593 | try { |
| 6594 | $orientation = $img->getImageOrientation(); |
| 6595 | } catch (ImagickException $e) { |
| 6596 | $orientation = 0; |
| 6597 | } |
| 6598 | $img->stripImage(); |
| 6599 | if ($orientation) { |
| 6600 | $img->setImageOrientation($orientation); |
| 6601 | } |
| 6602 | } |
| 6603 | $result = $img->writeImage($filename); |
| 6604 | } catch (Exception $e) { |
| 6605 | $result = false; |
| 6606 | } |
| 6607 | |
| 6608 | return $result; |
| 6609 | } |
| 6610 | |
| 6611 | /** |
| 6612 | * Assign the proper background to a gd image |
| 6613 | * |
| 6614 | * @param resource $image gd image resource |
| 6615 | * @param string $bgcolor background color in #rrggbb format |
| 6616 | */ |
| 6617 | protected function gdImageBackground($image, $bgcolor) |
| 6618 | { |
| 6619 | |
| 6620 | if ($bgcolor === 'transparent') { |
| 6621 | imagealphablending($image, false); |
| 6622 | imagesavealpha($image, true); |
| 6623 | } else { |
| 6624 | list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x"); |
| 6625 | $bgcolor1 = imagecolorallocate($image, $r, $g, $b); |
| 6626 | imagefill($image, 0, 0, $bgcolor1); |
| 6627 | } |
| 6628 | } |
| 6629 | |
| 6630 | /** |
| 6631 | * Prepare variables for exec convert of ImageMagick |
| 6632 | * |
| 6633 | * @param string $path |
| 6634 | * @param string $destformat |
| 6635 | * @param int $jpgQuality |
| 6636 | * @param array $imageSize |
| 6637 | * @param null $mime |
| 6638 | * |
| 6639 | * @return array |
| 6640 | * @throws elFinderAbortException |
| 6641 | */ |
| 6642 | protected function imageMagickConvertPrepare($path, $destformat, $jpgQuality, $imageSize = null, $mime = null) |
| 6643 | { |
| 6644 | if (is_null($imageSize)) { |
| 6645 | $imageSize = getimagesize($path); |
| 6646 | } |
| 6647 | if (is_null($mime)) { |
| 6648 | $mime = $this->mimetype($path); |
| 6649 | } |
| 6650 | $srcType = $this->getExtentionByMime($mime, ':'); |
| 6651 | $ani = false; |
| 6652 | if (preg_match('/^(?:gif|png|ico)/', $srcType)) { |
| 6653 | $cmd = ELFINDER_IDENTIFY_PATH . ' -- ' . escapeshellarg($srcType . $path); |
| 6654 | if ($this->procExec($cmd, $o) === 0) { |
| 6655 | $ani = preg_split('/(?:\r\n|\n|\r)/', trim($o)); |
| 6656 | if (count($ani) < 2) { |
| 6657 | $ani = false; |
| 6658 | } |
| 6659 | } |
| 6660 | } |
| 6661 | $coalesce = $index = $interlace = ''; |
| 6662 | $deconstruct = ' +repage'; |
| 6663 | if ($ani && $destformat !== 'png'/* not createTmb */) { |
| 6664 | if (is_null($destformat)) { |
| 6665 | $coalesce = ' -coalesce -repage 0x0'; |
| 6666 | $deconstruct = ' +repage -deconstruct -layers optimize'; |
| 6667 | } else if ($imageSize) { |
| 6668 | if ($srcType === 'ico:') { |
| 6669 | $index = '[0]'; |
| 6670 | foreach ($ani as $_i => $_info) { |
| 6671 | if (preg_match('/ (\d+)x(\d+) /', $_info, $m)) { |
| 6672 | if ($m[1] == $imageSize[0] && $m[2] == $imageSize[1]) { |
| 6673 | $index = '[' . $_i . ']'; |
| 6674 | break; |
| 6675 | } |
| 6676 | } |
| 6677 | } |
| 6678 | } |
| 6679 | } |
| 6680 | } else { |
| 6681 | $index = '[0]'; |
| 6682 | } |
| 6683 | if ($imageSize && ($imageSize[2] === IMAGETYPE_JPEG || $imageSize[2] === IMAGETYPE_JPEG2000)) { |
| 6684 | $jpgQuality = ' -quality ' . $jpgQuality; |
| 6685 | if ($this->options['jpgProgressive']) { |
| 6686 | $interlace = ' -interlace Plane'; |
| 6687 | } |
| 6688 | } else { |
| 6689 | $jpgQuality = ''; |
| 6690 | } |
| 6691 | $quotedPath = escapeshellarg($srcType . $path . $index); |
| 6692 | $quotedDstPath = escapeshellarg(($destformat ? ($destformat . ':') : $srcType) . $path); |
| 6693 | return compact('ani', 'index', 'coalesce', 'deconstruct', 'jpgQuality', 'quotedPath', 'quotedDstPath', 'interlace'); |
| 6694 | } |
| 6695 | |
| 6696 | /*********************** misc *************************/ |
| 6697 | |
| 6698 | /** |
| 6699 | * Find position of first occurrence of string in a string with multibyte support |
| 6700 | * |
| 6701 | * @param string $haystack The string being checked. |
| 6702 | * @param string $needle The string to find in haystack. |
| 6703 | * @param int $offset The search offset. If it is not specified, 0 is used. |
| 6704 | * |
| 6705 | * @return int|bool |
| 6706 | * @author Alexey Sukhotin |
| 6707 | **/ |
| 6708 | protected function stripos($haystack, $needle, $offset = 0) |
| 6709 | { |
| 6710 | if (function_exists('mb_stripos')) { |
| 6711 | return mb_stripos($haystack, $needle, $offset, 'UTF-8'); |
| 6712 | } else if (function_exists('mb_strtolower') && function_exists('mb_strpos')) { |
| 6713 | return mb_strpos(mb_strtolower($haystack, 'UTF-8'), mb_strtolower($needle, 'UTF-8'), $offset); |
| 6714 | } |
| 6715 | return stripos($haystack, $needle, $offset); |
| 6716 | } |
| 6717 | |
| 6718 | /** |
| 6719 | * Default serach match method (name match) |
| 6720 | * |
| 6721 | * @param String $name Item name |
| 6722 | * @param String $query Query word |
| 6723 | * @param String $path Item path |
| 6724 | * |
| 6725 | * @return bool @return bool |
| 6726 | */ |
| 6727 | protected function searchMatchName($name, $query, $path) |
| 6728 | { |
| 6729 | return $this->stripos($name, $query) !== false; |
| 6730 | } |
| 6731 | |
| 6732 | /** |
| 6733 | * Get server side available archivers |
| 6734 | * |
| 6735 | * @param bool $use_cache |
| 6736 | * |
| 6737 | * @return array |
| 6738 | * @throws elFinderAbortException |
| 6739 | */ |
| 6740 | protected function getArchivers($use_cache = true) |
| 6741 | { |
| 6742 | $sessionKey = 'archivers'; |
| 6743 | if ($use_cache) { |
| 6744 | if (isset($this->options['archivers']) && is_array($this->options['archivers']) && $this->options['archivers']) { |
| 6745 | $cache = $this->options['archivers']; |
| 6746 | } else { |
| 6747 | $cache = elFinder::$archivers; |
| 6748 | } |
| 6749 | if ($cache) { |
| 6750 | return $cache; |
| 6751 | } else { |
| 6752 | if ($cache = $this->session->get($sessionKey, array())) { |
| 6753 | return elFinder::$archivers = $cache; |
| 6754 | } |
| 6755 | } |
| 6756 | } |
| 6757 | |
| 6758 | $arcs = array( |
| 6759 | 'create' => array(), |
| 6760 | 'extract' => array() |
| 6761 | ); |
| 6762 | |
| 6763 | if ($this->procExec('') === 0) { |
| 6764 | |
| 6765 | $this->procExec(ELFINDER_TAR_PATH . ' --version', $o, $ctar); |
| 6766 | |
| 6767 | if ($ctar == 0) { |
| 6768 | $arcs['create']['application/x-tar'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-chf', 'ext' => 'tar'); |
| 6769 | $arcs['extract']['application/x-tar'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-xf', 'ext' => 'tar', 'toSpec' => '-C ', 'getsize' => array('argc' => '-xvf', 'toSpec' => '--to-stdout|wc -c', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]*([0-9]+)[^\r\n]*$/s', 'replace' => '$1')); |
| 6770 | unset($o); |
| 6771 | $this->procExec(ELFINDER_GZIP_PATH . ' --version', $o, $c); |
| 6772 | if ($c == 0) { |
| 6773 | $arcs['create']['application/x-gzip'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-czhf', 'ext' => 'tgz'); |
| 6774 | $arcs['extract']['application/x-gzip'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-xzf', 'ext' => 'tgz', 'toSpec' => '-C ', 'getsize' => array('argc' => '-xvf', 'toSpec' => '--to-stdout|wc -c', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]*([0-9]+)[^\r\n]*$/s', 'replace' => '$1')); |
| 6775 | } |
| 6776 | unset($o); |
| 6777 | $this->procExec(ELFINDER_BZIP2_PATH . ' --version', $o, $c); |
| 6778 | if ($c == 0) { |
| 6779 | $arcs['create']['application/x-bzip2'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-cjhf', 'ext' => 'tbz'); |
| 6780 | $arcs['extract']['application/x-bzip2'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-xjf', 'ext' => 'tbz', 'toSpec' => '-C ', 'getsize' => array('argc' => '-xvf', 'toSpec' => '--to-stdout|wc -c', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]*([0-9]+)[^\r\n]*$/s', 'replace' => '$1')); |
| 6781 | } |
| 6782 | unset($o); |
| 6783 | $this->procExec(ELFINDER_XZ_PATH . ' --version', $o, $c); |
| 6784 | if ($c == 0) { |
| 6785 | $arcs['create']['application/x-xz'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-cJhf', 'ext' => 'xz'); |
| 6786 | $arcs['extract']['application/x-xz'] = array('cmd' => ELFINDER_TAR_PATH, 'argc' => '-xJf', 'ext' => 'xz', 'toSpec' => '-C ', 'getsize' => array('argc' => '-xvf', 'toSpec' => '--to-stdout|wc -c', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]*([0-9]+)[^\r\n]*$/s', 'replace' => '$1')); |
| 6787 | } |
| 6788 | } |
| 6789 | unset($o); |
| 6790 | $this->procExec(ELFINDER_ZIP_PATH . ' -h', $o, $c); |
| 6791 | if ($c == 0) { |
| 6792 | $arcs['create']['application/zip'] = array('cmd' => ELFINDER_ZIP_PATH, 'argc' => '-r9 -q', 'ext' => 'zip'); |
| 6793 | } |
| 6794 | unset($o); |
| 6795 | $this->procExec(ELFINDER_UNZIP_PATH . ' --help', $o, $c); |
| 6796 | if ($c == 0) { |
| 6797 | $arcs['extract']['application/zip'] = array('cmd' => ELFINDER_UNZIP_PATH, 'argc' => '-q', 'ext' => 'zip', 'toSpec' => '-d ', 'getsize' => array('argc' => '-Z -t', 'regex' => '/^.+?,\s?([0-9]+).+$/', 'replace' => '$1')); |
| 6798 | } |
| 6799 | unset($o); |
| 6800 | $this->procExec(ELFINDER_RAR_PATH, $o, $c); |
| 6801 | if ($c == 0 || $c == 7) { |
| 6802 | $arcs['create']['application/x-rar'] = array('cmd' => ELFINDER_RAR_PATH, 'argc' => 'a -inul' . (defined('ELFINDER_RAR_MA4') && ELFINDER_RAR_MA4? ' -ma4' : '') . ' --', 'ext' => 'rar'); |
| 6803 | } |
| 6804 | unset($o); |
| 6805 | $this->procExec(ELFINDER_UNRAR_PATH, $o, $c); |
| 6806 | if ($c == 0 || $c == 7) { |
| 6807 | $arcs['extract']['application/x-rar'] = array('cmd' => ELFINDER_UNRAR_PATH, 'argc' => 'x -y', 'ext' => 'rar', 'toSpec' => '', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)(?:(?:[^\r\n0-9]+[0-9]+[^\r\n0-9]+([0-9]+)[^\r\n]+)|(?:[^\r\n0-9]+([0-9]+)[^\r\n0-9]+[0-9]+[^\r\n]*))$/s', 'replace' => '$1')); |
| 6808 | } |
| 6809 | unset($o); |
| 6810 | $this->procExec(ELFINDER_7Z_PATH, $o, $c); |
| 6811 | if ($c == 0) { |
| 6812 | $arcs['create']['application/x-7z-compressed'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'a --', 'ext' => '7z'); |
| 6813 | $arcs['extract']['application/x-7z-compressed'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'x -y', 'ext' => '7z', 'toSpec' => '-o', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1')); |
| 6814 | |
| 6815 | if (empty($arcs['create']['application/zip'])) { |
| 6816 | $arcs['create']['application/zip'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'a -tzip --', 'ext' => 'zip'); |
| 6817 | } |
| 6818 | if (empty($arcs['extract']['application/zip'])) { |
| 6819 | $arcs['extract']['application/zip'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'x -tzip -y', 'ext' => 'zip', 'toSpec' => '-o', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1')); |
| 6820 | } |
| 6821 | if (empty($arcs['create']['application/x-tar'])) { |
| 6822 | $arcs['create']['application/x-tar'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'a -ttar --', 'ext' => 'tar'); |
| 6823 | } |
| 6824 | if (empty($arcs['extract']['application/x-tar'])) { |
| 6825 | $arcs['extract']['application/x-tar'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'x -ttar -y', 'ext' => 'tar', 'toSpec' => '-o', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1')); |
| 6826 | } |
| 6827 | if (substr(PHP_OS, 0, 3) === 'WIN' && empty($arcs['extract']['application/x-rar'])) { |
| 6828 | $arcs['extract']['application/x-rar'] = array('cmd' => ELFINDER_7Z_PATH, 'argc' => 'x -trar -y', 'ext' => 'rar', 'toSpec' => '-o', 'getsize' => array('argc' => 'l', 'regex' => '/^.+(?:\r\n|\n|\r)[^\r\n0-9]+([0-9]+)[^\r\n]+$/s', 'replace' => '$1')); |
| 6829 | } |
| 6830 | } |
| 6831 | |
| 6832 | } |
| 6833 | |
| 6834 | // Use PHP ZipArchive Class |
| 6835 | if (class_exists('ZipArchive', false)) { |
| 6836 | if (empty($arcs['create']['application/zip'])) { |
| 6837 | $arcs['create']['application/zip'] = array('cmd' => 'phpfunction', 'argc' => array('self', 'zipArchiveZip'), 'ext' => 'zip'); |
| 6838 | } |
| 6839 | if (empty($arcs['extract']['application/zip'])) { |
| 6840 | $arcs['extract']['application/zip'] = array('cmd' => 'phpfunction', 'argc' => array('self', 'zipArchiveUnzip'), 'ext' => 'zip'); |
| 6841 | } |
| 6842 | } |
| 6843 | |
| 6844 | $this->session->set($sessionKey, $arcs); |
| 6845 | return elFinder::$archivers = $arcs; |
| 6846 | } |
| 6847 | |
| 6848 | /** |
| 6849 | * Resolve relative / (Unix-like)absolute path |
| 6850 | * |
| 6851 | * @param string $path target path |
| 6852 | * @param string $base base path |
| 6853 | * |
| 6854 | * @return string |
| 6855 | */ |
| 6856 | protected function getFullPath($path, $base) |
| 6857 | { |
| 6858 | $separator = $this->separator; |
| 6859 | $systemroot = $this->systemRoot; |
| 6860 | $base = (string)$base; |
| 6861 | |
| 6862 | if ($base[0] === $separator && substr($base, 0, strlen($systemroot)) !== $systemroot) { |
| 6863 | $base = $systemroot . substr($base, 1); |
| 6864 | } |
| 6865 | if ($base !== $systemroot) { |
| 6866 | $base = rtrim($base, $separator); |
| 6867 | } |
| 6868 | |
| 6869 | $sepquoted = preg_quote($separator, '#'); |
| 6870 | |
| 6871 | // normalize `//` to `/` |
| 6872 | $path = preg_replace('#' . $sepquoted . '+#', $separator, $path); // '#/+#' |
| 6873 | |
| 6874 | // remove `./` |
| 6875 | $path = preg_replace('#(?<=^|' . $sepquoted . ')\.' . $sepquoted . '#', '', $path); // '#(?<=^|/)\./#' |
| 6876 | |
| 6877 | // 'Here' |
| 6878 | if ($path === '') return $base; |
| 6879 | |
| 6880 | // join $base to $path if $path start `../` |
| 6881 | if (substr($path, 0, 3) === '..' . $separator) { |
| 6882 | $path = $base . $separator . $path; |
| 6883 | } |
| 6884 | |
| 6885 | // normalize `/../` |
| 6886 | $normreg = '#(' . $sepquoted . ')[^' . $sepquoted . ']+' . $sepquoted . '\.\.' . $sepquoted . '#'; // '#(/)[^\/]+/\.\./#' |
| 6887 | while (preg_match($normreg, $path)) { |
| 6888 | $path = preg_replace($normreg, '$1', $path, 1); |
| 6889 | } |
| 6890 | if ($path !== $systemroot) { |
| 6891 | $path = rtrim($path, $separator); |
| 6892 | } |
| 6893 | |
| 6894 | // discard the surplus `../` |
| 6895 | $path = str_replace('..' . $separator, '', $path); |
| 6896 | |
| 6897 | // Absolute path |
| 6898 | if ($path[0] === $separator || strpos($path, $systemroot) === 0) { |
| 6899 | return $path; |
| 6900 | } |
| 6901 | |
| 6902 | $preg_separator = '#' . $sepquoted . '#'; |
| 6903 | |
| 6904 | // Relative path from 'Here' |
| 6905 | if (substr($path, 0, 2) === '.' . $separator || $path[0] !== '.') { |
| 6906 | $arrn = preg_split($preg_separator, $path, -1, PREG_SPLIT_NO_EMPTY); |
| 6907 | if ($arrn[0] !== '.') { |
| 6908 | array_unshift($arrn, '.'); |
| 6909 | } |
| 6910 | $arrn[0] = rtrim($base, $separator); |
| 6911 | return join($separator, $arrn); |
| 6912 | } |
| 6913 | |
| 6914 | return $path; |
| 6915 | } |
| 6916 | |
| 6917 | /** |
| 6918 | * Remove directory recursive on local file system |
| 6919 | * |
| 6920 | * @param string $dir Target dirctory path |
| 6921 | * |
| 6922 | * @return boolean |
| 6923 | * @throws elFinderAbortException |
| 6924 | * @author Naoki Sawada |
| 6925 | */ |
| 6926 | public function rmdirRecursive($dir) |
| 6927 | { |
| 6928 | return self::localRmdirRecursive($dir); |
| 6929 | } |
| 6930 | |
| 6931 | /** |
| 6932 | * Create archive and return its path |
| 6933 | * |
| 6934 | * @param string $dir target dir |
| 6935 | * @param array $files files names list |
| 6936 | * @param string $name archive name |
| 6937 | * @param array $arc archiver options |
| 6938 | * |
| 6939 | * @return string|bool |
| 6940 | * @throws elFinderAbortException |
| 6941 | * @author Dmitry (dio) Levashov, |
| 6942 | * @author Alexey Sukhotin |
| 6943 | * @author Naoki Sawada |
| 6944 | */ |
| 6945 | protected function makeArchive($dir, $files, $name, $arc) |
| 6946 | { |
| 6947 | if ($arc['cmd'] === 'phpfunction') { |
| 6948 | if (is_callable($arc['argc'])) { |
| 6949 | call_user_func_array($arc['argc'], array($dir, $files, $name)); |
| 6950 | } |
| 6951 | } else { |
| 6952 | $cwd = getcwd(); |
| 6953 | if (chdir($dir)) { |
| 6954 | foreach ($files as $i => $file) { |
| 6955 | $files[$i] = '.' . DIRECTORY_SEPARATOR . basename($file); |
| 6956 | } |
| 6957 | $files = array_map('escapeshellarg', $files); |
| 6958 | $prefix = $switch = ''; |
| 6959 | // The zip command accepts the "-" at the beginning of the file name as a command switch, |
| 6960 | // and can't use '--' before archive name, so add "./" to name for security reasons. |
| 6961 | if ($arc['ext'] === 'zip' && strpos($arc['argc'], '-tzip') === false) { |
| 6962 | $prefix = './'; |
| 6963 | $switch = '-- '; |
| 6964 | } |
| 6965 | $cmd = $arc['cmd'] . ' ' . $arc['argc'] . ' ' . $prefix . escapeshellarg($name) . ' ' . $switch . implode(' ', $files); |
| 6966 | $err_out = ''; |
| 6967 | $this->procExec($cmd, $o, $c, $err_out, $dir); |
| 6968 | chdir($cwd); |
| 6969 | } else { |
| 6970 | return false; |
| 6971 | } |
| 6972 | } |
| 6973 | $path = $dir . DIRECTORY_SEPARATOR . $name; |
| 6974 | return file_exists($path) ? $path : false; |
| 6975 | } |
| 6976 | |
| 6977 | /** |
| 6978 | * Unpack archive |
| 6979 | * |
| 6980 | * @param string $path archive path |
| 6981 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
| 6982 | * @param bool|string $mode bool: remove archive ( unlink($path) ) | string: extract to directory |
| 6983 | * |
| 6984 | * @return void |
| 6985 | * @throws elFinderAbortException |
| 6986 | * @author Dmitry (dio) Levashov |
| 6987 | * @author Alexey Sukhotin |
| 6988 | * @author Naoki Sawada |
| 6989 | */ |
| 6990 | protected function unpackArchive($path, $arc, $mode = true) |
| 6991 | { |
| 6992 | if (is_string($mode)) { |
| 6993 | $dir = $mode; |
| 6994 | $chdir = null; |
| 6995 | $remove = false; |
| 6996 | } else { |
| 6997 | $dir = dirname($path); |
| 6998 | $chdir = $dir; |
| 6999 | $remove = $mode; |
| 7000 | } |
| 7001 | $dir = realpath($dir); |
| 7002 | $path = realpath($path); |
| 7003 | if ($arc['cmd'] === 'phpfunction') { |
| 7004 | if (is_callable($arc['argc'])) { |
| 7005 | call_user_func_array($arc['argc'], array($path, $dir)); |
| 7006 | } |
| 7007 | } else { |
| 7008 | $cwd = getcwd(); |
| 7009 | if (!$chdir || chdir($dir)) { |
| 7010 | if (!empty($arc['getsize'])) { |
| 7011 | // Check total file size after extraction |
| 7012 | $getsize = $arc['getsize']; |
| 7013 | if (is_array($getsize) && !empty($getsize['regex']) && !empty($getsize['replace'])) { |
| 7014 | $cmd = $arc['cmd'] . ' ' . $getsize['argc'] . ' ' . escapeshellarg($path) . (!empty($getsize['toSpec'])? (' ' . $getsize['toSpec']): ''); |
| 7015 | $this->procExec($cmd, $o, $c); |
| 7016 | if ($o) { |
| 7017 | $size = preg_replace($getsize['regex'], $getsize['replace'], trim($o)); |
| 7018 | $comp = function_exists('bccomp')? 'bccomp' : 'strnatcmp'; |
| 7019 | if (!empty($this->options['maxArcFilesSize'])) { |
| 7020 | if ($comp($size, (string)$this->options['maxArcFilesSize']) > 0) { |
| 7021 | throw new Exception(elFinder::ERROR_ARC_MAXSIZE); |
| 7022 | } |
| 7023 | } |
| 7024 | } |
| 7025 | unset($o, $c); |
| 7026 | } |
| 7027 | } |
| 7028 | if ($chdir) { |
| 7029 | $cmd = $arc['cmd'] . ' ' . $arc['argc'] . ' ' . escapeshellarg(basename($path)); |
| 7030 | } else { |
| 7031 | $cmd = $arc['cmd'] . ' ' . $arc['argc'] . ' ' . escapeshellarg($path) . ' ' . $arc['toSpec'] . escapeshellarg($dir); |
| 7032 | } |
| 7033 | $this->procExec($cmd, $o, $c); |
| 7034 | $chdir && chdir($cwd); |
| 7035 | } |
| 7036 | } |
| 7037 | $remove && unlink($path); |
| 7038 | } |
| 7039 | |
| 7040 | /** |
| 7041 | * Check and filter the extracted items |
| 7042 | * |
| 7043 | * @param string $path target local path |
| 7044 | * @param array $checks types to check default: ['symlink', 'name', 'writable', 'mime'] |
| 7045 | * |
| 7046 | * @return array ['symlinks' => [], 'names' => [], 'writables' => [], 'mimes' => [], 'rmNames' => [], 'totalSize' => 0] |
| 7047 | * @throws elFinderAbortException |
| 7048 | * @throws Exception |
| 7049 | * @author Naoki Sawada |
| 7050 | */ |
| 7051 | protected function checkExtractItems($path, $checks = null) |
| 7052 | { |
| 7053 | if (is_null($checks) || !is_array($checks)) { |
| 7054 | $checks = array('symlink', 'name', 'writable', 'mime'); |
| 7055 | } |
| 7056 | $chkSymlink = in_array('symlink', $checks); |
| 7057 | $chkName = in_array('name', $checks); |
| 7058 | $chkWritable = in_array('writable', $checks); |
| 7059 | $chkMime = in_array('mime', $checks); |
| 7060 | |
| 7061 | $res = array( |
| 7062 | 'symlinks' => array(), |
| 7063 | 'names' => array(), |
| 7064 | 'writables' => array(), |
| 7065 | 'mimes' => array(), |
| 7066 | 'rmNames' => array(), |
| 7067 | 'totalSize' => 0 |
| 7068 | ); |
| 7069 | |
| 7070 | if (is_dir($path)) { |
| 7071 | $files = self::localScandir($path); |
| 7072 | } else { |
| 7073 | $files = array(basename($path)); |
| 7074 | $path = dirname($path); |
| 7075 | } |
| 7076 | |
| 7077 | foreach ($files as $name) { |
| 7078 | $p = $path . DIRECTORY_SEPARATOR . $name; |
| 7079 | $utf8Name = elFinder::$instance->utf8Encode($name); |
| 7080 | if ($name !== $utf8Name) { |
| 7081 | $fsSame = false; |
| 7082 | if ($this->encoding) { |
| 7083 | // test as fs encoding |
| 7084 | $_utf8 = @iconv($this->encoding, 'utf-8//IGNORE', $name); |
| 7085 | if (@iconv('utf-8', $this->encoding.'//IGNORE', $_utf8) === $name) { |
| 7086 | $fsSame = true; |
| 7087 | $utf8Name = $_utf8; |
| 7088 | } else { |
| 7089 | $_name = $this->convEncIn($utf8Name, true); |
| 7090 | } |
| 7091 | } else { |
| 7092 | $_name = $utf8Name; |
| 7093 | } |
| 7094 | if (!$fsSame && rename($p, $path . DIRECTORY_SEPARATOR . $_name)) { |
| 7095 | $name = $_name; |
| 7096 | $p = $path . DIRECTORY_SEPARATOR . $name; |
| 7097 | } |
| 7098 | } |
| 7099 | if (!is_readable($p)) { |
| 7100 | // Perhaps a symbolic link to open_basedir restricted location |
| 7101 | self::localRmdirRecursive($p); |
| 7102 | $res['symlinks'][] = $p; |
| 7103 | $res['rmNames'][] = $utf8Name; |
| 7104 | continue; |
| 7105 | } |
| 7106 | if ($chkSymlink && is_link($p)) { |
| 7107 | self::localRmdirRecursive($p); |
| 7108 | $res['symlinks'][] = $p; |
| 7109 | $res['rmNames'][] = $utf8Name; |
| 7110 | continue; |
| 7111 | } |
| 7112 | $isDir = is_dir($p); |
| 7113 | if ($chkName && !$this->nameAccepted($name, $isDir)) { |
| 7114 | self::localRmdirRecursive($p); |
| 7115 | $res['names'][] = $p; |
| 7116 | $res['rmNames'][] = $utf8Name; |
| 7117 | continue; |
| 7118 | } |
| 7119 | if ($chkWritable && !$this->attr($p, 'write', null, $isDir)) { |
| 7120 | self::localRmdirRecursive($p); |
| 7121 | $res['writables'][] = $p; |
| 7122 | $res['rmNames'][] = $utf8Name; |
| 7123 | continue; |
| 7124 | } |
| 7125 | if ($isDir) { |
| 7126 | $cRes = $this->checkExtractItems($p, $checks); |
| 7127 | foreach ($cRes as $k => $v) { |
| 7128 | if (is_array($v)) { |
| 7129 | $res[$k] = array_merge($res[$k], $cRes[$k]); |
| 7130 | } else { |
| 7131 | $res[$k] += $cRes[$k]; |
| 7132 | } |
| 7133 | } |
| 7134 | } else { |
| 7135 | if ($chkMime) { |
| 7136 | $mimeByName = $this->mimeTypeNormalize( |
| 7137 | elFinderVolumeDriver::mimetypeInternalDetect($name), |
| 7138 | $name, |
| 7139 | pathinfo($name, PATHINFO_EXTENSION) |
| 7140 | ); |
| 7141 | |
| 7142 | if ($mimeByName && !$this->allowPutMime($mimeByName)) { |
| 7143 | self::localRmdirRecursive($p); |
| 7144 | $res['mimes'][] = $p; |
| 7145 | $res['rmNames'][] = $utf8Name; |
| 7146 | continue; |
| 7147 | } |
| 7148 | } |
| 7149 | $res['totalSize'] += (int)sprintf('%u', filesize($p)); |
| 7150 | } |
| 7151 | } |
| 7152 | $res['rmNames'] = array_unique($res['rmNames']); |
| 7153 | |
| 7154 | return $res; |
| 7155 | } |
| 7156 | |
| 7157 | /** |
| 7158 | * Return files of target directory that is dotfiles excludes. |
| 7159 | * |
| 7160 | * @param string $dir target directory path |
| 7161 | * |
| 7162 | * @return array |
| 7163 | * @throws Exception |
| 7164 | * @author Naoki Sawada |
| 7165 | */ |
| 7166 | protected static function localScandir($dir) |
| 7167 | { |
| 7168 | // PHP function scandir() is not work well in specific environment. I dont know why. |
| 7169 | // ref. https://github.com/Studio-42/elFinder/issues/1248 |
| 7170 | $files = array(); |
| 7171 | if ($dh = opendir($dir)) { |
| 7172 | while (false !== ($file = readdir($dh))) { |
| 7173 | if ($file !== '.' && $file !== '..') { |
| 7174 | $files[] = $file; |
| 7175 | } |
| 7176 | } |
| 7177 | closedir($dh); |
| 7178 | } else { |
| 7179 | throw new Exception('Can not open local directory.'); |
| 7180 | } |
| 7181 | return $files; |
| 7182 | } |
| 7183 | |
| 7184 | /** |
| 7185 | * Remove directory recursive on local file system |
| 7186 | * |
| 7187 | * @param string $dir Target dirctory path |
| 7188 | * |
| 7189 | * @return boolean |
| 7190 | * @throws elFinderAbortException |
| 7191 | * @author Naoki Sawada |
| 7192 | */ |
| 7193 | protected static function localRmdirRecursive($dir) |
| 7194 | { |
| 7195 | // Unlink symlink only; BSD/macOS `rm -rf` on a symlink can delete the target tree (Filester fix). |
| 7196 | if (is_link($dir)) { |
| 7197 | return unlink($dir); |
| 7198 | } |
| 7199 | |
| 7200 | // try system command |
| 7201 | if (is_callable('exec')) { |
| 7202 | $o = ''; |
| 7203 | $r = 1; |
| 7204 | if (substr(PHP_OS, 0, 3) === 'WIN') { |
| 7205 | if (!is_link($dir) && is_dir($dir)) { |
| 7206 | exec('rd /S /Q ' . escapeshellarg($dir), $o, $r); |
| 7207 | } else { |
| 7208 | exec('del /F /Q ' . escapeshellarg($dir), $o, $r); |
| 7209 | } |
| 7210 | } else { |
| 7211 | exec('rm -rf ' . escapeshellarg($dir), $o, $r); |
| 7212 | } |
| 7213 | if ($r === 0) { |
| 7214 | return true; |
| 7215 | } |
| 7216 | } |
| 7217 | if (!is_link($dir) && is_dir($dir)) { |
| 7218 | chmod($dir, 0777); |
| 7219 | if ($handle = opendir($dir)) { |
| 7220 | while (false !== ($file = readdir($handle))) { |
| 7221 | if ($file === '.' || $file === '..') { |
| 7222 | continue; |
| 7223 | } |
| 7224 | elFinder::extendTimeLimit(30); |
| 7225 | $path = $dir . DIRECTORY_SEPARATOR . $file; |
| 7226 | if (!is_link($dir) && is_dir($path)) { |
| 7227 | self::localRmdirRecursive($path); |
| 7228 | } else { |
| 7229 | chmod($path, 0666); |
| 7230 | unlink($path); |
| 7231 | } |
| 7232 | } |
| 7233 | closedir($handle); |
| 7234 | } |
| 7235 | return rmdir($dir); |
| 7236 | } else { |
| 7237 | chmod($dir, 0666); |
| 7238 | return unlink($dir); |
| 7239 | } |
| 7240 | } |
| 7241 | |
| 7242 | /** |
| 7243 | * Move item recursive on local file system |
| 7244 | * |
| 7245 | * @param string $src |
| 7246 | * @param string $target |
| 7247 | * @param bool $overWrite |
| 7248 | * @param bool $copyJoin |
| 7249 | * |
| 7250 | * @return boolean |
| 7251 | * @throws elFinderAbortException |
| 7252 | * @throws Exception |
| 7253 | * @author Naoki Sawada |
| 7254 | */ |
| 7255 | protected static function localMoveRecursive($src, $target, $overWrite = true, $copyJoin = true) |
| 7256 | { |
| 7257 | $res = false; |
| 7258 | if (!file_exists($target)) { |
| 7259 | return rename($src, $target); |
| 7260 | } |
| 7261 | if (!$copyJoin || !is_dir($target)) { |
| 7262 | if ($overWrite) { |
| 7263 | if (is_dir($target)) { |
| 7264 | $del = self::localRmdirRecursive($target); |
| 7265 | } else { |
| 7266 | $del = unlink($target); |
| 7267 | } |
| 7268 | if ($del) { |
| 7269 | return rename($src, $target); |
| 7270 | } |
| 7271 | } |
| 7272 | } else { |
| 7273 | foreach (self::localScandir($src) as $item) { |
| 7274 | $res |= self::localMoveRecursive($src . DIRECTORY_SEPARATOR . $item, $target . DIRECTORY_SEPARATOR . $item, $overWrite, $copyJoin); |
| 7275 | } |
| 7276 | } |
| 7277 | return (bool)$res; |
| 7278 | } |
| 7279 | |
| 7280 | /** |
| 7281 | * Create Zip archive using PHP class ZipArchive |
| 7282 | * |
| 7283 | * @param string $dir target dir |
| 7284 | * @param array $files files names list |
| 7285 | * @param string|object $zipPath Zip archive name |
| 7286 | * |
| 7287 | * @return bool |
| 7288 | * @author Naoki Sawada |
| 7289 | */ |
| 7290 | protected static function zipArchiveZip($dir, $files, $zipPath) |
| 7291 | { |
| 7292 | try { |
| 7293 | if ($start = is_string($zipPath)) { |
| 7294 | $zip = new ZipArchive(); |
| 7295 | if ($zip->open($dir . DIRECTORY_SEPARATOR . $zipPath, ZipArchive::CREATE) !== true) { |
| 7296 | $zip = false; |
| 7297 | } |
| 7298 | } else { |
| 7299 | $zip = $zipPath; |
| 7300 | } |
| 7301 | if ($zip) { |
| 7302 | foreach ($files as $file) { |
| 7303 | $path = $dir . DIRECTORY_SEPARATOR . $file; |
| 7304 | if (is_dir($path)) { |
| 7305 | $zip->addEmptyDir($file); |
| 7306 | $_files = array(); |
| 7307 | if ($handle = opendir($path)) { |
| 7308 | while (false !== ($entry = readdir($handle))) { |
| 7309 | if ($entry !== "." && $entry !== "..") { |
| 7310 | $_files[] = $file . DIRECTORY_SEPARATOR . $entry; |
| 7311 | } |
| 7312 | } |
| 7313 | closedir($handle); |
| 7314 | } |
| 7315 | if ($_files) { |
| 7316 | self::zipArchiveZip($dir, $_files, $zip); |
| 7317 | } |
| 7318 | } else { |
| 7319 | $zip->addFile($path, $file); |
| 7320 | } |
| 7321 | } |
| 7322 | $start && $zip->close(); |
| 7323 | } |
| 7324 | } catch (Exception $e) { |
| 7325 | return false; |
| 7326 | } |
| 7327 | return true; |
| 7328 | } |
| 7329 | |
| 7330 | /** |
| 7331 | * Unpack Zip archive using PHP class ZipArchive |
| 7332 | * |
| 7333 | * @param string $zipPath Zip archive name |
| 7334 | * @param string $toDir Extract to path |
| 7335 | * |
| 7336 | * @return bool |
| 7337 | * @author Naoki Sawada |
| 7338 | */ |
| 7339 | protected static function zipArchiveUnzip($zipPath, $toDir) |
| 7340 | { |
| 7341 | try { |
| 7342 | $zip = new ZipArchive(); |
| 7343 | if ($zip->open($zipPath) === true) { |
| 7344 | // Check total file size after extraction |
| 7345 | $num = $zip->numFiles; |
| 7346 | $size = 0; |
| 7347 | $maxSize = empty(self::$maxArcFilesSize)? '' : (string)self::$maxArcFilesSize; |
| 7348 | $comp = function_exists('bccomp')? 'bccomp' : 'strnatcmp'; |
| 7349 | for ($i = 0; $i < $num; $i++) { |
| 7350 | $stat = $zip->statIndex($i); |
| 7351 | $size += $stat['size']; |
| 7352 | if (strpos((string)$size, 'E') !== false) { |
| 7353 | // Cannot handle values exceeding PHP_INT_MAX |
| 7354 | throw new Exception(elFinder::ERROR_ARC_MAXSIZE); |
| 7355 | } |
| 7356 | if (!$maxSize) { |
| 7357 | if ($comp($size, $maxSize) > 0) { |
| 7358 | throw new Exception(elFinder::ERROR_ARC_MAXSIZE); |
| 7359 | } |
| 7360 | } |
| 7361 | } |
| 7362 | // do extract |
| 7363 | $zip->extractTo($toDir); |
| 7364 | $zip->close(); |
| 7365 | } |
| 7366 | } catch (Exception $e) { |
| 7367 | throw $e; |
| 7368 | } |
| 7369 | return true; |
| 7370 | } |
| 7371 | |
| 7372 | /** |
| 7373 | * Recursive symlinks search |
| 7374 | * |
| 7375 | * @param string $path file/dir path |
| 7376 | * |
| 7377 | * @return bool |
| 7378 | * @throws Exception |
| 7379 | * @author Dmitry (dio) Levashov |
| 7380 | */ |
| 7381 | protected static function localFindSymlinks($path) |
| 7382 | { |
| 7383 | if (is_link($path)) { |
| 7384 | return true; |
| 7385 | } |
| 7386 | |
| 7387 | if (is_dir($path)) { |
| 7388 | foreach (self::localScandir($path) as $name) { |
| 7389 | $p = $path . DIRECTORY_SEPARATOR . $name; |
| 7390 | if (is_link($p)) { |
| 7391 | return true; |
| 7392 | } |
| 7393 | if (is_dir($p) && self::localFindSymlinks($p)) { |
| 7394 | return true; |
| 7395 | } |
| 7396 | } |
| 7397 | } |
| 7398 | |
| 7399 | return false; |
| 7400 | } |
| 7401 | |
| 7402 | /**==================================* abstract methods *====================================**/ |
| 7403 | |
| 7404 | /*********************** paths/urls *************************/ |
| 7405 | |
| 7406 | /** |
| 7407 | * Return parent directory path |
| 7408 | * |
| 7409 | * @param string $path file path |
| 7410 | * |
| 7411 | * @return string |
| 7412 | * @author Dmitry (dio) Levashov |
| 7413 | **/ |
| 7414 | abstract protected function _dirname($path); |
| 7415 | |
| 7416 | /** |
| 7417 | * Return file name |
| 7418 | * |
| 7419 | * @param string $path file path |
| 7420 | * |
| 7421 | * @return string |
| 7422 | * @author Dmitry (dio) Levashov |
| 7423 | **/ |
| 7424 | abstract protected function _basename($path); |
| 7425 | |
| 7426 | /** |
| 7427 | * Join dir name and file name and return full path. |
| 7428 | * Some drivers (db) use int as path - so we give to concat path to driver itself |
| 7429 | * |
| 7430 | * @param string $dir dir path |
| 7431 | * @param string $name file name |
| 7432 | * |
| 7433 | * @return string |
| 7434 | * @author Dmitry (dio) Levashov |
| 7435 | **/ |
| 7436 | abstract protected function _joinPath($dir, $name); |
| 7437 | |
| 7438 | /** |
| 7439 | * Return normalized path |
| 7440 | * |
| 7441 | * @param string $path file path |
| 7442 | * |
| 7443 | * @return string |
| 7444 | * @author Dmitry (dio) Levashov |
| 7445 | **/ |
| 7446 | abstract protected function _normpath($path); |
| 7447 | |
| 7448 | /** |
| 7449 | * Return file path related to root dir |
| 7450 | * |
| 7451 | * @param string $path file path |
| 7452 | * |
| 7453 | * @return string |
| 7454 | * @author Dmitry (dio) Levashov |
| 7455 | **/ |
| 7456 | abstract protected function _relpath($path); |
| 7457 | |
| 7458 | /** |
| 7459 | * Convert path related to root dir into real path |
| 7460 | * |
| 7461 | * @param string $path rel file path |
| 7462 | * |
| 7463 | * @return string |
| 7464 | * @author Dmitry (dio) Levashov |
| 7465 | **/ |
| 7466 | abstract protected function _abspath($path); |
| 7467 | |
| 7468 | /** |
| 7469 | * Return fake path started from root dir. |
| 7470 | * Required to show path on client side. |
| 7471 | * |
| 7472 | * @param string $path file path |
| 7473 | * |
| 7474 | * @return string |
| 7475 | * @author Dmitry (dio) Levashov |
| 7476 | **/ |
| 7477 | abstract protected function _path($path); |
| 7478 | |
| 7479 | /** |
| 7480 | * Return true if $path is children of $parent |
| 7481 | * |
| 7482 | * @param string $path path to check |
| 7483 | * @param string $parent parent path |
| 7484 | * |
| 7485 | * @return bool |
| 7486 | * @author Dmitry (dio) Levashov |
| 7487 | **/ |
| 7488 | abstract protected function _inpath($path, $parent); |
| 7489 | |
| 7490 | /** |
| 7491 | * Return stat for given path. |
| 7492 | * Stat contains following fields: |
| 7493 | * - (int) size file size in b. required |
| 7494 | * - (int) ts file modification time in unix time. required |
| 7495 | * - (string) mime mimetype. required for folders, others - optionally |
| 7496 | * - (bool) read read permissions. required |
| 7497 | * - (bool) write write permissions. required |
| 7498 | * - (bool) locked is object locked. optionally |
| 7499 | * - (bool) hidden is object hidden. optionally |
| 7500 | * - (string) alias for symlinks - link target path relative to root path. optionally |
| 7501 | * - (string) target for symlinks - link target path. optionally |
| 7502 | * If file does not exists - returns empty array or false. |
| 7503 | * |
| 7504 | * @param string $path file path |
| 7505 | * |
| 7506 | * @return array|false |
| 7507 | * @author Dmitry (dio) Levashov |
| 7508 | **/ |
| 7509 | abstract protected function _stat($path); |
| 7510 | |
| 7511 | |
| 7512 | /***************** file stat ********************/ |
| 7513 | |
| 7514 | |
| 7515 | /** |
| 7516 | * Return true if path is dir and has at least one childs directory |
| 7517 | * |
| 7518 | * @param string $path dir path |
| 7519 | * |
| 7520 | * @return bool |
| 7521 | * @author Dmitry (dio) Levashov |
| 7522 | **/ |
| 7523 | abstract protected function _subdirs($path); |
| 7524 | |
| 7525 | /** |
| 7526 | * Return object width and height |
| 7527 | * Ususaly used for images, but can be realize for video etc... |
| 7528 | * |
| 7529 | * @param string $path file path |
| 7530 | * @param string $mime file mime type |
| 7531 | * |
| 7532 | * @return string |
| 7533 | * @author Dmitry (dio) Levashov |
| 7534 | **/ |
| 7535 | abstract protected function _dimensions($path, $mime); |
| 7536 | |
| 7537 | /******************** file/dir content *********************/ |
| 7538 | |
| 7539 | /** |
| 7540 | * Return files list in directory |
| 7541 | * |
| 7542 | * @param string $path dir path |
| 7543 | * |
| 7544 | * @return array |
| 7545 | * @author Dmitry (dio) Levashov |
| 7546 | **/ |
| 7547 | abstract protected function _scandir($path); |
| 7548 | |
| 7549 | /** |
| 7550 | * Open file and return file pointer |
| 7551 | * |
| 7552 | * @param string $path file path |
| 7553 | * @param string $mode open mode |
| 7554 | * |
| 7555 | * @return resource|false |
| 7556 | * @author Dmitry (dio) Levashov |
| 7557 | **/ |
| 7558 | abstract protected function _fopen($path, $mode = "rb"); |
| 7559 | |
| 7560 | /** |
| 7561 | * Close opened file |
| 7562 | * |
| 7563 | * @param resource $fp file pointer |
| 7564 | * @param string $path file path |
| 7565 | * |
| 7566 | * @return bool |
| 7567 | * @author Dmitry (dio) Levashov |
| 7568 | **/ |
| 7569 | abstract protected function _fclose($fp, $path = ''); |
| 7570 | |
| 7571 | /******************** file/dir manipulations *************************/ |
| 7572 | |
| 7573 | /** |
| 7574 | * Create dir and return created dir path or false on failed |
| 7575 | * |
| 7576 | * @param string $path parent dir path |
| 7577 | * @param string $name new directory name |
| 7578 | * |
| 7579 | * @return string|bool |
| 7580 | * @author Dmitry (dio) Levashov |
| 7581 | **/ |
| 7582 | abstract protected function _mkdir($path, $name); |
| 7583 | |
| 7584 | /** |
| 7585 | * Create file and return it's path or false on failed |
| 7586 | * |
| 7587 | * @param string $path parent dir path |
| 7588 | * @param string $name new file name |
| 7589 | * |
| 7590 | * @return string|bool |
| 7591 | * @author Dmitry (dio) Levashov |
| 7592 | **/ |
| 7593 | abstract protected function _mkfile($path, $name); |
| 7594 | |
| 7595 | /** |
| 7596 | * Create symlink |
| 7597 | * |
| 7598 | * @param string $source file to link to |
| 7599 | * @param string $targetDir folder to create link in |
| 7600 | * @param string $name symlink name |
| 7601 | * |
| 7602 | * @return bool |
| 7603 | * @author Dmitry (dio) Levashov |
| 7604 | **/ |
| 7605 | abstract protected function _symlink($source, $targetDir, $name); |
| 7606 | |
| 7607 | /** |
| 7608 | * Copy file into another file (only inside one volume) |
| 7609 | * |
| 7610 | * @param string $source source file path |
| 7611 | * @param $targetDir |
| 7612 | * @param string $name file name |
| 7613 | * |
| 7614 | * @return bool|string |
| 7615 | * @internal param string $target target dir path |
| 7616 | * @author Dmitry (dio) Levashov |
| 7617 | */ |
| 7618 | abstract protected function _copy($source, $targetDir, $name); |
| 7619 | |
| 7620 | /** |
| 7621 | * Move file into another parent dir. |
| 7622 | * Return new file path or false. |
| 7623 | * |
| 7624 | * @param string $source source file path |
| 7625 | * @param $targetDir |
| 7626 | * @param string $name file name |
| 7627 | * |
| 7628 | * @return bool|string |
| 7629 | * @internal param string $target target dir path |
| 7630 | * @author Dmitry (dio) Levashov |
| 7631 | */ |
| 7632 | abstract protected function _move($source, $targetDir, $name); |
| 7633 | |
| 7634 | /** |
| 7635 | * Remove file |
| 7636 | * |
| 7637 | * @param string $path file path |
| 7638 | * |
| 7639 | * @return bool |
| 7640 | * @author Dmitry (dio) Levashov |
| 7641 | **/ |
| 7642 | abstract protected function _unlink($path); |
| 7643 | |
| 7644 | /** |
| 7645 | * Remove dir |
| 7646 | * |
| 7647 | * @param string $path dir path |
| 7648 | * |
| 7649 | * @return bool |
| 7650 | * @author Dmitry (dio) Levashov |
| 7651 | **/ |
| 7652 | abstract protected function _rmdir($path); |
| 7653 | |
| 7654 | /** |
| 7655 | * Create new file and write into it from file pointer. |
| 7656 | * Return new file path or false on error. |
| 7657 | * |
| 7658 | * @param resource $fp file pointer |
| 7659 | * @param string $dir target dir path |
| 7660 | * @param string $name file name |
| 7661 | * @param array $stat file stat (required by some virtual fs) |
| 7662 | * |
| 7663 | * @return bool|string |
| 7664 | * @author Dmitry (dio) Levashov |
| 7665 | **/ |
| 7666 | abstract protected function _save($fp, $dir, $name, $stat); |
| 7667 | |
| 7668 | /** |
| 7669 | * Get file contents |
| 7670 | * |
| 7671 | * @param string $path file path |
| 7672 | * |
| 7673 | * @return string|false |
| 7674 | * @author Dmitry (dio) Levashov |
| 7675 | **/ |
| 7676 | abstract protected function _getContents($path); |
| 7677 | |
| 7678 | /** |
| 7679 | * Write a string to a file |
| 7680 | * |
| 7681 | * @param string $path file path |
| 7682 | * @param string $content new file content |
| 7683 | * |
| 7684 | * @return bool |
| 7685 | * @author Dmitry (dio) Levashov |
| 7686 | **/ |
| 7687 | abstract protected function _filePutContents($path, $content); |
| 7688 | |
| 7689 | /** |
| 7690 | * Extract files from archive |
| 7691 | * |
| 7692 | * @param string $path file path |
| 7693 | * @param array $arc archiver options |
| 7694 | * |
| 7695 | * @return bool |
| 7696 | * @author Dmitry (dio) Levashov, |
| 7697 | * @author Alexey Sukhotin |
| 7698 | **/ |
| 7699 | abstract protected function _extract($path, $arc); |
| 7700 | |
| 7701 | /** |
| 7702 | * Create archive and return its path |
| 7703 | * |
| 7704 | * @param string $dir target dir |
| 7705 | * @param array $files files names list |
| 7706 | * @param string $name archive name |
| 7707 | * @param array $arc archiver options |
| 7708 | * |
| 7709 | * @return string|bool |
| 7710 | * @author Dmitry (dio) Levashov, |
| 7711 | * @author Alexey Sukhotin |
| 7712 | **/ |
| 7713 | abstract protected function _archive($dir, $files, $name, $arc); |
| 7714 | |
| 7715 | /** |
| 7716 | * Get relative path from root path |
| 7717 | * Helper method to correctly calculate relative path regardless of trailing separator |
| 7718 | * |
| 7719 | * @param string $path Full file path |
| 7720 | * @return string Relative path from root |
| 7721 | * @author Filester Fix |
| 7722 | **/ |
| 7723 | protected function _getRelativePath($path) |
| 7724 | { |
| 7725 | // Normalize root path - remove trailing separators |
| 7726 | $root = rtrim($this->root, $this->separator . '/'); |
| 7727 | $rootLen = strlen($root); |
| 7728 | |
| 7729 | // Normalize file path |
| 7730 | $normalizedPath = str_replace($this->separator, '/', $path); |
| 7731 | $normalizedRoot = str_replace($this->separator, '/', $root); |
| 7732 | |
| 7733 | // Check if path starts with root (case-sensitive) |
| 7734 | if (strpos($normalizedPath, $normalizedRoot) === 0) { |
| 7735 | $relative = substr($normalizedPath, $rootLen); |
| 7736 | // Remove leading separator |
| 7737 | $relative = ltrim($relative, '/'); |
| 7738 | return $relative; |
| 7739 | } |
| 7740 | |
| 7741 | // Fallback: try to calculate using original method |
| 7742 | // Handle case where root ends with separator |
| 7743 | $rootWithSep = rtrim($this->root, $this->separator . '/') . $this->separator; |
| 7744 | if (strpos($path, $rootWithSep) === 0) { |
| 7745 | $relative = substr($path, strlen($rootWithSep)); |
| 7746 | return str_replace($this->separator, '/', $relative); |
| 7747 | } |
| 7748 | |
| 7749 | // Last fallback: old method |
| 7750 | if (strlen($path) > strlen($this->root)) { |
| 7751 | $relative = substr($path, strlen($this->root)); |
| 7752 | $relative = ltrim($relative, $this->separator . '/'); |
| 7753 | return str_replace($this->separator, '/', $relative); |
| 7754 | } |
| 7755 | |
| 7756 | return ''; |
| 7757 | } |
| 7758 | |
| 7759 | /** |
| 7760 | * Build file URL from base URL and relative path |
| 7761 | * Helper method to ensure correct URL construction with proper separator |
| 7762 | * |
| 7763 | * @param string $path Relative file path (already encoded if needed) |
| 7764 | * @return string Complete file URL |
| 7765 | * @author Filester Fix |
| 7766 | **/ |
| 7767 | protected function _buildFileUrl($path) |
| 7768 | { |
| 7769 | if (empty($this->URL)) { |
| 7770 | return ''; |
| 7771 | } |
| 7772 | |
| 7773 | // Remove trailing slash from base URL |
| 7774 | $baseUrl = rtrim($this->URL, '/'); |
| 7775 | |
| 7776 | // Ensure path starts with / |
| 7777 | $path = '/' . ltrim($path, '/'); |
| 7778 | |
| 7779 | return $baseUrl . $path; |
| 7780 | } |
| 7781 | |
| 7782 | /** |
| 7783 | * Detect available archivers |
| 7784 | * |
| 7785 | * @return void |
| 7786 | * @author Dmitry (dio) Levashov, |
| 7787 | * @author Alexey Sukhotin |
| 7788 | **/ |
| 7789 | abstract protected function _checkArchivers(); |
| 7790 | |
| 7791 | /** |
| 7792 | * Change file mode (chmod) |
| 7793 | * |
| 7794 | * @param string $path file path |
| 7795 | * @param string $mode octal string such as '0755' |
| 7796 | * |
| 7797 | * @return bool |
| 7798 | * @author David Bartle, |
| 7799 | **/ |
| 7800 | abstract protected function _chmod($path, $mode); |
| 7801 | |
| 7802 | |
| 7803 | } // END class |
| 7804 |