From 1e4ef812445e4d8617223ce917d0eb2215e9c3b3 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Tue, 12 Jul 2016 21:41:37 -0400 Subject: Import blocks from zip file, but needs updates as detailed in https://gitlab.com/zot/hubsites/merge_requests/2 --- Zotlabs/Module/Webpages.php | 222 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index bb8d9c6ed..6419d608b 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -210,4 +210,226 @@ class Webpages extends \Zotlabs\Web\Controller { return $o; } + function post() { + + if(($_FILES) && array_key_exists('zip_file',$_FILES)) { + $source = $_FILES["zip_file"]["tmp_name"]; + $type = $_FILES["zip_file"]["type"]; + $okay = false; + $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed'); + foreach ($accepted_types as $mime_type) { + if ($mime_type == $type) { + $okay = true; + break; + } + } + if(!$okay) { + json_return_and_die(array('message' => 'Invalid file MIME type')); + } + $zip = new \ZipArchive(); + if ($zip->open($source) === true) { + $tmp_folder_name = random_string(5); + $website = dirname($source) . '/' . $tmp_folder_name; + $zip->extractTo($website); // change this to the correct site path + $zip->close(); + @unlink($source); + + $hubsites = $this->import_website($website); + rrmdir($website); + $channel = \App::get_channel(); + $blocks = $this->import_blocks($channel, $hubsites['blocks']); + logger('blocks imported: ' . json_encode($blocks)); + } + + + } + } + + private function import_website($path) { + $hubsites = []; + $pages = []; + $blocks = []; + $layouts = []; + // Import pages + $dirtoscan = $path . '/pages/'; + if (is_dir($dirtoscan)) { + $dirlist = scandir($dirtoscan); + if ($dirlist) { + foreach ($dirlist as $element) { + if ($element === '.' || $element === '..') { + continue; + } + $folder = $dirtoscan . '/' . $element; + if (is_dir($folder)) { + $jsonfilepath = $folder . '/page.json'; + if (is_file($jsonfilepath)) { + $pagejson = json_decode(file_get_contents($jsonfilepath), true); + $pagejson['path'] = $folder . '/' . $pagejson['contentfile']; + if ($pagejson['contentfile'] === '') { + logger('hubsites plugin: Invalid page content file'); + return false; + } + $pagecontent = file_get_contents($folder . '/' . $pagejson['contentfile']); + if (!$pagecontent) { + logger('hubsites plugin: Failed to get file content for ' . $pagejson['contentfile']); + return false; + } + $pages[] = $pagejson; + } + } + } + } + } + $hubsites['pages'] = $pages; + // Import layouts + $dirtoscan = $path . '/layouts/'; + if (is_dir($dirtoscan)) { + $dirlist = scandir($dirtoscan); + if ($dirlist) { + foreach ($dirlist as $element) { + if ($element === '.' || $element === '..') { + continue; + } + $folder = $dirtoscan . '/' . $element; + if (is_dir($folder)) { + $jsonfilepath = $folder . '/layout.json'; + if (is_file($jsonfilepath)) { + $layoutjson = json_decode(file_get_contents($jsonfilepath), true); + $layoutjson['path'] = $folder . '/' . $layoutjson['contentfile']; + if ($layoutjson['contentfile'] === '') { + logger('hubsites plugin: Invalid layout content file'); + return false; + } + $layoutcontent = file_get_contents($folder . '/' . $layoutjson['contentfile']); + if (!$layoutcontent) { + logger('hubsites plugin: Failed to get file content for ' . $layoutjson['contentfile']); + return false; + } + $layouts[] = $layoutjson; + } + } + } + } + } + $hubsites['layouts'] = $layouts; + // Import blocks + $dirtoscan = $path . '/blocks/'; + if (is_dir($dirtoscan)) { + $dirlist = scandir($dirtoscan); + if ($dirlist) { + foreach ($dirlist as $element) { + if ($element === '.' || $element === '..') { + continue; + } + $folder = $dirtoscan . '/' . $element; + if (is_dir($folder)) { + $jsonfilepath = $folder . '/block.json'; + if (is_file($jsonfilepath)) { + $block = json_decode(file_get_contents($jsonfilepath), true); + $block['path'] = $folder . '/' . $block['contentfile']; + if ($block['contentfile'] === '') { + logger('hubsites plugin: Invalid block content file'); + return false; + } + $blockcontent = file_get_contents($folder . '/' . $block['contentfile']); + if (!$blockcontent) { + logger('hubsites plugin: Failed to get file content for ' . $block['contentfile']); + return false; + } + $blocks[] = $block; + } + } + } + } + } + $hubsites['blocks'] = $blocks; + //logger('hubsites: ' . json_encode($hubsites)); + return $hubsites; + } + + private function import_blocks($channel, $blocks) { + foreach ($blocks as &$b) { + + $arr = array(); + $arr['item_type'] = ITEM_TYPE_BLOCK; + $namespace = 'BUILDBLOCK'; + $arr['uid'] = $channel['channel_id']; + $arr['aid'] = $channel['channel_account_id']; + + $iid = q("select iid from item_id where service = 'BUILDBLOCK' and sid = '%s' and uid = %d", + dbesc($b['name']), + intval($channel['channel_id']) + ); + if($iid) { + $iteminfo = q("select mid,created,edited from item where id = %d", + intval($iid[0]['iid']) + ); + $arr['mid'] = $arr['parent_mid'] = $iteminfo[0]['mid']; + $arr['created'] = $iteminfo[0]['created']; + $arr['edited'] = (($b['edited']) ? datetime_convert('UTC', 'UTC', $b['edited']) : datetime_convert()); + } else { + $arr['created'] = (($b['created']) ? datetime_convert('UTC', 'UTC', $b['created']) : datetime_convert()); + $arr['edited'] = datetime_convert('UTC', 'UTC', '0000-00-00 00:00:00'); + $arr['mid'] = $arr['parent_mid'] = item_message_id(); + } + $arr['title'] = $b['title']; + $arr['body'] = file_get_contents($b['path']); + $arr['owner_xchan'] = get_observer_hash(); + $arr['author_xchan'] = (($b['author_xchan']) ? $b['author_xchan'] : get_observer_hash()); + if(($b['mimetype'] === 'text/bbcode' || $b['mimetype'] === 'text/html' || + $b['mimetype'] === 'text/markdown' ||$b['mimetype'] === 'text/plain' || + $b['mimetype'] === 'application/x-pdl' ||$b['mimetype'] === 'application/x-php')) { + $arr['mimetype'] = $b['mimetype']; + } else { + $arr['mimetype'] = 'text/bbcode'; + } + + $pagetitle = $b['name']; + + // Verify ability to use html or php!!! + $execflag = false; + if ($arr['mimetype'] === 'application/x-php') { + $z = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id where channel_id = %d limit 1", intval(local_channel()) + ); + + if ($z && (($z[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE) || ($z[0]['channel_pageflags'] & PAGE_ALLOWCODE))) { + $execflag = true; + } + } + + $remote_id = 0; + + $z = q("select * from item_id where sid = '%s' and service = '%s' and uid = %d limit 1", dbesc($pagetitle), dbesc($namespace), intval(local_channel()) + ); + + $i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1", dbesc($arr['mid']), intval(local_channel()) + ); + if ($z && $i) { + $remote_id = $z[0]['id']; + $arr['id'] = $i[0]['id']; + // don't update if it has the same timestamp as the original + if ($arr['edited'] > $i[0]['edited']) + $x = item_store_update($arr, $execflag); + } else { + if (($i) && (intval($i[0]['item_deleted']))) { + // was partially deleted already, finish it off + q("delete from item where mid = '%s' and uid = %d", dbesc($arr['mid']), intval(local_channel()) + ); + } + $x = item_store($arr, $execflag); + } + if ($x['success']) { + $item_id = $x['item_id']; + update_remote_id($channel, $item_id, $arr['item_type'], $pagetitle, $namespace, $remote_id, $arr['mid']); + $b['import_success'] = 1; + } else { + $b['import_success'] = 0; + } + } + return $blocks; +} + + + + } -- cgit v1.2.3 From ba903e21edb14639a948fdccafa35b743691c7cc Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Wed, 13 Jul 2016 06:17:12 -0400 Subject: Updated import_blocks for database calls --- Zotlabs/Module/Webpages.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index 9ece989a0..01a32bf24 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -355,9 +355,8 @@ class Webpages extends \Zotlabs\Web\Controller { $arr['uid'] = $channel['channel_id']; $arr['aid'] = $channel['channel_account_id']; - $iid = q("select iid from item_id where service = 'BUILDBLOCK' and sid = '%s' and uid = %d", - dbesc($b['name']), - intval($channel['channel_id']) + $iid = q("select iid from iconfig where k = 'BUILDBLOCK' and v = '%s' and cat = 'system'", + dbesc($b['name']) ); if($iid) { $iteminfo = q("select mid,created,edited from item where id = %d", @@ -398,8 +397,7 @@ class Webpages extends \Zotlabs\Web\Controller { $remote_id = 0; - $z = q("select * from item_id where sid = '%s' and service = '%s' and uid = %d limit 1", dbesc($pagetitle), dbesc($namespace), intval(local_channel()) - ); + $z = q("select * from iconfig where v = '%s' and k = '%s' and cat = 'service' limit 1", dbesc($pagetitle), dbesc($namespace)); $i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1", dbesc($arr['mid']), intval(local_channel()) ); -- cgit v1.2.3 From 960e9edff596c12dbd88a6ed92277fa51e962808 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Wed, 13 Jul 2016 21:46:59 -0400 Subject: Blocks, pages, and layouts import from zip file. Layouts are not applied to pages until imported twice though. --- Zotlabs/Module/Webpages.php | 199 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 190 insertions(+), 9 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index 01a32bf24..1c9586615 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -234,10 +234,13 @@ class Webpages extends \Zotlabs\Web\Controller { @unlink($source); $hubsites = $this->import_website($website); - rrmdir($website); $channel = \App::get_channel(); $blocks = $this->import_blocks($channel, $hubsites['blocks']); - logger('blocks imported: ' . json_encode($blocks)); + $pages = $this->import_pages($channel, $hubsites['pages']); + $layouts = $this->import_layouts($channel, $hubsites['layouts']); + if($blocks || $pages || $layouts) { // Without the if statement, the folder is deleted before the import_blocks function completes. + rrmdir($website); + } } @@ -265,12 +268,12 @@ class Webpages extends \Zotlabs\Web\Controller { $pagejson = json_decode(file_get_contents($jsonfilepath), true); $pagejson['path'] = $folder . '/' . $pagejson['contentfile']; if ($pagejson['contentfile'] === '') { - logger('hubsites plugin: Invalid page content file'); + logger('Invalid page content file'); return false; } $pagecontent = file_get_contents($folder . '/' . $pagejson['contentfile']); if (!$pagecontent) { - logger('hubsites plugin: Failed to get file content for ' . $pagejson['contentfile']); + logger('Failed to get file content for ' . $pagejson['contentfile']); return false; } $pages[] = $pagejson; @@ -296,12 +299,12 @@ class Webpages extends \Zotlabs\Web\Controller { $layoutjson = json_decode(file_get_contents($jsonfilepath), true); $layoutjson['path'] = $folder . '/' . $layoutjson['contentfile']; if ($layoutjson['contentfile'] === '') { - logger('hubsites plugin: Invalid layout content file'); + logger('Invalid layout content file'); return false; } $layoutcontent = file_get_contents($folder . '/' . $layoutjson['contentfile']); if (!$layoutcontent) { - logger('hubsites plugin: Failed to get file content for ' . $layoutjson['contentfile']); + logger('Failed to get file content for ' . $layoutjson['contentfile']); return false; } $layouts[] = $layoutjson; @@ -327,12 +330,12 @@ class Webpages extends \Zotlabs\Web\Controller { $block = json_decode(file_get_contents($jsonfilepath), true); $block['path'] = $folder . '/' . $block['contentfile']; if ($block['contentfile'] === '') { - logger('hubsites plugin: Invalid block content file'); + logger('Invalid block content file'); return false; } $blockcontent = file_get_contents($folder . '/' . $block['contentfile']); if (!$blockcontent) { - logger('hubsites plugin: Failed to get file content for ' . $block['contentfile']); + logger('Failed to get file content for ' . $block['contentfile']); return false; } $blocks[] = $block; @@ -342,7 +345,6 @@ class Webpages extends \Zotlabs\Web\Controller { } } $hubsites['blocks'] = $blocks; - //logger('hubsites: ' . json_encode($hubsites)); return $hubsites; } @@ -427,6 +429,185 @@ class Webpages extends \Zotlabs\Web\Controller { } +private function import_pages($channel, $pages) { + foreach ($pages as &$p) { + + $arr = array(); + $arr['item_type'] = ITEM_TYPE_WEBPAGE; + $namespace = 'WEBPAGE'; + $arr['uid'] = $channel['channel_id']; + $arr['aid'] = $channel['channel_account_id']; + + if($p['pagelink']) { + require_once('library/urlify/URLify.php'); + $pagetitle = strtolower(\URLify::transliterate($p['pagelink'])); + } + $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 + logger('hubsites plugin: $p[layout] = ' . $p['layout']); + if($p['layout'] !== '') { + $liid = q("select iid from iconfig where k = 'PDL' and v = '%s' and cat = 'system'", + dbesc($p['layout']) + ); + if($liid) { + $linfo = q("select mid from item where id = %d", + intval($liid[0]['iid']) + ); + logger('hubsites plugin: $linfo= ' . json_encode($linfo,true)); + $arr['layout_mid'] = $linfo[0]['mid']; + } + } + // See if the page already exists + $iid = q("select iid from iconfig where k = 'WEBPAGE' and v = '%s' and cat = 'system'", + dbesc($pagetitle) + ); + if($iid) { + // Get the existing page info + $pageinfo = q("select mid,layout_mid,created,edited from item where id = %d", + intval($iid[0]['iid']) + ); + $arr['mid'] = $arr['parent_mid'] = $pageinfo[0]['mid']; + $arr['created'] = $pageinfo[0]['created']; + $arr['edited'] = (($p['edited']) ? datetime_convert('UTC', 'UTC', $p['edited']) : datetime_convert()); + } else { + $arr['created'] = (($p['created']) ? datetime_convert('UTC', 'UTC', $p['created']) : datetime_convert()); + $arr['edited'] = datetime_convert('UTC', 'UTC', '0000-00-00 00:00:00'); + $arr['mid'] = $arr['parent_mid'] = item_message_id(); + } + $arr['title'] = $p['title']; + $arr['body'] = file_get_contents($p['path']); + $arr['term'] = $p['term']; // Not sure what this is supposed to be + + $arr['owner_xchan'] = get_observer_hash(); + $arr['author_xchan'] = (($p['author_xchan']) ? $p['author_xchan'] : get_observer_hash()); + if(($p['mimetype'] === 'text/bbcode' || $p['mimetype'] === 'text/html' || + $p['mimetype'] === 'text/markdown' ||$p['mimetype'] === 'text/plain' || + $p['mimetype'] === 'application/x-pdl' ||$p['mimetype'] === 'application/x-php')) { + $arr['mimetype'] = $p['mimetype']; + } else { + $arr['mimetype'] = 'text/bbcode'; + } + + // Verify ability to use html or php!!! + $execflag = false; + if ($arr['mimetype'] === 'application/x-php') { + $z = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id where channel_id = %d limit 1", intval(local_channel()) + ); + + if ($z && (($z[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE) || ($z[0]['channel_pageflags'] & PAGE_ALLOWCODE))) { + $execflag = true; + } + } + + $remote_id = 0; + + $z = q("select * from iconfig where v = '%s' and k = '%s' and cat = 'system' limit 1", + dbesc($pagetitle), + dbesc($namespace) + ); + + $i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1", + dbesc($arr['mid']), + intval(local_channel()) + ); + if ($z && $i) { + $remote_id = $z[0]['id']; + $arr['id'] = $i[0]['id']; + // don't update if it has the same timestamp as the original + if ($arr['edited'] > $i[0]['edited']) + $x = item_store_update($arr, $execflag); + } else { + if (($i) && (intval($i[0]['item_deleted']))) { + // was partially deleted already, finish it off + q("delete from item where mid = '%s' and uid = %d", dbesc($arr['mid']), intval(local_channel()) + ); + } + logger('hubsites plugin: item_store= ' . json_encode($arr,true)); + $x = item_store($arr, $execflag); + } + if ($x['success']) { + $item_id = $x['item_id']; + update_remote_id($channel, $item_id, $arr['item_type'], $pagetitle, $namespace, $remote_id, $arr['mid']); + $p['import_success'] = 1; + } else { + $p['import_success'] = 0; + } + } + return $pages; + +} + +private function import_layouts($channel, $layouts) { + foreach ($layouts as &$p) { + + $arr = array(); + $arr['item_type'] = ITEM_TYPE_PDL; + $namespace = 'PDL'; + $arr['uid'] = $channel['channel_id']; + $arr['aid'] = $channel['channel_account_id']; + $pagetitle = $p['name']; + // See if the layout already exists + $iid = q("select iid from iconfig where k = 'PDL' and v = '%s' and cat = 'system'", + dbesc($pagetitle) + ); + if($iid) { + // Get the existing layout info + $info = q("select mid,layout_mid,created,edited from item where id = %d", + intval($iid[0]['iid']) + ); + $arr['mid'] = $arr['parent_mid'] = $info[0]['mid']; + $arr['created'] = $info[0]['created']; + $arr['edited'] = (($p['edited']) ? datetime_convert('UTC', 'UTC', $p['edited']) : datetime_convert()); + } else { + $arr['created'] = (($p['created']) ? datetime_convert('UTC', 'UTC', $p['created']) : datetime_convert()); + $arr['edited'] = datetime_convert('UTC', 'UTC', '0000-00-00 00:00:00'); + $arr['mid'] = $arr['parent_mid'] = item_message_id(); + } + $arr['title'] = $p['description']; + $arr['body'] = file_get_contents($p['path']); + $arr['term'] = $p['term']; // Not sure what this is supposed to be + + $arr['owner_xchan'] = get_observer_hash(); + $arr['author_xchan'] = (($p['author_xchan']) ? $p['author_xchan'] : get_observer_hash()); + $arr['mimetype'] = 'text/bbcode'; + + $remote_id = 0; + + $z = q("select * from iconfig where v = '%s' and k = '%s' and cat = 'system' limit 1", + dbesc($pagetitle), + dbesc($namespace) + ); + + $i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1", + dbesc($arr['mid']), + intval(local_channel()) + ); + if ($z && $i) { + $remote_id = $z[0]['id']; + $arr['id'] = $i[0]['id']; + // don't update if it has the same timestamp as the original + if ($arr['edited'] > $i[0]['edited']) + $x = item_store_update($arr, $execflag); + } else { + if (($i) && (intval($i[0]['item_deleted']))) { + // was partially deleted already, finish it off + q("delete from item where mid = '%s' and uid = %d", dbesc($arr['mid']), intval(local_channel()) + ); + } + $x = item_store($arr, $execflag); + } + if ($x['success']) { + $item_id = $x['item_id']; + update_remote_id($channel, $item_id, $arr['item_type'], $pagetitle, $namespace, $remote_id, $arr['mid']); + $p['import_success'] = 1; + } else { + $p['import_success'] = 0; + } + } + return $layouts; + +} } -- cgit v1.2.3 From 514ffb74aa8457d8dec5c0158550d93d1a18c072 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Thu, 14 Jul 2016 22:24:23 -0400 Subject: Refactored the scan and import functions to reduce redundant code and simplify logic. Import of pages, layouts, and blocks works. --- Zotlabs/Module/Webpages.php | 396 +++----------------------------------------- 1 file changed, 23 insertions(+), 373 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index 1c9586615..766b4fc09 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -231,383 +231,33 @@ class Webpages extends \Zotlabs\Web\Controller { $website = dirname($source) . '/' . $tmp_folder_name; $zip->extractTo($website); // change this to the correct site path $zip->close(); - @unlink($source); + @unlink($source); // delete the compressed file now that the content has been extracted - $hubsites = $this->import_website($website); + require_once('include/import.php'); + $elements = []; + $elements['pages'] = scan_webpage_elements($website, 'page'); + $elements['layouts'] = scan_webpage_elements($website, 'layout'); + $elements['blocks'] = scan_webpage_elements($website, 'block'); + $channel = \App::get_channel(); - $blocks = $this->import_blocks($channel, $hubsites['blocks']); - $pages = $this->import_pages($channel, $hubsites['pages']); - $layouts = $this->import_layouts($channel, $hubsites['layouts']); - if($blocks || $pages || $layouts) { // Without the if statement, the folder is deleted before the import_blocks function completes. - rrmdir($website); + // Import layout first so that pages that reference new layouts will find + // the mid of layout items in the database + foreach($elements['layouts'] as &$layout) { + $layout = import_webpage_element($layout, $channel, 'layout'); } - } - - + foreach($elements['pages'] as &$page) { + $page = import_webpage_element($page, $channel, 'page'); + } + foreach($elements['blocks'] as &$block) { + $block = import_webpage_element($block, $channel, 'block'); + } + + // Without the if statement below, the folder is deleted before the import completes. + if($elements) { + rrmdir($website); // Delete the temporary decompressed files + } + } } } - private function import_website($path) { - $hubsites = []; - $pages = []; - $blocks = []; - $layouts = []; - // Import pages - $dirtoscan = $path . '/pages/'; - if (is_dir($dirtoscan)) { - $dirlist = scandir($dirtoscan); - if ($dirlist) { - foreach ($dirlist as $element) { - if ($element === '.' || $element === '..') { - continue; - } - $folder = $dirtoscan . '/' . $element; - if (is_dir($folder)) { - $jsonfilepath = $folder . '/page.json'; - if (is_file($jsonfilepath)) { - $pagejson = json_decode(file_get_contents($jsonfilepath), true); - $pagejson['path'] = $folder . '/' . $pagejson['contentfile']; - if ($pagejson['contentfile'] === '') { - logger('Invalid page content file'); - return false; - } - $pagecontent = file_get_contents($folder . '/' . $pagejson['contentfile']); - if (!$pagecontent) { - logger('Failed to get file content for ' . $pagejson['contentfile']); - return false; - } - $pages[] = $pagejson; - } - } - } - } - } - $hubsites['pages'] = $pages; - // Import layouts - $dirtoscan = $path . '/layouts/'; - if (is_dir($dirtoscan)) { - $dirlist = scandir($dirtoscan); - if ($dirlist) { - foreach ($dirlist as $element) { - if ($element === '.' || $element === '..') { - continue; - } - $folder = $dirtoscan . '/' . $element; - if (is_dir($folder)) { - $jsonfilepath = $folder . '/layout.json'; - if (is_file($jsonfilepath)) { - $layoutjson = json_decode(file_get_contents($jsonfilepath), true); - $layoutjson['path'] = $folder . '/' . $layoutjson['contentfile']; - if ($layoutjson['contentfile'] === '') { - logger('Invalid layout content file'); - return false; - } - $layoutcontent = file_get_contents($folder . '/' . $layoutjson['contentfile']); - if (!$layoutcontent) { - logger('Failed to get file content for ' . $layoutjson['contentfile']); - return false; - } - $layouts[] = $layoutjson; - } - } - } - } - } - $hubsites['layouts'] = $layouts; - // Import blocks - $dirtoscan = $path . '/blocks/'; - if (is_dir($dirtoscan)) { - $dirlist = scandir($dirtoscan); - if ($dirlist) { - foreach ($dirlist as $element) { - if ($element === '.' || $element === '..') { - continue; - } - $folder = $dirtoscan . '/' . $element; - if (is_dir($folder)) { - $jsonfilepath = $folder . '/block.json'; - if (is_file($jsonfilepath)) { - $block = json_decode(file_get_contents($jsonfilepath), true); - $block['path'] = $folder . '/' . $block['contentfile']; - if ($block['contentfile'] === '') { - logger('Invalid block content file'); - return false; - } - $blockcontent = file_get_contents($folder . '/' . $block['contentfile']); - if (!$blockcontent) { - logger('Failed to get file content for ' . $block['contentfile']); - return false; - } - $blocks[] = $block; - } - } - } - } - } - $hubsites['blocks'] = $blocks; - return $hubsites; - } - - private function import_blocks($channel, $blocks) { - foreach ($blocks as &$b) { - - $arr = array(); - $arr['item_type'] = ITEM_TYPE_BLOCK; - $namespace = 'BUILDBLOCK'; - $arr['uid'] = $channel['channel_id']; - $arr['aid'] = $channel['channel_account_id']; - - $iid = q("select iid from iconfig where k = 'BUILDBLOCK' and v = '%s' and cat = 'system'", - dbesc($b['name']) - ); - if($iid) { - $iteminfo = q("select mid,created,edited from item where id = %d", - intval($iid[0]['iid']) - ); - $arr['mid'] = $arr['parent_mid'] = $iteminfo[0]['mid']; - $arr['created'] = $iteminfo[0]['created']; - $arr['edited'] = (($b['edited']) ? datetime_convert('UTC', 'UTC', $b['edited']) : datetime_convert()); - } else { - $arr['created'] = (($b['created']) ? datetime_convert('UTC', 'UTC', $b['created']) : datetime_convert()); - $arr['edited'] = datetime_convert('UTC', 'UTC', '0000-00-00 00:00:00'); - $arr['mid'] = $arr['parent_mid'] = item_message_id(); - } - $arr['title'] = $b['title']; - $arr['body'] = file_get_contents($b['path']); - $arr['owner_xchan'] = get_observer_hash(); - $arr['author_xchan'] = (($b['author_xchan']) ? $b['author_xchan'] : get_observer_hash()); - if(($b['mimetype'] === 'text/bbcode' || $b['mimetype'] === 'text/html' || - $b['mimetype'] === 'text/markdown' ||$b['mimetype'] === 'text/plain' || - $b['mimetype'] === 'application/x-pdl' ||$b['mimetype'] === 'application/x-php')) { - $arr['mimetype'] = $b['mimetype']; - } else { - $arr['mimetype'] = 'text/bbcode'; - } - - $pagetitle = $b['name']; - - // Verify ability to use html or php!!! - $execflag = false; - if ($arr['mimetype'] === 'application/x-php') { - $z = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id where channel_id = %d limit 1", intval(local_channel()) - ); - - if ($z && (($z[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE) || ($z[0]['channel_pageflags'] & PAGE_ALLOWCODE))) { - $execflag = true; - } - } - - $remote_id = 0; - - $z = q("select * from iconfig where v = '%s' and k = '%s' and cat = 'service' limit 1", dbesc($pagetitle), dbesc($namespace)); - - $i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1", dbesc($arr['mid']), intval(local_channel()) - ); - if ($z && $i) { - $remote_id = $z[0]['id']; - $arr['id'] = $i[0]['id']; - // don't update if it has the same timestamp as the original - if ($arr['edited'] > $i[0]['edited']) - $x = item_store_update($arr, $execflag); - } else { - if (($i) && (intval($i[0]['item_deleted']))) { - // was partially deleted already, finish it off - q("delete from item where mid = '%s' and uid = %d", dbesc($arr['mid']), intval(local_channel()) - ); - } - $x = item_store($arr, $execflag); - } - if ($x['success']) { - $item_id = $x['item_id']; - update_remote_id($channel, $item_id, $arr['item_type'], $pagetitle, $namespace, $remote_id, $arr['mid']); - $b['import_success'] = 1; - } else { - $b['import_success'] = 0; - } - } - return $blocks; -} - - -private function import_pages($channel, $pages) { - foreach ($pages as &$p) { - - $arr = array(); - $arr['item_type'] = ITEM_TYPE_WEBPAGE; - $namespace = 'WEBPAGE'; - $arr['uid'] = $channel['channel_id']; - $arr['aid'] = $channel['channel_account_id']; - - if($p['pagelink']) { - require_once('library/urlify/URLify.php'); - $pagetitle = strtolower(\URLify::transliterate($p['pagelink'])); - } - $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 - logger('hubsites plugin: $p[layout] = ' . $p['layout']); - if($p['layout'] !== '') { - $liid = q("select iid from iconfig where k = 'PDL' and v = '%s' and cat = 'system'", - dbesc($p['layout']) - ); - if($liid) { - $linfo = q("select mid from item where id = %d", - intval($liid[0]['iid']) - ); - logger('hubsites plugin: $linfo= ' . json_encode($linfo,true)); - $arr['layout_mid'] = $linfo[0]['mid']; - } - } - // See if the page already exists - $iid = q("select iid from iconfig where k = 'WEBPAGE' and v = '%s' and cat = 'system'", - dbesc($pagetitle) - ); - if($iid) { - // Get the existing page info - $pageinfo = q("select mid,layout_mid,created,edited from item where id = %d", - intval($iid[0]['iid']) - ); - $arr['mid'] = $arr['parent_mid'] = $pageinfo[0]['mid']; - $arr['created'] = $pageinfo[0]['created']; - $arr['edited'] = (($p['edited']) ? datetime_convert('UTC', 'UTC', $p['edited']) : datetime_convert()); - } else { - $arr['created'] = (($p['created']) ? datetime_convert('UTC', 'UTC', $p['created']) : datetime_convert()); - $arr['edited'] = datetime_convert('UTC', 'UTC', '0000-00-00 00:00:00'); - $arr['mid'] = $arr['parent_mid'] = item_message_id(); - } - $arr['title'] = $p['title']; - $arr['body'] = file_get_contents($p['path']); - $arr['term'] = $p['term']; // Not sure what this is supposed to be - - $arr['owner_xchan'] = get_observer_hash(); - $arr['author_xchan'] = (($p['author_xchan']) ? $p['author_xchan'] : get_observer_hash()); - if(($p['mimetype'] === 'text/bbcode' || $p['mimetype'] === 'text/html' || - $p['mimetype'] === 'text/markdown' ||$p['mimetype'] === 'text/plain' || - $p['mimetype'] === 'application/x-pdl' ||$p['mimetype'] === 'application/x-php')) { - $arr['mimetype'] = $p['mimetype']; - } else { - $arr['mimetype'] = 'text/bbcode'; - } - - // Verify ability to use html or php!!! - $execflag = false; - if ($arr['mimetype'] === 'application/x-php') { - $z = q("select account_id, account_roles, channel_pageflags from account left join channel on channel_account_id = account_id where channel_id = %d limit 1", intval(local_channel()) - ); - - if ($z && (($z[0]['account_roles'] & ACCOUNT_ROLE_ALLOWCODE) || ($z[0]['channel_pageflags'] & PAGE_ALLOWCODE))) { - $execflag = true; - } - } - - $remote_id = 0; - - $z = q("select * from iconfig where v = '%s' and k = '%s' and cat = 'system' limit 1", - dbesc($pagetitle), - dbesc($namespace) - ); - - $i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1", - dbesc($arr['mid']), - intval(local_channel()) - ); - if ($z && $i) { - $remote_id = $z[0]['id']; - $arr['id'] = $i[0]['id']; - // don't update if it has the same timestamp as the original - if ($arr['edited'] > $i[0]['edited']) - $x = item_store_update($arr, $execflag); - } else { - if (($i) && (intval($i[0]['item_deleted']))) { - // was partially deleted already, finish it off - q("delete from item where mid = '%s' and uid = %d", dbesc($arr['mid']), intval(local_channel()) - ); - } - logger('hubsites plugin: item_store= ' . json_encode($arr,true)); - $x = item_store($arr, $execflag); - } - if ($x['success']) { - $item_id = $x['item_id']; - update_remote_id($channel, $item_id, $arr['item_type'], $pagetitle, $namespace, $remote_id, $arr['mid']); - $p['import_success'] = 1; - } else { - $p['import_success'] = 0; - } - } - return $pages; - -} - -private function import_layouts($channel, $layouts) { - foreach ($layouts as &$p) { - - $arr = array(); - $arr['item_type'] = ITEM_TYPE_PDL; - $namespace = 'PDL'; - $arr['uid'] = $channel['channel_id']; - $arr['aid'] = $channel['channel_account_id']; - $pagetitle = $p['name']; - // See if the layout already exists - $iid = q("select iid from iconfig where k = 'PDL' and v = '%s' and cat = 'system'", - dbesc($pagetitle) - ); - if($iid) { - // Get the existing layout info - $info = q("select mid,layout_mid,created,edited from item where id = %d", - intval($iid[0]['iid']) - ); - $arr['mid'] = $arr['parent_mid'] = $info[0]['mid']; - $arr['created'] = $info[0]['created']; - $arr['edited'] = (($p['edited']) ? datetime_convert('UTC', 'UTC', $p['edited']) : datetime_convert()); - } else { - $arr['created'] = (($p['created']) ? datetime_convert('UTC', 'UTC', $p['created']) : datetime_convert()); - $arr['edited'] = datetime_convert('UTC', 'UTC', '0000-00-00 00:00:00'); - $arr['mid'] = $arr['parent_mid'] = item_message_id(); - } - $arr['title'] = $p['description']; - $arr['body'] = file_get_contents($p['path']); - $arr['term'] = $p['term']; // Not sure what this is supposed to be - - $arr['owner_xchan'] = get_observer_hash(); - $arr['author_xchan'] = (($p['author_xchan']) ? $p['author_xchan'] : get_observer_hash()); - $arr['mimetype'] = 'text/bbcode'; - - $remote_id = 0; - - $z = q("select * from iconfig where v = '%s' and k = '%s' and cat = 'system' limit 1", - dbesc($pagetitle), - dbesc($namespace) - ); - - $i = q("select id, edited, item_deleted from item where mid = '%s' and uid = %d limit 1", - dbesc($arr['mid']), - intval(local_channel()) - ); - if ($z && $i) { - $remote_id = $z[0]['id']; - $arr['id'] = $i[0]['id']; - // don't update if it has the same timestamp as the original - if ($arr['edited'] > $i[0]['edited']) - $x = item_store_update($arr, $execflag); - } else { - if (($i) && (intval($i[0]['item_deleted']))) { - // was partially deleted already, finish it off - q("delete from item where mid = '%s' and uid = %d", dbesc($arr['mid']), intval(local_channel()) - ); - } - $x = item_store($arr, $execflag); - } - if ($x['success']) { - $item_id = $x['item_id']; - update_remote_id($channel, $item_id, $arr['item_type'], $pagetitle, $namespace, $remote_id, $arr['mid']); - $p['import_success'] = 1; - } else { - $p['import_success'] = 0; - } - } - return $layouts; - -} - - } -- cgit v1.2.3 From ff2f599142348162b6459a02aa014c7dbca84f76 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sat, 16 Jul 2016 19:25:44 -0400 Subject: Postpone remote folder import until filesystem mirroring matures. --- Zotlabs/Module/Webpages.php | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index 766b4fc09..16ecb5386 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -211,7 +211,7 @@ class Webpages extends \Zotlabs\Web\Controller { function post() { - if(($_FILES) && array_key_exists('zip_file',$_FILES)) { + if(($_FILES) && array_key_exists('zip_file',$_FILES) && isset($_POST['w_upload'])) { $source = $_FILES["zip_file"]["tmp_name"]; $type = $_FILES["zip_file"]["type"]; $okay = false; @@ -256,7 +256,41 @@ class Webpages extends \Zotlabs\Web\Controller { if($elements) { rrmdir($website); // Delete the temporary decompressed files } - } + } + + return null; + } + + if (($_POST) && array_key_exists('url',$_POST) && isset($_POST['remotesubmit'])) { + $ret = []; + // Warning: Do not edit the following line. The first symbol is UTF-8 @ + $url = str_replace('@','@',notags(trim($_REQUEST['url']))); + if(! allowed_url($url)) { + $ret['message'] = t('Channel is blocked on this site.'); + return null; + } + + $h = @parse_url($url); + + if(! $h || !x($h, 'host') || !x($h, 'path')) { + return null; + } + if(substr($h['path'],-1,1) === '/') { + $h['path'] = substr($h['path'],0,-1); + } + if(substr($h['path'],0,1) === '/') { + $h['path'] = substr($h['path'],1); + } + $folders = explode('/', $h['path']); + if(!(array_shift($folders) === 'cloud')) { + return null; + } + $nick = array_shift($folders); + if(!$nick) { + return null; + } + return null; + } } -- cgit v1.2.3 From e7b853175154688d60d83ca5935650d1128973c6 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sat, 16 Jul 2016 21:02:13 -0400 Subject: Stash changes to merge from dev --- Zotlabs/Module/Webpages.php | 56 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 13 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index 16ecb5386..c78109cb7 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -261,18 +261,14 @@ class Webpages extends \Zotlabs\Web\Controller { return null; } - if (($_POST) && array_key_exists('url',$_POST) && isset($_POST['remotesubmit'])) { + if (($_POST) && array_key_exists('path',$_POST) && isset($_POST['cloudsubmit'])) { $ret = []; // Warning: Do not edit the following line. The first symbol is UTF-8 @ - $url = str_replace('@','@',notags(trim($_REQUEST['url']))); - if(! allowed_url($url)) { - $ret['message'] = t('Channel is blocked on this site.'); - return null; - } + $path = str_replace('@','@',notags(trim($_REQUEST['path']))); - $h = @parse_url($url); + $h = @parse_url($path); - if(! $h || !x($h, 'host') || !x($h, 'path')) { + if(! $h || !x($h, 'path')) { return null; } if(substr($h['path'],-1,1) === '/') { @@ -282,13 +278,47 @@ class Webpages extends \Zotlabs\Web\Controller { $h['path'] = substr($h['path'],1); } $folders = explode('/', $h['path']); - if(!(array_shift($folders) === 'cloud')) { - return null; + $f = array_shift($folders); + + $channel = \App::get_channel(); + $nick = \App::$profile['channel_address']; + //check to see if the absolute path was provided (/cloud/channelname/path/to/folder) + if(($f === 'cloud') ) { + $g = array_shift($folders); + if( $g !== $nick) { + // if nick does not follow "cloud", then the top level folder must be called "cloud" + // and the given path must be relative to "/cloud/channelname/". + $folders = array_unshift($f,array_unshift($g, $folders)); + } } - $nick = array_shift($folders); - if(!$nick) { - return null; + $clouddir = 'store/' . $nick . '/'; + $folder_path = $clouddir . implode('/', $folders); + + +// if(!(is_dir($folder_path) && is_readable($folder_path))) { +// logger('path is not readable: ' . $folder_path); +// return null; +// } + $subdir = '/'; + $valid = true; + while($folders && $valid && is_dir($clouddir . $subdir) && is_readable($clouddir . $subdir)) { + logger('hashed path: ' . $clouddir . $subdir); + $valid = false; + $f = array_shift($folders); + $items = array_diff(scandir($clouddir . $subdir), array('.', '..')); // hashed names + foreach($items as $item) { + $filename = find_filename_by_hash($channel['channel_id'], $item); + if($filename === $f) { + $subdir .= $item . '/'; + $valid = true; + } + } } + if(!$valid) { + logger('path is not valid: ' . $folder_path); + } + + return null; } -- cgit v1.2.3 From 6c5086a933451969b72890c9b703d6ee141f9d9f Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 17 Jul 2016 11:52:21 -0400 Subject: Added functions to check cloud files path and return path with hashed names --- Zotlabs/Module/Webpages.php | 56 ++++----------------------------------------- 1 file changed, 5 insertions(+), 51 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index c78109cb7..e85c49b44 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -262,61 +262,15 @@ class Webpages extends \Zotlabs\Web\Controller { } if (($_POST) && array_key_exists('path',$_POST) && isset($_POST['cloudsubmit'])) { - $ret = []; - // Warning: Do not edit the following line. The first symbol is UTF-8 @ - $path = str_replace('@','@',notags(trim($_REQUEST['path']))); - $h = @parse_url($path); - - if(! $h || !x($h, 'path')) { - return null; - } - if(substr($h['path'],-1,1) === '/') { - $h['path'] = substr($h['path'],0,-1); - } - if(substr($h['path'],0,1) === '/') { - $h['path'] = substr($h['path'],1); - } - $folders = explode('/', $h['path']); - $f = array_shift($folders); - $channel = \App::get_channel(); - $nick = \App::$profile['channel_address']; - //check to see if the absolute path was provided (/cloud/channelname/path/to/folder) - if(($f === 'cloud') ) { - $g = array_shift($folders); - if( $g !== $nick) { - // if nick does not follow "cloud", then the top level folder must be called "cloud" - // and the given path must be relative to "/cloud/channelname/". - $folders = array_unshift($f,array_unshift($g, $folders)); - } + $dirpath = get_dirpath_by_cloudpath($channel, $_POST['path']); + if(!$dirpath) { + notice( t('Invalid folder path.') . EOL); + return null; } - $clouddir = 'store/' . $nick . '/'; - $folder_path = $clouddir . implode('/', $folders); - + info( t('Valid folder path.') . EOL); -// if(!(is_dir($folder_path) && is_readable($folder_path))) { -// logger('path is not readable: ' . $folder_path); -// return null; -// } - $subdir = '/'; - $valid = true; - while($folders && $valid && is_dir($clouddir . $subdir) && is_readable($clouddir . $subdir)) { - logger('hashed path: ' . $clouddir . $subdir); - $valid = false; - $f = array_shift($folders); - $items = array_diff(scandir($clouddir . $subdir), array('.', '..')); // hashed names - foreach($items as $item) { - $filename = find_filename_by_hash($channel['channel_id'], $item); - if($filename === $f) { - $subdir .= $item . '/'; - $valid = true; - } - } - } - if(!$valid) { - logger('path is not valid: ' . $folder_path); - } return null; -- cgit v1.2.3 From d6b28cdc575aba40c6a7861d4c2031d844a848d9 Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 17 Jul 2016 16:05:26 -0400 Subject: Importing webpage elements from manually entered cloud file path work. All detected elements are automatically imported. --- Zotlabs/Module/Webpages.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index e85c49b44..9a79478a9 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -269,9 +269,28 @@ class Webpages extends \Zotlabs\Web\Controller { notice( t('Invalid folder path.') . EOL); return null; } - info( t('Valid folder path.') . EOL); - + require_once('include/import.php'); + $elements = []; + $elements['pages'] = scan_webpage_elements($_POST['path'], 'page', true); + $elements['layouts'] = scan_webpage_elements($_POST['path'], 'layout', true); + $elements['blocks'] = scan_webpage_elements($_POST['path'], 'block', true); + logger('elements: ' . json_encode($elements)); + if(!(empty($elements['pages']) && empty($elements['blocks']) && empty($elements['layouts']))) { + info( t('Webpages elements detected.') . EOL); + } + // Import layout first so that pages that reference new layouts will find + // the mid of layout items in the database + foreach($elements['layouts'] as &$layout) { + $layout = import_webpage_element($layout, $channel, 'layout'); + } + foreach($elements['pages'] as &$page) { + $page = import_webpage_element($page, $channel, 'page'); + } + foreach($elements['blocks'] as &$block) { + $block = import_webpage_element($block, $channel, 'block'); + } + return null; -- cgit v1.2.3 From 0c7ad924a846eef183ca442be837b69a6014075a Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Wed, 20 Jul 2016 05:33:40 -0400 Subject: Starting to make the import selection page after element scan. --- Zotlabs/Module/Webpages.php | 178 ++++++++++++++++++++++++-------------------- 1 file changed, 98 insertions(+), 80 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index 9a79478a9..ab03b0a37 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -211,90 +211,108 @@ class Webpages extends \Zotlabs\Web\Controller { function post() { - if(($_FILES) && array_key_exists('zip_file',$_FILES) && isset($_POST['w_upload'])) { - $source = $_FILES["zip_file"]["tmp_name"]; - $type = $_FILES["zip_file"]["type"]; - $okay = false; - $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed'); - foreach ($accepted_types as $mime_type) { - if ($mime_type == $type) { - $okay = true; - break; - } - } - if(!$okay) { - json_return_and_die(array('message' => 'Invalid file MIME type')); - } - $zip = new \ZipArchive(); - if ($zip->open($source) === true) { - $tmp_folder_name = random_string(5); - $website = dirname($source) . '/' . $tmp_folder_name; - $zip->extractTo($website); // change this to the correct site path - $zip->close(); - @unlink($source); // delete the compressed file now that the content has been extracted + $action = $_REQUEST['action']; + if( $action ){ + switch ($action) { + case 'scan': + + // the state of this variable tracks whether website files have been scanned (null, true, false) + $cloud = null; + + // Website files are to be imported from an uploaded zip file + if(($_FILES) && array_key_exists('zip_file',$_FILES) && isset($_POST['w_upload'])) { + $source = $_FILES["zip_file"]["tmp_name"]; + $type = $_FILES["zip_file"]["type"]; + $okay = false; + $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed'); + foreach ($accepted_types as $mime_type) { + if ($mime_type == $type) { + $okay = true; + break; + } + } + if(!$okay) { + json_return_and_die(array('message' => 'Invalid file MIME type')); + } + $zip = new \ZipArchive(); + if ($zip->open($source) === true) { + $tmp_folder_name = random_string(5); + $website = dirname($source) . '/' . $tmp_folder_name; + $zip->extractTo($website); // change this to the correct site path + $zip->close(); + @unlink($source); // delete the compressed file now that the content has been extracted - require_once('include/import.php'); - $elements = []; - $elements['pages'] = scan_webpage_elements($website, 'page'); - $elements['layouts'] = scan_webpage_elements($website, 'layout'); - $elements['blocks'] = scan_webpage_elements($website, 'block'); - - $channel = \App::get_channel(); - // Import layout first so that pages that reference new layouts will find - // the mid of layout items in the database - foreach($elements['layouts'] as &$layout) { - $layout = import_webpage_element($layout, $channel, 'layout'); - } - foreach($elements['pages'] as &$page) { - $page = import_webpage_element($page, $channel, 'page'); - } - foreach($elements['blocks'] as &$block) { - $block = import_webpage_element($block, $channel, 'block'); - } - - // Without the if statement below, the folder is deleted before the import completes. - if($elements) { - rrmdir($website); // Delete the temporary decompressed files - } - } - - return null; - } - - if (($_POST) && array_key_exists('path',$_POST) && isset($_POST['cloudsubmit'])) { + $cloud = false; + } else { + notice( t('Error opening zip file') . EOL); + return null; + } + } - $channel = \App::get_channel(); - $dirpath = get_dirpath_by_cloudpath($channel, $_POST['path']); - if(!$dirpath) { - notice( t('Invalid folder path.') . EOL); - return null; - } - require_once('include/import.php'); - $elements = []; - $elements['pages'] = scan_webpage_elements($_POST['path'], 'page', true); - $elements['layouts'] = scan_webpage_elements($_POST['path'], 'layout', true); - $elements['blocks'] = scan_webpage_elements($_POST['path'], 'block', true); - logger('elements: ' . json_encode($elements)); - if(!(empty($elements['pages']) && empty($elements['blocks']) && empty($elements['layouts']))) { - info( t('Webpages elements detected.') . EOL); - } - - // Import layout first so that pages that reference new layouts will find - // the mid of layout items in the database - foreach($elements['layouts'] as &$layout) { - $layout = import_webpage_element($layout, $channel, 'layout'); - } - foreach($elements['pages'] as &$page) { - $page = import_webpage_element($page, $channel, 'page'); - } - foreach($elements['blocks'] as &$block) { - $block = import_webpage_element($block, $channel, 'block'); - } + // Website files are to be imported from the channel cloud files + if (($_POST) && array_key_exists('path',$_POST) && isset($_POST['cloudsubmit'])) { - - return null; - + $channel = \App::get_channel(); + $dirpath = get_dirpath_by_cloudpath($channel, $_POST['path']); + if(!$dirpath) { + notice( t('Invalid folder path.') . EOL); + return null; + } + $cloud = true; + + } + + // If the website files were uploaded or specified in the cloud files, then $cloud + // should be either true or false + if ($cloud !== null) { + require_once('include/import.php'); + $elements = []; + $elements['pages'] = scan_webpage_elements($_POST['path'], 'page', $cloud); + $elements['layouts'] = scan_webpage_elements($_POST['path'], 'layout', $cloud); + $elements['blocks'] = scan_webpage_elements($_POST['path'], 'block', $cloud); + + if(!(empty($elements['pages']) && empty($elements['blocks']) && empty($elements['layouts']))) { + info( t('Webpages elements detected.') . EOL); + + $o .= replace_macros(get_markup_template('webpage_import.tpl'), array( + '$title' => t('Import Webpage Elements'), + )); + + return $o; + + } else { + notice( t('No webpage elements detected.') . EOL); + } + // Import layout first so that pages that reference new layouts will find + // the mid of layout items in the database + foreach($elements['layouts'] as &$layout) { + $layout = import_webpage_element($layout, $channel, 'layout'); + } + foreach($elements['pages'] as &$page) { + $page = import_webpage_element($page, $channel, 'page'); + } + foreach($elements['blocks'] as &$block) { + $block = import_webpage_element($block, $channel, 'block'); + } + + } + + // If the website elements were imported from a zip file, delete the temporary decompressed files + if ($cloud === false && $website) { + rrmdir($website); // Delete the temporary decompressed files + } + + break; + case 'import': + break; + default : + break; + } } + + + + } } -- cgit v1.2.3 From 32366284a8372a4d5c7798b3ee3b238a614915ab Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sat, 30 Jul 2016 09:56:47 -0400 Subject: Import element selection page added to allow selective importing. --- Zotlabs/Module/Webpages.php | 113 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 22 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index ab03b0a37..ee9633243 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -45,7 +45,29 @@ class Webpages extends \Zotlabs\Web\Controller { $observer = \App::get_observer(); $channel = \App::get_channel(); - + + switch ($_SESSION['action']) { + case 'import': + $_SESSION['action'] = null; + $o .= replace_macros(get_markup_template('webpage_import.tpl'), array( + '$title' => t('Import Webpage Elements'), + '$action' => 'import', + '$pages' => $_SESSION['pages'], + '$layouts' => $_SESSION['layouts'], + '$blocks' => $_SESSION['blocks'], + )); + //logger('webpage_import.tpl: ' . $o); + return $o; + + case 'importselected': + $_SESSION['action'] = null; + break; + default : + $_SESSION['action'] = null; + break; + } + + if(\App::$is_sys && is_site_admin()) { $sys = get_sys_channel(); if($sys && intval($sys['channel_id'])) { @@ -270,29 +292,15 @@ class Webpages extends \Zotlabs\Web\Controller { $elements['pages'] = scan_webpage_elements($_POST['path'], 'page', $cloud); $elements['layouts'] = scan_webpage_elements($_POST['path'], 'layout', $cloud); $elements['blocks'] = scan_webpage_elements($_POST['path'], 'block', $cloud); - + $_SESSION['blocks'] = $elements['blocks']; + $_SESSION['layouts'] = $elements['layouts']; + $_SESSION['pages'] = $elements['pages']; if(!(empty($elements['pages']) && empty($elements['blocks']) && empty($elements['layouts']))) { info( t('Webpages elements detected.') . EOL); - - $o .= replace_macros(get_markup_template('webpage_import.tpl'), array( - '$title' => t('Import Webpage Elements'), - )); - - return $o; - + $_SESSION['action'] = 'import'; } else { notice( t('No webpage elements detected.') . EOL); - } - // Import layout first so that pages that reference new layouts will find - // the mid of layout items in the database - foreach($elements['layouts'] as &$layout) { - $layout = import_webpage_element($layout, $channel, 'layout'); - } - foreach($elements['pages'] as &$page) { - $page = import_webpage_element($page, $channel, 'page'); - } - foreach($elements['blocks'] as &$block) { - $block = import_webpage_element($block, $channel, 'block'); + $_SESSION['action'] = null; } } @@ -303,8 +311,69 @@ class Webpages extends \Zotlabs\Web\Controller { } break; - case 'import': - break; + + case 'importselected': + require_once('include/import.php'); + $channel = \App::get_channel(); + + // Import layout first so that pages that reference new layouts will find + // the mid of layout items in the database + + // Obtain the user-selected layouts to import and import them + $checkedlayouts = $_POST['layout']; + $layouts = []; + if (!empty($checkedlayouts)) { + foreach ($checkedlayouts as $name) { + foreach ($_SESSION['layouts'] as &$layout) { + if ($layout['name'] === $name) { + $layout['import'] = 1; + $layoutstoimport[] = $layout; + } + } + } + foreach ($layoutstoimport as $elementtoimport) { + $layouts[] = import_webpage_element($elementtoimport, $channel, 'layout'); + } + } + $_SESSION['import_layouts'] = $layouts; + + // Obtain the user-selected blocks to import and import them + $checkedblocks = $_POST['block']; + $blocks = []; + if (!empty($checkedblocks)) { + foreach ($checkedblocks as $name) { + foreach ($_SESSION['blocks'] as &$block) { + if ($block['name'] === $name) { + $block['import'] = 1; + $blockstoimport[] = $block; + } + } + } + foreach ($blockstoimport as $elementtoimport) { + $blocks[] = import_webpage_element($elementtoimport, $channel, 'block'); + } + } + $_SESSION['import_blocks'] = $blocks; + + // Obtain the user-selected pages to import and import them + $checkedpages = $_POST['page']; + $pages = []; + if (!empty($checkedpages)) { + foreach ($checkedpages as $pagelink) { + foreach ($_SESSION['pages'] as &$page) { + if ($page['pagelink'] === $pagelink) { + $page['import'] = 1; + $pagestoimport[] = $page; + } + } + } + foreach ($pagestoimport as $elementtoimport) { + $pages[] = import_webpage_element($elementtoimport, $channel, 'page'); + } + } + $_SESSION['import_pages'] = $pages; + break; + default : break; } -- cgit v1.2.3 From 7c475575549161d465189fa5e726739f4a4ac76c Mon Sep 17 00:00:00 2001 From: Andrew Manning Date: Sun, 31 Jul 2016 07:30:25 -0400 Subject: Improved UI. Removed logger statements. --- Zotlabs/Module/Webpages.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Webpages.php b/Zotlabs/Module/Webpages.php index ee9633243..d3199f223 100644 --- a/Zotlabs/Module/Webpages.php +++ b/Zotlabs/Module/Webpages.php @@ -51,12 +51,12 @@ class Webpages extends \Zotlabs\Web\Controller { $_SESSION['action'] = null; $o .= replace_macros(get_markup_template('webpage_import.tpl'), array( '$title' => t('Import Webpage Elements'), + '$importbtn' => t('Import selected'), '$action' => 'import', '$pages' => $_SESSION['pages'], '$layouts' => $_SESSION['layouts'], '$blocks' => $_SESSION['blocks'], )); - //logger('webpage_import.tpl: ' . $o); return $o; case 'importselected': @@ -254,7 +254,8 @@ class Webpages extends \Zotlabs\Web\Controller { } } if(!$okay) { - json_return_and_die(array('message' => 'Invalid file MIME type')); + notice( t('Invalid file type.') . EOL); + return; } $zip = new \ZipArchive(); if ($zip->open($source) === true) { @@ -263,7 +264,6 @@ class Webpages extends \Zotlabs\Web\Controller { $zip->extractTo($website); // change this to the correct site path $zip->close(); @unlink($source); // delete the compressed file now that the content has been extracted - $cloud = false; } else { notice( t('Error opening zip file') . EOL); @@ -289,14 +289,19 @@ class Webpages extends \Zotlabs\Web\Controller { if ($cloud !== null) { require_once('include/import.php'); $elements = []; - $elements['pages'] = scan_webpage_elements($_POST['path'], 'page', $cloud); - $elements['layouts'] = scan_webpage_elements($_POST['path'], 'layout', $cloud); - $elements['blocks'] = scan_webpage_elements($_POST['path'], 'block', $cloud); + if($cloud) { + $path = $_POST['path']; + } else { + $path = $website; + } + $elements['pages'] = scan_webpage_elements($path, 'page', $cloud); + $elements['layouts'] = scan_webpage_elements($path, 'layout', $cloud); + $elements['blocks'] = scan_webpage_elements($path, 'block', $cloud); $_SESSION['blocks'] = $elements['blocks']; $_SESSION['layouts'] = $elements['layouts']; $_SESSION['pages'] = $elements['pages']; if(!(empty($elements['pages']) && empty($elements['blocks']) && empty($elements['layouts']))) { - info( t('Webpages elements detected.') . EOL); + //info( t('Webpages elements detected.') . EOL); $_SESSION['action'] = 'import'; } else { notice( t('No webpage elements detected.') . EOL); @@ -306,7 +311,7 @@ class Webpages extends \Zotlabs\Web\Controller { } // If the website elements were imported from a zip file, delete the temporary decompressed files - if ($cloud === false && $website) { + if ($cloud === false && $website && $elements) { rrmdir($website); // Delete the temporary decompressed files } @@ -372,7 +377,10 @@ class Webpages extends \Zotlabs\Web\Controller { } } $_SESSION['import_pages'] = $pages; - break; + if(!(empty($_SESSION['import_pages']) && empty($_SESSION['import_blocks']) && empty($_SESSION['import_layouts']))) { + info( t('Import complete.') . EOL); + } + break; default : break; -- cgit v1.2.3 From 9b9621e10d669e7d1bc18a781c40bfc5687330c3 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Sun, 31 Jul 2016 20:14:25 -0700 Subject: add a few more path macros to portable menu elements (channelurl, pageurl, storeurl and baseurl) --- Zotlabs/Module/Impel.php | 4 ++++ Zotlabs/Module/Menu.php | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Impel.php b/Zotlabs/Module/Impel.php index 735c311d0..c1bf45a77 100644 --- a/Zotlabs/Module/Impel.php +++ b/Zotlabs/Module/Impel.php @@ -88,7 +88,11 @@ class Impel extends \Zotlabs\Web\Controller { foreach($j['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('[storeurl]',z_root() . '/store/' . $channel['channel_address'],$it['link']); $mitem['mitem_link'] = str_replace('[baseurl]',z_root(),$it['link']); + $mitem['mitem_desc'] = escape_tags($it['desc']); $mitem['mitem_order'] = intval($it['order']); if(is_array($it['flags'])) { diff --git a/Zotlabs/Module/Menu.php b/Zotlabs/Module/Menu.php index e98053f8c..1dec65c1f 100644 --- a/Zotlabs/Module/Menu.php +++ b/Zotlabs/Module/Menu.php @@ -65,7 +65,7 @@ class Menu extends \Zotlabs\Web\Controller { - function get() { + function get() { $uid = local_channel(); @@ -81,7 +81,7 @@ class Menu extends \Zotlabs\Web\Controller { if(argc() == 1) { - + $channel = (($sys) ? $sys : \App::get_channel()); // list menus $x = menu_list($uid); @@ -89,7 +89,7 @@ class Menu extends \Zotlabs\Web\Controller { for($y = 0; $y < count($x); $y ++) { $m = menu_fetch($x[$y]['menu_name'],$uid,get_observer_hash()); if($m) - $x[$y]['element'] = '[element]' . base64url_encode(json_encode(menu_element($m))) . '[/element]'; + $x[$y]['element'] = '[element]' . base64url_encode(json_encode(menu_element($channel,$m))) . '[/element]'; $x[$y]['bookmark'] = (($x[$y]['menu_flags'] & MENU_BOOKMARK) ? true : false); } } -- cgit v1.2.3 From 3d0c90cbc5b756c6d54c4d41a136c0a38e67b013 Mon Sep 17 00:00:00 2001 From: redmatrix Date: Sun, 31 Jul 2016 23:17:07 -0700 Subject: allow individual permissions on atokens --- Zotlabs/Module/Settings.php | 64 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 5 deletions(-) (limited to 'Zotlabs/Module') diff --git a/Zotlabs/Module/Settings.php b/Zotlabs/Module/Settings.php index af3a25c60..48ab6b8bf 100644 --- a/Zotlabs/Module/Settings.php +++ b/Zotlabs/Module/Settings.php @@ -164,7 +164,23 @@ class Settings extends \Zotlabs\Web\Controller { dbesc($expires) ); } + + $atoken_xchan = substr($channel['channel_hash'],0,16) . '.' . $name; + + $all_perms = \Zotlabs\Access\Permissions::Perms(); + + if($all_perms) { + foreach($all_perms as $perm => $desc) { + if(array_key_exists('perms_' . $perm, $_POST)) { + set_abconfig($channel['channel_id'],$atoken_xchan,'my_perms',$perm,intval($_POST['perms_' . $perm])); + } + else { + set_abconfig($channel['channel_id'],$atoken_xchan,'my_perms',$perm,0); + } + } + } + info( t('Token saved.') . EOL); return; } @@ -270,7 +286,7 @@ class Settings extends \Zotlabs\Web\Controller { $email = ((x($_POST,'email')) ? trim(notags($_POST['email'])) : ''); $account = \App::get_account(); if($email != $account['account_email']) { - if(! valid_email($email)) + if(! valid_email($email)) $errs[] = t('Not valid email.'); $adm = trim(get_config('system','admin_email')); if(($adm) && (strcasecmp($email,$adm) == 0)) { @@ -378,7 +394,7 @@ class Settings extends \Zotlabs\Web\Controller { intval(local_channel()) ); } - else { + else { $role_permissions = \Zotlabs\Access\PermissionRoles::role_perms($_POST['permissions_role']); if(! $role_permissions) { notice('Permissions category could not be found.'); @@ -773,9 +789,10 @@ class Settings extends \Zotlabs\Web\Controller { intval(local_channel()) ); - if($atoken) + if($atoken) { $atoken = $atoken[0]; - + $atoken_xchan = substr($channel['channel_hash'],0,16) . '.' . $atoken['atoken_name']; + } if($atoken && argc() > 3 && argv(3) === 'drop') { $r = q("delete from atoken where atoken_id = %d", intval($id) @@ -786,10 +803,40 @@ class Settings extends \Zotlabs\Web\Controller { intval(local_channel()) ); - $desc = t('Use this form to create temporary access identifiers to share things with non-members. These identities may be used in Access Control Lists and visitors may login using these credentials to access the private content.'); + $desc = t('Use this form to create temporary access identifiers to share things with non-members. These identities may be used in Access Control Lists and visitors may login using these credentials to access private content.'); $desc2 = t('You may also provide dropbox style access links to friends and associates by adding the Login Password to any specific site URL as shown. Examples:'); + $global_perms = \Zotlabs\Access\Permissions::Perms(); + + $existing = get_all_perms(local_channel(),(($atoken_xchan) ? $atoken_xchan : '')); + + if($atoken_xchan) { + $theirs = q("select * from abconfig where chan = %d and xchan = '%s' and cat = 'their_perms'", + intval(local_channel()), + dbesc($atoken_xchan) + ); + $their_perms = array(); + if($theirs) { + foreach($theirs as $t) { + $their_perms[$t['k']] = $t['v']; + } + } + } + foreach($global_perms as $k => $v) { + $thisperm = get_abconfig(local_channel(),$contact['abook_xchan'],'my_perms',$k); +//fixme + + $checkinherited = \Zotlabs\Access\PermissionLimits::Get(local_channel(),$k); + + if($existing[$k]) + $thisperm = "1"; + + $perms[] = array('perms_' . $k, $v, ((array_key_exists($k,$their_perms)) ? intval($their_perms[$k]) : ''),$thisperm, 1, (($checkinherited & PERMS_SPECIFIC) ? '' : '1'), '', $checkinherited); + } + + + $tpl = get_markup_template("settings_tokens.tpl"); $o .= replace_macros($tpl, array( '$form_security_token' => get_form_security_token("settings_tokens"), @@ -803,6 +850,13 @@ class Settings extends \Zotlabs\Web\Controller { '$name' => array('name', t('Login Name') . ' *', (($atoken) ? $atoken['atoken_name'] : ''),''), '$token'=> array('token', t('Login Password') . ' *',(($atoken) ? $atoken['atoken_token'] : autoname(8)), ''), '$expires'=> array('expires', t('Expires (yyyy-mm-dd)'), (($atoken['atoken_expires'] && $atoken['atoken_expires'] != NULL_DATE) ? datetime_convert('UTC',date_default_timezone_get(),$atoken['atoken_expires']) : ''), ''), + '$them' => t('Their Settings'), + '$me' => t('My Settings'), + '$perms' => $perms, + '$inherited' => t('inherited'), + '$notself' => '1', + '$permlbl' => t('Individual Permissions'), + '$permnote' => t('Some permissions may be inherited from your channel\'s privacy settings, which have higher priority than individual settings. You can not change those settings here.'), '$submit' => t('Submit') )); return $o; -- cgit v1.2.3