From c781a551ae84127ef05eaa36909dca44e49e1200 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 19 Apr 2017 23:43:24 +0200 Subject: [locationinfo] Better backend-specific property handling, get rid of URL - The backend URL still had special treatment for legacy reasons, when it would be perfectly fine to make it just another generic property the backend has to define. - Allow for the backend to declare a default value for properties. - Base class will now check and sanitize the setCredentials() input. --- .../locationinfo/inc/coursebackend.inc.php | 47 +++++++++++--- .../coursebackend/coursebackend_davinci.inc.php | 12 ++-- .../inc/coursebackend/coursebackend_dummy.inc.php | 17 +++-- .../coursebackend/coursebackend_hisinone.inc.php | 75 +++++++++++++--------- 4 files changed, 98 insertions(+), 53 deletions(-) (limited to 'modules-available/locationinfo/inc') diff --git a/modules-available/locationinfo/inc/coursebackend.inc.php b/modules-available/locationinfo/inc/coursebackend.inc.php index 7dc50549..0d84b0fb 100644 --- a/modules-available/locationinfo/inc/coursebackend.inc.php +++ b/modules-available/locationinfo/inc/coursebackend.inc.php @@ -22,10 +22,6 @@ abstract class CourseBackend * @var int as internal serverId */ protected $serverId; - /** - * @var string url of the service - */ - protected $location; /** * @const int max number of additional locations to fetch (for backends that benefit from request coalesc.) */ @@ -36,7 +32,6 @@ abstract class CourseBackend */ public final function __construct() { - $this->location = ""; $this->error = false; } @@ -97,7 +92,7 @@ abstract class CourseBackend /** - * @returns array with parameter name as key and and an array with type, help text and mask as value + * @returns \BackendProperty[] list of properties that need to be set */ public abstract function getCredentials(); @@ -110,12 +105,10 @@ abstract class CourseBackend * uses json to setCredentials, the json must follow the form given in * getCredentials * - * @param array $data with the credentials - * @param string $url address of the server - * @param int $serverId ID of the server + * @param array $data assoc array with data required by backend * @returns bool if the credentials were in the correct format */ - public abstract function setCredentials($data, $url, $serverId); + public abstract function setCredentialsInternal($data); /** * @return int desired caching time of results, in seconds. 0 = no caching @@ -215,6 +208,25 @@ abstract class CourseBackend return $returnValue; } + public final function setCredentials($serverId, $data) + { + foreach ($this->getCredentials() as $prop) { + if (!isset($data[$prop->property])) { + $data[$prop->property] = $prop->default; + } + if (in_array($prop->type, ['string', 'bool', 'int'])) { + settype($data[$prop->property], $prop->type); + } else { + settype($data[$prop->property], 'string'); + } + } + if ($this->setCredentialsInternal($data)) { + $this->serverId = $serverId; + return true; + } + return false; + } + /** * @return false if there was no error string with error message if there was one */ @@ -297,3 +309,18 @@ abstract class CourseBackend } } + +/** + * Class BackendProperty describes a property a backend requires to define its functionality + */ +class BackendProperty { + public $property; + public $type; + public $default; + public function __construct($property, $type, $default = '') + { + $this->property = $property; + $this->type = $type; + $this->default = $default; + } +} diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php index db75c19d..77928b39 100644 --- a/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php @@ -3,14 +3,16 @@ class CourseBackend_Davinci extends CourseBackend { - public function setCredentials($data, $location, $serverId) + private $location; + + public function setCredentialsInternal($data) { - if (empty($location)) { + if (empty($data['baseUrl'])) { $this->error = "No url is given"; return false; } + $location = preg_replace('#/+(davinciis\.dll)?\W*$#i', '', $data['baseUrl']); $this->location = $location . "/DAVINCIIS.dll?"; - $this->serverId = $serverId; //Davinci doesn't have credentials return true; } @@ -30,7 +32,9 @@ class CourseBackend_Davinci extends CourseBackend public function getCredentials() { - return array(); + return [ + new BackendProperty('baseUrl', 'string') + ]; } public function getDisplayName() diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php index 5bceac3e..b8329196 100644 --- a/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_dummy.inc.php @@ -13,7 +13,7 @@ class CourseBackend_Dummy extends CourseBackend * @param int $serverId ID of the server * @returns bool if the credentials were in the correct format */ - public function setCredentials($json, $location, $serverId) + public function setCredentialsInternal($json) { $x = $json; $this->pw = $x['password']; @@ -47,15 +47,14 @@ class CourseBackend_Dummy extends CourseBackend public function getCredentials() { $options = ["opt1", "opt2", "opt3", "opt4", "opt5", "opt6", "opt7", "opt8"]; - $credentials = [ - "username" => "string", - "password" => "password", - "integer" => "int", - "option" => $options, - "CheckTheBox" => "bool", - "CB2 t" => "bool" + return [ + new BackendProperty('username', 'string', 'default-user'), + new BackendProperty('password', 'password'), + new BackendProperty('integer', 'int', 7), + new BackendProperty('option', $options), + new BackendProperty('CheckTheBox', 'bool'), + new BackendProperty('CB2t', 'bool', true) ]; - return $credentials; } /** diff --git a/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php b/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php index 9e7c9db0..b01146a8 100644 --- a/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php +++ b/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php @@ -2,36 +2,59 @@ class CourseBackend_HisInOne extends CourseBackend { - private $username; - private $password; - private $open; + private $username = ''; + private $password = ''; + private $open = true; + private $location; + private $verifyHostname = true; + private $verifyCert = true; - public function setCredentials($data, $url, $serverId) + public function setCredentialsInternal($data) { - if (array_key_exists('password', $data) && array_key_exists('username', $data) && array_key_exists('role', $data) && isset($data['open'])) { - $this->error = false; - $this->password = $data['password']; - $this->username = $data['username'] . "\t" . $data['role']; - $this->open = $data['open']; - if ($url == "") { - $this->error = "No url is given"; - return false; - } - if ($this->open) { - $this->location = $url . "/qisserver/services2/OpenCourseService"; - } else { - $this->location = $url . "/qisserver/services2/CourseService"; + if (!$data['open']) { + // If not using OpenCourseService, require credentials + foreach (['username', 'password'] as $field) { + if (empty($data[$field])) { + $this->error = 'setCredentials: Missing field ' . $field; + return false; + } } - $this->serverId = $serverId; - } else { - $this->error = "wrong credentials"; + } + if (empty($data['baseUrl'])) { + $this->error = "No url is given"; return false; } + $this->error = false; + $this->username = $data['username'] . "\t" . $data['role']; + $this->password = $data['password']; + $this->open = $data['open']; + $url = preg_replace('#(/+qisserver(/+services\d+(/+OpenCourseService)?)?)?\W*$#i', '', $data['baseUrl']); + if ($this->open) { + $this->location = $url . "/qisserver/services2/OpenCourseService"; + } else { + $this->location = $url . "/qisserver/services2/CourseService"; + } + $this->verifyHostname = $data['verifyHostname']; + $this->verifyCert = $data['verifyCert']; + return true; } + public function getCredentials() + { + return [ + new BackendProperty('baseUrl', 'string'), + new BackendProperty('username', 'string'), + new BackendProperty('role', 'string'), + new BackendProperty('password', 'password'), + new BackendProperty('open', 'bool', true), + new BackendProperty('verifyCert', 'bool', true), + new BackendProperty('verifyHostname', 'bool', true) + ]; + } + public function checkConnection() { if (empty($this->location)) { @@ -159,8 +182,8 @@ class CourseBackend_HisInOne extends CourseBackend $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, - CURLOPT_SSL_VERIFYHOST => false, - CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => $this->verifyHostname, + CURLOPT_SSL_VERIFYPEER => $this->verifyCert, CURLOPT_URL => $this->location, CURLOPT_POSTFIELDS => $request, CURLOPT_HTTPHEADER => $header, @@ -197,14 +220,6 @@ class CourseBackend_HisInOne extends CourseBackend return "HisInOne"; } - - public function getCredentials() - { - $credentials = ["username" => "string", "role" => "string", "password" => "password", "open" => "bool"]; - return $credentials; - } - - public function fetchSchedulesInternal($requestedRoomIds) { if (empty($requestedRoomIds)) { -- cgit v1.2.3-55-g7522