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 // Handle a (maybe) sent form here
54 function doNetworkHandleForm () {
55         // Was the form sent?
56         if ((isFormSent()) || (isFormSent('edit')) || (isFormSent('delete')) || (isFormSent('do_edit')) || (isFormSent('do_delete'))) {
57                 // Do we have a 'do'?
58                 if (isGetRequestElementSet('do')) {
59                         // Process the request
60                         doAdminNetworkProcessForm();
61                 } else {
62                         // No 'do' found
63                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_DO_404--}');
64                 }
65         } // END - if
66 }
67
68 // Processes an admin form
69 function doAdminNetworkProcessForm () {
70         // Form really sent?
71         if ((!isFormSent()) && (!isFormSent('edit')) && (!isFormSent('delete')) && (!isFormSent('do_edit')) && (!isFormSent('do_delete'))) {
72                 // Abort here
73                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_FORM_NOT_SENT--}');
74                 return;
75         } elseif (!isGetRequestElementSet('do')) {
76                 // No 'do' found
77                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_DO_404--}');
78                 return;
79         }
80
81         // Create function name
82         $functionName = sprintf("doAdminNetworkProcess%s", capitalizeUnderscoreString(getRequestElement('do')));
83
84         // Is the function valid?
85         if (!function_exists($functionName)) {
86                 // Invalid function name
87                 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', false);
88         } // END - if
89
90         // Call-back the method handling our request
91         call_user_func($functionName);
92 }
93
94 // Checks wether the (short) network name is already used (valid)
95 function isNetworkNameValid ($name) {
96         // Query for it
97         $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1",
98                 array($name), __FUNCTION__, __LINE__);
99
100         // Does it exist?
101         $isValid = (SQL_NUMROWS($result) == 1);
102
103         // Free result
104         SQL_FREERESULT($result);
105
106         // Return result
107         return $isValid;
108 }
109
110 // Checks wether the given network type is already used (valid)
111 function isNetworkTypeHandleValid ($type, $networkId) {
112         // Query for it
113         $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handle`='%s' LIMIT 1",
114                 array($networkId, $type), __FUNCTION__, __LINE__);
115
116         // Does it exist?
117         $isValid = (SQL_NUMROWS($result) == 1);
118
119         // Free result
120         SQL_FREERESULT($result);
121
122         // Return result
123         return $isValid;
124 }
125
126 // Checks wether the given network request parameter is already used (valid)
127 function isNetworkRequestElementValid ($key, $type, $networkId) {
128         // Query for it
129         $result = SQL_QUERY_ESC("SELECT `network_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",
130                 array($networkId, $type, $key), __FUNCTION__, __LINE__);
131
132         // Does it exist?
133         $isValid = (SQL_NUMROWS($result) == 1);
134
135         // Free result
136         SQL_FREERESULT($result);
137
138         // Return result
139         return $isValid;
140 }
141
142 // Checks wether the given network API array translation
143 function isNetworkApiTranslationValid ($key, $type, $networkId) {
144         // Query for it
145         $result = SQL_QUERY_ESC("SELECT `network_api_id` FROM `{?_MYSQL_PREFIX?}_network_api_translation` WHERE `network_id`=%s AND `network_type_id`=%s AND `network_api_index`='%s' LIMIT 1",
146                 array($networkId, $type, $key), __FUNCTION__, __LINE__);
147
148         // Does it exist?
149         $isValid = (SQL_NUMROWS($result) == 1);
150
151         // Free result
152         SQL_FREERESULT($result);
153
154         // Return result
155         return $isValid;
156 }
157
158 // "Getter" for a network's data by provided id number
159 function getNetworkDataById ($networkId, $column = '') {
160         // Ids lower one are not accepted
161         if ($networkId < 1) {
162                 // Not good, should be fixed
163                 debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
164         } // END - if
165
166         // Set current network id
167         setCurrentNetworkId($networkId);
168
169         // Is it cached?
170         if (!isset($GLOBALS['network_data'][$networkId])) {
171                 // By default we have no data
172                 $GLOBALS['network_data'][$networkId] = array();
173
174                 // Query for the network data
175                 $result = SQL_QUERY_ESC("SELECT
176         `network_id`,
177         `network_short_name`,
178         `network_title`,
179         `network_reflink`,
180         `network_data_separator`,
181         `network_row_separator`,
182         `network_request_type`,
183         `network_charset`,
184         `network_require_id_card`,
185         `network_query_amount`
186 FROM
187         `{?_MYSQL_PREFIX?}_network_data`
188 WHERE
189         `network_id`=%s
190 LIMIT 1",
191                         array(bigintval($networkId)), __FUNCTION__, __LINE__);
192
193                 // Do we have an entry?
194                 if (SQL_NUMROWS($result) == 1) {
195                         // Then get it
196                         $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
197                 } // END - if
198
199                 // Free result
200                 SQL_FREERESULT($result);
201         } // END - if
202
203         // Return result
204         if (empty($column)) {
205                 // Return array
206                 return $GLOBALS['network_data'][$networkId];
207         } else {
208                 // Return column
209                 return $GLOBALS['network_data'][$networkId][$column];
210         }
211 }
212
213 // "Getter" for a network's data by provided type id number
214 function getNetworkDataByTypeId ($networkId, $column = '') {
215         // Ids lower one are not accepted
216         if ($networkId < 1) {
217                 // Not good, should be fixed
218                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
219         } // END - if
220
221         // Set current network id
222         setCurrentNetworkId($networkId);
223
224         // Is it cached?
225         if (!isset($GLOBALS['network_data'][$networkId])) {
226                 // By default we have no data
227                 $GLOBALS['network_data'][$networkId] = array();
228
229                 // Query for the network data
230                 $result = SQL_QUERY_ESC("SELECT
231         d.`network_id`,
232         d.`network_short_name`,
233         d.`network_title`,
234         d.`network_reflink`,
235         d.`network_data_separator`,
236         d.`network_row_separator`,
237         d.`network_request_type`,
238         d.`network_charset`,
239         d.`network_require_id_card`,
240         d.`network_query_amount`,
241         t.`network_type_handle`,
242         t.`network_type_api_url`,
243         t.`network_type_click_url`,
244         t.`network_type_banner_url`
245 FROM
246         `{?_MYSQL_PREFIX?}_network_data` AS d
247 LEFT JOIN
248         `{?_MYSQL_PREFIX?}_network_types` AS t
249 ON
250         d.`network_id`=t.`network_id`
251 WHERE
252         t.`network_type_id`=%s
253 LIMIT 1",
254                         array(bigintval($networkId)), __FUNCTION__, __LINE__);
255
256                 // Do we have an entry?
257                 if (SQL_NUMROWS($result) == 1) {
258                         // Then get it
259                         $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result);
260                 } // END - if
261
262                 // Free result
263                 SQL_FREERESULT($result);
264         } // END - if
265
266         // Return result
267         if (empty($column)) {
268                 // Return array
269                 return $GLOBALS['network_data'][$networkId];
270         } else {
271                 // Return column
272                 return $GLOBALS['network_data'][$networkId][$column];
273         }
274 }
275
276 // "Getter" for a network type data by provided id number
277 function getNetworkTypeDataById ($networkId) {
278         // Ids lower one are not accepted
279         if ($networkId < 1) {
280                 // Not good, should be fixed
281                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.');
282         } // END - if
283
284         // By default we have no data
285         $GLOBALS['network_type_data'][$networkId] = array();
286
287         // Query for the network data
288         $result = SQL_QUERY_ESC("SELECT
289         `network_type_id`
290         `network_id`,
291         `network_type_handle`,
292         `network_type_api_url`,
293         `network_type_click_url`,
294         `network_type_banner_url`
295 FROM
296         `{?_MYSQL_PREFIX?}_network_types`
297 WHERE
298         `network_type_id`=%s
299 LIMIT 1",
300                 array(bigintval($networkId)), __FUNCTION__, __LINE__);
301
302         // Do we have an entry?
303         if (SQL_NUMROWS($result) == 1) {
304                 // Then get it
305                 $GLOBALS['network_type_data'][$networkId] = SQL_FETCHARRAY($result);
306         } // END - if
307
308         // Free result
309         SQL_FREERESULT($result);
310
311         // Return result
312         return $GLOBALS['network_type_data'][$networkId];
313 }
314
315 // "Getter" for a network request parameter data by provided id number
316 function getNetworkRequestParamsDataById ($networkId) {
317         // Ids lower one are not accepted
318         if ($networkId < 1) {
319                 // Not good, should be fixed
320                 debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkId . ' is smaller than 1.');
321         } // END - if
322
323         // By default we have no data
324         $networkRequestData = array();
325
326         // Query for the network data
327         $result = SQL_QUERY_ESC("SELECT
328         `network_param_id`,
329         `network_id`,
330         `network_type_id`,
331         `network_request_param_key`,
332         `network_request_param_value`,
333         `network_request_param_default`
334 FROM
335         `{?_MYSQL_PREFIX?}_network_request_params`
336 WHERE
337         `network_param_id`=%s
338 LIMIT 1",
339                 array(bigintval($networkId)), __FUNCTION__, __LINE__);
340
341         // Do we have an entry?
342         if (SQL_NUMROWS($result) == 1) {
343                 // Then get it
344                 $networkRequestData = SQL_FETCHARRAY($result);
345         } // END - if
346
347         // Free result
348         SQL_FREERESULT($result);
349
350         // Return result
351         return $networkRequestData;
352 }
353
354 // Updates given network (id) with data from array
355 function doNetworkUpdateDataByArray ($networkId, $networkData) {
356         // Ids lower one are not accepted
357         if ($networkId < 1) {
358                 // Not good, should be fixed
359                 debug_report_bug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.');
360         } // END - if
361
362         // Just call our inner method
363         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($networkId)), array(), false, false);
364 }
365
366 // Updates given network type handler (id) with data from array
367 function doNetworkUpdateTypeByArray ($networkId, $networkTypeData) {
368         // Ids lower one are not accepted
369         if ($networkId < 1) {
370                 // Not good, should be fixed
371                 debug_report_bug(__FUNCTION__, __LINE__, 'Network type handler id ' . $networkId . ' is smaller than 1.');
372         } // END - if
373
374         // Just call our inner method
375         return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($networkId)), array(), false, false);
376 }
377
378 // Updates given network request parameters (id) with data from array
379 function doNetworkUpdateParamsByArray ($networkId, $networkParamData) {
380         // Ids lower one are not accepted
381         if ($networkId < 1) {
382                 // Not good, should be fixed
383                 debug_report_bug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkId . ' is smaller than 1.');
384         } // END - if
385
386         // Just call our inner method
387         return adminSaveSettings($networkParamData, '_network_request_params', sprintf("`network_param_id`=%s", bigintval($networkId)), array(), false, false);
388 }
389
390 // Removes given network entry
391 function doAdminRemoveNetworkEntry ($table, $column, $networkId, $limit = 1) {
392         // Remove the entry
393         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
394                 array($table, $column, $networkId, $limit), __FUNCTION__, __LINE__);
395
396         // Return affected rows
397         return SQL_AFFECTEDROWS();
398 }
399
400 // Generates a list of networks for given script and returns it
401 function generateAdminNetworkList () {
402         // Init output
403         $content = '';
404
405         // Query for all networks
406         $result = SQL_QUERY('SELECT
407         `network_id`,
408         `network_short_name`,
409         `network_title`
410 FROM
411         `{?_MYSQL_PREFIX?}_network_data`
412 ORDER BY
413         `network_short_name` ASC', __FUNCTION__, __LINE__);
414
415         // Do we have entries?
416         if (!SQL_HASZERONUMS($result)) {
417                 // List all entries
418                 $rows = array();
419                 while ($row = SQL_FETCHARRAY($result)) {
420                         // Is this valid, then add it
421                         if ((is_array($row)) && (isset($row['network_id']))) {
422                                 // Add entry
423                                 $rows[$row['network_id']] = $row;
424                         } // END - if
425                 } // END - while
426
427                 // Generate the selection box
428                 $content = generateSelectionBoxFromArray($rows, 'network_id', 'network_id', '', '', 'network');
429         } else {
430                 // Nothing selected
431                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
432         }
433
434         // Free the result
435         SQL_FREERESULT($result);
436
437         // Return the list
438         return $content;
439 }
440
441 // Generator (somewhat getter) for a list of network types for given network id
442 function generateAdminNetworkTypeList ($networkId) {
443         // Init content
444         $content = '';
445
446         // Query all types of this network
447         $result = SQL_QUERY_ESC("SELECT
448         `network_type_id`,`network_type_handle`
449 FROM
450         `{?_MYSQL_PREFIX?}_network_types`
451 WHERE
452         `network_id`=%s
453 ORDER BY
454         `network_type_handle` ASC",
455                 array(
456                         bigintval($networkId)
457                 ), __FUNCTION__, __LINE__);
458
459         // Do we have entries?
460         if (!SQL_HASZERONUMS($result)) {
461                 // List all entries
462                 $rows = array();
463                 while ($row = SQL_FETCHARRAY($result)) {
464                         // Is this valid, then add it
465                         if ((is_array($row)) && (isset($row['network_type_id']))) {
466                                 // Add entry
467                                 $rows[$row['network_type_id']] = $row;
468                         } // END - if
469                 } // END - while
470
471                 // Generate the selection box
472                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id');
473         } else {
474                 // Nothing selected
475                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
476         }
477
478         // Free the result
479         SQL_FREERESULT($result);
480
481         // Return content
482         return $content;
483 }
484
485 // Generator (somewhat getter) for a list of network types for all types
486 function generateAdminDistinctNetworkTypeList () {
487         // Init content
488         $content = '';
489
490         // Query all types of this network
491         $result = SQL_QUERY('SELECT
492         t.`network_type_id`, t.`network_type_handle`, d.`network_title`
493 FROM
494         `{?_MYSQL_PREFIX?}_network_types` AS t
495 LEFT JOIN
496         `{?_MYSQL_PREFIX?}_network_data` AS d
497 ON
498         t.`network_id`=d.`network_id`
499 ORDER BY
500         d.`network_short_name` ASC,
501         t.`network_type_handle` ASC', __FUNCTION__, __LINE__);
502
503         // Do we have entries?
504         if (!SQL_HASZERONUMS($result)) {
505                 // List all entries
506                 $rows = array();
507                 while ($row = SQL_FETCHARRAY($result)) {
508                         // Is this valid, then add it
509                         if ((is_array($row)) && (isset($row['network_type_id']))) {
510                                 // Add entry
511                                 $rows[$row['network_type_id']] = $row;
512                         } // END - if
513                 } // END - while
514
515                 // Generate the selection box
516                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id', '', '_title');
517         } else {
518                 // Nothing selected
519                 $content = loadTemplate('admin_settings_unsaved', false, '{--ADMIN_ENTRIES_404--}');
520         }
521
522         // Free the result
523         SQL_FREERESULT($result);
524         //* DEBUG: */ die('<pre>'.encodeEntities($content).'</pre>');
525
526         // Return content
527         return $content;
528 }
529
530 // Generator (somewhat getter) for network type options
531 function generateNetworkTypeOptions ($networkId) {
532         // Is this an array, then we just came back from edit/delete actions
533         if (is_array($networkId)) {
534                 $networkId = '';
535         } // END - if
536
537         // Is this cached?
538         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
539                 // Generate output and cache it
540                 $GLOBALS[__FUNCTION__][$networkId] = generateOptionList(
541                         'network_types',
542                         'network_type_id',
543                         'network_type_handle',
544                         $networkId,
545                         '',
546                         sprintf(
547                                 "WHERE `network_id`=%s",
548                                 bigintval(getRequestElement('network_id'))
549                         ),
550                         '',
551                         'translateNetworkTypeHandler'
552                 );
553         } // END - if
554
555         // Return content
556         return $GLOBALS[__FUNCTION__][$networkId];
557 }
558
559 // Generates an options list of all available (hard-coded) handlers
560 function generateNetworkTypesAvailableOptions () {
561         // Is it cached?
562         if (!isset($GLOBALS[__FUNCTION__])) {
563                 // Generate list
564                 $GLOBALS[__FUNCTION__] = generateOptionList(
565                         '/ARRAY/',
566                         array(
567                                 'banner',
568                                 'banner_click',
569                                 'banner_view',
570                                 'button',
571                                 'button_click',
572                                 'button_view',
573                                 'surfbar',
574                                 'surfbar_click',
575                                 'surfbar_view',
576                                 'forcedbanner',
577                                 'forcedtextlink',
578                                 'textlink',
579                                 'textlink_click',
580                                 'textlink_view',
581                                 'skybanner',
582                                 'skybanner_click',
583                                 'skybanner_view',
584                                 'layer',
585                                 'layer_click',
586                                 'layer_view',
587                                 'popup',
588                                 'popdown',
589                                 'textmail',
590                                 'htmlmail',
591                                 'lead',
592                                 'sale',
593                                 'payperactive',
594                                 'pagepeel',
595                                 'traffic'
596                         ),
597                         array(),
598                         '',
599                         '', '',
600                         $GLOBALS['network_types_disabled'],
601                         'translateNetworkTypeHandler'
602                 );
603         } // END - if
604
605         // Return content
606         return $GLOBALS[__FUNCTION__];
607 }
608
609 // Generates an options list (somewhat getter) ofr request keys
610 function generateNetworkRequestKeyOptions () {
611         // Is it cached?
612         if (!isset($GLOBALS[__FUNCTION__])) {
613                 // Generate and cache it
614                 $GLOBALS[__FUNCTION__] = generateOptionList(
615                         '/ARRAY/',
616                         array(
617                                 'id',
618                                 'sid',
619                                 'hash',
620                                 'password',
621                                 'reload',
622                                 'maximum_stay',
623                                 'minimum_stay',
624                                 'currency',
625                                 'type',
626                                 'remain',
627                                 'reward',
628                                 'size',
629                                 'erotic',
630                                 'extra',
631                                 'country'
632                         ),
633                         array(),
634                         '',
635                         '', '',
636                         $GLOBALS['network_params_disabled'],
637                         'translateNetworkRequestElement'
638                 );
639         } // END - if
640
641         // Return content
642         return $GLOBALS[__FUNCTION__];
643 }
644
645 // Generator (somewhat getter) for (return) array translation
646 function generateNetworkTranslationOptions ($default = '') {
647         // Is it cached?
648         if (!isset($GLOBALS[__FUNCTION__][$default])) {
649                 // Generate and cache it
650                 $GLOBALS[__FUNCTION__][$default] = generateOptionList(
651                         'network_translations',
652                         'network_translation_id',
653                         'network_translation_name',
654                         $default,
655                         '',
656                         '',
657                         $GLOBALS['network_translation_disabled'],
658                         'translateNetworkTranslationName'
659                 );
660         } // END - if
661
662         // Return content
663         return $GLOBALS[__FUNCTION__][$default];
664 }
665
666 // Generates an option list of request types
667 function generateNetworkRequestTypeOptions ($default = '') {
668         // Do we have cache?
669         if (!isset($GLOBALS[__FUNCTION__][$default])) {
670                 // Generate the list
671                 $GLOBALS[__FUNCTION__][$default] = generateOptionList(
672                         '/ARRAY/',
673                         array(
674                                 'GET',
675                                 'POST'
676                         ),
677                         array(
678                                 '{--ADMIN_NETWORK_REQUEST_TYPE_GET--}',
679                                 '{--ADMIN_NETWORK_REQUEST_TYPE_POST--}'
680                         ),
681                         $default
682                 );
683         } // END - if
684
685         // Return cache
686         return $GLOBALS[__FUNCTION__][$default];
687 }
688
689 // Generates an option list of network_api_active
690 function generateNetworkApiActiveOptions ($default = '') {
691         // Do we have cache?
692         if (!isset($GLOBALS[__FUNCTION__][$default])) {
693                 // Generate the list
694                 $GLOBALS[__FUNCTION__][$default] = generateYesNoOptionList($default);
695         } // END - if
696
697         // Return cache
698         return $GLOBALS[__FUNCTION__][$default];
699 }
700
701 // Translates 'translate_name' for e.g. templates
702 function translateNetworkTranslationName ($name) {
703         // Get the message id
704         return '{--ADMIN_NETWORK_TRANSLATE_' . strtoupper($name) . '_NAME--}';
705 }
706
707 // Translates the network type handler (e.g. banner, paidmail) for templates
708 function translateNetworkTypeHandler ($type) {
709         // Get the message id
710         return '{--ADMIN_NETWORK_TYPES_' . strtoupper($type) . '--}';
711 }
712
713 // Translates request type
714 function translateNetworkRequestType ($type) {
715         // Get the message id
716         return '{--ADMIN_NETWORK_REQUEST_TYPE_' . strtoupper($type) . '--}';
717 }
718
719 // Translates request parameter
720 function translateNetworkRequestElement ($param) {
721         // Get the message id
722         return '{--ADMIN_NETWORK_REQUEST_PARAMETER_' . strtoupper($param) . '--}';
723 }
724
725 // Translates API index
726 function translateNetworkApiIndex ($index) {
727         // Do we have cache?
728         if (!isset($GLOBALS['network_api_index'])) {
729                 // Get an array of all API array indexes
730                 $GLOBALS['network_api_index'] = array();
731
732                 // Get all entries
733                 $result = SQL_QUERY('SELECT
734         `network_api_id`,`network_api_index`,`network_translation_name`
735 FROM
736         `{?_MYSQL_PREFIX?}_network_api_translation`
737 INNER JOIN
738         `{?_MYSQL_PREFIX?}_network_translations`
739 ON
740         `network_api_index`=`network_translation_id`
741 ORDER BY
742         `sort` ASC', __FUNCTION__, __LINE__);
743
744                 // Do we have entries?
745                 if (!SQL_HASZERONUMS($result)) {
746                         // Get all entries
747                         while ($row = SQL_FETCHARRAY($result)) {
748                                 // Add it to our global array
749                                 $GLOBALS['network_api_index'][$row['network_api_index']] = $row;
750                         } // END - while
751                 } // END - if
752
753                 // Free result
754                 SQL_FREERESULT($result);
755         } // END - if
756
757         // Default name is unknown
758         $name = 'unknown';
759
760         // Is the entry there?
761         if (isset($GLOBALS['network_api_index'][$index])) {
762                 // Then get the name
763                 $name = $GLOBALS['network_api_index'][$index]['network_translation_name'];
764         } // END - if
765
766         // Return translation
767         return translateNetworkTranslationName($name);
768 }
769
770 // Translates network API configuration status (see function isNetworkApiConfigured()) by given id
771 function translateNetworkApiConfiguredStatusById ($networkId) {
772         // Do we have cache?
773         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
774                 // By default it is not configured
775                 $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_NOT_CONFIGURED--}';
776
777                 // So is it configured?
778                 if (isNetworkApiConfigured($networkId)) {
779                         // Yes, it is
780                         $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_CONFIGURED--}';
781                 } // END - if
782         } // END - if
783
784         // Return cache
785         return $GLOBALS[__FUNCTION__][$networkId];
786 }
787
788 // Checks if the given network is configured by looking its API configuration entry up
789 function isNetworkApiConfigured ($networkId) {
790         // Do we have cache?
791         if (!isset($GLOBALS[__FUNCTION__][$networkId])) {
792                 // Check for an entry in network_api_config
793                 $GLOBALS[__FUNCTION__][$networkId] = (countSumTotalData(
794                         bigintval($networkId),
795                         'network_api_config',
796                         'network_id',
797                         'network_id',
798                         true
799                 ) == 1);
800         } // END - if
801
802         // Return cache
803         return $GLOBALS[__FUNCTION__][$networkId];
804 }
805
806 // Checks wether the given network type handler is configured
807 function isNetworkTypeHandlerConfigured ($networkId, $networkTypeId) {
808         // Do we have cache?
809         if (!isset($GLOBALS[__FUNCTION__][$networkId][$networkTypeId])) {
810                 // Determine it
811                 $GLOBALS[__FUNCTION__][$networkId][$networkTypeId] = (countSumTotalData(
812                         bigintval($networkTypeId),
813                         'network_types_config',
814                         'network_data_id',
815                         'network_type_id',
816                         true,
817                         sprintf(' AND `network_id`=%s', bigintval($networkId))
818                 ) == 1);
819         } // END - if
820
821         // Return cache
822         return $GLOBALS[__FUNCTION__][$networkId][$networkTypeId];
823 }
824
825 //------------------------------------------------------------------------------
826 //                             Call-back functions
827 //------------------------------------------------------------------------------
828
829 // Callback function to add new network
830 function doAdminNetworkProcessAddNetwork () {
831         // We can say here, the form is sent, so check if the network is already added
832         if (isNetworkNameValid(postRequestElement('network_short_name'))) {
833                 // Already there
834                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_ALREADY_ADDED=' . postRequestElement('network_short_name') . '%}');
835                 return false;
836         } // END - if
837
838         // Remove the 'ok' part
839         unsetPostRequestElement('ok');
840
841         // Add the whole request to database
842         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_data'), __FUNCTION__, __LINE__);
843
844         // Add the id for output only
845         setPostRequestElement('network_id', SQL_INSERTID());
846
847         // Output message
848         if (!SQL_HASZEROAFFECTED()) {
849                 // Successfully added
850                 loadTemplate('admin_network_added', false, postRequestArray());
851         } else {
852                 // Not added
853                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_DATA_NOT_ADDED=' . postRequestElement('network_short_name') . '%}');
854         }
855 }
856
857 // Displays selected networks for editing
858 function doAdminNetworkProcessHandleNetwork () {
859         // Do we have selections?
860         if (ifPostContainsSelections()) {
861                 // Something has been selected, so start displaying one by one
862                 $OUT = '';
863                 foreach (postRequestElement('sel') as $networkId => $sel) {
864                         // Is this selected?
865                         if ($sel == 1) {
866                                 // Load this network's data
867                                 $networkData = getNetworkDataById($networkId);
868
869                                 // Do we have found the network?
870                                 if (count($networkData) > 0) {
871                                         if (isFormSent('edit')) {
872                                                 // Add row template for editing
873                                                 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
874                                         } elseif (isFormSent('delete')) {
875                                                 // Add row template for deleting
876                                                 $OUT .= loadTemplate('admin_delete_networks_row', true, $networkData);
877                                         } else {
878                                                 // Problem!
879                                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
880                                         }
881                                 } // END - if
882                         } // END - if
883                 } // END - foreach
884
885                 // If we have no rows, we don't need to display the edit form
886                 if (!empty($OUT)) {
887                         // Output main template
888                         if (isFormSent('edit')) {
889                                 loadTemplate('admin_edit_networks', false, $OUT);
890                         } elseif (isFormSent('delete')) {
891                                 loadTemplate('admin_delete_networks', false, $OUT);
892                         } else {
893                                 // Problem!
894                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
895                         }
896
897                         // Don't display the list/add new form
898                         $GLOBALS['network_display'] = false;
899                 } else {
900                         // Nothing selected/found
901                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_FOUND--}');
902                 }
903         } // END - if
904 }
905
906 // Handle network type form
907 function doAdminNetworkProcessHandleNetworkType () {
908         // Do we have selections?
909         if (ifPostContainsSelections()) {
910                 // Load network data
911                 $networkData = getNetworkDataById(getRequestElement('network_id'));
912
913                 // Something has been selected, so start displaying one by one
914                 $OUT = '';
915                 foreach (postRequestElement('sel') as $networkId => $sel) {
916                         // Is this selected?
917                         if ($sel == 1) {
918                                 // Load this network's data
919                                 $networkTypeData = getNetworkTypeDataById($networkId);
920
921                                 // Do we have found the network?
922                                 if (count($networkTypeData) > 0) {
923                                         if (isFormSent('edit')) {
924                                                 // Add row template for deleting
925                                                 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
926                                         } elseif (isFormSent('delete')) {
927                                                 // Add row template for deleting
928                                                 $OUT .= loadTemplate('admin_delete_network_types_row', true, $networkTypeData);
929                                         } else {
930                                                 // Problem!
931                                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
932                                         }
933                                 } // END - if
934                         } // END - if
935                 } // END - foreach
936
937                 // If we have no rows, we don't need to display the edit form
938                 if (!empty($OUT)) {
939                         // Output main template
940                         if (isFormSent('edit')) {
941                                 loadTemplate('admin_edit_network_types', false, $OUT);
942                         } elseif (isFormSent('delete')) {
943                                 loadTemplate('admin_delete_network_types', false, $OUT);
944                         } else {
945                                 // Problem!
946                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
947                         }
948
949                         // Don't display the list/add new form
950                         $GLOBALS['network_display'] = false;
951                 } else {
952                         // Nothing selected/found
953                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_TYPES_NOTHING_FOUND--}');
954                 }
955         } // END - if
956 }
957
958 // Handle network request parameter form
959 function doAdminNetworkProcessHandleRequestParams () {
960         // Do we have selections?
961         if (ifPostContainsSelections()) {
962                 // Init cache array
963                 $GLOBALS['network_params_disabled'] = array();
964
965                 // Load network data
966                 $networkData = getNetworkDataById(getRequestElement('network_id'));
967
968                 // Something has been selected, so start displaying one by one
969                 $OUT = '';
970                 foreach (postRequestElement('sel') as $networkId => $sel) {
971                         // Is this selected?
972                         if ($sel == 1) {
973                                 // Load this network's data
974                                 $networkRequestData = getNetworkRequestParamsDataById($networkId);
975
976                                 // Do we have found the network?
977                                 if (count($networkRequestData) > 0) {
978                                         if (isFormSent('edit')) {
979                                                 // Add row template for deleting
980                                                 $OUT .= loadTemplate('admin_edit_network_params_row', true, $networkRequestData);
981                                         } elseif (isFormSent('delete')) {
982                                                 // Get type data
983                                                 $networkRequestData['network_type_data'] = getNetworkTypeDataById($networkRequestData['network_type_id']);
984
985                                                 // Add row template for deleting
986                                                 $OUT .= loadTemplate('admin_delete_network_params_row', true, $networkRequestData);
987                                         } else {
988                                                 // Problem!
989                                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
990                                         }
991                                 } // END - if
992                         } // END - if
993                 } // END - foreach
994
995                 // If we have no rows, we don't need to display the edit form
996                 if (!empty($OUT)) {
997                         // Output main template
998                         if (isFormSent('edit')) {
999                                 loadTemplate('admin_edit_network_params', false, $OUT);
1000                         } elseif (isFormSent('delete')) {
1001                                 loadTemplate('admin_delete_network_params', false, $OUT);
1002                         } else {
1003                                 // Problem!
1004                                 debug_report_bug(__FUNCTION__, __LINE__, 'Cannot detect edit/del.');
1005                         }
1006
1007                         // Don't display the list/add new form
1008                         $GLOBALS['network_display'] = false;
1009                 } else {
1010                         // Nothing selected/found
1011                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_FOUND--}');
1012                 }
1013         } // END - if
1014 }
1015
1016 // Changes given networks
1017 function doAdminNetworkProcessChangeNetwork () {
1018         // Do we have selections?
1019         if (ifPostContainsSelections()) {
1020                 // By default nothing is updated
1021                 $updated = 0;
1022
1023                 // Something has been selected, so start updating them
1024                 foreach (postRequestElement('sel') as $networkId => $sel) {
1025                         // Update this entry?
1026                         if ($sel == 1) {
1027                                 // Init data array
1028                                 $networkData = array();
1029
1030                                 // Transfer whole array, except 'sel'
1031                                 foreach (postRequestArray() as $key => $entry) {
1032                                         // Skip 'sel' and submit button
1033                                         if (in_array($key, array('sel', 'do_edit'))) {
1034                                                 continue;
1035                                         } // END - if
1036
1037                                         // Do we have this enty?
1038                                         if (!isset($entry[$networkId])) {
1039                                                 // Not found, needs fixing
1040                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1041                                         } // END - if
1042
1043                                         // Add this entry
1044                                         $networkData[$key] = $entry[$networkId];
1045                                 } // END - foreach
1046
1047                                 // Update the network data
1048                                 $updated += doNetworkUpdateDataByArray($networkId, $networkData);
1049                         } // END - if
1050                 } // END - foreach
1051
1052                 // Do we have updates?
1053                 if ($updated > 0) {
1054                         // Updates done
1055                         displayMessage('{%message,ADMIN_NETWORK_UPDATED=' . $updated . '%}');
1056                 } else {
1057                         // Nothing changed
1058                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_CHANGED--}');
1059                 }
1060         } // END - if
1061 }
1062
1063 // Removes given networks
1064 function doAdminNetworkProcessRemoveNetwork () {
1065         // Do we have selections?
1066         if (ifPostContainsSelections()) {
1067                 // By default nothing is removed
1068                 $removed = 0;
1069
1070                 // Something has been selected, so start updating them
1071                 foreach (postRequestElement('sel') as $networkId => $sel) {
1072                         // Update this entry?
1073                         if ($sel == 1) {
1074                                 // Remove this entry
1075                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $networkId);
1076                         } // END - if
1077                 } // END - foreach
1078
1079                 // Do we have removes?
1080                 if ($removed > 0) {
1081                         // Removals done
1082                         displayMessage('{%message,ADMIN_NETWORK_REMOVED=' . $removed . '%}');
1083                 } else {
1084                         // Nothing removed
1085                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_NOTHING_REMOVED--}');
1086                 }
1087         } // END - if
1088 }
1089
1090 // Add a network type handler if not yet found
1091 function doAdminNetworkProcessAddNetworkType () {
1092         // Is the network type handle already used with given network?
1093         if (isNetworkTypeHandleValid(postRequestElement('network_type_handle'), getRequestElement('network_id'))) {
1094                 // Already added
1095                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_TYPES_HANDLE_ALREADY_ADDED=' . postRequestElement('network_type_handle') . '%}');
1096
1097                 // ... so abort here
1098                 return false;
1099         } // END - if
1100
1101         // Remove the 'ok' part
1102         unsetPostRequestElement('ok');
1103
1104         // Add id
1105         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1106
1107         // Is network_type_banner_url set?
1108         if (postRequestElement('network_type_banner_url') == '') {
1109                 // Remove empty value to get a NULL for an optional entry
1110                 unsetPostRequestElement('network_type_banner_url');
1111         } // END - if
1112
1113         // Add the whole request to database
1114         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_types'), __FUNCTION__, __LINE__);
1115
1116         // Output message
1117         if (!SQL_HASZEROAFFECTED()) {
1118                 // Successfully added
1119                 loadTemplate('admin_network_type_added', false, postRequestArray());
1120         } else {
1121                 // Not added
1122                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_TYPES_NOT_ADDED=' . postRequestElement('network_type_handle') . '%}');
1123         }
1124 }
1125
1126 // Changes given network type handlers
1127 function doAdminNetworkProcessChangeNetworkType () {
1128         // Do we have selections?
1129         if (ifPostContainsSelections()) {
1130                 // By default nothing is updated
1131                 $updated = 0;
1132
1133                 // Something has been selected, so start updating them
1134                 foreach (postRequestElement('sel') as $networkId => $sel) {
1135                         // Update this entry?
1136                         if ($sel == 1) {
1137                                 // Init data array
1138                                 $networkTypeData = array();
1139
1140                                 // Transfer whole array, except 'sel'
1141                                 foreach (postRequestArray() as $key => $entry) {
1142                                         // Skip 'sel' and submit button
1143                                         if (in_array($key, array('sel', 'do_edit'))) {
1144                                                 continue;
1145                                         } // END - if
1146
1147                                         // Do we have this enty?
1148                                         if (!isset($entry[$networkId])) {
1149                                                 // Not found, needs fixing
1150                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1151                                         } // END - if
1152
1153                                         // Fix empty network_type_banner_url to NULL
1154                                         if (($key == 'network_type_banner_url') && (trim($entry[$networkId]) == '')) {
1155                                                 // Set it to NULL
1156                                                 $entry[$networkId] = NULL;
1157                                         } // END - if
1158
1159                                         // Add this entry
1160                                         $networkTypeData[$key] = $entry[$networkId];
1161                                 } // END - foreach
1162
1163                                 // Update the network data
1164                                 $updated += doNetworkUpdateTypeByArray($networkId, $networkTypeData);
1165                         } // END - if
1166                 } // END - foreach
1167
1168                 // Do we have updates?
1169                 if ($updated > 0) {
1170                         // Updates done
1171                         displayMessage('{%message,ADMIN_NETWORK_TYPES_UPDATED=' . $updated . '%}');
1172                 } else {
1173                         // Nothing changed
1174                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_TYPES_NOTHING_CHANGED--}');
1175                 }
1176         } // END - if
1177 }
1178
1179 // Changes given network request parameters
1180 function doAdminNetworkProcessChangeNetworkParam () {
1181         // Do we have selections?
1182         if (ifPostContainsSelections()) {
1183                 // By default nothing is updated
1184                 $updated = 0;
1185
1186                 // Something has been selected, so start updating them
1187                 foreach (postRequestElement('sel') as $networkId => $sel) {
1188                         // Update this entry?
1189                         if ($sel == 1) {
1190                                 // Init data array
1191                                 $networkParamsData = array();
1192
1193                                 // Transfer whole array, except 'sel'
1194                                 foreach (postRequestArray() as $key => $entry) {
1195                                         // Skip 'sel' and submit button
1196                                         if (in_array($key, array('sel', 'do_edit'))) {
1197                                                 continue;
1198                                         } // END - if
1199
1200                                         // Do we have this enty?
1201                                         if (!isset($entry[$networkId])) {
1202                                                 // Not found, needs fixing
1203                                                 debug_report_bug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.');
1204                                         } // END - if
1205
1206                                         // Fix empty network_request_param_default to NULL
1207                                         if (($key == 'network_request_param_default') && (trim($entry[$networkId]) == '')) {
1208                                                 // Set it to NULL
1209                                                 $entry[$networkId] = NULL;
1210                                         } // END - if
1211
1212                                         // Add this entry
1213                                         $networkParamsData[$key] = $entry[$networkId];
1214                                 } // END - foreach
1215
1216                                 // Update the network data
1217                                 $updated += doNetworkUpdateParamsByArray($networkId, $networkParamsData);
1218                         } // END - if
1219                 } // END - foreach
1220
1221                 // Do we have updates?
1222                 if ($updated > 0) {
1223                         // Updates done
1224                         displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_UPDATED=' . $updated . '%}');
1225                 } else {
1226                         // Nothing changed
1227                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_CHANGED--}');
1228                 }
1229         } // END - if
1230 }
1231
1232 // Removes given network type handlers
1233 function doAdminNetworkProcessRemoveNetworkType () {
1234         // Do we have selections?
1235         if (ifPostContainsSelections()) {
1236                 // By default nothing is removed
1237                 $removed = 0;
1238
1239                 // Something has been selected, so start updating them
1240                 foreach (postRequestElement('sel') as $networkId => $sel) {
1241                         // Update this entry?
1242                         if ($sel == 1) {
1243                                 // Remove this entry
1244                                 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $networkId);
1245                         } // END - if
1246                 } // END - foreach
1247
1248                 // Do we have removes?
1249                 if ($removed > 0) {
1250                         // Removals done
1251                         displayMessage('{%message,ADMIN_NETWORK_TYPES_REMOVED=' . $removed . '%}');
1252                 } else {
1253                         // Nothing removed
1254                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_TYPES_NOTHING_REMOVED--}');
1255                 }
1256         } // END - if
1257 }
1258
1259 // Removes given network request parameters
1260 function doAdminNetworkProcessRemoveNetworkParam () {
1261         // Do we have selections?
1262         if (ifPostContainsSelections()) {
1263                 // By default nothing is removed
1264                 $removed = 0;
1265
1266                 // Something has been selected, so start updating them
1267                 foreach (postRequestElement('sel') as $networkId => $sel) {
1268                         // Update this entry?
1269                         if ($sel == 1) {
1270                                 // Remove this entry
1271                                 $removed += doAdminRemoveNetworkEntry('request_params', 'network_param_id', $networkId);
1272                         } // END - if
1273                 } // END - foreach
1274
1275                 // Do we have removes?
1276                 if ($removed > 0) {
1277                         // Removals done
1278                         displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_REMOVED=' . $removed . '%}');
1279                 } else {
1280                         // Nothing removed
1281                         loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_REMOVED--}');
1282                 }
1283         } // END - if
1284 }
1285
1286 // Adds a request parameter to given network and type
1287 function doAdminNetworkProcessAddNetworkParam () {
1288         // Is the request parameter already used with given network?
1289         if (isNetworkRequestElementValid(postRequestElement('network_request_param_key'), postRequestElement('network_type_id'), getRequestElement('network_id'))) {
1290                 // Already added
1291                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED=' . postRequestElement('network_request_param_key') . '%}');
1292
1293                 // ... so abort here
1294                 return false;
1295         } // END - if
1296
1297         // Remove the 'ok' part
1298         unsetPostRequestElement('ok');
1299
1300         // Add id
1301         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1302
1303         // Is network_request_param_default set?
1304         if (postRequestElement('network_request_param_default') == '') {
1305                 // Remove empty value to get a NULL for an optional entry
1306                 unsetPostRequestElement('network_request_param_default');
1307         } // END - if
1308
1309         // Add the whole request to database
1310         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_request_params'), __FUNCTION__, __LINE__);
1311
1312         // Output message
1313         if (!SQL_HASZEROAFFECTED()) {
1314                 // Successfully added
1315                 loadTemplate('admin_network_network_request_param_added', false, postRequestArray());
1316         } else {
1317                 // Not added
1318                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED=' . postRequestElement('network_request_param_key') . '%}');
1319         }
1320 }
1321
1322 // Adds a API response array entry
1323 function doAdminNetworkProcessAddNetworkApiTranslation () {
1324         // Is the request parameter already used with given network?
1325         if (isNetworkApiTranslationValid(postRequestElement('network_api_index'), postRequestElement('network_type_id'), getRequestElement('network_id'))) {
1326                 // Already added
1327                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_API_TRANSLATION_ALREADY_ADDED=' . postRequestElement('network_api_index') . '%}');
1328
1329                 // ... so abort here
1330                 return false;
1331         } // END - if
1332
1333         // Remove the 'ok' part
1334         unsetPostRequestElement('ok');
1335
1336         // Add id
1337         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1338
1339         // Add sorting
1340         setPostRequestElement('sort', (countSumTotalData(
1341                 bigintval(postRequestElement('network_id')),
1342                 'network_api_translation',
1343                 'network_api_id',
1344                 'network_id',
1345                 true,
1346                 sprintf(" AND `network_type_id`=%s", bigintval(postRequestElement('network_type_id')))
1347         ) + 1));
1348
1349         // Add the whole request to database
1350         SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_api_translation'), __FUNCTION__, __LINE__);
1351
1352         // Output message
1353         if (!SQL_HASZEROAFFECTED()) {
1354                 // Successfully added
1355                 loadTemplate('admin_network_api_translation_added', false, postRequestArray());
1356         } else {
1357                 // Not added
1358                 loadTemplate('admin_settings_unsaved', false, '{%message,ADMIN_NETWORK_API_TRANSLATION_NOT_ADDED=' . postRequestElement('network_api_index') . '%}');
1359         }
1360 }
1361
1362 // Adds/update network API configuration
1363 function doAdminNetworkProcessNetworkApiConfig () {
1364         // Remove the 'ok' part
1365         unsetPostRequestElement('ok');
1366
1367         // Add id
1368         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1369
1370         // Is network_api_referral_button set?
1371         if (postRequestElement('network_api_referral_button') == '') {
1372                 // Remove empty value to get a NULL for an optional entry
1373                 unsetPostRequestElement('network_api_referral_button');
1374         } // END - if
1375
1376         // Is there already an entry?
1377         if (isNetworkApiConfigured(getRequestElement('network_id'))) {
1378                 // Generate SQL query
1379                 $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_api_config', 'network_id', postRequestElement('network_id'), array('network_id'));
1380         } else {
1381                 // Insert new entry
1382                 $SQL = getInsertSqlFromArray(postRequestArray(), 'network_api_config');
1383         }
1384
1385         // Run the query
1386         SQL_QUERY($SQL, __FUNCTION__, __LINE__);
1387
1388         // Output message
1389         if (!SQL_HASZEROAFFECTED()) {
1390                 // Successfully added
1391                 displayMessage('{--ADMIN_CONFIG_NETWORK_API_SAVED--}');
1392         } else {
1393                 // Not added
1394                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_CONFIG_NETWORK_API_NOT_SAVED--}');
1395         }
1396 }
1397
1398 // Only adds network type configuration if not yet present
1399 function doAdminNetworkProcessAddNetworkTypesConfig () {
1400         // Remove the 'ok' part
1401         unsetPostRequestElement('ok');
1402
1403         // Add both ids
1404         setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
1405         setPostRequestElement('network_type_id', bigintval(getRequestElement('network_type_id')));
1406
1407         /*
1408          * Some parameters are optional, at least one must be given so check a bunch
1409          * of parameters.
1410          */
1411         foreach (array('network_min_waiting_time', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic') as $element) {
1412                 // Is this element empty?
1413                 if (postRequestElement($element) == '') {
1414                         // Then unset it to get a NULL for optional parameter
1415                         unsetPostRequestElement($element);
1416                 } // END - if
1417         } // END - foreach
1418
1419         // Initialize variables
1420         $content = array();
1421         $id = 'network_max_reload_time_ye';
1422         $skip = false;
1423         $postData = postRequestArray();
1424
1425         // Convert "reload time selections"
1426         convertSelectionsToEpocheTime($postData, $content, $id, $skip);
1427
1428         // Set the POST array back
1429         setPostRequestArray($postData);
1430
1431         // Is there already an entry?
1432         if (isNetworkTypeHandlerConfigured(getRequestElement('network_id'), getRequestElement('network_type_id'))) {
1433                 // This network type handler is already configured
1434                 displayMessage('{--ADMIN_NETWORK_TYPE_HANDLER_ALREADY_CONFIGURED--}');
1435                 return;
1436         } // END - if
1437
1438         // Get SQL query for new entry
1439         $SQL = getInsertSqlFromArray(postRequestArray(), 'network_types_config');
1440
1441         // Run the query
1442         SQL_QUERY($SQL, __FUNCTION__, __LINE__);
1443
1444         // Output message
1445         if (!SQL_HASZEROAFFECTED()) {
1446                 // Successfully added
1447                 displayMessage('{--ADMIN_NETWORK_TYPE_HANDLER_CONFIG_SAVED--}');
1448         } else {
1449                 // Not added
1450                 loadTemplate('admin_settings_unsaved', false, '{--ADMIN_NETWORK_TYPE_HANDLER_CONFIG_NOT_SAVED--}');
1451         }
1452 }
1453
1454 // Do expression code for this extension
1455 function doExpressionNetwork ($data) {
1456         // Construct replacer
1457         $replacer = sprintf(
1458                 "{DQUOTE} . %s(%s, '%s') . {DQUOTE}",
1459                 $data['callback'],
1460                 $data['matches'][4][$data['key']],
1461                 $data['extra_func']
1462         );
1463
1464         // Replace %network_id% with the current network id
1465         $replacer = str_replace('%network_id%', getCurrentNetworkId(), $replacer);
1466
1467         // Replace the code
1468         $code = replaceExpressionCode($data, $replacer);
1469
1470         // Return it
1471         return $code;
1472 }
1473
1474 // [EOF]
1475 ?>