-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Erik Osterman
committed
Feb 11, 2009
1 parent
19b63c4
commit 07a41cb
Showing
6 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Copyright (c) 2009 Erik Osterman (http://www.osterman.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* An implementation of the OpenFriends API | ||
* | ||
* Copyright 2009, Erik Osterman <e@osterman.com> | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @filesource | ||
* @copyright Copyright 2009, Erik Osterman <e@osterman.com> | ||
* @author Erik Osterman <e@osterman.com> | ||
* @link http://www.getopenfriends.com/ OpenFriends | ||
* @package OpenFriends | ||
* @version $Revision$ | ||
* @modifiedby $LastChangedBy$ | ||
* @lastmodified $Date$ | ||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | ||
*/ | ||
|
||
require 'OpenFriends/Peer.class.php'; | ||
require 'OpenFriends/Peer/Consumer.class.php'; | ||
require 'OpenFriends/Peer/Provider.class.php'; | ||
|
||
class OpenFriends | ||
{ | ||
public static function encode(Array $attributes) | ||
{ | ||
return json_encode($attributes); | ||
} | ||
|
||
public static function decode($descriptor) | ||
{ | ||
return json_decode($descriptor, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
/** | ||
* An implementation of the OpenFriends API | ||
* | ||
* Copyright 2009, Erik Osterman <e@osterman.com> | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @filesource | ||
* @copyright Copyright 2009, Erik Osterman <e@osterman.com> | ||
* @author Erik Osterman <e@osterman.com> | ||
* @link http://www.getopenfriends.com/ OpenFriends | ||
* @package OpenFriends | ||
* @version $Revision$ | ||
* @modifiedby $LastChangedBy$ | ||
* @lastmodified $Date$ | ||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | ||
*/ | ||
|
||
class OpenFriends_Peer extends OpenFriends implements Serializable | ||
{ | ||
protected $attributes; | ||
|
||
public function __construct($descriptor, Array $options = Array()) | ||
{ | ||
$this->unserialize($descriptor); | ||
$this->merge($options); | ||
} | ||
|
||
public function __get($attribute) | ||
{ | ||
return $this->getAttribute($attribute); | ||
} | ||
|
||
public function __set($attribute, $value) | ||
{ | ||
return $this->setAttribute($attribute, $value); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->descriptor(); | ||
} | ||
|
||
public function merge(Array $options) | ||
{ | ||
$this->attributes = array_merge($this->attributes, $options); | ||
} | ||
|
||
public function serialize() | ||
{ | ||
return $this->descriptor(); | ||
} | ||
|
||
public function unserialize($descriptor) | ||
{ | ||
$attributes = OpenFriends::decode($descriptor); | ||
if(is_array($attributes)) | ||
$this->attributes = $attributes; | ||
else | ||
$this->attributes = Array(); | ||
} | ||
|
||
public function getAttribute($attribute) | ||
{ | ||
if(isset($this->attributes[$attribute])) | ||
return $this->attributes[$attribute]; | ||
else | ||
return null; | ||
} | ||
|
||
public function setAttribute($attribute, $value) | ||
{ | ||
return $this->attributes[$attribute] = $value; | ||
} | ||
|
||
public function descriptor() | ||
{ | ||
return OpenFriends::encode($this->attributes); | ||
} | ||
|
||
// Sends the parameters to the consumer. Returns the response if the process | ||
// was successful, nil otherwise. | ||
public function performHttpPost($uri, $params, $timeout = 10) | ||
{ | ||
try { | ||
$ch = curl_init(); // Starts the curl handler | ||
curl_setopt($ch, CURLOPT_URL, (string)$uri); // Sets the paypal address for curl | ||
curl_setopt($ch, CURLOPT_FAILONERROR, true); | ||
curl_setopt($ch, CURLOPT_HEADER, false); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Returns result to a variable instead of echoing | ||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects | ||
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // Max redirects | ||
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // Sets a time limit for curl in seconds (do not set too low) | ||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | ||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | ||
curl_setopt($ch, CURLOPT_POST, true); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | ||
curl_setopt($ch, CURLOPT_USERAGENT, get_class($this)); | ||
$result = curl_exec($ch); | ||
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE ); | ||
if($status_code!=200 && $status_code != 302 ) | ||
throw new Exception((curl_error($ch) == ''? $status_code . ' Error' : curl_error($ch)), $status_code); | ||
curl_close($ch); | ||
return $result; | ||
} catch( Exception $e ) | ||
{ | ||
if(isset($ch)) | ||
curl_close($ch); | ||
throw $e; | ||
} | ||
} | ||
|
||
public function send(OpenFriends_Peer_Consumer $consumer, $contacts) | ||
{ | ||
$post = Array('provider' => (string)$this, | ||
'consumer' => (string)$consumer, | ||
'contacts' => $contacts); | ||
return $this->performHttpPost($consumer->url, $post); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* An implementation of the OpenFriends API | ||
* | ||
* Copyright 2009, Erik Osterman <e@osterman.com> | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @filesource | ||
* @copyright Copyright 2009, Erik Osterman <e@osterman.com> | ||
* @author Erik Osterman <e@osterman.com> | ||
* @link http://www.getopenfriends.com/ OpenFriends | ||
* @package OpenFriends | ||
* @version $Revision$ | ||
* @modifiedby $LastChangedBy$ | ||
* @lastmodified $Date$ | ||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | ||
*/ | ||
|
||
class OpenFriends_Peer_Consumer extends OpenFriends_Peer | ||
{ | ||
public function url() | ||
{ | ||
$urls = $this->urls; | ||
if($urls) | ||
return $urls[0]['value']; | ||
else | ||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
/** | ||
* An implementation of the OpenFriends API | ||
* | ||
* Copyright 2009, Erik Osterman <e@osterman.com> | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @filesource | ||
* @copyright Copyright 2009, Erik Osterman <e@osterman.com> | ||
* @author Erik Osterman <e@osterman.com> | ||
* @link http://www.getopenfriends.com/ OpenFriends | ||
* @package OpenFriends | ||
* @version $Revision$ | ||
* @modifiedby $LastChangedBy$ | ||
* @lastmodified $Date$ | ||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License | ||
*/ | ||
|
||
class OpenFriends_Peer_Provider extends OpenFriends_Peer | ||
{ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
//Example: provider application code for posting a Contact list to the Consumer | ||
|
||
require '../OpenFriends.class.php'; | ||
|
||
header('Content-Type: text/html'); | ||
|
||
if(isset($_REQUEST['consumer'])) | ||
$consumer = new OpenFriends_Peer_Consumer($_REQUEST['consumer']); | ||
else | ||
die("Must pass consumer descriptor"); | ||
|
||
$user = new stdClass; | ||
$user->email = 'e@osterman.com'; | ||
$user->friends = Array( | ||
Array('id' => 1, | ||
'displayName' => 'Chris Messina', | ||
'urls' => Array(Array('value' => "http://factoryjoe.com/blog", "type" => "blog")) | ||
), | ||
Array('id' => 2, | ||
'displayName' => 'Joseph Smarr', | ||
'emails' => Array( | ||
Array('value' => 'joseph@plaxo.com', 'type' => 'work', 'primary' => 'true'), | ||
Array('value' => 'jsmarr@gmail.com', 'type' => 'home'), | ||
), | ||
), | ||
); | ||
$provider_attributes = Array('displayName' => 'OpenFriends Provider', | ||
'urls' => Array(Array('value' => 'http://laptop.dev.osterman.com/~e/openfriends/provider.php', 'type' => 'provider')), | ||
); | ||
$provider_descriptor = OpenFriends::encode($provider_attributes); | ||
$provider = new OpenFriends_Peer_Provider($provider_descriptor, Array('token' => $consumer->token)); | ||
|
||
?> | ||
<style> | ||
input, textarea { display: block; } | ||
</style> | ||
<form method="POST" action="<?php echo $consumer->url(); ?>" /> | ||
<label for"provider">Provider:</label> | ||
<textarea rows="10" cols="80" name="provider"><?php echo $provider; ?></textarea> | ||
<label for="consumer">Consumer:</label> | ||
<textarea rows="10" cols="80" name="consumer"><?php echo $consumer; ?></textarea> | ||
<label for="friends">Friends:</label> | ||
<textarea rows="10" cols="80" name="friends"><?php echo OpenFriends::encode($user->friends); ?></textarea> | ||
<input type="submit"> | ||
</form> | ||
|
||
|