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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
|
<?php
/**
* Controller for the CLI
*
* 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
*/
namespace cli;
use album;
use auth\validator;
use category;
use conf\conf;
use file;
use group;
use import\cli as cliimport;
use person;
use photo;
use place;
use template\block;
use user;
use xmp\decoder as xmpdecoder;
use xmp\reader as xmpreader;
use Exception;
use albumNotFoundException;
use categoryNotFoundException;
use cliAPINotCompatibleException;
use cliException;
use cliNoFilesException;
use cliNoPasswordException;
use cliNoUserException;
use cliNotInCWDException;
use cliUnknownErrorException;
use cliUserNotAdminException;
use importException;
use importFileNotFoundException;
use importFileNotImportableException;
use importFileNotInPathException;
use importIdIsNotNumericException;
use importMultipleMatchesException;
use personNotFoundException;
use placeNotFoundException;
use userAlreadyExistsException;
/**
* Controller class for the CLI
*/
class cli {
/**
* Defines the API version between the /bin/zoph binary and the files in the webroot
* these must be equal.
*/
const API=9;
/**
* @var user The user that is doing the import
*/
private $user;
/**
* @var string Commandline arguments
*/
private $args;
/**
* List of files to be imported
*/
private $files=array();
private $photos=array();
/**
* Create cli object
* @param User user doing the import
* @param int API version of the executable script. This is used to check if the executable
* script is compatible with the scripts in php directory
* @param $args array of CLI arguments
*/
public function __construct(user $user, $api, array $args) {
if ($api != static::API) {
throw new cliAPINotCompatibleException("This Zoph installation is not compatible " .
"with the Zoph executable you are running.");
}
$this->user=$user;
if (!$user->isAdmin()) {
throw new cliUserNotAdminException("CLI_USER must be an admin user");
}
user::setCurrent($user);
$user->prefs->load();
$user->loadLanguage();
$this->args=new arguments($args);
}
/**
* Run the CLI
*/
public function run() {
$this->processFiles();
switch (arguments::$command) {
case "import":
$this->doImport();
break;
case "update":
$this->doUpdate();
break;
case "new":
$this->addNew();
break;
case "config":
$this->doConfig();
break;
case "getconfig":
$this->doGetConfig();
break;
case "dumpconfig":
$this->doDumpConfig();
break;
case "adduser":
$this->doAddUser();
break;
case "deleteuser":
$this->doDeleteUser();
break;
case "showuser":
$this->doShowUser();
break;
case "updateuser":
$this->doUpdateUser();
break;
case "version":
static::showVersion();
break;
case "help":
static::showHelp();
break;
default:
throw new cliUnknownErrorException("Unknown command, please file a bug");
}
}
/**
* Check list of files
*/
private function processFiles() {
$files=$this->args->getFiles();
foreach ($files as $filename) {
try {
if (arguments::$command=="import") {
$file=new file($filename);
$file->check();
$file->getMime();
if ($file->type=="directory" && conf::get("import.cli.recursive")) {
$this->files=array_merge($this->files, file::getFromDir($file, true));
} else if ($file->type!="image") {
throw new importFileNotImportableException("$file is not an image\n");
} else {
$this->files[]=$file;
}
} else {
if (conf::get("import.cli.useids")) {
$file=$filename;
if (is_numeric($file)) {
$this->photos[]=$this->lookupFileById($file);
} else if (preg_match("/^[0-9]+-[0-9]+$/", $file)) {
list($start, $end) = explode("-", $file);
foreach (range($start, $end) as $id) {
try {
$this->photos[]=$this->lookupFileById($id);
} catch (importException $e) {
echo $e->getMessage();
}
}
} else {
throw new importIdIsNotNumericException(
"$file is not numeric, but --useids is set.\n");
}
} else {
$this->photos[]=$this->lookupFile($filename);
}
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
/**
* Looks up a photo by photo_id
*/
private function lookupFileById($id) {
$photo=new photo((int) $id);
$count=$photo->lookup();
if ($count==1) {
return $photo;
} else if ($count==0) {
throw new importFileNotFoundException("No photo with id $id was found\n");
} else {
throw new importMultipleMatchesException(
"Multiple photos with id $id were found. This is probably a bug");
}
}
/**
* Looks up a file by filename
*/
private function lookupFile($file) {
$filename=basename($file);
$path=dirname($file);
if ($path==".") {
// No path given
//unset($path);
$path="./";
}
if (substr($path, 0, 2)=="./") {
// Path relative to the current dir given, change into absolute path
$path="/" . file::cleanupPath(getcwd() . "/" . $path);
}
if ($path[0]=="/") {
// absolute path given
$path="/" . file::cleanupPath($path) . "/";
// check if path is in conf::get("path.images")
if (substr($path, 0, strlen(conf::get("path.images")))!=conf::get("path.images")) {
throw new importFileNotInPathException($path . "/" . $file ." is not in the images path (" .
conf::get("path.images") . "), skipping.\n");
} else {
$path=substr($path, strlen(conf::get("path.images")));
if ($path[0]=="/") {
// conf::get("path.images") didn't end in '/', let's cut it off
$path=substr($path, 1);
}
}
} else {
$path=file::cleanupPath($path);
}
$photos=photo::getByName($filename, $path);
if (empty($photos)) {
$photos=photo::getByName($filename);
}
if (empty($photos)) {
throw new importFileNotFoundException($file ." not found.\n");
} else if (sizeof($photos)==1) {
return $photos[0];
} else {
throw new importMultipleMatchesException("Multiple files named " . $file ." found.\n");
}
}
/**
* Process --import
*/
private function doImport() {
$vars=$this->args->getVars();
if (conf::get("import.cli.add.auto")) {
$vars=$this->addNew();
}
if (is_array($this->files) && !empty($this->files)) {
if (!isset($vars["_dirpattern"])) {
$photos=array();
foreach (array_unique($this->files) as $file) {
$photo=new photo();
$photo->file["orig"]=$file;
if (isset($vars["_xmp"])) {
$this->processXMP($photo, $file);
}
$photos[]=$photo;
}
} else {
$photos=$this->processDirpattern();
}
cliimport::photos($photos, $vars);
} else {
throw new cliNoFilesException("Nothing to do, exiting");
}
}
/**
* Process --update
*/
private function doUpdate() {
if (is_array($this->photos) && !empty($this->photos)) {
$total=sizeof($this->photos);
$cur=0;
foreach ($this->photos as $photo) {
cliimport::progress($cur, $total);
$cur++;
$photo->lookup();
$photo->setFields($this->args->getVars());
$photo->update();
$photo->updateRelations($this->args->getVars(), "_id");
if (conf::get("import.cli.thumbs")===true) {
$photo->thumbnail(true);
}
if (conf::get("import.cli.exif")===true) {
$photo->updateEXIF();
}
if (conf::get("import.cli.size")===true) {
$photo->updateSize();
}
if (conf::get("import.cli.hash")===true) {
$photo->getHash();
}
}
} else {
throw new cliNoFilesException("Nothing to do, exiting");
}
}
/**
* Add albums, categories, places, people that should be added because of --new or --autoadd
* if $vars is given,
*/
public function addNew() {
$vars=$this->args->getVars();
$newvars=array();
$returnVars=array();
foreach ($vars as $var => $array) {
switch ($var) {
case "_new_album":
$newvars["_album_id"]=array();
foreach ($array as $new) {
$album=new album();
$album->set("album", $new["name"]);
$album->set("parent_album_id", (int) $new["parent"]);
$album->insert();
$newvars["_album_id"][]=$album->getId();
}
break;
case "_new_cat":
$newvars["_category_id"]=array();
foreach ($array as $new) {
$cat=new category();
$cat->set("category", $new["name"]);
$cat->set("parent_category_id", (int) $new["parent"]);
$cat->insert();
$newvars["_category_id"][]=$cat->getId();
}
break;
case "_new_place":
foreach ($array as $new) {
$place=new place();
$place->set("title", $new["name"]);
$place->set("parent_place_id", (int) $new["parent"]);
$place->insert();
$newvars["location_id"]=$place->getId();
}
break;
case "_new_person":
$newvars["_person_id"]=array();
foreach ($array as $new) {
$person=new person();
$person->setName($new);
$person->insert();
$newvars["_person_id"][]=$person->getId();
}
break;
case "_new_photographer":
foreach ($array as $new) {
$person=new person();
$person->setName($new);
$person->insert();
$newvars["photographer_id"]=$person->getId();
}
break;
default:
$returnVars[$var]=$array;
break;
}
}
foreach ($newvars as $name => $array) {
if (array_key_exists($name, $returnVars) && is_array($returnVars[$name])) {
$returnVars[$name]=array_merge($returnVars[$name], $array);
}
$returnVars[$name]=$array;
}
return($returnVars);
}
/**
* Process --config
*/
private function doConfig() {
$vars=$this->args->getVars();
$name=$vars["_configitem"];
$default=isset($vars["_configdefault"]);
$item=conf::getItemByName($name);
if ($default) {
$value=$item->getDefault();
} else {
$value=$vars["_configvalue"];
}
if (conf::get("import.cli.verbose") > 0) {
echo "Setting config \"$name\" to \"$value\"" .
($default ? " (default)" : "") . "\n";
}
$item->setValue($value);
$item->update();
}
/**
* Process --getconfig
*/
private function doGetConfig() {
$vars=$this->args->getVars();
$name=$vars["_getconfigitem"];
$item=conf::getItemByName($name);
echo $item->displayValue() . "\n";
}
/**
* Process --dump-config
*/
private function doDumpConfig() {
$conf=conf::getAll();
foreach ($conf as $item) {
foreach ($item->getItems() as $citem) {
echo $citem->getName() . ": " . $citem->displayValue() . "\n";
}
}
}
/**
* Process --user add
*/
private function doAddUser() {
$vars=$this->args->getVars();
if (!isset($vars["_username"])) {
throw new cliNoUserException(
sprintf(translate("No user (%s) specified"), "--username"));
}
if (!isset($vars["_password"])) {
throw new cliNoPasswordException(
translate("No password specified"));
}
if (user::exists($vars["_username"])) {
throw new userAlreadyExistsException("Username already exists.");
}
$user = new user();
$user->set("user_name", $vars["_username"]);
$user->set("password", validator::hashPassword($vars["_password"]));
if (isset($vars["_admin"]) && $vars["_admin"] === true) {
$user->set("user_class", 0);
} else {
$user->set("user_class", 1);
}
$user->insert();
}
/**
* Process --user delete
*/
private function doDeleteUser() {
$vars=$this->args->getVars();
if (!isset($vars["_userid"])) {
throw new cliNoUserException(
sprintf(translate("No user (%s) specified"), "--username / --userid"));
}
$u = new user($vars["_userid"]);
$u->lookup();
$delete = false;
if (!isset($vars["_force"])) {
if (posix_isatty(STDIN)) {
// Script is run from a terminal
echo "Are you sure you want to delete user " . $u->getName() . "? [y/N] ";
$response = chop(fgets(STDIN));
if (in_array($response, [ "y", "Y" ])) {
$delete = true;
}
} else {
throw new cliException("When deleting a user from a script you must supply -f");
}
} else {
$delete = true;
}
if ($delete) {
$u->delete();
}
}
/**
* Process --user show
*/
private function doShowUser() {
$vars=$this->args->getVars();
if (!isset($vars["_userid"])) {
throw new cliNoUserException(
sprintf(translate("No user (%s) specified"), "--username / --userid"));
}
$u = new user($vars["_userid"]);
$u->lookup();
$da=$u->getDisplayArray();
$groups = array_map(
function ($g) {
$g->lookup();
return $g->getName();
}, $u->getGroups()
);
$tpl = new block("cliShowUser", array(
"userid" => $u->get("user_id"),
"username" => $da["username"],
"userclass" => $da["class"],
"groups" => $groups,
"lastlogin" => $da["last login"],
"rights" => $u->getAccessRightsArray()
));
echo $tpl . "\n\n";
}
/**
* Process --user update
*/
private function doUpdateUser() {
$vars=$this->args->getVars();
if (!isset($vars["_userid"])) {
throw new cliNoUserException(
sprintf(translate("No user (%s) specified"), "--username / --userid"));
}
$u = new user($vars["_userid"]);
$u->lookup();
$u = $this->doUpdateAccess($u);
$u = $this->doUpdateGroups($u);
if (isset($vars["_password"])) {
$u->set("password", validator::hashPassword($vars["_password"]));
}
if (isset($vars["_admin"]) && $vars["_admin"] === true) {
$u->set("user_class", 0);
}
if (isset($vars["_no_admin"]) && $vars["_no_admin"] === true) {
$u->set("user_class", 1);
}
$u->update();
}
private function doUpdateAccess(user $u) {
$vars=$this->args->getVars();
if (isset($vars["_add_access"]) && is_array($vars["_add_access"])) {
foreach ($vars["_add_access"] as $access) {
$u->set($access, 1);
}
}
if (isset($vars["_remove_access"]) && is_array($vars["_remove_access"])) {
foreach ($vars["_remove_access"] as $access) {
$u->set($access, 0);
}
}
return $u;
}
private function doUpdateGroups(user $u) {
$vars=$this->args->getVars();
if (isset($vars["_groups"]) && is_array($vars["_groups"])) {
foreach ($vars["_groups"] as $group) {
$g = group::getByName($group);
$g->addMember($u);
}
}
if (isset($vars["_remove_groups"]) && is_array($vars["_remove_groups"])) {
foreach ($vars["_remove_groups"] as $group) {
$g = group::getByName($group);
$g->removeMember($u);
}
}
return $u;
}
/**
* Process the --dirpattern setting
*/
public function processDirpattern() {
$vars=$this->args->getVars();
$patt=str_split($vars["_dirpattern"]);
$cur=getcwd();
$curlen=strlen($cur);
foreach ($this->files as $file) {
if (substr($file, 0, $curlen) != $cur) {
throw new cliNotInCWDException("Sorry, --dirpattern can only be used when " .
"importing files under the current dir. i.e. do not use absolute paths " .
"or '../' when specifying --dirpattern.");
}
$filename=substr($file, $curlen + 1);
$dirs=explode("/", $filename);
array_pop($dirs);
$photo=new photo();
$photo->file["orig"]=$file;
$counter=0;
foreach ($dirs as $dir) {
if (isset($patt[$counter])) {
switch ($patt[$counter]) {
case "a":
// album
$album=album::getByName($dir);
if ($album[0] instanceof album) {
if (!is_array($photo->_album_id)) {
$photo->_album_id=array();
}
$photo->_album_id[]=$album[0]->getId();
} else {
throw new albumNotFoundException("Album not found: " . $dir);
}
break;
case "c":
// category
$cat=category::getByName($dir);
if ($cat[0] instanceof category) {
if (!is_array($photo->_category_id)) {
$photo->_category_id=array();
}
$photo->_category_id[]=$cat[0]->getId();
} else {
throw new categoryNotFoundException("Category not found: " . $dir);
}
break;
case "l":
// location
$place=place::getByName($dir);
if ($place[0] instanceof place) {
$photo->set("location_id", $place[0]->getId());
} else {
throw new placeNotFoundException("Place not found: " . $dir);
}
break;
case "p":
// person
$person=person::getByName($dir);
if ($person[0] instanceof person) {
if (!is_array($photo->_person_id)) {
$photo->_person_id=array();
}
$photo->_person_id[]=$person[0]->getId();
} else {
throw new personNotFoundException("Person not found: " . $dir);
}
break;
case "D":
// dir / path
$path=$photo->_path;
if (!empty($path)) {
$path .= "/";
}
$photo->_path=$path . $dir;
break;
case "P":
// photographer
$person=person::getByName($dir);
if ($person[0] instanceof person) {
$photo->set("photographer_id", $person[0]->getId());
} else {
throw new personNotFoundException("Person not found: " . $dir);
}
break;
default:
// should never happen...
throw new cliUnknownErrorException("Unknown error");
}
}
$counter++;
}
$photos[]=$photo;
}
return $photos;
}
/**
* Process XMP data in photo
*/
private function processXMP(photo &$photo, file $file) : void {
$xmp = new xmpReader($file);
$data=new xmpdecoder($xmp->getXMP());
if (sizeof($data)===0) {
$data = new xmpdecoder($xmp->getXMPfromSidecar());
}
$subjects = $data->getSubjects();
if (!empty($subjects)) {
$this->processXMPSubjects($photo, $subjects);
}
$people = $data->getPeople();
if (!empty($people)) {
$this->processXMPPeople($photo, $people);
}
$rating = $data->getRating();
if ($rating) {
$photo->_rate=$rating;
}
$photo->set("sidecar", $xmp->sidecar);
}
/**
* Process XMP data in photo
* Read subjects (in Zoph stored as categories)
*/
private function processXMPSubjects(photo &$photo, $subjects) : void {
foreach ($subjects as $subject) {
$categories = category::getByName($subject);
$category = array_pop($categories);
if ($category instanceof category) {
if (!isset($photo->_category_id)) {
$photo->_category_id=array();
}
$photo->_category_id[]=$category->getId();
}
}
}
/**
* Process XMP data in photo
* Read people
*/
private function processXMPpeople(photo &$photo, $people) : void {
foreach ($people as $name) {
$people = person::getByName($name);
$person = array_pop($people);
if ($person instanceof person) {
if (!isset($photo->_person_id)) {
$photo->_person_id=array();
}
$photo->_person_id[]=$person->getId();
}
}
}
/**
* Show help
*/
private static function showHelp() {
echo "zoph " . VERSION . "\n";
echo <<<END
Usage: zoph [OPTIONS] [IMAGE ...]
OPTIONS:
--instance "INSTANCE"
--import
--update
--version
--help
--album "ALBUM"
--category "CATEGORY"
--photographer "FIRST_NAME LAST_NAME"
--location "PLACE"
--person "FIRST_NAME LAST_NAME"
--field "FIELD=VALUE"
--[no-]thumbs
--[no-]exif
--[no-]size
--useids
--move
--copy
--[no-]dateddirs
--[no-]hierarchical
--path
END;
}
/**
* Tells user which Zoph version is being used
*/
private static function showVersion() {
echo "Zoph v" . VERSION . ", released " . RELEASEDATE . "\n";
}
}
?>
|