$Webcit_PrefMsgid = 0;
if ($num_msgs > 0) {
    foreach ($msgs as $msgnum) {
        print_r($msgnum);
        // Fetch the message from the server
        list($ok, $response, $fields) = ctdl_fetch_message($msgnum);
        // Bail out gracefully if the message isn't there.
        if (!$ok) {
            echo "Error: " . $response . "\n";
            return false;
        }
        if (isset($fields['part'])) {
            $parts = explode('|', $fields['part']);
            print_r($parts);
            if ($parts[4] == "text/x-vcard") {
                list($size, $OldVcard) = download_attachment($msgnum, $result[2]);
            } else {
                ctdl_dele($msgnum);
            }
        } else {
            if ($fields['subj'] == "__ WebCit Preferences __") {
                $Webcit_PrefMsgid = $msgnum;
                $Webcit_Preferences = $fields;
            }
        }
    }
}
"begin:vcard\nn:Surename;CName;mitte;Mrs;IV\ntitle:master\nfn:CName Surename\norg:citadel.org\nadr:blarg;Road To nowhere ;10;Metropolis;NRW;12345;Country\ntel;home:888888888\ntel;work:99999999999\nemail;internet:user@samplecitadel.org\nemail;internet:me@samplecitadel.org\nemail;internet:myself@samplecitadel.org\nemail;internet:i@samplecitadel.org\nFBURL;PREF:http://samplecitadel.org/Cname_Lastname.vfb\nUID:Citadel vCard: personal card for Cname Lastname at samplecitadel.org\nend:vcard\n";
$entarray = array();
$entarray['post'] = 1;
$entarray['format_type'] = FMT_RFC822;
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//  Author: Jenny Murphy - http://google.com/+JennyMurphy
// Verify that the parameters we want are there
if (!isset($_GET['timeline_item_id']) || !isset($_GET['attachment_id'])) {
    http_response_code(400);
    exit;
}
require_once 'config.php';
require_once 'mirror-client.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php';
require_once 'util.php';
$client = get_google_api_client();
// Authenticate if we're not already
if (!isset($_SESSION['userid']) || get_credentials($_SESSION['userid']) == null) {
    header('Location: ' . $base_url . '/oauth2callback.php');
    exit;
} else {
    $client->setAccessToken(get_credentials($_SESSION['userid']));
}
// A glass service for interacting with the Mirror API
$mirror_service = new Google_MirrorService($client);
// fetch the metadata
$attachment = $mirror_service->timeline_attachments->get($_GET['timeline_item_id'], $_GET['attachment_id']);
// set the content type header
header('Content-type: ' . $attachment->getContentType());
// echo the bytes
echo download_attachment($_GET['timeline_item_id'], $attachment);
function save_header_images($imageType, $fileMasterId, $fileName, $articleId)
{
    global $articleErrors;
    if ($imageType == "header" || $imageType == "promoLarge") {
        $imageDownloadUrlPath = $fileMasterId . "+w640/" . $fileName;
    } elseif ($imageType == "homepage") {
        $imageDownloadUrlPath = $fileMasterId . "+w288+h140+re0+cr1+ar0/" . $fileName;
    } else {
        $imageDownloadUrlPath = $fileMasterId . "+w1024/" . $fileName;
    }
    $imageDownloadUrl = "http://image.adam.automotive.com/f/" . $imageDownloadUrlPath;
    $file_name = $fileMasterId . "_" . $imageType . "_" . $fileName;
    $attachmentID = download_attachment($imageDownloadUrl, $file_name, $articleId);
    if (empty($attachmentID)) {
        $articleErrors[] = "ERROR: Download attachment failed. url  - {$imageDownloadUrl} articleId - {$articleId} entityType - HeaderImages  {$imageType} <br>";
    }
    if ($attachmentID[0]) {
        update_post_meta($articleId, $imageType, $attachmentID[0]);
        if ($imageType == "header") {
            update_post_meta($articleId, "_thumbnail_id", $attachmentID[0]);
        } elseif ($imageType == "promoLarge") {
            update_post_meta($articleId, "post_flipper-image_thumbnail_id", $attachmentID[0]);
        } elseif ($imageType == "homepage") {
            update_post_meta($articleId, "post_home-image_thumbnail_id", $attachmentID[0]);
        }
        return $attachmentID[0];
    } else {
        return false;
    }
}
function import_attachment_by_articles()
{
    wp_reset_postdata();
    query_posts(array('post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'DESC', 'posts_per_page' => '-1'));
    if (have_posts()) {
        $i = 1;
        while (have_posts()) {
            the_post();
            $post_meta = get_post_meta(get_the_ID());
            if (!empty($post_meta['headerImages'])) {
                $post_meta_headerImages = unserialize($post_meta['headerImages'][0]);
                foreach ($post_meta_headerImages as $key => $header_images) {
                    $metaPostID = $media_item = null;
                    echo get_the_ID() . "--" . $header_images . "--";
                    $metaPostID = get_post_id_by_meta_key_and_value('post_id', get_the_ID());
                    $media_item = get_permalink($metaPostID);
                    if (!empty($metaPostID) && !empty($media_item)) {
                        echo "{$metaPostID} available <br>";
                        set_post_thumbnail(get_the_ID(), $metaPostID);
                        update_post_meta(get_the_ID(), 'post_promo-image_thumbnail_id', $metaPostID);
                        continue;
                    } else {
                        //print_r($metaPostID);
                        echo " {$metaPostID} not attachment {$media_item} <br>";
                    }
                    $attachment_id = null;
                    if ($key == "header" || $key == "promoSmall" || $key == "homepage") {
                        add_image_size('promo-small', 288, 9999);
                    } elseif ($key == "promoLarge" || $key == 'imagegroup') {
                        add_image_size('promo-large', 650, 317);
                        add_image_size('promo-small', 288, 9999);
                    }
                    $filename = get_attachment_details_from_table($key, get_the_ID(), $header_images);
                    if (empty($filename)) {
                        $filename = $header_images . '-' . $key . '.jpg';
                    }
                    $url = "http://image.adam.automotive.com/f/{$header_images}/{$filename}";
                    $attachment_id = download_attachment($url, $filename, get_the_ID());
                    if (!empty($attachment_id)) {
                        update_post_meta($attachment_id, 'post_id', get_the_ID());
                        update_post_meta($attachment_id, 'imageType', $key);
                        //if($key=="header"){
                        set_post_thumbnail(get_the_ID(), $attachment_id);
                        update_post_meta(get_the_ID(), 'post_promo-image_thumbnail_id', $attachment_id);
                        //}
                        echo "Success";
                    } else {
                        echo "Fail";
                    }
                    echo "<br>";
                }
            }
            //    echo "<pre>";
            //    print_r($post_meta['headerImages']);
            //    echo "</pre>";
            //    echo "<br>";
        }
        wp_reset_postdata();
    }
}