diff --git a/include/api.php b/include/api.php index c89a5b1644..8b0d0c0d91 100644 --- a/include/api.php +++ b/include/api.php @@ -688,59 +688,8 @@ function api_get_user(App $a, $contact_id = null) $usr = dba::selectFirst('user', ['default-location'], ['uid' => api_user()]); $profile = dba::selectFirst('profile', ['about'], ['uid' => api_user(), 'is-default' => true]); - - /// @TODO old-lost code? (twice) - // Counting is deactivated by now, due to performance issues - // count public wall messages - //$r = q("SELECT COUNT(*) as `count` FROM `item` WHERE `uid` = %d AND `wall`", - // intval($uinfo[0]['uid']) - //); - //$countitms = $r[0]['count']; - $countitms = 0; - } else { - // Counting is deactivated by now, due to performance issues - //$r = q("SELECT count(*) as `count` FROM `item` - // WHERE `contact-id` = %d", - // intval($uinfo[0]['id']) - //); - //$countitms = $r[0]['count']; - $countitms = 0; } - - /// @TODO old-lost code? (twice) - /* - // Counting is deactivated by now, due to performance issues - // count friends - $r = q("SELECT count(*) as `count` FROM `contact` - WHERE `uid` = %d AND `rel` IN ( %d, %d ) - AND `self`=0 AND NOT `blocked` AND NOT `pending` AND `hidden`=0", - intval($uinfo[0]['uid']), - intval(CONTACT_IS_SHARING), - intval(CONTACT_IS_FRIEND) - ); - $countfriends = $r[0]['count']; - - $r = q("SELECT count(*) as `count` FROM `contact` - WHERE `uid` = %d AND `rel` IN ( %d, %d ) - AND `self`=0 AND NOT `blocked` AND NOT `pending` AND `hidden`=0", - intval($uinfo[0]['uid']), - intval(CONTACT_IS_FOLLOWER), - intval(CONTACT_IS_FRIEND) - ); - $countfollowers = $r[0]['count']; - - $r = q("SELECT count(*) as `count` FROM item where starred = 1 and uid = %d and deleted = 0", - intval($uinfo[0]['uid']) - ); - $starred = $r[0]['count']; - - - if (! $uinfo[0]['self']) { - $countfriends = 0; - $countfollowers = 0; - $starred = 0; - } - */ + $countitems = 0; $countfriends = 0; $countfollowers = 0; $starred = 0; @@ -790,7 +739,7 @@ function api_get_user(App $a, $contact_id = null) 'time_zone' => 'UTC', 'geo_enabled' => false, 'verified' => true, - 'statuses_count' => intval($countitms), + 'statuses_count' => intval($countitems), 'lang' => '', 'contributors_enabled' => false, 'is_translator' => false, @@ -1173,18 +1122,8 @@ function api_statuses_update($type) if ($throttle_day > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60); - $r = q( - "SELECT COUNT(*) AS `posts_day` FROM `item` WHERE `uid`=%d AND `wall` - AND `created` > '%s' AND `id` = `parent`", - intval(api_user()), - dbesc($datefrom) - ); - - if (DBM::is_result($r)) { - $posts_day = $r[0]["posts_day"]; - } else { - $posts_day = 0; - } + $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom]; + $posts_day = dba::count('item', $condition); if ($posts_day > $throttle_day) { logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG); @@ -1197,18 +1136,8 @@ function api_statuses_update($type) if ($throttle_week > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7); - $r = q( - "SELECT COUNT(*) AS `posts_week` FROM `item` WHERE `uid`=%d AND `wall` - AND `created` > '%s' AND `id` = `parent`", - intval(api_user()), - dbesc($datefrom) - ); - - if (DBM::is_result($r)) { - $posts_week = $r[0]["posts_week"]; - } else { - $posts_week = 0; - } + $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom]; + $posts_week = dba::count('item', $condition); if ($posts_week > $throttle_week) { logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG); @@ -1221,18 +1150,8 @@ function api_statuses_update($type) if ($throttle_month > 0) { $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30); - $r = q( - "SELECT COUNT(*) AS `posts_month` FROM `item` WHERE `uid`=%d AND `wall` - AND `created` > '%s' AND `id` = `parent`", - intval(api_user()), - dbesc($datefrom) - ); - - if (DBM::is_result($r)) { - $posts_month = $r[0]["posts_month"]; - } else { - $posts_month = 0; - } + $condition = ["`uid` = ? AND `wall` AND `created` > ? AND `id` = `parent`", api_user(), $datefrom]; + $posts_month = dba::count('item', $condition); if ($posts_month > $throttle_month) { logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG); @@ -2806,20 +2725,16 @@ function api_format_items_activities(&$item, $type = "json") 'attendmaybe' => [], ]; - $items = q( - 'SELECT * FROM `item` - WHERE `uid` = %d AND `thr-parent` = "%s" AND `visible` AND NOT `deleted`', - intval($item['uid']), - dbesc($item['uri']) - ); + $condition = ['uid' => $item['uid'], 'thr-parent' => $item['uri']]; + $ret = Item::selectForUser($item['uid'], ['author-id', 'verb'], $condition); - foreach ($items as $i) { + while ($item = dba::fetch($ret)) { // not used as result should be structured like other user data //builtin_activity_puller($i, $activities); // get user data and add it to the array of the activity - $user = api_get_user($a, $i['author-id']); - switch ($i['verb']) { + $user = api_get_user($a, $item['author-id']); + switch ($item['verb']) { case ACTIVITY_LIKE: $activities['like'][] = $user; break; @@ -2840,6 +2755,8 @@ function api_format_items_activities(&$item, $type = "json") } } + dba::close($ret); + if ($type == "xml") { $xml_activities = []; foreach ($activities as $k => $v) { @@ -3924,16 +3841,13 @@ function api_fr_photoalbum_delete($type) // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore // to the user and the contacts of the users (drop_items() performs the federation of the deletion to other networks foreach ($r as $rr) { - $photo_item = q( - "SELECT `id` FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'", - intval(local_user()), - dbesc($rr['resource-id']) - ); + $condition = ['uid' => local_user(), 'resource-id' => $rr['resource-id'], 'type' => 'photo']; + $photo_item = Item::selectFirstForUser(local_user(), ['id'], $condition); if (!DBM::is_result($photo_item)) { throw new InternalServerErrorException("problem with deleting items occured"); } - Item::deleteForUser(['id' => $photo_item[0]['id']], api_user()); + Item::deleteForUser(['id' => $photo_item['id']], api_user()); } // now let's delete all photos from the album @@ -4214,18 +4128,15 @@ function api_fr_photo_delete($type) // return success of deletion or error message if ($result) { // retrieve the id of the parent element (the photo element) - $photo_item = q( - "SELECT `id` FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'", - intval(local_user()), - dbesc($photo_id) - ); + $condition = ['uid' => local_user(), 'resource-id' => $photo_id, 'type' => 'photo']; + $photo_item = Item::selectFirstForUser(local_user(), ['id'], $condition); if (!DBM::is_result($photo_item)) { throw new InternalServerErrorException("problem with deleting items occured"); } // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore // to the user and the contacts of the users (drop_items() do all the necessary magic to avoid orphans in database and federate deletion) - Item::deleteForUser(['id' => $photo_item[0]['id']], api_user()); + Item::deleteForUser(['id' => $photo_item['id']], api_user()); $answer = ['result' => 'deleted', 'message' => 'photo with id `' . $photo_id . '` has been deleted from server.']; return api_format_data("photo_delete", $type, ['$result' => $answer]); @@ -4713,12 +4624,10 @@ function prepare_photo_data($type, $scale, $photo_id) } // retrieve item element for getting activities (like, dislike etc.) related to photo - $item = q( - "SELECT * FROM `item` WHERE `uid` = %d AND `resource-id` = '%s' AND `type` = 'photo'", - intval(local_user()), - dbesc($photo_id) - ); - $data['photo']['friendica_activities'] = api_format_items_activities($item[0], $type); + $condition = ['uid' => local_user(), 'resource-id' => $photo_id, 'type' => 'photo']; + $item = Item::selectFirstForUser(local_user(), ['id'], $condition); + + $data['photo']['friendica_activities'] = api_format_items_activities($item, $type); // retrieve comments on photo $condition = ["`parent` = ? AND `uid` = ? AND (`verb` = ? OR `type`='photo')", @@ -5013,35 +4922,26 @@ function api_in_reply_to($item) $in_reply_to['screen_name'] = null; if (($item['thr-parent'] != $item['uri']) && (intval($item['parent']) != intval($item['id']))) { - $r = q( - "SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", - intval($item['uid']), - dbesc($item['thr-parent']) - ); - - if (DBM::is_result($r)) { - $in_reply_to['status_id'] = intval($r[0]['id']); + $parent = Item::selectFirst(['id'], ['uid' => $item['uid'], 'uri' => $item['thr-parent']]); + if (DBM::is_result($parent)) { + $in_reply_to['status_id'] = intval($parent['id']); } else { $in_reply_to['status_id'] = intval($item['parent']); } $in_reply_to['status_id_str'] = (string) intval($in_reply_to['status_id']); - $r = q( - "SELECT `contact`.`nick`, `contact`.`name`, `contact`.`id`, `contact`.`url` FROM `item` - STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`author-id` - WHERE `item`.`id` = %d LIMIT 1", - intval($in_reply_to['status_id']) - ); + $fields = ['author-nick', 'author-name', 'author-id', 'author-link']; + $parent = Item::selectFirst($fields, ['id' => $in_reply_to['status_id']]); - if (DBM::is_result($r)) { - if ($r[0]['nick'] == "") { - $r[0]['nick'] = api_get_nick($r[0]["url"]); + if (DBM::is_result($parent)) { + if ($parent['author-nick'] == "") { + $parent['author-nick'] = api_get_nick($parent['author-link']); } - $in_reply_to['screen_name'] = (($r[0]['nick']) ? $r[0]['nick'] : $r[0]['name']); - $in_reply_to['user_id'] = intval($r[0]['id']); - $in_reply_to['user_id_str'] = (string) intval($r[0]['id']); + $in_reply_to['screen_name'] = (($parent['author-nick']) ? $parent['author-nick'] : $parent['author-name']); + $in_reply_to['user_id'] = intval($parent['author-id']); + $in_reply_to['user_id_str'] = (string) intval($parent['author-id']); } // There seems to be situation, where both fields are identical: diff --git a/include/conversation.php b/include/conversation.php index 97be7e9aa9..6ef2e73aa2 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -633,16 +633,12 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order = $location_e = $location; $owner_name_e = $owner_name; - if ($item['item_network'] == "") { - $item['item_network'] = $item['network']; - } - $tmp_item = [ 'template' => $tpl, 'id' => (($preview) ? 'P0' : $item['item_id']), 'guid' => (($preview) ? 'Q0' : $item['guid']), - 'network' => $item['item_network'], - 'network_name' => ContactSelector::networkToName($item['item_network'], $profile_link), + 'network' => $item['network'], + 'network_name' => ContactSelector::networkToName($item['network'], $profile_link), 'linktitle' => L10n::t('View %s\'s profile @ %s', $profile_name, $item['author-link']), 'profile_url' => $profile_link, 'item_photo_menu' => item_photo_menu($item), @@ -688,7 +684,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order = Addon::callHooks('display_item', $arr); $threads[$threadsid]['id'] = $item['item_id']; - $threads[$threadsid]['network'] = $item['item_network']; + $threads[$threadsid]['network'] = $item['network']; $threads[$threadsid]['items'] = [$arr['output']]; } diff --git a/include/items.php b/include/items.php index c54869c3e5..ee514c8fc4 100644 --- a/include/items.php +++ b/include/items.php @@ -335,17 +335,14 @@ function drop_item($id) { // locate item to be deleted - $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1", - intval($id) - ); + $fields = ['id', 'uid', 'contact-id', 'deleted']; + $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $id]); - if (!DBM::is_result($r)) { + if (!DBM::is_result($item)) { notice(L10n::t('Item not found.') . EOL); goaway(System::baseUrl() . '/' . $_SESSION['return_url']); } - $item = $r[0]; - if ($item['deleted']) { return 0; } @@ -364,7 +361,6 @@ function drop_item($id) { } if ((local_user() == $item['uid']) || $contact_id) { - // Check if we should do HTML-based delete confirmation if ($_REQUEST['confirm']) { //
can't take arguments in its "action" parameter diff --git a/include/text.php b/include/text.php index be8869fb84..10f6264586 100644 --- a/include/text.php +++ b/include/text.php @@ -1177,8 +1177,8 @@ function put_item_in_cache(&$item, $update = false) } if ($update && ($item["id"] > 0)) { - dba::update('item', ['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]], - ['id' => $item["id"]], false); + Item::update(['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]], + ['id' => $item["id"]]); } } diff --git a/mod/display.php b/mod/display.php index 7198d4bb5f..6a07fa1762 100644 --- a/mod/display.php +++ b/mod/display.php @@ -41,80 +41,70 @@ function display_init(App $a) } } - $r = false; + $item = null; + + $fields = ['id', 'parent', 'author-id', 'body', 'uid']; // If there is only one parameter, then check if this parameter could be a guid if ($a->argc == 2) { $nick = ""; - $r = false; // Does the local user have this item? if (local_user()) { - $r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid` - FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated` - AND `guid` = ? AND `uid` = ? LIMIT 1", $a->argv[1], local_user()); - if (DBM::is_result($r)) { + $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'uid' => local_user()]); + if (DBM::is_result($item)) { $nick = $a->user["nickname"]; } } // Is it an item with uid=0? - if (!DBM::is_result($r)) { - $r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid` - FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated` - AND NOT `private` AND `uid` = 0 - AND `guid` = ? LIMIT 1", $a->argv[1]); - } - - if (!DBM::is_result($r)) { - $a->error = 404; - notice(L10n::t('Item not found.') . EOL); - return; + if (!DBM::is_result($item)) { + $item = Item::selectFirstForUser(local_user(), $fields, ['guid' => $a->argv[1], 'private' => false, 'uid' => 0]); } } elseif (($a->argc == 3) && ($nick == 'feed-item')) { - $r = dba::fetch_first("SELECT `id`, `parent`, `author-id`, `body`, `uid` - FROM `item` WHERE `visible` AND NOT `deleted` AND NOT `moderated` - AND NOT `private` AND `uid` = 0 - AND `id` = ? LIMIT 1", $a->argv[2]); + $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $a->argv[2], 'private' => false, 'uid' => 0]); } - if (DBM::is_result($r)) { - if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) { - logger('Directly serving XML for id '.$r["id"], LOGGER_DEBUG); - displayShowFeed($r["id"], false); - } + if (!DBM::is_result($item) || $item['deleted']) { + $a->error = 404; + notice(L10n::t('Item not found.') . EOL); + return; + } - if ($r["id"] != $r["parent"]) { - $r = dba::fetch_first("SELECT `id`, `author-id`, `body`, `uid` FROM `item` - WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated` - AND `id` = ?", $r["parent"]); - } + if (strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) { + logger('Directly serving XML for id '.$item["id"], LOGGER_DEBUG); + displayShowFeed($item["id"], false); + } - $profiledata = display_fetchauthor($a, $r); + if ($item["id"] != $item["parent"]) { + $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $item["parent"]]); + } - if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) { - $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"])); + $profiledata = display_fetchauthor($a, $item); - if (($nickname != $a->user["nickname"])) { - $r = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile` - INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid` - WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1", - $nickname - ); - if (DBM::is_result($r)) { - $profiledata = $r; - } - $profiledata["network"] = NETWORK_DFRN; - } else { - $profiledata = []; + if (strstr(normalise_link($profiledata["url"]), normalise_link(System::baseUrl()))) { + $nickname = str_replace(normalise_link(System::baseUrl())."/profile/", "", normalise_link($profiledata["url"])); + + if (($nickname != $a->user["nickname"])) { + $profile = dba::fetch_first("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile` + INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid` + WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1", + $nickname + ); + if (DBM::is_result($profile)) { + $profiledata = $profile; } + $profiledata["network"] = NETWORK_DFRN; + } else { + $profiledata = []; } } Profile::load($a, $nick, 0, $profiledata); } -function display_fetchauthor($a, $item) { +function display_fetchauthor($a, $item) +{ $author = dba::selectFirst('contact', ['name', 'nick', 'photo', 'network', 'url'], ['id' => $item['author-id']]); $profiledata = []; @@ -198,7 +188,8 @@ function display_fetchauthor($a, $item) { return($profiledata); } -function display_content(App $a, $update = false, $update_uid = 0) { +function display_content(App $a, $update = false, $update_uid = 0) +{ if (Config::get('system','block_public') && !local_user() && !remote_user()) { notice(L10n::t('Public access denied.') . EOL); return; @@ -224,27 +215,25 @@ function display_content(App $a, $update = false, $update_uid = 0) { if ($a->argc == 2) { $item_parent = 0; + $fields = ['id', 'parent', 'parent-uri']; if (local_user()) { - $r = dba::fetch_first("SELECT `id`, `parent`, `parent-uri` FROM `item` - WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated` - AND `guid` = ? AND `uid` = ?", $a->argv[1], local_user()); - if (DBM::is_result($r)) { - $item_id = $r["id"]; - $item_parent = $r["parent"]; - $item_parent_uri = $r['parent-uri']; + $condition = ['guid' => $a->argv[1], 'uid' => local_user()]; + $item = Item::selectFirstForUser(local_user(), $fields, $condition); + if (DBM::is_result($item)) { + $item_id = $item["id"]; + $item_parent = $item["parent"]; + $item_parent_uri = $item['parent-uri']; } } if ($item_parent == 0) { - $r = dba::fetch_first("SELECT `item`.`id`, `item`.`parent`, `item`.`parent-uri` FROM `item` - WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated` - AND NOT `item`.`private` AND `item`.`uid` = 0 - AND `item`.`guid` = ?", $a->argv[1]); - if (DBM::is_result($r)) { - $item_id = $r["id"]; - $item_parent = $r["parent"]; - $item_parent_uri = $r['parent-uri']; + $condition = ['private' => false, 'guid' => $a->argv[1], 'uid' => 0]; + $item = Item::selectFirstForUser(local_user(), $fields, $condition); + if (DBM::is_result($item)) { + $item_id = $item["id"]; + $item_parent = $item["parent"]; + $item_parent_uri = $item['parent-uri']; } } } @@ -274,7 +263,7 @@ function display_content(App $a, $update = false, $update_uid = 0) { $groups = []; $contact = null; - $remote_contact = false; + $is_remote_contact = false; $contact_id = 0; @@ -289,30 +278,27 @@ function display_content(App $a, $update = false, $update_uid = 0) { if ($contact_id) { $groups = Group::getIdsByContactId($contact_id); - $r = dba::fetch_first("SELECT * FROM `contact` WHERE `id` = ? AND `uid` = ? LIMIT 1", - $contact_id, - $a->profile['uid'] - ); - if (DBM::is_result($r)) { - $contact = $r; - $remote_contact = true; + $remote_contact = dba::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $a->profile['uid']]); + if (DBM::is_result($remote_contact)) { + $contact = $remote_contact; + $is_remote_contact = true; } } - if (!$remote_contact) { + if (!$is_remote_contact) { if (local_user()) { $contact_id = $_SESSION['cid']; $contact = $a->contact; } } - $r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $a->profile['uid']); - if (DBM::is_result($r)) { - $a->page_contact = $r; + $page_contact = dba::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]); + if (DBM::is_result($page_contact)) { + $a->page_contact = $page_contact; } $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false); - if (x($a->profile, 'hidewall') && !$is_owner && !$remote_contact) { + if (x($a->profile, 'hidewall') && !$is_owner && !$is_remote_contact) { notice(L10n::t('Access to this profile has been restricted.') . EOL); return; } @@ -333,7 +319,7 @@ function display_content(App $a, $update = false, $update_uid = 0) { $o .= status_editor($a, $x, 0, true); } - $sql_extra = item_permissions_sql($a->profile['uid'], $remote_contact, $groups); + $sql_extra = item_permissions_sql($a->profile['uid'], $is_remote_contact, $groups); if (local_user() && (local_user() == $a->profile['uid'])) { $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true]; @@ -349,21 +335,19 @@ function display_content(App $a, $update = false, $update_uid = 0) { $condition = ["`item`.`parent-uri` = (SELECT `parent-uri` FROM `item` WHERE `id` = ?) AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, local_user()]; $params = ['order' => ['uid', 'parent' => true, 'gravity', 'id']]; - $r = Item::selectForUser(local_user(), [], $condition, $params); + $items_obj = Item::selectForUser(local_user(), [], $condition, $params); - if (!DBM::is_result($r)) { + if (!DBM::is_result($items_obj)) { notice(L10n::t('Item not found.') . EOL); return $o; } - $s = dba::inArray($r); - if ($unseen) { $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true]; - dba::update('item', ['unseen' => false], $condition); + Item::update(['unseen' => false], $condition); } - $items = conv_sort($s, "`commented`"); + $items = conv_sort(dba::inArray($items_obj), "`commented`"); if (!$update) { $o .= ""; @@ -425,7 +409,8 @@ function display_content(App $a, $update = false, $update_uid = 0) { return $o; } -function displayShowFeed($item_id, $conversation) { +function displayShowFeed($item_id, $conversation) +{ $xml = DFRN::itemFeed($item_id, $conversation); if ($xml == '') { System::httpExit(500); diff --git a/mod/editpost.php b/mod/editpost.php index aa2c296845..04caaf0a20 100644 --- a/mod/editpost.php +++ b/mod/editpost.php @@ -8,37 +8,36 @@ use Friendica\Core\Addon; use Friendica\Core\Config; use Friendica\Core\L10n; use Friendica\Core\System; +use Friendica\Model\Item; use Friendica\Database\DBM; function editpost_content(App $a) { $o = ''; - if (! local_user()) { + if (!local_user()) { notice(L10n::t('Permission denied.') . EOL); return; } $post_id = (($a->argc > 1) ? intval($a->argv[1]) : 0); - if (! $post_id) { + if (!$post_id) { notice(L10n::t('Item not found') . EOL); return; } - $itm = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", - intval($post_id), - intval(local_user()) - ); - - if (! DBM::is_result($itm)) { + $fields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', + 'type', 'body', 'title', 'file']; + $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $post_id, 'uid' => local_user()]); + if (!DBM::is_result($item)) { notice(L10n::t('Item not found') . EOL); return; } $geotag = ''; - $o .= replace_macros(get_markup_template("section_title.tpl"),[ + $o .= replace_macros(get_markup_template("section_title.tpl"), [ '$title' => L10n::t('Edit post') ]); @@ -61,7 +60,7 @@ function editpost_content(App $a) { $tpl = get_markup_template("jot.tpl"); - if (strlen($itm['allow_cid']) || strlen($itm['allow_gid']) || strlen($itm['deny_cid']) || strlen($itm['deny_gid'])) { + if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) { $lockstate = 'lock'; } else { $lockstate = 'unlock'; @@ -70,40 +69,27 @@ function editpost_content(App $a) { $jotplugins = ''; $jotnets = ''; - $mail_disabled = ((function_exists('imap_open') && (! Config::get('system','imap_disabled'))) ? 0 : 1); + $mail_disabled = ((function_exists('imap_open') && !Config::get('system','imap_disabled')) ? 0 : 1); $mail_enabled = false; $pubmail_enabled = false; - if(! $mail_disabled) { + if (!$mail_disabled) { $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval(local_user()) ); if (DBM::is_result($r)) { $mail_enabled = true; - if(intval($r[0]['pubmail'])) + if (intval($r[0]['pubmail'])) { $pubmail_enabled = true; + } } } - // I don't think there's any need for the $jotnets when editing the post, - // and including them makes it difficult for the JS-free theme, so let's - // disable them -/* if($mail_enabled) { - $selected = (($pubmail_enabled) ? ' checked="checked" ' : ''); - $jotnets .= '
' - . L10n::t("Post to Email") . '
'; - }*/ - - - Addon::callHooks('jot_tool', $jotplugins); //Addon::callHooks('jot_networks', $jotnets); - - //$tpl = replace_macros($tpl,array('$jotplugins' => $jotplugins)); - - $o .= replace_macros($tpl,[ + $o .= replace_macros($tpl, [ '$is_edit' => true, '$return_path' => $_SESSION['return_url'], '$action' => 'item', @@ -124,8 +110,8 @@ function editpost_content(App $a) { '$shortnoloc' => L10n::t('clear location'), '$wait' => L10n::t('Please wait'), '$permset' => L10n::t('Permission settings'), - '$ptyp' => $itm[0]['type'], - '$content' => undo_post_tagging($itm[0]['body']), + '$ptyp' => $item['type'], + '$content' => undo_post_tagging($item['body']), '$post_id' => $post_id, '$baseurl' => System::baseUrl(), '$defloc' => $a->user['default-location'], @@ -134,9 +120,9 @@ function editpost_content(App $a) { '$emailcc' => L10n::t('CC: email addresses'), '$public' => L10n::t('Public post'), '$jotnets' => $jotnets, - '$title' => htmlspecialchars($itm[0]['title']), + '$title' => htmlspecialchars($item['title']), '$placeholdertitle' => L10n::t('Set title'), - '$category' => file_tag_file_to_list($itm[0]['file'], 'category'), + '$category' => file_tag_file_to_list($item['file'], 'category'), '$placeholdercategory' => (Feature::isEnabled(local_user(),'categories') ? L10n::t("Categories \x28comma-separated list\x29") : ''), '$emtitle' => L10n::t('Example: bob@example.com, mary@example.com'), '$lockstate' => $lockstate, diff --git a/mod/fetch.php b/mod/fetch.php index 6892990c24..da616ad5bb 100644 --- a/mod/fetch.php +++ b/mod/fetch.php @@ -7,7 +7,10 @@ use Friendica\App; use Friendica\Core\L10n; use Friendica\Core\System; use Friendica\Protocol\Diaspora; +use Friendica\Model\Item; +use Friendica\Model\User; use Friendica\Util\XML; +use Friendica\Database\DBM; function fetch_init(App $a) { @@ -20,24 +23,14 @@ function fetch_init(App $a) $guid = $a->argv[2]; // Fetch the item - $item = q( - "SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`, `coord` - FROM `item` WHERE `wall` AND NOT `private` AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1", - dbesc($guid), - NETWORK_DFRN, - NETWORK_DIASPORA - ); - if (!$item) { - $r = q( - "SELECT `author-link` - FROM `item` WHERE `uid` = 0 AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1", - dbesc($guid), - NETWORK_DFRN, - NETWORK_DIASPORA - ); - - if ($r) { - $parts = parse_url($r[0]["author-link"]); + $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network']; + $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]]; + $item = Item::selectFirst($fields, $condition); + if (!DBM::is_result($item)) { + $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]]; + $item = Item::selectFirst(['author-link'], $condition); + if (DBM::is_result($item)) { + $parts = parse_url($item["author-link"]); $host = $parts["scheme"]."://".$parts["host"]; if (normalise_link($host) != normalise_link(System::baseUrl())) { @@ -54,20 +47,13 @@ function fetch_init(App $a) } // Fetch some data from the author (We could combine both queries - but I think this is more readable) - $r = q( - "SELECT `user`.`prvkey`, `contact`.`addr`, `user`.`nickname`, `contact`.`nick` FROM `user` - INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self` - WHERE `user`.`uid` = %d", - intval($item[0]["uid"]) - ); - - if (!$r) { + $user = User::getOwnerDataById($item["uid"]); + if (!$user) { header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found')); killme(); } - $user = $r[0]; - $status = Diaspora::buildStatus($item[0], $user); + $status = Diaspora::buildStatus($item, $user); $xml = Diaspora::buildPostXml($status["type"], $status["message"]); // Send the envelope diff --git a/mod/photos.php b/mod/photos.php index f6540c2608..9cd3482746 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -19,6 +19,7 @@ use Friendica\Model\Group; use Friendica\Model\Item; use Friendica\Model\Photo; use Friendica\Model\Profile; +use Friendica\Model\User; use Friendica\Network\Probe; use Friendica\Object\Image; use Friendica\Protocol\DFRN; @@ -175,19 +176,14 @@ function photos_post(App $a) killme(); } - $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` - WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1", - intval($page_owner_uid) - ); + $owner_record = User::getOwnerDataById($page_owner_uid); - if (!DBM::is_result($r)) { + if (!$owner_record) { notice(L10n::t('Contact information unavailable') . EOL); logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid); killme(); } - $owner_record = $r[0]; - if ($a->argc > 3 && $a->argv[2] === 'album') { $album = hex2bin($a->argv[3]); @@ -487,14 +483,11 @@ function photos_post(App $a) } if ($item_id) { - $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", - intval($item_id), - intval($page_owner_uid) - ); + $item = Item::selectFirst(['tag', 'inform'], ['id' => $item_id, 'uid' => $page_owner_uid]); } - if (DBM::is_result($r)) { - $old_tag = $r[0]['tag']; - $old_inform = $r[0]['inform']; + if (DBM::is_result($item)) { + $old_tag = $item['tag']; + $old_inform = $item['inform']; } if (strlen($rawtags)) { diff --git a/mod/tagrm.php b/mod/tagrm.php index b9991d68da..dbe6f10877 100644 --- a/mod/tagrm.php +++ b/mod/tagrm.php @@ -10,29 +10,25 @@ use Friendica\Core\System; use Friendica\Database\DBM; use Friendica\Model\Item; -function tagrm_post(App $a) { - +function tagrm_post(App $a) +{ if (!local_user()) { goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); } - if ((x($_POST,'submit')) && ($_POST['submit'] === L10n::t('Cancel'))) { + if (x($_POST,'submit') && ($_POST['submit'] === L10n::t('Cancel'))) { goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); } - $tag = ((x($_POST,'tag')) ? hex2bin(notags(trim($_POST['tag']))) : ''); - $item = ((x($_POST,'item')) ? intval($_POST['item']) : 0 ); + $tag = (x($_POST,'tag') ? hex2bin(notags(trim($_POST['tag']))) : ''); + $item_id = (x($_POST,'item') ? intval($_POST['item']) : 0); - $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", - intval($item), - intval(local_user()) - ); - - if (!DBM::is_result($r)) { + $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]); + if (!DBM::is_result($item)) { goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); } - $arr = explode(',', $r[0]['tag']); + $arr = explode(',', $item['tag']); for ($x = 0; $x < count($arr); $x ++) { if ($arr[$x] === $tag) { unset($arr[$x]); @@ -42,7 +38,7 @@ function tagrm_post(App $a) { $tag_str = implode(',',$arr); - Item::update(['tag' => $tag_str], ['id' => $item]); + Item::update(['tag' => $tag_str], ['id' => $item_id]); info(L10n::t('Tag removed') . EOL ); goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); @@ -52,8 +48,8 @@ function tagrm_post(App $a) { -function tagrm_content(App $a) { - +function tagrm_content(App $a) +{ $o = ''; if (!local_user()) { @@ -61,22 +57,18 @@ function tagrm_content(App $a) { // NOTREACHED } - $item = (($a->argc > 1) ? intval($a->argv[1]) : 0); - if (!$item) { + $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0); + if (!$item_id) { goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); // NOTREACHED } - $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", - intval($item), - intval(local_user()) - ); - - if (!DBM::is_result($r)) { + $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]); + if (!DBM::is_result($item)) { goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); } - $arr = explode(',', $r[0]['tag']); + $arr = explode(',', $item['tag']); if (!count($arr)) { goaway(System::baseUrl() . '/' . $_SESSION['photo_return']); @@ -87,7 +79,7 @@ function tagrm_content(App $a) { $o .= '

' . L10n::t('Select a tag to remove: ') . '

'; $o .= ''; - $o .= ''; + $o .= ''; $o .= '