GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Midgard Components Framework 3rd generation
Homepage: http://www.midgard-project.org
Clone URL: git://github.com/bergie/midcom.git
commit  a34614e25e8c509b727d9d678a8aa26ef6e66d7a
tree    9b5c105cdb8e0a9b8b2fa9d22327ef9ba8dd8c48
parent  b3622d5782fbc3f32c5127bb2d02f3293b5ddfee
midcom / midcom_core / helpers / webdav.php
100644 477 lines (413 sloc) 14.451 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* @package midcom_core
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
 
// We use the PEAR WebDAV server class
require 'HTTP/WebDAV/Server.php';
 
// The PATH_INFO needs to be provided so that creates will work
$_SERVER['PATH_INFO'] = $_MIDCOM->context->uri;
 
/**
* WebDAV server for MidCOM 3
*
* @package midcom_core
*/
class midcom_core_helpers_webdav extends HTTP_WebDAV_Server
{
    private $logger = null;
    private $controller = null;
    private $route_id = '';
    private $action_method = '';
    private $action_arguments = array();
    
    public function __construct($controller)
    {
        $this->logger = new midcom_core_helpers_log('webdav');
        $this->controller = $controller;
        parent::HTTP_WebDAV_Server();
    }
 
    /**
* Serve a WebDAV request
*
* @access public
*/
    public function serve($route_id, $action_method, $action_arguments)
    {
        $this->route_id = $route_id;
        $this->action_method = $action_method;
        $this->action_arguments = $action_arguments;
    
        // special treatment for litmus compliance test
        // reply on its identifier header
        // not needed for the test itself but eases debugging
        foreach(apache_request_headers() as $key => $value)
        {
            if (stristr($key, 'litmus'))
            {
                error_log("Litmus test {$value}");
                header("X-Litmus-reply: {$value}");
            }
        }
 
        $this->logger->log("\n\n=================================================", false);
        $this->logger->log("Serving {$_SERVER['REQUEST_METHOD']} request for {$_SERVER['REQUEST_URI']}");
        $this->logger->log("Controller: " . get_class($this->controller) . ", action: {$this->action_method}");
        
        header("X-Dav-Method: {$_SERVER['REQUEST_METHOD']}");
        
        // let the base class do all the work
        parent::ServeRequest();
        $this->logger->log("Path was: {$this->path}");
        flush();
        die();
    }
 
    /**
* PROPFIND method handler
*
* @param array general parameter passing array
* @param array return array for file properties
* @return bool true on success
*/
    function PROPFIND(&$options, &$files)
    {
        $_MIDCOM->authorization->require_user();
 
        // Run the controller
        $controller = $this->controller;
        $action_method = $this->action_method;
        $data = array();
        $controller->$action_method($this->route_id, $data, $this->action_arguments);
        
        if (!isset($data['children']))
        {
            // Controller did not return children
            $data['children'] = array();
        }
        
        // Get children from component instance
        $page = new midgard_page($_MIDCOM->context->page['guid']);
        $data['children'] = array_merge
        (
            $data['children'],
            $_MIDCOM->context->component_instance->get_node_children($page)
        );
        
        // Convert children to PROPFIND elements
        $this->children_to_files($data['children'], &$files);
        
        return true;
    }
 
    private function children_to_files($children, &$files)
    {
        foreach ($children as $child)
        {
            $child_props = array
            (
                'props' => array(),
                'path' => $child['uri'],
            );
            $child_props['props'][] = $this->mkprop('displayname', $child['title']);
            $child_props['props'][] = $this->mkprop('resourcetype', $child['resource']);
            $child_props['props'][] = $this->mkprop('getcontenttype', $child['mimetype']);
            $files['files'][] = $child_props;
        }
    }
 
    /**
* GET method handler
*
* @param array parameter passing array
* @return bool true on success
*/
    function GET(&$options)
    {
        $info = $this->get_path_info();
        
        if (is_null($info['object']))
        {
            throw new midcom_exception_notfound("Not found");
        }
 
        $_MIDCOM->authorization->require_user();
 
        switch ($info['variant'])
        {
            case 'content_html':
                $options['data'] = $info['object']->content;
                $options['mimetype'] = 'text/html';
                $options['mtime'] = $info['object']->metadata->revised;
                return true;
            /*case 'object_xml':
$options['data'] = $info['object']->serialize();
$options['mimetype'] = 'text/xml';
$options['mtime'] = $info['object']->metadata->revised;*/
            default:
                throw new midcom_exception_notfound("Not found");
        }
        
        throw new midcom_exception_notfound('No route matches current URL');
    }
 
    /**
* PUT method handler
*
* @param array parameter passing array
* @return bool true on success
*/
    function PUT(&$options)
    {
        $info = $this->get_path_info($options['path']);
 
        if (is_null($info['object']))
        {
            // Creation support
            if (is_null($info['parent']))
            {
                $this->logger->log("No parent known");
                throw new midcom_exception_notfound("Not found");
            }
            
            $_MIDCOM->authorization->require_do('midgard:create', $info['parent']);
            
            $this->logger->log("Trying to create {$options['path']}.");
            
            $file_type = pathinfo($options['path'], PATHINFO_EXTENSION);
            switch ($file_type)
            {
                case 'html':
                    $info['object'] = $info['parent'];
                    $info['variant'] = 'content_html';
                    break;
                default:
                    return "415 Unsupported media type";
            }
        }
        
        $_MIDCOM->authorization->require_user();
        
        $_MIDCOM->authorization->require_do('midgard:update', $info['object']);
        
        switch ($info['variant'])
        {
            case 'content_html':
                $info['object']->content = file_get_contents('php://input');
                $info['object']->update();
                return true;
            default:
                return '405 Method Not Allowed';
        }
    }
 
    /**
* MKCOL method handler
*
* @param array general parameter passing array
* @return bool true on success
*/
    public function MKCOL($options)
    {
        $info = $this->get_path_info($options['path']);
        
        // Creation support
        if (is_null($info['parent']))
        {
            $this->logger->log("No parent known");
            throw new midcom_exception_notfound("Not found");
        }
        
        $_MIDCOM->authorization->require_do('midgard:create', $info['parent']);
        
        $this->logger->log("Trying to create {$options['path']}");
        $page = new midgard_page();
        $page->up = $info['parent']->id;
        $page->name = basename($options['path']);
        $page->title = $page->name;
        $page->info = 'active';
        if (!$page->create())
        {
            return false;
        }
        
        return '201 Created';
    }
 
    /**
* LOCK method handler
*
* @param array general parameter passing array
* @return bool true on success
*/
    function LOCK(&$options)
    {
        $this->logger->log("Options: " . serialize($options));
        $info = $this->get_path_info();
 
        $shared = false;
        if ($options['scope'] == 'shared')
        {
            $shared = true;
        }
        
        if (is_null($info['object']))
        {
            throw new midcom_exception_notfound("Not found");
        }
        
        if (midcom_core_helpers_metadata::is_locked($info['object']))
        {
            $this->logger->log("Object is locked by another user");
            return "423 Locked";
        }
 
        midcom_core_helpers_metadata::lock($info['object'], $shared, $options['locktoken']);
        $options['timeout'] = time() + $_MIDCOM->configuration->get('metadata_lock_timeout');
        
        return "200 OK";
    }
    
    /**
* UNLOCK method handler
*
* @param array general parameter passing array
* @return bool true on success
*/
    function UNLOCK(&$options)
    {
        $info = $this->get_path_info();
        
        if (is_null($info['object']))
        {
            throw new midcom_exception_notfound("Not found");
        }
        
        if (midcom_core_helpers_metadata::is_locked($info['object']))
        {
            $this->logger->log("Object is locked by another user {$info['object']->metadata->locker}");
            return "423 Locked";
        }
 
        $this->logger->log("Unlocking");
        midcom_core_helpers_metadata::unlock($info['object']);
 
        return "200 OK";
    }
 
    /**
* No authentication is needed here
*
* @access private
* @param string HTTP Authentication type (Basic, Digest, ...)
* @param string Username
* @param string Password
* @return bool true on successful authentication
*/
    function checkAuth($type, $user, $pass)
    {
        if (!$_MIDCOM->authentication->is_user())
        {
            if (!$_MIDCOM->authentication->login($username, $password))
            {
                return false;
            }
        }
        return true;
    }
 
    /**
* checkLock() helper
*
* @param string resource path to check for locks
* @return bool true on success
*/
    function checkLock($path)
    {
        $this->logger->log("checkLock {$path}");
        $info = $this->get_path_info($path);
        if (is_null($info['object']))
        {
            $this->logger->log("{$path} Not Found");
            return false;
        }
        
        if (!midcom_core_helpers_metadata::is_locked($info['object'], false))
        {
            $this->logger->log("Not locked, locked = {$info['object']->metadata->locked}, locker = {$info['object']->metadata->locker}");
            return false;
        }
 
        // Populate lock info from metadata
        $lock = array
        (
            'type' => 'write',
            'scope' => 'shared',
            'depth' => 0,
            'owner' => $info['object']->metadata->locker,
            'created' => strtotime($info['object']->metadata->locked . ' GMT'),
            'modified' => strtotime($info['object']->metadata->locked . ' GMT'),
            'expires' => strtotime($info['object']->metadata->locked . ' GMT') + $_MIDCOM->configuration->get('metadata_lock_timeout') * 60,
        );
        
        if ($info['object']->metadata->locker)
        {
            $lock['scope'] = 'exclusive';
        }
        
        $lock_token = $info['object']->parameter('midcom_core_helper_metadata', 'lock_token');
        if ($lock_token)
        {
            $lock['token'] = $lock_token;
        }
        
        $this->logger->log(serialize($lock));
        return $lock;
    }
    
    private function get_path_info($path = null)
    {
        if (is_null($path))
        {
            $local_path = implode('/', $_MIDCOM->dispatcher->argv);
            $this->logger->log("Get path info \"{$local_path}\" from ARGV");
        }
        else
        {
            $local_path = substr($path, strlen($_MIDCOM->context->prefix));
            $this->logger->log("Get path info \"{$local_path}\" from OPTIONS");
        }
        $path = $local_path;
        
        static $info = array();
        if (isset($info[$path]))
        {
            return $info[$path];
        }
        
        $current_page = new midgard_page($_MIDCOM->context->page['guid']);
        
        $argv = array();
        $args = explode('/', $path);
        foreach ($args as $arg)
        {
            if (empty($arg))
            {
                continue;
            }
            $argv[] = $arg;
        }
        
        $info[$path] = array
        (
            'object' => null,
            'parent' => null,
            'variant' => 'default',
        );
        
        /*if ($_MIDCOM->context->page['id'] == $_MIDCOM->context->host->root)
{
// We're at MidCOM host root page, handle special URL cases
}*/
 
        if (count($argv) == 0)
        {
            $info[$path]['object'] = $current_page;
            $info[$path]['variant'] = 'collection';
            return $info[$path];
        }
 
        if (count($argv) == 1)
        {
            // Only one argument, check it
            switch ($argv[0])
            {
                case '__content.html':
                    $info[$path]['object'] = $current_page;
                    $info[$path]['variant'] = 'content_html';
                    return $info[$path];
                /*case '__midgard_page.xml':
$info[$path]['object'] = $current_page;
$info[$path]['variant'] = 'object_xml';
return $info[$path];*/
                default:
                    $info[$path]['parent'] = $current_page;
                    $qb = midgard_page::new_query_builder();
                    $qb->add_constraint('up', '=', $current_page->id);
                    $qb->add_constraint('name', '=', $argv[0]);
                    $pages = $qb->execute();
                    if (count($pages) > 0)
                    {
                        $info[$path]['object'] = $pages[0];
                        $info[$path]['variant'] = 'collection';
                        return $info[$path];
                    }
                    break;
            }
        }
        
        return $info[$path];
    }
 
    /**
* Logging method for WebDAV debugging
*/
    private function add_to_log($string, $addtime = true)
    {
        $log_file = fopen('/tmp/midcom-webdav.log', 'a');
        if (!$log_file)
        {
            return;
        }
        
        if ($addtime)
        {
            $string = date('r') . ": {$string}";
        }
        
        fwrite($log_file, "{$string}\n");
        fclose($log_file);
    }
}
?>