New method detectServerAddress() added which should rewrite your code ito.
[core.git] / inc / classes / third_party / api / primusportal / class_PrimeraApi.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( "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 extends BaseFrameworkSystem {
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 __construct ($PPUsername, $PPPassword) {
125                 // Call parent constructor
126                 parent::__construct();
127
128                 // Clean up a little
129                 $this->removeSystemArray();
130                 $this->removeNumberFormaters();
131
132                 // Set data (DEPRECATED!)
133                 $this->username = $PPUsername;
134                 $this->password = $PPPassword;
135         }
136
137         /**
138          * Anfrage senden und Rückgabecode in Variable speichern
139          */
140         function queryApi ( $data = array() ) {
141                 $fp = fsockopen($this->host, 80, $this->errno, $this->_err);
142                 if (!$fp) return false;
143
144                 $data["PrimusInterface_Username"] = base64_encode($this->username);
145                 $data["PrimusInterface_Password"] = base64_encode(md5($this->password));
146
147                 // POST-Daten übermitteln:
148                 $data = http_build_query($data, '', '&');
149
150                 fputs($fp, "POST {$this->path}HTTP/1.1\r\n");
151                 fputs($fp, "Host: {$this->host}\r\n");
152                 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
153                 fputs($fp, "Content-length: ". strlen($data) ."\r\n");
154                 fputs($fp, "Connection: close\r\n\r\n");
155                 fputs($fp, $data);
156
157                 $return = '';
158                 while (!feof($fp)) {
159                         $return.=fgets($fp,128);
160                 }
161
162                 $content = explode("<!-- return-start -->", $return);
163                 return $content[1];
164         }
165
166         /**
167          * Funktion parst die Rückgabe vom Transferskript:
168          */
169         function parseContent ( $content ) {
170                 $x = explode("\n", $content);
171                 $return = array();
172                 foreach($x as $currentLine) {
173                         $line_exploded = explode($this->seperator, $currentLine,2);
174                         if (count($line_exploded) > 1) {
175                                 $return[$line_exploded[0]] = $line_exploded[1];
176                         }
177                 }
178                 return $return;
179         }
180
181         /**
182          * @param int/string $Receiver UserID / Username des Empfängers
183          * @param int$Amount Betrag in ganzzahligen Primera
184          * @param string $Description Beschreibung (Sichtbar in Einzelauflistung)
185          */
186         public function payPrimera ($Receiver, $Amount, $Description = '') {
187                 $valid = false;
188                 $PostData = array(
189                         'PrimusInterface_Action'      => 'Pay',
190                         'PrimusInterface_Receiver'    => base64_encode($Receiver),
191                         'PrimusInterface_Amount'      => base64_encode($Amount),
192                         'PrimusInterface_Description' => base64_encode($Description) );
193
194                 $PostReturn = $this->parseContent( $this->queryApi($PostData) );
195
196                 $this->data = $PostReturn;
197                 if ($PostReturn['status'] == '200') {
198                         $valid = true;
199                 }
200                 return $valid;
201         }
202
203         /**
204          * Überprüft den Status eines Primus-Users
205          * - existiert der User
206          * - ist er aktiv
207          * @param string/int $User Userid / Username
208          */
209         function CheckPrimusUser ($User) {
210                 $valid = false;
211                 $PostData = array(
212                         'PrimusInterface_Action'          => 'CheckPrimusUser',
213                         'PrimusInterface_CheckPrimusUser' => $User);
214
215                 $PostReturn = $this->parseContent( $this->queryApi($PostData) );
216
217                 $this->data = $PostReturn;
218
219                 if ($PostReturn['status'] == self::PI_USER_CHECK_OK) {
220                         $valid = true;
221                 }
222                 return $valid;
223         }
224
225         /**
226          * Die Funktion liefer den aktuellen Primerastand
227          */
228         function getPrimera() {
229                 $primera = false;
230                 $PostData = array( "PrimusInterface_Action" => "GetPrimera" );
231                 $PostReturn = $this->parseContent( $this->queryApi($PostData) );
232
233                 $this->data = $PostReturn;
234                 if ($PostReturn['status'] == self::PI_GET_PRIMERA_DONE) {
235                         $primera = $PostReturn["primera"];
236                 }
237                 return $primera;
238         }
239 }
240
241 // [EOF]
242 ?>