openrat-webdav

git clone http://git.code.weiherhei.de/openrat-webdav.git
Log | Files | Refs | README

URIParser.class.php (3294B)


      1 <?php
      2 
      3 
      4 use dav\exception\NotFoundException;
      5 
      6 /**
      7  * Class URIParser.
      8  * Parsing a DAV url in the format "/<projectname>/<folder>/<folder>/<object>".
      9  */
     10 class URIParser
     11 {
     12     const ROOT    = 'root';
     13     const PROJECT = 'project';
     14     const FOLDER  = 'folder';
     15 
     16     public $type;
     17     public $projectid;
     18     public $objectid;
     19     public $folderid;
     20     public $basename;
     21 
     22     private $uri;
     23     /**
     24      * @var CMS
     25      */
     26     private $client;
     27 
     28     public function __construct( $client, $uri )
     29     {
     30         $this->uri = $uri;
     31         $this->client = $client;
     32         $this->parseURI();
     33     }
     34 
     35     /**
     36      * URI parsen.
     37      */
     38     private function parseURI()
     39     {
     40         $uri = $this->uri;
     41 
     42         Logger::trace('WEBDAV: Parsen der URI ' . $uri);
     43 
     44         $uriParts = explode('/', $uri);
     45 
     46         $first = array_shift($uriParts);
     47         if ($first) {
     48             throw new RuntimeException( 'URI does not begin with \'/\'.' );
     49         }
     50 
     51         $projectName = array_shift($uriParts);
     52 
     53         if ( ! $projectName ) {
     54             $this->type = self::ROOT;  // Root-Verzeichnis
     55             return;
     56         }
     57 
     58         $this->type = '';
     59         try {
     60 
     61             $result = $this->client->projectlist();
     62         }
     63         catch( Exception $e) {
     64             Logger::error("Failed to read projects: \n".$this->client->__toString()."\n".$e->getMessage() );
     65             throw $e;
     66         }
     67 
     68 
     69         $projects = $result['projects'];
     70 
     71         foreach( $projects as $id=>$projectinfo)
     72             if   ( $projectinfo['name'] == $projectName)
     73                 $this->projectid = $id;
     74 
     75         if ( ! $this->projectid ) {
     76             $this->basename = $projectName;
     77             $this->type = self::PROJECT;
     78             return;
     79         }
     80 
     81         $project = $this->client->project($this->projectid);
     82         Logger::trace( print_r( $project,true) );
     83 
     84 
     85         $objectid = $project['rootobjectid'];
     86         $folderid = $objectid;
     87         $type     = 'folder';
     88         $name     = $projectName;
     89 
     90         while (sizeof($uriParts) > 0) {
     91             $name = array_shift($uriParts);
     92 
     93             if   ( !$name )
     94                 continue; // empty path segments
     95 
     96             $folder = $this->client->folder($objectid);
     97             $folderid = $objectid;
     98 
     99             $found = false;
    100             foreach ($folder['content']['object'] as $oid => $object) {
    101                 if ($object['filename'] == $name) {
    102                     $found = true;
    103 
    104                     $type     = $object['type'];
    105                     $objectid = $object['id'  ];
    106 
    107                     break;
    108                 }
    109 
    110             }
    111 
    112             if (!$found) {
    113                 $objectid = null;
    114                 break;
    115             }
    116         }
    117 
    118         $this->type = $type;
    119         $this->folderid   = $folderid;
    120         $this->objectid   = $objectid;
    121         $this->basename   = $name;
    122     }
    123 
    124 
    125     public function isRoot() {
    126 
    127         return $this->type == self::ROOT;
    128     }
    129 
    130     public function exists() {
    131         return boolval($this->objectid);
    132     }
    133 
    134 
    135 
    136     /**
    137      * Representation of this URI.
    138      *
    139      * @return string
    140      */
    141     public function __toString()
    142     {
    143         return "DAV-URI: $this->uri ==> [$this->type] projectid: $this->projectid / objectid: $this->objectid folderid: $this->folderid name: $this->basename";
    144     }
    145 }