2013-06-28 02:35:59 +00:00
< ? php /** @file */
2013-07-04 02:30:52 +00:00
require_once ( 'include/items.php' );
2013-12-30 12:25:55 +00:00
require_once ( 'include/contact_selectors.php' );
2013-06-28 02:35:59 +00:00
function thing_init ( & $a ) {
if ( ! local_user ())
return ;
2014-02-12 00:56:39 +00:00
2013-06-28 02:35:59 +00:00
$account_id = $a -> get_account ();
2013-07-04 02:30:52 +00:00
$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' ]);
2013-07-04 02:30:52 +00:00
$verb = escape_tags ( $_REQUEST [ 'verb' ]);
2014-02-12 22:44:27 +00:00
$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' ];
2013-12-28 21:28:49 +00:00
$photo = $_REQUEST [ 'img' ];
2013-06-28 02:35:59 +00:00
$hash = random_string ();
2013-07-04 02:30:52 +00:00
$verbs = obj_verbs ();
/**
* verbs : [ 0 ] = first person singular , e . g . " I want " , [ 1 ] = 3 rd 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
/**
* 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 ;
}
2013-07-04 02:30:52 +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 " .
*/
/**
* 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 ;
2013-12-30 12:25:55 +00:00
if ( $term_hash ) {
$t = q ( " select * from obj left join term on obj_obj = term_hash where term_hash != '' and obj_type = %d and term_hash = '%s' limit 1 " ,
intval ( TERM_OBJ_THING ),
dbesc ( $term_hash )
);
if ( ! $t ) {
notice ( t ( 'Item not found.' ) . EOL );
return ;
}
$orig_record = $t [ 0 ];
if ( $photo != $orig_record [ 'imgurl' ]) {
$arr = import_profile_photo ( $photo , get_observer_hash (), true );
$local_photo = $arr [ 0 ];
$local_photo_type = $arr [ 3 ];
}
else
$local_photo = $orig_record [ 'imgurl' ];
$r = q ( " update term set term = '%s', url = '%s', imgurl = '%s' where term_hash = '%s' and uid = %d limit 1 " ,
dbesc ( $name ),
dbesc (( $url ) ? $url : z_root () . '/thing/' . $term_hash ),
dbesc ( $local_photo ),
dbesc ( $term_hash ),
intval ( local_user ())
);
info ( t ( 'Thing updated' ) . EOL );
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 " ,
intval ( local_user ())
);
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 ;
2013-12-28 21:28:49 +00:00
$local_photo = null ;
if ( $photo ) {
$arr = import_profile_photo ( $photo , get_observer_hash (), true );
$local_photo = $arr [ 0 ];
$local_photo_type = $arr [ 3 ];
}
2013-07-04 02:30:52 +00:00
$r = q ( " select * from term where uid = %d and otype = %d and type = %d and term = '%s' limit 1 " ,
2013-06-28 02:35:59 +00:00
intval ( local_user ()),
2013-07-04 02:30:52 +00:00
intval ( TERM_OBJ_THING ),
intval ( TERM_THING ),
dbesc ( $name )
);
if ( ! $r ) {
$r = q ( " insert into term ( aid, uid, oid, otype, type, term, url, imgurl, term_hash )
values ( % d , % d , % d , % d , % d , '%s' , '%s' , '%s' , '%s' ) " ,
intval ( $account_id ),
intval ( local_user ()),
0 ,
intval ( TERM_OBJ_THING ),
intval ( TERM_THING ),
dbesc ( $name ),
dbesc (( $url ) ? $url : z_root () . '/thing/' . $hash ),
2013-12-28 21:28:49 +00:00
dbesc (( $photo ) ? $local_photo : '' ),
2013-07-04 02:30:52 +00:00
dbesc ( $hash )
);
$r = q ( " select * from term where uid = %d and otype = %d and type = %d and term = '%s' limit 1 " ,
intval ( local_user ()),
intval ( TERM_OBJ_THING ),
intval ( TERM_THING ),
dbesc ( $name )
);
}
$term = $r [ 0 ];
2013-07-04 05:51:49 +00:00
$r = q ( " insert into obj ( obj_page, obj_verb, obj_type, obj_channel, obj_obj) values ('%s','%s', %d, %d, '%s') " ,
2013-07-05 03:35:42 +00:00
dbesc ( $profile [ 'profile_guid' ]),
2013-07-04 02:30:52 +00:00
dbesc ( $verb ),
intval ( TERM_OBJ_THING ),
2013-07-04 05:51:49 +00:00
intval ( local_user ()),
dbesc ( $term [ 'term_hash' ])
2013-06-28 02:35:59 +00:00
);
2013-07-04 02:30:52 +00:00
if ( ! $r ) {
2013-07-05 03:35:42 +00:00
notice ( t ( 'Object store: failed' ));
2013-07-04 02:30:52 +00:00
return ;
}
2013-12-30 12:25:55 +00:00
info ( t ( 'Thing added' ));
2013-07-04 02:30:52 +00:00
2013-12-28 21:28:49 +00:00
2014-02-12 22:44:27 +00:00
if ( $activity ) {
$arr = array ();
$links = array ( array ( 'rel' => 'alternate' , 'type' => 'text/html' , 'href' => $term [ 'url' ]));
if ( $local_photo )
$links [] = array ( 'rel' => 'photo' , 'type' => $local_photo_type , 'href' => $local_photo );
2013-07-04 02:30:52 +00:00
2014-02-12 22:44:27 +00:00
$objtype = ACTIVITY_OBJ_THING ;
2013-07-04 02:30:52 +00:00
2014-02-12 22:44:27 +00:00
$obj = json_encode ( array (
'type' => $objtype ,
'id' => $term [ 'url' ],
'link' => $links ,
'title' => $term [ 'term' ],
'content' => $term [ 'term' ]
));
2013-07-04 02:30:52 +00:00
2014-02-12 22:44:27 +00:00
$bodyverb = str_replace ( 'OBJ: ' , '' , t ( 'OBJ: %1$s %2$s %3$s' ));
2013-07-04 02:30:52 +00:00
2014-02-12 22:44:27 +00:00
$arr [ 'owner_xchan' ] = $channel [ 'channel_hash' ];
$arr [ 'author_xchan' ] = $channel [ 'channel_hash' ];
2013-12-28 21:28:49 +00:00
2013-07-05 03:35:42 +00:00
2014-02-12 22:44:27 +00:00
$arr [ 'item_flags' ] = ITEM_ORIGIN | ITEM_WALL | ITEM_THREAD_TOP ;
2013-07-04 02:30:52 +00:00
2014-02-12 22:44:27 +00:00
$ulink = '[zrl=' . $channel [ 'xchan_url' ] . ']' . $channel [ 'channel_name' ] . '[/zrl]' ;
$plink = '[zrl=' . $term [ 'url' ] . ']' . $term [ 'term' ] . '[/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' " ,
intval ( local_user ()),
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 () . '>' ;
}
2013-06-28 02:35:59 +00:00
2014-02-12 22:44:27 +00:00
$ret = post_activity_item ( $arr );
}
2013-06-28 02:35:59 +00:00
}
function thing_content ( & $a ) {
2013-07-04 02:30:52 +00:00
2013-12-30 12:25:55 +00:00
if ( argc () == 2 ) {
2013-12-29 08:51:27 +00:00
$r = q ( " select * from obj left join term on obj_obj = term_hash where term_hash != '' and obj_type = %d and term_hash = '%s' 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' ),
'$canedit' => (( local_user () && local_user () == $r [ 0 ][ 'obj_channel' ]) ? true : false ),
'$thing' => $r [ 0 ] ));
2013-12-29 08:51:27 +00:00
}
else {
notice ( t ( 'item not found.' ) . EOL );
return ;
}
2013-07-04 02:30:52 +00:00
}
2013-06-28 02:35:59 +00:00
2014-06-24 00:05:03 +00:00
$channel = $a -> get_channel ();
if ( ! ( local_user () && $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
$thing_hash = '' ;
2014-06-24 00:05:03 +00:00
2013-12-30 12:25:55 +00:00
if ( argc () == 3 && argv ( 1 ) === 'edit' ) {
$thing_hash = argv ( 2 );
$r = q ( " select * from obj left join term on obj_obj = term_hash where term_hash != '' and obj_type = %d and term_hash = '%s' limit 1 " ,
intval ( TERM_OBJ_THING ),
dbesc ( $thing_hash )
);
if (( ! $r ) || ( $r [ 0 ][ 'obj_channel' ] != local_user ())) {
notice ( t ( 'Permission denied.' ) . EOL );
return '' ;
}
$o .= replace_macros ( get_markup_template ( 'thing_edit.tpl' ), array (
'$thing_hdr' => t ( 'Edit Thing' ),
'$multiprof' => feature_enabled ( local_user (), 'multi_profiles' ),
'$profile_lbl' => t ( 'Select a profile' ),
'$profile_select' => contact_profile_assign ( $r [ 0 ][ 'obj_page' ]),
2014-06-24 00:05:03 +00:00
'$verb_lbl' => $channel [ 'channel_name' ],
2013-12-30 12:25:55 +00:00
'$verb_select' => obj_verb_selector ( $r [ 0 ][ 'obj_verb' ]),
2014-02-12 22:44:27 +00:00
'$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' ),
'$thething' => $r [ 0 ][ 'term' ],
'$url_lbl' => t ( 'URL of thing (optional)' ),
'$theurl' => $r [ 0 ][ 'url' ],
'$img_lbl' => t ( 'URL for photo of thing (optional)' ),
'$imgurl' => $r [ 0 ][ 'imgurl' ],
'$submit' => t ( 'Submit' )
));
return $o ;
}
if ( argc () == 3 && argv ( 1 ) === 'drop' ) {
$thing_hash = argv ( 2 );
$r = q ( " select * from obj left join term on obj_obj = term_hash where term_hash != '' and obj_type = %d and term_hash = '%s' limit 1 " ,
intval ( TERM_OBJ_THING ),
dbesc ( $thing_hash )
);
if (( ! $r ) || ( $r [ 0 ][ 'obj_channel' ] != local_user ())) {
notice ( t ( 'Permission denied.' ) . EOL );
return '' ;
}
$x = q ( " delete from obj where obj_obj = '%s' and obj_type = %d and obj_channel = %d limit 1 " ,
dbesc ( $thing_hash ),
intval ( TERM_OBJ_THING ),
intval ( local_user ())
);
$x = q ( " delete from term where term_hash = '%s' and uid = %d limit 1 " ,
dbesc ( $thing_hash ),
intval ( local_user ())
);
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' ),
2013-07-05 03:35:42 +00:00
'$multiprof' => feature_enabled ( local_user (), 'multi_profiles' ),
'$profile_lbl' => t ( 'Select a profile' ),
'$profile_select' => contact_profile_assign ( '' ),
2014-06-24 00:05:03 +00:00
'$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)' ),
2013-07-05 03:35:42 +00:00
'$submit' => t ( 'Submit' )
));
return $o ;
2013-06-28 02:35:59 +00:00
}