streams/util/dcp

158 lines
3.5 KiB
Text
Raw Normal View History

#!/usr/bin/env php
<?php
// file import to DAV utility
2021-12-02 22:33:36 +00:00
use Zotlabs\Daemon\Run;
if(!file_exists('include/cli_startup.php')) {
echo 'Run dcp from the top level Hubzilla web directory, as util/dcp <args>' . PHP_EOL;
exit(1);
}
require_once('include/cli_startup.php');
require_once('include/attach.php');
cli_startup();
if($argc < 3) {
2021-12-02 23:02:31 +00:00
echo 'Usage: ' . $argv[0] . ' src dstdir' . "\n";
echo 'Always run from the toplevel web directory.' . "\n";
echo 'destination should begin with store/$nickname/desired/path or $nickname/desired/path' . "\n";
echo 'Example: util/dcp /etc/motd store/joe/etc' . "\n";
exit;
}
$recursive = false;
$dstfile = $argv[$argc - 1];
if(strpos($dstfile,'store/') === 0)
$dstfile = substr($dstfile,6);
2018-10-04 02:10:52 +00:00
if(strpos($dstfile,'/')) {
$nick = substr($dstfile,0,strpos($dstfile,'/'));
$dstfile = substr($dstfile,strlen($nick)+1);
}
else {
$nick = $dstfile;
$dstfile = '';
}
$channel = channelx_by_nick($nick);
if(! $channel)
return;
2017-08-23 03:32:02 +00:00
for($x = 1; $x < ($argc - 1); $x ++) {
if(($argv[$x] === '-r') || ($argv[$x] === '-R')) {
$recursive = true;
break;
}
}
$rootdir = ((strlen(trim($dstfile,'/'))) ? false : true);
$isadir = false;
2017-08-23 03:32:02 +00:00
if(($recursive) || ($argc > 3))
$isadir = true;
if($rootdir) {
$folder = '';
}
else {
$r = q("select * from attach where display_path = '%s' and uid = %d limit 1",
dbesc($dstfile),
intval($channel['channel_id'])
);
if($r && $r[0]['is_dir']) {
$isadir = true;
$basepath = $dstfile;
$folder = $r[0]['hash'];
}
else {
$pathname = (($isadir) ? $dstfile : dirname($dstfile));
$arr = [
'pathname' => $pathname,
'allow_cid' => $channel['channel_allow_cid'],
'allow_gid' => $channel['channel_allow_gid'],
'deny_cid' => $channel['channel_deny_cid'],
'deny_gid' => $channel['channel_deny_gid'],
];
$folder = '';
if($pathname && $isadir) {
$x = attach_mkdirp($channel,$channel['channel_hash'],$arr);
if($x['success'])
$folder = $x['data']['hash'];
}
}
}
for($x = 1; $x < ($argc - 1); $x ++) {
if(($argv[$x] === '-r') || ($argv[$x] === '-R')) {
continue;
}
if(is_dir($argv[$x])) {
if($recursive) {
dcp_recurse($channel,$argv[$x],$basepath,$folder);
}
else {
continue;
}
}
else {
$dstname = (($isadir) ? '' : basename($dstfile));
$cmd = [ 'Importfile', $channel['channel_id'], $argv[$x], $folder, $dstname ];
2021-12-02 22:33:36 +00:00
Run::Summon($cmd);
}
}
function dcp_recurse($channel,$src,$basepath,$folder) {
$dir = opendir($src);
if($dir) {
while(($entry = readdir($dir)) !== false) {
if($entry === '.' || $entry === '..')
continue;
2017-08-23 03:32:02 +00:00
$dstfile = $basepath . '/' . $entry;
if(is_dir($src . '/' . $entry)) {
$r = q("select * from attach where display_path = '%s' and uid = %d limit 1",
dbesc($dstfile),
intval($channel['channel_id'])
);
if($r && $r[0]['is_dir']) {
$folder = $r[0]['hash'];
}
else {
$arr = [
'pathname' => $dstfile,
'allow_cid' => $channel['channel_allow_cid'],
'allow_gid' => $channel['channel_allow_gid'],
'deny_cid' => $channel['channel_deny_cid'],
'deny_gid' => $channel['channel_deny_gid'],
];
$folder = '';
$x = attach_mkdirp($channel,$channel['channel_hash'],$arr);
if($x['success'])
$folder = $x['data']['hash'];
}
2017-08-23 03:32:02 +00:00
dcp_recurse($channel,$src . '/' . $entry,$dstfile,$folder);
}
else {
$cmd = [ 'Importfile', $channel['channel_id'], $src . '/' . $entry, $folder ];
2021-12-02 22:33:36 +00:00
Run::Summon($cmd);
}
}
closedir($dir);
}
}