PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tantan/facebook/plugin-class.php

http://facebook-photos.googlecode.com/
PHP | 290 lines | 255 code | 17 blank | 18 comment | 38 complexity | 48ea55e72e7898e41eacbb0926f90453 MD5 | raw file
  1. <?php
  2. /*
  3. $Revision: 33 $
  4. $Date: 2008-03-28 22:06:02 +0100 (Fri, 28 Mar 2008) $
  5. $Author: joetan54 $
  6. */
  7. class TanTanFacebookPlugin {
  8. var $facebook;
  9. var $session;
  10. var $albums;
  11. var $photos;
  12. var $lastError;
  13. var $perPage; // only applies to photo search (not by albums)
  14. function TanTanFacebookPlugin() {
  15. add_action('admin_menu', array(&$this, 'addhooks'));
  16. add_action('load-upload.php', array(&$this, 'addPhotosTab'));// WP < 2.5
  17. // WP >= 2.5
  18. add_action('media_buttons_context', array(&$this, 'media_buttons'));
  19. add_action('media_upload_tantan-facebook-photos', array(&$this, 'media_upload_content'));
  20. add_action('media_upload_tantan-facebook-photos-of-me', array(&$this, 'media_upload_content_of_me'));
  21. add_action('activate_tantan/facebook-photos.php', array(&$this, 'activate'));
  22. if ($_GET['tantanActivate'] == 'facebook-photos') {
  23. $this->showConfigNotice();
  24. }
  25. $this->photos = array();
  26. $this->albums = array();
  27. $this->perPage = 1000;
  28. }
  29. function activate() {
  30. wp_redirect('plugins.php?tantanActivate=facebook-photos');
  31. exit;
  32. }
  33. function deactivate() {}
  34. function showConfigNotice() {
  35. add_action('admin_notices', create_function('', 'echo \'<div id="message" class="updated fade"><p>Facebook Photos <strong>activated</strong>. <a href="options-general.php?page=tantan/facebook/plugin-class.php">Configure the plugin &gt;</a></p></div>\';'));
  36. }
  37. function addhooks() {
  38. add_options_page('Facebook', 'Facebook', 10, __FILE__, array(&$this, 'admin'));
  39. $this->version_check();
  40. }
  41. function version_check() {
  42. global $TanTanVersionCheck;
  43. if (is_object($TanTanVersionCheck)) {
  44. $data = get_plugin_data(dirname(__FILE__).'/../facebook-photos.php');
  45. $TanTanVersionCheck->versionCheck(603, $data['Version']);
  46. }
  47. }
  48. function admin() {
  49. if (!class_exists('FacebookRestClient')) require_once(dirname(__FILE__).'/facebookapi_php5_restlib.php');
  50. $this->facebook = new FacebookRestClient(FACEBOOK_API_KEY, FACEBOOK_API_SECRET, null, true);
  51. if ($_POST['action'] == 'get-new-session') {
  52. $this->saveNewSession();
  53. }
  54. if ($_POST['action'] == 'savebase') {
  55. $url = parse_url(get_bloginfo('siteurl'));
  56. $baseurl = $url['path'] . '/' . $_POST['baseurl'];
  57. if (!ereg('.*/$', $baseurl)) $baseurl .= '/';
  58. if (strlen($_POST['baseurl']) <= 0) {
  59. $baseurl = false;
  60. }
  61. update_option('silas_facebook_baseurl_pre', $url['path'] . '/');
  62. update_option('silas_facebook_baseurl', $baseurl);
  63. } elseif ($_POST['action'] == 'save') {
  64. $this->saveNewSession();
  65. } elseif ($_POST['action'] == 'logout') {
  66. update_option('tantan_facebook_session', '');
  67. }
  68. $baseurl = get_option('tantan_facebook_baseurl');
  69. $baseurl_pre = get_option('tantan_facebook_baseurl_pre');
  70. $apikey = get_option('tantan_facebook_apikey');
  71. $secret = get_option('tantan_facebook_secret');
  72. $session = get_option('tantan_facebook_session');
  73. if (!$session) {
  74. $authToken = $this->facebook->auth_createToken();
  75. //$authToken = $authToken['token'];
  76. include(dirname(__FILE__).'/admin-options.html');
  77. } else {
  78. $this->facebook->session_key = $session['session_key'];
  79. $this->facebook->secret = $session['secret'];
  80. try {
  81. $user = array_pop($this->facebook->users_getInfo($session['uid'], array('name', 'first_name', 'last_name')));
  82. include(dirname(__FILE__).'/admin-options.html');
  83. } catch (FacebookRestClientException $e) {
  84. $this->handleException($e);
  85. echo
  86. '<div style="margin:10px 20px;border:2px solid #ccc; padding:10px;">'.
  87. '<p>Or, you can reset your Facebook.com login.</p>'.
  88. '<form method="post"><input type="hidden" name="action" value="logout" />'.
  89. '<input type="submit" value="Reset Facebook.com login &gt;" />'.
  90. '</form></div>';
  91. }
  92. }
  93. }
  94. function handleException($e=false) {
  95. if ($e) $this->lastError = $e;
  96. switch ($this->lastError->getCode()) {
  97. case 104: // incorrect signature (app is logged in somewhere else?)
  98. case 102: // invalid session / timeout
  99. $this->showSessionError();
  100. break;
  101. case -1000:
  102. $this->showNotConfigured();
  103. break;
  104. default:
  105. echo '<div id="message" class="error fade"><p><strong>Error ('.$this->lastError->getCode().'): '.$this->lastError->getMessage().'</strong></p>'.
  106. '<xmp style="overflow:auto">'.$this->lastError->getTraceAsString().'</xmp>'.
  107. '</div>';
  108. break;
  109. }
  110. }
  111. function showNotConfigured() {
  112. echo '<p>&nbsp;</p><p><strong>Error:</strong> The Facebook Photos plugin has not been configured yet.</p>'.
  113. '<p>You can configure the plugin in the <strong>Options -&gt; Facebook</strong> menu.</p>';
  114. }
  115. function showSessionError() {
  116. update_option('tantan_facebook_session', '');
  117. $this->facebook->session_key = '';
  118. $this->facebook->secret = FACEBOOK_API_SECRET;
  119. $authToken = $this->facebook->auth_createToken();
  120. include(dirname(__FILE__).'/admin-get-new-session.html');
  121. }
  122. function saveNewSession() {
  123. try {
  124. $session = $this->facebook->auth_getSession($_POST['auth_token']);
  125. if ($session['session_key']) {
  126. //$session['uid_numeric'] = $facebook->users_getLoggedInUser();
  127. update_option('tantan_facebook_session', $session);
  128. } else {
  129. update_option('tantan_facebook_session', '');
  130. throw new Exception("Error applying Facebook session information.", -1001);
  131. }
  132. } catch (FacebookRestClientException $e) {
  133. $this->handleException($e);
  134. } catch (Exception $e) {
  135. $this->handleException($e);
  136. }
  137. }
  138. function media_buttons($context) {
  139. global $post_ID, $temp_ID;
  140. $dir = dirname(__FILE__);
  141. $pluginRootURL = get_option('siteurl').substr($dir, strpos($dir, '/wp-content'));
  142. $image_btn = $pluginRootURL.'/icon.gif';
  143. $image_title = 'Facebook Photos';
  144. $uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
  145. $media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
  146. $out = ' <a href="'.$media_upload_iframe_src.'&tab=tantan-facebook-photos&TB_iframe=true&height=500&width=640" class="thickbox" title="'.$image_title.'"><img src="'.$image_btn.'" alt="'.$image_title.'" /></a>';
  147. return $context.$out;
  148. }
  149. function media_upload_content_of_me() {
  150. $_REQUEST['tt-type'] = 'tagged';
  151. add_filter('tantan_media_upload_page_links', array(&$this, 'media_upload_page_links'));
  152. $this->media_upload_content('of-me');
  153. }
  154. function media_upload_content($type='') {
  155. $this->upload_files_tantan_facebook();
  156. add_filter('media_upload_tabs', array(&$this, 'media_upload_tabs'));
  157. add_action('admin_print_scripts', array(&$this, 'upload_tabs_scripts'));
  158. add_action('admin_print_scripts', 'media_admin_css');
  159. add_action('tantan_media_upload_header', 'media_upload_header');
  160. try {
  161. wp_iframe(array(&$this, 'photosTab'));
  162. } catch (FacebookRestClientException $e) {
  163. $this->handleException($e);
  164. }
  165. }
  166. function media_upload_page_links($args) {
  167. return paginate_links( $args );
  168. }
  169. function media_upload_tabs($tabs) {
  170. return array(
  171. 'tantan-facebook-photos' => __('My Photos'), // handler action suffix => tab text
  172. 'tantan-facebook-photos-of-me' => __('Photos of Me'),
  173. );
  174. }
  175. function addPhotosTab() {
  176. add_filter('wp_upload_tabs', array(&$this, 'wp_upload_tabs'));
  177. add_action('upload_files_tantan_facebook', array(&$this, 'upload_files_tantan_facebook'));
  178. add_action('admin_print_scripts', array(&$this, 'upload_tabs_scripts'));
  179. }
  180. function wp_upload_tabs ($array) {
  181. /*
  182. 0 => tab display name,
  183. 1 => required cap,
  184. 2 => function that produces tab content,
  185. 3 => total number objects OR array(total, objects per page),
  186. 4 => add_query_args
  187. */
  188. if ($this->lastError) {
  189. $args = array();
  190. $tab = array(
  191. 'tantan_facebook' => array('Photos (Facebook)', 'upload_files', array(&$this, 'handleException'), 0, $args),
  192. );
  193. return array_merge($array, $tab);
  194. } else {
  195. $numAlbums = count($this->albums);
  196. $numPhotos = count($this->photos);
  197. $paged = array();
  198. $args = array('tt-type' => ''); // this doesn't do anything in WP 2.1.2
  199. $tab = array(
  200. 'tantan_facebook' => array('Photos (Facebook)', 'upload_files', array(&$this, 'photosTab'), $paged, $args),
  201. );
  202. return array_merge($array, $tab);
  203. }
  204. }
  205. function upload_tabs_scripts() {
  206. include(dirname(__FILE__).'/admin-tab-head.html');
  207. }
  208. function upload_files_tantan_facebook() {
  209. if (!class_exists('FacebookRestClient')) require_once(dirname(__FILE__).'/facebookapi_php5_restlib.php');
  210. $this->facebook = new FacebookRestClient(FACEBOOK_API_KEY, FACEBOOK_API_SECRET, null, true);
  211. if ($_POST['action'] == 'get-new-session') {
  212. $this->saveNewSession();
  213. }
  214. try {
  215. $this->session = get_option('tantan_facebook_session');
  216. if ($this->session) {
  217. $this->facebook->session_key = $this->session['session_key'];
  218. $this->facebook->secret = $this->session['secret'];
  219. if (isset($POST['aid'])) unset($_REQUEST['tt-type']);
  220. if ($_REQUEST['tt-type'] == 'tagged') {
  221. $this->perPage = 50;
  222. $this->photos = $this->facebook->photos_get($this->session['uid'], '', '');
  223. } else {
  224. $this->albums = $this->facebook->photos_getAlbums($this->session['uid'], '');
  225. }
  226. } else {
  227. throw new Exception('Plugin is not configured', -1000);
  228. }
  229. } catch (FacebookRestClientException $e) {
  230. $this->lastError = $e;
  231. // see wp_upload_tabs for the catch... kinda ugly, but need to do this because of the way WP plugin hooks / filters are defined
  232. } catch (Exception $e) {
  233. $this->lastError = $e;
  234. }
  235. }
  236. function photosTab() {
  237. $offsetpage = (int) $_GET['paged'];
  238. if (!$offsetpage) $offsetpage = 1;
  239. try {
  240. if ($_REQUEST['tt-type'] == 'tagged') {
  241. $photos = array_slice($this->photos, ($offsetpage-1)*$this->perPage, $this->perPage);
  242. $numPhotos = count($this->photos);
  243. } else {
  244. if (isset($_POST['aid'])) {
  245. foreach ($this->albums as $album) {
  246. if ($album['aid'] == $_POST['aid']) break;
  247. }
  248. $photos = $this->facebook->photos_get('', $_POST['aid'], '');
  249. } else {
  250. $album = $this->albums[$offsetpage-1];
  251. $_POST['aid'] = $album['aid'];
  252. $photos = $this->facebook->photos_get('', $album['aid'], '');
  253. }
  254. $numPhotos = count($photos);
  255. }
  256. $albums = $this->albums;
  257. //$photos = $facebook->photos_get($session['uid'], '', '');
  258. //$photos = $facebook->call_method('facebook.fql.query', array('query' => 'SELECT pid, aid, owner, src, src_big, src_small, link, caption, created FROM photo WHERE owner = '.$session['uid']));
  259. do_action('tantan_media_upload_header');
  260. $page_links = apply_filters('tantan_media_upload_page_links',array(
  261. 'base' => add_query_arg( 'paged', '%#%' ),
  262. 'format' => '',
  263. 'total' => ceil($numPhotos / $this->perPage),
  264. 'mid_size' => 1,
  265. 'current' => ($offsetpage ? $offsetpage : 1),
  266. ));
  267. include(dirname(__FILE__).'/admin-photos-tab.html');
  268. } catch (FacebookRestClientException $e) {
  269. $this->handleException($e);
  270. }
  271. }
  272. }
  273. ?>