convertSelectionsToEpocheTimeInPostData() is a wrapper and only needs the POST field...
[mailer.git] / inc / libs / network_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 11/04/2009 *
4  * ===================                          Last change: 11/04/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : network_functions.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for ext-network                        *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer ext-network                      *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Private setter for current network id
44 function setCurrentNetworkId ($networkId) {
45         $GLOBALS['current_network_id'] = bigintval($networkId);
46 }
47
48 // Private getter for current network id
49 function getCurrentNetworkId () {
50         return $GLOBALS['current_network_id'];
51 }
52
53 // Detects if a supported form has been sent
54 function detectNetworkProcessForm () {
55         // 'do' must be provided in URL
56         if (!isGetRequestElementSet('do')) {
57                 // Not provided!
58                 reportBug(__FUNCTION__, __LINE__, 'No "do" has been provided. Please fix your templates.');
59         } // END - if
60
61         // Default is invalid
62         $GLOBALS['network_form_name'] = 'invalid';
63
64         // Now search all valid
65         foreach (array('save_config', 'add', 'edit', 'delete', 'do_edit', 'do_delete') as $formName) {
66                 // Is it detected
67                 if (isFormSent($formName)) {
68                         // Use this form name
69                         $GLOBALS['network_form_name'] = $formName;
70
71                         // Remove it generically here
72                         unsetPostRequestElement($formName);
73
74                         // Abort loop
75                         break;
76                 } // END - if
77         } // END - foreach
78
79         // Has the form being detected?
80         if ($GLOBALS['network_form_name'] == 'invalid') {
81                 // Not supported
82                 reportBug(__FUNCTION__, __LINE__, 'POST form could not be detected, postData=<pre>' . print_r(postRequestArray(), TRUE));
83         } // END - if
84 }
85
86 // Handle a (maybe) sent form here
87 function doNetworkHandleForm () {
88         // Is there a form sent?
89         if (countRequestPost() > 0) {
90                 // Detect sent POST form
91                 detectNetworkProcessForm();
92         } elseif (!isGetRequestElementSet('do')) {
93                 // Skip any further requests
94                 return;
95         }
96
97         // Process the request
98         doAdminNetworkProcessForm();
99 }
100
101 // Processes an admin form
102 function doAdminNetworkProcessForm () {
103         // Create function name
104         $functionName = sprintf("doAdminNetworkProcess%s", capitalizeUnderscoreString(getRequestElement('do')));
105
106         // Is the function valid?
107         if (!function_exists($functionName)) {
108                 // Invalid function name
109                 reportBug(__FUNCTION__, __LINE__, 'Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', FALSE);
110         } // END - if
111
112         // Init global arrays
113         $GLOBALS['network_types_disabled'] = array();
114
115         // Call-back the method handling our request
116         call_user_func($functionName);
117 }
118
119 // Checks whether the (short) network name is already used (valid)
120 function isNetworkNameValid ($name) {
121         // Is there cache?
122         if (!isset($GLOBALS[__FUNCTION__][$name])) {
123                 // Does it exist?
124                 $GLOBALS[__FUNCTION__][$name] = (countSumTotalData($name, 'network_data', 'network_id', 'network_short_name', TRUE) == 1);
125         } // END - if
126
127         // Return result
128         return $GLOBALS[__FUNCTION__][$name];
129 }
130
131 // Checks whether the (short) named network is activated
132 function isNetworkActiveByShortName ($name) {
133         // Is there cache?
134         if (!isset($GLOBALS[__FUNCTION__][$name])) {
135                 // Does it exist?
136                 $GLOBALS[__FUNCTION__][$name] = ((isNetworkNameValid($name)) && (countSumTotalData($name, 'network_data', 'network_id', 'network_short_name', TRUE, " AND `network_active`='Y'") == 1));
137         } // END - if
138
139         // Return result
140         return $GLOBALS[__FUNCTION__][$name];
141 }
142
143 // Checks whether the network by given id is activated
144 function isNetworkActiveById ($networkId) {
145         // Is there cache?
146         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
147                 // Does it exist?
148                 $GLOBALS[__FUNCTION__][$networkId] = (countSumTotalData(bigintval($networkId), 'network_data', 'network_id', 'network_id', TRUE, " AND `network_active`='Y'") == 1);
149         } // END - if
150
151         // Return result
152         return $GLOBALS[__FUNCTION__][$networkId];
153 }
154
155 // "Getter" for 'network_activated' column depending on current administrator's expert setting
156 function getNetworkActivatedColumn ($whereAnd = 'WHERE', $table = '') {
157         // Is there cache?
158         if (!isset($GLOBALS[__FUNCTION__][$whereAnd][$table])) {
159                 // Default is exclude deactivated networks
160                 $GLOBALS[__FUNCTION__][$whereAnd][$table] = ' ' . $whereAnd . ' ' . $table . "`network_active`='Y'";
161
162                 // Is the export setting on?
163                 if (isAdminsExpertSettingEnabled()) {
164                         // Then allow all networks
165                         $GLOBALS[__FUNCTION__][$whereAnd][$table] = '';
166                 } // END - if
167         } // END - if
168
169         // Return cache
170         return $GLOBALS[__FUNCTION__][$whereAnd][$table];
171 }
172
173 // Checks whether the given network type is already used (valid)
174 function isNetworkTypeHandleValid ($type, $networkId) {
175         // Query for it
176         $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handler`='%s' LIMIT 1",
177                 array(
178                         $networkId,
179                         $type
180                 ), __FUNCTION__, __LINE__);
181
182         // Does it exist?
183         $isValid = (SQL_NUMROWS($result) == 1);
184
185         // Free result
186         SQL_FREERESULT($result);
187
188         // Return result
189         return $isValid;
190 }
191
192 // Checks whether the given network request parameter is already used (valid)
193 function isNetworkRequestElementValid ($key, $type, $networkId) {
194         // Query for it
195         $result = SQL_QUERY_ESC("SELECT `network_request_param_id` FROM `{?_MYSQL_PREFIX?}_network_request_params` WHERE `network_id`=%s AND `network_type_id`=%s AND `network_request_param_key`='%s' LIMIT 1",
196                 array(
197                         $networkId,
198                         $type,
199                         $key
200                 ), __FUNCTION__, __LINE__);
201
202         // Does it exist?
203         $isValid = (SQL_NUMROWS($result) == 1);
204
205         // Free result
206         SQL_FREERESULT($result);
207
208         // Return result
209         return $isValid;
210 }
211
212 // Checks whether the given vcheck request parameter is already used (valid)
213 function isNetworkVcheckElementValid ($key, $networkId) {
214         // Query for it
215         $result = SQL_QUERY_ESC("SELECT `network_vcheck_param_id` FROM `{?_MYSQL_PREFIX?}_network_vcheck_params` WHERE `network_id`=%s AND `network_vcheck_param_key`='%s' LIMIT 1",
216                 array(
217                         $networkId,
218                         $key
219                 ), __FUNCTION__, __LINE__);
220
221         // Does it exist?
222         $isValid = (SQL_NUMROWS($result) == 1);
223
224         // Free result
225         SQL_FREERESULT($result);
226
227         // Return result
228         return $isValid;
229 }
230
231 // Checks whether the given network API array translation
232 function isNetworkArrayTranslationValid ($key, $type, $networkId) {
233         // Query for it
234         $result = SQL_QUERY_ESC("SELECT `network_array_id` FROM `{?_MYSQL_PREFIX?}_network_array_translation` WHERE `network_id`=%s AND `network_type_id`=%s AND `network_array_index`='%s' LIMIT 1",
235                 array(
236                         $networkId,
237                         $type,
238                         $key
239                 ), __FUNCTION__, __LINE__);
240
241         // Does it exist?
242         $isValid = (SQL_NUMROWS($result) == 1);
243
244         // Free result
245         SQL_FREERESULT($result);
246
247         // Return result
248         return $isValid;
249 }
250
251 // "Getter" for a network's data by provided id number
252 function getNetworkDataById ($networkId, $column = '') {
253         // Ids lower one are not accepted
254         if ($networkId < 1) {
255                 // Not good, should be fixed
256                 reportBug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
257         } elseif ((!isNetworkActiveById($networkId)) && (!isAdminsExpertSettingEnabled())) {
258                 // Do not load inactive network data
259                 reportBug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is not active.');
260         }
261
262         // Set current network id
263         setCurrentNetworkId($networkId);
264
265         // Is it cached?
266         if (!isset($GLOBALS['network_data'][$networkId])) {
267                 // By default we have no data
268                 $GLOBALS['network_data'][$networkId] = array();
269
270                 // Query for the network data
271                 $result = SQL_QUERY_ESC('SELECT
272         `network_id`,
273         `network_short_name`,
274         `network_title`,
275         `network_reflink`,
276         `network_data_separator`,
277         `network_row_separator`,
278         `network_request_type`,
279         `network_charset`,
280         `network_require_id_card`,
281         `network_query_amount`
282 FROM
283         `{?_MYSQL_PREFIX?}_network_data`
284 WHERE
285         `network_id`=%s
286 LIMIT 1',
287                         array(bigintval($networkId)), __FUNCTION__, __LINE__);
288
289                 // Is there an entry?
290                 if (SQL_NUMROWS($result) == 1) {
291                         // Then get it
292                         $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
293                 } // END - if
294
295                 // Free result
296                 SQL_FREERESULT($result);
297         } // END - if
298
299         // Return result
300         if ((empty($column)) && (isset($GLOBALS['network_data'][$networkId]))) {
301                 // Return array
302                 return $GLOBALS['network_data'][$networkId];
303         } elseif (isset($GLOBALS['network_data'][$networkId][$column])) {
304                 // Return column
305                 return $GLOBALS['network_data'][$networkId][$column];
306         }
307
308         // Return NULL
309         return NULL;
310 }
311
312 // "Getter" for a network's data by provided type id number
313 function getNetworkDataByTypeId ($networkId, $column = '') {
314         // Ids lower one are not accepted
315         if ($networkId < 1) {
316                 // Not good, should be fixed
317                 reportBug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
318         } elseif ((!isNetworkActiveById($networkId)) && (!isAdminsExpertSettingEnabled())) {
319                 // Do not load inactive network data
320                 reportBug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is not active.');
321         }
322
323         // Set current network id
324         setCurrentNetworkId($networkId);
325
326         // Is it cached?
327         if (!isset($GLOBALS['network_data'][$networkId])) {
328                 // By default we have no data
329                 $GLOBALS['network_data'][$networkId] = array();
330
331                 // Query for the network data
332                 $result = SQL_QUERY_ESC('SELECT
333         d.`network_id`,
334         d.`network_short_name`,
335         d.`network_title`,
336         d.`network_reflink`,
337         d.`network_data_separator`,
338         d.`network_row_separator`,
339         d.`network_request_type`,
340         d.`network_charset`,
341         d.`network_require_id_card`,
342         d.`network_query_amount`,
343         t.`network_type_handler`,
344         t.`network_type_api_url`,
345         t.`network_type_click_url`,
346         t.`network_type_banner_url`,
347         t.`network_text_encoding`
348 FROM
349         `{?_MYSQL_PREFIX?}_network_data` AS d
350 LEFT JOIN
351         `{?_MYSQL_PREFIX?}_network_types` AS t
352 ON
353         d.`network_id`=t.`network_id`
354 WHERE
355         t.`network_type_id`=%s
356 LIMIT 1',
357                         array(bigintval($networkId)), __FUNCTION__, __LINE__);
358
359                 // Is there an entry?
360                 if (SQL_NUMROWS($result) == 1) {
361                         // Then get it
362                         $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
363                 } // END - if
364
365                 // Free result
366                 SQL_FREERESULT($result);
367         } // END - if
368
369         // Return result
370         if (empty($column)) {
371                 // Return array
372                 return $GLOBALS['network_data'][$networkId];
373         } else {
374                 // Return column
375                 return $GLOBALS['network_data'][$networkId][$column];
376         }
377 }
378
379 // "Getter" for a network type data by provided id number
380 function getNetworkTypeDataById ($networkTypeId) {
381         // Ids lower one are not accepted
382         if ($networkTypeId < 1) {
383                 // Not good, should be fixed
384                 reportBug(__FUNCTION__, __LINE__, 'Network type id ' . $networkTypeId . ' is smaller than 1.');
385         } // END - if
386
387         // By default we have no data
388         $GLOBALS['network_type_data'][$networkTypeId] = array();
389
390         // Query for the network data
391         $result = SQL_QUERY_ESC('SELECT
392         `network_type_id`,
393         `network_id`,
394         `network_type_handler`,
395         `network_type_api_url`,
396         `network_type_click_url`,
397         `network_type_banner_url`
398 FROM
399         `{?_MYSQL_PREFIX?}_network_types`
400 WHERE
401         `network_type_id`=%s
402 LIMIT 1',
403                 array(bigintval($networkTypeId)), __FUNCTION__, __LINE__);
404
405         // Is there an entry?
406         if (SQL_NUMROWS($result) == 1) {
407                 // Then get it
408                 $GLOBALS['network_type_data'][$networkTypeId] = SQL_FETCHARRAY($result);
409         } // END - if
410
411         // Free result
412         SQL_FREERESULT($result);
413
414         // Return result
415         return $GLOBALS['network_type_data'][$networkTypeId];
416 }
417
418 // "Getter" for a network request parameter data by provided id number
419 function getNetworkRequestParamsDataById ($networkRequestId) {
420         // Ids lower one are not accepted
421         if ($networkRequestId < 1) {
422                 // Not good, should be fixed
423                 reportBug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkRequestId . ' is smaller than 1.');
424         } // END - if
425
426         // By default we have no data
427         $networkRequestData = array();
428
429         // Query for the network data
430         $result = SQL_QUERY_ESC('SELECT
431         `network_request_param_id`,
432         `network_id`,
433         `network_type_id`,
434         `network_request_param_key`,
435         `network_request_param_value`,
436         `network_request_param_default`
437 FROM
438         `{?_MYSQL_PREFIX?}_network_request_params`
439 WHERE
440         `network_request_param_id`=%s
441 LIMIT 1',
442                 array(bigintval($networkRequestId)), __FUNCTION__, __LINE__);
443
444         // Is there an entry?
445         if (SQL_NUMROWS($result) == 1) {
446                 // Then get it
447                 $networkRequestData = SQL_FETCHARRAY($result);
448         } // END - if
449
450         // Free result
451         SQL_FREERESULT($result);
452
453         // Return result
454         return $networkRequestData;
455 }
456
457 // "Getter" for a network array translation data by provided id number
458 function getNetworkArrayTranslationsDataById ($networkTranslationId) {
459         // Ids lower one are not accepted
460         if ($networkTranslationId < 1) {
461                 // Not good, should be fixed
462                 reportBug(__FUNCTION__, __LINE__, 'Network array translation id ' . $networkTranslationId . ' is smaller than 1.');
463         } // END - if
464
465         // By default we have no data
466         $networkTranslationData = array();
467
468         // Query for the network data
469         $result = SQL_QUERY_ESC('SELECT
470         `network_array_id`,
471         `network_id`,
472         `network_type_id`,
473         `network_array_index`,
474         `network_array_sort`
475 FROM
476         `{?_MYSQL_PREFIX?}_network_array_translation`
477 WHERE
478         `network_array_id`=%s
479 LIMIT 1',
480                 array(bigintval($networkTranslationId)), __FUNCTION__, __LINE__);
481
482         // Is there an entry?
483         if (SQL_NUMROWS($result) == 1) {
484                 // Then get it
485                 $networkTranslationData = SQL_FETCHARRAY($result);
486         } // END - if
487
488         // Free result
489         SQL_FREERESULT($result);
490
491         // Return result
492         return $networkTranslationData;
493 }
494
495 // Updates given network (id) with data from array
496 function doNetworkUpdateDataByArray ($networkId, $networkData) {
497         // Ids lower one are not accepted
498         if ($networkId < 1) {
499                 // Not good, should be fixed
500                 reportBug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
501         } // END - if
502
503         // Just call our inner method
504         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($networkId)), array(), FALSE, FALSE);
505 }
506
507 // Updates given network type handler (id) with data from array
508 function doNetworkUpdateTypeByArray ($networkTypeId, $networkTypeData) {
509         // Ids lower one are not accepted
510         if ($networkTypeId < 1) {
511                 // Not good, should be fixed
512                 reportBug(__FUNCTION__, __LINE__, 'Network type handler id ' . $networkTypeId . ' is smaller than 1.');
513         } // END - if
514
515         // Just call our inner method
516         return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($networkTypeId)), array(), FALSE, FALSE);
517 }
518
519 // Updates given network request parameters (id) with data from array
520 function doNetworkUpdateParamsByArray ($networkParamsId, $networkParamsData) {
521         // Ids lower one are not accepted
522         if ($networkParamsId < 1) {
523                 // Not good, should be fixed
524                 reportBug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkParamsId . ' is smaller than 1.');
525         } // END - if
526
527         // Just call our inner method
528         return adminSaveSettings($networkParamsData, '_network_request_params', sprintf("`network_request_param_id`=%s", bigintval($networkParamsId)), array(), FALSE, FALSE);
529 }
530
531 // Updates given network array translations (id) with data from array
532 function doNetworkUpdateArrayTranslationsByArray ($networkTranslationsId, $networkTranslationsData) {
533         // Ids lower one are not accepted
534         if ($networkTranslationsId < 1) {
535                 // Not good, should be fixed
536                 reportBug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkTranslationsId . ' is smaller than 1.');
537         } // END - if
538
539         // Just call our inner method
540         return adminSaveSettings($networkTranslationsData, '_network_array_translation', sprintf("`network_array_id`=%s", bigintval($networkTranslationsId)), array(), FALSE, FALSE);
541 }
542
543 // Removes given network entry
544 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
545         // Remove the entry
546         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
547                 array(
548                         $table,
549                         $column,
550                         $id,
551                         $limit
552                 ), __FUNCTION__, __LINE__);
553
554         // Return affected rows
555         return SQL_AFFECTEDROWS();
556 }
557
558 // Generates a list of networks for given script and returns it
559 function generateAdminNetworkList () {
560         // Init output
561         $content = '';
562
563         // Query for all networks
564         $result = SQL_QUERY('SELECT
565         `network_id`,
566         `network_short_name`,
567         `network_title`
568 FROM
569         `{?_MYSQL_PREFIX?}_network_data`
570 ' . getNetworkActivatedColumn() . '
571 ORDER BY
572         `network_short_name` ASC', __FUNCTION__, __LINE__);
573
574         // Are there entries?
575         if (!SQL_HASZERONUMS($result)) {
576                 // List all entries
577                 $rows = array();
578                 while ($row = SQL_FETCHARRAY($result)) {
579                         // Is this valid, then add it
580                         if ((is_array($row)) && (isset($row['network_id']))) {
581                                 // Add entry
582                                 $rows[$row['network_id']] = $row;
583                         } // END - if
584                 } // END - while
585
586                 // Generate the selection box
587                 $content = generateSelectionBoxFromArray($rows, 'network_id', 'network_id', '', '', 'network');
588         } else {
589                 // Nothing selected
590                 $content = loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_ENTRIES_404--}');
591         }
592
593         // Free the result
594         SQL_FREERESULT($result);
595
596         // Return the list
597         return $content;
598 }
599
600 // Generator (somewhat getter) for a list of network types for given network id
601 function generateAdminNetworkTypeList ($networkId) {
602         // Init content
603         $content = '';
604
605         // Query all types of this network
606         $result = SQL_QUERY_ESC('SELECT
607         `network_type_id`,
608         `network_type_handler`
609 FROM
610         `{?_MYSQL_PREFIX?}_network_types`
611 WHERE
612         `network_id`=%s
613         ' . getNetworkActivatedColumn('AND') . '
614 ORDER BY
615         `network_type_handler` ASC',
616                 array(
617                         bigintval($networkId)
618                 ), __FUNCTION__, __LINE__);
619
620         // Are there entries?
621         if (!SQL_HASZERONUMS($result)) {
622                 // List all entries
623                 $rows = array();
624                 while ($row = SQL_FETCHARRAY($result)) {
625                         // Is this valid, then add it
626                         if ((is_array($row)) && (isset($row['network_type_id']))) {
627                                 // Add entry
628                                 $rows[$row['network_type_id']] = $row;
629                         } // END - if
630                 } // END - while
631
632                 // Generate the selection box
633                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id');
634         } else {
635                 // Nothing selected
636                 $content = loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_ENTRIES_404--}');
637         }
638
639         // Free the result
640         SQL_FREERESULT($result);
641
642         // Return content
643         return $content;
644 }
645
646 // Generator (somewhat getter) for a list of network types for all types
647 function generateAdminDistinctNetworkTypeList () {
648         // Init content
649         $content = '';
650
651         // Query all types of this network
652         $result = SQL_QUERY('SELECT
653         t.`network_type_id`,
654         t.`network_type_handler`,
655         d.`network_title`
656 FROM
657         `{?_MYSQL_PREFIX?}_network_types` AS t
658 LEFT JOIN
659         `{?_MYSQL_PREFIX?}_network_data` AS d
660 ON
661         t.`network_id`=d.`network_id`
662 ' . getNetworkActivatedColumn('WHERE', 'd') . '
663 ORDER BY
664         d.`network_short_name` ASC,
665         t.`network_type_handler` ASC', __FUNCTION__, __LINE__);
666
667         // Are there entries?
668         if (!SQL_HASZERONUMS($result)) {
669                 // List all entries
670                 $rows = array();
671                 while ($row = SQL_FETCHARRAY($result)) {
672                         // Is this valid, then add it
673                         if ((is_array($row)) && (isset($row['network_type_id']))) {
674                                 // Add entry
675                                 $rows[$row['network_type_id']] = $row;
676                         } // END - if
677                 } // END - while
678
679                 // Generate the selection box
680                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id', '', '_title');
681         } else {
682                 // Nothing selected
683                 $content = loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_ENTRIES_404--}');
684         }
685
686         // Free the result
687         SQL_FREERESULT($result);
688         //* DEBUG: */ die('<pre>'.encodeEntities($content).'</pre>');
689
690         // Return content
691         return $content;
692 }
693
694 // Generator (somewhat getter) for network type options
695 function generateNetworkTypeOptions ($networkId) {
696         // Is this an array, then we just came back from edit/delete actions
697         if (is_array($networkId)) {
698                 // Set it as empty string
699                 $networkId = '';
700         } // END - if
701
702         // Is this cached?
703         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
704                 // Generate output and cache it
705                 $GLOBALS[__FUNCTION__][$networkId] = generateOptions(
706                         'network_types',
707                         'network_type_id',
708                         'network_type_handler',
709                         $networkId,
710                         '',
711                         sprintf(
712                                 "WHERE `network_id`=%s" . getNetworkActivatedColumn('AND'),
713                                 bigintval(getRequestElement('network_id'))
714                         ),
715                         '',
716                         'translateNetworkTypeHandler'
717                 );
718         } // END - if
719
720         // Return content
721         return $GLOBALS[__FUNCTION__][$networkId];
722 }
723
724 // Generates an options list of all available (hard-coded) handlers
725 function generateNetworkTypesAvailableOptions ($defaultType = NULL) {
726         // Is it cached?
727         if (!isset($GLOBALS[__FUNCTION__][$defaultType])) {
728                 // Generate list
729                 $GLOBALS[__FUNCTION__][$defaultType] = generateOptions(
730                         '/ARRAY/',
731                         array(
732                                 'banner',
733                                 'banner_click',
734                                 'banner_view',
735                                 'button',
736                                 'button_click',
737                                 'button_view',
738                                 'surfbar',
739                                 'surfbar_click',
740                                 'surfbar_view',
741                                 'forcedbanner',
742                                 'forcedtextlink',
743                                 'textlink',
744                                 'textlink_click',
745                                 'textlink_view',
746                                 'skybanner',
747                                 'skybanner_click',
748                                 'skybanner_view',
749                                 'halfbanner',
750                                 'halfbanner_click',
751                                 'halfbanner_view',
752                                 'layer',
753                                 'layer_click',
754                                 'layer_view',
755                                 'popup',
756                                 'popdown',
757                                 'textmail',
758                                 'htmlmail',
759                                 'lead',
760                                 'sale',
761                                 'lead_sale',
762                                 'payperactive',
763                                 'pagepeel',
764                                 'traffic',
765                                 'signature',
766                                 'signature_click',
767                                 'signature_view',
768                         ),
769                         array(),
770                         $defaultType,
771                         '', '',
772                         $GLOBALS['network_types_disabled'],
773                         'translateNetworkTypeHandler'
774                 );
775         } // END - if
776
777         // Return content
778         return $GLOBALS[__FUNCTION__][$defaultType];
779 }
780
781 // Generates an options list of all available (hard-coded) text encoders
782 function generateNetworkTextEncodingAvailableOptions ($defaultEncoding = NULL) {
783         // Is it cached?
784         if (!isset($GLOBALS[__FUNCTION__][$defaultEncoding])) {
785                 // Generate list
786                 $GLOBALS[__FUNCTION__][$defaultEncoding] = generateOptions(
787                         '/ARRAY/',
788                         array(
789                                 'NONE',
790                                 'BASE64',
791                         ),
792                         array(),
793                         $defaultEncoding,
794                         '', '',
795                         array(),
796                         'translateNetworkTextEncoding'
797                 );
798         } // END - if
799
800         // Return content
801         return $GLOBALS[__FUNCTION__][$defaultEncoding];
802 }
803
804 // Generates an options list (somewhat getter) for request keys
805 function generateNetworkRequestKeyOptions () {
806         // Is it cached?
807         if (!isset($GLOBALS[__FUNCTION__])) {
808                 // Generate and cache it
809                 $GLOBALS[__FUNCTION__] = generateOptions(
810                         '/ARRAY/',
811                         array(
812                                 'affiliate_id',
813                                 'sid',
814                                 'hash',
815                                 'password',
816                                 'reload',
817                                 'maximum_stay',
818                                 'minimum_stay',
819                                 'currency',
820                                 'type',
821                                 'remain',
822                                 'reward',
823                                 'size',
824                                 'erotic',
825                                 'extra',
826                                 'country'
827                         ),
828                         array(),
829                         '',
830                         '', '',
831                         $GLOBALS['network_request_params_disabled'],
832                         'translateNetworkRequestParameterKey'
833                 );
834         } // END - if
835
836         // Return content
837         return $GLOBALS[__FUNCTION__];
838 }
839
840 // Generates an options list for vcheck request keys
841 function generateNetworkVcheckKeyOptions () {
842         // Is it cached?
843         if (!isset($GLOBALS[__FUNCTION__])) {
844                 // Generate and cache it
845                 $GLOBALS[__FUNCTION__] = generateOptions(
846                         '/ARRAY/',
847                         array(
848                                 'network_key',
849                                 'sid',
850                                 'payment',
851                                 'remote_address',
852                                 'campaign_id',
853                                 'status',
854                                 'reason',
855                                 'type',
856                                 'network_name',
857                                 'extra_value1',
858                                 'extra_value2',
859                                 'extra_value3',
860                                 'extra_value4',
861                         ),
862                         array(),
863                         '',
864                         '', '',
865                         $GLOBALS['network_vcheck_params_disabled'],
866                         'translateNetworkVcheckParameterKey'
867                 );
868         } // END - if
869
870         // Return content
871         return $GLOBALS[__FUNCTION__];
872 }
873
874 // Generator (somewhat getter) for (return) array translation
875 function generateNetworkTranslationOptions ($default = '') {
876         // Is it cached?
877         if (!isset($GLOBALS[__FUNCTION__][$default])) {
878                 // Generate and cache it
879                 $GLOBALS[__FUNCTION__][$default] = generateOptions(
880                         'network_translations',
881                         'network_translation_id',
882                         'network_translation_name',
883                         $default,
884                         '',
885                         '',
886                         $GLOBALS['network_array_translation_disabled'],
887                         'translateNetworkTranslationName'
888                 );
889         } // END - if
890
891         // Return content
892         return $GLOBALS[__FUNCTION__][$default];
893 }
894
895 // Generates an option list of request types
896 function generateNetworkRequestTypeOptions ($default = '') {
897         // Is there cache?
898         if (!isset($GLOBALS[__FUNCTION__][$default])) {
899                 // Generate the list
900                 $GLOBALS[__FUNCTION__][$default] = generateOptions(
901                         '/ARRAY/',
902                         array(
903                                 'GET',
904                                 'POST'
905                         ),
906                         array(
907                                 '{--ADMIN_NETWORK_REQUEST_TYPE_GET--}',
908                                 '{--ADMIN_NETWORK_REQUEST_TYPE_POST--}'
909                         ),
910                         $default
911                 );
912         } // END - if
913
914         // Return cache
915         return $GLOBALS[__FUNCTION__][$default];
916 }
917
918 // Generates an option list of network_api_active
919 function generateNetworkApiActiveOptions ($default = '') {
920         // Is there cache?
921         if (!isset($GLOBALS[__FUNCTION__][$default])) {
922                 // Generate the list
923                 $GLOBALS[__FUNCTION__][$default] = generateYesNoOptions($default);
924         } // END - if
925
926         // Return cache
927         return $GLOBALS[__FUNCTION__][$default];
928 }
929
930 // Translates 'translate_name' for e.g. templates
931 function translateNetworkTranslationName ($name) {
932         // Generate id
933         $messageId = 'ADMIN_NETWORK_TRANSLATE_' . strtoupper($name) . '_NAME';
934
935         // Is the message id there?
936         if (!isMessageIdValid($messageId)) {
937                 // Not valid name
938                 reportBug(__FUNCTION__, __LINE__, 'name=' . $name . ' is invalid.');
939         } // END - if
940
941         // Return message id
942         return '{--' . $messageId . '--}';
943 }
944
945 // Translates the network type handler (e.g. banner, paidmail) for templates
946 function translateNetworkTypeHandler ($type) {
947         // Generate id
948         $messageId = 'ADMIN_NETWORK_TYPE_HANDLER_' . strtoupper($type);
949
950         // Is the message id there?
951         if (!isMessageIdValid($messageId)) {
952                 // Not valid type
953                 reportBug(__FUNCTION__, __LINE__, 'type=' . $type . ' is invalid.');
954         } // END - if
955
956         // Return message id
957         return '{--' . $messageId . '--}';
958 }
959
960 // Translates request type
961 function translateNetworkRequestType ($type) {
962         // Generate id
963         $messageId = 'ADMIN_NETWORK_REQUEST_TYPE_' . strtoupper($type) . '';
964
965         // Is the message id there?
966         if (!isMessageIdValid($messageId)) {
967                 // Not valid type
968                 reportBug(__FUNCTION__, __LINE__, 'type=' . $type . ' is invalid.');
969         } // END - if
970
971         // Return message id
972         return '{--' . $messageId . '--}';
973 }
974
975 // Translates request parameter
976 function translateNetworkRequestParameterKey ($param) {
977         // Generate id
978         $messageId = 'ADMIN_NETWORK_REQUEST_PARAMETER_' . strtoupper($param) . '';
979
980         // Is the message id there?
981         if (!isMessageIdValid($messageId)) {
982                 // Not valid param
983                 reportBug(__FUNCTION__, __LINE__, 'param=' . $param . ' is invalid.');
984         } // END - if
985
986         // Return message id
987         return '{--' . $messageId . '--}';
988 }
989
990 // Translates vheck request parameter
991 function translateNetworkVcheckParameterKey ($param) {
992         // Generate id
993         $messageId = 'ADMIN_NETWORK_VCHECK_PARAMETER_' . strtoupper($param) . '';
994
995         // Is the message id there?
996         if (!isMessageIdValid($messageId)) {
997                 // Not valid param
998                 reportBug(__FUNCTION__, __LINE__, 'param=' . $param . ' is invalid.');
999         } // END - if
1000
1001         // Return message id
1002         return '{--' . $messageId . '--}';
1003 }
1004
1005 // Translate text-encoding
1006 function translateNetworkTextEncoding ($encoding) {
1007         // Generate id
1008         $messageId = 'ADMIN_NETWORK_TYPE_TEXT_ENCODING_' . strtoupper($encoding) . '';
1009
1010         // Is the message id there?
1011         if (!isMessageIdValid($messageId)) {
1012                 // Not valid encoding
1013                 reportBug(__FUNCTION__, __LINE__, 'encoding=' . $encoding . ' is invalid.');
1014         } // END - if
1015
1016         // Return message id
1017         return '{--' . $messageId . '--}';
1018 }
1019
1020 // Translates API index
1021 function translateNetworkApiIndex ($index) {
1022         // Is there cache?
1023         if (!isset($GLOBALS['network_array_index'])) {
1024                 // Get an array of all API array indexes
1025                 $GLOBALS['network_array_index'] = array();
1026
1027                 // Get all entries
1028                 $result = SQL_QUERY('SELECT
1029         `network_array_id`,
1030         `network_array_index`,
1031         `network_translation_name`
1032 FROM
1033         `{?_MYSQL_PREFIX?}_network_array_translation`
1034 INNER JOIN
1035         `{?_MYSQL_PREFIX?}_network_translations`
1036 ON
1037         `network_array_index`=`network_translation_id`
1038 ORDER BY
1039         `network_array_sort` ASC', __FUNCTION__, __LINE__);
1040
1041                 // Are there entries?
1042                 if (!SQL_HASZERONUMS($result)) {
1043                         // Get all entries
1044                         while ($row = SQL_FETCHARRAY($result)) {
1045                                 // Add it to our global array
1046                                 $GLOBALS['network_array_index'][$row['network_array_index']] = $row;
1047                         } // END - while
1048                 } // END - if
1049
1050                 // Free result
1051                 SQL_FREERESULT($result);
1052         } // END - if
1053
1054         // Default name is unknown
1055         $name = 'unknown';
1056
1057         // Is the entry there?
1058         if (isset($GLOBALS['network_array_index'][$index])) {
1059                 // Then get the name
1060                 $name = $GLOBALS['network_array_index'][$index]['network_translation_name'];
1061         } // END - if
1062
1063         // Return translation
1064         return translateNetworkTranslationName($name);
1065 }
1066
1067 // Translates network API configuration status (see function isNetworkApiConfigured()) by given id
1068 function translateNetworkApiConfiguredStatusById ($networkId) {
1069         // Is there cache?
1070         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
1071                 // By default it is not configured
1072                 $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_NOT_CONFIGURED--}';
1073
1074                 // So is it configured?
1075                 if (!isNetworkActiveById($networkId)) {
1076                         // Network is not active
1077                         $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_NOT_ACTIVE--}';
1078                 } elseif (isNetworkApiConfigured($networkId)) {
1079                         // Yes, it is
1080                         $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_CONFIGURED--}';
1081                 } // END - if
1082         } // END - if
1083
1084         // Return cache
1085         return $GLOBALS[__FUNCTION__][$networkId];
1086 }
1087
1088 // Checks if the given network is configured by looking its API configuration entry up
1089 function isNetworkApiConfigured ($networkId) {
1090         // Is there cache?
1091         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
1092                 // Check for an entry in network_api_config
1093                 $GLOBALS[__FUNCTION__][$networkId] = (countSumTotalData(
1094                         bigintval($networkId),
1095                         'network_api_config',
1096                         'network_id',
1097                         'network_id',
1098                         true
1099                 ) == 1);
1100         } // END - if
1101
1102         // Return cache
1103         return $GLOBALS[__FUNCTION__][$networkId];
1104 }
1105
1106 // Checks whether the given network type handler is configured
1107 function isNetworkTypeHandlerConfigured ($networkId, $networkTypeId) {
1108         // Is there cache?
1109         if (!isset($GLOBALS[__FUNCTION__][$networkId][$networkTypeId])) {
1110                 // Determine it
1111                 $GLOBALS[__FUNCTION__][$networkId][$networkTypeId] = (countSumTotalData(
1112                         bigintval($networkTypeId),
1113                         'network_types_config',
1114                         'network_data_id',
1115                         'network_type_id',
1116                         true,
1117                         sprintf(' AND `network_id`=%s', bigintval($networkId))
1118                 ) == 1);
1119         } // END - if
1120
1121         // Return cache
1122         return $GLOBALS[__FUNCTION__][$networkId][$networkTypeId];
1123 }
1124
1125 // Handles the network-payment-check request
1126 function handleNetworkPaymentCheckRequest () {
1127         // @TODO Implement this function, don't forget to set HTTP status back to '200 OK' if everything went fine
1128         reportBug(__FUNCTION__, __LINE__, 'Not yet implemented.');
1129 }
1130
1131 //------------------------------------------------------------------------------
1132 //                             Call-back functions
1133 //------------------------------------------------------------------------------
1134
1135 // Callback function to add new network
1136 function doAdminNetworkProcessAddNetwork () {
1137         // We can say here, the form is sent, so check if the network is already added
1138         if (isNetworkNameValid(postRequestElement('network_short_name'))) {
1139                 // Already there
1140                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_ALREADY_ADDED=' . postRequestElement('network_short_name') . '%}');
1141                 return FALSE;
1142         } // END - if
1143
1144         // Add the whole request to database
1145         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_data'), __FUNCTION__, __LINE__);
1146
1147         // Add the id for output only
1148         setPostRequestElement('network_id', SQL_INSERTID());
1149
1150         // Output message
1151         if (!SQL_HASZEROAFFECTED()) {
1152                 // Successfully added
1153                 loadTemplate('admin_network_added', FALSE, postRequestArray());
1154         } else {
1155                 // Not added
1156                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_DATA_NOT_ADDED=' . postRequestElement('network_short_name') . '%}');
1157         }
1158 }
1159
1160 // Displays selected networks for editing
1161 function doAdminNetworkProcessHandleNetworks () {
1162         // Is there selections?
1163         if (ifPostContainsSelections()) {
1164                 // Something has been selected, so start displaying one by one
1165                 $OUT = '';
1166                 foreach (postRequestElement('sel') as $networkId => $sel) {
1167                         // Is this selected?
1168                         if ($sel == 1) {
1169                                 // Load this network's data
1170                                 $networkData = getNetworkDataById($networkId);
1171
1172                                 // Is there found the network?
1173                                 if (count($networkData) > 0) {
1174                                         // Add row template with given form name
1175                                         $OUT .= loadTemplate('admin_' . $GLOBALS['network_form_name'] . '_networks_row', TRUE, $networkData);
1176                                 } // END - if
1177                         } // END - if
1178                 } // END - foreach
1179
1180                 // If we have no rows, we don't need to display the edit form
1181                 if (!empty($OUT)) {
1182                         // Init array with generic element
1183                         $content = array(
1184                                 'rows' => $OUT
1185                         );
1186
1187                         // Output main template
1188                         loadTemplate('admin_' . $GLOBALS['network_form_name'] . '_networks', FALSE, $content);
1189
1190                         // Don't display the list/add new form
1191                         $GLOBALS['network_display'] = FALSE;
1192                 } else {
1193                         // Nothing selected/found
1194                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_NOTHING_FOUND--}');
1195                 }
1196         } // END - if
1197 }
1198
1199 // Handle network type form
1200 function doAdminNetworkProcessHandleNetworkTypes () {
1201         // Is there selections?
1202         if (ifPostContainsSelections()) {
1203                 // Load network data
1204                 $networkData = getNetworkDataById(getRequestElement('network_id'));
1205
1206                 // Something has been selected, so start displaying one by one
1207                 $OUT = '';
1208                 foreach (postRequestElement('sel') as $networkId => $sel) {
1209                         // Is this selected?
1210                         if ($sel == 1) {
1211                                 // Load this network's data
1212                                 $networkTypeData = getNetworkTypeDataById($networkId);
1213
1214                                 // Is there found the network?
1215                                 if (count($networkTypeData) > 0) {
1216                                         if (isFormSent('edit')) {
1217                                                 // Add row template for deleting
1218                                                 $OUT .= loadTemplate('admin_edit_network_types_row', TRUE, $networkTypeData);
1219                                         } elseif (isFormSent('delete')) {
1220                                                 // Add row template for deleting
1221                                                 $OUT .= loadTemplate('admin_delete_network_types_row', TRUE, $networkTypeData);
1222                                         } else {
1223                                                 // Problem!
1224                                                 reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.');
1225                                         }
1226                                 } // END - if
1227                         } // END - if
1228                 } // END - foreach
1229
1230                 // If we have no rows, we don't need to display the edit form
1231                 if (!empty($OUT)) {
1232                         // Prepare array with generic elements
1233                         $content = array(
1234                                 'rows'       => $OUT,
1235                                 'network_id' => bigintval(getRequestElement('network_id'))
1236                         );
1237
1238                         // Output main template
1239                         if (isFormSent('edit')) {
1240                                 loadTemplate('admin_edit_network_types', FALSE, $content);
1241                         } elseif (isFormSent('delete')) {
1242                                 loadTemplate('admin_delete_network_types', FALSE, $content);
1243                         } else {
1244                                 // Problem!
1245                                 reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.');
1246                         }
1247
1248                         // Don't display the list/add new form
1249                         $GLOBALS['network_display'] = FALSE;
1250                 } else {
1251                         // Nothing selected/found
1252                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_TYPE_HANDLER_NOTHING_FOUND--}');
1253                 }
1254         } // END - if
1255 }
1256
1257 // Handle network request parameter form
1258 function doAdminNetworkProcessHandleRequestParams () {
1259         // Is there selections?
1260         if (ifPostContainsSelections()) {
1261                 // Init cache array
1262                 $GLOBALS['network_request_params_disabled'] = array();
1263
1264                 // Load network data
1265                 $networkData = getNetworkDataById(getRequestElement('network_id'));
1266
1267                 // Something has been selected, so start displaying one by one
1268                 $OUT = '';
1269                 foreach (postRequestElement('sel') as $networkId => $sel) {
1270                         // Is this selected?
1271                         if ($sel == 1) {
1272                                 // Load this network's data
1273                                 $networkRequestData = getNetworkRequestParamsDataById($networkId);
1274
1275                                 // Is there found the network?
1276                                 if (count($networkRequestData) > 0) {
1277                                         if (isFormSent('edit')) {
1278                                                 // Add row template for deleting
1279                                                 $OUT .= loadTemplate('admin_edit_network_request_params_row', TRUE, $networkRequestData);
1280                                         } elseif (isFormSent('delete')) {
1281                                                 // Get type data
1282                                                 $networkRequestData['network_type_data'] = getNetworkTypeDataById($networkRequestData['network_type_id']);
1283
1284                                                 // Add row template for deleting
1285                                                 $OUT .= loadTemplate('admin_delete_network_request_params_row', TRUE, $networkRequestData);
1286                                         } else {
1287                                                 // Problem!
1288                                                 reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.');
1289                                         }
1290                                 } // END - if
1291                         } // END - if
1292                 } // END - foreach
1293
1294                 // If we have no rows, we don't need to display the edit form
1295                 if (!empty($OUT)) {
1296                         // Prepare array with generic elements
1297                         $content = array(
1298                                 'rows'       => $OUT,
1299                                 'network_id' => bigintval(getRequestElement('network_id'))
1300                         );
1301
1302                         // Output main template
1303                         if (isFormSent('edit')) {
1304                                 loadTemplate('admin_edit_network_request_params', FALSE, $content);
1305                         } elseif (isFormSent('delete')) {
1306                                 loadTemplate('admin_delete_network_request_params', FALSE, $content);
1307                         } else {
1308                                 // Problem!
1309                                 reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.');
1310                         }
1311
1312                         // Don't display the list/add new form
1313                         $GLOBALS['network_display'] = FALSE;
1314                 } else {
1315                         // Nothing selected/found
1316                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_FOUND--}');
1317                 }
1318         } // END - if
1319 }
1320
1321 // Changes given networks
1322 function doAdminNetworkProcessChangeNetworks () {
1323         // Is there selections?
1324         if (ifPostContainsSelections()) {
1325                 // By default nothing is updated
1326                 $updated = 0;
1327
1328                 // Something has been selected, so start updating them
1329                 foreach (postRequestElement('sel') as $networkId => $sel) {
1330                         // Update this entry?
1331                         if ($sel == 1) {
1332                                 // Init data array
1333                                 $networkData = array();
1334
1335                                 // Transfer whole array, except 'sel'
1336                                 foreach (postRequestArray() as $key => $entry) {
1337                                         // Skip 'sel' and submit button
1338                                         if (in_array($key, array('sel', 'do_edit'))) {
1339                                                 continue;
1340                                         } // END - if
1341
1342                                         // Is there this enty?
1343                                         if (!isset($entry[$networkId])) {
1344                                                 // Not found, needs fixing
1345                                                 reportBug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1346                                         } // END - if
1347
1348                                         // Add this entry
1349                                         $networkData[$key] = $entry[$networkId];
1350                                 } // END - foreach
1351
1352                                 // Update the network data
1353                                 $updated += doNetworkUpdateDataByArray($networkId, $networkData);
1354                         } // END - if
1355                 } // END - foreach
1356
1357                 // Is there updates?
1358                 if ($updated > 0) {
1359                         // Updates done
1360                         displayMessage('{%message,ADMIN_NETWORK_UPDATED=' . $updated . '%}');
1361                 } else {
1362                         // Nothing changed
1363                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_NOTHING_CHANGED--}');
1364                 }
1365         } // END - if
1366 }
1367
1368 // Removes given networks
1369 function doAdminNetworkProcessRemoveNetworks () {
1370         // Is there selections?
1371         if (ifPostContainsSelections()) {
1372                 // By default nothing is removed
1373                 $removed = 0;
1374
1375                 // Something has been selected, so start updating them
1376                 foreach (postRequestElement('sel') as $networkId => $sel) {
1377                         // Update this entry?
1378                         if ($sel == 1) {
1379                                 // Remove this entry
1380                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $networkId);
1381                         } // END - if
1382                 } // END - foreach
1383
1384                 // Is there removes?
1385                 if ($removed > 0) {
1386                         // Removals done
1387                         displayMessage('{%message,ADMIN_NETWORK_REMOVED=' . $removed . '%}');
1388                 } else {
1389                         // Nothing removed
1390                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_NOTHING_REMOVED--}');
1391                 }
1392         } // END - if
1393 }
1394
1395 // Add a network type handler if not yet found
1396 function doAdminNetworkProcessAddNetworkType () {
1397         // Is the network type handle already used with given network?
1398         if (isNetworkTypeHandleValid(postRequestElement('network_type_handler'), getRequestElement('network_id'))) {
1399                 // Already added
1400                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_TYPE_HANDLER_ALREADY_ADDED=' . postRequestElement('network_type_handler') . '%}');
1401
1402                 // ... so abort here
1403                 return FALSE;
1404         } // END - if
1405
1406         // Add id
1407         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1408
1409         // Is network_type_banner_url set?
1410         if (!isPostRequestElementSet('network_type_banner_url')) {
1411                 // Remove empty value to get a NULL for an optional entry
1412                 unsetPostRequestElement('network_type_banner_url');
1413         } // END - if
1414
1415         // Add the whole request to database
1416         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_types'), __FUNCTION__, __LINE__);
1417
1418         // Output message
1419         if (!SQL_HASZEROAFFECTED()) {
1420                 // Successfully added
1421                 loadTemplate('admin_network_type_added', FALSE, postRequestArray());
1422         } else {
1423                 // Not added
1424                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_TYPE_HANDLER_NOT_ADDED=' . postRequestElement('network_type_handler') . '%}');
1425         }
1426 }
1427
1428 // Changes given network type handlers
1429 function doAdminNetworkProcessChangeHandlerTypes () {
1430         // Is there selections?
1431         if (ifPostContainsSelections()) {
1432                 // By default nothing is updated
1433                 $updated = 0;
1434
1435                 // Something has been selected, so start updating them
1436                 foreach (postRequestElement('sel') as $networkId => $sel) {
1437                         // Update this entry?
1438                         if ($sel == 1) {
1439                                 // Init data array
1440                                 $networkTypeData = array();
1441
1442                                 // Transfer whole array, except 'sel'
1443                                 foreach (postRequestArray() as $key => $entry) {
1444                                         // Skip 'sel' and submit button
1445                                         if (in_array($key, array('sel', 'do_edit'))) {
1446                                                 continue;
1447                                         } // END - if
1448
1449                                         // Is there this enty?
1450                                         if (!isset($entry[$networkId])) {
1451                                                 // Not found, needs fixing
1452                                                 reportBug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1453                                         } // END - if
1454
1455                                         // Fix empty network_type_banner_url to NULL
1456                                         if (($key == 'network_type_banner_url') && (trim($entry[$networkId]) == '')) {
1457                                                 // Set it to NULL
1458                                                 $entry[$networkId] = NULL;
1459                                         } // END - if
1460
1461                                         // Add this entry
1462                                         $networkTypeData[$key] = $entry[$networkId];
1463                                 } // END - foreach
1464
1465                                 // Update the network data
1466                                 $updated += doNetworkUpdateTypeByArray($networkId, $networkTypeData);
1467                         } // END - if
1468                 } // END - foreach
1469
1470                 // Is there updates?
1471                 if ($updated > 0) {
1472                         // Updates done
1473                         displayMessage('{%message,ADMIN_NETWORK_TYPE_HANDLER_UPDATED=' . $updated . '%}');
1474                 } else {
1475                         // Nothing changed
1476                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_TYPE_HANDLER_NOTHING_CHANGED--}');
1477                 }
1478         } // END - if
1479 }
1480
1481 // Changes given network request parameters
1482 function doAdminNetworkProcessChangeRequestParams () {
1483         // Is there selections?
1484         if (ifPostContainsSelections()) {
1485                 // By default nothing is updated
1486                 $updated = 0;
1487
1488                 // Something has been selected, so start updating them
1489                 foreach (postRequestElement('sel') as $networkId => $sel) {
1490                         // Update this entry?
1491                         if ($sel == 1) {
1492                                 // Init data array
1493                                 $networkParamsData = array();
1494
1495                                 // Transfer whole array, except 'sel'
1496                                 foreach (postRequestArray() as $key => $entry) {
1497                                         // Skip 'sel' and submit button
1498                                         if (in_array($key, array('sel', 'do_edit'))) {
1499                                                 continue;
1500                                         } // END - if
1501
1502                                         // Is there this enty?
1503                                         if (!isset($entry[$networkId])) {
1504                                                 // Not found, needs fixing
1505                                                 reportBug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1506                                         } // END - if
1507
1508                                         // Fix empty network_request_param_default to NULL
1509                                         if (($key == 'network_request_param_default') && (trim($entry[$networkId]) == '')) {
1510                                                 // Set it to NULL
1511                                                 $entry[$networkId] = NULL;
1512                                         } // END - if
1513
1514                                         // Add this entry
1515                                         $networkParamsData[$key] = $entry[$networkId];
1516                                 } // END - foreach
1517
1518                                 // Update the network data
1519                                 $updated += doNetworkUpdateParamsByArray($networkId, $networkParamsData);
1520                         } // END - if
1521                 } // END - foreach
1522
1523                 // Is there updates?
1524                 if ($updated > 0) {
1525                         // Updates done
1526                         displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_UPDATED=' . $updated . '%}');
1527                 } else {
1528                         // Nothing changed
1529                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_CHANGED--}');
1530                 }
1531         } // END - if
1532 }
1533
1534 // Changes given network array translations
1535 function doAdminNetworkProcessChangeArrayTranslations () {
1536         // Is there selections?
1537         if (ifPostContainsSelections()) {
1538                 // By default nothing is updated
1539                 $updated = 0;
1540
1541                 // Something has been selected, so start updating them
1542                 foreach (postRequestElement('sel') as $networkId => $sel) {
1543                         // Update this entry?
1544                         if ($sel == 1) {
1545                                 // Init data array
1546                                 $networkTranslationsData = array();
1547
1548                                 // Transfer whole array, except 'sel'
1549                                 foreach (postRequestArray() as $key => $entry) {
1550                                         // Skip 'sel' and submit button
1551                                         if (in_array($key, array('sel', 'do_edit'))) {
1552                                                 continue;
1553                                         } // END - if
1554
1555                                         // Is there this enty?
1556                                         if (!isset($entry[$networkId])) {
1557                                                 // Not found, needs fixing
1558                                                 reportBug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1559                                         } // END - if
1560
1561                                         // Fix empty network_request_param_default to NULL
1562                                         if (($key == 'network_request_param_default') && (trim($entry[$networkId]) == '')) {
1563                                                 // Set it to NULL
1564                                                 $entry[$networkId] = NULL;
1565                                         } // END - if
1566
1567                                         // Add this entry
1568                                         $networkTranslationsData[$key] = $entry[$networkId];
1569                                 } // END - foreach
1570
1571                                 // Update the network data
1572                                 $updated += doNetworkUpdateArrayTranslationsByArray($networkId, $networkTranslationsData);
1573                         } // END - if
1574                 } // END - foreach
1575
1576                 // Is there updates?
1577                 if ($updated > 0) {
1578                         // Updates done
1579                         displayMessage('{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_UPDATED=' . $updated . '%}');
1580                 } else {
1581                         // Nothing changed
1582                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_ARRAY_TRANSLATION_NOTHING_CHANGED--}');
1583                 }
1584         } // END - if
1585 }
1586
1587 // Removes given network type handlers
1588 function doAdminNetworkProcessRemoveNetworkTypes () {
1589         // Is there selections?
1590         if (ifPostContainsSelections()) {
1591                 // By default nothing is removed
1592                 $removed = 0;
1593
1594                 // Something has been selected, so start updating them
1595                 foreach (postRequestElement('sel') as $networkId => $sel) {
1596                         // Update this entry?
1597                         if ($sel == 1) {
1598                                 // Remove this entry
1599                                 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $networkId);
1600                         } // END - if
1601                 } // END - foreach
1602
1603                 // Is there removes?
1604                 if ($removed > 0) {
1605                         // Removals done
1606                         displayMessage('{%message,ADMIN_NETWORK_TYPE_HANDLER_REMOVED=' . $removed . '%}');
1607                 } else {
1608                         // Nothing removed
1609                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_TYPE_HANDLER_NOTHING_REMOVED--}');
1610                 }
1611         } // END - if
1612 }
1613
1614 // Removes given network request parameters
1615 function doAdminNetworkProcessRemoveNetworkRequestParams () {
1616         // Is there selections?
1617         if (ifPostContainsSelections()) {
1618                 // By default nothing is removed
1619                 $removed = 0;
1620
1621                 // Something has been selected, so start updating them
1622                 foreach (postRequestElement('sel') as $networkId => $sel) {
1623                         // Update this entry?
1624                         if ($sel == 1) {
1625                                 // Remove this entry
1626                                 $removed += doAdminRemoveNetworkEntry('request_params', 'network_request_param_id', $networkId);
1627                         } // END - if
1628                 } // END - foreach
1629
1630                 // Is there removes?
1631                 if ($removed > 0) {
1632                         // Removals done
1633                         displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_REMOVED=' . $removed . '%}');
1634                 } else {
1635                         // Nothing removed
1636                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_REMOVED--}');
1637                 }
1638         } // END - if
1639 }
1640
1641 // Removes given network array translations
1642 function doAdminNetworkProcessRemoveNetworkArrayTranslation () {
1643         // Is there selections?
1644         if (ifPostContainsSelections()) {
1645                 // By default nothing is removed
1646                 $removed = 0;
1647
1648                 // Something has been selected, so start updating them
1649                 foreach (postRequestElement('sel') as $networkId => $sel) {
1650                         // Update this entry?
1651                         if ($sel == 1) {
1652                                 // Remove this entry
1653                                 $removed += doAdminRemoveNetworkEntry('array_translation', 'network_array_id', $networkId);
1654                         } // END - if
1655                 } // END - foreach
1656
1657                 // Is there removes?
1658                 if ($removed > 0) {
1659                         // Removals done
1660                         displayMessage('{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_REMOVED=' . $removed . '%}');
1661                 } else {
1662                         // Nothing removed
1663                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_ARRAY_TRANSLATION_NOTHING_REMOVED--}');
1664                 }
1665         } // END - if
1666 }
1667
1668 // Adds a request parameter to given network and type
1669 function doAdminNetworkProcessAddRequestParam () {
1670         // Is the request parameter already used with given network?
1671         if (isNetworkRequestElementValid(postRequestElement('network_request_param_key'), postRequestElement('network_type_id'), getRequestElement('network_id'))) {
1672                 // Already added
1673                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED=' . postRequestElement('network_request_param_key') . '%}');
1674
1675                 // ... so abort here
1676                 return FALSE;
1677         } // END - if
1678
1679         // Add id
1680         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1681
1682         // Is network_request_param_default set?
1683         if (!isPostRequestElementSet('network_request_param_default')) {
1684                 // Remove empty value to get a NULL for an optional entry
1685                 unsetPostRequestElement('network_request_param_default');
1686         } // END - if
1687
1688         // Add the whole request to database
1689         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_request_params'), __FUNCTION__, __LINE__);
1690
1691         // Output message
1692         if (!SQL_HASZEROAFFECTED()) {
1693                 // Successfully added
1694                 loadTemplate('admin_network_request_param_added', FALSE, postRequestArray());
1695         } else {
1696                 // Not added
1697                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED=' . postRequestElement('network_request_param_key') . '%}');
1698         }
1699 }
1700
1701 // Adds a vheck request parameter to given network
1702 function doAdminNetworkProcessAddVcheckParam () {
1703         // Is the request parameter already used with given network?
1704         if (isNetworkVcheckElementValid(postRequestElement('network_vcheck_param_key'), getRequestElement('network_id'))) {
1705                 // Already added
1706                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_VCHECK_PARAMETER_ALREADY_ADDED=' . postRequestElement('network_vcheck_param_key') . '%}');
1707
1708                 // ... so abort here
1709                 return FALSE;
1710         } // END - if
1711
1712         // Add id
1713         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1714
1715         // Is network_vcheck_param_default set?
1716         if (!isPostRequestElementSet('network_vcheck_param_default')) {
1717                 // Remove empty value to get a NULL for an optional entry
1718                 unsetPostRequestElement('network_vcheck_param_default');
1719         } // END - if
1720
1721         // Add the whole vcheck to database
1722         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_vcheck_params'), __FUNCTION__, __LINE__);
1723
1724         // Output message
1725         if (!SQL_HASZEROAFFECTED()) {
1726                 // Successfully added
1727                 loadTemplate('admin_network_vcheck_param_added', FALSE, postRequestArray());
1728         } else {
1729                 // Not added
1730                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_VCHECK_PARAMETER_NOT_ADDED=' . postRequestElement('network_vcheck_param_key') . '%}');
1731         }
1732 }
1733
1734 // Adds a API response array entry
1735 function doAdminNetworkProcessAddNetworkArrayTranslation () {
1736         // Is the request parameter already used with given network?
1737         if (isNetworkArrayTranslationValid(postRequestElement('network_array_index'), postRequestElement('network_type_id'), getRequestElement('network_id'))) {
1738                 // Already added
1739                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_ALREADY_ADDED=' . postRequestElement('network_array_index') . '%}');
1740
1741                 // ... so abort here
1742                 return FALSE;
1743         } // END - if
1744
1745         // Add id
1746         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1747
1748         // Add sorting
1749         setPostRequestElement('network_array_sort', (countSumTotalData(
1750                 bigintval(postRequestElement('network_id')),
1751                 'network_array_translation',
1752                 'network_array_id',
1753                 'network_id',
1754                 true,
1755                 sprintf(" AND `network_type_id`=%s", bigintval(postRequestElement('network_type_id')))
1756         ) + 1));
1757
1758         // Add the whole request to database
1759         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_array_translation'), __FUNCTION__, __LINE__);
1760
1761         // Output message
1762         if (!SQL_HASZEROAFFECTED()) {
1763                 // Successfully added
1764                 loadTemplate('admin_network_array_translation_added', FALSE, postRequestArray());
1765         } else {
1766                 // Not added
1767                 loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_NOT_ADDED=' . postRequestElement('network_array_index') . '%}');
1768         }
1769 }
1770
1771 // Handle network array translation form
1772 function doAdminNetworkProcessHandleArrayTranslations () {
1773         // Is there selections?
1774         if (ifPostContainsSelections()) {
1775                 // Init cache array
1776                 $GLOBALS['network_array_translation_disabled'] = array();
1777
1778                 // Load network data
1779                 $networkData = getNetworkDataById(getRequestElement('network_id'));
1780
1781                 // Something has been selected, so start displaying one by one
1782                 $OUT = '';
1783                 foreach (postRequestElement('sel') as $networkId => $sel) {
1784                         // Is this selected?
1785                         if ($sel == 1) {
1786                                 // Load this network's data
1787                                 $networkTranslationsData = getNetworkArrayTranslationsDataById($networkId);
1788
1789                                 // Is there found the network?
1790                                 if (count($networkTranslationsData) > 0) {
1791                                         if (isFormSent('edit')) {
1792                                                 // Add row template for deleting
1793                                                 $OUT .= loadTemplate('admin_edit_network_array_translation_row', TRUE, $networkTranslationsData);
1794                                         } elseif (isFormSent('delete')) {
1795                                                 // Get type data
1796                                                 $networkTranslationsData['network_type_data'] = getNetworkTypeDataById($networkTranslationsData['network_type_id']);
1797
1798                                                 // Add row template for deleting
1799                                                 $OUT .= loadTemplate('admin_delete_network_array_translation_row', TRUE, $networkTranslationsData);
1800                                         } else {
1801                                                 // Problem!
1802                                                 reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.');
1803                                         }
1804                                 } // END - if
1805                         } // END - if
1806                 } // END - foreach
1807
1808                 // If we have no rows, we don't need to display the edit form
1809                 if (!empty($OUT)) {
1810                         // Prepare array with generic elements
1811                         $content = array(
1812                                 'rows'       => $OUT,
1813                                 'network_id' => bigintval(getRequestElement('network_id'))
1814                         );
1815
1816                         // Output main template
1817                         if (isFormSent('edit')) {
1818                                 loadTemplate('admin_edit_network_array_translation', FALSE, $content);
1819                         } elseif (isFormSent('delete')) {
1820                                 loadTemplate('admin_delete_network_array_translation', FALSE, $content);
1821                         } else {
1822                                 // Problem!
1823                                 reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.');
1824                         }
1825
1826                         // Don't display the list/add new form
1827                         $GLOBALS['network_display'] = FALSE;
1828                 } else {
1829                         // Nothing selected/found
1830                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_FOUND--}');
1831                 }
1832         } // END - if
1833 }
1834
1835 // Adds/update network API configuration
1836 function doAdminNetworkProcessNetworkApiConfig () {
1837         // Add id
1838         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1839
1840         // Is network_api_referral_button set?
1841         if (!isPostRequestElementSet('network_api_referral_button')) {
1842                 // Remove empty value to get a NULL for an optional entry
1843                 unsetPostRequestElement('network_api_referral_button');
1844         } // END - if
1845
1846         // Is there already an entry?
1847         if (isNetworkApiConfigured(getRequestElement('network_id'))) {
1848                 // Generate SQL query
1849                 $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_api_config', 'network_id', postRequestElement('network_id'), array('network_id'));
1850         } else {
1851                 // Insert new entry
1852                 $SQL = getInsertSqlFromArray(postRequestArray(), 'network_api_config');
1853         }
1854
1855         // Run the query
1856         SQL_QUERY($SQL, __FUNCTION__, __LINE__);
1857
1858         // Output message
1859         if (!SQL_HASZEROAFFECTED()) {
1860                 // Successfully added
1861                 displayMessage('{--ADMIN_CONFIG_NETWORK_API_SAVED--}');
1862         } else {
1863                 // Not added
1864                 loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_API_NOT_SAVED--}');
1865         }
1866 }
1867
1868 // Only adds network type configuration if not yet present
1869 function doAdminNetworkProcessAddHandlerTypesConfig ($displayMessage = TRUE) {
1870         // Add both ids
1871         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1872         setPostRequestElement('network_type_id', bigintval(getRequestElement('network_type_id')));
1873
1874         // Translate German comma to dot
1875         setPostRequestElement('network_min_payment', convertCommaToDot(postRequestElement('network_min_payment')));
1876
1877         /*
1878          * Some parameters are optional, at least one must be given so check a bunch
1879          * of parameters.
1880          */
1881         foreach (array('network_min_waiting_time', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic') as $element) {
1882                 // Is this element empty?
1883                 if (!isPostRequestElementSet($element)) {
1884                         // Then unset it to get a NULL for optional parameter
1885                         unsetPostRequestElement($element);
1886                 } // END - if
1887         } // END - foreach
1888
1889         // Convert data in POST array
1890         convertSelectionsToEpocheTimeInPostData('network_max_reload_time_ye');
1891
1892         // Is there already an entry?
1893         if (isNetworkTypeHandlerConfigured(getRequestElement('network_id'), getRequestElement('network_type_id'))) {
1894                 // This network type handler is already configured
1895                 displayMessage('{--ADMIN_NETWORK_HANDLER_TYPE_HANDLER_ALREADY_CONFIGURED--}');
1896                 return;
1897         } // END - if
1898
1899         // Copy 'set all' and remove it from POST data
1900         $setAll = (postRequestElement('set_all') === 'Y');
1901         unsetPostRequestElement('set_all');
1902
1903         // Shall we set for all?
1904         if ($setAll === TRUE) {
1905                 // Get all handlers
1906                 $result = SQL_QUERY_ESC('SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s ORDER BY `network_type_id` ASC',
1907                         array(bigintval(getRequestElement('network_id'))), __FUNCTION__, __LINE__);
1908
1909                 // Are there entries?
1910                 if (SQL_HASZERONUMS($result)) {
1911                         // No, then abort here
1912                         displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_SET_ALL_404--}');
1913                         return;
1914                 } // END - if
1915
1916                 // Init number of rows
1917                 $numRows = 0;
1918
1919                 // Fetch all ids
1920                 while (list($typeId) = SQL_FETCHROW($result)) {
1921                         // Set it in GET data
1922                         setGetRequestElement('network_type_id', $typeId);
1923
1924                         // Call this function again
1925                         $numRows += doAdminNetworkProcessAddHandlerTypesConfig(FALSE);
1926                 } // END - while
1927
1928                 // Free result
1929                 SQL_FREERESULT($result);
1930
1931                 // Output message
1932                 if ($numRows > 0) {
1933                         // Something has been updated
1934                         displayMessage('{%message,ADMIN_CONFIG_NETWORK_HANDLER_TYPE_ALL_HANDLER_SAVED=' . bigintval($numRows) . '%}');
1935                 } else {
1936                         // Nothing has been saved
1937                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}');
1938                 }
1939         } else {
1940                 // Get SQL query for new entry
1941                 $SQL = getInsertSqlFromArray(postRequestArray(), 'network_types_config');
1942
1943                 // Run the query
1944                 SQL_QUERY($SQL, __FUNCTION__, __LINE__);
1945
1946                 // Shall we display the message?
1947                 if ($displayMessage === TRUE) {
1948                         // Output message
1949                         if (!SQL_HASZEROAFFECTED()) {
1950                                 // Successfully added
1951                                 displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_SAVED--}');
1952                         } else {
1953                                 // Not added
1954                                 loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_SAVED--}');
1955                         }
1956                 } else {
1957                         // Return amount of affected rows (1 or 2)
1958                         return SQL_AFFECTEDROWS();
1959                 }
1960         }
1961 }
1962
1963 // Only changes network type configuration if not yet present
1964 function doAdminNetworkProcessEditHandlerTypesConfig ($displayMessage = TRUE) {
1965         /*
1966          * Some parameters are optional, at least one must be given so check a bunch
1967          * of parameters.
1968          */
1969         foreach (array('network_min_waiting_time', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic') as $element) {
1970                 // Is this element empty?
1971                 if (!isPostRequestElementSet($element)) {
1972                         // Then unset it to get a NULL for optional parameter
1973                         unsetPostRequestElement($element);
1974                 } // END - if
1975         } // END - foreach
1976
1977         // Convert time selections in POST data
1978         convertSelectionsToEpocheTimeInPostData('network_max_reload_time_ye');
1979
1980         // Is there already an entry?
1981         if (!isNetworkTypeHandlerConfigured(getRequestElement('network_id'), getRequestElement('network_type_id'))) {
1982                 // This network type handler is not configured
1983                 displayMessage('{--ADMIN_NETWORK_HANDLER_TYPE_HANDLER_NOT_CONFIGURED--}');
1984                 return;
1985         } // END - if
1986
1987         // Copy 'set all' and remove it from POST data
1988         $setAll = (postRequestElement('set_all') === 'Y');
1989         unsetPostRequestElement('set_all');
1990
1991         // Shall we set for all?
1992         if ($setAll === TRUE) {
1993                 // Get all data entries
1994                 $result = SQL_QUERY_ESC('SELECT `network_data_id` FROM `{?_MYSQL_PREFIX?}_network_types_config` WHERE `network_id`=%s ORDER BY `network_type_id` ASC',
1995                         array(bigintval(getRequestElement('network_id'))), __FUNCTION__, __LINE__);
1996
1997                 // Are there entries?
1998                 if (SQL_HASZERONUMS($result)) {
1999                         // No, then abort here
2000                         displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_SET_ALL_404--}');
2001                         return;
2002                 } // END - if
2003
2004                 // Init number of rows
2005                 $numRows = 0;
2006
2007                 // Fetch all ids
2008                 while (list($dataId) = SQL_FETCHROW($result)) {
2009                         // Set it in GET data
2010                         setPostRequestElement('network_data_id', $dataId);
2011
2012                         // Call this function again
2013                         $numRows += doAdminNetworkProcessEditHandlerTypesConfig(FALSE);
2014                 } // END - while
2015
2016                 // Free result
2017                 SQL_FREERESULT($result);
2018
2019                 // Output message
2020                 if ($numRows > 0) {
2021                         // Something has been updated
2022                         displayMessage('{%message,ADMIN_CONFIG_NETWORK_HANDLER_TYPE_ALL_HANDLER_SAVED=' . bigintval($numRows) . '%}');
2023                 } else {
2024                         // Nothing has been saved
2025                         loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}');
2026                 }
2027         } else {
2028                 // Get SQL query for new entry
2029                 $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_types_config', 'network_data_id', postRequestElement('network_data_id'), array('network_data_id'));
2030
2031                 // Run the query
2032                 SQL_QUERY($SQL, __FUNCTION__, __LINE__);
2033
2034                 // Shall we display the message?
2035                 if ($displayMessage === TRUE) {
2036                         // Output message
2037                         if (!SQL_HASZEROAFFECTED()) {
2038                                 // Successfully added
2039                                 displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_SAVED--}');
2040                         } else {
2041                                 // Not added
2042                                 loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}');
2043                         }
2044                 } else {
2045                         // Return amount of affected rows (1 or 2)
2046                         return SQL_AFFECTEDROWS();
2047                 }
2048         }
2049 }
2050
2051 // Do expression code for this extension
2052 function doExpressionNetwork ($data) {
2053         // Construct replacer
2054         $replacer = sprintf(
2055                 "{DQUOTE} . %s(%s, '%s') . {DQUOTE}",
2056                 $data['callback'],
2057                 $data['matches'][4][$data['key']],
2058                 $data['extra_func']
2059         );
2060
2061         // Replace %network_id% with the current network id
2062         $replacer = str_replace('%network_id%', getCurrentNetworkId(), $replacer);
2063
2064         // Replace the code
2065         $code = replaceExpressionCode($data, $replacer);
2066
2067         // Return it
2068         return $code;
2069 }
2070
2071 // ----------------------------------------------------------------------------
2072 //                     Call-back functions for exporting data
2073 // ----------------------------------------------------------------------------
2074
2075 // Callback function to export network tables
2076 function doAdminNetworkProcessExport () {
2077         // Init table with all valid what->table entries
2078         $validExports = array(
2079                 // General network data
2080                 'list_network_data'              => 'data',
2081                 // Network type handler
2082                 'list_network_types'             => 'types',
2083                 // Network request parameter
2084                 'list_network_request_params'    => 'request_params',
2085                 // Vcheck request parameter
2086                 'list_network_vcheck_params'     => 'vcheck_params',
2087                 // Network API response array index translation
2088                 'list_network_array_translation' => 'array_translation',
2089         );
2090
2091         // Is the 'what' key valid?
2092         if (!isset($validExports[getWhat()])) {
2093                 // Not valid
2094                 reportBug(__FUNCTION__, __LINE__, 'what=' . getWhat() . ' - not supported');
2095         } // END - if
2096
2097         // Generate call-back, some tables require to export not all columns
2098         $callbackName = 'doAdminNetworkExport' . capitalizeUnderscoreString($validExports[getWhat()]);
2099
2100         // Is the call-back function there?
2101         if (!function_exists($callbackName)) {
2102                 // No, this is really bad
2103                 reportBug(__FUNCTION__, __LINE__, 'Invalid call-back function ' . $callbackName . ' detected.');
2104         } elseif (isset($GLOBALS[__FUNCTION__][$callbackName])) {
2105                 // Already called!
2106                 reportBug(__FUNCTION__, __LINE__, 'Double-call of export function ' . $callbackName . ' detected.');
2107         }
2108
2109         // Call the function
2110         call_user_func($callbackName);
2111
2112         // Mark it as called
2113         $GLOBALS[__FUNCTION__][$callbackName] = TRUE;
2114
2115         // Don't display the list/add new form
2116         $GLOBALS['network_display'] = FALSE;
2117 }
2118
2119 // Exports (and displays) the table 'network_data'
2120 function doAdminNetworkExportData () {
2121         // Query for all networks
2122         $result = SQL_QUERY('SELECT
2123         `network_short_name`,
2124         `network_title`,
2125         `network_reflink`,
2126         `network_data_separator`,
2127         `network_row_separator`,
2128         `network_request_type`,
2129         `network_charset`,
2130         `network_require_id_card`,
2131         `network_query_amount`,
2132         `network_active`
2133 FROM
2134         `{?_MYSQL_PREFIX?}_network_data`
2135 ORDER BY
2136         `network_id` ASC',
2137                 __FUNCTION__, __LINE__);
2138
2139         // Start an empty SQL query
2140         $SQL = "INSERT INTO `&#123;&#63;_MYSQL_PREFIX&#63;&#125;_network_data` (`network_short_name`, `network_title`, `network_reflink`, `network_data_separator`, `network_row_separator`, `network_request_type`, `network_charset`, `network_require_id_card`, `network_query_amount`, `network_active`) VALUES\n";
2141
2142         // Load all entries
2143         while ($content = SQL_FETCHARRAY($result)) {
2144                 // Add row
2145                 $SQL .= "('" .
2146                         $content['network_short_name'] . "', '" .
2147                         $content['network_title'] . "', '" .
2148                         $content['network_reflink'] . "', '" .
2149                         $content['network_data_separator'] . "', '" .
2150                         $content['network_row_separator'] . "', '" .
2151                         $content['network_request_type'] . "', '" .
2152                         $content['network_charset'] . "', '" .
2153                         $content['network_require_id_card'] . "', " .
2154                         $content['network_query_amount'] . ", '" .
2155                         $content['network_active'] . "'),\n";
2156         } // END - while
2157
2158         // Remove last commata and close braces
2159         $SQL = substr($SQL, 0, -2);
2160
2161         // Free result
2162         SQL_FREERESULT($result);
2163
2164         // Output the SQL query
2165         loadTemplate('admin_export_network_data', FALSE, $SQL);
2166 }
2167
2168 // Exports (and displays) the table 'network_types'
2169 function doAdminNetworkExportTypes () {
2170         // 'network_id' must be set
2171         if (!isGetRequestElementSet('network_id')) {
2172                 // Only network handlers of one network will be exported per time
2173                 reportBug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.');
2174         } // END - if
2175
2176         // Get all network types of given network
2177         $result = SQL_QUERY_ESC('SELECT
2178         `network_type_id`,
2179         `network_id`,
2180         `network_type_handler`,
2181         `network_type_api_url`,
2182         `network_type_click_url`,
2183         `network_type_banner_url`,
2184         `network_type_reload_time_unit`,
2185         `network_text_encoding`
2186 FROM
2187         `{?_MYSQL_PREFIX?}_network_types`
2188 WHERE
2189         `network_id`=%s
2190 ORDER BY
2191         `network_type_id` ASC',
2192                 array(
2193                         bigintval(getRequestElement('network_id'))
2194                 ), __FUNCTION__, __LINE__);
2195
2196         // Start an empty SQL query
2197         $SQL = "INSERT INTO `&#123;&#63;_MYSQL_PREFIX&#63;&#125;_network_types` (`network_type_id`, `network_id`, `network_type_handler`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url`, `network_type_reload_time_unit`, `network_text_encoding`) VALUES\n";
2198
2199         // Load all entries
2200         while ($content = SQL_FETCHARRAY($result)) {
2201                 // Add row
2202                 $SQL .= '(' .
2203                         $content['network_type_id'] . ', ' .
2204                         $content['network_id'] . ", '" .
2205                         $content['network_type_handler'] . "', '" .
2206                         $content['network_type_api_url'] . "', '" .
2207                         $content['network_type_click_url'] . "', ";
2208                 
2209                 // Is the column NULL?
2210                 if (is_null($content['network_type_banner_url'])) {
2211                         // Column is NULL
2212                         $SQL .= 'NULL';
2213                 } else {
2214                         // Column is set
2215                         $SQL .= chr(39) . $content['network_type_banner_url'] . chr(39);
2216                 }
2217
2218                 // Add more
2219                 $SQL .= ",'" . $content['network_type_reload_time_unit'] . "','" . $content['network_text_encoding'] . "'),\n";
2220         } // END - while
2221
2222         // Remove last commata and close braces
2223         $SQL = substr($SQL, 0, -2);
2224
2225         // Free result
2226         SQL_FREERESULT($result);
2227
2228         // Output the SQL query
2229         loadTemplate('admin_export_network_types', FALSE, $SQL);
2230 }
2231
2232 // Exports (and displays) the table 'network_request_params'
2233 function doAdminNetworkExportRequestParams () {
2234         // 'network_id' must be set
2235         if (!isGetRequestElementSet('network_id')) {
2236                 // Only network request parameters of one network will be exported per time
2237                 reportBug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.');
2238         } // END - if
2239
2240         // Get all network types of given network
2241         $result = SQL_QUERY_ESC('SELECT
2242         `network_id`,
2243         `network_type_id`,
2244         `network_request_param_key`,
2245         `network_request_param_value`,
2246         `network_request_param_default`
2247 FROM
2248         `{?_MYSQL_PREFIX?}_network_request_params`
2249 WHERE
2250         `network_id`=%s
2251 ORDER BY
2252         `network_type_id` ASC ,
2253         `network_request_param_id` ASC',
2254                 array(
2255                         bigintval(getRequestElement('network_id'))
2256                 ), __FUNCTION__, __LINE__);
2257
2258         // Start an empty SQL query
2259         $SQL = "INSERT INTO `&#123;&#63;_MYSQL_PREFIX&#63;&#125;_network_request_params` (`network_id`, `network_type_id`, `network_request_param_key`, `network_request_param_value`, `network_request_param_default`) VALUES\n";
2260
2261         // Load all entries
2262         while ($content = SQL_FETCHARRAY($result)) {
2263                 // Add row
2264                 $SQL .= '(' .
2265                         $content['network_id'] . ', ' .
2266                         $content['network_type_id'] . ", '" .
2267                         $content['network_request_param_key'] . "', '" .
2268                         $content['network_request_param_value'] . "', ";
2269                 
2270                 // Is the column NULL?
2271                 if (is_null($content['network_request_param_default'])) {
2272                         // Column is NULL
2273                         $SQL .= "NULL),\n";
2274                 } else {
2275                         // Column is set
2276                         $SQL .= chr(39) . $content['network_request_param_default'] . "'),\n";
2277                 }
2278         } // END - while
2279
2280         // Remove last commata and close braces
2281         $SQL = substr($SQL, 0, -2);
2282
2283         // Free result
2284         SQL_FREERESULT($result);
2285
2286         // Output the SQL query
2287         loadTemplate('admin_export_network_request_params', FALSE, $SQL);
2288 }
2289
2290 // Exports (and displays) the table 'network_vcheck_params'
2291 function doAdminNetworkExportVcheckParams () {
2292         // 'network_id' must be set
2293         if (!isGetRequestElementSet('network_id')) {
2294                 // Only network vcheck parameters of one network will be exported per time
2295                 reportBug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.');
2296         } // END - if
2297
2298         // Get all network types of given network
2299         $result = SQL_QUERY_ESC('SELECT
2300         `network_id`,
2301         `network_vcheck_param_key`,
2302         `network_vcheck_param_value`,
2303         `network_vcheck_param_default`
2304 FROM
2305         `{?_MYSQL_PREFIX?}_network_vcheck_params`
2306 WHERE
2307         `network_id`=%s
2308 ORDER BY
2309         `network_vcheck_param_id` ASC',
2310                 array(
2311                         bigintval(getRequestElement('network_id'))
2312                 ), __FUNCTION__, __LINE__);
2313
2314         // Start an empty SQL query
2315         $SQL = "INSERT INTO `&#123;&#63;_MYSQL_PREFIX&#63;&#125;_network_vcheck_params` (`network_id`, `network_vcheck_param_key`, `network_vcheck_param_value`, `network_vcheck_param_default`) VALUES\n";
2316
2317         // Load all entries
2318         while ($content = SQL_FETCHARRAY($result)) {
2319                 // Add row
2320                 $SQL .= '(' .
2321                         $content['network_id'] . ", '" .
2322                         $content['network_vcheck_param_key'] . "', '" .
2323                         $content['network_vcheck_param_value'] . "', ";
2324                 
2325                 // Is the column NULL?
2326                 if (is_null($content['network_vcheck_param_default'])) {
2327                         // Column is NULL
2328                         $SQL .= "NULL),\n";
2329                 } else {
2330                         // Column is set
2331                         $SQL .= chr(39) . $content['network_vcheck_param_default'] . "'),\n";
2332                 }
2333         } // END - while
2334
2335         // Remove last commata and close braces
2336         $SQL = substr($SQL, 0, -2);
2337
2338         // Free result
2339         SQL_FREERESULT($result);
2340
2341         // Output the SQL query
2342         loadTemplate('admin_export_network_vcheck_params', FALSE, $SQL);
2343 }
2344
2345 // Exports (and displays) the table 'network_array_translation'
2346 function doAdminNetworkExportArrayTranslation () {
2347         // 'network_id' must be set
2348         if (!isGetRequestElementSet('network_id')) {
2349                 // Only network API array index translations of one network will be exported per time
2350                 reportBug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.');
2351         } // END - if
2352
2353         // Get all network types of given network
2354         $result = SQL_QUERY_ESC('SELECT
2355         `network_id`,
2356         `network_type_id`,
2357         `network_array_index`,
2358         `network_array_sort`
2359 FROM
2360         `{?_MYSQL_PREFIX?}_network_array_translation`
2361 WHERE
2362         `network_id`=%s
2363 ORDER BY
2364         `network_type_id` ASC,
2365         `network_array_sort` ASC',
2366                 array(
2367                         bigintval(getRequestElement('network_id'))
2368                 ), __FUNCTION__, __LINE__);
2369
2370         // Start an empty SQL query
2371         $SQL = "INSERT INTO `&#123;&#63;_MYSQL_PREFIX&#63;&#125;_network_array_translation` (`network_id`, `network_type_id`, `network_array_index`, `network_array_sort`) VALUES\n";
2372
2373         // Load all entries
2374         while ($content = SQL_FETCHARRAY($result)) {
2375                 // Add row
2376                 $SQL .= '(' .
2377                         $content['network_id'] . ', ' .
2378                         $content['network_type_id'] . ', ' .
2379                         $content['network_array_index'] . ', ' .
2380                         $content['network_array_sort'] . "),\n";
2381         } // END - while
2382
2383         // Remove last commata and close braces
2384         $SQL = substr($SQL, 0, -2);
2385
2386         // Free result
2387         SQL_FREERESULT($result);
2388
2389         // Output the SQL query
2390         loadTemplate('admin_export_network_array_translation', FALSE, $SQL);
2391 }
2392
2393 // [EOF]
2394 ?>