]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
Fix unused code in include
[friendica.git] / include / items.php
index 61a9c7a1fab5fab034050cd703a73f50df9b2e52..6000a851e9e9379eb6ec6dff18bfd943fcbba445 100644 (file)
@@ -10,24 +10,23 @@ use Friendica\Core\PConfig;
 use Friendica\Core\Worker;
 use Friendica\Core\System;
 use Friendica\Database\DBM;
-use Friendica\Model\GlobalContact;
-use Friendica\Object\Contact;
+use Friendica\Model\Contact;
+use Friendica\Model\GContact;
+use Friendica\Model\Group;
+use Friendica\Model\User;
+use Friendica\Object\Image;
 use Friendica\Protocol\DFRN;
 use Friendica\Protocol\OStatus;
-use Friendica\Util\Lock;
+use Friendica\Protocol\Feed;
 
 require_once 'include/bbcode.php';
-require_once 'include/oembed.php';
-require_once 'include/crypto.php';
 require_once 'include/tags.php';
 require_once 'include/files.php';
 require_once 'include/text.php';
 require_once 'include/threads.php';
 require_once 'include/plaintext.php';
-require_once 'include/feed.php';
 require_once 'mod/share.php';
 require_once 'include/enotify.php';
-require_once 'include/group.php';
 
 function construct_verb($item) {
        if ($item['verb']) {
@@ -38,14 +37,20 @@ function construct_verb($item) {
 
 /* limit_body_size()
  *
- *             The purpose of this function is to apply system message length limits to
- *             imported messages without including any embedded photos in the length
+ *
+ *
  */
-if (! function_exists('limit_body_size')) {
-function limit_body_size($body) {
-
-//     logger('limit_body_size: start', LOGGER_DEBUG);
 
+/**
+ * The purpose of this function is to apply system message length limits to
+ * imported messages without including any embedded photos in the length
+ *
+ * @brief Truncates imported message body string length to max_import_size
+ * @param string $body
+ * @return string
+ */
+function limit_body_size($body)
+{
        $maxlen = get_max_import_size();
 
        // If the length of the body, including the embedded images, is smaller
@@ -57,7 +62,6 @@ function limit_body_size($body) {
                $orig_body = $body;
                $new_body = '';
                $textlen = 0;
-               $max_found = false;
 
                $img_start = strpos($orig_body, '[img');
                $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
@@ -112,19 +116,17 @@ function limit_body_size($body) {
                        if ($textlen < $maxlen) {
                                logger('limit_body_size: the limit happens after the end of the last image', LOGGER_DEBUG);
                                $new_body = $new_body . substr($orig_body, 0, $maxlen - $textlen);
-                               $textlen = $maxlen;
                        }
                } else {
                        logger('limit_body_size: the text size with embedded images extracted did not violate the limit', LOGGER_DEBUG);
                        $new_body = $new_body . $orig_body;
-                       $textlen += strlen($orig_body);
                }
 
                return $new_body;
        } else {
                return $body;
        }
-}}
+}
 
 function title_is_body($title, $body) {
 
@@ -171,12 +173,6 @@ function add_page_info_data($data) {
                return "";
        }
 
-       if (sizeof($data["images"]) > 0) {
-               $preview = $data["images"][0];
-       } else {
-               $preview = "";
-       }
-
        // Escape some bad characters
        $data["url"] = str_replace(array("[", "]"), array("&#91;", "&#93;"), htmlentities($data["url"], ENT_QUOTES, 'UTF-8', false));
        $data["title"] = str_replace(array("[", "]"), array("&#91;", "&#93;"), htmlentities($data["title"], ENT_QUOTES, 'UTF-8', false));
@@ -423,7 +419,7 @@ function uri_to_guid($uri, $host = "") {
  * @return array Item array with removed conversation data
  */
 function store_conversation($arr) {
-       if (in_array($arr['network'], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)) && !empty($arr['uri'])) {
+       if (in_array(defaults($arr, 'network', NETWORK_PHANTOM), array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)) && !empty($arr['uri'])) {
                $conversation = array('item-uri' => $arr['uri'], 'received' => DBM::date());
 
                if (isset($arr['parent-uri']) && ($arr['parent-uri'] != $arr['uri'])) {
@@ -481,8 +477,8 @@ function store_conversation($arr) {
 }
 
 /// @TODO add type-hint array
-function item_store($arr, $force_parent = false, $notify = false, $dontcache = false) {
-
+function item_store($arr, $force_parent = false, $notify = false, $dontcache = false)
+{
        $a = get_app();
 
        // If it is a posting where users should get notifications, then define it as wall posting
@@ -504,6 +500,8 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
                                $arr['guid'] = uri_to_guid($arr['uri'], $a->get_hostname());
                        }
                }
+       } else {
+               $arr['network'] = trim(defaults($arr, 'network', NETWORK_PHANTOM));
        }
 
        if ($notify) {
@@ -583,7 +581,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
         * We have to check several networks since Friendica posts could be repeated
         * via OStatus (maybe Diasporsa as well)
         */
-       if (in_array(trim($arr['network']), array(NETWORK_DIASPORA, NETWORK_DFRN, NETWORK_OSTATUS, ""))) {
+       if (in_array($arr['network'], array(NETWORK_DIASPORA, NETWORK_DFRN, NETWORK_OSTATUS, ""))) {
                $r = q("SELECT `id`, `network` FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `network` IN ('%s', '%s', '%s')  LIMIT 1",
                                dbesc(trim($arr['uri'])),
                                intval($uid),
@@ -646,7 +644,6 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
        $arr['attach']        = ((x($arr, 'attach'))        ? notags(trim($arr['attach']))        : '');
        $arr['app']           = ((x($arr, 'app'))           ? notags(trim($arr['app']))           : '');
        $arr['origin']        = ((x($arr, 'origin'))        ? intval($arr['origin'])              : 0 );
-       $arr['network']       = ((x($arr, 'network'))       ? trim($arr['network'])               : '');
        $arr['postopts']      = ((x($arr, 'postopts'))      ? trim($arr['postopts'])              : '');
        $arr['resource-id']   = ((x($arr, 'resource-id'))   ? trim($arr['resource-id'])           : '');
        $arr['event-id']      = ((x($arr, 'event-id'))      ? intval($arr['event-id'])            : 0 );
@@ -676,18 +673,19 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
                $arr['plink'] = System::baseUrl() . '/display/' . urlencode($arr['guid']);
        }
 
-       if ($arr['network'] == "") {
+       if ($arr['network'] == NETWORK_PHANTOM) {
                $r = q("SELECT `network` FROM `contact` WHERE `network` IN ('%s', '%s', '%s') AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
                        dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS),
                        dbesc(normalise_link($arr['author-link'])),
                        intval($arr['uid'])
                );
 
-               if (!DBM::is_result($r))
+               if (!DBM::is_result($r)) {
                        $r = q("SELECT `network` FROM `gcontact` WHERE `network` IN ('%s', '%s', '%s') AND `nurl` = '%s' LIMIT 1",
                                dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS),
                                dbesc(normalise_link($arr['author-link']))
                        );
+               }
 
                if (!DBM::is_result($r)) {
                        $r = q("SELECT `network` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
@@ -735,17 +733,17 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
                logger("Contact-id was missing for post ".$arr["guid"]." from user id ".$uid." - now set to ".$arr["contact-id"], LOGGER_DEBUG);
        }
 
-       if ($arr["gcontact-id"] == 0) {
+       if (!x($arr, "gcontact-id")) {
                /*
                 * The gcontact should mostly behave like the contact. But is is supposed to be global for the system.
                 * This means that wall posts, repeated posts, etc. should have the gcontact id of the owner.
                 * On comments the author is the better choice.
                 */
                if ($arr['parent-uri'] === $arr['uri']) {
-                       $arr["gcontact-id"] = GlobalContact::getId(array("url" => $arr['owner-link'], "network" => $arr['network'],
+                       $arr["gcontact-id"] = GContact::getId(array("url" => $arr['owner-link'], "network" => $arr['network'],
                                                                 "photo" => $arr['owner-avatar'], "name" => $arr['owner-name']));
                } else {
-                       $arr["gcontact-id"] = GlobalContact::getId(array("url" => $arr['author-link'], "network" => $arr['network'],
+                       $arr["gcontact-id"] = GContact::getId(array("url" => $arr['author-link'], "network" => $arr['network'],
                                                                 "photo" => $arr['author-avatar'], "name" => $arr['author-name']));
                }
        }
@@ -854,7 +852,6 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
                        logger("item_store: Checking if parent ".$parent_id." has to be tagged as mention for user ".$arr['uid'], LOGGER_DEBUG);
                        $u = q("SELECT `nickname` FROM `user` WHERE `uid` = %d", intval($arr['uid']));
                        if (DBM::is_result($u)) {
-                               $a = get_app();
                                $self = normalise_link(System::baseUrl() . '/profile/' . $u[0]['nickname']);
                                logger("item_store: 'myself' is ".$self." for parent ".$parent_id." checking against ".$arr['author-link']." and ".$arr['owner-link'], LOGGER_DEBUG);
                                if ((normalise_link($arr['author-link']) == $self) || (normalise_link($arr['owner-link']) == $self)) {
@@ -1051,7 +1048,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
        }
 
        // Set parent id
-       $r = dba::update('item', array('parent' => $parent_id), array('id' => $current_post));
+       dba::update('item', array('parent' => $parent_id), array('id' => $current_post));
 
        $arr['id'] = $current_post;
        $arr['parent'] = $parent_id;
@@ -1205,8 +1202,6 @@ function item_body_set_hashtags(&$item) {
        // Otherwise there could be problems with hashtags like #test and #test2
        rsort($tags);
 
-       $a = get_app();
-
        $URLSearchString = "^\[\]";
 
        // All hashtags should point to the home server if "local_tags" is activated
@@ -1315,7 +1310,6 @@ function get_item_contact($item, $contacts) {
        foreach ($contacts as $contact) {
                if ($contact['id'] == $item['contact-id']) {
                        return $contact;
-                       break; // NOTREACHED
                }
        }
        return false;
@@ -1327,16 +1321,13 @@ function get_item_contact($item, $contacts) {
  * @param int $item_id
  * @return bool true if item was deleted, else false
  */
-function tag_deliver($uid, $item_id) {
-
-       $a = get_app();
-
+function tag_deliver($uid, $item_id)
+{
        $mention = false;
 
        $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
                intval($uid)
        );
-
        if (! DBM::is_result($u)) {
                return;
        }
@@ -1344,7 +1335,6 @@ function tag_deliver($uid, $item_id) {
        $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
        $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
 
-
        $i = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
                intval($item_id),
                intval($uid)
@@ -1550,7 +1540,7 @@ function consume_feed($xml, $importer, &$contact, &$hub, $datedir = 0, $pass = 0
        if ($contact['network'] === NETWORK_FEED) {
                if ($pass < 2) {
                        logger("Consume feeds", LOGGER_DEBUG);
-                       feed_import($xml, $importer, $contact, $hub);
+                       Feed::import($xml, $importer, $contact, $hub);
                }
                return;
        }
@@ -1674,15 +1664,13 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) {
        if (is_array($contact)) {
                if (($contact['network'] == NETWORK_OSTATUS && $contact['rel'] == CONTACT_IS_SHARING)
                        || ($sharing && $contact['rel'] == CONTACT_IS_FOLLOWER)) {
-                       $r = dba::update('contact', array('rel' => CONTACT_IS_FRIEND, 'writable' => true),
+                       dba::update('contact', array('rel' => CONTACT_IS_FRIEND, 'writable' => true),
                                        array('id' => $contact['id'], 'uid' => $importer['uid']));
                }
                // send email notification to owner?
        } else {
-
                // create contact record
-
-               $r = q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
+               q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
                        `blocked`, `readonly`, `pending`, `writable`)
                        VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1)",
                        intval($importer['uid']),
@@ -1695,6 +1683,7 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) {
                        dbesc(NETWORK_OSTATUS),
                        intval(CONTACT_IS_FOLLOWER)
                );
+
                $r = q("SELECT `id`, `network` FROM `contact` WHERE `uid` = %d AND `url` = '%s' AND `pending` = 1 LIMIT 1",
                                intval($importer['uid']),
                                dbesc($url)
@@ -1708,9 +1697,7 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) {
                $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
                        intval($importer['uid'])
                );
-
                if (DBM::is_result($r) && !in_array($r[0]['page-flags'], array(PAGE_SOAPBOX, PAGE_FREELOVE, PAGE_COMMUNITY))) {
-
                        // create notification
                        $hash = random_string();
 
@@ -1720,11 +1707,7 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) {
                                                        'hash' => $hash, 'datetime' => datetime_convert()));
                        }
 
-                       $def_gid = get_default_group($importer['uid'], $contact_record["network"]);
-
-                       if (intval($def_gid)) {
-                               group_add_member($importer['uid'], '', $contact_record['id'], $def_gid);
-                       }
+                       Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']);
 
                        if (($r[0]['notify-flags'] & NOTIFY_INTRO) &&
                                in_array($r[0]['page-flags'], array(PAGE_NORMAL))) {
@@ -1746,7 +1729,7 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) {
 
                        }
                } elseif (DBM::is_result($r) && in_array($r[0]['page-flags'], array(PAGE_SOAPBOX, PAGE_FREELOVE, PAGE_COMMUNITY))) {
-                       $r = q("UPDATE `contact` SET `pending` = 0 WHERE `uid` = %d AND `url` = '%s' AND `pending` LIMIT 1",
+                       q("UPDATE `contact` SET `pending` = 0 WHERE `uid` = %d AND `url` = '%s' AND `pending` LIMIT 1",
                                        intval($importer['uid']),
                                        dbesc($url)
                        );
@@ -1802,7 +1785,7 @@ function subscribe_to_hub($url, $importer, $contact, $hubmode = 'subscribe') {
        logger('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)) {
-               $r = dba::update('contact', array('hub-verify' => $verify_token), array('id' => $contact['id']));
+               dba::update('contact', array('hub-verify' => $verify_token), array('id' => $contact['id']));
        }
 
        post_url($url, $params);
@@ -1813,16 +1796,22 @@ function subscribe_to_hub($url, $importer, $contact, $hubmode = 'subscribe') {
 
 }
 
-function fix_private_photos($s, $uid, $item = null, $cid = 0) {
-
-       if (Config::get('system','disable_embedded')) {
+/**
+ *
+ * @param string $s
+ * @param int    $uid
+ * @param array  $item
+ * @param int    $cid
+ * @return string
+ */
+function fix_private_photos($s, $uid, $item = null, $cid = 0)
+{
+       if (Config::get('system', 'disable_embedded')) {
                return $s;
        }
 
-       $a = get_app();
-
        logger('fix_private_photos: check for photos', LOGGER_DEBUG);
-       $site = substr(System::baseUrl(),strpos(System::baseUrl(),'://'));
+       $site = substr(System::baseUrl(), strpos(System::baseUrl(), '://'));
 
        $orig_body = $s;
        $new_body = '';
@@ -1830,19 +1819,18 @@ function fix_private_photos($s, $uid, $item = null, $cid = 0) {
        $img_start = strpos($orig_body, '[img');
        $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
        $img_len = ($img_start !== false ? strpos(substr($orig_body, $img_start + $img_st_close + 1), '[/img]') : false);
-       while ( ($img_st_close !== false) && ($img_len !== false) ) {
 
+       while (($img_st_close !== false) && ($img_len !== false)) {
                $img_st_close++; // make it point to AFTER the closing bracket
                $image = substr($orig_body, $img_start + $img_st_close, $img_len);
 
                logger('fix_private_photos: found photo ' . $image, LOGGER_DEBUG);
 
-
-               if (stristr($image , $site . '/photo/')) {
+               if (stristr($image, $site . '/photo/')) {
                        // Only embed locally hosted photos
                        $replace = false;
                        $i = basename($image);
-                       $i = str_replace(array('.jpg', '.png', '.gif'),array('', '',''), $i);
+                       $i = str_replace(array('.jpg', '.png', '.gif'), array('', '', ''), $i);
                        $x = strpos($i, '-');
 
                        if ($x) {
@@ -1852,7 +1840,6 @@ function fix_private_photos($s, $uid, $item = null, $cid = 0) {
                                        dbesc($i),
                                        intval($res),
                                        intval($uid)
-
                                );
                                if (DBM::is_result($r)) {
                                        /*
@@ -1887,11 +1874,11 @@ function fix_private_photos($s, $uid, $item = null, $cid = 0) {
                                                        $width = intval($match[1]);
                                                        $height = intval($match[2]);
 
-                                                       $ph = new Photo($data, $type);
-                                                       if ($ph->isValid()) {
-                                                               $ph->scaleImage(max($width, $height));
-                                                               $data = $ph->imageString();
-                                                               $type = $ph->getType();
+                                                       $Image = new Image($data, $type);
+                                                       if ($Image->isValid()) {
+                                                               $Image->scaleDown(max($width, $height));
+                                                               $data = $Image->asString();
+                                                               $type = $Image->getType();
                                                        }
                                                }
 
@@ -1958,9 +1945,9 @@ function compare_permissions($obj1, $obj2) {
 /// @TODO type-hint is array
 function enumerate_permissions($obj) {
        $allow_people = expand_acl($obj['allow_cid']);
-       $allow_groups = expand_groups(expand_acl($obj['allow_gid']));
+       $allow_groups = Group::expand(expand_acl($obj['allow_gid']));
        $deny_people  = expand_acl($obj['deny_cid']);
-       $deny_groups  = expand_groups(expand_acl($obj['deny_gid']));
+       $deny_groups  = Group::expand(expand_acl($obj['deny_gid']));
        $recipients   = array_unique(array_merge($allow_people, $allow_groups));
        $deny         = array_unique(array_merge($deny_people, $deny_groups));
        $recipients   = array_diff($recipients, $deny);
@@ -2165,7 +2152,7 @@ function drop_item($id, $interactive = true) {
                logger('delete item: ' . $item['id'], LOGGER_DEBUG);
 
                // delete the item
-               $r = dba::update('item', array('deleted' => true, 'title' => '', 'body' => '',
+               dba::update('item', array('deleted' => true, 'title' => '', 'body' => '',
                                        'edited' => datetime_convert(), 'changed' => datetime_convert()),
                                array('id' => $item['id']));