function template()
 {
     global $Silas, $wp_query;
     $current = $wp_query->get_queried_object();
     if ($current->post_title) {
         $photoAlbumTitle = $current->post_title;
     } else {
         $photoAlbumTitle = 'Photo Gallery';
     }
     if (!isset($_SERVER['_TANTAN_FLICKR_REQUEST_URI'])) {
         return;
     }
     $auth_token = get_option('silas_flickr_token');
     $photoTemplate = 'error.html';
     if ($auth_token) {
         require_once dirname(__FILE__) . '/lib.flickr.php';
         $flickr = new TanTanFlickr();
         $flickr->setToken($auth_token);
         $flickr->setOption(array('hidePrivatePhotos' => get_option('silas_flickr_hideprivate')));
         $parts = explode('/', substr($_SERVER['_TANTAN_FLICKR_REQUEST_URI'], strlen(TANTAN_FLICKR_BASEURL)));
         $request = array();
         $title = '';
         $i = 0;
         if (isset($_POST['refreshCache']) && $_POST['refreshCache']) {
             $flickr->startClearCache();
         }
         while ($i < count($parts)) {
             // figgure out the album and/or photo to show
             if (($parts[$i] == 'tags' || $parts[$i] == 'album' || $parts[$i] == 'group' || $parts[$i] == 'photo') && !ereg(".html\$", $parts[$i])) {
                 $request[$parts[$i]] = $parts[$i + 1];
             }
             $i += 1;
         }
         $per_page = 30;
         // max specified by the Flickr API TOS
         $page = 1;
         if (isset($_GET['page'])) {
             $page = (int) $_GET['page'];
         }
         if ($request['photo']) {
             if ($request['album']) {
                 // within context of album
                 $album = $flickr->getAlbum($request['album']);
                 $context = $flickr->getContext($request['photo'], $request['album']);
             } elseif ($request['group']) {
                 // within context of group
                 $group = $flickr->getGroup($request['group']);
                 $context = $flickr->getContextByGroup($request['photo'], $request['group']);
                 if ($photo['visibility']['ispublic'] <= 0) {
                     $photo = array();
                     // sorry, no private photos
                 }
                 add_action('wp_head', array(&$this, 'meta_noindex'));
             } else {
                 // just an individual photo
                 $context = $flickr->getContext($request['photo']);
             }
             $photo = $flickr->getPhoto($request['photo']);
             if ($flickr->getOption('hidePrivatePhotos') && $photo['visibility']['ispublic'] <= 0) {
                 $photo = array();
             } else {
                 $sizes = $flickr->getPhotoSizes($request['photo']);
                 $comments = $flickr->getComments($request['photo']);
             }
             $user = $flickr->auth_checkToken();
             $nsid = $user['user']['nsid'];
             if (count($photo['urls'])) {
                 $photourl = array_pop($photo['urls']);
             } else {
                 $photourl = 'http://www.flickr.com/photos/' . $photo['owner']['nsid'] . '/' . $photo['id'] . '/';
             }
             if ($request['group'] && !TANTAN_FLICKR_DISPLAYGROUPS) {
                 $message = __("Sorry, this feature is not enabled.", 'tantan-flickr');
                 $photoTemplate = 'error.html';
             } elseif ($photo['owner']['nsid'] != $nsid) {
                 if ((int) $photo['license'] > 0 && $photo['usage']['canblog']) {
                     $owner = $flickr->people_getInfo($photo['owner']['nsid']);
                     $photoTemplate = 'photoalbum-photo.html';
                 } else {
                     $message = __("This photo is not available. ", 'tantan-flickr');
                     if (count($photo['urls'])) {
                         $message .= '<a href="' . array_pop($photo['urls']) . '">' . __('View this photo at Flickr', 'tantan-flickr') . '</a>';
                     }
                     $photoTemplate = 'error.html';
                 }
             } else {
                 if ($photo['media'] == 'video') {
                     $photoTemplate = 'photoalbum-video.html';
                 } else {
                     $photoTemplate = 'photoalbum-photo.html';
                 }
             }
         } elseif ($request['album']) {
             $album = $flickr->getAlbum($request['album']);
             $user = $flickr->auth_checkToken();
             $nsid = $user['user']['nsid'];
             if ($album['owner'] != $nsid) {
                 $message = __("This album is not available. ", 'tantan-flickr') . '<a href="http://www.flickr.com/photos/' . $album['owner'] . '/sets/' . $album['id'] . '/">' . __('View this album on Flickr', 'tantan-flickr') . '</a>';
                 $photoTemplate = 'error.html';
             } elseif (isset($request['tags'])) {
                 $message = __("Sorry, this feature is not supported", 'tantan-flickr');
                 $photoTemplate = 'error.html';
                 if ($request['tags']) {
                 } else {
                     // return popular tags for an album
                     $message = __("Sorry, this feature is not supported", 'tantan-flickr');
                     $photoTemplate = 'error.html';
                 }
             } else {
                 $photos = $flickr->getPhotos($request['album'], NULL, $per_page, $page);
                 if (isset($photos[$album['primary']])) {
                     $primary = $photos[$album['primary']];
                 } else {
                     $primary = $flickr->getPhoto($album['primary']);
                     $primary['sizes'] = $flickr->getPhotoSizes($album['primary']);
                 }
                 $photoTemplate = 'photoalbum-album.html';
             }
         } elseif ($request['group']) {
             if (!TANTAN_FLICKR_DISPLAYGROUPS) {
                 $message = __("Sorry, this feature is not enabled.", 'tantan-flickr');
                 $photoTemplate = 'error.html';
             } else {
                 $group = $flickr->getGroup($request['group']);
                 if (isset($request['tags'])) {
                     if ($request['tags']) {
                         $photos = $flickr->getPhotosByGroup($request['group'], $request['tags'], NULL, NULL, $per_page, $page);
                         $photoTemplate = 'photoalbum-tags-group.html';
                     } else {
                         // return popular tags for a group
                         $message = __("Sorry, this feature is not supported", 'tantan-flickr');
                         $photoTemplate = 'error.html';
                     }
                 } else {
                     $photos = $flickr->getPhotosByGroup($request['group'], NULL, NULL, NULL, $per_page, $page);
                     $photoTemplate = 'photoalbum-group.html';
                 }
             }
         } elseif (isset($request['tags'])) {
             if ($request['tags']) {
                 $photos = $flickr->getPhotosByTags($request['tags']);
                 //$related = $flickr->getRelatedTags($request['tags']);
                 $photoTemplate = 'photoalbum-tags.html';
             } else {
                 $tags = $flickr->getTags();
                 $photoTemplate = 'photoalbum-tagcloud.html';
             }
         } else {
             $title = $photoAlbumTitle;
             $albums = $flickr->manualSort($flickr->getAlbums(), get_option('silas_flickr_albumorder'));
             $hideAlbums = get_option('silas_flickr_hidealbums');
             // remove albums marked as hidden
             if (is_array($hideAlbums)) {
                 foreach ($albums as $k => $a) {
                     if (in_array($a['id'], $hideAlbums)) {
                         unset($albums[$k]);
                     }
                 }
             }
             $photoTemplate = 'photoalbum-albums-index.html';
         }
         $this->request = $request;
         if ($request['group']) {
             add_action('wp_head', array(&$this, 'meta_noindex'));
         }
         add_action('wp_head', array(&$this, 'rss_feed'));
         add_action('wp_head', array(&$this, 'header'));
         add_action('wp_footer', array(&$this, 'footer'));
         $errorMessages = $flickr->getErrorMsgs();
         if (is_object($Silas)) {
             if ($request['photo']) {
                 if ($album) {
                     $Silas->addBreadCrumb($photoAlbumTitle, '../../../../');
                     $Silas->addBreadCrumb($album['title'], '../../' . $album['pagename']);
                 } else {
                     $Silas->addBreadCrumb($photoAlbumTitle, '../../');
                 }
                 $Silas->setPageTitle($photo['title']);
             } elseif ($request['album']) {
                 $Silas->addBreadCrumb($photoAlbumTitle, '../../');
                 $Silas->setPageTitle($album['title']);
             } else {
                 $Silas->setPageTitle($photoAlbumTitle);
             }
         } else {
             if ($request['photo']) {
                 $this->setPageTitle($photo['title']);
             } elseif ($request['album']) {
                 $this->setPageTitle($album['title']);
             } else {
                 $this->setPageTitle($photoAlbumTitle);
             }
             add_filter('wp_title', array(&$this, 'wp_title'));
         }
     } else {
         $message = __("The photo album has not been configured.", 'tantan-flickr');
     }
     if (file_exists(TEMPLATEPATH . '/photoalbum-resources.php')) {
         require_once TEMPLATEPATH . '/photoalbum-resources.php';
     } else {
         require_once dirname(__FILE__) . '/../templates/photoalbum-resources.php';
     }
     if (file_exists(TEMPLATEPATH . '/photoalbum-index.php')) {
         include TEMPLATEPATH . '/photoalbum-index.php';
     } elseif (file_exists(dirname(__FILE__) . '/../templates/photoalbum-index.php')) {
         include dirname(__FILE__) . '/../templates/photoalbum-index.php';
     } elseif (file_exists(TEMPLATEPATH . '/photos.php')) {
         include TEMPLATEPATH . '/photos.php';
     } else {
         include dirname(__FILE__) . '/view.php';
     }
     if (count($errorMessages) > 0) {
         echo "<!-- \n" . $errorMessages . "\n-->\n";
     }
     $flickr->clearCacheStale();
     exit;
 }
 function admin()
 {
     if (TANTAN_FLICKR_CACHEMODE == "fs" && !is_writable(dirname(__FILE__) . '/flickr-cache/')) {
         echo "<div class='wrap'>\n            <h2>Permissions Error</h2>\n            <p>This plugin requires that the directory <strong>" . dirname(__FILE__) . "/flickr-cache/</strong> be writable by the web server.</p> \n            <p>You may want to try to manually delete this flickr-cache directory, and have the plugin try to create it to see if that will fix this problem.</p>\n            <p>Otherwise, please contact your server administrator to ensure the proper permissions are set for this directory. </p>\n            </div>\n            ";
         return;
     } elseif (!get_settings('permalink_structure')) {
         $error = "In order to view your photo album, your <a href='options-permalink.php'>WordPress permalinks</a> need to be set to something other than <em>Default</em>.";
         /*
                 } elseif (!function_exists('curl_init')) {
                     $error = "You do not have the required libraries to use this plugin. The PHP library <a href='http://us2.php.net/curl'>libcurl</a> needs to be installed on your server.";
         */
     } elseif (@constant('DB_CHARSET') === null) {
         $error = "Your database character encoding does not seem to be set. It is <strong>strongly</strong> recommended that you set it to <em>utf8</em> for maximum compatibility. <a href=\"http://codex.wordpress.org/Editing_wp-config.php#Database_character_set\">Instructions are available here.</a> " . "Once you have set your database encoding, please deactivate and reactivate this plugin.";
     }
     if ($_POST['action'] == 'savekey') {
         update_option('silas_flickr_apikey', $_POST['flickr_apikey']);
         update_option('silas_flickr_sharedsecret', $_POST['flickr_sharedsecret']);
         $message = "Saved API Key.";
     } elseif ($_POST['action'] == 'resetkey') {
         update_option('silas_flickr_apikey', false);
         update_option('silas_flickr_sharedsecret', false);
         $message = "API Key reset";
     }
     $flickr_apikey = get_option('silas_flickr_apikey');
     $flickr_sharedsecret = get_option('silas_flickr_sharedsecret');
     if ($flickr_apikey && $flickr_sharedsecret) {
         require_once dirname(__FILE__) . '/lib.flickr.php';
         $flickr = new TanTanFlickr();
         if ($flickr->cache == 'db') {
             global $wpdb;
             $wpdb->hide_errors();
             $charset_collate = '';
             if (method_exists($wpdb, 'supports_collation') && $wpdb->supports_collation() || version_compare(mysql_get_server_info($wpdb->dbh), '4.1.0', '>=')) {
                 if (!empty($wpdb->charset)) {
                     $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
                 }
                 if (!empty($wpdb->collate)) {
                     $charset_collate .= " COLLATE {$wpdb->collate}";
                 }
             }
             $wpdb->query("\n                CREATE TABLE IF NOT EXISTS `{$flickr->cache_table}` (\n                    `command` CHAR( 255 ) NOT NULL ,\n                    `request` CHAR( 35 ) NOT NULL ,\n                    `response` MEDIUMTEXT NOT NULL ,\n                    `created` DATETIME NOT NULL ,\n                    `expiration` DATETIME NOT NULL ,\n                    INDEX ( `request` ),\n\t\t\t\t\tINDEX ( `command` )\n                ) {$charset_collate}");
             //
             $wpdb->query("CREATE INDEX commandCreated on {$flickr->cache_table}(command, created)");
             $wpdb->show_errors();
             if (function_exists('wp_schedule_event')) {
                 wp_clear_scheduled_hook('tantan_flickr_clear_cache_event');
                 wp_schedule_event(time(), 'daily', 'tantan_flickr_clear_cache_event');
             }
         }
         if ($_POST['action'] == 'save') {
             $flickr->clearCache();
             $token = $flickr->auth_getToken($_POST['frob']);
             if ($token) {
                 update_option('silas_flickr_token', $token);
             } else {
                 $error = $flickr->getErrorMsg();
             }
         } elseif ($_POST['action'] == 'logout') {
             update_option('silas_flickr_token', '');
             $flickr->clearCache();
         } elseif ($_POST['action'] == 'savebase') {
             $url = parse_url(get_bloginfo('siteurl'));
             $baseurl = $url['path'] . '/' . $_POST['baseurl'];
             if (!ereg('.*/$', $baseurl)) {
                 $baseurl .= '/';
             }
             if ($_POST['synidcateoff'] || strlen($_POST['baseurl']) <= 0) {
                 $baseurl = false;
             }
             update_option('silas_flickr_baseurl_pre', $url['path'] . '/');
             update_option('silas_flickr_baseurl', $baseurl);
             update_option('silas_flickr_hideprivate', $_POST['hideprivate']);
             update_option('silas_flickr_showbadge', $_POST['showbadge']);
             update_option('silas_flickr_linkoptions', $_POST['linkoptions']);
         }
         $auth_token = get_option('silas_flickr_token');
         $baseurl = get_option('silas_flickr_baseurl');
         $baseurl_pre = get_option('silas_flickr_baseurl_pre');
         $hideprivate = get_option('silas_flickr_hideprivate');
         $showbadge = get_option('silas_flickr_showbadge');
         $linkoptions = get_option('silas_flickr_linkoptions');
         $hideAlbums = get_option('silas_flickr_hidealbums');
         $hideGroups = get_option('silas_flickr_hidegroups');
         $groupOrder = get_option('silas_flickr_grouporder');
         $albumOrder = get_option('silas_flickr_albumorder');
         $flickrAuth = false;
         if (!$auth_token) {
             $flickr->clearCache();
             $frob = $flickr->getFrob();
             $error = $flickr->getErrorMsg();
             $flickrAuth = false;
         } else {
             $flickr->setToken($auth_token);
             $flickr->setOption(array('hidePrivatePhotos' => get_option('silas_flickr_hideprivate')));
             $user = $flickr->auth_checkToken();
             if (!$user) {
                 // get a new frob and try to re-authenticate
                 $error = $flickr->getErrorMsg();
                 update_option('silas_flickr_token', '');
                 $flickr->setToken('');
                 $frob = $flickr->getFrob();
             } else {
                 $flickrAuth = true;
                 $flickr->setUser($user);
                 update_option('silas_flickr_user', $user);
             }
         }
     }
     // apikey check
     if ($flickrAuth) {
         // passed authentication
         if ($_POST['action'] == 'clearcache') {
             if ($_POST['album'] == 'all') {
                 if ($flickr->clearCache()) {
                     $message = "Successfully cleared the cache.";
                 } else {
                     $error = "Cache clear failed. Try manually deleting the 'flickr-cache' directory or the silas_flickr_cache database table.";
                 }
             } else {
                 $flickr->startClearCache();
                 $albums = $flickr->getAlbums();
                 $photos = $flickr->getPhotos($_POST['album']);
                 $flickr->doneClearCache();
                 $message = "Refreshed " . count($photos) . " photos in " . $albums[$_POST['album']]['title'] . ".";
             }
         } elseif ($_POST['action'] == 'savealbumsettings') {
             if (!is_array($_POST['hideAlbum'])) {
                 $_POST['hideAlbum'] = array();
             }
             update_option('silas_flickr_hidealbums', $_POST['hideAlbum']);
             $hideAlbums = $_POST['hideAlbum'];
             if (!is_array($_POST['albumOrder'])) {
                 $_POST['albumOrder'] = array();
             }
             asort($_POST['albumOrder']);
             update_option('silas_flickr_albumorder', $_POST['albumOrder']);
             $albumOrder = $_POST['albumOrder'];
             $message .= "Saved album settings. ";
             if (is_array($_POST['clearAlbum'])) {
                 $flickr->startClearCache();
                 foreach ($_POST['clearAlbum'] as $album_id) {
                     $photos = $flickr->getPhotos($album_id);
                 }
                 $flickr->doneClearCache();
                 $message .= "Cleared cache for selected albums. ";
             }
         } elseif ($_POST['action'] == 'savegroupsettings') {
             if (!is_array($_POST['hideGroup'])) {
                 $_POST['hideGroup'] = array();
             }
             update_option('silas_flickr_hidegroups', $_POST['hideGroup']);
             $hideGroups = $_POST['hideGroup'];
             if (!is_array($_POST['groupOrder'])) {
                 $_POST['groupOrder'] = array();
             }
             asort($_POST['groupOrder']);
             update_option('silas_flickr_grouporder', $_POST['groupOrder']);
             $groupOrder = $_POST['groupOrder'];
             $message .= "Saved group settings. ";
             if (is_array($_POST['clearGroup'])) {
                 $flickr->startClearCache();
                 foreach ($_POST['clearGroup'] as $group_id) {
                     $photos = $flickr->getPhotosByGroup($group_id);
                 }
                 $flickr->doneClearCache();
                 $message .= "Cleared cache for selected groups. ";
             }
         }
     }
     include dirname(__FILE__) . '/admin-options.html';
 }