Reset rewritten, SQL fixed, zeros are now numeric
[mailer.git] / inc / libs / primera_functions.php
1 <?php
2 /**
3  * PrimusInterface
4  * -------------------------------------------
5  * Mit dieser Klasse ist ein einfacher Primeratransfer von Ihrem Account
6  * zu dem Account eines bei Primusportal.de registrierten Mitglieds möglich.
7  *
8  *------------------ Aenderungen durch Roland Haeder 09.08.2008 ----------------
9  * Klasse umbenannt nach PrimeraApi, damit sie in das Framework besser passt.
10  * Zudem sind alle oeffentlichen Attribute nun privat, der Konstruktor hat den
11  * neuen "magischen" Namen __construct() und "normale" Konstanten sind nach
12  * Klassenkonstanten umbenannt. Unsinnige else-Bloecke sind noch umgewandelt.
13  * Methodennamen fangen nun immer mit einem kleinen Buchstaben an. Zudem sind
14  * die Methoden Pay,Query und Parse umbenannt.
15  *------------------ Aenderungen durch Roland Haeder 09.08.2008 ----------------
16  *
17  * Die Einbindung des Interfaces geschieht folgendermaßen:
18  *  1. Einbindung der Klasse in Ihr PHP-Auszahlungsskript:
19  *     CODE:
20  *       require_once( "PFAD/ZU/DER/KLASSE/PrimusInterface.class.php" );
21  *       $PPUsername = "username"; // Ihr Username bei Primusportal
22  *       $PPPassword = "passwort"; // Ihr Passwort bei Primusportal
23  *
24  *       $Interface = new PrimusInterface($PPUsername, $PPPassword);
25  *  2. Durchführen einer Auszahlung:
26  *     CODE:
27  *       $Status = $Interface->payPrimera($PayReceiver, $PayAmount, $PayDescription);
28  *
29  *     Wobei $PayReicer der Username des Empfängers bei
30  *     Primusportal.de ist. $PayAmount ist der gerundete( !! ) Betrag an Primera,
31  *     die der Empfänger erhalten soll. $PayDescription ist eine von Ihnen 
32  *     festgelegte kurze Beschreibung. Die Länge dieses Textes darf 100 Zeichen
33  *     nicht überschreiten. Beispiel:
34  *       $status = $Interface->payPrimera("garbage", 10000, "Auszahlung IhreSeite.de - ID: 12345");
35  *  3. Überprüfung des Status (Rückgabecode):
36  *     CODE:
37  *       if (!$status) {
38  *         // Ein Fehler ist aufgetreten
39  *     // Fehlerbehandlung hier einfügen...
40  *       }else {
41  *         // Auszahlung erfolgreich durchgeführt
42  *         // Führen Sie hier Ihre Datenbankabfragen durch, um die Auszahlung zu
43  *         // bestätigen...
44  *       }
45  *
46  *     Die komplette Rückgabe des Interfaces wird als assoziatives Array in der Klassen-
47  *     variable __data gespeichert:
48  *     __data => array("status" => Rückgabecode (PI_DONE, PI_SENDER_ERROR, ...),
49  *                     "statustext" => Status in Worten (z.B.: "Transaktion erfolgreich durchgeführt"),
50  *                     ")
51  *
52  *
53  * @author              Andreas Schmidt <xxgarbagexx@web.de>
54  * @author              Roland Haeder <webmaster@ship-simu.org>
55  * @version             1.0 - beta
56  * @copyright   (c) 2007 by Primusportal.de
57  * @copyright   (c) 2008 by Roland Haeder
58  */
59 class PrimeraApi {
60         /**
61          * Fehler - Interfacebenutzer
62          */
63         const PI_ERROR = -1;
64
65         /**
66          * Statuscode für erfolgreich ausgeführte Transaktion
67          */
68         const PI_DONE = 200;
69
70         /**
71          * Fehler - User existiert nicht oder ist gesperrt
72          */
73         const PI_RECEIVER_ERROR = 301;
74
75         /**
76          * Sender-Account Fehler (User nicht existent, gesperrt, ...)
77          */
78         const PI_SENDER_ERROR = 401;
79
80         /**
81          * Betrag fehler
82          */
83         const PI_AMOUNT_ERROR = 501;
84
85         /**
86          * Zu wenig Primera
87          */
88         const PI_TOO_LESS_PRIMERA = 502;
89
90         /**
91          * User nicht aktiv oder existiert nicht
92          */
93         const PI_USER_CHECK_ERROR = 601;
94
95         /**
96          * User aktiv
97          */
98         const PI_USER_CHECK_OK = 602;
99
100         /**
101          * Primerastand erfolgreich geholt
102          */
103         const PI_GET_PRIMERA_DONE = 701;
104
105         /**
106          * URL für das Interface auf dem Primusserver:
107          */
108         private $host = "www.primusportal.de";
109         private $path = "/transfer.interface.2.0.php";
110
111         private $errno = 0;
112         private $err = "";
113
114         private $seperator = ":";
115
116         private $username = "";
117         private $password = "";
118
119         private $data = array();
120
121         /**
122          * Konstruktor
123          */
124         public function PrimeraApi ($PPUsername, $PPPassword) {
125                 // Set data
126                 $this->username = $PPUsername;
127                 $this->password = $PPPassword;
128         }
129
130         /**
131          * Anfrage senden und Rückgabecode in Variable speichern
132          */
133         function queryApi ( $data = array() ) {
134                 $data["PrimusInterface_Username"] = base64_encode($this->username);
135                 $data["PrimusInterface_Password"] = base64_encode(md5($this->password));
136
137                 // Send POST request
138                 $return = POST_URL($this->host.$this->path);
139
140                 $content = explode("<!-- return-start -->", $return);
141                 return $content[1];
142         }
143
144         /**
145          * Funktion parst die Rückgabe vom Transferskript:
146          */
147         function parseContent ( $content ) {
148                 $x = explode("\n", $content);
149                 $return = array();
150                 foreach($x as $currentLine) {
151                         $line_exploded = explode(":", $currentLine,2);
152                         if (count($line_exploded) > 1) {
153                                 $return[$line_exploded[0]] = $line_exploded[1];
154                         }
155                 }
156                 return $return;
157         }
158
159         /**
160          * @param int/string $Receiver UserID / Username des Empfängers
161          * @param int $Amount Betrag in ganzzahligen Primera
162          * @param string $Description Beschreibung (Sichtbar in Einzelauflistung)
163          */
164         public function payPrimera ($Receiver, $Amount, $Description = "") {
165                 $valid = false;
166                 $PostData = array("PrimusInterface_Action" => "Pay",
167                         "PrimusInterface_Receiver" => base64_encode($Receiver),
168                         "PrimusInterface_Amount" => base64_encode($Amount),
169                         "PrimusInterface_Description" => base64_encode($Description) );
170
171                 $PostReturn = $this->parseContent( $this->queryApi($PostData) );
172
173                 $this->data = $PostReturn;
174                 if ($PostReturn["status"] == "200") {
175                         $valid = true;
176                 }
177                 return $valid;
178         }
179
180         /**
181          * Überprüft den Status eines Primus-Users
182          * - existiert der User
183          * - ist er aktiv
184          * @param string/int $User Userid / Username
185          */
186         function CheckPrimusUser($User) {
187                 $valid = false;
188                 $PostData = array("PrimusInterface_Action"=> "CheckPrimusUser",
189                         "PrimusInterface_CheckPrimusUser" => $User);
190
191                 $PostReturn = $this->parseContent( $this->queryApi($PostData) );
192
193                 $this->data = $PostReturn;
194
195                 if ($PostReturn["status"] == self::PI_USER_CHECK_OK) {
196                         $valid = true;
197                 }
198                 return $valid;
199         }
200
201         /**
202          * Die Funktion liefer den aktuellen Primerastand
203          */
204         function getPrimera() {
205                 $primera = false;
206                 $PostData = array( "PrimusInterface_Action" => "GetPrimera" );
207                 $PostReturn = $this->parseContent( $this->queryApi($PostData) );
208
209                 $this->data = $PostReturn;
210                 if ($PostReturn["status"] == self::PI_GET_PRIMERA_DONE) {
211                         $primera = $PostReturn["primera"];
212                 }
213                 return $primera;
214         }
215 }
216
217 // [EOF]
218 ?>