]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
Merge remote-tracking branch 'upstream/develop' into 1511-new-ostatus-delivery
[friendica.git] / include / items.php
index 5c7aa52e6fa70df0822da0aa9eb873bd3f06826c..53f4784c2d053fe06661a979d4ffdca507d71937 100644 (file)
@@ -2381,85 +2381,45 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                $contact_updated = $photo_timestamp;
 
                require_once("include/Photo.php");
-               $photo_failure = false;
-               $have_photo = false;
+               $photos = import_profile_photo($photo_url,$contact['uid'],$contact['id']);
 
-               $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
-                       intval($contact['id']),
-                       intval($contact['uid'])
+               q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
+                       WHERE `uid` = %d AND `id` = %d AND NOT `self`",
+                       dbesc(datetime_convert()),
+                       dbesc($photos[0]),
+                       dbesc($photos[1]),
+                       dbesc($photos[2]),
+                       intval($contact['uid']),
+                       intval($contact['id'])
                );
-               if(count($r)) {
-                       $resource_id = $r[0]['resource-id'];
-                       $have_photo = true;
-               }
-               else {
-                       $resource_id = photo_new_resource();
-               }
-
-               $img_str = fetch_url($photo_url,true);
-               // guess mimetype from headers or filename
-               $type = guess_image_type($photo_url,true);
-
-
-               $img = new Photo($img_str, $type);
-               if($img->is_valid()) {
-                       if($have_photo) {
-                               q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
-                                       dbesc($resource_id),
-                                       intval($contact['id']),
-                                       intval($contact['uid'])
-                               );
-                       }
-
-                       $img->scaleImageSquare(175);
-
-                       $hash = $resource_id;
-                       $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 4);
-
-                       $img->scaleImage(80);
-                       $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 5);
-
-                       $img->scaleImage(48);
-                       $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), 'Contact Photos', 6);
-
-                       $a = get_app();
-
-                       q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
-                               WHERE `uid` = %d AND `id` = %d",
-                               dbesc(datetime_convert()),
-                               dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
-                               dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
-                               dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
-                               intval($contact['uid']),
-                               intval($contact['id'])
-                       );
-               }
        }
 
        if((is_array($contact)) && ($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
                if ($name_updated > $contact_updated)
                        $contact_updated = $name_updated;
 
-               $r = q("select * from contact where uid = %d and id = %d limit 1",
+               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
                        intval($contact['uid']),
                        intval($contact['id'])
                );
 
-               $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d",
+               $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d AND `name` != '%s' AND NOT `self`",
                        dbesc(notags(trim($new_name))),
                        dbesc(datetime_convert()),
                        intval($contact['uid']),
-                       intval($contact['id'])
+                       intval($contact['id']),
+                       dbesc(notags(trim($new_name)))
                );
 
                // do our best to update the name on content items
 
-               if(count($r)) {
-                       q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
+               if(count($r) AND (notags(trim($new_name)) != $r[0]['name'])) {
+                       q("UPDATE `item` SET `author-name` = '%s' WHERE `author-name` = '%s' AND `author-link` = '%s' AND `uid` = %d AND `author-name` != '%s'",
                                dbesc(notags(trim($new_name))),
                                dbesc($r[0]['name']),
                                dbesc($r[0]['url']),
-                               intval($contact['uid'])
+                               intval($contact['uid']),
+                               dbesc(notags(trim($new_name)))
                        );
                }
        }
@@ -2558,6 +2518,11 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                        if(! $item['deleted'])
                                                logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
 
+                                       if($item['object-type'] === ACTIVITY_OBJ_EVENT) {
+                                               logger("Deleting event ".$item['event-id'], LOGGER_DEBUG);
+                                               event_delete($item['event-id']);
+                                       }
+
                                        if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
                                                $xo = parse_xml_string($item['object'],false);
                                                $xt = parse_xml_string($item['target'],false);
@@ -2765,7 +2730,11 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                                $datarray['parent-uri'] = $parent_uri;
                                $datarray['uid'] = $importer['uid'];
                                $datarray['contact-id'] = $contact['id'];
-                               if((activity_match($datarray['verb'],ACTIVITY_LIKE)) || (activity_match($datarray['verb'],ACTIVITY_DISLIKE))) {
+                               if(($datarray['verb'] === ACTIVITY_LIKE)
+                                       || ($datarray['verb'] === ACTIVITY_DISLIKE)
+                                       || ($datarray['verb'] === ACTIVITY_ATTEND)
+                                       || ($datarray['verb'] === ACTIVITY_ATTENDNO)
+                                       || ($datarray['verb'] === ACTIVITY_ATTENDMAYBE)) {
                                        $datarray['type'] = 'activity';
                                        $datarray['gravity'] = GRAVITY_LIKE;
                                        // only one like or dislike per person
@@ -2845,11 +2814,12 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 
                                if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
                                        $ev = bbtoevent($datarray['body']);
-                                       if(x($ev,'desc') && x($ev,'start')) {
+                                       if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
                                                $ev['uid'] = $importer['uid'];
                                                $ev['uri'] = $item_id;
                                                $ev['edited'] = $datarray['edited'];
                                                $ev['private'] = $datarray['private'];
+                                               $ev['guid'] = $datarray['guid'];
 
                                                if(is_array($contact))
                                                        $ev['cid'] = $contact['id'];
@@ -3109,85 +3079,46 @@ function local_delivery($importer,$data) {
 
                logger('local_delivery: Updating photo for ' . $importer['name']);
                require_once("include/Photo.php");
-               $photo_failure = false;
-               $have_photo = false;
-
-               $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
-                       intval($importer['id']),
-                       intval($importer['importer_uid'])
-               );
-               if(count($r)) {
-                       $resource_id = $r[0]['resource-id'];
-                       $have_photo = true;
-               }
-               else {
-                       $resource_id = photo_new_resource();
-               }
-
-               $img_str = fetch_url($photo_url,true);
-               // guess mimetype from headers or filename
-               $type = guess_image_type($photo_url,true);
 
+               $photos = import_profile_photo($photo_url,$importer['importer_uid'],$importer['id']);
 
-               $img = new Photo($img_str, $type);
-               if($img->is_valid()) {
-                       if($have_photo) {
-                               q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `contact-id` = %d AND `uid` = %d",
-                                       dbesc($resource_id),
-                                       intval($importer['id']),
-                                       intval($importer['importer_uid'])
-                               );
-                       }
-
-                       $img->scaleImageSquare(175);
-
-                       $hash = $resource_id;
-                       $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 4);
-
-                       $img->scaleImage(80);
-                       $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 5);
-
-                       $img->scaleImage(48);
-                       $r = $img->store($importer['importer_uid'], $importer['id'], $hash, basename($photo_url), 'Contact Photos', 6);
-
-                       $a = get_app();
-
-                       q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
-                               WHERE `uid` = %d AND `id` = %d",
-                               dbesc(datetime_convert()),
-                               dbesc($a->get_baseurl() . '/photo/' . $hash . '-4.'.$img->getExt()),
-                               dbesc($a->get_baseurl() . '/photo/' . $hash . '-5.'.$img->getExt()),
-                               dbesc($a->get_baseurl() . '/photo/' . $hash . '-6.'.$img->getExt()),
-                               intval($importer['importer_uid']),
-                               intval($importer['id'])
-                       );
-               }
+               q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'
+                       WHERE `uid` = %d AND `id` = %d AND NOT `self`",
+                       dbesc(datetime_convert()),
+                       dbesc($photos[0]),
+                       dbesc($photos[1]),
+                       dbesc($photos[2]),
+                       intval($importer['importer_uid']),
+                       intval($importer['id'])
+               );
        }
 
        if(($name_updated) && (strlen($new_name)) && ($name_updated > $importer['name-date'])) {
                if ($name_updated > $contact_updated)
                        $contact_updated = $name_updated;
 
-               $r = q("select * from contact where uid = %d and id = %d limit 1",
+               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
                        intval($importer['importer_uid']),
                        intval($importer['id'])
                );
 
-               $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d",
+               $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d AND `name` != '%s' AND NOT `self`",
                        dbesc(notags(trim($new_name))),
                        dbesc(datetime_convert()),
                        intval($importer['importer_uid']),
-                       intval($importer['id'])
+                       intval($importer['id']),
+                       dbesc(notags(trim($new_name)))
                );
 
                // do our best to update the name on content items
 
-               if(count($r)) {
-                       q("update item set `author-name` = '%s' where `author-name` = '%s' and `author-link` = '%s' and uid = %d",
+               if(count($r) AND (notags(trim($new_name)) != $r[0]['name'])) {
+                       q("UPDATE `item` SET `author-name` = '%s' WHERE `author-name` = '%s' AND `author-link` = '%s' AND `uid` = %d AND `author-name` != '%s'",
                                dbesc(notags(trim($new_name))),
                                dbesc($r[0]['name']),
                                dbesc($r[0]['url']),
-                               intval($importer['importer_uid'])
+                               intval($importer['importer_uid']),
+                               dbesc(notags(trim($new_name)))
                        );
                }
        }
@@ -3539,6 +3470,11 @@ function local_delivery($importer,$data) {
 
                                        logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
 
+                                       if($item['object-type'] === ACTIVITY_OBJ_EVENT) {
+                                               logger("Deleting event ".$item['event-id'], LOGGER_DEBUG);
+                                               event_delete($item['event-id']);
+                                       }
+
                                        if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
                                                $xo = parse_xml_string($item['object'],false);
                                                $xt = parse_xml_string($item['target'],false);
@@ -3752,7 +3688,11 @@ function local_delivery($importer,$data) {
                                $datarray['owner-avatar'] = $own[0]['thumb'];
                                $datarray['contact-id'] = $importer['id'];
 
-                               if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE)) {
+                               if(($datarray['verb'] === ACTIVITY_LIKE) 
+                                       || ($datarray['verb'] === ACTIVITY_DISLIKE)
+                                       || ($datarray['verb'] === ACTIVITY_ATTEND)
+                                       || ($datarray['verb'] === ACTIVITY_ATTENDNO)
+                                       || ($datarray['verb'] === ACTIVITY_ATTENDMAYBE)) {
                                        $is_like = true;
                                        $datarray['type'] = 'activity';
                                        $datarray['gravity'] = GRAVITY_LIKE;
@@ -3941,7 +3881,11 @@ function local_delivery($importer,$data) {
                                $datarray['parent-uri'] = $parent_uri;
                                $datarray['uid'] = $importer['importer_uid'];
                                $datarray['contact-id'] = $importer['id'];
-                               if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
+                               if(($datarray['verb'] === ACTIVITY_LIKE) 
+                                       || ($datarray['verb'] === ACTIVITY_DISLIKE)
+                                       || ($datarray['verb'] === ACTIVITY_ATTEND)
+                                       || ($datarray['verb'] === ACTIVITY_ATTENDNO)
+                                       || ($datarray['verb'] === ACTIVITY_ATTENDMAYBE)) {
                                        $datarray['type'] = 'activity';
                                        $datarray['gravity'] = GRAVITY_LIKE;
                                        // only one like or dislike per person
@@ -4061,12 +4005,13 @@ function local_delivery($importer,$data) {
 
                        if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
                                $ev = bbtoevent($datarray['body']);
-                               if(x($ev,'desc') && x($ev,'start')) {
+                               if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
                                        $ev['cid'] = $importer['id'];
                                        $ev['uid'] = $importer['uid'];
                                        $ev['uri'] = $item_id;
                                        $ev['edited'] = $datarray['edited'];
                                        $ev['private'] = $datarray['private'];
+                                       $ev['guid'] = $datarray['guid'];
 
                                        $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                                dbesc($item_id),
@@ -4368,7 +4313,7 @@ function subscribe_to_hub($url,$importer,$contact,$hubmode = 'subscribe') {
 }
 
 
-function atom_author($tag,$name,$uri,$h,$w,$photo) {
+function atom_author($tag,$name,$uri,$h,$w,$photo,$geo) {
        $o = '';
        if(! $tag)
                return $o;
@@ -4386,6 +4331,10 @@ function atom_author($tag,$name,$uri,$h,$w,$photo) {
        $o .= "\t".'<link rel="avatar" type="image/jpeg" media:width="' . $w . '" media:height="' . $h . '" href="' . $photo . '" />' . "\r\n";
 
        if ($tag == "author") {
+
+               if($geo)
+                       $o .= '<georss:point>'.xmlify($geo).'</georss:point>'."\r\n";
+
                $r = q("SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
                                `profile`.`name`, `profile`.`pub_keywords`, `profile`.`about`,
                                `profile`.`homepage`,`contact`.`nick` FROM `profile`
@@ -4449,11 +4398,11 @@ function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
        $o = "\r\n\r\n<entry>\r\n";
 
        if(is_array($author))
-               $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb']);
+               $o .= atom_author('author',$author['name'],$author['url'],80,80,$author['thumb'], $item['coord']);
        else
-               $o .= atom_author('author',(($item['author-name']) ? $item['author-name'] : $item['name']),(($item['author-link']) ? $item['author-link'] : $item['url']),80,80,(($item['author-avatar']) ? $item['author-avatar'] : $item['thumb']));
+               $o .= atom_author('author',(($item['author-name']) ? $item['author-name'] : $item['name']),(($item['author-link']) ? $item['author-link'] : $item['url']),80,80,(($item['author-avatar']) ? $item['author-avatar'] : $item['thumb']), $item['coord']);
        if(strlen($item['owner-name']))
-               $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar']);
+               $o .= atom_author('dfrn:owner',$item['owner-name'],$item['owner-link'],80,80,$item['owner-avatar'], $item['coord']);
 
        if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
                $parent = q("SELECT `guid` FROM `item` WHERE `id` = %d", intval($item["parent"]));
@@ -4530,7 +4479,8 @@ function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) {
        //$o .= "\t".'<link rel="self" type="application/atom+xml" href="'.xmlify($a->get_baseurl().'/api/statuses/show/'.$item['id'].'.atom').'"/>'."\r\n";
        //$o .= "\t".'<link rel="edit" type="application/atom+xml" href="'.xmlify($a->get_baseurl().'/api/statuses/show/'.$item['id'].'.atom').'"/>'."\r\n";
 
-       $o .= item_get_attachment($item);
+       // Deactivated since it was meant only for OStatus
+       //$o .= item_get_attachment($item);
 
        $o .= item_getfeedattach($item);