Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:osclass/osclass-plugins into dev…
Browse files Browse the repository at this point in the history
…elop
  • Loading branch information
juanramon committed May 27, 2011
2 parents 1e7788a + e0f499b commit e0cd72c
Show file tree
Hide file tree
Showing 11 changed files with 2,542 additions and 0 deletions.
171 changes: 171 additions & 0 deletions facebook/OSCFacebook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php
require_once 'src/facebook.php';
class OSCFacebook {

private static $instance ;
private $facebook;
private $user;
private $loginUrl;
private $logoutUrl;
private $user_profile;

public static function newInstance() {
if(!self::$instance instanceof self) {
self::$instance = new self ;
}
return self::$instance ;
}

public function __construct() {
}

public function init($appId, $secret) {
$this->facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secret,
'cookie' => true
));
$this->logoutUrl = $this->facebook->getLogoutUrl();
$this->loginUrl = $this->facebook->getLoginUrl(array('scope' => 'email'));

$this->user = $this->getUser();
if ($this->user) {
try {
$this->user_profile = $this->facebook->api('/me');
$conn = getConnection();
$user = $conn->osc_dbFetchResult(sprintf("SELECT * FROM %st_facebook_connect WHERE i_facebook_uid = %s", DB_TABLE_PREFIX, $this->user));
// It's linked on our DB!
if($user) {
require_once LIB_PATH . 'osclass/UserActions.php' ;
$uActions = new UserActions(false);
$logged = $uActions->bootstrap_login($user['fk_i_user_id']) ;

if($logged==0) {
osc_add_flash_error_message(_m('The username doesn\'t exist')) ;
} else if($logged==1) {
osc_add_flash_error_message(_m('The user has not been validated yet'));
} else if($logged==2) {
osc_add_flash_error_message(_m('The user has been suspended'));
} else if($logged==3) {

}
} else {
if(isset($this->user_profile['email'])) {
$osc_user = $conn->osc_dbFetchResult(sprintf("SELECT s_name FROM %st_user WHERE s_email = '%s'", DB_TABLE_PREFIX, $this->user_profile['email']));
// Exists on our DB, ask him to link it
if(isset($osc_user['s_name'])) {
// User is logged into her/his OSClass account
if(osc_is_web_user_logged_in()) {
$user = User::newInstance()->findByPrimaryKey(osc_logged_user_id());
if($user) {
$conn->osc_dbExec(sprintf("REPLACE INTO `%st_facebook_connect` SET `fk_i_user_id` = %d, `i_facebook_uid` = '%s'", DB_TABLE_PREFIX, osc_logged_user_id(), $this->user_profile['id']));
} else {
osc_add_flash_ok_message(__('Hey! We just discovered some user with your same email address. Log into your account to link it to Facebook.', 'facebook'));
}
} else {
osc_add_flash_ok_message(__('Hey! We just discovered some user with your same email address. Log into your account to link it to Facebook.', 'facebook'));
}
// Auto-register him
} else {
$this->register_user($this->user_profile);
}
} else {
osc_add_flash_error_message(__('Some error occured trying to connect with Facebook.','facebook'));
header("Location: " . $this->logoutUrl);
exit();
}
}
} catch (FacebookApiException $e) {
//error_log($e);
$this->user = null;
}
}
return $this->facebook;
}

public function getFacebook() {
return $this->facebook;
}

public function getUser() {
if($this->user==null) {
$this->user = $this->facebook->getUser();
}
return $this->user;
}

public function logoutUrl() {
return $this->logoutUrl;
}

public function loginUrl() {
return $this->loginUrl;
}

public function profile() {
if($this->user_profile==null) {
$this->user_profile = $this->facebook->api('/me');
}
return $this->user_profile;
}

private function register_user($user) {

$input['s_name'] = $user['name'];
$input['s_email'] = $user['email'];
$input['s_password'] = sha1(osc_genRandomPassword());
$input['dt_reg_date'] = DB_FUNC_NOW;

$code = osc_genRandomPassword();
$input['s_secret'] = $code;
$manager = User::newInstance();
$email_taken = $manager->findByEmail($input['s_email']) ;
if($email_taken == null) {
$manager->insert($input) ;
$conn = getConnection();
$userId = $manager->getConnection()->get_last_id() ;
$conn->osc_dbExec(sprintf("REPLACE INTO `%st_facebook_connect` SET `fk_i_user_id` = %d, `i_facebook_uid` = '%s'", DB_TABLE_PREFIX, $userId, $user['id']));

osc_run_hook('user_register_completed') ;

if( osc_user_validation_enabled()) {
$user = $manager->findByPrimaryKey($userId) ;
$mPages = new Page() ;
$locale = osc_current_user_locale() ;
$aPage = $mPages->findByInternalName('email_user_validation') ;
$content = array() ;
if(isset($aPage['locale'][$locale]['s_title'])) {
$content = $aPage['locale'][$locale] ;
} else {
$content = current($aPage['locale']) ;
}
if (!is_null($content)) {
$validation_url = osc_user_activate_url($user['pk_i_id'], $input['s_secret']);
$words = array();
$words[] = array('{USER_NAME}', '{USER_EMAIL}', '{WEB_URL}', '{VALIDATION_LINK}', '{VALIDATION_URL}') ;
$words[] = array($user['s_name'], $user['s_email'], '<a href="'.osc_base_url().'" >'.osc_base_url().'</a>', '<a href="' . $validation_url . '" >' . $validation_url . '</a>', '<a href="' . $validation_url . '" >' . $validation_url . '</a>') ;
$title = osc_mailBeauty($content['s_title'], $words) ;
$body = osc_mailBeauty($content['s_text'], $words) ;

$emailParams = array('subject' => $title
,'to' => Params::getParam('s_email')
,'to_name' => Params::getParam('s_name')
,'body' => $body
,'alt_body' => $body
) ;
osc_sendMail($emailParams) ;
}
osc_add_flash_ok_message(sprintf(__("An automatic account for %s has been created. You'll receive an email to confirm.", 'facebook'), osc_page_title()));
} else {
$manager->update(
array('b_active' => '1')
,array('pk_i_id' => $userId)
);
osc_add_flash_ok_message(sprintf(__("An automatic account for %s has been created. You're ready to go.", 'facebook'), osc_page_title()));
}
}
}

}

?>
28 changes: 28 additions & 0 deletions facebook/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Facebook PHP SDK (v.3.0.0)
==========================

The new PHP SDK (v3.0.0) is a major upgrade to the older one (v2.2.x):

- Uses OAuth authentication flows instead of our legacy authentication flow
- Consists of two classes. The first (class BaseFacebook) maintains the core of the upgrade, and the second one (class Facebook) is a small subclass that uses PHP sessions to store the user id and access token.

If you’re currently using the PHP SDK (v2.2.x) for authentication, you will recall that the login code looked like this:

$facebook = new Facebook(…);
$session = $facebook->getSession();
if ($session) {
// proceed knowing you have a valid user session
} else {
// proceed knowing you require user login and/or authentication
}

The login code is now:

$facebook = new Facebook(…);
$user = $facebook->getUser();
if ($user) {
// proceed knowing you have a logged in user who's authenticated
} else {
// proceed knowing you require user login and/or authentication
}

71 changes: 71 additions & 0 deletions facebook/conf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* OSCLass – software for creating and publishing online classified
* advertising platforms
*
* Copyright (C) 2010 OSCLASS
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
?>
<?php

if(Params::getParam('plugin_action')=='done') {
osc_set_preference('fbc_appId', Params::getParam('fbc_appId'), 'facebook_connect', 'STRING');
osc_set_preference('fbc_secret', Params::getParam('fbc_secret'), 'facebook_connect', 'STRING');
osc_reset_preferences();
}

?>
<div id="settings_form" style="border: 1px solid #ccc; background: #eee; ">
<div style="padding: 20px;">
<div style="float: left; width: 50%;">
<fieldset>
<legend><?php _e('Jobs Options', 'jobs_attributes'); ?></legend>
<form name="jobs_form" id="jobs_form" action="<?php echo osc_admin_base_url(true);?>" method="GET" enctype="multipart/form-data" >
<input type="hidden" name="page" value="plugins" />
<input type="hidden" name="action" value="renderplugin" />
<input type="hidden" name="file" value="facebook/conf.php" />
<input type="hidden" name="plugin_action" value="done" />
<?php _e("Please enter your Facebook appId and secret*:", 'facebook'); ?><br />
<label>appId:</label> <input type="text" name="fbc_appId" id="fbc_appId" value="<?php echo osc_get_preference('fbc_appId','facebook_connect'); ?>" maxlength="100" size="60" /><br />
<label>secret:</label> <input type="text" name="fbc_secret" id="fbc_secret" value="<?php echo osc_get_preference('fbc_secret', 'facebook_connect'); ?>" maxlength="100" size="60" /><br />

<button type="submit"><?php echo __('Update', 'facebook');?></button>
</form>
</fieldset>
</div>
<div style="float: left; width: 50%;">
<fieldset>
<legend><?php _e("Facebook Connect Help", 'facebook');?></legend>

<h3><?php _e("What is Facebook Connect Plugin?");?></h3>
<?php _e("Facebook Connect plugin allows your users to log into your webpage with their Facebookaccounts.", 'facebook');?>
<br/>
<br/>
<h3><?php _e("Using Facebook login", 'facebook');?></h3>
<?php echo __('You can freely obtain an appId and secret key (needed to use Facebook login on your website) after signing up on this URL:','facebook'); ?> <a rel="nofollow" target="_blank" href="http://www.facebook.com/developers/createapp.php">http://www.facebook.com/developers/createapp.php</a><br />
<?php _e("In order to use Facebook login in your website you should include at least one facebook button for login (and logout). To do that place the following code where you want it to appear:",'facebook');?><br/>
<pre>
&lt;?php fbc_button(); ?&gt;
</pre>
<br />
<div style="font-size: small;"><strong>*</strong> <?php echo __('You can freely obtain an appId and secret key after signing up on this URL:'); ?> <a rel="nofollow" target="_blank" href="http://www.facebook.com/developers/createapp.php">http://www.facebook.com/developers/createapp.php</a>.</div>
<br/>

</fieldset>
</div>
<div style="clear: both;"></div>
</div>
</div>
102 changes: 102 additions & 0 deletions facebook/examples/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '191721697511167',
'secret' => '2a2630e9a2a5b604d42ded7bb33edb31',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}

// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>

<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>

<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>

<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>

<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Loading

0 comments on commit e0cd72c

Please sign in to comment.