Skip to content
This repository has been archived by the owner on Mar 1, 2018. It is now read-only.

Commit

Permalink
1.8.1 (fix #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
fs654 committed Mar 15, 2016
1 parent f1ddc5c commit 194bc1b
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 11 additions & 0 deletions app/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
44 changes: 44 additions & 0 deletions app/contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<description>A Facebook data syncing tool for your owncloud contacts</description>
<licence>AGPL</licence>
<author>NOIJN</author>
<version>1.8-beta</version>
<version>1.8.1</version>
<namespace>FbSync</namespace>
<category>other</category>
<dependencies>
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
9 changes: 9 additions & 0 deletions controller/contactscontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions controller/facebookcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions css/fbsync.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions js/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div class="sync-contact tooltipped" title="'+contact['name'];
if(contact['photo'] == true) {
var contactdivEnd = '"><img src="'+contact['photourl']+'" height="100" width="100" /></div>';
} else {
var contactdivEnd = '"></div>';
}

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) {

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion templates/status.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
</div>
<div id="warning2" class="intro-info">
If you want to sync your birthdays, <u>switch your facebook to English first</u>!<br />
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! :(<br />
If you don't want to, you can use the secondary method. Unfortunately, the year won't be correct, but the date will.
</div>
<div id="login" class="intro-info">
<b>First!</b> Login here!
Expand Down
14 changes: 10 additions & 4 deletions templates/sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@
title="Will sync profile pictures for people matched with one of your facebook friend">
Sync pictures
</button>
<button id="syncbday" class="tooltipped-bottom syncbutton"
title="Will sync birthdays only for the contacts who doesn't have one set yet">
Sync birthdays
</button>
<span class="tooltipped-bottom joinedbuttons"
title="Will sync birthdays for the contacts who doesn't have one set or if the already
defined date is older than the date we have">
<button id="syncbday" class="syncbutton">
Sync birthdays&nbsp;&nbsp;&nbsp;1
</button>
<button id="syncbdayalt" class="syncbutton">
2
</button>
</span>
<button id="delpictures" class="tooltipped-bottom syncbutton"
title="WARNING! Will remove all the profiles pictures on your selected addressbook(s)">
Remove all pictures
Expand Down

0 comments on commit 194bc1b

Please sign in to comment.