From: Roland Häder Date: Tue, 19 Apr 2011 00:23:39 +0000 (+0000) Subject: PrimeraApi improved, some code cosmetics: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=963c572825d800849d69095e28ae13ae22eebd97 PrimeraApi improved, some code cosmetics: - PrimerApi improved: + All methods and fields are now written "lowerCamelCase" + Many double-quotes replaced with single to speed up things + POST request is now sent in one big pice (and not line-by-line) - Code cosmetics applied --- diff --git a/inc/classes/main/database/databases/class_LocalFileDatabase.php b/inc/classes/main/database/databases/class_LocalFileDatabase.php index 854a8888..9e7e3f42 100644 --- a/inc/classes/main/database/databases/class_LocalFileDatabase.php +++ b/inc/classes/main/database/databases/class_LocalFileDatabase.php @@ -495,7 +495,7 @@ class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontend } catch (FrameworkException $e) { // Catch all exceptions and store them in last error $this->lastException = $e; - $this->lastError = $e->getMessage(); + $this->lastError = $e->getMessage(); // Throw an SQL exception throw new SqlException (array($this, sprintf("Cannot write data to table '%s'", $tableName), self::DB_CODE_TABLE_UNWRITEABLE), self::EXCEPTION_SQL_QUERY); diff --git a/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php b/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php index dd6e1abe..557ab085 100644 --- a/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php +++ b/inc/classes/main/database/wrapper/class_UserDatabaseWrapper.php @@ -118,7 +118,7 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper implements ManageableAccou // Is it still null? if (is_null($searchInstance)) { // Throw an exception here - throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + throw new NullPointerException($updateInstance, self::EXCEPTION_IS_NULL_POINTER); } // END - if } // END - if diff --git a/inc/classes/third_party/api/primusportal/class_PrimeraApi.php b/inc/classes/third_party/api/primusportal/class_PrimeraApi.php index 5e651500..2251e4a5 100644 --- a/inc/classes/third_party/api/primusportal/class_PrimeraApi.php +++ b/inc/classes/third_party/api/primusportal/class_PrimeraApi.php @@ -1,60 +1,73 @@ payPrimera($PayReceiver, $PayAmount, $PayDescription); + * $Status = $apiInstance->payPrimera($PayReceiver, $PayAmount, $PayDescription); * * Wobei $PayReicer der Username des Empfängers bei * Primusportal.de ist. $PayAmount ist der gerundete( !! ) Betrag an Primera, * die der Empfänger erhalten soll. $PayDescription ist eine von Ihnen * festgelegte kurze Beschreibung. Die Länge dieses Textes darf 100 Zeichen * nicht überschreiten. Beispiel: - * $status = $Interface->payPrimera("garbage", 10000, "Auszahlung IhreSeite.de - ID: 12345"); + * $status = $apiInstance->payPrimera('garbage', 10000, 'Auszahlung IhreSeite.de - ID: 12345'); * 3. Überprüfung des Status (Rückgabecode): * CODE: - * if (!$status) { + * if ($status === false) { * // Ein Fehler ist aufgetreten * // Fehlerbehandlung hier einfügen... - * }else { + * } else { * // Auszahlung erfolgreich durchgeführt * // Führen Sie hier Ihre Datenbankabfragen durch, um die Auszahlung zu * // bestätigen... * } * * Die komplette Rückgabe des Interfaces wird als assoziatives Array in der Klassen- - * variable __data gespeichert: - * __data => array('status' => Rückgabecode (PI_DONE, PI_SENDER_ERROR, ...), - * "statustext" => Status in Worten (z.B.: "Transaktion erfolgreich durchgeführt"), - * ") + * variable $data gespeichert: + * $data = array( + * 'status' => Rückgabecode (PI_DONE, PI_SENDER_ERROR, ...), + * 'statustext' => Status in Worten (z.B.: 'Transaktion erfolgreich durchgeführt'), + * ); * * * @author Andreas Schmidt - * @author Roland Haeder + * @author Roland Haeder * @version 1.0 - beta * @copyright (c) 2007 by Primusportal.de - * @copyright (c) 2008 by Roland Haeder + * @copyright (c) 2008, 2011 by Roland Haeder */ class PrimeraApi extends BaseFrameworkSystem { /** @@ -102,11 +115,16 @@ class PrimeraApi extends BaseFrameworkSystem { */ const PI_GET_PRIMERA_DONE = 701; + /** + * HTTP-EOL + */ + const HTTP_EOL = "\r\n"; + /** * URL für das Interface auf dem Primusserver: */ - private $host = "www.primusportal.de"; - private $path = "/transfer.interface.2.0.php"; + private $host = 'www.primusportal.de'; + private $path = '/transfer.interface.2.0.php'; private $errno = 0; private $err = ''; @@ -121,56 +139,59 @@ class PrimeraApi extends BaseFrameworkSystem { /** * Konstruktor */ - public function __construct ($PPUsername, $PPPassword) { + public function __construct ($primusUsername, $primusPassword) { // Call parent constructor parent::__construct(); - // Set data (DEPRECATED!) - $this->username = $PPUsername; - $this->password = $PPPassword; + // Set data + $this->username = $primusUsername; + $this->password = $primusPassword; } /** * Anfrage senden und Rueckgabecode in Variable speichern */ - function queryApi ( $data = array() ) { + private function queryApi ( $data = array() ) { $fp = fsockopen($this->host, 80, $this->errno, $this->_err); if (!$fp) return false; - $data["PrimusInterface_Username"] = base64_encode($this->username); - $data["PrimusInterface_Password"] = base64_encode(md5($this->password)); + $data['PrimusInterface_Username'] = base64_encode($this->username); + $data['PrimusInterface_Password'] = base64_encode(md5($this->password)); // POST-Daten uebermitteln: - $data = http_build_query($data, '', '&'); + $queryData = http_build_query($data, '', '&'); + + $request .= 'POST ' . $this->path . 'HTTP/1.1' . self::HTTP_EOL; + $request .= 'Host: ' . $this->host . self::HTTP_EOL; + $request .= 'Content-type: application/x-www-form-urlencoded' . self::HTTP_EOL; + $request .= 'Content-length: '. strlen($queryData) . self::HTTP_EOL; + $request .= 'Connection: close' . self::HTTP_EOL; + $request .= self::HTTP_EOL; + $request .= $queryData; - fputs($fp, "POST {$this->path}HTTP/1.1\r\n"); - fputs($fp, "Host: {$this->host}\r\n"); - fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); - fputs($fp, "Content-length: ". strlen($data) ."\r\n"); - fputs($fp, "Connection: close\r\n\r\n"); - fputs($fp, $data); + fputs($fp, $request); $return = ''; while (!feof($fp)) { - $return.=fgets($fp,128); - } + $return .= fgets($fp, 128); + } // END - while - $content = explode("", $return); + $content = explode('', $return); return $content[1]; } /** * Funktion parst die Rückgabe vom Transferskript: */ - function parseContent ( $content ) { + private function parseContent ( $content ) { $x = explode("\n", $content); $return = array(); foreach($x as $currentLine) { $line_exploded = explode($this->seperator, $currentLine,2); if (count($line_exploded) > 1) { $return[$line_exploded[0]] = $line_exploded[1]; - } - } + } // END - if + } // END - foreach return $return; } @@ -181,18 +202,19 @@ class PrimeraApi extends BaseFrameworkSystem { */ public function payPrimera ($Receiver, $Amount, $Description = '') { $valid = false; - $PostData = array( + $postData = array( 'PrimusInterface_Action' => 'Pay', 'PrimusInterface_Receiver' => base64_encode($Receiver), 'PrimusInterface_Amount' => base64_encode($Amount), - 'PrimusInterface_Description' => base64_encode($Description) ); + 'PrimusInterface_Description' => base64_encode($Description) + ); - $PostReturn = $this->parseContent( $this->queryApi($PostData) ); + $postReturn = $this->parseContent( $this->queryApi($postData) ); - $this->data = $PostReturn; - if ($PostReturn['status'] == '200') { + $this->data = $postReturn; + if ($postReturn['status'] == '200') { $valid = true; - } + } // END - if return $valid; } @@ -202,37 +224,40 @@ class PrimeraApi extends BaseFrameworkSystem { * - ist er aktiv * @param string/int $User Userid / Username */ - function CheckPrimusUser ($User) { + public function checkPrimusUser ($userName) { $valid = false; - $PostData = array( + $postData = array( 'PrimusInterface_Action' => 'CheckPrimusUser', - 'PrimusInterface_CheckPrimusUser' => $User); + 'PrimusInterface_CheckPrimusUser' => $userName + ); - $PostReturn = $this->parseContent( $this->queryApi($PostData) ); + $postReturn = $this->parseContent( $this->queryApi($postData) ); - $this->data = $PostReturn; + $this->data = $postReturn; - if ($PostReturn['status'] == self::PI_USER_CHECK_OK) { + if ($postReturn['status'] == self::PI_USER_CHECK_OK) { $valid = true; - } + } // END - if return $valid; } /** * Die Funktion liefer den aktuellen Primerastand */ - function getPrimera() { + public function getPrimera() { $primera = false; - $PostData = array( "PrimusInterface_Action" => "GetPrimera" ); - $PostReturn = $this->parseContent( $this->queryApi($PostData) ); - - $this->data = $PostReturn; - if ($PostReturn['status'] == self::PI_GET_PRIMERA_DONE) { - $primera = $PostReturn["primera"]; - } + $postData = array( + 'PrimusInterface_Action' => 'GetPrimera' + ); + $postReturn = $this->parseContent( $this->queryApi($postData) ); + + $this->data = $postReturn; + if ($postReturn['status'] == self::PI_GET_PRIMERA_DONE) { + $primera = $postReturn['primera']; + } // END - if return $primera; } } // [EOF] -?> \ No newline at end of file +?>