示例#1
0
文件: Webdav.php 项目: honzap/php
 /**
  * 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);
 }
示例#2
0
文件: Webdav.php 项目: jyxo/php
 /**
  * Performs the test.
  *
  * @return \Jyxo\Beholder\Result
  */
 public function run() : \Jyxo\Beholder\Result
 {
     // The \Jyxo\Webdav\Client class is required
     if (!class_exists(\Jyxo\Webdav\Client::class)) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, sprintf('Class %s missing', \Jyxo\Webdav\Client::class));
     }
     $random = md5(uniqid((string) time(), true));
     $dir = trim($this->dir, '/');
     if (!empty($dir)) {
         $dir = '/' . $dir;
     }
     $path = $dir . '/beholder-' . $random . '.txt';
     $content = $random;
     // Status label
     $serverUrl = $this->server;
     if (!preg_match('~^https?://~', $this->server)) {
         $serverUrl = 'http://' . $serverUrl;
     }
     $parsed = parse_url($serverUrl);
     $host = $parsed['host'];
     $port = !empty($parsed['port']) ? $parsed['port'] : 80;
     $description = (false !== filter_var($host, FILTER_VALIDATE_IP) ? gethostbyaddr($host) : $host) . ':' . $port . $dir;
     try {
         $webdav = new \Jyxo\Webdav\Client([$serverUrl]);
         foreach ($this->options as $name => $value) {
             $webdav->setRequestOption($name, $value);
         }
         // Writing
         $webdav->put($path, $content);
         // Exists
         if (!$webdav->exists($path)) {
             return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Exists 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
         $webdav->unlink($path);
     } catch (\Jyxo\Webdav\FileNotCreatedException $e) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Write 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\FileNotDeletedException $e) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Delete 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);
 }