Created
December 10, 2013 19:28
-
-
Save andrebian/7896623 to your computer and use it in GitHub Desktop.
Exemplo de post na timeline do Facebook no CakePHP
This file contains hidden or 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
<?php | |
App::uses('AppController', 'Controller'); | |
App::import('Vendor', 'Facebook', array('file' => 'facebook-php-sdk/src/facebook.php')); | |
App::import('Network/Http', 'HttpSocket'); | |
/** | |
* Users Controller | |
* | |
* @property User $User | |
*/ | |
class UsersController extends AppController | |
{ | |
public $uses = array('User', 'UsersSkill', 'Group', 'Materia', 'MateriasUser', 'Node'); | |
public $components = array('SendEmail'); | |
public function facebookLogin() | |
{ | |
$this->view = 'login'; | |
$facebook = new Facebook(array( | |
'appId' => Configure::read('facebookAPI.appId'), | |
'secret' => Configure::read('facebookAPI.secret') | |
)); | |
$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. | |
$userProfile = $facebook->api('/me'); | |
$this->Session->write('facebookLogin', true); | |
if ( $databaseUser = $this->User->findByFacebookId($user) ) { | |
if ($this->Auth->login( $databaseUser['User'] )) { | |
$reactivation = ''; | |
if ( $this->Auth->user('status') == 'inactive' ) { | |
$reactivation = 'Seu cadastro estava inativo até o momento. Ao realizar este login ativamos o mesmo novamente.'; | |
} | |
$this->Session->setFlash('Login realizado com sucesso! ' . $reactivation, 'default', array('class' => 'alert alert-success')); | |
$this->redirect('/minha-conta'); | |
} | |
} else { | |
$userData = $userProfile; | |
if ( $imagem = $facebook->api('/'.$user.'?fields=name,picture') ) { | |
$userData['photo'] = $imagem['picture']['data']['url']; | |
} | |
if ( $this->facebookRegister( $userData ) ) { | |
// Postando na linha do tempo | |
$message = ' | |
Estou fazendo parte do Me Ajuda Professor, venha você também - http://meajudaprofessor.com.br ;) | |
'; | |
$HttpSocket = new HttpSocket(); | |
$results = $HttpSocket->post('https://graph.facebook.com/' . $user . '/feed', array('access_token' => $facebook->getAccessToken(), | |
'message' => $message)); | |
$this->redirect('/meu-perfil'); | |
} | |
} | |
} 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 { | |
$statusUrl = $facebook->getLoginStatusUrl(); | |
$loginUrl = $facebook->getLoginUrl(array('scope' => array('publish_stream', 'user_friends'))); | |
$this->redirect($loginUrl); | |
} | |
} | |
/** | |
* | |
* @param array $facebookUser | |
* @throws NotFoundException | |
*/ | |
public function facebookRegister( $facebookUser = array() ) | |
{ | |
if ( empty($facebookUser) ) { | |
throw new NotFoundException('Sua sessão do Facebook expirou'); | |
} | |
$address = array(0 => 'ND', 1 => 'ND'); | |
if ( isset($facebookUser['location']) && !empty($facebookUser['location']['name']) ) { | |
$address = explode(', ', $facebookUser['location']['name']); | |
} | |
$user = array('User' => array( | |
'facebook_id' => $facebookUser['id'], | |
'facebook_login' => $facebookUser['username'], | |
'name' => $facebookUser['first_name'], | |
'surname' => $facebookUser['last_name'], | |
'email' => isset($facebookUser['email']) ? $facebookUser['email'] : '', | |
'facebook_profile' => $facebookUser['link'], | |
'addr_city' => $address[0], | |
'addr_country' => isset($address[1]) ? $address[1] : 'BR', | |
'avatar' => 'avatar.jpg' | |
) | |
); | |
if ( $this->User->save( $user ) ) { | |
$user = $this->User->read(null, $this->User->id); | |
if ( isset($facebookUser['photo']) && !empty($facebookUser['photo']) ) { | |
if ( !$this->__saveImageFromFacebook( $facebookUser['photo'], $user['User']['id'] ) ) { | |
$this->User->saveField('avatar', ''); | |
} | |
} | |
if ( $this->Auth->login($user['User']) ) { | |
$this->Session->setFlash( | |
'<b>Seu cadastro realizado com sucesso mas ainda não sabemos se você é um aluno ou professor, | |
por favor informe-nos isto agora</b>', | |
'default', array('class' => 'alert alert-info')); | |
} | |
} else { | |
$this->Session->setFlash('Não foi possÃvel realizar seu cadastro', 'default', array('class' => 'alert alert-error')); | |
$this->redirect('/login'); | |
} | |
return true; | |
} | |
/** | |
* | |
* @param type $url | |
* @return string | |
*/ | |
private function __saveImageFromFacebook( $url, $userId ) | |
{ | |
$dir = 'files/users/'.$userId; | |
if($imgOrigem = imagecreatefromjpeg($url)) { | |
$x = imagesx ($imgOrigem); | |
$y = imagesy ($imgOrigem); | |
$imgFinal = imagecreatetruecolor ($x, $y); | |
imagecopyresized($imgFinal, $imgOrigem, 0, 0, 0, 0, $x, $y, $x, $y); | |
if( !is_dir($dir) ) { | |
mkdir($dir); | |
} | |
imagejpeg ($imgFinal, $dir.'/avatar.jpg'); | |
imagedestroy($imgOrigem); | |
imagedestroy($imgFinal); | |
return 'avatar.jpg'; | |
} | |
return ''; | |
} | |
/** | |
* | |
*/ | |
public function logout($noRedirect = false) | |
{ | |
if (class_exists('Facebook') ) { | |
$facebook = new Facebook(array( | |
'appId' => Configure::read('facebookAPI.appId'), | |
'secret' => Configure::read('facebookAPI.secret') | |
)); | |
$facebook->destroySession(); | |
} | |
$this->Auth->logout(); | |
if (!$noRedirect) { | |
$this->redirect('/'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment