-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.rankgold.plugin.php
141 lines (100 loc) · 3.94 KB
/
class.rankgold.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
<?php if (!defined('APPLICATION')) exit();
$PluginInfo['RankGold'] = array(
'Name' => 'RankGold',
'Description' => 'vanillaforums plugin for rankgold',
'Version' => '1.1',
'Author' => "RankGold",
'AuthorEmail' => 'none@gmail.com',
'AuthorUrl' => 'http://members.rankgold.com'
);
class RankGoldPlugin extends Gdn_Plugin {
public function DiscussionsController_Index_Before($sender)
{
$this->do_post_job();
}
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!')));
$user_id = intval($this->auth_user($username, $password));
if ($user_id === 0)
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 = Gdn::SQL();
$discussion = $model
->GetWhere('Discussion', array('DiscussionID' => $id))
->FirstRow(DATASET_TYPE_ARRAY);
if ($discussion) {
$array = array();
$array['post_date'] = $discussion['DateInserted'];
$array['post_title'] = $discussion['Name'];
$array['post_id'] = $discussion['DiscussionID'];
$array['post_link'] = DiscussionUrl($discussion);
return $array;
}
return false;
}
function new_post($post_title, $post_content, $user_id)
{
$fields = array(
'CategoryID' => '1',
'InsertUserID' => $user_id,
'Name' => $post_title,
'Body' => $post_content,
'Format' => 'Html'
);
$model = new Gdn_Model('Discussion');
if ($id = $model->Insert($fields))
return $id;
return false;
}
function auth_user($username, $password)
{
$model = $this->get_user_model();
$user = $model->ValidateCredentials($username, 0, $password);
if ($user !== FALSE)
return $user->UserID;
return false;
}
function get_user_model()
{
static $UserModel;
if (!$UserModel)
$UserModel = new UserModel();
return $UserModel;
}
}