]> git.mxchange.org Git - friendica.git/blobdiff - mod/item.php
Fix formatting and PHP Notice in frio theme
[friendica.git] / mod / item.php
index 400acfc971a3c5bad9ba6e3efdfe1bcb7a80b442..1faef960163c7845bad16b15a814d58a747dd667 100644 (file)
@@ -1,4 +1,7 @@
 <?php
+/**
+ * @file mod/item.php
+ */
 
 /*
  * This is the POST destination for most all locally posted
  * Posts that originate externally or do not fall into the above
  * posting categories go through item_store() instead of this function.
  */
-
 use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
-use Friendica\Model\GlobalContact;
+use Friendica\Model\Contact;
+use Friendica\Model\GContact;
 use Friendica\Network\Probe;
 use Friendica\Protocol\Diaspora;
+use Friendica\Protocol\Email;
+use Friendica\Util\Emailer;
 
-require_once 'include/crypto.php';
 require_once 'include/enotify.php';
-require_once 'include/email.php';
 require_once 'include/tags.php';
 require_once 'include/files.php';
 require_once 'include/threads.php';
 require_once 'include/text.php';
 require_once 'include/items.php';
-require_once 'include/Contact.php';
 
 function item_post(App $a) {
 
-       if ((! local_user()) && (! remote_user()) && (! x($_REQUEST, 'commenter'))) {
+       if (!local_user() && !remote_user() && !x($_REQUEST, 'commenter')) {
                return;
        }
 
@@ -78,8 +80,8 @@ function item_post(App $a) {
        }
 
        // Is this a reply to something?
-       $parent = ((x($_REQUEST, 'parent')) ? intval($_REQUEST['parent']) : 0);
-       $parent_uri = ((x($_REQUEST, 'parent_uri')) ? trim($_REQUEST['parent_uri']) : '');
+       $parent = (x($_REQUEST, 'parent') ? intval($_REQUEST['parent']) : 0);
+       $parent_uri = (x($_REQUEST, 'parent_uri') ? trim($_REQUEST['parent_uri']) : '');
 
        $parent_item = null;
        $parent_contact = null;
@@ -119,7 +121,7 @@ function item_post(App $a) {
                        }
                }
 
-               if (! DBM::is_result($r)) {
+               if (!DBM::is_result($r)) {
                        notice( t('Unable to locate original post.') . EOL);
                        if (x($_REQUEST, 'return')) {
                                goaway($return_path);
@@ -146,7 +148,7 @@ function item_post(App $a) {
                        $thrparent = q("SELECT `author-link`, `network` FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($thr_parent));
                        if (DBM::is_result($thrparent) && ($thrparent[0]["network"] === NETWORK_OSTATUS)
                                && (normalise_link($parent_contact["url"]) != normalise_link($thrparent[0]["author-link"]))) {
-                               $parent_contact = get_contact_details_by_url($thrparent[0]["author-link"]);
+                               $parent_contact = Contact::getDetailsByURL($thrparent[0]["author-link"]);
 
                                if (!isset($parent_contact["nick"])) {
                                        $probed_contact = Probe::uri($thrparent[0]["author-link"]);
@@ -198,7 +200,7 @@ function item_post(App $a) {
 
        // First check that the parent exists and it is a wall item.
 
-       if ((x($_REQUEST, 'commenter')) && ((! $parent) || (! $parent_item['wall']))) {
+       if (x($_REQUEST, 'commenter') && (!$parent || !$parent_item['wall'])) {
                notice(t('Permission denied.') . EOL) ;
                if (x($_REQUEST, 'return')) {
                        goaway($return_path);
@@ -206,11 +208,14 @@ function item_post(App $a) {
                killme();
        }
 
+       // Allow commenting if it is an answer to a public post
+       $allow_comment = ($profile_uid == 0) && $parent && in_array($parent_item['network'], [NETWORK_OSTATUS, NETWORK_DIASPORA]);
+
        /*
         * Now check that it is a page_type of PAGE_BLOG, and that valid personal details
         * have been provided, and run any anti-spam plugins
         */
-       if ((! can_write_wall($a, $profile_uid)) && (! $allow_moderated)) {
+       if (!(can_write_wall($a, $profile_uid) || $allow_comment) && !$allow_moderated) {
                notice(t('Permission denied.') . EOL) ;
                if (x($_REQUEST, 'return')) {
                        goaway($return_path);
@@ -374,11 +379,11 @@ function item_post(App $a) {
        $self   = false;
        $contact_id = 0;
 
-       if ((local_user()) && (local_user() == $profile_uid)) {
+       if (local_user() && ((local_user() == $profile_uid) || $allow_comment)) {
                $self = true;
-               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
+               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
                        intval($_SESSION['uid']));
-       } elseif(remote_user()) {
+       } elseif (remote_user()) {
                if (x($_SESSION, 'remote') && is_array($_SESSION['remote'])) {
                        foreach ($_SESSION['remote'] as $v) {
                                if ($v['uid'] == $profile_uid) {
@@ -401,10 +406,10 @@ function item_post(App $a) {
 
        // get contact info for owner
 
-       if ($profile_uid == local_user()) {
+       if ($profile_uid == local_user() || $allow_comment) {
                $contact_record = $author;
        } else {
-               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
+               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
                        intval($profile_uid)
                );
                if (DBM::is_result($r)) {
@@ -422,6 +427,114 @@ function item_post(App $a) {
                }
        }
 
+       // Look for any tags and linkify them
+       $str_tags = '';
+       $inform   = '';
+
+       $tags = get_tags($body);
+
+       /*
+        * add a statusnet style reply tag if the original post was from there
+        * and we are replying, and there isn't one already
+        */
+       if ($parent && ($parent_contact['network'] == NETWORK_OSTATUS)) {
+               $contact = '@[url=' . $parent_contact['url'] . ']' . $parent_contact['nick'] . '[/url]';
+
+               if (!in_array($contact, $tags)) {
+                       $body = $contact . ' ' . $body;
+                       $tags[] = $contact;
+               }
+
+               $toplevel_contact = "";
+               $toplevel_parent = q("SELECT `contact`.* FROM `contact`
+                                               INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `contact`.`url` = `item`.`author-link`
+                                               WHERE `item`.`id` = `item`.`parent` AND `item`.`parent` = %d", intval($parent));
+               if (DBM::is_result($toplevel_parent)) {
+                       if (!empty($toplevel_parent[0]['addr'])) {
+                               $toplevel_contact = '@' . $toplevel_parent[0]['addr'];
+                       } else {
+                               $toplevel_contact = '@' . $toplevel_parent[0]['nick'] . '+' . $toplevel_parent[0]['id'];
+                       }
+               } else {
+                       $toplevel_parent = q("SELECT `author-link`, `author-name` FROM `item` WHERE `id` = `parent` AND `parent` = %d", intval($parent));
+                       $toplevel_contact = '@[url=' . $toplevel_parent[0]['author-link'] . ']' . $toplevel_parent[0]['author-name'] . '[/url]';
+               }
+
+               if (!in_array($toplevel_contact, $tags)) {
+                       $tags[] = $toplevel_contact;
+               }
+       }
+
+       $tagged = array();
+
+       $private_forum = false;
+       $only_to_forum = false;
+       $forum_contact = array();
+
+       if (count($tags)) {
+               foreach ($tags as $tag) {
+
+                       $tag_type = substr($tag, 0, 1);
+
+                       if ($tag_type == '#') {
+                               continue;
+                       }
+
+                       /*
+                        * If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
+                        * Robert Johnson should be first in the $tags array
+                        */
+                       $fullnametagged = false;
+                       /// @TODO $tagged is initialized above if() block and is not filled, maybe old-lost code?
+                       foreach ($tagged as $nextTag) {
+                               if (stristr($nextTag, $tag . ' ')) {
+                                       $fullnametagged = true;
+                                       break;
+                               }
+                       }
+                       if ($fullnametagged) {
+                               continue;
+                       }
+
+                       $success = handle_tag($a, $body, $inform, $str_tags, (local_user()) ? local_user() : $profile_uid , $tag, $network);
+                       if ($success['replaced']) {
+                               $tagged[] = $tag;
+                       }
+                       // When the forum is private or the forum is addressed with a "!" make the post private
+                       if (is_array($success['contact']) && ($success['contact']['prv'] || ($tag_type == '!'))) {
+                               $private_forum = $success['contact']['prv'];
+                               $only_to_forum = ($tag_type == '!');
+                               $private_id = $success['contact']['id'];
+                               $forum_contact = $success['contact'];
+                       } elseif (is_array($success['contact']) && $success['contact']['forum'] &&
+                               ($str_contact_allow == '<' . $success['contact']['id'] . '>')) {
+                               $private_forum = false;
+                               $only_to_forum = true;
+                               $private_id = $success['contact']['id'];
+                               $forum_contact = $success['contact'];
+                       }
+               }
+       }
+
+       $original_contact_id = $contact_id;
+
+       if (!$parent && count($forum_contact) && ($private_forum || $only_to_forum)) {
+               // we tagged a forum in a top level post. Now we change the post
+               $private = $private_forum;
+
+               $str_group_allow = '';
+               $str_contact_deny = '';
+               $str_group_deny = '';
+               if ($private_forum) {
+                       $str_contact_allow = '<' . $private_id . '>';
+               } else {
+                       $str_contact_allow = '';
+               }
+               $contact_id = $private_id;
+               $contact_record = $forum_contact;
+               $_REQUEST['origin'] = false;
+       }
+
        /*
         * When a photo was uploaded into the message using the (profile wall) ajax
         * uploader, The permissions are initially set to disallow anybody but the
@@ -434,22 +547,22 @@ function item_post(App $a) {
 
        $match = null;
 
-       if ((! $preview) && preg_match_all("/\[img([\=0-9x]*?)\](.*?)\[\/img\]/",$body,$match)) {
+       if (!$preview && preg_match_all("/\[img([\=0-9x]*?)\](.*?)\[\/img\]/",$body,$match)) {
                $images = $match[2];
                if (count($images)) {
 
                        $objecttype = ACTIVITY_OBJ_IMAGE;
 
                        foreach ($images as $image) {
-                               if (! stristr($image,System::baseUrl() . '/photo/')) {
+                               if (!stristr($image, System::baseUrl() . '/photo/')) {
                                        continue;
                                }
                                $image_uri = substr($image,strrpos($image,'/') + 1);
                                $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
-                               if (! strlen($image_uri)) {
+                               if (!strlen($image_uri)) {
                                        continue;
                                }
-                               $srch = '<' . intval($contact_id) . '>';
+                               $srch = '<' . intval($original_contact_id) . '>';
 
                                $r = q("SELECT `id` FROM `photo` WHERE `allow_cid` = '%s' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''
                                        AND `resource-id` = '%s' AND `uid` = %d LIMIT 1",
@@ -470,7 +583,7 @@ function item_post(App $a) {
                                        dbesc($str_group_deny),
                                        dbesc($image_uri),
                                        intval($profile_uid),
-                                       dbesc( t('Wall Photos'))
+                                       dbesc(t('Wall Photos'))
                                );
                        }
                }
@@ -528,122 +641,16 @@ function item_post(App $a) {
                require_once 'include/plaintext.php';
                $objectdata = get_attached_data($body);
 
-               if ($post["type"] == "link") {
+               if ($objectdata["type"] == "link") {
                        $objecttype = ACTIVITY_OBJ_BOOKMARK;
-               } elseif ($post["type"] == "video") {
+               } elseif ($objectdata["type"] == "video") {
                        $objecttype = ACTIVITY_OBJ_VIDEO;
-               } elseif ($post["type"] == "photo") {
+               } elseif ($objectdata["type"] == "photo") {
                        $objecttype = ACTIVITY_OBJ_IMAGE;
                }
 
        }
 
-       // Look for any tags and linkify them
-       $str_tags = '';
-       $inform   = '';
-
-       $tags = get_tags($body);
-
-       /*
-        * add a statusnet style reply tag if the original post was from there
-        * and we are replying, and there isn't one already
-        */
-       if ($parent && ($parent_contact['network'] == NETWORK_OSTATUS)) {
-               $contact = '@[url=' . $parent_contact['url'] . ']' . $parent_contact['nick'] . '[/url]';
-
-               if (!in_array($contact, $tags)) {
-                       $body = $contact . ' ' . $body;
-                       $tags[] = $contact;
-               }
-
-               $toplevel_contact = "";
-               $toplevel_parent = q("SELECT `contact`.* FROM `contact`
-                                               INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `contact`.`url` = `item`.`author-link`
-                                               WHERE `item`.`id` = `item`.`parent` AND `item`.`parent` = %d", intval($parent));
-               if (DBM::is_result($toplevel_parent)) {
-                       if (!empty($toplevel_parent[0]['addr'])) {
-                               $toplevel_contact = '@' . $toplevel_parent[0]['addr'];
-                       } else {
-                               $toplevel_contact = '@' . $toplevel_parent[0]['nick'] . '+' . $toplevel_parent[0]['id'];
-                       }
-               } else {
-                       $toplevel_parent = q("SELECT `author-link`, `author-name` FROM `item` WHERE `id` = `parent` AND `parent` = %d", intval($parent));
-                       $toplevel_contact = '@[url=' . $toplevel_parent[0]['author-link'] . ']' . $toplevel_parent[0]['author-name'] . '[/url]';
-               }
-
-               if (!in_array($toplevel_contact, $tags)) {
-                       $tags[] = $toplevel_contact;
-               }
-       }
-
-       $tagged = array();
-
-       $private_forum = false;
-       $only_to_forum = false;
-       $forum_contact = array();
-
-       if (count($tags)) {
-               foreach ($tags as $tag) {
-
-                       $tag_type = substr($tag, 0, 1);
-
-                       if ($tag_type == '#') {
-                               continue;
-                       }
-
-                       /*
-                        * If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
-                        * Robert Johnson should be first in the $tags array
-                        */
-                       $fullnametagged = false;
-                       /// @TODO $tagged is initialized above if() block and is not filled, maybe old-lost code?
-                       foreach ($tagged as $nextTag) {
-                               if (stristr($nextTag, $tag . ' ')) {
-                                       $fullnametagged = true;
-                                       break;
-                               }
-                       }
-                       if ($fullnametagged) {
-                               continue;
-                       }
-
-                       $success = handle_tag($a, $body, $inform, $str_tags, (local_user()) ? local_user() : $profile_uid , $tag, $network);
-                       if ($success['replaced']) {
-                               $tagged[] = $tag;
-                       }
-                       // When the forum is private or the forum is addressed with a "!" make the post private
-                       if (is_array($success['contact']) && ($success['contact']['prv'] || ($tag_type == '!'))) {
-                               $private_forum = $success['contact']['prv'];
-                               $only_to_forum = ($tag_type == '!');
-                               $private_id = $success['contact']['id'];
-                               $forum_contact = $success['contact'];
-                       } elseif (is_array($success['contact']) && $success['contact']['forum'] &&
-                               ($str_contact_allow == '<' . $success['contact']['id'] . '>')) {
-                               $private_forum = false;
-                               $only_to_forum = true;
-                               $private_id = $success['contact']['id'];
-                               $forum_contact = $success['contact'];
-                       }
-               }
-       }
-
-       if (!$parent && count($forum_contact) && ($private_forum || $only_to_forum)) {
-               // we tagged a forum in a top level post. Now we change the post
-               $private = $private_forum;
-
-               $str_group_allow = '';
-               $str_contact_deny = '';
-               $str_group_deny = '';
-               if ($private_forum) {
-                       $str_contact_allow = '<' . $private_id . '>';
-               } else {
-                       $str_contact_allow = '';
-               }
-               $contact_id = $private_id;
-               $contact_record = $forum_contact;
-               $_REQUEST['origin'] = false;
-       }
-
        $attachments = '';
        $match = false;
 
@@ -703,11 +710,11 @@ function item_post(App $a) {
        $datarray['owner-name']    = $contact_record['name'];
        $datarray['owner-link']    = $contact_record['url'];
        $datarray['owner-avatar']  = $contact_record['thumb'];
-       $datarray['owner-id']      = get_contact($datarray['owner-link'], 0);
+       $datarray['owner-id']      = Contact::getIdForURL($datarray['owner-link'], 0);
        $datarray['author-name']   = $author['name'];
        $datarray['author-link']   = $author['url'];
        $datarray['author-avatar'] = $author['thumb'];
-       $datarray['author-id']     = get_contact($datarray['author-link'], 0);
+       $datarray['author-id']     = Contact::getIdForURL($datarray['author-link'], 0);
        $datarray['created']       = datetime_convert();
        $datarray['edited']        = datetime_convert();
        $datarray['commented']     = datetime_convert();
@@ -738,7 +745,7 @@ function item_post(App $a) {
        $datarray['postopts']      = $postopts;
        $datarray['origin']        = $origin;
        $datarray['moderated']     = $allow_moderated;
-       $datarray['gcontact-id']   = GlobalContact::getId(array("url" => $datarray['author-link'], "network" => $datarray['network'],
+       $datarray['gcontact-id']   = GContact::getId(array("url" => $datarray['author-link'], "network" => $datarray['network'],
                                                        "photo" => $datarray['author-avatar'], "name" => $datarray['author-name']));
        $datarray['object']        = $object;
 
@@ -834,7 +841,7 @@ function item_post(App $a) {
                // update filetags in pconfig
                file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
 
-               Worker::add(PRIORITY_HIGH, "notifier", 'edit_post', $post_id);
+               Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $post_id);
                if ((x($_REQUEST, 'return')) && strlen($return_path)) {
                        logger('return: ' . $return_path);
                        goaway($return_path);
@@ -984,7 +991,7 @@ function item_post(App $a) {
 
 
                // Store the comment signature information in case we need to relay to Diaspora
-               Diaspora::store_comment_signature($datarray, $author, ($self ? $user['prvkey'] : false), $post_id);
+               Diaspora::storeCommentSignature($datarray, $author, ($self ? $user['prvkey'] : false), $post_id);
 
        } else {
                $parent = $post_id;
@@ -1027,22 +1034,22 @@ function item_post(App $a) {
                                $disclaimer .= sprintf( t('You may visit them online at %s'), System::baseUrl() . '/profile/' . $a->user['nickname']) . EOL;
                                $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
                                if (!$datarray['title']=='') {
-                                       $subject = email_header_encode($datarray['title'], 'UTF-8');
+                                       $subject = Email::encodeHeader($datarray['title'], 'UTF-8');
                                } else {
-                                       $subject = email_header_encode('[Friendica]' . ' ' . sprintf( t('%s posted an update.'), $a->user['username']), 'UTF-8');
+                                       $subject = Email::encodeHeader('[Friendica]' . ' ' . sprintf( t('%s posted an update.'), $a->user['username']), 'UTF-8');
                                }
                                $link = '<a href="' . System::baseUrl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
                                $html    = prepare_body($datarray);
                                $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
                                include_once 'include/html2plain.php';
                                $params = array (
-                                   'fromName' => $a->user['username'],
-                                   'fromEmail' => $a->user['email'],
-                                   'toEmail' => $addr,
-                                   'replyTo' => $a->user['email'],
-                                   'messageSubject' => $subject,
-                                   'htmlVersion' => $message,
-                                   'textVersion' => html2plain($html.$disclaimer),
+                                       'fromName' => $a->user['username'],
+                                       'fromEmail' => $a->user['email'],
+                                       'toEmail' => $addr,
+                                       'replyTo' => $a->user['email'],
+                                       'messageSubject' => $subject,
+                                       'htmlVersion' => $message,
+                                       'textVersion' => html2plain($html.$disclaimer)
                                );
                                Emailer::send($params);
                        }
@@ -1064,10 +1071,10 @@ function item_post(App $a) {
        // We now do it in the background to save some time.
        // This is important in interactive environments like the frontend or the API.
        // We don't fork a new process since this is done anyway with the following command
-       Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "create_shadowentry", $post_id);
+       Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "CreateShadowEntry", $post_id);
 
        // Call the background process that is delivering the item to the receivers
-       Worker::add(PRIORITY_HIGH, "notifier", $notify_type, $post_id);
+       Worker::add(PRIORITY_HIGH, "Notifier", $notify_type, $post_id);
 
        logger('post_complete');
 
@@ -1239,7 +1246,7 @@ function handle_tag(App $a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $n
                        if (!DBM::is_result($r)) {
                                $probed = Probe::uri($name);
                                if ($result['network'] != NETWORK_PHANTOM) {
-                                       GlobalContact::update($probed);
+                                       GContact::update($probed);
                                        $r = q("SELECT `url`, `name`, `nick`, `network`, `alias`, `notify` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
                                                dbesc(normalise_link($probed["url"])));
                                }