Skip to content

Instantly share code, notes, and snippets.

@loe
Created September 10, 2008 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loe/9847 to your computer and use it in GitHub Desktop.
Save loe/9847 to your computer and use it in GitHub Desktop.
<?php
$gallery_basedir = "/var/www/gallery.andrewloe.com/";
// Gallery requirements
if (!isset($gallery_basedir))
{
$gallery_basedir = './';
}
require_once($gallery_basedir . "init.inc");
require_once($gallery_basedir . 'embed.php');
// Mail server information
$emailUser = "username";
$emailPass = "password";
// Gallery information
$albumIdnumber = 13680; // Album to add items to
$externalownerId = 1; // Owner id of the items
// Connect to mailbox
print("Connecting to mailbox.\n");
$mbox = imap_open("{localhost:995/pop3/ssl/novalidate-cert}INBOX", $emailUser, $emailPass);
if (!imap_errors()) {
// Get the number of messages
$numOfMessages = imap_num_msg($mbox);
// Loop through each email
print("Looping through messages.\n");
for ($i = 1; $i <= $numOfMessages; $i++) {
// get the message structure
$struct = imap_fetchstructure($mbox, $i);
$numOfParts = count($struct->parts);
// Loop through the message parts
print("Looping through message parts.\n");
for ($k = 0; $k < $numOfParts; $k++) {
if ($struct->parts[$k]->type == 5) { // Its an image!
$filename = "Moblog-" . date("d.m.y-H.i.s") . "-" . $k . ".jpg";
// capture the body
$body = imap_fetchbody($mbox, $i, 1);
if ($struct->parts[0]->encoding == 3) {
$body = base64_decode($body);
}
if ($body == "") {
$title = $filename;
}
else {
$title = $body;
}
// capture the attachments
$data = imap_fetchbody($mbox, $i, $k + 1);
$tmpfname = tempnam("/tmp", "moblog");
$handle = fopen($tmpfname, "w");
fwrite($handle, imap_base64($data));
fclose($handle);
// Call to Gallery
print("Adding the image to gallery.\n");
addToGallery($albumIdnumber, $externalownerId, $filename, $filename, $title, $filename, $body, "image/jpeg");
// unlink($tmpfname);
}
}
// Delete the message
imap_delete($mbox, $i);
}
}
// Delete messages
imap_expunge($mbox);
// Disconnect
imap_close($mbox);
// Interfaces with Gallery to add the images.
function addToGallery($albumId, $ownerId, $fileName, $itemName, $title, $summary, $description, $mimeType) {
// Initiate
print("Initiate.\n");
list ($ret) = GalleryEmbed::init(array("g2Uri" => "/", "embedUri" => "/main.php", "activeUserId" => $ownerId, "fullInit" => true));
// print_r($ret);
// Lock Album
print("Lock the album.\n");
list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($albumId);
// print_r($ret);
// Add Item To Album
print("Add the item.\n");
print_r($fileName);
print("\n");
list ($ret, $newitem) = GalleryCoreApi::addItemToAlbum($fileName, $fileName, $title, $summary, $description, $mimeType, $albumId);
print_r($ret);
// Release Album
print("Release the item.\n");
list ($ret) = GalleryCoreApi::releaseLocks($lockId);
// print_r($ret);
// Finish Up
print("Finish up.\n");
list ($ret) = GalleryEmbed::done();
// print_r($ret);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment