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 - 2011 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                 debug_report_bug(__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                 debug_report_bug(__FUNCTION__, __LINE__, 'POST form could not be detected.');
80         } // END - if
81 }
82
83 // Handle a (maybe) sent form here
84 function doNetworkHandleForm () {
85         // Do we have 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                 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', false);
107         } // END - if
108
109         // Call-back the method handling our request
110         call_user_func($functionName);
111 }
112
113 // Checks wether the (short) network name is already used (valid)
114 function isNetworkNameValid ($name) {
115         // Query for it
116         $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1",
117                 array($name), __FUNCTION__, __LINE__);
118
119         // Does it exist?
120         $isValid = (SQL_NUMROWS($result) == 1);
121
122         // Free result
123         SQL_FREERESULT($result);
124
125         // Return result
126         return $isValid;
127 }
128
129 // Checks wether the given network type is already used (valid)
130 function isNetworkTypeHandleValid ($type, $networkId) {
131         // Query for it
132         $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handler`='%s' LIMIT 1",
133                 array(
134                         $networkId,
135                         $type
136                 ), __FUNCTION__, __LINE__);
137
138         // Does it exist?
139         $isValid = (SQL_NUMROWS($result) == 1);
140
141         // Free result
142         SQL_FREERESULT($result);
143
144         // Return result
145         return $isValid;
146 }
147
148 // Checks wether the given network request parameter is already used (valid)
149 function isNetworkRequestElementValid ($key, $type, $networkId) {
150         // Query for it
151         $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",
152                 array(
153                         $networkId,
154                         $type,
155                         $key
156                 ), __FUNCTION__, __LINE__);
157
158         // Does it exist?
159         $isValid = (SQL_NUMROWS($result) == 1);
160
161         // Free result
162         SQL_FREERESULT($result);
163
164         // Return result
165         return $isValid;
166 }
167
168 // Checks wether the given network API array translation
169 function isNetworkArrayTranslationValid ($key, $type, $networkId) {
170         // Query for it
171         $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",
172                 array(
173                         $networkId,
174                         $type,
175                         $key
176                 ), __FUNCTION__, __LINE__);
177
178         // Does it exist?
179         $isValid = (SQL_NUMROWS($result) == 1);
180
181         // Free result
182         SQL_FREERESULT($result);
183
184         // Return result
185         return $isValid;
186 }
187
188 // "Getter" for a network's data by provided id number
189 function getNetworkDataById ($networkId, $column = '') {
190         // Ids lower one are not accepted
191         if ($networkId < 1) {
192                 // Not good, should be fixed
193                 debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
194         } // END - if
195
196         // Set current network id
197         setCurrentNetworkId($networkId);
198
199         // Is it cached?
200         if (!isset($GLOBALS['network_data'][$networkId])) {
201                 // By default we have no data
202                 $GLOBALS['network_data'][$networkId] = array();
203
204                 // Query for the network data
205                 $result = SQL_QUERY_ESC('SELECT
206         `network_id`,
207         `network_short_name`,
208         `network_title`,
209         `network_reflink`,
210         `network_data_separator`,
211         `network_row_separator`,
212         `network_request_type`,
213         `network_charset`,
214         `network_require_id_card`,
215         `network_query_amount`
216 FROM
217         `{?_MYSQL_PREFIX?}_network_data`
218 WHERE
219         `network_id`=%s
220 LIMIT 1',
221                         array(bigintval($networkId)), __FUNCTION__, __LINE__);
222
223                 // Do we have an entry?
224                 if (SQL_NUMROWS($result) == 1) {
225                         // Then get it
226                         $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
227                 } // END - if
228
229                 // Free result
230                 SQL_FREERESULT($result);
231         } // END - if
232
233         // Return result
234         if (empty($column)) {
235                 // Return array
236                 return $GLOBALS['network_data'][$networkId];
237         } else {
238                 // Return column
239                 return $GLOBALS['network_data'][$networkId][$column];
240         }
241 }
242
243 // "Getter" for a network's data by provided type id number
244 function getNetworkDataByTypeId ($networkId, $column = '') {
245         // Ids lower one are not accepted
246         if ($networkId < 1) {
247                 // Not good, should be fixed
248                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
249         } // END - if
250
251         // Set current network id
252         setCurrentNetworkId($networkId);
253
254         // Is it cached?
255         if (!isset($GLOBALS['network_data'][$networkId])) {
256                 // By default we have no data
257                 $GLOBALS['network_data'][$networkId] = array();
258
259                 // Query for the network data
260                 $result = SQL_QUERY_ESC('SELECT
261         d.`network_id`,
262         d.`network_short_name`,
263         d.`network_title`,
264         d.`network_reflink`,
265         d.`network_data_separator`,
266         d.`network_row_separator`,
267         d.`network_request_type`,
268         d.`network_charset`,
269         d.`network_require_id_card`,
270         d.`network_query_amount`,
271         t.`network_type_handler`,
272         t.`network_type_api_url`,
273         t.`network_type_click_url`,
274         t.`network_type_banner_url`
275 FROM
276         `{?_MYSQL_PREFIX?}_network_data` AS d
277 LEFT JOIN
278         `{?_MYSQL_PREFIX?}_network_types` AS t
279 ON
280         d.`network_id`=t.`network_id`
281 WHERE
282         t.`network_type_id`=%s
283 LIMIT 1',
284                         array(bigintval($networkId)), __FUNCTION__, __LINE__);
285
286                 // Do we have an entry?
287                 if (SQL_NUMROWS($result) == 1) {
288                         // Then get it
289                         $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
290                 } // END - if
291
292                 // Free result
293                 SQL_FREERESULT($result);
294         } // END - if
295
296         // Return result
297         if (empty($column)) {
298                 // Return array
299                 return $GLOBALS['network_data'][$networkId];
300         } else {
301                 // Return column
302                 return $GLOBALS['network_data'][$networkId][$column];
303         }
304 }
305
306 // "Getter" for a network type data by provided id number
307 function getNetworkTypeDataById ($networkId) {
308         // Ids lower one are not accepted
309         if ($networkId < 1) {
310                 // Not good, should be fixed
311                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
312         } // END - if
313
314         // By default we have no data
315         $GLOBALS['network_type_data'][$networkId] = array();
316
317         // Query for the network data
318         $result = SQL_QUERY_ESC('SELECT
319         `network_type_id`,
320         `network_id`,
321         `network_type_handler`,
322         `network_type_api_url`,
323         `network_type_click_url`,
324         `network_type_banner_url`
325 FROM
326         `{?_MYSQL_PREFIX?}_network_types`
327 WHERE
328         `network_type_id`=%s
329 LIMIT 1',
330                 array(bigintval($networkId)), __FUNCTION__, __LINE__);
331
332         // Do we have an entry?
333         if (SQL_NUMROWS($result) == 1) {
334                 // Then get it
335                 $GLOBALS['network_type_data'][$networkId] = SQL_FETCHARRAY($result);
336         } // END - if
337
338         // Free result
339         SQL_FREERESULT($result);
340
341         // Return result
342         return $GLOBALS['network_type_data'][$networkId];
343 }
344
345 // "Getter" for a network request parameter data by provided id number
346 function getNetworkRequestParamsDataById ($networkId) {
347         // Ids lower one are not accepted
348         if ($networkId < 1) {
349                 // Not good, should be fixed
350                 debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkId . ' is smaller than 1.');
351         } // END - if
352
353         // By default we have no data
354         $networkRequestData = array();
355
356         // Query for the network data
357         $result = SQL_QUERY_ESC('SELECT
358         `network_request_param_id`,
359         `network_id`,
360         `network_type_id`,
361         `network_request_param_key`,
362         `network_request_param_value`,
363         `network_request_param_default`
364 FROM
365         `{?_MYSQL_PREFIX?}_network_request_params`
366 WHERE
367         `network_request_param_id`=%s
368 LIMIT 1',
369                 array(bigintval($networkId)), __FUNCTION__, __LINE__);
370
371         // Do we have an entry?
372         if (SQL_NUMROWS($result) == 1) {
373                 // Then get it
374                 $networkRequestData = SQL_FETCHARRAY($result);
375         } // END - if
376
377         // Free result
378         SQL_FREERESULT($result);
379
380         // Return result
381         return $networkRequestData;
382 }
383
384 // Updates given network (id) with data from array
385 function doNetworkUpdateDataByArray ($networkId, $networkData) {
386         // Ids lower one are not accepted
387         if ($networkId < 1) {
388                 // Not good, should be fixed
389                 debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
390         } // END - if
391
392         // Just call our inner method
393         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($networkId)), array(), false, false);
394 }
395
396 // Updates given network type handler (id) with data from array
397 function doNetworkUpdateTypeByArray ($networkId, $networkTypeData) {
398         // Ids lower one are not accepted
399         if ($networkId < 1) {
400                 // Not good, should be fixed
401                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type handler id ' . $networkId . ' is smaller than 1.');
402         } // END - if
403
404         // Just call our inner method
405         return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($networkId)), array(), false, false);
406 }
407
408 // Updates given network request parameters (id) with data from array
409 function doNetworkUpdateParamsByArray ($networkId, $networkParamData) {
410         // Ids lower one are not accepted
411         if ($networkId < 1) {
412                 // Not good, should be fixed
413                 debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkId . ' is smaller than 1.');
414         } // END - if
415
416         // Just call our inner method
417         return adminSaveSettings($networkParamData, '_network_request_params', sprintf("`network_request_param_id`=%s", bigintval($networkId)), array(), false, false);
418 }
419
420 // Removes given network entry
421 function doAdminRemoveNetworkEntry ($table, $column, $networkId, $limit = 1) {
422         // Remove the entry
423         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
424                 array(
425                         $table,
426                         $column,
427                         $networkId,
428                         $limit
429                 ), __FUNCTION__, __LINE__);
430
431         // Return affected rows
432         return SQL_AFFECTEDROWS();
433 }
434
435 // Generates a list of networks for given script and returns it
436 function generateAdminNetworkList () {
437         // Init output
438         $content = '';
439
440         // Query for all networks
441         $result = SQL_QUERY('SELECT
442         `network_id`,
443         `network_short_name`,
444         `network_title`
445 FROM
446         `{?_MYSQL_PREFIX?}_network_data`
447 ORDER BY
448         `network_short_name` ASC', __FUNCTION__, __LINE__);
449
450         // Do we have entries?
451         if (!SQL_HASZERONUMS($result)) {
452                 // List all entries
453                 $rows = array();
454                 while ($row = SQL_FETCHARRAY($result)) {
455                         // Is this valid, then add it
456                         if ((is_array($row)) && (isset($row['network_id']))) {
457                                 // Add entry
458                                 $rows[$row['network_id']] = $row;
459                         } // END - if
460                 } // END - while
461
462                 // Generate the selection box
463                 $content = generateSelectionBoxFromArray($rows, 'network_id', 'network_id', '', '', 'network');
464         } else {
465                 // Nothing selected
466                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
467         }
468
469         // Free the result
470         SQL_FREERESULT($result);
471
472         // Return the list
473         return $content;
474 }
475
476 // Generator (somewhat getter) for a list of network types for given network id
477 function generateAdminNetworkTypeList ($networkId) {
478         // Init content
479         $content = '';
480
481         // Query all types of this network
482         $result = SQL_QUERY_ESC('SELECT
483         `network_type_id`,
484         `network_type_handler`
485 FROM
486         `{?_MYSQL_PREFIX?}_network_types`
487 WHERE
488         `network_id`=%s
489 ORDER BY
490         `network_type_handler` ASC',
491                 array(
492                         bigintval($networkId)
493                 ), __FUNCTION__, __LINE__);
494
495         // Do we have entries?
496         if (!SQL_HASZERONUMS($result)) {
497                 // List all entries
498                 $rows = array();
499                 while ($row = SQL_FETCHARRAY($result)) {
500                         // Is this valid, then add it
501                         if ((is_array($row)) && (isset($row['network_type_id']))) {
502                                 // Add entry
503                                 $rows[$row['network_type_id']] = $row;
504                         } // END - if
505                 } // END - while
506
507                 // Generate the selection box
508                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id');
509         } else {
510                 // Nothing selected
511                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
512         }
513
514         // Free the result
515         SQL_FREERESULT($result);
516
517         // Return content
518         return $content;
519 }
520
521 // Generator (somewhat getter) for a list of network types for all types
522 function generateAdminDistinctNetworkTypeList () {
523         // Init content
524         $content = '';
525
526         // Query all types of this network
527         $result = SQL_QUERY('SELECT
528         t.`network_type_id`, t.`network_type_handler`, d.`network_title`
529 FROM
530         `{?_MYSQL_PREFIX?}_network_types` AS t
531 LEFT JOIN
532         `{?_MYSQL_PREFIX?}_network_data` AS d
533 ON
534         t.`network_id`=d.`network_id`
535 ORDER BY
536         d.`network_short_name` ASC,
537         t.`network_type_handler` ASC', __FUNCTION__, __LINE__);
538
539         // Do we have entries?
540         if (!SQL_HASZERONUMS($result)) {
541                 // List all entries
542                 $rows = array();
543                 while ($row = SQL_FETCHARRAY($result)) {
544                         // Is this valid, then add it
545                         if ((is_array($row)) && (isset($row['network_type_id']))) {
546                                 // Add entry
547                                 $rows[$row['network_type_id']] = $row;
548                         } // END - if
549                 } // END - while
550
551                 // Generate the selection box
552                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id', '', '_title');
553         } else {
554                 // Nothing selected
555                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
556         }
557
558         // Free the result
559         SQL_FREERESULT($result);
560         //* DEBUG: */ die('<pre>'.encodeEntities($content).'</pre>');
561
562         // Return content
563         return $content;
564 }
565
566 // Generator (somewhat getter) for network type options
567 function generateNetworkTypeOptions ($networkId) {
568         // Is this an array, then we just came back from edit/delete actions
569         if (is_array($networkId)) {
570                 $networkId = '';
571         } // END - if
572
573         // Is this cached?
574         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
575                 // Generate output and cache it
576                 $GLOBALS[__FUNCTION__][$networkId] = generateOptionList(
577                         'network_types',
578                         'network_type_id',
579                         'network_type_handler',
580                         $networkId,
581                         '',
582                         sprintf(
583                                 "WHERE `network_id`=%s",
584                                 bigintval(getRequestElement('network_id'))
585                         ),
586                         '',
587                         'translateNetworkTypeHandler'
588                 );
589         } // END - if
590
591         // Return content
592         return $GLOBALS[__FUNCTION__][$networkId];
593 }
594
595 // Generates an options list of all available (hard-coded) handlers
596 function generateNetworkTypesAvailableOptions () {
597         // Is it cached?
598         if (!isset($GLOBALS[__FUNCTION__])) {
599                 // Generate list
600                 $GLOBALS[__FUNCTION__] = generateOptionList(
601                         '/ARRAY/',
602                         array(
603                                 'banner',
604                                 'banner_click',
605                                 'banner_view',
606                                 'button',
607                                 'button_click',
608                                 'button_view',
609                                 'surfbar',
610                                 'surfbar_click',
611                                 'surfbar_view',
612                                 'forcedbanner',
613                                 'forcedtextlink',
614                                 'textlink',
615                                 'textlink_click',
616                                 'textlink_view',
617                                 'skybanner',
618                                 'skybanner_click',
619                                 'skybanner_view',
620                                 'layer',
621                                 'layer_click',
622                                 'layer_view',
623                                 'popup',
624                                 'popdown',
625                                 'textmail',
626                                 'htmlmail',
627                                 'lead',
628                                 'sale',
629                                 'payperactive',
630                                 'pagepeel',
631                                 'traffic'
632                         ),
633                         array(),
634                         '',
635                         '', '',
636                         $GLOBALS['network_types_disabled'],
637                         'translateNetworkTypeHandler'
638                 );
639         } // END - if
640
641         // Return content
642         return $GLOBALS[__FUNCTION__];
643 }
644
645 // Generates an options list (somewhat getter) ofr request keys
646 function generateNetworkRequestKeyOptions () {
647         // Is it cached?
648         if (!isset($GLOBALS[__FUNCTION__])) {
649                 // Generate and cache it
650                 $GLOBALS[__FUNCTION__] = generateOptionList(
651                         '/ARRAY/',
652                         array(
653                                 'id',
654                                 'sid',
655                                 'hash',
656                                 'password',
657                                 'reload',
658                                 'maximum_stay',
659                                 'minimum_stay',
660                                 'currency',
661                                 'type',
662                                 'remain',
663                                 'reward',
664                                 'size',
665                                 'erotic',
666                                 'extra',
667                                 'country'
668                         ),
669                         array(),
670                         '',
671                         '', '',
672                         $GLOBALS['network_request_params_disabled'],
673                         'translateNetworkRequestParamKey'
674                 );
675         } // END - if
676
677         // Return content
678         return $GLOBALS[__FUNCTION__];
679 }
680
681 // Generator (somewhat getter) for (return) array translation
682 function generateNetworkTranslationOptions ($default = '') {
683         // Is it cached?
684         if (!isset($GLOBALS[__FUNCTION__][$default])) {
685                 // Generate and cache it
686                 $GLOBALS[__FUNCTION__][$default] = generateOptionList(
687                         'network_translations',
688                         'network_translation_id',
689                         'network_translation_name',
690                         $default,
691                         '',
692                         '',
693                         $GLOBALS['network_translation_disabled'],
694                         'translateNetworkTranslationName'
695                 );
696         } // END - if
697
698         // Return content
699         return $GLOBALS[__FUNCTION__][$default];
700 }
701
702 // Generates an option list of request types
703 function generateNetworkRequestTypeOptions ($default = '') {
704         // Do we have cache?
705         if (!isset($GLOBALS[__FUNCTION__][$default])) {
706                 // Generate the list
707                 $GLOBALS[__FUNCTION__][$default] = generateOptionList(
708                         '/ARRAY/',
709                         array(
710                                 'GET',
711                                 'POST'
712                         ),
713                         array(
714                                 '{--ADMIN_NETWORK_REQUEST_TYPE_GET--}',
715                                 '{--ADMIN_NETWORK_REQUEST_TYPE_POST--}'
716                         ),
717                         $default
718                 );
719         } // END - if
720
721         // Return cache
722         return $GLOBALS[__FUNCTION__][$default];
723 }
724
725 // Generates an option list of network_api_active
726 function generateNetworkApiActiveOptions ($default = '') {
727         // Do we have cache?
728         if (!isset($GLOBALS[__FUNCTION__][$default])) {
729                 // Generate the list
730                 $GLOBALS[__FUNCTION__][$default] = generateYesNoOptionList($default);
731         } // END - if
732
733         // Return cache
734         return $GLOBALS[__FUNCTION__][$default];
735 }
736
737 // Translates 'translate_name' for e.g. templates
738 function translateNetworkTranslationName ($name) {
739         // Get the message id
740         return '{--ADMIN_NETWORK_TRANSLATE_' . strtoupper($name) . '_NAME--}';
741 }
742
743 // Translates the network type handler (e.g. banner, paidmail) for templates
744 function translateNetworkTypeHandler ($type) {
745         // Get the message id
746         return '{--ADMIN_NETWORK_HANDLER_TYPES_' . strtoupper($type) . '--}';
747 }
748
749 // Translates request type
750 function translateNetworkRequestType ($type) {
751         // Get the message id
752         return '{--ADMIN_NETWORK_REQUEST_TYPE_' . strtoupper($type) . '--}';
753 }
754
755 // Translates request parameter
756 function translateNetworkRequestParamKey ($param) {
757         // Get the message id
758         return '{--ADMIN_NETWORK_REQUEST_PARAMETER_' . strtoupper($param) . '--}';
759 }
760
761 // Translates API index
762 function translateNetworkApiIndex ($index) {
763         // Do we have cache?
764         if (!isset($GLOBALS['network_array_index'])) {
765                 // Get an array of all API array indexes
766                 $GLOBALS['network_array_index'] = array();
767
768                 // Get all entries
769                 $result = SQL_QUERY('SELECT
770         `network_array_id`,
771         `network_array_index`,
772         `network_translation_name`
773 FROM
774         `{?_MYSQL_PREFIX?}_network_array_translation`
775 INNER JOIN
776         `{?_MYSQL_PREFIX?}_network_translations`
777 ON
778         `network_array_index`=`network_translation_id`
779 ORDER BY
780         `sort` ASC', __FUNCTION__, __LINE__);
781
782                 // Do we have entries?
783                 if (!SQL_HASZERONUMS($result)) {
784                         // Get all entries
785                         while ($row = SQL_FETCHARRAY($result)) {
786                                 // Add it to our global array
787                                 $GLOBALS['network_array_index'][$row['network_array_index']] = $row;
788                         } // END - while
789                 } // END - if
790
791                 // Free result
792                 SQL_FREERESULT($result);
793         } // END - if
794
795         // Default name is unknown
796         $name = 'unknown';
797
798         // Is the entry there?
799         if (isset($GLOBALS['network_array_index'][$index])) {
800                 // Then get the name
801                 $name = $GLOBALS['network_array_index'][$index]['network_translation_name'];
802         } // END - if
803
804         // Return translation
805         return translateNetworkTranslationName($name);
806 }
807
808 // Translates network API configuration status (see function isNetworkApiConfigured()) by given id
809 function translateNetworkApiConfiguredStatusById ($networkId) {
810         // Do we have cache?
811         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
812                 // By default it is not configured
813                 $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_NOT_CONFIGURED--}';
814
815                 // So is it configured?
816                 if (isNetworkApiConfigured($networkId)) {
817                         // Yes, it is
818                         $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_CONFIGURED--}';
819                 } // END - if
820         } // END - if
821
822         // Return cache
823         return $GLOBALS[__FUNCTION__][$networkId];
824 }
825
826 // Checks if the given network is configured by looking its API configuration entry up
827 function isNetworkApiConfigured ($networkId) {
828         // Do we have cache?
829         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
830                 // Check for an entry in network_api_config
831                 $GLOBALS[__FUNCTION__][$networkId] = (countSumTotalData(
832                         bigintval($networkId),
833                         'network_api_config',
834                         'network_id',
835                         'network_id',
836                         true
837                 ) == 1);
838         } // END - if
839
840         // Return cache
841         return $GLOBALS[__FUNCTION__][$networkId];
842 }
843
844 // Checks wether the given network type handler is configured
845 function isNetworkTypeHandlerConfigured ($networkId, $networkTypeId) {
846         // Do we have cache?
847         if (!isset($GLOBALS[__FUNCTION__][$networkId][$networkTypeId])) {
848                 // Determine it
849                 $GLOBALS[__FUNCTION__][$networkId][$networkTypeId] = (countSumTotalData(
850                         bigintval($networkTypeId),
851                         'network_types_config',
852                         'network_data_id',
853                         'network_type_id',
854                         true,
855                         sprintf(' AND `network_id`=%s', bigintval($networkId))
856                 ) == 1);
857         } // END - if
858
859         // Return cache
860         return $GLOBALS[__FUNCTION__][$networkId][$networkTypeId];
861 }
862
863 //------------------------------------------------------------------------------
864 //                             Call-back functions
865 //------------------------------------------------------------------------------
866
867 // Callback function to add new network
868 function doAdminNetworkProcessAddNetwork () {
869         // We can say here, the form is sent, so check if the network is already added
870         if (isNetworkNameValid(postRequestElement('network_short_name'))) {
871                 // Already there
872                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_ALREADY_ADDED=' . postRequestElement('network_short_name') . '%}');
873                 return false;
874         } // END - if
875
876         // Remove the 'ok' part
877         unsetPostRequestElement('ok');
878
879         // Add the whole request to database
880         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_data'), __FUNCTION__, __LINE__);
881
882         // Add the id for output only
883         setPostRequestElement('network_id', SQL_INSERTID());
884
885         // Output message
886         if (!SQL_HASZEROAFFECTED()) {
887                 // Successfully added
888                 loadTemplate('admin_network_added', false, postRequestArray());
889         } else {
890                 // Not added
891                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_DATA_NOT_ADDED=' . postRequestElement('network_short_name') . '%}');
892         }
893 }
894
895 // Displays selected networks for editing
896 function doAdminNetworkProcessHandleNetworks () {
897         // Do we have selections?
898         if (ifPostContainsSelections()) {
899                 // Something has been selected, so start displaying one by one
900                 $OUT = '';
901                 foreach (postRequestElement('sel') as $networkId => $sel) {
902                         // Is this selected?
903                         if ($sel == 1) {
904                                 // Load this network's data
905                                 $networkData = getNetworkDataById($networkId);
906
907                                 // Do we have found the network?
908                                 if (count($networkData) > 0) {
909                                         // Add row template with given form name
910                                         $OUT .= loadTemplate('admin_' . $GLOBALS['network_form_name'] . '_networks_row', true, $networkData);
911                                 } // END - if
912                         } // END - if
913                 } // END - foreach
914
915                 // If we have no rows, we don't need to display the edit form
916                 if (!empty($OUT)) {
917                         // Output main template
918                         loadTemplate('admin_' . $GLOBALS['network_form_name'] . '_networks', false, $OUT);
919
920                         // Don't display the list/add new form
921                         $GLOBALS['network_display'] = false;
922                 } else {
923                         // Nothing selected/found
924                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_FOUND--}');
925                 }
926         } // END - if
927 }
928
929 // Handle network type form
930 function doAdminNetworkProcessHandleNetworkTypes () {
931         // Do we have selections?
932         if (ifPostContainsSelections()) {
933                 // Load network data
934                 $networkData = getNetworkDataById(getRequestElement('network_id'));
935
936                 // Something has been selected, so start displaying one by one
937                 $OUT = '';
938                 foreach (postRequestElement('sel') as $networkId => $sel) {
939                         // Is this selected?
940                         if ($sel == 1) {
941                                 // Load this network's data
942                                 $networkTypeData = getNetworkTypeDataById($networkId);
943
944                                 // Do we have found the network?
945                                 if (count($networkTypeData) > 0) {
946                                         if (isFormSent('edit')) {
947                                                 // Add row template for deleting
948                                                 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
949                                         } elseif (isFormSent('delete')) {
950                                                 // Add row template for deleting
951                                                 $OUT .= loadTemplate('admin_delete_network_types_row', true, $networkTypeData);
952                                         } else {
953                                                 // Problem!
954                                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
955                                         }
956                                 } // END - if
957                         } // END - if
958                 } // END - foreach
959
960                 // If we have no rows, we don't need to display the edit form
961                 if (!empty($OUT)) {
962                         // Output main template
963                         if (isFormSent('edit')) {
964                                 loadTemplate('admin_edit_network_types', false, $OUT);
965                         } elseif (isFormSent('delete')) {
966                                 loadTemplate('admin_delete_network_types', false, $OUT);
967                         } else {
968                                 // Problem!
969                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
970                         }
971
972                         // Don't display the list/add new form
973                         $GLOBALS['network_display'] = false;
974                 } else {
975                         // Nothing selected/found
976                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_HANDLER_TYPES_NOTHING_FOUND--}');
977                 }
978         } // END - if
979 }
980
981 // Handle network request parameter form
982 function doAdminNetworkProcessHandleRequestParams () {
983         // Do we have selections?
984         if (ifPostContainsSelections()) {
985                 // Init cache array
986                 $GLOBALS['network_request_params_disabled'] = array();
987
988                 // Load network data
989                 $networkData = getNetworkDataById(getRequestElement('network_id'));
990
991                 // Something has been selected, so start displaying one by one
992                 $OUT = '';
993                 foreach (postRequestElement('sel') as $networkId => $sel) {
994                         // Is this selected?
995                         if ($sel == 1) {
996                                 // Load this network's data
997                                 $networkRequestData = getNetworkRequestParamsDataById($networkId);
998
999                                 // Do we have found the network?
1000                                 if (count($networkRequestData) > 0) {
1001                                         if (isFormSent('edit')) {
1002                                                 // Add row template for deleting
1003                                                 $OUT .= loadTemplate('admin_edit_network_request_params_row', true, $networkRequestData);
1004                                         } elseif (isFormSent('delete')) {
1005                                                 // Get type data
1006                                                 $networkRequestData['network_type_data'] = getNetworkTypeDataById($networkRequestData['network_type_id']);
1007
1008                                                 // Add row template for deleting
1009                                                 $OUT .= loadTemplate('admin_delete_network_request_params_row', true, $networkRequestData);
1010                                         } else {
1011                                                 // Problem!
1012                                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
1013                                         }
1014                                 } // END - if
1015                         } // END - if
1016                 } // END - foreach
1017
1018                 // If we have no rows, we don't need to display the edit form
1019                 if (!empty($OUT)) {
1020                         // Output main template
1021                         if (isFormSent('edit')) {
1022                                 loadTemplate('admin_edit_network_request_params', false, $OUT);
1023                         } elseif (isFormSent('delete')) {
1024                                 loadTemplate('admin_delete_network_request_params', false, $OUT);
1025                         } else {
1026                                 // Problem!
1027                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
1028                         }
1029
1030                         // Don't display the list/add new form
1031                         $GLOBALS['network_display'] = false;
1032                 } else {
1033                         // Nothing selected/found
1034                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_FOUND--}');
1035                 }
1036         } // END - if
1037 }
1038
1039 // Changes given networks
1040 function doAdminNetworkProcessChangeNetworks () {
1041         // Do we have selections?
1042         if (ifPostContainsSelections()) {
1043                 // By default nothing is updated
1044                 $updated = 0;
1045
1046                 // Something has been selected, so start updating them
1047                 foreach (postRequestElement('sel') as $networkId => $sel) {
1048                         // Update this entry?
1049                         if ($sel == 1) {
1050                                 // Init data array
1051                                 $networkData = array();
1052
1053                                 // Transfer whole array, except 'sel'
1054                                 foreach (postRequestArray() as $key => $entry) {
1055                                         // Skip 'sel' and submit button
1056                                         if (in_array($key, array('sel', 'do_edit'))) {
1057                                                 continue;
1058                                         } // END - if
1059
1060                                         // Do we have this enty?
1061                                         if (!isset($entry[$networkId])) {
1062                                                 // Not found, needs fixing
1063                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1064                                         } // END - if
1065
1066                                         // Add this entry
1067                                         $networkData[$key] = $entry[$networkId];
1068                                 } // END - foreach
1069
1070                                 // Update the network data
1071                                 $updated += doNetworkUpdateDataByArray($networkId, $networkData);
1072                         } // END - if
1073                 } // END - foreach
1074
1075                 // Do we have updates?
1076                 if ($updated > 0) {
1077                         // Updates done
1078                         displayMessage('{%message,ADMIN_NETWORK_UPDATED=' . $updated . '%}');
1079                 } else {
1080                         // Nothing changed
1081                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_CHANGED--}');
1082                 }
1083         } // END - if
1084 }
1085
1086 // Removes given networks
1087 function doAdminNetworkProcessRemoveNetworks () {
1088         // Do we have selections?
1089         if (ifPostContainsSelections()) {
1090                 // By default nothing is removed
1091                 $removed = 0;
1092
1093                 // Something has been selected, so start updating them
1094                 foreach (postRequestElement('sel') as $networkId => $sel) {
1095                         // Update this entry?
1096                         if ($sel == 1) {
1097                                 // Remove this entry
1098                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $networkId);
1099                         } // END - if
1100                 } // END - foreach
1101
1102                 // Do we have removes?
1103                 if ($removed > 0) {
1104                         // Removals done
1105                         displayMessage('{%message,ADMIN_NETWORK_REMOVED=' . $removed . '%}');
1106                 } else {
1107                         // Nothing removed
1108                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_REMOVED--}');
1109                 }
1110         } // END - if
1111 }
1112
1113 // Add a network type handler if not yet found
1114 function doAdminNetworkProcessAddNetworkType () {
1115         // Is the network type handle already used with given network?
1116         if (isNetworkTypeHandleValid(postRequestElement('network_type_handler'), getRequestElement('network_id'))) {
1117                 // Already added
1118                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_HANDLER_TYPES_HANDLE_ALREADY_ADDED=' . postRequestElement('network_type_handler') . '%}');
1119
1120                 // ... so abort here
1121                 return false;
1122         } // END - if
1123
1124         // Remove the 'ok' part
1125         unsetPostRequestElement('ok');
1126
1127         // Add id
1128         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1129
1130         // Is network_type_banner_url set?
1131         if (postRequestElement('network_type_banner_url') == '') {
1132                 // Remove empty value to get a NULL for an optional entry
1133                 unsetPostRequestElement('network_type_banner_url');
1134         } // END - if
1135
1136         // Add the whole request to database
1137         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_types'), __FUNCTION__, __LINE__);
1138
1139         // Output message
1140         if (!SQL_HASZEROAFFECTED()) {
1141                 // Successfully added
1142                 loadTemplate('admin_network_type_added', false, postRequestArray());
1143         } else {
1144                 // Not added
1145                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_HANDLER_TYPES_NOT_ADDED=' . postRequestElement('network_type_handler') . '%}');
1146         }
1147 }
1148
1149 // Changes given network type handlers
1150 function doAdminNetworkProcessChangeHandlerTypes () {
1151         // Do we have selections?
1152         if (ifPostContainsSelections()) {
1153                 // By default nothing is updated
1154                 $updated = 0;
1155
1156                 // Something has been selected, so start updating them
1157                 foreach (postRequestElement('sel') as $networkId => $sel) {
1158                         // Update this entry?
1159                         if ($sel == 1) {
1160                                 // Init data array
1161                                 $networkTypeData = array();
1162
1163                                 // Transfer whole array, except 'sel'
1164                                 foreach (postRequestArray() as $key => $entry) {
1165                                         // Skip 'sel' and submit button
1166                                         if (in_array($key, array('sel', 'do_edit'))) {
1167                                                 continue;
1168                                         } // END - if
1169
1170                                         // Do we have this enty?
1171                                         if (!isset($entry[$networkId])) {
1172                                                 // Not found, needs fixing
1173                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1174                                         } // END - if
1175
1176                                         // Fix empty network_type_banner_url to NULL
1177                                         if (($key == 'network_type_banner_url') && (trim($entry[$networkId]) == '')) {
1178                                                 // Set it to NULL
1179                                                 $entry[$networkId] = NULL;
1180                                         } // END - if
1181
1182                                         // Add this entry
1183                                         $networkTypeData[$key] = $entry[$networkId];
1184                                 } // END - foreach
1185
1186                                 // Update the network data
1187                                 $updated += doNetworkUpdateTypeByArray($networkId, $networkTypeData);
1188                         } // END - if
1189                 } // END - foreach
1190
1191                 // Do we have updates?
1192                 if ($updated > 0) {
1193                         // Updates done
1194                         displayMessage('{%message,ADMIN_NETWORK_HANDLER_TYPES_UPDATED=' . $updated . '%}');
1195                 } else {
1196                         // Nothing changed
1197                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_HANDLER_TYPES_NOTHING_CHANGED--}');
1198                 }
1199         } // END - if
1200 }
1201
1202 // Changes given network request parameters
1203 function doAdminNetworkProcessChangeRequestParams () {
1204         // Do we have selections?
1205         if (ifPostContainsSelections()) {
1206                 // By default nothing is updated
1207                 $updated = 0;
1208
1209                 // Something has been selected, so start updating them
1210                 foreach (postRequestElement('sel') as $networkId => $sel) {
1211                         // Update this entry?
1212                         if ($sel == 1) {
1213                                 // Init data array
1214                                 $networkParamsData = array();
1215
1216                                 // Transfer whole array, except 'sel'
1217                                 foreach (postRequestArray() as $key => $entry) {
1218                                         // Skip 'sel' and submit button
1219                                         if (in_array($key, array('sel', 'do_edit'))) {
1220                                                 continue;
1221                                         } // END - if
1222
1223                                         // Do we have this enty?
1224                                         if (!isset($entry[$networkId])) {
1225                                                 // Not found, needs fixing
1226                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1227                                         } // END - if
1228
1229                                         // Fix empty network_request_param_default to NULL
1230                                         if (($key == 'network_request_param_default') && (trim($entry[$networkId]) == '')) {
1231                                                 // Set it to NULL
1232                                                 $entry[$networkId] = NULL;
1233                                         } // END - if
1234
1235                                         // Add this entry
1236                                         $networkParamsData[$key] = $entry[$networkId];
1237                                 } // END - foreach
1238
1239                                 // Update the network data
1240                                 $updated += doNetworkUpdateParamsByArray($networkId, $networkParamsData);
1241                         } // END - if
1242                 } // END - foreach
1243
1244                 // Do we have updates?
1245                 if ($updated > 0) {
1246                         // Updates done
1247                         displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_UPDATED=' . $updated . '%}');
1248                 } else {
1249                         // Nothing changed
1250                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_CHANGED--}');
1251                 }
1252         } // END - if
1253 }
1254
1255 // Removes given network type handlers
1256 function doAdminNetworkProcessRemoveNetworkTypes () {
1257         // Do we have selections?
1258         if (ifPostContainsSelections()) {
1259                 // By default nothing is removed
1260                 $removed = 0;
1261
1262                 // Something has been selected, so start updating them
1263                 foreach (postRequestElement('sel') as $networkId => $sel) {
1264                         // Update this entry?
1265                         if ($sel == 1) {
1266                                 // Remove this entry
1267                                 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $networkId);
1268                         } // END - if
1269                 } // END - foreach
1270
1271                 // Do we have removes?
1272                 if ($removed > 0) {
1273                         // Removals done
1274                         displayMessage('{%message,ADMIN_NETWORK_HANDLER_TYPES_REMOVED=' . $removed . '%}');
1275                 } else {
1276                         // Nothing removed
1277                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_HANDLER_TYPES_NOTHING_REMOVED--}');
1278                 }
1279         } // END - if
1280 }
1281
1282 // Removes given network request parameters
1283 function doAdminNetworkProcessRemoveNetworkRequestParams () {
1284         // Do we have selections?
1285         if (ifPostContainsSelections()) {
1286                 // By default nothing is removed
1287                 $removed = 0;
1288
1289                 // Something has been selected, so start updating them
1290                 foreach (postRequestElement('sel') as $networkId => $sel) {
1291                         // Update this entry?
1292                         if ($sel == 1) {
1293                                 // Remove this entry
1294                                 $removed += doAdminRemoveNetworkEntry('request_params', 'network_request_param_id', $networkId);
1295                         } // END - if
1296                 } // END - foreach
1297
1298                 // Do we have removes?
1299                 if ($removed > 0) {
1300                         // Removals done
1301                         displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_REMOVED=' . $removed . '%}');
1302                 } else {
1303                         // Nothing removed
1304                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_REMOVED--}');
1305                 }
1306         } // END - if
1307 }
1308
1309 // Adds a request parameter to given network and type
1310 function doAdminNetworkProcessAddNetworkRequestParams () {
1311         // Is the request parameter already used with given network?
1312         if (isNetworkRequestElementValid(postRequestElement('network_request_param_key'), postRequestElement('network_type_id'), getRequestElement('network_id'))) {
1313                 // Already added
1314                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED=' . postRequestElement('network_request_param_key') . '%}');
1315
1316                 // ... so abort here
1317                 return false;
1318         } // END - if
1319
1320         // Remove the 'ok' part
1321         unsetPostRequestElement('ok');
1322
1323         // Add id
1324         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1325
1326         // Is network_request_param_default set?
1327         if (postRequestElement('network_request_param_default') == '') {
1328                 // Remove empty value to get a NULL for an optional entry
1329                 unsetPostRequestElement('network_request_param_default');
1330         } // END - if
1331
1332         // Add the whole request to database
1333         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_request_params'), __FUNCTION__, __LINE__);
1334
1335         // Output message
1336         if (!SQL_HASZEROAFFECTED()) {
1337                 // Successfully added
1338                 loadTemplate('admin_network_request_param_added', false, postRequestArray());
1339         } else {
1340                 // Not added
1341                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED=' . postRequestElement('network_request_param_key') . '%}');
1342         }
1343 }
1344
1345 // Adds a API response array entry
1346 function doAdminNetworkProcessAddNetworkArrayTranslation () {
1347         // Is the request parameter already used with given network?
1348         if (isNetworkArrayTranslationValid(postRequestElement('network_array_index'), postRequestElement('network_type_id'), getRequestElement('network_id'))) {
1349                 // Already added
1350                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_ALREADY_ADDED=' . postRequestElement('network_array_index') . '%}');
1351
1352                 // ... so abort here
1353                 return false;
1354         } // END - if
1355
1356         // Remove the 'ok' part
1357         unsetPostRequestElement('ok');
1358
1359         // Add id
1360         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1361
1362         // Add sorting
1363         setPostRequestElement('sort', (countSumTotalData(
1364                 bigintval(postRequestElement('network_id')),
1365                 'network_array_translation',
1366                 'network_array_id',
1367                 'network_id',
1368                 true,
1369                 sprintf(" AND `network_type_id`=%s", bigintval(postRequestElement('network_type_id')))
1370         ) + 1));
1371
1372         // Add the whole request to database
1373         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_array_translation'), __FUNCTION__, __LINE__);
1374
1375         // Output message
1376         if (!SQL_HASZEROAFFECTED()) {
1377                 // Successfully added
1378                 loadTemplate('admin_network_array_translation_added', false, postRequestArray());
1379         } else {
1380                 // Not added
1381                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_NOT_ADDED=' . postRequestElement('network_array_index') . '%}');
1382         }
1383 }
1384
1385 // Adds/update network API configuration
1386 function doAdminNetworkProcessNetworkApiConfig () {
1387         // Remove the 'ok' part
1388         unsetPostRequestElement('ok');
1389
1390         // Add id
1391         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1392
1393         // Is network_api_referral_button set?
1394         if (postRequestElement('network_api_referral_button') == '') {
1395                 // Remove empty value to get a NULL for an optional entry
1396                 unsetPostRequestElement('network_api_referral_button');
1397         } // END - if
1398
1399         // Is there already an entry?
1400         if (isNetworkApiConfigured(getRequestElement('network_id'))) {
1401                 // Generate SQL query
1402                 $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_api_config', 'network_id', postRequestElement('network_id'), array('network_id'));
1403         } else {
1404                 // Insert new entry
1405                 $SQL = getInsertSqlFromArray(postRequestArray(), 'network_api_config');
1406         }
1407
1408         // Run the query
1409         SQL_QUERY($SQL, __FUNCTION__, __LINE__);
1410
1411         // Output message
1412         if (!SQL_HASZEROAFFECTED()) {
1413                 // Successfully added
1414                 displayMessage('{--ADMIN_CONFIG_NETWORK_API_SAVED--}');
1415         } else {
1416                 // Not added
1417                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_CONFIG_NETWORK_API_NOT_SAVED--}');
1418         }
1419 }
1420
1421 // Only adds network type configuration if not yet present
1422 function doAdminNetworkProcessAddHandlerTypesConfig () {
1423         // Remove the 'ok' part
1424         unsetPostRequestElement('ok');
1425
1426         // Add both ids
1427         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1428         setPostRequestElement('network_type_id', bigintval(getRequestElement('network_type_id')));
1429
1430         /*
1431          * Some parameters are optional, at least one must be given so check a bunch
1432          * of parameters.
1433          */
1434         foreach (array('network_min_waiting_time', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic') as $element) {
1435                 // Is this element empty?
1436                 if (postRequestElement($element) == '') {
1437                         // Then unset it to get a NULL for optional parameter
1438                         unsetPostRequestElement($element);
1439                 } // END - if
1440         } // END - foreach
1441
1442         // Initialize variables
1443         $content = array();
1444         $id = 'network_max_reload_time_ye';
1445         $skip = false;
1446         $postData = postRequestArray();
1447
1448         // Convert "reload time selections"
1449         convertSelectionsToEpocheTime($postData, $content, $id, $skip);
1450
1451         // Set the POST array back
1452         setPostRequestArray($postData);
1453
1454         // Is there already an entry?
1455         if (isNetworkTypeHandlerConfigured(getRequestElement('network_id'), getRequestElement('network_type_id'))) {
1456                 // This network type handler is already configured
1457                 displayMessage('{--ADMIN_NETWORK_HANDLER_TYPE_HANDLER_ALREADY_CONFIGURED--}');
1458                 return;
1459         } // END - if
1460
1461         // Get SQL query for new entry
1462         $SQL = getInsertSqlFromArray(postRequestArray(), 'network_types_config');
1463
1464         // Run the query
1465         SQL_QUERY($SQL, __FUNCTION__, __LINE__);
1466
1467         // Output message
1468         if (!SQL_HASZEROAFFECTED()) {
1469                 // Successfully added
1470                 displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_SAVED--}');
1471         } else {
1472                 // Not added
1473                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_SAVED--}');
1474         }
1475 }
1476
1477 // Only changes network type configuration if not yet present
1478 function doAdminNetworkProcessEditHandlerTypesConfig () {
1479         // Remove the 'ok' part
1480         unsetPostRequestElement('ok');
1481
1482         // Add both ids
1483         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1484         setPostRequestElement('network_type_id', bigintval(getRequestElement('network_type_id')));
1485
1486         /*
1487          * Some parameters are optional, at least one must be given so check a bunch
1488          * of parameters.
1489          */
1490         foreach (array('network_min_waiting_time', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic') as $element) {
1491                 // Is this element empty?
1492                 if (postRequestElement($element) == '') {
1493                         // Then unset it to get a NULL for optional parameter
1494                         unsetPostRequestElement($element);
1495                 } // END - if
1496         } // END - foreach
1497
1498         // Initialize variables
1499         $content = array();
1500         $id = 'network_max_reload_time_ye';
1501         $skip = false;
1502         $postData = postRequestArray();
1503
1504         // Convert "reload time selections"
1505         convertSelectionsToEpocheTime($postData, $content, $id, $skip);
1506
1507         // Set the POST array back
1508         setPostRequestArray($postData);
1509
1510         // Is there already an entry?
1511         if (!isNetworkTypeHandlerConfigured(getRequestElement('network_id'), getRequestElement('network_type_id'))) {
1512                 // This network type handler is not configured
1513                 displayMessage('{--ADMIN_NETWORK_HANDLER_TYPE_HANDLER_NOT_CONFIGURED--}');
1514                 return;
1515         } // END - if
1516
1517         // Get SQL query for new entry
1518         $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_types_config', 'network_data_id', postRequestElement('network_data_id'), array('network_data_id'));
1519
1520         // Run the query
1521         SQL_QUERY($SQL, __FUNCTION__, __LINE__);
1522
1523         // Output message
1524         if (!SQL_HASZEROAFFECTED()) {
1525                 // Successfully added
1526                 displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_SAVED--}');
1527         } else {
1528                 // Not added
1529                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}');
1530         }
1531 }
1532
1533 // Do expression code for this extension
1534 function doExpressionNetwork ($data) {
1535         // Construct replacer
1536         $replacer = sprintf(
1537                 "{DQUOTE} . %s(%s, '%s') . {DQUOTE}",
1538                 $data['callback'],
1539                 $data['matches'][4][$data['key']],
1540                 $data['extra_func']
1541         );
1542
1543         // Replace %network_id% with the current network id
1544         $replacer = str_replace('%network_id%', getCurrentNetworkId(), $replacer);
1545
1546         // Replace the code
1547         $code = replaceExpressionCode($data, $replacer);
1548
1549         // Return it
1550         return $code;
1551 }
1552
1553 // ----------------------------------------------------------------------------
1554 //                     Call-back functions for exporting data
1555 // ----------------------------------------------------------------------------
1556
1557 // Callback function to export network tables
1558 function doAdminNetworkProcessExport () {
1559         // Init table with all valid what->table entries
1560         $validExports = array(
1561                 // General network data
1562                 'list_networks'                  => 'network_data',
1563                 // Network type handler
1564                 'list_network_types'             => 'network_types',
1565                 // Network request parameter
1566                 'list_network_request_params'    => 'network_request_params',
1567                 // Network API response array index translation
1568                 'list_network_array_translation' => 'network_array_translation',
1569         );
1570
1571         // Is the 'what' key valid?
1572         if (!isset($validExports[getWhat()])) {
1573                 // Not valid
1574                 debug_report_bug(__FUNCTION__, __LINE__, 'what=' . getWhat() . ' - not supported');
1575         } // END - if
1576
1577         // Generate call-back, some tables require to export not all columns
1578         $callbackName = 'doAdminNetworkExport' . capitalizeUnderscoreString($validExports[getWhat()]);
1579
1580         // Is the call-back function there?
1581         if (!function_exists($callbackName)) {
1582                 // No, this is really bad
1583                 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid call-back function ' . $callbackName . ' detected.');
1584         } elseif (isset($GLOBALS[__FUNCTION__][$callbackName])) {
1585                 // Already called!
1586                 debug_report_bug(__FUNCTION__, __LINE__, 'Double-call of export function ' . $callbackName . ' detected.');
1587         }
1588
1589         // Call the function
1590         call_user_func($callbackName);
1591
1592         // Mark it as called
1593         $GLOBALS[__FUNCTION__][$callbackName] = true;
1594
1595         // Don't display the list/add new form
1596         $GLOBALS['network_display'] = false;
1597 }
1598
1599 // Exports (and displays) the table 'network_data'
1600 function doAdminNetworkExportNetworkData () {
1601         // Query for all networks
1602         $result = SQL_QUERY('SELECT
1603         `network_short_name`,
1604         `network_title`,
1605         `network_reflink`,
1606         `network_data_separator`,
1607         `network_row_separator`,
1608         `network_request_type`,
1609         `network_charset`,
1610         `network_require_id_card`,
1611         `network_query_amount`
1612 FROM
1613         `{?_MYSQL_PREFIX?}_network_data`
1614 ORDER BY
1615         `network_id` ASC',
1616                 __FUNCTION__, __LINE__);
1617
1618         // Start an empty SQL query
1619         $SQL = '';
1620
1621         // Load all entries
1622         while ($content = SQL_FETCHARRAY($result)) {
1623                 // Add row
1624                 $SQL .= "<pre>('" .
1625                         $content['network_short_name'] . "', '" .
1626                         $content['network_title'] . "', '" .
1627                         $content['network_reflink'] . "', '" .
1628                         $content['network_data_separator'] . "', '" .
1629                         $content['network_row_separator'] . "', '" .
1630                         $content['network_request_type'] . "', '" .
1631                         $content['network_charset'] . "', '" .
1632                         $content['network_require_id_card'] . "', " .
1633                         $content['network_query_amount'] . '),</pre>';
1634         } // END - while
1635
1636         // Remove last commata and close braces
1637         $SQL = substr($SQL, 0, -7) . '</pre>';
1638
1639         // Free result
1640         SQL_FREERESULT($result);
1641
1642         // Output the SQL query
1643         loadTemplate('admin_export_network_data', false, $SQL);
1644 }
1645
1646 // Exports (and displays) the table 'network_types'
1647 function doAdminNetworkExportNetworkTypes () {
1648         // 'network_id' must be set
1649         if (!isGetRequestElementSet('network_id')) {
1650                 // Only network handlers of one network will be exported per time
1651                 debug_report_bug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.');
1652         } // END - if
1653
1654         // Get all network types of given network
1655         $result = SQL_QUERY_ESC('SELECT
1656         `network_type_id`,
1657         `network_id`,
1658         `network_type_handler`,
1659         `network_type_api_url`,
1660         `network_type_click_url`,
1661         `network_type_banner_url`
1662 FROM
1663         `{?_MYSQL_PREFIX?}_network_types`
1664 WHERE
1665         `network_id`=%s
1666 ORDER BY
1667         `network_type_id` ASC',
1668                 array(
1669                         bigintval(getRequestElement('network_id'))
1670                 ), __FUNCTION__, __LINE__);
1671
1672         // Start an empty SQL query
1673         $SQL = "<pre>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`) VALUES\n";
1674
1675         // Load all entries
1676         while ($content = SQL_FETCHARRAY($result)) {
1677                 // Add row
1678                 $SQL .= '(' .
1679                         $content['network_type_id'] . ', ' .
1680                         $content['network_id'] . ", '" .
1681                         $content['network_type_handler'] . "', '" .
1682                         $content['network_type_api_url'] . "', '" .
1683                         $content['network_type_click_url'] . "', ";
1684                 
1685                 // Is the column NULL?
1686                 if (is_null($content['network_type_banner_url'])) {
1687                         // Column is NULL
1688                         $SQL .= "NULL),\n";
1689                 } else {
1690                         // Column is set
1691                         $SQL .= $content['network_type_banner_url'] . "'),\n";
1692                 }
1693         } // END - while
1694
1695         // Remove last commata and close braces
1696         $SQL = substr($SQL, 0, -2) . '</pre>';
1697
1698         // Free result
1699         SQL_FREERESULT($result);
1700
1701         // Output the SQL query
1702         loadTemplate('admin_export_network_types', false, $SQL);
1703 }
1704
1705 // Exports (and displays) the table 'network_request_params'
1706 function doAdminNetworkExportNetworkRequestParams () {
1707         // 'network_id' must be set
1708         if (!isGetRequestElementSet('network_id')) {
1709                 // Only network request parameters of one network will be exported per time
1710                 debug_report_bug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.');
1711         } // END - if
1712
1713         // Get all network types of given network
1714         $result = SQL_QUERY_ESC('SELECT
1715         `network_id`,
1716         `network_type_id`,
1717         `network_request_param_key`,
1718         `network_request_param_value`,
1719         `network_request_param_default`
1720 FROM
1721         `{?_MYSQL_PREFIX?}_network_request_params`
1722 WHERE
1723         `network_id`=%s
1724 ORDER BY
1725         `network_type_id` ASC ,
1726         `network_request_param_id` ASC',
1727                 array(
1728                         bigintval(getRequestElement('network_id'))
1729                 ), __FUNCTION__, __LINE__);
1730
1731         // Start an empty SQL query
1732         $SQL = "<pre>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";
1733
1734         // Load all entries
1735         while ($content = SQL_FETCHARRAY($result)) {
1736                 // Add row
1737                 $SQL .= '(' .
1738                         $content['network_id'] . ', ' .
1739                         $content['network_type_id'] . ", '" .
1740                         $content['network_request_param_key'] . "', '" .
1741                         $content['network_request_param_value'] . "', '";
1742                 
1743                 // Is the column NULL?
1744                 if (is_null($content['network_request_param_default'])) {
1745                         // Column is NULL
1746                         $SQL .= "NULL),\n";
1747                 } else {
1748                         // Column is set
1749                         $SQL .= $content['network_request_param_default'] . "'),\n";
1750                 }
1751         } // END - while
1752
1753         // Remove last commata and close braces
1754         $SQL = substr($SQL, 0, -2) . '</pre>';
1755
1756         // Free result
1757         SQL_FREERESULT($result);
1758
1759         // Output the SQL query
1760         loadTemplate('admin_export_network_request_params', false, $SQL);
1761 }
1762
1763 // Exports (and displays) the table 'network_array_translation'
1764 function doAdminNetworkExportNetworkArrayTranslation () {
1765         // 'network_id' must be set
1766         if (!isGetRequestElementSet('network_id')) {
1767                 // Only network API array index translations of one network will be exported per time
1768                 debug_report_bug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.');
1769         } // END - if
1770
1771         // Get all network types of given network
1772         $result = SQL_QUERY_ESC('SELECT
1773         `network_id`,
1774         `network_type_id`,
1775         `network_array_index`,
1776         `sort`
1777 FROM
1778         `{?_MYSQL_PREFIX?}_network_array_translation`
1779 WHERE
1780         `network_id`=%s
1781 ORDER BY
1782         `network_type_id` ASC,
1783         `sort` ASC',
1784                 array(
1785                         bigintval(getRequestElement('network_id'))
1786                 ), __FUNCTION__, __LINE__);
1787
1788         // Start an empty SQL query
1789         $SQL = "<pre>INSERT INTO `&#123;&#63;_MYSQL_PREFIX&#63;&#125;_network_array_translation` (`network_id`,`network_type_id`,`network_array_index`,`sort`) VALUES\n";
1790
1791         // Load all entries
1792         while ($content = SQL_FETCHARRAY($result)) {
1793                 // Add row
1794                 $SQL .= '(' .
1795                         $content['network_id'] . ', ' .
1796                         $content['network_type_id'] . ', ' .
1797                         $content['network_array_index'] . ', ' .
1798                         $content['sort'] . "),\n";
1799         } // END - while
1800
1801         // Remove last commata and close braces
1802         $SQL = substr($SQL, 0, -2) . '</pre>';
1803
1804         // Free result
1805         SQL_FREERESULT($result);
1806
1807         // Output the SQL query
1808         loadTemplate('admin_export_network_array_translation', false, $SQL);
1809 }
1810
1811 // [EOF]
1812 ?>