Adding of network type handlers finished (listing is still work-in-progress)
[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 // Updates given network (id) with data from array
152 function doNetworkUpdateDataByArray ($id, $networkData) {
153         // Ids lower one are not accepted
154         if ($id < 1) {
155                 // Not good, should be fixed
156                 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
157         } // END - if
158
159         // Just call our inner method
160         return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false);
161 }
162
163 // Removes given network entry
164 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
165         // Remove the entry
166         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
167                 array($table, $column, $id, $limit), __FILE__, __LINE__);
168
169         // Return affected rows
170         return SQL_AFFECTEDROWS();
171 }
172
173 // Generates a list of networks for given script and returns it
174 function generateAdminNetworkList () {
175         // Init output
176         $content = '';
177
178         // Query for all networks
179         $result = SQL_QUERY("SELECT
180         `network_id`, `network_title`
181 FROM
182         `{?_MYSQL_PREFIX?}_network_data`
183 ORDER BY
184         `network_title` ASC", __FILE__, __LINE__);
185
186         // Do we have entries?
187         if (SQL_NUMROWS($result) > 0) {
188                 // List all entries
189                 $rows = array();
190                 while ($row = SQL_FETCHARRAY($result)) {
191                         // Is this valid, then add it
192                         if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row;
193                 } // END - while
194
195                 // Generate the selection box
196                 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id', 'network_title');
197         } else {
198                 // Nothing selected
199                 $content = loadTemplate('admin_settings_saved', false, getMessage('ADMIN_ENTRIES_404'));
200         }
201
202         // Free the result
203         SQL_FREERESULT($result);
204
205         // Return the list
206         return $content;
207 }
208
209 //------------------------------------------------------------------------------
210 //                             Call-back functions
211 //------------------------------------------------------------------------------
212
213 // Callback function to add new network
214 function doAdminNetworkProcessAddnetworkForm () {
215         // We can say here, the form is sent, so check if the network is already added
216         if (isNetworkNameValid(postRequestElement('network_short_name'))) {
217                 // Already there
218                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestElement('network_short_name')));
219                 return false;
220         } // END - if
221
222         // Remove the 'ok' part
223         unsetPostRequestElement('ok');
224
225         // Add the whole request to database
226         SQL_QUERY("INSERT INTO
227         `{?_MYSQL_PREFIX?}_network_data`
228 (
229         `" . implode('`,`', array_keys(postRequestArray())) . "`
230 ) VALUES (
231         '" . implode("','", array_values(postRequestArray())) . "'
232 )", __FUNCTION__, __LINE__);
233
234         // Add the id for output only
235         setPostRequestElement('network_id', SQL_INSERTID());
236
237         // Output message
238         if (SQL_AFFECTEDROWS() == 1) {
239                 // Successfully added
240                 loadTemplate('admin_network_added', false, postRequestArray());
241         } else {
242                 // Not added
243                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestElement('network_short_name')));
244         }
245 }
246
247 // Displays selected networks for editing
248 function doAdminNetworkProcessHandlenetworkForm () {
249         // Do we have selections?
250         if (countPostSelection() > 0) {
251                 // Something has been selected, so start displaying one by one
252                 $SW = 2; $OUT = '';
253                 foreach (postRequestElement('sel') as $id => $sel) {
254                         // Is this selected?
255                         if ($sel == 1) {
256                                 // Load this network's data
257                                 $networkData = getNetworkDataById($id);
258
259                                 // Do we have found the network?
260                                 if (count($networkData) > 0) {
261                                         // Add color
262                                         $networkData['sw'] = $SW;
263
264                                         if (isPostRequestElementSet('edit')) {
265                                                 // Make selection box for network_request_type
266                                                 $networkData['network_request_type'] = generateOptionList(
267                                                         '/ARRAY/',
268                                                         array('GET','POST'),
269                                                         array(
270                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
271                                                                 getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
272                                                         ),
273                                                         $networkData['network_request_type']
274                                                 );
275
276                                                 // Add row template for editing
277                                                 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
278                                         } elseif (isPostRequestElementSet('del')) {
279                                                 // Translate the request type
280                                                 $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
281
282                                                 // Add row template for deleting
283                                                 $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
284                                         } else {
285                                                 // Problem!
286                                                 debug_report_bug('Cannot detect edit/del.');
287                                         }
288
289                                         // Switch colors
290                                         $SW = 3 - $SW;
291                                 } // END - if
292                         } // END - if
293                 } // END - foreach
294
295                 // If we have no rows, we don't need to display the edit form
296                 if (!empty($OUT)) {
297                         // Output main template
298                         if (isPostRequestElementSet('edit')) {
299                                 loadTemplate('admin_edit_networks', false, $OUT);
300                         } elseif (isPostRequestElementSet('del')) {
301                                 loadTemplate('admin_del_networks', false, $OUT);
302                         } else {
303                                 // Problem!
304                                 debug_report_bug('Cannot detect edit/del.');
305                         }
306
307                         // Don't display the list/add new form
308                         $GLOBALS['network_display'] = false;
309                 } else {
310                         // Nothing selected/found
311                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND'));
312                 }
313         } // END - if
314 }
315
316 // Changes given networks
317 function doAdminNetworkProcessChangenetworkForm () {
318         // Do we have selections?
319         if (countPostSelection() > 0) {
320                 // By default nothing is updated
321                 $updated = 0;
322
323                 // Something has been selected, so start updating them
324                 foreach (postRequestElement('sel') as $id => $sel) {
325                         // Update this entry?
326                         if ($sel == 1) {
327                                 // Init data array
328                                 $networkData = array();
329
330                                 // Transfer whole array, except 'sel'
331                                 foreach (postRequestArray() as $key => $entry) {
332                                         // Skip 'sel' and submit button
333                                         if (in_array($key, array('sel', 'change'))) continue;
334
335                                         // Do we have this enty?
336                                         if (!isset($entry[$id])) {
337                                                 // Not found, needs fixing
338                                                 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
339                                         } // END - if
340
341                                         // Add this entry
342                                         $networkData[$key] = $entry[$id];
343                                 } // END - foreach
344
345                                 // Update the network data
346                                 $updated += doNetworkUpdateDataByArray($id, $networkData);
347                         } // END - if
348                 } // END - foreach
349
350                 // Do we have updates?
351                 if ($updated > 0) {
352                         // Updates done
353                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
354                 } else {
355                         // Nothing changed
356                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
357                 }
358         } // END - if
359 }
360
361 // Removes given networks
362 function doAdminNetworkProcessRemovenetworkForm () {
363         // Do we have selections?
364         if (countPostSelection() > 0) {
365                 // By default nothing is removed
366                 $removed = 0;
367
368                 // Something has been selected, so start updating them
369                 foreach (postRequestElement('sel') as $id => $sel) {
370                         // Update this entry?
371                         if ($sel == 1) {
372                                 // Remove this entry
373                                 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
374                         } // END - if
375                 } // END - foreach
376
377                 // Do we have removes?
378                 if ($removed > 0) {
379                         // Removals done
380                         loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
381                 } else {
382                         // Nothing removed
383                         loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
384                 }
385         } // END - if
386 }
387
388 // Add a network type if not yet found
389 function doAdminNetworkProcessAddnetworktypeForm () {
390         // Is the network type handle already used with given network?
391         if (isNetworkTypeHandleValid(postRequestElement('network_type_handle'), getRequestElement('network'))) {
392                 // Already added
393                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_HANDLE_ALREADY_ADDED', postRequestElement('network_type_handle')));
394
395                 // ... so abort here
396                 return false;
397         } // END - if
398
399         // Remove the 'ok' part
400         unsetPostRequestElement('ok');
401
402         // Add id
403         setPostRequestElement('network_id', getRequestElement('network'));
404
405         // Add the whole request to database
406         SQL_QUERY("INSERT INTO
407         `{?_MYSQL_PREFIX?}_network_types`
408 (
409         `" . implode('`,`', array_keys(postRequestArray())) . "`
410 ) VALUES (
411         '" . implode("','", array_values(postRequestArray())) . "'
412 )", __FUNCTION__, __LINE__);
413
414         // Output message
415         if (SQL_AFFECTEDROWS() == 1) {
416                 // Successfully added
417                 loadTemplate('admin_network_type_added', false, postRequestArray());
418         } else {
419                 // Not added
420                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_NOT_ADDED', postRequestElement('network_type_handle')));
421         }
422 }
423
424 // [EOF]
425 ?>