]> git.mxchange.org Git - friendica.git/commitdiff
de-duplicate photo importation logic
authorMike Macgirvin <mike@macgirvin.com>
Tue, 26 Oct 2010 21:50:38 +0000 (14:50 -0700)
committerMike Macgirvin <mike@macgirvin.com>
Tue, 26 Oct 2010 21:50:38 +0000 (14:50 -0700)
include/Photo.php
mod/dfrn_confirm.php
mod/follow.php

index e418cde4621231048851053a22441ee93c76b773..ca9ae0703c3162e1d0d853d4131b9b5a284fe0e7 100644 (file)
@@ -208,3 +208,44 @@ class Photo {
 }}
 
 
+function import_profile_photo($photo,$uid,$cid) {
+
+       $a = get_app();
+
+       $photo_failure = false;
+
+       $filename = basename($photo);
+       $img_str = fetch_url($photo,true);
+       $img = new Photo($img_str);
+       if($img->is_valid()) {
+
+               $img->scaleImageSquare(175);
+                                       
+               $hash = photo_new_resource();
+
+               $r = $img->store($uid, $cid, $hash, $filename, t('Contact Photos'), 4 );
+
+               if($r === false)
+                       $photo_failure = true;
+
+               $img->scaleImage(80);
+
+               $r = $img->store($uid, $cid, $hash, $filename, t('Contact Photos'), 5 );
+
+               if($r === false)
+                       $photo_failure = true;
+
+               $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
+               $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
+       }
+       else
+               $photo_failure = true;
+
+       if($photo_failure) {
+               $photo = $a->get_baseurl() . '/images/default-profile.jpg';
+               $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
+       }
+
+       return(array($photo,$thumb));
+
+}
index cc1edea229baee150eb6dca2aa82c2b5b6491837..705f74f1355bb77c738b7ab085dece8c903f8b24 100644 (file)
@@ -229,40 +229,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
 
                require_once("Photo.php");
 
-               $photo_failure = false;
-
-               $filename = basename($contact['photo']);
-               $img_str = fetch_url($contact['photo'],true);
-               $img = new Photo($img_str);
-               if($img->is_valid()) {
-
-                       $img->scaleImageSquare(175);
-                                       
-                       $hash = photo_new_resource();
-
-                       $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 4 );
-
-                       if($r === false)
-                               $photo_failure = true;
-
-                       $img->scaleImage(80);
-
-                       $r = $img->store($uid, $contact_id, $hash, $filename, t('Contact Photos'), 5 );
-
-                       if($r === false)
-                               $photo_failure = true;
-
-                       $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
-                       $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
-               }
-               else
-                       $photo_failure = true;
-
-               if($photo_failure) {
-                       $photo = $a->get_baseurl() . '/images/default-profile.jpg';
-                       $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
-               }
-
+               $photos = import_profile_photo($contact['photo'],$uid,$contact_id);
 
                if($contact['network'] === 'dfrn') {
 
@@ -281,8 +248,8 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                                `duplex` = %d,
                                `network` = 'dfrn' WHERE `id` = %d LIMIT 1
                        ",
-                               dbesc($photo),
-                               dbesc($thumb),
+                               dbesc($photos[0]),
+                               dbesc($photos[1]),
                                intval($new_relation),
                                dbesc(datetime_convert()),
                                dbesc(datetime_convert()),
@@ -324,8 +291,8 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                                `pending` = 0
                                WHERE `id` = %d LIMIT 1
                        ",
-                               dbesc($photo),
-                               dbesc($thumb),
+                               dbesc($photos[0]),
+                               dbesc($photos[1]),
                                dbesc(datetime_convert()),
                                dbesc(datetime_convert()),
                                dbesc(datetime_convert()),
@@ -463,47 +430,19 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
 
                // We're good but now we have to scrape the profile photo and send notifications.
 
-               require_once("Photo.php");
 
-               $photo_failure = false;
 
                $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
                        intval($dfrn_record));
-               if(count($r)) {
-
-                       $filename = basename($r[0]['photo']);
-                       $img_str = fetch_url($r[0]['photo'],true);
-                       $img = new Photo($img_str);
-                       if($img->is_valid()) {
-
-                               $img->scaleImageSquare(175);
-                                       
-                               $hash = photo_new_resource();
-
-                               $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 4);
-
-                               if($r === false)
-                                       $photo_failure = true;
-                                       
-                               $img->scaleImage(80);
-                               $r = $img->store($local_uid, $dfrn_record, $hash, $filename, t('Contact Photos') , 5);
-
-                               if($r === false)
-                                       $photo_failure = true;
 
-                               $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
-                               $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';      
-                       }
-                       else
-                               $photo_failure = true;
-               }
+               if(count($r))
+                       $photo = $r[0]['photo'];
                else
-                       $photo_failure = true;
-
-               if($photo_failure) {
                        $photo = $a->get_baseurl() . '/images/default-profile.jpg';
-                       $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
-               }
+                               
+               require_once("Photo.php");
+
+               $photos = import_profile_photo($photo,$local_uid,$dfrn_record);
 
                $new_relation = REL_FAN;
                if(($relation == REL_VIP) || ($duplex))
@@ -521,8 +460,8 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        `duplex` = %d, 
                        `network` = 'dfrn' WHERE `id` = %d LIMIT 1
                ",
-                       dbesc($photo),
-                       dbesc($thumb),
+                       dbesc($photos[0]),
+                       dbesc($photos[1]),
                        intval($new_relation),
                        dbesc(datetime_convert()),
                        dbesc(datetime_convert()),
index 7ac9a50c315ca2c4fee0ef0203ba49b68132f2c9..29c5f4230686344181b6ae12f0177678e4a4cfa1 100644 (file)
@@ -70,15 +70,17 @@ function follow_post(&$a) {
                $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
 
        // check if we already have a contact
+       // the poll url is more reliable than the profile url, as we may have
+       // indirect links or webfinger links
 
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
                intval(local_user()),
                dbesc($poll)
        );                      
-       if($r) {
+       if(count($r)) {
                // update contact
                if($r[0]['rel'] == REL_VIP) {
-                       q("UPDATE `contact` SET `rel` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
+                       q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
                                intval(REL_BUD),
                                intval($r[0]['id']),
                                intval(local_user())
@@ -117,39 +119,7 @@ function follow_post(&$a) {
 
        require_once("Photo.php");
 
-       $photo_failure = false;
-
-       $filename = basename($vcard['photo']);
-       $img_str = fetch_url($vcard['photo'],true);
-       $img = new Photo($img_str);
-       if($img->is_valid()) {
-
-               $img->scaleImageSquare(175);
-                                       
-               $hash = photo_new_resource();
-
-               $r = $img->store(local_user(), $contact_id, $hash, $filename, t('Contact Photos'), 4 );
-
-               if($r === false)
-                       $photo_failure = true;
-
-               $img->scaleImage(80);
-
-               $r = $img->store(local_user(), $contact_id, $hash, $filename, t('Contact Photos'), 5 );
-
-               if($r === false)
-                       $photo_failure = true;
-
-               $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg';
-               $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg';
-       }
-       else
-               $photo_failure = true;
-
-       if($photo_failure) {
-               $photo = $a->get_baseurl() . '/images/default-profile.jpg';
-               $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg';
-       }
+       $photos = import_profile_photo($vcard['photo'],local_user(),$contact_id);
 
        $r = q("UPDATE `contact` SET `photo` = '%s', 
                        `thumb` = '%s', 
@@ -158,8 +128,8 @@ function follow_post(&$a) {
                        `avatar-date` = '%s'
                        WHERE `id` = %d LIMIT 1
                ",
-                       dbesc($photo),
-                       dbesc($thumb),
+                       dbesc($photos[0]),
+                       dbesc($photos[1]),
                        dbesc(datetime_convert()),
                        dbesc(datetime_convert()),
                        dbesc(datetime_convert()),