]> git.mxchange.org Git - core.git/blob - inc/main/third_party/api/primusportal/class_PrimeraApi.php
Continued:
[core.git] / inc / main / third_party / api / primusportal / class_PrimeraApi.php
1 <?php
2 // Own namespace
3 namespace PrimeraPortal\Api;
4
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7
8 /**
9  * PrimeraApi
10  * -------------------------------------------
11  * Mit dieser Klasse ist ein einfacher Primeratransfer von Ihrem Account
12  * zu dem Account eines bei Primusportal.de registrierten Mitglieds m�glich.
13  *
14  * ----------------- Aenderungen durch Roland Haeder 09.08.2008 ---------------
15  * Klasse umbenannt nach PrimeraApi, damit sie in das Framework besser passt.
16  * Zudem sind alle oeffentlichen Attribute nun privat, der Konstruktor hat den
17  * neuen "magischen" Namen __construct() und "normale" Konstanten sind nach
18  * Klassenkonstanten umbenannt. Unsinnige else-Bloecke sind noch umgewandelt.
19  * Methodennamen fangen nun immer mit einem kleinen Buchstaben an. Zudem sind
20  * die Methoden Pay,Query und Parse umbenannt.
21  * ----------------- Aenderungen durch Roland Haeder 09.08.2008 ---------------
22  *
23  * ----------------- Aenderungen durch Roland Haeder 19.04.2011 ---------------
24  * Es ist nun bei allen Methoden- und Feldnamen der erste Buchstabe
25  * kleingeschrieben, damit es besser in meine Namenskonvention passt. Noch mehr
26  * doppelte Anfuehrungszeichen in einfache umgewandelt, damit die Klasse
27  * performanter (wegen sonst doppeltes Parsen) wird.
28  * ----------------- Aenderungen durch Roland Haeder 19.04.2011 ---------------
29  *
30  * ------------ Achtung! Bitte zuerst lesen, bevor Sie weiterlesen: -----------
31  * Das meiste aus der unteren Anleitung sollte auf die hier verwendeten Namen
32  * portiert sein. Falls Sie Fragen haben, bitte melden Sie sich bei Roland
33  * Haeder. Funktionell wurde hier aber nichts geaendert.
34  * ------------ Achtung! Bitte zuerst lesen, bevor Sie weiterlesen: -----------
35  *
36  * Die Einbindung des Interfaces geschieht folgenderma�en:
37  *  1. Einbindung der Klasse in Ihr PHP-Auszahlungsskript:
38  *     CODE:
39  *       $primusUsername = 'username'; // Ihr Username bei Primusportal
40  *       $primusPassword = 'passwort'; // Ihr Passwort bei Primusportal
41  *
42  *       $apiInstance = ObjectFactory::createObjectByName('PrimeraApi', array($primusUsername, $primusPassword));
43  *  2. Durchf�hren einer Auszahlung:
44  *     CODE:
45  *       $Status = $apiInstance->payPrimera($PayReceiver, $PayAmount, $PayDescription);
46  *
47  *     Wobei $PayReicer der Username des Empf�ngers bei
48  *     Primusportal.de ist. $PayAmount ist der gerundete( !! ) Betrag an Primera,
49  *     die der Empf�nger erhalten soll. $PayDescription ist eine von Ihnen 
50  *     festgelegte kurze Beschreibung. Die L�nge dieses Textes darf 100 Zeichen
51  *     nicht �berschreiten. Beispiel:
52  *       $status = $apiInstance->payPrimera('garbage', 10000, 'Auszahlung IhreSeite.de - ID: 12345');
53  *  3. �berpr�fung des Status (R�ckgabecode):
54  *     CODE:
55  *       if ($status === FALSE) {
56  *         // Ein Fehler ist aufgetreten
57  *     // Fehlerbehandlung hier einf�gen...
58  *       } else {
59  *         // Auszahlung erfolgreich durchgef�hrt
60  *         // F�hren Sie hier Ihre Datenbankabfragen durch, um die Auszahlung zu
61  *         // best�tigen...
62  *       }
63  *
64  *     Die komplette R�ckgabe des Interfaces wird als assoziatives Array in der Klassen-
65  *     variable $data gespeichert:
66  *     $data = array(
67  *           'status' => R�ckgabecode (PI_DONE, PI_SENDER_ERROR, ...),
68  *           'statustext' => Status in Worten (z.B.: 'Transaktion erfolgreich durchgef�hrt'),
69  *     );
70  *
71  *
72  * @author              Andreas Schmidt <xxgarbagexx@web.de>
73  * @author              Roland Haeder <webmaster.shipsimu.org>
74  * @version             1.0 - beta
75  * @copyright   (c) 2007 by Primusportal.de
76  * @copyright   (c) 2008, 2011 by Roland Haeder
77  */
78 class PrimeraApi extends BaseFrameworkSystem {
79         /**
80          * Fehler - Interfacebenutzer
81          */
82         const PI_ERROR = -1;
83
84         /**
85          * Statuscode f�r erfolgreich ausgef�hrte Transaktion
86          */
87         const PI_DONE = 200;
88
89         /**
90          * Fehler - User existiert nicht oder ist gesperrt
91          */
92         const PI_RECEIVER_ERROR = 301;
93
94         /**
95          * Sender-Account Fehler (User nicht existent, gesperrt, ...)
96          */
97         const PI_SENDER_ERROR = 401;
98
99         /**
100          * Betrag fehler
101          */
102         const PI_AMOUNT_ERROR = 501;
103
104         /**
105          * Zu wenig Primera
106          */
107         const PI_TOO_LESS_PRIMERA = 502;
108
109         /**
110          * User nicht aktiv oder existiert nicht
111          */
112         const PI_USER_CHECK_ERROR = 601;
113
114         /**
115          * User aktiv
116          */
117         const PI_USER_CHECK_OK = 602;
118
119         /**
120          * Primerastand erfolgreich geholt
121          */
122         const PI_GET_PRIMERA_DONE = 701;
123
124         /**
125          * HTTP-EOL
126          */
127         const HTTP_EOL = "\r\n";
128
129         /**
130          * URL f�r das Interface auf dem Primusserver:
131          */
132         private $host = 'www.primusportal.de';
133         private $path = '/transfer.interface.2.0.php';
134
135         private $errno = 0;
136         private $err = '';
137
138         private $separator = ':';
139
140         private $username = '';
141         private $password = '';
142
143         private $data = array();
144
145         /**
146          * Konstruktor
147          */
148         public function __construct ($primusUsername, $primusPassword) {
149                 // Call parent constructor
150                 parent::__construct();
151
152                 // Set data
153                 $this->username = $primusUsername;
154                 $this->password = $primusPassword;
155         }
156
157         /**
158          * Anfrage senden und Rueckgabecode in Variable speichern
159          */
160         private function queryApi ( $data = array() ) {
161                 $fp = fsockopen($this->host, 80, $this->errno, $this->_err);
162                 if (!$fp) return false;
163
164                 $data['PrimusInterface_Username'] = base64_encode($this->username);
165                 $data['PrimusInterface_Password'] = base64_encode(md5($this->password));
166
167                 // POST-Daten uebermitteln:
168                 $queryData = http_build_query($data, '', '&');
169
170                 $request .= 'POST ' . $this->path . 'HTTP/1.1' . self::HTTP_EOL;
171                 $request .= 'Host: ' . $this->host . self::HTTP_EOL;
172                 $request .= 'Content-type: application/x-www-form-urlencoded' . self::HTTP_EOL;
173                 $request .= 'Content-length: '. strlen($queryData) . self::HTTP_EOL;
174                 $request .= 'Connection: close' . self::HTTP_EOL;
175                 $request .= self::HTTP_EOL;
176                 $request .= $queryData;
177
178                 fputs($fp, $request);
179
180                 $return = '';
181                 while (!feof($fp)) {
182                         $return .= fgets($fp, 128);
183                 } // END - while
184
185                 $content = explode('<!-- return-start -->', $return);
186                 return $content[1];
187         }
188
189         /**
190          * Funktion parst die R�ckgabe vom Transferskript:
191          */
192         private function parseContent ( $content ) {
193                 $x = explode("\n", $content);
194                 $return = array();
195                 foreach($x as $currentLine) {
196                         $line_exploded = explode($this->separator, $currentLine,2);
197                         if (count($line_exploded) > 1) {
198                                 $return[$line_exploded[0]] = $line_exploded[1];
199                         } // END - if
200                 } // END - foreach
201                 return $return;
202         }
203
204         /**
205          * @param int/string $Receiver UserID / Username des Empf�ngers
206          * @param int$Amount Betrag in ganzzahligen Primera
207          * @param string $Description Beschreibung (Sichtbar in Einzelauflistung)
208          */
209         public function payPrimera ($Receiver, $Amount, $Description = '') {
210                 $valid = FALSE;
211                 $postData = array(
212                         'PrimusInterface_Action'      => 'Pay',
213                         'PrimusInterface_Receiver'    => base64_encode($Receiver),
214                         'PrimusInterface_Amount'      => base64_encode($Amount),
215                         'PrimusInterface_Description' => base64_encode($Description)
216                 );
217
218                 $postReturn = $this->parseContent( $this->queryApi($postData) );
219
220                 $this->data = $postReturn;
221                 if ($postReturn['status'] == '200') {
222                         $valid = TRUE;
223                 } // END - if
224                 return $valid;
225         }
226
227         /**
228          * �berpr�ft den Status eines Primus-Users
229          * - existiert der User
230          * - ist er aktiv
231          * @param string/int $User Userid / Username
232          */
233         public function checkPrimusUser ($userName) {
234                 $valid = FALSE;
235                 $postData = array(
236                         'PrimusInterface_Action'          => 'CheckPrimusUser',
237                         'PrimusInterface_CheckPrimusUser' => $userName
238                 );
239
240                 $postReturn = $this->parseContent( $this->queryApi($postData) );
241
242                 $this->data = $postReturn;
243
244                 if ($postReturn['status'] == self::PI_USER_CHECK_OK) {
245                         $valid = TRUE;
246                 } // END - if
247                 return $valid;
248         }
249
250         /**
251          * Die Funktion liefer den aktuellen Primerastand
252          */
253         public function getPrimera() {
254                 $primera = FALSE;
255                 $postData = array(
256                         'PrimusInterface_Action' => 'GetPrimera'
257                 );
258                 $postReturn = $this->parseContent( $this->queryApi($postData) );
259
260                 $this->data = $postReturn;
261
262                 if ($postReturn['status'] == self::PI_GET_PRIMERA_DONE) {
263                         $primera = $postReturn['primera'];
264                 } // END - if
265
266                 return $primera;
267         }
268
269 }