2007-12-08 03:10:07 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2010-01-05 10:32:39 +00:00
|
|
|
* Name: Handle Archives
|
2007-12-08 03:10:07 +00:00
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
|
|
|
* Description: Allow users to upload archives (zip, etc)
|
2009-01-16 08:18:41 +00:00
|
|
|
* Documentation:
|
|
|
|
* Note: requires exec() access and an external unzip command
|
|
|
|
* <p>Any command line unzipper should work, some examples:
|
|
|
|
* <p>unzip: <code>unzip -d "%d" "%f"</code>
|
|
|
|
* <br>7-zip: <code>7zr x -o"%d" "%f"</code>
|
2007-12-08 03:10:07 +00:00
|
|
|
*/
|
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class ArchiveFileHandler extends Extension {
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onInitExt(InitExtEvent $event) {
|
2009-05-11 21:09:24 +00:00
|
|
|
global $config;
|
|
|
|
$config->set_default_string('archive_extract_command', 'unzip -d "%d" "%f"');
|
|
|
|
}
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
2009-05-11 21:09:24 +00:00
|
|
|
$sb = new SetupBlock("Archive Handler Options");
|
|
|
|
$sb->add_text_option("archive_tmp_dir", "Temporary folder: ");
|
|
|
|
$sb->add_text_option("archive_extract_command", "<br>Extraction command: ");
|
|
|
|
$sb->add_label("<br>%f for archive, %d for temporary directory");
|
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
2007-12-08 17:13:15 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onDataUpload(DataUploadEvent $event) {
|
2009-05-11 21:09:24 +00:00
|
|
|
if($this->supported_ext($event->type)) {
|
2007-12-08 17:13:15 +00:00
|
|
|
global $config;
|
2008-04-06 21:38:32 +00:00
|
|
|
$tmp = sys_get_temp_dir();
|
2007-12-08 17:13:15 +00:00
|
|
|
$tmpdir = "$tmp/shimmie-archive-{$event->hash}";
|
|
|
|
$cmd = $config->get_string('archive_extract_command');
|
2009-08-03 09:19:33 +00:00
|
|
|
$cmd = str_replace('%f', $event->tmpname, $cmd);
|
|
|
|
$cmd = str_replace('%d', $tmpdir, $cmd);
|
|
|
|
exec($cmd);
|
2015-08-23 15:09:52 +00:00
|
|
|
$results = add_dir($tmpdir);
|
|
|
|
if(count($results) > 0) {
|
2017-05-12 06:43:10 +00:00
|
|
|
// Not all themes have the add_status() method, so need to check before calling.
|
|
|
|
if (method_exists($this->theme, "add_status")) {
|
|
|
|
$this->theme->add_status("Adding files", $results);
|
|
|
|
}
|
2015-05-24 15:08:26 +00:00
|
|
|
}
|
2009-08-03 09:19:33 +00:00
|
|
|
deltree($tmpdir);
|
2012-05-08 17:32:55 +00:00
|
|
|
$event->image_id = -2; // default -1 = upload wasn't handled
|
2007-12-08 03:10:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function supported_ext($ext) {
|
|
|
|
$exts = array("zip");
|
2009-06-05 19:53:00 +00:00
|
|
|
return in_array(strtolower($ext), $exts);
|
2007-12-08 03:10:07 +00:00
|
|
|
}
|
|
|
|
}
|