Aaron Parecki
9 years ago
7 changed files with 138 additions and 2 deletions
-
30controllers/controllers.php
-
73controllers/hooks.php
-
1public/index.php
-
1schema/mysql.sql
-
3schema/sqlite.sql
-
29views/email.php
-
3views/layout.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); |
|||
}); |
@ -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> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue