From 194bc1b6601f9ec7d8860ec283551a0e89fa7ac4 Mon Sep 17 00:00:00 2001 From: fs654 Date: Tue, 15 Mar 2016 07:44:58 +0100 Subject: [PATCH] 1.8.1 (fix #23) --- CHANGELOG.md | 3 ++ app/contact.php | 11 +++++++ app/contacts.php | 44 +++++++++++++++++++++++++++ appinfo/info.xml | 2 +- appinfo/routes.php | 1 + controller/contactscontroller.php | 9 ++++++ controller/facebookcontroller.php | 41 ++++++++++++++++++++++++++ css/fbsync.css | 14 +++++++++ js/sync.js | 49 +++++++++++++++++++++++++++++++ templates/status.php | 3 +- templates/sync.php | 14 ++++++--- 11 files changed, 185 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10fa7b1..7133a85 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +1.8.1: + - Added secondary way to get birthdays (no langage change but no correct year) + 1.8: - OC9 compatibility & OC8 depreciation - Fix unwanted error in log (fix #21) diff --git a/app/contact.php b/app/contact.php index 03efdaa..aa036bb 100755 --- a/app/contact.php +++ b/app/contact.php @@ -233,6 +233,17 @@ private function updateFBID($fbid=false){ return $this->save(); } + /** + * set Birthday + */ + public function updateorsetBirthday($timestamp){ + if(isset($this->vcard->BDAY)) { + unset($this->vcard->BDAY); + } + $this->vcard->add('BDAY', $timestamp); + return $this->save(); + } + /** * Set FBID */ diff --git a/app/contacts.php b/app/contacts.php index 7efc892..849c0b4 100755 --- a/app/contacts.php +++ b/app/contacts.php @@ -237,6 +237,50 @@ public function perfectMatch() { return $edited; } + /** + * Update birthdays with a secondary system + * @NoAdminRequired + */ + public function updateBirthdays() { + $contacts = $this->getList(); + $birthdays = $this->fbController->getBirthdays(); + + $syncedContacts = array(); + + foreach ($contacts as $contact) { + + if(isset($contact->vcard->FBID)) { + $FBID = (string)$contact->vcard->FBID; + + if(isset($birthdays[$FBID])) { + + $bdate = $birthdays[$FBID]; + $BDAY = strtotime($contact->vcard->BDAY); + + // Check if birthday exist or if lower than the value we have + // if greater, then we have a better value + if($BDAY > $bdate || is_null($contact->vcard->BDAY)) { + $birthday = date('Y-m-d', $bdate); + $contact->updateorsetBirthday($birthday); + $syncedContacts[] = Array( + "error" => false, + "id" => $contact->id, + "name" => $contact->getName(), + "name" => $contact->getName(), + "addressbook" => $contact->addressbook, + "photo" => isset($contact->vcard->PHOTO), + "photourl" => $contact->getPhoto(100), + "birthday" => $birthday + ); + } + } + } + } + + return $syncedContacts; + + } + /** * Match approx name using Jaro-Winkler algorithm * @NoAdminRequired diff --git a/appinfo/info.xml b/appinfo/info.xml index 6665503..2b040dd 100755 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ A Facebook data syncing tool for your owncloud contacts AGPL NOIJN - 1.8-beta + 1.8.1 FbSync other diff --git a/appinfo/routes.php b/appinfo/routes.php index b96bd06..dcbae43 100755 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -26,6 +26,7 @@ ['name' => 'contacts#setPhoto', 'url' => '/setphoto/{id}', 'verb' => 'GET'], ['name' => 'contacts#setBirthday', 'url' => '/setbday/{id}', 'verb' => 'GET'], ['name' => 'contacts#getFbContacts', 'url' => '/FBcontacts', 'verb' => 'GET'], + ['name' => 'contacts#setBirthdayAlt', 'url' => '/setBirthdayAlt', 'verb' => 'GET'], ['name' => 'contacts#getPhoto', 'url' => '/getphoto/{id}', 'verb' => 'GET'], ['name' => 'contacts#getPhoto', 'url' => '/getphoto/{id}/{size}', 'verb' => 'GET'], ['name' => 'contacts#deletePhotos', 'url' => '/contacts/delphotos', 'verb' => 'GET'], diff --git a/controller/contactscontroller.php b/controller/contactscontroller.php index e939705..8d53e78 100755 --- a/controller/contactscontroller.php +++ b/controller/contactscontroller.php @@ -88,6 +88,15 @@ public function setBirthday($id){ return new JSONResponse($this->app->setBirthday($id)); } + /** + * @NoAdminRequired + * @NoCSRFRequired + * @return JSONResponse + */ + public function setBirthdayAlt(){ + return new JSONResponse($this->app->updateBirthdays()); + } + /** * @NoAdminRequired * @NoCSRFRequired diff --git a/controller/facebookcontroller.php b/controller/facebookcontroller.php index 19930a1..4ab5a64 100755 --- a/controller/facebookcontroller.php +++ b/controller/facebookcontroller.php @@ -15,6 +15,7 @@ use OCP\AppFramework\Controller; use OCP\IUser; use OCP\ICache; +use Sabre\VObject; use OCA\FbSync\AppInfo\Application as App; require("simple_html_dom.php"); @@ -317,6 +318,46 @@ public function getBirthday($fbid) { return $birthday->innertext; } + /** + * Get birthday if the 1st method don't work + * This function use the ical from facebook. + * - Pros: it's pretty simple and you don't need any permission + * - Cons: you don't have the correct year + * @var integer The Facebook ID + * @return string Birthday date to Y-m-d format + */ + public function getBirthdays() { + + if(!$this->islogged()) { + return false; + } + + // Get events page + $html = $this->dorequest('https://www.facebook.com/events/birthdays')[1]; + if (is_null($html) || empty($html)) { + return false; + } + + // Parse ical url + $re = "/webcal:\\/\\/(www.facebook.com\\/ical\\/b.php\\?uid\\=[a-z0-9?\\/.=;&_]{20,50})/i"; + preg_match_all($re, $html, $matches); + $url = 'https://'.htmlspecialchars_decode($matches[1][0]); + + // Parse ical + $calICS = file_get_contents($url); + $vcalendar = VObject\Reader::read($calICS); + $birthdays = array(); + + foreach($vcalendar->VEVENT as $calendar) { + $fbid = explode('@', $calendar->UID)[0]; + $fbid = substr($fbid,1); + $dateTime = $calendar->DTSTART->getDateTime(); + $birthdays[$fbid] = $dateTime->getTimestamp(); + }; + + return $birthdays; + } + /** * Check if logged to facebook * @NoAdminRequired diff --git a/css/fbsync.css b/css/fbsync.css index 1a0cfd9..131f887 100755 --- a/css/fbsync.css +++ b/css/fbsync.css @@ -149,6 +149,20 @@ justify-content: space-between; width: auto; } +#controls .joinedbuttons { + display: inline-block; +} +#controls .joinedbuttons > button:first-child { + border-radius: 3px 0 0 3px; + margin-right: 0; + border-right: 1px solid rgba(0,0,0,0.1); + display: block; + float: left; +} +#controls .joinedbuttons > button:last-child { + border-radius: 0 3px 3px 0; + margin-left: 0; +} #controls .button, #controls button { height: auto; diff --git a/js/sync.js b/js/sync.js index 5254f3c..5ad6dec 100755 --- a/js/sync.js +++ b/js/sync.js @@ -135,6 +135,43 @@ function syncBirthdays() { }) } +function syncBirthdaysAlt() { + + $('#loader').fadeIn(); + + var synced = 0; + $.get(OC.generateUrl('apps/fbsync/setBirthdayAlt')) + .done(function(response) { + if(response.length>0) { + + response.forEach(function(contact, index, array){ + var contactdivStart = '
'; + } else { + var contactdivEnd = '">'; + } + + synced++; + $('#sync-success > .sync-results').append(contactdivStart+': '+contact['birthday']+contactdivEnd); + + // Status + $('.tooltipped').tipsy(); + isDoneSyncing(synced, 0, 0, response.length, "#syncbdayalt"); + }) + + } else { + + } + + }).fail(function() { + + error++; + isDoneSyncing(0, 1, 0, 1, "#syncbdayalt"); + + }); +} + (function ($, OC) { @@ -179,6 +216,18 @@ function syncBirthdays() { $('.sync-results').empty(); syncBirthdays(); }) + $("#syncbdayalt").click(function() { + if (confirm("This secondary button uses the facebook calendar to get the desired data. It's useful if you don't want to change your facebook langage to get the first method working or if the 1 button is failing. Please remember that the saved year won't be correct.")) { + // Fix for tooltip on disabled buttons + $('.tooltip').fadeOut(); + // Save and set new text + $("#syncbdayalt").data('text', $("#syncbdayalt").text()).text('Loading...').addClass('loading'); + $(".syncbutton").prop('disabled',true); + // Empty previous sync data + $('.sync-results').empty(); + syncBirthdaysAlt(); + } + }) $("#delpictures").click(function() { if (confirm("Are you sure ?!")) { // Fix for tooltip on disabled buttons diff --git a/templates/status.php b/templates/status.php index 658139c..d32b0e5 100755 --- a/templates/status.php +++ b/templates/status.php @@ -37,7 +37,8 @@
If you want to sync your birthdays, switch your facebook to English first!
- It's a limitation I can't avoid because we don't use their API! :( + It's a limitation I can't avoid because we don't use their API! :(
+ If you don't want to, you can use the secondary method. Unfortunately, the year won't be correct, but the date will.
First! Login here! diff --git a/templates/sync.php b/templates/sync.php index 3c16b8d..b06a47f 100755 --- a/templates/sync.php +++ b/templates/sync.php @@ -29,10 +29,16 @@ title="Will sync profile pictures for people matched with one of your facebook friend"> Sync pictures - + + + +