Merge branch 'dev' of ../p3 into dev

This commit is contained in:
nobody 2020-12-10 16:37:34 -08:00
commit 82e5639e73
5 changed files with 211 additions and 2 deletions

View file

@ -0,0 +1,59 @@
<?php
namespace Zotlabs\Daemon;
use Zotlabs\Web\HTTPSig;
require_once('include/cli_startup.php');
require_once('include/attach.php');
require_once('include/import.php');
class Content_importer {
static public function run($argc,$argv) {
cli_startup();
$page = $argv[1];
$since = $argv[2];
$until = $argv[3];
$channel_address = $argv[4];
$hz_server = urldecode($argv[5]);
$m = parse_url($hz_server);
$channel = channelx_by_nick($channel_address);
if(! $channel) {
logger('itemhelper: channel not found');
killme();
}
$headers = [
'X-API-Token' => random_string(),
'X-API-Request' => $hz_server . '/api/z/1.0/item/export_page?f=&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page ,
'Host' => $m['host'],
'(request-target)' => 'get /api/z/1.0/item/export_page?f=&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page ,
];
$headers = HTTPSig::create_sig($headers,$channel['channel_prvkey'], channel_url($channel),true,'sha512');
$x = z_fetch_url($hz_server . '/api/z/1.0/item/export_page?f=&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page,false,$redirects,[ 'headers' => $headers ]);
if(! $x['success']) {
logger('no API response',LOGGER_DEBUG);
killme();
}
$j = json_decode($x['body'],true);
if (! $j) {
killme();
}
if(! ($j['item'] || count($j['item'])))
killme();
import_items($channel,$j['item'],false,((array_key_exists('relocate',$j)) ? $j['relocate'] : null));
killme();
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace Zotlabs\Daemon;
use Zotlabs\Web\HTTPSig;
require_once('include/cli_startup.php');
require_once('include/attach.php');
require_once('include/import.php');
class File_importer {
static public function run($argc,$argv) {
cli_startup();
$attach_id = $argv[1];
$channel_address = $argv[2];
$hz_server = urldecode($argv[3]);
$m = parse_url($hz_server);
$channel = channelx_by_nick($channel_address);
if(! $channel) {
logger('filehelper: channel not found');
killme();
}
$headers = [
'X-API-Token' => random_string(),
'X-API-Request' => $hz_server . '/api/z/1.0/file/export?f=&file_id=' . $attach_id,
'Host' => $m['host'],
'(request-target)' => 'get /api/z/1.0/file/export?f=&file_id=' . $attach_id,
];
$headers = HTTPSig::create_sig($headers,$channel['channel_prvkey'],channel_url($channel),true,'sha512');
$x = z_fetch_url($hz_server . '/api/z/1.0/file/export?f=&file_id=' . $attach_id,false,$redirects,[ 'headers' => $headers ]);
if(! $x['success']) {
logger('no API response',LOGGER_DEBUG);
return;
}
$j = json_decode($x['body'],true);
$r = sync_files($channel,[$j]);
killme();
}
}

View file

@ -5,6 +5,7 @@ namespace Zotlabs\Module;
use App; use App;
use URLify; use URLify;
use Zotlabs\Web\Controller; use Zotlabs\Web\Controller;
use Zotlabs\Web\HTTPSig;
use Zotlabs\Lib\Libzot; use Zotlabs\Lib\Libzot;
use Zotlabs\Lib\Connect; use Zotlabs\Lib\Connect;
use Zotlabs\Daemon\Run; use Zotlabs\Daemon\Run;
@ -39,7 +40,7 @@ class Import extends Controller {
$data = null; $data = null;
$seize = ((x($_REQUEST,'make_primary')) ? intval($_REQUEST['make_primary']) : 0); $seize = ((x($_REQUEST,'make_primary')) ? intval($_REQUEST['make_primary']) : 0);
$import_posts = ((x($_REQUEST,'import_posts')) ? intval($_REQUEST['import_posts']) : 0); $import_posts = ((x($_REQUEST,'import_posts')) ? intval($_REQUEST['import_posts']) : 0);
$moving = intval($_REQUEST['moving']); $moving = false; // intval($_REQUEST['moving']);
$src = $_FILES['filename']['tmp_name']; $src = $_FILES['filename']['tmp_name'];
$filename = basename($_FILES['filename']['name']); $filename = basename($_FILES['filename']['name']);
$filesize = intval($_FILES['filename']['size']); $filesize = intval($_FILES['filename']['size']);
@ -525,6 +526,97 @@ class Import extends Controller {
if ($import_posts && array_key_exists('item',$data) && $data['item']) { if ($import_posts && array_key_exists('item',$data) && $data['item']) {
import_items($channel,$data['item'],false,$relocate); import_items($channel,$data['item'],false,$relocate);
} }
if ($api_path && $import_posts) { // we are importing from a server and not a file
$m = parse_url($api_path);
$hz_server = $m['scheme'] . '://' . $m['host'];
$since = datetime_convert(date_default_timezone_get(),date_default_timezone_get(),'0001-01-01 00:00');
$until = datetime_convert(date_default_timezone_get(),date_default_timezone_get(),'now + 1 day');
$poll_interval = get_config('system','poll_interval',3);
$page = 0;
while (1) {
$headers = [
'X-API-Token' => random_string(),
'X-API-Request' => $hz_server . '/api/z/1.0/item/export_page?f=&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page ,
'Host' => $m['host'],
'(request-target)' => 'get /api/z/1.0/item/export_page?f=&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page ,
];
$headers = HTTPSig::create_sig($headers,$channel['channel_prvkey'], channel_url($channel),true,'sha512');
$x = z_fetch_url($hz_server . '/api/z/1.0/item/export_page?f=&since=' . urlencode($since) . '&until=' . urlencode($until) . '&page=' . $page,false,$redirects,[ 'headers' => $headers ]);
// logger('z_fetch: ' . print_r($x,true));
if (! $x['success']) {
logger('no API response');
break;
}
$j = json_decode($x['body'],true);
if (! $j) {
break;
}
if (! ($j['item'] || count($j['item']))) {
break;
}
Run::Summon([ 'Content_importer', sprintf('%d',$page), $since, $until, $channel['channel_address'], urlencode($hz_server) ]);
sleep($poll_interval);
$page ++;
continue;
}
$headers = [
'X-API-Token' => random_string(),
'X-API-Request' => $hz_server . '/api/z/1.0/files?f=&since=' . urlencode($since) . '&until=' . urlencode($until),
'Host' => $m['host'],
'(request-target)' => 'get /api/z/1.0/files?f=&since=' . urlencode($since) . '&until=' . urlencode($until),
];
$headers = HTTPSig::create_sig($headers,$channel['channel_prvkey'], channel_url($channel),true,'sha512');
$x = z_fetch_url($hz_server . '/api/z/1.0/files?f=&since=' . urlencode($since) . '&until=' . urlencode($until),false,$redirects,[ 'headers' => $headers ]);
if (! $x['success']) {
logger('no API response');
return;
}
$j = json_decode($x['body'],true);
if (! $j) {
return;
}
if (! $j['success']) {
return;
}
$poll_interval = get_config('system','poll_interval',3);
if(count($j['results'])) {
$todo = count($j['results']);
logger('total to process: ' . $todo,LOGGER_DEBUG);
foreach($j['results'] as $jj) {
Run::Summon([ 'File_importer',$jj['hash'], $channel['channel_address'], urlencode($hz_server) ]);
sleep($poll_interval);
}
}
notice(t('Files and Posts imported.') . EOL);
}
notifications_on($channel['channel_id'],$saved_notification_flags); notifications_on($channel['channel_id'],$saved_notification_flags);

View file

@ -114,6 +114,14 @@ function api_login() {
dbesc($keyId), dbesc($keyId),
dbesc($keyId) dbesc($keyId)
); );
if (! $r) {
HTTPSig::get_zotfinger_key($keyId);
$r = q("select * from hubloc where hubloc_addr = '%s' or hubloc_id_url = '%s'",
dbesc($keyId),
dbesc($keyId)
);
}
if ($r) { if ($r) {
$r = Libzot::zot_record_preferred($r); $r = Libzot::zot_record_preferred($r);
$c = channelx_by_hash($r['hubloc_hash']); $c = channelx_by_hash($r['hubloc_hash']);

View file

@ -23,7 +23,7 @@
<div id="import-common-desc" class="section-content-info-wrapper">{{$common}}</div> <div id="import-common-desc" class="section-content-info-wrapper">{{$common}}</div>
{{include file="field_checkbox.tpl" field=$make_primary}} {{include file="field_checkbox.tpl" field=$make_primary}}
{{include file="field_checkbox.tpl" field=$moving}} {{**include file="field_checkbox.tpl" field=$moving**}}
{{include file="field_input.tpl" field=$newname}} {{include file="field_input.tpl" field=$newname}}
<div id="import-common-desc" class="section-content-info-wrapper">{{$pleasewait}}</div> <div id="import-common-desc" class="section-content-info-wrapper">{{$pleasewait}}</div>