streams/Zotlabs/Module/Thing.php

393 lines
11 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
namespace Zotlabs\Module;
2018-06-01 04:05:09 +00:00
2019-04-17 23:54:06 +00:00
use App;
use Zotlabs\Web\Controller;
use Zotlabs\Lib\Libprofile;
2018-06-05 01:40:11 +00:00
use Zotlabs\Lib\Libsync;
2018-06-01 04:05:09 +00:00
2019-04-17 23:54:06 +00:00
/**
* @file Zotlabs/Module/Thing.php
*/
2016-04-19 03:38:38 +00:00
require_once('include/security.php');
2019-01-18 21:04:05 +00:00
2016-04-19 03:38:38 +00:00
require_once('include/acl_selectors.php');
2019-04-17 23:54:06 +00:00
class Thing extends Controller {
2016-04-19 03:38:38 +00:00
function init() {
2016-04-19 03:38:38 +00:00
if(! local_channel())
return;
2016-04-19 03:38:38 +00:00
$channel = \App::get_channel();
if($_SERVER['REQUEST_METHOD'] === 'GET' && argc() < 2) {
2019-04-17 23:54:06 +00:00
Libprofile::load($channel['channel_address']);
}
2016-04-19 03:38:38 +00:00
$term_hash = (($_REQUEST['term_hash']) ? $_REQUEST['term_hash'] : '');
2016-04-19 03:38:38 +00:00
$name = escape_tags($_REQUEST['term']);
$verb = escape_tags($_REQUEST['verb']);
$activity = intval($_REQUEST['activity']);
$profile_guid = escape_tags($_REQUEST['profile_assign']);
$url = $_REQUEST['url'];
2016-04-19 03:38:38 +00:00
$photo = $_REQUEST['img'];
2016-04-19 03:38:38 +00:00
$hash = random_string();
2016-04-19 03:38:38 +00:00
$verbs = obj_verbs();
2016-04-19 03:38:38 +00:00
/**
* verbs: [0] = first person singular, e.g. "I want", [1] = 3rd person singular, e.g. "Bill wants"
2016-04-19 03:38:38 +00:00
* We use the first person form when creating an activity, but the third person for use in activities
* @FIXME There is no accounting for verb gender for languages where this is significant. We may eventually
* require obj_verbs() to provide full conjugations and specify which form to use in the $_REQUEST params to this module.
*/
2016-04-19 03:38:38 +00:00
$translated_verb = $verbs[$verb][1];
2016-04-19 03:38:38 +00:00
/*
* The site administrator can do things that normals cannot.
* This is restricted because it will likely cause
* an activitystreams protocol violation and the activity might
* choke in some other network and result in unnecessary
2016-04-19 03:38:38 +00:00
* support requests. It isn't because we're trying to be heavy-handed
* about what you can and can't do.
2016-04-19 03:38:38 +00:00
*/
2016-04-19 03:38:38 +00:00
if(! $translated_verb) {
if(is_site_admin())
$translated_verb = $verb;
}
2016-04-19 03:38:38 +00:00
/*
* Things, objects: We do not provide definite (a, an) or indefinite (the) articles or singular/plural designators
* That needs to be specified in your thing. e.g. Mike has "a carrot", Greg wants "balls", Bob likes "the Boston Red Sox".
2016-04-19 03:38:38 +00:00
*/
2016-04-19 03:38:38 +00:00
/*
* Future work on this module might produce more complex activities with targets, e.g. Phillip likes Karen's moustache
* and to describe other non-thing objects like channels, such as Karl wants Susan - where Susan represents a channel profile.
*/
2016-04-19 03:38:38 +00:00
if((! $name) || (! $translated_verb))
return;
2019-02-15 00:01:40 +00:00
$acl = new \Zotlabs\Access\AccessControl($channel);
2016-04-19 03:38:38 +00:00
if(array_key_exists('contact_allow',$_REQUEST)
|| array_key_exists('group_allow',$_REQUEST)
|| array_key_exists('contact_deny',$_REQUEST)
|| array_key_exists('group_deny',$_REQUEST)) {
$acl->set_from_array($_REQUEST);
}
2016-04-19 03:38:38 +00:00
$x = $acl->get();
2016-04-19 03:38:38 +00:00
if($term_hash) {
$t = q("select * from obj where obj_obj = '%s' and obj_channel = %d limit 1",
dbesc($term_hash),
intval(local_channel())
);
if(! $t) {
notice( t('Item not found.') . EOL);
return;
}
$orig_record = $t[0];
if($photo != $orig_record['obj_imgurl']) {
2017-10-04 00:03:24 +00:00
delete_thing_photo($orig_record['obj_imgurl'],get_observer_hash());
2016-04-19 03:38:38 +00:00
$arr = import_xchan_photo($photo,get_observer_hash(),true);
$local_photo = $arr[0];
$local_photo_type = $arr[3];
}
else
$local_photo = $orig_record['obj_imgurl'];
2016-04-19 03:38:38 +00:00
$r = q("update obj set obj_term = '%s', obj_url = '%s', obj_imgurl = '%s', obj_edited = '%s', allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s' where obj_obj = '%s' and obj_channel = %d ",
dbesc($name),
dbesc(($url) ? $url : z_root() . '/thing/' . $term_hash),
dbesc($local_photo),
dbesc(datetime_convert()),
dbesc($x['allow_cid']),
dbesc($x['allow_gid']),
dbesc($x['deny_cid']),
dbesc($x['deny_gid']),
dbesc($term_hash),
intval(local_channel())
);
2016-04-19 03:38:38 +00:00
info( t('Thing updated') . EOL);
2016-04-19 03:38:38 +00:00
$r = q("select * from obj where obj_channel = %d and obj_obj = '%s' limit 1",
intval(local_channel()),
dbesc($term_hash)
);
if($r) {
2018-06-05 01:40:11 +00:00
Libsync::build_sync_packet(0, array('obj' => $r));
2016-04-19 03:38:38 +00:00
}
2016-04-19 03:38:38 +00:00
return;
}
2016-04-19 03:38:38 +00:00
$sql = (($profile_guid) ? " and profile_guid = '" . dbesc($profile_guid) . "' " : " and is_default = 1 ");
$p = q("select profile_guid, is_default from profile where uid = %d $sql limit 1",
intval(local_channel())
);
2016-04-19 03:38:38 +00:00
if($p)
$profile = $p[0];
else
return;
2016-04-19 03:38:38 +00:00
$local_photo = null;
2016-04-19 03:38:38 +00:00
if($photo) {
$arr = import_xchan_photo($photo,get_observer_hash(),true);
$local_photo = $arr[0];
$local_photo_type = $arr[3];
}
2016-04-19 03:38:38 +00:00
$created = datetime_convert();
$url = (($url) ? $url : z_root() . '/thing/' . $hash);
2016-04-19 03:38:38 +00:00
$r = q("insert into obj ( obj_page, obj_verb, obj_type, obj_channel, obj_obj, obj_term, obj_url, obj_imgurl, obj_created, obj_edited, allow_cid, allow_gid, deny_cid, deny_gid ) values ('%s','%s', %d, %d, '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s') ",
dbesc($profile['profile_guid']),
dbesc($verb),
intval(TERM_OBJ_THING),
intval(local_channel()),
dbesc($hash),
dbesc($name),
dbesc($url),
dbesc(($photo) ? $local_photo : ''),
dbesc($created),
dbesc($created),
dbesc($x['allow_cid']),
dbesc($x['allow_gid']),
dbesc($x['deny_cid']),
dbesc($x['deny_gid'])
);
2016-04-19 03:38:38 +00:00
if(! $r) {
notice( t('Object store: failed'));
return;
}
2016-04-19 03:38:38 +00:00
info( t('Thing added'));
2016-04-19 03:38:38 +00:00
$r = q("select * from obj where obj_channel = %d and obj_obj = '%s' limit 1",
intval(local_channel()),
dbesc($hash)
);
if($r) {
2018-06-05 01:40:11 +00:00
Libsync::build_sync_packet(0, array('obj' => $r));
2016-04-19 03:38:38 +00:00
}
2016-04-19 03:38:38 +00:00
if($activity) {
$arr = array();
$links = array(array('rel' => 'alternate','type' => 'text/html', 'href' => $url));
if($local_photo)
$links[] = array('rel' => 'photo', 'type' => $local_photo_type, 'href' => $local_photo);
2016-04-19 03:38:38 +00:00
$objtype = ACTIVITY_OBJ_THING;
2016-04-19 03:38:38 +00:00
$obj = json_encode(array(
'type' => $objtype,
'id' => $url,
'link' => $links,
'title' => $name,
'content' => $name
));
2016-04-19 03:38:38 +00:00
$bodyverb = str_replace('OBJ: ', '',t('OBJ: %1$s %2$s %3$s'));
2016-04-19 03:38:38 +00:00
$arr['owner_xchan'] = $channel['channel_hash'];
$arr['author_xchan'] = $channel['channel_hash'];
2016-04-19 03:38:38 +00:00
$arr['item_origin'] = 1;
$arr['item_wall'] = 1;
$arr['item_thread_top'] = 1;
2016-04-19 03:38:38 +00:00
$ulink = '[zrl=' . $channel['xchan_url'] . ']' . $channel['channel_name'] . '[/zrl]';
$plink = '[zrl=' . $url . ']' . $name . '[/zrl]';
2016-04-19 03:38:38 +00:00
$arr['body'] = sprintf( $bodyverb, $ulink, $translated_verb, $plink );
2016-04-19 03:38:38 +00:00
if($local_photo)
$arr['body'] .= "\n\n[zmg]" . $local_photo . "[/zmg]";
2016-04-19 03:38:38 +00:00
$arr['verb'] = $verb;
$arr['obj_type'] = $objtype;
$arr['obj'] = $obj;
2016-04-19 03:38:38 +00:00
if(! $profile['is_default']) {
$arr['item_private'] = true;
$str = '';
$r = q("select abook_xchan from abook where abook_channel = %d and abook_profile = '%s'",
intval(local_channel()),
dbesc($profile_guid)
);
if($r) {
$arr['allow_cid'] = '';
foreach($r as $rr)
$arr['allow_cid'] .= '<' . $rr['abook_xchan'] . '>';
}
else
$arr['allow_cid'] = '<' . get_observer_hash() . '>';
}
2016-04-19 03:38:38 +00:00
$ret = post_activity_item($arr);
}
}
function get() {
2016-04-19 03:38:38 +00:00
// @FIXME one problem with things is we can't share them unless we provide the channel in the url
// so we can definitively lookup the owner.
2016-04-19 03:38:38 +00:00
if(argc() == 2) {
2016-04-19 03:38:38 +00:00
$r = q("select obj_channel from obj where obj_type = %d and obj_obj = '%s' limit 1",
intval(TERM_OBJ_THING),
dbesc(argv(1))
);
if($r)
2016-04-19 03:38:38 +00:00
$sql_extra = permissions_sql($r[0]['obj_channel']);
2016-04-19 03:38:38 +00:00
$r = q("select * from obj where obj_type = %d and obj_obj = '%s' $sql_extra limit 1",
intval(TERM_OBJ_THING),
dbesc(argv(1))
);
2016-04-19 03:38:38 +00:00
if($r) {
return replace_macros(get_markup_template('show_thing.tpl'), array(
'$header' => t('Show Thing'),
'$edit' => t('Edit'),
'$delete' => t('Delete'),
'$canedit' => ((local_channel() && local_channel() == $r[0]['obj_channel']) ? true : false),
2016-04-19 03:38:38 +00:00
'$thing' => $r[0] ));
}
else {
notice( t('item not found.') . EOL);
return;
}
}
2016-04-19 03:38:38 +00:00
$channel = \App::get_channel();
2016-04-19 03:38:38 +00:00
if(! (local_channel() && $channel)) {
notice( t('Permission denied.') . EOL);
return;
}
2019-02-15 00:01:40 +00:00
$acl = new \Zotlabs\Access\AccessControl($channel);
2016-04-19 03:38:38 +00:00
$channel_acl = $acl->get();
2016-04-19 03:38:38 +00:00
$lockstate = (($acl->is_private()) ? 'lock' : 'unlock');
2016-04-19 03:38:38 +00:00
$thing_hash = '';
2016-04-19 03:38:38 +00:00
if(argc() == 3 && argv(1) === 'edit') {
$thing_hash = argv(2);
2016-04-19 03:38:38 +00:00
$r = q("select * from obj where obj_type = %d and obj_obj = '%s' limit 1",
intval(TERM_OBJ_THING),
dbesc($thing_hash)
);
2016-04-19 03:38:38 +00:00
if((! $r) || ($r[0]['obj_channel'] != local_channel())) {
notice( t('Permission denied.') . EOL);
return '';
}
2016-04-19 03:38:38 +00:00
$o .= replace_macros(get_markup_template('thing_edit.tpl'),array(
'$thing_hdr' => t('Edit Thing'),
'$multiprof' => feature_enabled(local_channel(),'multi_profiles'),
'$profile_lbl' => t('Select a profile'),
'$profile_select' => contact_profile_assign($r[0]['obj_page']),
'$verb_lbl' => $channel['channel_name'],
'$verb_select' => obj_verb_selector($r[0]['obj_verb']),
'$activity' => array('activity',t('Post an activity'),true,t('Only sends to viewers of the applicable profile')),
'$thing_hash' => $thing_hash,
'$thing_lbl' => t('Name of thing e.g. something'),
'$thething' => $r[0]['obj_term'],
'$url_lbl' => t('URL of thing (optional)'),
'$theurl' => $r[0]['obj_url'],
'$img_lbl' => t('URL for photo of thing (optional)'),
'$imgurl' => $r[0]['obj_imgurl'],
'$permissions' => t('Permissions'),
'$aclselect' => populate_acl($channel_acl,false),
2016-08-05 11:37:47 +00:00
'$allow_cid' => acl2json($channel_acl['allow_cid']),
'$allow_gid' => acl2json($channel_acl['allow_gid']),
'$deny_cid' => acl2json($channel_acl['deny_cid']),
'$deny_gid' => acl2json($channel_acl['deny_gid']),
2016-04-19 03:38:38 +00:00
'$lockstate' => $lockstate,
'$submit' => t('Submit')
));
2016-04-19 03:38:38 +00:00
return $o;
}
2016-04-19 03:38:38 +00:00
if(argc() == 3 && argv(1) === 'drop') {
$thing_hash = argv(2);
2016-04-19 03:38:38 +00:00
$r = q("select * from obj where obj_type = %d and obj_obj = '%s' limit 1",
intval(TERM_OBJ_THING),
dbesc($thing_hash)
);
2016-04-19 03:38:38 +00:00
if((! $r) || ($r[0]['obj_channel'] != local_channel())) {
notice( t('Permission denied.') . EOL);
return '';
}
2017-10-04 00:03:24 +00:00
delete_thing_photo($r[0]['obj_imgurl'],get_observer_hash());
2016-04-19 03:38:38 +00:00
$x = q("delete from obj where obj_obj = '%s' and obj_type = %d and obj_channel = %d",
dbesc($thing_hash),
intval(TERM_OBJ_THING),
intval(local_channel())
);
2016-04-19 03:38:38 +00:00
$r[0]['obj_deleted'] = 1;
2018-06-05 01:40:11 +00:00
Libsync::build_sync_packet(0,array('obj' => $r));
2016-04-19 03:38:38 +00:00
return $o;
}
2016-04-19 03:38:38 +00:00
$o .= replace_macros(get_markup_template('thing_input.tpl'),array(
'$thing_hdr' => t('Add Thing to your Profile'),
'$multiprof' => feature_enabled(local_channel(),'multi_profiles'),
'$profile_lbl' => t('Select a profile'),
'$profile_select' => contact_profile_assign(''),
'$verb_lbl' => $channel['channel_name'],
'$activity' => array('activity',t('Post an activity'),((array_key_exists('activity',$_REQUEST)) ? $_REQUEST['activity'] : true),t('Only sends to viewers of the applicable profile')),
'$verb_select' => obj_verb_selector(),
'$thing_lbl' => t('Name of thing e.g. something'),
'$url_lbl' => t('URL of thing (optional)'),
'$img_lbl' => t('URL for photo of thing (optional)'),
'$permissions' => t('Permissions'),
'$aclselect' => populate_acl($channel_acl,false),
2016-08-05 11:37:47 +00:00
'$allow_cid' => acl2json($channel_acl['allow_cid']),
'$allow_gid' => acl2json($channel_acl['allow_gid']),
'$deny_cid' => acl2json($channel_acl['deny_cid']),
'$deny_gid' => acl2json($channel_acl['deny_gid']),
2016-04-19 03:38:38 +00:00
'$lockstate' => $lockstate,
'$submit' => t('Submit')
));
2016-04-19 03:38:38 +00:00
return $o;
}
2016-04-19 03:38:38 +00:00
}