summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Parecki <aaron@parecki.com>2015-07-25 05:44:10 -0700
committerAaron Parecki <aaron@parecki.com>2015-07-25 05:44:10 -0700
commitc3fb722ad4bade9c4027c04de9b8dee511ab5756 (patch)
treeda4711f4ea9b4d1b79003c1c3d28acadeba9f781
parent92b55f2636a82c5eafc24d1e2adc84b61258161e (diff)
add post-by-email support to quill
-rw-r--r--controllers/controllers.php30
-rw-r--r--controllers/hooks.php73
-rw-r--r--public/index.php1
-rw-r--r--schema/mysql.sql1
-rw-r--r--schema/sqlite.sql3
-rw-r--r--views/email.php29
-rw-r--r--views/layout.php3
7 files changed, 138 insertions, 2 deletions
diff --git a/controllers/controllers.php b/controllers/controllers.php
index 72ac2b8..95974d2 100644
--- a/controllers/controllers.php
+++ b/controllers/controllers.php
@@ -232,6 +232,36 @@ $app->get('/add-to-home', function() use($app) {
}
});
+$app->get('/email', function() use($app) {
+ if($user=require_login($app)) {
+
+ $test_response = '';
+ if($user->last_micropub_response) {
+ try {
+ if(@json_decode($user->last_micropub_response)) {
+ $d = json_decode($user->last_micropub_response);
+ $test_response = $d->response;
+ }
+ } catch(Exception $e) {
+ }
+ }
+
+ if(!$user->email_username) {
+ $host = parse_url($user->url, PHP_URL_HOST);
+ $user->email_username = $host . '.' . rand(100000,999999);
+ $user->save();
+ }
+
+ $html = render('email', array(
+ 'title' => 'Post-by-Email',
+ 'micropub_endpoint' => $user->micropub_endpoint,
+ 'test_response' => $test_response,
+ 'user' => $user
+ ));
+ $app->response()->body($html);
+ }
+});
+
$app->get('/settings', function() use($app) {
if($user=require_login($app)) {
$html = render('settings', array('title' => 'Settings', 'include_facebook' => true, 'authorizing' => false));
diff --git a/controllers/hooks.php b/controllers/hooks.php
new file mode 100644
index 0000000..b597c7b
--- /dev/null
+++ b/controllers/hooks.php
@@ -0,0 +1,73 @@
+<?php
+
+$app->post('/mailgun', function() use($app) {
+ $params = $app->request()->params();
+
+ // Find the user for this email
+ if(!preg_match('/([^ <>]+)@'.Config::$hostname.'/', $params['To'], $match)) {
+ $app->response()->body('invalid recipient');
+ return;
+ }
+
+ $user = ORM::for_table('users')->where('email_username', $match[1])->find_one();
+ if(!$user) {
+ $app->response()->body('user not found');
+ return;
+ }
+
+ if(!$user->micropub_access_token) {
+ $app->response()->body('user has no access token');
+ return;
+ }
+
+ $data = array(
+ 'published' => (k($params, 'Date') ? date('c', strtotime(k($params, 'Date'))) : date('c'))
+ );
+
+ if(k($params, 'Subject'))
+ $data['name'] = k($params, 'Subject');
+
+ if(k($params['body-plain'])
+ $data['content'] = k($params, 'body-plain');
+
+ // Set tags for any hashtags used in the body
+ if(preg_match_all('/#([^ ]+)/', $data['content'], $matches)) {
+ $tags = array();
+ foreach($matches[1] as $m)
+ $tags[] = $m;
+ if($tags) {
+ if($user->send_category_as_array != 1) {
+ $data['category'] = $tags;
+ } else {
+ $data['category'] = implode(',', $tags);
+ }
+ }
+ }
+
+ // Handle attachments
+ $filename = false;
+
+ foreach($_FILES as $file) {
+ // If a photo was included, set the filename to the downloaded file
+ if(preg_match('/image/', $file['type'])) {
+ $filename = $file['tmp_name'];
+ }
+
+ // Sometimes MMSs are sent with a txt file attached instead of in the body
+ if(preg_match('/text\/plain/', $file['type'])) {
+ $content = trim(file_get_contents($file['tmp_name']));
+ if($content) {
+ $data['content'] = $content;
+ }
+ }
+ }
+
+ $r = micropub_post_for_user($user, $data, $filename);
+
+ if(k($r, 'location'))
+ $result = 'created post at ' . $r['location'];
+ else
+ $result = 'error creating post';
+
+ $app->response()->body($result);
+});
diff --git a/public/index.php b/public/index.php
index fde6984..e6a0f6a 100644
--- a/public/index.php
+++ b/public/index.php
@@ -14,6 +14,7 @@ $app = new \Slim\Slim(array(
require 'controllers/auth.php';
require 'controllers/controllers.php';
require 'controllers/editor.php';
+require 'controllers/hooks.php';
session_name('quill');
session_set_cookie_params(86400*30);
diff --git a/schema/mysql.sql b/schema/mysql.sql
index 5f33ce6..e9f6d1d 100644
--- a/schema/mysql.sql
+++ b/schema/mysql.sql
@@ -19,5 +19,6 @@ CREATE TABLE `users` (
`twitter_token_secret` text,
`twitter_username` varchar(255) DEFAULT NULL,
`instagram_access_token` text,
+ `email_username` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
diff --git a/schema/sqlite.sql b/schema/sqlite.sql
index ac691e3..9f05c1e 100644
--- a/schema/sqlite.sql
+++ b/schema/sqlite.sql
@@ -18,5 +18,6 @@ CREATE TABLE users (
twitter_access_token TEXT,
twitter_token_secret TEXT,
twitter_username TEXT,
- instagram_access_token TEXT
+ instagram_access_token TEXT,
+ email_username TEXT
); \ No newline at end of file
diff --git a/views/email.php b/views/email.php
new file mode 100644
index 0000000..1f97905
--- /dev/null
+++ b/views/email.php
@@ -0,0 +1,29 @@
+<div class="narrow">
+ <?= partial('partials/header') ?>
+
+ <div class="jumbotron" style="margin-top: 20px;">
+ <p>
+ Send email or MMS to<br>
+ <a href="mailto:<?= $this->user->email_username . '@' . Config::$hostname ?>"><?= $this->user->email_username . '@' . Config::$hostname ?></a>
+ </p>
+ </div>
+
+ <div style="width: 80%; margin: 0 auto;">
+ <h3>Email Subject</h3>
+ <p>If you add a subject line to your email, it will be sent as the "name" property which indicates to your Micropub endpoint that this is a blog post.</p>
+
+ <h3>Email and MMS body</h3>
+ <p>The text of your email or MMS will be send as the "content" property, which is the main contents of your post. Plaintext only for now.</p>
+
+ <h3>Photo</h3>
+ <p>If you attach a photo to your email or MMS, it will be sent to your Micropub endpoint. (Only one photo is currently supported.)</p>
+ </div>
+
+ <div>
+ <?php if($this->test_response): ?>
+ <h4>Last response from your Micropub endpoint</h4>
+ <pre id="test_response" style="width: 100%; min-height: 280px;"><?= htmlspecialchars($this->test_response) ?></pre>
+ <?php endif; ?>
+ </div>
+
+</div> \ No newline at end of file
diff --git a/views/layout.php b/views/layout.php
index 7c2dab9..7bbba4a 100644
--- a/views/layout.php
+++ b/views/layout.php
@@ -63,10 +63,11 @@ if(property_exists($this, 'include_facebook')) {
<? if(session('me')) { ?>
<li><a href="/editor">Editor</a></li>
- <li><a href="/new">New Note</a></li>
+ <li><a href="/new">Note</a></li>
<li><a href="/bookmark">Bookmark</a></li>
<li><a href="/favorite">Favorite</a></li>
<li><a href="/photo">Photo</a></li>
+ <li><a href="/email">Email</a></li>
<? } ?>
<li><a href="/docs">Docs</a></li>