Skip to content
Snippets Groups Projects
Commit f3a9aa15 authored by Moritz Strohm's avatar Moritz Strohm
Browse files

improved code quality and compatibility with PHP 8.2

parent af84da31
No related branches found
No related tags found
No related merge requests found
......@@ -16,14 +16,14 @@ class OwnCloudPlugin extends StudIPPlugin implements FilesystemPlugin {
parent::__construct();
}
public function getFileSelectNavigation()
public function getFileSelectNavigation() : Navigation
{
$nav = new Navigation(Config::get()->OWNCLOUD_NAME);
$nav->setImage(Icon::create("cloud", "clickable"));
return $nav;
}
public function getFolder($folder_id = null)
public function getFolder($folder_id = null) : ?OwncloudFolder
{
if (Navigation::hasItem('/files/my_files')) {
Navigation::activateItem('/files/my_files');
......@@ -40,14 +40,16 @@ class OwnCloudPlugin extends StudIPPlugin implements FilesystemPlugin {
$folder_path = explode("/", $folder_id);
$name = rawurldecode(array_pop($folder_path));
$parent_folder_id = implode("/", $folder_path);
$folder = new OwncloudFolder(array(
'id' => $folder_id,
'name' => $name,
'parent_id' => $parent_folder_id,
'range_type' => $this->getPluginId(),
'range_id' => $this->getPluginName()
), $this->getPluginId());
return $folder;
return new OwncloudFolder(
[
'id' => $folder_id,
'name' => $name,
'parent_id' => $parent_folder_id,
'range_type' => $this->getPluginId(),
'range_id' => $this->getPluginName()
],
$this->getPluginId()
);
}
public function download_action()
......@@ -83,15 +85,14 @@ class OwnCloudPlugin extends StudIPPlugin implements FilesystemPlugin {
header("Content-Length: ".$info['size_download']);
header("Content-Type: ".$info['content_type']);
echo $content;
return;
}
/**
* @param $file_id : this is a path to the file. Remind that the chunks of this path need to be rawurlencoded!
* @param bool $with_blob : should OwnCloudPlugin retrieve the blob of the file as well?
* @return FileRef|null : Returns the FileRef on success and null if the file doesn't exist.
* @param $file_id This is a path to the file. Remind that the chunks of this path need to be rawurlencoded!
* @param bool $with_blob Should OwnCloudPlugin retrieve the blob of the file as well?
* @return FileType|null Returns the FileRef on success and null if the file doesn't exist.
*/
public function getPreparedFile($file_id, $with_blob = false)
public function getPreparedFile($file_id, $with_blob = false) : ?FileType
{
if (!$this->isFile($file_id)) {
return null;
......@@ -188,7 +189,7 @@ class OwnCloudPlugin extends StudIPPlugin implements FilesystemPlugin {
if ($prop->childNodes) {
foreach ($prop->childNodes as $attr) {
if (strtolower($attr->tagName) === "d:resourcetype") {
return $file_attributes['type'] = ($attr->childNodes[0] && strtolower($attr->childNodes[0]->tagName) === "d:collection")
return ($attr->childNodes[0] && strtolower($attr->childNodes[0]->tagName) === "d:collection")
? "folder"
: "file";
}
......@@ -199,15 +200,15 @@ class OwnCloudPlugin extends StudIPPlugin implements FilesystemPlugin {
}
}
}
return $file_attributes['type'];
return '';
}
protected function isFolder($id)
protected function isFolder(string $id) : bool
{
return $this->getType($id) == 'folder';
}
protected function isFile($id)
protected function isFile(string $id) : bool
{
return $this->getType($id) == 'file';
}
......
......@@ -4,13 +4,13 @@ namespace Owncloud;
class OAuth {
static public function request($url, $type = "get", $payload = array())
static public function request(string $url, string $type = "get", array $payload = []) : string
{
$header = array();
$header = [];
$accessToken = self::getAccessToken();
if (!$accessToken) {
throw new Exception(sprintf("No valid access token. Please refresh your connection to %s.", Config::get()->OWNCLOUD_NAME));
throw new \Exception(sprintf("No valid access token. Please refresh your connection to %s.", Config::get()->OWNCLOUD_NAME));
}
$header[] = OwnCloudFolder::getAuthHeader();
......@@ -30,11 +30,14 @@ class OAuth {
$result = curl_exec($r);
curl_close($r);
if ($result === false) {
return '';
}
return $result;
}
static public function isReady()
static public function isReady() : bool
{
return self::hasAccessToken() || \Config::get()->OWNCLOUD_REFRESH_TOKEN;
}
......@@ -51,13 +54,13 @@ class OAuth {
}
}
static public function hasAccessToken()
static public function hasAccessToken() : bool
{
return \UserConfig::get($GLOBALS['user']->id)->OWNCLOUD_ACCESS_TOKEN
&& (\UserConfig::get($GLOBALS['user']->id)->OWNCLOUD_ACCESS_TOKEN_EXPIRES >= time());
}
static public function refreshAccessToken()
static public function refreshAccessToken() : void
{
$owncloud = \Config::get()->OWNCLOUD_ENDPOINT ?: \UserConfig::get($GLOBALS['user']->id)->OWNCLOUD_ENDPOINT_USER;
if ($owncloud[strlen($owncloud) - 1] !== "/") {
......@@ -136,7 +139,7 @@ class OAuth {
}
}
static public function removeAccessToken()
static public function removeAccessToken() : void
{
\UserConfig::get($GLOBALS['user']->id)->delete("OWNCLOUD_ACCESS_TOKEN");
\UserConfig::get($GLOBALS['user']->id)->delete("OWNCLOUD_REFRESH_TOKEN");
......
......@@ -65,7 +65,7 @@ class OwncloudFile implements FileType
/**
* @returns The User object representing the author.
* @returns User The User object representing the author.
*/
public function getUser()
{
......@@ -108,7 +108,7 @@ class OwncloudFile implements FileType
* on the server disk. Other implementations shall just return
* an empty string.
*
* @returns The file system path for the file or an empty string if the
* @returns string The file system path for the file or an empty string if the
* file doesn't have a path in the file system.
*/
public function getPath() : string
......@@ -212,7 +212,7 @@ class OwncloudFile implements FileType
'file-copy'
);
$actionMenu->addLink(
$this->getDownloadURL('force_download'),
$this->getDownloadURL(),
_('Link kopieren'),
Icon::create('group'),
['class' => 'copyable-link'],
......@@ -315,7 +315,7 @@ class OwncloudFile implements FileType
/**
* Returns an object of the class StandardFile or a derived class.
* @return StandardFile
* @return StandardFile|array Either the resulting StandardFile or an array with all errors.
*/
public function convertToStandardFile()
{
......@@ -339,7 +339,6 @@ class OwncloudFile implements FileType
$content = curl_exec($r);
$info = curl_getinfo($r);
curl_close($r);
$path = $GLOBALS['TMP_PATH']."/owncloudplugin_".md5(uniqid());
file_put_contents(
......@@ -418,7 +417,7 @@ class OwncloudFile implements FileType
{
$webdav = OwnCloudFolder::getWebDavURL();
$file_path = $this->getFolderType()->id . (mb_strlen($this->getFolderType()->id) ? '/' : '') . rawurlencode($this->getFilename());
$file_path = $this->getFolderType()->getId() . (mb_strlen($this->getFolderType()->getId()) ? '/' : '') . rawurlencode($this->getFilename());
$header = array();
$header[] = OwnCloudFolder::getAuthHeader();
......@@ -438,7 +437,6 @@ class OwncloudFile implements FileType
curl_setopt($r, CURLOPT_VERBOSE, true);
}
curl_exec($r);
$status = curl_getinfo($r, CURLINFO_HTTP_CODE);
curl_close($r);
fclose($fh_res);
......
......@@ -2,7 +2,7 @@
class OwncloudFolder extends VirtualFolderType {
protected $did_propfind = false;
protected bool $did_propfind = false;
public static function availableInRange($range_id_or_object, $user_id)
{
......@@ -24,7 +24,7 @@ class OwncloudFolder extends VirtualFolderType {
return true;
}
public function isFileDownloadable($file_id, $user_id)
public function isFileDownloadable($fileref_or_id, $user_id)
{
return true;
}
......@@ -49,7 +49,7 @@ class OwncloudFolder extends VirtualFolderType {
$webdav = $this->getWebDavURL();
$header = array();
$header[] = self::getAuthHeader();
$header[] = "Destination: ". $webdav . $this->id;
$header[] = "Destination: ". $webdav . $this->getId();
$r = curl_init();
curl_setopt($r, CURLOPT_CUSTOMREQUEST, "MOVE");
......@@ -73,7 +73,7 @@ class OwncloudFolder extends VirtualFolderType {
public function delete()
{
return $this->deleteFile($this->id);
return $this->deleteFile($this->getId());
}
public function deleteFile($file_ref_id)
......@@ -109,6 +109,7 @@ class OwncloudFolder extends VirtualFolderType {
if ($this->fileExists($file->getFilename())) {
$file_name = $file->getFilename();
$end = '';
if (strpos($file_name, ".")) {
$end = substr($file_name, strpos($file_name, "."));
$name_raw = substr($file_name, 0, strpos($file_name, "."));
......@@ -145,7 +146,6 @@ class OwncloudFolder extends VirtualFolderType {
curl_setopt($r, CURLOPT_VERBOSE, true);
}
curl_exec($r);
$status = curl_getinfo($r, CURLINFO_HTTP_CODE);
curl_close($r);
fclose($fh_res);
......@@ -161,6 +161,7 @@ class OwncloudFolder extends VirtualFolderType {
$name = rawurldecode(end($tmp_parts));
if ($this->fileExists($name)) {
$end = '';
if (strpos($name, ".")) {
$end = substr($name, strpos($name, "."));
$name_raw = substr($name, 0, strpos($name, "."));
......@@ -193,7 +194,6 @@ class OwncloudFolder extends VirtualFolderType {
}
curl_exec($r);
$status = curl_getinfo($r, CURLINFO_HTTP_CODE);
curl_close($r);
$plugin = PluginManager::getInstance()->getPlugin("OwnCloudPlugin");
......@@ -208,6 +208,7 @@ class OwncloudFolder extends VirtualFolderType {
$name = end($tmp_parts);
if ($this->fileExists($name)) {
$end = '';
if (strpos($name, ".")) {
$end = substr($name, strpos($name, "."));
$name_raw = substr($name, 0, strpos($name, "."));
......@@ -239,7 +240,6 @@ class OwncloudFolder extends VirtualFolderType {
}
curl_exec($r);
$status = curl_getinfo($r, CURLINFO_HTTP_CODE);
curl_close($r);
$plugin = PluginManager::getInstance()->getPlugin("OwnCloudPlugin");
......@@ -254,6 +254,7 @@ class OwncloudFolder extends VirtualFolderType {
$webdav = $this->getWebDavURL();
if ($this->fileExists(rawurlencode($name))) {
$end = '';
if (strpos($name, ".")) {
$end = substr($name, strpos($name, "."));
$name_raw = substr($name, 0, strpos($name, "."));
......@@ -285,25 +286,24 @@ class OwncloudFolder extends VirtualFolderType {
}
curl_exec($r);
$status = curl_getinfo($r, CURLINFO_HTTP_CODE);
curl_close($r);
$plugin = PluginManager::getInstance()->getPlugin("OwnCloudPlugin");
return $plugin->getPreparedFile($destination);
}
public function createSubfolder(FolderType $foldertype)
public function createSubfolder(FolderType $folderdata)
{
$webdav = $this->getWebDavURL();
if ($foldertype instanceof StandardFolder) {
$name = rawurlencode($foldertype->name);
if ($folderdata instanceof StandardFolder) {
$name = rawurlencode($folderdata->name);
} else {
if ($foldertype->getId()) {
$name = explode('/', $foldertype->getId());
if ($folderdata->getId()) {
$name = explode('/', $folderdata->getId());
$name = end($name);
} else {
$name = rawurlencode($foldertype->name);
$name = rawurlencode($folderdata->name);
}
}
if ($this->folderExists($name)) {
......@@ -340,17 +340,16 @@ class OwncloudFolder extends VirtualFolderType {
return (($status >= 200) && ($status < 300)) ? $plugin->getFolder($destination) : false;
}
public static function getWebDavURL()
public static function getWebDavURL() : string
{
$url = Config::get()->OWNCLOUD_ENDPOINT ?: UserConfig::get($GLOBALS['user']->id)->OWNCLOUD_ENDPOINT_USER;
if ($url[strlen($url) - 1] !== "/") {
$url .= "/";
}
$webdav = $url . "remote.php/webdav/";
return $webdav;
return $url . "remote.php/webdav/";
}
protected function fetchObjects()
protected function fetchObjects() : void
{
if ($this->did_propfind === true) {
return;
......
......@@ -16,7 +16,7 @@ class ConfigureController extends PluginController
$config->unsetValue("OWNCLOUD_REFRESH_TOKEN");
$config->unsetValue("OWNCLOUD_ACTIVATED");
$config->unsetValue("OWNCLOUD_ACCESS_TOKEN_EXPIRES");
PageLayout::postMessage(MessageBox::success(_("Zugang zur Owncloud wurde aufgehoben.")));
PageLayout::postSuccess(_("Zugang zur Owncloud wurde aufgehoben."));
$this->redirect(URLHelper::getURL("dispatch.php/files"));
return;
}
......@@ -36,8 +36,7 @@ class ConfigureController extends PluginController
$this->redirect("oauth/request_access_token");
}
}
PageLayout::postMessage(MessageBox::success(_("Konfiguration gespeichert.")));
return;
PageLayout::postSuccess(_("Konfiguration gespeichert."));
}
}
}
......@@ -45,16 +45,16 @@ class OauthController extends PluginController
URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']);
$redirect_uri = PluginEngine::getURL($this->plugin, array(), "oauth/receive_access_token", true);
$payload = array(
$payload = [
'grant_type' => "authorization_code",
'code' => Request::get("code"),
'redirect_uri' => $redirect_uri,
'client_id' => $client_id,
'client_secret' => $client_secret,
'format' => "json"
);
];
$header = array();
$header = [];
$header[] = "Accept: application/json";
$header[] = "Authorization: Basic ".base64_encode($client_id . ":" .$client_secret);
......@@ -76,18 +76,13 @@ class OauthController extends PluginController
$json = json_decode($response, true);
if ($json['error']) {
if (!empty($json['error'])) {
PageLayout::postError(_("Authentifizierungsfehler:")." ".$json['error']);
$this->redirect(URLHelper::getURL("dispatch.php/files/index"));
} else {
if ($response === false) {
PageLayout::postError(_("Fehler beim Abrufen der OAuth-Token:"), array(curl_error($r)));
}
if (false) {
var_dump($json);
$this->render_nothing();
return;
}
$config = \UserConfig::get($GLOBALS['user']->id);
$config->store("OWNCLOUD_ACCESS_TOKEN", $json['access_token']);
$config->store("OWNCLOUD_REFRESH_TOKEN", $json['refresh_token']);
......
......@@ -4,30 +4,30 @@ class InitPlugin extends Migration
{
public function up()
{
$configs = array("OWNCLOUD_ENDPOINT", "OWNCLOUD_CLIENT_ID", "OWNCLOUD_CLIENT_SECRET");
$userconfigs = array("OWNCLOUD_ENDPOINT_USER", "OWNCLOUD_CLIENT_ID_USER", "OWNCLOUD_CLIENT_SECRET_USER", "OWNCLOUD_ACTIVATED", "OWNCLOUD_ACCESS_TOKEN", "OWNCLOUD_ACCESS_TOKEN_EXPIRES", "OWNCLOUD_REFRESH_TOKEN");
$configs = ["OWNCLOUD_ENDPOINT", "OWNCLOUD_CLIENT_ID", "OWNCLOUD_CLIENT_SECRET"];
$userconfigs = ["OWNCLOUD_ENDPOINT_USER", "OWNCLOUD_CLIENT_ID_USER", "OWNCLOUD_CLIENT_SECRET_USER", "OWNCLOUD_ACTIVATED", "OWNCLOUD_ACCESS_TOKEN", "OWNCLOUD_ACCESS_TOKEN_EXPIRES", "OWNCLOUD_REFRESH_TOKEN"];
foreach ($configs as $config) {
Config::get()->create($config, array(
Config::get()->create($config, [
'value' => "",
'type' => "string",
'range' => "global",
'section' => "Owncloud"
));
]);
}
foreach ($userconfigs as $config) {
Config::get()->create($config, array(
Config::get()->create($config, [
'value' => "",
'type' => in_array($config, ["OWNCLOUD_ACTIVATED"]) ? "boolean" : "string",
'range' => "user",
'section' => "Owncloud"
));
]);
}
}
public function down()
{
$configs = array("OWNCLOUD_ENDPOINT", "OWNCLOUD_CLIENT_ID", "OWNCLOUD_CLIENT_SECRET");
$userconfigs = array("OWNCLOUD_ENDPOINT_USER", "OWNCLOUD_CLIENT_ID_USER", "OWNCLOUD_CLIENT_SECRET_USER", "OWNCLOUD_ACTIVATED", "OWNCLOUD_ACCESS_TOKEN", "OWNCLOUD_ACCESS_TOKEN_EXPIRES", "OWNCLOUD_REFRESH_TOKEN");
$configs = ["OWNCLOUD_ENDPOINT", "OWNCLOUD_CLIENT_ID", "OWNCLOUD_CLIENT_SECRET"];
$userconfigs = ["OWNCLOUD_ENDPOINT_USER", "OWNCLOUD_CLIENT_ID_USER", "OWNCLOUD_CLIENT_SECRET_USER", "OWNCLOUD_ACTIVATED", "OWNCLOUD_ACCESS_TOKEN", "OWNCLOUD_ACCESS_TOKEN_EXPIRES", "OWNCLOUD_REFRESH_TOKEN"];
foreach ($configs as $config) {
Config::get()->delete($config);
}
......@@ -35,4 +35,4 @@ class InitPlugin extends Migration
Config::get()->delete($config);
}
}
}
\ No newline at end of file
}
......@@ -4,16 +4,16 @@ class AddUnsecureSsl extends Migration
{
public function up()
{
Config::get()->create("OWNCLOUD_SSL_VERIFYPEER", array(
Config::get()->create("OWNCLOUD_SSL_VERIFYPEER", [
'value' => "1",
'type' => "boolean",
'range' => "global",
'section' => "Owncloud"
));
]);
}
public function down()
{
Config::get()->delete("OWNCLOUD_SSL_VERIFYPEER");
}
}
\ No newline at end of file
}
......@@ -4,16 +4,16 @@ class AddCustomName extends Migration
{
public function up()
{
Config::get()->create("OWNCLOUD_NAME", array(
Config::get()->create("OWNCLOUD_NAME", [
'value' => "OwnCloud",
'type' => "string",
'range' => "global",
'section' => "Owncloud"
));
]);
}
public function down()
{
Config::get()->delete("OWNCLOUD_NAME");
}
}
\ No newline at end of file
}
<form action="<?= PluginEngine::getLink($plugin, array(), "configure/myarea") ?>"
<form action="<?= PluginEngine::getLink($plugin, [], "configure/myarea") ?>"
method="post"
class="default owncloud"
autocomplete="off">
......@@ -12,7 +12,7 @@
<?= MessageBox::info(sprintf(_("%s ist verknüpft"), Config::get()->OWNCLOUD_NAME)) ?>
<? elseif((UserConfig::get($GLOBALS['user']->id)->OWNCLOUD_ENDPOINT_USER || Config::get()->OWNCLOUD_ENDPOINT) && UserConfig::get($GLOBALS['user']->id)->OWNCLOUD_ACTIVATED) : ?>
<div style="text-align: center;">
<?= \Studip\LinkButton::create(sprintf(_("%s für Stud.IP freigeben"), Config::get()->OWNCLOUD_NAME), PluginEngine::getURL($plugin, array(), "oauth/request_access_token")) ?>
<?= \Studip\LinkButton::create(sprintf(_("%s für Stud.IP freigeben"), Config::get()->OWNCLOUD_NAME), PluginEngine::getURL($plugin, [], "oauth/request_access_token")) ?>
</div>
<? endif ?>
......@@ -39,7 +39,7 @@
<label>
<? URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']) ?>
<?= sprintf(_("Redirect-URI (zum Eintragen in der %s)"), Config::get()->OWNCLOUD_NAME) ?>
<input type="text" readonly value="<?= htmlReady(PluginEngine::getURL($plugin, array(), "oauth/receive_access_token"), true) ?>">
<input type="text" readonly value="<?= htmlReady(PluginEngine::getURL($plugin, [], "oauth/receive_access_token"), true) ?>">
<? URLHelper::setBaseURL("/") ?>
</label>
<? endif ?>
......@@ -53,7 +53,7 @@
<div data-dialog-button>
<?= \Studip\Button::create(_("Speichern")) ?>
<? if (\Owncloud\OAuth::isReady()) : ?>
<?= \Studip\Button::create(_("Zugang aufheben"), "remove", array('data-confirm' => _("Wirklich den Zugang zur Owncloud aufheben?"))) ?>
<?= \Studip\Button::create(_("Zugang aufheben"), "remove", ['data-confirm' => _("Wirklich den Zugang zur Owncloud aufheben?")]) ?>
<? endif ?>
</div>
</form>
......@@ -66,4 +66,4 @@
background-position: calc(100% - 5px) 4px;
background-size: 20px 20px;
}
</style>
\ No newline at end of file
</style>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment