+// ----------------------------------------------------------------------------
+// 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' . PHP_EOL;
+
+ // 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' . PHP_EOL;
+
+ // 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'] . "', ";
+
+ // Is the column NULL?
+ if ((is_null($content['network_type_click_url'])) || (empty($content['network_type_click_url']))) {
+ // Column is NULL
+ $SQL .= 'NULL, ';
+ } else {
+ // Column is set
+ $SQL .= chr(39) . $content['network_type_click_url'] . chr(39) . ', ';
+ }
+
+ // Is the column NULL?
+ if ((is_null($content['network_type_banner_url'])) || (empty($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 .= chr(39) . $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' . PHP_EOL;
+
+ // 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' . PHP_EOL;
+
+ // 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' . PHP_EOL;
+
+ // 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);
+}
+
+// ----------------------------------------------------------------------------
+// Call-back functions for AJAX requests
+// ----------------------------------------------------------------------------
+
+// AJAX call-back function for quering a single API
+function doAjaxAdminNetworkQuerySingleApi () {
+ // This must be be done only by admins
+ if (!isAdmin()) {
+ // Only allowed for admins
+ reportBug(__FUNCTION__, __LINE__, 'Only allowed for admins.');
+ } elseif (!isPostRequestElementSet('network_type_id')) {
+ // Required POST field 'network_type_id' is not there
+ reportBug(__FUNCTION__, __LINE__, 'Required POST field "network_type_id" is missing.');
+ }
+
+ // Get network + type handler data
+ $networkData = getNetworkDataByTypeId(postRequestElement('network_type_id'));
+
+ // Is it set?
+ if (is_null($networkData)) {
+ // Provided type id is not found
+ reportBug(__FUNCTION__, __LINE__, 'Requested network type id ' . postRequestElement('network_type_id') . ' does not exist.');
+ } elseif ((!isDebugModeEnabled()) && ($networkData['network_active'] == 'N')) {
+ // Network not active
+ reportBug(__FUNCTION__, __LINE__, 'Network ' . $networkData['network_title'] . ' is not active. network_id=' . $networkData['network_id'] . ',network_type_id=' . postRequestElement('network_type_id'));
+ } elseif (!isNetworkApiConfigured($networkData['network_id'])) {
+ // Network not configured
+ reportBug(__FUNCTION__, __LINE__, 'Network ' . $networkData['network_title'] . ' is not configured yet. network_id=' . $networkData['network_id'] . ',network_type_id=' . postRequestElement('network_type_id'));
+ } elseif (!isNetworkTypeHandlerConfigured($networkData['network_id'], postRequestElement('network_type_id'))) {
+ // Network type handler not configured
+ reportBug(__FUNCTION__, __LINE__, 'Network type handler ' . $networkData['network_type_handler'] . ' for network ' . $networkData['network_title'] . ' is not configured yet. network_id=' . $networkData['network_id'] . ',network_type_id=' . postRequestElement('network_type_id'));
+ }
+
+ // Now load request parameters
+ $requestParams = getNetworkRequestParametersByTypeId(postRequestElement('network_type_id'));
+
+ // Is there at least one entry?
+ if (count($requestParams) == 0) {
+ // No entry found, please setup some first
+ reportBug(__FUNCTION__, __LINE__, 'Network ' . $networkData['network_title'] . ' with id ' . $networkData['network_id'] . ' has no request parameters.');
+ } // END - if
+
+ // Handle all keys
+ handleNetworkRequestParameterKeys($requestParams);
+
+ /*
+ * Array element network_request_param_value contains the request parameter
+ * keys, network_request_param_default contains values. Now the request can
+ * be build.
+ */
+ $requestData = array();
+ foreach ($requestParams as $key => $params) {
+ // Add id
+ $requestData[$params['network_request_param_value']] = $params['network_request_param_default'];
+ } // END - foreach
+
+ // Everything is setup and ready to send out to the affiliate network's API
+ $response = queryNetworkApi($networkData, $requestData);
+
+ // Is the returned HTTP status '200 OK'?
+ if (!isHttpStatusOkay($response[0])) {
+ // Not HTTP/1.x 200 OK
+ reportBug(__FUNCTION__, __LINE__, 'HTTP response code is not 200 OK, have: ' . $response[0]);
+ } // END - if
+
+ // Load "success" message
+ setAjaxReplyContent('{%message,ADMIN_NETWORK_QUERY_TYPE_OKAY=' . $networkData['network_title'] . '%}');
+
+ // All fine
+ setHttpStatus('200 OK');
+}
+
+// AJAX call-back function to return a JSON with all network type handler ids
+function doAjaxAdminNetworkListById () {
+ // This must be be done only by admins
+ if (!isAdmin()) {
+ // Only allowed for admins
+ reportBug(__FUNCTION__, __LINE__, 'Only allowed for admins.');
+ } elseif (!isPostRequestElementSet('network_id')) {
+ // Required POST field 'network_id' is not there
+ reportBug(__FUNCTION__, __LINE__, 'Required POST field "network_id" is missing.');
+ }
+
+ // Load all network type handlers by given network id and extract only network_type_id
+ $networkTypes = getArrayFromArrayIndex(getNetworkTypeDataById(postRequestElement('network_id')), 'network_type_id');
+
+ // Set generated array
+ setAjaxReplyContent(encodeJson($networkTypes));
+
+ // All fine
+ setHttpStatus('200 OK');
+}
+