This repository has been archived by the owner on Apr 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapimail.php
executable file
·86 lines (68 loc) · 1.96 KB
/
apimail.php
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/php
<?php
require_once("common.php");
// Search an author (in the first arg)
if (isset($argv[1]))
$ffrom = $argv[1];
else
$ffrom = "";
// Search a title for the paste (in the second arg)
if (isset($argv[2]))
$subject = $argv[2];
else
$subject = "";
// Receive mail content
$content = file("php://stdin");
$cnt = array();
$boundary = null;
$pass = false;
$i = -1;
foreach($content as $k => $line)
{
// Separate body email content
if (substr($line, 0, 2 + strlen($boundary)) == "--".$boundary)
{
$cnt[] = "";
$i++;
$pass = true;
}
// Don't save headers
else if (($pass || empty($boundary)) && (trim($line) == "" || !empty($cnt[$i])))
{
if ($i < 0)
{
$cnt[] = "";
$i++;
}
$cnt[$i] .= $line;
}
// Save email part separator
else if (preg_match("#^Content-Type: [^;]+; boundary=\"(.+)\"#", $line, $out))
$boundary = $out[1];
// Read From field if $ffrom is empty
else if (empty($ffrom) && preg_match("#^From: (.+)#", $line, $out))
$ffrom = $out[1];
// Read Subject field if $subject is empty
else if (empty($subject) && preg_match("#^Subject: (.+)#", $line, $out))
$subject = $out[1];
}
// Extract username instead of email adress if it exists
if (preg_match("#([^<]+) <#ui", $ffrom, $out))
$from = $out[1];
else
$from = $ffrom;
// Create the paste
$paste = new Paste();
$paste->title = $subject;
$paste->author = $from;
$paste->date = time();
$paste->content = utf8_encode(trim($cnt[max(0,$i-1)]));
// Save the paste and give read right to all users (if mail user is different from php one)
$link = $paste->save();
chmod(Paste::get_path($paste->filename), 0644);
// Send confirmation email
$headers = 'From: paste@p0m.fr' . "\r\n" .
'Content-Type: text/plain; charset="utf-8"' . "\r\n" .
'X-Mailer: '.ucfirst(HTTP_URL);
mail($ffrom, "Re: ".$subject, "Bonjour,\n\nVotre paste a bien été publié à l'adresse suivante :\nhttp://".HTTP_URL."/?".$link."\n\n-- \n".HTTP_URL, $headers);
?>