]> git.mxchange.org Git - mailer.git/blobdiff - inc/libs/primera_functions.php
More misc fixes and rewrites (sorry, lame description)
[mailer.git] / inc / libs / primera_functions.php
index c28da8cc79b7ab824036ffa20906419e044faf39..3f7f445f79f577809e72029c9387691a4f9690eb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * PrimusInterface
+ * PrimeraApi
  * -------------------------------------------
  * Mit dieser Klasse ist ein einfacher Primeratransfer von Ihrem Account
  * zu dem Account eines bei Primusportal.de registrierten Mitglieds möglich.
  * Die Einbindung des Interfaces geschieht folgendermaßen:
  *  1. Einbindung der Klasse in Ihr PHP-Auszahlungsskript:
  *     CODE:
- *       require_once( "PFAD/ZU/DER/KLASSE/PrimusInterface.class.php" );
+ *       require_once( "PFAD/ZU/DER/KLASSE/PrimeraApi.class.php" );
  *       $PPUsername = "username"; // Ihr Username bei Primusportal
  *       $PPPassword = "passwort"; // Ihr Passwort bei Primusportal
  *
- *       $Interface = new PrimusInterface($PPUsername, $PPPassword);
+ *       $Interface = new PrimeraApi($PPUsername, $PPPassword);
  *  2. Durchführen einer Auszahlung:
  *     CODE:
  *       $Status = $Interface->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 
+ *     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");
@@ -45,7 +45,7 @@
  *
  *     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, ...),
+ *     __data => array('status' => Rückgabecode (PI_DONE, PI_SENDER_ERROR, ...),
  *                     "statustext" => Status in Worten (z.B.: "Transaktion erfolgreich durchgeführt"),
  *                     ")
  *
@@ -105,23 +105,25 @@ class PrimeraApi {
        /**
         * URL für das Interface auf dem Primusserver:
         */
-       private $host = "www.primusportal.de";
-       private $path = "/transfer.interface.2.0.php";
+       var $host = "http://www.primusportal.de";
+       var $path = "/transfer.interface.2.0.php";
 
-       private $errno = 0;
-       private $err = "";
+       var $errno = 0;
+       var $err = '';
 
-       private $seperator = ":";
+       var $seperator = ':';
 
-       private $username = "";
-       private $password = "";
+       var $username = '';
+       var $password = '';
 
-       private $data = array();
+       var $data = array();
+
+       var $headers = '';
 
        /**
         * Konstruktor
         */
-       public function PrimeraApi ($PPUsername, $PPPassword) {
+       function PrimeraApi ($PPUsername, $PPPassword) {
                // Set data
                $this->username = $PPUsername;
                $this->password = $PPPassword;
@@ -131,13 +133,23 @@ class PrimeraApi {
         * Anfrage senden und Rückgabecode in Variable speichern
         */
        function queryApi ( $data = array() ) {
-               $data["PrimusInterface_Username"] = base64_encode($this->username);
-               $data["PrimusInterface_Password"] = base64_encode(md5($this->password));
+               // Base64-encode username and password hash
+               $data['PrimusInterface_Username'] = base64_encode($this->username);
+               $data['PrimusInterface_Password'] = base64_encode($this->password);
 
                // Send POST request
-               $return = POST_URL($this->host.$this->path);
+               $return = sendPostRequest($this->host.$this->path, $data);
+
+               // Convert the array into a full string
+               $returnStr = implode("\n", $return);
+
+               // Extract the real content, strip header away
+               $content = explode('<!-- return-start -->', $returnStr);
 
-               $content = explode("<!-- return-start -->", $return);
+               // Store headers away for debugging
+               $this->headers = $content[0];
+
+               // Return the content
                return $content[1];
        }
 
@@ -148,7 +160,7 @@ class PrimeraApi {
                $x = explode("\n", $content);
                $return = array();
                foreach($x as $currentLine) {
-                       $line_exploded = explode(":", $currentLine,2);
+                       $line_exploded = explode(':', $currentLine,2);
                        if (count($line_exploded) > 1) {
                                $return[$line_exploded[0]] = $line_exploded[1];
                        }
@@ -161,17 +173,17 @@ class PrimeraApi {
         * @param int $Amount Betrag in ganzzahligen Primera
         * @param string $Description Beschreibung (Sichtbar in Einzelauflistung)
         */
-       public function payPrimera ($Receiver, $Amount, $Description = "") {
+       function payPrimera ($Receiver, $Amount, $Description = '') {
                $valid = false;
-               $PostData = array("PrimusInterface_Action" => "Pay",
-                       "PrimusInterface_Receiver" => base64_encode($Receiver),
-                       "PrimusInterface_Amount" => base64_encode($Amount),
-                       "PrimusInterface_Description" => base64_encode($Description) );
+               $postData = array('PrimusInterface_Action' => 'Pay',
+                       'PrimusInterface_Receiver' => base64_encode($Receiver),
+                       'PrimusInterface_Amount' => base64_encode($Amount),
+                       '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;
                }
                return $valid;
@@ -185,14 +197,14 @@ class PrimeraApi {
         */
        function CheckPrimusUser($User) {
                $valid = false;
-               $PostData = array("PrimusInterface_Action"=> "CheckPrimusUser",
-                       "PrimusInterface_CheckPrimusUser" => $User);
+               $postData = array('PrimusInterface_Action'=> 'CheckPrimusUser',
+                       'PrimusInterface_CheckPrimusUser' => $User);
 
-               $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;
                }
                return $valid;
@@ -203,16 +215,64 @@ class PrimeraApi {
         */
        function getPrimera() {
                $primera = false;
-               $PostData = array( "PrimusInterface_Action" => "GetPrimera" );
-               $PostReturn = $this->parseContent( $this->queryApi($PostData) );
+               $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"];
+               $this->data = $postReturn;
+               if ($postReturn['status'] == self::PI_GET_PRIMERA_DONE) {
+                       $primera = $postReturn['primera'];
                }
                return $primera;
        }
+       /**
+        * Getter fuer data
+        */
+       function getData () {
+               return $this->data;
+       }
 }
 
+// Function to test the Primera API by getting the amount. If the returned value
+// is not false the API data is valid, else invalid
+function PRIMERA_TEST_API () {
+       // Get new instance
+       $api = new PrimeraApi(REQUEST_POST('primera_api_name'), REQUEST_POST('primera_api_md5'));
+
+       // Was that fine?
+       return ($api->getPrimera() !== false);
+}
+// Execute the withdraw of a sponsor only!
+function PRIMERA_EXECUTE_WITHDRAW ($primusNick, $userMd5, $amount) {
+       // Is the sponsor extension installed?
+       if (!EXT_IS_ACTIVE('sponsor')) {
+               // No, abort here
+               return false;
+       } elseif (!IS_SPONSOR()) {
+               // No sponsor, not allowed to withdraw!
+               return false;
+       }
+
+       // Get new instance
+       $api = new PrimeraApi($primusNick, $userMd5);
+
+       // Prepare purpose
+       $eval = "\$purpose = \"".COMPILE_CODE(sprintf(getMessage('PRIMERA_API_PURPOSE_WITHDRAW'), getSession('sponsorid')))."\";";
+       eval($eval);
+
+       // Pay the Primera
+       return $api->payPrimera($primusNick, $amount, $purpose);
+}
+// Execute the payout
+function PRIMERA_EXECUTE_PAYOUT ($primusNick, $userMd5, $amount) {
+       // Get new instance
+       $api = new PrimeraApi(getConfig('primera_api_name'), getConfig('primera_api_md5'));
+
+       // Prepare purpose
+       $eval = "\$purpose = \"".COMPILE_CODE(sprintf(getMessage('PRIMERA_API_PURPOSE_PAYOUT'), getUserId()))."\";";
+       eval($eval);
+
+       // Pay the Primera
+       return $api->payPrimera($primusNick, $amount, $purpose);
+}
 // [EOF]
-?>
\ No newline at end of file
+?>