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