summaryrefslogtreecommitdiff
path: root/apps/mm/scripts/newmail.php
blob: e4fc740e64ea01c7e76106b7ad4c91f010b598fb (plain)
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
<?php
// What directory are we in on the server? /////////////////////////////////////
$_dir_scripts = dirname(__FILE__);
$_dir_app     = dirname($_dir_scripts);
$_dir_apps    = dirname($_dir_app);
$_dir_base    = dirname($_dir_apps);
require_once($_dir_base.'/stub.php');

$cmdline = isset($argv[0]); // called from the command line
@$method = $_SERVER['REQUEST_METHOD']; // What HTTP method was used
if ( ($method=='PUT') || $cmdline ) {
	// We're going to be uploading a new message.
	
	// uniqid() isn't 'secure', it doesn't need to be, it's to prevent
	// random collisions.
	$tmpfile = "$BASE/tmp/".uniqid(getmypid().'.');
	$infile = ($cmdline?'php://stdin':'php://input');
	$out = fopen($tmpfile, "w");
	$in  = fopen($infile, "r");
	while ($data = fread($in, 1024))
		fwrite($out, $data);
	fclose($out);
	fclose($in);
	
	require_once('Message.class.php');
	$msg = Message::add($tmpfile);
	$id = $msg->msgid();
	
	if ($cmdline) {
		echo $id."\n";
	} else {
		$m->status('201 Created');
		header("Location: ".$m->baseUrl().'messages/'.$id);
	}
	exit();
}