Created
September 12, 2016 15:39
-
-
Save robertholf/76b0127b4781f4cd7e87e6b6c3c47616 to your computer and use it in GitHub Desktop.
Laravel 3 (SurroundsMe)
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 | |
class SmeSocial_Gather_Task { | |
protected $provider = null; | |
protected $token = null; | |
public function run ($arguments) | |
{ | |
$time_start = microtime(true); | |
$this->_log("Social Snapshot started."); | |
if(empty($arguments) or (count($arguments) < 3)) | |
{ | |
$this->_log("Error: Incorrect Arguments."); | |
return false; | |
} | |
$provider = array_shift($arguments); | |
$brand = array_shift($arguments); | |
$action = array_shift($arguments); | |
$this->_log("Grabbing token for: ". $provider); | |
$token = BrandSocialAuth::where('brand_id', '=', $brand)->where('provider', '=', $provider)->first(); | |
if(empty($token)) | |
{ | |
$this->_log("Error: empty token."); | |
return false; | |
} | |
if(in_array($provider, array('facebook', 'foursquare', 'google'))) | |
{ | |
$this->_log("Grabbing page token for: ". $provider); | |
$page_token = BrandSocialAuthMeta::where('brand_id', '=', $brand)->where('provider', '=', $provider)->first(); | |
if(!empty($page_token)) | |
{ | |
$token = $page_token; | |
} | |
} | |
$this->_log("Checking for refresh time."); | |
if($token->expires <= time()) | |
{ | |
$this->_log("Refreshing Token."); | |
$refresh = SmeSocial::make($provider)->refresh($token); | |
if(!empty($refresh)) | |
{ | |
$token->fill($refresh); | |
$token->save(); | |
} | |
} | |
$this->_log("Retrieving data."); | |
if($provider == 'analytics') | |
{ | |
$account = $this->get_analytics($brand); | |
if(!empty($account)) | |
{ | |
$data = SmeSocial::make($provider)->import_data($token, $account); | |
} | |
else | |
{ | |
$data['status'] = 'error'; | |
} | |
} | |
else | |
{ | |
$data = SmeSocial::make($provider)->import_data($token); | |
} | |
$status = array(); | |
$this->_log($data['status']); | |
if($data['status'] == 'success') | |
{ | |
$this->_log("Saving gathered data."); | |
if($provider == 'analytics') | |
{ | |
$status['status'] = $this->put_data($brand, $provider, array('action' => $action, 'data' => $data['data'], 'account' => $account)) ? 'success' : 'error'; | |
} | |
else | |
{ | |
$status['status'] = $this->put_data($brand, $provider, array('action' => $action, 'data' => $data['data'])) ? 'success' : 'error'; | |
} | |
} | |
else | |
{ | |
$status = $data; | |
} | |
$this->_log("Social Snapshot completed: " . (microtime(true) - $time_start) . "s"); | |
return $status; | |
} | |
private function get_analytics($brand) | |
{ | |
$metadata = BrandSocialAuthMetaKVS::where('provider', '=', 'analytics') | |
->where('brand_id', '=', (int) $brand) | |
->get(array('key as meta_key', 'value as meta_value')); | |
$account = array(); | |
foreach($metadata as $key => $value) | |
{ | |
if(!in_array($value->meta_key, array('account', 'property', 'profile'))) | |
{ | |
continue; | |
} | |
$account[$value->meta_key] = $value->meta_value; | |
} | |
return $account; | |
} | |
private function put_data($brand, $provider, $data) | |
{ | |
switch($provider) | |
{ | |
case "twitter": | |
$status = $this->put_twitter($brand, $data); | |
break; | |
case "facebook": | |
$status = $this->put_facebook($brand, $data); | |
break; | |
case "foursquare": | |
$status = $this->put_foursquare($brand, $data); | |
break; | |
case "instagram": | |
$status = $this->put_instagram($brand, $data); | |
break; | |
case "linkedin": | |
$status = $this->put_linkedin($brand, $data); | |
break; | |
case "google": | |
$status = $this->put_google($brand, $data); | |
break; | |
case "klout": | |
$status = $this->put_klout($brand, $data); | |
break; | |
case 'analytics': | |
$status = $this->put_analytics($brand, $data); | |
break; | |
default: | |
$status = true; | |
break; | |
} | |
return $status; | |
} | |
private function put_twitter($brand, $container) | |
{ | |
$data = $container['data']; | |
$status = DB::table('metrics_social_twitter')->insert_get_id(array( | |
'brand_id' => $brand, | |
'action' => $container['action'], | |
'followers_count' => $data->followers_count, | |
'friends_count' => $data->friends_count, | |
'favourites_count' => $data->favourites_count, | |
'statuses_count' => $data->statuses_count, | |
'listed_count' => $data->listed_count, | |
'mentions_count' => 0, | |
'created_at' => date(DB::connection()->grammar()->grammar->datetime), | |
'updated_at' => date(DB::connection()->grammar()->grammar->datetime), | |
)); | |
return $status; | |
} | |
private function put_facebook($brand, $container) | |
{ | |
$data = $container['data']; | |
$status = DB::table('metrics_social_facebook')->insert_get_id(array( | |
'brand_id' => $brand, | |
'action' => $container['action'], | |
'likes_count' => $data->likes, | |
'new_like_count' => $data->new_like_count, | |
'were_here_count' => $data->were_here_count, | |
'talking_about_count' => $data->talking_about_count, | |
'created_at' => date(DB::connection()->grammar()->grammar->datetime), | |
'updated_at' => date(DB::connection()->grammar()->grammar->datetime), | |
)); | |
return $status; | |
} | |
private function put_foursquare($brand, $container) | |
{ | |
$data = $container['data']; | |
$status = DB::table('metrics_social_foursquare')->insert_get_id(array( | |
'brand_id' => $brand, | |
'action' => $container['action'], | |
'checkins_count' => $data->venue->stats->checkinsCount, | |
'tips_count' => $data->venue->stats->tipCount, | |
'users_count' => $data->venue->stats->usersCount, | |
'photos_count' => $data->venue->photos->count, | |
'listed_count' => $data->venue->listed->count, | |
'created_at' => date(DB::connection()->grammar()->grammar->datetime), | |
'updated_at' => date(DB::connection()->grammar()->grammar->datetime), | |
)); | |
return $status; | |
} | |
private function put_instagram($brand, $container) | |
{ | |
$data = $container['data']; | |
$status = DB::table('metrics_social_instagram')->insert_get_id(array( | |
'brand_id' => $brand, | |
'action' => $container['action'], | |
'media_count' => $data->counts->media, | |
'follows_count' => $data->counts->follows, | |
'followed_by_count' => $data->counts->followed_by, | |
'created_at' => date(DB::connection()->grammar()->grammar->datetime), | |
'updated_at' => date(DB::connection()->grammar()->grammar->datetime), | |
)); | |
return $status; | |
} | |
private function put_linkedin($brand, $container) | |
{ | |
$data = $container['data']; | |
$status = DB::table('metrics_social_linkedin')->insert(array( | |
'brand_id' => $brand, | |
'action' => $container['action'], | |
'followers_count' => $data['numConnections'], | |
'created_at' => date(DB::connection()->grammar()->grammar->datetime), | |
'updated_at' => date(DB::connection()->grammar()->grammar->datetime), | |
)); | |
return $status; | |
} | |
private function put_google($brand, $container) | |
{ | |
$data = $container['data']; | |
$status = DB::table('metrics_social_google')->insert(array( | |
'brand_id' => $brand, | |
'action' => $container['action'], | |
'plus_one_count' => isset($data['plusOneCount']) ? $data['plusOneCount'] : 0, | |
'circled_by_count' => isset($data['circledByCount']) ? $data['circledByCount'] : 0, | |
'created_at' => date(DB::connection()->grammar()->grammar->datetime), | |
'updated_at' => date(DB::connection()->grammar()->grammar->datetime), | |
)); | |
return $status; | |
} | |
private function put_klout($brand, $container) | |
{ | |
$data = $container['data']; | |
$status = DB::table('metrics_social_klout')->insert(array( | |
'brand_id' => $brand, | |
'action' => $container['action'], | |
'score' => isset($data['score']) ? $data['score'] : 0, | |
'influencers_count' => isset($data['myInfluencersCount']) ? $data['myInfluencersCount'] : 0, | |
'influencees_count' => isset($data['myInfluenceesCount']) ? $data['myInfluenceesCount'] : 0, | |
'created_at' => date(DB::connection()->grammar()->grammar->datetime), | |
'updated_at' => date(DB::connection()->grammar()->grammar->datetime), | |
)); | |
return $status; | |
} | |
private function put_analytics($brand, $container) | |
{ | |
$data = $container['data']; | |
$status = DB::table('metrics_web_analytics')->insert(array( | |
'brand_id' => $brand, | |
'action' => $container['action'], | |
'account' => $container['account']['account'], | |
'property' => $container['account']['property'], | |
'profile' => $container['account']['profile'], | |
'visit_overall' => isset($data['visit_overall']) ? $data['visit_overall'] : 0, | |
'visit_direct' => isset($data['visit_direct']) ? $data['visit_direct'] : 0, | |
'visit_referral' => isset($data['visit_referral']) ? $data['visit_referral'] : 0, | |
'visit_search' => isset($data['visit_search']) ? $data['visit_search'] : 0, | |
'visit_social' => isset($data['visit_social']) ? $data['visit_social'] : 0, | |
'date_start' => date(DB::connection()->grammar()->grammar->datetime), | |
'date_end' => date(DB::connection()->grammar()->grammar->datetime), | |
'created_at' => date(DB::connection()->grammar()->grammar->datetime), | |
'updated_at' => date(DB::connection()->grammar()->grammar->datetime), | |
)); | |
return $status; | |
} | |
private function _log($message) | |
{ | |
$time = '[' . date("Y-m-d H:i:s") . ']'; | |
print_r($time . ' ' . $message . "\n"); | |
} | |
} |
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 | |
namespace SmeSocial; | |
use Config; | |
use DB; | |
use Redirect; | |
use Input; | |
use URL; | |
class Driver_OAuth2 extends SmeSocial { | |
/** | |
* @return mixed | |
*/ | |
public function authenticate() | |
{ | |
// set callback | |
$callback_url = URL::base().'/'.str_finish(Config::get('smesocial::smesocial.url.callback'), '/').$this->provider->name; | |
$this->provider->callback = $callback_url; | |
// authorize | |
return Redirect::to($this->provider->authorize(array( | |
'redirect_uri' => $this->provider->callback | |
))); | |
} | |
/** | |
* @return mixed | |
* @throws SentrySocialException | |
*/ | |
public function callback() | |
{ | |
if($this->provider->name == 'klout') | |
{ | |
$code = array(); | |
$code['authCode'] = Input::get('authCode'); | |
$code['user'] = Input::get('user'); | |
} | |
else | |
{ | |
$code = Input::get('code'); | |
} | |
if ( ! $code) | |
{ | |
if(Input::get('error')) | |
{ | |
switch (Input::get('error')) | |
{ | |
case 'access_denied': | |
throw new \Exception('User cancelled the request.'); | |
break; | |
default: | |
throw new \Exception(Input::get('error')); | |
break; | |
} | |
} | |
elseif(Input::get('denied')) | |
{ | |
throw new \Exception('User cancelled the request.'); | |
} | |
} | |
return $this->provider->access($code); | |
} | |
public function refresh($user_token, $format = 'array') | |
{ | |
if(empty($user_token->refresh)) | |
{ | |
$refresh = $user_token; | |
} | |
else | |
{ | |
$refresh = $this->provider->access($user_token->refresh, array('grant_type' => 'refresh_token')); | |
} | |
if($format == 'object') | |
{ | |
return $refresh; | |
} | |
$to_array = array(); | |
foreach($refresh as $key => $value) | |
{ | |
$to_array[$key] = $value; | |
} | |
return $to_array; | |
} | |
public function disconnect($user_token) | |
{ | |
if(!in_array($user_token->provider, array('google', 'facebook', 'analytics'))) | |
{ | |
return true; | |
} | |
$token = Libraries_OAuth2_Token::make('access', array('uid' => $user_token->uid, 'access_token' => $user_token->token)); | |
return $this->provider->disconnect($token); | |
} | |
public function get_user_info($user_token) | |
{ | |
$token = Libraries_OAuth2_Token::make('access', array('uid' => $user_token->uid, 'access_token' => $user_token->token)); | |
return $this->provider->get_user_info($token); | |
} | |
public function get_pages($user_token) | |
{ | |
$token = Libraries_OAuth2_Token::make('access', array('uid' => $user_token->uid, 'access_token' => $user_token->token)); | |
return $this->provider->get_pages($token); | |
} | |
public function post_pages($user_token, $data) | |
{ | |
$token = Libraries_OAuth2_Token::make('access', array('uid' => $user_token->uid, 'access_token' => $user_token->token)); | |
return $this->provider->post_data($token, $data); | |
} | |
public function import_data($user_token, $ext = array()) | |
{ | |
$token = Libraries_OAuth2_Token::make('access', array('uid' => $user_token->uid, 'access_token' => $user_token->token)); | |
if($user_token->provider == 'analytics') | |
{ | |
return $this->provider->import_data($token, $ext); | |
} | |
return $this->provider->import_data($token); | |
} | |
public function get_provider($user_token) | |
{ | |
if($user_token->expires <= time()) | |
{ | |
$refresh = $this->refresh($user_token); | |
if(!empty($refresh)) | |
{ | |
$user_token->fill($refresh); | |
$user_token->save(); | |
} | |
} | |
$token = Libraries_OAuth2_Token::make('access', array('uid' => $user_token->uid, 'access_token' => $user_token->token)); | |
$this->provider->set_token($token); | |
return $this->provider; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment