aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib
diff options
context:
space:
mode:
authorMario Vavti <mario@mariovavti.com>2017-03-08 09:39:46 +0100
committerMario Vavti <mario@mariovavti.com>2017-03-08 09:39:46 +0100
commitbc2b948f1f6e62b1c277a4042200bb6678956f3f (patch)
tree8586c30e495607eee23f16c0aad40974f0711275 /Zotlabs/Lib
parent23e3e2c50499fab52769929a448e73012fd915af (diff)
parentff9442474d07cce24c8f66db39ec34471c3874a2 (diff)
downloadvolse-hubzilla-2.2.tar.gz
volse-hubzilla-2.2.tar.bz2
volse-hubzilla-2.2.zip
Merge branch 2.2RC2.2
Diffstat (limited to 'Zotlabs/Lib')
-rw-r--r--Zotlabs/Lib/Apps.php144
-rw-r--r--Zotlabs/Lib/NativeWiki.php210
-rw-r--r--Zotlabs/Lib/NativeWikiPage.php664
-rw-r--r--Zotlabs/Lib/Permcat.php146
-rw-r--r--Zotlabs/Lib/ThreadItem.php10
5 files changed, 1140 insertions, 34 deletions
diff --git a/Zotlabs/Lib/Apps.php b/Zotlabs/Lib/Apps.php
index ac03e11e1..0ca2f7a99 100644
--- a/Zotlabs/Lib/Apps.php
+++ b/Zotlabs/Lib/Apps.php
@@ -38,6 +38,7 @@ class Apps {
if(plugin_is_installed($plugin)) {
$x = self::parse_app_description($f,$translate);
if($x) {
+ $x['plugin'] = $plugin;
$ret[] = $x;
}
}
@@ -54,7 +55,6 @@ class Apps {
return;
$apps = self::get_system_apps(false);
-
self::$installed_system_apps = q("select * from app where app_system = 1 and app_channel = %d",
intval(local_channel())
);
@@ -102,11 +102,13 @@ class Apps {
foreach(self::$installed_system_apps as $iapp) {
if($iapp['app_id'] == hash('whirlpool',$app['name'])) {
$notfound = false;
- if($iapp['app_version'] != $app['version']) {
+ if(($iapp['app_version'] != $app['version'])
+ || ($app['plugin'] && (! $iapp['app_plugin']))) {
return intval($iapp['app_id']);
}
}
}
+
return $notfound;
}
@@ -144,8 +146,11 @@ class Apps {
$ret['type'] = 'system';
foreach($ret as $k => $v) {
- if(strpos($v,'http') === 0)
- $ret[$k] = zid($v);
+ if(strpos($v,'http') === 0) {
+ if(! (local_channel() && strpos($v,z_root()) === 0)) {
+ $ret[$k] = zid($v);
+ }
+ }
}
if(array_key_exists('desc',$ret))
@@ -157,6 +162,8 @@ class Apps {
if(array_key_exists('version',$ret))
$ret['version'] = str_replace(array('\'','"'),array('&#39;','&dquot;'),$ret['version']);
+ if(array_key_exists('categories',$ret))
+ $ret['categories'] = str_replace(array('\'','"'),array('&#39;','&dquot;'),$ret['categories']);
if(array_key_exists('requires',$ret)) {
$requires = explode(',',$ret['requires']);
@@ -238,9 +245,9 @@ class Apps {
'Profile Photo' => t('Profile Photo')
);
- if(array_key_exists($arr['name'],$apps))
+ if(array_key_exists($arr['name'],$apps)) {
$arr['name'] = $apps[$arr['name']];
-
+ }
}
@@ -255,6 +262,7 @@ class Apps {
* list: normal mode for viewing an app on the app page
* no buttons are shown
* edit: viewing the app page in editing mode provides a delete button
+ * nav: render apps for app-bin
*/
$installed = false;
@@ -267,14 +275,20 @@ class Apps {
self::translate_system_apps($papp);
+ if(($papp['plugin']) && (! plugin_is_installed($papp['plugin'])))
+ return '';
+
$papp['papp'] = self::papp_encode($papp);
if(! strstr($papp['url'],'://'))
$papp['url'] = z_root() . ((strpos($papp['url'],'/') === 0) ? '' : '/') . $papp['url'];
foreach($papp as $k => $v) {
- if(strpos($v,'http') === 0 && $k != 'papp')
- $papp[$k] = zid($v);
+ if(strpos($v,'http') === 0 && $k != 'papp') {
+ if(! (local_channel() && strpos($v,z_root()) === 0)) {
+ $papp[$k] = zid($v);
+ }
+ }
if($k === 'desc')
$papp['desc'] = str_replace(array('\'','"'),array('&#39;','&dquot;'),$papp['desc']);
@@ -332,14 +346,23 @@ class Apps {
}
$install_action = (($installed) ? t('Update') : t('Install'));
+ $icon = ((strpos($papp['photo'],'icon:') === 0) ? substr($papp['photo'],5) : '');
return replace_macros(get_markup_template('app.tpl'),array(
'$app' => $papp,
+ '$icon' => $icon,
'$hosturl' => $hosturl,
'$purchase' => (($papp['page'] && (! $installed)) ? t('Purchase') : ''),
'$install' => (($hosturl && $mode == 'view') ? $install_action : ''),
'$edit' => ((local_channel() && $installed && $mode == 'edit') ? t('Edit') : ''),
- '$delete' => ((local_channel() && $installed && $mode == 'edit') ? t('Delete') : '')
+ '$delete' => ((local_channel() && $installed && $mode == 'edit') ? t('Delete') : ''),
+ '$undelete' => ((local_channel() && $installed && $mode == 'edit') ? t('Undelete') : ''),
+ '$deleted' => $papp['deleted'],
+ '$feature' => (($papp['embed']) ? false : true),
+ '$featured' => ((strpos($papp['categories'], 'nav_featured_app') === false) ? false : true),
+ '$navapps' => (($mode == 'nav') ? true : false),
+ '$add' => t('Add to app-tray'),
+ '$remove' => t('Remove from app-tray')
));
}
@@ -382,36 +405,82 @@ class Apps {
intval($uid)
);
if($x) {
- $x[0]['app_deleted'] = 1;
- q("delete from term where otype = %d and oid = %d",
- intval(TERM_OBJ_APP),
- intval($x[0]['id'])
- );
- if($x[0]['app_system']) {
- $r = q("update app set app_deleted = 1 where app_id = '%s' and app_channel = %d",
- dbesc($app['guid']),
- intval($uid)
+ if(! intval($x[0]['app_deleted'])) {
+ $x[0]['app_deleted'] = 1;
+ q("delete from term where otype = %d and oid = %d",
+ intval(TERM_OBJ_APP),
+ intval($x[0]['id'])
);
+ if($x[0]['app_system']) {
+ $r = q("update app set app_deleted = 1 where app_id = '%s' and app_channel = %d",
+ dbesc($app['guid']),
+ intval($uid)
+ );
+ }
+ else {
+ $r = q("delete from app where app_id = '%s' and app_channel = %d",
+ dbesc($app['guid']),
+ intval($uid)
+ );
+
+ // we don't sync system apps - they may be completely different on the other system
+ build_sync_packet($uid,array('app' => $x));
+ }
}
else {
- $r = q("delete from app where app_id = '%s' and app_channel = %d",
+ self::app_undestroy($uid,$app);
+ }
+ }
+ }
+ }
+
+ static public function app_undestroy($uid,$app) {
+
+ // undelete a system app
+
+ if($uid && $app['guid']) {
+
+ $x = q("select * from app where app_id = '%s' and app_channel = %d limit 1",
+ dbesc($app['guid']),
+ intval($uid)
+ );
+ if($x) {
+ if($x[0]['app_system']) {
+ $r = q("update app set app_deleted = 0 where app_id = '%s' and app_channel = %d",
dbesc($app['guid']),
intval($uid)
);
-
- // we don't sync system apps - they may be completely different on the other system
- build_sync_packet($uid,array('app' => $x));
}
}
}
}
+ static public function app_feature($uid,$app) {
+ $r = q("select id from app where app_id = '%s' and app_channel = %d limit 1",
+ dbesc($app['guid']),
+ intval($uid)
+ );
+
+ $x = q("select * from term where otype = %d and oid = %d and term = 'nav_featured_app' limit 1",
+ intval(TERM_OBJ_APP),
+ intval($r[0]['id'])
+ );
+
+ if($x) {
+ q("delete from term where otype = %d and oid = %d and term = 'nav_featured_app'",
+ intval(TERM_OBJ_APP),
+ intval($x[0]['oid'])
+ );
+ }
+ else {
+ store_item_tag($uid,$r[0]['id'],TERM_OBJ_APP,TERM_CATEGORY,'nav_featured_app',escape_tags(z_root() . '/apps/?f=&cat=nav_featured_app'));
+ }
+ }
static public function app_installed($uid,$app) {
- $r = q("select id from app where app_id = '%s' and app_version = '%s' and app_channel = %d limit 1",
+ $r = q("select id from app where app_id = '%s' and app_channel = %d limit 1",
dbesc((array_key_exists('guid',$app)) ? $app['guid'] : ''),
- dbesc((array_key_exists('version',$app)) ? $app['version'] : ''),
intval($uid)
);
return(($r) ? true : false);
@@ -421,7 +490,7 @@ class Apps {
static public function app_list($uid, $deleted = false, $cat = '') {
if($deleted)
- $sql_extra = " and app_deleted = 1 ";
+ $sql_extra = "";
else
$sql_extra = " and app_deleted = 0 ";
@@ -445,6 +514,7 @@ class Apps {
$r = q("select * from app where app_channel = %d $sql_extra order by app_name asc",
intval($uid)
);
+
if($r) {
for($x = 0; $x < count($r); $x ++) {
if(! $r[$x]['app_system'])
@@ -455,6 +525,7 @@ class Apps {
);
}
}
+
return($r);
}
@@ -467,7 +538,7 @@ class Apps {
static public function app_store($arr) {
- // logger('app_store: ' . print_r($arr,true));
+ //logger('app_store: ' . print_r($arr,true));
$darray = array();
$ret = array('success' => false);
@@ -478,7 +549,7 @@ class Apps {
if((! $darray['app_url']) || (! $darray['app_channel']))
return $ret;
- if($arr['photo'] && ! strstr($arr['photo'],z_root())) {
+ if($arr['photo'] && (strpos($arr['photo'],'icon:') !== 0) && (! strstr($arr['photo'],z_root()))) {
$x = import_xchan_photo($arr['photo'],get_observer_hash(),true);
$arr['photo'] = $x[1];
}
@@ -494,13 +565,14 @@ class Apps {
$darray['app_addr'] = ((x($arr,'addr')) ? escape_tags($arr['addr']) : '');
$darray['app_price'] = ((x($arr,'price')) ? escape_tags($arr['price']) : '');
$darray['app_page'] = ((x($arr,'page')) ? escape_tags($arr['page']) : '');
+ $darray['app_plugin'] = ((x($arr,'plugin')) ? escape_tags($arr['plugin']) : '');
$darray['app_requires'] = ((x($arr,'requires')) ? escape_tags($arr['requires']) : '');
$darray['app_system'] = ((x($arr,'system')) ? intval($arr['system']) : 0);
$darray['app_deleted'] = ((x($arr,'deleted')) ? intval($arr['deleted']) : 0);
$created = datetime_convert();
- $r = q("insert into app ( app_id, app_sig, app_author, app_name, app_desc, app_url, app_photo, app_version, app_channel, app_addr, app_price, app_page, app_requires, app_created, app_edited, app_system, app_deleted ) values ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d )",
+ $r = q("insert into app ( app_id, app_sig, app_author, app_name, app_desc, app_url, app_photo, app_version, app_channel, app_addr, app_price, app_page, app_requires, app_created, app_edited, app_system, app_plugin, app_deleted ) values ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', %d )",
dbesc($darray['app_id']),
dbesc($darray['app_sig']),
dbesc($darray['app_author']),
@@ -517,6 +589,7 @@ class Apps {
dbesc($created),
dbesc($created),
intval($darray['app_system']),
+ dbesc($darray['app_plugin']),
intval($darray['app_deleted'])
);
if($r) {
@@ -545,6 +618,7 @@ class Apps {
static public function app_update($arr) {
+ //logger('app_update: ' . print_r($arr,true));
$darray = array();
$ret = array('success' => false);
@@ -555,7 +629,7 @@ class Apps {
if((! $darray['app_url']) || (! $darray['app_channel']) || (! $darray['app_id']))
return $ret;
- if($arr['photo'] && ! strstr($arr['photo'],z_root())) {
+ if($arr['photo'] && (strpos($arr['photo'],'icon:') !== 0) && (! strstr($arr['photo'],z_root()))) {
$x = import_xchan_photo($arr['photo'],get_observer_hash(),true);
$arr['photo'] = $x[1];
}
@@ -569,13 +643,14 @@ class Apps {
$darray['app_addr'] = ((x($arr,'addr')) ? escape_tags($arr['addr']) : '');
$darray['app_price'] = ((x($arr,'price')) ? escape_tags($arr['price']) : '');
$darray['app_page'] = ((x($arr,'page')) ? escape_tags($arr['page']) : '');
+ $darray['app_plugin'] = ((x($arr,'plugin')) ? escape_tags($arr['plugin']) : '');
$darray['app_requires'] = ((x($arr,'requires')) ? escape_tags($arr['requires']) : '');
$darray['app_system'] = ((x($arr,'system')) ? intval($arr['system']) : 0);
$darray['app_deleted'] = ((x($arr,'deleted')) ? intval($arr['deleted']) : 0);
$edited = datetime_convert();
- $r = q("update app set app_sig = '%s', app_author = '%s', app_name = '%s', app_desc = '%s', app_url = '%s', app_photo = '%s', app_version = '%s', app_addr = '%s', app_price = '%s', app_page = '%s', app_requires = '%s', app_edited = '%s', app_system = %d, app_deleted = %d where app_id = '%s' and app_channel = %d",
+ $r = q("update app set app_sig = '%s', app_author = '%s', app_name = '%s', app_desc = '%s', app_url = '%s', app_photo = '%s', app_version = '%s', app_addr = '%s', app_price = '%s', app_page = '%s', app_requires = '%s', app_edited = '%s', app_system = %d, app_plugin = '%s', app_deleted = %d where app_id = '%s' and app_channel = %d",
dbesc($darray['app_sig']),
dbesc($darray['app_author']),
dbesc($darray['app_name']),
@@ -589,6 +664,7 @@ class Apps {
dbesc($darray['app_requires']),
dbesc($edited),
intval($darray['app_system']),
+ dbesc($darray['app_plugin']),
intval($darray['app_deleted']),
dbesc($darray['app_id']),
intval($darray['app_channel'])
@@ -655,6 +731,9 @@ class Apps {
if($app['app_photo'])
$ret['photo'] = $app['app_photo'];
+ if($app['app_icon'])
+ $ret['icon'] = $app['app_icon'];
+
if($app['app_version'])
$ret['version'] = $app['app_version'];
@@ -673,6 +752,9 @@ class Apps {
if($app['app_system'])
$ret['system'] = $app['app_system'];
+ if($app['app_plugin'])
+ $ret['plugin'] = $app['app_plugin'];
+
if($app['app_deleted'])
$ret['deleted'] = $app['app_deleted'];
@@ -690,6 +772,8 @@ class Apps {
if(! $embed)
return $ret;
+ $ret['embed'] = true;
+
if(array_key_exists('categories',$ret))
unset($ret['categories']);
diff --git a/Zotlabs/Lib/NativeWiki.php b/Zotlabs/Lib/NativeWiki.php
new file mode 100644
index 000000000..7786ec25a
--- /dev/null
+++ b/Zotlabs/Lib/NativeWiki.php
@@ -0,0 +1,210 @@
+<?php
+
+namespace Zotlabs\Lib;
+
+define ( 'NWIKI_ITEM_RESOURCE_TYPE', 'nwiki' );
+
+class NativeWiki {
+
+
+ static public function listwikis($channel, $observer_hash) {
+
+ $sql_extra = item_permissions_sql($channel['channel_id'], $observer_hash);
+ $wikis = q("SELECT * FROM item
+ WHERE resource_type = '%s' AND mid = parent_mid AND uid = %d AND item_deleted = 0 $sql_extra",
+ dbesc(NWIKI_ITEM_RESOURCE_TYPE),
+ intval($channel['channel_id'])
+ );
+
+ if($wikis) {
+ foreach($wikis as &$w) {
+ $w['rawName'] = get_iconfig($w, 'wiki', 'rawName');
+ $w['htmlName'] = escape_tags($w['rawName']);
+ $w['urlName'] = urlencode(urlencode($w['rawName']));
+ $w['mimeType'] = get_iconfig($w, 'wiki', 'mimeType');
+ $w['lock'] = (($w['item_private'] || $w['allow_cid'] || $w['allow_gid'] || $w['deny_cid'] || $w['deny_gid']) ? true : false);
+ }
+ }
+ // TODO: query db for wikis the observer can access. Return with two lists, for read and write access
+ return array('wikis' => $wikis);
+ }
+
+
+ function create_wiki($channel, $observer_hash, $wiki, $acl) {
+
+ // Generate unique resource_id using the same method as item_message_id()
+ do {
+ $dups = false;
+ $resource_id = random_string();
+ $r = q("SELECT mid FROM item WHERE resource_id = '%s' AND resource_type = '%s' AND uid = %d LIMIT 1",
+ dbesc($resource_id),
+ dbesc(NWIKI_ITEM_RESOURCE_TYPE),
+ intval($channel['channel_id'])
+ );
+ if($r)
+ $dups = true;
+ } while($dups == true);
+
+ $ac = $acl->get();
+ $mid = item_message_id();
+
+ $arr = array(); // Initialize the array of parameters for the post
+ $item_hidden = ((intval($wiki['postVisible']) === 0) ? 1 : 0);
+ $wiki_url = z_root() . '/wiki/' . $channel['channel_address'] . '/' . $wiki['urlName'];
+ $arr['aid'] = $channel['channel_account_id'];
+ $arr['uid'] = $channel['channel_id'];
+ $arr['mid'] = $mid;
+ $arr['parent_mid'] = $mid;
+ $arr['item_hidden'] = $item_hidden;
+ $arr['resource_type'] = NWIKI_ITEM_RESOURCE_TYPE;
+ $arr['resource_id'] = $resource_id;
+ $arr['owner_xchan'] = $channel['channel_hash'];
+ $arr['author_xchan'] = $observer_hash;
+ $arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . urlencode($arr['mid']);
+ $arr['llink'] = $arr['plink'];
+ $arr['title'] = $wiki['htmlName']; // name of new wiki;
+ $arr['allow_cid'] = $ac['allow_cid'];
+ $arr['allow_gid'] = $ac['allow_gid'];
+ $arr['deny_cid'] = $ac['deny_cid'];
+ $arr['deny_gid'] = $ac['deny_gid'];
+ $arr['item_wall'] = 1;
+ $arr['item_origin'] = 1;
+ $arr['item_thread_top'] = 1;
+ $arr['item_private'] = intval($acl->is_private());
+ $arr['verb'] = ACTIVITY_CREATE;
+ $arr['obj_type'] = ACTIVITY_OBJ_WIKI;
+ $arr['body'] = '[table][tr][td][h1]New Wiki[/h1][/td][/tr][tr][td][zrl=' . $wiki_url . ']' . $wiki['htmlName'] . '[/zrl][/td][/tr][/table]';
+
+ $arr['public_policy'] = map_scope(\Zotlabs\Access\PermissionLimits::Get($channel['channel_id'],'view_wiki'),true);
+
+ // Save the wiki name information using iconfig. This is shareable.
+ if(! set_iconfig($arr, 'wiki', 'rawName', $wiki['rawName'], true)) {
+ return array('item' => null, 'success' => false);
+ }
+ if(! set_iconfig($arr, 'wiki', 'mimeType', $wiki['mimeType'], true)) {
+ return array('item' => null, 'success' => false);
+ }
+
+ $post = item_store($arr);
+
+ $item_id = $post['item_id'];
+
+ if($item_id) {
+ \Zotlabs\Daemon\Master::Summon(array('Notifier', 'activity', $item_id));
+ return array('item' => $post['item'], 'item_id' => $item_id, 'success' => true);
+ }
+ else {
+ return array('item' => null, 'success' => false);
+ }
+ }
+
+ static public function sync_a_wiki_item($uid,$id,$resource_id) {
+
+
+ $r = q("SELECT * from item WHERE uid = %d AND ( id = %d OR ( resource_type = '%s' and resource_id = %d )) ",
+ intval($uid),
+ intval($id),
+ dbesc(NWIKI_ITEM_RESOURCE_TYPE),
+ intval($resource_id)
+ );
+ if($r) {
+ xchan_query($r);
+ $sync_item = fetch_post_tags($r);
+ build_sync_packet($uid,array('wiki' => array(encode_item($sync_item[0],true))));
+ }
+ }
+
+ function delete_wiki($channel_id,$observer_hash,$resource_id) {
+
+ $w = self::get_wiki($channel_id,$observer_hash,$resource_id);
+ $item = $w['wiki'];
+ if(! $item) {
+ return array('item' => null, 'success' => false);
+ }
+ else {
+ $drop = drop_item($item['id'], false, DROPITEM_NORMAL, true);
+ }
+
+ info( t('Wiki files deleted successfully'));
+
+ return array('item' => $item, 'item_id' => $item['id'], 'success' => (($drop === 1) ? true : false));
+ }
+
+
+ static public function get_wiki($channel_id, $observer_hash, $resource_id) {
+
+ $sql_extra = item_permissions_sql($channel_id,$observer_hash);
+
+ $item = q("SELECT * FROM item WHERE uid = %d AND resource_type = '%s' AND resource_id = '%s' AND item_deleted = 0
+ $sql_extra limit 1",
+ intval($channel_id),
+ dbesc(NWIKI_ITEM_RESOURCE_TYPE),
+ dbesc($resource_id)
+ );
+ if(! $item) {
+ return array('wiki' => null);
+ }
+ else {
+
+ $w = $item[0]; // wiki item table record
+ // Get wiki metadata
+ $rawName = get_iconfig($w, 'wiki', 'rawName');
+ $mimeType = get_iconfig($w, 'wiki', 'mimeType');
+
+ return array(
+ 'wiki' => $w,
+ 'rawName' => $rawName,
+ 'htmlName' => escape_tags($rawName),
+ 'urlName' => urlencode(urlencode($rawName)),
+ 'mimeType' => $mimeType
+ );
+ }
+ }
+
+
+ static public function exists_by_name($uid, $urlName) {
+
+ $sql_extra = item_permissions_sql($uid);
+
+ $item = q("SELECT item.id, resource_id FROM item left join iconfig on iconfig.iid = item.id
+ WHERE resource_type = '%s' AND iconfig.v = '%s' AND uid = %d
+ AND item_deleted = 0 $sql_extra limit 1",
+ dbesc(NWIKI_ITEM_RESOURCE_TYPE),
+ dbesc(urldecode($urlName)),
+ intval($uid)
+ );
+
+ if($item) {
+ return array('id' => $item[0]['id'], 'resource_id' => $item[0]['resource_id']);
+ }
+ else {
+ return array('id' => null, 'resource_id' => null);
+ }
+ }
+
+
+ static public function get_permissions($resource_id, $owner_id, $observer_hash) {
+ // TODO: For now, only the owner can edit
+ $sql_extra = item_permissions_sql($owner_id, $observer_hash);
+
+ if(local_channel() && local_channel() == $owner_id) {
+ return [ 'read' => true, 'write' => true, 'success' => true ];
+ }
+
+ $r = q("SELECT * FROM item WHERE uid = %d and resource_type = '%s' AND resource_id = '%s' $sql_extra LIMIT 1",
+ intval($owner_id),
+ dbesc(NWIKI_ITEM_RESOURCE_TYPE),
+ dbesc($resource_id)
+ );
+
+ if(! $r) {
+ return array('read' => false, 'write' => false, 'success' => true);
+ }
+ else {
+ // TODO: Create a new permission setting for wiki analogous to webpages. Until
+ // then, use webpage permissions
+ $write = perm_is_allowed($owner_id, $observer_hash,'write_wiki');
+ return array('read' => true, 'write' => $write, 'success' => true);
+ }
+ }
+}
diff --git a/Zotlabs/Lib/NativeWikiPage.php b/Zotlabs/Lib/NativeWikiPage.php
new file mode 100644
index 000000000..4086a023e
--- /dev/null
+++ b/Zotlabs/Lib/NativeWikiPage.php
@@ -0,0 +1,664 @@
+<?php
+
+namespace Zotlabs\Lib;
+
+use \Zotlabs\Lib as Zlib;
+
+class NativeWikiPage {
+
+ static public function page_list($channel_id,$observer_hash, $resource_id) {
+
+ // TODO: Create item table records for pages so that metadata like title can be applied
+ $w = Zlib\NativeWiki::get_wiki($channel_id,$observer_hash,$resource_id);
+
+ $pages[] = [
+ 'resource_id' => '',
+ 'title' => 'Home',
+ 'url' => 'Home',
+ 'link_id' => 'id_wiki_home_0'
+ ];
+
+ $sql_extra = item_permissions_sql($channel_id,$observer_hash);
+
+ $r = q("select * from item where resource_type = 'nwikipage' and resource_id = '%s' and uid = %d and item_deleted = 0
+ $sql_extra group by mid",
+ dbesc($resource_id),
+ intval($channel_id)
+ );
+ if($r) {
+ $items = fetch_post_tags($r,true);
+ foreach($items as $page_item) {
+ $title = get_iconfig($page_item['id'],'nwikipage','pagetitle',t('(No Title)'));
+ if(urldecode($title) !== 'Home') {
+ $pages[] = [
+ 'resource_id' => $resource_id,
+ 'title' => escape_tags($title),
+ 'url' => urlencode(urlencode($title)),
+ 'link_id' => 'id_' . substr($resource_id, 0, 10) . '_' . $page_item['id']
+ ];
+ }
+ }
+ }
+
+ return array('pages' => $pages, 'wiki' => $w);
+ }
+
+
+ static public function create_page($channel_id, $observer_hash, $name, $resource_id) {
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+
+ if (! $w['wiki']) {
+ return array('content' => null, 'message' => 'Error reading wiki', 'success' => false);
+ }
+
+ // create an empty activity
+
+ $arr = [];
+ $arr['uid'] = $channel_id;
+ $arr['author_xchan'] = $observer_hash;
+ $arr['resource_type'] = 'nwikipage';
+ $arr['resource_id'] = $resource_id;
+ $arr['allow_cid'] = $w['wiki']['allow_cid'];
+ $arr['allow_gid'] = $w['wiki']['allow_gid'];
+ $arr['deny_cid'] = $w['wiki']['deny_cid'];
+ $arr['deny_gid'] = $w['wiki']['deny_gid'];
+
+ $arr['public_policy'] = map_scope(\Zotlabs\Access\PermissionLimits::Get($channel_id,'view_wiki'),true);
+
+ // We may wish to change this some day.
+ $arr['item_unpublished'] = 1;
+
+ set_iconfig($arr,'nwikipage','pagetitle',(($name) ? $name : t('(No Title)')),true);
+
+ $p = post_activity_item($arr, false, false);
+
+ if($p['item_id']) {
+ $page = [
+ 'rawName' => $name,
+ 'htmlName' => escape_tags($name),
+ 'urlName' => urlencode($name),
+
+ ];
+
+ return array('page' => $page, 'item_id' => $p['item_id'], 'item' => $p['activity'], 'wiki' => $w, 'message' => '', 'success' => true);
+ }
+ return [ 'success' => false, 'message' => t('Wiki page create failed.') ];
+ }
+
+ static public function rename_page($arr) {
+
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $pageNewName = ((array_key_exists('pageNewName',$arr)) ? $arr['pageNewName'] : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+ if(! $w['wiki']) {
+ return array('message' => t('Wiki not found.'), 'success' => false);
+ }
+
+
+ $ic = q("select * from iconfig left join item on iconfig.iid = item.id
+ where uid = %d and cat = 'nwikipage' and k = 'pagetitle' and v = '%s'",
+ intval($channel_id),
+ dbesc($pageNewName)
+ );
+
+ if($ic) {
+ return [ 'success' => false, 'message' => t('Destination name already exists') ];
+ }
+
+
+ $ids = [];
+
+ $ic = q("select *, item.id as item_id from iconfig left join item on iconfig.iid = item.id
+ where uid = %d and cat = 'nwikipage' and k = 'pagetitle' and v = '%s'",
+ intval($channel_id),
+ dbesc($pageUrlName)
+ );
+
+ if($ic) {
+ foreach($ic as $c) {
+ set_iconfig($c['item_id'],'nwikipage','pagetitle',$pageNewName);
+ }
+
+ $page = [
+ 'rawName' => $pageNewName,
+ 'htmlName' => escape_tags($pageNewName),
+ 'urlName' => urlencode(escape_tags($pageNewName))
+ ];
+
+ return [ 'success' => true, 'page' => $page ];
+ }
+
+ return [ 'success' => false, 'item_id' => $c['item_id'], 'message' => t('Page not found') ];
+
+ }
+
+ static public function get_page_content($arr) {
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? intval($arr['channel_id']) : 0);
+ $revision = ((array_key_exists('revision',$arr)) ? intval($arr['revision']) : (-1));
+
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+ if (! $w['wiki']) {
+ return array('content' => null, 'message' => 'Error reading wiki', 'success' => false);
+ }
+
+ $item = self::load_page($arr);
+
+ if($item) {
+ $content = $item['body'];
+
+ return [
+ 'content' => json_encode($content),
+ 'mimeType' => $w['mimeType'],
+ 'message' => '',
+ 'success' => true
+ ];
+ }
+
+ return array('content' => null, 'message' => t('Error reading page content'), 'success' => false);
+
+ }
+
+ static public function page_history($arr) {
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+ if (!$w['wiki']) {
+ return array('history' => null, 'message' => 'Error reading wiki', 'success' => false);
+ }
+
+ $items = self::load_page_history($arr);
+
+ $history = [];
+
+ if($items) {
+ $processed = 0;
+ foreach($items as $item) {
+ if($processed > 1000)
+ break;
+ $processed ++;
+ $history[] = [
+ 'revision' => $item['revision'],
+ 'date' => datetime_convert('UTC',date_default_timezone_get(),$item['edited']),
+ 'name' => $item['author']['xchan_name'],
+ 'title' => get_iconfig($item,'nwikipage','commit_msg')
+ ];
+
+ }
+
+ return [ 'success' => true, 'history' => $history ];
+ }
+
+ return [ 'success' => false ];
+
+ }
+
+
+ static public function load_page($arr) {
+
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+ $revision = ((array_key_exists('revision',$arr)) ? $arr['revision'] : (-1));
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+
+ if (! $w['wiki']) {
+ return array('content' => null, 'message' => 'Error reading wiki', 'success' => false);
+ }
+
+ $ids = '';
+
+ $ic = q("select * from iconfig left join item on iconfig.iid = item.id where uid = %d and cat = 'nwikipage' and k = 'pagetitle' and v = '%s'",
+ intval($channel_id),
+ dbesc($pageUrlName)
+ );
+
+ if($ic) {
+ foreach($ic as $c) {
+ if($ids)
+ $ids .= ',';
+ $ids .= intval($c['iid']);
+ }
+ }
+
+ $sql_extra = item_permissions_sql($channel_id,$observer_hash);
+
+ if($revision == (-1))
+ $sql_extra .= " order by revision desc ";
+ elseif($revision)
+ $sql_extra .= " and revision = " . intval($revision) . " ";
+
+ $r = null;
+
+
+ if($ids) {
+ $r = q("select * from item where resource_type = 'nwikipage' and resource_id = '%s' and uid = %d and id in ( $ids ) $sql_extra limit 1",
+ dbesc($resource_id),
+ intval($channel_id)
+ );
+
+ if($r) {
+ $items = fetch_post_tags($r,true);
+ return $items[0];
+ }
+ }
+
+ return null;
+ }
+
+ static public function load_page_history($arr) {
+
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+ $revision = ((array_key_exists('revision',$arr)) ? $arr['revision'] : (-1));
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+ if (! $w['wiki']) {
+ return array('content' => null, 'message' => 'Error reading wiki', 'success' => false);
+ }
+
+ $ids = '';
+
+ $ic = q("select * from iconfig left join item on iconfig.iid = item.id where uid = %d and cat = 'nwikipage' and k = 'pagetitle' and v = '%s'",
+ intval($channel_id),
+ dbesc($pageUrlName)
+ );
+
+ if($ic) {
+ foreach($ic as $c) {
+ if($ids)
+ $ids .= ',';
+ $ids .= intval($c['iid']);
+ }
+ }
+
+ $sql_extra = item_permissions_sql($channel_id,$observer_hash);
+
+ $sql_extra .= " order by revision desc ";
+
+ $r = null;
+ if($ids) {
+ $r = q("select * from item where resource_type = 'nwikipage' and resource_id = '%s' and uid = %d and id in ( $ids ) and item_deleted = 0 $sql_extra",
+ dbesc($resource_id),
+ intval($channel_id)
+ );
+ if($r) {
+ xchan_query($r);
+ $items = fetch_post_tags($r,true);
+ return $items;
+ }
+ }
+
+ return null;
+ }
+
+
+
+ static public function prepare_content($s) {
+
+ $text = preg_replace_callback('{
+ (?:\n\n|\A\n?)
+ ( # $1 = the code block -- one or more lines, starting with a space/tab
+ (?>
+ [ ]{'.'4'.'} # Lines must start with a tab or a tab-width of spaces
+ .*\n+
+ )+
+ )
+ ((?=^[ ]{0,'.'4'.'}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
+ }xm',
+ 'self::nwiki_prepare_content_callback', $s);
+
+ return $text;
+ }
+
+ static public function nwiki_prepare_content_callback($matches) {
+ $codeblock = $matches[1];
+
+ $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES, UTF8, false);
+ return "\n\n" . $codeblock ;
+ }
+
+
+
+ static public function save_page($arr) {
+
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $content = ((array_key_exists('content',$arr)) ? purify_html(Zlib\NativeWikiPage::prepare_content($arr['content'])) : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+ $revision = ((array_key_exists('revision',$arr)) ? $arr['revision'] : 0);
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+
+ if (!$w['wiki']) {
+ return array('message' => t('Error reading wiki'), 'success' => false);
+ }
+
+ // fetch the most recently saved revision.
+
+ $item = self::load_page($arr);
+ if(! $item) {
+ return array('message' => t('Page not found'), 'success' => false);
+ }
+
+ // change just the fields we need to change to create a revision;
+
+ unset($item['id']);
+ unset($item['author']);
+
+ $item['parent'] = 0;
+ $item['body'] = $content;
+ $item['author_xchan'] = $observer_hash;
+ $item['revision'] = (($arr['revision']) ? intval($arr['revision']) + 1 : intval($item['revision']) + 1);
+ $item['edited'] = datetime_convert();
+
+ if($item['iconfig'] && is_array($item['iconfig']) && count($item['iconfig'])) {
+ for($x = 0; $x < count($item['iconfig']); $x ++) {
+ unset($item['iconfig'][$x]['id']);
+ unset($item['iconfig'][$x]['iid']);
+ }
+ }
+
+ $ret = item_store($item, false, false);
+
+ if($ret['item_id'])
+ return array('message' => '', 'item_id' => $ret['item_id'], 'filename' => $filename, 'success' => true);
+ else
+ return array('message' => t('Page update failed.'), 'success' => false);
+ }
+
+ static public function delete_page($arr) {
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+
+ if(! $w['wiki']) {
+ return [ 'success' => false, 'message' => t('Error reading wiki') ];
+ }
+
+ $ids = [];
+
+ $ic = q("select * from iconfig left join item on iconfig.iid = item.id
+ where uid = %d and cat = 'nwikipage' and k = 'pagetitle' and v = '%s'",
+ intval($channel_id),
+ dbesc($pageUrlName)
+ );
+
+ if($ic) {
+ foreach($ic as $c) {
+ $ids[] = intval($c['iid']);
+ }
+ }
+
+ if($ids) {
+ drop_items($ids);
+ return [ 'success' => true ];
+ }
+
+ return [ 'success' => false, 'message' => t('Nothing deleted') ];
+ }
+
+ static public function revert_page($arr) {
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $commitHash = ((array_key_exists('commitHash',$arr)) ? $arr['commitHash'] : null);
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+
+ if (! $commitHash) {
+ return array('content' => $content, 'message' => 'No commit was provided', 'success' => false);
+ }
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+ if (!$w['wiki']) {
+ return array('content' => $content, 'message' => 'Error reading wiki', 'success' => false);
+ }
+
+ $x = $arr;
+
+ if(intval($commitHash) > 0) {
+ unset($x['commitHash']);
+ $x['revision'] = intval($commitHash) - 1;
+ $loaded = self::load_page($x);
+
+ if($loaded) {
+ $content = $loaded['body'];
+ return [ 'content' => $content, 'success' => true ];
+ }
+ return [ 'content' => $content, 'success' => false ];
+ }
+ }
+
+ static public function compare_page($arr) {
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : '');
+ $resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
+ $currentCommit = ((array_key_exists('currentCommit',$arr)) ? $arr['currentCommit'] : (-1));
+ $compareCommit = ((array_key_exists('compareCommit',$arr)) ? $arr['compareCommit'] : 0);
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+
+ if (!$w['wiki']) {
+ return array('message' => t('Error reading wiki'), 'success' => false);
+ }
+
+ $x = $arr;
+ $x['revision'] = (-1);
+
+ $currpage = self::load_page($x);
+ if($currpage)
+ $currentContent = $currpage['body'];
+
+ $x['revision'] = $compareCommit;
+ $comppage = self::load_page($x);
+ if($comppage)
+ $compareContent = $comppage['body'];
+
+ if($currpage && $comppage) {
+ require_once('library/class.Diff.php');
+ $diff = \Diff::toTable(\Diff::compare($currentContent, $compareContent));
+
+ return [ 'success' => true, 'diff' => $diff ];
+ }
+ return [ 'success' => false, 'message' => t('Compare: object not found.') ];
+
+ }
+
+ static public function commit($arr) {
+
+ $commit_msg = ((array_key_exists('commit_msg', $arr)) ? $arr['commit_msg'] : t('Page updated'));
+ $observer_hash = ((array_key_exists('observer_hash',$arr)) ? $arr['observer_hash'] : '');
+ $channel_id = ((array_key_exists('channel_id',$arr)) ? $arr['channel_id'] : 0);
+ $pageUrlName = ((array_key_exists('pageUrlName',$arr)) ? $arr['pageUrlName'] : t('Untitled'));
+
+ if(array_key_exists('resource_id', $arr)) {
+ $resource_id = $arr['resource_id'];
+ }
+ else {
+ return array('message' => t('Wiki resource_id required for git commit'), 'success' => false);
+ }
+
+ $w = Zlib\NativeWiki::get_wiki($channel_id, $observer_hash, $resource_id);
+ if (! $w['wiki']) {
+ return array('message' => t('Error reading wiki'), 'success' => false);
+ }
+
+
+ $page = self::load_page($arr);
+
+ if($page) {
+ set_iconfig($page['id'],'nwikipage','commit_msg',escape_tags($commit_msg),true);
+ return [ 'success' => true, 'item_id' => $page['id'], 'page' => $page ];
+ }
+
+ return [ 'success' => false, 'message' => t('Page not found.') ];
+
+ }
+
+ static public function convert_links($s, $wikiURL) {
+
+ if (strpos($s,'[[') !== false) {
+ preg_match_all("/\[\[(.*?)\]\]/", $s, $match);
+ $pages = $pageURLs = array();
+ foreach ($match[1] as $m) {
+ // TODO: Why do we need to double urlencode for this to work?
+ $pageURLs[] = urlencode(urlencode(escape_tags($m)));
+ $pages[] = $m;
+ }
+ $idx = 0;
+ while(strpos($s,'[[') !== false) {
+ $replace = '<a href="'.$wikiURL.'/'.$pageURLs[$idx].'">'.$pages[$idx].'</a>';
+ $s = preg_replace("/\[\[(.*?)\]\]/", $replace, $s, 1);
+ $idx++;
+ }
+ }
+ return $s;
+ }
+
+ /**
+ * Replace the instances of the string [toc] with a list element that will be populated by
+ * a table of contents by the JavaScript library
+ * @param string $s
+ * @return string
+ */
+ static public function generate_toc($s) {
+ if (strpos($s,'[toc]') !== false) {
+ //$toc_md = wiki_toc($s); // Generate Markdown-formatted list prior to HTML render
+ $toc_md = '<ul id="wiki-toc"></ul>'; // use the available jQuery plugin http://ndabas.github.io/toc/
+ $s = preg_replace("/\[toc\]/", $toc_md, $s, -1);
+ }
+ return $s;
+ }
+
+ /**
+ * Converts a select set of bbcode tags. Much of the code is copied from include/bbcode.php
+ * @param string $s
+ * @return string
+ */
+ static public function bbcode($s) {
+
+ $s = str_replace(array('[baseurl]', '[sitename]'), array(z_root(), get_config('system', 'sitename')), $s);
+
+ $s = preg_replace_callback("/\[observer\.language\=(.*?)\](.*?)\[\/observer\]/ism",'oblanguage_callback', $s);
+
+ $s = preg_replace_callback("/\[observer\.language\!\=(.*?)\](.*?)\[\/observer\]/ism",'oblanguage_necallback', $s);
+
+
+ $observer = \App::get_observer();
+ if ($observer) {
+ $s1 = '<span class="bb_observer" title="' . t('Different viewers will see this text differently') . '">';
+ $s2 = '</span>';
+ $obsBaseURL = $observer['xchan_connurl'];
+ $obsBaseURL = preg_replace("/\/poco\/.*$/", '', $obsBaseURL);
+ $s = str_replace('[observer.baseurl]', $obsBaseURL, $s);
+ $s = str_replace('[observer.url]', $observer['xchan_url'], $s);
+ $s = str_replace('[observer.name]', $s1 . $observer['xchan_name'] . $s2, $s);
+ $s = str_replace('[observer.address]', $s1 . $observer['xchan_addr'] . $s2, $s);
+ $s = str_replace('[observer.webname]', substr($observer['xchan_addr'], 0, strpos($observer['xchan_addr'], '@')), $s);
+ $s = str_replace('[observer.photo]', '', $s);
+ }
+ else {
+ $s = str_replace('[observer.baseurl]', '', $s);
+ $s = str_replace('[observer.url]', '', $s);
+ $s = str_replace('[observer.name]', '', $s);
+ $s = str_replace('[observer.address]', '', $s);
+ $s = str_replace('[observer.webname]', '', $s);
+ $s = str_replace('[observer.photo]', '', $s);
+ }
+
+ return $s;
+ }
+
+ static public function get_file_ext($arr) {
+ if($arr['mimeType'] == 'text/bbcode')
+ return '.bb';
+ else
+ return '.md';
+ }
+
+ // This function is derived from
+ // http://stackoverflow.com/questions/32068537/generate-table-of-contents-from-markdown-in-php
+ static public function toc($content) {
+ // ensure using only "\n" as line-break
+ $source = str_replace(["\r\n", "\r"], "\n", $content);
+
+ // look for markdown TOC items
+ preg_match_all(
+ '/^(?:=|-|#).*$/m',
+ $source,
+ $matches,
+ PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE
+ );
+
+ // preprocess: iterate matched lines to create an array of items
+ // where each item is an array(level, text)
+ $file_size = strlen($source);
+ foreach ($matches[0] as $item) {
+ $found_mark = substr($item[0], 0, 1);
+ if ($found_mark == '#') {
+ // text is the found item
+ $item_text = $item[0];
+ $item_level = strrpos($item_text, '#') + 1;
+ $item_text = substr($item_text, $item_level);
+ } else {
+ // text is the previous line (empty if <hr>)
+ $item_offset = $item[1];
+ $prev_line_offset = strrpos($source, "\n", -($file_size - $item_offset + 2));
+ $item_text =
+ substr($source, $prev_line_offset, $item_offset - $prev_line_offset - 1);
+ $item_text = trim($item_text);
+ $item_level = $found_mark == '=' ? 1 : 2;
+ }
+ if (!trim($item_text) OR strpos($item_text, '|') !== FALSE) {
+ // item is an horizontal separator or a table header, don't mind
+ continue;
+ }
+ $raw_toc[] = ['level' => $item_level, 'text' => trim($item_text)];
+ }
+ $o = '';
+ foreach($raw_toc as $t) {
+ $level = intval($t['level']);
+ $text = $t['text'];
+ switch ($level) {
+ case 1:
+ $li = '* ';
+ break;
+ case 2:
+ $li = ' * ';
+ break;
+ case 3:
+ $li = ' * ';
+ break;
+ case 4:
+ $li = ' * ';
+ break;
+ default:
+ $li = '* ';
+ break;
+ }
+ $o .= $li . $text . "\n";
+ }
+ return $o;
+ }
+
+}
diff --git a/Zotlabs/Lib/Permcat.php b/Zotlabs/Lib/Permcat.php
new file mode 100644
index 000000000..505ee2cfc
--- /dev/null
+++ b/Zotlabs/Lib/Permcat.php
@@ -0,0 +1,146 @@
+<?php
+
+namespace Zotlabs\Lib;
+
+use \Zotlabs\Access as Zaccess;
+
+class Permcat {
+
+ private $permcats = [];
+
+ public function __construct($channel_id) {
+
+ $perms = [];
+
+ // first check role perms for a perms_connect setting
+
+ $role = get_pconfig($channel_id,'system','permissions_role');
+ if($role) {
+ $x = Zaccess\PermissionRoles::role_perms($role);
+ if($x['perms_connect']) {
+ $perms = Zaccess\Permissions::FilledPerms($x['perms_connect']);
+ }
+ }
+
+ // if no role perms it may be a custom role, see if there any autoperms
+
+ if(! $perms) {
+ $perms = Zaccess\Permissions::FilledAutoPerms($channel_id);
+ }
+
+ // if no autoperms it may be a custom role with manual perms
+
+ if(! $perms) {
+ $r = q("select channel_hash from channel where channel_id = %d",
+ intval($channel_id)
+ );
+ if($r) {
+ $x = q("select * from abconfig where chan = %d and xchan = '%s' and cat = 'my_perms'",
+ intval($channel_id),
+ dbesc($r[0]['channel_hash'])
+ );
+ if($x) {
+ foreach($x as $xv) {
+ $perms[$xv['k']] = intval($xv['v']);
+ }
+ }
+ }
+ }
+
+ // nothing was found - create a filled permission array where all permissions are 0
+
+ if(! $perms) {
+ $perms = Zaccess\Permissions::FilledPerms([]);
+ }
+
+ $this->permcats[] = [
+ 'name' => 'default',
+ 'localname' => t('default','permcat'),
+ 'perms' => Zaccess\Permissions::Operms($perms),
+ 'system' => 1
+ ];
+
+
+ $p = $this->load_permcats($channel_id);
+ if($p) {
+ for($x = 0; $x < count($p); $x++) {
+ $this->permcats[] = [
+ 'name' => $p[$x][0],
+ 'localname' => $p[$x][1],
+ 'perms' => Zaccess\Permissions::Operms(Zaccess\Permissions::FilledPerms($p[$x][2])),
+ 'system' => intval($p[$x][3])
+ ];
+ }
+ }
+ }
+
+
+ public function listing() {
+ return $this->permcats;
+ }
+
+ public function fetch($name) {
+ if($name && $this->permcats) {
+ foreach($this->permcats as $permcat) {
+ if(strcasecmp($permcat['name'],$name) === 0) {
+ return $permcat;
+ }
+ }
+ }
+ return ['error' => true];
+ }
+
+ public function load_permcats($uid) {
+
+ $permcats = [
+ [ 'follower', t('follower','permcat'),
+ [ 'view_stream','view_profile','view_contacts','view_storage','view_pages','view_wiki',
+ 'post_like' ], 1
+ ],
+ [ 'contributor', t('contributor','permcat'),
+ [ 'view_stream','view_profile','view_contacts','view_storage','view_pages','view_wiki',
+ 'post_wall','post_comments','write_wiki','post_like','tag_deliver','chat' ], 1
+ ],
+ [ 'publisher', t('publisher','permcat'),
+ [ 'view_stream','view_profile','view_contacts','view_storage','view_pages',
+ 'write_storage','post_wall','write_pages','write_wiki','post_comments','post_like','tag_deliver',
+ 'chat', 'republish' ], 1
+ ]
+ ];
+
+ if($uid) {
+ $x = q("select * from pconfig where uid = %d and cat = 'permcat'",
+ intval($uid)
+ );
+ if($x) {
+ foreach($x as $xv) {
+ $value = ((preg_match('|^a:[0-9]+:{.*}$|s', $xv['v'])) ? unserialize($xv['v']) : $xv['v']);
+ $permcats[] = [ $xv['k'], $xv['k'], $value, 0 ];
+ }
+ }
+ }
+
+ call_hooks('permcats',$permcats);
+
+ return $permcats;
+
+ }
+
+ static public function find_permcat($arr,$name) {
+ if((! $arr) || (! $name))
+ return false;
+ foreach($arr as $p)
+ if($p['name'] == $name)
+ return $p['value'];
+ }
+
+ static public function update($channel_id, $name,$permarr) {
+ PConfig::Set($channel_id,'permcat',$name,$permarr);
+ }
+
+ static public function delete($channel_id,$name) {
+ PConfig::Delete($channel_id,'permcat',$name);
+ }
+
+
+} \ No newline at end of file
diff --git a/Zotlabs/Lib/ThreadItem.php b/Zotlabs/Lib/ThreadItem.php
index a1666e148..07b782309 100644
--- a/Zotlabs/Lib/ThreadItem.php
+++ b/Zotlabs/Lib/ThreadItem.php
@@ -136,7 +136,7 @@ class ThreadItem {
$filer = ((($conv->get_profile_owner() == local_channel()) && (! array_key_exists('real_uid',$item))) ? t("Save to Folder") : false);
$profile_avatar = $item['author']['xchan_photo_m'];
- $profile_link = chanlink_url($item['author']['xchan_url']);
+ $profile_link = chanlink_hash($item['author_xchan']);
$profile_name = $item['author']['xchan_name'];
$location = format_location($item);
@@ -295,7 +295,7 @@ class ThreadItem {
$owner_address = substr($item['owner']['xchan_addr'],0,strpos($item['owner']['xchan_addr'],'@'));
$viewthread = $item['llink'];
if($conv->get_mode() === 'channel')
- $viewthread = z_root() . '/channel/' . $owner_address . '?f=&mid=' . $item['mid'];
+ $viewthread = z_root() . '/channel/' . $owner_address . '?f=&mid=' . urlencode($item['mid']);
$comment_count_txt = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
$list_unseen_txt = (($unseen_comments) ? sprintf('%d unseen',$unseen_comments) : '');
@@ -335,6 +335,8 @@ class ThreadItem {
'wall' => t('Wall-to-Wall'),
'vwall' => t('via Wall-To-Wall:'),
'profile_url' => $profile_link,
+ 'thread_action_menu' => thread_action_menu($item,$conv->get_mode()),
+ 'thread_author_menu' => thread_author_menu($item,$conv->get_mode()),
'item_photo_menu' => item_photo_menu($item),
'dreport' => $dreport,
'name' => $profile_name,
@@ -407,7 +409,7 @@ class ThreadItem {
'comment' => $this->get_comment_box($indent),
'previewing' => ($conv->is_preview() ? ' preview ' : ''),
'wait' => t('Please wait'),
- 'submid' => substr($item['mid'],0,32),
+ 'submid' => str_replace(['+','='], ['',''], base64_encode(substr($item['mid'],0,32))),
'thread_level' => $thread_level
);
@@ -765,7 +767,7 @@ class ThreadItem {
return;
if($this->is_toplevel() && ($this->get_data_value('author_xchan') != $this->get_data_value('owner_xchan'))) {
- $this->owner_url = chanlink_url($this->data['owner']['xchan_url']);
+ $this->owner_url = chanlink_hash($this->data['owner']['xchan_hash']);
$this->owner_photo = $this->data['owner']['xchan_photo_m'];
$this->owner_name = $this->data['owner']['xchan_name'];
$this->wall_to_wall = true;