function fpf_deactivate() { if (get_option($GLOBALS['opt_fb_sess_uid'])) { fpf_auth($GLOBALS['fpf_name'], $GLOBALS['fpf_version'], 0, "OFF: " . get_option($GLOBALS['opt_fb_sess_uid']) . " (" . get_option($GLOBALS['opt_fb_sess_uname']) . ")"); } }
/** * Output the plugin's Admin Page */ function fpf_admin_page() { global $fpf_name, $fpf_version, $fpf_identifier, $fpf_homepage; global $fpf_opt_access_token, $fpf_opt_token_expiration, $fpf_opt_last_uid_search; global $fpf_shown_tab; $fpf_shown_tab = 2; $allTabsClass = "fpf_admin_tab"; $allTabBtnsClass = "fpf_admin_tab_btn"; $tab1Id = "fpf_admin_fbsetup"; $tab2Id = "fpf_admin_utils"; $tab3Id = "fpf_admin_addon"; $tab4Id = "fpf_admin_supportinfo"; ?> <div class="wrap"> <h2><?php echo $fpf_name; ?> </h2> <?php //Check $_POST for what we're doing, and update any necessary options if (isset($_POST[$fpf_opt_access_token])) { //We're saving a new access token. Let's use it to try and fetch the userID, to verify that it's valid before saving. //Also, store the expiration timestamp. We need to store this as the debug_token endpoint is only available to the current //app's developer (so a regular user can't get it again - only when the token is first assigned). $user = fpf_get("https://graph.facebook.com/me?access_token=" . $_POST[$fpf_opt_access_token] . "&fields=name,id"); if (isset($user->id) && !isset($user->error)) { update_option($fpf_opt_access_token, $_POST[$fpf_opt_access_token]); update_option($fpf_opt_token_expiration, time() + $_POST[$fpf_opt_token_expiration]); fpf_auth($fpf_name, $fpf_version, 2, "SET: " . $user->id . " (" . $user->name . ")"); ?> <div class="updated"><p><strong><?php echo 'Facebook Session Saved (Name: ' . $user->name . ', ID: ' . $user->id . ')'; ?> </strong></p></div><?php } else { update_option($fpf_opt_access_token, 0); update_option($fpf_opt_token_expiration, 0); ?> <div class="updated"><p><strong><?php echo 'Error: Failed to get a valid access token from Facebook. Response: ' . (isset($user->error->message) ? $user->error->message : "Unknown"); ?> </strong></p></div><?php } } else { if (isset($_POST['delete_token'])) { //No need to output an 'updated' message, because the lack of a token will be detected and shown as an error below. update_option($fpf_opt_access_token, 0); } else { if (isset($_POST[$fpf_opt_last_uid_search])) { update_option($fpf_opt_last_uid_search, $_POST[$fpf_opt_last_uid_search]); ?> <div class="updated"><p><strong><?php echo 'Album search completed.'; ?> </strong></p></div><?php } else { do_action('fpf_extra_panel_actions', $_POST); } } } //Whenever the admin panel is loaded, verify that the access_token is valid by trying to fetch the name and id. //If not, clear it from the database, forcing the user to (re-)validate. $access_token = get_option($fpf_opt_access_token); $user = fpf_get("https://graph.facebook.com/me?access_token=" . $access_token . "&fields=name,id"); if (!$access_token) { ?> <div class="error"><p><strong><?php echo 'This plugin does not have a valid Facebook access token. Please authorize it by logging in below.'; ?> </strong></p></div><?php } else { if (!$user) { ?> <div class="error"><p><strong><?php echo 'An error occurred while validating your Facebook access token (empty reply). Please re-authorize by logging in below.'; ?> </strong></p></div><?php update_option($fpf_opt_access_token, 0); } else { if (isset($user->error)) { ?> <div class="error"><p><strong><?php echo $user->error->message . "<br /><br />Please re-authorize this plugin by logging into Facebook below."; ?> </strong></p></div><?php update_option($fpf_opt_access_token, 0); } } } //Re-get the access_token, in case it was cleared by an error above) $access_token = get_option($fpf_opt_access_token); if (!$access_token) { $fpf_shown_tab = 1; } ?> <!-- Tab Navigation --> <script type="text/javascript"> function fpf_swap_tabs(show_tab_id) { //Hide all the tabs, then show just the one specified jQuery(".<?php echo $allTabsClass; ?> ").hide(); jQuery("#" + show_tab_id).show(); //Unhighlight all the tab buttons, then highlight just the one specified jQuery(".<?php echo $allTabBtnsClass; ?> ").attr("class", "<?php echo $allTabBtnsClass; ?> "); jQuery("#" + show_tab_id + "_btn").addClass("fpf-admin_tab_selected"); } </script> <div> <ul class="fpf-admin_tabs"> <li id="<?php echo $tab1Id; ?> _btn" class="<?php echo $allTabBtnsClass; ?> <?php echo $fpf_shown_tab == 1 ? "fpf-admin_tab_selected" : ""; ?> "><a href="javascript:void(0);" onclick="fpf_swap_tabs('<?php echo $tab1Id; ?> ');">Facebook Setup</a></li> <li id="<?php echo $tab2Id; ?> _btn" class="<?php echo $allTabBtnsClass; ?> <?php echo $fpf_shown_tab == 2 ? "fpf-admin_tab_selected" : ""; ?> "><a href="javascript:void(0);" onclick="fpf_swap_tabs('<?php echo $tab2Id; ?> ')";>Utilities</a></li> <?php if (defined('FPF_ADDON')) { ?> <li id="<?php echo $tab3Id; ?> _btn" class="<?php echo $allTabBtnsClass; ?> <?php echo $fpf_shown_tab == 3 ? "fpf-admin_tab_selected" : ""; ?> "><a href="javascript:void(0);" onclick="fpf_swap_tabs('<?php echo $tab3Id; ?> ')";>Addon</a></li> <?php } ?> <li id="<?php echo $tab4Id; ?> _btn" class="<?php echo $allTabBtnsClass; ?> <?php echo $fpf_shown_tab == 4 ? "fpf-admin_tab_selected" : ""; ?> "><a href="javascript:void(0);" onclick="fpf_swap_tabs('<?php echo $tab4Id; ?> ')";>Support Info</a></li> </ul> </div> <!--Start Main panel content--> <div class="fpf-admin_wrapper"> <div class="<?php echo $allTabsClass; ?> " id="<?php echo $tab1Id; ?> " style="display:<?php echo $fpf_shown_tab == 1 ? "block" : "none"; ?> "> <h3>Overview</h3> This plugin allows you to create Wordpress photo galleries from any Facebook album you can access.<br /><br /> To get started, you must first connect with your Facebook account using the button below. Once connected, you can create a gallery by making a new Wordpress post or page and pasting in one line of special HTML, like this:<br /><br /> <b><!--<?php echo $fpf_identifier; ?> 1234567890123456789 --><!--/<?php echo $fpf_identifier; ?> --></b><br /><br /> Whenever you save a post or page containing these tags, this plugin will automatically download the album information and insert its contents between them. You are free to include any normal content you like before or after, as usual.<br /><br /> The example number above (1234567890123456789) is an ID that tells the plugin which Facebook album you'd like to import. To find a list of available albums, you can use the "Search for Albums" feature under the "Utilities" tab.<br /><br /> That's all there is to it! For more information on how to customize your albums, help, and a demo, please see the full documentation on the <a href="<?php echo $fpf_homepage; ?> "><b>plugin homepage</b></a>.<br /><br /> And if you like this plugin, please don't forget to <a href="javascript:void(0);" onclick="fpf_swap_tabs('<?php echo $tab4Id; ?> ');jQuery('html, body').animate({ scrollTop: jQuery(document).height() }, 'slow');"><b>donate</b></a> a few bucks to buy me a beer (or a pitcher). I promise to enjoy every ounce of it :)<br /><br /> <hr /> <?php //SECTION - Facebook Authorization. See notes at the bottom of this file. ?> <h3>Facebook Authorization</h3> <?php if ($access_token) { ?> <i>This plugin is successfully connected with <b><?php echo $user->name; ?> </b>'s Facebook account and is ready to create galleries.</i> If you'd like to remove the connection and authorize a different user, click the button below:<br /><br /> <?php } else { ?> Before this plugin can be used, you must connect it to your Facebook account. Please click the following button to login.<br /><br /> <?php } ?> <!--Deauthorize button--> <?php if ($access_token) { ?> <form method="post" action=""> <input type="hidden" id="delete_token" name="delete_token" value="0" /> <input type="submit" class="button-secondary" style="width:127px;" value="Deauthorize" /> </form> <?php } ?> <!--Login/Renew button--> <!--Facebook requires the auth dialog to be initialized on a domain specified in the FPF app settings. It therefore resides--> <!--on my auth server, shown here in an iFrame. Once the user authorizes, easyXDM will communicate the token back to this--> <!--admin panel where it can be saved. EasyXDM creates the iFrame for us, sends a message to tell it what to name the button,--> <!--then waits for the login token. The iFrame lives in the "authorizeFrame" container.--> <!--The iFrame may be named "Login with Facebook" or "Renew," based on if there's already a token in the database. A "renew" button--> <!--will only be shown if there's 59 days or less until expiration (since FB doesn't allow you to renew in the first day).--> <?php if (!$access_token || $access_token && (get_option($fpf_opt_token_expiration) - time()) / 60 / 60 / 24 < 59.0) { ?> <div id="graph_step1" style="width:150px;height:23px;float:left;"> <div id="authorizeFrame" style="height:30px;overflow:hidden;"></div> <script type="text/javascript" src="<?php echo plugins_url(dirname(plugin_basename(__FILE__))); ?> /easyXDM/easyXDM.min.js"></script> <script> var socket = new easyXDM.Socket( { //EasyXDM will setup the iFrame here container: "authorizeFrame", remote: "http://auth.justin-klein.com/FPF-Auth", //Once it's ready, send a message to tell it what to name the login button & which plugin version we're using onReady: function() { var message = {btnName:'<?php echo $access_token ? "Renew" : "Login with Facebook"; ?> ', pluginVersion:'<?php echo $fpf_version; ?> '}; socket.postMessage(JSON.stringify(message)); }, //And wait for a response - which will come once the user has logged in with Facebook. //When the response comes, auto-submit the invisible form below to save the token. onMessage: function(message, origin) { var response = JSON.parse(message); jQuery('#<?php echo $fpf_opt_access_token; ?> ').val(response.accessToken); jQuery('#<?php echo $fpf_opt_token_expiration; ?> ').val(response.expiresIn); jQuery('#graph_token_submit').submit(); } }); </script> </div> <form method="post" id="graph_token_submit" action=""> <input type="hidden" id="<?php echo $fpf_opt_access_token; ?> " name="<?php echo $fpf_opt_access_token; ?> " value="0" /> <input type="hidden" id="<?php echo $fpf_opt_token_expiration; ?> " name="<?php echo $fpf_opt_token_expiration; ?> " value="0" /> </form> <?php if ($access_token) { ?> <span style="float:left;"><small>(Expires in <?php echo human_time_diff(get_option($fpf_opt_token_expiration)); ?> )</small></span><?php } ?> <br clear="all" /> <?php } ?> <hr /> <?php //Output the token expiration, for testing. //NOTE: This will only work for MY user account (they only allow the developer of an app to debug that app's access tokens) //See https://developers.facebook.com/docs/howtos/login/debugging-access-tokens echo "<small><strong>Debug</strong><br />"; if ($access_token) { echo "Token: {$access_token}<br />"; echo "Expected Expiration: " . human_time_diff(get_option($fpf_opt_token_expiration)) . "<br />"; $tokenResponse = fpf_get("https://graph.facebook.com/debug_token?input_token=" . get_option($fpf_opt_access_token) . '&access_token=' . get_option($fpf_opt_access_token)); if (isset($tokenResponse->data->expires_at)) { $expiresMin = (int) (($tokenResponse->data->expires_at - time()) / 60); $expiresH = (int) ($expiresMin / 60); $expiresMin -= $expiresH * 60; echo "True Expiration: {$expiresH}" . "h {$expiresMin}" . "m"; } else { echo "True Expiration: Unknown"; } } else { echo "Token: None"; } echo "</small>"; ?> </div><!--end tab--> <div class="<?php echo $allTabsClass; ?> " id="<?php echo $tab2Id; ?> " style="display:<?php echo $fpf_shown_tab == 2 ? "block" : "none"; ?> "> <?php //SECTION - Search for albums ?> <h3>Search for Albums</h3> <form name="listalbums" method="post" action=""> To get a list of album IDs that you can use to create galleries, enter a Facebook Page or User ID below and click "Search."<br /><br /> Your User ID is <b><?php echo $user->id; ?> </b>. To get a friend or page's ID, click on one of their photos - the URL will be something like <b>facebook.com/photo.php?fbid=012&set=a.345.678.900</b>. The last set of numbers (900 in this example) is their ID.<br /><br /> <input type="text" name="<?php echo $fpf_opt_last_uid_search; ?> " value="<?php echo get_option($fpf_opt_last_uid_search); ?> " size="20"> <input type="submit" class="button-secondary" name="Submit" value="Search" /> </form> <?php //If we just requested a search, do it and show results. add_option($fpf_opt_last_uid_search, $user->id); if (isset($_POST[$fpf_opt_last_uid_search])) { //Get the name of the user/page whose ID we're searching $search_uid = get_option($fpf_opt_last_uid_search); $response = fpf_get("https://graph.facebook.com/{$search_uid}?access_token={$access_token}&fields=name"); $search_name = $response->name; if (!$search_name) { $search_name = "(Unknown User)"; } //Get the list of albums $response = fpf_get("https://graph.facebook.com/{$search_uid}/albums?access_token={$access_token}&limit=999&fields=id,link,name"); $albums = $response->data; //..And show the list. echo "<div class='postbox' style='margin-top:5px; width:550px;'>"; echo "<h3 class='hndle' style='padding:6px;'><span>Available Facebook Albums for <a href='http://www.facebook.com/profile.php?id={$search_uid}' target='_fb'>{$search_name}</a>:</span></h3>"; echo "<div class='inside'><small>"; if (is_array($albums) && count($albums) > 0) { foreach ($albums as $album) { echo '<!--' . $fpf_identifier . ' ' . $album->id . ' --><!--/' . $fpf_identifier . '--> (<a href="' . $album->link . '">' . $album->name . '</a>)<br />'; } } else { echo "None found.<br />"; } echo "</small></div></div>"; } ?> <hr /> <?php //SECTION - Fetch all albums ?> <h3>Refresh Albums from Facebook</h3> This will scan all your posts and pages for galleries created with this plugin, and regenerate each one it finds by re-fetching its information from Facebook. The only reason to use this would be if you've changed or updated something in many of your albums and want those changes to be reflected here as well. It can be slow if you have lots of galleries, so use with caution.<br /><br /> <div class="postbox" style="width:400px; height:80px; padding:10px; float:left; text-align:center;"> <form name="fetchallposts" method="post" action=""> <input type="hidden" name="fetch_pages" value="Y"> <input type="submit" class="button-secondary" name="Submit" value="Re-Fetch All Albums in Pages" /> </form> <br /> <form name="fetchallpages" method="post" action=""> <input type="hidden" name="fetch_posts" value="Y"> <input type="submit" class="button-secondary" name="Submit" value="Re-Fetch All Albums in Posts" /> </form> </div> <?php //For an old custom addon I implemented for a customer; leave it for backwards-compatilibity. if (function_exists('fpf_output_cron_panel')) { fpf_output_cron_panel(); } ?> <br clear="all" /> <?php //When we click one of the "fetch now" buttons if (isset($_POST['fetch_pages']) || isset($_POST['fetch_posts'])) { //Get the collection of pages or posts if (isset($_POST['fetch_pages'])) { echo "<b>Checking All Pages for Facebook Albums</b>:<br />"; $pages = get_pages(array('post_status' => 'publish,private')); } else { echo "<b>Checking All Posts for Facebook Albums</b>:<br />"; $pages = get_posts('post_type=post&numberposts=-1&post_status=publish,private'); } echo "<div class='postbox' style='width:90%;padding:10px;'><pre>"; echo fpf_refetch_all($pages, true); echo "</pre></div>"; } ?> </div><!--end tab--> <div class="<?php echo $allTabsClass; ?> " id="<?php echo $tab3Id; ?> " style="display:<?php echo $fpf_shown_tab == 3 ? "block" : "none"; ?> "> <h3>Addon Options <small>(Version <?php echo FPF_ADDON_VER; ?> )</small></h3> <?php do_action('fpf_addon_admin_tab'); ?> </div><!--end tab--> <div class="<?php echo $allTabsClass; ?> " id="<?php echo $tab4Id; ?> " style="display:<?php echo $fpf_shown_tab == 4 ? "block" : "none"; ?> "> <h3>Support Information</h3> <div style="width:600px;"> Before submitting a support request, please make sure to carefully read all the documentation and FAQs on the <a href="<?php echo $fpf_homepage; ?> " target="_support">plugin homepage</a>. Every problem that's ever been reported has a solution posted there.<br /><br /> If you do choose to submit a request, please do so on the <a href="<?php echo $fpf_homepage; ?> " target="_support">plugin homepage</a>, <b><i><u>not</u></i></b> on Wordpress.org (which I rarely check). Also, be sure to include the following information about your Wordpress hosting environment:<br /> </div> <div style="width:600px; padding:5px; margin:8px 0; background-color:#EEEDDA; border:1px solid #CCC;"> <b>Host URL: </b> <?php echo $_SERVER["HTTP_HOST"]; ?> <br /> <b>Site URL: </b> <?php echo get_bloginfo('url'); ?> <br /> <b>Wordpress URL: </b> <?php echo get_bloginfo('wpurl'); ?> <br /> <b>Wordpress Version:</b> <?php echo $GLOBALS['wp_version']; ?> <br /> <b>Plugin Version:</b> <?php echo $fpf_version; ?> <br /> <b>Browser:</b> <?php echo $_SERVER['HTTP_USER_AGENT']; ?> <br /> <b>Theme:</b> <?php echo get_current_theme(); ?> <br /> <b>Server:</b> <?php echo substr($_SERVER['SERVER_SOFTWARE'], 0, 45) . (strlen($_SERVER['SERVER_SOFTWARE']) > 45 ? "..." : ""); ?> <br /> <b>Active Plugins:</b> <?php $active_plugins = get_option('active_plugins'); $plug_info = get_plugins(); echo "<b>" . count($active_plugins) . "</b><small> ("; foreach ($active_plugins as $name) { echo $plug_info[$name]['Title'] . " " . $plug_info[$name]['Version'] . "; "; } echo "</small>)<br />"; ?> </div> <hr /> <h3>Donate</h3> Many hours have gone into making this plugin as versatile and easy to use as possible, far beyond my own personal needs. Although I offer it to you freely, please keep in mind that each hour spent extending and supporting it was an hour that could've also gone towards income-generating work. If you find it useful, a small donation would be greatly appreciated. <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_s-xclick" /> <input type="hidden" name="hosted_button_id" value="L32NVEXQWYN8A" /> <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" /> <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" /> </form> </div><!--end tab--> </div><!-- div fpf-admin_wrapper --> </div><!-- div wrap --> <?php }
/** * Check the POST var for what we're doing, show a message, update any necessary options, * and return the corresponding $action_performed. */ function do_POST_actions($facebook) { global $fpf_name, $fpf_version, $fpf_homepage; global $opt_thumb_path, $opt_last_uid_search; global $opt_fb_sess_key, $opt_fb_sess_sec, $opt_fb_sess_uid, $opt_fb_sess_uname; if (isset($_POST['options_updated'])) { $action_performed = JGALLERY_ACTION_UPDATE; update_option($opt_thumb_path, $_POST[$opt_thumb_path]); ?> <div class="updated"><p><strong><?php echo 'Options saved.'; ?> </strong></p></div><?php } else { if (isset($_POST[$opt_last_uid_search])) { $action_performed = JGALLERY_ACTION_SEARCH; update_option($opt_last_uid_search, $_POST[$opt_last_uid_search]); ?> <div class="updated"><p><strong><?php echo 'Album search completed.'; ?> </strong></p></div><?php } else { if (isset($_POST['fetch_pages'])) { $action_performed = JGALLERY_ACTION_FETCHPAGES; } else { if (isset($_POST['fetch_posts'])) { $action_performed = JGALLERY_ACTION_FETCHPOSTS; } else { if (isset($_POST['save-facebook-session'])) { //We're connecting the useraccount to facebook, and the user just did STEP 2 //We need to use the connection token to create a new session and save it, //which we'll use from now on to reconnect as the authenticated user. //See important note at the top of the file for why this works (it's an infinite session) $token = $_POST['save-facebook-session']; try { $new_session = $facebook->api_client->auth_getSession($token); } catch (Exception $e) { $new_session = 0; } $errorMsg = 0; if (!$new_session) { $errorMsg = "Failed to get an authenticated session."; } if (!$new_session['secret']) { $errorMsg = "Failed to get a session secret. See <a href=\"" . $fpf_homepage . "#faq3\">FAQ3</a>."; } if ($new_session['expires'] > 0) { $errorMsg = "Failed to generate an infinite session."; } //Success! Save the key, secret, userID, and username if (!$errorMsg) { $user = $facebook->api_client->users_getInfo($new_session['uid'], array('name')); update_option($opt_fb_sess_key, $new_session['session_key']); update_option($opt_fb_sess_sec, $new_session['secret']); update_option($opt_fb_sess_uid, $new_session['uid']); update_option($opt_fb_sess_uname, $user[0]['name']); fpf_auth($fpf_name, $fpf_version, 2, "SET: " . $new_session['uid'] . " (" . $user[0]['name'] . ")"); ?> <div class="updated"><p><strong><?php echo 'Facebook Session Saved. (UID: ' . $new_session['uid'] . ')'; ?> </strong></p></div><?php } else { update_option($opt_fb_sess_key, 0); update_option($opt_fb_sess_sec, 0); update_option($opt_fb_sess_uid, 0); update_option($opt_fb_sess_uname, 0); ?> <div class="updated"><p><strong><?php echo 'An error occurred while linking with Facebook: ' . $errorMsg; ?> </strong></p></div><?php } } } } } } return $action_performed; }