/
Messages.php
87 lines (74 loc) · 2.83 KB
/
Messages.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
87
<?php
require_once "User.php";
require_once "Message.php";
class Messages {
public function __construct($user) {
if($user == null || !($user instanceof User))
throw new Exception("Messages::__construct(): $user must be a User object.");
if(!$user->getProp()->isLoggedIn)
throw new Exception("Messages::__contstruct(): $user is not logged in.");
$this->user = $user;
}
public function sendMessage($msg) {
if($msg == null || !($msg instanceof Message))
throw new Exception("Messages::sendMessage(): $msg must be a Message object.");
$subject = urlencode($msg->subject);
$body = urlencode($msg->body);
$to = $msg->dest->getUsername();
$this->user->httpPost("http://www.reddit.com/api/compose", "uh=" . $this->user->getProp()->modhash . "&to=$to&subject=$subject&thing_id=&text=$body&id=%23compose-message");
}
// Read messages from a user's inbox/sentbox.
//
// Boxtypes:
// all - reads every message from a user's inbox
// messages - reads only private messages sent to a user
// comments - reads only comment replies
// selfreply - reads only replies to self posts.
// sent - reads only private messages that a user has sent.
//
// If $after is specified, only messages after message $after will be returned. (Useful for paging).
// $after may be specified as a string containing a message's name (e.g., "t4_3byo9") or as a Message object.
public function getMessages($boxType = "all", $after = null) {
if($boxType != "all" && $boxType != "messages" && $boxType != "comments" && $boxType != "selfreply" && $boxType != "inbox" && $boxType != "sent")
throw new Exception("Messages::getMessages(): Invalid box type '$boxType'.");
if($boxType == "all")
$boxType = "inbox";
// Handle after-based paging.
if($after != null) {
if($after instanceof Message)
$after = $after->name;
$json = $this->user->httpGet("http://www.reddit.com/message/$boxType/.json?after=$after");
}
else
$json = $this->user->httpGet("http://www.reddit.com/message/$boxType/.json");
$json = json_decode($json['response']);
$before = $json->data->before;
$after = $json->data->after;
$ret = array();
foreach($json->data->children as $c) {
$msg = new Message();
$msg->kind = $c->kind;
$msg->body = $c->data->body;
$msg->was_comment = $c->data->was_comment;
$msg->name = $c->data->name;
if ($boxType == "sent")
$msg->author = $this->user;
else
$msg->author = new User($c->data->author);
if ($boxType != "sent")
$msg->dest = $this->user;
else
$msg->dest = new User($c->data->dest);
$msg->created = $c->data->created;
$msg->created_utc = $c->data->created_utc;
$msg->body_html = $c->data->body_html;
$msg->context = $c->data->context;
$msg->new = $c->data->new;
$msg->id = $c->data->id;
$msg->subject = $c->data->subject;
$ret[] = $msg;
}
return $ret;
}
}
?>