Guest User

Untitled

a guest
Feb 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. <?php
  2.  
  3. $gallery_basedir = "/var/www/gallery.andrewloe.com/";
  4.  
  5. // Gallery requirements
  6. if (!isset($gallery_basedir))
  7. {
  8. $gallery_basedir = './';
  9. }
  10.  
  11. require_once($gallery_basedir . "init.inc");
  12. require_once($gallery_basedir . 'embed.php');
  13.  
  14. // Mail server information
  15. $emailUser = "username";
  16. $emailPass = "password";
  17.  
  18. // Gallery information
  19. $albumIdnumber = 13680; // Album to add items to
  20. $externalownerId = 1; // Owner id of the items
  21.  
  22. // Connect to mailbox
  23. print("Connecting to mailbox.\n");
  24. $mbox = imap_open("{localhost:995/pop3/ssl/novalidate-cert}INBOX", $emailUser, $emailPass);
  25.  
  26. if (!imap_errors()) {
  27.  
  28. // Get the number of messages
  29. $numOfMessages = imap_num_msg($mbox);
  30.  
  31. // Loop through each email
  32. print("Looping through messages.\n");
  33. for ($i = 1; $i <= $numOfMessages; $i++) {
  34.  
  35. // get the message structure
  36. $struct = imap_fetchstructure($mbox, $i);
  37.  
  38. $numOfParts = count($struct->parts);
  39.  
  40. // Loop through the message parts
  41. print("Looping through message parts.\n");
  42. for ($k = 0; $k < $numOfParts; $k++) {
  43.  
  44. if ($struct->parts[$k]->type == 5) { // Its an image!
  45.  
  46. $filename = "Moblog-" . date("d.m.y-H.i.s") . "-" . $k . ".jpg";
  47.  
  48. // capture the body
  49. $body = imap_fetchbody($mbox, $i, 1);
  50. if ($struct->parts[0]->encoding == 3) {
  51. $body = base64_decode($body);
  52. }
  53.  
  54. if ($body == "") {
  55. $title = $filename;
  56. }
  57. else {
  58. $title = $body;
  59. }
  60. // capture the attachments
  61. $data = imap_fetchbody($mbox, $i, $k + 1);
  62.  
  63. $tmpfname = tempnam("/tmp", "moblog");
  64.  
  65. $handle = fopen($tmpfname, "w");
  66. fwrite($handle, imap_base64($data));
  67. fclose($handle);
  68.  
  69. // Call to Gallery
  70. print("Adding the image to gallery.\n");
  71. addToGallery($albumIdnumber, $externalownerId, $filename, $filename, $title, $filename, $body, "image/jpeg");
  72.  
  73. // unlink($tmpfname);
  74. }
  75. }
  76.  
  77. // Delete the message
  78. imap_delete($mbox, $i);
  79. }
  80. }
  81.  
  82. // Delete messages
  83. imap_expunge($mbox);
  84.  
  85. // Disconnect
  86. imap_close($mbox);
  87.  
  88. // Interfaces with Gallery to add the images.
  89. function addToGallery($albumId, $ownerId, $fileName, $itemName, $title, $summary, $description, $mimeType) {
  90.  
  91. // Initiate
  92. print("Initiate.\n");
  93. list ($ret) = GalleryEmbed::init(array("g2Uri" => "/", "embedUri" => "/main.php", "activeUserId" => $ownerId, "fullInit" => true));
  94. // print_r($ret);
  95.  
  96. // Lock Album
  97. print("Lock the album.\n");
  98. list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($albumId);
  99. // print_r($ret);
  100.  
  101. // Add Item To Album
  102. print("Add the item.\n");
  103. print_r($fileName);
  104. print("\n");
  105. list ($ret, $newitem) = GalleryCoreApi::addItemToAlbum($fileName, $fileName, $title, $summary, $description, $mimeType, $albumId);
  106. print_r($ret);
  107.  
  108. // Release Album
  109. print("Release the item.\n");
  110. list ($ret) = GalleryCoreApi::releaseLocks($lockId);
  111. // print_r($ret);
  112.  
  113. // Finish Up
  114. print("Finish up.\n");
  115. list ($ret) = GalleryEmbed::done();
  116. // print_r($ret);
  117. }
  118.  
  119. ?>
Add Comment
Please, Sign In to add comment