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