From: Roland Häder Date: Sun, 3 Feb 2008 22:05:51 +0000 (+0000) Subject: Wernis extension can now contact the API X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=33a744d80e9c5ec1c13e8f643143146166975e9b;p=mailer.git Wernis extension can now contact the API --- diff --git a/inc/extensions/ext-wernis.php b/inc/extensions/ext-wernis.php index d82e998115..33ea71e911 100644 --- a/inc/extensions/ext-wernis.php +++ b/inc/extensions/ext-wernis.php @@ -65,6 +65,7 @@ PRIMARY KEY(id) $SQLs[] = "ALTER TABLE "._MYSQL_PREFIX."_config ADD wernis_min_amount bigint(20) not null default '40000'"; $SQLs[] = "ALTER TABLE "._MYSQL_PREFIX."_config ADD wernis_api_id bigint(20) not null default '0'"; $SQLs[] = "ALTER TABLE "._MYSQL_PREFIX."_config ADD wernis_api_md5 varchar(32) not null default ''"; + $SQLs[] = "ALTER TABLE "._MYSQL_PREFIX."_config ADD wernis_api_url varchar(255) not null default 'http://www.wds66.com/api/'"; $SQLs[] = "INSERT INTO "._MYSQL_PREFIX."_admin_menu (action, what, title, descr, sort) VALUES('wernis', '', '{!POINTS!}-Management', 'Konfiguration zur WDS66-API einstellen, Auszahlungen auflisten usw..', 15)"; $SQLs[] = "INSERT INTO "._MYSQL_PREFIX."_admin_menu (action, what, title, descr, sort) VALUES('wernis', 'config_wernis', 'WDS66-API-Einstellungen', 'Konfiguration zur WDS66-API einstellen.', 1)"; $SQLs[] = "INSERT INTO "._MYSQL_PREFIX."_admin_menu (action, what, title, descr, sort) VALUES('wernis', 'list_wernis', '{!POINTS!}-Anfragen auflisten', 'Listet alle {!POINTS!}-Auszahlungsanfragen Ihrer Mitglieder auf.', 2)"; @@ -103,6 +104,7 @@ default: // Do stuff when extension is loaded $CONFIG['wernis_min_amount'] = $DUMMY['wernis_min_amount']; $CONFIG['wernis_api_id'] = $DUMMY['wernis_api_id']; $CONFIG['wernis_api_md5'] = $DUMMY['wernis_api_md5']; + $CONFIG['wernis_api_url'] = $DUMMY['wernis_api_url']; unset($DUMMY); break; } diff --git a/inc/functions.php b/inc/functions.php index d675eca81b..161c73c35d 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1718,12 +1718,25 @@ function ADD_EMAIL_NAV($PAGES, $offset, $show_form, $colspan, $return=false) { // function MXCHANGE_OPEN($script) { + // Compile the script name + $script = COMPILE_CODE($script); + + // Use default SERVER_URL by default... ;) So? + $url = SERVER_URL; + if (substr($script, 0, 7) == "http://") { + // Use the hostname from script URL as new hostname + $url = substr($script, 7); + $extract = explode("/", $url); + $url = $extract[0]; + // Done extracting the URL :) + } + // Extract host name - $host = str_replace("http://", "", SERVER_URL); + $host = str_replace("http://", "", $url); if (ereg("/", $host)) $host = substr($host, 0, strpos($host, "/")); // Generate relative URL - $script = substr(SERVER_URL, (strpos(SERVER_URL, $host) + strlen($host)))."/".basename($script); + $script = substr($script, (strlen($url) + 7)); if (substr($script, 0, 1) == "/") $script = substr($script, 1); // Open connection diff --git a/inc/language/wernis_de.php b/inc/language/wernis_de.php index 77fbdfc51a..a57ab71099 100644 --- a/inc/language/wernis_de.php +++ b/inc/language/wernis_de.php @@ -32,8 +32,7 @@ ************************************************************************/ // Some security stuff... -if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) -{ +if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) { $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php"; require($INC); } @@ -45,10 +44,18 @@ define('WERNIS_ADMIN_CONFIG_TITLE', "Einstellungen zum WDS66-Hauptaccount:"); define('WERNIS_ADMIN_MIN_AMOUNT', "Minimumguthaben von {!POINTS!} für Auszahlungen."); define('WERNIS_ADMIN_API_ID', "WDS66-API-ID (api_id=x)"); define('WERNIS_ADMIN_API_MD5', "WDS66-API-Key (api_md5=xxxxx; 32-stellig)"); +define('WERNIS_ADMIN_API_URL', "Basis-URL der API-Skripte"); define('WERNIS_ADMIN_CONFIG_NOTE', "Hier können Sie schon für 5,00€=25.000 Abfragen ein API-Account beantragen (dazu ist ein kostenloses {!POINTS!}-Account dennoch nötig!)"); define('MEMBER_WERNIS_MODE_CHOOSE', "Möchten Sie {!POINTS!} zu Ihrem WDS66-Hauptaccount ausbezahlt bekommen oder {!POINTS!} hier einzahlen?"); define('MEMBER_WERNIS_MODE_WITHDRAW', "{!POINTS!} einzahlen"); define('MEMBER_WERNIS_MODE_PAYOUT', "{!POINTS!} auszahlen"); +// API messages +define('WERNIS_API_REQUEST_DATA_INVALID', "Interner Fehler: Abfragedaten sind defekt!"); +define('WERNIS_API_REQUEST_DATA_MISSING', "Interner Fehler: Abfragedaten sind nicht komplett!"); +define('WERNIS_API_REQUEST_ERROR', "Abfragefehler: %s"); +define('WERNIS_API_REQUEST_FAILED', "Unbekannter Fehler %s von API erhalten! Bitte im [Forum melden], den Fehlercode nennen und wie es dazu gekommen ist."); +define('WERNIS_API_REQUEST_FAILED_AUTH', "Authorisierung an API fehlgeschlagen! Bitte API-ID und Key überprüfen. [Hier] direkt zum Export-Account. Sie benötigen Ihren Usernamen und Passwort!"); + // ?> diff --git a/inc/libs/wernis_functions.php b/inc/libs/wernis_functions.php index c96ac9e968..8dff531395 100644 --- a/inc/libs/wernis_functions.php +++ b/inc/libs/wernis_functions.php @@ -32,11 +32,106 @@ ************************************************************************/ // Some security stuff... -if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) -{ +if (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) { $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php"; require($INC); } +// Sends out a request to the API and returns it's result +function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) { + global $CONFIG; + + // Is the requestData an array? + if (!is_array($requestData)) { + // Then abort here! + return array( + 'status' => "failed_general", + 'message' => WERNIS_API_REQUEST_DATA_INVALID + ); + } + + // Is the API id and MD5 hash there? + if ((empty($CONFIG['wernis_api_id'])) || (empty($CONFIG['wernis_api_md5']))) { + // Abort here... + return array( + 'status' => "failed_general", + 'message' => WERNIS_API_REQUEST_DATA_MISSING + ); + } + + // Construct the request string + $requestString = $CONFIG['wernis_api_url'] . $scriptName."?api_id=".$CONFIG['wernis_api_id']."&api_key=".$CONFIG['wernis_api_md5']; + foreach ($requestData as $key=>$value) { + $requestString .= "&".$key."=".$value; + } + + // Get the raw response from the lower function + $response = MXCHANGE_OPEN($requestString); + + // Check the response header if all is fine + if (strpos($response[0], "200") === false) { + // Something bad happend... :( + return array( + 'status' => "request_eror", + 'message' => sprintf(WERNIS_API_REQUEST_ERROR, $response[0]) + ); + } + + // All (maybe) fine so remove the response header from server + $response = $response[(count($response) - 1)]; + + // Prepare the returning result for higher functions + if (substr($response, 0, 1) == "&") { + // Remove the leading & (which can be used in Flash) + $response = substr($response, 1); + } + + // Bring back the response + $data = explode("=", $response); + + // Default return array (should not stay empty) + $return = array(); + + // We use only the first two entries (which shall be fine) + if ($data[0] == "error") { + // The request has failed... :( + switch ($data[1]) { + case "AUTH": // Authorization has failed + $return = array( + 'status' => "auth_failed", + 'message' => WERNIS_API_REQUEST_FAILED_AUTH + ); + break; + + default: // Unknown error (maybe new?) + $return = array( + 'status' => "request_failed", + 'message' => sprintf(WERNIS_API_REQUEST_FAILED, $data[1]) + ); + break; + } + } else { + // All fine here + $return = array( + 'status' => "OK", + 'response' => $response + ); + } + + // Return the result + return $return; +} + +// Tests the function by calling balance.php on the API +function WERNIS_TEST_API () { + // Get config first + global $CONFIG; + + // Prepare the request to the API + $return = WERNIS_SEND_REQUEST("balance.php"); + + die("
".print_r($return, true)."
"); +} + // ?> diff --git a/inc/modules/admin/what-config_wernis.php b/inc/modules/admin/what-config_wernis.php index ea86768545..7650ae1bad 100644 --- a/inc/modules/admin/what-config_wernis.php +++ b/inc/modules/admin/what-config_wernis.php @@ -41,14 +41,24 @@ if ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!IS_ADMIN())) ADD_DESCR("admin", basename(__FILE__)); if (isset($_POST['ok'])) { - // Save settings - ADMIN_SAVE_SETTINGS($_POST, "_config", "config='0'"); + // First merge temporary the new API data into config + $CONFIG = array_merge($CONFIG, $_POST); + + // Let's test the API first (hold your horses here, cowboy! Thanks. :) ) + if (WERNIS_TEST_API()) { + // Save settings + ADMIN_SAVE_SETTINGS($_POST, "_config", "config='0'"); + } else { + // Failed to test the API! + LOAD_TEMPLATE("admin_settings_saved", false, WERNIS_ERROR_MESSAGE()); + } } else { // Prepare data for template output $content = array( 'min_amount' => $CONFIG['wernis_min_amount'], 'api_id' => $CONFIG['wernis_api_id'], - 'api_md5' => $CONFIG['wernis_api_md5'] + 'api_md5' => $CONFIG['wernis_api_md5'], + 'api_url' => $CONFIG['wernis_api_url'] ); // Load template diff --git a/templates/de/html/admin/admin_config_wernis.tpl b/templates/de/html/admin/admin_config_wernis.tpl index d362c17f2e..32511bce83 100644 --- a/templates/de/html/admin/admin_config_wernis.tpl +++ b/templates/de/html/admin/admin_config_wernis.tpl @@ -13,7 +13,8 @@ {--WERNIS_ADMIN_API_ID--}:   + class="admin_normal" value="$content[api_id]" size="2" + maxlength="20" />   @@ -23,7 +24,7 @@   + maxlength="32" />   @@ -36,7 +37,17 @@   + maxlength="20" /> + + +   + + + {--WERNIS_ADMIN_API_URL--}: +   +