예제 #1
0
 function update_albums()
 {
     global $wpdb;
     $this->increase_time_limit();
     // reset album import progress
     $this->update_progress(true);
     // if this is the first import then reset the order at the end to make the newest on top
     $reset_order = count(fb_get_album()) > 0 ? false : true;
     // get albums for each user from Facebook
     $fb_albums = array();
     $fb_photos = array();
     foreach ($this->sessions as $key => $session) {
         // setup general info
         $uid = $session['uid'];
         $this->select_session($uid);
         // get all albums
         $result = $this->facebook->photos_getAlbums($uid, null);
         if (!is_array($result)) {
             // the current user has no photos so move on
             continue;
         }
         $fb_albums = array_merge($fb_albums, $result);
         $this->update_progress();
         // get all photos - queries are limited to 5,000 items per query so we need to split them up
         // technically this could still error out if the user 100+ photos per album, in that case
         // the following number would need to change to 25 or lower
         $albums_per_query = 50;
         $i = 0;
         $album_offset = 0;
         while ($album_offset < count($result)) {
             $photos = $this->facebook->fql_query("SELECT pid, aid, owner, src, src_big, src_small, link, caption, created FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner = '{$uid}' LIMIT {$albums_per_query} OFFSET {$album_offset})");
             $fb_photos = array_merge($fb_photos, (array) $photos);
             $album_offset = $albums_per_query * ++$i;
         }
         $this->update_progress();
         // get photos of user
         $fb_user_photos = $this->facebook->photos_get($uid, null, null);
         if ($fb_user_photos) {
             foreach ($fb_user_photos as $k => $v) {
                 $fb_user_photos[$k]['aid'] = $uid;
             }
             $fb_photos = array_merge($fb_photos, (array) $fb_user_photos);
             $fb_albums[] = array('aid' => $uid, 'cover_pid' => $fb_user_photos[0]['pid'], 'owner' => $uid, 'name' => 'Photos of ' . (count($this->sessions) > 1 ? $session['name'] : 'Me'), 'created' => time(), 'modified' => time(), 'description' => '', 'location' => '', 'link' => "http://www.facebook.com/photo_search.php?id={$uid}", 'size' => count($fb_user_photos));
         }
         if (!$fb_albums || $this->facebook->error_code) {
             $this->msg = 'Fotobook encountered an error while retrieving your photos. [Error #' . $this->facebook->error_code . ']';
             return false;
         }
     }
     // put all the albums in an array with the aid as the key
     $albums = fb_get_album();
     if ($albums) {
         foreach ($albums as $album) {
             $wp_albums[$album['aid']] = $album;
         }
     }
     // go through all the facebook albums see which ones need to be added
     foreach ($fb_albums as $fb_album) {
         $wp_album = isset($wp_albums[$fb_album['aid']]) ? $wp_albums[$fb_album['aid']] : false;
         $album_data = array('cover_pid' => $fb_album['cover_pid'], 'owner' => $fb_album['owner'], 'name' => $fb_album['name'], 'created' => !empty($fb_album['created']) ? date('Y-m-d H:i:s', $fb_album['created']) : '', 'modified' => !empty($fb_album['modified']) ? date('Y-m-d H:i:s', $fb_album['modified']) : '', 'description' => $fb_album['description'], 'location' => $fb_album['location'], 'link' => $fb_album['link'], 'size' => $fb_album['size']);
         // if it already exists, just update it with any updated info
         if ($wp_album) {
             // check to make sure the page exists and update the name of the page if needed
             if (fb_page_exists($wp_album['page_id'])) {
                 $album_data['page_id'] = $wp_album['page_id'];
                 if ($fb_album['name'] != $wp_album['name']) {
                     fb_update_page($wp_album['page_id'], $fb_album['name']);
                 }
             } else {
                 $album_data['page_id'] = fb_add_page($fb_album['name']);
             }
             $wpdb->update(FB_ALBUM_TABLE, $album_data, array('aid' => $fb_album['aid']));
         } else {
             $album_data['aid'] = $fb_album['aid'];
             $album_data['page_id'] = fb_add_page($fb_album['name']);
             $album_data['hidden'] = 0;
             $album_data['ordinal'] = fb_get_next_ordinal();
             $wpdb->insert(FB_ALBUM_TABLE, $album_data);
         }
     }
     // update the photos
     $wpdb->query('DELETE FROM ' . FB_PHOTO_TABLE);
     $ordinal = 1;
     foreach ($fb_photos as $photo) {
         if ($last_aid !== $photo['aid']) {
             // reset ordinal if we're on a new album now
             $ordinal = 1;
         }
         $album_data = array('pid' => $photo['pid'], 'aid' => $photo['aid'], 'owner' => $photo['owner'], 'src' => $photo['src'], 'src_big' => $photo['src_big'], 'src_small' => $photo['src_small'], 'link' => $photo['link'], 'caption' => $photo['caption'], 'created' => date('Y-m-d H:i:s', $photo['created']), 'ordinal' => $ordinal);
         $wpdb->insert(FB_PHOTO_TABLE, $album_data);
         // handle ordinal
         $last_aid = $photo['aid'];
         $ordinal++;
     }
     // put IDs of all albums in an array
     foreach ($fb_albums as $fb_album) {
         $album_ids[] = $fb_album['aid'];
     }
     $wp_albums = fb_get_album();
     if (count($wp_albums) > 0) {
         // delete albums that have been removed off of Facebook
         foreach ($wp_albums as $fb_album) {
             if (!@in_array($fb_album['aid'], $album_ids)) {
                 fb_delete_page($fb_album['page_id']);
                 $wpdb->query('DELETE FROM `' . FB_ALBUM_TABLE . "` WHERE `aid` = '" . $fb_album['aid'] . "'");
             }
         }
         // delete superfluous pages
         foreach ($wp_albums as $fb_album) {
             $album_pages[] = $fb_album['page_id'];
         }
         $wp_pages = $wpdb->get_results('SELECT `ID` FROM `' . FB_POSTS_TABLE . "` WHERE `post_parent` = '" . get_option('fb_albums_page') . "'", ARRAY_A);
         foreach ($wp_pages as $page) {
             if (!in_array($page['ID'], $album_pages)) {
                 fb_delete_page($page['ID']);
             }
         }
     }
     // now reset the order if needed
     if ($reset_order) {
         fb_reset_album_order();
     }
     if (!$this->msg) {
         $this->msg = 'Albums imported successfully.';
     }
     $this->update_progress(true);
 }
예제 #2
0
 function update_albums()
 {
     global $wpdb;
     $this->increase_time_limit();
     // reset album import progress
     $this->update_progress(true);
     // if this is the first import then reset the order at the end to make the newest on top
     $reset_order = count(fb_get_album()) > 0 ? false : true;
     // get albums for each user from Facebook
     $fb_albums = array();
     $fb_photos = array();
     foreach ($this->sessions as $key => $session) {
         // setup general info
         $uid = $session['uid'];
         $this->select_session($uid);
         try {
             //START CHANGES HERE
             if (is_numeric($session['gid'])) {
                 //groups don't have albums, just photos. Here we get all the photos and fake an album
                 $gid = $session['gid'];
                 $group_photos = $this->facebook->fql_query("SELECT pid, aid, owner, src, src_big, src_small, link, caption, created FROM photo WHERE pid IN (SELECT pid FROM photo_tag WHERE subject={$gid})");
                 //$this->facebook->call_method('Photos.get', array('subj_id' => $gid)); -this is supposed to get all a groups photos but doesnt
                 if ($group_photos) {
                     foreach ($group_photos as $key => $value) {
                         $value['aid'] = $gid;
                         //this may cause some problems
                         $value['owner'] = $gid;
                         //this is fine, I think
                         array_push($fb_photos, $value);
                     }
                     //get group info
                     $group_info = $this->facebook->fql_query("SELECT gid,name FROM group WHERE gid={$gid}");
                     //fake an album
                     $fb_albums[] = array('aid' => $gid, 'cover_pid' => $group_photos[0]['pid'], 'owner' => $gid, 'name' => $group_info[0]['name'] . ' Group Photos', 'created' => time(), 'modified' => time(), 'description' => '', 'location' => '', 'link' => "http://www.facebook.com/group.php?v=photos&ref=ts&gid=" . $gid, 'size' => count($group_photos));
                 }
             } else {
                 // get all albums
                 $result = $this->facebook->photos_getAlbums($uid, null);
                 if (!is_array($result)) {
                     // the current user has no photos so move on
                     continue;
                 }
                 $fb_albums = array_merge($fb_albums, $result);
                 $this->update_progress();
             }
             // get all photos - queries are limited to 5,000 items per query so we need to split them up
             // technically this could still error out if the user 100+ photos per album, in that case
             // the following number would need to change to 25 or lower
             $albums_per_query = 50;
             $i = 0;
             $album_offset = 0;
             while ($album_offset < count($result)) {
                 $photos = $this->facebook->fql_query("SELECT pid, aid, owner, src, src_big, src_small, link, caption, created FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner = '{$uid}' LIMIT {$albums_per_query} OFFSET {$album_offset})");
                 $fb_photos = array_merge($fb_photos, (array) $photos);
                 $album_offset = $albums_per_query * ++$i;
             }
             $this->update_progress();
             // get photos of user
             $fb_user_photos = $this->facebook->photos_get($uid, null, null);
             if ($fb_user_photos) {
                 foreach ($fb_user_photos as $k => $v) {
                     $fb_user_photos[$k]['aid'] = $uid;
                 }
                 $fb_photos = array_merge($fb_photos, (array) $fb_user_photos);
                 $fb_albums[] = array('aid' => $uid, 'cover_pid' => $fb_user_photos[0]['pid'], 'owner' => $uid, 'name' => 'Photos of ' . (count($this->sessions) > 1 ? $session['name'] : 'Me'), 'created' => time(), 'modified' => time(), 'description' => '', 'location' => '', 'link' => "http://www.facebook.com/photo_search.php?id={$uid}", 'size' => count($fb_user_photos));
             }
         } catch (Exception $e) {
             if ($e->getCode() == 102) {
                 unset($this->sessions[$key]);
                 update_option('fb_facebook_session', $this->sessions);
                 $this->msg = "The account for {$session['name']} is no longer active.  Please add the account again from the settings panel.";
             } else {
                 $this->msg = "There was an error while retrieving your photos: {$e->getMessage()} [Error #{$e->getCode()}]";
             }
             return false;
         }
     }
     // put all the albums in an array with the aid as the key
     $albums = fb_get_album();
     if ($albums) {
         foreach ($albums as $album) {
             $wp_albums[$album['aid']] = $album;
         }
     }
     // go through all the facebook albums see which ones need to be added
     foreach ($fb_albums as $fb_album) {
         $wp_album = isset($wp_albums[$fb_album['aid']]) ? $wp_albums[$fb_album['aid']] : false;
         $album_data = array('cover_pid' => $fb_album['cover_pid'], 'owner' => $fb_album['owner'], 'name' => $fb_album['name'], 'created' => !empty($fb_album['created']) ? date('Y-m-d H:i:s', $fb_album['created']) : '', 'modified' => !empty($fb_album['modified']) ? date('Y-m-d H:i:s', $fb_album['modified']) : '', 'description' => $fb_album['description'], 'location' => $fb_album['location'], 'link' => $fb_album['link'], 'size' => $fb_album['size']);
         // if it already exists, just update it with any updated info
         if ($wp_album) {
             // check to make sure the page exists and update the name of the page if needed
             if (fb_page_exists($wp_album['page_id'])) {
                 $album_data['page_id'] = $wp_album['page_id'];
                 if ($fb_album['name'] != $wp_album['name']) {
                     fb_update_page($wp_album['page_id'], $fb_album['name']);
                 }
             } else {
                 $album_data['page_id'] = fb_add_page($fb_album['name']);
             }
             $wpdb->update(FB_ALBUM_TABLE, $album_data, array('aid' => $fb_album['aid']));
         } else {
             $album_data['aid'] = $fb_album['aid'];
             $album_data['page_id'] = fb_add_page($fb_album['name']);
             $album_data['hidden'] = 0;
             $album_data['ordinal'] = fb_get_next_ordinal();
             $wpdb->insert(FB_ALBUM_TABLE, $album_data);
         }
     }
     // update the photos
     $wpdb->query('DELETE FROM ' . FB_PHOTO_TABLE);
     $ordinal = 1;
     foreach ($fb_photos as $photo) {
         if ($last_aid !== $photo['aid']) {
             // reset ordinal if we're on a new album now
             $ordinal = 1;
         }
         $album_data = array('pid' => $photo['pid'], 'aid' => $photo['aid'], 'owner' => $photo['owner'], 'src' => $photo['src'], 'src_big' => $photo['src_big'], 'src_small' => $photo['src_small'], 'link' => $photo['link'], 'caption' => $photo['caption'], 'created' => date('Y-m-d H:i:s', $photo['created']), 'ordinal' => $ordinal);
         $wpdb->insert(FB_PHOTO_TABLE, $album_data);
         // handle ordinal
         $last_aid = $photo['aid'];
         $ordinal++;
     }
     // put IDs of all albums in an array
     foreach ($fb_albums as $fb_album) {
         $album_ids[] = $fb_album['aid'];
     }
     $wp_albums = fb_get_album();
     if (count($wp_albums) > 0) {
         // delete albums that have been removed off of Facebook
         foreach ($wp_albums as $fb_album) {
             if (!@in_array($fb_album['aid'], $album_ids)) {
                 fb_delete_page($fb_album['page_id']);
                 $wpdb->query('DELETE FROM `' . FB_ALBUM_TABLE . "` WHERE `aid` = '" . $fb_album['aid'] . "'");
             }
         }
         // delete superfluous pages
         foreach ($wp_albums as $fb_album) {
             $album_pages[] = $fb_album['page_id'];
         }
         $wp_pages = $wpdb->get_results('SELECT `ID` FROM `' . FB_POSTS_TABLE . "` WHERE `post_parent` = '" . get_option('fb_albums_page') . "'", ARRAY_A);
         foreach ($wp_pages as $page) {
             if (!in_array($page['ID'], $album_pages)) {
                 fb_delete_page($page['ID']);
             }
         }
     }
     // now reset the order if needed
     if ($reset_order) {
         fb_reset_album_order();
     }
     if (!$this->msg) {
         $this->msg = 'Albums imported successfully.';
     }
     $this->update_progress(true);
 }