PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/facebook.php

https://github.com/chrisnharvey/CodeIgniter-Facebook-Spark
PHP | 286 lines | 223 code | 50 blank | 13 comment | 25 complexity | ab43a7bafbb15e6fd10e07d1ba476cb2 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter Facebook Spark
  4. *
  5. * Author: Chris Harvey (Back2theMovies)
  6. * Website: http://www.chrisnharvey.com/code
  7. * Email: chris@chrisnharvey.com
  8. *
  9. * Originally developed for Back2theMovies (http://www.b2tm.com)
  10. *
  11. **/
  12. class Facebook {
  13. public $_graph_url = "https://graph.facebook.com/";
  14. public $_app_id;
  15. private $_secret;
  16. function __construct()
  17. {
  18. $this->_CI =& get_instance();
  19. $this->_CI->load->config('facebook');
  20. $this->_CI->load->library("session");
  21. $this->_CI->load->helper("url");
  22. $this->_app_id = $this->_CI->config->item('facebook_app_id');
  23. $this->_secret = $this->_CI->config->item('facebook_secret');
  24. $this->_default_scope = $this->_CI->config->item('facebook_default_scope');
  25. $this->_cookie_name = "fbsr_".$this->_app_id;
  26. if(!$this->_CI->session->userdata("facebook_scope"))
  27. {
  28. $this->set_scope($this->_default_scope);
  29. }
  30. if(!$this->_CI->session->userdata("facebook_redirect_uri"))
  31. {
  32. $this->set_redirect_uri(current_url());
  33. }
  34. }
  35. public function login_url()
  36. {
  37. $scope = $this->_CI->session->userdata("facebook_scope");
  38. $redirect_uri = $this->_CI->session->userdata("facebook_redirect_uri");
  39. if(!isset($scope))
  40. {
  41. $scope = $this->_default_scope;
  42. }
  43. if(empty($scope))
  44. {
  45. $scope_string = "";
  46. }
  47. else
  48. {
  49. $scope_string = "&scope=".$scope;
  50. }
  51. if(empty($redirect_uri))
  52. {
  53. $callback_string = "&redirect_uri=".site_url();
  54. }
  55. else
  56. {
  57. $callback_string = "&redirect_uri=".$redirect_uri;
  58. }
  59. if(!isset($redirect_uri))
  60. {
  61. $redirect_uri = "";
  62. }
  63. return "https://www.facebook.com/dialog/oauth?client_id=".$this->_app_id.$callback_string.$scope_string;
  64. }
  65. private function _unset($key)
  66. {
  67. $this->_CI->session->unset_userdata($key);
  68. }
  69. public function is_logged_in()
  70. {
  71. $check = $this->call("get", "me");
  72. if($this->get_access_token() && $check)
  73. {
  74. return TRUE;
  75. }
  76. else
  77. {
  78. return FALSE;
  79. }
  80. }
  81. public function call($method, $uri, $params = array()){
  82. $token = $this->get_access_token();
  83. $token = $token['token'];
  84. $url_string = $this->_graph_url.$uri."?access_token=".$token;
  85. if($method == "get")
  86. {
  87. foreach($params as $param => $value)
  88. {
  89. $url_string .= "$".$param."=".$value;
  90. }
  91. }
  92. if($uri == "me")
  93. {
  94. try
  95. {
  96. $response = $this->curl_call($method, $url_string, $params);
  97. }
  98. catch(facebookException $e)
  99. {
  100. $this->_unset("facebook_access_token");
  101. return FALSE;
  102. }
  103. }
  104. else
  105. {
  106. try
  107. {
  108. $response = $this->curl_call($method, $url_string, $params);
  109. }
  110. catch(facebookException $e)
  111. {
  112. $this->call("get", "me");
  113. return FALSE;
  114. }
  115. }
  116. $response = json_decode($response); // Decode the JSON response into an array
  117. return $response;
  118. }
  119. private function curl_call($method = 'get', $url, $params = array())
  120. {
  121. $ch = curl_init();
  122. if($method == "post")
  123. {
  124. curl_setopt($ch, CURLOPT_POST, TRUE);
  125. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  126. }
  127. curl_setopt($ch, CURLOPT_URL, $url);
  128. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  129. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  130. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  131. $response = curl_exec ($ch);
  132. curl_close ($ch);
  133. $response_a = json_decode($response);
  134. if(isset($response_a->error))
  135. {
  136. throw new facebookException($response_a->error->type." - ".$response_a->error->message);
  137. }
  138. else
  139. {
  140. return $response; // Return the response
  141. }
  142. }
  143. protected function parse_signed_request($signed_request) {
  144. list($encoded_sig, $payload) = explode('.', $signed_request, 2);
  145. // decode the data
  146. $sig = $this->base64_url_decode($encoded_sig);
  147. $data = json_decode($this->base64_url_decode($payload), true);
  148. if (strtoupper($data['algorithm']) !== 'HMAC-SHA256')
  149. {
  150. return NULL;
  151. }
  152. // check sig
  153. $expected_sig = hash_hmac('sha256', $payload, $this->_secret, $raw = true);
  154. if ($sig !== $expected_sig)
  155. {
  156. return NULL;
  157. }
  158. return $data;
  159. }
  160. protected static function base64_url_decode($input) {
  161. return base64_decode(strtr($input, '-_', '+/'));
  162. }
  163. public function get_access_token()
  164. {
  165. $sess_access_token = $this->_CI->session->userdata("facebook_access_token");
  166. if(!empty($sess_access_token))
  167. {
  168. return $this->_CI->session->userdata("facebook_access_token");
  169. }
  170. elseif(isset($_REQUEST['signed_request']))
  171. {
  172. $signed_request = $this->parse_signed_request($_REQUEST['signed_request']);
  173. }
  174. elseif(isset($_COOKIE[$this->_cookie_name]))
  175. {
  176. $signed_request = $this->parse_signed_request($_COOKIE[$this->_cookie_name]);
  177. }
  178. if(isset($signed_request))
  179. {
  180. try
  181. {
  182. $call_url = $this->_graph_url."oauth/access_token?client_id=".$this->_app_id."&redirect_uri=&client_secret=".$this->_secret."&code=".$signed_request['code'];
  183. $curl = $this->curl_call('get', $call_url);
  184. $token = parse_str($curl);
  185. if(isset($access_token) && isset($expires))
  186. {
  187. $this->set_access_token($access_token, $expires);
  188. return $this->_CI->session->userdata("facebook_access_token");
  189. }
  190. elseif(isset($access_token) && !isset($expires))
  191. {
  192. $this->set_access_token($access_token);
  193. return $this->_CI->session->userdata("facebook_access_token");
  194. }
  195. else
  196. {
  197. return FALSE;
  198. }
  199. }
  200. catch(facebookException $e)
  201. {
  202. $this->_unset("facebook_access_token");
  203. return FALSE;
  204. }
  205. }
  206. }
  207. public function set_access_token($access_token, $expires = FALSE)
  208. {
  209. if($expires != FALSE)
  210. {
  211. $this->_CI->session->set_userdata('facebook_access_token', array("token" => $access_token, "expires" => $expires));
  212. }
  213. else
  214. {
  215. $this->_CI->session->set_userdata('facebook_access_token', array("token" => $access_token));
  216. }
  217. }
  218. public function set_redirect_uri($redirect_uri)
  219. {
  220. $this->_CI->session->set_userdata('facebook_redirect_uri', $redirect_uri);
  221. }
  222. public function set_scope($scope)
  223. {
  224. $this->_CI->session->set_userdata('facebook_scope', $scope);
  225. }
  226. }
  227. class facebookException extends Exception {
  228. function __construct($string)
  229. {
  230. parent::__construct($string);
  231. }
  232. public function __toString() {
  233. return "exception '".__CLASS__ ."' with message '".$this->getMessage()."' in ".$this->getFile().":".$this->getLine()."\nStack trace:\n".$this->getTraceAsString();
  234. }
  235. }
  236. /* End of file */