streams/include/import.php

2108 lines
55 KiB
PHP
Raw Normal View History

<?php
use Zotlabs\Lib\IConfig;
2018-06-01 02:42:13 +00:00
use Zotlabs\Lib\Libzot;
2018-06-20 02:33:50 +00:00
use Zotlabs\Web\HTTPSig;
2019-05-29 23:29:55 +00:00
use Zotlabs\Lib\Apps;
2019-10-13 22:49:50 +00:00
use Zotlabs\Lib\Connect;
2020-03-19 23:56:48 +00:00
use Zotlabs\Lib\LibBlock;
use Zotlabs\Daemon\Run;
2021-03-11 01:53:54 +00:00
use Zotlabs\Access\PermissionRoles;
use Zotlabs\Access\PermissionLimits;
2015-09-09 00:51:48 +00:00
require_once('include/menu.php');
/**
* @brief Import a channel.
*
* @param array $channel
* @param int $account_id
* @param int $seize
* @return boolean|array
*/
function import_channel($channel, $account_id, $seize, $newname = '') {
2019-05-29 23:29:55 +00:00
if (! array_key_exists('channel_system',$channel)) {
$channel['channel_system'] = (($channel['channel_pageflags'] & 0x1000) ? 1 : 0);
$channel['channel_removed'] = (($channel['channel_pageflags'] & 0x8000) ? 1 : 0);
}
2021-02-25 08:35:36 +00:00
if (isset($channel['channel_removed']) && intval($channel['channel_removed'])) {
notice( t('Unable to import a removed channel.') . EOL);
return false;
}
// Ignore the hash provided and re-calculate
2018-07-27 02:37:04 +00:00
$channel['channel_hash'] = Libzot::make_xchan_hash($channel['channel_guid'],$channel['channel_pubkey']);
2019-05-29 23:29:55 +00:00
if ($newname) {
$channel['channel_address'] = $newname;
}
// Check for duplicate channels
$r = q("select * from channel where (channel_guid = '%s' or channel_hash = '%s' or channel_address = '%s' ) limit 1",
dbesc($channel['channel_guid']),
dbesc($channel['channel_hash']),
dbesc($channel['channel_address'])
);
2019-09-03 03:47:34 +00:00
if ($r && $r[0]['channel_guid'] == $channel['channel_guid'] && $r[0]['channel_pubkey'] === $channel['channel_pubkey'] && $r[0]['channel_hash'] === $channel['channel_hash']) {
// do not return a dead or deleted or system channel
if ($r[0]['channel_deleted'] > NULL_DATE
|| intval($r[0]['channel_removed'])
|| intval($r[0]['channel_moved'])
|| intval($r[0]['channel_system'])) {
logger('attempt to import to a channel that was removed. ', print_r($channel,true));
notice( t('A channel with these settings was discovered and is not usable as it was removed or reserved for system use. Import failed.') . EOL);
return false;
}
return $r[0];
2019-09-03 03:47:34 +00:00
}
2019-05-29 23:29:55 +00:00
if (($r) || (check_webbie(array($channel['channel_address'])) !== $channel['channel_address'])) {
if ($r[0]['channel_guid'] === $channel['channel_guid'] || $r[0]['channel_hash'] === $channel['channel_hash']) {
logger('mod_import: duplicate channel. ', print_r($channel,true));
notice( t('Cannot create a duplicate channel identifier on this system. Import failed.') . EOL);
return false;
}
else {
// try at most ten times to generate a unique address.
$x = 0;
$found_unique = false;
do {
$tmp = $channel['channel_address'] . mt_rand(1000,9999);
$r = q("select * from channel where channel_address = '%s' limit 1",
dbesc($tmp)
);
2019-05-29 23:29:55 +00:00
if (! $r) {
$channel['channel_address'] = $tmp;
$found_unique = true;
break;
}
$x ++;
} while ($x < 10);
2019-05-29 23:29:55 +00:00
if (! $found_unique) {
logger('mod_import: duplicate channel. randomisation failed.', print_r($channel,true));
notice( t('Unable to create a unique channel address. Import failed.') . EOL);
return false;
}
}
}
unset($channel['channel_id']);
$channel['channel_account_id'] = $account_id;
$channel['channel_primary'] = (($seize) ? 1 : 0);
2019-05-29 23:29:55 +00:00
if ($channel['channel_pageflags'] & PAGE_ALLOWCODE) {
if (! is_site_admin()) {
$channel['channel_pageflags'] = $channel['channel_pageflags'] ^ PAGE_ALLOWCODE;
2019-05-29 23:29:55 +00:00
}
}
2016-07-18 05:40:39 +00:00
// remove all the permissions related settings, we will import/upgrade them after the channel
// is created.
$disallowed = [
'channel_id', 'channel_r_stream', 'channel_r_profile', 'channel_r_abook',
'channel_r_storage', 'channel_r_pages', 'channel_w_stream', 'channel_w_wall',
'channel_w_comment', 'channel_w_mail', 'channel_w_like', 'channel_w_tagwall',
'channel_w_chat', 'channel_w_storage', 'channel_w_pages', 'channel_a_republish',
'channel_a_delegate', 'perm_limits', 'channel_password', 'channel_salt',
2018-10-31 04:28:51 +00:00
'channel_moved', 'channel_removed', 'channel_deleted', 'channel_system'
2016-07-18 05:40:39 +00:00
];
$clean = [];
2019-05-29 23:29:55 +00:00
foreach ($channel as $k => $v) {
if (in_array($k,$disallowed)) {
2016-07-18 05:40:39 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2016-07-18 05:40:39 +00:00
$clean[$k] = $v;
}
2019-05-29 23:29:55 +00:00
if ($clean) {
channel_store_lowlevel($clean);
2016-07-18 05:40:39 +00:00
}
$r = q("select * from channel where channel_account_id = %d and channel_guid = '%s' limit 1",
intval($account_id),
2017-03-31 04:04:55 +00:00
dbesc($channel['channel_guid'])
);
2019-05-29 23:29:55 +00:00
if (! $r) {
2016-12-30 10:31:53 +00:00
logger('mod_import: channel not found. ' . print_r($channel,true));
notice( t('Cloned channel not found. Import failed.') . EOL);
return false;
}
2016-07-18 05:40:39 +00:00
// extract the permissions from the original imported array and use our new channel_id to set them
// These could be in the old channel permission stule or the new pconfig. We have a function to
// translate and store them no matter which they throw at us.
$channel['channel_id'] = $r[0]['channel_id'];
// reset
$channel = $r[0];
set_default_login_identity($account_id,$channel['channel_id'],false);
logger('import step 1');
$_SESSION['import_step'] = 1;
return $channel;
}
/**
* @brief Import pconfig for channel.
*
* @param array $channel
* @param array $configs
*/
function import_config($channel, $configs) {
2019-05-29 23:29:55 +00:00
if ($channel && $configs) {
foreach ($configs as $config) {
unset($config['id']);
$config['uid'] = $channel['channel_id'];
2019-05-29 23:29:55 +00:00
if ($config['cat'] === 'system' && $config['k'] === 'import_system_apps') {
continue;
}
2021-03-12 04:00:59 +00:00
set_pconfig($channel['channel_id'],$config['cat'],$config['k'],$config['v']);
}
load_pconfig($channel['channel_id']);
$permissions_role = get_pconfig($channel['channel_id'],'system','permissions_role');
if ($permissions_role === 'social_federation') {
// Convert Hubzilla's social_federation role to 'social' with relaxed comment permissions
set_pconfig($channel['channel_id'],'systems','permissions_role','social');
PermissionLimits::Set($channel['channel_id'],'post_comments', PERMS_AUTHED);
}
else {
// If the requested permissions_role doesn't exist on this system,
// convert it to 'social'
$role_permissions = PermissionRoles::role_perms($permissions_role);
if (! $role_permissions) {
set_pconfig($channel['channel_id'],'system','permissions_role','social');
}
}
}
}
function import_xign($channel, $xigns) {
2019-05-29 23:29:55 +00:00
if ($channel && $xigns) {
foreach ($xigns as $xign) {
unset($xign['id']);
$xign['uid'] = $channel['channel_id'];
create_table_from_array('xign', $xign);
}
}
}
function sync_xign($channel, $xigns) {
2019-05-29 23:29:55 +00:00
if ($channel && $xigns) {
foreach ($xigns as $xign) {
unset($xign['id']);
$xign['uid'] = $channel['channel_id'];
2019-05-29 23:29:55 +00:00
if (! $xign['xchan']) {
continue;
2019-05-29 23:29:55 +00:00
}
if ($xign['deleted']) {
q("delete from xign where uid = %d and xchan = '%s' ",
intval($xign['uid']),
dbesc($xign['xchan'])
);
continue;
}
$r = q("select * from xign where uid = %d and xchan = '%s' ",
intval($xign['uid']),
dbesc($xign['xchan'])
);
2019-05-29 23:29:55 +00:00
if (! $r) {
create_table_from_array('xign', $xign);
2019-05-29 23:29:55 +00:00
}
}
}
}
2020-03-19 23:56:48 +00:00
function import_block($channel, $blocks) {
if ($channel && $blocks) {
foreach ($blocks as $block) {
unset($block['block_id']);
$block['block_channel_id'] = $channel['channel_id'];
LibBlock::store($block);
}
}
}
function sync_block($channel, $blocks) {
if ($channel && $blocks) {
foreach ($blocks as $block) {
unset($block['block_id']);
$block['block_channel_id'] = $channel['channel_id'];
if (! $block['block_entity']) {
continue;
}
if ($block['deleted']) {
LibBlock::remove($channel['channel_id'],$block['block_entity']);
continue;
}
LibBlock::store($channel['channel_id'],$block);
}
}
}
/**
* @brief Import profiles.
*
* @param array $channel
* @param array $profiles
*/
function import_profiles($channel, $profiles) {
2019-05-29 23:29:55 +00:00
if ($channel && $profiles) {
foreach ($profiles as $profile) {
unset($profile['id']);
$profile['aid'] = get_account_id();
$profile['uid'] = $channel['channel_id'];
2016-06-12 23:34:27 +00:00
convert_oldfields($profile,'name','fullname');
convert_oldfields($profile,'with','partner');
convert_oldfields($profile,'work','employment');
/**
* @TODO put all the applicable photos into the export.
*/
2016-06-12 23:34:27 +00:00
2019-05-29 23:29:55 +00:00
if ((strpos($profile['thumb'],'/photo/profile/l/') !== false) || intval($profile['is_default'])) {
$profile['photo'] = z_root() . '/photo/profile/l/' . $channel['channel_id'];
$profile['thumb'] = z_root() . '/photo/profile/m/' . $channel['channel_id'];
}
else {
$profile['photo'] = z_root() . '/photo/' . basename($profile['photo']);
$profile['thumb'] = z_root() . '/photo/' . basename($profile['thumb']);
}
profile_store_lowlevel($profile);
}
}
}
/**
* @brief Import hublocs.
*
* @param array $channel
* @param array $hublocs
* @param boolean $seize
* @param boolean $moving (optional) default false
*/
2019-05-29 23:29:55 +00:00
function import_hublocs($channel, $hublocs, $seize, $moving = false) {
2019-05-29 23:29:55 +00:00
if ($channel && $hublocs) {
foreach ($hublocs as $hubloc) {
// verify the hash. We can only do this if we already stored the xchan corresponding to this hubloc
// as we need the public key from there
if ($hubloc['hubloc_network'] === 'zot6') {
$x = q("select xchan_pubkey from xchan where xchan_guid = '%s' and xchan_hash = '%s'",
dbesc($hubloc['hubloc_guid']),
dbesc($hubloc['hubloc_hash'])
);
2019-05-29 23:29:55 +00:00
if (! $x) {
logger('hubloc could not be verified. ' . print_r($hubloc,true));
continue;
}
$hash = Libzot::make_xchan_hash($hubloc['hubloc_guid'],$x[0]['xchan_pubkey']);
if ($hash !== $hubloc['hubloc_hash']) {
logger('forged hubloc: ' . print_r($hubloc,true));
continue;
}
}
2019-05-29 23:29:55 +00:00
if ($moving && $hubloc['hubloc_hash'] === $channel['channel_hash'] && $hubloc['hubloc_url'] !== z_root()) {
$hubloc['hubloc_deleted'] = 1;
}
2017-03-31 04:04:55 +00:00
$arr = [
2018-07-23 05:04:20 +00:00
'id' => $hubloc['hubloc_guid'],
'id_sig' => $hubloc['hubloc_guid_sig'],
'location' => $hubloc['hubloc_url'],
'location_sig' => $hubloc['hubloc_url_sig']
2017-03-31 04:04:55 +00:00
];
2019-05-29 23:29:55 +00:00
if (($hubloc['hubloc_hash'] === $channel['channel_hash']) && intval($hubloc['hubloc_primary']) && ($seize)) {
$hubloc['hubloc_primary'] = 0;
2019-05-29 23:29:55 +00:00
}
2019-05-29 23:29:55 +00:00
if (($x = Libzot::gethub($arr,false)) === false) {
unset($hubloc['hubloc_id']);
hubloc_store_lowlevel($hubloc);
}
else {
q("UPDATE hubloc set hubloc_primary = %d, hubloc_deleted = %d where hubloc_id = %d",
intval($hubloc['hubloc_primary']),
intval($hubloc['hubloc_deleted']),
intval($x['hubloc_id'])
);
}
}
}
}
/**
* @brief Import things.
*
* @param array $channel
* @param array $objs
*/
function import_objs($channel, $objs) {
2019-05-29 23:29:55 +00:00
if ($channel && $objs) {
foreach ($objs as $obj) {
$baseurl = $obj['obj_baseurl'];
unset($obj['obj_id']);
unset($obj['obj_baseurl']);
$obj['obj_channel'] = $channel['channel_id'];
2019-05-29 23:29:55 +00:00
if ($baseurl && (strpos($obj['obj_url'], $baseurl . '/thing/') !== false)) {
$obj['obj_url'] = str_replace($baseurl, z_root(), $obj['obj_url']);
}
2019-05-29 23:29:55 +00:00
if ($obj['obj_imgurl']) {
2020-08-25 04:36:56 +00:00
$x = import_remote_xchan_photo($obj['obj_imgurl'], $channel['channel_hash'], true);
if ($x) {
$obj['obj_imgurl'] = $x[0];
}
}
create_table_from_array('obj', $obj);
}
}
}
/**
* @brief Import things.
*
* @param array $channel
* @param array $objs
*/
function sync_objs($channel, $objs) {
2019-05-29 23:29:55 +00:00
if ($channel && $objs) {
$columns = db_columns('obj');
2019-05-29 23:29:55 +00:00
foreach ($objs as $obj) {
2019-05-29 23:29:55 +00:00
if (array_key_exists('obj_deleted',$obj) && $obj['obj_deleted'] && $obj['obj_obj']) {
q("delete from obj where obj_obj = '%s' and obj_channel = %d",
dbesc($obj['obj_obj']),
intval($channel['channel_id'])
);
continue;
}
$baseurl = $obj['obj_baseurl'];
unset($obj['obj_id']);
unset($obj['obj_baseurl']);
$obj['obj_channel'] = $channel['channel_id'];
2019-05-29 23:29:55 +00:00
if ($baseurl && (strpos($obj['obj_url'], $baseurl . '/thing/') !== false)) {
$obj['obj_url'] = str_replace($baseurl, z_root(), $obj['obj_url']);
}
$exists = false;
$x = q("select * from obj where obj_obj = '%s' and obj_channel = %d limit 1",
dbesc($obj['obj_obj']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($x) {
if ($x[0]['obj_edited'] >= $obj['obj_edited']) {
continue;
2019-05-29 23:29:55 +00:00
}
$exists = true;
}
2019-05-29 23:29:55 +00:00
if ($obj['obj_imgurl']) {
// import_xchan_photo() has a switch to store thing images
2020-08-25 04:36:56 +00:00
$x = import_remote_xchan_photo($obj['obj_imgurl'], $channel['channel_hash'], true);
if ($x) {
$obj['obj_imgurl'] = $x[0];
}
}
$hash = $obj['obj_obj'];
2019-05-29 23:29:55 +00:00
if ($exists) {
2015-09-03 06:09:51 +00:00
unset($obj['obj_obj']);
2019-05-29 23:29:55 +00:00
foreach ($obj as $k => $v) {
if (! in_array($k,$columns)) {
continue;
}
$r = q("UPDATE obj SET " . TQUOT . "%s" . TQUOT . " = '%s' WHERE obj_obj = '%s' AND obj_channel = %d",
dbesc($k),
dbesc($v),
dbesc($hash),
intval($channel['channel_id'])
);
}
}
else {
create_table_from_array('obj', $obj);
}
}
}
}
/**
* @brief Import apps.
*
* @param array $channel
* @param array $apps
*/
function import_apps($channel, $apps) {
2019-05-29 23:29:55 +00:00
if ($channel && $apps) {
foreach ($apps as $app) {
2019-05-29 23:29:55 +00:00
if (array_key_exists('app_system',$app) && intval($app['app_system'])) {
continue;
2019-05-29 23:29:55 +00:00
}
$term = ((array_key_exists('term',$app) && is_array($app['term'])) ? $app['term'] : null);
2016-05-05 00:35:27 +00:00
unset($app['id']);
unset($app['app_channel']);
2016-05-05 00:35:27 +00:00
unset($app['term']);
$app['app_channel'] = $channel['channel_id'];
2019-05-29 23:29:55 +00:00
if ($app['app_photo']) {
// overload import_xchan_photo()
2020-08-25 04:36:56 +00:00
$x = import_remote_xchan_photo($app['app_photo'], $channel['channel_hash'], true);
if ($x) {
$app['app_photo'] = $x[0];
}
}
2016-05-05 00:35:27 +00:00
$hash = $app['app_id'];
create_table_from_array('app', $app);
2016-05-05 00:35:27 +00:00
2019-05-29 23:29:55 +00:00
if ($term) {
2016-05-05 00:35:27 +00:00
$x = q("select * from app where app_id = '%s' and app_channel = %d limit 1",
dbesc($hash),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($x) {
foreach ($term as $t) {
if (array_key_exists('type',$t)) {
2016-06-01 04:45:33 +00:00
$t['ttype'] = $t['type'];
2019-05-29 23:29:55 +00:00
}
2016-06-01 04:45:33 +00:00
store_item_tag($channel['channel_id'],$x[0]['id'],TERM_OBJ_APP,$t['ttype'],escape_tags($t['term']),escape_tags($t['url']));
2016-05-05 00:35:27 +00:00
}
}
}
}
}
2015-09-03 06:09:51 +00:00
}
/**
* @brief Sync apps.
*
* @param array $channel
* @param array $apps
*/
function sync_apps($channel, $apps) {
2015-09-03 06:09:51 +00:00
2019-05-29 23:29:55 +00:00
if ($channel && $apps) {
$columns = db_columns('app');
2019-05-29 23:29:55 +00:00
foreach ($apps as $app) {
2015-09-03 06:09:51 +00:00
2016-05-05 00:35:27 +00:00
$exists = false;
$term = ((array_key_exists('term',$app)) ? $app['term'] : null);
2019-05-29 23:29:55 +00:00
if (array_key_exists('app_system',$app) && intval($app['app_system'])) {
continue;
2019-05-29 23:29:55 +00:00
}
2016-05-05 00:35:27 +00:00
$x = q("select * from app where app_id = '%s' and app_channel = %d limit 1",
dbesc($app['app_id']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($x) {
2016-05-05 00:35:27 +00:00
$exists = $x[0];
}
2019-05-29 23:29:55 +00:00
if (array_key_exists('app_deleted',$app) && $app['app_deleted'] && $app['app_id']) {
q("delete from app where app_id = '%s' and app_channel = %d",
2019-05-29 23:29:55 +00:00
dbesc($app['app_id']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($exists) {
2016-05-05 00:35:27 +00:00
q("delete from term where otype = %d and oid = %d",
intval(TERM_OBJ_APP),
intval($exists['id'])
);
2016-05-05 00:35:27 +00:00
}
continue;
}
2015-09-03 06:09:51 +00:00
unset($app['id']);
unset($app['app_channel']);
2016-05-05 00:35:27 +00:00
unset($app['term']);
2019-05-29 23:29:55 +00:00
if ($exists) {
2016-05-05 00:35:27 +00:00
q("delete from term where otype = %d and oid = %d",
intval(TERM_OBJ_APP),
intval($exists['id'])
);
2016-05-05 00:35:27 +00:00
}
2015-09-03 06:09:51 +00:00
2019-05-29 23:29:55 +00:00
if ((! $app['app_created']) || ($app['app_created'] <= NULL_DATE)) {
2015-09-03 06:09:51 +00:00
$app['app_created'] = datetime_convert();
2019-05-29 23:29:55 +00:00
}
if ((! $app['app_edited']) || ($app['app_edited'] <= NULL_DATE)) {
2015-09-03 06:09:51 +00:00
$app['app_edited'] = datetime_convert();
2019-05-29 23:29:55 +00:00
}
2015-09-03 06:09:51 +00:00
$app['app_channel'] = $channel['channel_id'];
2019-05-29 23:29:55 +00:00
if ($app['app_photo']) {
// use import_xchan_photo()
2020-08-25 04:36:56 +00:00
$x = import_remote_xchan_photo($app['app_photo'],$channel['channel_hash'],true);
if ($x) {
$app['app_photo'] = $x[0];
}
2015-09-03 06:09:51 +00:00
}
2019-05-29 23:29:55 +00:00
if ($exists && $term) {
foreach ($term as $t) {
if (array_key_exists('type',$t)) {
2016-06-01 04:45:33 +00:00
$t['ttype'] = $t['type'];
2019-05-29 23:29:55 +00:00
}
2016-06-01 04:45:33 +00:00
store_item_tag($channel['channel_id'],$exists['id'],TERM_OBJ_APP,$t['ttype'],escape_tags($t['term']),escape_tags($t['url']));
2016-05-05 00:35:27 +00:00
}
}
2015-09-03 06:09:51 +00:00
2019-05-29 23:29:55 +00:00
if ($exists) {
if ($exists['app_edited'] >= $app['app_edited']) {
2015-09-03 06:09:51 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2015-09-03 06:09:51 +00:00
}
$hash = $app['app_id'];
2019-05-29 23:29:55 +00:00
if ($exists) {
2015-09-03 06:09:51 +00:00
unset($app['app_id']);
2019-05-29 23:29:55 +00:00
foreach ($app as $k => $v) {
2019-05-29 23:29:55 +00:00
if (! in_array($k,$columns)) {
continue;
}
$r = q("UPDATE app SET " . TQUOT . "%s" . TQUOT . " = '%s' WHERE app_id = '%s' AND app_channel = %d",
2015-09-03 06:09:51 +00:00
dbesc($k),
dbesc($v),
dbesc($hash),
intval($channel['channel_id'])
);
}
}
else {
create_table_from_array('app',$app);
2019-05-29 23:29:55 +00:00
if ($term) {
$x = q("select * from app where app_id = '%s' and app_channel = %d",
2016-05-05 00:35:27 +00:00
dbesc($hash),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($x) {
foreach ($term as $t) {
if (array_key_exists('type',$t)) {
2016-06-01 04:45:33 +00:00
$t['ttype'] = $t['type'];
2019-05-29 23:29:55 +00:00
}
2016-06-01 04:45:33 +00:00
store_item_tag($channel['channel_id'],$x[0]['id'],TERM_OBJ_APP,$t['ttype'],escape_tags($t['term']),escape_tags($t['url']));
2016-05-05 00:35:27 +00:00
}
}
}
2015-09-03 06:09:51 +00:00
}
}
}
2015-09-04 01:44:40 +00:00
}
/**
* @brief Import system apps.
* System apps from the original server may not exist on this system
* (e.g. apps associated with addons that are not installed here).
* Check the system apps that were provided in the import file to see if they
* exist here and if so, install them locally. Preserve categories that
* might have been added by this channel on the other server.
* Do not use any paths from the original as they will point to a different server.
* @param array $channel
* @param array $apps
*/
function import_sysapps($channel, $apps) {
2019-05-29 23:29:55 +00:00
if ($channel && $apps) {
2019-05-29 23:29:55 +00:00
$sysapps = Apps::get_system_apps(false);
2019-05-29 23:29:55 +00:00
foreach ($apps as $app) {
2019-05-29 23:29:55 +00:00
if (array_key_exists('app_system',$app) && (! intval($app['app_system']))) {
continue;
2019-05-29 23:29:55 +00:00
}
2019-05-29 23:29:55 +00:00
if (array_key_exists('app_deleted',$app) && (intval($app['app_deleted']))) {
continue;
2019-05-29 23:29:55 +00:00
}
$term = ((array_key_exists('term',$app) && is_array($app['term'])) ? $app['term'] : null);
2019-05-29 23:29:55 +00:00
foreach ($sysapps as $sysapp) {
if ($app['app_id'] === hash('whirlpool',$sysapp['app_name'])) {
// install this app on this server
$newapp = $sysapp;
$newapp['uid'] = $channel['channel_id'];
$newapp['guid'] = hash('whirlpool',$newapp['name']);
$installed = q("select id from app where app_id = '%s' and app_channel = %d limit 1",
dbesc($newapp['guid']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($installed) {
break;
}
$newapp['system'] = 1;
2019-05-29 23:29:55 +00:00
if ($term) {
$newapp['categories'] = array_elm_to_str($term,'term');
}
2019-05-29 23:29:55 +00:00
Apps::app_install($channel['channel_id'],$newapp);
}
}
}
}
}
/**
* @brief Sync system apps.
*
* @param array $channel
* @param array $apps
*/
function sync_sysapps($channel, $apps) {
2019-05-29 23:29:55 +00:00
$sysapps = Apps::get_system_apps(false);
2019-05-29 23:29:55 +00:00
if ($channel && $apps) {
$columns = db_columns('app');
2019-05-29 23:29:55 +00:00
foreach ($apps as $app) {
$exists = false;
$term = ((array_key_exists('term',$app)) ? $app['term'] : null);
2019-05-29 23:29:55 +00:00
if (array_key_exists('app_system',$app) && (! intval($app['app_system']))) {
continue;
2019-05-29 23:29:55 +00:00
}
2019-05-29 23:29:55 +00:00
foreach ($sysapps as $sysapp) {
if ($app['app_id'] === hash('whirlpool',$sysapp['app_name'])) {
if (array_key_exists('app_deleted',$app) && $app['app_deleted'] && $app['app_id']) {
q("update app set app_deleted = 1 where app_id = '%s' and app_channel = %d",
dbesc($app['app_id']),
intval($channel['channel_id'])
);
}
else {
// install this app on this server
$newapp = $sysapp;
$newapp['uid'] = $channel['channel_id'];
$newapp['guid'] = hash('whirlpool',$newapp['name']);
$newapp['system'] = 1;
2019-05-29 23:29:55 +00:00
if ($term) {
$newapp['categories'] = array_elm_to_str($term,'term');
}
2019-05-29 23:29:55 +00:00
Apps::app_install($channel['channel_id'],$newapp);
}
}
}
}
}
}
/**
* @brief Import chatrooms.
*
* @param array $channel
* @param array $chatrooms
*/
function import_chatrooms($channel, $chatrooms) {
2015-09-04 01:44:40 +00:00
2019-05-29 23:29:55 +00:00
if ($channel && $chatrooms) {
foreach ($chatrooms as $chatroom) {
2015-09-04 01:44:40 +00:00
2019-05-29 23:29:55 +00:00
if (! $chatroom['cr_name']) {
2015-09-04 01:44:40 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2015-09-04 01:44:40 +00:00
unset($chatroom['cr_id']);
unset($chatroom['cr_aid']);
unset($chatroom['cr_uid']);
$chatroom['cr_aid'] = $channel['channel_account_id'];
$chatroom['cr_uid'] = $channel['channel_id'];
create_table_from_array('chatroom', $chatroom);
2015-09-04 01:44:40 +00:00
}
}
}
/**
* @brief Sync chatrooms.
*
* @param array $channel
* @param array $chatrooms
*/
function sync_chatrooms($channel, $chatrooms) {
2015-09-04 01:44:40 +00:00
2019-05-29 23:29:55 +00:00
if ($channel && $chatrooms) {
$columns = db_columns('chatroom');
2019-05-29 23:29:55 +00:00
foreach ($chatrooms as $chatroom) {
2015-09-04 01:44:40 +00:00
2019-05-29 23:29:55 +00:00
if (! $chatroom['cr_name']) {
2015-09-04 01:44:40 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2015-09-04 01:44:40 +00:00
2019-05-29 23:29:55 +00:00
if (array_key_exists('cr_deleted',$chatroom) && $chatroom['cr_deleted']) {
q("delete from chatroom where cr_name = '%s' and cr_uid = %d",
dbesc($chatroom['cr_name']),
intval($channel['channel_id'])
);
continue;
}
2015-09-04 01:44:40 +00:00
unset($chatroom['cr_id']);
unset($chatroom['cr_aid']);
unset($chatroom['cr_uid']);
2019-05-29 23:29:55 +00:00
if ((! $chatroom['cr_created']) || ($chatroom['cr_created'] <= NULL_DATE)) {
2015-09-04 01:44:40 +00:00
$chatroom['cr_created'] = datetime_convert();
2019-05-29 23:29:55 +00:00
}
if ((! $chatroom['cr_edited']) || ($chatroom['cr_edited'] <= NULL_DATE)) {
2015-09-04 01:44:40 +00:00
$chatroom['cr_edited'] = datetime_convert();
2019-05-29 23:29:55 +00:00
}
2015-09-04 01:44:40 +00:00
$chatroom['cr_aid'] = $channel['channel_account_id'];
$chatroom['cr_uid'] = $channel['channel_id'];
$exists = false;
$x = q("select * from chatroom where cr_name = '%s' and cr_uid = %d limit 1",
dbesc($chatroom['cr_name']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($x) {
if ($x[0]['cr_edited'] >= $chatroom['cr_edited']) {
2015-09-04 01:44:40 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2015-09-04 01:44:40 +00:00
$exists = true;
}
$name = $chatroom['cr_name'];
2019-05-29 23:29:55 +00:00
if ($exists) {
foreach ($chatroom as $k => $v) {
2019-05-29 23:29:55 +00:00
if (! in_array($k,$columns)) {
continue;
}
$r = q("UPDATE chatroom SET " . TQUOT . "%s" . TQUOT . " = '%s' WHERE cr_name = '%s' AND cr_uid = %d",
2015-09-04 01:44:40 +00:00
dbesc($k),
dbesc($v),
dbesc($name),
intval($channel['channel_id'])
);
}
}
else {
create_table_from_array('chatroom', $chatroom);
2015-09-04 01:44:40 +00:00
}
}
}
}
2015-09-08 01:14:30 +00:00
/**
* @brief Import items to channel.
*
* @param array $channel where to import to
* @param array $items
* @param boolean $sync default false
* @param array $relocate default null
*/
function import_items($channel, $items, $sync = false, $relocate = null) {
2015-09-08 01:14:30 +00:00
2019-05-29 23:29:55 +00:00
if ($channel && $items) {
$allow_code = channel_codeallowed($channel['channel_id']);
$deliver = false; // Don't deliver any messages or notifications when importing
2019-05-29 23:29:55 +00:00
foreach ($items as $i) {
2016-04-27 00:38:44 +00:00
$item_result = false;
$item = get_item_elements($i,$allow_code);
2019-05-29 23:29:55 +00:00
if (! $item)
2015-09-08 01:14:30 +00:00
continue;
2019-05-29 23:29:55 +00:00
if ($relocate && $item['mid'] === $item['parent_mid']) {
item_url_replace($channel,$item,$relocate['url'],z_root(),$relocate['channel_address']);
}
2015-09-08 01:14:30 +00:00
$r = q("select id, edited from item where mid = '%s' and uid = %d limit 1",
dbesc($item['mid']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($r) {
// flags may have changed and we are probably relocating the post,
// so force an update even if we have the same timestamp
2019-05-29 23:29:55 +00:00
if ($item['edited'] >= $r[0]['edited']) {
2015-09-08 01:14:30 +00:00
$item['id'] = $r[0]['id'];
$item['uid'] = $channel['channel_id'];
2018-08-28 01:58:47 +00:00
$item_result = item_store_update($item,$allow_code,$deliver,false);
}
2015-09-08 01:14:30 +00:00
}
else {
$item['aid'] = $channel['channel_account_id'];
$item['uid'] = $channel['channel_id'];
2018-08-28 01:58:47 +00:00
$item_result = item_store($item,$allow_code,$deliver,false);
2015-09-08 01:14:30 +00:00
}
// preserve conversations you've been involved in from being expired
$stored = $item_result['item'];
2019-05-29 23:29:55 +00:00
if ((is_array($stored)) && ($stored['id'] != $stored['parent'])
&& ($stored['author_xchan'] === $channel['channel_hash'])) {
retain_item($stored['item']['parent']);
}
2020-02-07 04:25:09 +00:00
fix_attached_permissions($channel['channel_id'],$item['body'],$item['allow_cid'],$item['allow_gid'],$item['deny_cid'],$item['deny_gid']);
2019-05-29 23:29:55 +00:00
if ($sync && $item['item_wall']) {
2017-10-11 01:40:02 +00:00
// deliver singletons if we have any
2019-05-29 23:29:55 +00:00
if ($item_result && $item_result['success']) {
Run::Summon( [ 'Notifier','single_activity',$item_result['item_id'] ]);
2017-10-11 01:40:02 +00:00
}
}
2015-09-08 01:14:30 +00:00
}
}
}
/**
* @brief Sync items to channel.
*
* @see import_items
*
* @param array $channel where to import to
* @param array $items
* @param array $relocate default null
*/
function sync_items($channel, $items, $relocate = null) {
import_items($channel, $items, true, $relocate);
2015-09-08 01:14:30 +00:00
}
/**
* @brief Import events.
*
* @param array $channel
* @param array $events
*/
function import_events($channel, $events) {
2015-09-08 04:01:49 +00:00
2019-05-29 23:29:55 +00:00
if ($channel && $events) {
foreach ($events as $event) {
2015-09-08 04:01:49 +00:00
unset($event['id']);
$event['aid'] = $channel['channel_account_id'];
$event['uid'] = $channel['channel_id'];
2016-06-12 23:34:27 +00:00
convert_oldfields($event,'start','dtstart');
convert_oldfields($event,'finish','dtend');
convert_oldfields($event,'type','etype');
convert_oldfields($event,'ignore','dismissed');
2015-09-08 04:01:49 +00:00
create_table_from_array('event', $event);
2015-09-08 04:01:49 +00:00
}
}
}
/**
* @brief Sync events.
*
* @param array $channel
* @param array $events
*/
function sync_events($channel, $events) {
2015-09-08 04:01:49 +00:00
2019-05-29 23:29:55 +00:00
if ($channel && $events) {
$columns = db_columns('event');
2019-05-29 23:29:55 +00:00
foreach ($events as $event) {
2015-09-08 04:01:49 +00:00
2019-06-12 02:44:55 +00:00
if ((! $event['event_hash']) || (! $event['dtstart'])) {
2015-09-08 04:01:49 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2015-09-08 04:01:49 +00:00
2019-05-29 23:29:55 +00:00
if ($event['event_deleted']) {
$r = q("delete from event where event_hash = '%s' and uid = %d",
2015-09-08 04:01:49 +00:00
dbesc($event['event_hash']),
intval($channel['channel_id'])
);
2019-06-12 03:16:38 +00:00
$r = q("select id from item where resource_type = 'event' and resource_id = '%s' and uid = %d",
dbesc($event['event_hash']),
intval($channel['channel_id'])
);
if ($r) {
drop_item($r[0]['id'],false,(($event['event_xchan'] === $channel['channel_hash']) ? DROPITEM_PHASE1 : DROPITEM_NORMAL));
}
2015-09-08 04:01:49 +00:00
continue;
}
unset($event['id']);
$event['aid'] = $channel['channel_account_id'];
$event['uid'] = $channel['channel_id'];
2016-06-12 23:34:27 +00:00
convert_oldfields($event,'start','dtstart');
convert_oldfields($event,'finish','dtend');
convert_oldfields($event,'type','etype');
convert_oldfields($event,'ignore','dismissed');
2015-09-08 04:01:49 +00:00
$exists = false;
$x = q("select * from event where event_hash = '%s' and uid = %d limit 1",
dbesc($event['event_hash']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($x) {
if ($x[0]['edited'] >= $event['edited']) {
2015-09-08 04:01:49 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2015-09-08 04:01:49 +00:00
$exists = true;
}
2019-05-29 23:29:55 +00:00
if ($exists) {
foreach ($event as $k => $v) {
2019-05-29 23:29:55 +00:00
if (! in_array($k,$columns)) {
continue;
}
$r = q("UPDATE event SET " . TQUOT . "%s" . TQUOT . " = '%s' WHERE event_hash = '%s' AND uid = %d",
2015-09-08 04:01:49 +00:00
dbesc($k),
dbesc($v),
dbesc($event['event_hash']),
intval($channel['channel_id'])
);
}
}
else {
create_table_from_array('event', $event);
2015-09-08 04:01:49 +00:00
}
}
}
}
/**
* @brief Import menus.
*
* @param array $channel
* @param array $menus
*/
function import_menus($channel, $menus) {
2016-04-01 01:53:05 +00:00
2019-05-29 23:29:55 +00:00
if ($channel && $menus) {
foreach ($menus as $menu) {
$m = [];
2015-09-09 00:51:48 +00:00
$m['menu_channel_id'] = $channel['channel_id'];
$m['menu_name'] = $menu['pagetitle'];
$m['menu_desc'] = $menu['desc'];
2019-05-29 23:29:55 +00:00
if ($menu['created']) {
$m['menu_created'] = datetime_convert('UTC','UTC',$menu['created']);
}
if ($menu['edited']) {
$m['menu_edited'] = datetime_convert('UTC','UTC', $menu['edited']);
}
2015-09-09 00:51:48 +00:00
$m['menu_flags'] = 0;
2019-05-29 23:29:55 +00:00
if ($menu['flags']) {
if (in_array('bookmark',$menu['flags'])) {
2015-09-09 00:51:48 +00:00
$m['menu_flags'] |= MENU_BOOKMARK;
2019-05-29 23:29:55 +00:00
}
if (in_array('system',$menu['flags'])) {
2015-09-09 00:51:48 +00:00
$m['menu_flags'] |= MENU_SYSTEM;
2019-05-29 23:29:55 +00:00
}
2015-09-09 00:51:48 +00:00
}
$menu_id = menu_create($m);
2019-05-29 23:29:55 +00:00
if ($menu_id) {
if (is_array($menu['items'])) {
foreach ($menu['items'] as $it) {
$mitem = [];
2015-09-09 00:51:48 +00:00
$mitem['mitem_link'] = str_replace('[channelurl]',z_root() . '/channel/' . $channel['channel_address'],$it['link']);
$mitem['mitem_link'] = str_replace('[pageurl]',z_root() . '/page/' . $channel['channel_address'],$it['link']);
2016-08-02 00:44:21 +00:00
$mitem['mitem_link'] = str_replace('[cloudurl]',z_root() . '/cloud/' . $channel['channel_address'],$it['link']);
2015-09-09 00:51:48 +00:00
$mitem['mitem_link'] = str_replace('[baseurl]',z_root(),$it['link']);
2015-09-09 00:51:48 +00:00
$mitem['mitem_desc'] = escape_tags($it['desc']);
$mitem['mitem_order'] = intval($it['order']);
2019-05-29 23:29:55 +00:00
if (is_array($it['flags'])) {
2015-09-09 00:51:48 +00:00
$mitem['mitem_flags'] = 0;
2019-05-29 23:29:55 +00:00
if (in_array('zid',$it['flags'])) {
2015-09-09 00:51:48 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_ZID;
2019-05-29 23:29:55 +00:00
}
if (in_array('new-window',$it['flags'])) {
2015-09-09 00:51:48 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_NEWWIN;
2019-05-29 23:29:55 +00:00
}
if (in_array('chatroom',$it['flags'])) {
2015-09-09 00:51:48 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_CHATROOM;
2019-05-29 23:29:55 +00:00
}
2015-09-09 00:51:48 +00:00
}
menu_add_item($menu_id,$channel['channel_id'],$mitem);
}
}
2015-09-09 00:51:48 +00:00
}
}
}
}
/**
* @brief Sync menus.
*
* @param array $channel
* @param array $menus
*/
function sync_menus($channel, $menus) {
2015-09-09 00:51:48 +00:00
2019-05-29 23:29:55 +00:00
if ($channel && $menus) {
2019-05-29 23:29:55 +00:00
foreach ($menus as $menu) {
$m = [];
2015-09-09 00:51:48 +00:00
$m['menu_channel_id'] = $channel['channel_id'];
$m['menu_name'] = $menu['pagetitle'];
$m['menu_desc'] = $menu['desc'];
2019-05-29 23:29:55 +00:00
if ($menu['created']) {
$m['menu_created'] = datetime_convert('UTC','UTC',$menu['created']);
}
if ($menu['edited']) {
$m['menu_edited'] = datetime_convert('UTC','UTC',$menu['edited']);
}
2015-09-09 00:51:48 +00:00
$m['menu_flags'] = 0;
2019-05-29 23:29:55 +00:00
if ($menu['flags']) {
if (in_array('bookmark',$menu['flags'])) {
2015-09-09 00:51:48 +00:00
$m['menu_flags'] |= MENU_BOOKMARK;
2019-05-29 23:29:55 +00:00
}
if (in_array('system',$menu['flags'])) {
2015-09-09 00:51:48 +00:00
$m['menu_flags'] |= MENU_SYSTEM;
2019-05-29 23:29:55 +00:00
}
2015-09-09 00:51:48 +00:00
}
$editing = false;
2015-09-09 01:40:56 +00:00
2015-09-09 00:51:48 +00:00
$r = q("select * from menu where menu_name = '%s' and menu_channel_id = %d limit 1",
dbesc($m['menu_name']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($r) {
if ($r[0]['menu_edited'] >= $m['menu_edited']) {
2015-09-09 00:51:48 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
if ($menu['menu_deleted']) {
2015-09-09 00:51:48 +00:00
menu_delete_id($r[0]['menu_id'],$channel['channel_id']);
continue;
}
$menu_id = $r[0]['menu_id'];
2015-09-09 01:40:56 +00:00
$m['menu_id'] = $r[0]['menu_id'];
2015-09-09 00:51:48 +00:00
$x = menu_edit($m);
2019-05-29 23:29:55 +00:00
if (! $x) {
2015-09-09 00:51:48 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2015-09-09 00:51:48 +00:00
$editing = true;
}
2019-05-29 23:29:55 +00:00
if (! $editing) {
2015-09-09 00:51:48 +00:00
$menu_id = menu_create($m);
}
2019-05-29 23:29:55 +00:00
if ($menu_id) {
if ($editing) {
2015-09-09 00:51:48 +00:00
// don't try syncing - just delete all the entries and start over
q("delete from menu_item where mitem_menu_id = %d",
intval($menu_id)
);
}
2019-05-29 23:29:55 +00:00
if (is_array($menu['items'])) {
foreach ($menu['items'] as $it) {
$mitem = [];
2015-09-09 00:51:48 +00:00
$mitem['mitem_link'] = str_replace('[channelurl]',z_root() . '/channel/' . $channel['channel_address'],$it['link']);
$mitem['mitem_link'] = str_replace('[pageurl]',z_root() . '/page/' . $channel['channel_address'],$it['link']);
2016-08-02 00:44:21 +00:00
$mitem['mitem_link'] = str_replace('[cloudurl]',z_root() . '/cloud/' . $channel['channel_address'],$it['link']);
2015-09-09 00:51:48 +00:00
$mitem['mitem_link'] = str_replace('[baseurl]',z_root(),$it['link']);
2015-09-09 00:51:48 +00:00
$mitem['mitem_desc'] = escape_tags($it['desc']);
$mitem['mitem_order'] = intval($it['order']);
2019-05-29 23:29:55 +00:00
if (is_array($it['flags'])) {
2015-09-09 00:51:48 +00:00
$mitem['mitem_flags'] = 0;
2019-05-29 23:29:55 +00:00
if (in_array('zid',$it['flags'])) {
2015-09-09 00:51:48 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_ZID;
2019-05-29 23:29:55 +00:00
}
if (in_array('new-window',$it['flags'])) {
2015-09-09 00:51:48 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_NEWWIN;
2019-05-29 23:29:55 +00:00
}
if (in_array('chatroom',$it['flags'])) {
2015-09-09 00:51:48 +00:00
$mitem['mitem_flags'] |= MENU_ITEM_CHATROOM;
2019-05-29 23:29:55 +00:00
}
2015-09-09 00:51:48 +00:00
}
menu_add_item($menu_id,$channel['channel_id'],$mitem);
}
}
2015-09-09 00:51:48 +00:00
}
}
}
}
2015-09-08 01:14:30 +00:00
/**
* @brief Import likes.
*
* @param array $channel
* @param array $likes
*/
function import_likes($channel, $likes) {
2019-05-29 23:29:55 +00:00
if ($channel && $likes) {
foreach ($likes as $like) {
if ($like['deleted']) {
2015-09-11 02:18:12 +00:00
q("delete from likes where liker = '%s' and likee = '%s' and verb = '%s' and target_type = '%s' and target_id = '%s'",
dbesc($like['liker']),
dbesc($like['likee']),
dbesc($like['verb']),
dbesc($like['target_type']),
dbesc($like['target_id'])
);
continue;
}
2015-09-11 02:18:12 +00:00
unset($like['id']);
unset($like['iid']);
$like['channel_id'] = $channel['channel_id'];
$r = q("select * from likes where liker = '%s' and likee = '%s' and verb = '%s' and target_type = '%s' and target_id = '%s' and i_mid = '%s'",
dbesc($like['liker']),
dbesc($like['likee']),
dbesc($like['verb']),
dbesc($like['target_type']),
dbesc($like['target_id']),
dbesc($like['i_mid'])
);
2019-05-29 23:29:55 +00:00
if ($r) {
2015-09-11 02:18:12 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
create_table_from_array('likes', $like);
2015-09-11 02:18:12 +00:00
}
}
2015-09-11 02:18:12 +00:00
}
2015-09-08 01:14:30 +00:00
function import_conv($channel,$convs) {
2019-05-29 23:29:55 +00:00
if ($channel && $convs) {
foreach ($convs as $conv) {
if ($conv['deleted']) {
2016-10-22 04:44:15 +00:00
q("delete from conv where guid = '%s' and uid = %d",
dbesc($conv['guid']),
intval($channel['channel_id'])
);
continue;
}
unset($conv['id']);
$conv['uid'] = $channel['channel_id'];
$conv['subject'] = str_rot47(base64url_encode($conv['subject']));
$r = q("select id from conv where guid = '%s' and uid = %d limit 1",
dbesc($conv['guid']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($r) {
continue;
2019-05-29 23:29:55 +00:00
}
create_table_from_array('conv',$conv);
}
}
}
/**
* @brief Import mails.
*
* @param array $channel
* @param array $mails
* @param boolean $sync (optional) default false
*/
function import_mail($channel, $mails, $sync = false) {
2019-05-29 23:29:55 +00:00
if ($channel && $mails) {
foreach ($mails as $mail) {
if (array_key_exists('flags',$mail) && in_array('deleted',$mail['flags'])) {
q("delete from mail where mid = '%s' and uid = %d",
dbesc($mail['message_id']),
intval($channel['channel_id'])
);
continue;
}
2019-05-29 23:29:55 +00:00
if (array_key_exists('flags',$mail) && in_array('recalled',$mail['flags'])) {
q("update mail set mail_recalled = 1 where mid = '%s' and uid = %d",
dbesc($mail['message_id']),
intval($channel['channel_id'])
);
continue;
}
$m = get_mail_elements($mail);
2019-05-29 23:29:55 +00:00
if (! $m) {
continue;
2019-05-29 23:29:55 +00:00
}
$m['account_id'] = $channel['channel_account_id'];
$m['channel_id'] = $channel['channel_id'];
2016-04-27 00:38:44 +00:00
$mail_id = mail_store($m);
2019-05-29 23:29:55 +00:00
if ($sync && $mail_id) {
2019-03-01 00:48:34 +00:00
// Not applicable to Zap which does not federate with singletons
// Run::Summon(array('Notifier','single_mail',$mail_id));
2017-10-11 01:40:02 +00:00
}
}
}
}
/**
* @brief Synchronise mails.
*
* @see import_mail
* @param array $channel
* @param array $mails
*/
function sync_mail($channel, $mails) {
import_mail($channel, $mails, true);
2016-04-27 00:38:44 +00:00
}
2015-09-08 01:14:30 +00:00
/**
* @brief Synchronise files.
*
* @param array $channel
* @param array $files
*/
function sync_files($channel, $files) {
2015-09-08 01:14:30 +00:00
2016-04-07 01:07:29 +00:00
require_once('include/attach.php');
2019-05-29 23:29:55 +00:00
if ($channel && $files) {
2019-07-30 01:32:57 +00:00
$limit = engr_units_to_bytes(service_class_fetch($channel['channel_id'], 'attach_upload_limit'));
2019-05-29 23:29:55 +00:00
foreach ($files as $f) {
if (! $f) {
2016-04-07 01:07:29 +00:00
continue;
2019-05-29 23:29:55 +00:00
}
2016-04-07 01:07:29 +00:00
$fetch_url = $f['fetch_url'];
$oldbase = dirname($fetch_url);
$original_channel = $f['original_channel'];
2019-05-29 23:29:55 +00:00
if (! ($fetch_url && $original_channel)) {
continue;
2019-05-29 23:29:55 +00:00
}
2016-04-07 01:07:29 +00:00
2019-06-12 00:47:21 +00:00
$has_undeleted_attachments = false;
2019-05-29 23:29:55 +00:00
if ($f['attach']) {
foreach ($f['attach'] as $att) {
$attachment_stored = false;
2016-06-12 23:34:27 +00:00
convert_oldfields($att,'data','content');
2019-06-12 00:37:27 +00:00
if (intval($att['deleted'])) {
logger('deleting attachment');
2019-06-12 00:47:21 +00:00
attach_delete($channel['channel_id'],$att['hash']);
continue;
}
2019-06-12 00:47:21 +00:00
$has_undeleted_attachments = true;
$attach_exists = false;
2016-09-29 23:20:26 +00:00
$x = attach_by_hash($att['hash'],$channel['channel_hash']);
logger('sync_files duplicate check: attach_exists=' . $attach_exists, LOGGER_DEBUG);
logger('sync_files duplicate check: att=' . print_r($att,true), LOGGER_DEBUG);
logger('sync_files duplicate check: attach_by_hash() returned ' . print_r($x,true), LOGGER_DEBUG);
2019-05-29 23:29:55 +00:00
if ($x['success']) {
2019-08-26 00:35:14 +00:00
$orig_attach = $x['data'];
$attach_exists = true;
2019-08-26 00:35:14 +00:00
$attach_id = $orig_attach['id'];
}
2016-04-07 01:07:29 +00:00
$newfname = 'store/' . $channel['channel_address'] . '/' . get_attach_binname($att['content']);
2016-04-07 01:07:29 +00:00
2016-04-07 01:46:29 +00:00
unset($att['id']);
$att['aid'] = $channel['channel_account_id'];
$att['uid'] = $channel['channel_id'];
// check for duplicate folder names with the same parent.
// If we have a duplicate that doesn't match this hash value
// change the name so that the contents won't be "covered over"
// by the existing directory. Use the same logic we use for
// duplicate files.
2019-05-29 23:29:55 +00:00
if (strpos($att['filename'],'.') !== false) {
$basename = substr($att['filename'],0,strrpos($att['filename'],'.'));
$ext = substr($att['filename'],strrpos($att['filename'],'.'));
}
else {
$basename = $att['filename'];
$ext = '';
}
$r = q("select filename from attach where ( filename = '%s' OR filename like '%s' ) and folder = '%s' and hash != '%s' and uid = %d ",
dbesc($basename . $ext),
dbesc($basename . '(%)' . $ext),
dbesc($att['folder']),
dbesc($att['hash']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($r) {
$x = 1;
do {
$found = false;
2019-05-29 23:29:55 +00:00
foreach ($r as $rr) {
if ($rr['filename'] === $basename . '(' . $x . ')' . $ext) {
$found = true;
break;
}
}
2019-05-29 23:29:55 +00:00
if ($found) {
$x++;
2019-05-29 23:29:55 +00:00
}
}
2019-05-29 23:29:55 +00:00
while ($found);
$att['filename'] = $basename . '(' . $x . ')' . $ext;
}
2019-05-29 23:29:55 +00:00
else {
$att['filename'] = $basename . $ext;
2019-05-29 23:29:55 +00:00
}
// end duplicate detection
/// @FIXME update attachment structures if they are modified rather than created
$att['content'] = $newfname;
// Note: we use $att['hash'] below after it has been escaped to
// fetch the file contents.
// If the hash ever contains any escapable chars this could cause
// problems. Currently it does not.
2019-05-29 23:29:55 +00:00
if (!isset($att['os_path'])) {
2016-09-29 01:37:05 +00:00
$att['os_path'] = '';
2019-05-29 23:29:55 +00:00
}
2019-05-29 23:29:55 +00:00
if ($attach_exists) {
logger('sync_files attach exists: ' . print_r($att,true), LOGGER_DEBUG);
// process/sync a remote rename/move operation
2019-05-29 23:29:55 +00:00
if ($orig_attach && $orig_attach['content'] && $orig_attach['content'] !== $newfname) {
logger('rename: ' . $orig_attach['content'] . ' -> ' . $newfname);
rename($orig_attach['content'],$newfname);
}
2019-05-29 23:29:55 +00:00
if (! dbesc_array($att)) {
2016-10-13 07:30:41 +00:00
continue;
}
2021-02-19 05:40:22 +00:00
$columns = db_columns('attach');
$str = '';
2019-05-29 23:29:55 +00:00
foreach ($att as $k => $v) {
2021-02-19 05:40:22 +00:00
if (! in_array($k,$columns)) {
continue;
}
2019-05-29 23:29:55 +00:00
if ($str) {
$str .= ",";
2019-05-29 23:29:55 +00:00
}
$str .= " " . TQUOT . $k . TQUOT . " = '" . $v . "' ";
}
$r = dbq("update attach set " . $str . " where id = " . intval($attach_id) );
}
else {
logger('sync_files attach does not exists: ' . print_r($att,true), LOGGER_DEBUG);
2019-05-29 23:29:55 +00:00
if ($limit !== false) {
$r = q("select sum(filesize) as total from attach where aid = %d ",
intval($channel['channel_account_id'])
);
2019-05-29 23:29:55 +00:00
if (($r) && (($r[0]['total'] + $att['filesize']) > $limit)) {
logger('service class limit exceeded');
continue;
}
}
create_table_from_array('attach',$att);
}
2016-04-07 01:07:29 +00:00
// is this a directory?
2019-05-29 23:29:55 +00:00
if ($att['filetype'] === 'multipart/mixed' && $att['is_dir']) {
os_mkdir($newfname, STORAGE_DEFAULT_PERMISSIONS,true);
$attachment_stored = true;
2016-04-07 01:07:29 +00:00
continue;
}
else {
// it's a file
// for the sync version of this algorithm (as opposed to 'offline import')
// we will fetch the actual file from the source server so it can be
// streamed directly to disk and avoid consuming PHP memory if it's a huge
// audio/video file or something.
2016-04-07 01:07:29 +00:00
$time = datetime_convert();
$parr = [
'hash' => $channel['channel_hash'],
'time' => $time,
'resource' => $att['hash'],
'revision' => 0,
'signature' => Libzot::sign($channel['channel_hash'] . '.' . $time, $channel['channel_prvkey'])
];
2016-04-07 01:07:29 +00:00
$store_path = $newfname;
$fp = fopen($newfname,'w');
2019-05-29 23:29:55 +00:00
if (! $fp) {
logger('failed to open storage file.',LOGGER_NORMAL,LOG_ERR);
2016-04-07 01:07:29 +00:00
continue;
}
2016-04-07 01:07:29 +00:00
$redirects = 0;
2017-11-29 21:51:54 +00:00
2019-02-26 03:45:50 +00:00
$m = parse_url($fetch_url);
$headers = [
'Accept' => 'application/x-zot+json',
'Sigtoken' => random_string(),
'Host' => $m['host'],
2019-06-12 00:27:31 +00:00
'(request-target)' => 'post ' . $m['path'] . '/' . $att['hash']
2019-02-26 03:45:50 +00:00
];
2017-11-29 21:51:54 +00:00
2018-06-20 02:33:50 +00:00
$headers = HTTPSig::create_sig($headers,$channel['channel_prvkey'], channel_url($channel),true,'sha512');
2019-02-26 03:45:50 +00:00
$x = z_post_url($fetch_url . '/' . $att['hash'],$parr,$redirects,[ 'filep' => $fp, 'headers' => $headers]);
2016-04-07 01:07:29 +00:00
fclose($fp);
2019-05-29 23:29:55 +00:00
if ($x['success']) {
2016-04-07 01:07:29 +00:00
$attachment_stored = true;
}
continue;
}
}
}
2019-06-12 00:47:21 +00:00
if (($has_undeleted_attachments) && (! $attachment_stored)) {
/// @TODO should we queue this and retry or delete everything or what?
logger('attachment store failed',LOGGER_NORMAL,LOG_ERR);
2016-04-07 01:07:29 +00:00
}
2019-05-29 23:29:55 +00:00
if ($f['photo']) {
foreach ($f['photo'] as $p) {
2016-04-07 01:07:29 +00:00
unset($p['id']);
$p['aid'] = $channel['channel_account_id'];
$p['uid'] = $channel['channel_id'];
2016-06-12 23:34:27 +00:00
convert_oldfields($p,'data','content');
convert_oldfields($p,'scale','imgscale');
convert_oldfields($p,'size','filesize');
convert_oldfields($p,'type','mimetype');
// if this is a profile photo, undo the profile photo bit
// for any other photo which previously held it.
2019-05-29 23:29:55 +00:00
if ($p['photo_usage'] == PHOTO_PROFILE) {
$e = q("update photo set photo_usage = %d where photo_usage = %d
and resource_id != '%s' and uid = %d ",
intval(PHOTO_NORMAL),
intval(PHOTO_PROFILE),
dbesc($p['resource_id']),
intval($channel['channel_id'])
);
}
// same for cover photos
2019-05-29 23:29:55 +00:00
if ($p['photo_usage'] == PHOTO_COVER) {
$e = q("update photo set photo_usage = %d where photo_usage = %d
and resource_id != '%s' and uid = %d ",
intval(PHOTO_NORMAL),
intval(PHOTO_COVER),
dbesc($p['resource_id']),
intval($channel['channel_id'])
);
}
if (intval($p['os_storage'])) {
$p['content'] = $store_path . ((intval($p['imgscale'])) ? '-' . $p['imgscale'] : EMPTY_STR);
2019-05-29 23:29:55 +00:00
}
else {
$p['content'] = (($p['content']) ? base64_decode($p['content']) : '');
2019-05-29 23:29:55 +00:00
}
2019-06-11 23:44:26 +00:00
if (intval($p['imgscale']) && ((intval($p['os_storage'])) || (! $p['content']))) {
2017-12-10 23:25:44 +00:00
$time = datetime_convert();
$parr = [
'hash' => $channel['channel_hash'],
'time' => $time,
'resource' => $att['hash'],
'revision' => 0,
'signature' => Libzot::sign($channel['channel_hash'] . '.' . $time, $channel['channel_prvkey']),
2017-12-10 23:25:44 +00:00
'resolution' => $p['imgscale']
];
2017-12-10 23:25:44 +00:00
2019-06-12 00:27:31 +00:00
2017-12-10 23:25:44 +00:00
$stored_image = $newfname . '-' . intval($p['imgscale']);
2019-06-12 00:27:31 +00:00
logger('fetching_photo: ' . $stored_image);
2017-12-10 23:25:44 +00:00
$fp = fopen($stored_image,'w');
2019-05-29 23:29:55 +00:00
if (! $fp) {
2017-12-10 23:25:44 +00:00
logger('failed to open storage file.',LOGGER_NORMAL,LOG_ERR);
continue;
}
$redirects = 0;
2019-02-26 03:45:50 +00:00
$m = parse_url($fetch_url);
$headers = [
'Accept' => 'application/x-zot+json',
'Sigtoken' => random_string(),
'Host' => $m['host'],
2019-06-12 00:27:31 +00:00
'(request-target)' => 'post ' . $m['path'] . '/' . $att['hash']
2019-02-26 03:45:50 +00:00
];
2018-06-20 02:33:50 +00:00
$headers = HTTPSig::create_sig($headers,$channel['channel_prvkey'], channel_url($channel),true,'sha512');
2017-12-10 23:25:44 +00:00
2019-02-26 03:45:50 +00:00
$x = z_post_url($fetch_url . '/' . $att['hash'],$parr,$redirects,[ 'filep' => $fp, 'headers' => $headers]);
2017-12-10 23:25:44 +00:00
fclose($fp);
2019-02-26 03:45:50 +00:00
2019-06-12 00:27:31 +00:00
if (! intval($p['os_storage'])) {
$p['content'] = file_get_contents($stored_image);
unlink($stored_image);
}
2017-12-10 23:25:44 +00:00
}
2016-04-07 01:07:29 +00:00
2019-05-29 23:29:55 +00:00
if (!isset($p['display_path'])) {
2016-09-23 00:08:16 +00:00
$p['display_path'] = '';
2019-05-29 23:29:55 +00:00
}
2016-09-23 00:08:16 +00:00
$exists = q("select * from photo where resource_id = '%s' and imgscale = %d and uid = %d limit 1",
dbesc($p['resource_id']),
intval($p['imgscale']),
intval($channel['channel_id'])
);
2019-05-29 23:29:55 +00:00
if ($exists) {
$str = '';
2019-05-29 23:29:55 +00:00
foreach ($p as $k => $v) {
$matches = false;
2019-05-29 23:29:55 +00:00
if (preg_match('/([^a-zA-Z0-9\-\_\.])/',$k,$matches)) {
continue;
}
2019-05-29 23:29:55 +00:00
if ($str) {
$str .= ",";
2019-05-29 23:29:55 +00:00
}
$str .= " " . TQUOT . $k . TQUOT . " = '" . (($k === 'content') ? dbescbin($v) : dbesc($v)) . "' ";
}
$r = dbq("update photo set " . $str . " where id = " . intval($exists[0]['id']) );
}
else {
create_table_from_array('photo',$p, [ 'content' ] );
}
2016-04-07 01:07:29 +00:00
}
}
Run::Summon([ 'Thumbnail' , $att['hash'] ]);
2019-05-29 23:29:55 +00:00
if ($f['item']) {
sync_items($channel,$f['item'],
['channel_address' => $original_channel,'url' => $oldbase]
);
2016-04-07 01:07:29 +00:00
}
}
}
}
2015-09-08 01:14:30 +00:00
/**
* @brief Rename a key in an array.
*
* Replaces $old key with $new key in $arr.
*
* @param[in,out] array &$arr The array where to work on
* @param string $old The old key in the array
* @param string $new The new key in the array
*/
function convert_oldfields(&$arr, $old, $new) {
2019-05-29 23:29:55 +00:00
if (array_key_exists($old, $arr)) {
2016-06-12 23:34:27 +00:00
$arr[$new] = $arr[$old];
unset($arr[$old]);
}
}
function scan_webpage_elements($path, $type, $cloud = false) {
2019-05-29 23:29:55 +00:00
$channel = App::get_channel();
2017-02-01 01:32:52 +00:00
$dirtoscan = $path;
switch ($type) {
case 'page':
$dirtoscan .= '/pages/';
$json_filename = 'page.json';
break;
case 'layout':
$dirtoscan .= '/layouts/';
$json_filename = 'layout.json';
break;
case 'block':
$dirtoscan .= '/blocks/';
$json_filename = 'block.json';
break;
default :
2019-05-29 23:29:55 +00:00
return [];
2017-02-01 01:32:52 +00:00
}
if($cloud) {
$dirtoscan = get_dirpath_by_cloudpath($channel, $dirtoscan);
}
$elements = [];
2019-05-29 23:29:55 +00:00
if (is_dir($dirtoscan)) {
2017-02-01 01:32:52 +00:00
$dirlist = scandir($dirtoscan);
2019-05-29 23:29:55 +00:00
if ($dirlist) {
foreach ($dirlist as $element) {
if ($element === '.' || $element === '..') {
2017-02-01 01:32:52 +00:00
continue;
}
$folder = $dirtoscan . '/' . $element;
2019-05-29 23:29:55 +00:00
if (is_dir($folder)) {
if ($cloud) {
2017-02-01 01:32:52 +00:00
$jsonfilepath = $folder . '/' . get_filename_by_cloudname($json_filename, $channel, $folder);
}
2017-02-01 01:32:52 +00:00
else {
$jsonfilepath = $folder . '/' . $json_filename;
}
2019-05-29 23:29:55 +00:00
if (is_file($jsonfilepath)) {
2017-02-01 01:32:52 +00:00
$metadata = json_decode(file_get_contents($jsonfilepath), true);
2019-05-29 23:29:55 +00:00
if ($cloud) {
2017-02-01 01:32:52 +00:00
$contentfilename = get_filename_by_cloudname($metadata['contentfile'], $channel, $folder);
$metadata['path'] = $folder . '/' . $contentfilename;
}
else {
$contentfilename = $metadata['contentfile'];
$metadata['path'] = $folder . '/' . $contentfilename;
}
2019-05-29 23:29:55 +00:00
if ($metadata['contentfile'] === '') {
2017-02-01 01:32:52 +00:00
logger('Invalid ' . $type . ' content file');
return false;
}
2017-02-01 01:32:52 +00:00
$content = file_get_contents($folder . '/' . $contentfilename);
2019-05-29 23:29:55 +00:00
if (!$content) {
if (is_readable($folder . '/' . $contentfilename)) {
2017-02-01 01:32:52 +00:00
$content = '';
}
2017-02-01 01:32:52 +00:00
else {
logger('Failed to get file content for ' . $metadata['contentfile']);
return false;
}
}
2017-02-01 01:32:52 +00:00
$elements[] = $metadata;
}
}
}
}
}
2017-02-01 01:32:52 +00:00
return $elements;
}
2017-02-01 01:32:52 +00:00
function import_webpage_element($element, $channel, $type) {
2019-05-29 23:29:55 +00:00
$arr = []; // construct information for the webpage element item table record
2019-05-29 23:29:55 +00:00
switch ($type) {
2017-02-01 01:32:52 +00:00
//
// PAGES
//
case 'page':
$arr['item_type'] = ITEM_TYPE_WEBPAGE;
$namespace = 'WEBPAGE';
$name = $element['pagelink'];
2019-05-29 23:29:55 +00:00
if ($name) {
2017-02-01 01:32:52 +00:00
require_once('library/urlify/URLify.php');
$name = strtolower(\URLify::transliterate($name));
}
2017-02-01 01:32:52 +00:00
$arr['title'] = $element['title'];
$arr['term'] = $element['term'];
$arr['layout_mid'] = ''; // by default there is no layout associated with the page
// If a layout was specified, find it in the database and get its info. If
// it does not exist, leave layout_mid empty
2019-05-29 23:29:55 +00:00
if ($element['layout'] !== '') {
$liid = q("select iid from iconfig where k = 'PDL' and v = '%s' and cat = 'system'",
2017-02-01 01:32:52 +00:00
dbesc($element['layout'])
);
2019-05-29 23:29:55 +00:00
if ($liid) {
$linfo = q("select mid from item where id = %d",
2017-02-01 01:32:52 +00:00
intval($liid[0]['iid'])
);
$arr['layout_mid'] = $linfo[0]['mid'];
}
}
2017-02-01 01:32:52 +00:00
break;
//
// LAYOUTS
//
case 'layout':
$arr['item_type'] = ITEM_TYPE_PDL;
$namespace = 'PDL';
$name = $element['name'];
$arr['title'] = $element['description'];
$arr['term'] = $element['term'];
break;
//
// BLOCKS
//
case 'block':
$arr['item_type'] = ITEM_TYPE_BLOCK;
$namespace = 'BUILDBLOCK';
$name = $element['name'];
$arr['title'] = $element['title'];
2017-02-01 01:32:52 +00:00
break;
default :
return null; // return null if invalid element type
}
$arr['uid'] = local_channel();
2017-02-01 01:32:52 +00:00
$arr['aid'] = $channel['channel_account_id'];
2017-02-01 01:32:52 +00:00
// Check if an item already exists based on the name
$iid = q("select iid from iconfig where k = '" . $namespace . "' and v = '%s' and cat = 'system'",
dbesc($name)
);
2019-05-29 23:29:55 +00:00
if ($iid) { // If the item does exist, get the item metadata
2017-02-01 01:32:52 +00:00
$iteminfo = q("select mid,created,edited from item where id = %d",
intval($iid[0]['iid'])
);
2017-02-01 01:32:52 +00:00
$arr['mid'] = $arr['parent_mid'] = $iteminfo[0]['mid'];
$arr['created'] = $iteminfo[0]['created'];
}
else { // otherwise, generate the creation times and unique id
$arr['created'] = datetime_convert('UTC', 'UTC');
2019-02-19 22:59:22 +00:00
$arr['uuid'] = new_uuid();
$arr['mid'] = $arr['parent_mid'] = z_root() . '/item/' . $arr['uuid'];
2017-02-01 01:32:52 +00:00
}
// Update the edited time whether or not the element already exists
$arr['edited'] = datetime_convert('UTC', 'UTC');
// Import the actual element content
$arr['body'] = file_get_contents($element['path']);
// The element owner is the channel importing the elements
$arr['owner_xchan'] = get_observer_hash();
// The author is either the owner or whomever was specified
$arr['author_xchan'] = (($element['author_xchan']) ? $element['author_xchan'] : get_observer_hash());
// Import mimetype if it is a valid mimetype for the element
$mimetypes = [
2017-03-31 04:04:55 +00:00
'text/bbcode',
2017-02-01 01:32:52 +00:00
'text/html',
'text/markdown',
'text/plain',
'application/x-pdl',
'application/x-php'
2017-02-01 01:32:52 +00:00
];
// Blocks and pages can have any of the valid mimetypes, but layouts must be text/bbcode
2019-05-29 23:29:55 +00:00
if ((in_array($element['mimetype'], $mimetypes)) && in_array($type, [ 'page', 'block' ]) ) {
2017-02-01 01:32:52 +00:00
$arr['mimetype'] = $element['mimetype'];
}
else {
$arr['mimetype'] = 'text/bbcode';
}
2017-02-01 01:32:52 +00:00
// Verify ability to use html or php!!!
$execflag = channel_codeallowed(local_channel());
$i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1",
dbesc($arr['mid']),
2017-02-01 01:32:52 +00:00
intval(local_channel())
);
IConfig::Set($arr,'system',$namespace,(($name) ? $name : substr($arr['mid'],0,16)),true);
2019-05-29 23:29:55 +00:00
if ($i) {
2017-02-01 01:32:52 +00:00
$arr['id'] = $i[0]['id'];
// don't update if it has the same timestamp as the original
2019-05-29 23:29:55 +00:00
if ($arr['edited'] > $i[0]['edited']) {
$x = item_store_update($arr,$execflag);
2019-05-29 23:29:55 +00:00
}
2017-02-01 01:32:52 +00:00
}
else {
2019-05-29 23:29:55 +00:00
if (($i) && (intval($i[0]['item_deleted']))) {
2017-02-01 01:32:52 +00:00
// was partially deleted already, finish it off
q("delete from item where mid = '%s' and uid = %d",
dbesc($arr['mid']),
2017-02-01 01:32:52 +00:00
intval(local_channel())
);
}
2019-05-29 23:29:55 +00:00
else {
$x = item_store($arr,$execflag);
2019-05-29 23:29:55 +00:00
}
2017-02-01 01:32:52 +00:00
}
2019-05-29 23:29:55 +00:00
if ($x && $x['success']) {
2017-02-01 01:32:52 +00:00
$element['import_success'] = 1;
}
else {
$element['import_success'] = 0;
}
return $element;
}
function get_webpage_elements($channel, $type = 'all') {
2019-05-29 23:29:55 +00:00
$elements = [];
if (!$channel['channel_id']) {
return null;
2017-02-01 01:32:52 +00:00
}
2019-05-29 23:29:55 +00:00
switch ($type) {
2017-02-01 01:32:52 +00:00
case 'all':
// If all, execute all the pages, layouts, blocks case statements
case 'pages':
$elements['pages'] = null;
$owner = $channel['channel_id'];
$sql_extra = item_permissions_sql($owner);
$r = q("select * from iconfig left join item on iconfig.iid = item.id
where item.uid = %d and iconfig.cat = 'system' and iconfig.k = 'WEBPAGE' and item_type = %d
2017-02-01 01:32:52 +00:00
$sql_extra order by item.created desc",
intval($owner),
intval(ITEM_TYPE_WEBPAGE)
);
2017-02-01 01:32:52 +00:00
$pages = null;
2019-05-29 23:29:55 +00:00
if ($r) {
$elements['pages'] = [];
$pages = [];
2019-05-29 23:29:55 +00:00
foreach ($r as $rr) {
2017-02-01 01:32:52 +00:00
unobscure($rr);
//$lockstate = (($rr['allow_cid'] || $rr['allow_gid'] || $rr['deny_cid'] || $rr['deny_gid']) ? 'lock' : 'unlock');
2019-05-29 23:29:55 +00:00
$element_arr = [
'type' => 'webpage',
'title' => $rr['title'],
'body' => $rr['body'],
'created' => $rr['created'],
'edited' => $rr['edited'],
'mimetype' => $rr['mimetype'],
'pagetitle' => $rr['v'],
'mid' => $rr['mid'],
'layout_mid' => $rr['layout_mid']
];
$pages[$rr['iid']][] = [
'url' => $rr['iid'],
'pagetitle' => $rr['v'],
'title' => $rr['title'],
'created' => datetime_convert('UTC',date_default_timezone_get(),$rr['created']),
'edited' => datetime_convert('UTC',date_default_timezone_get(),$rr['edited']),
'bb_element' => '[element]' . base64url_encode(json_encode($element_arr)) . '[/element]',
2017-02-01 01:32:52 +00:00
//'lockstate' => $lockstate
2019-05-29 23:29:55 +00:00
];
2017-02-01 01:32:52 +00:00
$elements['pages'][] = $element_arr;
}
}
2019-05-29 23:29:55 +00:00
if ($type !== 'all') {
2017-02-01 01:32:52 +00:00
break;
}
2017-02-01 01:32:52 +00:00
case 'layouts':
$elements['layouts'] = null;
$owner = $channel['channel_id'];
$sql_extra = item_permissions_sql($owner);
$r = q("select * from iconfig left join item on iconfig.iid = item.id
where item.uid = %d and iconfig.cat = 'system' and iconfig.k = 'PDL' and item_type = %d
2017-02-01 01:32:52 +00:00
$sql_extra order by item.created desc",
intval($owner),
intval(ITEM_TYPE_PDL)
);
2019-05-29 23:29:55 +00:00
if ($r) {
$elements['layouts'] = [];
2019-05-29 23:29:55 +00:00
foreach ($r as $rr) {
2017-02-01 01:32:52 +00:00
unobscure($rr);
$elements['layouts'][] = array(
'type' => 'layout',
'description' => $rr['title'], // description of the layout
'body' => $rr['body'],
'created' => $rr['created'],
'edited' => $rr['edited'],
'mimetype' => $rr['mimetype'],
'name' => $rr['v'], // name of reference for the layout
'mid' => $rr['mid'],
2017-02-01 01:32:52 +00:00
);
}
}
2019-05-29 23:29:55 +00:00
if ($type !== 'all') {
2017-02-01 01:32:52 +00:00
break;
}
2017-02-01 01:32:52 +00:00
case 'blocks':
$elements['blocks'] = null;
$owner = $channel['channel_id'];
$sql_extra = item_permissions_sql($owner);
$r = q("select iconfig.iid, iconfig.k, iconfig.v, mid, title, body, mimetype, created, edited from iconfig
2017-02-01 01:32:52 +00:00
left join item on iconfig.iid = item.id
where uid = %d and iconfig.cat = 'system' and iconfig.k = 'BUILDBLOCK'
2017-02-01 01:32:52 +00:00
and item_type = %d order by item.created desc",
intval($owner),
intval(ITEM_TYPE_BLOCK)
);
2019-05-29 23:29:55 +00:00
if ($r) {
$elements['blocks'] = [];
2019-05-29 23:29:55 +00:00
foreach ($r as $rr) {
2017-02-01 01:32:52 +00:00
unobscure($rr);
2019-05-29 23:29:55 +00:00
$elements['blocks'][] = [
2017-02-01 01:32:52 +00:00
'type' => 'block',
'title' => $rr['title'],
2017-02-01 01:32:52 +00:00
'body' => $rr['body'],
'created' => $rr['created'],
'edited' => $rr['edited'],
'mimetype' => $rr['mimetype'],
'name' => $rr['v'],
2017-02-01 01:32:52 +00:00
'mid' => $rr['mid']
2019-05-29 23:29:55 +00:00
];
2017-02-01 01:32:52 +00:00
}
}
2019-05-29 23:29:55 +00:00
if ($type !== 'all') {
2017-02-01 01:32:52 +00:00
break;
}
2017-02-01 01:32:52 +00:00
default:
break;
}
2017-02-01 01:32:52 +00:00
return $elements;
}
/**
* @brief Create a compressed zip file.
*
* @param array $files List of files to put in zip file
* @param string $destination
* @param boolean $overwrite
* @return boolean Success status
*/
2019-05-29 23:29:55 +00:00
function create_zip_file($files = [], $destination = '', $overwrite = false) {
2017-02-01 01:32:52 +00:00
// if the zip file already exists and overwrite is false, return false
2019-05-29 23:29:55 +00:00
if (file_exists($destination) && ! $overwrite) {
2017-02-01 01:32:52 +00:00
return false;
}
//vars
$valid_files = [];
2017-02-01 01:32:52 +00:00
// if files were passed in...
2019-05-29 23:29:55 +00:00
if (is_array($files)) {
2017-02-01 01:32:52 +00:00
// cycle through each file
2019-05-29 23:29:55 +00:00
foreach ($files as $file) {
2017-02-01 01:32:52 +00:00
// make sure the file exists
2019-05-29 23:29:55 +00:00
if (file_exists($file)) {
2017-02-01 01:32:52 +00:00
$valid_files[] = $file;
}
}
}
2017-02-01 01:32:52 +00:00
// if we have good files...
2019-05-29 23:29:55 +00:00
if (count($valid_files)) {
2017-02-01 01:32:52 +00:00
//create the archive
$zip = new ZipArchive();
if($zip->open($destination, $overwrite ? ZipArchive::OVERWRITE : ZipArchive::CREATE) !== true) {
2017-02-01 01:32:52 +00:00
return false;
}
2017-02-01 01:32:52 +00:00
// add the files
2019-05-29 23:29:55 +00:00
foreach ($valid_files as $file) {
2017-02-01 01:32:52 +00:00
$zip->addFile($file, $file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
// check to make sure the file exists
return file_exists($destination);
}
2017-02-01 01:32:52 +00:00
else {
return false;
}
}