/
AttributeChangerPlugin.php
executable file
·547 lines (373 loc) · 16.8 KB
/
AttributeChangerPlugin.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?php
include_once('AttributeChangerPlugin/ADD_ALLOWED_ATTRIBUTE_VALUES_PAGE.php');
/*
ALSO WANT TO ADD ALLOWED ATTRIBUTE VALUES DYNAMICAL << on this now
NEED TO MAKE SURE DOES NOT GIVE ERROR IF THE EMAIL IS THE ONLY ATTRIBUTE IN THE DB
ALSO HTMLSPECIALCHARS EVURYTHING
*/
class AttributeChangerPlugin extends phplistPlugin {
// File location, selected attribute column matches, all entry data, index by id
// $sessions;
// $session_increment;
public $name = "AttributeChangerPlugin";
public $pageTitles = array( // Entries in the plugin menu of the dashboard
'front_page' => 'Begin Data Collection',
);
public $topMenuLinks = array( // Entries in the top menu at the top of each page
'front_page' => array('category' => 'subscribers'),
);
public $description = 'Begin Data Collection';
public $Current_Session = null;
public $AttributeChangerData = null;
//THIS SHOULD BE UPGRADED TO A GENERIC TYPE AS PER THE TABLE SETTINGS
function __construct()
{
parent::__construct();
$this->coderoot = dirname(__FILE__).'/AttributeChangerPlugin/';
require_once(dirname(__FILE__).'/AttributeChangerPlugin/Single_Session.php');
$this->attribute_changer_tablename = $GLOBALS['table_prefix'].'plugins_attribute_changer_plugin';
$this->AttributeChangerData = array(
'tables' => array(
'user' => $GLOBALS['tables']['user'],
'attribute' => $GLOBALS['tables']['attribute'],
'user_attribute' => $GLOBALS['tables']['user_attribute'],
),
'table_prefix' => $GLOBALS['table_prefix'],
'atribute_value_table_prefix' => $GLOBALS['table_prefix']."listattr_",
'case_array' => array(
'textarea' => 'case_1',
'textline' => 'case_1',
'hidden' => 'case_1',
'date' => 'case_1',
'checkbox' => 'case_1',
'radio' => 'case_2',
'select' => 'case_2',
'checkboxgroup' => 'case_3',
),
'attribute_changer_tablename' => $GLOBALS['table_prefix'].'plugins_attribute_changer_plugin',
'attribute_changer_table_structure' => array(
"id" => array("integer not null primary key auto_increment","ID"),
"adminid" => array("integer not null","adminid"),
"value" => array('longtext',''),
),
'displayAmounts'=> array(
10=>10,
100=>100,
1000=>1000,
10000=>10000,
'all'=>'all',
),
'PLUGIN_CLASS_DIR' => dirname(__FILE__),
'www_files_dir' => '/lists/admin/plugins/AttributeChangerPlugin/',
'PLUGIN_FILES_DIR' => dirname(__FILE__).'/AttributeChangerPlugin/',
'temp_uploads_dir' => dirname(__FILE__).'/AttributeChangerPlugin/temp_table_uploads',
);
$GLOBALS['AttributeChangerPlugin'] = $this;
}
function adminMenu() {
return $this->pageTitles;
}
function print_something() {
print('something');
}
//SAME AS admintoken struct expect missing entered and expires
// public $attribute_changer_table_structure = array(
// "id" => array("integer not null primary key auto_increment","ID"),
// "adminid" => array("integer not null","adminid"),
// "value" => array('longtext',''),
// );
function Serialize_And_Store() {
print("arara222ra<br>");
if(!isset($this->Current_Session) || $this->Current_Session == null) {
print("ERR NO SESION");
die();
}
$session_s = base64_encode(serialize($this->Current_Session));
$truncate_query = sprintf("truncate table %s", $this->AttributeChangerData['attribute_changer_tablename']);
Sql_Query($truncate_query);
$serialize_insert_query = sprintf("insert into %s (value) values ('%s')", $this->AttributeChangerData['attribute_changer_tablename'], $session_s);
Sql_Query($serialize_insert_query);
}
function Retreive_And_Unserialize() {
$retrieve_serialized_query = sprintf("select value from %s", $this->AttributeChangerData['attribute_changer_tablename']);
$retrieve_s_return = Sql_Query($retrieve_serialized_query);
if(!$retrieve_s_return) {
print("ERROR NO STORED SESSION");
die();
}
$returned_result = Sql_Fetch_Assoc($retrieve_s_return);
if(!isset($returned_result['value'])) {
print("ERROR Improperly stored value data");
die();
}
//print_r($returned_result);
$serialized_session = $returned_result['value'];
//print($serialized_session);
$this->Current_Session = unserialize(base64_decode($serialized_session));
}
function Test_Create_Session_Table() {
$attribute_changer_tablename = $this->AttributeChangerData['attribute_changer_tablename'];
if(!Sql_Check_For_Table($attribute_changer_tablename)) {
Sql_create_Table ($attribute_changer_tablename, $this->AttributeChangerData['attribute_changer_table_structure']);
}
}
function New_Session($session_create_table_query ) {
$this->Test_Create_Session_Table();
if($this->Current_Session != null) {
if(!empty($this->Current_Session->file_location)) {
if(is_file($this->Current_Session->file_location)) {
unlink($this->Current_Session->file_location);
}
}
}
$this->Current_Session = new Single_Session;
return $this->Current_Session;
}
function Close_Session() {
if($this->Current_Session != null) {
if(isset($this->Current_Session->file_location)) {
if(is_file($this->Current_Session->file_location)) {
unlink($this->Current_Session->file_location);
}
}
$this->Current_Session = null;
}
}
function Test_Create_Temp_Dir() {
$temp_dir = $this->AttributeChangerData['temp_uploads_dir'];
$attribute_changer_dir = $this->AttributeChangerData['PLUGIN_FILES_DIR'];
if(!file_exists($attribute_changer_dir)) {
return false;
}
else if(!is_dir($attribute_changer_dir)) {
return false;
}
else{
if(!file_exists($temp_dir)) {
if(!mkdir($temp_dir)){
$error = error_get_last();
print($error['message']);
}
return true;
}
else{
if(is_dir($temp_dir)) {
return true;
}
else {
return false;
}
}
}
}
//now must use new format for entries --- > pass with [id]=>[value]
function Test_Entry($entry) {
if($this->Current_Session == null) {
return "ERROR NO CURRENT SESSION";
}
if(!array_key_exists("email", $entry) || !filter_var($entry['email'], FILTER_VALIDATE_EMAIL)) {
return false;
}
$email = $entry['email'];
unset($entry['email']);
$changing_attributes = array();
$entry_query = sprintf('select id from %s where email = "%s"', $this->AttributeChangerData['tables']['user'], $email);
$user_sql_result = Sql_Fetch_Row_Query($entry_query);
if(!$user_sql_result[0]) {
$is_new_entry = true;
}
else{
$is_new_entry = false;
}
foreach ($entry as $attribute_id => $value_array) {
if( ($return_values = $this->Test_Attribute_Values($email, $is_new_entry, $attribute_id, $value_array)) != false ) {
//print_r($return_values);
$changing_attributes[$attribute_id] = $return_values;
}
}
if(!$user_sql_result[0]) {
print('<br>'.$email.'<br>');
$this->Add_New_Entry($email, $changing_attributes);
print('<br>'.'<br>');
print_r($changing_attributes);
print('<br>');
}
else{
$this->Add_Modify_Entry($email, $changing_attributes);
}
}
// //automatically checks for compliance with values and return array with ids where needed
function Test_Attribute_Values($email, $is_new_entry, $attribute_id, $value_array) {
$case_array = $this->AttributeChangerData['case_array'];
if($this->Current_Session == null) {
return false;
}
$Session = $this->Current_Session;
if(!isset($Session->attribute_list[$attribute_id])) {
//print("<br>att id<br>".$attribute_id.'<br>');
//print_r($Session->attribute_list);
return false;
}
else{
$att = $Session->attribute_list[$attribute_id];
if(!is_array($value_array)) {
if($case_array[$att['type']] === 'case_2' || $case_array[$att['type']] === 'case_3' ) {
return false;
}
else{
return array($value_array);
}
}
else {
$return_values = array();
if($case_array[$att['type']] == 'case_2' || $case_array[$att['type']] == 'case_3' ) {
foreach ($value_array as $value) {
if(($value_id = array_search($value, $att['allowed_value_ids']))) {
if(!in_array($value_id, $return_values)) {
//print('<br>value id : '.$value_id);
array_push($return_values, $value_id);
}
}
else{
//ELSE IS NOT CURRENTLY AN ALLOWED VALUE, SO..
if(strlen(trim($value)) > 0) {
Process_New_Attribute_Value($email, $attribute_id, trim($value), $is_new_entry);
}
}
}
}
else {
foreach ($value_array as $value) {
if(!in_array($value, $return_values)) {
array_push($return_values, $value);
}
}
}
//print_r($return_values);
return $return_values;
}
return false;
}
}
/////////////////////////////
//HERE
function Add_Modify_Entry($email_key, $attribute_values) {
//print_r($attribute_values);
if($this->Current_Session == null) {
return "ERROR NO CURRENT SESSION";
}
$Session = $this->Current_Session;
$case_array = $this->AttributeChangerData['case_array'];
if($attribute_values == null || !is_array($attribute_values) || count($attribute_values) == 0 ) {
//do nothing, already have a user but theres nothing to updateprint
}
else{
if(!isset($Session->Current_User_Values[$email_key])) {
if($this->Get_Current_User_Values($email_key) == false) {
return false;
}
}
// print_r($this->Current_Session->Current_User_Values);
// print("<br>");
$new_entries = array();
$is_new_value = false;
foreach ($attribute_values as $attribute_id => $attribute_value_array) {
$new_entries[$attribute_id] = array();
foreach ($attribute_value_array as $value) {
if($case_array[$Session->attribute_list[$attribute_id]['type']] === 'case_1' || $case_array[$Session->attribute_list[$attribute_id]['type']] === 'case_2') {
if($value != $Session->Current_User_Values[$email_key][$attribute_id]) {
array_push($new_entries[$attribute_id], $value);
$is_new_value = true;
}
}
else if(!in_array($value, $Session->Current_User_Values[$email_key][$attribute_id])) {
array_push($new_entries[$attribute_id], $value);
$is_new_value = true;
}
}
}
if($is_new_value == true) {
if(!isset($Session->Modify_Entry_List[$email_key])) {
$Session->Modify_Entry_List[$email_key] = array();
}
foreach ($new_entries as $attribute_id => $value_array) {
if(!isset($Session->Modify_Entry_List[$email_key][$attribute_id])) {
$Session->Modify_Entry_List[$email_key][$attribute_id] = array();
}
foreach ($value_array as $value) {
if(!in_array($value, $Session->Modify_Entry_List[$email_key][$attribute_id])) {
array_push($Session->Modify_Entry_List[$email_key][$attribute_id], $value);
}
}
}
}
}
}
function Get_Current_User_Values($email_key) {
$Session = $this->Current_Session;
$case_array = $this->AttributeChangerData['case_array'];
if($this->Current_Session == null) {
return "ERROR NO CURRENT SESSION";
}
$current_user_query = sprintf('select id from %s where email = "%s"', $this->AttributeChangerData['tables']['user'], $email_key);
$current_user_sql_result = Sql_Fetch_Row_Query($current_user_query);
if(!$current_user_sql_result[0]) {
return false;
}
if(!isset($Session->Current_User_Values[$email_key])) {
$Session->Current_User_Values[$email_key] = array();
$Session->Current_User_Values[$email_key]['id'] = $current_user_sql_result[0];
foreach ($Session->attribute_list as $attribute_id => $attribute_data) {
$Current_User_Value_Query = sprintf("select value from %s where attributeid = %d and userid = %s", $this->AttributeChangerData['tables']['user_attribute'], $attribute_id, $current_user_sql_result[0]);
$current_attribute_return = Sql_Fetch_Row_Query($Current_User_Value_Query);
if(!$current_attribute_return[0]) {
continue;
}
else{
$Session->Current_User_Values[$email_key][$attribute_id] = array();
if($case_array[$attribute_data['type']] == 'case_3') {
$exploded_current_values_ids = explode(',', $current_attribute_return[0]);
$Session->Current_User_Values[$email_key][$attribute_id] = $exploded_current_values_ids;
}
else {
$Session->Current_User_Values[$email_key][$attribute_id] = $current_attribute_return[0];
}
}
}
}
return true;
}
//attribute values array([att_id] => values), values have been test all are acceptable
function Add_New_Entry($email_key, $attribute_values) {
if($this->Current_Session == null) {
return "ERROR NO CURRENT SESSION";
}
$Session = $this->Current_Session;
if($attribute_values == null || !is_array($attribute_values) || count($attribute_values) == 0 ) {
//just set email
if(!isset($Session->New_Entry_List[$email_key])) {
$Session->New_Entry_List[$email_key] = array();
}
//else no need to do anything
return true;
}
else{
foreach ($attribute_values as $attribute_id => $single_attribute_values) {
if(!is_array($single_attribute_values)) {
//shouldnt happen
}
else{
if(!isset($Session->New_Entry_List[$email_key][$attribute_id])) {
$Session->New_Entry_List[$email_key][$attribute_id] = array();
}
foreach ($single_attribute_values as $numberic_key => $single_value) {
if(!in_array($single_value, $Session->New_Entry_List[$email_key][$attribute_id])) {
array_push($Session->New_Entry_List[$email_key][$attribute_id], $single_value);
}
}
}
}
}
}
}
?>