From c3fb722ad4bade9c4027c04de9b8dee511ab5756 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sat, 25 Jul 2015 05:44:10 -0700 Subject: add post-by-email support to quill --- controllers/controllers.php | 30 +++++++++++++++++++ controllers/hooks.php | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 controllers/hooks.php (limited to 'controllers') 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 @@ +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); +}); -- cgit v1.2.3