/
main.php
377 lines (274 loc) · 8.73 KB
/
main.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* Tests the REST API in phpList with common functions
* The test is using a client rest approach with the Snoopy-object-class
* Creating list, user, message
* And cleaning up after
*/
include 'phplist_restapi_helper.php';
$plugin = $GLOBALS["plugins"][$_GET["pi"]];
$url = apiUrl( $website );
$login = getConfig('restapi_test_login');
$password = getConfig('restapi_test_password');
if (empty($login)) {
print Error(s('Please configure the login details in the settings page'));
return;
}
?>
<html>
<h1>RESTAPI TEST</h1>
<h2>Step 1 - Login</h2>
<?php
//Get the loginname and password!
$id = $_SESSION["logindetails"]["id"];
$api = new phpList_RESTAPI_Helper( $url );
$result = $api->login( $login, $password );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
?>
<h2>Step 2 - Count existing lists</h2>
<?php
$result = $api->listsGet();
if ($result->status != 'success'){
echo $result->data->message;
return;
}
//Present the lists fetched
echo "Number of lists is " . count($result->data);
foreach($result->data as $key => $list){
if ( $key == 0 ) echo ' ( ';
if ( $key > 0 && $key < count( $result->data ) ) echo ' , ';
echo $list->name;
if ( $key > 0 && $key == count( $result->data ) - 1 ) echo ' ) ';
}
?>
<h2>Step 3 - Create a new list</h2>
<?php
$result = $api->listAdd( 'Testlist', 'This is just a test');
if ($result->status != 'success'){
echo $result->data->message;
return;
}
$list_id = $result->data->id;
echo 'The new list ID = ' . $result->data->id;
?>
<h2>Step 4 - Change the name of the list</h2>
<?php
$result = $api->listUpdate( $list_id, 'Testlist ' . date('Y-m-d H:i:s'));
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'The new list has a new name: ' . $result->data->name;
?>
<h2>Step 5 - Count users / subscribers</h2>
<?php
$result = $api->usersGet();
if ($result->status != 'success'){
echo $result->data->message;
return;
}
//Present the lists fetched
echo "Total number of users: " . count($result->data);
?>
<h2>Step 6 - Check if your admin address is in users</h2>
<?php
$result = $api->userGetByEmail( $admin->email );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
$user_id = $result->data->id;
if (!$user_id){
//Add admin email to users
$result = $api->userAdd( $admin->email, 1, 1, '#PasswordNotSet#' );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
$user_id = $result->data->id;
echo "Admin Email (" . $admin->email . ") is in Users now with id = " . $user_id . '<br/>';
}
echo "Admin Email id = " . $user_id;
?>
<h2>Step 7 - Change user password</h2>
<?php
$result = $api->userUpdate( $user_id, $admin->email, 1, 1, '#NewPassword#' );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'The Admin User password is changed!';
?>
<h2>Step 8 - Add User to list</h2>
<?php
$result = $api->listUserAdd( $list_id, $user_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'The Admin User is assigned to the new list created. ';
?>
<h2>Step 9 - Lists where the user is assigned</h2>
<?php
$result = $api->listsUser( $user_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
foreach($result->data as $key => $list){
if ( $key > 0 && $key < count( $result->data ) ) echo ' , ';
echo $list->name;
}
?>
<h2>Step 10 - Check if template for test exists (create)</h2>
<?php
$result = $api->templateGetByTitle( 'API Test' );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
$template_id = $result->data->id;
if ( !$template_id ){
$template_content = file_get_contents(__DIR__ . '/template.html');
$result = $api->templateAdd( 'API Test', $template_content );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'New template created for test.<br/>';
$template_id = $result->data->id;
}
$result = $api->templateGet( $template_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'Title for template (ID=' . $result->data->id . '): ' . $result->data->title;
?>
<h2>Step 11 - Update template title</h2>
<?php
$template_content = file_get_contents(__DIR__ . '/template.html');
$result = $api->templateUpdate( $template_id, 'API Test ' . date('Y-m-d H:i:s'), $template_content );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'Title for template (ID=' . $result->data->id . '): ' . $result->data->title;
?>
<h2>Step 12 - List all templates</h2>
<?php
$result = $api->templatesGet();
if ($result->status != 'success'){
echo $result->data->message;
return;
}
foreach($result->data as $key => $template){
if ( $key > 0 && $key < count( $result->data ) ) echo ' , ';
echo $template->title;
}
?>
<h2>Step 13 - Create a message</h2>
<?php
$result = $api->messageAdd( 'Test API from plugin', $admin->email, $admin->email, 'TEST API', 'TEST API', $template_id, date('Y-m-d H:i:s'));
if ($result->status != 'success'){
echo $result->data->message;
return;
}
$message_id = $result->data->id;
echo 'Message created (ID=' . $message_id . ').';
?>
<h2>Step 14 - Update message</h2>
<?php
$result = $api->messageUpdate( $message_id, 'Test API from plugin updated ' .date('Y-m-d H:i:s'), $admin->email, $admin->email, 'TEST API', 'TEST API', $template_id, date('Y-m-d H:i:s'), 'submitted', $user_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'Message updated and submitted (ID=' . $result->data->id . ').';
?>
<h2>Step 15 - Messages count</h2>
<?php
$result = $api->messagesGet();
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'Messages in phpList: ' . count($result->data) . '.';
?>
<h2>Step 16 - Assign message to list</h2>
<?php
$result = $api->listMessageAdd( $list_id, $message_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'Message assigned to the created list.';
?>
<h2>Step 17 - Process Queue</h2>
<?php
$result = $api->processQueue( $admin->loginname, $admin->password, true );
sleep(5);
?>
<h2>Step 18 - Unassign user from list</h2>
<?php
$result = $api->listUserDelete( $list_id, $user_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo $result->data . '.';
?>
<h2>Step 19 - Delete the user</h2>
<?php
$result = $api->userDelete( $user_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'Admin deleted from Users.';
?>
<h2>Step 20 - Delete the list</h2>
<?php
$api->listDelete( $list_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'The new list is now deleted';
?>
<h2>Step 21 - Delete template</h2>
<?php
$api->templateDelete( $template_id );
if ($result->status != 'success'){
echo $result->data->message;
return;
}
echo 'The template is now deleted';
?>
</html>
<?php
/**
* @return $trimmedUrl
*/
function apiUrl( $website ){
$url = '';
if( !empty( $_SERVER["HTTPS"] ) ){
// Check which protocol to use
if($_SERVER["HTTPS"]!=="off") {
$url = 'https://'; //https
} else {
$url = 'http://'; //http
}
} else {
$url = 'http://'; //http
}
$api_url = str_replace( 'page=main&pi=restapi_test', 'page=call&pi=restapi', $_SERVER['REQUEST_URI'] );
$api_url1 = str_replace( 'page=main&pi=restapi', 'page=call&pi=restapi', $api_url );
$concatenatedUrl = $url . $website . $api_url1;
$trimmedUrl = rtrim($concatenatedUrl,'/');
return $trimmedUrl;
}
?>