api: unify naming

master
Andrew Dolgov 3 years ago
parent 71f2f4288f
commit d1c83fad14

@ -8,21 +8,27 @@ class API extends Handler {
private $seq;
static function param_to_bool($p) {
private static function _param_to_bool($p) {
return $p && ($p !== "f" && $p !== "false");
}
private function _wrap($status, $reply) {
print json_encode(array("seq" => $this->seq,
"status" => $status,
"content" => $reply));
}
function before($method) {
if (parent::before($method)) {
header("Content-Type: text/json");
if (empty($_SESSION["uid"]) && $method != "login" && $method != "isloggedin") {
$this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN'));
$this->_wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN'));
return false;
}
if (!empty($_SESSION["uid"]) && $method != "logout" && !get_pref('ENABLE_API_ACCESS')) {
$this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED'));
$this->_wrap(self::STATUS_ERR, array("error" => 'API_DISABLED'));
return false;
}
@ -33,20 +39,14 @@ class API extends Handler {
return false;
}
function wrap($status, $reply) {
print json_encode(array("seq" => $this->seq,
"status" => $status,
"content" => $reply));
}
function getVersion() {
$rv = array("version" => get_version());
$this->wrap(self::STATUS_OK, $rv);
$this->_wrap(self::STATUS_OK, $rv);
}
function getApiLevel() {
$rv = array("level" => self::API_LEVEL);
$this->wrap(self::STATUS_OK, $rv);
$this->_wrap(self::STATUS_OK, $rv);
}
function login() {
@ -62,31 +62,31 @@ class API extends Handler {
if ($uid = UserHelper::find_user_by_login($login)) {
if (get_pref("ENABLE_API_ACCESS", $uid)) {
if (UserHelper::authenticate($login, $password, false, Auth_Base::AUTH_SERVICE_API)) { // try login with normal password
$this->wrap(self::STATUS_OK, array("session_id" => session_id(),
$this->_wrap(self::STATUS_OK, array("session_id" => session_id(),
"api_level" => self::API_LEVEL));
} else if (UserHelper::authenticate($login, $password_base64, false, Auth_Base::AUTH_SERVICE_API)) { // else try with base64_decoded password
$this->wrap(self::STATUS_OK, array("session_id" => session_id(),
$this->_wrap(self::STATUS_OK, array("session_id" => session_id(),
"api_level" => self::API_LEVEL));
} else { // else we are not logged in
user_error("Failed login attempt for $login from " . UserHelper::get_user_ip(), E_USER_WARNING);
$this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
$this->_wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
}
} else {
$this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
$this->_wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
}
} else {
$this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
$this->_wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
return;
}
}
function logout() {
UserHelper::logout();
$this->wrap(self::STATUS_OK, array("status" => "OK"));
$this->_wrap(self::STATUS_OK, array("status" => "OK"));
}
function isLoggedIn() {
$this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != ''));
$this->_wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != ''));
}
function getUnread() {
@ -94,33 +94,33 @@ class API extends Handler {
$is_cat = clean($_REQUEST["is_cat"]);
if ($feed_id) {
$this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat)));
$this->_wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat)));
} else {
$this->wrap(self::STATUS_OK, array("unread" => Feeds::_get_global_unread()));
$this->_wrap(self::STATUS_OK, array("unread" => Feeds::_get_global_unread()));
}
}
/* Method added for ttrss-reader for Android */
function getCounters() {
$this->wrap(self::STATUS_OK, Counters::get_all());
$this->_wrap(self::STATUS_OK, Counters::get_all());
}
function getFeeds() {
$cat_id = clean($_REQUEST["cat_id"]);
$unread_only = self::param_to_bool(clean($_REQUEST["unread_only"] ?? 0));
$unread_only = self::_param_to_bool(clean($_REQUEST["unread_only"] ?? 0));
$limit = (int) clean($_REQUEST["limit"] ?? 0);
$offset = (int) clean($_REQUEST["offset"] ?? 0);
$include_nested = self::param_to_bool(clean($_REQUEST["include_nested"] ?? false));
$include_nested = self::_param_to_bool(clean($_REQUEST["include_nested"] ?? false));
$feeds = $this->api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested);
$feeds = $this->_api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested);
$this->wrap(self::STATUS_OK, $feeds);
$this->_wrap(self::STATUS_OK, $feeds);
}
function getCategories() {
$unread_only = self::param_to_bool(clean($_REQUEST["unread_only"] ?? false));
$enable_nested = self::param_to_bool(clean($_REQUEST["enable_nested"] ?? false));
$include_empty = self::param_to_bool(clean($_REQUEST['include_empty'] ?? false));
$unread_only = self::_param_to_bool(clean($_REQUEST["unread_only"] ?? false));
$enable_nested = self::_param_to_bool(clean($_REQUEST["enable_nested"] ?? false));
$include_empty = self::_param_to_bool(clean($_REQUEST['include_empty'] ?? false));
// TODO do not return empty categories, return Uncategorized and standard virtual cats
@ -160,7 +160,7 @@ class API extends Handler {
}
foreach (array(-2,-1,0) as $cat_id) {
if ($include_empty || !$this->isCategoryEmpty($cat_id)) {
if ($include_empty || !$this->_is_cat_empty($cat_id)) {
$unread = getFeedUnread($cat_id, true);
if ($unread || !$unread_only) {
@ -171,7 +171,7 @@ class API extends Handler {
}
}
$this->wrap(self::STATUS_OK, $cats);
$this->_wrap(self::STATUS_OK, $cats);
}
function getHeadlines() {
@ -186,21 +186,21 @@ class API extends Handler {
$offset = (int)clean($_REQUEST["skip"]);
$filter = clean($_REQUEST["filter"] ?? "");
$is_cat = self::param_to_bool(clean($_REQUEST["is_cat"] ?? false));
$show_excerpt = self::param_to_bool(clean($_REQUEST["show_excerpt"] ?? false));
$show_content = self::param_to_bool(clean($_REQUEST["show_content"]));
$is_cat = self::_param_to_bool(clean($_REQUEST["is_cat"] ?? false));
$show_excerpt = self::_param_to_bool(clean($_REQUEST["show_excerpt"] ?? false));
$show_content = self::_param_to_bool(clean($_REQUEST["show_content"]));
/* all_articles, unread, adaptive, marked, updated */
$view_mode = clean($_REQUEST["view_mode"] ?? null);
$include_attachments = self::param_to_bool(clean($_REQUEST["include_attachments"] ?? false));
$include_attachments = self::_param_to_bool(clean($_REQUEST["include_attachments"] ?? false));
$since_id = (int)clean($_REQUEST["since_id"] ?? 0);
$include_nested = self::param_to_bool(clean($_REQUEST["include_nested"] ?? false));
$include_nested = self::_param_to_bool(clean($_REQUEST["include_nested"] ?? false));
$sanitize_content = !isset($_REQUEST["sanitize"]) ||
self::param_to_bool($_REQUEST["sanitize"]);
$force_update = self::param_to_bool(clean($_REQUEST["force_update"] ?? false));
$has_sandbox = self::param_to_bool(clean($_REQUEST["has_sandbox"] ?? false));
self::_param_to_bool($_REQUEST["sanitize"]);
$force_update = self::_param_to_bool(clean($_REQUEST["force_update"] ?? false));
$has_sandbox = self::_param_to_bool(clean($_REQUEST["has_sandbox"] ?? false));
$excerpt_length = (int)clean($_REQUEST["excerpt_length"] ?? 0);
$check_first_id = (int)clean($_REQUEST["check_first_id"] ?? 0);
$include_header = self::param_to_bool(clean($_REQUEST["include_header"] ?? false));
$include_header = self::_param_to_bool(clean($_REQUEST["include_header"] ?? false));
$_SESSION['hasSandbox'] = $has_sandbox;
@ -210,18 +210,18 @@ class API extends Handler {
$search = clean($_REQUEST["search"] ?? "");
list($headlines, $headlines_header) = $this->api_get_headlines($feed_id, $limit, $offset,
list($headlines, $headlines_header) = $this->_api_get_headlines($feed_id, $limit, $offset,
$filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order,
$include_attachments, $since_id, $search,
$include_nested, $sanitize_content, $force_update, $excerpt_length, $check_first_id, $skip_first_id_check);
if ($include_header) {
$this->wrap(self::STATUS_OK, array($headlines_header, $headlines));
$this->_wrap(self::STATUS_OK, array($headlines_header, $headlines));
} else {
$this->wrap(self::STATUS_OK, $headlines);
$this->_wrap(self::STATUS_OK, $headlines);
}
} else {
$this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
$this->_wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
}
}
@ -277,11 +277,11 @@ class API extends Handler {
$num_updated = $sth->rowCount();
$this->wrap(self::STATUS_OK, array("status" => "OK",
$this->_wrap(self::STATUS_OK, array("status" => "OK",
"updated" => $num_updated));
} else {
$this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
$this->_wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
}
}
@ -290,7 +290,7 @@ class API extends Handler {
$article_ids = explode(",", clean($_REQUEST["article_id"]));
$sanitize_content = !isset($_REQUEST["sanitize"]) ||
self::param_to_bool($_REQUEST["sanitize"]);
self::_param_to_bool($_REQUEST["sanitize"]);
if ($article_ids) {
@ -317,9 +317,9 @@ class API extends Handler {
"title" => $line["title"],
"link" => $line["link"],
"labels" => Article::_get_labels($line['id']),
"unread" => self::param_to_bool($line["unread"]),
"marked" => self::param_to_bool($line["marked"]),
"published" => self::param_to_bool($line["published"]),
"unread" => self::_param_to_bool($line["unread"]),
"marked" => self::_param_to_bool($line["marked"]),
"published" => self::_param_to_bool($line["published"]),
"comments" => $line["comments"],
"author" => $line["author"],
"updated" => (int) strtotime($line["updated"]),
@ -334,7 +334,7 @@ class API extends Handler {
if ($sanitize_content) {
$article["content"] = Sanitizer::sanitize(
$line["content"],
self::param_to_bool($line['hide_images']),
self::_param_to_bool($line['hide_images']),
false, $line["site_url"], false, $line["id"]);
} else {
$article["content"] = $line["content"];
@ -354,9 +354,9 @@ class API extends Handler {
}
$this->wrap(self::STATUS_OK, $articles);
$this->_wrap(self::STATUS_OK, $articles);
} else {
$this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
$this->_wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
}
}
@ -374,7 +374,7 @@ class API extends Handler {
$config["num_feeds"] = $row["cf"];
$this->wrap(self::STATUS_OK, $config);
$this->_wrap(self::STATUS_OK, $config);
}
function updateFeed() {
@ -384,7 +384,7 @@ class API extends Handler {
RSSUtils::update_rss_feed($feed_id);
}
$this->wrap(self::STATUS_OK, array("status" => "OK"));
$this->_wrap(self::STATUS_OK, array("status" => "OK"));
}
function catchupFeed() {
@ -397,13 +397,13 @@ class API extends Handler {
Feeds::_catchup($feed_id, $is_cat, $_SESSION["uid"], $mode);
$this->wrap(self::STATUS_OK, array("status" => "OK"));
$this->_wrap(self::STATUS_OK, array("status" => "OK"));
}
function getPref() {
$pref_name = clean($_REQUEST["pref_name"]);
$this->wrap(self::STATUS_OK, array("value" => get_pref($pref_name)));
$this->_wrap(self::STATUS_OK, array("value" => get_pref($pref_name)));
}
function getLabels() {
@ -439,14 +439,14 @@ class API extends Handler {
"checked" => $checked));
}
$this->wrap(self::STATUS_OK, $rv);
$this->_wrap(self::STATUS_OK, $rv);
}
function setArticleLabel() {
$article_ids = explode(",", clean($_REQUEST["article_ids"]));
$label_id = (int) clean($_REQUEST['label_id']);
$assign = self::param_to_bool(clean($_REQUEST['assign']));
$assign = self::_param_to_bool(clean($_REQUEST['assign']));
$label = Labels::find_caption(Labels::feed_to_label_id($label_id), $_SESSION["uid"]);
@ -466,7 +466,7 @@ class API extends Handler {
}
}
$this->wrap(self::STATUS_OK, array("status" => "OK",
$this->_wrap(self::STATUS_OK, array("status" => "OK",
"updated" => $num_updated));
}
@ -477,10 +477,10 @@ class API extends Handler {
if ($plugin && method_exists($plugin, $method)) {
$reply = $plugin->$method();
$this->wrap($reply[0], $reply[1]);
$this->_wrap($reply[0], $reply[1]);
} else {
$this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method));
$this->_wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method));
}
}
@ -490,13 +490,13 @@ class API extends Handler {
$content = strip_tags(clean($_REQUEST["content"]));
if (Article::_create_published_article($title, $url, $content, "", $_SESSION["uid"])) {
$this->wrap(self::STATUS_OK, array("status" => 'OK'));
$this->_wrap(self::STATUS_OK, array("status" => 'OK'));
} else {
$this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
$this->_wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
}
}
static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) {
private static function _api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) {
$feeds = array();
@ -632,7 +632,7 @@ class API extends Handler {
return $feeds;
}
static function api_get_headlines($feed_id, $limit, $offset,
private static function _api_get_headlines($feed_id, $limit, $offset,
$filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
$include_attachments, $since_id,
$search = "", $include_nested = false, $sanitize_content = true,
@ -650,7 +650,7 @@ class API extends Handler {
if ($row = $sth->fetch()) {
$last_updated = strtotime($row["last_updated"]);
$cache_images = self::param_to_bool($row["cache_images"]);
$cache_images = self::_param_to_bool($row["cache_images"]);
if (!$cache_images && time() - $last_updated > 120) {
RSSUtils::update_rss_feed($feed_id, true);
@ -723,9 +723,9 @@ class API extends Handler {
$headline_row = array(
"id" => (int)$line["id"],
"guid" => $line["guid"],
"unread" => self::param_to_bool($line["unread"]),
"marked" => self::param_to_bool($line["marked"]),
"published" => self::param_to_bool($line["published"]),
"unread" => self::_param_to_bool($line["unread"]),
"marked" => self::_param_to_bool($line["marked"]),
"published" => self::_param_to_bool($line["published"]),
"updated" => (int)strtotime($line["updated"]),
"is_updated" => $is_updated,
"title" => $line["title"],
@ -747,7 +747,7 @@ class API extends Handler {
if ($sanitize_content) {
$headline_row["content"] = Sanitizer::sanitize(
$line["content"],
self::param_to_bool($line['hide_images']),
self::_param_to_bool($line['hide_images']),
false, $line["site_url"], false, $line["id"]);
} else {
$headline_row["content"] = $line["content"];
@ -764,7 +764,7 @@ class API extends Handler {
$headline_row["comments_count"] = (int)$line["num_comments"];
$headline_row["comments_link"] = $line["comments"];
$headline_row["always_display_attachments"] = self::param_to_bool($line["always_display_enclosures"]);
$headline_row["always_display_attachments"] = self::_param_to_bool($line["always_display_enclosures"]);
$headline_row["author"] = $line["author"];
@ -813,9 +813,9 @@ class API extends Handler {
if ($row = $sth->fetch()) {
Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]);
$this->wrap(self::STATUS_OK, array("status" => "OK"));
$this->_wrap(self::STATUS_OK, array("status" => "OK"));
} else {
$this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
$this->_wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
}
}
@ -828,26 +828,26 @@ class API extends Handler {
if ($feed_url) {
$rc = Feeds::_subscribe($feed_url, $category_id, $login, $password);
$this->wrap(self::STATUS_OK, array("status" => $rc));
$this->_wrap(self::STATUS_OK, array("status" => $rc));
} else {
$this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
$this->_wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
}
}
function getFeedTree() {
$include_empty = self::param_to_bool(clean($_REQUEST['include_empty']));
$include_empty = self::_param_to_bool(clean($_REQUEST['include_empty']));
$pf = new Pref_Feeds($_REQUEST);
$_REQUEST['mode'] = 2;
$_REQUEST['force_show_empty'] = $include_empty;
$this->wrap(self::STATUS_OK,
$this->_wrap(self::STATUS_OK,
array("categories" => $pf->makefeedtree()));
}
// only works for labels or uncategorized for the time being
private function isCategoryEmpty($id) {
private function _is_cat_empty($id) {
if ($id == -2) {
$sth = $this->pdo->prepare("SELECT COUNT(id) AS count FROM ttrss_labels2

Loading…
Cancel
Save