2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 11/04/2009 *
4 * =================== Last change: 11/04/2009 *
6 * -------------------------------------------------------------------- *
7 * File : network_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for ext-network *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer ext-network *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Handle a (maybe) sent form here
46 function doNetworkHandleForm () {
48 if ((isFormSent()) || (isPostRequestParameterSet('edit')) || (isPostRequestParameterSet('del')) || (isPostRequestParameterSet('change')) || (isPostRequestParameterSet('remove'))) {
50 if (isGetRequestParameterSet('do')) {
51 // Process the request
52 doAdminNetworkProcessForm();
55 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_DO_404'));
60 // Processes an admin form
61 function doAdminNetworkProcessForm () {
63 if ((!isFormSent()) && (!isPostRequestParameterSet('edit')) && (!isPostRequestParameterSet('del')) && (!isPostRequestParameterSet('change')) && (!isPostRequestParameterSet('remove'))) {
65 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_FORM_NOT_SENT'));
67 } elseif (!isGetRequestParameterSet('do')) {
69 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_DO_404'));
73 // Create function name
74 $functionName = sprintf("doAdminNetworkProcess%sForm", ucfirst(strtolower(getRequestParameter('do'))));
76 // Is the function valid?
77 if (!function_exists($functionName)) {
78 // Invalid function name
79 debug_report_bug('Invalid do ' . getRequestParameter('do') . ', function ' . $functionName .' does not exist.', false);
82 // Call-back the method handling our request
83 call_user_func($functionName);
86 // Checks wether the (short) network name is already used (valid)
87 function isNetworkNameValid ($name) {
89 $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1",
90 array($name), __FUNCTION__, __LINE__);
93 $isValid = (SQL_NUMROWS($result) == 1);
96 SQL_FREERESULT($result);
102 // Checks wether the given network type is already used (valid)
103 function isNetworkTypeHandleValid ($type, $networkId) {
105 $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handle`='%s' LIMIT 1",
106 array($networkId, $type), __FUNCTION__, __LINE__);
109 $isValid = (SQL_NUMROWS($result) == 1);
112 SQL_FREERESULT($result);
118 // Checks wether the given network request parameter is already used (valid)
119 function isNetworkRequestParameterValid ($key, $type, $networkId) {
121 $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",
122 array($networkId, $type, $key), __FUNCTION__, __LINE__);
125 $isValid = (SQL_NUMROWS($result) == 1);
128 SQL_FREERESULT($result);
134 // "Getter" for a network's data by provided id number
135 function getNetworkDataById ($id) {
136 // Ids lower one are not accepted
138 // Not good, should be fixed
139 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
142 // By default we have no data
143 $networkData = array();
145 // Query for the network data
146 $result = SQL_QUERY_ESC("SELECT
147 `network_id`, `network_short_name`, `network_title`, `network_reflink`, `network_data_seperator`, `network_row_seperator`, `network_request_type`, `network_charset`
149 `{?_MYSQL_PREFIX?}_network_data`
153 array(bigintval($id)), __FUNCTION__, __LINE__);
155 // Do we have an entry?
156 if (SQL_NUMROWS($result) == 1) {
158 $networkData = SQL_FETCHARRAY($result);
162 SQL_FREERESULT($result);
168 // "Getter" for a network type data by provided id number
169 function getNetworkTypeDataById ($id) {
170 // Ids lower one are not accepted
172 // Not good, should be fixed
173 debug_report_bug('Network type id ' . $id . ' is smaller than 1.');
176 // By default we have no data
177 $networkTypeData = array();
179 // Query for the network data
180 $result = SQL_QUERY_ESC("SELECT
181 `network_type_id`, `network_id`, `network_type_handle`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url`
183 `{?_MYSQL_PREFIX?}_network_types`
187 array(bigintval($id)), __FUNCTION__, __LINE__);
189 // Do we have an entry?
190 if (SQL_NUMROWS($result) == 1) {
192 $networkTypeData = SQL_FETCHARRAY($result);
196 SQL_FREERESULT($result);
199 return $networkTypeData;
202 // Updates given network (id) with data from array
203 function doNetworkUpdateDataByArray ($id, $networkData) {
204 // Ids lower one are not accepted
206 // Not good, should be fixed
207 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
210 // Just call our inner method
211 return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false);
214 // Updates given network type handler (id) with data from array
215 function doNetworkUpdateTypeByArray ($id, $networkTypeData) {
216 // Ids lower one are not accepted
218 // Not good, should be fixed
219 debug_report_bug('Network type handler id ' . $id . ' is smaller than 1.');
222 // Just call our inner method
223 return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($id)), array(), false, false);
226 // Removes given network entry
227 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
229 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
230 array($table, $column, $id, $limit), __FILE__, __LINE__);
232 // Return affected rows
233 return SQL_AFFECTEDROWS();
236 // Generates a list of networks for given script and returns it
237 function generateAdminNetworkList () {
241 // Query for all networks
242 $result = SQL_QUERY("SELECT
243 `network_id`, `network_short_name`, `network_title`
245 `{?_MYSQL_PREFIX?}_network_data`
247 `network_short_name` ASC", __FILE__, __LINE__);
249 // Do we have entries?
250 if (SQL_NUMROWS($result) > 0) {
253 while ($row = SQL_FETCHARRAY($result)) {
254 // Is this valid, then add it
255 if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row;
258 // Generate the selection box
259 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id');
262 $content = loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_ENTRIES_404'));
266 SQL_FREERESULT($result);
272 //------------------------------------------------------------------------------
273 // Call-back functions
274 //------------------------------------------------------------------------------
276 // Callback function to add new network
277 function doAdminNetworkProcessAddnetworkForm () {
278 // We can say here, the form is sent, so check if the network is already added
279 if (isNetworkNameValid(postRequestParameter('network_short_name'))) {
281 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestParameter('network_short_name')));
285 // Remove the 'ok' part
286 unsetPostRequestParameter('ok');
288 // Add the whole request to database
289 SQL_QUERY("INSERT INTO
290 `{?_MYSQL_PREFIX?}_network_data`
292 `" . implode('`,`', array_keys(postRequestArray())) . "`
294 '" . implode("','", array_values(postRequestArray())) . "'
295 )", __FUNCTION__, __LINE__);
297 // Add the id for output only
298 setPostRequestParameter('network_id', SQL_INSERTID());
301 if (SQL_AFFECTEDROWS() == 1) {
302 // Successfully added
303 loadTemplate('admin_network_added', false, postRequestArray());
306 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestParameter('network_short_name')));
310 // Displays selected networks for editing
311 function doAdminNetworkProcessHandlenetworkForm () {
312 // Do we have selections?
313 if (countPostSelection() > 0) {
314 // Something has been selected, so start displaying one by one
316 foreach (postRequestParameter('sel') as $id => $sel) {
319 // Load this network's data
320 $networkData = getNetworkDataById($id);
322 // Do we have found the network?
323 if (count($networkData) > 0) {
325 $networkData['sw'] = $SW;
327 if (isPostRequestParameterSet('edit')) {
328 // Make selection box for network_request_type
329 $networkData['network_request_type'] = generateOptionList(
333 getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
334 getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
336 $networkData['network_request_type']
339 // Add row template for editing
340 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
341 } elseif (isPostRequestParameterSet('del')) {
342 // Translate the request type
343 $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
345 // Add row template for deleting
346 $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
349 debug_report_bug('Cannot detect edit/del.');
358 // If we have no rows, we don't need to display the edit form
360 // Output main template
361 if (isPostRequestParameterSet('edit')) {
362 loadTemplate('admin_edit_networks', false, $OUT);
363 } elseif (isPostRequestParameterSet('del')) {
364 loadTemplate('admin_del_networks', false, $OUT);
367 debug_report_bug('Cannot detect edit/del.');
370 // Don't display the list/add new form
371 $GLOBALS['network_display'] = false;
373 // Nothing selected/found
374 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND'));
379 // Handle network type form
380 function doAdminNetworkProcessHandlenetworktypeForm () {
381 // Do we have selections?
382 if (countPostSelection() > 0) {
384 $networkData = getNetworkDataById(getRequestParameter('network'));
386 // Something has been selected, so start displaying one by one
388 foreach (postRequestParameter('sel') as $id => $sel) {
391 // Load this network's data
392 $networkTypeData = getNetworkTypeDataById($id);
394 // Do we have found the network?
395 if (count($networkTypeData) > 0) {
397 $networkTypeData['sw'] = $SW;
399 if (isPostRequestParameterSet('edit')) {
400 // Add row template for deleting
401 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
402 } elseif (isPostRequestParameterSet('del')) {
403 // Fix empty banner URL
404 if (trim($networkTypeData['network_type_banner_url']) == '') $networkTypeData['network_type_banner_url'] = '---';
406 // Add row template for deleting
407 $OUT .= loadTemplate('admin_del_network_types_row', true, $networkTypeData);
410 debug_report_bug('Cannot detect edit/del.');
419 // If we have no rows, we don't need to display the edit form
421 // Prepare content for template
424 'network_data' => getNetworkDataById(getRequestParameter('network'))
427 // Output main template
428 if (isPostRequestParameterSet('edit')) {
429 loadTemplate('admin_edit_network_types', false, $content);
430 } elseif (isPostRequestParameterSet('del')) {
431 loadTemplate('admin_del_network_types', false, $content);
434 debug_report_bug('Cannot detect edit/del.');
437 // Don't display the list/add new form
438 $GLOBALS['network_display'] = false;
440 // Nothing selected/found
441 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_FOUND'));
446 // Changes given networks
447 function doAdminNetworkProcessChangenetworkForm () {
448 // Do we have selections?
449 if (countPostSelection() > 0) {
450 // By default nothing is updated
453 // Something has been selected, so start updating them
454 foreach (postRequestParameter('sel') as $id => $sel) {
455 // Update this entry?
458 $networkData = array();
460 // Transfer whole array, except 'sel'
461 foreach (postRequestArray() as $key => $entry) {
462 // Skip 'sel' and submit button
463 if (in_array($key, array('sel', 'change'))) continue;
465 // Do we have this enty?
466 if (!isset($entry[$id])) {
467 // Not found, needs fixing
468 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
472 $networkData[$key] = $entry[$id];
475 // Update the network data
476 $updated += doNetworkUpdateDataByArray($id, $networkData);
480 // Do we have updates?
483 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
486 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
491 // Removes given networks
492 function doAdminNetworkProcessRemovenetworkForm () {
493 // Do we have selections?
494 if (countPostSelection() > 0) {
495 // By default nothing is removed
498 // Something has been selected, so start updating them
499 foreach (postRequestParameter('sel') as $id => $sel) {
500 // Update this entry?
503 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
507 // Do we have removes?
510 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
513 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
518 // Add a network type handler if not yet found
519 function doAdminNetworkProcessAddnetworktypeForm () {
520 // Is the network type handle already used with given network?
521 if (isNetworkTypeHandleValid(postRequestParameter('network_type_handle'), getRequestParameter('network'))) {
523 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_HANDLE_ALREADY_ADDED', postRequestParameter('network_type_handle')));
529 // Remove the 'ok' part
530 unsetPostRequestParameter('ok');
533 setPostRequestParameter('network_id', getRequestParameter('network'));
535 // Is network_type_banner_url set?
536 if (postRequestParameter('network_type_banner_url') == '') {
537 // Remove empty value to get a NULL for an optional entry
538 unsetPostRequestParameter('network_type_banner_url');
541 // Add the whole request to database
542 SQL_QUERY("INSERT INTO
543 `{?_MYSQL_PREFIX?}_network_types`
545 `" . implode('`,`', array_keys(postRequestArray())) . "`
547 '" . implode("','", array_values(postRequestArray())) . "'
548 )", __FUNCTION__, __LINE__);
551 if (SQL_AFFECTEDROWS() == 1) {
552 // Successfully added
553 loadTemplate('admin_network_type_added', false, postRequestArray());
556 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_NOT_ADDED', postRequestParameter('network_type_handle')));
560 // Changes given network type handlers
561 function doAdminNetworkProcessChangenetworktypeForm () {
562 // Do we have selections?
563 if (countPostSelection() > 0) {
564 // By default nothing is updated
567 // Something has been selected, so start updating them
568 foreach (postRequestParameter('sel') as $id => $sel) {
569 // Update this entry?
572 $networkTypeData = array();
574 // Transfer whole array, except 'sel'
575 foreach (postRequestArray() as $key => $entry) {
576 // Skip 'sel' and submit button
577 if (in_array($key, array('sel', 'change'))) continue;
579 // Do we have this enty?
580 if (!isset($entry[$id])) {
581 // Not found, needs fixing
582 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
586 $networkTypeData[$key] = $entry[$id];
589 // Update the network data
590 $updated += doNetworkUpdateTypeByArray($id, $networkTypeData);
594 // Do we have updates?
597 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_UPDATED', $updated));
600 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_CHANGED'));
605 // Removes given network type handlers
606 function doAdminNetworkProcessRemovenetworktypeForm () {
607 // Do we have selections?
608 if (countPostSelection() > 0) {
609 // By default nothing is removed
612 // Something has been selected, so start updating them
613 foreach (postRequestParameter('sel') as $id => $sel) {
614 // Update this entry?
617 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $id);
621 // Do we have removes?
624 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_REMOVED', $removed));
627 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_REMOVED'));
632 // Adds a request parameter to given network and type
633 function doAdminNetworkProcessAddnetworkparamForm () {
634 // Is the request parameter already used with given network?
635 if (isNetworkRequestParameterValid(postRequestParameter('request_param_key'), postRequestParameter('network_type_id'), getRequestParameter('network'))) {
637 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED', postRequestParameter('request_param_key')));
643 // Remove the 'ok' part
644 unsetPostRequestParameter('ok');
647 setPostRequestParameter('network_id', getRequestParameter('network'));
649 // Is request_param_default set?
650 if (postRequestParameter('request_param_default') == '') {
651 // Remove empty value to get a NULL for an optional entry
652 unsetPostRequestParameter('request_param_default');
655 // Add the whole request to database
656 SQL_QUERY("INSERT INTO
657 `{?_MYSQL_PREFIX?}_network_request_params`
659 `" . implode('`,`', array_keys(postRequestArray())) . "`
661 '" . implode("','", array_values(postRequestArray())) . "'
662 )", __FUNCTION__, __LINE__);
665 if (SQL_AFFECTEDROWS() == 1) {
666 // Successfully added
667 loadTemplate('admin_network_request_param_added', false, postRequestArray());
670 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED', postRequestParameter('request_param_key')));