Adding of request parameters partialy 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  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // Handle a (maybe) sent form here
45 function doNetworkHandleForm () {
46         // Was the form sent?
47         if ((isFormSent()) || (isPostRequestElementSet('edit')) || (isPostRequestElementSet('del')) || (isPostRequestElementSet('change')) || (isPostRequestElementSet('remove'))) {
48                 // Do we have a 'do'?
49                 if (isGetRequestElementSet('do')) {
50                         // Process the request
51                         doAdminNetworkProcessForm();
52                 } else {
53                         // No 'do' found
54                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_DO_404'));
55                 }
56         } // END - if
57 }
58
59 // Processes an admin form
60 function doAdminNetworkProcessForm () {
61         // Form really sent?
62         if ((!isFormSent()) && (!isPostRequestElementSet('edit')) && (!isPostRequestElementSet('del')) && (!isPostRequestElementSet('change')) && (!isPostRequestElementSet('remove'))) {
63                 // Abort here
64                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_FORM_NOT_SENT'));
65                 return;
66         } elseif (!isGetRequestElementSet('do')) {
67                 // No 'do' found
68                 loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_DO_404'));
69                 return;
70         }
71
72         // Create function name
73         $functionName = sprintf("doAdminNetworkProcess%sForm", ucfirst(strtolower(getRequestElement('do'))));
74
75         // Is the function valid?
76         if (!function_exists($functionName)) {
77                 // Invalid function name
78                 debug_report_bug('Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', false);
79         } // END - if
80
81         // Call-back the method handling our request
82         call_user_func($functionName);
83 }
84
85 // Checks wether the (short) network name is already used (valid)
86 function isNetworkNameValid ($name) {
87         // Query for it
88         $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1",
89                 array($name), __FUNCTION__, __LINE__);
90
91         // Does it exist?
92         $isValid = (SQL_NUMROWS($result) == 1);
93
94         // Free result
95         SQL_FREERESULT($result);
96
97         // Return result
98         return $isValid;
99 }
100
101 // Checks wether the given network type is already used (valid)
102 function isNetworkTypeHandleValid ($type, $networkId) {
103         // Query for it
104         $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handle`='%s' LIMIT 1",
105                 array($networkId, $type), __FUNCTION__, __LINE__);
106
107         // Does it exist?
108         $isValid = (SQL_NUMROWS($result) == 1);
109
110         // Free result
111         SQL_FREERESULT($result);
112
113         // Return result
114         return $isValid;
115 }
116
117 // "Getter" for a network's data by provided id number
118 function getNetworkDataById ($id) {
119         // Ids lower one are not accepted
120         if ($id < 1) {
121                 // Not good, should be fixed
122                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
123         } // END - if
124
125         // By default we have no data
126         $networkData = array();
127
128         // Query for the network data
129         $result = SQL_QUERY_ESC("SELECT
130         `network_id`, `network_short_name`, `network_title`, `network_reflink`, `network_data_seperator`, `network_row_seperator`, `network_request_type`, `network_charset`
131 FROM
132         `{?_MYSQL_PREFIX?}_network_data`
133 WHERE
134         `network_id`=%s
135 LIMIT 1",
136                 array(bigintval($id)), __FUNCTION__, __LINE__);
137
138         // Do we have an entry?
139         if (SQL_NUMROWS($result) == 1) {
140                 // Then get it
141                 $networkData = SQL_FETCHARRAY($result);
142         } // END - if
143
144         // Free result
145         SQL_FREERESULT($result);
146
147         // Return result
148         return $networkData;
149 }
150
151 // "Getter" for a network type data by provided id number
152 function getNetworkTypeDataById ($id) {
153         // Ids lower one are not accepted
154         if ($id < 1) {
155                 // Not good, should be fixed
156                 debug_report_bug('Network type id ' . $id . ' is smaller than 1.');
157         } // END - if
158
159         // By default we have no data
160         $networkTypeData = array();
161
162         // Query for the network data
163         $result = SQL_QUERY_ESC("SELECT
164         `network_type_id`, `network_id`, `network_type_handle`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url`
165 FROM
166         `{?_MYSQL_PREFIX?}_network_types`
167 WHERE
168         `network_type_id`=%s
169 LIMIT 1",
170                 array(bigintval($id)), __FUNCTION__, __LINE__);
171
172         // Do we have an entry?
173         if (SQL_NUMROWS($result) == 1) {
174                 // Then get it
175                 $networkTypeData = SQL_FETCHARRAY($result);
176         } // END - if
177
178         // Free result
179         SQL_FREERESULT($result);
180
181         // Return result
182         return $networkTypeData;
183 }
184
185 // Updates given network (id) with data from array
186 function doNetworkUpdateDataByArray ($id, $networkData) {
187         // Ids lower one are not accepted
188         if ($id < 1) {
189                 // Not good, should be fixed
190                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
191         } // END - if
192
193         // Just call our inner method
194         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false);
195 }
196
197 // Updates given network type handler (id) with data from array
198 function doNetworkUpdateTypeByArray ($id, $networkTypeData) {
199         // Ids lower one are not accepted
200         if ($id < 1) {
201                 // Not good, should be fixed
202                 debug_report_bug('Network type handler id ' . $id . ' is smaller than 1.');
203         } // END - if
204
205         // Just call our inner method
206         return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($id)), array(), false, false);
207 }
208
209 // Removes given network entry
210 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
211         // Remove the entry
212         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
213                 array($table, $column, $id, $limit), __FILE__, __LINE__);
214
215         // Return affected rows
216         return SQL_AFFECTEDROWS();
217 }
218
219 // Generates a list of networks for given script and returns it
220 function generateAdminNetworkList () {
221         // Init output
222         $content = '';
223
224         // Query for all networks
225         $result = SQL_QUERY("SELECT
226         `network_id`, `network_title`
227 FROM
228         `{?_MYSQL_PREFIX?}_network_data`
229 ORDER BY
230         `network_title` ASC", __FILE__, __LINE__);
231
232         // Do we have entries?
233         if (SQL_NUMROWS($result) > 0) {
234                 // List all entries
235                 $rows = array();
236                 while ($row = SQL_FETCHARRAY($result)) {
237                         // Is this valid, then add it
238                         if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row;
239                 } // END - while
240
241                 // Generate the selection box
242                 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id', 'network_title');
243         } else {
244                 // Nothing selected
245                 $content = loadTemplate('admin_settings_saved', false, getMessage('ADMIN_ENTRIES_404'));
246         }
247
248         // Free the result
249         SQL_FREERESULT($result);
250
251         // Return the list
252         return $content;
253 }
254
255 //------------------------------------------------------------------------------
256 //                             Call-back functions
257 //------------------------------------------------------------------------------
258
259 // Callback function to add new network
260 function doAdminNetworkProcessAddnetworkForm () {
261         // We can say here, the form is sent, so check if the network is already added
262         if (isNetworkNameValid(postRequestElement('network_short_name'))) {
263                 // Already there
264                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestElement('network_short_name')));
265                 return false;
266         } // END - if
267
268         // Remove the 'ok' part
269         unsetPostRequestElement('ok');
270
271         // Add the whole request to database
272         SQL_QUERY("INSERT INTO
273         `{?_MYSQL_PREFIX?}_network_data`
274 (
275         `" . implode('`,`', array_keys(postRequestArray())) . "`
276 ) VALUES (
277         '" . implode("','", array_values(postRequestArray())) . "'
278 )", __FUNCTION__, __LINE__);
279
280         // Add the id for output only
281         setPostRequestElement('network_id', SQL_INSERTID());
282
283         // Output message
284         if (SQL_AFFECTEDROWS() == 1) {
285                 // Successfully added
286                 loadTemplate('admin_network_added', false, postRequestArray());
287         } else {
288                 // Not added
289                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestElement('network_short_name')));
290         }
291 }
292
293 // Displays selected networks for editing
294 function doAdminNetworkProcessHandlenetworkForm () {
295         // Do we have selections?
296         if (countPostSelection() > 0) {
297                 // Something has been selected, so start displaying one by one
298                 $SW = 2; $OUT = '';
299                 foreach (postRequestElement('sel') as $id => $sel) {
300                         // Is this selected?
301                         if ($sel == 1) {
302                                 // Load this network's data
303                                 $networkData = getNetworkDataById($id);
304
305                                 // Do we have found the network?
306                                 if (count($networkData) > 0) {
307                                         // Add color
308                                         $networkData['sw'] = $SW;
309
310                                         if (isPostRequestElementSet('edit')) {
311                                                 // Make selection box for network_request_type
312                                                 $networkData['network_request_type'] = generateOptionList(
313                                                         '/ARRAY/',
314                                                         array('GET','POST'),
315                                                         array(
316                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
317                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
318                                                         ),
319                                                         $networkData['network_request_type']
320                                                 );
321
322                                                 // Add row template for editing
323                                                 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
324                                         } elseif (isPostRequestElementSet('del')) {
325                                                 // Translate the request type
326                                                 $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
327
328                                                 // Add row template for deleting
329                                                 $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
330                                         } else {
331                                                 // Problem!
332                                                 debug_report_bug('Cannot detect edit/del.');
333                                         }
334
335                                         // Switch colors
336                                         $SW = 3 - $SW;
337                                 } // END - if
338                         } // END - if
339                 } // END - foreach
340
341                 // If we have no rows, we don't need to display the edit form
342                 if (!empty($OUT)) {
343                         // Output main template
344                         if (isPostRequestElementSet('edit')) {
345                                 loadTemplate('admin_edit_networks', false, $OUT);
346                         } elseif (isPostRequestElementSet('del')) {
347                                 loadTemplate('admin_del_networks', false, $OUT);
348                         } else {
349                                 // Problem!
350                                 debug_report_bug('Cannot detect edit/del.');
351                         }
352
353                         // Don't display the list/add new form
354                         $GLOBALS['network_display'] = false;
355                 } else {
356                         // Nothing selected/found
357                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND'));
358                 }
359         } // END - if
360 }
361
362 // Handle network type form
363 function doAdminNetworkProcessHandlenetworktypeForm () {
364         // Do we have selections?
365         if (countPostSelection() > 0) {
366                 // Load network data
367                 $networkData = getNetworkDataById(getRequestElement('network'));
368
369                 // Something has been selected, so start displaying one by one
370                 $SW = 2; $OUT = '';
371                 foreach (postRequestElement('sel') as $id => $sel) {
372                         // Is this selected?
373                         if ($sel == 1) {
374                                 // Load this network's data
375                                 $networkTypeData = getNetworkTypeDataById($id);
376
377                                 // Do we have found the network?
378                                 if (count($networkTypeData) > 0) {
379                                         // Add color
380                                         $networkTypeData['sw'] = $SW;
381
382                                         if (isPostRequestElementSet('edit')) {
383                                                 // Add row template for deleting
384                                                 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
385                                         } elseif (isPostRequestElementSet('del')) {
386                                                 // Fix empty banner URL
387                                                 if (trim($networkTypeData['network_type_banner_url']) == '') $networkTypeData['network_type_banner_url'] = '---';
388
389                                                 // Add row template for deleting
390                                                 $OUT .= loadTemplate('admin_del_network_types_row', true, $networkTypeData);
391                                         } else {
392                                                 // Problem!
393                                                 debug_report_bug('Cannot detect edit/del.');
394                                         }
395
396                                         // Switch colors
397                                         $SW = 3 - $SW;
398                                 } // END - if
399                         } // END - if
400                 } // END - foreach
401
402                 // If we have no rows, we don't need to display the edit form
403                 if (!empty($OUT)) {
404                         // Prepare content for template
405                         $content = array(
406                                 'rows' => $OUT,
407                                 'network_data' => getNetworkDataById(getRequestElement('network'))
408                         );
409
410                         // Output main template
411                         if (isPostRequestElementSet('edit')) {
412                                 loadTemplate('admin_edit_network_types', false, $content);
413                         } elseif (isPostRequestElementSet('del')) {
414                                 loadTemplate('admin_del_network_types', false, $content);
415                         } else {
416                                 // Problem!
417                                 debug_report_bug('Cannot detect edit/del.');
418                         }
419
420                         // Don't display the list/add new form
421                         $GLOBALS['network_display'] = false;
422                 } else {
423                         // Nothing selected/found
424                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_FOUND'));
425                 }
426         } // END - if
427 }
428
429 // Changes given networks
430 function doAdminNetworkProcessChangenetworkForm () {
431         // Do we have selections?
432         if (countPostSelection() > 0) {
433                 // By default nothing is updated
434                 $updated = 0;
435
436                 // Something has been selected, so start updating them
437                 foreach (postRequestElement('sel') as $id => $sel) {
438                         // Update this entry?
439                         if ($sel == 1) {
440                                 // Init data array
441                                 $networkData = array();
442
443                                 // Transfer whole array, except 'sel'
444                                 foreach (postRequestArray() as $key => $entry) {
445                                         // Skip 'sel' and submit button
446                                         if (in_array($key, array('sel', 'change'))) continue;
447
448                                         // Do we have this enty?
449                                         if (!isset($entry[$id])) {
450                                                 // Not found, needs fixing
451                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
452                                         } // END - if
453
454                                         // Add this entry
455                                         $networkData[$key] = $entry[$id];
456                                 } // END - foreach
457
458                                 // Update the network data
459                                 $updated += doNetworkUpdateDataByArray($id, $networkData);
460                         } // END - if
461                 } // END - foreach
462
463                 // Do we have updates?
464                 if ($updated > 0) {
465                         // Updates done
466                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
467                 } else {
468                         // Nothing changed
469                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
470                 }
471         } // END - if
472 }
473
474 // Removes given networks
475 function doAdminNetworkProcessRemovenetworkForm () {
476         // Do we have selections?
477         if (countPostSelection() > 0) {
478                 // By default nothing is removed
479                 $removed = 0;
480
481                 // Something has been selected, so start updating them
482                 foreach (postRequestElement('sel') as $id => $sel) {
483                         // Update this entry?
484                         if ($sel == 1) {
485                                 // Remove this entry
486                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
487                         } // END - if
488                 } // END - foreach
489
490                 // Do we have removes?
491                 if ($removed > 0) {
492                         // Removals done
493                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
494                 } else {
495                         // Nothing removed
496                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
497                 }
498         } // END - if
499 }
500
501 // Add a network type handler if not yet found
502 function doAdminNetworkProcessAddnetworktypeForm () {
503         // Is the network type handle already used with given network?
504         if (isNetworkTypeHandleValid(postRequestElement('network_type_handle'), getRequestElement('network'))) {
505                 // Already added
506                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_HANDLE_ALREADY_ADDED', postRequestElement('network_type_handle')));
507
508                 // ... so abort here
509                 return false;
510         } // END - if
511
512         // Remove the 'ok' part
513         unsetPostRequestElement('ok');
514
515         // Add id
516         setPostRequestElement('network_id', getRequestElement('network'));
517
518         // Add the whole request to database
519         SQL_QUERY("INSERT INTO
520         `{?_MYSQL_PREFIX?}_network_types`
521 (
522         `" . implode('`,`', array_keys(postRequestArray())) . "`
523 ) VALUES (
524         '" . implode("','", array_values(postRequestArray())) . "'
525 )", __FUNCTION__, __LINE__);
526
527         // Output message
528         if (SQL_AFFECTEDROWS() == 1) {
529                 // Successfully added
530                 loadTemplate('admin_network_type_added', false, postRequestArray());
531         } else {
532                 // Not added
533                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_NOT_ADDED', postRequestElement('network_type_handle')));
534         }
535 }
536
537 // Changes given network type handlers
538 function doAdminNetworkProcessChangenetworktypeForm () {
539         // Do we have selections?
540         if (countPostSelection() > 0) {
541                 // By default nothing is updated
542                 $updated = 0;
543
544                 // Something has been selected, so start updating them
545                 foreach (postRequestElement('sel') as $id => $sel) {
546                         // Update this entry?
547                         if ($sel == 1) {
548                                 // Init data array
549                                 $networkTypeData = array();
550
551                                 // Transfer whole array, except 'sel'
552                                 foreach (postRequestArray() as $key => $entry) {
553                                         // Skip 'sel' and submit button
554                                         if (in_array($key, array('sel', 'change'))) continue;
555
556                                         // Do we have this enty?
557                                         if (!isset($entry[$id])) {
558                                                 // Not found, needs fixing
559                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
560                                         } // END - if
561
562                                         // Add this entry
563                                         $networkTypeData[$key] = $entry[$id];
564                                 } // END - foreach
565
566                                 // Update the network data
567                                 $updated += doNetworkUpdateTypeByArray($id, $networkTypeData);
568                         } // END - if
569                 } // END - foreach
570
571                 // Do we have updates?
572                 if ($updated > 0) {
573                         // Updates done
574                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_UPDATED', $updated));
575                 } else {
576                         // Nothing changed
577                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_CHANGED'));
578                 }
579         } // END - if
580 }
581
582 // Removes given network type handlers
583 function doAdminNetworkProcessRemovenetworktypeForm () {
584         // Do we have selections?
585         if (countPostSelection() > 0) {
586                 // By default nothing is removed
587                 $removed = 0;
588
589                 // Something has been selected, so start updating them
590                 foreach (postRequestElement('sel') as $id => $sel) {
591                         // Update this entry?
592                         if ($sel == 1) {
593                                 // Remove this entry
594                                 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $id);
595                         } // END - if
596                 } // END - foreach
597
598                 // Do we have removes?
599                 if ($removed > 0) {
600                         // Removals done
601                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_REMOVED', $removed));
602                 } else {
603                         // Nothing removed
604                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_REMOVED'));
605                 }
606         } // END - if
607 }
608
609 // [EOF]
610 ?>