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