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