more hard slogging on the api

This commit is contained in:
friendica 2013-01-19 06:08:13 -08:00
parent d8d8dd5ced
commit 994f322d47

View File

@ -1,10 +1,11 @@
<?php <?php
require_once("bbcode.php");
require_once("datetime.php"); require_once("bbcode.php");
require_once("conversation.php"); require_once("datetime.php");
require_once("oauth.php"); require_once("conversation.php");
require_once("html2plain.php"); require_once("oauth.php");
require_once('include/security.php'); require_once("html2plain.php");
require_once('include/security.php');
/* /*
* *
@ -12,7 +13,13 @@
* *
*/ */
$API = Array();
/**
** TWITTER API
*/
$API = array();
$called_api = Null; $called_api = Null;
// All commands which require authentication accept a "channel" parameter // All commands which require authentication accept a "channel" parameter
@ -114,6 +121,7 @@
/************************** /**************************
* MAIN API ENTRY POINT * * MAIN API ENTRY POINT *
**************************/ **************************/
function api_call(&$a){ function api_call(&$a){
GLOBAL $API, $called_api; GLOBAL $API, $called_api;
@ -373,6 +381,7 @@
} }
// FIXME
function api_item_get_user(&$a, $item) { function api_item_get_user(&$a, $item) {
global $usercache; global $usercache;
@ -387,6 +396,7 @@
if(($normalised != 'mailbox') && (x($a->contacts[$normalised]))) if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
return api_get_user($a,$a->contacts[$normalised]['id']); return api_get_user($a,$a->contacts[$normalised]['id']);
} }
// We don't know this person directly. // We don't know this person directly.
list($nick, $name) = array_map("trim",explode("(",$item['author-name'])); list($nick, $name) = array_map("trim",explode("(",$item['author-name']));
@ -466,9 +476,6 @@
return $ret; return $ret;
} }
/**
** TWITTER API
*/
/** /**
* Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful;
@ -524,27 +531,27 @@
function api_statuses_mediap(&$a, $type) { function api_statuses_mediap(&$a, $type) {
if (api_user()===false) { if (api_user() === false) {
logger('api_statuses_update: no user'); logger('api_statuses_update: no user');
return false; return false;
} }
$user_info = api_get_user($a); $user_info = api_get_user($a);
$_REQUEST['type'] = 'wall'; $_REQUEST['type'] = 'wall';
$_REQUEST['profile_uid'] = api_user(); $_REQUEST['profile_uid'] = api_user();
$_REQUEST['api_source'] = true; $_REQUEST['api_source'] = true;
$txt = requestdata('status');
//$txt = urldecode(requestdata('status')); $txt = requestdata('status');
require_once('library/HTMLPurifier.auto.php'); require_once('library/HTMLPurifier.auto.php');
require_once('include/html2bbcode.php'); require_once('include/html2bbcode.php');
if((strpos($txt,'<') !== false) || (strpos($txt,'>') !== false)) { if((strpos($txt,'<') !== false) || (strpos($txt,'>') !== false)) {
$txt = html2bb_video($txt); $txt = html2bb_video($txt);
$config = HTMLPurifier_Config::createDefault(); $config = HTMLPurifier_Config::createDefault();
$config->set('Cache.DefinitionImpl', null); $config->set('Cache.DefinitionImpl', null);
$purifier = new HTMLPurifier($config); $purifier = new HTMLPurifier($config);
$txt = $purifier->purify($txt); $txt = $purifier->purify($txt);
} }
$txt = html2bbcode($txt); $txt = html2bbcode($txt);
@ -552,10 +559,10 @@
$_REQUEST['silent']='1'; //tell wall_upload function to return img info instead of echo $_REQUEST['silent']='1'; //tell wall_upload function to return img info instead of echo
require_once('mod/wall_upload.php'); require_once('mod/wall_upload.php');
$bebop = wall_upload_post($a); $posted = wall_upload_post($a);
//now that we have the img url in bbcode we can add it to the status and insert the wall item. //now that we have the img url in bbcode we can add it to the status and insert the wall item.
$_REQUEST['body']=$txt."\n\n".$bebop; $_REQUEST['body']=$txt."\n\n".$posted;
require_once('mod/item.php'); require_once('mod/item.php');
item_post($a); item_post($a);
@ -564,8 +571,6 @@
} }
api_register_func('api/statuses/mediap','api_statuses_mediap', true); api_register_func('api/statuses/mediap','api_statuses_mediap', true);
function api_statuses_update(&$a, $type) { function api_statuses_update(&$a, $type) {
if (api_user() === false) { if (api_user() === false) {
logger('api_statuses_update: no user'); logger('api_statuses_update: no user');
@ -784,18 +789,20 @@
*/ */
function api_statuses_home_timeline(&$a, $type){ function api_statuses_home_timeline(&$a, $type){
if (api_user()===false) return false; if (api_user() === false)
return false;
$user_info = api_get_user($a); $user_info = api_get_user($a);
// get last newtork messages // get last network messages
// params // params
$count = (x($_REQUEST,'count')?$_REQUEST['count']:20); $count = (x($_REQUEST,'count')?$_REQUEST['count']:20);
$page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0);
if ($page<0) $page=0; if($page < 0)
$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); $page = 0;
$max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
$max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0);
$exclude_replies = (x($_REQUEST,'exclude_replies')?1:0); $exclude_replies = (x($_REQUEST,'exclude_replies')?1:0);
//$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0);
@ -809,31 +816,28 @@
if ($exclude_replies > 0) if ($exclude_replies > 0)
$sql_extra .= ' AND `item`.`parent` = `item`.`id`'; $sql_extra .= ' AND `item`.`parent` = `item`.`id`';
$r = q("SELECT `item`.*, `item`.`id` AS `item_id`, $r = q("SELECT * from item WHERE uid = %d and item_restrict = 0
`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
`contact`.`network`, `contact`.`thumb`, `contact`.`dfrn_id`, `contact`.`self`,
`contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
FROM `item`, `contact`
WHERE `item`.`uid` = %d
AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
AND `contact`.`id` = `item`.`contact-id`
AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
$sql_extra $sql_extra
AND `item`.`id`>%d AND id > %d
ORDER BY `item`.`received` DESC LIMIT %d ,%d ", ORDER BY received DESC LIMIT %d ,%d ",
intval($user_info['uid']), intval($user_info['uid']),
intval($since_id), intval($since_id),
intval($start), intval($count) intval($start),
intval($count)
); );
xchan_query($r);
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info);
// We aren't going to try to figure out at the item, group, and page // We aren't going to try to figure out at the item, group, and page
// level which items you've seen and which you haven't. If you're looking // level which items you've seen and which you haven't. If you're looking
// at the network timeline just mark everything seen. // at the network timeline just mark everything seen.
$r = q("UPDATE `item` SET `unseen` = 0 $r = q("UPDATE `item` SET item_flags = ( item_flags ^ %d )
WHERE `unseen` = 1 AND `uid` = %d", WHERE item_flags & %d and uid = %d",
intval(ITEM_UNSEEN),
intval(ITEM_UNSEEN),
intval($user_info['uid']) intval($user_info['uid'])
); );
@ -879,41 +883,21 @@
if ($max_id > 0) if ($max_id > 0)
$sql_extra = 'AND `item`.`id` <= '.intval($max_id); $sql_extra = 'AND `item`.`id` <= '.intval($max_id);
/*$r = q("SELECT `item`.*, `item`.`id` AS `item_id`, $r = q("SELECT * from item where id in (select distinct(uri) from item where item_restrict = 0
`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, and allow_cid = '' and allow_gid = ''
`contact`.`network`, `contact`.`thumb`, `contact`.`dfrn_id`, `contact`.`self`, and deny_cid = '' and deny_gid = ''
`contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid` and not ( item_flags & %d ) and ( item_flags & %d )
FROM `item`, `contact`
WHERE `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
AND `item`.`private` = 0 AND `item`.`wall` = 1 AND `user`.`hidewall` = 0
AND `contact`.`id` = `item`.`contact-id`
AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
$sql_extra $sql_extra
AND `item`.`id`>%d AND id > %d)
ORDER BY `item`.`received` DESC LIMIT %d ,%d ", ORDER BY received DESC LIMIT %d, %d ",
intval(ITEM_PRIVATE),
intval(ITEM_WALL),
intval($since_id), intval($since_id),
intval($start), intval($count) intval($start),
);*/ intval($count)
$r = q("SELECT `item`.*, `item`.`id` AS `item_id`, );
`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
`contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`, xchan_query($r);
`contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`,
`user`.`nickname`, `user`.`hidewall`
FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
LEFT JOIN `user` ON `user`.`uid` = `item`.`uid`
WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
AND `item`.`private` = 0 AND `item`.`wall` = 1 AND `user`.`hidewall` = 0
AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
$sql_extra
AND `item`.`id`>%d
ORDER BY `received` DESC LIMIT %d, %d ",
intval($since_id),
intval($start),
intval($count));
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info);
@ -926,7 +910,7 @@
break; break;
case "as": case "as":
$as = api_format_as($a, $ret, $user_info); $as = api_format_as($a, $ret, $user_info);
$as['title'] = $a->config['sitename']." Public Timeline"; $as['title'] = $a->config['sitename']. " " . t('Public Timeline');
$as['link']['url'] = $a->get_baseurl()."/"; $as['link']['url'] = $a->get_baseurl()."/";
return($as); return($as);
break; break;
@ -938,6 +922,7 @@
/** /**
* *
*/ */
function api_statuses_show(&$a, $type){ function api_statuses_show(&$a, $type){
if (api_user()===false) return false; if (api_user()===false) return false;
@ -945,7 +930,7 @@
$user_info = api_get_user($a); $user_info = api_get_user($a);
// params // params
$id = intval($a->argv[3]); $id = intval(argv(3));
logger('API: api_statuses_show: '.$id); logger('API: api_statuses_show: '.$id);
@ -958,17 +943,10 @@
else else
$sql_extra .= " AND `item`.`id` = %d"; $sql_extra .= " AND `item`.`id` = %d";
$r = q("SELECT `item`.*, `item`.`id` AS `item_id`, $r = q("select * from item where item_restrict = 0 $sql_extra",
`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
`contact`.`network`, `contact`.`thumb`, `contact`.`dfrn_id`, `contact`.`self`,
`contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
FROM `item`, `contact`
WHERE `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
AND `contact`.`id` = `item`.`contact-id`
AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
$sql_extra",
intval($id) intval($id)
); );
xchan_query($r);
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info);
@ -1325,7 +1303,7 @@
function api_format_messages($item, $recipient, $sender) { function api_format_messages($item, $recipient, $sender) {
// standard meta information // standard meta information
$ret=Array( $ret = array(
'id' => $item['id'], 'id' => $item['id'],
'created_at' => api_date($item['created']), 'created_at' => api_date($item['created']),
'sender_id' => $sender['id'] , 'sender_id' => $sender['id'] ,
@ -1364,25 +1342,26 @@
//logger('api_format_items: ' . print_r($user_info,true)); //logger('api_format_items: ' . print_r($user_info,true));
$a = get_app(); $a = get_app();
$ret = Array(); $ret = array();
foreach($r as $item) { foreach($r as $item) {
localize_item($item); localize_item($item);
$status_user = (($item['cid']==$user_info['id'])?$user_info: api_item_get_user($a,$item)); $status_user = (($item['cid']==$user_info['id'])?$user_info: api_item_get_user($a,$item));
if ($item['parent']!=$item['id']) { if($item['parent'] != $item['id']) {
$r = q("select id from item where parent=%s and id<%s order by id desc limit 1", $r = q("select id from item where parent= %d and id < %d order by id desc limit 1",
intval($item['parent']), intval($item['id'])); intval($item['parent']),
intval($item['id'])
);
if ($r) if ($r)
$in_reply_to_status_id = $r[0]['id']; $in_reply_to_status_id = $r[0]['id'];
else else
$in_reply_to_status_id = $item['parent']; $in_reply_to_status_id = $item['parent'];
$r = q("select `item`.`contact-id`, `contact`.nick, `item`.`author-name` from item, contact xchan_query($r);
where `contact`.`id` = `item`.`contact-id` and `item`.id=%d", intval($in_reply_to_status_id));
$in_reply_to_screen_name = $r[0]['author-name']; $in_reply_to_screen_name = $r[0]['author']['xchan_name'];
$in_reply_to_user_id = $r[0]['contact-id']; $in_reply_to_user_id = $r[0]['author']['abook_id'];
} else { } else {
$in_reply_to_screen_name = ''; $in_reply_to_screen_name = '';
@ -1399,41 +1378,39 @@
else else
$statustext = trim($statustitle."\n\n".$statusbody); $statustext = trim($statustitle."\n\n".$statusbody);
if (($item["network"] == NETWORK_FEED) and (strlen($statustext)> 1000))
$statustext = substr($statustext, 0, 1000)."... \n".$item["plink"];
$status = array( $status = array(
'text' => $statustext, 'text' => $statustext,
'truncated' => False, 'truncated' => False,
'created_at'=> api_date($item['created']), 'created_at' => api_date($item['created']),
'in_reply_to_status_id' => $in_reply_to_status_id, 'in_reply_to_status_id' => $in_reply_to_status_id,
'source' => (($item['app']) ? $item['app'] : 'web'), 'source' => (($item['app']) ? $item['app'] : 'web'),
'id' => intval($item['id']), 'id' => intval($item['id']),
'in_reply_to_user_id' => $in_reply_to_user_id, 'in_reply_to_user_id' => $in_reply_to_user_id,
'in_reply_to_screen_name' => $in_reply_to_screen_name, 'in_reply_to_screen_name' => $in_reply_to_screen_name,
'geo' => '', 'geo' => '',
'favorited' => $item['starred'] ? true : false, 'favorited' => (($item['item_flags'] & ITEM_STARRED) ? true : false),
'user' => $status_user , 'user' => $status_user ,
'statusnet_html' => trim(bbcode($item['body'])), 'statusnet_html' => trim(bbcode($item['body'])),
'statusnet_conversation_id' => $item['parent'], 'statusnet_conversation_id' => $item['parent'],
); );
// Seesmic doesn't like the following content // Seesmic doesn't like the following content
if ($_SERVER['HTTP_USER_AGENT'] != 'Seesmic') { if ($_SERVER['HTTP_USER_AGENT'] != 'Seesmic') {
$status2 = array( $status2 = array(
'updated' => api_date($item['edited']), 'updated' => api_date($item['edited']),
'published' => api_date($item['created']), 'published' => api_date($item['created']),
'message_id' => $item['uri'], 'message_id' => $item['uri'],
'url' => ($item['plink']!=''?$item['plink']:$item['author-link']), 'url' => $item['plink'],
'coordinates' => $item['coord'], 'coordinates' => $item['coord'],
'place' => $item['location'], 'place' => $item['location'],
'contributors' => '', 'contributors' => '',
'annotations' => '', 'annotations' => '',
'entities' => '', 'entities' => '',
'objecttype' => (($item['obj_type']) ? $item['obj_type'] : ACTIVITY_OBJ_NOTE), 'objecttype' => (($item['obj_type']) ? $item['obj_type'] : ACTIVITY_OBJ_NOTE),
'verb' => (($item['verb']) ? $item['verb'] : ACTIVITY_POST), 'verb' => (($item['verb']) ? $item['verb'] : ACTIVITY_POST),
'self' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type, 'self' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
'edit' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type, 'edit' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
); );
$status = array_merge($status, $status2); $status = array_merge($status, $status2);