X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=inline;f=inc%2Flibs%2Fnetwork_functions.php;h=2a52309768c51b950fef9e855b15ce58e1e3fa72;hb=f2edf32502b2774dc17ef419a2e6a1c60e361c0a;hp=2f6fd916d37d5509378397588a9e7914ab63094d;hpb=5d10272969b47744efad309ba14e889fb68165c3;p=mailer.git diff --git a/inc/libs/network_functions.php b/inc/libs/network_functions.php index 2f6fd916d3..2a52309768 100644 --- a/inc/libs/network_functions.php +++ b/inc/libs/network_functions.php @@ -14,11 +14,10 @@ * $Date:: $ * * $Tag:: 0.2.1-FINAL $ * * $Author:: $ * - * Needs to be in all Files and every File needs "svn propset * - * svn:keywords Date Revision" (autoprobset!) at least!!!!!! * * -------------------------------------------------------------------- * * Copyright (c) 2003 - 2009 by Roland Haeder * - * For more information visit: http://www.mxchange.org * + * Copyright (c) 2009 - 2012 by Mailer Developer Team * + * For more information visit: http://mxchange.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -41,52 +40,141 @@ if (!defined('__SECURITY')) { die(); } // END - if -// Handle a (maybe) sent form here -function doNetworkHandleForm () { - // Was the form sent? - if ((isFormSent()) || (isPostRequestElementSet('edit')) || (isPostRequestElementSet('del')) || (isPostRequestElementSet('change')) || (isPostRequestElementSet('remove'))) { - // Do we have a 'do'? - if (isGetRequestElementSet('do')) { - // Process the request - doAdminNetworkProcessForm(); - } else { - // No 'do' found - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_DO_404')); - } +// Private setter for current network id +function setCurrentNetworkId ($networkId) { + $GLOBALS['current_network_id'] = bigintval($networkId); +} + +// Private getter for current network id +function getCurrentNetworkId () { + return $GLOBALS['current_network_id']; +} + +// Detects if a supported form has been sent +function detectNetworkProcessForm () { + // 'do' must be provided in URL + if (!isGetRequestElementSet('do')) { + // Not provided! + reportBug(__FUNCTION__, __LINE__, 'No "do" has been provided. Please fix your templates.'); + } // END - if + + // Default is invalid + $GLOBALS['network_form_name'] = 'invalid'; + + // Now search all valid + foreach (array('ok', 'edit', 'delete', 'do_edit', 'do_delete') as $form) { + // Is it detected + if (isFormSent($form)) { + // Use this form name + $GLOBALS['network_form_name'] = $form; + + // Abort loop + break; + } // END - if + } // END - foreach + + // Has the form being detected? + if ($GLOBALS['network_form_name'] == 'invalid') { + // Not supported + reportBug(__FUNCTION__, __LINE__, 'POST form could not be detected.'); } // END - if } -// Processes an admin form -function doAdminNetworkProcessForm () { - // Form really sent? - if ((!isFormSent()) && (!isPostRequestElementSet('edit')) && (!isPostRequestElementSet('del')) && (!isPostRequestElementSet('change')) && (!isPostRequestElementSet('remove'))) { - // Abort here - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_FORM_NOT_SENT')); - return; +// Handle a (maybe) sent form here +function doNetworkHandleForm () { + // Is there a form sent? + if (countRequestPost() > 0) { + // Detect sent POST form + detectNetworkProcessForm(); } elseif (!isGetRequestElementSet('do')) { - // No 'do' found - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_DO_404')); + // Skip any further requests return; } + // Process the request + doAdminNetworkProcessForm(); +} + +// Processes an admin form +function doAdminNetworkProcessForm () { // Create function name - $functionName = sprintf("doAdminNetworkProcess%sForm", ucfirst(strtolower(getRequestElement('do')))); + $functionName = sprintf("doAdminNetworkProcess%s", capitalizeUnderscoreString(getRequestElement('do'))); // Is the function valid? if (!function_exists($functionName)) { // Invalid function name - debug_report_bug('Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', false); + reportBug(__FUNCTION__, __LINE__, 'Invalid do ' . getRequestElement('do') . ', function ' . $functionName .' does not exist.', FALSE); } // END - if + // Init global arrays + $GLOBALS['network_types_disabled'] = array(); + // Call-back the method handling our request call_user_func($functionName); } -// Checks wether the (short) network name is already used (valid) +// Checks whether the (short) network name is already used (valid) function isNetworkNameValid ($name) { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$name])) { + // Does it exist? + $GLOBALS[__FUNCTION__][$name] = (countSumTotalData($name, 'network_data', 'network_id', 'network_short_name', TRUE) == 1); + } // END - if + + // Return result + return $GLOBALS[__FUNCTION__][$name]; +} + +// Checks whether the (short) named network is activated +function isNetworkActiveByShortName ($name) { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$name])) { + // Does it exist? + $GLOBALS[__FUNCTION__][$name] = ((isNetworkNameValid($name)) && (countSumTotalData($name, 'network_data', 'network_id', 'network_short_name', TRUE, " AND `network_active`='Y'") == 1)); + } // END - if + + // Return result + return $GLOBALS[__FUNCTION__][$name]; +} + +// Checks whether the network by given id is activated +function isNetworkActiveById ($networkId) { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$networkId])) { + // Does it exist? + $GLOBALS[__FUNCTION__][$networkId] = (countSumTotalData(bigintval($networkId), 'network_data', 'network_id', 'network_id', TRUE, " AND `network_active`='Y'") == 1); + } // END - if + + // Return result + return $GLOBALS[__FUNCTION__][$networkId]; +} + +// "Getter" for 'network_activated' column depending on current administrator's expert setting +function getNetworkActivatedColumn ($whereAnd = 'WHERE', $table = '') { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$whereAnd][$table])) { + // Default is exclude deactivated networks + $GLOBALS[__FUNCTION__][$whereAnd][$table] = ' ' . $whereAnd . ' ' . $table . "`network_active`='Y'"; + + // Is the export setting on? + if (isAdminsExpertSettingEnabled()) { + // Then allow all networks + $GLOBALS[__FUNCTION__][$whereAnd][$table] = ''; + } // END - if + } // END - if + + // Return cache + return $GLOBALS[__FUNCTION__][$whereAnd][$table]; +} + +// Checks whether the given network type is already used (valid) +function isNetworkTypeHandleValid ($type, $networkId) { // Query for it - $result = SQL_QUERY_ESC("SELECT `network_id` FROM `{?_MYSQL_PREFIX?}_network_data` WHERE `network_short_name`='%s' LIMIT 1", - array($name), __FUNCTION__, __LINE__); + $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handler`='%s' LIMIT 1", + array( + $networkId, + $type + ), __FUNCTION__, __LINE__); // Does it exist? $isValid = (SQL_NUMROWS($result) == 1); @@ -98,11 +186,54 @@ function isNetworkNameValid ($name) { return $isValid; } -// Checks wether the given network type is already used (valid) -function isNetworkTypeHandleValid ($type, $networkId) { +// Checks whether the given network request parameter is already used (valid) +function isNetworkRequestElementValid ($key, $type, $networkId) { + // Query for it + $result = SQL_QUERY_ESC("SELECT `network_request_param_id` FROM `{?_MYSQL_PREFIX?}_network_request_params` WHERE `network_id`=%s AND `network_type_id`=%s AND `network_request_param_key`='%s' LIMIT 1", + array( + $networkId, + $type, + $key + ), __FUNCTION__, __LINE__); + + // Does it exist? + $isValid = (SQL_NUMROWS($result) == 1); + + // Free result + SQL_FREERESULT($result); + + // Return result + return $isValid; +} + +// Checks whether the given vcheck request parameter is already used (valid) +function isNetworkVcheckElementValid ($key, $networkId) { + // Query for it + $result = SQL_QUERY_ESC("SELECT `network_vcheck_param_id` FROM `{?_MYSQL_PREFIX?}_network_vcheck_params` WHERE `network_id`=%s AND `network_vcheck_param_key`='%s' LIMIT 1", + array( + $networkId, + $key + ), __FUNCTION__, __LINE__); + + // Does it exist? + $isValid = (SQL_NUMROWS($result) == 1); + + // Free result + SQL_FREERESULT($result); + + // Return result + return $isValid; +} + +// Checks whether the given network API array translation +function isNetworkArrayTranslationValid ($key, $type, $networkId) { // Query for it - $result = SQL_QUERY_ESC("SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s AND `network_type_handle`='%s' LIMIT 1", - array($networkId, $type), __FUNCTION__, __LINE__); + $result = SQL_QUERY_ESC("SELECT `network_array_id` FROM `{?_MYSQL_PREFIX?}_network_array_translation` WHERE `network_id`=%s AND `network_type_id`=%s AND `network_array_index`='%s' LIMIT 1", + array( + $networkId, + $type, + $key + ), __FUNCTION__, __LINE__); // Does it exist? $isValid = (SQL_NUMROWS($result) == 1); @@ -115,102 +246,307 @@ function isNetworkTypeHandleValid ($type, $networkId) { } // "Getter" for a network's data by provided id number -function getNetworkDataById ($id) { +function getNetworkDataById ($networkId, $column = '') { + // Ids lower one are not accepted + if ($networkId < 1) { + // Not good, should be fixed + reportBug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.'); + } elseif ((!isNetworkActiveById($networkId)) && (!isAdminsExpertSettingEnabled())) { + // Do not load inactive network data + reportBug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is not active.'); + } + + // Set current network id + setCurrentNetworkId($networkId); + + // Is it cached? + if (!isset($GLOBALS['network_data'][$networkId])) { + // By default we have no data + $GLOBALS['network_data'][$networkId] = array(); + + // Query for the network data + $result = SQL_QUERY_ESC('SELECT + `network_id`, + `network_short_name`, + `network_title`, + `network_reflink`, + `network_data_separator`, + `network_row_separator`, + `network_request_type`, + `network_charset`, + `network_require_id_card`, + `network_query_amount` +FROM + `{?_MYSQL_PREFIX?}_network_data` +WHERE + `network_id`=%s +LIMIT 1', + array(bigintval($networkId)), __FUNCTION__, __LINE__); + + // Is there an entry? + if (SQL_NUMROWS($result) == 1) { + // Then get it + $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result); + } // END - if + + // Free result + SQL_FREERESULT($result); + } // END - if + + // Return result + if ((empty($column)) && (isset($GLOBALS['network_data'][$networkId]))) { + // Return array + return $GLOBALS['network_data'][$networkId]; + } elseif (isset($GLOBALS['network_data'][$networkId][$column])) { + // Return column + return $GLOBALS['network_data'][$networkId][$column]; + } + + // Return NULL + return NULL; +} + +// "Getter" for a network's data by provided type id number +function getNetworkDataByTypeId ($networkId, $column = '') { + // Ids lower one are not accepted + if ($networkId < 1) { + // Not good, should be fixed + reportBug(__FUNCTION__, __LINE__, 'Network type id ' . $networkId . ' is smaller than 1.'); + } elseif ((!isNetworkActiveById($networkId)) && (!isAdminsExpertSettingEnabled())) { + // Do not load inactive network data + reportBug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is not active.'); + } + + // Set current network id + setCurrentNetworkId($networkId); + + // Is it cached? + if (!isset($GLOBALS['network_data'][$networkId])) { + // By default we have no data + $GLOBALS['network_data'][$networkId] = array(); + + // Query for the network data + $result = SQL_QUERY_ESC('SELECT + d.`network_id`, + d.`network_short_name`, + d.`network_title`, + d.`network_reflink`, + d.`network_data_separator`, + d.`network_row_separator`, + d.`network_request_type`, + d.`network_charset`, + d.`network_require_id_card`, + d.`network_query_amount`, + t.`network_type_handler`, + t.`network_type_api_url`, + t.`network_type_click_url`, + t.`network_type_banner_url`, + t.`network_text_encoding` +FROM + `{?_MYSQL_PREFIX?}_network_data` AS d +LEFT JOIN + `{?_MYSQL_PREFIX?}_network_types` AS t +ON + d.`network_id`=t.`network_id` +WHERE + t.`network_type_id`=%s +LIMIT 1', + array(bigintval($networkId)), __FUNCTION__, __LINE__); + + // Is there an entry? + if (SQL_NUMROWS($result) == 1) { + // Then get it + $GLOBALS['network_data'][$networkId] = SQL_FETCHARRAY($result); + } // END - if + + // Free result + SQL_FREERESULT($result); + } // END - if + + // Return result + if (empty($column)) { + // Return array + return $GLOBALS['network_data'][$networkId]; + } else { + // Return column + return $GLOBALS['network_data'][$networkId][$column]; + } +} + +// "Getter" for a network type data by provided id number +function getNetworkTypeDataById ($networkTypeId) { // Ids lower one are not accepted - if ($id < 1) { + if ($networkTypeId < 1) { // Not good, should be fixed - debug_report_bug('Network id ' . $id . ' is smaller than 1.'); + reportBug(__FUNCTION__, __LINE__, 'Network type id ' . $networkTypeId . ' is smaller than 1.'); } // END - if // By default we have no data - $networkData = array(); + $GLOBALS['network_type_data'][$networkTypeId] = array(); // Query for the network data - $result = SQL_QUERY_ESC("SELECT - `network_id`, `network_short_name`, `network_title`, `network_reflink`, `network_data_seperator`, `network_row_seperator`, `network_request_type`, `network_charset` + $result = SQL_QUERY_ESC('SELECT + `network_type_id`, + `network_id`, + `network_type_handler`, + `network_type_api_url`, + `network_type_click_url`, + `network_type_banner_url` FROM - `{?_MYSQL_PREFIX?}_network_data` + `{?_MYSQL_PREFIX?}_network_types` WHERE - `network_id`=%s -LIMIT 1", - array(bigintval($id)), __FUNCTION__, __LINE__); + `network_type_id`=%s +LIMIT 1', + array(bigintval($networkTypeId)), __FUNCTION__, __LINE__); - // Do we have an entry? + // Is there an entry? if (SQL_NUMROWS($result) == 1) { // Then get it - $networkData = SQL_FETCHARRAY($result); + $GLOBALS['network_type_data'][$networkTypeId] = SQL_FETCHARRAY($result); } // END - if // Free result SQL_FREERESULT($result); // Return result - return $networkData; + return $GLOBALS['network_type_data'][$networkTypeId]; } -// "Getter" for a network type data by provided id number -function getNetworkTypeDataById ($id) { +// "Getter" for a network request parameter data by provided id number +function getNetworkRequestParamsDataById ($networkRequestId) { // Ids lower one are not accepted - if ($id < 1) { + if ($networkRequestId < 1) { // Not good, should be fixed - debug_report_bug('Network type id ' . $id . ' is smaller than 1.'); + reportBug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkRequestId . ' is smaller than 1.'); } // END - if // By default we have no data - $networkTypeData = array(); + $networkRequestData = array(); // Query for the network data - $result = SQL_QUERY_ESC("SELECT - `network_type_id`, `network_id`, `network_type_handle`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url` + $result = SQL_QUERY_ESC('SELECT + `network_request_param_id`, + `network_id`, + `network_type_id`, + `network_request_param_key`, + `network_request_param_value`, + `network_request_param_default` FROM - `{?_MYSQL_PREFIX?}_network_types` + `{?_MYSQL_PREFIX?}_network_request_params` WHERE - `network_type_id`=%s -LIMIT 1", - array(bigintval($id)), __FUNCTION__, __LINE__); + `network_request_param_id`=%s +LIMIT 1', + array(bigintval($networkRequestId)), __FUNCTION__, __LINE__); + + // Is there an entry? + if (SQL_NUMROWS($result) == 1) { + // Then get it + $networkRequestData = SQL_FETCHARRAY($result); + } // END - if + + // Free result + SQL_FREERESULT($result); + + // Return result + return $networkRequestData; +} + +// "Getter" for a network array translation data by provided id number +function getNetworkArrayTranslationsDataById ($networkTranslationId) { + // Ids lower one are not accepted + if ($networkTranslationId < 1) { + // Not good, should be fixed + reportBug(__FUNCTION__, __LINE__, 'Network array translation id ' . $networkTranslationId . ' is smaller than 1.'); + } // END - if + + // By default we have no data + $networkTranslationData = array(); + + // Query for the network data + $result = SQL_QUERY_ESC('SELECT + `network_array_id`, + `network_id`, + `network_type_id`, + `network_array_index`, + `network_array_sort` +FROM + `{?_MYSQL_PREFIX?}_network_array_translation` +WHERE + `network_array_id`=%s +LIMIT 1', + array(bigintval($networkTranslationId)), __FUNCTION__, __LINE__); - // Do we have an entry? + // Is there an entry? if (SQL_NUMROWS($result) == 1) { // Then get it - $networkTypeData = SQL_FETCHARRAY($result); + $networkTranslationData = SQL_FETCHARRAY($result); } // END - if // Free result SQL_FREERESULT($result); // Return result - return $networkTypeData; + return $networkTranslationData; } // Updates given network (id) with data from array -function doNetworkUpdateDataByArray ($id, $networkData) { +function doNetworkUpdateDataByArray ($networkId, $networkData) { // Ids lower one are not accepted - if ($id < 1) { + if ($networkId < 1) { // Not good, should be fixed - debug_report_bug('Network id ' . $id . ' is smaller than 1.'); + reportBug(__FUNCTION__, __LINE__, 'Network id ' . $networkId . ' is smaller than 1.'); } // END - if // Just call our inner method - return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($id)), array(), false, false); + return adminSaveSettings($networkData, '_network_data', sprintf("`network_id`=%s", bigintval($networkId)), array(), FALSE, FALSE); } // Updates given network type handler (id) with data from array -function doNetworkUpdateTypeByArray ($id, $networkTypeData) { +function doNetworkUpdateTypeByArray ($networkTypeId, $networkTypeData) { + // Ids lower one are not accepted + if ($networkTypeId < 1) { + // Not good, should be fixed + reportBug(__FUNCTION__, __LINE__, 'Network type handler id ' . $networkTypeId . ' is smaller than 1.'); + } // END - if + + // Just call our inner method + return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($networkTypeId)), array(), FALSE, FALSE); +} + +// Updates given network request parameters (id) with data from array +function doNetworkUpdateParamsByArray ($networkParamsId, $networkParamsData) { + // Ids lower one are not accepted + if ($networkParamsId < 1) { + // Not good, should be fixed + reportBug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkParamsId . ' is smaller than 1.'); + } // END - if + + // Just call our inner method + return adminSaveSettings($networkParamsData, '_network_request_params', sprintf("`network_request_param_id`=%s", bigintval($networkParamsId)), array(), FALSE, FALSE); +} + +// Updates given network array translations (id) with data from array +function doNetworkUpdateArrayTranslationsByArray ($networkTranslationsId, $networkTranslationsData) { // Ids lower one are not accepted - if ($id < 1) { + if ($networkTranslationsId < 1) { // Not good, should be fixed - debug_report_bug('Network type handler id ' . $id . ' is smaller than 1.'); + reportBug(__FUNCTION__, __LINE__, 'Network request parameter id ' . $networkTranslationsId . ' is smaller than 1.'); } // END - if // Just call our inner method - return adminSaveSettings($networkTypeData, '_network_types', sprintf("`network_type_id`=%s", bigintval($id)), array(), false, false); + return adminSaveSettings($networkTranslationsData, '_network_array_translation', sprintf("`network_array_id`=%s", bigintval($networkTranslationsId)), array(), FALSE, FALSE); } // Removes given network entry function doAdminRemoveNetworkEntry ($table, $column, $id, $limit = 1) { // Remove the entry SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_network_%s` WHERE `%s`=%s LIMIT %s", - array($table, $column, $id, $limit), __FILE__, __LINE__); + array( + $table, + $column, + $id, + $limit + ), __FUNCTION__, __LINE__); // Return affected rows return SQL_AFFECTEDROWS(); @@ -222,27 +558,33 @@ function generateAdminNetworkList () { $content = ''; // Query for all networks - $result = SQL_QUERY("SELECT - `network_id`, `network_title` + $result = SQL_QUERY('SELECT + `network_id`, + `network_short_name`, + `network_title` FROM `{?_MYSQL_PREFIX?}_network_data` +' . getNetworkActivatedColumn() . ' ORDER BY - `network_title` ASC", __FILE__, __LINE__); + `network_short_name` ASC', __FUNCTION__, __LINE__); - // Do we have entries? - if (SQL_NUMROWS($result) > 0) { + // Are there entries? + if (!SQL_HASZERONUMS($result)) { // List all entries $rows = array(); while ($row = SQL_FETCHARRAY($result)) { // Is this valid, then add it - if ((is_array($row)) && (isset($row['network_id']))) $rows[] = $row; + if ((is_array($row)) && (isset($row['network_id']))) { + // Add entry + $rows[$row['network_id']] = $row; + } // END - if } // END - while // Generate the selection box - $content = generateSelectionBoxFromArray($rows, 'network', 'network_id', 'network_title'); + $content = generateSelectionBoxFromArray($rows, 'network_id', 'network_id', '', '', 'network'); } else { // Nothing selected - $content = loadTemplate('admin_settings_saved', false, getMessage('ADMIN_ENTRIES_404')); + $content = loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_ENTRIES_404--}'); } // Free the result @@ -252,189 +594,722 @@ ORDER BY return $content; } -//------------------------------------------------------------------------------ -// Call-back functions -//------------------------------------------------------------------------------ +// Generator (somewhat getter) for a list of network types for given network id +function generateAdminNetworkTypeList ($networkId) { + // Init content + $content = ''; -// Callback function to add new network -function doAdminNetworkProcessAddnetworkForm () { - // We can say here, the form is sent, so check if the network is already added - if (isNetworkNameValid(postRequestElement('network_short_name'))) { - // Already there - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_ALREADY_ADDED', postRequestElement('network_short_name'))); - return false; - } // END - if + // Query all types of this network + $result = SQL_QUERY_ESC('SELECT + `network_type_id`, + `network_type_handler` +FROM + `{?_MYSQL_PREFIX?}_network_types` +WHERE + `network_id`=%s + ' . getNetworkActivatedColumn('AND') . ' +ORDER BY + `network_type_handler` ASC', + array( + bigintval($networkId) + ), __FUNCTION__, __LINE__); - // Remove the 'ok' part - unsetPostRequestElement('ok'); + // Are there entries? + if (!SQL_HASZERONUMS($result)) { + // List all entries + $rows = array(); + while ($row = SQL_FETCHARRAY($result)) { + // Is this valid, then add it + if ((is_array($row)) && (isset($row['network_type_id']))) { + // Add entry + $rows[$row['network_type_id']] = $row; + } // END - if + } // END - while - // Add the whole request to database - SQL_QUERY("INSERT INTO - `{?_MYSQL_PREFIX?}_network_data` -( - `" . implode('`,`', array_keys(postRequestArray())) . "` -) VALUES ( - '" . implode("','", array_values(postRequestArray())) . "' -)", __FUNCTION__, __LINE__); + // Generate the selection box + $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id'); + } else { + // Nothing selected + $content = loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_ENTRIES_404--}'); + } - // Add the id for output only - setPostRequestElement('network_id', SQL_INSERTID()); + // Free the result + SQL_FREERESULT($result); - // Output message - if (SQL_AFFECTEDROWS() == 1) { - // Successfully added - loadTemplate('admin_network_added', false, postRequestArray()); + // Return content + return $content; +} + +// Generator (somewhat getter) for a list of network types for all types +function generateAdminDistinctNetworkTypeList () { + // Init content + $content = ''; + + // Query all types of this network + $result = SQL_QUERY('SELECT + t.`network_type_id`, + t.`network_type_handler`, + d.`network_title` +FROM + `{?_MYSQL_PREFIX?}_network_types` AS t +LEFT JOIN + `{?_MYSQL_PREFIX?}_network_data` AS d +ON + t.`network_id`=d.`network_id` +' . getNetworkActivatedColumn('WHERE', 'd') . ' +ORDER BY + d.`network_short_name` ASC, + t.`network_type_handler` ASC', __FUNCTION__, __LINE__); + + // Are there entries? + if (!SQL_HASZERONUMS($result)) { + // List all entries + $rows = array(); + while ($row = SQL_FETCHARRAY($result)) { + // Is this valid, then add it + if ((is_array($row)) && (isset($row['network_type_id']))) { + // Add entry + $rows[$row['network_type_id']] = $row; + } // END - if + } // END - while + + // Generate the selection box + $content = generateSelectionBoxFromArray($rows, 'network_type', 'network_type_id', '', '_title'); } else { - // Not added - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_DATA_NOT_ADDED', postRequestElement('network_short_name'))); + // Nothing selected + $content = loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_ENTRIES_404--}'); } -} -// Displays selected networks for editing -function doAdminNetworkProcessHandlenetworkForm () { - // Do we have selections? - if (countPostSelection() > 0) { - // Something has been selected, so start displaying one by one - $SW = 2; $OUT = ''; - foreach (postRequestElement('sel') as $id => $sel) { - // Is this selected? - if ($sel == 1) { - // Load this network's data - $networkData = getNetworkDataById($id); + // Free the result + SQL_FREERESULT($result); + //* DEBUG: */ die('
'.encodeEntities($content).''); - // Do we have found the network? - if (count($networkData) > 0) { - // Add color - $networkData['sw'] = $SW; - - if (isPostRequestElementSet('edit')) { - // Make selection box for network_request_type - $networkData['network_request_type'] = generateOptionList( - '/ARRAY/', - array('GET','POST'), - array( - getMessage('ADMIN_NETWORK_REQUEST_TYPE_GET'), - getMessage('ADMIN_NETWORK_REQUEST_TYPE_POST') - ), - $networkData['network_request_type'] - ); - - // Add row template for editing - $OUT .= loadTemplate('admin_edit_networks_row', true, $networkData); - } elseif (isPostRequestElementSet('del')) { - // Translate the request type - $networkData['network_request_type'] = getMessage('ADMIN_NETWORK_REQUEST_TYPE_' . $networkData['network_request_type']); + // Return content + return $content; +} - // Add row template for deleting - $OUT .= loadTemplate('admin_del_networks_row', true, $networkData); - } else { - // Problem! - debug_report_bug('Cannot detect edit/del.'); - } +// Generator (somewhat getter) for network type options +function generateNetworkTypeOptions ($networkId) { + // Is this an array, then we just came back from edit/delete actions + if (is_array($networkId)) { + // Set it as empty string + $networkId = ''; + } // END - if - // Switch colors - $SW = 3 - $SW; - } // END - if - } // END - if - } // END - foreach + // Is this cached? + if (!isset($GLOBALS[__FUNCTION__][$networkId])) { + // Generate output and cache it + $GLOBALS[__FUNCTION__][$networkId] = generateOptions( + 'network_types', + 'network_type_id', + 'network_type_handler', + $networkId, + '', + sprintf( + "WHERE `network_id`=%s" . getNetworkActivatedColumn('AND'), + bigintval(getRequestElement('network_id')) + ), + '', + 'translateNetworkTypeHandler' + ); + } // END - if - // If we have no rows, we don't need to display the edit form - if (!empty($OUT)) { - // Output main template - if (isPostRequestElementSet('edit')) { - loadTemplate('admin_edit_networks', false, $OUT); - } elseif (isPostRequestElementSet('del')) { - loadTemplate('admin_del_networks', false, $OUT); - } else { - // Problem! - debug_report_bug('Cannot detect edit/del.'); - } + // Return content + return $GLOBALS[__FUNCTION__][$networkId]; +} - // Don't display the list/add new form - $GLOBALS['network_display'] = false; - } else { - // Nothing selected/found - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_FOUND')); - } +// Generates an options list of all available (hard-coded) handlers +function generateNetworkTypesAvailableOptions ($defaultType = NULL) { + // Is it cached? + if (!isset($GLOBALS[__FUNCTION__][$defaultType])) { + // Generate list + $GLOBALS[__FUNCTION__][$defaultType] = generateOptions( + '/ARRAY/', + array( + 'banner', + 'banner_click', + 'banner_view', + 'button', + 'button_click', + 'button_view', + 'surfbar', + 'surfbar_click', + 'surfbar_view', + 'forcedbanner', + 'forcedtextlink', + 'textlink', + 'textlink_click', + 'textlink_view', + 'skybanner', + 'skybanner_click', + 'skybanner_view', + 'layer', + 'layer_click', + 'layer_view', + 'popup', + 'popdown', + 'textmail', + 'htmlmail', + 'lead', + 'sale', + 'payperactive', + 'pagepeel', + 'traffic', + 'signature', + 'signature_click', + 'signature_view', + ), + array(), + $defaultType, + '', '', + $GLOBALS['network_types_disabled'], + 'translateNetworkTypeHandler' + ); } // END - if + + // Return content + return $GLOBALS[__FUNCTION__][$defaultType]; } -// Handle network type form -function doAdminNetworkProcessHandlenetworktypeForm () { - // Do we have selections? - if (countPostSelection() > 0) { - // Load network data - $networkData = getNetworkDataById(getRequestElement('network')); +// Generates an options list of all available (hard-coded) text encoders +function generateNetworkTextEncodingAvailableOptions ($defaultEncoding = NULL) { + // Is it cached? + if (!isset($GLOBALS[__FUNCTION__][$defaultEncoding])) { + // Generate list + $GLOBALS[__FUNCTION__][$defaultEncoding] = generateOptions( + '/ARRAY/', + array( + 'NONE', + 'BASE64', + ), + array(), + $defaultEncoding, + '', '', + array(), + 'translateNetworkTextEncoding' + ); + } // END - if - // Something has been selected, so start displaying one by one - $SW = 2; $OUT = ''; - foreach (postRequestElement('sel') as $id => $sel) { - // Is this selected? - if ($sel == 1) { - // Load this network's data - $networkTypeData = getNetworkTypeDataById($id); + // Return content + return $GLOBALS[__FUNCTION__][$defaultEncoding]; +} - // Do we have found the network? - if (count($networkTypeData) > 0) { - // Add color - $networkTypeData['sw'] = $SW; +// Generates an options list (somewhat getter) for request keys +function generateNetworkRequestKeyOptions () { + // Is it cached? + if (!isset($GLOBALS[__FUNCTION__])) { + // Generate and cache it + $GLOBALS[__FUNCTION__] = generateOptions( + '/ARRAY/', + array( + 'affiliate_id', + 'sid', + 'hash', + 'password', + 'reload', + 'maximum_stay', + 'minimum_stay', + 'currency', + 'type', + 'remain', + 'reward', + 'size', + 'erotic', + 'extra', + 'country' + ), + array(), + '', + '', '', + $GLOBALS['network_request_params_disabled'], + 'translateNetworkRequestParameterKey' + ); + } // END - if - if (isPostRequestElementSet('edit')) { - // Add row template for deleting - $OUT .= loadTemplate('admin_edit_network_types_row', true, $networkTypeData); - } elseif (isPostRequestElementSet('del')) { - // Fix empty banner URL - if (trim($networkTypeData['network_type_banner_url']) == '') $networkTypeData['network_type_banner_url'] = '---'; + // Return content + return $GLOBALS[__FUNCTION__]; +} - // Add row template for deleting - $OUT .= loadTemplate('admin_del_network_types_row', true, $networkTypeData); - } else { - // Problem! - debug_report_bug('Cannot detect edit/del.'); - } +// Generates an options list for vcheck request keys +function generateNetworkVcheckKeyOptions () { + // Is it cached? + if (!isset($GLOBALS[__FUNCTION__])) { + // Generate and cache it + $GLOBALS[__FUNCTION__] = generateOptions( + '/ARRAY/', + array( + 'network_key', + 'sid', + 'payment', + 'remote_address', + 'campaign_id', + 'status', + 'reason', + 'type', + 'network_name', + 'extra_value1', + 'extra_value2', + 'extra_value3', + 'extra_value4', + ), + array(), + '', + '', '', + $GLOBALS['network_vcheck_params_disabled'], + 'translateNetworkVcheckParameterKey' + ); + } // END - if - // Switch colors - $SW = 3 - $SW; - } // END - if - } // END - if - } // END - foreach + // Return content + return $GLOBALS[__FUNCTION__]; +} - // If we have no rows, we don't need to display the edit form - if (!empty($OUT)) { - // Prepare content for template - $content = array( - 'rows' => $OUT, - 'network_data' => getNetworkDataById(getRequestElement('network')) - ); +// Generator (somewhat getter) for (return) array translation +function generateNetworkTranslationOptions ($default = '') { + // Is it cached? + if (!isset($GLOBALS[__FUNCTION__][$default])) { + // Generate and cache it + $GLOBALS[__FUNCTION__][$default] = generateOptions( + 'network_translations', + 'network_translation_id', + 'network_translation_name', + $default, + '', + '', + $GLOBALS['network_array_translation_disabled'], + 'translateNetworkTranslationName' + ); + } // END - if - // Output main template - if (isPostRequestElementSet('edit')) { - loadTemplate('admin_edit_network_types', false, $content); - } elseif (isPostRequestElementSet('del')) { - loadTemplate('admin_del_network_types', false, $content); - } else { - // Problem! - debug_report_bug('Cannot detect edit/del.'); - } + // Return content + return $GLOBALS[__FUNCTION__][$default]; +} - // Don't display the list/add new form - $GLOBALS['network_display'] = false; - } else { - // Nothing selected/found - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_TYPE_NOTHING_FOUND')); - } +// Generates an option list of request types +function generateNetworkRequestTypeOptions ($default = '') { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$default])) { + // Generate the list + $GLOBALS[__FUNCTION__][$default] = generateOptions( + '/ARRAY/', + array( + 'GET', + 'POST' + ), + array( + '{--ADMIN_NETWORK_REQUEST_TYPE_GET--}', + '{--ADMIN_NETWORK_REQUEST_TYPE_POST--}' + ), + $default + ); + } // END - if + + // Return cache + return $GLOBALS[__FUNCTION__][$default]; +} + +// Generates an option list of network_api_active +function generateNetworkApiActiveOptions ($default = '') { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$default])) { + // Generate the list + $GLOBALS[__FUNCTION__][$default] = generateYesNoOptions($default); + } // END - if + + // Return cache + return $GLOBALS[__FUNCTION__][$default]; +} + +// Translates 'translate_name' for e.g. templates +function translateNetworkTranslationName ($name) { + // Generate id + $messageId = 'ADMIN_NETWORK_TRANSLATE_' . strtoupper($name) . '_NAME'; + + // Is the message id there? + if (!isMessageIdValid($messageId)) { + // Not valid name + reportBug(__FUNCTION__, __LINE__, 'name=' . $name . ' is invalid.'); + } // END - if + + // Return message id + return '{--' . $messageId . '--}'; +} + +// Translates the network type handler (e.g. banner, paidmail) for templates +function translateNetworkTypeHandler ($type) { + // Generate id + $messageId = 'ADMIN_NETWORK_TYPE_HANDLER_' . strtoupper($type); + + // Is the message id there? + if (!isMessageIdValid($messageId)) { + // Not valid type + reportBug(__FUNCTION__, __LINE__, 'type=' . $type . ' is invalid.'); + } // END - if + + // Return message id + return '{--' . $messageId . '--}'; +} + +// Translates request type +function translateNetworkRequestType ($type) { + // Generate id + $messageId = 'ADMIN_NETWORK_REQUEST_TYPE_' . strtoupper($type) . ''; + + // Is the message id there? + if (!isMessageIdValid($messageId)) { + // Not valid type + reportBug(__FUNCTION__, __LINE__, 'type=' . $type . ' is invalid.'); + } // END - if + + // Return message id + return '{--' . $messageId . '--}'; +} + +// Translates request parameter +function translateNetworkRequestParameterKey ($param) { + // Generate id + $messageId = 'ADMIN_NETWORK_REQUEST_PARAMETER_' . strtoupper($param) . ''; + + // Is the message id there? + if (!isMessageIdValid($messageId)) { + // Not valid param + reportBug(__FUNCTION__, __LINE__, 'param=' . $param . ' is invalid.'); + } // END - if + + // Return message id + return '{--' . $messageId . '--}'; +} + +// Translates vheck request parameter +function translateNetworkVcheckParameterKey ($param) { + // Generate id + $messageId = 'ADMIN_NETWORK_VCHECK_PARAMETER_' . strtoupper($param) . ''; + + // Is the message id there? + if (!isMessageIdValid($messageId)) { + // Not valid param + reportBug(__FUNCTION__, __LINE__, 'param=' . $param . ' is invalid.'); + } // END - if + + // Return message id + return '{--' . $messageId . '--}'; +} + +// Translate text-encoding +function translateNetworkTextEncoding ($encoding) { + // Generate id + $messageId = 'ADMIN_NETWORK_TYPE_TEXT_ENCODING_' . strtoupper($encoding) . ''; + + // Is the message id there? + if (!isMessageIdValid($messageId)) { + // Not valid encoding + reportBug(__FUNCTION__, __LINE__, 'encoding=' . $encoding . ' is invalid.'); + } // END - if + + // Return message id + return '{--' . $messageId . '--}'; +} + +// Translates API index +function translateNetworkApiIndex ($index) { + // Is there cache? + if (!isset($GLOBALS['network_array_index'])) { + // Get an array of all API array indexes + $GLOBALS['network_array_index'] = array(); + + // Get all entries + $result = SQL_QUERY('SELECT + `network_array_id`, + `network_array_index`, + `network_translation_name` +FROM + `{?_MYSQL_PREFIX?}_network_array_translation` +INNER JOIN + `{?_MYSQL_PREFIX?}_network_translations` +ON + `network_array_index`=`network_translation_id` +ORDER BY + `network_array_sort` ASC', __FUNCTION__, __LINE__); + + // Are there entries? + if (!SQL_HASZERONUMS($result)) { + // Get all entries + while ($row = SQL_FETCHARRAY($result)) { + // Add it to our global array + $GLOBALS['network_array_index'][$row['network_array_index']] = $row; + } // END - while + } // END - if + + // Free result + SQL_FREERESULT($result); + } // END - if + + // Default name is unknown + $name = 'unknown'; + + // Is the entry there? + if (isset($GLOBALS['network_array_index'][$index])) { + // Then get the name + $name = $GLOBALS['network_array_index'][$index]['network_translation_name']; + } // END - if + + // Return translation + return translateNetworkTranslationName($name); +} + +// Translates network API configuration status (see function isNetworkApiConfigured()) by given id +function translateNetworkApiConfiguredStatusById ($networkId) { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$networkId])) { + // By default it is not configured + $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_NOT_CONFIGURED--}'; + + // So is it configured? + if (!isNetworkActiveById($networkId)) { + // Network is not active + $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_NOT_ACTIVE--}'; + } elseif (isNetworkApiConfigured($networkId)) { + // Yes, it is + $GLOBALS[__FUNCTION__][$networkId] = '{--ADMIN_NETWORK_API_CONFIGURED--}'; + } // END - if + } // END - if + + // Return cache + return $GLOBALS[__FUNCTION__][$networkId]; +} + +// Checks if the given network is configured by looking its API configuration entry up +function isNetworkApiConfigured ($networkId) { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$networkId])) { + // Check for an entry in network_api_config + $GLOBALS[__FUNCTION__][$networkId] = (countSumTotalData( + bigintval($networkId), + 'network_api_config', + 'network_id', + 'network_id', + true + ) == 1); + } // END - if + + // Return cache + return $GLOBALS[__FUNCTION__][$networkId]; +} + +// Checks whether the given network type handler is configured +function isNetworkTypeHandlerConfigured ($networkId, $networkTypeId) { + // Is there cache? + if (!isset($GLOBALS[__FUNCTION__][$networkId][$networkTypeId])) { + // Determine it + $GLOBALS[__FUNCTION__][$networkId][$networkTypeId] = (countSumTotalData( + bigintval($networkTypeId), + 'network_types_config', + 'network_data_id', + 'network_type_id', + true, + sprintf(' AND `network_id`=%s', bigintval($networkId)) + ) == 1); + } // END - if + + // Return cache + return $GLOBALS[__FUNCTION__][$networkId][$networkTypeId]; +} + +// Handles the network-payment-check request +function handleNetworkPaymentCheckRequest () { + // @TODO Implement this function, don't forget to set HTTP status back to '200 OK' if everything went fine + reportBug(__FUNCTION__, __LINE__, 'Not yet implemented.'); +} + +//------------------------------------------------------------------------------ +// Call-back functions +//------------------------------------------------------------------------------ + +// Callback function to add new network +function doAdminNetworkProcessAddNetwork () { + // We can say here, the form is sent, so check if the network is already added + if (isNetworkNameValid(postRequestElement('network_short_name'))) { + // Already there + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_ALREADY_ADDED=' . postRequestElement('network_short_name') . '%}'); + return FALSE; + } elseif (!isNetworkActiveByShortName(postRequestElement('network_short_name'))) { + // Network is not active + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_NOT_ACTIVE=' . postRequestElement('network_short_name') . '%}'); + return FALSE; + } + + // Remove the 'ok' part + unsetPostRequestElement('ok'); + + // Add the whole request to database + SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_data'), __FUNCTION__, __LINE__); + + // Add the id for output only + setPostRequestElement('network_id', SQL_INSERTID()); + + // Output message + if (!SQL_HASZEROAFFECTED()) { + // Successfully added + loadTemplate('admin_network_added', FALSE, postRequestArray()); + } else { + // Not added + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_DATA_NOT_ADDED=' . postRequestElement('network_short_name') . '%}'); + } +} + +// Displays selected networks for editing +function doAdminNetworkProcessHandleNetworks () { + // Is there selections? + if (ifPostContainsSelections()) { + // Something has been selected, so start displaying one by one + $OUT = ''; + foreach (postRequestElement('sel') as $networkId => $sel) { + // Is this selected? + if ($sel == 1) { + // Load this network's data + $networkData = getNetworkDataById($networkId); + + // Is there found the network? + if (count($networkData) > 0) { + // Add row template with given form name + $OUT .= loadTemplate('admin_' . $GLOBALS['network_form_name'] . '_networks_row', TRUE, $networkData); + } // END - if + } // END - if + } // END - foreach + + // If we have no rows, we don't need to display the edit form + if (!empty($OUT)) { + // Output main template + loadTemplate('admin_' . $GLOBALS['network_form_name'] . '_networks', FALSE, $OUT); + + // Don't display the list/add new form + $GLOBALS['network_display'] = FALSE; + } else { + // Nothing selected/found + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_NOTHING_FOUND--}'); + } + } // END - if +} + +// Handle network type form +function doAdminNetworkProcessHandleNetworkTypes () { + // Is there selections? + if (ifPostContainsSelections()) { + // Load network data + $networkData = getNetworkDataById(getRequestElement('network_id')); + + // Something has been selected, so start displaying one by one + $OUT = ''; + foreach (postRequestElement('sel') as $networkId => $sel) { + // Is this selected? + if ($sel == 1) { + // Load this network's data + $networkTypeData = getNetworkTypeDataById($networkId); + + // Is there found the network? + if (count($networkTypeData) > 0) { + if (isFormSent('edit')) { + // Add row template for deleting + $OUT .= loadTemplate('admin_edit_network_types_row', TRUE, $networkTypeData); + } elseif (isFormSent('delete')) { + // Add row template for deleting + $OUT .= loadTemplate('admin_delete_network_types_row', TRUE, $networkTypeData); + } else { + // Problem! + reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.'); + } + } // END - if + } // END - if + } // END - foreach + + // If we have no rows, we don't need to display the edit form + if (!empty($OUT)) { + // Output main template + if (isFormSent('edit')) { + loadTemplate('admin_edit_network_types', FALSE, $OUT); + } elseif (isFormSent('delete')) { + loadTemplate('admin_delete_network_types', FALSE, $OUT); + } else { + // Problem! + reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.'); + } + + // Don't display the list/add new form + $GLOBALS['network_display'] = FALSE; + } else { + // Nothing selected/found + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_TYPE_HANDLER_NOTHING_FOUND--}'); + } + } // END - if +} + +// Handle network request parameter form +function doAdminNetworkProcessHandleRequestParams () { + // Is there selections? + if (ifPostContainsSelections()) { + // Init cache array + $GLOBALS['network_request_params_disabled'] = array(); + + // Load network data + $networkData = getNetworkDataById(getRequestElement('network_id')); + + // Something has been selected, so start displaying one by one + $OUT = ''; + foreach (postRequestElement('sel') as $networkId => $sel) { + // Is this selected? + if ($sel == 1) { + // Load this network's data + $networkRequestData = getNetworkRequestParamsDataById($networkId); + + // Is there found the network? + if (count($networkRequestData) > 0) { + if (isFormSent('edit')) { + // Add row template for deleting + $OUT .= loadTemplate('admin_edit_network_request_params_row', TRUE, $networkRequestData); + } elseif (isFormSent('delete')) { + // Get type data + $networkRequestData['network_type_data'] = getNetworkTypeDataById($networkRequestData['network_type_id']); + + // Add row template for deleting + $OUT .= loadTemplate('admin_delete_network_request_params_row', TRUE, $networkRequestData); + } else { + // Problem! + reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.'); + } + } // END - if + } // END - if + } // END - foreach + + // If we have no rows, we don't need to display the edit form + if (!empty($OUT)) { + // Output main template + if (isFormSent('edit')) { + loadTemplate('admin_edit_network_request_params', FALSE, $OUT); + } elseif (isFormSent('delete')) { + loadTemplate('admin_delete_network_request_params', FALSE, $OUT); + } else { + // Problem! + reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.'); + } + + // Don't display the list/add new form + $GLOBALS['network_display'] = FALSE; + } else { + // Nothing selected/found + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_FOUND--}'); + } } // END - if } // Changes given networks -function doAdminNetworkProcessChangenetworkForm () { - // Do we have selections? - if (countPostSelection() > 0) { +function doAdminNetworkProcessChangeNetworks () { + // Is there selections? + if (ifPostContainsSelections()) { // By default nothing is updated $updated = 0; // Something has been selected, so start updating them - foreach (postRequestElement('sel') as $id => $sel) { + foreach (postRequestElement('sel') as $networkId => $sel) { // Update this entry? if ($sel == 1) { // Init data array @@ -443,106 +1318,108 @@ function doAdminNetworkProcessChangenetworkForm () { // Transfer whole array, except 'sel' foreach (postRequestArray() as $key => $entry) { // Skip 'sel' and submit button - if (in_array($key, array('sel', 'change'))) continue; + if (in_array($key, array('sel', 'do_edit'))) { + continue; + } // END - if - // Do we have this enty? - if (!isset($entry[$id])) { + // Is there this enty? + if (!isset($entry[$networkId])) { // Not found, needs fixing - debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.'); + reportBug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.'); } // END - if // Add this entry - $networkData[$key] = $entry[$id]; + $networkData[$key] = $entry[$networkId]; } // END - foreach // Update the network data - $updated += doNetworkUpdateDataByArray($id, $networkData); + $updated += doNetworkUpdateDataByArray($networkId, $networkData); } // END - if } // END - foreach - // Do we have updates? + // Is there updates? if ($updated > 0) { // Updates done - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_UPDATED', $updated)); + displayMessage('{%message,ADMIN_NETWORK_UPDATED=' . $updated . '%}'); } else { // Nothing changed - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_CHANGED')); + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_NOTHING_CHANGED--}'); } } // END - if } // Removes given networks -function doAdminNetworkProcessRemovenetworkForm () { - // Do we have selections? - if (countPostSelection() > 0) { +function doAdminNetworkProcessRemoveNetworks () { + // Is there selections? + if (ifPostContainsSelections()) { // By default nothing is removed $removed = 0; // Something has been selected, so start updating them - foreach (postRequestElement('sel') as $id => $sel) { + foreach (postRequestElement('sel') as $networkId => $sel) { // Update this entry? if ($sel == 1) { // Remove this entry - $removed += doAdminRemoveNetworkEntry('data', 'network_id', $id); + $removed += doAdminRemoveNetworkEntry('data', 'network_id', $networkId); } // END - if } // END - foreach - // Do we have removes? + // Is there removes? if ($removed > 0) { // Removals done - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_REMOVED', $removed)); + displayMessage('{%message,ADMIN_NETWORK_REMOVED=' . $removed . '%}'); } else { // Nothing removed - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_NOTHING_REMOVED')); + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_NOTHING_REMOVED--}'); } } // END - if } // Add a network type handler if not yet found -function doAdminNetworkProcessAddnetworktypeForm () { +function doAdminNetworkProcessAddNetworkType () { // Is the network type handle already used with given network? - if (isNetworkTypeHandleValid(postRequestElement('network_type_handle'), getRequestElement('network'))) { + if (isNetworkTypeHandleValid(postRequestElement('network_type_handler'), getRequestElement('network_id'))) { // Already added - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_HANDLE_ALREADY_ADDED', postRequestElement('network_type_handle'))); + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_TYPE_HANDLER_ALREADY_ADDED=' . postRequestElement('network_type_handler') . '%}'); // ... so abort here - return false; + return FALSE; } // END - if // Remove the 'ok' part unsetPostRequestElement('ok'); // Add id - setPostRequestElement('network_id', getRequestElement('network')); + setPostRequestElement('network_id', bigintval(getRequestElement('network_id'))); + + // Is network_type_banner_url set? + if (postRequestElement('network_type_banner_url') == '') { + // Remove empty value to get a NULL for an optional entry + unsetPostRequestElement('network_type_banner_url'); + } // END - if // Add the whole request to database - SQL_QUERY("INSERT INTO - `{?_MYSQL_PREFIX?}_network_types` -( - `" . implode('`,`', array_keys(postRequestArray())) . "` -) VALUES ( - '" . implode("','", array_values(postRequestArray())) . "' -)", __FUNCTION__, __LINE__); + SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_types'), __FUNCTION__, __LINE__); // Output message - if (SQL_AFFECTEDROWS() == 1) { + if (!SQL_HASZEROAFFECTED()) { // Successfully added - loadTemplate('admin_network_type_added', false, postRequestArray()); + loadTemplate('admin_network_type_added', FALSE, postRequestArray()); } else { // Not added - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_NOT_ADDED', postRequestElement('network_type_handle'))); + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_TYPE_HANDLER_NOT_ADDED=' . postRequestElement('network_type_handler') . '%}'); } } // Changes given network type handlers -function doAdminNetworkProcessChangenetworktypeForm () { - // Do we have selections? - if (countPostSelection() > 0) { +function doAdminNetworkProcessChangeHandlerTypes () { + // Is there selections? + if (ifPostContainsSelections()) { // By default nothing is updated $updated = 0; // Something has been selected, so start updating them - foreach (postRequestElement('sel') as $id => $sel) { + foreach (postRequestElement('sel') as $networkId => $sel) { // Update this entry? if ($sel == 1) { // Init data array @@ -551,59 +1428,961 @@ function doAdminNetworkProcessChangenetworktypeForm () { // Transfer whole array, except 'sel' foreach (postRequestArray() as $key => $entry) { // Skip 'sel' and submit button - if (in_array($key, array('sel', 'change'))) continue; + if (in_array($key, array('sel', 'do_edit'))) { + continue; + } // END - if - // Do we have this enty? - if (!isset($entry[$id])) { + // Is there this enty? + if (!isset($entry[$networkId])) { // Not found, needs fixing - debug_report_bug('No entry in key=' . $key . ', id=' . $id . ' found.'); + reportBug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.'); + } // END - if + + // Fix empty network_type_banner_url to NULL + if (($key == 'network_type_banner_url') && (trim($entry[$networkId]) == '')) { + // Set it to NULL + $entry[$networkId] = NULL; } // END - if // Add this entry - $networkTypeData[$key] = $entry[$id]; + $networkTypeData[$key] = $entry[$networkId]; } // END - foreach // Update the network data - $updated += doNetworkUpdateTypeByArray($id, $networkTypeData); + $updated += doNetworkUpdateTypeByArray($networkId, $networkTypeData); } // END - if } // END - foreach - // Do we have updates? + // Is there updates? if ($updated > 0) { // Updates done - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_UPDATED', $updated)); + displayMessage('{%message,ADMIN_NETWORK_TYPE_HANDLER_UPDATED=' . $updated . '%}'); } else { // Nothing changed - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_TYPE_NOTHING_CHANGED')); + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_TYPE_HANDLER_NOTHING_CHANGED--}'); + } + } // END - if +} + +// Changes given network request parameters +function doAdminNetworkProcessChangeRequestParams () { + // Is there selections? + if (ifPostContainsSelections()) { + // By default nothing is updated + $updated = 0; + + // Something has been selected, so start updating them + foreach (postRequestElement('sel') as $networkId => $sel) { + // Update this entry? + if ($sel == 1) { + // Init data array + $networkParamsData = array(); + + // Transfer whole array, except 'sel' + foreach (postRequestArray() as $key => $entry) { + // Skip 'sel' and submit button + if (in_array($key, array('sel', 'do_edit'))) { + continue; + } // END - if + + // Is there this enty? + if (!isset($entry[$networkId])) { + // Not found, needs fixing + reportBug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.'); + } // END - if + + // Fix empty network_request_param_default to NULL + if (($key == 'network_request_param_default') && (trim($entry[$networkId]) == '')) { + // Set it to NULL + $entry[$networkId] = NULL; + } // END - if + + // Add this entry + $networkParamsData[$key] = $entry[$networkId]; + } // END - foreach + + // Update the network data + $updated += doNetworkUpdateParamsByArray($networkId, $networkParamsData); + } // END - if + } // END - foreach + + // Is there updates? + if ($updated > 0) { + // Updates done + displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_UPDATED=' . $updated . '%}'); + } else { + // Nothing changed + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_CHANGED--}'); + } + } // END - if +} + +// Changes given network array translations +function doAdminNetworkProcessChangeArrayTranslations () { + // Is there selections? + if (ifPostContainsSelections()) { + // By default nothing is updated + $updated = 0; + + // Something has been selected, so start updating them + foreach (postRequestElement('sel') as $networkId => $sel) { + // Update this entry? + if ($sel == 1) { + // Init data array + $networkTranslationsData = array(); + + // Transfer whole array, except 'sel' + foreach (postRequestArray() as $key => $entry) { + // Skip 'sel' and submit button + if (in_array($key, array('sel', 'do_edit'))) { + continue; + } // END - if + + // Is there this enty? + if (!isset($entry[$networkId])) { + // Not found, needs fixing + reportBug(__FUNCTION__, __LINE__, 'No entry in key=' . $key . ', id=' . $networkId . ' found.'); + } // END - if + + // Fix empty network_request_param_default to NULL + if (($key == 'network_request_param_default') && (trim($entry[$networkId]) == '')) { + // Set it to NULL + $entry[$networkId] = NULL; + } // END - if + + // Add this entry + $networkTranslationsData[$key] = $entry[$networkId]; + } // END - foreach + + // Update the network data + $updated += doNetworkUpdateArrayTranslationsByArray($networkId, $networkTranslationsData); + } // END - if + } // END - foreach + + // Is there updates? + if ($updated > 0) { + // Updates done + displayMessage('{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_UPDATED=' . $updated . '%}'); + } else { + // Nothing changed + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_ARRAY_TRANSLATION_NOTHING_CHANGED--}'); } } // END - if } // Removes given network type handlers -function doAdminNetworkProcessRemovenetworktypeForm () { - // Do we have selections? - if (countPostSelection() > 0) { +function doAdminNetworkProcessRemoveNetworkTypes () { + // Is there selections? + if (ifPostContainsSelections()) { + // By default nothing is removed + $removed = 0; + + // Something has been selected, so start updating them + foreach (postRequestElement('sel') as $networkId => $sel) { + // Update this entry? + if ($sel == 1) { + // Remove this entry + $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $networkId); + } // END - if + } // END - foreach + + // Is there removes? + if ($removed > 0) { + // Removals done + displayMessage('{%message,ADMIN_NETWORK_TYPE_HANDLER_REMOVED=' . $removed . '%}'); + } else { + // Nothing removed + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_TYPE_HANDLER_NOTHING_REMOVED--}'); + } + } // END - if +} + +// Removes given network request parameters +function doAdminNetworkProcessRemoveNetworkRequestParams () { + // Is there selections? + if (ifPostContainsSelections()) { + // By default nothing is removed + $removed = 0; + + // Something has been selected, so start updating them + foreach (postRequestElement('sel') as $networkId => $sel) { + // Update this entry? + if ($sel == 1) { + // Remove this entry + $removed += doAdminRemoveNetworkEntry('request_params', 'network_request_param_id', $networkId); + } // END - if + } // END - foreach + + // Is there removes? + if ($removed > 0) { + // Removals done + displayMessage('{%message,ADMIN_NETWORK_REQUEST_PARAMETER_REMOVED=' . $removed . '%}'); + } else { + // Nothing removed + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_REMOVED--}'); + } + } // END - if +} + +// Removes given network array translations +function doAdminNetworkProcessRemoveNetworkArrayTranslation () { + // Is there selections? + if (ifPostContainsSelections()) { // By default nothing is removed $removed = 0; // Something has been selected, so start updating them - foreach (postRequestElement('sel') as $id => $sel) { + foreach (postRequestElement('sel') as $networkId => $sel) { // Update this entry? if ($sel == 1) { // Remove this entry - $removed += doAdminRemoveNetworkEntry('types', 'network_type_id', $id); + $removed += doAdminRemoveNetworkEntry('array_translation', 'network_array_id', $networkId); } // END - if } // END - foreach - // Do we have removes? + // Is there removes? if ($removed > 0) { // Removals done - loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_NETWORK_TYPE_REMOVED', $removed)); + displayMessage('{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_REMOVED=' . $removed . '%}'); } else { // Nothing removed - loadTemplate('admin_settings_saved', false, getMessage('ADMIN_NETWORK_TYPE_NOTHING_REMOVED')); + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_ARRAY_TRANSLATION_NOTHING_REMOVED--}'); + } + } // END - if +} + +// Adds a request parameter to given network and type +function doAdminNetworkProcessAddRequestParam () { + // Is the request parameter already used with given network? + if (isNetworkRequestElementValid(postRequestElement('network_request_param_key'), postRequestElement('network_type_id'), getRequestElement('network_id'))) { + // Already added + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_ALREADY_ADDED=' . postRequestElement('network_request_param_key') . '%}'); + + // ... so abort here + return FALSE; + } // END - if + + // Remove the 'ok' part + unsetPostRequestElement('ok'); + + // Add id + setPostRequestElement('network_id', bigintval(getRequestElement('network_id'))); + + // Is network_request_param_default set? + if (postRequestElement('network_request_param_default') == '') { + // Remove empty value to get a NULL for an optional entry + unsetPostRequestElement('network_request_param_default'); + } // END - if + + // Add the whole request to database + SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_request_params'), __FUNCTION__, __LINE__); + + // Output message + if (!SQL_HASZEROAFFECTED()) { + // Successfully added + loadTemplate('admin_network_request_param_added', FALSE, postRequestArray()); + } else { + // Not added + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_REQUEST_PARAMETER_NOT_ADDED=' . postRequestElement('network_request_param_key') . '%}'); + } +} + +// Adds a vheck request parameter to given network +function doAdminNetworkProcessAddVcheckParam () { + // Is the request parameter already used with given network? + if (isNetworkVcheckElementValid(postRequestElement('network_vcheck_param_key'), getRequestElement('network_id'))) { + // Already added + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_VCHECK_PARAMETER_ALREADY_ADDED=' . postRequestElement('network_vcheck_param_key') . '%}'); + + // ... so abort here + return FALSE; + } // END - if + + // Remove the 'ok' part + unsetPostRequestElement('ok'); + + // Add id + setPostRequestElement('network_id', bigintval(getRequestElement('network_id'))); + + // Is network_vcheck_param_default set? + if (postRequestElement('network_vcheck_param_default') == '') { + // Remove empty value to get a NULL for an optional entry + unsetPostRequestElement('network_vcheck_param_default'); + } // END - if + + // Add the whole vcheck to database + SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_vcheck_params'), __FUNCTION__, __LINE__); + + // Output message + if (!SQL_HASZEROAFFECTED()) { + // Successfully added + loadTemplate('admin_network_vcheck_param_added', FALSE, postRequestArray()); + } else { + // Not added + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_VCHECK_PARAMETER_NOT_ADDED=' . postRequestElement('network_vcheck_param_key') . '%}'); + } +} + +// Adds a API response array entry +function doAdminNetworkProcessAddNetworkArrayTranslation () { + // Is the request parameter already used with given network? + if (isNetworkArrayTranslationValid(postRequestElement('network_array_index'), postRequestElement('network_type_id'), getRequestElement('network_id'))) { + // Already added + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_ALREADY_ADDED=' . postRequestElement('network_array_index') . '%}'); + + // ... so abort here + return FALSE; + } // END - if + + // Remove the 'ok' part + unsetPostRequestElement('ok'); + + // Add id + setPostRequestElement('network_id', bigintval(getRequestElement('network_id'))); + + // Add sorting + setPostRequestElement('network_array_sort', (countSumTotalData( + bigintval(postRequestElement('network_id')), + 'network_array_translation', + 'network_array_id', + 'network_id', + true, + sprintf(" AND `network_type_id`=%s", bigintval(postRequestElement('network_type_id'))) + ) + 1)); + + // Add the whole request to database + SQL_QUERY(getInsertSqlFromArray(postRequestArray(), 'network_array_translation'), __FUNCTION__, __LINE__); + + // Output message + if (!SQL_HASZEROAFFECTED()) { + // Successfully added + loadTemplate('admin_network_array_translation_added', FALSE, postRequestArray()); + } else { + // Not added + loadTemplate('admin_settings_unsaved', FALSE, '{%message,ADMIN_NETWORK_ARRAY_TRANSLATION_NOT_ADDED=' . postRequestElement('network_array_index') . '%}'); + } +} + +// Handle network array translation form +function doAdminNetworkProcessHandleArrayTranslations () { + // Is there selections? + if (ifPostContainsSelections()) { + // Init cache array + $GLOBALS['network_array_translation_disabled'] = array(); + + // Load network data + $networkData = getNetworkDataById(getRequestElement('network_id')); + + // Something has been selected, so start displaying one by one + $OUT = ''; + foreach (postRequestElement('sel') as $networkId => $sel) { + // Is this selected? + if ($sel == 1) { + // Load this network's data + $networkTranslationsData = getNetworkArrayTranslationsDataById($networkId); + + // Is there found the network? + if (count($networkTranslationsData) > 0) { + if (isFormSent('edit')) { + // Add row template for deleting + $OUT .= loadTemplate('admin_edit_network_array_translation_row', TRUE, $networkTranslationsData); + } elseif (isFormSent('delete')) { + // Get type data + $networkTranslationsData['network_type_data'] = getNetworkTypeDataById($networkTranslationsData['network_type_id']); + + // Add row template for deleting + $OUT .= loadTemplate('admin_delete_network_array_translation_row', TRUE, $networkTranslationsData); + } else { + // Problem! + reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.'); + } + } // END - if + } // END - if + } // END - foreach + + // If we have no rows, we don't need to display the edit form + if (!empty($OUT)) { + // Output main template + if (isFormSent('edit')) { + loadTemplate('admin_edit_network_array_translation', FALSE, $OUT); + } elseif (isFormSent('delete')) { + loadTemplate('admin_delete_network_array_translation', FALSE, $OUT); + } else { + // Problem! + reportBug(__FUNCTION__, __LINE__, 'Cannot detect edit/delete.'); + } + + // Don't display the list/add new form + $GLOBALS['network_display'] = FALSE; + } else { + // Nothing selected/found + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_NETWORK_REQUEST_PARAMETER_NOTHING_FOUND--}'); + } + } // END - if +} + +// Adds/update network API configuration +function doAdminNetworkProcessNetworkApiConfig () { + // Remove the 'ok' part + unsetPostRequestElement('ok'); + + // Add id + setPostRequestElement('network_id', bigintval(getRequestElement('network_id'))); + + // Is network_api_referral_button set? + if (postRequestElement('network_api_referral_button') == '') { + // Remove empty value to get a NULL for an optional entry + unsetPostRequestElement('network_api_referral_button'); + } // END - if + + // Is there already an entry? + if (isNetworkApiConfigured(getRequestElement('network_id'))) { + // Generate SQL query + $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_api_config', 'network_id', postRequestElement('network_id'), array('network_id')); + } else { + // Insert new entry + $SQL = getInsertSqlFromArray(postRequestArray(), 'network_api_config'); + } + + // Run the query + SQL_QUERY($SQL, __FUNCTION__, __LINE__); + + // Output message + if (!SQL_HASZEROAFFECTED()) { + // Successfully added + displayMessage('{--ADMIN_CONFIG_NETWORK_API_SAVED--}'); + } else { + // Not added + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_API_NOT_SAVED--}'); + } +} + +// Only adds network type configuration if not yet present +function doAdminNetworkProcessAddHandlerTypesConfig ($displayMessage = TRUE) { + // Remove the 'ok' part + unsetPostRequestElement('ok'); + + // Add both ids + setPostRequestElement('network_id', bigintval(getRequestElement('network_id'))); + setPostRequestElement('network_type_id', bigintval(getRequestElement('network_type_id'))); + + /* + * Some parameters are optional, at least one must be given so check a bunch + * of parameters. + */ + foreach (array('network_min_waiting_time', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic') as $element) { + // Is this element empty? + if (postRequestElement($element) == '') { + // Then unset it to get a NULL for optional parameter + unsetPostRequestElement($element); + } // END - if + } // END - foreach + + // Convert data in POST array + convertSelectionsToEpocheTimeInPostData($content, 'network_max_reload_time_ye', $skip); + + // Is there already an entry? + if (isNetworkTypeHandlerConfigured(getRequestElement('network_id'), getRequestElement('network_type_id'))) { + // This network type handler is already configured + displayMessage('{--ADMIN_NETWORK_HANDLER_TYPE_HANDLER_ALREADY_CONFIGURED--}'); + return; + } // END - if + + // Copy 'set all' and remove it from POST data + $setAll = (postRequestElement('set_all') === 'Y'); + unsetPostRequestElement('set_all'); + + // Shall we set for all? + if ($setAll === TRUE) { + // Get all handlers + $result = SQL_QUERY_ESC('SELECT `network_type_id` FROM `{?_MYSQL_PREFIX?}_network_types` WHERE `network_id`=%s ORDER BY `network_type_id` ASC', + array(bigintval(getRequestElement('network_id'))), __FUNCTION__, __LINE__); + + // Are there entries? + if (SQL_HASZERONUMS($result)) { + // No, then abort here + displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_SET_ALL_404--}'); + return; + } // END - if + + // Init number of rows + $numRows = 0; + + // Fetch all ids + while (list($typeId) = SQL_FETCHROW($result)) { + // Set it in GET data + setGetRequestElement('network_type_id', $typeId); + + // Call this function again + $numRows += doAdminNetworkProcessAddHandlerTypesConfig(FALSE); + } // END - while + + // Free result + SQL_FREERESULT($result); + + // Output message + if ($numRows > 0) { + // Something has been updated + displayMessage('{%message,ADMIN_CONFIG_NETWORK_HANDLER_TYPE_ALL_HANDLER_SAVED=' . bigintval($numRows) . '%}'); + } else { + // Nothing has been saved + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}'); + } + } else { + // Get SQL query for new entry + $SQL = getInsertSqlFromArray(postRequestArray(), 'network_types_config'); + + // Run the query + SQL_QUERY($SQL, __FUNCTION__, __LINE__); + + // Shall we display the message? + if ($displayMessage === TRUE) { + // Output message + if (!SQL_HASZEROAFFECTED()) { + // Successfully added + displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_SAVED--}'); + } else { + // Not added + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_SAVED--}'); + } + } else { + // Return amount of affected rows (1 or 2) + return SQL_AFFECTEDROWS(); + } + } +} + +// Only changes network type configuration if not yet present +function doAdminNetworkProcessEditHandlerTypesConfig ($displayMessage = TRUE) { + // Remove the 'ok' part + unsetPostRequestElement('ok'); + + /* + * Some parameters are optional, at least one must be given so check a bunch + * of parameters. + */ + foreach (array('network_min_waiting_time', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic') as $element) { + // Is this element empty? + if (postRequestElement($element) == '') { + // Then unset it to get a NULL for optional parameter + unsetPostRequestElement($element); + } // END - if + } // END - foreach + + // Convert time selections in POST data + convertSelectionsToEpocheTimeInPostData('network_max_reload_time_ye'); + + // Is there already an entry? + if (!isNetworkTypeHandlerConfigured(getRequestElement('network_id'), getRequestElement('network_type_id'))) { + // This network type handler is not configured + displayMessage('{--ADMIN_NETWORK_HANDLER_TYPE_HANDLER_NOT_CONFIGURED--}'); + return; + } // END - if + + // Copy 'set all' and remove it from POST data + $setAll = (postRequestElement('set_all') === 'Y'); + unsetPostRequestElement('set_all'); + + // Shall we set for all? + if ($setAll === TRUE) { + // Get all data entries + $result = SQL_QUERY_ESC('SELECT `network_data_id` FROM `{?_MYSQL_PREFIX?}_network_types_config` WHERE `network_id`=%s ORDER BY `network_type_id` ASC', + array(bigintval(getRequestElement('network_id'))), __FUNCTION__, __LINE__); + + // Are there entries? + if (SQL_HASZERONUMS($result)) { + // No, then abort here + displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_SET_ALL_404--}'); + return; + } // END - if + + // Init number of rows + $numRows = 0; + + // Fetch all ids + while (list($dataId) = SQL_FETCHROW($result)) { + // Set it in GET data + setPostRequestElement('network_data_id', $dataId); + + // Call this function again + $numRows += doAdminNetworkProcessEditHandlerTypesConfig(FALSE); + } // END - while + + // Free result + SQL_FREERESULT($result); + + // Output message + if ($numRows > 0) { + // Something has been updated + displayMessage('{%message,ADMIN_CONFIG_NETWORK_HANDLER_TYPE_ALL_HANDLER_SAVED=' . bigintval($numRows) . '%}'); + } else { + // Nothing has been saved + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}'); + } + } else { + // Get SQL query for new entry + $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_types_config', 'network_data_id', postRequestElement('network_data_id'), array('network_data_id')); + + // Run the query + SQL_QUERY($SQL, __FUNCTION__, __LINE__); + + // Shall we display the message? + if ($displayMessage === TRUE) { + // Output message + if (!SQL_HASZEROAFFECTED()) { + // Successfully added + displayMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_SAVED--}'); + } else { + // Not added + loadTemplate('admin_settings_unsaved', FALSE, '{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}'); + } + } else { + // Return amount of affected rows (1 or 2) + return SQL_AFFECTEDROWS(); } + } +} + +// Do expression code for this extension +function doExpressionNetwork ($data) { + // Construct replacer + $replacer = sprintf( + "{DQUOTE} . %s(%s, '%s') . {DQUOTE}", + $data['callback'], + $data['matches'][4][$data['key']], + $data['extra_func'] + ); + + // Replace %network_id% with the current network id + $replacer = str_replace('%network_id%', getCurrentNetworkId(), $replacer); + + // Replace the code + $code = replaceExpressionCode($data, $replacer); + + // Return it + return $code; +} + +// ---------------------------------------------------------------------------- +// Call-back functions for exporting data +// ---------------------------------------------------------------------------- + +// Callback function to export network tables +function doAdminNetworkProcessExport () { + // Init table with all valid what->table entries + $validExports = array( + // General network data + 'list_network_data' => 'data', + // Network type handler + 'list_network_types' => 'types', + // Network request parameter + 'list_network_request_params' => 'request_params', + // Vcheck request parameter + 'list_network_vcheck_params' => 'vcheck_params', + // Network API response array index translation + 'list_network_array_translation' => 'array_translation', + ); + + // Is the 'what' key valid? + if (!isset($validExports[getWhat()])) { + // Not valid + reportBug(__FUNCTION__, __LINE__, 'what=' . getWhat() . ' - not supported'); } // END - if + + // Generate call-back, some tables require to export not all columns + $callbackName = 'doAdminNetworkExport' . capitalizeUnderscoreString($validExports[getWhat()]); + + // Is the call-back function there? + if (!function_exists($callbackName)) { + // No, this is really bad + reportBug(__FUNCTION__, __LINE__, 'Invalid call-back function ' . $callbackName . ' detected.'); + } elseif (isset($GLOBALS[__FUNCTION__][$callbackName])) { + // Already called! + reportBug(__FUNCTION__, __LINE__, 'Double-call of export function ' . $callbackName . ' detected.'); + } + + // Call the function + call_user_func($callbackName); + + // Mark it as called + $GLOBALS[__FUNCTION__][$callbackName] = TRUE; + + // Don't display the list/add new form + $GLOBALS['network_display'] = FALSE; +} + +// Exports (and displays) the table 'network_data' +function doAdminNetworkExportData () { + // Query for all networks + $result = SQL_QUERY('SELECT + `network_short_name`, + `network_title`, + `network_reflink`, + `network_data_separator`, + `network_row_separator`, + `network_request_type`, + `network_charset`, + `network_require_id_card`, + `network_query_amount`, + `network_active` +FROM + `{?_MYSQL_PREFIX?}_network_data` +ORDER BY + `network_id` ASC', + __FUNCTION__, __LINE__); + + // Start an empty SQL query + $SQL = "INSERT INTO `{?_MYSQL_PREFIX?}_network_data` (`network_short_name`, `network_title`, `network_reflink`, `network_data_separator`, `network_row_separator`, `network_request_type`, `network_charset`, `network_require_id_card`, `network_query_amount`, `network_active`) VALUES\n"; + + // Load all entries + while ($content = SQL_FETCHARRAY($result)) { + // Add row + $SQL .= "('" . + $content['network_short_name'] . "', '" . + $content['network_title'] . "', '" . + $content['network_reflink'] . "', '" . + $content['network_data_separator'] . "', '" . + $content['network_row_separator'] . "', '" . + $content['network_request_type'] . "', '" . + $content['network_charset'] . "', '" . + $content['network_require_id_card'] . "', " . + $content['network_query_amount'] . ", '" . + $content['network_active'] . "'),\n"; + } // END - while + + // Remove last commata and close braces + $SQL = substr($SQL, 0, -2); + + // Free result + SQL_FREERESULT($result); + + // Output the SQL query + loadTemplate('admin_export_network_data', FALSE, $SQL); +} + +// Exports (and displays) the table 'network_types' +function doAdminNetworkExportTypes () { + // 'network_id' must be set + if (!isGetRequestElementSet('network_id')) { + // Only network handlers of one network will be exported per time + reportBug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.'); + } // END - if + + // Get all network types of given network + $result = SQL_QUERY_ESC('SELECT + `network_type_id`, + `network_id`, + `network_type_handler`, + `network_type_api_url`, + `network_type_click_url`, + `network_type_banner_url`, + `network_type_reload_time_unit`, + `network_text_encoding` +FROM + `{?_MYSQL_PREFIX?}_network_types` +WHERE + `network_id`=%s +ORDER BY + `network_type_id` ASC', + array( + bigintval(getRequestElement('network_id')) + ), __FUNCTION__, __LINE__); + + // Start an empty SQL query + $SQL = "INSERT INTO `{?_MYSQL_PREFIX?}_network_types` (`network_type_id`, `network_id`, `network_type_handler`, `network_type_api_url`, `network_type_click_url`, `network_type_banner_url`, `network_type_reload_time_unit`, `network_text_encoding`) VALUES\n"; + + // Load all entries + while ($content = SQL_FETCHARRAY($result)) { + // Add row + $SQL .= '(' . + $content['network_type_id'] . ', ' . + $content['network_id'] . ", '" . + $content['network_type_handler'] . "', '" . + $content['network_type_api_url'] . "', '" . + $content['network_type_click_url'] . "', "; + + // Is the column NULL? + if (is_null($content['network_type_banner_url'])) { + // Column is NULL + $SQL .= 'NULL'; + } else { + // Column is set + $SQL .= chr(39) . $content['network_type_banner_url'] . chr(39); + } + + // Add more + $SQL .= ",'" . $content['network_type_reload_time_unit'] . "','" . $content['network_text_encoding'] . "'),\n"; + } // END - while + + // Remove last commata and close braces + $SQL = substr($SQL, 0, -2); + + // Free result + SQL_FREERESULT($result); + + // Output the SQL query + loadTemplate('admin_export_network_types', FALSE, $SQL); +} + +// Exports (and displays) the table 'network_request_params' +function doAdminNetworkExportRequestParams () { + // 'network_id' must be set + if (!isGetRequestElementSet('network_id')) { + // Only network request parameters of one network will be exported per time + reportBug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.'); + } // END - if + + // Get all network types of given network + $result = SQL_QUERY_ESC('SELECT + `network_id`, + `network_type_id`, + `network_request_param_key`, + `network_request_param_value`, + `network_request_param_default` +FROM + `{?_MYSQL_PREFIX?}_network_request_params` +WHERE + `network_id`=%s +ORDER BY + `network_type_id` ASC , + `network_request_param_id` ASC', + array( + bigintval(getRequestElement('network_id')) + ), __FUNCTION__, __LINE__); + + // Start an empty SQL query + $SQL = "INSERT INTO `{?_MYSQL_PREFIX?}_network_request_params` (`network_id`, `network_type_id`, `network_request_param_key`, `network_request_param_value`, `network_request_param_default`) VALUES\n"; + + // Load all entries + while ($content = SQL_FETCHARRAY($result)) { + // Add row + $SQL .= '(' . + $content['network_id'] . ', ' . + $content['network_type_id'] . ", '" . + $content['network_request_param_key'] . "', '" . + $content['network_request_param_value'] . "', "; + + // Is the column NULL? + if (is_null($content['network_request_param_default'])) { + // Column is NULL + $SQL .= "NULL),\n"; + } else { + // Column is set + $SQL .= chr(39) . $content['network_request_param_default'] . "'),\n"; + } + } // END - while + + // Remove last commata and close braces + $SQL = substr($SQL, 0, -2); + + // Free result + SQL_FREERESULT($result); + + // Output the SQL query + loadTemplate('admin_export_network_request_params', FALSE, $SQL); +} + +// Exports (and displays) the table 'network_vcheck_params' +function doAdminNetworkExportVcheckParams () { + // 'network_id' must be set + if (!isGetRequestElementSet('network_id')) { + // Only network vcheck parameters of one network will be exported per time + reportBug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.'); + } // END - if + + // Get all network types of given network + $result = SQL_QUERY_ESC('SELECT + `network_id`, + `network_vcheck_param_key`, + `network_vcheck_param_value`, + `network_vcheck_param_default` +FROM + `{?_MYSQL_PREFIX?}_network_vcheck_params` +WHERE + `network_id`=%s +ORDER BY + `network_vcheck_param_id` ASC', + array( + bigintval(getRequestElement('network_id')) + ), __FUNCTION__, __LINE__); + + // Start an empty SQL query + $SQL = "INSERT INTO `{?_MYSQL_PREFIX?}_network_vcheck_params` (`network_id`, `network_vcheck_param_key`, `network_vcheck_param_value`, `network_vcheck_param_default`) VALUES\n"; + + // Load all entries + while ($content = SQL_FETCHARRAY($result)) { + // Add row + $SQL .= '(' . + $content['network_id'] . ", '" . + $content['network_vcheck_param_key'] . "', '" . + $content['network_vcheck_param_value'] . "', "; + + // Is the column NULL? + if (is_null($content['network_vcheck_param_default'])) { + // Column is NULL + $SQL .= "NULL),\n"; + } else { + // Column is set + $SQL .= chr(39) . $content['network_vcheck_param_default'] . "'),\n"; + } + } // END - while + + // Remove last commata and close braces + $SQL = substr($SQL, 0, -2); + + // Free result + SQL_FREERESULT($result); + + // Output the SQL query + loadTemplate('admin_export_network_vcheck_params', FALSE, $SQL); +} + +// Exports (and displays) the table 'network_array_translation' +function doAdminNetworkExportArrayTranslation () { + // 'network_id' must be set + if (!isGetRequestElementSet('network_id')) { + // Only network API array index translations of one network will be exported per time + reportBug(__FUNCTION__, __LINE__, 'network_id not provided, please fix your links.'); + } // END - if + + // Get all network types of given network + $result = SQL_QUERY_ESC('SELECT + `network_id`, + `network_type_id`, + `network_array_index`, + `network_array_sort` +FROM + `{?_MYSQL_PREFIX?}_network_array_translation` +WHERE + `network_id`=%s +ORDER BY + `network_type_id` ASC, + `network_array_sort` ASC', + array( + bigintval(getRequestElement('network_id')) + ), __FUNCTION__, __LINE__); + + // Start an empty SQL query + $SQL = "INSERT INTO `{?_MYSQL_PREFIX?}_network_array_translation` (`network_id`, `network_type_id`, `network_array_index`, `network_array_sort`) VALUES\n"; + + // Load all entries + while ($content = SQL_FETCHARRAY($result)) { + // Add row + $SQL .= '(' . + $content['network_id'] . ', ' . + $content['network_type_id'] . ', ' . + $content['network_array_index'] . ', ' . + $content['network_array_sort'] . "),\n"; + } // END - while + + // Remove last commata and close braces + $SQL = substr($SQL, 0, -2); + + // Free result + SQL_FREERESULT($result); + + // Output the SQL query + loadTemplate('admin_export_network_array_translation', FALSE, $SQL); } // [EOF]