]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/Browser/GuessContentType.php
Update strings
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / Browser / GuessContentType.php
1 <?php
2
3 /**
4  * GuessContentType addon
5  *
6  * A lot of the built-in File objects just return application/octet-stream
7  * as a content-type by default. This is a problem for some clients, because
8  * they expect a correct contenttype.
9  *
10  * There's really no accurate, fast and portable way to determine the contenttype
11  * so this extension does what the rest of the world does, and guesses it based
12  * on the file extension.
13  *
14  * @package Sabre
15  * @subpackage DAV
16  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
17  * @author Evert Pot (http://www.rooftopsolutions.nl/) 
18  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
19  */
20 class Sabre_DAV_Browser_GuessContentType extends Sabre_DAV_ServerPlugin {
21
22     /**
23      * List of recognized file extensions
24      *
25      * Feel free to add more
26      *
27      * @var array
28      */
29     public $extensionMap = array(
30
31         // images
32         'jpg' => 'image/jpeg',
33         'gif' => 'image/gif',
34         'png' => 'image/png',
35
36         // groupware
37         'ics' => 'text/calendar',
38         'vcf' => 'text/x-vcard',
39
40         // text
41         'txt' => 'text/plain',
42
43     );
44
45     /**
46      * Initializes the addon
47      *
48      * @param Sabre_DAV_Server $server
49      * @return void
50      */
51     public function initialize(Sabre_DAV_Server $server) {
52
53         // Using a relatively low priority (200) to allow other extensions
54         // to set the content-type first.
55         $server->subscribeEvent('afterGetProperties',array($this,'afterGetProperties'),200);
56
57     }
58
59     /**
60      * Handler for teh afterGetProperties event
61      *
62      * @param string $path
63      * @param array $properties
64      * @return void
65      */
66     public function afterGetProperties($path, &$properties) {
67
68         if (array_key_exists('{DAV:}getcontenttype', $properties[404])) {
69
70             list(, $fileName) = Sabre_DAV_URLUtil::splitPath($path);
71             $contentType = $this->getContentType($fileName);
72
73             if ($contentType) {
74                 $properties[200]['{DAV:}getcontenttype'] = $contentType;
75                 unset($properties[404]['{DAV:}getcontenttype']);
76             }
77
78         }
79
80     }
81
82     /**
83      * Simple method to return the contenttype
84      *
85      * @param string $fileName
86      * @return string
87      */
88     protected function getContentType($fileName) {
89
90         // Just grabbing the extension
91         $extension = strtolower(substr($fileName,strrpos($fileName,'.')+1));
92         if (isset($this->extensionMap[$extension]))
93             return $this->extensionMap[$extension];
94
95     }
96
97 }