2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2008-04-11 06:12:07 +00:00
|
|
|
/**
|
|
|
|
* Name: Bulk Add
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* License: GPLv2
|
|
|
|
* Description: Bulk add server-side images
|
2009-01-16 08:18:41 +00:00
|
|
|
* Documentation:
|
|
|
|
* 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 /home/bob/uploads/holiday/2008/
|
|
|
|
* and point shimmie ad /home/bob/uploads, then images will be
|
|
|
|
* tagged "holiday 2008")
|
2008-04-11 06:12:07 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
class BulkAdd implements Extension {
|
2007-06-30 01:19:11 +00:00
|
|
|
var $theme;
|
2007-08-24 22:29:34 +00:00
|
|
|
|
2008-08-23 12:08:19 +00:00
|
|
|
public function receive_event(Event $event) {
|
2008-09-06 16:59:02 +00:00
|
|
|
if(is_null($this->theme)) $this->theme = get_theme_object($this);
|
2007-06-30 01:19:11 +00:00
|
|
|
|
2008-09-06 17:48:03 +00:00
|
|
|
if(($event instanceof PageRequestEvent) && $event->page_matches("bulk_add")) {
|
2007-08-24 22:29:34 +00:00
|
|
|
if($event->user->is_admin() && 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']);
|
2007-07-17 07:45:35 +00:00
|
|
|
$this->theme->display_upload_results($event->page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-22 09:41:30 +00:00
|
|
|
if($event instanceof AdminBuildingEvent) {
|
2007-04-16 11:58:25 +00:00
|
|
|
global $page;
|
2007-06-30 01:19:11 +00:00
|
|
|
$this->theme->display_admin_block($page);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-24 22:29:34 +00:00
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
private function add_image($tmpname, $filename, $tags) {
|
2007-12-08 02:51:52 +00:00
|
|
|
if(file_exists($tmpname)) {
|
|
|
|
global $user;
|
|
|
|
$pathinfo = pathinfo($filename);
|
|
|
|
$metadata['filename'] = $pathinfo['basename'];
|
|
|
|
$metadata['extension'] = $pathinfo['extension'];
|
|
|
|
$metadata['tags'] = $tags;
|
|
|
|
$metadata['source'] = null;
|
2009-01-04 14:01:59 +00:00
|
|
|
try {
|
|
|
|
$event = new DataUploadEvent($user, $tmpname, $metadata);
|
|
|
|
send_event($event);
|
|
|
|
}
|
|
|
|
catch(Exception $ex) {
|
|
|
|
return $ex->getMessage();
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function add_dir($base, $subdir="") {
|
|
|
|
global $page;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
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
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
$dir = opendir("$base/$subdir");
|
|
|
|
while($filename = readdir($dir)) {
|
|
|
|
$fullpath = "$base/$subdir/$filename";
|
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)) {
|
2007-04-16 11:58:25 +00:00
|
|
|
if($filename[0] != ".") {
|
|
|
|
$this->add_dir($base, "$subdir/$filename");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$tmpfile = $fullpath;
|
2007-12-08 04:08:29 +00:00
|
|
|
$tags = $subdir;
|
|
|
|
$tags = str_replace("/", " ", $tags);
|
|
|
|
$tags = str_replace("__", " ", $tags);
|
2008-12-15 23:17:56 +00:00
|
|
|
$tags = trim($tags);
|
|
|
|
$list .= "<br>".html_escape("$subdir/$filename (".str_replace(" ", ", ", $tags).")... ");
|
2007-12-08 04:08:29 +00:00
|
|
|
$error = $this->add_image($tmpfile, $filename, $tags);
|
2007-12-08 02:51:52 +00:00
|
|
|
if(is_null($error)) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$list .= "ok\n";
|
|
|
|
}
|
|
|
|
else {
|
2008-12-15 23:17:56 +00:00
|
|
|
$list .= "failed:<br>$error\n";
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dir);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
add_event_listener(new BulkAdd());
|
|
|
|
?>
|