1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
<?php
/**
* Class that takes care of individual files
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Jeroen Roos
* @package Zoph
*/
use conf\conf;
/**
* This class takes care of individual files
* For now, this is only used in the import module of Zoph
* in the future the photo class will be split into a "file" and "photo"
* part, which will make things more flexible.
*/
class file {
/** @var string File name */
private $name;
/** @var string Path where the file is located */
private $path;
/** @var string type of file ("image", "archive", "ignore" ...) */
public $type;
/** @var string Destination filename when copied or moved */
private $destName;
/** @var string Destination path when copied or moved */
private $destPath;
/** @var bool Make a backup if the destination file exists */
public $backup=false;
/**
* Create a new file object from a filename
* @param string filename
*/
public function __construct($filename) {
if (substr($filename, 0, 1)!="/") {
$filename=getcwd() . "/" . $filename;
}
if (is_link($filename)) {
if (!@stat($filename)) {
throw new fileSymlinkProblemException(
"There's something wrong with symlink $filename\n");
}
} else if (is_dir($filename) && !conf::get("import.cli.recursive")) {
throw new fileDirectoryNotSupportedException($filename . " is a directory\n");
}
$this->name=basename($filename);
$this->path=realpath(dirname($filename));
}
/**
* Whether or not this file is a symlink
* @param bool whether or not this is a symlink
*/
public function isLink() {
return is_link($this);
}
/**
* Get filename with extension removed
* @return string filename without ext
*/
public function getNameNoExt() {
$fileinfo = pathinfo((string)$this);
return $fileinfo['filename'];
}
/**
* Returns the link destination. Contrary to the PHP readlink() function,
* this function recurses through the links until it has located a real
* file. So, in case a link points to a link, which points to a link,
* which points to... I guess you got it.
* Also, it will simply return a file object if the file is not a link.
*/
public function readlink() {
if ($this->isLink()) {
$file=new file(readlink($this));
return $file->readlink();
} else {
return $this;
}
}
/**
* This function returns the name of a file, referenced by a directory
* and an MD5 hash of the filename.
*/
public static function getFromMD5($dir, $md5) {
$files=glob($dir . "/*");
foreach ($files as $file) {
$f=realpath($file);
log::msg($f . ": " . md5($f), log::DEBUG, log::IMPORT);
if (md5($f) == $md5) {
return new file($f);
}
}
}
/**
* Returns full path + filename
*/
public function __toString() {
return $this->getPath() . "/" . $this->getName();
}
/**
* Returns filename
*/
public function getName() {
return $this->name;
}
/**
* Returns full path
*/
public function getPath() {
return $this->path;
}
/**
* When a symlink is copied or moved, the name changes
* this function returns the new name
*/
public function getDestName() {
return $this->destName;
}
/**
* This generates an MD5 for a filename, to uniquely identify a file
* that is not (yet) in the database and therefore has no db key.
*/
public function getMD5() {
return md5($this->path . "/" . $this->name);
}
/**
* Deletes a file after doing some checks
* @param bool Also delete related files, such as thumbnails
* @param bool Do not delete the referenced file, only related files
* @todo 'related' files really should be part of the photo object.
* @see photo
*/
public function delete($thumbs=false, $thumbsOnly=false) {
log::msg("Deleting " . $this, log::NOTIFY, log::IMPORT);
if (!$thumbsOnly && file_exists($this)) {
if (!is_dir($this) && is_writable($this)) {
unlink($this);
} else {
log::msg(sprintf(translate("Could not delete %s."), $this),
log::ERROR, log::IMPORT);
return false;
}
}
if ($thumbs) {
$dir=dirname($this);
$file=basename($this);
$midname=$dir . "/" . MID_PREFIX . "/" .
MID_PREFIX . "_" . $file;
$thumbname=$dir . "/" . THUMB_PREFIX . "/" .
THUMB_PREFIX . "_" . $file;
$mid=new file($midname);
$mid->delete();
$thumb=new file($thumbname);
$thumb->delete();
$ignore=new file($this . ".zophignore");
$ignore->delete();
}
}
/**
* Set the destination for copy or move operations;
* @param string destination of the file
*/
public function setDestination($path) {
$this->destPath="/" . file::cleanupPath($path) . "/";
$this->destName=basename($this->readlink());
}
/**
* Makes checks if a file can be found and read
*/
public function check() {
if (!file_exists($this)) {
throw new fileNotFoundException("File not found: $this\n");
}
if (!is_readable($this)) {
throw new fileNotReadableException("Cannot read file: $this\n");
}
if (!conf::get("import.cli.copy") && !is_writable($this)) {
throw new fileNotWritableException("Cannot move file: $this\n");
}
}
/**
* Makes checks to see if a file can be copied
*/
public function checkCopy() {
// First checks are the same...
$this->check();
if (!is_writable($this->destPath)) {
throw new fileDirNotWritableException("Directory not writable: " .
$this->destPath);
}
if (file_exists($this->destPath . $this->destName)) {
if ($this->backup) {
$backupname=$this->destName;
$counter=1;
while (file_exists($this->destPath . $backupname)) {
// Find the . in the filename
$pos=strrpos($this->destName, ".") ?: strlen($this->destName);
$backupname=substr($this->destName, 0, $pos) . "_" . $counter . substr($this->destName, $pos);
$counter++;
}
rename($this->destPath . $this->destName, $this->destPath . $backupname);
} else {
throw new fileExistsException("File already exists: " .
$this->destPath . $this->destName);
}
}
return true;
}
/**
* Makes checks if a file can be moved
*/
public function checkMove() {
// First checks are the same...
$this->checkCopy();
if (!is_writable($this)) {
throw new fileNotWritableException("File is not writable: " . $this);
}
return true;
}
/**
* Moves a file
*/
public function move() {
$destPath=$this->destPath;
$destName=$this->destName;
$dest=$destPath . "/" . $destName;
log::msg("Going to move $this to $dest", log::DEBUG, log::GENERAL);
$this->checkMove();
if ($this->isLink()) {
// in case of a link, we copy the link destination and delete the link
$copy=$this->readlink();
$copy->setDestination($destPath);
$newfile=$copy->copy();
unlink($this);
return $newfile;
} else {
if (rename($this, $dest)) {
return new file($dest);
} else {
throw new fileMoveFailedException("Could not move $this to $dest");
}
}
}
/**
* Copies a file
*/
public function copy() {
$destPath=$this->destPath;
$destName=$this->destName;
$dest=$destPath . "/" . $destName;
$this->checkCopy();
if (copy($this, $dest)) {
return new file($dest);
} else {
throw new fileCopyFailedException("Could not copy $this to $dest");
}
}
/**
* Changes the permissions for a file
*/
public function chmod($mode = null) {
if ($mode===null) {
$mode=octdec(conf::get("import.filemode"));
}
if (!chmod($this, $mode)) {
log::msg("Could not change permissions for <b>" . $this . "</b>",
log::ERROR, log::IMPORT);
}
}
/**
* Gets MIME type for this file
*/
public function getMime() {
$fileinfo=new finfo(FILEINFO_MIME, conf::get("path.magic"));
$mime=explode(";", $fileinfo->file($this->readlink()));
log::msg("<b>" . $this->readlink() . "</b>: " . $mime[0], log::DEBUG, log::IMPORT);
$this->setFiletype($mime[0]);
return $mime[0];
}
/**
* Read first bytes from file
* @param int number of bytes
* @return string header of file
*/
private function read(int $bytes) {
$fp = fopen($this, "rb");
$data=fread($fp, $bytes);
fclose($fp);
return $data;
}
/**
* Gets type of file for this file
*/
private function setFiletype($mime) {
switch ($mime) {
case "image/jpeg":
case "image/png":
case "image/gif":
$type="image";
break;
case "application/x-bzip2":
case "application/x-gzip":
case "application/x-tar":
case "application/gzip":
case "application/zip":
$type="archive";
break;
case "text/xml":
case "application/xml":
case "text/plain":
case "application/gpx+xml":
$type="xml";
break;
case "directory":
$type="directory";
break;
default:
$type=false;
}
if ($type == "xml") {
$header=$this->read(100);
if (strpos($header, "<gpx") !== false) {
$type="gpx";
} else if (strpos($header, "<x:xmpmeta") !== false) {
$type="xmp";
}
}
$this->type=$type;
return $type;
}
/**
* Get files in a specific directory
*
* This function creates a list of files in a specific directory and
* filters it on a given search string and filetypes.
* @param string The dir to search
* @param bool Whether or not to descent into directories
* @param string Search string
*/
public static function getFromDir($dir, $recursive = false, $search=null) {
$files = scandir($dir);
$return = array();
foreach ($files as $filename) {
if ($filename[0]!=".") {
if (is_dir($dir . "/" . $filename)) {
if ($recursive) {
$return=array_merge($return, static::getFromDir($dir . "/" . $filename, true));
}
} else if (is_null($search) || preg_match($search, $filename)) {
$file=new file($dir . "/" . $filename);
if (!file_exists($dir . "/" . $filename . ".zophignore")) {
$file->getMime();
} else {
$file->type = "ignore";
}
if ($file->type) {
$return[]=$file;
}
}
}
}
return $return;
}
/**
* Cleans up a path, by removing all double slashes, "/./",
* leading and trailing slashes.
*/
public static function cleanupPath($path) {
$search = array("/(\/+)/", "/(\/\.\/)/", "/(\/$)/", "/(^\/)/");
$replace = array("/", "/", "", "");
return preg_replace($search, $replace, $path);
}
/**
* Create a directory
* @param string directory to create
* @return bool true when succesful
* @throws fileDirCreationFailedException when creation fails
*/
private static function createDir($directory) {
if (!file_exists($directory)) {
if (@mkdir($directory, octdec(conf::get("import.dirmode")))) {
if (!defined("CLI") || conf::get("import.cli.verbose")>=1) {
log::msg(translate("Created directory") . ": $directory", log::NOTIFY, log::GENERAL);
}
return true;
} else {
throw new fileDirCreationFailedException(
translate("Could not create directory") . ": $directory<br>\n");
}
}
}
/**
* Recursively create directory
* checks if the parent dir of the dir to be created exists and if not so, tries to
* create it first
* @param string directory to create
* @return bool true when succesful
*/
public static function createDirRecursive($directory) {
$directory="/" . static::cleanupPath($directory);
if (!file_exists(dirname($directory))) {
static::createDirRecursive(dirname($directory));
}
try {
static::createDir($directory);
} catch (fileDirCreationFailedException $e) {
log::msg($e->getMessage(), log::FATAL, log::GENERAL);
}
}
}
?>
|