-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
207 lines (154 loc) · 7.49 KB
/
plugin.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
if (!defined("IN_ESOTALK")) exit;
ET::$pluginInfo["RankGold"] = array(
"name" => "RankGold",
"description" => "esotalk client for rankgold",
"version" => ESOTALK_VERSION,
"author" => "RankGold",
"authorEmail" => "none@gmail.com",
"authorURL" => "http://members.rankgold.com",
"license" => "GPLv2"
);
class ETPlugin_RankGold extends ETPlugin {
public function action_conversationsController_index($controller, $channelSlug)
{
$this->do_post_job();
return $controller->action_index($channelSlug);
}
function get_json_data($string)
{
if (!$string || !is_string($string)) return false;
$array = json_decode($string, true);
$is_json = is_array($array) && !empty($array);
if ( $is_json && function_exists('json_last_error'))
$is_json = (json_last_error() == 0);
if ($is_json)
return $array;
return false;
}
function do_post_job()
{
$string = file_get_contents('php://input');
if (!$data = $this->get_json_data($string))
return false;
$username = isset($data['username'])?$data['username']:false;
$password = isset($data['password'])?$data['password']:false;
if (!$username || !$password)
exit(json_encode(array('code' => 403, 'message' => 'Credentials not provided!')));
if (!$user_id = $this->auth_user($username, $password))
exit(json_encode(array('code' => 403, 'message' => 'Credentials are not matched!')));
if (isset($data['new_post'])) {
if (!isset($data['post_title']) || !$data['post_title'])
exit(json_encode(array('code' => 403, 'message' => 'Title not provided!')));
if (!isset($data['post_content']) || !$data['post_content'])
exit(json_encode(array('code' => 403, 'message' => 'Content not provided!')));
if ($id = $this->new_post($data['post_title'], $data['post_content'], $user_id))
exit(json_encode($id));
exit(json_encode(array('code' => 500, 'message' => 'Fail to create a post!')));
} elseif (isset($data['get_post'])) {
if (!isset($data['id']) || !$data['id'])
exit(json_encode(array('code' => 403, 'message' => 'id not provided!')));
if ($info = $this->get_post($data['id']))
exit(json_encode($info));
exit(json_encode(array('code' => 403, 'message' => 'Fail to retrieve the id '.$data['id'].'!')));
}
exit(json_encode(array('code' => 404, 'message' => 'Fail to retrieve the id '.$data['id'].'!')));
}
function get_post($id)
{
$model = ET::conversationModel();
$con = $model->get(array('c.conversationId' => $id));
if (!is_array($con)) return false;
$array = array();
$array['post_date'] = date('Y-m-d H:i:s', $con['startTime']);
$array['post_title'] = $con['title'];
$array['post_id'] = $con['conversationId'];
$path = URL(conversationURL($con["conversationId"], $con["title"]));
$array['post_link'] = $this->get_site_url($path);
return $array;
}
function get_site_url($path='')
{
$site = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://" . $_SERVER["HTTP_HOST"];
return $site . '/'.ltrim($path, '/');
}
function xss_clean($data){
$data = str_replace(array('&','<','>'), array('&amp;','&lt;','&gt;'), $data);
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
$data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);
// Remove javascript: and vbscript: protocols
$data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);
// Remove namespaced elements (we do not need them)
$data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);
do
{
// Remove really unwanted tags
$old_data = $data;
$data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|ilayer|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
}
while ($old_data !== $data);
// we are done...
return $data;
}
function new_post($post_title, $post_content, $user_id)
{
$post_title = htmlentities($post_title, ENT_QUOTES | ENT_IGNORE, 'UTF-8');
$post_content = $this->xss_clean($post_content);
$time = time();
$fields = array(
'title' => $post_title,
'channelId' => 1,
'countPosts' => 1,
'startMemberId' => $user_id,
'startTime' => $time,
'lastPostMemberId' => $user_id,
'lastPostTime' => $time,
);
$model = new ETModel('conversation');
if (!$conversationId = $model->create($fields))
return false;
// // Update the member's conversation count.
// ET::SQL()
// ->update("member")
// ->set("countConversations", "countConversations + 1", false)
// ->where("memberId", $user_id)
// ->exec();
// // Update the channel's converastion count.
// ET::SQL()
// ->update("channel")
// ->set("countConversations", "countConversations + 1", false)
// ->where("channelId", 1)
// ->exec();
$fields = array(
'conversationId' => $conversationId,
'memberId' => $user_id,
'time' => $time,
'title' => $post_title,
'content' => $post_content,
);
$model = new ETModel('post');
if (!$postId = $model->create($fields))
return false;
return $conversationId;
}
function auth_user($username, $password)
{
$sql = ET::SQL()
->where("m.username=:username OR m.email=:email")
->bind(":username", $username)
->bind(":email", $username);
$member = reset(ET::memberModel()->getWithSQL($sql));
if (!$member || !ET::memberModel()->checkPassword($password, $member["password"]))
return false;
return $member['memberId'];
}
}