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