use Friendica\Content\Feature;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
-use Friendica\Core\Addon;
use Friendica\Core\Authentication;
use Friendica\Core\Config;
+use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\NotificationsManager;
define('API_METHOD_POST', 'POST,PUT');
define('API_METHOD_DELETE', 'POST,DELETE');
+define('API_LOG_PREFIX', 'API {action} - ');
+
$API = [];
$called_api = [];
return "Twidere";
}
- Logger::log("Unrecognized user-agent ".$_SERVER['HTTP_USER_AGENT'], Logger::DEBUG);
+ Logger::info(API_LOG_PREFIX . 'Unrecognized user-agent', ['module' => 'api', 'action' => 'source', 'http_user_agent' => $_SERVER['HTTP_USER_AGENT']]);
} else {
- Logger::log("Empty user-agent", Logger::DEBUG);
+ Logger::info(API_LOG_PREFIX . 'Empty user-agent', ['module' => 'api', 'action' => 'source']);
}
return "api";
list($consumer, $token) = $oauth1->verify_request($request);
if (!is_null($token)) {
$oauth1->loginUser($token->uid);
- Addon::callHooks('logged_in', $a->user);
+ Hook::callAll('logged_in', $a->user);
return;
}
echo __FILE__.__LINE__.__FUNCTION__ . "<pre>";
var_dump($consumer, $token);
die();
} catch (Exception $e) {
- Logger::log($e);
+ Logger::warning(API_LOG_PREFIX . 'error', ['module' => 'api', 'action' => 'login', 'exception' => $e->getMessage()]);
}
// workaround for HTTP-auth in CGI mode
}
if (empty($_SERVER['PHP_AUTH_USER'])) {
- Logger::log('API_login: ' . print_r($_SERVER, true), Logger::DEBUG);
+ Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]);
header('WWW-Authenticate: Basic realm="Friendica"');
throw new UnauthorizedException("This API requires login");
}
* Addons should never set 'authenticated' except to indicate success - as hooks may be chained
* and later addons should not interfere with an earlier one that succeeded.
*/
- Addon::callHooks('authenticate', $addon_auth);
+ Hook::callAll('authenticate', $addon_auth);
if ($addon_auth['authenticated'] && count($addon_auth['user_record'])) {
$record = $addon_auth['user_record'];
}
if (!DBA::isResult($record)) {
- Logger::log('API_login failure: ' . print_r($_SERVER, true), Logger::DEBUG);
+ Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]);
header('WWW-Authenticate: Basic realm="Friendica"');
//header('HTTP/1.0 401 Unauthorized');
//die('This api requires login');
$_SESSION["allow_api"] = true;
- Addon::callHooks('logged_in', $a->user);
+ Hook::callAll('logged_in', $a->user);
}
/**
api_login($a);
}
- Logger::log('API call for ' . $a->user['username'] . ': ' . $a->query_string);
- Logger::log('API parameters: ' . print_r($_REQUEST, true));
+ Logger::info(API_LOG_PREFIX . 'username {username}', ['module' => 'api', 'action' => 'call', 'username' => $a->user['username']]);
+ Logger::debug(API_LOG_PREFIX . 'parameters', ['module' => 'api', 'action' => 'call', 'parameters' => $_REQUEST]);
$stamp = microtime(true);
$return = call_user_func($info['func'], $type);
$duration = (float) (microtime(true) - $stamp);
- Logger::log("API call duration: " . round($duration, 2) . "\t" . $a->query_string, Logger::DEBUG);
+
+ Logger::info(API_LOG_PREFIX . 'username {username}', ['module' => 'api', 'action' => 'call', 'username' => $a->user['username'], 'duration' => round($duration, 2)]);
if (Config::get("system", "profiler")) {
$duration = microtime(true)-$a->performance["start"];
/// @TODO round() really everywhere?
- Logger::log(
- parse_url($a->query_string, PHP_URL_PATH) . ": " . sprintf(
- "Database: %s/%s, Cache %s/%s, Network: %s, I/O: %s, Other: %s, Total: %s",
- round($a->performance["database"] - $a->performance["database_write"], 3),
- round($a->performance["database_write"], 3),
- round($a->performance["cache"], 3),
- round($a->performance["cache_write"], 3),
- round($a->performance["network"], 2),
- round($a->performance["file"], 2),
- round($duration - ($a->performance["database"]
- + $a->performance["cache"] + $a->performance["cache_write"]
- + $a->performance["network"] + $a->performance["file"]), 2),
- round($duration, 2)
- ),
- Logger::DEBUG
+ Logger::debug(
+ API_LOG_PREFIX . 'performance',
+ [
+ 'module' => 'api',
+ 'action' => 'call',
+ 'database_read' => round($a->performance["database"] - $a->performance["database_write"], 3),
+ 'database_write' => round($a->performance["database_write"], 3),
+ 'cache_read' => round($a->performance["cache"], 3),
+ 'cache_write' => round($a->performance["cache_write"], 3),
+ 'network_io' => round($a->performance["network"], 2),
+ 'file_io' => round($a->performance["file"], 2),
+ 'other_io' => round($duration - ($a->performance["database"]
+ + $a->performance["cache"] + $a->performance["cache_write"]
+ + $a->performance["network"] + $a->performance["file"]), 2),
+ 'total' => round($duration, 2)
+ ]
);
if (Config::get("rendertime", "callstack")) {
$o .= $func . ": " . $time . "\n";
}
}
- Logger::log($o, Logger::DEBUG);
+ Logger::debug(API_LOG_PREFIX . $o, ['module' => 'api', 'action' => 'call']);
}
}
}
}
- Logger::log('API call not implemented: ' . $a->query_string);
+ Logger::warning(API_LOG_PREFIX . 'not implemented', ['module' => 'api', 'action' => 'call']);
throw new NotImplementedException();
} catch (HTTPException $e) {
header("HTTP/1.1 {$e->httpcode} {$e->httpdesc}");
$extra_query = "";
$url = "";
- Logger::log("api_get_user: Fetching user data for user ".$contact_id, Logger::DEBUG);
+ Logger::info(API_LOG_PREFIX . 'Fetching data for user {user}', ['module' => 'api', 'action' => 'get_user', 'user' => $contact_id]);
// Searching for contact URL
if (!is_null($contact_id) && (intval($contact_id) == 0)) {
}
}
- Logger::log("api_get_user: user ".$user, Logger::DEBUG);
+ Logger::info(API_LOG_PREFIX . 'getting user {user}', ['module' => 'api', 'action' => 'get_user', 'user' => $user]);
if (!$user) {
if (api_user() === false) {
}
}
- Logger::log('api_user: ' . $extra_query . ', user: ' . $user);
+ Logger::info(API_LOG_PREFIX . 'found user {user}', ['module' => 'api', 'action' => 'get_user', 'user' => $user, 'extra_query' => $extra_query]);
// user info
$uinfo = q(
$ret = $data;
break;
}
-
return $ret;
}
$returndata["image"] = ["w" => $media["width"],
"h" => $media["height"],
"image_type" => $media["type"],
- "preview" => $media["preview"]];
+ "friendica_preview_url" => $media["preview"]];
Logger::log("Media uploaded: " . print_r($returndata, true), Logger::DEBUG);
$a = \get_app();
$user_info = api_get_user($a);
- if (api_user() === false || $user_info === false) {
- throw new ForbiddenException();
- }
+ if (api_user() === false || $user_info === false) { throw new ForbiddenException(); }
- $data = [];
-
- if (empty($_REQUEST['q'])) {
- throw new BadRequestException("q parameter is required.");
- }
+ if (empty($_REQUEST['q'])) { throw new BadRequestException('q parameter is required.'); }
+
+ $searchTerm = trim(rawurldecode($_REQUEST['q']));
+ $data = [];
+ $data['status'] = [];
+ $count = 15;
+ $exclude_replies = !empty($_REQUEST['exclude_replies']);
if (!empty($_REQUEST['rpp'])) {
$count = $_REQUEST['rpp'];
} elseif (!empty($_REQUEST['count'])) {
$count = $_REQUEST['count'];
- } else {
- $count = 15;
}
-
+
$since_id = defaults($_REQUEST, 'since_id', 0);
$max_id = defaults($_REQUEST, 'max_id', 0);
$page = (!empty($_REQUEST['page']) ? $_REQUEST['page'] - 1 : 0);
-
$start = $page * $count;
+ $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
+ if (preg_match('/^#(\w+)$/', $searchTerm, $matches) === 1 && isset($matches[1])) {
+ $searchTerm = $matches[1];
+ $condition = ["`oid` > ?
+ AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
+ AND `otype` = ? AND `type` = ? AND `term` = ?",
+ $since_id, local_user(), TERM_OBJ_POST, TERM_HASHTAG, $searchTerm];
+ if ($max_id > 0) {
+ $condition[0] .= ' AND `oid` <= ?';
+ $condition[] = $max_id;
+ }
+ $terms = DBA::select('term', ['oid'], $condition, []);
+ $itemIds = [];
+ while ($term = DBA::fetch($terms)) {
+ $itemIds[] = $term['oid'];
+ }
+ DBA::close($terms);
- $condition = ["`gravity` IN (?, ?) AND `item`.`id` > ?
- AND (`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))
- AND `item`.`body` LIKE CONCAT('%',?,'%')",
- GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, api_user(), $_REQUEST['q']];
+ if (empty($itemIds)) {
+ return api_format_data('statuses', $type, $data);
+ }
- if ($max_id > 0) {
- $condition[0] .= " AND `item`.`id` <= ?";
- $condition[] = $max_id;
+ $preCondition = ['`id` IN (' . implode(', ', $itemIds) . ')'];
+ if ($exclude_replies) {
+ $preCondition[] = '`id` = `parent`';
+ }
+
+ $condition = [implode(' AND ', $preCondition)];
+ } else {
+ $condition = ["`id` > ?
+ " . ($exclude_replies ? " AND `id` = `parent` " : ' ') . "
+ AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
+ AND `body` LIKE CONCAT('%',?,'%')",
+ $since_id, api_user(), $_REQUEST['q']];
+ if ($max_id > 0) {
+ $condition[0] .= ' AND `id` <= ?';
+ $condition[] = $max_id;
+ }
}
- $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
$statuses = Item::selectForUser(api_user(), [], $condition, $params);
$data['status'] = api_format_items(Item::inArray($statuses), $user_info);
- return api_format_data("statuses", $type, $data);
+ bindComments($data['status']);
+
+ return api_format_data('statuses', $type, $data);
}
/// @TODO move to top of file or somewhere better
Item::update(['unseen' => false], ['unseen' => true, 'id' => $idarray]);
}
}
+
+ bindComments($ret);
$data = ['status' => $ret];
switch ($type) {
return api_format_data("statuses", $type, $data);
}
+
/// @TODO move to top of file or somewhere better
api_register_func('api/statuses/home_timeline', 'api_statuses_home_timeline', true);
api_register_func('api/statuses/friends_timeline', 'api_statuses_home_timeline', true);
$ret = api_format_items($r, $user_info, false, $type);
+ bindComments($ret);
+
$data = ['status' => $ret];
switch ($type) {
case "atom":
$ret = api_format_items(Item::inArray($statuses), $user_info, false, $type);
+ bindComments($ret);
+
$data = ['status' => $ret];
switch ($type) {
case "atom":
$id = intval(defaults($a->argv, 4, 0));
}
- Logger::log('API: api_conversation_show: '.$id);
+ Logger::info(API_LOG_PREFIX . '{subaction}', ['module' => 'api', 'action' => 'conversation', 'subaction' => 'show', 'id' => $id]);
// try to fetch the item for the local user - or the public item, if there is no local one
$item = Item::selectFirst(['parent-uri'], ['id' => $id]);
$ret = api_format_items(Item::inArray($statuses), $user_info, true, $type);
+ bindComments($ret);
+
$data = ['status' => $ret];
switch ($type) {
case "atom":
// in friendica starred item are private
// return favorites only for self
- Logger::log('api_favorites: self:' . $user_info['self']);
+ Logger::info(API_LOG_PREFIX . 'for {self}', ['module' => 'api', 'action' => 'favorites', 'self' => $user_info['self']]);
if ($user_info['self'] == 0) {
$ret = [];
$ret = api_format_items(Item::inArray($statuses), $user_info, false, $type);
}
+ bindComments($ret);
+
$data = ['status' => $ret];
switch ($type) {
case "atom":
$ret = [];
- foreach ($r as $item) {
+ foreach ((array)$r as $item) {
localize_item($item);
list($status_user, $owner_user) = api_item_get_user($a, $item);
$contact_id = defaults($_REQUEST, 'user_id');
if (empty($contact_id)) {
- Logger::log("No user_id specified", Logger::DEBUG);
+ Logger::notice(API_LOG_PREFIX . 'No user_id specified', ['module' => 'api', 'action' => 'friendships_destroy']);
throw new BadRequestException("no user_id specified");
}
$contact = DBA::selectFirst('contact', ['url'], ['id' => $contact_id, 'uid' => 0, 'self' => false]);
if(!DBA::isResult($contact)) {
- Logger::log("No contact found for ID" . $contact_id, Logger::DEBUG);
+ Logger::notice(API_LOG_PREFIX . 'No contact found for ID {contact}', ['module' => 'api', 'action' => 'friendships_destroy', 'contact' => $contact_id]);
throw new NotFoundException("no contact found to given ID");
}
$contact = DBA::selectFirst('contact', [], $condition);
if (!DBA::isResult($contact)) {
- Logger::log("Not following Contact", Logger::DEBUG);
+ Logger::notice(API_LOG_PREFIX . 'Not following contact', ['module' => 'api', 'action' => 'friendships_destroy']);
throw new NotFoundException("Not following Contact");
}
if (!in_array($contact['network'], Protocol::NATIVE_SUPPORT)) {
- Logger::log("Not supported", Logger::DEBUG);
+ Logger::notice(API_LOG_PREFIX . 'Not supported for {network}', ['module' => 'api', 'action' => 'friendships_destroy', 'network' => $contact['network']]);
throw new ExpectationFailedException("Not supported");
}
Contact::terminateFriendship($owner, $contact, $dissolve);
}
else {
- Logger::log("No owner found", Logger::DEBUG);
+ Logger::notice(API_LOG_PREFIX . 'No owner {uid} found', ['module' => 'api', 'action' => 'friendships_destroy', 'uid' => $uid]);
throw new NotFoundException("Error Processing Request");
}
$r = $oauth1->fetch_request_token(OAuthRequest::from_request());
} catch (Exception $e) {
echo "error=" . OAuthUtil::urlencode_rfc3986($e->getMessage());
- killme();
+ exit();
}
echo $r;
- killme();
+ exit();
}
/**
$r = $oauth1->fetch_access_token(OAuthRequest::from_request());
} catch (Exception $e) {
echo "error=". OAuthUtil::urlencode_rfc3986($e->getMessage());
- killme();
+ exit();
}
echo $r;
- killme();
+ exit();
}
/// @TODO move to top of file or somewhere better
'sec' => $sec, 'expire' => time() + 45];
DBA::insert('profile_check', $fields);
- Logger::log($contact['name'] . ' ' . $sec, Logger::DEBUG);
+ Logger::info(API_LOG_PREFIX . 'for contact {contact}', ['module' => 'api', 'action' => 'friendica_remoteauth', 'contact' => $contact['name'], 'hey' => $sec]);
$dest = ($url ? '&destination_url=' . $url : '');
System::externalRedirect(
// https://github.com/friendica/friendica/issues/1010
// This is a bugfix for that.
if (intval($in_reply_to['status_id']) == intval($item['id'])) {
- Logger::log('this message should never appear: id: '.$item['id'].' similar to reply-to: '.$in_reply_to['status_id'], Logger::DEBUG);
+ Logger::warning(API_LOG_PREFIX . 'ID {id} is similar to reply-to {reply-to}', ['module' => 'api', 'action' => 'in_reply_to', 'id' => $item['id'], 'reply-to' => $in_reply_to['status_id']]);
$in_reply_to['status_id'] = null;
$in_reply_to['user_id'] = null;
$in_reply_to['status_id_str'] = null;
$notes = $xmlnotes;
}
-
return api_format_data("notes", $type, ['note' => $notes]);
}
/// @TODO move to top of file or somewhere better
api_register_func('api/saved_searches/list', 'api_saved_searches_list', true);
+/*
+ * Bind comment numbers(friendica_comments: Int) on each statuses page of *_timeline / favorites / search
+ *
+ * @brief Number of comments
+ *
+ * @param object $data [Status, Status]
+ *
+ * @return void
+ */
+function bindComments(&$data)
+{
+ if (count($data) == 0) {
+ return;
+ }
+
+ $ids = [];
+ $comments = [];
+ foreach ($data as $item) {
+ $ids[] = $item['id'];
+ }
+
+ $idStr = DBA::escape(implode(', ', $ids));
+ $sql = "SELECT `parent`, COUNT(*) as comments FROM `item` WHERE `parent` IN ($idStr) AND `deleted` = ? AND `gravity`= ? GROUP BY `parent`";
+ $items = DBA::p($sql, 0, GRAVITY_COMMENT);
+ $itemsData = DBA::toArray($items);
+
+ foreach ($itemsData as $item) {
+ $comments[$item['parent']] = $item['comments'];
+ }
+
+ foreach ($data as $idx => $item) {
+ $id = $item['id'];
+ $data[$idx]['friendica_comments'] = isset($comments[$id]) ? $comments[$id] : 0;
+ }
+}
+
/*
@TODO Maybe open to implement?
To.Do: