]> git.mxchange.org Git - friendica.git/blobdiff - mod/item.php
More usage of dbm::is_result($r) instead of count($r):
[friendica.git] / mod / item.php
index f7dc36fd414c0ed81cb454d7c153a84938c5d072..3d6433bec0a7fdf16d0b94cdc4f2f187d510f519 100644 (file)
@@ -102,7 +102,7 @@ function item_post(&$a) {
                }
 
                // if this isn't the real parent of the conversation, find it
-               if($r !== false && count($r)) {
+               if(dbm::is_result($r)) {
                        $parid = $r[0]['parent'];
                        $parent_uri = $r[0]['uri'];
                        if($r[0]['id'] != $r[0]['parent']) {
@@ -112,10 +112,10 @@ function item_post(&$a) {
                        }
                }
 
-               if(($r === false) || (! count($r))) {
+               if(! dbm::is_result($r)) {
                        notice( t('Unable to locate original post.') . EOL);
                        if(x($_REQUEST,'return'))
-                               goaway($a->get_baseurl() . "/" . $return_path );
+                               goaway($return_path);
                        killme();
                }
                $parent_item = $r[0];
@@ -141,7 +141,7 @@ function item_post(&$a) {
 
                                $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
                                        dbesc(normalise_link($thrparent[0]["author-link"])));
-                               if (count($r)) {
+                               if (dbm::is_result($r)) {
                                        $parent_contact = $r[0];
                                        $parent_contact["thumb"] = $parent_contact["photo"];
                                        $parent_contact["micro"] = $parent_contact["photo"];
@@ -173,7 +173,8 @@ function item_post(&$a) {
        $profile_uid = ((x($_REQUEST,'profile_uid')) ? intval($_REQUEST['profile_uid']) : 0);
        $post_id     = ((x($_REQUEST,'post_id'))     ? intval($_REQUEST['post_id'])     : 0);
        $app         = ((x($_REQUEST,'source'))      ? strip_tags($_REQUEST['source'])  : '');
-       $extid       = ((x($_REQUEST,'extid'))       ? strip_tags($_REQUEST['extid'])  : '');
+       $extid       = ((x($_REQUEST,'extid'))       ? strip_tags($_REQUEST['extid'])   : '');
+       $object      = ((x($_REQUEST,'object'))      ? $_REQUEST['object']              : '');
 
        // Check for multiple posts with the same message id (when the post was created via API)
        if (($message_id != '') AND ($profile_uid != 0)) {
@@ -197,7 +198,7 @@ function item_post(&$a) {
        if((x($_REQUEST,'commenter')) && ((! $parent) || (! $parent_item['wall']))) {
                notice( t('Permission denied.') . EOL) ;
                if(x($_REQUEST,'return'))
-                       goaway($a->get_baseurl() . "/" . $return_path );
+                       goaway($return_path);
                killme();
        }
 
@@ -209,7 +210,7 @@ function item_post(&$a) {
        if((! can_write_wall($a,$profile_uid)) && (! $allow_moderated)) {
                notice( t('Permission denied.') . EOL) ;
                if(x($_REQUEST,'return'))
-                       goaway($a->get_baseurl() . "/" . $return_path );
+                       goaway($return_path);
                killme();
        }
 
@@ -329,7 +330,7 @@ function item_post(&$a) {
                                $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
                                        intval(local_user())
                                );
-                               if(count($r) && intval($r[0]['pubmail']))
+                               if(dbm::is_result($r) && intval($r[0]['pubmail']))
                                        $pubmail_enabled = true;
                        }
                }
@@ -339,7 +340,7 @@ function item_post(&$a) {
                                killme();
                        info( t('Empty post discarded.') . EOL );
                        if(x($_REQUEST,'return'))
-                               goaway($a->get_baseurl() . "/" . $return_path );
+                               goaway($return_path);
                        killme();
                }
        }
@@ -463,7 +464,7 @@ function item_post(&$a) {
                                        intval($profile_uid)
                                );
 
-                               if(! count($r))
+                               if(! dbm::is_result($r))
                                        continue;
 
                                $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
@@ -718,6 +719,7 @@ function item_post(&$a) {
        $datarray['moderated']     = $allow_moderated;
        $datarray['gcontact-id']   = get_gcontact_id(array("url" => $datarray['author-link'], "network" => $datarray['network'],
                                                        "photo" => $datarray['author-avatar'], "name" => $datarray['author-name']));
+       $datarray['object']        = $object;
 
        /**
         * These fields are for the convenience of plugins...
@@ -744,6 +746,9 @@ function item_post(&$a) {
 
        if($preview) {
                require_once('include/conversation.php');
+               // We set the datarray ID to -1 because in preview mode the dataray
+               // doesn't have an ID.
+               $datarray["id"] = -1;
                $o = conversation($a,array(array_merge($contact_record,$datarray)),'search', false, true);
                logger('preview: ' . $o);
                echo json_encode(array('preview' => $o));
@@ -756,7 +761,7 @@ function item_post(&$a) {
        if(x($datarray,'cancel')) {
                logger('mod_item: post cancelled by plugin.');
                if($return_path) {
-                       goaway($a->get_baseurl() . "/" . $return_path);
+                       goaway($return_path);
                }
 
                $json = array('cancel' => 1);
@@ -795,7 +800,7 @@ function item_post(&$a) {
                proc_run(PRIORITY_HIGH, "include/notifier.php", 'edit_post', $post_id);
                if((x($_REQUEST,'return')) && strlen($return_path)) {
                        logger('return: ' . $return_path);
-                       goaway($a->get_baseurl() . "/" . $return_path );
+                       goaway($return_path);
                }
                killme();
        } else
@@ -812,7 +817,7 @@ function item_post(&$a) {
                                        `tag`, `inform`, `verb`, `object-type`, `postopts`,
                                        `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`,
                                        `pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file`,
-                                       `rendered-html`, `rendered-hash`,
+                                       `rendered-html`, `rendered-hash`, `gcontact-id`, `object`,
                                        `parent`, `parent-uri`, `plink`, `last-child`, `visible`)
                VALUES('%s', '%s', %d, '%s', %d, %d, '%s', %d,
                        '%s', '%s', '%s', %d,
@@ -822,7 +827,7 @@ function item_post(&$a) {
                        '%s', '%s', '%s', '%s', '%s',
                        '%s', '%s', '%s', '%s', %d,
                        %d, '%s', %d, %d, %d, '%s',
-                       '%s', '%s',
+                       '%s', '%s', %d, '%s',
                        %d, '%s', '%s', %d, %d)",
                dbesc($datarray['guid']),
                dbesc($datarray['extid']),
@@ -870,6 +875,8 @@ function item_post(&$a) {
                dbesc($datarray['file']),
                dbesc($datarray['rendered-html']),
                dbesc($datarray['rendered-hash']),
+               intval($datarray['gcontact-id']),
+               dbesc($datarray['object']),
                intval($datarray['parent']),
                dbesc($datarray['parent-uri']),
                dbesc($datarray['plink']),
@@ -877,17 +884,26 @@ function item_post(&$a) {
                intval($datarray['visible'])
               );
 
-       $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
-               dbesc($datarray['uri']));
-       if(!count($r)) {
+       if (dbm::is_result($r)) {
+               $r = q("SELECT LAST_INSERT_ID() AS `item-id`");
+               if (dbm::is_result($r)) {
+                       $post_id = $r[0]['item-id'];
+               } else {
+                       $post_id = 0;
+               }
+       } else {
+               logger('mod_item: unable to create post.');
+               $post_id = 0;
+       }
+
+       if ($post_id == 0) {
                q("COMMIT");
                logger('mod_item: unable to retrieve post that was just stored.');
-               notice( t('System error. Post not saved.') . EOL);
-               goaway($a->get_baseurl() . "/" . $return_path );
+               notice(t('System error. Post not saved.') . EOL);
+               goaway($return_path);
                // NOTREACHED
        }
 
-       $post_id = $r[0]['id'];
        logger('mod_item: saved item ' . $post_id);
 
        $datarray["id"] = $post_id;
@@ -1000,61 +1016,24 @@ function item_post(&$a) {
                }
        }
 
-       create_tags_from_item($post_id);
-       create_files_from_item($post_id);
-
        if ($post_id == $parent) {
                add_thread($post_id);
-               q("COMMIT");
-
-               add_shadow_thread($post_id);
        } else {
                update_thread($parent, true);
-               q("COMMIT");
-
-               // Insert an item entry for UID=0 for global entries
-               // We have to remove or change some data before that,
-               // so that the post appear like a regular received post.
-               // Additionally there is some data that isn't a database field.
-               $arr = $datarray;
-
-               $arr['app'] = $arr['source'];
-               unset($arr['source']);
-
-               unset($arr['self']);
-               unset($arr['wall']);
-               unset($arr['origin']);
-               unset($arr['api_source']);
-               unset($arr['message_id']);
-               unset($arr['profile_uid']);
-               unset($arr['post_id']);
-               unset($arr['dropitems']);
-               unset($arr['commenter']);
-               unset($arr['return']);
-               unset($arr['preview']);
-               unset($arr['post_id_random']);
-               unset($arr['emailcc']);
-               unset($arr['pubmail_enable']);
-               unset($arr['category']);
-               unset($arr['jsreload']);
-
-               if (in_array($arr['type'], array("net-comment", "wall-comment"))) {
-                       $arr['type'] = 'remote-comment';
-               } elseif ($arr['type'] == 'wall') {
-                       $arr['type'] = 'remote';
-               }
-               add_shadow_entry($arr);
        }
 
-       // This is a real juggling act on shared hosting services which kill your processes
-       // e.g. dreamhost. We used to start delivery to our native delivery agents in the background
-       // and then run our plugin delivery from the foreground. We're now doing plugin delivery first,
-       // because as soon as you start loading up a bunch of remote delivey processes, *this* page is
-       // likely to get killed off. If you end up looking at an /item URL and a blank page,
-       // it's very likely the delivery got killed before all your friends could be notified.
-       // Currently the only realistic fixes are to use a reliable server - which precludes shared hosting,
-       // or cut back on plugins which do remote deliveries.
+       q("COMMIT");
+
+       create_tags_from_item($post_id);
+       create_files_from_item($post_id);
+
+       // Insert an item entry for UID=0 for global entries.
+       // 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
+       proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "include/create_shadowentry.php", $post_id);
 
+       // Call the background process that is delivering the item to the receivers
        proc_run(PRIORITY_HIGH, "include/notifier.php", $notify_type, $post_id);
 
        logger('post_complete');
@@ -1070,7 +1049,7 @@ function item_post_return($baseurl, $api_source, $return_path) {
                return;
 
        if($return_path) {
-               goaway($baseurl . "/" . $return_path);
+               goaway($return_path);
        }
 
        $json = array('success' => 1);