+// Only adds network type configuration if not yet present
+function doAdminNetworkProcessAddHandlerTypesConfig ($displayMessage = TRUE, $convertComma = TRUE) {
+ // Add both ids
+ setPostRequestElement('network_id', bigintval(getRequestElement('network_id')));
+ setPostRequestElement('network_type_id', bigintval(getRequestElement('network_type_id')));
+
+ // Convert comma to dot?
+ if ($convertComma === TRUE) {
+ // Translate German comma to dot
+ convertCommaToDotInPostData('network_min_payment');
+ convertCommaToDotInPostData('network_min_remain_budget');
+ convertCommaToDotInPostData('network_min_remain_clicks');
+ } // END - if
+
+ /*
+ * Some parameters are optional, at least one must be given so check a bunch
+ * of parameters.
+ */
+ foreach (array('network_min_waiting_time', 'network_max_waiting_time', 'network_min_remain_budget', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic', 'network_media_size', 'network_media_output') as $element) {
+ // Is this element empty?
+ if (!isPostRequestElementSet($element)) {
+ // Then unset it to get a NULL for optional parameter
+ unsetPostRequestElement($element);
+ } // END - if
+ } // END - foreach
+
+ // Convert data in POST array
+ 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 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, 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
+ displayErrorMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}');
+ }
+ } else {
+ // Get SQL query for new entry
+ $SQL = getInsertSqlFromArray(postRequestArray(), 'network_handler_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
+ displayErrorMessage('{--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, $convertComma = TRUE) {
+ // Convert comma to dot?
+ if ($convertComma === TRUE) {
+ // Translate German comma to dot
+ convertCommaToDotInPostData('network_min_payment');
+ convertCommaToDotInPostData('network_min_remain_budget');
+ convertCommaToDotInPostData('network_min_remain_clicks');
+ } // END - if
+
+ /*
+ * Some parameters are optional, at least one must be given so check a bunch
+ * of parameters.
+ */
+ foreach (array('network_min_waiting_time', 'network_max_waiting_time', 'network_min_remain_budget', 'network_min_remain_clicks', 'network_min_payment', 'network_allow_erotic', 'network_media_size', 'network_media_output') as $element) {
+ // Is this element empty?
+ if (!isPostRequestElementSet($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_handler_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, 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
+ displayErrorMessage('{--ADMIN_CONFIG_NETWORK_HANDLER_TYPE_HANDLER_NOT_CHANGED--}');
+ }
+ } else {
+ // Get SQL query for new entry
+ $SQL = getUpdateSqlFromArray(postRequestArray(), 'network_handler_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
+ displayErrorMessage('{--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']
+ );
+
+ // Check matches[2] as it might contain more clues to look for
+ $moreData = explode(',', $data['matches'][2][$data['key']]);
+
+ // First must be 'network' so unshift it
+ shift_array($moreData, 'network');
+
+ // The second element must be a callable function
+ if (!is_callable($moreData[0])) {
+ // Is not callable!
+ reportBug(__FUNCTION__, __LINE__, 'Call-back function ' . $moreData[0] . ' cannot be called.');
+ } // END - if
+
+ // Is the current network id set?
+ if (isCurrentNetworkIdSet()) {
+ // Replace %network_id% with the current network id
+ $replacer = str_replace('%network_id%', getCurrentNetworkId(), $replacer);
+ } // END - if
+
+ // 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',
+ // 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_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(getNetworkTypeDataFromId(postRequestElement('network_id')), 'network_type_id');
+
+ // Set generated array
+ setAjaxReplyContent(encodeJson($networkTypes));
+
+ // All fine
+ setHttpStatus('200 OK');
+}
+