2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2008-04-11 06:12:07 +00:00
|
|
|
* Name: Bulk Add
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
2012-02-08 02:52:11 +00:00
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
2008-04-11 06:12:07 +00:00
|
|
|
* License: GPLv2
|
|
|
|
* Description: Bulk add server-side images
|
2009-01-16 08:18:41 +00:00
|
|
|
* Documentation:
|
2009-01-17 02:25:39 +00:00
|
|
|
* Upload the images into a new directory via ftp or similar, go to
|
|
|
|
* shimmie's admin page and put that directory in the bulk add box.
|
|
|
|
* If there are subdirectories, they get used as tags (eg if you
|
|
|
|
* upload into <code>/home/bob/uploads/holiday/2008/</code> and point
|
|
|
|
* shimmie at <code>/home/bob/uploads</code>, then images will be
|
2009-01-16 08:18:41 +00:00
|
|
|
* tagged "holiday 2008")
|
2010-05-28 10:39:10 +00:00
|
|
|
* <p><b>Note:</b> requires the "admin" extension to be enabled
|
2008-04-11 06:12:07 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class BulkAdd extends Extension {
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2009-05-11 21:09:24 +00:00
|
|
|
global $page, $user;
|
|
|
|
if($event->page_matches("bulk_add")) {
|
2010-05-28 13:26:46 +00:00
|
|
|
if($user->is_admin() && $user->check_auth_token() && isset($_POST['dir'])) {
|
2007-05-04 23:31:30 +00:00
|
|
|
set_time_limit(0);
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->add_dir($_POST['dir']);
|
2009-05-11 21:09:24 +00:00
|
|
|
$this->theme->display_upload_results($page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-11 21:09:24 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2012-06-17 19:05:16 +00:00
|
|
|
public function onCommand(CommandEvent $event) {
|
|
|
|
if($event->cmd == "help") {
|
|
|
|
print " bulk-add [directory]\n";
|
2014-04-06 19:47:01 +00:00
|
|
|
print " Import this directory\n\n";
|
2012-06-17 19:05:16 +00:00
|
|
|
}
|
|
|
|
if($event->cmd == "bulk-add") {
|
|
|
|
if(count($event->args) == 1) {
|
|
|
|
$this->add_dir($event->args[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event) {
|
2010-05-28 13:26:46 +00:00
|
|
|
$this->theme->display_admin_block();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-08-24 22:29:34 +00:00
|
|
|
|
2012-02-08 02:52:11 +00:00
|
|
|
/**
|
|
|
|
* Generate the necessary DataUploadEvent for a given image and tags.
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
private function add_image($tmpname, $filename, $tags) {
|
2009-08-03 09:19:33 +00:00
|
|
|
assert(file_exists($tmpname));
|
|
|
|
|
|
|
|
$pathinfo = pathinfo($filename);
|
|
|
|
if(!array_key_exists('extension', $pathinfo)) {
|
|
|
|
throw new UploadException("File has no extension");
|
|
|
|
}
|
2014-04-06 19:47:01 +00:00
|
|
|
$metadata = array();
|
2009-08-03 09:19:33 +00:00
|
|
|
$metadata['filename'] = $pathinfo['basename'];
|
|
|
|
$metadata['extension'] = $pathinfo['extension'];
|
|
|
|
$metadata['tags'] = $tags;
|
|
|
|
$metadata['source'] = null;
|
2012-08-18 18:45:39 +00:00
|
|
|
$event = new DataUploadEvent($tmpname, $metadata);
|
2009-08-03 09:19:33 +00:00
|
|
|
send_event($event);
|
|
|
|
if($event->image_id == -1) {
|
|
|
|
throw new UploadException("File type not recognised");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 04:04:37 +00:00
|
|
|
private function add_dir(/*string*/ $base, $subdir="") {
|
2007-04-16 11:58:25 +00:00
|
|
|
if(!is_dir($base)) {
|
2007-06-30 01:19:11 +00:00
|
|
|
$this->theme->add_status("Error", "$base is not a directory");
|
2007-07-20 12:30:07 +00:00
|
|
|
return;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$list = "";
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2009-08-03 09:19:33 +00:00
|
|
|
foreach(glob("$base/$subdir/*") as $fullpath) {
|
|
|
|
$fullpath = str_replace("//", "/", $fullpath);
|
|
|
|
$shortpath = str_replace($base, "", $fullpath);
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2007-12-08 04:30:11 +00:00
|
|
|
if(is_link($fullpath)) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
else if(is_dir($fullpath)) {
|
2009-08-03 09:19:33 +00:00
|
|
|
$this->add_dir($base, str_replace($base, "", $fullpath));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-08-03 09:19:33 +00:00
|
|
|
$pathinfo = pathinfo($fullpath);
|
2012-06-21 08:11:26 +00:00
|
|
|
$matches = array();
|
|
|
|
if(preg_match("/\d+ - (.*)\.([a-zA-Z]+)/", $pathinfo["basename"], $matches)) {
|
|
|
|
$tags = $matches[1];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$tags = $subdir;
|
|
|
|
$tags = str_replace("/", " ", $tags);
|
|
|
|
$tags = str_replace("__", " ", $tags);
|
|
|
|
$tags = trim($tags);
|
|
|
|
}
|
2009-08-03 09:19:33 +00:00
|
|
|
$list .= "<br>".html_escape("$shortpath (".str_replace(" ", ", ", $tags).")... ");
|
|
|
|
try{
|
|
|
|
$this->add_image($fullpath, $pathinfo["basename"], $tags);
|
2007-04-16 11:58:25 +00:00
|
|
|
$list .= "ok\n";
|
|
|
|
}
|
2009-08-03 09:19:33 +00:00
|
|
|
catch(Exception $ex) {
|
|
|
|
$list .= "failed:<br>". $ex->getMessage();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-15 22:37:15 +00:00
|
|
|
if(strlen($list) > 0) {
|
|
|
|
$this->theme->add_status("Adding $subdir", $list);
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-26 02:54:51 +00:00
|
|
|
|