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