b97060e9d45387c3f771c52848e1861c5b6b6f07
[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 // Removes given network entry
198 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
199         // Remove the entry
200         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
201                 array($table, $column, $id, $limit), __FILE__, __LINE__);
202
203         // Return affected rows
204         return SQL_AFFECTEDROWS();
205 }
206
207 // Generates a list of networks for given script and returns it
208 function generateAdminNetworkList () {
209         // Init output
210         $content = '';
211
212         // Query for all networks
213         $result = SQL_QUERY("SELECT
214         `network_id`, `network_title`
215 FROM
216         `{?_MYSQL_PREFIX?}_network_data`
217 ORDER BY
218         `network_title` ASC", __FILE__, __LINE__);
219
220         // Do we have entries?
221         if (SQL_NUMROWS($result) > 0) {
222                 // List all entries
223                 $rows = array();
224                 while ($row = SQL_FETCHARRAY($result)) {
225                         // Is this valid, then add it
226                         if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row;
227                 } // END - while
228
229                 // Generate the selection box
230                 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id', 'network_title');
231         } else {
232                 // Nothing selected
233                 $content = loadTemplate('admin_settings_saved', false, getMessage('ADMIN_ENTRIES_404'));
234         }
235
236         // Free the result
237         SQL_FREERESULT($result);
238
239         // Return the list
240         return $content;
241 }
242
243 //------------------------------------------------------------------------------
244 //                             Call-back functions
245 //------------------------------------------------------------------------------
246
247 // Callback function to add new network
248 function doAdminNetworkProcessAddnetworkForm () {
249         // We can say here, the form is sent, so check if the network is already added
250         if (isNetworkNameValid(postRequestElement('network_short_name'))) {
251                 // Already there
252                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestElement('network_short_name')));
253                 return false;
254         } // END - if
255
256         // Remove the 'ok' part
257         unsetPostRequestElement('ok');
258
259         // Add the whole request to database
260         SQL_QUERY("INSERT INTO
261         `{?_MYSQL_PREFIX?}_network_data`
262 (
263         `" . implode('`,`', array_keys(postRequestArray())) . "`
264 ) VALUES (
265         '" . implode("','", array_values(postRequestArray())) . "'
266 )", __FUNCTION__, __LINE__);
267
268         // Add the id for output only
269         setPostRequestElement('network_id', SQL_INSERTID());
270
271         // Output message
272         if (SQL_AFFECTEDROWS() == 1) {
273                 // Successfully added
274                 loadTemplate('admin_network_added', false, postRequestArray());
275         } else {
276                 // Not added
277                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestElement('network_short_name')));
278         }
279 }
280
281 // Displays selected networks for editing
282 function doAdminNetworkProcessHandlenetworkForm () {
283         // Do we have selections?
284         if (countPostSelection() > 0) {
285                 // Something has been selected, so start displaying one by one
286                 $SW = 2; $OUT = '';
287                 foreach (postRequestElement('sel') as $id => $sel) {
288                         // Is this selected?
289                         if ($sel == 1) {
290                                 // Load this network's data
291                                 $networkData = getNetworkDataById($id);
292
293                                 // Do we have found the network?
294                                 if (count($networkData) > 0) {
295                                         // Add color
296                                         $networkData['sw'] = $SW;
297
298                                         if (isPostRequestElementSet('edit')) {
299                                                 // Make selection box for network_request_type
300                                                 $networkData['network_request_type'] = generateOptionList(
301                                                         '/ARRAY/',
302                                                         array('GET','POST'),
303                                                         array(
304                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
305                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
306                                                         ),
307                                                         $networkData['network_request_type']
308                                                 );
309
310                                                 // Add row template for editing
311                                                 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
312                                         } elseif (isPostRequestElementSet('del')) {
313                                                 // Translate the request type
314                                                 $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
315
316                                                 // Add row template for deleting
317                                                 $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
318                                         } else {
319                                                 // Problem!
320                                                 debug_report_bug('Cannot detect edit/del.');
321                                         }
322
323                                         // Switch colors
324                                         $SW = 3 - $SW;
325                                 } // END - if
326                         } // END - if
327                 } // END - foreach
328
329                 // If we have no rows, we don't need to display the edit form
330                 if (!empty($OUT)) {
331                         // Output main template
332                         if (isPostRequestElementSet('edit')) {
333                                 loadTemplate('admin_edit_networks', false, $OUT);
334                         } elseif (isPostRequestElementSet('del')) {
335                                 loadTemplate('admin_del_networks', false, $OUT);
336                         } else {
337                                 // Problem!
338                                 debug_report_bug('Cannot detect edit/del.');
339                         }
340
341                         // Don't display the list/add new form
342                         $GLOBALS['network_display'] = false;
343                 } else {
344                         // Nothing selected/found
345                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND'));
346                 }
347         } // END - if
348 }
349
350 // Handle network type form
351 function doAdminNetworkProcessHandlenetworktypeForm () {
352         // Do we have selections?
353         if (countPostSelection() > 0) {
354                 // Load network data
355                 $networkData = getNetworkDataById(getRequestElement('network'));
356
357                 // Something has been selected, so start displaying one by one
358                 $SW = 2; $OUT = '';
359                 foreach (postRequestElement('sel') as $id => $sel) {
360                         // Is this selected?
361                         if ($sel == 1) {
362                                 // Load this network's data
363                                 $networkTypeData = getNetworkTypeDataById($id);
364
365                                 // Do we have found the network?
366                                 if (count($networkTypeData) > 0) {
367                                         // Add color
368                                         $networkTypeData['sw'] = $SW;
369
370                                         if (isPostRequestElementSet('edit')) {
371                                                 // Add row template for deleting
372                                                 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
373                                         } elseif (isPostRequestElementSet('del')) {
374                                                 // Fix empty banner URL
375                                                 if (trim($networkTypeData['network_type_banner_url']) == '') $networkTypeData['network_type_banner_url'] = '---';
376
377                                                 // Add row template for deleting
378                                                 $OUT .= loadTemplate('admin_del_network_types_row', true, $networkTypeData);
379                                         } else {
380                                                 // Problem!
381                                                 debug_report_bug('Cannot detect edit/del.');
382                                         }
383
384                                         // Switch colors
385                                         $SW = 3 - $SW;
386                                 } // END - if
387                         } // END - if
388                 } // END - foreach
389
390                 // If we have no rows, we don't need to display the edit form
391                 if (!empty($OUT)) {
392                         // Prepare content for template
393                         $content = array(
394                                 'rows' => $OUT,
395                                 'network_data' => getNetworkDataById(getRequestElement('network'))
396                         );
397
398                         // Output main template
399                         if (isPostRequestElementSet('edit')) {
400                                 loadTemplate('admin_edit_network_types', false, $content);
401                         } elseif (isPostRequestElementSet('del')) {
402                                 loadTemplate('admin_del_network_types', false, $content);
403                         } else {
404                                 // Problem!
405                                 debug_report_bug('Cannot detect edit/del.');
406                         }
407
408                         // Don't display the list/add new form
409                         $GLOBALS['network_display'] = false;
410                 } else {
411                         // Nothing selected/found
412                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_TYPE_NOTHING_FOUND'));
413                 }
414         } // END - if
415 }
416
417 // Changes given networks
418 function doAdminNetworkProcessChangenetworkForm () {
419         // Do we have selections?
420         if (countPostSelection() > 0) {
421                 // By default nothing is updated
422                 $updated = 0;
423
424                 // Something has been selected, so start updating them
425                 foreach (postRequestElement('sel') as $id => $sel) {
426                         // Update this entry?
427                         if ($sel == 1) {
428                                 // Init data array
429                                 $networkData = array();
430
431                                 // Transfer whole array, except 'sel'
432                                 foreach (postRequestArray() as $key => $entry) {
433                                         // Skip 'sel' and submit button
434                                         if (in_array($key, array('sel', 'change'))) continue;
435
436                                         // Do we have this enty?
437                                         if (!isset($entry[$id])) {
438                                                 // Not found, needs fixing
439                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
440                                         } // END - if
441
442                                         // Add this entry
443                                         $networkData[$key] = $entry[$id];
444                                 } // END - foreach
445
446                                 // Update the network data
447                                 $updated += doNetworkUpdateDataByArray($id, $networkData);
448                         } // END - if
449                 } // END - foreach
450
451                 // Do we have updates?
452                 if ($updated > 0) {
453                         // Updates done
454                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
455                 } else {
456                         // Nothing changed
457                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
458                 }
459         } // END - if
460 }
461
462 // Removes given networks
463 function doAdminNetworkProcessRemovenetworkForm () {
464         // Do we have selections?
465         if (countPostSelection() > 0) {
466                 // By default nothing is removed
467                 $removed = 0;
468
469                 // Something has been selected, so start updating them
470                 foreach (postRequestElement('sel') as $id => $sel) {
471                         // Update this entry?
472                         if ($sel == 1) {
473                                 // Remove this entry
474                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
475                         } // END - if
476                 } // END - foreach
477
478                 // Do we have removes?
479                 if ($removed > 0) {
480                         // Removals done
481                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
482                 } else {
483                         // Nothing removed
484                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
485                 }
486         } // END - if
487 }
488
489 // Add a network type if not yet found
490 function doAdminNetworkProcessAddnetworktypeForm () {
491         // Is the network type handle already used with given network?
492         if (isNetworkTypeHandleValid(postRequestElement('network_type_handle'), getRequestElement('network'))) {
493                 // Already added
494                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_HANDLE_ALREADY_ADDED', postRequestElement('network_type_handle')));
495
496                 // ... so abort here
497                 return false;
498         } // END - if
499
500         // Remove the 'ok' part
501         unsetPostRequestElement('ok');
502
503         // Add id
504         setPostRequestElement('network_id', getRequestElement('network'));
505
506         // Add the whole request to database
507         SQL_QUERY("INSERT INTO
508         `{?_MYSQL_PREFIX?}_network_types`
509 (
510         `" . implode('`,`', array_keys(postRequestArray())) . "`
511 ) VALUES (
512         '" . implode("','", array_values(postRequestArray())) . "'
513 )", __FUNCTION__, __LINE__);
514
515         // Output message
516         if (SQL_AFFECTEDROWS() == 1) {
517                 // Successfully added
518                 loadTemplate('admin_network_type_added', false, postRequestArray());
519         } else {
520                 // Not added
521                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_NOT_ADDED', postRequestElement('network_type_handle')));
522         }
523 }
524
525 // [EOF]
526 ?>