diff options
author | Mario Vavti <mario@mariovavti.com> | 2017-03-08 09:39:46 +0100 |
---|---|---|
committer | Mario Vavti <mario@mariovavti.com> | 2017-03-08 09:39:46 +0100 |
commit | bc2b948f1f6e62b1c277a4042200bb6678956f3f (patch) | |
tree | 8586c30e495607eee23f16c0aad40974f0711275 /Zotlabs/Lib/NativeWiki.php | |
parent | 23e3e2c50499fab52769929a448e73012fd915af (diff) | |
parent | ff9442474d07cce24c8f66db39ec34471c3874a2 (diff) | |
download | volse-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/NativeWiki.php')
-rw-r--r-- | Zotlabs/Lib/NativeWiki.php | 210 |
1 files changed, 210 insertions, 0 deletions
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); + } + } +} |