/** * Performs the test. * * @return \Jyxo\Beholder\Result */ public function run() { // The http extension is required if (!extension_loaded('http')) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension http missing'); } // The \Jyxo\Webdav\Client class is required if (!class_exists('\\Jyxo\\Webdav\\Client')) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Class \\Jyxo\\Webdav\\Client missing'); } $random = md5(uniqid(time(), true)); $dir = trim($this->dir, '/'); if (!empty($dir)) { $dir = '/' . $dir; } $path = $dir . '/beholder-' . $random . '.txt'; $content = $random; // Status label $serverUrl = $this->server; if ('http://' !== substr($this->server, 0, 7)) { $serverUrl = 'http://' . $serverUrl; } $parsed = parse_url($serverUrl); $ip = $parsed['host']; $port = !empty($parsed['port']) ? $parsed['port'] : 80; $description = (false !== filter_var($ip, FILTER_VALIDATE_IP) ? gethostbyaddr($ip) : $ip) . ':' . $port . $dir; try { $webdav = new \Jyxo\Webdav\Client(array($serverUrl)); foreach ($this->options as $name => $value) { $webdav->setOption($name, $value); } // Writing if (!$webdav->put($path, $content)) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Write error %s', $description)); } // Reading $readContent = $webdav->get($path); if (strlen($readContent) !== strlen($content)) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Read error %s', $description)); } // Deleting if (!$webdav->unlink($path)) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Delete error %s', $description)); } } catch (\Jyxo\Webdav\FileNotExistException $e) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Read error %s', $description)); } catch (\Jyxo\Webdav\Exception $e) { return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Error %s', $description)); } // OK return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $description); }