]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
Log function
[friendica.git] / include / items.php
index e06df925e34ee9530c0c2f51d8c614175b00433b..72ae3a8e87ef42f5761558287b3989220bce74e7 100644 (file)
@@ -3,13 +3,16 @@
  * @file include/items.php
  */
 
+use Friendica\BaseObject;
 use Friendica\Content\Feature;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
+use Friendica\Core\Protocol;
 use Friendica\Core\System;
-use Friendica\Database\DBM;
+use Friendica\Database\DBA;
 use Friendica\Model\Item;
 use Friendica\Protocol\DFRN;
 use Friendica\Protocol\Feed;
@@ -23,9 +26,14 @@ require_once 'include/text.php';
 require_once 'mod/share.php';
 require_once 'include/enotify.php';
 
-function add_page_info_data($data, $no_photos = false) {
+function add_page_info_data(array $data, $no_photos = false)
+{
        Addon::callHooks('page_info_data', $data);
 
+       if (empty($data['type'])) {
+               return '';
+       }
+
        // It maybe is a rich content, but if it does have everything that a link has,
        // then treat it that way
        if (($data["type"] == "rich") && is_string($data["title"]) &&
@@ -65,7 +73,8 @@ function add_page_info_data($data, $no_photos = false) {
                $text .= " title='".$data["title"]."'";
        }
 
-       if (!empty($data["images"])) {
+       // Only embedd a picture link when it seems to be a valid picture ("width" is set)
+       if (!empty($data["images"]) && !empty($data["images"][0]["width"])) {
                $preview = str_replace(["[", "]"], ["[", "]"], htmlentities($data["images"][0]["src"], ENT_QUOTES, 'UTF-8', false));
                // if the preview picture is larger than 500 pixels then show it in a larger mode
                // But only, if the picture isn't higher than large (To prevent huge posts)
@@ -82,7 +91,7 @@ function add_page_info_data($data, $no_photos = false) {
        $hashtags = "";
        if (isset($data["keywords"]) && count($data["keywords"])) {
                $hashtags = "\n";
-               foreach ($data["keywords"] AS $keyword) {
+               foreach ($data["keywords"] as $keyword) {
                        /// @TODO make a positive list of allowed characters
                        $hashtag = str_replace([" ", "+", "/", ".", "#", "'", "’", "`", "(", ")", "„", "“"],
                                                ["", "", "", "", "", "", "", "", "", "", "", ""], $keyword);
@@ -93,15 +102,15 @@ function add_page_info_data($data, $no_photos = false) {
        return "\n".$text.$hashtags;
 }
 
-function query_page_info($url, $photo = "", $keywords = false, $keyword_blacklist = "") {
-
+function query_page_info($url, $photo = "", $keywords = false, $keyword_blacklist = "")
+{
        $data = ParseUrl::getSiteinfoCached($url, true);
 
        if ($photo != "") {
                $data["images"][0]["src"] = $photo;
        }
 
-       logger('fetch page info for ' . $url . ' ' . print_r($data, true), LOGGER_DEBUG);
+       Logger::log('fetch page info for ' . $url . ' ' . print_r($data, true), LOGGER_DEBUG);
 
        if (!$keywords && isset($data["keywords"])) {
                unset($data["keywords"]);
@@ -109,8 +118,10 @@ function query_page_info($url, $photo = "", $keywords = false, $keyword_blacklis
 
        if (($keyword_blacklist != "") && isset($data["keywords"])) {
                $list = explode(", ", $keyword_blacklist);
-               foreach ($list AS $keyword) {
+
+               foreach ($list as $keyword) {
                        $keyword = trim($keyword);
+
                        $index = array_search($keyword, $data["keywords"]);
                        if ($index !== false) {
                                unset($data["keywords"][$index]);
@@ -121,12 +132,13 @@ function query_page_info($url, $photo = "", $keywords = false, $keyword_blacklis
        return $data;
 }
 
-function add_page_keywords($url, $photo = "", $keywords = false, $keyword_blacklist = "") {
+function add_page_keywords($url, $photo = "", $keywords = false, $keyword_blacklist = "")
+{
        $data = query_page_info($url, $photo, $keywords, $keyword_blacklist);
 
        $tags = "";
        if (isset($data["keywords"]) && count($data["keywords"])) {
-               foreach ($data["keywords"] AS $keyword) {
+               foreach ($data["keywords"] as $keyword) {
                        $hashtag = str_replace([" ", "+", "/", ".", "#", "'"],
                                ["", "", "", "", "", ""], $keyword);
 
@@ -141,17 +153,22 @@ function add_page_keywords($url, $photo = "", $keywords = false, $keyword_blackl
        return $tags;
 }
 
-function add_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "") {
+function add_page_info($url, $no_photos = false, $photo = "", $keywords = false, $keyword_blacklist = "")
+{
        $data = query_page_info($url, $photo, $keywords, $keyword_blacklist);
 
-       $text = add_page_info_data($data, $no_photos);
+       $text = '';
+
+       if (is_array($data)) {
+               $text = add_page_info_data($data, $no_photos);
+       }
 
        return $text;
 }
 
-function add_page_info_to_body($body, $texturl = false, $no_photos = false) {
-
-       logger('add_page_info_to_body: fetch page info for body ' . $body, LOGGER_DEBUG);
+function add_page_info_to_body($body, $texturl = false, $no_photos = false)
+{
+       Logger::log('add_page_info_to_body: fetch page info for body ' . $body, LOGGER_DEBUG);
 
        $URLSearchString = "^\[\]";
 
@@ -227,122 +244,118 @@ function add_page_info_to_body($body, $texturl = false, $no_photos = false) {
  * model where comments can have sub-threads. That would require some massive sorting
  * to get all the feed items into a mostly linear ordering, and might still require
  * recursion.
- *
- * @TODO find proper type-hints
  */
-function consume_feed($xml, $importer, $contact, &$hub, $datedir = 0, $pass = 0) {
-       if ($contact['network'] === NETWORK_OSTATUS) {
+function consume_feed($xml, array $importer, array $contact, &$hub, $datedir = 0, $pass = 0)
+{
+       if ($contact['network'] === Protocol::OSTATUS) {
                if ($pass < 2) {
                        // Test - remove before flight
                        //$tempfile = tempnam(get_temppath(), "ostatus2");
                        //file_put_contents($tempfile, $xml);
-                       logger("Consume OStatus messages ", LOGGER_DEBUG);
+                       Logger::log("Consume OStatus messages ", LOGGER_DEBUG);
                        OStatus::import($xml, $importer, $contact, $hub);
                }
+
                return;
        }
 
-       if ($contact['network'] === NETWORK_FEED) {
+       if ($contact['network'] === Protocol::FEED) {
                if ($pass < 2) {
-                       logger("Consume feeds", LOGGER_DEBUG);
+                       Logger::log("Consume feeds", LOGGER_DEBUG);
                        Feed::import($xml, $importer, $contact, $hub);
                }
+
                return;
        }
 
-       if ($contact['network'] === NETWORK_DFRN) {
-               logger("Consume DFRN messages", LOGGER_DEBUG);
-
-               $r = q("SELECT `contact`.*, `contact`.`uid` AS `importer_uid`,
-                                       `contact`.`pubkey` AS `cpubkey`,
-                                       `contact`.`prvkey` AS `cprvkey`,
-                                       `contact`.`thumb` AS `thumb`,
-                                       `contact`.`url` as `url`,
-                                       `contact`.`name` as `senderName`,
-                                       `user`.*
-                       FROM `contact`
-                       LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
-                       WHERE `contact`.`id` = %d AND `user`.`uid` = %d",
-                       dbesc($contact["id"]), dbesc($importer["uid"])
-               );
-               if (DBM::is_result($r)) {
-                       logger("Now import the DFRN feed");
-                       DFRN::import($xml, $r[0], true);
+       if ($contact['network'] === Protocol::DFRN) {
+               Logger::log("Consume DFRN messages", LOGGER_DEBUG);
+               $dfrn_importer = DFRN::getImporter($contact["id"], $importer["uid"]);
+               if (!empty($dfrn_importer)) {
+                       Logger::log("Now import the DFRN feed");
+                       DFRN::import($xml, $dfrn_importer, true);
                        return;
                }
        }
 }
 
-function subscribe_to_hub($url, $importer, $contact, $hubmode = 'subscribe') {
-
-       $a = get_app();
-       $r = null;
-
-       if (is_array($importer)) {
-               $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
-                       intval($importer['uid'])
-               );
-       }
-
+function subscribe_to_hub($url, array $importer, array $contact, $hubmode = 'subscribe')
+{
        /*
         * Diaspora has different message-ids in feeds than they do
         * through the direct Diaspora protocol. If we try and use
         * the feed, we'll get duplicates. So don't.
         */
-       if ((!DBM::is_result($r)) || $contact['network'] === NETWORK_DIASPORA) {
+       if ($contact['network'] === Protocol::DIASPORA) {
+               return;
+       }
+
+       // Without an importer we don't have a user id - so we quit
+       if (empty($importer)) {
+               return;
+       }
+
+       $a = BaseObject::getApp();
+
+       $user = DBA::selectFirst('user', ['nickname'], ['uid' => $importer['uid']]);
+
+       // No user, no nickname, we quit
+       if (!DBA::isResult($user)) {
                return;
        }
 
-       $push_url = System::baseUrl() . '/pubsub/' . $r[0]['nickname'] . '/' . $contact['id'];
+       $push_url = System::baseUrl() . '/pubsub/' . $user['nickname'] . '/' . $contact['id'];
 
        // Use a single verify token, even if multiple hubs
        $verify_token = ((strlen($contact['hub-verify'])) ? $contact['hub-verify'] : random_string());
 
        $params= 'hub.mode=' . $hubmode . '&hub.callback=' . urlencode($push_url) . '&hub.topic=' . urlencode($contact['poll']) . '&hub.verify=async&hub.verify_token=' . $verify_token;
 
-       logger('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: '  . $push_url . ' with verifier ' . $verify_token);
+       Logger::log('subscribe_to_hub: ' . $hubmode . ' ' . $contact['name'] . ' to hub ' . $url . ' endpoint: '  . $push_url . ' with verifier ' . $verify_token);
 
        if (!strlen($contact['hub-verify']) || ($contact['hub-verify'] != $verify_token)) {
-               dba::update('contact', ['hub-verify' => $verify_token], ['id' => $contact['id']]);
+               DBA::update('contact', ['hub-verify' => $verify_token], ['id' => $contact['id']]);
        }
 
-       Network::post($url, $params);
+       $postResult = Network::post($url, $params);
 
-       logger('subscribe_to_hub: returns: ' . $a->get_curl_code(), LOGGER_DEBUG);
+       Logger::log('subscribe_to_hub: returns: ' . $postResult->getReturnCode(), LOGGER_DEBUG);
 
        return;
 
 }
 
-/// @TODO type-hint is array
-function drop_items($items) {
+function drop_items(array $items)
+{
        $uid = 0;
 
        if (!local_user() && !remote_user()) {
                return;
        }
 
-       if (count($items)) {
+       if (!empty($items)) {
                foreach ($items as $item) {
                        $owner = Item::deleteForUser(['id' => $item], local_user());
-                       if ($owner && !$uid)
+
+                       if ($owner && !$uid) {
                                $uid = $owner;
+                       }
                }
        }
 }
 
-function drop_item($id) {
-
-       $a = get_app();
+function drop_item($id)
+{
+       $a = BaseObject::getApp();
 
        // locate item to be deleted
 
-       $fields = ['id', 'uid', 'contact-id', 'deleted'];
+       $fields = ['id', 'uid', 'guid', 'contact-id', 'deleted'];
        $item = Item::selectFirstForUser(local_user(), $fields, ['id' => $id]);
 
-       if (!DBM::is_result($item)) {
+       if (!DBA::isResult($item)) {
                notice(L10n::t('Item not found.') . EOL);
-               goaway(System::baseUrl() . '/' . $_SESSION['return_url']);
+               $a->internalRedirect('network');
        }
 
        if ($item['deleted']) {
@@ -353,7 +366,7 @@ function drop_item($id) {
 
        // check if logged in user is either the author or owner of this item
 
-       if (is_array($_SESSION['remote'])) {
+       if (!empty($_SESSION['remote'])) {
                foreach ($_SESSION['remote'] as $visitor) {
                        if ($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
                                $contact_id = $visitor['cid'];
@@ -364,11 +377,12 @@ function drop_item($id) {
 
        if ((local_user() == $item['uid']) || $contact_id) {
                // Check if we should do HTML-based delete confirmation
-               if ($_REQUEST['confirm']) {
+               if (!empty($_REQUEST['confirm'])) {
                        // <form> can't take arguments in its "action" parameter
                        // so add any arguments as hidden inputs
                        $query = explode_querystring($a->query_string);
                        $inputs = [];
+
                        foreach ($query['args'] as $arg) {
                                if (strpos($arg, 'confirm=') === false) {
                                        $arg_parts = explode('=', $arg);
@@ -387,24 +401,25 @@ function drop_item($id) {
                        ]);
                }
                // Now check how the user responded to the confirmation query
-               if ($_REQUEST['canceled']) {
-                       goaway(System::baseUrl() . '/' . $_SESSION['return_url']);
+               if (!empty($_REQUEST['canceled'])) {
+                       $a->internalRedirect('display/' . $item['guid']);
                }
 
                // delete the item
                Item::deleteForUser(['id' => $item['id']], local_user());
 
-               goaway(System::baseUrl() . '/' . $_SESSION['return_url']);
+               $a->internalRedirect('network');
                //NOTREACHED
        } else {
                notice(L10n::t('Permission denied.') . EOL);
-               goaway(System::baseUrl() . '/' . $_SESSION['return_url']);
+               $a->internalRedirect('display/' . $item['guid']);
                //NOTREACHED
        }
 }
 
 /* arrange the list in years */
-function list_post_dates($uid, $wall) {
+function list_post_dates($uid, $wall)
+{
        $dnow = DateTimeFormat::localNow('Y-m-d');
 
        $dthen = Item::firstPostDate($uid, $wall);
@@ -440,7 +455,8 @@ function list_post_dates($uid, $wall) {
        return $ret;
 }
 
-function posted_date_widget($url, $uid, $wall) {
+function posted_date_widget($url, $uid, $wall)
+{
        $o = '';
 
        if (!Feature::isEnabled($uid, 'archives')) {
@@ -454,14 +470,11 @@ function posted_date_widget($url, $uid, $wall) {
                return $o;
        */
 
-       $visible_years = PConfig::get($uid,'system','archive_visible_years');
-       if (!$visible_years) {
-               $visible_years = 5;
-       }
+       $visible_years = PConfig::get($uid, 'system', 'archive_visible_years', 5);
 
        $ret = list_post_dates($uid, $wall);
 
-       if (!DBM::is_result($ret)) {
+       if (!DBA::isResult($ret)) {
                return $o;
        }