summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHorus32016-07-03 18:07:29 +0200
committerHorus32016-07-03 18:07:29 +0200
commitf5cf66b6929928fe5a4a6cf227f9a2f2195329e5 (patch)
tree94155faa0b84a236503ee0843eec749989fd9625
downloadimap2rss-f5cf66b6929928fe5a4a6cf227f9a2f2195329e5.tar.gz
Initial commit.
-rw-r--r--.gitignore6
-rw-r--r--bootstrap.php6
-rw-r--r--composer.json6
-rw-r--r--composer.lock107
-rw-r--r--feed.php42
-rw-r--r--imap.php69
-rw-r--r--index.php11
7 files changed, 247 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a2888d2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+*.swp
+*~
+*.db
+*.xml
+
+vendor/
diff --git a/bootstrap.php b/bootstrap.php
new file mode 100644
index 0000000..68af7d0
--- /dev/null
+++ b/bootstrap.php
@@ -0,0 +1,6 @@
+<?php
+
+require_once __DIR__ . '/vendor/autoload.php';
+require_once __DIR__ . '/imap.php';
+require_once __DIR__ . '/feed.php';
+
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..bd337c6
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,6 @@
+{
+ "require": {
+ "php-imap/php-imap": "^2.0",
+ "suin/php-rss-writer": "^1.4"
+ }
+}
diff --git a/composer.lock b/composer.lock
new file mode 100644
index 0000000..91d9352
--- /dev/null
+++ b/composer.lock
@@ -0,0 +1,107 @@
+{
+ "_readme": [
+ "This file locks the dependencies of your project to a known state",
+ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
+ "This file is @generated automatically"
+ ],
+ "hash": "ceef96fe5f0bf30814febdce51afaaf4",
+ "content-hash": "5e9081cc591450aaf17d67806d687c74",
+ "packages": [
+ {
+ "name": "php-imap/php-imap",
+ "version": "2.0.6",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/barbushin/php-imap.git",
+ "reference": "3174b23aaf45cc570972209f366c384f257399a8"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/barbushin/php-imap/zipball/3174b23aaf45cc570972209f366c384f257399a8",
+ "reference": "3174b23aaf45cc570972209f366c384f257399a8",
+ "shasum": ""
+ },
+ "require": {
+ "ext-imap": "*",
+ "php": ">=5.3.0"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "PhpImap\\": "src/PhpImap/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "BSD 3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Sergey Barbushin",
+ "email": "barbushin@gmail.com",
+ "homepage": "http://linkedin.com/in/barbushin"
+ }
+ ],
+ "description": "PHP class to access mailbox by POP3/IMAP/NNTP using IMAP extension",
+ "homepage": "https://github.com/barbushin/php-imap",
+ "keywords": [
+ "imap",
+ "mail",
+ "php"
+ ],
+ "time": "2015-12-14 02:46:12"
+ },
+ {
+ "name": "suin/php-rss-writer",
+ "version": "1.4.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/suin/php-rss-writer.git",
+ "reference": "a7dd2bbc287a05b266406d3afa298d44ebf115f3"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/suin/php-rss-writer/zipball/a7dd2bbc287a05b266406d3afa298d44ebf115f3",
+ "reference": "a7dd2bbc287a05b266406d3afa298d44ebf115f3",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.4.0"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-0": {
+ "Suin\\RSSWriter": "src"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Hidehito Nozawa aka Suin",
+ "email": "suinyeze@gmail.com"
+ }
+ ],
+ "description": "Yet another simple RSS writer library for PHP 5.4 or later.",
+ "homepage": "https://github.com/suin/php-rss-writer",
+ "keywords": [
+ "feed",
+ "generator",
+ "php",
+ "rss",
+ "writer"
+ ],
+ "time": "2016-03-19 06:15:37"
+ }
+ ],
+ "packages-dev": [],
+ "aliases": [],
+ "minimum-stability": "stable",
+ "stability-flags": [],
+ "prefer-stable": false,
+ "prefer-lowest": false,
+ "platform": [],
+ "platform-dev": []
+}
diff --git a/feed.php b/feed.php
new file mode 100644
index 0000000..b1f7630
--- /dev/null
+++ b/feed.php
@@ -0,0 +1,42 @@
+<?php
+use Suin\RSSWriter\Channel;
+use Suin\RSSWriter\Feed;
+use Suin\RSSWriter\Item;
+
+function generate_feed($mail, $user_string, $inbox_string) {
+ $feed = new Feed();
+
+ $channel = new Channel();
+ $channel
+ ->title(htmlspecialchars($user_string . " / " . $inbox_string, ENT_XML1, 'UTF-8'))
+ ->description($inbox_string)
+ ->url("https://www.maxmail.xyz")
+ ->language('en-US')
+ ->copyright('Copyright 2016, maxmail.xyz')
+ ->pubDate(strtotime("r", date(time())))
+ ->lastBuildDate(strtotime("r", date(time())))
+ ->ttl(60)
+ ->appendTo($feed);
+
+ for( $i = 0; $i < count($mail); $i++ ) {
+ $item = new Item();
+
+ if( is_null($mail[$i]['textPlain']) ) {
+ $body = $mail[$i]['textHtml'];
+ } else {
+ $body = $mail[$i]['textPlain'];
+ }
+ $item
+ ->title( htmlspecialchars($mail[$i]["mailbox"] . ": " . $mail[$i]['subject'], ENT_XML1, 'UTF-8') )
+ ->description( "<pre>" . $body . "</pre>" )
+ #->description($body)
+ ->url("https://www.maxmail.xyz/" . $user_string)
+ ->author( htmlspecialchars($mail[$i]['fromName'] . "<" . $mail[$i]['fromAddress'] . ">", ENT_XML1, 'UTF-8') )
+ ->pubDate( strtotime($mail[$i]['date'] ) )
+ ->appendTo($channel);
+ }
+
+ ob_start();
+ echo $feed;
+ return ob_get_clean();
+}
diff --git a/imap.php b/imap.php
new file mode 100644
index 0000000..191447c
--- /dev/null
+++ b/imap.php
@@ -0,0 +1,69 @@
+<?php
+function date_compare($a, $b)
+{
+ $t1 = strtotime($a['date']);
+ $t2 = strtotime($b['date']);
+ return -($t1 - $t2);
+}
+
+function getMail($user, $pass, $inbox, $max_items){
+ $mail = array();
+
+ foreach($inbox as $cur_inbox) {
+ try {
+ $cur_mail = getMailbox($user, $pass, $max_items, $cur_inbox);
+ if ( false === $cur_mail ) {
+ // Mailbox is empty.
+ continue;
+ }
+ } catch( Exception $e ) {
+ header( $_SERVER["SERVER_PROTOCOL"] . " 403 Forbidden" );
+ exit(1);
+ }
+
+ $mail = array_merge($mail, $cur_mail);
+
+ }
+
+ usort($mail, 'date_compare');
+
+ return $mail;
+}
+
+function getMailbox( $user, $password, $max_items, $inbox, $imap_host = "mx.iamfabulous.de" ) {
+ require_once __DIR__ . '/vendor/autoload.php';
+
+ $mailbox = new PhpImap\Mailbox('{'.$imap_host.'}'.$inbox, $user, $password);
+
+ // Read all messaged into an array:
+ $mailsIds = $mailbox->searchMailbox('ALL');
+ if(!$mailsIds) {
+ #throw new Exception('Mailbox is empty');
+ return false;
+ }
+
+ $return = array();
+
+ if ( (count($mailsIds) - $max_items) < 0) {
+ $limit = 0;
+ } else {
+ $limit = count($mailsIds) - $max_items;
+ }
+
+ $cnt = 0;
+ for( $i = count($mailsIds) - 1; $i >= $limit; $i-- ) {
+ $mbox = $mailbox->getMail($mailsIds[$i], $markAsSeen = false);
+ $return[$cnt]["date"] = $mbox->date;
+ $return[$cnt]["subject"] = $mbox->subject;
+ $return[$cnt]["fromName"] = $mbox->fromName;
+ $return[$cnt]["fromAddress"] = $mbox->fromAddress;
+ $return[$cnt]["to"] = $mbox->to;
+ $return[$cnt]["toString"] = $mbox->toString;
+ $return[$cnt]["messageId"] = $mbox->messageId;
+ $return[$cnt]["textPlain"] = $mbox->textPlain;
+ $return[$cnt]["textHtml"] = $mbox->textHtml;
+ $return[$cnt]["mailbox"] = $inbox;
+ $cnt++;
+ }
+ return $return;
+}
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..b40b8e1
--- /dev/null
+++ b/index.php
@@ -0,0 +1,11 @@
+<?php
+$user = "feed@maxmail.xyz";
+$pass = "1234";
+$inbox = array("INBOX", "Trash");
+$max_items = 10;
+
+$mail = getMail($user, $pass, $inbox, $max_items);
+
+$inbox_string = implode(",", $inbox);
+
+$feed = generate_feed($mail, $user, $inbox_string);