streams/mod/thing.php

364 lines
10 KiB
PHP
Raw Normal View History

<?php
/**
* @file mod/thing.php
* @brief
*/
2013-06-28 02:35:59 +00:00
require_once('include/items.php');
2015-09-02 02:27:41 +00:00
require_once('include/security.php');
2013-12-30 12:25:55 +00:00
require_once('include/contact_selectors.php');
2015-09-02 02:27:41 +00:00
require_once('include/acl_selectors.php');
2013-06-28 02:35:59 +00:00
function thing_init(&$a) {
2015-01-29 04:56:04 +00:00
if(! local_channel())
2013-06-28 02:35:59 +00:00
return;
$account_id = $a->get_account();
$channel = $a->get_channel();
2013-12-30 12:25:55 +00:00
$term_hash = (($_REQUEST['term_hash']) ? $_REQUEST['term_hash'] : '');
2013-06-28 02:35:59 +00:00
$name = escape_tags($_REQUEST['term']);
$verb = escape_tags($_REQUEST['verb']);
$activity = intval($_REQUEST['activity']);
2014-02-12 00:56:39 +00:00
$profile_guid = escape_tags($_REQUEST['profile_assign']);
2013-06-28 02:35:59 +00:00
$url = $_REQUEST['link'];
$photo = $_REQUEST['img'];
2013-06-28 02:35:59 +00:00
$hash = random_string();
$verbs = obj_verbs();
/**
* verbs: [0] = first person singular, e.g. "I want", [1] = 3rd person singular, e.g. "Bill wants"
* 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.
*/
$translated_verb = $verbs[$verb][1];
2013-07-04 03:37:39 +00:00
/*
2013-07-04 03:37:39 +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
* support requests. It isn't because we're trying to be heavy-handed
* about what you can and can't do.
*/
if(! $translated_verb) {
if(is_site_admin())
$translated_verb = $verb;
}
/*
* 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".
*/
/*
* 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.
*/
if((! $name) || (! $translated_verb))
return;
2015-09-02 02:01:19 +00:00
$acl = new AccessList($channel);
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);
}
$x = $acl->get();
2013-12-30 12:25:55 +00:00
if($term_hash) {
2015-09-02 00:06:58 +00:00
$t = q("select * from obj where obj_obj = '%s' and obj_channel = %d limit 1",
dbesc($term_hash),
intval(local_channel())
2013-12-30 12:25:55 +00:00
);
if(! $t) {
notice( t('Item not found.') . EOL);
return;
}
$orig_record = $t[0];
2015-09-02 00:06:58 +00:00
if($photo != $orig_record['obj_imgurl']) {
$arr = import_xchan_photo($photo,get_observer_hash(),true);
2013-12-30 12:25:55 +00:00
$local_photo = $arr[0];
$local_photo_type = $arr[3];
}
else
2015-09-02 02:01:19 +00:00
$local_photo = $orig_record['obj_imgurl'];
2013-12-30 12:25:55 +00:00
2015-09-02 02:01:19 +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 ",
2013-12-30 12:25:55 +00:00
dbesc($name),
dbesc(($url) ? $url : z_root() . '/thing/' . $term_hash),
dbesc($local_photo),
2015-09-02 00:06:58 +00:00
dbesc(datetime_convert()),
2015-09-02 02:01:19 +00:00
dbesc($x['allow_cid']),
dbesc($x['allow_gid']),
dbesc($x['deny_cid']),
dbesc($x['deny_gid']),
2013-12-30 12:25:55 +00:00
dbesc($term_hash),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2013-12-30 12:25:55 +00:00
);
info( t('Thing updated') . EOL);
$r = q("select * from obj where obj_channel = %d and obj_obj = '%s' limit 1",
intval(local_channel()),
dbesc($term_hash)
);
if($r) {
build_sync_packet(0, array('obj' => $r));
}
2013-12-30 12:25:55 +00:00
return;
}
2013-07-05 03:35:42 +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",
2015-01-29 04:56:04 +00:00
intval(local_channel())
2013-07-05 03:35:42 +00:00
);
2014-02-12 00:56:39 +00:00
2013-07-05 03:35:42 +00:00
if($p)
$profile = $p[0];
else
2013-06-28 02:35:59 +00:00
return;
$local_photo = null;
if($photo) {
$arr = import_xchan_photo($photo,get_observer_hash(),true);
$local_photo = $arr[0];
$local_photo_type = $arr[3];
}
2015-09-02 00:06:58 +00:00
$created = datetime_convert();
$url = (($url) ? $url : z_root() . '/thing/' . $hash);
2015-09-02 02:01:19 +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') ",
2013-07-05 03:35:42 +00:00
dbesc($profile['profile_guid']),
dbesc($verb),
intval(TERM_OBJ_THING),
2015-01-29 04:56:04 +00:00
intval(local_channel()),
2015-09-02 00:06:58 +00:00
dbesc($hash),
dbesc($name),
dbesc($url),
dbesc(($photo) ? $local_photo : ''),
dbesc($created),
2015-09-02 02:01:19 +00:00
dbesc($created),
dbesc($x['allow_cid']),
dbesc($x['allow_gid']),
dbesc($x['deny_cid']),
dbesc($x['deny_gid'])
2013-06-28 02:35:59 +00:00
);
if(! $r) {
2013-07-05 03:35:42 +00:00
notice( t('Object store: failed'));
return;
}
2013-12-30 12:25:55 +00:00
info( t('Thing added'));
2015-09-02 00:06:58 +00:00
$r = q("select * from obj where obj_channel = %d and obj_obj = '%s' limit 1",
intval(local_channel()),
dbesc($hash)
);
if($r) {
build_sync_packet(0, array('obj' => $r));
}
if($activity) {
$arr = array();
2015-09-02 00:06:58 +00:00
$links = array(array('rel' => 'alternate','type' => 'text/html', 'href' => $url));
if($local_photo)
$links[] = array('rel' => 'photo', 'type' => $local_photo_type, 'href' => $local_photo);
$objtype = ACTIVITY_OBJ_THING;
$obj = json_encode(array(
'type' => $objtype,
2015-09-02 00:06:58 +00:00
'id' => $url,
'link' => $links,
2015-09-02 00:06:58 +00:00
'title' => $name,
'content' => $name
));
$bodyverb = str_replace('OBJ: ', '',t('OBJ: %1$s %2$s %3$s'));
$arr['owner_xchan'] = $channel['channel_hash'];
$arr['author_xchan'] = $channel['channel_hash'];
$arr['item_origin'] = 1;
$arr['item_wall'] = 1;
$arr['item_thread_top'] = 1;
2013-07-05 03:35:42 +00:00
$ulink = '[zrl=' . $channel['xchan_url'] . ']' . $channel['channel_name'] . '[/zrl]';
2015-09-02 00:06:58 +00:00
$plink = '[zrl=' . $url . ']' . $name . '[/zrl]';
$arr['body'] = sprintf( $bodyverb, $ulink, $translated_verb, $plink );
if($local_photo)
$arr['body'] .= "\n\n[zmg]" . $local_photo . "[/zmg]";
$arr['verb'] = $verb;
$arr['obj_type'] = $objtype;
$arr['object'] = $obj;
if(! $profile['is_default']) {
$arr['item_private'] = true;
$str = '';
$r = q("select abook_xchan from abook where abook_channel = %d and abook_profile = '%s'",
2015-01-29 04:56:04 +00:00
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() . '>';
}
$ret = post_activity_item($arr);
}
2013-06-28 02:35:59 +00:00
}
function thing_content(&$a) {
2015-09-15 04:25:10 +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.
2013-12-30 12:25:55 +00:00
if(argc() == 2) {
2015-09-15 04:25:10 +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)
$sql_extra = permissions_sql($r[0]['obj_channel']);
2015-09-02 02:27:41 +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))
);
if($r) {
2013-12-30 12:25:55 +00:00
return replace_macros(get_markup_template('show_thing.tpl'), array(
'$header' => t('Show Thing'),
'$edit' => t('Edit'),
'$delete' => t('Delete'),
2015-01-29 04:56:04 +00:00
'$canedit' => ((local_channel() && local_channel() == $r[0]['obj_channel']) ? true : false),
2013-12-30 12:25:55 +00:00
'$thing' => $r[0] ));
}
else {
notice( t('item not found.') . EOL);
return;
}
}
2013-06-28 02:35:59 +00:00
$channel = $a->get_channel();
2015-01-29 04:56:04 +00:00
if(! (local_channel() && $channel)) {
2013-12-30 23:41:42 +00:00
notice( t('Permission denied.') . EOL);
2013-12-30 12:25:55 +00:00
return;
2013-12-30 23:41:42 +00:00
}
2013-12-30 12:25:55 +00:00
2015-09-02 02:27:41 +00:00
$acl = new AccessList($channel);
$channel_acl = $acl->get();
$lockstate = (($acl->is_private()) ? 'lock' : 'unlock');
2013-12-30 12:25:55 +00:00
$thing_hash = '';
if(argc() == 3 && argv(1) === 'edit') {
$thing_hash = argv(2);
2015-09-02 00:06:58 +00:00
$r = q("select * from obj where obj_type = %d and obj_obj = '%s' limit 1",
2013-12-30 12:25:55 +00:00
intval(TERM_OBJ_THING),
dbesc($thing_hash)
);
2015-01-29 04:56:04 +00:00
if((! $r) || ($r[0]['obj_channel'] != local_channel())) {
2013-12-30 12:25:55 +00:00
notice( t('Permission denied.') . EOL);
return '';
}
$o .= replace_macros(get_markup_template('thing_edit.tpl'),array(
'$thing_hdr' => t('Edit Thing'),
2015-01-29 04:56:04 +00:00
'$multiprof' => feature_enabled(local_channel(),'multi_profiles'),
2013-12-30 12:25:55 +00:00
'$profile_lbl' => t('Select a profile'),
'$profile_select' => contact_profile_assign($r[0]['obj_page']),
'$verb_lbl' => $channel['channel_name'],
2013-12-30 12:25:55 +00:00
'$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')),
2013-12-30 12:25:55 +00:00
'$thing_hash' => $thing_hash,
'$thing_lbl' => t('Name of thing e.g. something'),
2015-09-02 00:06:58 +00:00
'$thething' => $r[0]['obj_term'],
2013-12-30 12:25:55 +00:00
'$url_lbl' => t('URL of thing (optional)'),
2015-09-02 00:06:58 +00:00
'$theurl' => $r[0]['obj_url'],
2013-12-30 12:25:55 +00:00
'$img_lbl' => t('URL for photo of thing (optional)'),
2015-09-02 00:06:58 +00:00
'$imgurl' => $r[0]['obj_imgurl'],
2015-09-02 02:27:41 +00:00
'$permissions' => t('Permissions'),
'$aclselect' => populate_acl($channel_acl,false),
'$lockstate' => $lockstate,
2013-12-30 12:25:55 +00:00
'$submit' => t('Submit')
));
return $o;
}
if(argc() == 3 && argv(1) === 'drop') {
$thing_hash = argv(2);
2015-09-02 00:06:58 +00:00
$r = q("select * from obj where obj_type = %d and obj_obj = '%s' limit 1",
2013-12-30 12:25:55 +00:00
intval(TERM_OBJ_THING),
dbesc($thing_hash)
);
2015-01-29 04:56:04 +00:00
if((! $r) || ($r[0]['obj_channel'] != local_channel())) {
2013-12-30 12:25:55 +00:00
notice( t('Permission denied.') . EOL);
return '';
}
PostgreSQL support initial commit There were 11 main types of changes: - UPDATE's and DELETE's sometimes had LIMIT 1 at the end of them. This is not only non-compliant but it would certainly not do what whoever wrote it thought it would. It is likely this mistake was just copied from Friendica. All of these instances, the LIMIT 1 was simply removed. - Bitwise operations (and even some non-zero int checks) erroneously rely on MySQL implicit integer-boolean conversion in the WHERE clauses. This is non-compliant (and bad programming practice to boot). Proper explicit boolean conversions were added. New queries should use proper conventions. - MySQL has a different operator for bitwise XOR than postgres. Rather than add yet another dba_ func, I converted them to "& ~" ("AND NOT") when turning off, and "|" ("OR") when turning on. There were no true toggles (XOR). New queries should refrain from using XOR when not necessary. - There are several fields which the schema has marked as NOT NULL, but the inserts don't specify them. The reason this works is because mysql totally ignores the constraint and adds an empty text default automatically. Again, non-compliant, obviously. In these cases a default of empty text was added. - Several statements rely on a non-standard MySQL feature (http://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html). These queries can all be rewritten to be standards compliant. Interestingly enough, the newly rewritten standards compliant queries run a zillion times faster, even on MySQL. - A couple of function/operator name translations were needed (RAND/RANDOM, GROUP_CONCAT/STRING_AGG, UTC_NOW, REGEXP/~, ^/#) -- assist functions added in the dba_ - INTERVALs: postgres requires quotes around the value, mysql requires that there are not quotes around the value -- assist functions added in the dba_ - NULL_DATE's -- Postgres does not allow the invalid date '0000-00-00 00:00:00' (there is no such thing as year 0 or month 0 or day 0). We use '0001-01-01 00:00:00' for postgres. Conversions are handled in Zot/item packets automagically by quoting all dates with dbescdate(). - char(##) specifications in the schema creates fields with blank spaces that aren't trimmed in the code. MySQL apparently treats char(##) as varchar(##), again, non-compliant. Since postgres works better with text fields anyway, this ball of bugs was simply side-stepped by using 'text' datatype for all text fields in the postgres schema. varchar was used in a couple of places where it actually seemed appropriate (size constraint), but without rigorously vetting that all of the PHP code actually validates data, new bugs might come out from under the rug. - postgres doesn't store nul bytes and a few other non-printables in text fields, even when quoted. bytea fields were used when storing binary data (photo.data, attach.data). A new dbescbin() function was added to handle this transparently. - postgres does not support LIMIT #,# syntax. All databases support LIMIT # OFFSET # syntax. Statements were updated to be standard. These changes require corresponding changes in the coding standards. Please review those before adding any code going forward. Still on my TODO list: - remove quotes from non-reserved identifiers and make reserved identifiers use dba func for quoting - Rewrite search queries for better results (both MySQL and Postgres)
2014-11-13 20:21:58 +00:00
$x = q("delete from obj where obj_obj = '%s' and obj_type = %d and obj_channel = %d",
2013-12-30 12:25:55 +00:00
dbesc($thing_hash),
intval(TERM_OBJ_THING),
2015-01-29 04:56:04 +00:00
intval(local_channel())
2013-12-30 12:25:55 +00:00
);
$r[0]['obj_deleted'] = 1;
build_sync_packet(0,array('obj' => $r));
2013-12-30 12:25:55 +00:00
return $o;
}
2013-07-05 03:35:42 +00:00
$o .= replace_macros(get_markup_template('thing_input.tpl'),array(
2013-12-30 12:25:55 +00:00
'$thing_hdr' => t('Add Thing to your Profile'),
2015-01-29 04:56:04 +00:00
'$multiprof' => feature_enabled(local_channel(),'multi_profiles'),
2013-07-05 03:35:42 +00:00
'$profile_lbl' => t('Select a profile'),
'$profile_select' => contact_profile_assign(''),
'$verb_lbl' => $channel['channel_name'],
2014-08-11 01:18:41 +00:00
'$activity' => array('activity',t('Post an activity'),((array_key_exists('activity',$_REQUEST)) ? $_REQUEST['activity'] : true),t('Only sends to viewers of the applicable profile')),
2013-07-05 03:35:42 +00:00
'$verb_select' => obj_verb_selector(),
2013-12-30 12:25:55 +00:00
'$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)'),
2015-09-02 02:27:41 +00:00
'$permissions' => t('Permissions'),
'$aclselect' => populate_acl($channel_acl,false),
'$lockstate' => $lockstate,
2013-07-05 03:35:42 +00:00
'$submit' => t('Submit')
));
return $o;
2013-06-28 02:35:59 +00:00
}