/
MY_Email_Api_Object.php
259 lines (222 loc) · 6.86 KB
/
MY_Email_Api_Object.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php defined('SYSPATH') or die('No direct script access.');
/**
* This class handles GET request for Email via the API.
*
* @version 25 - Emmanuel Kala 2010-10-27
*
* PHP version 5
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi - http://source.ushahididev.com
* @module API Controller
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*/
class Email_Api_Object extends Api_Object_Core {
public function __construct($api_service)
{
parent::__construct($api_service);
}
/**
* Empty declaration for OOP compliance
*/
public function perform_task()
{
// Authenticate the user
if ( ! $this->api_service->_login(TRUE))
{
$this->set_error_message($this->response(2));
return;
}
$this->_list_all_email_msgs();
}
/**
* Handles actions for email messages
*/
public function email_action()
{
// Authenticate the user
if ( ! $this->api_service->_login(TRUE))
{
$this->set_error_message($this->response(2));
return;
}
if ( ! $this->api_service->verify_array_index($this->request, 'action'))
{
$this->set_error_message(array(
"error" => $this->api_service->get_error_msg(001,'action')
));
return;
}
else
{
$this->by = $this->request['action'];
}
switch ($this->by)
{
case "d":
$this->_delete_email_msg();
break;
case "s":
$this->_spam_email_msg();
break;
default:
$this->set_error_message(array(
"error" => $this->api_service->get_error_msg(001)
));
}
}
/**
* List first 20 email messages
*
* @return array
*/
public function _list_all_email_msgs()
{
$ret_json_or_xml = ''; // Will hold the return JSON/XML string
$items = ORM::factory('message')
->where('service_messageid', '2')
->where('message_type','1')
->orderby('message_date','desc')
->find_all($this->list_limit);
// Set the no. of records fetched
$this->record_count = $items->count();
$json_categories = array();
$i = 0;
//No record found.
if ($items->count() == 0)
{
$this->response_data = $this->response(4);
return;
}
foreach ($items as $email)
{
if ( $response_type == 'json')
{
$json_categories[] = array("email" => $item);
}
else
{
$json_categories['email'.$i] = array('email' =>
$twitter);
$this->replar[] = 'email'.$i;
}
}
// Create the json array
$data = array("payload" => array(
"domain" => $this->domain,
"count" => $json_categories),
"error" => $this->api_service->get_error_msg(0));
if ($this->response_type == 'json')
{
$ret_json_or_xml = $this->array_as_json($data);
}
else
{
$ret_json_or_xml = $this->array_as_xml($data, $this->replar);
}
$this->response_data = $ret_json_or_xml;
}
/**
* Delete existing email message
*
* @return Array
*/
public function _delete_email_msg()
{
$ret_val = 0; // Initialize a 0 return value; successful execution
if ($_POST)
{
$post = Validation::factory($_POST);
// Add some filters
$post->pre_filter('trim', TRUE);
// Add some rules, the input field, followed by a list of
//checks, carried out in order
$post->add_rules('action','required', 'alpha', 'length[1,1]');
$post->add_rules('message_id','required','numeric');
if ($post->validate(FALSE))
{
$email_id = $post->message_id;
$email = new Message_Model($email_id);
if ($email->loaded == true)
{
$email->delete();
}
else
{
//email id doesn't exist in DB
//TODO i18nize the string
$this->error_messages .= "Email ID does not exist.";
$ret_value = 1;
}
}
else
{
//TODO i18nize the string
$this->error_messages .= "Email ID is required.";
$ret_value = 1;
}
}
else
{
$ret_value = 3;
}
$this->response_data = $this->response($ret_value);
}
/**
* Spam / Unspam existing email message
*
* @return Array
*/
public function _spam_email_msg()
{
$ret_val = 0; // Initialize a 0 return value; successful execution
if($_POST)
{
$post = Validation::factory($_POST);
// Add some filters
$post->pre_filter('trim', TRUE);
// Add some rules, the input field, followed by a list of
//checks, carried out in order
$post->add_rules('action','required', 'alpha', 'length[1,1]');
$post->add_rules('message_id','required','numeric');
if ($post->validate(FALSE))
{
$email_id = $post->message_id;
$email = new Message_Model($email_id);
if ($email->loaded == true)
{
if ($email->message_level == '1')
{
$email->message_level = '99';
}
else
{
$email->message_level = '1';
}
$email->save();
}
else
{
//email id doesn't exist in DB
//TODO i18nize the string
$this->error_messages .= "Email ID does not exist.";
$ret_value = 1;
}
}
else
{
//TODO i18nize the string
$this->error_messages .= "Email ID is required.";
$ret_value = 1;
}
}
else
{
$ret_value = 3;
}
$this->response_data = $this->response($ret_value);
}
}