From 2e2f984c459eabf83fc5bfdbd60c892d0f549895 Mon Sep 17 00:00:00 2001 From: Klaus Weidenbach Date: Fri, 24 Mar 2017 23:32:53 +0100 Subject: Add some documentation for import functions. --- Zotlabs/Module/Import.php | 21 +- Zotlabs/Module/Import_items.php | 91 +++---- include/hubloc.php | 104 +++++--- include/import.php | 562 ++++++++++++++++++++++++---------------- include/text.php | 10 +- 5 files changed, 465 insertions(+), 323 deletions(-) diff --git a/Zotlabs/Module/Import.php b/Zotlabs/Module/Import.php index 55c1a8d8b..b98c9be9b 100644 --- a/Zotlabs/Module/Import.php +++ b/Zotlabs/Module/Import.php @@ -83,6 +83,7 @@ class Import extends \Zotlabs\Web\Controller { $api_path .= 'channel/export/basic?f=&channel=' . $channelname; if($import_posts) $api_path .= '&posts=1'; + $binary = false; $redirects = 0; $opts = array('http_auth' => $email . ':' . $password); @@ -104,8 +105,8 @@ class Import extends \Zotlabs\Web\Controller { $data = json_decode($data,true); - // logger('import: data: ' . print_r($data,true)); - // print_r($data); + //logger('import: data: ' . print_r($data,true)); + //print_r($data); if(! array_key_exists('compatibility',$data)) { call_hooks('import_foreign_channel_data',$data); @@ -248,7 +249,7 @@ class Import extends \Zotlabs\Web\Controller { logger('import step 6'); - + // import xchans $xchans = $data['xchan']; if($xchans) { foreach($xchans as $xchan) { @@ -292,13 +293,11 @@ class Import extends \Zotlabs\Web\Controller { dbesc($photodate), dbesc($xchan['xchan_hash']) ); - } logger('import step 7'); } - $friends = 0; $feeds = 0; @@ -367,19 +366,20 @@ class Import extends \Zotlabs\Web\Controller { logger('import step 8'); } + // import groups $groups = $data['group']; if($groups) { $saved = array(); foreach($groups as $group) { $saved[$group['hash']] = array('old' => $group['id']); - if(array_key_exists('name',$group)) { + if(array_key_exists('name', $group)) { $group['gname'] = $group['name']; unset($group['name']); } unset($group['id']); $group['uid'] = $channel['channel_id']; - create_table_from_array('groups',$group); + create_table_from_array('groups', $group); } $r = q("select * from groups where uid = %d", intval($channel['channel_id']) @@ -391,7 +391,7 @@ class Import extends \Zotlabs\Web\Controller { } } - + // import group members $group_members = $data['group_member']; if($group_members) { foreach($group_members as $group_member) { @@ -401,7 +401,7 @@ class Import extends \Zotlabs\Web\Controller { if($x['old'] == $group_member['gid']) $group_member['gid'] = $x['new']; } - create_table_from_array('group_member',$group_member); + create_table_from_array('group_member', $group_member); } } @@ -450,7 +450,6 @@ class Import extends \Zotlabs\Web\Controller { notifications_on($channel['channel_id'],$saved_notification_flags); - if(array_key_exists('item_id',$data) && $data['item_id']) import_item_ids($channel,$data['item_id']); @@ -490,7 +489,7 @@ class Import extends \Zotlabs\Web\Controller { function get() { if(! get_account_id()) { - notice( t('You must be logged in to use this feature.')); + notice( t('You must be logged in to use this feature.') . EOL); return ''; } diff --git a/Zotlabs/Module/Import_items.php b/Zotlabs/Module/Import_items.php index f20cbfe7e..133e37d9e 100644 --- a/Zotlabs/Module/Import_items.php +++ b/Zotlabs/Module/Import_items.php @@ -3,54 +3,58 @@ namespace Zotlabs\Module; require_once('include/import.php'); - +/** + * @brief Module for importing items. + * + * Import existing posts and content from an export file. + */ class Import_items extends \Zotlabs\Web\Controller { function post() { - + if(! local_channel()) return; - + $data = null; - + $src = $_FILES['filename']['tmp_name']; $filename = basename($_FILES['filename']['name']); $filesize = intval($_FILES['filename']['size']); $filetype = $_FILES['filename']['type']; - + 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. - + if(strpos($filename,'.gz')){ @rename($src,$src . '.gz'); @system('gunzip ' . escapeshellarg($src . '.gz')); } - + if($filesize) { $data = @file_get_contents($src); } unlink($src); } - + if(! $src) { - + $old_address = ((x($_REQUEST,'old_address')) ? $_REQUEST['old_address'] : ''); - + if(! $old_address) { - logger('mod_import: nothing to import.'); + logger('Nothing to import.'); notice( t('Nothing to import.') . EOL); return; } - + $email = ((x($_REQUEST,'email')) ? $_REQUEST['email'] : ''); $password = ((x($_REQUEST,'password')) ? $_REQUEST['password'] : ''); - + $year = ((x($_REQUEST,'year')) ? $_REQUEST['year'] : ''); - + $channelname = substr($old_address,0,strpos($old_address,'@')); $servername = substr($old_address,strpos($old_address,'@')+1); - + $scheme = 'https://'; $api_path = '/api/red/channel/export/items?f=&channel=' . $channelname . '&year=' . intval($year); $binary = false; @@ -64,68 +68,65 @@ class Import_items extends \Zotlabs\Web\Controller { $data = $ret['body']; else notice( t('Unable to download data from old server') . EOL); - } - + if(! $data) { - logger('mod_import: empty file.'); + logger('Empty file.'); notice( t('Imported file is empty.') . EOL); return; } - - $data = json_decode($data,true); - - // logger('import: data: ' . print_r($data,true)); - // print_r($data); - + + $data = json_decode($data, true); + + //logger('import: data: ' . print_r($data,true)); + //print_r($data); + if(! is_array($data)) return; - + 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); + $t = sprintf( t('Warning: Database versions differ by %1$d updates.'), $v2 - $v1 ); + notice($t . EOL); } } - + $channel = \App::get_channel(); - - + if(array_key_exists('item',$data) && $data['item']) { import_items($channel,$data['item'],false,((array_key_exists('relocate',$data)) ? $data['relocate'] : null)); } - + if(array_key_exists('item_id',$data) && $data['item_id']) { import_item_ids($channel,$data['item_id']); } - + info( t('Import completed') . EOL); - return; } - - - - + + + /** + * @brief Generate item import page. + * + * @return string with parsed HTML. + */ function get() { - + if(! local_channel()) { notice( t('Permission denied') . EOL); return login(); } - - $o = replace_macros(get_markup_template('item_import.tpl'),array( + + $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'), '$submit' => t('Submit') )); - + return $o; - } - - - + } diff --git a/include/hubloc.php b/include/hubloc.php index e17be028c..599252522 100644 --- a/include/hubloc.php +++ b/include/hubloc.php @@ -1,6 +1,17 @@ - ((array_key_exists('hubloc_deleted',$arr)) ? $arr['hubloc_deleted'] : 0) ]; - return create_table_from_array('hubloc',$store); - + return create_table_from_array('hubloc', $store); } @@ -45,9 +55,8 @@ function prune_hub_reinstalls() { // see if this url has more than one sitekey, indicating it has been re-installed. if(count($x) > 1) { - - $d1 = datetime_convert('UTC','UTC',$x[0]['c']); - $d2 = datetime_convert('UTC','UTC','now - 3 days'); + $d1 = datetime_convert('UTC', 'UTC', $x[0]['c']); + $d2 = datetime_convert('UTC', 'UTC', 'now - 3 days'); // allow some slop period, say 3 days - just in case this is a glitch or transient occurrence // Then remove any hublocs pointing to the oldest entry. @@ -63,18 +72,22 @@ function prune_hub_reinstalls() { } } -function remove_obsolete_hublocs() { - - logger('remove_obsolete_hublocs',LOGGER_DEBUG); - // Get rid of any hublocs which are ours but aren't valid anymore - - // e.g. they point to a different and perhaps transient URL that we aren't using. +/** + * @brief Remove obsolete hublocs. + * + * Get rid of any hublocs which are ours but aren't valid anymore - + * e.g. they point to a different and perhaps transient URL that we aren't using. + * + * I need to stress that this shouldn't happen. fix_system_urls() fixes hublocs + * when it discovers the URL has changed. So it's unclear how we could end up + * with URLs pointing to the old site name. But it happens. This may be an artifact + * of an old bug or maybe a regression in some newer code. In any event, they + * mess up communications and we have to take action if we find any. + */ +function remove_obsolete_hublocs() { - // I need to stress that this shouldn't happen. fix_system_urls() fixes hublocs - // when it discovers the URL has changed. So it's unclear how we could end up - // with URLs pointing to the old site name. But it happens. This may be an artifact - // of an old bug or maybe a regression in some newer code. In any event, they - // mess up communications and we have to take action if we find any. + logger('remove_obsolete_hublocs', LOGGER_DEBUG); // First make sure we have any hublocs (at all) with this URL and sitekey. // We don't want to perform this operation while somebody is in the process @@ -82,27 +95,25 @@ function remove_obsolete_hublocs() { $r = q("select hubloc_id from hubloc where hubloc_url = '%s' and hubloc_sitekey = '%s'", dbesc(z_root()), - dbesc(get_config('system','pubkey')) + dbesc(get_config('system', 'pubkey')) ); if((! $r) || (! count($r))) return; - $channels = array(); - // Good. We have at least one *valid* hubloc. // Do we have any invalid ones? $r = q("select hubloc_id from hubloc where hubloc_sitekey = '%s' and hubloc_url != '%s'", - dbesc(get_config('system','pubkey')), + dbesc(get_config('system', 'pubkey')), dbesc(z_root()) ); $p = q("select hubloc_id from hubloc where hubloc_sitekey != '%s' and hubloc_url = '%s'", - dbesc(get_config('system','pubkey')), + dbesc(get_config('system', 'pubkey')), dbesc(z_root()) ); if(is_array($r) && is_array($p)) - $r = array_merge($r,$p); + $r = array_merge($r, $p); if(! $r) return; @@ -111,8 +122,8 @@ function remove_obsolete_hublocs() { logger('remove_obsolete_hublocs: removing ' . count($r) . ' hublocs.'); - $interval = ((get_config('system','delivery_interval') !== false) - ? intval(get_config('system','delivery_interval')) : 2 ); + $interval = ((get_config('system', 'delivery_interval') !== false) + ? intval(get_config('system', 'delivery_interval')) : 2 ); foreach($r as $rr) { q("update hubloc set hubloc_deleted = 1 where hubloc_id = %d", @@ -120,10 +131,10 @@ function remove_obsolete_hublocs() { ); $x = q("select channel_id from channel where channel_hash = '%s' limit 1", - dbesc($rr['hubloc_hash']) + dbesc($rr['hubloc_hash']) ); if($x) { - Zotlabs\Daemon\Master::Summon(array('Notifier','location',$x[0]['channel_id'])); + Zotlabs\Daemon\Master::Summon(array('Notifier', 'location', $x[0]['channel_id'])); if($interval) @time_sleep_until(microtime(true) + (float) $interval); } @@ -131,8 +142,15 @@ function remove_obsolete_hublocs() { } -// This actually changes other structures to match the given (presumably current) hubloc primary selection - +/** + * @brief Change primary hubloc. + * + * This actually changes other structures to match the given (presumably current) + * hubloc primary selection. + * + * @param array $hubloc + * @return boolean + */ function hubloc_change_primary($hubloc) { if(! is_array($hubloc)) { @@ -170,7 +188,7 @@ function hubloc_change_primary($hubloc) { dbesc($hubloc['hubloc_hash']) ); if(! $r) { - logger('xchan not found'); + logger('xchan not found'); return false; } if($r[0]['xchan_addr'] === $hubloc['hubloc_addr']) { @@ -179,7 +197,7 @@ function hubloc_change_primary($hubloc) { } $url = $hubloc['hubloc_url']; - $lwebbie = substr($hubloc['hubloc_addr'],0,strpos($hubloc['hubloc_addr'],'@')); + $lwebbie = substr($hubloc['hubloc_addr'], 0, strpos($hubloc['hubloc_addr'], '@')); $r = q("update xchan set xchan_addr = '%s', xchan_url = '%s', xchan_follow = '%s', xchan_connurl = '%s' where xchan_hash = '%s'", dbesc($hubloc['hubloc_addr']), @@ -191,14 +209,19 @@ function hubloc_change_primary($hubloc) { if(! $r) logger('xchan_update failed.'); - logger('primary hubloc changed.' . print_r($hubloc,true),LOGGER_DEBUG); + logger('primary hubloc changed.' . print_r($hubloc, true), LOGGER_DEBUG); return true; - } -// We use the post url to distinguish between http and https hublocs. -// The https might be alive, and the http dead. +/** + * @brief Mark a hubloc as down. + * + * We use the post url to distinguish between http and https hublocs. + * The https might be alive, and the http dead. + * + * @param string $posturl Hubloc callback url which to disable + */ function hubloc_mark_as_down($posturl) { $r = q("update hubloc set hubloc_status = ( hubloc_status | %d ) where hubloc_callback = '%s'", intval(HUBLOC_OFFLINE), @@ -208,22 +231,21 @@ function hubloc_mark_as_down($posturl) { - function ping_site($url) { $ret = array('success' => false); $sys = get_sys_channel(); - $m = zot_build_packet($sys,'ping'); - $r = zot_zot($url . '/post',$m); + $m = zot_build_packet($sys, 'ping'); + $r = zot_zot($url . '/post', $m); if(! $r['success']) { $ret['message'] = 'no answer from ' . $url; return $ret; } - $packet_result = json_decode($r['body'],true); + $packet_result = json_decode($r['body'], true); if(! $packet_result['success']) { - $ret['message'] = 'packet failure from ' . $url; + $ret['message'] = 'packet failure from ' . $url; return $ret; } diff --git a/include/import.php b/include/import.php index d0e15c89d..f6ebd4bff 100644 --- a/include/import.php +++ b/include/import.php @@ -1,8 +1,19 @@ $v) { @@ -286,23 +329,25 @@ function sync_objs($channel,$objs) { ); } } - else { - create_table_from_array('obj',$obj); + else { + create_table_from_array('obj', $obj); } } } } - - - - -function import_apps($channel,$apps) { +/** + * @brief Import apps. + * + * @param array $channel + * @param array $apps + */ +function import_apps($channel, $apps) { if($channel && $apps) { foreach($apps as $app) { - $term = ((array_key_exists('term',$app) && is_array($app['term'])) ? $app['term'] : null); + $term = ((array_key_exists('term',$app) && is_array($app['term'])) ? $app['term'] : null); unset($app['id']); unset($app['app_channel']); @@ -311,13 +356,13 @@ function import_apps($channel,$apps) { $app['app_channel'] = $channel['channel_id']; if($app['app_photo']) { - $x = import_xchan_photo($app['app_photo'],$channel['channel_hash'],true); + $x = import_xchan_photo($app['app_photo'], $channel['channel_hash'], true); $app['app_photo'] = $x[0]; } $hash = $app['app_id']; - create_table_from_array('app',$app); + create_table_from_array('app', $app); if($term) { $x = q("select * from app where app_id = '%s' and app_channel = %d limit 1", @@ -328,20 +373,22 @@ function import_apps($channel,$apps) { foreach($term as $t) { if(array_key_exists('type',$t)) $t['ttype'] = $t['type']; + store_item_tag($channel['channel_id'],$x[0]['id'],TERM_OBJ_APP,$t['ttype'],escape_tags($t['term']),escape_tags($t['url'])); } } } - - - } } } - - -function sync_apps($channel,$apps) { +/** + * @brief Sync apps. + * + * @param array $channel + * @param array $apps + */ +function sync_apps($channel, $apps) { if($channel && $apps) { foreach($apps as $app) { @@ -356,20 +403,20 @@ function sync_apps($channel,$apps) { if($x) { $exists = $x[0]; } - + 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", - dbesc($app['app_id']), - intval($channel['channel_id']) - ); + q("delete from app where app_id = '%s' and app_channel = %d", + dbesc($app['app_id']), + intval($channel['channel_id']) + ); if($exists) { q("delete from term where otype = %d and oid = %d", intval(TERM_OBJ_APP), intval($exists['id']) - ); + ); } - continue; - } + continue; + } unset($app['id']); unset($app['app_channel']); @@ -379,7 +426,7 @@ function sync_apps($channel,$apps) { q("delete from term where otype = %d and oid = %d", intval(TERM_OBJ_APP), intval($exists['id']) - ); + ); } if((! $app['app_created']) || ($app['app_created'] <= NULL_DATE)) @@ -440,9 +487,13 @@ function sync_apps($channel,$apps) { } } - - -function import_chatrooms($channel,$chatrooms) { +/** + * @brief Import chatrooms. + * + * @param array $channel + * @param array $chatrooms + */ +function import_chatrooms($channel, $chatrooms) { if($channel && $chatrooms) { foreach($chatrooms as $chatroom) { @@ -457,14 +508,18 @@ function import_chatrooms($channel,$chatrooms) { $chatroom['cr_aid'] = $channel['channel_account_id']; $chatroom['cr_uid'] = $channel['channel_id']; - create_table_from_array('chatroom',$chatroom); + create_table_from_array('chatroom', $chatroom); } } } - - -function sync_chatrooms($channel,$chatrooms) { +/** + * @brief Sync chatrooms. + * + * @param array $channel + * @param array $chatrooms + */ +function sync_chatrooms($channel, $chatrooms) { if($channel && $chatrooms) { foreach($chatrooms as $chatroom) { @@ -473,13 +528,12 @@ function sync_chatrooms($channel,$chatrooms) { continue; 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; - } - + q("delete from chatroom where cr_name = '%s' and cr_uid = %d", + dbesc($chatroom['cr_name']), + intval($channel['channel_id']) + ); + continue; + } unset($chatroom['cr_id']); unset($chatroom['cr_aid']); @@ -502,6 +556,7 @@ function sync_chatrooms($channel,$chatrooms) { if($x) { if($x[0]['cr_edited'] >= $chatroom['cr_edited']) continue; + $exists = true; } $name = $chatroom['cr_name']; @@ -517,21 +572,28 @@ function sync_chatrooms($channel,$chatrooms) { } } else { - create_table_from_array('chatroom',$chatroom); + create_table_from_array('chatroom', $chatroom); } } } } - -function import_items($channel,$items,$sync = false,$relocate = null) { +/** + * @brief Import items to channel. + * + * @param array $channel where to import to + * @param array $items + * @param boolean $sync + * @param array $relocate default null + */ +function import_items($channel, $items, $sync = false, $relocate = null) { if($channel && $items) { - + $allow_code = channel_codeallowed($channel['channel_id']); - $deliver = false; // Don't deliver any messages or notifications when importing + $deliver = false; // Don't deliver any messages or notifications when importing foreach($items as $i) { $item_result = false; @@ -549,14 +611,14 @@ function import_items($channel,$items,$sync = false,$relocate = null) { ); if($r) { - // flags may have changed and we are probably relocating the post, + // flags may have changed and we are probably relocating the post, // so force an update even if we have the same timestamp if($item['edited'] >= $r[0]['edited']) { $item['id'] = $r[0]['id']; $item['uid'] = $channel['channel_id']; $item_result = item_store_update($item,$allow_code,$deliver); - } + } } else { $item['aid'] = $channel['channel_account_id']; @@ -578,9 +640,17 @@ function import_items($channel,$items,$sync = false,$relocate = null) { } } - -function sync_items($channel,$items,$relocate = null) { - import_items($channel,$items,true,$relocate); +/** + * @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); } @@ -594,20 +664,26 @@ function import_item_ids($channel,$itemids) { ); if(! $r) continue; - $z = q("select * from iconfig where iconfig.cat = 'system' and iconfig.k = '%s' + $z = q("select * from iconfig where iconfig.cat = 'system' and iconfig.k = '%s' and iconfig.v = '%s' and iid = %d limit 1", dbesc($i['service']), dbesc($i['sid']), intval($r[0]['id']) ); if(! $z) { - \Zotlabs\Lib\IConfig::Set($r[0]['id'],'system',$i['service'],$i['sid'],true); + IConfig::Set($r[0]['id'],'system',$i['service'],$i['sid'],true); } } } } -function import_events($channel,$events) { +/** + * @brief Import events. + * + * @param array $channel + * @param array $events + */ +function import_events($channel, $events) { if($channel && $events) { foreach($events as $event) { @@ -619,13 +695,18 @@ function import_events($channel,$events) { convert_oldfields($event,'type','etype'); convert_oldfields($event,'ignore','dismissed'); - create_table_from_array('event',$event); + create_table_from_array('event', $event); } } } - -function sync_events($channel,$events) { +/** + * @brief Sync events. + * + * @param array $channel + * @param array $events + */ +function sync_events($channel, $events) { if($channel && $events) { foreach($events as $event) { @@ -637,7 +718,7 @@ function sync_events($channel,$events) { $r = q("delete from event where event_hash = '%s' and uid = %d", dbesc($event['event_hash']), intval($channel['channel_id']) - ); + ); continue; } @@ -650,7 +731,6 @@ function sync_events($channel,$events) { convert_oldfields($event,'type','etype'); convert_oldfields($event,'ignore','dismissed'); - $exists = false; $x = q("select * from event where event_hash = '%s' and uid = %d limit 1", @@ -660,6 +740,7 @@ function sync_events($channel,$events) { if($x) { if($x[0]['edited'] >= $event['edited']) continue; + $exists = true; } @@ -674,15 +755,19 @@ function sync_events($channel,$events) { } } else { - create_table_from_array('event',$event); + create_table_from_array('event', $event); } } } } - -function import_menus($channel,$menus) { - +/** + * @brief Import menus. + * + * @param array $channel + * @param array $menus + */ +function import_menus($channel, $menus) { if($channel && $menus) { foreach($menus as $menu) { @@ -701,7 +786,6 @@ function import_menus($channel,$menus) { $m['menu_flags'] |= MENU_BOOKMARK; if(in_array('system',$menu['flags'])) $m['menu_flags'] |= MENU_SYSTEM; - } $menu_id = menu_create($m); @@ -729,16 +813,19 @@ function import_menus($channel,$menus) { } menu_add_item($menu_id,$channel['channel_id'],$mitem); } - } + } } } } - - } - -function sync_menus($channel,$menus) { +/** + * @brief Sync menus. + * + * @param array $channel + * @param array $menus + */ +function sync_menus($channel, $menus) { if($channel && $menus) { foreach($menus as $menu) { @@ -795,7 +882,6 @@ function sync_menus($channel,$menus) { foreach($menu['items'] as $it) { $mitem = array(); - $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']); $mitem['mitem_link'] = str_replace('[cloudurl]',z_root() . '/cloud/' . $channel['channel_address'],$it['link']); @@ -814,15 +900,19 @@ function sync_menus($channel,$menus) { } menu_add_item($menu_id,$channel['channel_id'],$mitem); } - } + } } } } } - - -function import_likes($channel,$likes) { +/** + * @brief Import likes. + * + * @param array $channel + * @param array $likes + */ +function import_likes($channel, $likes) { if($channel && $likes) { foreach($likes as $like) { if($like['deleted']) { @@ -835,7 +925,7 @@ function import_likes($channel,$likes) { ); continue; } - + unset($like['id']); unset($like['iid']); $like['channel_id'] = $channel['channel_id']; @@ -850,9 +940,9 @@ function import_likes($channel,$likes) { if($r) continue; - create_table_from_array('likes',$like); + create_table_from_array('likes', $like); } - } + } } function import_conv($channel,$convs) { @@ -865,7 +955,7 @@ function import_conv($channel,$convs) { ); continue; } - + unset($conv['id']); $conv['uid'] = $channel['channel_id']; @@ -877,14 +967,19 @@ function import_conv($channel,$convs) { ); if($r) continue; + create_table_from_array('conv',$conv); } - } + } } - - -function import_mail($channel,$mails,$sync = false) { +/** + * @brief Import mails. + * + * @param array $channel + * @param array $mails + */ +function import_mail($channel, $mails, $sync = false) { if($channel && $mails) { foreach($mails as $mail) { if(array_key_exists('flags',$mail) && in_array('deleted',$mail['flags'])) { @@ -913,14 +1008,27 @@ function import_mail($channel,$mails,$sync = false) { Zotlabs\Daemon\Master::Summon(array('Notifier','single_mail',$mail_id)); } } - } + } } -function sync_mail($channel,$mails) { - import_mail($channel,$mails,true); +/** + * @brief Synchronise mails. + * + * @see import_mail + * @param array $channel + * @param array $mails + */ +function sync_mail($channel, $mails) { + import_mail($channel, $mails, true); } -function sync_files($channel,$files) { +/** + * @brief Synchronise files. + * + * @param array $channel + * @param array $files + */ +function sync_files($channel, $files) { require_once('include/attach.php'); @@ -934,7 +1042,7 @@ function sync_files($channel,$files) { $original_channel = $f['original_channel']; if(! ($fetch_url && $original_channel)) - continue; + continue; if($f['attach']) { $attachment_stored = false; @@ -964,12 +1072,11 @@ function sync_files($channel,$files) { $att['aid'] = $channel['channel_account_id']; $att['uid'] = $channel['channel_id']; - - // check for duplicate folder names with the same parent. + // 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. + // 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. if(strpos($att['filename'],'.') !== false) { $basename = substr($att['filename'],0,strrpos($att['filename'],'.')); @@ -1000,7 +1107,7 @@ function sync_files($channel,$files) { } if($found) $x++; - } + } while($found); $att['filename'] = $basename . '(' . $x . ')' . $ext; } @@ -1009,30 +1116,31 @@ function sync_files($channel,$files) { // end duplicate detection -// @fixme - update attachment structures if they are modified rather than created + /// @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. + // fetch the file contents. // If the hash ever contains any escapable chars this could cause - // problems. Currently it does not. + // problems. Currently it does not. - // @TODO implement os_path + /// @TODO implement os_path if(!isset($att['os_path'])) $att['os_path'] = ''; - if($attach_exists) { logger('sync_files attach exists: ' . print_r($att,true), LOGGER_DEBUG); if(! dbesc_array($att)) continue; + $str = ''; - foreach($att as $k => $v) { - if($str) - $str .= ","; - $str .= " " . TQUOT . $k . TQUOT . " = '" . $v . "' "; - } + foreach($att as $k => $v) { + if($str) + $str .= ","; + + $str .= " " . TQUOT . $k . TQUOT . " = '" . $v . "' "; + } $r = dbq("update attach set " . $str . " where id = " . intval($attach_id) ); } else { @@ -1040,7 +1148,6 @@ function sync_files($channel,$files) { create_table_from_array('attach',$att); } - // is this a directory? if($att['filetype'] === 'multipart/mixed' && $att['is_dir']) { @@ -1049,17 +1156,16 @@ function sync_files($channel,$files) { 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 + // 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. + // audio/video file or something. $time = datetime_convert(); - $parr = array('hash' => $channel['channel_hash'], - 'time' => $time, + $parr = array('hash' => $channel['channel_hash'], + 'time' => $time, 'resource' => $att['hash'], 'revision' => 0, 'signature' => base64url_encode(rsa_sign($channel['channel_hash'] . '.' . $time, $channel['channel_prvkey'])) @@ -1084,7 +1190,7 @@ function sync_files($channel,$files) { } } if(! $attachment_stored) { - // @TODO should we queue this and retry or delete everything or what? + /// @TODO should we queue this and retry or delete everything or what? logger('attachment store failed',LOGGER_NORMAL,LOG_ERR); } if($f['photo']) { @@ -1128,7 +1234,6 @@ function sync_files($channel,$files) { else $p['content'] = base64_decode($p['content']); - if(!isset($p['display_path'])) $p['display_path'] = ''; @@ -1138,17 +1243,18 @@ function sync_files($channel,$files) { intval($channel['channel_id']) ); - if($exists) { if(! dbesc_array($p)) continue; - $str = ''; - foreach($p as $k => $v) { - if($str) - $str .= ","; - $str .= " " . TQUOT . $k . TQUOT . " = '" . $v . "' "; - } - $r = dbq("update photo set " . $str . " where id = " . intval($exists[0]['id']) ); + + $str = ''; + foreach($p as $k => $v) { + if($str) + $str .= ","; + + $str .= " " . TQUOT . $k . TQUOT . " = '" . $v . "' "; + } + $r = dbq("update photo set " . $str . " where id = " . intval($exists[0]['id']) ); } else { create_attach_from_array('photo',$p); @@ -1164,9 +1270,17 @@ function sync_files($channel,$files) { } } - -function convert_oldfields(&$arr,$old,$new) { - if(array_key_exists($old,$arr)) { +/** + * @brief Rename a key in an array. + * + * Replaces $old key with $new key in $arr. + * + * @param array[in,out] $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) { + if(array_key_exists($old, $arr)) { $arr[$new] = $arr[$old]; unset($arr[$old]); } @@ -1240,14 +1354,15 @@ function scan_webpage_elements($path, $type, $cloud = false) { } } } + return $elements; } - + function import_webpage_element($element, $channel, $type) { - + $arr = array(); // construct information for the webpage element item table record - + switch($type) { // // PAGES @@ -1256,26 +1371,26 @@ function import_webpage_element($element, $channel, $type) { $arr['item_type'] = ITEM_TYPE_WEBPAGE; $namespace = 'WEBPAGE'; $name = $element['pagelink']; - if($name) { + if($name) { require_once('library/urlify/URLify.php'); $name = strtolower(\URLify::transliterate($name)); - } + } $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 - if($element['layout'] !== '') { - $liid = q("select iid from iconfig where k = 'PDL' and v = '%s' and cat = 'system'", + // it does not exist, leave layout_mid empty + if($element['layout'] !== '') { + $liid = q("select iid from iconfig where k = 'PDL' and v = '%s' and cat = 'system'", dbesc($element['layout']) - ); + ); if($liid) { - $linfo = q("select mid from item where id = %d", + $linfo = q("select mid from item where id = %d", intval($liid[0]['iid']) - ); - $arr['layout_mid'] = $linfo[0]['mid']; - } - } + ); + $arr['layout_mid'] = $linfo[0]['mid']; + } + } break; // // LAYOUTS @@ -1295,15 +1410,15 @@ function import_webpage_element($element, $channel, $type) { $namespace = 'BUILDBLOCK'; $name = $element['name']; $arr['title'] = $element['title']; - + break; default : return null; // return null if invalid element type } - + $arr['uid'] = local_channel(); $arr['aid'] = $channel['channel_account_id']; - + // 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) @@ -1333,7 +1448,7 @@ function import_webpage_element($element, $channel, $type) { 'text/markdown', 'text/plain', 'application/x-pdl', - 'application/x-php' + 'application/x-php' ]; // Blocks and pages can have any of the valid mimetypes, but layouts must be text/bbcode if((in_array($element['mimetype'], $mimetypes)) && ($type === 'page' || $type === 'block') ) { @@ -1347,14 +1462,13 @@ function import_webpage_element($element, $channel, $type) { $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']), + $i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1", + dbesc($arr['mid']), intval(local_channel()) ); - - \Zotlabs\Lib\IConfig::Set($arr,'system',$namespace,(($name) ? $name : substr($arr['mid'],0,16)),true); - - + + IConfig::Set($arr,'system',$namespace,(($name) ? $name : substr($arr['mid'],0,16)),true); + if($i) { $arr['id'] = $i[0]['id']; // don't update if it has the same timestamp as the original @@ -1372,23 +1486,23 @@ function import_webpage_element($element, $channel, $type) { else $x = item_store($arr,$execflag); } - + if($x && $x['success']) { - $item_id = $x['item_id']; + //$item_id = $x['item_id']; //update_remote_id($channel, $item_id, $arr['item_type'], $name, $namespace, $remote_id, $arr['mid']); $element['import_success'] = 1; } else { $element['import_success'] = 0; } - - return $element; + + return $element; } function get_webpage_elements($channel, $type = 'all') { $elements = array(); - if(!$channel['channel_id']) { - return null; + if(!$channel['channel_id']) { + return null; } switch($type) { case 'all': @@ -1396,17 +1510,16 @@ function get_webpage_elements($channel, $type = 'all') { case 'pages': $elements['pages'] = null; $owner = $channel['channel_id']; - - $sql_extra = item_permissions_sql($owner); + $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 + $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 $sql_extra order by item.created desc", intval($owner), intval(ITEM_TYPE_WEBPAGE) ); - + $pages = null; if($r) { @@ -1439,7 +1552,6 @@ function get_webpage_elements($channel, $type = 'all') { ); $elements['pages'][] = $element_arr; } - } if($type !== 'all') { break; @@ -1448,91 +1560,91 @@ function get_webpage_elements($channel, $type = 'all') { case 'layouts': $elements['layouts'] = null; $owner = $channel['channel_id']; - - $sql_extra = item_permissions_sql($owner); + $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 + $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 $sql_extra order by item.created desc", intval($owner), intval(ITEM_TYPE_PDL) ); - - $layouts = null; if($r) { $elements['layouts'] = array(); - $layouts = array(); + foreach($r as $rr) { 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'], + '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'], ); } } - + if($type !== 'all') { break; } - + case 'blocks': $elements['blocks'] = null; $owner = $channel['channel_id']; - - $sql_extra = item_permissions_sql($owner); + $sql_extra = item_permissions_sql($owner); - $r = q("select iconfig.iid, iconfig.k, iconfig.v, mid, title, body, mimetype, created, edited from iconfig + $r = q("select iconfig.iid, iconfig.k, iconfig.v, mid, title, body, mimetype, created, edited from iconfig left join item on iconfig.iid = item.id - where uid = %d and iconfig.cat = 'system' and iconfig.k = 'BUILDBLOCK' + where uid = %d and iconfig.cat = 'system' and iconfig.k = 'BUILDBLOCK' and item_type = %d order by item.created desc", intval($owner), intval(ITEM_TYPE_BLOCK) ); - - $blocks = null; if($r) { $elements['blocks'] = array(); - $blocks = array(); + foreach($r as $rr) { unobscure($rr); $elements['blocks'][] = array( 'type' => 'block', - 'title' => $rr['title'], + 'title' => $rr['title'], 'body' => $rr['body'], 'created' => $rr['created'], 'edited' => $rr['edited'], 'mimetype' => $rr['mimetype'], - 'name' => $rr['v'], + 'name' => $rr['v'], 'mid' => $rr['mid'] ); } - } - + if($type !== 'all') { break; } - + default: break; } + return $elements; } -/* creates a compressed zip file */ - +/** + * @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 + */ function create_zip_file($files = array(), $destination = '', $overwrite = false) { // if the zip file already exists and overwrite is false, return false if(file_exists($destination) && !$overwrite) { @@ -1549,13 +1661,13 @@ function create_zip_file($files = array(), $destination = '', $overwrite = false $valid_files[] = $file; } } - } + } // if we have good files... if(count($valid_files)) { //create the archive $zip = new ZipArchive(); - if($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { + if($zip->open($destination, $overwrite ? ZipArchive::OVERWRITE : ZipArchive::CREATE) !== true) { return false; } // add the files @@ -1569,7 +1681,7 @@ function create_zip_file($files = array(), $destination = '', $overwrite = false // check to make sure the file exists return file_exists($destination); - } + } else { return false; } diff --git a/include/text.php b/include/text.php index 0a2073272..fa9dadc31 100644 --- a/include/text.php +++ b/include/text.php @@ -3050,7 +3050,15 @@ function array2XML($obj, $array) { } } - +/** + * @brief Inserts an array into $table. + * + * @TODO Why is this function in include/text.php? + * + * @param string $table + * @param array $arr + * @return boolean|PDOStatement + */ function create_table_from_array($table, $arr) { if(! ($arr && $table)) -- cgit v1.2.3