streams/Zotlabs/Module/Import_items.php

145 lines
4.3 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
namespace Zotlabs\Module;
2021-12-02 22:33:36 +00:00
use App;
use Zotlabs\Web\Controller;
2016-04-19 03:38:38 +00:00
require_once('include/import.php');
/**
* @brief Module for importing items.
*
* Import existing posts and content from an export file.
*/
2021-12-02 23:02:31 +00:00
class Import_items extends Controller
{
2016-04-19 03:38:38 +00:00
2021-12-02 23:02:31 +00:00
public function post()
{
2021-12-02 23:02:31 +00:00
if (!local_channel())
return;
2021-12-02 23:02:31 +00:00
check_form_security_token_redirectOnErr('/import_items', 'import_items');
2021-12-02 23:02:31 +00:00
$data = null;
2021-12-02 23:02:31 +00:00
$src = $_FILES['filename']['tmp_name'];
$filename = basename($_FILES['filename']['name']);
$filesize = intval($_FILES['filename']['size']);
$filetype = $_FILES['filename']['type'];
2021-12-02 23:02:31 +00:00
if ($src) {
// This is OS specific and could also fail if your tmpdir isn't very large
// mostly used for Diaspora which exports gzipped files.
2021-12-02 23:02:31 +00:00
if (strpos($filename, '.gz')) {
@rename($src, $src . '.gz');
@system('gunzip ' . escapeshellarg($src . '.gz'));
}
2021-12-02 23:02:31 +00:00
if ($filesize) {
$data = @file_get_contents($src);
}
unlink($src);
}
2021-12-02 23:02:31 +00:00
if (!$src) {
2021-12-02 23:02:31 +00:00
$old_address = ((x($_REQUEST, 'old_address')) ? $_REQUEST['old_address'] : '');
2021-12-02 23:02:31 +00:00
if (!$old_address) {
logger('Nothing to import.');
notice(t('Nothing to import.') . EOL);
return;
}
2021-12-02 23:02:31 +00:00
$email = ((x($_REQUEST, 'email')) ? $_REQUEST['email'] : '');
$password = ((x($_REQUEST, 'password')) ? $_REQUEST['password'] : '');
2021-12-02 23:02:31 +00:00
$year = ((x($_REQUEST, 'year')) ? $_REQUEST['year'] : '');
2021-12-02 23:02:31 +00:00
$channelname = substr($old_address, 0, strpos($old_address, '@'));
$servername = substr($old_address, strpos($old_address, '@') + 1);
2021-12-02 23:02:31 +00:00
$scheme = 'https://';
$api_path = '/api/red/channel/export/items?f=&zap_compat=1&channel=' . $channelname . '&year=' . intval($year);
$binary = false;
$redirects = 0;
$opts = array('http_auth' => $email . ':' . $password);
$url = $scheme . $servername . $api_path;
$ret = z_fetch_url($url, $binary, $redirects, $opts);
if (!$ret['success'])
$ret = z_fetch_url('http://' . $servername . $api_path, $binary, $redirects, $opts);
if ($ret['success'])
$data = $ret['body'];
else
notice(t('Unable to download data from old server') . EOL);
}
2021-12-02 23:02:31 +00:00
if (!$data) {
logger('Empty file.');
notice(t('Imported file is empty.') . EOL);
return;
}
2021-12-02 23:02:31 +00:00
$data = json_decode($data, true);
2021-12-02 23:02:31 +00:00
//logger('import: data: ' . print_r($data,true));
//print_r($data);
2021-12-02 23:02:31 +00:00
if (!is_array($data))
return;
2021-02-19 01:55:51 +00:00
// if(array_key_exists('compatibility',$data) && array_key_exists('database',$data['compatibility'])) {
// $v1 = substr($data['compatibility']['database'],-4);
// $v2 = substr(DB_UPDATE_VERSION,-4);
// if($v2 > $v1) {
// $t = sprintf( t('Warning: Database versions differ by %1$d updates.'), $v2 - $v1 );
// notice($t . EOL);
// }
// }
2021-12-02 23:02:31 +00:00
$codebase = 'zap';
2021-02-19 01:55:51 +00:00
2021-12-02 23:02:31 +00:00
if ((!array_path_exists('compatibility/codebase', $data)) || $data['compatibility']['codebase'] !== $codebase) {
notice(t('Data export format is not compatible with this software'));
return;
}
2021-12-02 23:02:31 +00:00
$channel = App::get_channel();
2021-12-02 23:02:31 +00:00
if (array_key_exists('item', $data) && $data['item']) {
import_items($channel, $data['item'], false, ((array_key_exists('relocate', $data)) ? $data['relocate'] : null));
}
2021-12-02 23:02:31 +00:00
info(t('Import completed') . EOL);
}
2021-12-02 23:02:31 +00:00
/**
* @brief Generate item import page.
*
* @return string with parsed HTML.
*/
public function get()
{
2021-12-02 23:02:31 +00:00
if (!local_channel()) {
notice(t('Permission denied') . EOL);
return login();
}
2021-12-02 23:02:31 +00:00
$o = replace_macros(get_markup_template('item_import.tpl'), array(
'$title' => t('Import Items'),
'$desc' => t('Use this form to import existing posts and content from an export file.'),
'$label_filename' => t('File to Upload'),
'$form_security_token' => get_form_security_token('import_items'),
'$submit' => t('Submit')
));
2021-12-02 23:02:31 +00:00
return $o;
}
2016-04-19 03:38:38 +00:00
}