Even more rewrites/fixes from EL branch (please report any broken part after you...
[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  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Handle a (maybe) sent form here
46 function doNetworkHandleForm () {
47         // Was the form sent?
48         if ((isFormSent()) || (isFormSent('edit')) || (isFormSent('del')) || (isFormSent('change')) || (isFormSent('remove'))) {
49                 // Do we have a 'do'?
50                 if (isGetRequestParameterSet('do')) {
51                         // Process the request
52                         doAdminNetworkProcessForm();
53                 } else {
54                         // No 'do' found
55                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_DO_404'));
56                 }
57         } // END - if
58 }
59
60 // Processes an admin form
61 function doAdminNetworkProcessForm () {
62         // Form really sent?
63         if ((!isFormSent()) && (!isFormSent('edit')) && (!isFormSent('del')) && (!isFormSent('change')) && (!isFormSent('remove'))) {
64                 // Abort here
65                 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_FORM_NOT_SENT'));
66                 return;
67         } elseif (!isGetRequestParameterSet('do')) {
68                 // No 'do' found
69                 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_DO_404'));
70                 return;
71         }
72
73         // Create function name
74         $functionName = sprintf("doAdminNetworkProcess%sForm", ucfirst(strtolower(getRequestParameter('do'))));
75
76         // Is the function valid?
77         if (!function_exists($functionName)) {
78                 // Invalid function name
79                 debug_report_bug('Invalid do ' . getRequestParameter('do') . ', function ' . $functionName .' does not exist.', false);
80         } // END - if
81
82         // Call-back the method handling our request
83         call_user_func($functionName);
84 }
85
86 // Checks wether the (short) network name is already used (valid)
87 function isNetworkNameValid ($name) {
88         // Query for it
89         $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1",
90                 array($name), __FUNCTION__, __LINE__);
91
92         // Does it exist?
93         $isValid = (SQL_NUMROWS($result) == 1);
94
95         // Free result
96         SQL_FREERESULT($result);
97
98         // Return result
99         return $isValid;
100 }
101
102 // Checks wether the given network type is already used (valid)
103 function isNetworkTypeHandleValid ($type, $networkId) {
104         // Query for it
105         $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handle`='%s' LIMIT 1",
106                 array($networkId, $type), __FUNCTION__, __LINE__);
107
108         // Does it exist?
109         $isValid = (SQL_NUMROWS($result) == 1);
110
111         // Free result
112         SQL_FREERESULT($result);
113
114         // Return result
115         return $isValid;
116 }
117
118 // Checks wether the given network request parameter is already used (valid)
119 function isNetworkRequestParameterValid ($key, $type, $networkId) {
120         // Query for it
121         $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",
122                 array($networkId, $type, $key), __FUNCTION__, __LINE__);
123
124         // Does it exist?
125         $isValid = (SQL_NUMROWS($result) == 1);
126
127         // Free result
128         SQL_FREERESULT($result);
129
130         // Return result
131         return $isValid;
132 }
133
134 // "Getter" for a network's data by provided id number
135 function getNetworkDataById ($id, $column = '') {
136         // Ids lower one are not accepted
137         if ($id < 1) {
138                 // Not good, should be fixed
139                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
140         } // END - if
141
142         // Is it cached?
143         if (!isset($GLOBALS['network_data'][$id])) {
144                 // By default we have no data
145                 $GLOBALS['network_data'][$id] = array();
146
147                 // Query for the network data
148                 $result = SQL_QUERY_ESC("SELECT
149         `network_id`, `network_short_name`, `network_title`, `network_reflink`, `network_data_seperator`, `network_row_seperator`, `network_request_type`, `network_charset`
150 FROM
151         `{?_MYSQL_PREFIX?}_network_data`
152 WHERE
153         `network_id`=%s
154 LIMIT 1",
155                         array(bigintval($id)), __FUNCTION__, __LINE__);
156
157                 // Do we have an entry?
158                 if (SQL_NUMROWS($result) == 1) {
159                         // Then get it
160                         $GLOBALS['network_data'][$id] = SQL_FETCHARRAY($result);
161                 } // END - if
162
163                 // Free result
164                 SQL_FREERESULT($result);
165         } // END - if
166
167         // Return result
168         if (empty($column)) {
169                 // Return array
170                 return $GLOBALS['network_data'][$id];
171         } else {
172                 // Return column
173                 return $GLOBALS['network_data'][$id][$column];
174         }
175 }
176
177 // "Getter" for a network type data by provided id number
178 function getNetworkTypeDataById ($id) {
179         // Ids lower one are not accepted
180         if ($id < 1) {
181                 // Not good, should be fixed
182                 debug_report_bug('Network type id ' . $id . ' is smaller than 1.');
183         } // END - if
184
185         // By default we have no data
186         $GLOBALS['network_type_data'][$id] = array();
187
188         // Query for the network data
189         $result = SQL_QUERY_ESC("SELECT
190         `network_type_id`, `network_id`, `network_type_handle`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url`
191 FROM
192         `{?_MYSQL_PREFIX?}_network_types`
193 WHERE
194         `network_type_id`=%s
195 LIMIT 1",
196                 array(bigintval($id)), __FUNCTION__, __LINE__);
197
198         // Do we have an entry?
199         if (SQL_NUMROWS($result) == 1) {
200                 // Then get it
201                 $GLOBALS['network_type_data'][$id] = SQL_FETCHARRAY($result);
202         } // END - if
203
204         // Free result
205         SQL_FREERESULT($result);
206
207         // Return result
208         return $GLOBALS['network_type_data'][$id];
209 }
210
211 // "Getter" for a network request parameter data by provided id number
212 function getNetworkRequestParamsDataById ($id) {
213         // Ids lower one are not accepted
214         if ($id < 1) {
215                 // Not good, should be fixed
216                 debug_report_bug('Network request parameter id ' . $id . ' is smaller than 1.');
217         } // END - if
218
219         // By default we have no data
220         $networkRequestData = array();
221
222         // Query for the network data
223         $result = SQL_QUERY_ESC("SELECT
224         `network_param_id`, `network_id`, `network_type_id`, `request_param_key`, `request_param_value`, `request_param_default`
225 FROM
226         `{?_MYSQL_PREFIX?}_network_request_params`
227 WHERE
228         `network_param_id`=%s
229 LIMIT 1",
230                 array(bigintval($id)), __FUNCTION__, __LINE__);
231
232         // Do we have an entry?
233         if (SQL_NUMROWS($result) == 1) {
234                 // Then get it
235                 $networkRequestData = SQL_FETCHARRAY($result);
236         } // END - if
237
238         // Free result
239         SQL_FREERESULT($result);
240
241         // Return result
242         return $networkRequestData;
243 }
244
245 // Updates given network (id) with data from array
246 function doNetworkUpdateDataByArray ($id, $networkData) {
247         // Ids lower one are not accepted
248         if ($id < 1) {
249                 // Not good, should be fixed
250                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
251         } // END - if
252
253         // Just call our inner method
254         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false);
255 }
256
257 // Updates given network type handler (id) with data from array
258 function doNetworkUpdateTypeByArray ($id, $networkTypeData) {
259         // Ids lower one are not accepted
260         if ($id < 1) {
261                 // Not good, should be fixed
262                 debug_report_bug('Network type handler id ' . $id . ' is smaller than 1.');
263         } // END - if
264
265         // Just call our inner method
266         return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($id)), array(), false, false);
267 }
268
269 // Updates given network request parameters (id) with data from array
270 function doNetworkUpdateParamsByArray ($id, $networkParamData) {
271         // Ids lower one are not accepted
272         if ($id < 1) {
273                 // Not good, should be fixed
274                 debug_report_bug('Network request parameter id ' . $id . ' is smaller than 1.');
275         } // END - if
276
277         // Just call our inner method
278         return adminSaveSettings($networkParamData, '_network_request_params', sprintf("`network_param_id`=%s", bigintval($id)), array(), false, false);
279 }
280
281 // Removes given network entry
282 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
283         // Remove the entry
284         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
285                 array($table, $column, $id, $limit), __FILE__, __LINE__);
286
287         // Return affected rows
288         return SQL_AFFECTEDROWS();
289 }
290
291 // Generates a list of networks for given script and returns it
292 function generateAdminNetworkList () {
293         // Init output
294         $content = '';
295
296         // Query for all networks
297         $result = SQL_QUERY("SELECT
298         `network_id`, `network_short_name`, `network_title`
299 FROM
300         `{?_MYSQL_PREFIX?}_network_data`
301 ORDER BY
302         `network_short_name` ASC", __FILE__, __LINE__);
303
304         // Do we have entries?
305         if (SQL_NUMROWS($result) > 0) {
306                 // List all entries
307                 $rows = array();
308                 while ($row = SQL_FETCHARRAY($result)) {
309                         // Is this valid, then add it
310                         if ((is_array($row)) && (isset($row['network_id']))) {
311                                 // Add entry
312                                 $rows[$row['network_id']] = $row;
313                         } // END - if
314                 } // END - while
315
316                 // Generate the selection box
317                 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id');
318         } else {
319                 // Nothing selected
320                 $content = loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_ENTRIES_404'));
321         }
322
323         // Free the result
324         SQL_FREERESULT($result);
325
326         // Return the list
327         return $content;
328 }
329
330 // Generator (somewhat getter) for a list of network types for given network id
331 function generateAdminNetworkTypeList ($networkId) {
332         // Init content
333         $content = '';
334
335         // Query all types of this network
336         $result = SQL_QUERY_ESC("SELECT
337         `network_type_id`, `network_type_handle`
338 FROM
339         `{?_MYSQL_PREFIX?}_network_types`
340 WHERE
341         `network_id`=%s
342 ORDER BY
343         `network_type_handle` ASC",
344                 array(
345                         bigintval($networkId)
346                 ), __FILE__, __LINE__);
347
348         // Do we have entries?
349         if (SQL_NUMROWS($result) > 0) {
350                 // List all entries
351                 $rows = array();
352                 while ($row = SQL_FETCHARRAY($result)) {
353                         // Is this valid, then add it
354                         if ((is_array($row)) && (isset($row['network_type_id']))) {
355                                 // Add entry
356                                 $rows[$row['network_type_id']] = $row;
357                         } // END - if
358                 } // END - while
359
360                 // Generate the selection box
361                 $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id');
362         } else {
363                 // Nothing selected
364                 $content = loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_ENTRIES_404'));
365         }
366
367         // Free the result
368         SQL_FREERESULT($result);
369
370         // Return content
371         return $content;
372 }
373
374 // Generator (somewhat getter) for network type options
375 function generateNetworkTypeOptions ($id) {
376         // Is this an array, then we just came back from edit/delete actions
377         if (is_array($id)) $id = '';
378
379         // Is this cached?
380         if (!isset($GLOBALS['network_type_options'][$id])) {
381                 // Generate output and cache it
382                 $GLOBALS['network_type_options'][$id] = generateOptionList(
383                         'network_types',
384                         'network_type_id',
385                         'network_type_handle',
386                         $id,
387                         '',
388                         sprintf(
389                                 "WHERE `network_id`=%s",
390                                 bigintval(getRequestParameter('network'))
391                         )
392                 );
393         } // END - if
394
395         // Return content
396         return $GLOBALS['network_type_options'][$id];
397 }
398
399 // Generator (somewhat getter) for request key options
400 function generateNetworkRequestKeyOptions ($key) {
401         // Is it cached?
402         if (!isset($GLOBALS['network_request_param_key'][$key])) {
403                 // Generate and cache it
404                 $GLOBALS['network_request_param_key'][$key] = generateOptionList(
405                         '/ARRAY/',
406                         array(
407                                 'id',
408                                 'sid',
409                                 'hash',
410                                 'password',
411                                 'reload',
412                                 'maximum_stay',
413                                 'minimum_stay',
414                                 'currency',
415                                 'type',
416                                 'remain',
417                                 'reward',
418                                 'size',
419                                 'erotic',
420                                 'extra'
421                         ),
422                         array(
423                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_ID--}',
424                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_SID--}',
425                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_HASH--}',
426                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_PASSWORD--}',
427                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_RELOAD--}',
428                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_MAXIMUM_STAY--}',
429                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_MINIMUM_STAY--}',
430                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_CURRENCY--}',
431                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_TYPE--}',
432                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_REMAIN--}',
433                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_REWARD--}',
434                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_SIZE--}',
435                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_EROTIC--}',
436                                 '{--ADMIN_NETWORK_REQUEST_PARAMETER_EXTRA--}'
437                         ),
438                         $key,
439                         '', '',
440                         $GLOBALS['network_params_disabled']
441                 );
442         } // END - if
443
444         // Return content
445         return $GLOBALS['network_request_param_key'][$key];
446 }
447
448 // Generator (somewhat getter) for (return) array translation
449 function generateNetworkArrayTranslationOptions ($default) {
450         // Is it cached?
451         if (!isset($GLOBALS['network_array_translation'][$default])) {
452                 // Generate and cache it
453                 $GLOBALS['network_array_translation'][$default] = generateOptionList(
454                         'network_request_params',
455                         'network_param_id',
456                         'request_param_key',
457                         $default,
458                         '',
459                         sprintf("WHERE `network_id`='%s'", bigintval(getRequestParameter('network'))),
460                         $GLOBALS['network_array_translation_disabled'],
461                         'ADMIN_NETWORK_REQUEST_PARAMETER_'
462                 );
463         } // END - if
464
465         // Return content
466         return $GLOBALS['network_array_translation'][$default];
467 }
468
469 //------------------------------------------------------------------------------
470 //                             Call-back functions
471 //------------------------------------------------------------------------------
472
473 // Callback function to add new network
474 function doAdminNetworkProcessAddnetworkForm () {
475         // We can say here, the form is sent, so check if the network is already added
476         if (isNetworkNameValid(postRequestParameter('network_short_name'))) {
477                 // Already there
478                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestParameter('network_short_name')));
479                 return false;
480         } // END - if
481
482         // Remove the 'ok' part
483         unsetPostRequestParameter('ok');
484
485         // Add the whole request to database
486         SQL_QUERY("INSERT INTO
487         `{?_MYSQL_PREFIX?}_network_data`
488 (
489         `" . implode('`,`', array_keys(postRequestArray())) . "`
490 ) VALUES (
491         '" . implode("','", array_values(postRequestArray())) . "'
492 )", __FUNCTION__, __LINE__);
493
494         // Add the id for output only
495         setPostRequestParameter('network_id', SQL_INSERTID());
496
497         // Output message
498         if (SQL_AFFECTEDROWS() == 1) {
499                 // Successfully added
500                 loadTemplate('admin_network_added', false, postRequestArray());
501         } else {
502                 // Not added
503                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestParameter('network_short_name')));
504         }
505 }
506
507 // Displays selected networks for editing
508 function doAdminNetworkProcessHandlenetworkForm () {
509         // Do we have selections?
510         if (countPostSelection() > 0) {
511                 // Something has been selected, so start displaying one by one
512                 $SW = 2; $OUT = '';
513                 foreach (postRequestParameter('sel') as $id => $sel) {
514                         // Is this selected?
515                         if ($sel == 1) {
516                                 // Load this network's data
517                                 $networkData = getNetworkDataById($id);
518
519                                 // Do we have found the network?
520                                 if (count($networkData) > 0) {
521                                         // Add color
522                                         $networkData['sw'] = $SW;
523
524                                         if (isFormSent('edit')) {
525                                                 // Make selection box for network_request_type
526                                                 $networkData['network_request_type'] = generateOptionList(
527                                                         '/ARRAY/',
528                                                         array(
529                                                                 'GET',
530                                                                 'POST'
531                                                         ),
532                                                         array(
533                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
534                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
535                                                         ),
536                                                         $networkData['network_request_type']
537                                                 );
538
539                                                 // Add row template for editing
540                                                 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
541                                         } elseif (isFormSent('del')) {
542                                                 // Translate the request type
543                                                 $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
544
545                                                 // Add row template for deleting
546                                                 $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
547                                         } else {
548                                                 // Problem!
549                                                 debug_report_bug('Cannot detect edit/del.');
550                                         }
551
552                                         // Switch colors
553                                         $SW = 3 - $SW;
554                                 } // END - if
555                         } // END - if
556                 } // END - foreach
557
558                 // If we have no rows, we don't need to display the edit form
559                 if (!empty($OUT)) {
560                         // Output main template
561                         if (isFormSent('edit')) {
562                                 loadTemplate('admin_edit_networks', false, $OUT);
563                         } elseif (isFormSent('del')) {
564                                 loadTemplate('admin_del_networks', false, $OUT);
565                         } else {
566                                 // Problem!
567                                 debug_report_bug('Cannot detect edit/del.');
568                         }
569
570                         // Don't display the list/add new form
571                         $GLOBALS['network_display'] = false;
572                 } else {
573                         // Nothing selected/found
574                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND'));
575                 }
576         } // END - if
577 }
578
579 // Handle network type form
580 function doAdminNetworkProcessHandlenetworktypeForm () {
581         // Do we have selections?
582         if (countPostSelection() > 0) {
583                 // Load network data
584                 $networkData = getNetworkDataById(getRequestParameter('network'));
585
586                 // Something has been selected, so start displaying one by one
587                 $SW = 2; $OUT = '';
588                 foreach (postRequestParameter('sel') as $id => $sel) {
589                         // Is this selected?
590                         if ($sel == 1) {
591                                 // Load this network's data
592                                 $networkTypeData = getNetworkTypeDataById($id);
593
594                                 // Do we have found the network?
595                                 if (count($networkTypeData) > 0) {
596                                         // Add color
597                                         $networkTypeData['sw'] = $SW;
598
599                                         if (isFormSent('edit')) {
600                                                 // Add row template for deleting
601                                                 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
602                                         } elseif (isFormSent('del')) {
603                                                 // Fix empty banner URL
604                                                 if (trim($networkTypeData['network_type_banner_url']) == '') $networkTypeData['network_type_banner_url'] = '---';
605
606                                                 // Add row template for deleting
607                                                 $OUT .= loadTemplate('admin_del_network_types_row', true, $networkTypeData);
608                                         } else {
609                                                 // Problem!
610                                                 debug_report_bug('Cannot detect edit/del.');
611                                         }
612
613                                         // Switch colors
614                                         $SW = 3 - $SW;
615                                 } // END - if
616                         } // END - if
617                 } // END - foreach
618
619                 // If we have no rows, we don't need to display the edit form
620                 if (!empty($OUT)) {
621                         // Prepare content for template
622                         $content = array(
623                                 'rows'         => $OUT,
624                                 'network_data' => getNetworkDataById(getRequestParameter('network'))
625                         );
626
627                         // Output main template
628                         if (isFormSent('edit')) {
629                                 loadTemplate('admin_edit_network_types', false, $content);
630                         } elseif (isFormSent('del')) {
631                                 loadTemplate('admin_del_network_types', false, $content);
632                         } else {
633                                 // Problem!
634                                 debug_report_bug('Cannot detect edit/del.');
635                         }
636
637                         // Don't display the list/add new form
638                         $GLOBALS['network_display'] = false;
639                 } else {
640                         // Nothing selected/found
641                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_FOUND'));
642                 }
643         } // END - if
644 }
645
646 // Handle network request parameter form
647 function doAdminNetworkProcessHandlerequestparamsForm () {
648         // Do we have selections?
649         if (countPostSelection() > 0) {
650                 // Init cache array
651                 $GLOBALS['network_params_disabled'] = array();
652
653                 // Load network data
654                 $networkData = getNetworkDataById(getRequestParameter('network'));
655
656                 // Something has been selected, so start displaying one by one
657                 $SW = 2; $OUT = '';
658                 foreach (postRequestParameter('sel') as $id => $sel) {
659                         // Is this selected?
660                         if ($sel == 1) {
661                                 // Load this network's data
662                                 $networkRequestData = getNetworkRequestParamsDataById($id);
663
664                                 // Do we have found the network?
665                                 if (count($networkRequestData) > 0) {
666                                         // Add color
667                                         $networkRequestData['sw'] = $SW;
668
669                                         if (isFormSent('edit')) {
670                                                 // Add options list for network type
671                                                 $networkRequestData['type_options'] = generateNetworkTypeOptions($networkRequestData['network_type_id']);
672
673                                                 // Add options list for request key
674                                                 $networkRequestData['key_options'] = generateNetworkRequestKeyOptions($networkRequestData['request_param_key']);
675
676                                                 // Add row template for deleting
677                                                 $OUT .= loadTemplate('admin_edit_network_params_row', true, $networkRequestData);
678                                         } elseif (isFormSent('del')) {
679                                                 // Fix empty banner URL
680                                                 if (trim($networkRequestData['request_param_default']) == '') $networkRequestData['request_param_default'] = '---';
681
682                                                 // Get type data
683                                                 $networkRequestData['network_type_data'] = getNetworkTypeDataById($networkRequestData['network_type_id']);
684
685                                                 // Add row template for deleting
686                                                 $OUT .= loadTemplate('admin_del_network_params_row', true, $networkRequestData);
687                                         } else {
688                                                 // Problem!
689                                                 debug_report_bug('Cannot detect edit/del.');
690                                         }
691
692                                         // Switch colors
693                                         $SW = 3 - $SW;
694                                 } // END - if
695                         } // END - if
696                 } // END - foreach
697
698                 // If we have no rows, we don't need to display the edit form
699                 if (!empty($OUT)) {
700                         // Prepare content for template
701                         $content = array(
702                                 'rows'         => $OUT,
703                                 'network_data' => getNetworkDataById(getRequestParameter('network'))
704                         );
705
706                         // Output main template
707                         if (isFormSent('edit')) {
708                                 loadTemplate('admin_edit_network_params', false, $content);
709                         } elseif (isFormSent('del')) {
710                                 loadTemplate('admin_del_network_params', false, $content);
711                         } else {
712                                 // Problem!
713                                 debug_report_bug('Cannot detect edit/del.');
714                         }
715
716                         // Don't display the list/add new form
717                         $GLOBALS['network_display'] = false;
718                 } else {
719                         // Nothing selected/found
720                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_FOUND'));
721                 }
722         } // END - if
723 }
724
725 // Changes given networks
726 function doAdminNetworkProcessChangenetworkForm () {
727         // Do we have selections?
728         if (countPostSelection() > 0) {
729                 // By default nothing is updated
730                 $updated = 0;
731
732                 // Something has been selected, so start updating them
733                 foreach (postRequestParameter('sel') as $id => $sel) {
734                         // Update this entry?
735                         if ($sel == 1) {
736                                 // Init data array
737                                 $networkData = array();
738
739                                 // Transfer whole array, except 'sel'
740                                 foreach (postRequestArray() as $key => $entry) {
741                                         // Skip 'sel' and submit button
742                                         if (in_array($key, array('sel', 'change'))) continue;
743
744                                         // Do we have this enty?
745                                         if (!isset($entry[$id])) {
746                                                 // Not found, needs fixing
747                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
748                                         } // END - if
749
750                                         // Add this entry
751                                         $networkData[$key] = $entry[$id];
752                                 } // END - foreach
753
754                                 // Update the network data
755                                 $updated += doNetworkUpdateDataByArray($id, $networkData);
756                         } // END - if
757                 } // END - foreach
758
759                 // Do we have updates?
760                 if ($updated > 0) {
761                         // Updates done
762                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
763                 } else {
764                         // Nothing changed
765                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
766                 }
767         } // END - if
768 }
769
770 // Removes given networks
771 function doAdminNetworkProcessRemovenetworkForm () {
772         // Do we have selections?
773         if (countPostSelection() > 0) {
774                 // By default nothing is removed
775                 $removed = 0;
776
777                 // Something has been selected, so start updating them
778                 foreach (postRequestParameter('sel') as $id => $sel) {
779                         // Update this entry?
780                         if ($sel == 1) {
781                                 // Remove this entry
782                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
783                         } // END - if
784                 } // END - foreach
785
786                 // Do we have removes?
787                 if ($removed > 0) {
788                         // Removals done
789                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
790                 } else {
791                         // Nothing removed
792                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
793                 }
794         } // END - if
795 }
796
797 // Add a network type handler if not yet found
798 function doAdminNetworkProcessAddnetworktypeForm () {
799         // Is the network type handle already used with given network?
800         if (isNetworkTypeHandleValid(postRequestParameter('network_type_handle'), getRequestParameter('network'))) {
801                 // Already added
802                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_HANDLE_ALREADY_ADDED', postRequestParameter('network_type_handle')));
803
804                 // ... so abort here
805                 return false;
806         } // END - if
807
808         // Remove the 'ok' part
809         unsetPostRequestParameter('ok');
810
811         // Add id
812         setPostRequestParameter('network_id', bigintval(getRequestParameter('network')));
813
814         // Is network_type_banner_url set?
815         if (postRequestParameter('network_type_banner_url') == '') {
816                 // Remove empty value to get a NULL for an optional entry
817                 unsetPostRequestParameter('network_type_banner_url');
818         } // END - if
819
820         // Add the whole request to database
821         SQL_QUERY("INSERT INTO
822         `{?_MYSQL_PREFIX?}_network_types`
823 (
824         `" . implode('`,`', array_keys(postRequestArray())) . "`
825 ) VALUES (
826         '" . implode("','", array_values(postRequestArray())) . "'
827 )", __FUNCTION__, __LINE__);
828
829         // Output message
830         if (SQL_AFFECTEDROWS() == 1) {
831                 // Successfully added
832                 loadTemplate('admin_network_type_added', false, postRequestArray());
833         } else {
834                 // Not added
835                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_NOT_ADDED', postRequestParameter('network_type_handle')));
836         }
837 }
838
839 // Changes given network type handlers
840 function doAdminNetworkProcessChangenetworktypeForm () {
841         // Do we have selections?
842         if (countPostSelection() > 0) {
843                 // By default nothing is updated
844                 $updated = 0;
845
846                 // Something has been selected, so start updating them
847                 foreach (postRequestParameter('sel') as $id => $sel) {
848                         // Update this entry?
849                         if ($sel == 1) {
850                                 // Init data array
851                                 $networkTypeData = array();
852
853                                 // Transfer whole array, except 'sel'
854                                 foreach (postRequestArray() as $key => $entry) {
855                                         // Skip 'sel' and submit button
856                                         if (in_array($key, array('sel', 'change'))) continue;
857
858                                         // Do we have this enty?
859                                         if (!isset($entry[$id])) {
860                                                 // Not found, needs fixing
861                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
862                                         } // END - if
863
864                                         // Fix empty network_type_banner_url to NULL
865                                         if (($key == 'network_type_banner_url') && (trim($entry[$id]) == '')) {
866                                                 // Set it to NULL
867                                                 $entry[$id] = null;
868                                         } // END - if
869
870                                         // Add this entry
871                                         $networkTypeData[$key] = $entry[$id];
872                                 } // END - foreach
873
874                                 // Update the network data
875                                 $updated += doNetworkUpdateTypeByArray($id, $networkTypeData);
876                         } // END - if
877                 } // END - foreach
878
879                 // Do we have updates?
880                 if ($updated > 0) {
881                         // Updates done
882                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_UPDATED', $updated));
883                 } else {
884                         // Nothing changed
885                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_CHANGED'));
886                 }
887         } // END - if
888 }
889
890 // Changes given network request parameters
891 function doAdminNetworkProcessChangenetworkparamForm () {
892         // Do we have selections?
893         if (countPostSelection() > 0) {
894                 // By default nothing is updated
895                 $updated = 0;
896
897                 // Something has been selected, so start updating them
898                 foreach (postRequestParameter('sel') as $id => $sel) {
899                         // Update this entry?
900                         if ($sel == 1) {
901                                 // Init data array
902                                 $networkParamsData = array();
903
904                                 // Transfer whole array, except 'sel'
905                                 foreach (postRequestArray() as $key => $entry) {
906                                         // Skip 'sel' and submit button
907                                         if (in_array($key, array('sel', 'change'))) continue;
908
909                                         // Do we have this enty?
910                                         if (!isset($entry[$id])) {
911                                                 // Not found, needs fixing
912                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
913                                         } // END - if
914
915                                         // Fix empty request_param_default to NULL
916                                         if (($key == 'request_param_default') && (trim($entry[$id]) == '')) {
917                                                 // Set it to NULL
918                                                 $entry[$id] = null;
919                                         } // END - if
920
921                                         // Add this entry
922                                         $networkParamsData[$key] = $entry[$id];
923                                 } // END - foreach
924
925                                 // Update the network data
926                                 $updated += doNetworkUpdateParamsByArray($id, $networkParamsData);
927                         } // END - if
928                 } // END - foreach
929
930                 // Do we have updates?
931                 if ($updated > 0) {
932                         // Updates done
933                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_UPDATED', $updated));
934                 } else {
935                         // Nothing changed
936                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_CHANGED'));
937                 }
938         } // END - if
939 }
940
941 // Removes given network type handlers
942 function doAdminNetworkProcessRemovenetworktypeForm () {
943         // Do we have selections?
944         if (countPostSelection() > 0) {
945                 // By default nothing is removed
946                 $removed = 0;
947
948                 // Something has been selected, so start updating them
949                 foreach (postRequestParameter('sel') as $id => $sel) {
950                         // Update this entry?
951                         if ($sel == 1) {
952                                 // Remove this entry
953                                 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $id);
954                         } // END - if
955                 } // END - foreach
956
957                 // Do we have removes?
958                 if ($removed > 0) {
959                         // Removals done
960                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_REMOVED', $removed));
961                 } else {
962                         // Nothing removed
963                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_REMOVED'));
964                 }
965         } // END - if
966 }
967
968 // Removes given network request parameters
969 function doAdminNetworkProcessRemovenetworkparamForm () {
970         // Do we have selections?
971         if (countPostSelection() > 0) {
972                 // By default nothing is removed
973                 $removed = 0;
974
975                 // Something has been selected, so start updating them
976                 foreach (postRequestParameter('sel') as $id => $sel) {
977                         // Update this entry?
978                         if ($sel == 1) {
979                                 // Remove this entry
980                                 $removed += doAdminRemoveNetworkEntry('request_params', 'network_param_id', $id);
981                         } // END - if
982                 } // END - foreach
983
984                 // Do we have removes?
985                 if ($removed > 0) {
986                         // Removals done
987                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_REMOVED', $removed));
988                 } else {
989                         // Nothing removed
990                         loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_REMOVED'));
991                 }
992         } // END - if
993 }
994
995 // Adds a request parameter to given network and type
996 function doAdminNetworkProcessAddnetworkparamForm () {
997         // Is the request parameter already used with given network?
998         if (isNetworkRequestParameterValid(postRequestParameter('request_param_key'), postRequestParameter('network_type_id'), getRequestParameter('network'))) {
999                 // Already added
1000                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED', postRequestParameter('request_param_key')));
1001
1002                 // ... so abort here
1003                 return false;
1004         } // END - if
1005
1006         // Remove the 'ok' part
1007         unsetPostRequestParameter('ok');
1008
1009         // Add id
1010         setPostRequestParameter('network_id', bigintval(getRequestParameter('network')));
1011
1012         // Is request_param_default set?
1013         if (postRequestParameter('request_param_default') == '') {
1014                 // Remove empty value to get a NULL for an optional entry
1015                 unsetPostRequestParameter('request_param_default');
1016         } // END - if
1017
1018         // Add the whole request to database
1019         SQL_QUERY("INSERT INTO
1020         `{?_MYSQL_PREFIX?}_network_request_params`
1021 (
1022         `" . implode('`,`', array_keys(postRequestArray())) . "`
1023 ) VALUES (
1024         '" . implode("','", array_values(postRequestArray())) . "'
1025 )", __FUNCTION__, __LINE__);
1026
1027         // Output message
1028         if (SQL_AFFECTEDROWS() == 1) {
1029                 // Successfully added
1030                 loadTemplate('admin_network_request_param_added', false, postRequestArray());
1031         } else {
1032                 // Not added
1033                 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED', postRequestParameter('request_param_key')));
1034         }
1035 }
1036
1037 // Do expression code for this extension
1038 function doExpressionNetwork ($data) {
1039         // Construct replacer
1040         $replacer = sprintf(
1041                 "\" . %s(%s, '%s') . \"",
1042                 $data['callback'],
1043                 $data['matches'][4][$data['key']],
1044                 $data['extra_func']
1045         );
1046
1047         // Replace the code
1048         $code = replaceExpressionCode($data, $replacer);
1049
1050         // Return it
1051         return $code;
1052 }
1053
1054 // [EOF]
1055 ?>