@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * @param string $string the string which will be escaped and printed |
35 | 35 | */ |
36 | 36 | function p($string) { |
37 | - print(\OCP\Util::sanitizeHTML($string)); |
|
37 | + print(\OCP\Util::sanitizeHTML($string)); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | * @param string|array $string the string which will be printed as it is |
44 | 44 | */ |
45 | 45 | function print_unescaped($string) { |
46 | - print($string); |
|
46 | + print($string); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -53,13 +53,13 @@ discard block |
||
53 | 53 | * if an array is given it will add all scripts |
54 | 54 | */ |
55 | 55 | function script($app, $file = null) { |
56 | - if(is_array($file)) { |
|
57 | - foreach($file as $f) { |
|
58 | - OC_Util::addScript($app, $f); |
|
59 | - } |
|
60 | - } else { |
|
61 | - OC_Util::addScript($app, $file); |
|
62 | - } |
|
56 | + if(is_array($file)) { |
|
57 | + foreach($file as $f) { |
|
58 | + OC_Util::addScript($app, $f); |
|
59 | + } |
|
60 | + } else { |
|
61 | + OC_Util::addScript($app, $file); |
|
62 | + } |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -69,13 +69,13 @@ discard block |
||
69 | 69 | * if an array is given it will add all scripts |
70 | 70 | */ |
71 | 71 | function vendor_script($app, $file = null) { |
72 | - if(is_array($file)) { |
|
73 | - foreach($file as $f) { |
|
74 | - OC_Util::addVendorScript($app, $f); |
|
75 | - } |
|
76 | - } else { |
|
77 | - OC_Util::addVendorScript($app, $file); |
|
78 | - } |
|
72 | + if(is_array($file)) { |
|
73 | + foreach($file as $f) { |
|
74 | + OC_Util::addVendorScript($app, $f); |
|
75 | + } |
|
76 | + } else { |
|
77 | + OC_Util::addVendorScript($app, $file); |
|
78 | + } |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | /** |
@@ -85,13 +85,13 @@ discard block |
||
85 | 85 | * if an array is given it will add all styles |
86 | 86 | */ |
87 | 87 | function style($app, $file = null) { |
88 | - if(is_array($file)) { |
|
89 | - foreach($file as $f) { |
|
90 | - OC_Util::addStyle($app, $f); |
|
91 | - } |
|
92 | - } else { |
|
93 | - OC_Util::addStyle($app, $file); |
|
94 | - } |
|
88 | + if(is_array($file)) { |
|
89 | + foreach($file as $f) { |
|
90 | + OC_Util::addStyle($app, $f); |
|
91 | + } |
|
92 | + } else { |
|
93 | + OC_Util::addStyle($app, $file); |
|
94 | + } |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -101,13 +101,13 @@ discard block |
||
101 | 101 | * if an array is given it will add all styles |
102 | 102 | */ |
103 | 103 | function vendor_style($app, $file = null) { |
104 | - if(is_array($file)) { |
|
105 | - foreach($file as $f) { |
|
106 | - OC_Util::addVendorStyle($app, $f); |
|
107 | - } |
|
108 | - } else { |
|
109 | - OC_Util::addVendorStyle($app, $file); |
|
110 | - } |
|
104 | + if(is_array($file)) { |
|
105 | + foreach($file as $f) { |
|
106 | + OC_Util::addVendorStyle($app, $f); |
|
107 | + } |
|
108 | + } else { |
|
109 | + OC_Util::addVendorStyle($app, $file); |
|
110 | + } |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | * if an array is given it will add all styles |
117 | 117 | */ |
118 | 118 | function translation($app) { |
119 | - OC_Util::addTranslations($app); |
|
119 | + OC_Util::addTranslations($app); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
@@ -126,15 +126,15 @@ discard block |
||
126 | 126 | * if an array is given it will add all components |
127 | 127 | */ |
128 | 128 | function component($app, $file) { |
129 | - if(is_array($file)) { |
|
130 | - foreach($file as $f) { |
|
131 | - $url = link_to($app, 'component/' . $f . '.html'); |
|
132 | - OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url)); |
|
133 | - } |
|
134 | - } else { |
|
135 | - $url = link_to($app, 'component/' . $file . '.html'); |
|
136 | - OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url)); |
|
137 | - } |
|
129 | + if(is_array($file)) { |
|
130 | + foreach($file as $f) { |
|
131 | + $url = link_to($app, 'component/' . $f . '.html'); |
|
132 | + OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url)); |
|
133 | + } |
|
134 | + } else { |
|
135 | + $url = link_to($app, 'component/' . $file . '.html'); |
|
136 | + OC_Util::addHeader('link', array('rel' => 'import', 'href' => $url)); |
|
137 | + } |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | /** |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | * For further information have a look at \OCP\IURLGenerator::linkTo |
148 | 148 | */ |
149 | 149 | function link_to( $app, $file, $args = array() ) { |
150 | - return \OC::$server->getURLGenerator()->linkTo($app, $file, $args); |
|
150 | + return \OC::$server->getURLGenerator()->linkTo($app, $file, $args); |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | /** |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | * @return string url to the online documentation |
156 | 156 | */ |
157 | 157 | function link_to_docs($key) { |
158 | - return \OC::$server->getURLGenerator()->linkToDocs($key); |
|
158 | + return \OC::$server->getURLGenerator()->linkToDocs($key); |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | /** |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | * For further information have a look at \OCP\IURLGenerator::imagePath |
168 | 168 | */ |
169 | 169 | function image_path( $app, $image ) { |
170 | - return \OC::$server->getURLGenerator()->imagePath( $app, $image ); |
|
170 | + return \OC::$server->getURLGenerator()->imagePath( $app, $image ); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | * @return string link to the image |
177 | 177 | */ |
178 | 178 | function mimetype_icon( $mimetype ) { |
179 | - return \OC::$server->getMimeTypeDetector()->mimeTypeIcon( $mimetype ); |
|
179 | + return \OC::$server->getMimeTypeDetector()->mimeTypeIcon( $mimetype ); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | /** |
@@ -186,14 +186,14 @@ discard block |
||
186 | 186 | * @return link to the preview |
187 | 187 | */ |
188 | 188 | function preview_icon( $path ) { |
189 | - return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]); |
|
189 | + return \OC::$server->getURLGenerator()->linkToRoute('core.Preview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path]); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
193 | 193 | * @param string $path |
194 | 194 | */ |
195 | 195 | function publicPreview_icon ( $path, $token ) { |
196 | - return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 't' => $token]); |
|
196 | + return \OC::$server->getURLGenerator()->linkToRoute('files_sharing.PublicPreview.getPreview', ['x' => 32, 'y' => 32, 'file' => $path, 't' => $token]); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | * For further information have a look at OC_Helper::humanFileSize |
205 | 205 | */ |
206 | 206 | function human_file_size( $bytes ) { |
207 | - return OC_Helper::humanFileSize( $bytes ); |
|
207 | + return OC_Helper::humanFileSize( $bytes ); |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | /** |
@@ -213,9 +213,9 @@ discard block |
||
213 | 213 | * @return $timestamp without time value |
214 | 214 | */ |
215 | 215 | function strip_time($timestamp){ |
216 | - $date = new \DateTime("@{$timestamp}"); |
|
217 | - $date->setTime(0, 0, 0); |
|
218 | - return intval($date->format('U')); |
|
216 | + $date = new \DateTime("@{$timestamp}"); |
|
217 | + $date->setTime(0, 0, 0); |
|
218 | + return intval($date->format('U')); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | /** |
@@ -227,39 +227,39 @@ discard block |
||
227 | 227 | * @return string timestamp |
228 | 228 | */ |
229 | 229 | function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) { |
230 | - /** @var \OC\DateTimeFormatter $formatter */ |
|
231 | - $formatter = \OC::$server->query('DateTimeFormatter'); |
|
230 | + /** @var \OC\DateTimeFormatter $formatter */ |
|
231 | + $formatter = \OC::$server->query('DateTimeFormatter'); |
|
232 | 232 | |
233 | - if ($dateOnly){ |
|
234 | - return $formatter->formatDateSpan($timestamp, $fromTime); |
|
235 | - } |
|
236 | - return $formatter->formatTimeSpan($timestamp, $fromTime); |
|
233 | + if ($dateOnly){ |
|
234 | + return $formatter->formatDateSpan($timestamp, $fromTime); |
|
235 | + } |
|
236 | + return $formatter->formatTimeSpan($timestamp, $fromTime); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | function html_select_options($options, $selected, $params=array()) { |
240 | - if (!is_array($selected)) { |
|
241 | - $selected=array($selected); |
|
242 | - } |
|
243 | - if (isset($params['combine']) && $params['combine']) { |
|
244 | - $options = array_combine($options, $options); |
|
245 | - } |
|
246 | - $value_name = $label_name = false; |
|
247 | - if (isset($params['value'])) { |
|
248 | - $value_name = $params['value']; |
|
249 | - } |
|
250 | - if (isset($params['label'])) { |
|
251 | - $label_name = $params['label']; |
|
252 | - } |
|
253 | - $html = ''; |
|
254 | - foreach($options as $value => $label) { |
|
255 | - if ($value_name && is_array($label)) { |
|
256 | - $value = $label[$value_name]; |
|
257 | - } |
|
258 | - if ($label_name && is_array($label)) { |
|
259 | - $label = $label[$label_name]; |
|
260 | - } |
|
261 | - $select = in_array($value, $selected) ? ' selected="selected"' : ''; |
|
262 | - $html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>'."\n"; |
|
263 | - } |
|
264 | - return $html; |
|
240 | + if (!is_array($selected)) { |
|
241 | + $selected=array($selected); |
|
242 | + } |
|
243 | + if (isset($params['combine']) && $params['combine']) { |
|
244 | + $options = array_combine($options, $options); |
|
245 | + } |
|
246 | + $value_name = $label_name = false; |
|
247 | + if (isset($params['value'])) { |
|
248 | + $value_name = $params['value']; |
|
249 | + } |
|
250 | + if (isset($params['label'])) { |
|
251 | + $label_name = $params['label']; |
|
252 | + } |
|
253 | + $html = ''; |
|
254 | + foreach($options as $value => $label) { |
|
255 | + if ($value_name && is_array($label)) { |
|
256 | + $value = $label[$value_name]; |
|
257 | + } |
|
258 | + if ($label_name && is_array($label)) { |
|
259 | + $label = $label[$label_name]; |
|
260 | + } |
|
261 | + $select = in_array($value, $selected) ? ' selected="selected"' : ''; |
|
262 | + $html .= '<option value="' . \OCP\Util::sanitizeHTML($value) . '"' . $select . '>' . \OCP\Util::sanitizeHTML($label) . '</option>'."\n"; |
|
263 | + } |
|
264 | + return $html; |
|
265 | 265 | } |
@@ -33,98 +33,98 @@ |
||
33 | 33 | * use server side events with caution, to many open requests can hang the server |
34 | 34 | */ |
35 | 35 | class OC_EventSource implements \OCP\IEventSource { |
36 | - /** |
|
37 | - * @var bool |
|
38 | - */ |
|
39 | - private $fallback; |
|
36 | + /** |
|
37 | + * @var bool |
|
38 | + */ |
|
39 | + private $fallback; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @var int |
|
43 | - */ |
|
44 | - private $fallBackId = 0; |
|
41 | + /** |
|
42 | + * @var int |
|
43 | + */ |
|
44 | + private $fallBackId = 0; |
|
45 | 45 | |
46 | - /** |
|
47 | - * @var bool |
|
48 | - */ |
|
49 | - private $started = false; |
|
46 | + /** |
|
47 | + * @var bool |
|
48 | + */ |
|
49 | + private $started = false; |
|
50 | 50 | |
51 | - protected function init() { |
|
52 | - if ($this->started) { |
|
53 | - return; |
|
54 | - } |
|
55 | - $this->started = true; |
|
51 | + protected function init() { |
|
52 | + if ($this->started) { |
|
53 | + return; |
|
54 | + } |
|
55 | + $this->started = true; |
|
56 | 56 | |
57 | - // prevent php output buffering, caching and nginx buffering |
|
58 | - OC_Util::obEnd(); |
|
59 | - header('Cache-Control: no-cache'); |
|
60 | - header('X-Accel-Buffering: no'); |
|
61 | - $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
|
62 | - if ($this->fallback) { |
|
63 | - $this->fallBackId = (int)$_GET['fallback_id']; |
|
64 | - /** |
|
65 | - * FIXME: The default content-security-policy of ownCloud forbids inline |
|
66 | - * JavaScript for security reasons. IE starting on Windows 10 will |
|
67 | - * however also obey the CSP which will break the event source fallback. |
|
68 | - * |
|
69 | - * As a workaround thus we set a custom policy which allows the execution |
|
70 | - * of inline JavaScript. |
|
71 | - * |
|
72 | - * @link https://github.com/owncloud/core/issues/14286 |
|
73 | - */ |
|
74 | - header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
|
75 | - header("Content-Type: text/html"); |
|
76 | - echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
77 | - } else { |
|
78 | - header("Content-Type: text/event-stream"); |
|
79 | - } |
|
80 | - if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
81 | - header('Location: '.\OC::$WEBROOT); |
|
82 | - exit(); |
|
83 | - } |
|
84 | - if (!(\OC::$server->getRequest()->passesCSRFCheck())) { |
|
85 | - $this->send('error', 'Possible CSRF attack. Connection will be closed.'); |
|
86 | - $this->close(); |
|
87 | - exit(); |
|
88 | - } |
|
89 | - flush(); |
|
90 | - } |
|
57 | + // prevent php output buffering, caching and nginx buffering |
|
58 | + OC_Util::obEnd(); |
|
59 | + header('Cache-Control: no-cache'); |
|
60 | + header('X-Accel-Buffering: no'); |
|
61 | + $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; |
|
62 | + if ($this->fallback) { |
|
63 | + $this->fallBackId = (int)$_GET['fallback_id']; |
|
64 | + /** |
|
65 | + * FIXME: The default content-security-policy of ownCloud forbids inline |
|
66 | + * JavaScript for security reasons. IE starting on Windows 10 will |
|
67 | + * however also obey the CSP which will break the event source fallback. |
|
68 | + * |
|
69 | + * As a workaround thus we set a custom policy which allows the execution |
|
70 | + * of inline JavaScript. |
|
71 | + * |
|
72 | + * @link https://github.com/owncloud/core/issues/14286 |
|
73 | + */ |
|
74 | + header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'"); |
|
75 | + header("Content-Type: text/html"); |
|
76 | + echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy |
|
77 | + } else { |
|
78 | + header("Content-Type: text/event-stream"); |
|
79 | + } |
|
80 | + if(!\OC::$server->getRequest()->passesStrictCookieCheck()) { |
|
81 | + header('Location: '.\OC::$WEBROOT); |
|
82 | + exit(); |
|
83 | + } |
|
84 | + if (!(\OC::$server->getRequest()->passesCSRFCheck())) { |
|
85 | + $this->send('error', 'Possible CSRF attack. Connection will be closed.'); |
|
86 | + $this->close(); |
|
87 | + exit(); |
|
88 | + } |
|
89 | + flush(); |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * send a message to the client |
|
94 | - * |
|
95 | - * @param string $type |
|
96 | - * @param mixed $data |
|
97 | - * |
|
98 | - * @throws \BadMethodCallException |
|
99 | - * if only one parameter is given, a typeless message will be send with that parameter as data |
|
100 | - */ |
|
101 | - public function send($type, $data = null) { |
|
102 | - if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
|
103 | - throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
104 | - } |
|
105 | - $this->init(); |
|
106 | - if (is_null($data)) { |
|
107 | - $data = $type; |
|
108 | - $type = null; |
|
109 | - } |
|
110 | - if ($this->fallback) { |
|
111 | - $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
|
112 | - . $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL; |
|
113 | - echo $response; |
|
114 | - } else { |
|
115 | - if ($type) { |
|
116 | - echo 'event: ' . $type . PHP_EOL; |
|
117 | - } |
|
118 | - echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL; |
|
119 | - } |
|
120 | - echo PHP_EOL; |
|
121 | - flush(); |
|
122 | - } |
|
92 | + /** |
|
93 | + * send a message to the client |
|
94 | + * |
|
95 | + * @param string $type |
|
96 | + * @param mixed $data |
|
97 | + * |
|
98 | + * @throws \BadMethodCallException |
|
99 | + * if only one parameter is given, a typeless message will be send with that parameter as data |
|
100 | + */ |
|
101 | + public function send($type, $data = null) { |
|
102 | + if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { |
|
103 | + throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); |
|
104 | + } |
|
105 | + $this->init(); |
|
106 | + if (is_null($data)) { |
|
107 | + $data = $type; |
|
108 | + $type = null; |
|
109 | + } |
|
110 | + if ($this->fallback) { |
|
111 | + $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack(' |
|
112 | + . $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL; |
|
113 | + echo $response; |
|
114 | + } else { |
|
115 | + if ($type) { |
|
116 | + echo 'event: ' . $type . PHP_EOL; |
|
117 | + } |
|
118 | + echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL; |
|
119 | + } |
|
120 | + echo PHP_EOL; |
|
121 | + flush(); |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * close the connection of the event source |
|
126 | - */ |
|
127 | - public function close() { |
|
128 | - $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it |
|
129 | - } |
|
124 | + /** |
|
125 | + * close the connection of the event source |
|
126 | + */ |
|
127 | + public function close() { |
|
128 | + $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it |
|
129 | + } |
|
130 | 130 | } |
@@ -27,53 +27,53 @@ |
||
27 | 27 | */ |
28 | 28 | |
29 | 29 | class OC_L10N_String implements JsonSerializable { |
30 | - /** @var \OC\L10N\L10N */ |
|
31 | - protected $l10n; |
|
30 | + /** @var \OC\L10N\L10N */ |
|
31 | + protected $l10n; |
|
32 | 32 | |
33 | - /** @var string */ |
|
34 | - protected $text; |
|
33 | + /** @var string */ |
|
34 | + protected $text; |
|
35 | 35 | |
36 | - /** @var array */ |
|
37 | - protected $parameters; |
|
36 | + /** @var array */ |
|
37 | + protected $parameters; |
|
38 | 38 | |
39 | - /** @var integer */ |
|
40 | - protected $count; |
|
39 | + /** @var integer */ |
|
40 | + protected $count; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param \OC\L10N\L10N $l10n |
|
44 | - * @param string|string[] $text |
|
45 | - * @param array $parameters |
|
46 | - * @param int $count |
|
47 | - */ |
|
48 | - public function __construct(\OC\L10N\L10N $l10n, $text, $parameters, $count = 1) { |
|
49 | - $this->l10n = $l10n; |
|
50 | - $this->text = $text; |
|
51 | - $this->parameters = $parameters; |
|
52 | - $this->count = $count; |
|
53 | - } |
|
42 | + /** |
|
43 | + * @param \OC\L10N\L10N $l10n |
|
44 | + * @param string|string[] $text |
|
45 | + * @param array $parameters |
|
46 | + * @param int $count |
|
47 | + */ |
|
48 | + public function __construct(\OC\L10N\L10N $l10n, $text, $parameters, $count = 1) { |
|
49 | + $this->l10n = $l10n; |
|
50 | + $this->text = $text; |
|
51 | + $this->parameters = $parameters; |
|
52 | + $this->count = $count; |
|
53 | + } |
|
54 | 54 | |
55 | - public function __toString() { |
|
56 | - $translations = $this->l10n->getTranslations(); |
|
55 | + public function __toString() { |
|
56 | + $translations = $this->l10n->getTranslations(); |
|
57 | 57 | |
58 | - $text = $this->text; |
|
59 | - if(array_key_exists($this->text, $translations)) { |
|
60 | - if(is_array($translations[$this->text])) { |
|
61 | - $fn = $this->l10n->getPluralFormFunction(); |
|
62 | - $id = $fn($this->count); |
|
63 | - $text = $translations[$this->text][$id]; |
|
64 | - } |
|
65 | - else{ |
|
66 | - $text = $translations[$this->text]; |
|
67 | - } |
|
68 | - } |
|
58 | + $text = $this->text; |
|
59 | + if(array_key_exists($this->text, $translations)) { |
|
60 | + if(is_array($translations[$this->text])) { |
|
61 | + $fn = $this->l10n->getPluralFormFunction(); |
|
62 | + $id = $fn($this->count); |
|
63 | + $text = $translations[$this->text][$id]; |
|
64 | + } |
|
65 | + else{ |
|
66 | + $text = $translations[$this->text]; |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - // Replace %n first (won't interfere with vsprintf) |
|
71 | - $text = str_replace('%n', $this->count, $text); |
|
72 | - return vsprintf($text, $this->parameters); |
|
73 | - } |
|
70 | + // Replace %n first (won't interfere with vsprintf) |
|
71 | + $text = str_replace('%n', $this->count, $text); |
|
72 | + return vsprintf($text, $this->parameters); |
|
73 | + } |
|
74 | 74 | |
75 | 75 | |
76 | - public function jsonSerialize() { |
|
77 | - return $this->__toString(); |
|
78 | - } |
|
76 | + public function jsonSerialize() { |
|
77 | + return $this->__toString(); |
|
78 | + } |
|
79 | 79 | } |
@@ -30,156 +30,156 @@ |
||
30 | 30 | |
31 | 31 | |
32 | 32 | class OC_FileChunking { |
33 | - protected $info; |
|
34 | - protected $cache; |
|
35 | - |
|
36 | - /** |
|
37 | - * TTL of chunks |
|
38 | - * |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - protected $ttl; |
|
42 | - |
|
43 | - static public function decodeName($name) { |
|
44 | - preg_match('/(?P<name>.*)-chunking-(?P<transferid>\d+)-(?P<chunkcount>\d+)-(?P<index>\d+)/', $name, $matches); |
|
45 | - return $matches; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @param string[] $info |
|
50 | - */ |
|
51 | - public function __construct($info) { |
|
52 | - $this->info = $info; |
|
53 | - $this->ttl = \OC::$server->getConfig()->getSystemValue('cache_chunk_gc_ttl', 86400); |
|
54 | - } |
|
55 | - |
|
56 | - public function getPrefix() { |
|
57 | - $name = $this->info['name']; |
|
58 | - $transferid = $this->info['transferid']; |
|
59 | - |
|
60 | - return $name.'-chunking-'.$transferid.'-'; |
|
61 | - } |
|
62 | - |
|
63 | - protected function getCache() { |
|
64 | - if (!isset($this->cache)) { |
|
65 | - $this->cache = new \OC\Cache\File(); |
|
66 | - } |
|
67 | - return $this->cache; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Stores the given $data under the given $key - the number of stored bytes is returned |
|
72 | - * |
|
73 | - * @param string $index |
|
74 | - * @param resource $data |
|
75 | - * @return int |
|
76 | - */ |
|
77 | - public function store($index, $data) { |
|
78 | - $cache = $this->getCache(); |
|
79 | - $name = $this->getPrefix().$index; |
|
80 | - $cache->set($name, $data, $this->ttl); |
|
81 | - |
|
82 | - return $cache->size($name); |
|
83 | - } |
|
84 | - |
|
85 | - public function isComplete() { |
|
86 | - $prefix = $this->getPrefix(); |
|
87 | - $cache = $this->getCache(); |
|
88 | - $chunkcount = (int)$this->info['chunkcount']; |
|
89 | - |
|
90 | - for($i=($chunkcount-1); $i >= 0; $i--) { |
|
91 | - if (!$cache->hasKey($prefix.$i)) { |
|
92 | - return false; |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - return true; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Assembles the chunks into the file specified by the path. |
|
101 | - * Chunks are deleted afterwards. |
|
102 | - * |
|
103 | - * @param resource $f target path |
|
104 | - * |
|
105 | - * @return integer assembled file size |
|
106 | - * |
|
107 | - * @throws \OC\InsufficientStorageException when file could not be fully |
|
108 | - * assembled due to lack of free space |
|
109 | - */ |
|
110 | - public function assemble($f) { |
|
111 | - $cache = $this->getCache(); |
|
112 | - $prefix = $this->getPrefix(); |
|
113 | - $count = 0; |
|
114 | - for ($i = 0; $i < $this->info['chunkcount']; $i++) { |
|
115 | - $chunk = $cache->get($prefix.$i); |
|
116 | - // remove after reading to directly save space |
|
117 | - $cache->remove($prefix.$i); |
|
118 | - $count += fwrite($f, $chunk); |
|
119 | - // let php release the memory to work around memory exhausted error with php 5.6 |
|
120 | - $chunk = null; |
|
121 | - } |
|
122 | - |
|
123 | - return $count; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Returns the size of the chunks already present |
|
128 | - * @return integer size in bytes |
|
129 | - */ |
|
130 | - public function getCurrentSize() { |
|
131 | - $cache = $this->getCache(); |
|
132 | - $prefix = $this->getPrefix(); |
|
133 | - $total = 0; |
|
134 | - for ($i = 0; $i < $this->info['chunkcount']; $i++) { |
|
135 | - $total += $cache->size($prefix.$i); |
|
136 | - } |
|
137 | - return $total; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Removes all chunks which belong to this transmission |
|
142 | - */ |
|
143 | - public function cleanup() { |
|
144 | - $cache = $this->getCache(); |
|
145 | - $prefix = $this->getPrefix(); |
|
146 | - for($i=0; $i < $this->info['chunkcount']; $i++) { |
|
147 | - $cache->remove($prefix.$i); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Removes one specific chunk |
|
153 | - * @param string $index |
|
154 | - */ |
|
155 | - public function remove($index) { |
|
156 | - $cache = $this->getCache(); |
|
157 | - $prefix = $this->getPrefix(); |
|
158 | - $cache->remove($prefix.$index); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Assembles the chunks into the file specified by the path. |
|
163 | - * Also triggers the relevant hooks and proxies. |
|
164 | - * |
|
165 | - * @param \OC\Files\Storage\Storage $storage storage |
|
166 | - * @param string $path target path relative to the storage |
|
167 | - * @return bool true on success or false if file could not be created |
|
168 | - * |
|
169 | - * @throws \OC\ServerNotAvailableException |
|
170 | - */ |
|
171 | - public function file_assemble($storage, $path) { |
|
172 | - // use file_put_contents as method because that best matches what this function does |
|
173 | - if (\OC\Files\Filesystem::isValidPath($path)) { |
|
174 | - $target = $storage->fopen($path, 'w'); |
|
175 | - if ($target) { |
|
176 | - $count = $this->assemble($target); |
|
177 | - fclose($target); |
|
178 | - return $count > 0; |
|
179 | - } else { |
|
180 | - return false; |
|
181 | - } |
|
182 | - } |
|
183 | - return false; |
|
184 | - } |
|
33 | + protected $info; |
|
34 | + protected $cache; |
|
35 | + |
|
36 | + /** |
|
37 | + * TTL of chunks |
|
38 | + * |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + protected $ttl; |
|
42 | + |
|
43 | + static public function decodeName($name) { |
|
44 | + preg_match('/(?P<name>.*)-chunking-(?P<transferid>\d+)-(?P<chunkcount>\d+)-(?P<index>\d+)/', $name, $matches); |
|
45 | + return $matches; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @param string[] $info |
|
50 | + */ |
|
51 | + public function __construct($info) { |
|
52 | + $this->info = $info; |
|
53 | + $this->ttl = \OC::$server->getConfig()->getSystemValue('cache_chunk_gc_ttl', 86400); |
|
54 | + } |
|
55 | + |
|
56 | + public function getPrefix() { |
|
57 | + $name = $this->info['name']; |
|
58 | + $transferid = $this->info['transferid']; |
|
59 | + |
|
60 | + return $name.'-chunking-'.$transferid.'-'; |
|
61 | + } |
|
62 | + |
|
63 | + protected function getCache() { |
|
64 | + if (!isset($this->cache)) { |
|
65 | + $this->cache = new \OC\Cache\File(); |
|
66 | + } |
|
67 | + return $this->cache; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Stores the given $data under the given $key - the number of stored bytes is returned |
|
72 | + * |
|
73 | + * @param string $index |
|
74 | + * @param resource $data |
|
75 | + * @return int |
|
76 | + */ |
|
77 | + public function store($index, $data) { |
|
78 | + $cache = $this->getCache(); |
|
79 | + $name = $this->getPrefix().$index; |
|
80 | + $cache->set($name, $data, $this->ttl); |
|
81 | + |
|
82 | + return $cache->size($name); |
|
83 | + } |
|
84 | + |
|
85 | + public function isComplete() { |
|
86 | + $prefix = $this->getPrefix(); |
|
87 | + $cache = $this->getCache(); |
|
88 | + $chunkcount = (int)$this->info['chunkcount']; |
|
89 | + |
|
90 | + for($i=($chunkcount-1); $i >= 0; $i--) { |
|
91 | + if (!$cache->hasKey($prefix.$i)) { |
|
92 | + return false; |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + return true; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Assembles the chunks into the file specified by the path. |
|
101 | + * Chunks are deleted afterwards. |
|
102 | + * |
|
103 | + * @param resource $f target path |
|
104 | + * |
|
105 | + * @return integer assembled file size |
|
106 | + * |
|
107 | + * @throws \OC\InsufficientStorageException when file could not be fully |
|
108 | + * assembled due to lack of free space |
|
109 | + */ |
|
110 | + public function assemble($f) { |
|
111 | + $cache = $this->getCache(); |
|
112 | + $prefix = $this->getPrefix(); |
|
113 | + $count = 0; |
|
114 | + for ($i = 0; $i < $this->info['chunkcount']; $i++) { |
|
115 | + $chunk = $cache->get($prefix.$i); |
|
116 | + // remove after reading to directly save space |
|
117 | + $cache->remove($prefix.$i); |
|
118 | + $count += fwrite($f, $chunk); |
|
119 | + // let php release the memory to work around memory exhausted error with php 5.6 |
|
120 | + $chunk = null; |
|
121 | + } |
|
122 | + |
|
123 | + return $count; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Returns the size of the chunks already present |
|
128 | + * @return integer size in bytes |
|
129 | + */ |
|
130 | + public function getCurrentSize() { |
|
131 | + $cache = $this->getCache(); |
|
132 | + $prefix = $this->getPrefix(); |
|
133 | + $total = 0; |
|
134 | + for ($i = 0; $i < $this->info['chunkcount']; $i++) { |
|
135 | + $total += $cache->size($prefix.$i); |
|
136 | + } |
|
137 | + return $total; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Removes all chunks which belong to this transmission |
|
142 | + */ |
|
143 | + public function cleanup() { |
|
144 | + $cache = $this->getCache(); |
|
145 | + $prefix = $this->getPrefix(); |
|
146 | + for($i=0; $i < $this->info['chunkcount']; $i++) { |
|
147 | + $cache->remove($prefix.$i); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Removes one specific chunk |
|
153 | + * @param string $index |
|
154 | + */ |
|
155 | + public function remove($index) { |
|
156 | + $cache = $this->getCache(); |
|
157 | + $prefix = $this->getPrefix(); |
|
158 | + $cache->remove($prefix.$index); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Assembles the chunks into the file specified by the path. |
|
163 | + * Also triggers the relevant hooks and proxies. |
|
164 | + * |
|
165 | + * @param \OC\Files\Storage\Storage $storage storage |
|
166 | + * @param string $path target path relative to the storage |
|
167 | + * @return bool true on success or false if file could not be created |
|
168 | + * |
|
169 | + * @throws \OC\ServerNotAvailableException |
|
170 | + */ |
|
171 | + public function file_assemble($storage, $path) { |
|
172 | + // use file_put_contents as method because that best matches what this function does |
|
173 | + if (\OC\Files\Filesystem::isValidPath($path)) { |
|
174 | + $target = $storage->fopen($path, 'w'); |
|
175 | + if ($target) { |
|
176 | + $count = $this->assemble($target); |
|
177 | + fclose($target); |
|
178 | + return $count > 0; |
|
179 | + } else { |
|
180 | + return false; |
|
181 | + } |
|
182 | + } |
|
183 | + return false; |
|
184 | + } |
|
185 | 185 | } |
@@ -33,210 +33,210 @@ |
||
33 | 33 | */ |
34 | 34 | class OC_DB { |
35 | 35 | |
36 | - /** |
|
37 | - * get MDB2 schema manager |
|
38 | - * |
|
39 | - * @return \OC\DB\MDB2SchemaManager |
|
40 | - */ |
|
41 | - private static function getMDB2SchemaManager() { |
|
42 | - return new \OC\DB\MDB2SchemaManager(\OC::$server->getDatabaseConnection()); |
|
43 | - } |
|
36 | + /** |
|
37 | + * get MDB2 schema manager |
|
38 | + * |
|
39 | + * @return \OC\DB\MDB2SchemaManager |
|
40 | + */ |
|
41 | + private static function getMDB2SchemaManager() { |
|
42 | + return new \OC\DB\MDB2SchemaManager(\OC::$server->getDatabaseConnection()); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Prepare a SQL query |
|
47 | - * @param string $query Query string |
|
48 | - * @param int $limit |
|
49 | - * @param int $offset |
|
50 | - * @param bool $isManipulation |
|
51 | - * @throws \OC\DatabaseException |
|
52 | - * @return OC_DB_StatementWrapper prepared SQL query |
|
53 | - * |
|
54 | - * SQL query via Doctrine prepare(), needs to be execute()'d! |
|
55 | - */ |
|
56 | - static public function prepare( $query , $limit = null, $offset = null, $isManipulation = null) { |
|
57 | - $connection = \OC::$server->getDatabaseConnection(); |
|
45 | + /** |
|
46 | + * Prepare a SQL query |
|
47 | + * @param string $query Query string |
|
48 | + * @param int $limit |
|
49 | + * @param int $offset |
|
50 | + * @param bool $isManipulation |
|
51 | + * @throws \OC\DatabaseException |
|
52 | + * @return OC_DB_StatementWrapper prepared SQL query |
|
53 | + * |
|
54 | + * SQL query via Doctrine prepare(), needs to be execute()'d! |
|
55 | + */ |
|
56 | + static public function prepare( $query , $limit = null, $offset = null, $isManipulation = null) { |
|
57 | + $connection = \OC::$server->getDatabaseConnection(); |
|
58 | 58 | |
59 | - if ($isManipulation === null) { |
|
60 | - //try to guess, so we return the number of rows on manipulations |
|
61 | - $isManipulation = self::isManipulation($query); |
|
62 | - } |
|
59 | + if ($isManipulation === null) { |
|
60 | + //try to guess, so we return the number of rows on manipulations |
|
61 | + $isManipulation = self::isManipulation($query); |
|
62 | + } |
|
63 | 63 | |
64 | - // return the result |
|
65 | - try { |
|
66 | - $result =$connection->prepare($query, $limit, $offset); |
|
67 | - } catch (\Doctrine\DBAL\DBALException $e) { |
|
68 | - throw new \OC\DatabaseException($e->getMessage(), $query); |
|
69 | - } |
|
70 | - // differentiate between query and manipulation |
|
71 | - $result = new OC_DB_StatementWrapper($result, $isManipulation); |
|
72 | - return $result; |
|
73 | - } |
|
64 | + // return the result |
|
65 | + try { |
|
66 | + $result =$connection->prepare($query, $limit, $offset); |
|
67 | + } catch (\Doctrine\DBAL\DBALException $e) { |
|
68 | + throw new \OC\DatabaseException($e->getMessage(), $query); |
|
69 | + } |
|
70 | + // differentiate between query and manipulation |
|
71 | + $result = new OC_DB_StatementWrapper($result, $isManipulation); |
|
72 | + return $result; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * tries to guess the type of statement based on the first 10 characters |
|
77 | - * the current check allows some whitespace but does not work with IF EXISTS or other more complex statements |
|
78 | - * |
|
79 | - * @param string $sql |
|
80 | - * @return bool |
|
81 | - */ |
|
82 | - static public function isManipulation( $sql ) { |
|
83 | - $selectOccurrence = stripos($sql, 'SELECT'); |
|
84 | - if ($selectOccurrence !== false && $selectOccurrence < 10) { |
|
85 | - return false; |
|
86 | - } |
|
87 | - $insertOccurrence = stripos($sql, 'INSERT'); |
|
88 | - if ($insertOccurrence !== false && $insertOccurrence < 10) { |
|
89 | - return true; |
|
90 | - } |
|
91 | - $updateOccurrence = stripos($sql, 'UPDATE'); |
|
92 | - if ($updateOccurrence !== false && $updateOccurrence < 10) { |
|
93 | - return true; |
|
94 | - } |
|
95 | - $deleteOccurrence = stripos($sql, 'DELETE'); |
|
96 | - if ($deleteOccurrence !== false && $deleteOccurrence < 10) { |
|
97 | - return true; |
|
98 | - } |
|
99 | - return false; |
|
100 | - } |
|
75 | + /** |
|
76 | + * tries to guess the type of statement based on the first 10 characters |
|
77 | + * the current check allows some whitespace but does not work with IF EXISTS or other more complex statements |
|
78 | + * |
|
79 | + * @param string $sql |
|
80 | + * @return bool |
|
81 | + */ |
|
82 | + static public function isManipulation( $sql ) { |
|
83 | + $selectOccurrence = stripos($sql, 'SELECT'); |
|
84 | + if ($selectOccurrence !== false && $selectOccurrence < 10) { |
|
85 | + return false; |
|
86 | + } |
|
87 | + $insertOccurrence = stripos($sql, 'INSERT'); |
|
88 | + if ($insertOccurrence !== false && $insertOccurrence < 10) { |
|
89 | + return true; |
|
90 | + } |
|
91 | + $updateOccurrence = stripos($sql, 'UPDATE'); |
|
92 | + if ($updateOccurrence !== false && $updateOccurrence < 10) { |
|
93 | + return true; |
|
94 | + } |
|
95 | + $deleteOccurrence = stripos($sql, 'DELETE'); |
|
96 | + if ($deleteOccurrence !== false && $deleteOccurrence < 10) { |
|
97 | + return true; |
|
98 | + } |
|
99 | + return false; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * execute a prepared statement, on error write log and throw exception |
|
104 | - * @param mixed $stmt OC_DB_StatementWrapper, |
|
105 | - * an array with 'sql' and optionally 'limit' and 'offset' keys |
|
106 | - * .. or a simple sql query string |
|
107 | - * @param array $parameters |
|
108 | - * @return OC_DB_StatementWrapper |
|
109 | - * @throws \OC\DatabaseException |
|
110 | - */ |
|
111 | - static public function executeAudited( $stmt, array $parameters = null) { |
|
112 | - if (is_string($stmt)) { |
|
113 | - // convert to an array with 'sql' |
|
114 | - if (stripos($stmt, 'LIMIT') !== false) { //OFFSET requires LIMIT, so we only need to check for LIMIT |
|
115 | - // TODO try to convert LIMIT OFFSET notation to parameters |
|
116 | - $message = 'LIMIT and OFFSET are forbidden for portability reasons,' |
|
117 | - . ' pass an array with \'limit\' and \'offset\' instead'; |
|
118 | - throw new \OC\DatabaseException($message); |
|
119 | - } |
|
120 | - $stmt = array('sql' => $stmt, 'limit' => null, 'offset' => null); |
|
121 | - } |
|
122 | - if (is_array($stmt)) { |
|
123 | - // convert to prepared statement |
|
124 | - if ( ! array_key_exists('sql', $stmt) ) { |
|
125 | - $message = 'statement array must at least contain key \'sql\''; |
|
126 | - throw new \OC\DatabaseException($message); |
|
127 | - } |
|
128 | - if ( ! array_key_exists('limit', $stmt) ) { |
|
129 | - $stmt['limit'] = null; |
|
130 | - } |
|
131 | - if ( ! array_key_exists('limit', $stmt) ) { |
|
132 | - $stmt['offset'] = null; |
|
133 | - } |
|
134 | - $stmt = self::prepare($stmt['sql'], $stmt['limit'], $stmt['offset']); |
|
135 | - } |
|
136 | - self::raiseExceptionOnError($stmt, 'Could not prepare statement'); |
|
137 | - if ($stmt instanceof OC_DB_StatementWrapper) { |
|
138 | - $result = $stmt->execute($parameters); |
|
139 | - self::raiseExceptionOnError($result, 'Could not execute statement'); |
|
140 | - } else { |
|
141 | - if (is_object($stmt)) { |
|
142 | - $message = 'Expected a prepared statement or array got ' . get_class($stmt); |
|
143 | - } else { |
|
144 | - $message = 'Expected a prepared statement or array got ' . gettype($stmt); |
|
145 | - } |
|
146 | - throw new \OC\DatabaseException($message); |
|
147 | - } |
|
148 | - return $result; |
|
149 | - } |
|
102 | + /** |
|
103 | + * execute a prepared statement, on error write log and throw exception |
|
104 | + * @param mixed $stmt OC_DB_StatementWrapper, |
|
105 | + * an array with 'sql' and optionally 'limit' and 'offset' keys |
|
106 | + * .. or a simple sql query string |
|
107 | + * @param array $parameters |
|
108 | + * @return OC_DB_StatementWrapper |
|
109 | + * @throws \OC\DatabaseException |
|
110 | + */ |
|
111 | + static public function executeAudited( $stmt, array $parameters = null) { |
|
112 | + if (is_string($stmt)) { |
|
113 | + // convert to an array with 'sql' |
|
114 | + if (stripos($stmt, 'LIMIT') !== false) { //OFFSET requires LIMIT, so we only need to check for LIMIT |
|
115 | + // TODO try to convert LIMIT OFFSET notation to parameters |
|
116 | + $message = 'LIMIT and OFFSET are forbidden for portability reasons,' |
|
117 | + . ' pass an array with \'limit\' and \'offset\' instead'; |
|
118 | + throw new \OC\DatabaseException($message); |
|
119 | + } |
|
120 | + $stmt = array('sql' => $stmt, 'limit' => null, 'offset' => null); |
|
121 | + } |
|
122 | + if (is_array($stmt)) { |
|
123 | + // convert to prepared statement |
|
124 | + if ( ! array_key_exists('sql', $stmt) ) { |
|
125 | + $message = 'statement array must at least contain key \'sql\''; |
|
126 | + throw new \OC\DatabaseException($message); |
|
127 | + } |
|
128 | + if ( ! array_key_exists('limit', $stmt) ) { |
|
129 | + $stmt['limit'] = null; |
|
130 | + } |
|
131 | + if ( ! array_key_exists('limit', $stmt) ) { |
|
132 | + $stmt['offset'] = null; |
|
133 | + } |
|
134 | + $stmt = self::prepare($stmt['sql'], $stmt['limit'], $stmt['offset']); |
|
135 | + } |
|
136 | + self::raiseExceptionOnError($stmt, 'Could not prepare statement'); |
|
137 | + if ($stmt instanceof OC_DB_StatementWrapper) { |
|
138 | + $result = $stmt->execute($parameters); |
|
139 | + self::raiseExceptionOnError($result, 'Could not execute statement'); |
|
140 | + } else { |
|
141 | + if (is_object($stmt)) { |
|
142 | + $message = 'Expected a prepared statement or array got ' . get_class($stmt); |
|
143 | + } else { |
|
144 | + $message = 'Expected a prepared statement or array got ' . gettype($stmt); |
|
145 | + } |
|
146 | + throw new \OC\DatabaseException($message); |
|
147 | + } |
|
148 | + return $result; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * saves database schema to xml file |
|
153 | - * @param string $file name of file |
|
154 | - * @param int $mode |
|
155 | - * @return bool |
|
156 | - * |
|
157 | - * TODO: write more documentation |
|
158 | - */ |
|
159 | - public static function getDbStructure($file) { |
|
160 | - $schemaManager = self::getMDB2SchemaManager(); |
|
161 | - return $schemaManager->getDbStructure($file); |
|
162 | - } |
|
151 | + /** |
|
152 | + * saves database schema to xml file |
|
153 | + * @param string $file name of file |
|
154 | + * @param int $mode |
|
155 | + * @return bool |
|
156 | + * |
|
157 | + * TODO: write more documentation |
|
158 | + */ |
|
159 | + public static function getDbStructure($file) { |
|
160 | + $schemaManager = self::getMDB2SchemaManager(); |
|
161 | + return $schemaManager->getDbStructure($file); |
|
162 | + } |
|
163 | 163 | |
164 | - /** |
|
165 | - * Creates tables from XML file |
|
166 | - * @param string $file file to read structure from |
|
167 | - * @return bool |
|
168 | - * |
|
169 | - * TODO: write more documentation |
|
170 | - */ |
|
171 | - public static function createDbFromStructure( $file ) { |
|
172 | - $schemaManager = self::getMDB2SchemaManager(); |
|
173 | - $result = $schemaManager->createDbFromStructure($file); |
|
174 | - return $result; |
|
175 | - } |
|
164 | + /** |
|
165 | + * Creates tables from XML file |
|
166 | + * @param string $file file to read structure from |
|
167 | + * @return bool |
|
168 | + * |
|
169 | + * TODO: write more documentation |
|
170 | + */ |
|
171 | + public static function createDbFromStructure( $file ) { |
|
172 | + $schemaManager = self::getMDB2SchemaManager(); |
|
173 | + $result = $schemaManager->createDbFromStructure($file); |
|
174 | + return $result; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * update the database schema |
|
179 | - * @param string $file file to read structure from |
|
180 | - * @throws Exception |
|
181 | - * @return string|boolean |
|
182 | - */ |
|
183 | - public static function updateDbFromStructure($file) { |
|
184 | - $schemaManager = self::getMDB2SchemaManager(); |
|
185 | - try { |
|
186 | - $result = $schemaManager->updateDbFromStructure($file); |
|
187 | - } catch (Exception $e) { |
|
188 | - \OCP\Util::writeLog('core', 'Failed to update database structure ('.$e.')', \OCP\Util::FATAL); |
|
189 | - throw $e; |
|
190 | - } |
|
191 | - return $result; |
|
192 | - } |
|
177 | + /** |
|
178 | + * update the database schema |
|
179 | + * @param string $file file to read structure from |
|
180 | + * @throws Exception |
|
181 | + * @return string|boolean |
|
182 | + */ |
|
183 | + public static function updateDbFromStructure($file) { |
|
184 | + $schemaManager = self::getMDB2SchemaManager(); |
|
185 | + try { |
|
186 | + $result = $schemaManager->updateDbFromStructure($file); |
|
187 | + } catch (Exception $e) { |
|
188 | + \OCP\Util::writeLog('core', 'Failed to update database structure ('.$e.')', \OCP\Util::FATAL); |
|
189 | + throw $e; |
|
190 | + } |
|
191 | + return $result; |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * remove all tables defined in a database structure xml file |
|
196 | - * @param string $file the xml file describing the tables |
|
197 | - */ |
|
198 | - public static function removeDBStructure($file) { |
|
199 | - $schemaManager = self::getMDB2SchemaManager(); |
|
200 | - $schemaManager->removeDBStructure($file); |
|
201 | - } |
|
194 | + /** |
|
195 | + * remove all tables defined in a database structure xml file |
|
196 | + * @param string $file the xml file describing the tables |
|
197 | + */ |
|
198 | + public static function removeDBStructure($file) { |
|
199 | + $schemaManager = self::getMDB2SchemaManager(); |
|
200 | + $schemaManager->removeDBStructure($file); |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * check if a result is an error and throws an exception, works with \Doctrine\DBAL\DBALException |
|
205 | - * @param mixed $result |
|
206 | - * @param string $message |
|
207 | - * @return void |
|
208 | - * @throws \OC\DatabaseException |
|
209 | - */ |
|
210 | - public static function raiseExceptionOnError($result, $message = null) { |
|
211 | - if($result === false) { |
|
212 | - if ($message === null) { |
|
213 | - $message = self::getErrorMessage(); |
|
214 | - } else { |
|
215 | - $message .= ', Root cause:' . self::getErrorMessage(); |
|
216 | - } |
|
217 | - throw new \OC\DatabaseException($message, \OC::$server->getDatabaseConnection()->errorCode()); |
|
218 | - } |
|
219 | - } |
|
203 | + /** |
|
204 | + * check if a result is an error and throws an exception, works with \Doctrine\DBAL\DBALException |
|
205 | + * @param mixed $result |
|
206 | + * @param string $message |
|
207 | + * @return void |
|
208 | + * @throws \OC\DatabaseException |
|
209 | + */ |
|
210 | + public static function raiseExceptionOnError($result, $message = null) { |
|
211 | + if($result === false) { |
|
212 | + if ($message === null) { |
|
213 | + $message = self::getErrorMessage(); |
|
214 | + } else { |
|
215 | + $message .= ', Root cause:' . self::getErrorMessage(); |
|
216 | + } |
|
217 | + throw new \OC\DatabaseException($message, \OC::$server->getDatabaseConnection()->errorCode()); |
|
218 | + } |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * returns the error code and message as a string for logging |
|
223 | - * works with DoctrineException |
|
224 | - * @return string |
|
225 | - */ |
|
226 | - public static function getErrorMessage() { |
|
227 | - $connection = \OC::$server->getDatabaseConnection(); |
|
228 | - return $connection->getError(); |
|
229 | - } |
|
221 | + /** |
|
222 | + * returns the error code and message as a string for logging |
|
223 | + * works with DoctrineException |
|
224 | + * @return string |
|
225 | + */ |
|
226 | + public static function getErrorMessage() { |
|
227 | + $connection = \OC::$server->getDatabaseConnection(); |
|
228 | + return $connection->getError(); |
|
229 | + } |
|
230 | 230 | |
231 | - /** |
|
232 | - * Checks if a table exists in the database - the database prefix will be prepended |
|
233 | - * |
|
234 | - * @param string $table |
|
235 | - * @return bool |
|
236 | - * @throws \OC\DatabaseException |
|
237 | - */ |
|
238 | - public static function tableExists($table) { |
|
239 | - $connection = \OC::$server->getDatabaseConnection(); |
|
240 | - return $connection->tableExists($table); |
|
241 | - } |
|
231 | + /** |
|
232 | + * Checks if a table exists in the database - the database prefix will be prepended |
|
233 | + * |
|
234 | + * @param string $table |
|
235 | + * @return bool |
|
236 | + * @throws \OC\DatabaseException |
|
237 | + */ |
|
238 | + public static function tableExists($table) { |
|
239 | + $connection = \OC::$server->getDatabaseConnection(); |
|
240 | + return $connection->tableExists($table); |
|
241 | + } |
|
242 | 242 | } |
@@ -29,118 +29,118 @@ |
||
29 | 29 | * |
30 | 30 | */ |
31 | 31 | class OC_Hook{ |
32 | - public static $thrownExceptions = []; |
|
32 | + public static $thrownExceptions = []; |
|
33 | 33 | |
34 | - static private $registered = array(); |
|
34 | + static private $registered = array(); |
|
35 | 35 | |
36 | - /** |
|
37 | - * connects a function to a hook |
|
38 | - * |
|
39 | - * @param string $signalClass class name of emitter |
|
40 | - * @param string $signalName name of signal |
|
41 | - * @param string|object $slotClass class name of slot |
|
42 | - * @param string $slotName name of slot |
|
43 | - * @return bool |
|
44 | - * |
|
45 | - * This function makes it very easy to connect to use hooks. |
|
46 | - * |
|
47 | - * TODO: write example |
|
48 | - */ |
|
49 | - static public function connect($signalClass, $signalName, $slotClass, $slotName ) { |
|
50 | - // If we're trying to connect to an emitting class that isn't |
|
51 | - // yet registered, register it |
|
52 | - if( !array_key_exists($signalClass, self::$registered )) { |
|
53 | - self::$registered[$signalClass] = array(); |
|
54 | - } |
|
55 | - // If we're trying to connect to an emitting method that isn't |
|
56 | - // yet registered, register it with the emitting class |
|
57 | - if( !array_key_exists( $signalName, self::$registered[$signalClass] )) { |
|
58 | - self::$registered[$signalClass][$signalName] = array(); |
|
59 | - } |
|
36 | + /** |
|
37 | + * connects a function to a hook |
|
38 | + * |
|
39 | + * @param string $signalClass class name of emitter |
|
40 | + * @param string $signalName name of signal |
|
41 | + * @param string|object $slotClass class name of slot |
|
42 | + * @param string $slotName name of slot |
|
43 | + * @return bool |
|
44 | + * |
|
45 | + * This function makes it very easy to connect to use hooks. |
|
46 | + * |
|
47 | + * TODO: write example |
|
48 | + */ |
|
49 | + static public function connect($signalClass, $signalName, $slotClass, $slotName ) { |
|
50 | + // If we're trying to connect to an emitting class that isn't |
|
51 | + // yet registered, register it |
|
52 | + if( !array_key_exists($signalClass, self::$registered )) { |
|
53 | + self::$registered[$signalClass] = array(); |
|
54 | + } |
|
55 | + // If we're trying to connect to an emitting method that isn't |
|
56 | + // yet registered, register it with the emitting class |
|
57 | + if( !array_key_exists( $signalName, self::$registered[$signalClass] )) { |
|
58 | + self::$registered[$signalClass][$signalName] = array(); |
|
59 | + } |
|
60 | 60 | |
61 | - // don't connect hooks twice |
|
62 | - foreach (self::$registered[$signalClass][$signalName] as $hook) { |
|
63 | - if ($hook['class'] === $slotClass and $hook['name'] === $slotName) { |
|
64 | - return false; |
|
65 | - } |
|
66 | - } |
|
67 | - // Connect the hook handler to the requested emitter |
|
68 | - self::$registered[$signalClass][$signalName][] = array( |
|
69 | - "class" => $slotClass, |
|
70 | - "name" => $slotName |
|
71 | - ); |
|
61 | + // don't connect hooks twice |
|
62 | + foreach (self::$registered[$signalClass][$signalName] as $hook) { |
|
63 | + if ($hook['class'] === $slotClass and $hook['name'] === $slotName) { |
|
64 | + return false; |
|
65 | + } |
|
66 | + } |
|
67 | + // Connect the hook handler to the requested emitter |
|
68 | + self::$registered[$signalClass][$signalName][] = array( |
|
69 | + "class" => $slotClass, |
|
70 | + "name" => $slotName |
|
71 | + ); |
|
72 | 72 | |
73 | - // No chance for failure ;-) |
|
74 | - return true; |
|
75 | - } |
|
73 | + // No chance for failure ;-) |
|
74 | + return true; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * emits a signal |
|
79 | - * |
|
80 | - * @param string $signalClass class name of emitter |
|
81 | - * @param string $signalName name of signal |
|
82 | - * @param mixed $params default: array() array with additional data |
|
83 | - * @return bool true if slots exists or false if not |
|
84 | - * @throws \OC\HintException |
|
85 | - * @throws \OC\ServerNotAvailableException Emits a signal. To get data from the slot use references! |
|
86 | - * |
|
87 | - * TODO: write example |
|
88 | - */ |
|
89 | - static public function emit($signalClass, $signalName, $params = []) { |
|
77 | + /** |
|
78 | + * emits a signal |
|
79 | + * |
|
80 | + * @param string $signalClass class name of emitter |
|
81 | + * @param string $signalName name of signal |
|
82 | + * @param mixed $params default: array() array with additional data |
|
83 | + * @return bool true if slots exists or false if not |
|
84 | + * @throws \OC\HintException |
|
85 | + * @throws \OC\ServerNotAvailableException Emits a signal. To get data from the slot use references! |
|
86 | + * |
|
87 | + * TODO: write example |
|
88 | + */ |
|
89 | + static public function emit($signalClass, $signalName, $params = []) { |
|
90 | 90 | |
91 | - // Return false if no hook handlers are listening to this |
|
92 | - // emitting class |
|
93 | - if( !array_key_exists($signalClass, self::$registered )) { |
|
94 | - return false; |
|
95 | - } |
|
91 | + // Return false if no hook handlers are listening to this |
|
92 | + // emitting class |
|
93 | + if( !array_key_exists($signalClass, self::$registered )) { |
|
94 | + return false; |
|
95 | + } |
|
96 | 96 | |
97 | - // Return false if no hook handlers are listening to this |
|
98 | - // emitting method |
|
99 | - if( !array_key_exists( $signalName, self::$registered[$signalClass] )) { |
|
100 | - return false; |
|
101 | - } |
|
97 | + // Return false if no hook handlers are listening to this |
|
98 | + // emitting method |
|
99 | + if( !array_key_exists( $signalName, self::$registered[$signalClass] )) { |
|
100 | + return false; |
|
101 | + } |
|
102 | 102 | |
103 | - // Call all slots |
|
104 | - foreach( self::$registered[$signalClass][$signalName] as $i ) { |
|
105 | - try { |
|
106 | - call_user_func( array( $i["class"], $i["name"] ), $params ); |
|
107 | - } catch (Exception $e){ |
|
108 | - self::$thrownExceptions[] = $e; |
|
109 | - \OC::$server->getLogger()->logException($e); |
|
110 | - if($e instanceof \OC\HintException) { |
|
111 | - throw $e; |
|
112 | - } |
|
113 | - if($e instanceof \OC\ServerNotAvailableException) { |
|
114 | - throw $e; |
|
115 | - } |
|
116 | - } |
|
117 | - } |
|
103 | + // Call all slots |
|
104 | + foreach( self::$registered[$signalClass][$signalName] as $i ) { |
|
105 | + try { |
|
106 | + call_user_func( array( $i["class"], $i["name"] ), $params ); |
|
107 | + } catch (Exception $e){ |
|
108 | + self::$thrownExceptions[] = $e; |
|
109 | + \OC::$server->getLogger()->logException($e); |
|
110 | + if($e instanceof \OC\HintException) { |
|
111 | + throw $e; |
|
112 | + } |
|
113 | + if($e instanceof \OC\ServerNotAvailableException) { |
|
114 | + throw $e; |
|
115 | + } |
|
116 | + } |
|
117 | + } |
|
118 | 118 | |
119 | - return true; |
|
120 | - } |
|
119 | + return true; |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * clear hooks |
|
124 | - * @param string $signalClass |
|
125 | - * @param string $signalName |
|
126 | - */ |
|
127 | - static public function clear($signalClass='', $signalName='') { |
|
128 | - if ($signalClass) { |
|
129 | - if ($signalName) { |
|
130 | - self::$registered[$signalClass][$signalName]=array(); |
|
131 | - }else{ |
|
132 | - self::$registered[$signalClass]=array(); |
|
133 | - } |
|
134 | - }else{ |
|
135 | - self::$registered=array(); |
|
136 | - } |
|
137 | - } |
|
122 | + /** |
|
123 | + * clear hooks |
|
124 | + * @param string $signalClass |
|
125 | + * @param string $signalName |
|
126 | + */ |
|
127 | + static public function clear($signalClass='', $signalName='') { |
|
128 | + if ($signalClass) { |
|
129 | + if ($signalName) { |
|
130 | + self::$registered[$signalClass][$signalName]=array(); |
|
131 | + }else{ |
|
132 | + self::$registered[$signalClass]=array(); |
|
133 | + } |
|
134 | + }else{ |
|
135 | + self::$registered=array(); |
|
136 | + } |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * DO NOT USE! |
|
141 | - * For unit tests ONLY! |
|
142 | - */ |
|
143 | - static public function getHooks() { |
|
144 | - return self::$registered; |
|
145 | - } |
|
139 | + /** |
|
140 | + * DO NOT USE! |
|
141 | + * For unit tests ONLY! |
|
142 | + */ |
|
143 | + static public function getHooks() { |
|
144 | + return self::$registered; |
|
145 | + } |
|
146 | 146 | } |
@@ -48,606 +48,606 @@ |
||
48 | 48 | * Collection of useful functions |
49 | 49 | */ |
50 | 50 | class OC_Helper { |
51 | - private static $templateManager; |
|
52 | - |
|
53 | - /** |
|
54 | - * Creates an absolute url for public use |
|
55 | - * @param string $service id |
|
56 | - * @param bool $add_slash |
|
57 | - * @return string the url |
|
58 | - * |
|
59 | - * Returns a absolute url to the given service. |
|
60 | - */ |
|
61 | - public static function linkToPublic($service, $add_slash = false) { |
|
62 | - if ($service === 'files') { |
|
63 | - $url = OC::$server->getURLGenerator()->getAbsoluteURL('/s'); |
|
64 | - } else { |
|
65 | - $url = OC::$server->getURLGenerator()->getAbsoluteURL(OC::$server->getURLGenerator()->linkTo('', 'public.php').'?service='.$service); |
|
66 | - } |
|
67 | - return $url . (($add_slash && $service[strlen($service) - 1] != '/') ? '/' : ''); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Make a human file size |
|
72 | - * @param int $bytes file size in bytes |
|
73 | - * @return string a human readable file size |
|
74 | - * |
|
75 | - * Makes 2048 to 2 kB. |
|
76 | - */ |
|
77 | - public static function humanFileSize($bytes) { |
|
78 | - if ($bytes < 0) { |
|
79 | - return "?"; |
|
80 | - } |
|
81 | - if ($bytes < 1024) { |
|
82 | - return "$bytes B"; |
|
83 | - } |
|
84 | - $bytes = round($bytes / 1024, 0); |
|
85 | - if ($bytes < 1024) { |
|
86 | - return "$bytes KB"; |
|
87 | - } |
|
88 | - $bytes = round($bytes / 1024, 1); |
|
89 | - if ($bytes < 1024) { |
|
90 | - return "$bytes MB"; |
|
91 | - } |
|
92 | - $bytes = round($bytes / 1024, 1); |
|
93 | - if ($bytes < 1024) { |
|
94 | - return "$bytes GB"; |
|
95 | - } |
|
96 | - $bytes = round($bytes / 1024, 1); |
|
97 | - if ($bytes < 1024) { |
|
98 | - return "$bytes TB"; |
|
99 | - } |
|
100 | - |
|
101 | - $bytes = round($bytes / 1024, 1); |
|
102 | - return "$bytes PB"; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Make a php file size |
|
107 | - * @param int $bytes file size in bytes |
|
108 | - * @return string a php parseable file size |
|
109 | - * |
|
110 | - * Makes 2048 to 2k and 2^41 to 2048G |
|
111 | - */ |
|
112 | - public static function phpFileSize($bytes) { |
|
113 | - if ($bytes < 0) { |
|
114 | - return "?"; |
|
115 | - } |
|
116 | - if ($bytes < 1024) { |
|
117 | - return $bytes . "B"; |
|
118 | - } |
|
119 | - $bytes = round($bytes / 1024, 1); |
|
120 | - if ($bytes < 1024) { |
|
121 | - return $bytes . "K"; |
|
122 | - } |
|
123 | - $bytes = round($bytes / 1024, 1); |
|
124 | - if ($bytes < 1024) { |
|
125 | - return $bytes . "M"; |
|
126 | - } |
|
127 | - $bytes = round($bytes / 1024, 1); |
|
128 | - return $bytes . "G"; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Make a computer file size |
|
133 | - * @param string $str file size in human readable format |
|
134 | - * @return float a file size in bytes |
|
135 | - * |
|
136 | - * Makes 2kB to 2048. |
|
137 | - * |
|
138 | - * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
139 | - */ |
|
140 | - public static function computerFileSize($str) { |
|
141 | - $str = strtolower($str); |
|
142 | - if (is_numeric($str)) { |
|
143 | - return floatval($str); |
|
144 | - } |
|
145 | - |
|
146 | - $bytes_array = array( |
|
147 | - 'b' => 1, |
|
148 | - 'k' => 1024, |
|
149 | - 'kb' => 1024, |
|
150 | - 'mb' => 1024 * 1024, |
|
151 | - 'm' => 1024 * 1024, |
|
152 | - 'gb' => 1024 * 1024 * 1024, |
|
153 | - 'g' => 1024 * 1024 * 1024, |
|
154 | - 'tb' => 1024 * 1024 * 1024 * 1024, |
|
155 | - 't' => 1024 * 1024 * 1024 * 1024, |
|
156 | - 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
157 | - 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
158 | - ); |
|
159 | - |
|
160 | - $bytes = floatval($str); |
|
161 | - |
|
162 | - if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { |
|
163 | - $bytes *= $bytes_array[$matches[1]]; |
|
164 | - } else { |
|
165 | - return false; |
|
166 | - } |
|
167 | - |
|
168 | - $bytes = round($bytes); |
|
169 | - |
|
170 | - return $bytes; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Recursive copying of folders |
|
175 | - * @param string $src source folder |
|
176 | - * @param string $dest target folder |
|
177 | - * |
|
178 | - */ |
|
179 | - static function copyr($src, $dest) { |
|
180 | - if (is_dir($src)) { |
|
181 | - if (!is_dir($dest)) { |
|
182 | - mkdir($dest); |
|
183 | - } |
|
184 | - $files = scandir($src); |
|
185 | - foreach ($files as $file) { |
|
186 | - if ($file != "." && $file != "..") { |
|
187 | - self::copyr("$src/$file", "$dest/$file"); |
|
188 | - } |
|
189 | - } |
|
190 | - } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) { |
|
191 | - copy($src, $dest); |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Recursive deletion of folders |
|
197 | - * @param string $dir path to the folder |
|
198 | - * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
199 | - * @return bool |
|
200 | - */ |
|
201 | - static function rmdirr($dir, $deleteSelf = true) { |
|
202 | - if (is_dir($dir)) { |
|
203 | - $files = new RecursiveIteratorIterator( |
|
204 | - new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
205 | - RecursiveIteratorIterator::CHILD_FIRST |
|
206 | - ); |
|
207 | - |
|
208 | - foreach ($files as $fileInfo) { |
|
209 | - /** @var SplFileInfo $fileInfo */ |
|
210 | - if ($fileInfo->isLink()) { |
|
211 | - unlink($fileInfo->getPathname()); |
|
212 | - } else if ($fileInfo->isDir()) { |
|
213 | - rmdir($fileInfo->getRealPath()); |
|
214 | - } else { |
|
215 | - unlink($fileInfo->getRealPath()); |
|
216 | - } |
|
217 | - } |
|
218 | - if ($deleteSelf) { |
|
219 | - rmdir($dir); |
|
220 | - } |
|
221 | - } elseif (file_exists($dir)) { |
|
222 | - if ($deleteSelf) { |
|
223 | - unlink($dir); |
|
224 | - } |
|
225 | - } |
|
226 | - if (!$deleteSelf) { |
|
227 | - return true; |
|
228 | - } |
|
229 | - |
|
230 | - return !file_exists($dir); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * @return \OC\Files\Type\TemplateManager |
|
235 | - */ |
|
236 | - static public function getFileTemplateManager() { |
|
237 | - if (!self::$templateManager) { |
|
238 | - self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
239 | - } |
|
240 | - return self::$templateManager; |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * detect if a given program is found in the search PATH |
|
245 | - * |
|
246 | - * @param string $name |
|
247 | - * @param bool $path |
|
248 | - * @internal param string $program name |
|
249 | - * @internal param string $optional search path, defaults to $PATH |
|
250 | - * @return bool true if executable program found in path |
|
251 | - */ |
|
252 | - public static function canExecute($name, $path = false) { |
|
253 | - // path defaults to PATH from environment if not set |
|
254 | - if ($path === false) { |
|
255 | - $path = getenv("PATH"); |
|
256 | - } |
|
257 | - // we look for an executable file of that name |
|
258 | - $exts = [""]; |
|
259 | - $check_fn = "is_executable"; |
|
260 | - // Default check will be done with $path directories : |
|
261 | - $dirs = explode(PATH_SEPARATOR, $path); |
|
262 | - // WARNING : We have to check if open_basedir is enabled : |
|
263 | - $obd = OC::$server->getIniWrapper()->getString('open_basedir'); |
|
264 | - if ($obd != "none") { |
|
265 | - $obd_values = explode(PATH_SEPARATOR, $obd); |
|
266 | - if (count($obd_values) > 0 and $obd_values[0]) { |
|
267 | - // open_basedir is in effect ! |
|
268 | - // We need to check if the program is in one of these dirs : |
|
269 | - $dirs = $obd_values; |
|
270 | - } |
|
271 | - } |
|
272 | - foreach ($dirs as $dir) { |
|
273 | - foreach ($exts as $ext) { |
|
274 | - if ($check_fn("$dir/$name" . $ext)) |
|
275 | - return true; |
|
276 | - } |
|
277 | - } |
|
278 | - return false; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * copy the contents of one stream to another |
|
283 | - * |
|
284 | - * @param resource $source |
|
285 | - * @param resource $target |
|
286 | - * @return array the number of bytes copied and result |
|
287 | - */ |
|
288 | - public static function streamCopy($source, $target) { |
|
289 | - if (!$source or !$target) { |
|
290 | - return array(0, false); |
|
291 | - } |
|
292 | - $bufSize = 8192; |
|
293 | - $result = true; |
|
294 | - $count = 0; |
|
295 | - while (!feof($source)) { |
|
296 | - $buf = fread($source, $bufSize); |
|
297 | - $bytesWritten = fwrite($target, $buf); |
|
298 | - if ($bytesWritten !== false) { |
|
299 | - $count += $bytesWritten; |
|
300 | - } |
|
301 | - // note: strlen is expensive so only use it when necessary, |
|
302 | - // on the last block |
|
303 | - if ($bytesWritten === false |
|
304 | - || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
305 | - ) { |
|
306 | - // write error, could be disk full ? |
|
307 | - $result = false; |
|
308 | - break; |
|
309 | - } |
|
310 | - } |
|
311 | - return array($count, $result); |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Adds a suffix to the name in case the file exists |
|
316 | - * |
|
317 | - * @param string $path |
|
318 | - * @param string $filename |
|
319 | - * @return string |
|
320 | - */ |
|
321 | - public static function buildNotExistingFileName($path, $filename) { |
|
322 | - $view = \OC\Files\Filesystem::getView(); |
|
323 | - return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Adds a suffix to the name in case the file exists |
|
328 | - * |
|
329 | - * @param string $path |
|
330 | - * @param string $filename |
|
331 | - * @return string |
|
332 | - */ |
|
333 | - public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
334 | - if ($path === '/') { |
|
335 | - $path = ''; |
|
336 | - } |
|
337 | - if ($pos = strrpos($filename, '.')) { |
|
338 | - $name = substr($filename, 0, $pos); |
|
339 | - $ext = substr($filename, $pos); |
|
340 | - } else { |
|
341 | - $name = $filename; |
|
342 | - $ext = ''; |
|
343 | - } |
|
344 | - |
|
345 | - $newpath = $path . '/' . $filename; |
|
346 | - if ($view->file_exists($newpath)) { |
|
347 | - if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
348 | - //Replace the last "(number)" with "(number+1)" |
|
349 | - $last_match = count($matches[0]) - 1; |
|
350 | - $counter = $matches[1][$last_match][0] + 1; |
|
351 | - $offset = $matches[0][$last_match][1]; |
|
352 | - $match_length = strlen($matches[0][$last_match][0]); |
|
353 | - } else { |
|
354 | - $counter = 2; |
|
355 | - $match_length = 0; |
|
356 | - $offset = false; |
|
357 | - } |
|
358 | - do { |
|
359 | - if ($offset) { |
|
360 | - //Replace the last "(number)" with "(number+1)" |
|
361 | - $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
362 | - } else { |
|
363 | - $newname = $name . ' (' . $counter . ')'; |
|
364 | - } |
|
365 | - $newpath = $path . '/' . $newname . $ext; |
|
366 | - $counter++; |
|
367 | - } while ($view->file_exists($newpath)); |
|
368 | - } |
|
369 | - |
|
370 | - return $newpath; |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
375 | - * |
|
376 | - * @param array $input The array to work on |
|
377 | - * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
378 | - * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
379 | - * @return array |
|
380 | - * |
|
381 | - * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
382 | - * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
383 | - * |
|
384 | - */ |
|
385 | - public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
386 | - $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
387 | - $ret = array(); |
|
388 | - foreach ($input as $k => $v) { |
|
389 | - $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
390 | - } |
|
391 | - return $ret; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * performs a search in a nested array |
|
396 | - * @param array $haystack the array to be searched |
|
397 | - * @param string $needle the search string |
|
398 | - * @param string $index optional, only search this key name |
|
399 | - * @return mixed the key of the matching field, otherwise false |
|
400 | - * |
|
401 | - * performs a search in a nested array |
|
402 | - * |
|
403 | - * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
|
404 | - */ |
|
405 | - public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
406 | - $aIt = new RecursiveArrayIterator($haystack); |
|
407 | - $it = new RecursiveIteratorIterator($aIt); |
|
408 | - |
|
409 | - while ($it->valid()) { |
|
410 | - if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) { |
|
411 | - return $aIt->key(); |
|
412 | - } |
|
413 | - |
|
414 | - $it->next(); |
|
415 | - } |
|
416 | - |
|
417 | - return false; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * calculates the maximum upload size respecting system settings, free space and user quota |
|
422 | - * |
|
423 | - * @param string $dir the current folder where the user currently operates |
|
424 | - * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
425 | - * @return int number of bytes representing |
|
426 | - */ |
|
427 | - public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
428 | - if (is_null($freeSpace) || $freeSpace < 0){ |
|
429 | - $freeSpace = self::freeSpace($dir); |
|
430 | - } |
|
431 | - return min($freeSpace, self::uploadLimit()); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Calculate free space left within user quota |
|
436 | - * |
|
437 | - * @param string $dir the current folder where the user currently operates |
|
438 | - * @return int number of bytes representing |
|
439 | - */ |
|
440 | - public static function freeSpace($dir) { |
|
441 | - $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
442 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
443 | - $freeSpace = max($freeSpace, 0); |
|
444 | - return $freeSpace; |
|
445 | - } else { |
|
446 | - return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
447 | - } |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Calculate PHP upload limit |
|
452 | - * |
|
453 | - * @return int PHP upload file size limit |
|
454 | - */ |
|
455 | - public static function uploadLimit() { |
|
456 | - $ini = \OC::$server->getIniWrapper(); |
|
457 | - $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
|
458 | - $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
|
459 | - if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
460 | - return INF; |
|
461 | - } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
462 | - return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
463 | - } else { |
|
464 | - return min($upload_max_filesize, $post_max_size); |
|
465 | - } |
|
466 | - } |
|
467 | - |
|
468 | - /** |
|
469 | - * Checks if a function is available |
|
470 | - * |
|
471 | - * @param string $function_name |
|
472 | - * @return bool |
|
473 | - */ |
|
474 | - public static function is_function_enabled($function_name) { |
|
475 | - if (!function_exists($function_name)) { |
|
476 | - return false; |
|
477 | - } |
|
478 | - $ini = \OC::$server->getIniWrapper(); |
|
479 | - $disabled = explode(',', $ini->get('disable_functions')); |
|
480 | - $disabled = array_map('trim', $disabled); |
|
481 | - if (in_array($function_name, $disabled)) { |
|
482 | - return false; |
|
483 | - } |
|
484 | - $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist')); |
|
485 | - $disabled = array_map('trim', $disabled); |
|
486 | - if (in_array($function_name, $disabled)) { |
|
487 | - return false; |
|
488 | - } |
|
489 | - return true; |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Try to find a program |
|
494 | - * |
|
495 | - * @param string $program |
|
496 | - * @return null|string |
|
497 | - */ |
|
498 | - public static function findBinaryPath($program) { |
|
499 | - $memcache = \OC::$server->getMemCacheFactory()->create('findBinaryPath'); |
|
500 | - if ($memcache->hasKey($program)) { |
|
501 | - return $memcache->get($program); |
|
502 | - } |
|
503 | - $result = null; |
|
504 | - if (self::is_function_enabled('exec')) { |
|
505 | - $exeSniffer = new ExecutableFinder(); |
|
506 | - // Returns null if nothing is found |
|
507 | - $result = $exeSniffer->find($program); |
|
508 | - if (empty($result)) { |
|
509 | - $paths = getenv('PATH'); |
|
510 | - if (empty($paths)) { |
|
511 | - $paths = '/usr/local/bin /usr/bin /opt/bin /bin'; |
|
512 | - } else { |
|
513 | - $paths = str_replace(':',' ',getenv('PATH')); |
|
514 | - } |
|
515 | - $command = 'find ' . $paths . ' -name ' . escapeshellarg($program) . ' 2> /dev/null'; |
|
516 | - exec($command, $output, $returnCode); |
|
517 | - if (count($output) > 0) { |
|
518 | - $result = escapeshellcmd($output[0]); |
|
519 | - } |
|
520 | - } |
|
521 | - } |
|
522 | - // store the value for 5 minutes |
|
523 | - $memcache->set($program, $result, 300); |
|
524 | - return $result; |
|
525 | - } |
|
526 | - |
|
527 | - /** |
|
528 | - * Calculate the disc space for the given path |
|
529 | - * |
|
530 | - * @param string $path |
|
531 | - * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
532 | - * @return array |
|
533 | - * @throws \OCP\Files\NotFoundException |
|
534 | - */ |
|
535 | - public static function getStorageInfo($path, $rootInfo = null) { |
|
536 | - // return storage info without adding mount points |
|
537 | - $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
538 | - |
|
539 | - if (!$rootInfo) { |
|
540 | - $rootInfo = \OC\Files\Filesystem::getFileInfo($path, false); |
|
541 | - } |
|
542 | - if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
543 | - throw new \OCP\Files\NotFoundException(); |
|
544 | - } |
|
545 | - $used = $rootInfo->getSize(); |
|
546 | - if ($used < 0) { |
|
547 | - $used = 0; |
|
548 | - } |
|
549 | - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
550 | - $storage = $rootInfo->getStorage(); |
|
551 | - $sourceStorage = $storage; |
|
552 | - if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
553 | - $includeExtStorage = false; |
|
554 | - $sourceStorage = $storage->getSourceStorage(); |
|
555 | - } |
|
556 | - if ($includeExtStorage) { |
|
557 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
558 | - || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
559 | - ) { |
|
560 | - /** @var \OC\Files\Storage\Home $storage */ |
|
561 | - $user = $storage->getUser(); |
|
562 | - } else { |
|
563 | - $user = \OC::$server->getUserSession()->getUser()->getUID(); |
|
564 | - } |
|
565 | - if ($user) { |
|
566 | - $quota = OC_Util::getUserQuota($user); |
|
567 | - } else { |
|
568 | - $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
569 | - } |
|
570 | - if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
571 | - // always get free space / total space from root + mount points |
|
572 | - return self::getGlobalStorageInfo(); |
|
573 | - } |
|
574 | - } |
|
575 | - |
|
576 | - // TODO: need a better way to get total space from storage |
|
577 | - if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
578 | - /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
579 | - $quota = $sourceStorage->getQuota(); |
|
580 | - } |
|
581 | - $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
582 | - if ($free >= 0) { |
|
583 | - $total = $free + $used; |
|
584 | - } else { |
|
585 | - $total = $free; //either unknown or unlimited |
|
586 | - } |
|
587 | - if ($total > 0) { |
|
588 | - if ($quota > 0 && $total > $quota) { |
|
589 | - $total = $quota; |
|
590 | - } |
|
591 | - // prevent division by zero or error codes (negative values) |
|
592 | - $relative = round(($used / $total) * 10000) / 100; |
|
593 | - } else { |
|
594 | - $relative = 0; |
|
595 | - } |
|
596 | - |
|
597 | - $ownerId = $storage->getOwner($path); |
|
598 | - $ownerDisplayName = ''; |
|
599 | - $owner = \OC::$server->getUserManager()->get($ownerId); |
|
600 | - if($owner) { |
|
601 | - $ownerDisplayName = $owner->getDisplayName(); |
|
602 | - } |
|
603 | - |
|
604 | - return [ |
|
605 | - 'free' => $free, |
|
606 | - 'used' => $used, |
|
607 | - 'quota' => $quota, |
|
608 | - 'total' => $total, |
|
609 | - 'relative' => $relative, |
|
610 | - 'owner' => $ownerId, |
|
611 | - 'ownerDisplayName' => $ownerDisplayName, |
|
612 | - ]; |
|
613 | - } |
|
614 | - |
|
615 | - /** |
|
616 | - * Get storage info including all mount points and quota |
|
617 | - * |
|
618 | - * @return array |
|
619 | - */ |
|
620 | - private static function getGlobalStorageInfo() { |
|
621 | - $quota = OC_Util::getUserQuota(\OCP\User::getUser()); |
|
622 | - |
|
623 | - $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
624 | - $used = $rootInfo['size']; |
|
625 | - if ($used < 0) { |
|
626 | - $used = 0; |
|
627 | - } |
|
628 | - |
|
629 | - $total = $quota; |
|
630 | - $free = $quota - $used; |
|
631 | - |
|
632 | - if ($total > 0) { |
|
633 | - if ($quota > 0 && $total > $quota) { |
|
634 | - $total = $quota; |
|
635 | - } |
|
636 | - // prevent division by zero or error codes (negative values) |
|
637 | - $relative = round(($used / $total) * 10000) / 100; |
|
638 | - } else { |
|
639 | - $relative = 0; |
|
640 | - } |
|
641 | - |
|
642 | - return array('free' => $free, 'used' => $used, 'total' => $total, 'relative' => $relative); |
|
643 | - |
|
644 | - } |
|
645 | - |
|
646 | - /** |
|
647 | - * Returns whether the config file is set manually to read-only |
|
648 | - * @return bool |
|
649 | - */ |
|
650 | - public static function isReadOnlyConfigEnabled() { |
|
651 | - return \OC::$server->getConfig()->getSystemValue('config_is_read_only', false); |
|
652 | - } |
|
51 | + private static $templateManager; |
|
52 | + |
|
53 | + /** |
|
54 | + * Creates an absolute url for public use |
|
55 | + * @param string $service id |
|
56 | + * @param bool $add_slash |
|
57 | + * @return string the url |
|
58 | + * |
|
59 | + * Returns a absolute url to the given service. |
|
60 | + */ |
|
61 | + public static function linkToPublic($service, $add_slash = false) { |
|
62 | + if ($service === 'files') { |
|
63 | + $url = OC::$server->getURLGenerator()->getAbsoluteURL('/s'); |
|
64 | + } else { |
|
65 | + $url = OC::$server->getURLGenerator()->getAbsoluteURL(OC::$server->getURLGenerator()->linkTo('', 'public.php').'?service='.$service); |
|
66 | + } |
|
67 | + return $url . (($add_slash && $service[strlen($service) - 1] != '/') ? '/' : ''); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Make a human file size |
|
72 | + * @param int $bytes file size in bytes |
|
73 | + * @return string a human readable file size |
|
74 | + * |
|
75 | + * Makes 2048 to 2 kB. |
|
76 | + */ |
|
77 | + public static function humanFileSize($bytes) { |
|
78 | + if ($bytes < 0) { |
|
79 | + return "?"; |
|
80 | + } |
|
81 | + if ($bytes < 1024) { |
|
82 | + return "$bytes B"; |
|
83 | + } |
|
84 | + $bytes = round($bytes / 1024, 0); |
|
85 | + if ($bytes < 1024) { |
|
86 | + return "$bytes KB"; |
|
87 | + } |
|
88 | + $bytes = round($bytes / 1024, 1); |
|
89 | + if ($bytes < 1024) { |
|
90 | + return "$bytes MB"; |
|
91 | + } |
|
92 | + $bytes = round($bytes / 1024, 1); |
|
93 | + if ($bytes < 1024) { |
|
94 | + return "$bytes GB"; |
|
95 | + } |
|
96 | + $bytes = round($bytes / 1024, 1); |
|
97 | + if ($bytes < 1024) { |
|
98 | + return "$bytes TB"; |
|
99 | + } |
|
100 | + |
|
101 | + $bytes = round($bytes / 1024, 1); |
|
102 | + return "$bytes PB"; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Make a php file size |
|
107 | + * @param int $bytes file size in bytes |
|
108 | + * @return string a php parseable file size |
|
109 | + * |
|
110 | + * Makes 2048 to 2k and 2^41 to 2048G |
|
111 | + */ |
|
112 | + public static function phpFileSize($bytes) { |
|
113 | + if ($bytes < 0) { |
|
114 | + return "?"; |
|
115 | + } |
|
116 | + if ($bytes < 1024) { |
|
117 | + return $bytes . "B"; |
|
118 | + } |
|
119 | + $bytes = round($bytes / 1024, 1); |
|
120 | + if ($bytes < 1024) { |
|
121 | + return $bytes . "K"; |
|
122 | + } |
|
123 | + $bytes = round($bytes / 1024, 1); |
|
124 | + if ($bytes < 1024) { |
|
125 | + return $bytes . "M"; |
|
126 | + } |
|
127 | + $bytes = round($bytes / 1024, 1); |
|
128 | + return $bytes . "G"; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Make a computer file size |
|
133 | + * @param string $str file size in human readable format |
|
134 | + * @return float a file size in bytes |
|
135 | + * |
|
136 | + * Makes 2kB to 2048. |
|
137 | + * |
|
138 | + * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418 |
|
139 | + */ |
|
140 | + public static function computerFileSize($str) { |
|
141 | + $str = strtolower($str); |
|
142 | + if (is_numeric($str)) { |
|
143 | + return floatval($str); |
|
144 | + } |
|
145 | + |
|
146 | + $bytes_array = array( |
|
147 | + 'b' => 1, |
|
148 | + 'k' => 1024, |
|
149 | + 'kb' => 1024, |
|
150 | + 'mb' => 1024 * 1024, |
|
151 | + 'm' => 1024 * 1024, |
|
152 | + 'gb' => 1024 * 1024 * 1024, |
|
153 | + 'g' => 1024 * 1024 * 1024, |
|
154 | + 'tb' => 1024 * 1024 * 1024 * 1024, |
|
155 | + 't' => 1024 * 1024 * 1024 * 1024, |
|
156 | + 'pb' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
157 | + 'p' => 1024 * 1024 * 1024 * 1024 * 1024, |
|
158 | + ); |
|
159 | + |
|
160 | + $bytes = floatval($str); |
|
161 | + |
|
162 | + if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) { |
|
163 | + $bytes *= $bytes_array[$matches[1]]; |
|
164 | + } else { |
|
165 | + return false; |
|
166 | + } |
|
167 | + |
|
168 | + $bytes = round($bytes); |
|
169 | + |
|
170 | + return $bytes; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Recursive copying of folders |
|
175 | + * @param string $src source folder |
|
176 | + * @param string $dest target folder |
|
177 | + * |
|
178 | + */ |
|
179 | + static function copyr($src, $dest) { |
|
180 | + if (is_dir($src)) { |
|
181 | + if (!is_dir($dest)) { |
|
182 | + mkdir($dest); |
|
183 | + } |
|
184 | + $files = scandir($src); |
|
185 | + foreach ($files as $file) { |
|
186 | + if ($file != "." && $file != "..") { |
|
187 | + self::copyr("$src/$file", "$dest/$file"); |
|
188 | + } |
|
189 | + } |
|
190 | + } elseif (file_exists($src) && !\OC\Files\Filesystem::isFileBlacklisted($src)) { |
|
191 | + copy($src, $dest); |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Recursive deletion of folders |
|
197 | + * @param string $dir path to the folder |
|
198 | + * @param bool $deleteSelf if set to false only the content of the folder will be deleted |
|
199 | + * @return bool |
|
200 | + */ |
|
201 | + static function rmdirr($dir, $deleteSelf = true) { |
|
202 | + if (is_dir($dir)) { |
|
203 | + $files = new RecursiveIteratorIterator( |
|
204 | + new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), |
|
205 | + RecursiveIteratorIterator::CHILD_FIRST |
|
206 | + ); |
|
207 | + |
|
208 | + foreach ($files as $fileInfo) { |
|
209 | + /** @var SplFileInfo $fileInfo */ |
|
210 | + if ($fileInfo->isLink()) { |
|
211 | + unlink($fileInfo->getPathname()); |
|
212 | + } else if ($fileInfo->isDir()) { |
|
213 | + rmdir($fileInfo->getRealPath()); |
|
214 | + } else { |
|
215 | + unlink($fileInfo->getRealPath()); |
|
216 | + } |
|
217 | + } |
|
218 | + if ($deleteSelf) { |
|
219 | + rmdir($dir); |
|
220 | + } |
|
221 | + } elseif (file_exists($dir)) { |
|
222 | + if ($deleteSelf) { |
|
223 | + unlink($dir); |
|
224 | + } |
|
225 | + } |
|
226 | + if (!$deleteSelf) { |
|
227 | + return true; |
|
228 | + } |
|
229 | + |
|
230 | + return !file_exists($dir); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * @return \OC\Files\Type\TemplateManager |
|
235 | + */ |
|
236 | + static public function getFileTemplateManager() { |
|
237 | + if (!self::$templateManager) { |
|
238 | + self::$templateManager = new \OC\Files\Type\TemplateManager(); |
|
239 | + } |
|
240 | + return self::$templateManager; |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * detect if a given program is found in the search PATH |
|
245 | + * |
|
246 | + * @param string $name |
|
247 | + * @param bool $path |
|
248 | + * @internal param string $program name |
|
249 | + * @internal param string $optional search path, defaults to $PATH |
|
250 | + * @return bool true if executable program found in path |
|
251 | + */ |
|
252 | + public static function canExecute($name, $path = false) { |
|
253 | + // path defaults to PATH from environment if not set |
|
254 | + if ($path === false) { |
|
255 | + $path = getenv("PATH"); |
|
256 | + } |
|
257 | + // we look for an executable file of that name |
|
258 | + $exts = [""]; |
|
259 | + $check_fn = "is_executable"; |
|
260 | + // Default check will be done with $path directories : |
|
261 | + $dirs = explode(PATH_SEPARATOR, $path); |
|
262 | + // WARNING : We have to check if open_basedir is enabled : |
|
263 | + $obd = OC::$server->getIniWrapper()->getString('open_basedir'); |
|
264 | + if ($obd != "none") { |
|
265 | + $obd_values = explode(PATH_SEPARATOR, $obd); |
|
266 | + if (count($obd_values) > 0 and $obd_values[0]) { |
|
267 | + // open_basedir is in effect ! |
|
268 | + // We need to check if the program is in one of these dirs : |
|
269 | + $dirs = $obd_values; |
|
270 | + } |
|
271 | + } |
|
272 | + foreach ($dirs as $dir) { |
|
273 | + foreach ($exts as $ext) { |
|
274 | + if ($check_fn("$dir/$name" . $ext)) |
|
275 | + return true; |
|
276 | + } |
|
277 | + } |
|
278 | + return false; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * copy the contents of one stream to another |
|
283 | + * |
|
284 | + * @param resource $source |
|
285 | + * @param resource $target |
|
286 | + * @return array the number of bytes copied and result |
|
287 | + */ |
|
288 | + public static function streamCopy($source, $target) { |
|
289 | + if (!$source or !$target) { |
|
290 | + return array(0, false); |
|
291 | + } |
|
292 | + $bufSize = 8192; |
|
293 | + $result = true; |
|
294 | + $count = 0; |
|
295 | + while (!feof($source)) { |
|
296 | + $buf = fread($source, $bufSize); |
|
297 | + $bytesWritten = fwrite($target, $buf); |
|
298 | + if ($bytesWritten !== false) { |
|
299 | + $count += $bytesWritten; |
|
300 | + } |
|
301 | + // note: strlen is expensive so only use it when necessary, |
|
302 | + // on the last block |
|
303 | + if ($bytesWritten === false |
|
304 | + || ($bytesWritten < $bufSize && $bytesWritten < strlen($buf)) |
|
305 | + ) { |
|
306 | + // write error, could be disk full ? |
|
307 | + $result = false; |
|
308 | + break; |
|
309 | + } |
|
310 | + } |
|
311 | + return array($count, $result); |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Adds a suffix to the name in case the file exists |
|
316 | + * |
|
317 | + * @param string $path |
|
318 | + * @param string $filename |
|
319 | + * @return string |
|
320 | + */ |
|
321 | + public static function buildNotExistingFileName($path, $filename) { |
|
322 | + $view = \OC\Files\Filesystem::getView(); |
|
323 | + return self::buildNotExistingFileNameForView($path, $filename, $view); |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Adds a suffix to the name in case the file exists |
|
328 | + * |
|
329 | + * @param string $path |
|
330 | + * @param string $filename |
|
331 | + * @return string |
|
332 | + */ |
|
333 | + public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { |
|
334 | + if ($path === '/') { |
|
335 | + $path = ''; |
|
336 | + } |
|
337 | + if ($pos = strrpos($filename, '.')) { |
|
338 | + $name = substr($filename, 0, $pos); |
|
339 | + $ext = substr($filename, $pos); |
|
340 | + } else { |
|
341 | + $name = $filename; |
|
342 | + $ext = ''; |
|
343 | + } |
|
344 | + |
|
345 | + $newpath = $path . '/' . $filename; |
|
346 | + if ($view->file_exists($newpath)) { |
|
347 | + if (preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { |
|
348 | + //Replace the last "(number)" with "(number+1)" |
|
349 | + $last_match = count($matches[0]) - 1; |
|
350 | + $counter = $matches[1][$last_match][0] + 1; |
|
351 | + $offset = $matches[0][$last_match][1]; |
|
352 | + $match_length = strlen($matches[0][$last_match][0]); |
|
353 | + } else { |
|
354 | + $counter = 2; |
|
355 | + $match_length = 0; |
|
356 | + $offset = false; |
|
357 | + } |
|
358 | + do { |
|
359 | + if ($offset) { |
|
360 | + //Replace the last "(number)" with "(number+1)" |
|
361 | + $newname = substr_replace($name, '(' . $counter . ')', $offset, $match_length); |
|
362 | + } else { |
|
363 | + $newname = $name . ' (' . $counter . ')'; |
|
364 | + } |
|
365 | + $newpath = $path . '/' . $newname . $ext; |
|
366 | + $counter++; |
|
367 | + } while ($view->file_exists($newpath)); |
|
368 | + } |
|
369 | + |
|
370 | + return $newpath; |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
375 | + * |
|
376 | + * @param array $input The array to work on |
|
377 | + * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default) |
|
378 | + * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8 |
|
379 | + * @return array |
|
380 | + * |
|
381 | + * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is. |
|
382 | + * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715 |
|
383 | + * |
|
384 | + */ |
|
385 | + public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') { |
|
386 | + $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER; |
|
387 | + $ret = array(); |
|
388 | + foreach ($input as $k => $v) { |
|
389 | + $ret[mb_convert_case($k, $case, $encoding)] = $v; |
|
390 | + } |
|
391 | + return $ret; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * performs a search in a nested array |
|
396 | + * @param array $haystack the array to be searched |
|
397 | + * @param string $needle the search string |
|
398 | + * @param string $index optional, only search this key name |
|
399 | + * @return mixed the key of the matching field, otherwise false |
|
400 | + * |
|
401 | + * performs a search in a nested array |
|
402 | + * |
|
403 | + * taken from http://www.php.net/manual/en/function.array-search.php#97645 |
|
404 | + */ |
|
405 | + public static function recursiveArraySearch($haystack, $needle, $index = null) { |
|
406 | + $aIt = new RecursiveArrayIterator($haystack); |
|
407 | + $it = new RecursiveIteratorIterator($aIt); |
|
408 | + |
|
409 | + while ($it->valid()) { |
|
410 | + if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) { |
|
411 | + return $aIt->key(); |
|
412 | + } |
|
413 | + |
|
414 | + $it->next(); |
|
415 | + } |
|
416 | + |
|
417 | + return false; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * calculates the maximum upload size respecting system settings, free space and user quota |
|
422 | + * |
|
423 | + * @param string $dir the current folder where the user currently operates |
|
424 | + * @param int $freeSpace the number of bytes free on the storage holding $dir, if not set this will be received from the storage directly |
|
425 | + * @return int number of bytes representing |
|
426 | + */ |
|
427 | + public static function maxUploadFilesize($dir, $freeSpace = null) { |
|
428 | + if (is_null($freeSpace) || $freeSpace < 0){ |
|
429 | + $freeSpace = self::freeSpace($dir); |
|
430 | + } |
|
431 | + return min($freeSpace, self::uploadLimit()); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Calculate free space left within user quota |
|
436 | + * |
|
437 | + * @param string $dir the current folder where the user currently operates |
|
438 | + * @return int number of bytes representing |
|
439 | + */ |
|
440 | + public static function freeSpace($dir) { |
|
441 | + $freeSpace = \OC\Files\Filesystem::free_space($dir); |
|
442 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
443 | + $freeSpace = max($freeSpace, 0); |
|
444 | + return $freeSpace; |
|
445 | + } else { |
|
446 | + return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
447 | + } |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Calculate PHP upload limit |
|
452 | + * |
|
453 | + * @return int PHP upload file size limit |
|
454 | + */ |
|
455 | + public static function uploadLimit() { |
|
456 | + $ini = \OC::$server->getIniWrapper(); |
|
457 | + $upload_max_filesize = OCP\Util::computerFileSize($ini->get('upload_max_filesize')); |
|
458 | + $post_max_size = OCP\Util::computerFileSize($ini->get('post_max_size')); |
|
459 | + if ((int)$upload_max_filesize === 0 and (int)$post_max_size === 0) { |
|
460 | + return INF; |
|
461 | + } elseif ((int)$upload_max_filesize === 0 or (int)$post_max_size === 0) { |
|
462 | + return max($upload_max_filesize, $post_max_size); //only the non 0 value counts |
|
463 | + } else { |
|
464 | + return min($upload_max_filesize, $post_max_size); |
|
465 | + } |
|
466 | + } |
|
467 | + |
|
468 | + /** |
|
469 | + * Checks if a function is available |
|
470 | + * |
|
471 | + * @param string $function_name |
|
472 | + * @return bool |
|
473 | + */ |
|
474 | + public static function is_function_enabled($function_name) { |
|
475 | + if (!function_exists($function_name)) { |
|
476 | + return false; |
|
477 | + } |
|
478 | + $ini = \OC::$server->getIniWrapper(); |
|
479 | + $disabled = explode(',', $ini->get('disable_functions')); |
|
480 | + $disabled = array_map('trim', $disabled); |
|
481 | + if (in_array($function_name, $disabled)) { |
|
482 | + return false; |
|
483 | + } |
|
484 | + $disabled = explode(',', $ini->get('suhosin.executor.func.blacklist')); |
|
485 | + $disabled = array_map('trim', $disabled); |
|
486 | + if (in_array($function_name, $disabled)) { |
|
487 | + return false; |
|
488 | + } |
|
489 | + return true; |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Try to find a program |
|
494 | + * |
|
495 | + * @param string $program |
|
496 | + * @return null|string |
|
497 | + */ |
|
498 | + public static function findBinaryPath($program) { |
|
499 | + $memcache = \OC::$server->getMemCacheFactory()->create('findBinaryPath'); |
|
500 | + if ($memcache->hasKey($program)) { |
|
501 | + return $memcache->get($program); |
|
502 | + } |
|
503 | + $result = null; |
|
504 | + if (self::is_function_enabled('exec')) { |
|
505 | + $exeSniffer = new ExecutableFinder(); |
|
506 | + // Returns null if nothing is found |
|
507 | + $result = $exeSniffer->find($program); |
|
508 | + if (empty($result)) { |
|
509 | + $paths = getenv('PATH'); |
|
510 | + if (empty($paths)) { |
|
511 | + $paths = '/usr/local/bin /usr/bin /opt/bin /bin'; |
|
512 | + } else { |
|
513 | + $paths = str_replace(':',' ',getenv('PATH')); |
|
514 | + } |
|
515 | + $command = 'find ' . $paths . ' -name ' . escapeshellarg($program) . ' 2> /dev/null'; |
|
516 | + exec($command, $output, $returnCode); |
|
517 | + if (count($output) > 0) { |
|
518 | + $result = escapeshellcmd($output[0]); |
|
519 | + } |
|
520 | + } |
|
521 | + } |
|
522 | + // store the value for 5 minutes |
|
523 | + $memcache->set($program, $result, 300); |
|
524 | + return $result; |
|
525 | + } |
|
526 | + |
|
527 | + /** |
|
528 | + * Calculate the disc space for the given path |
|
529 | + * |
|
530 | + * @param string $path |
|
531 | + * @param \OCP\Files\FileInfo $rootInfo (optional) |
|
532 | + * @return array |
|
533 | + * @throws \OCP\Files\NotFoundException |
|
534 | + */ |
|
535 | + public static function getStorageInfo($path, $rootInfo = null) { |
|
536 | + // return storage info without adding mount points |
|
537 | + $includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false); |
|
538 | + |
|
539 | + if (!$rootInfo) { |
|
540 | + $rootInfo = \OC\Files\Filesystem::getFileInfo($path, false); |
|
541 | + } |
|
542 | + if (!$rootInfo instanceof \OCP\Files\FileInfo) { |
|
543 | + throw new \OCP\Files\NotFoundException(); |
|
544 | + } |
|
545 | + $used = $rootInfo->getSize(); |
|
546 | + if ($used < 0) { |
|
547 | + $used = 0; |
|
548 | + } |
|
549 | + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
550 | + $storage = $rootInfo->getStorage(); |
|
551 | + $sourceStorage = $storage; |
|
552 | + if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
|
553 | + $includeExtStorage = false; |
|
554 | + $sourceStorage = $storage->getSourceStorage(); |
|
555 | + } |
|
556 | + if ($includeExtStorage) { |
|
557 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
558 | + || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') |
|
559 | + ) { |
|
560 | + /** @var \OC\Files\Storage\Home $storage */ |
|
561 | + $user = $storage->getUser(); |
|
562 | + } else { |
|
563 | + $user = \OC::$server->getUserSession()->getUser()->getUID(); |
|
564 | + } |
|
565 | + if ($user) { |
|
566 | + $quota = OC_Util::getUserQuota($user); |
|
567 | + } else { |
|
568 | + $quota = \OCP\Files\FileInfo::SPACE_UNLIMITED; |
|
569 | + } |
|
570 | + if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
571 | + // always get free space / total space from root + mount points |
|
572 | + return self::getGlobalStorageInfo(); |
|
573 | + } |
|
574 | + } |
|
575 | + |
|
576 | + // TODO: need a better way to get total space from storage |
|
577 | + if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) { |
|
578 | + /** @var \OC\Files\Storage\Wrapper\Quota $storage */ |
|
579 | + $quota = $sourceStorage->getQuota(); |
|
580 | + } |
|
581 | + $free = $sourceStorage->free_space($rootInfo->getInternalPath()); |
|
582 | + if ($free >= 0) { |
|
583 | + $total = $free + $used; |
|
584 | + } else { |
|
585 | + $total = $free; //either unknown or unlimited |
|
586 | + } |
|
587 | + if ($total > 0) { |
|
588 | + if ($quota > 0 && $total > $quota) { |
|
589 | + $total = $quota; |
|
590 | + } |
|
591 | + // prevent division by zero or error codes (negative values) |
|
592 | + $relative = round(($used / $total) * 10000) / 100; |
|
593 | + } else { |
|
594 | + $relative = 0; |
|
595 | + } |
|
596 | + |
|
597 | + $ownerId = $storage->getOwner($path); |
|
598 | + $ownerDisplayName = ''; |
|
599 | + $owner = \OC::$server->getUserManager()->get($ownerId); |
|
600 | + if($owner) { |
|
601 | + $ownerDisplayName = $owner->getDisplayName(); |
|
602 | + } |
|
603 | + |
|
604 | + return [ |
|
605 | + 'free' => $free, |
|
606 | + 'used' => $used, |
|
607 | + 'quota' => $quota, |
|
608 | + 'total' => $total, |
|
609 | + 'relative' => $relative, |
|
610 | + 'owner' => $ownerId, |
|
611 | + 'ownerDisplayName' => $ownerDisplayName, |
|
612 | + ]; |
|
613 | + } |
|
614 | + |
|
615 | + /** |
|
616 | + * Get storage info including all mount points and quota |
|
617 | + * |
|
618 | + * @return array |
|
619 | + */ |
|
620 | + private static function getGlobalStorageInfo() { |
|
621 | + $quota = OC_Util::getUserQuota(\OCP\User::getUser()); |
|
622 | + |
|
623 | + $rootInfo = \OC\Files\Filesystem::getFileInfo('', 'ext'); |
|
624 | + $used = $rootInfo['size']; |
|
625 | + if ($used < 0) { |
|
626 | + $used = 0; |
|
627 | + } |
|
628 | + |
|
629 | + $total = $quota; |
|
630 | + $free = $quota - $used; |
|
631 | + |
|
632 | + if ($total > 0) { |
|
633 | + if ($quota > 0 && $total > $quota) { |
|
634 | + $total = $quota; |
|
635 | + } |
|
636 | + // prevent division by zero or error codes (negative values) |
|
637 | + $relative = round(($used / $total) * 10000) / 100; |
|
638 | + } else { |
|
639 | + $relative = 0; |
|
640 | + } |
|
641 | + |
|
642 | + return array('free' => $free, 'used' => $used, 'total' => $total, 'relative' => $relative); |
|
643 | + |
|
644 | + } |
|
645 | + |
|
646 | + /** |
|
647 | + * Returns whether the config file is set manually to read-only |
|
648 | + * @return bool |
|
649 | + */ |
|
650 | + public static function isReadOnlyConfigEnabled() { |
|
651 | + return \OC::$server->getConfig()->getSystemValue('config_is_read_only', false); |
|
652 | + } |
|
653 | 653 | } |
@@ -32,26 +32,26 @@ |
||
32 | 32 | * @package OC\BackgroundJob |
33 | 33 | */ |
34 | 34 | abstract class TimedJob extends Job { |
35 | - protected $interval = 0; |
|
35 | + protected $interval = 0; |
|
36 | 36 | |
37 | - /** |
|
38 | - * set the interval for the job |
|
39 | - * |
|
40 | - * @param int $interval |
|
41 | - */ |
|
42 | - public function setInterval($interval) { |
|
43 | - $this->interval = $interval; |
|
44 | - } |
|
37 | + /** |
|
38 | + * set the interval for the job |
|
39 | + * |
|
40 | + * @param int $interval |
|
41 | + */ |
|
42 | + public function setInterval($interval) { |
|
43 | + $this->interval = $interval; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * run the job if |
|
48 | - * |
|
49 | - * @param JobList $jobList |
|
50 | - * @param ILogger $logger |
|
51 | - */ |
|
52 | - public function execute($jobList, ILogger $logger = null) { |
|
53 | - if ((time() - $this->lastRun) > $this->interval) { |
|
54 | - parent::execute($jobList, $logger); |
|
55 | - } |
|
56 | - } |
|
46 | + /** |
|
47 | + * run the job if |
|
48 | + * |
|
49 | + * @param JobList $jobList |
|
50 | + * @param ILogger $logger |
|
51 | + */ |
|
52 | + public function execute($jobList, ILogger $logger = null) { |
|
53 | + if ((time() - $this->lastRun) > $this->interval) { |
|
54 | + parent::execute($jobList, $logger); |
|
55 | + } |
|
56 | + } |
|
57 | 57 | } |
@@ -32,14 +32,14 @@ |
||
32 | 32 | * @package OC\BackgroundJob |
33 | 33 | */ |
34 | 34 | abstract class QueuedJob extends Job { |
35 | - /** |
|
36 | - * run the job, then remove it from the joblist |
|
37 | - * |
|
38 | - * @param JobList $jobList |
|
39 | - * @param ILogger $logger |
|
40 | - */ |
|
41 | - public function execute($jobList, ILogger $logger = null) { |
|
42 | - $jobList->remove($this, $this->argument); |
|
43 | - parent::execute($jobList, $logger); |
|
44 | - } |
|
35 | + /** |
|
36 | + * run the job, then remove it from the joblist |
|
37 | + * |
|
38 | + * @param JobList $jobList |
|
39 | + * @param ILogger $logger |
|
40 | + */ |
|
41 | + public function execute($jobList, ILogger $logger = null) { |
|
42 | + $jobList->remove($this, $this->argument); |
|
43 | + parent::execute($jobList, $logger); |
|
44 | + } |
|
45 | 45 | } |