Backend for songs.zachdecook.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
define('LARAVEL_START', microtime(true));
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/load-eloquent.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
use App\Song;

$imap = imap_open($mailbox, $username, $password);
$check = imap_check($imap); $number = $check->Nmsgs;
// Just check the newest 10 messages.
for($i = 0; $i < 10; $i++) {
    // TODO: Process these in order?
    $header = imap_header($imap, $number - $i);
    if (strpos($header->to[0]->mailbox, "+songs")){
        $json = json_decode($header->subject);
        $song = Song::findOrFail($json->id);
        $body = quoted_printable_decode(imap_body($imap, $number - $i));
        $song->text = $body;
        $song->title = $json->title;
        $song->author = $json->author;
        $song->key = $json->key;
        $song->verse = $json->verse;
        $song->save();
        imap_delete($imap, $number - $i);
        printf("updated song {$song->id}");
        exit(0);
    }
}
exit(0);