/** * Initialize a Magister 6 account. * @param string $username - Username, used to log in to this account. */ public function __construct($username) { $this->interfaces = (object) array(); $this->rmi = Magister::getRMI(); $this->rmi->setAccountId($this->getInterface('Information')->getId()); $this->username = $username; }
/** * Log in to Magister 6. * @param string $username - Magister 6 username. * @param string $password - Magister 6 password. * @return {MagisterAccount} * @throws {MagisterException} */ public function login($school, $username, $password) { $rmi = Magister::getRMI(); $rmi->setSchool($school); $current = $this->get('/api/sessies/huidige', [], true, false); if (isset($current->state) && $current->state == 'active') { return new Account($username); } $this->delete('/api/sessies/huidige', [], true, false); $results = $this->post('/api/sessies', [], ['Gebruikersnaam' => $username, 'Wachtwoord' => $password, 'IngelogdBlijven' => true], true, false); if (isset($results->message)) { throw new MagisterException($results->message); } if (isset($results->omschrijving)) { throw new MagisterException($results->omschrijving); } return new Account($username); }
/** * Perform a HTTP request. * @param string $method - The HTTP method. * @param string $url - The URL to parse. * @param array $arguments - The arguments to replace in the URL. * @param object $data - An object representing what to send as JSON with * the request. Defaults to an empty object. * @param boolean $prependApi - Whether to prepend /api/personen/<id> to the * URL. Defaults to true. * @param boolean $newPrepend - Whether to to use /api/leerlingen/<id> * instead of /api/personen/<id> when using $prependApi. Defaults to false. * @return string|object */ protected function request($method, $url, $arguments, $data = [], $parse = true, $prependApi = true, $newPrepend = false) { $rmi = Magister::getRMI(); if ($prependApi) { $url = ($newPrepend ? '/api/leerlingen/' : '/api/personen/') . '/' . $rmi->getAccountId() . $url; } foreach ($arguments as $key => $value) { $url = str_replace(':' . $key, $value, $url); } $req = (new RawMagisterRequest())->setUrl($url)->setMethod($method); if ($method == 'POST' || $method == 'PUT') { $req->setBody(json_encode($data)); } $response = $req->send(); if ($parse) { return $response->getJson(); } return $response->getBody(); }
/** * Send the request. * @return {RawMagisterResponse} */ public function send() { $startTime = microtime(true); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://' . Magister::getRMI()->getSchool() . '.magister.net' . $this->url); curl_setopt($ch, CURLOPT_USERAGENT, Config::load()->connection_useragent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, Magister::getRMI()->getSessionFile()); curl_setopt($ch, CURLOPT_COOKIEFILE, Magister::getRMI()->getSessionFile()); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, Config::load()->connection_timeout_connect); curl_setopt($ch, CURLOPT_TIMEOUT, Config::load()->connection_timeout_total); switch ($this->method) { case 'GET': break; case 'POST': curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8')); break; case 'PUT': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8')); case 'DELETE': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); } /* Wait for a bit to prevent the firewall from hating us. */ usleep(Config::load()->connection_wait); /* Now download. */ $responseBody = curl_exec($ch); if (curl_errno($ch)) { throw new MagisterException('cURL Error (' . curl_errno($ch) . '): ' . curl_error($ch)); } curl_close($ch); $response = new RawMagisterResponse($this->method, $this->url, $responseBody, $this->body); Magister::getRMI()->history[] = (object) array('url' => $this->url, 'method' => $this->method, 'startTime' => $startTime, 'endTime' => microtime(true), 'body' => $this->body, 'response' => $response); return $response; }