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 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
44 // Handle a (maybe) sent form here
45 function doNetworkHandleForm () {
47 if ((isFormSent()) || (isPostRequestParameterSet('edit')) || (isPostRequestParameterSet('del')) || (isPostRequestParameterSet('change')) || (isPostRequestParameterSet('remove'))) {
49 if (isGetRequestParameterSet('do')) {
50 // Process the request
51 doAdminNetworkProcessForm();
54 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_DO_404'));
59 // Processes an admin form
60 function doAdminNetworkProcessForm () {
62 if ((!isFormSent()) && (!isPostRequestParameterSet('edit')) && (!isPostRequestParameterSet('del')) && (!isPostRequestParameterSet('change')) && (!isPostRequestParameterSet('remove'))) {
64 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_FORM_NOT_SENT'));
66 } elseif (!isGetRequestParameterSet('do')) {
68 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_DO_404'));
72 // Create function name
73 $functionName = sprintf("doAdminNetworkProcess%sForm", ucfirst(strtolower(getRequestParameter('do'))));
75 // Is the function valid?
76 if (!function_exists($functionName)) {
77 // Invalid function name
78 debug_report_bug('Invalid do ' . getRequestParameter('do') . ', function ' . $functionName .' does not exist.', false);
81 // Call-back the method handling our request
82 call_user_func($functionName);
85 // Checks wether the (short) network name is already used (valid)
86 function isNetworkNameValid ($name) {
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__);
92 $isValid = (SQL_NUMROWS($result) == 1);
95 SQL_FREERESULT($result);
101 // Checks wether the given network type is already used (valid)
102 function isNetworkTypeHandleValid ($type, $networkId) {
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__);
108 $isValid = (SQL_NUMROWS($result) == 1);
111 SQL_FREERESULT($result);
117 // Checks wether the given network request parameter is already used (valid)
118 function isNetworkRequestParameterValid ($key, $type, $networkId) {
120 $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",
121 array($networkId, $type, $key), __FUNCTION__, __LINE__);
124 $isValid = (SQL_NUMROWS($result) == 1);
127 SQL_FREERESULT($result);
133 // "Getter" for a network's data by provided id number
134 function getNetworkDataById ($id) {
135 // Ids lower one are not accepted
137 // Not good, should be fixed
138 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
141 // By default we have no data
142 $networkData = array();
144 // Query for the network data
145 $result = SQL_QUERY_ESC("SELECT
146 `network_id`, `network_short_name`, `network_title`, `network_reflink`, `network_data_seperator`, `network_row_seperator`, `network_request_type`, `network_charset`
148 `{?_MYSQL_PREFIX?}_network_data`
152 array(bigintval($id)), __FUNCTION__, __LINE__);
154 // Do we have an entry?
155 if (SQL_NUMROWS($result) == 1) {
157 $networkData = SQL_FETCHARRAY($result);
161 SQL_FREERESULT($result);
167 // "Getter" for a network type data by provided id number
168 function getNetworkTypeDataById ($id) {
169 // Ids lower one are not accepted
171 // Not good, should be fixed
172 debug_report_bug('Network type id ' . $id . ' is smaller than 1.');
175 // By default we have no data
176 $networkTypeData = array();
178 // Query for the network data
179 $result = SQL_QUERY_ESC("SELECT
180 `network_type_id`, `network_id`, `network_type_handle`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url`
182 `{?_MYSQL_PREFIX?}_network_types`
186 array(bigintval($id)), __FUNCTION__, __LINE__);
188 // Do we have an entry?
189 if (SQL_NUMROWS($result) == 1) {
191 $networkTypeData = SQL_FETCHARRAY($result);
195 SQL_FREERESULT($result);
198 return $networkTypeData;
201 // Updates given network (id) with data from array
202 function doNetworkUpdateDataByArray ($id, $networkData) {
203 // Ids lower one are not accepted
205 // Not good, should be fixed
206 debug_report_bug('Network id ' . $id . ' is smaller than 1.');
209 // Just call our inner method
210 return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false);
213 // Updates given network type handler (id) with data from array
214 function doNetworkUpdateTypeByArray ($id, $networkTypeData) {
215 // Ids lower one are not accepted
217 // Not good, should be fixed
218 debug_report_bug('Network type handler id ' . $id . ' is smaller than 1.');
221 // Just call our inner method
222 return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($id)), array(), false, false);
225 // Removes given network entry
226 function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) {
228 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s",
229 array($table, $column, $id, $limit), __FILE__, __LINE__);
231 // Return affected rows
232 return SQL_AFFECTEDROWS();
235 // Generates a list of networks for given script and returns it
236 function generateAdminNetworkList () {
240 // Query for all networks
241 $result = SQL_QUERY("SELECT
242 `network_id`, `network_title`
244 `{?_MYSQL_PREFIX?}_network_data`
246 `network_title` ASC", __FILE__, __LINE__);
248 // Do we have entries?
249 if (SQL_NUMROWS($result) > 0) {
252 while ($row = SQL_FETCHARRAY($result)) {
253 // Is this valid, then add it
254 if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row;
257 // Generate the selection box
258 $content = generateSelectionBoxFromArray($rows, 'network', 'network_id', 'network_title');
261 $content = loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_ENTRIES_404'));
265 SQL_FREERESULT($result);
271 //------------------------------------------------------------------------------
272 // Call-back functions
273 //------------------------------------------------------------------------------
275 // Callback function to add new network
276 function doAdminNetworkProcessAddnetworkForm () {
277 // We can say here, the form is sent, so check if the network is already added
278 if (isNetworkNameValid(postRequestParameter('network_short_name'))) {
280 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestParameter('network_short_name')));
284 // Remove the 'ok' part
285 unsetPostRequestParameter('ok');
287 // Add the whole request to database
288 SQL_QUERY("INSERT INTO
289 `{?_MYSQL_PREFIX?}_network_data`
291 `" . implode('`,`', array_keys(postRequestArray())) . "`
293 '" . implode("','", array_values(postRequestArray())) . "'
294 )", __FUNCTION__, __LINE__);
296 // Add the id for output only
297 setPostRequestParameter('network_id', SQL_INSERTID());
300 if (SQL_AFFECTEDROWS() == 1) {
301 // Successfully added
302 loadTemplate('admin_network_added', false, postRequestArray());
305 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestParameter('network_short_name')));
309 // Displays selected networks for editing
310 function doAdminNetworkProcessHandlenetworkForm () {
311 // Do we have selections?
312 if (countPostSelection() > 0) {
313 // Something has been selected, so start displaying one by one
315 foreach (postRequestParameter('sel') as $id => $sel) {
318 // Load this network's data
319 $networkData = getNetworkDataById($id);
321 // Do we have found the network?
322 if (count($networkData) > 0) {
324 $networkData['sw'] = $SW;
326 if (isPostRequestParameterSet('edit')) {
327 // Make selection box for network_request_type
328 $networkData['network_request_type'] = generateOptionList(
332 getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'),
333 getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST')
335 $networkData['network_request_type']
338 // Add row template for editing
339 $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData);
340 } elseif (isPostRequestParameterSet('del')) {
341 // Translate the request type
342 $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']);
344 // Add row template for deleting
345 $OUT .= loadTemplate('admin_del_networks_row', true, $networkData);
348 debug_report_bug('Cannot detect edit/del.');
357 // If we have no rows, we don't need to display the edit form
359 // Output main template
360 if (isPostRequestParameterSet('edit')) {
361 loadTemplate('admin_edit_networks', false, $OUT);
362 } elseif (isPostRequestParameterSet('del')) {
363 loadTemplate('admin_del_networks', false, $OUT);
366 debug_report_bug('Cannot detect edit/del.');
369 // Don't display the list/add new form
370 $GLOBALS['network_display'] = false;
372 // Nothing selected/found
373 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND'));
378 // Handle network type form
379 function doAdminNetworkProcessHandlenetworktypeForm () {
380 // Do we have selections?
381 if (countPostSelection() > 0) {
383 $networkData = getNetworkDataById(getRequestParameter('network'));
385 // Something has been selected, so start displaying one by one
387 foreach (postRequestParameter('sel') as $id => $sel) {
390 // Load this network's data
391 $networkTypeData = getNetworkTypeDataById($id);
393 // Do we have found the network?
394 if (count($networkTypeData) > 0) {
396 $networkTypeData['sw'] = $SW;
398 if (isPostRequestParameterSet('edit')) {
399 // Add row template for deleting
400 $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData);
401 } elseif (isPostRequestParameterSet('del')) {
402 // Fix empty banner URL
403 if (trim($networkTypeData['network_type_banner_url']) == '') $networkTypeData['network_type_banner_url'] = '---';
405 // Add row template for deleting
406 $OUT .= loadTemplate('admin_del_network_types_row', true, $networkTypeData);
409 debug_report_bug('Cannot detect edit/del.');
418 // If we have no rows, we don't need to display the edit form
420 // Prepare content for template
423 'network_data' => getNetworkDataById(getRequestParameter('network'))
426 // Output main template
427 if (isPostRequestParameterSet('edit')) {
428 loadTemplate('admin_edit_network_types', false, $content);
429 } elseif (isPostRequestParameterSet('del')) {
430 loadTemplate('admin_del_network_types', false, $content);
433 debug_report_bug('Cannot detect edit/del.');
436 // Don't display the list/add new form
437 $GLOBALS['network_display'] = false;
439 // Nothing selected/found
440 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_FOUND'));
445 // Changes given networks
446 function doAdminNetworkProcessChangenetworkForm () {
447 // Do we have selections?
448 if (countPostSelection() > 0) {
449 // By default nothing is updated
452 // Something has been selected, so start updating them
453 foreach (postRequestParameter('sel') as $id => $sel) {
454 // Update this entry?
457 $networkData = array();
459 // Transfer whole array, except 'sel'
460 foreach (postRequestArray() as $key => $entry) {
461 // Skip 'sel' and submit button
462 if (in_array($key, array('sel', 'change'))) continue;
464 // Do we have this enty?
465 if (!isset($entry[$id])) {
466 // Not found, needs fixing
467 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
471 $networkData[$key] = $entry[$id];
474 // Update the network data
475 $updated += doNetworkUpdateDataByArray($id, $networkData);
479 // Do we have updates?
482 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated));
485 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED'));
490 // Removes given networks
491 function doAdminNetworkProcessRemovenetworkForm () {
492 // Do we have selections?
493 if (countPostSelection() > 0) {
494 // By default nothing is removed
497 // Something has been selected, so start updating them
498 foreach (postRequestParameter('sel') as $id => $sel) {
499 // Update this entry?
502 $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id);
506 // Do we have removes?
509 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed));
512 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED'));
517 // Add a network type handler if not yet found
518 function doAdminNetworkProcessAddnetworktypeForm () {
519 // Is the network type handle already used with given network?
520 if (isNetworkTypeHandleValid(postRequestParameter('network_type_handle'), getRequestParameter('network'))) {
522 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_HANDLE_ALREADY_ADDED', postRequestParameter('network_type_handle')));
528 // Remove the 'ok' part
529 unsetPostRequestParameter('ok');
532 setPostRequestParameter('network_id', getRequestParameter('network'));
534 // Is network_type_banner_url set?
535 if (postRequestParameter('network_type_banner_url') == '') {
536 // Remove empty value to get a NULL for an optional entry
537 unsetPostRequestParameter('network_type_banner_url');
540 // Add the whole request to database
541 SQL_QUERY("INSERT INTO
542 `{?_MYSQL_PREFIX?}_network_types`
544 `" . implode('`,`', array_keys(postRequestArray())) . "`
546 '" . implode("','", array_values(postRequestArray())) . "'
547 )", __FUNCTION__, __LINE__);
550 if (SQL_AFFECTEDROWS() == 1) {
551 // Successfully added
552 loadTemplate('admin_network_type_added', false, postRequestArray());
555 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_NOT_ADDED', postRequestParameter('network_type_handle')));
559 // Changes given network type handlers
560 function doAdminNetworkProcessChangenetworktypeForm () {
561 // Do we have selections?
562 if (countPostSelection() > 0) {
563 // By default nothing is updated
566 // Something has been selected, so start updating them
567 foreach (postRequestParameter('sel') as $id => $sel) {
568 // Update this entry?
571 $networkTypeData = array();
573 // Transfer whole array, except 'sel'
574 foreach (postRequestArray() as $key => $entry) {
575 // Skip 'sel' and submit button
576 if (in_array($key, array('sel', 'change'))) continue;
578 // Do we have this enty?
579 if (!isset($entry[$id])) {
580 // Not found, needs fixing
581 debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.');
585 $networkTypeData[$key] = $entry[$id];
588 // Update the network data
589 $updated += doNetworkUpdateTypeByArray($id, $networkTypeData);
593 // Do we have updates?
596 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_UPDATED', $updated));
599 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_CHANGED'));
604 // Removes given network type handlers
605 function doAdminNetworkProcessRemovenetworktypeForm () {
606 // Do we have selections?
607 if (countPostSelection() > 0) {
608 // By default nothing is removed
611 // Something has been selected, so start updating them
612 foreach (postRequestParameter('sel') as $id => $sel) {
613 // Update this entry?
616 $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $id);
620 // Do we have removes?
623 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPES_REMOVED', $removed));
626 loadTemplate('admin_settings_unsaved', false, getMessage('ADMIN_NETWORK_TYPES_NOTHING_REMOVED'));
631 // Adds a request parameter to given network and type
632 function doAdminNetworkProcessAddnetworkparamForm () {
633 // Is the request parameter already used with given network?
634 if (isNetworkRequestParameterValid(postRequestParameter('request_param_key'), postRequestParameter('network_type_id'), getRequestParameter('network'))) {
636 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED', postRequestParameter('request_param_key')));
642 // Remove the 'ok' part
643 unsetPostRequestParameter('ok');
646 setPostRequestParameter('network_id', getRequestParameter('network'));
648 // Is request_param_default set?
649 if (postRequestParameter('request_param_default') == '') {
650 // Remove empty value to get a NULL for an optional entry
651 unsetPostRequestParameter('request_param_default');
654 // Add the whole request to database
655 SQL_QUERY("INSERT INTO
656 `{?_MYSQL_PREFIX?}_network_request_params`
658 `" . implode('`,`', array_keys(postRequestArray())) . "`
660 '" . implode("','", array_values(postRequestArray())) . "'
661 )", __FUNCTION__, __LINE__);
664 if (SQL_AFFECTEDROWS() == 1) {
665 // Successfully added
666 loadTemplate('admin_network_request_param_added', false, postRequestArray());
669 loadTemplate('admin_settings_unsaved', false, getMaskedMessage('ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED', postRequestParameter('request_param_key')));