@@ -11,32 +11,32 @@ |
||
11 | 11 | */ |
12 | 12 | class Version13000Date20170926101637 extends BigIntMigration { |
13 | 13 | |
14 | - /** |
|
15 | - * @return array Returns an array with the following structure |
|
16 | - * ['table1' => ['column1', 'column2'], ...] |
|
17 | - * @since 13.0.0 |
|
18 | - */ |
|
19 | - protected function getColumnsByTable() { |
|
20 | - return [ |
|
21 | - 'admin_settings' => ['id'], |
|
22 | - 'authtoken' => ['id'], |
|
23 | - 'bruteforce_attempts' => ['id'], |
|
24 | - 'comments' => ['id', 'parent_id', 'topmost_parent_id'], |
|
25 | - // Disabled for now 'filecache' => ['fileid', 'storage', 'parent', 'mimetype', 'mimepart'], |
|
26 | - 'file_locks' => ['id'], |
|
27 | - 'jobs' => ['id'], |
|
28 | - // Disabled for now 'mimetypes' => ['id'], |
|
29 | - 'mounts' => ['id'], |
|
30 | - 'personal_settings' => ['id'], |
|
31 | - 'properties' => ['id'], |
|
32 | - 'share' => ['id', 'parent', 'file_source'], |
|
33 | - // Disabled for now 'storages' => ['numeric_id'], |
|
34 | - 'systemtag' => ['id'], |
|
35 | - 'systemtag_group' => ['systemtagid'], |
|
36 | - 'systemtag_object_mapping' => ['systemtagid'], |
|
37 | - 'vcategory' => ['id'], |
|
38 | - 'vcategory_to_object' => ['objid', 'categoryid'], |
|
39 | - ]; |
|
40 | - } |
|
14 | + /** |
|
15 | + * @return array Returns an array with the following structure |
|
16 | + * ['table1' => ['column1', 'column2'], ...] |
|
17 | + * @since 13.0.0 |
|
18 | + */ |
|
19 | + protected function getColumnsByTable() { |
|
20 | + return [ |
|
21 | + 'admin_settings' => ['id'], |
|
22 | + 'authtoken' => ['id'], |
|
23 | + 'bruteforce_attempts' => ['id'], |
|
24 | + 'comments' => ['id', 'parent_id', 'topmost_parent_id'], |
|
25 | + // Disabled for now 'filecache' => ['fileid', 'storage', 'parent', 'mimetype', 'mimepart'], |
|
26 | + 'file_locks' => ['id'], |
|
27 | + 'jobs' => ['id'], |
|
28 | + // Disabled for now 'mimetypes' => ['id'], |
|
29 | + 'mounts' => ['id'], |
|
30 | + 'personal_settings' => ['id'], |
|
31 | + 'properties' => ['id'], |
|
32 | + 'share' => ['id', 'parent', 'file_source'], |
|
33 | + // Disabled for now 'storages' => ['numeric_id'], |
|
34 | + 'systemtag' => ['id'], |
|
35 | + 'systemtag_group' => ['systemtagid'], |
|
36 | + 'systemtag_object_mapping' => ['systemtagid'], |
|
37 | + 'vcategory' => ['id'], |
|
38 | + 'vcategory_to_object' => ['objid', 'categoryid'], |
|
39 | + ]; |
|
40 | + } |
|
41 | 41 | |
42 | 42 | } |
@@ -25,294 +25,294 @@ |
||
25 | 25 | namespace OC; |
26 | 26 | |
27 | 27 | class DateTimeFormatter implements \OCP\IDateTimeFormatter { |
28 | - /** @var \DateTimeZone */ |
|
29 | - protected $defaultTimeZone; |
|
28 | + /** @var \DateTimeZone */ |
|
29 | + protected $defaultTimeZone; |
|
30 | 30 | |
31 | - /** @var \OCP\IL10N */ |
|
32 | - protected $defaultL10N; |
|
31 | + /** @var \OCP\IL10N */ |
|
32 | + protected $defaultL10N; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Constructor |
|
36 | - * |
|
37 | - * @param \DateTimeZone $defaultTimeZone Set the timezone for the format |
|
38 | - * @param \OCP\IL10N $defaultL10N Set the language for the format |
|
39 | - */ |
|
40 | - public function __construct(\DateTimeZone $defaultTimeZone, \OCP\IL10N $defaultL10N) { |
|
41 | - $this->defaultTimeZone = $defaultTimeZone; |
|
42 | - $this->defaultL10N = $defaultL10N; |
|
43 | - } |
|
34 | + /** |
|
35 | + * Constructor |
|
36 | + * |
|
37 | + * @param \DateTimeZone $defaultTimeZone Set the timezone for the format |
|
38 | + * @param \OCP\IL10N $defaultL10N Set the language for the format |
|
39 | + */ |
|
40 | + public function __construct(\DateTimeZone $defaultTimeZone, \OCP\IL10N $defaultL10N) { |
|
41 | + $this->defaultTimeZone = $defaultTimeZone; |
|
42 | + $this->defaultL10N = $defaultL10N; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Get TimeZone to use |
|
47 | - * |
|
48 | - * @param \DateTimeZone $timeZone The timezone to use |
|
49 | - * @return \DateTimeZone The timezone to use, falling back to the current user's timezone |
|
50 | - */ |
|
51 | - protected function getTimeZone($timeZone = null) { |
|
52 | - if ($timeZone === null) { |
|
53 | - $timeZone = $this->defaultTimeZone; |
|
54 | - } |
|
45 | + /** |
|
46 | + * Get TimeZone to use |
|
47 | + * |
|
48 | + * @param \DateTimeZone $timeZone The timezone to use |
|
49 | + * @return \DateTimeZone The timezone to use, falling back to the current user's timezone |
|
50 | + */ |
|
51 | + protected function getTimeZone($timeZone = null) { |
|
52 | + if ($timeZone === null) { |
|
53 | + $timeZone = $this->defaultTimeZone; |
|
54 | + } |
|
55 | 55 | |
56 | - return $timeZone; |
|
57 | - } |
|
56 | + return $timeZone; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Get \OCP\IL10N to use |
|
61 | - * |
|
62 | - * @param \OCP\IL10N $l The locale to use |
|
63 | - * @return \OCP\IL10N The locale to use, falling back to the current user's locale |
|
64 | - */ |
|
65 | - protected function getLocale($l = null) { |
|
66 | - if ($l === null) { |
|
67 | - $l = $this->defaultL10N; |
|
68 | - } |
|
59 | + /** |
|
60 | + * Get \OCP\IL10N to use |
|
61 | + * |
|
62 | + * @param \OCP\IL10N $l The locale to use |
|
63 | + * @return \OCP\IL10N The locale to use, falling back to the current user's locale |
|
64 | + */ |
|
65 | + protected function getLocale($l = null) { |
|
66 | + if ($l === null) { |
|
67 | + $l = $this->defaultL10N; |
|
68 | + } |
|
69 | 69 | |
70 | - return $l; |
|
71 | - } |
|
70 | + return $l; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Generates a DateTime object with the given timestamp and TimeZone |
|
75 | - * |
|
76 | - * @param mixed $timestamp |
|
77 | - * @param \DateTimeZone $timeZone The timezone to use |
|
78 | - * @return \DateTime |
|
79 | - */ |
|
80 | - protected function getDateTime($timestamp, \DateTimeZone $timeZone = null) { |
|
81 | - if ($timestamp === null) { |
|
82 | - return new \DateTime('now', $timeZone); |
|
83 | - } else if (!$timestamp instanceof \DateTime) { |
|
84 | - $dateTime = new \DateTime('now', $timeZone); |
|
85 | - $dateTime->setTimestamp($timestamp); |
|
86 | - return $dateTime; |
|
87 | - } |
|
88 | - if ($timeZone) { |
|
89 | - $timestamp->setTimezone($timeZone); |
|
90 | - } |
|
91 | - return $timestamp; |
|
92 | - } |
|
73 | + /** |
|
74 | + * Generates a DateTime object with the given timestamp and TimeZone |
|
75 | + * |
|
76 | + * @param mixed $timestamp |
|
77 | + * @param \DateTimeZone $timeZone The timezone to use |
|
78 | + * @return \DateTime |
|
79 | + */ |
|
80 | + protected function getDateTime($timestamp, \DateTimeZone $timeZone = null) { |
|
81 | + if ($timestamp === null) { |
|
82 | + return new \DateTime('now', $timeZone); |
|
83 | + } else if (!$timestamp instanceof \DateTime) { |
|
84 | + $dateTime = new \DateTime('now', $timeZone); |
|
85 | + $dateTime->setTimestamp($timestamp); |
|
86 | + return $dateTime; |
|
87 | + } |
|
88 | + if ($timeZone) { |
|
89 | + $timestamp->setTimezone($timeZone); |
|
90 | + } |
|
91 | + return $timestamp; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Formats the date of the given timestamp |
|
96 | - * |
|
97 | - * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
98 | - * @param string $format Either 'full', 'long', 'medium' or 'short' |
|
99 | - * full: e.g. 'EEEE, MMMM d, y' => 'Wednesday, August 20, 2014' |
|
100 | - * long: e.g. 'MMMM d, y' => 'August 20, 2014' |
|
101 | - * medium: e.g. 'MMM d, y' => 'Aug 20, 2014' |
|
102 | - * short: e.g. 'M/d/yy' => '8/20/14' |
|
103 | - * The exact format is dependent on the language |
|
104 | - * @param \DateTimeZone $timeZone The timezone to use |
|
105 | - * @param \OCP\IL10N $l The locale to use |
|
106 | - * @return string Formatted date string |
|
107 | - */ |
|
108 | - public function formatDate($timestamp, $format = 'long', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
109 | - return $this->format($timestamp, 'date', $format, $timeZone, $l); |
|
110 | - } |
|
94 | + /** |
|
95 | + * Formats the date of the given timestamp |
|
96 | + * |
|
97 | + * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
98 | + * @param string $format Either 'full', 'long', 'medium' or 'short' |
|
99 | + * full: e.g. 'EEEE, MMMM d, y' => 'Wednesday, August 20, 2014' |
|
100 | + * long: e.g. 'MMMM d, y' => 'August 20, 2014' |
|
101 | + * medium: e.g. 'MMM d, y' => 'Aug 20, 2014' |
|
102 | + * short: e.g. 'M/d/yy' => '8/20/14' |
|
103 | + * The exact format is dependent on the language |
|
104 | + * @param \DateTimeZone $timeZone The timezone to use |
|
105 | + * @param \OCP\IL10N $l The locale to use |
|
106 | + * @return string Formatted date string |
|
107 | + */ |
|
108 | + public function formatDate($timestamp, $format = 'long', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
109 | + return $this->format($timestamp, 'date', $format, $timeZone, $l); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * Formats the date of the given timestamp |
|
114 | - * |
|
115 | - * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
116 | - * @param string $format Either 'full', 'long', 'medium' or 'short' |
|
117 | - * full: e.g. 'EEEE, MMMM d, y' => 'Wednesday, August 20, 2014' |
|
118 | - * long: e.g. 'MMMM d, y' => 'August 20, 2014' |
|
119 | - * medium: e.g. 'MMM d, y' => 'Aug 20, 2014' |
|
120 | - * short: e.g. 'M/d/yy' => '8/20/14' |
|
121 | - * The exact format is dependent on the language |
|
122 | - * Uses 'Today', 'Yesterday' and 'Tomorrow' when applicable |
|
123 | - * @param \DateTimeZone $timeZone The timezone to use |
|
124 | - * @param \OCP\IL10N $l The locale to use |
|
125 | - * @return string Formatted relative date string |
|
126 | - */ |
|
127 | - public function formatDateRelativeDay($timestamp, $format = 'long', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
128 | - if (substr($format, -1) !== '*' && substr($format, -1) !== '*') { |
|
129 | - $format .= '^'; |
|
130 | - } |
|
112 | + /** |
|
113 | + * Formats the date of the given timestamp |
|
114 | + * |
|
115 | + * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
116 | + * @param string $format Either 'full', 'long', 'medium' or 'short' |
|
117 | + * full: e.g. 'EEEE, MMMM d, y' => 'Wednesday, August 20, 2014' |
|
118 | + * long: e.g. 'MMMM d, y' => 'August 20, 2014' |
|
119 | + * medium: e.g. 'MMM d, y' => 'Aug 20, 2014' |
|
120 | + * short: e.g. 'M/d/yy' => '8/20/14' |
|
121 | + * The exact format is dependent on the language |
|
122 | + * Uses 'Today', 'Yesterday' and 'Tomorrow' when applicable |
|
123 | + * @param \DateTimeZone $timeZone The timezone to use |
|
124 | + * @param \OCP\IL10N $l The locale to use |
|
125 | + * @return string Formatted relative date string |
|
126 | + */ |
|
127 | + public function formatDateRelativeDay($timestamp, $format = 'long', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
128 | + if (substr($format, -1) !== '*' && substr($format, -1) !== '*') { |
|
129 | + $format .= '^'; |
|
130 | + } |
|
131 | 131 | |
132 | - return $this->format($timestamp, 'date', $format, $timeZone, $l); |
|
133 | - } |
|
132 | + return $this->format($timestamp, 'date', $format, $timeZone, $l); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Gives the relative date of the timestamp |
|
137 | - * Only works for past dates |
|
138 | - * |
|
139 | - * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
140 | - * @param int|\DateTime $baseTimestamp Timestamp to compare $timestamp against, defaults to current time |
|
141 | - * @return string Dates returned are: |
|
142 | - * < 1 month => Today, Yesterday, n days ago |
|
143 | - * < 13 month => last month, n months ago |
|
144 | - * >= 13 month => last year, n years ago |
|
145 | - * @param \OCP\IL10N $l The locale to use |
|
146 | - * @return string Formatted date span |
|
147 | - */ |
|
148 | - public function formatDateSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l = null) { |
|
149 | - $l = $this->getLocale($l); |
|
150 | - $timestamp = $this->getDateTime($timestamp); |
|
151 | - $timestamp->setTime(0, 0, 0); |
|
135 | + /** |
|
136 | + * Gives the relative date of the timestamp |
|
137 | + * Only works for past dates |
|
138 | + * |
|
139 | + * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
140 | + * @param int|\DateTime $baseTimestamp Timestamp to compare $timestamp against, defaults to current time |
|
141 | + * @return string Dates returned are: |
|
142 | + * < 1 month => Today, Yesterday, n days ago |
|
143 | + * < 13 month => last month, n months ago |
|
144 | + * >= 13 month => last year, n years ago |
|
145 | + * @param \OCP\IL10N $l The locale to use |
|
146 | + * @return string Formatted date span |
|
147 | + */ |
|
148 | + public function formatDateSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l = null) { |
|
149 | + $l = $this->getLocale($l); |
|
150 | + $timestamp = $this->getDateTime($timestamp); |
|
151 | + $timestamp->setTime(0, 0, 0); |
|
152 | 152 | |
153 | - if ($baseTimestamp === null) { |
|
154 | - $baseTimestamp = time(); |
|
155 | - } |
|
156 | - $baseTimestamp = $this->getDateTime($baseTimestamp); |
|
157 | - $baseTimestamp->setTime(0, 0, 0); |
|
158 | - $dateInterval = $timestamp->diff($baseTimestamp); |
|
153 | + if ($baseTimestamp === null) { |
|
154 | + $baseTimestamp = time(); |
|
155 | + } |
|
156 | + $baseTimestamp = $this->getDateTime($baseTimestamp); |
|
157 | + $baseTimestamp->setTime(0, 0, 0); |
|
158 | + $dateInterval = $timestamp->diff($baseTimestamp); |
|
159 | 159 | |
160 | - if ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 0) { |
|
161 | - return $l->t('today'); |
|
162 | - } else if ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 1) { |
|
163 | - if ($timestamp > $baseTimestamp) { |
|
164 | - return $l->t('tomorrow'); |
|
165 | - } else { |
|
166 | - return $l->t('yesterday'); |
|
167 | - } |
|
168 | - } else if ($dateInterval->y == 0 && $dateInterval->m == 0) { |
|
169 | - if ($timestamp > $baseTimestamp) { |
|
170 | - return $l->n('in %n day', 'in %n days', $dateInterval->d); |
|
171 | - } else { |
|
172 | - return $l->n('%n day ago', '%n days ago', $dateInterval->d); |
|
173 | - } |
|
174 | - } else if ($dateInterval->y == 0 && $dateInterval->m == 1) { |
|
175 | - if ($timestamp > $baseTimestamp) { |
|
176 | - return $l->t('next month'); |
|
177 | - } else { |
|
178 | - return $l->t('last month'); |
|
179 | - } |
|
180 | - } else if ($dateInterval->y == 0) { |
|
181 | - if ($timestamp > $baseTimestamp) { |
|
182 | - return $l->n('in %n month', 'in %n months', $dateInterval->m); |
|
183 | - } else { |
|
184 | - return $l->n('%n month ago', '%n months ago', $dateInterval->m); |
|
185 | - } |
|
186 | - } else if ($dateInterval->y == 1) { |
|
187 | - if ($timestamp > $baseTimestamp) { |
|
188 | - return $l->t('next year'); |
|
189 | - } else { |
|
190 | - return $l->t('last year'); |
|
191 | - } |
|
192 | - } |
|
193 | - if ($timestamp > $baseTimestamp) { |
|
194 | - return $l->n('in %n year', 'in %n years', $dateInterval->y); |
|
195 | - } else { |
|
196 | - return $l->n('%n year ago', '%n years ago', $dateInterval->y); |
|
197 | - } |
|
198 | - } |
|
160 | + if ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 0) { |
|
161 | + return $l->t('today'); |
|
162 | + } else if ($dateInterval->y == 0 && $dateInterval->m == 0 && $dateInterval->d == 1) { |
|
163 | + if ($timestamp > $baseTimestamp) { |
|
164 | + return $l->t('tomorrow'); |
|
165 | + } else { |
|
166 | + return $l->t('yesterday'); |
|
167 | + } |
|
168 | + } else if ($dateInterval->y == 0 && $dateInterval->m == 0) { |
|
169 | + if ($timestamp > $baseTimestamp) { |
|
170 | + return $l->n('in %n day', 'in %n days', $dateInterval->d); |
|
171 | + } else { |
|
172 | + return $l->n('%n day ago', '%n days ago', $dateInterval->d); |
|
173 | + } |
|
174 | + } else if ($dateInterval->y == 0 && $dateInterval->m == 1) { |
|
175 | + if ($timestamp > $baseTimestamp) { |
|
176 | + return $l->t('next month'); |
|
177 | + } else { |
|
178 | + return $l->t('last month'); |
|
179 | + } |
|
180 | + } else if ($dateInterval->y == 0) { |
|
181 | + if ($timestamp > $baseTimestamp) { |
|
182 | + return $l->n('in %n month', 'in %n months', $dateInterval->m); |
|
183 | + } else { |
|
184 | + return $l->n('%n month ago', '%n months ago', $dateInterval->m); |
|
185 | + } |
|
186 | + } else if ($dateInterval->y == 1) { |
|
187 | + if ($timestamp > $baseTimestamp) { |
|
188 | + return $l->t('next year'); |
|
189 | + } else { |
|
190 | + return $l->t('last year'); |
|
191 | + } |
|
192 | + } |
|
193 | + if ($timestamp > $baseTimestamp) { |
|
194 | + return $l->n('in %n year', 'in %n years', $dateInterval->y); |
|
195 | + } else { |
|
196 | + return $l->n('%n year ago', '%n years ago', $dateInterval->y); |
|
197 | + } |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Formats the time of the given timestamp |
|
202 | - * |
|
203 | - * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
204 | - * @param string $format Either 'full', 'long', 'medium' or 'short' |
|
205 | - * full: e.g. 'h:mm:ss a zzzz' => '11:42:13 AM GMT+0:00' |
|
206 | - * long: e.g. 'h:mm:ss a z' => '11:42:13 AM GMT' |
|
207 | - * medium: e.g. 'h:mm:ss a' => '11:42:13 AM' |
|
208 | - * short: e.g. 'h:mm a' => '11:42 AM' |
|
209 | - * The exact format is dependent on the language |
|
210 | - * @param \DateTimeZone $timeZone The timezone to use |
|
211 | - * @param \OCP\IL10N $l The locale to use |
|
212 | - * @return string Formatted time string |
|
213 | - */ |
|
214 | - public function formatTime($timestamp, $format = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
215 | - return $this->format($timestamp, 'time', $format, $timeZone, $l); |
|
216 | - } |
|
200 | + /** |
|
201 | + * Formats the time of the given timestamp |
|
202 | + * |
|
203 | + * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
204 | + * @param string $format Either 'full', 'long', 'medium' or 'short' |
|
205 | + * full: e.g. 'h:mm:ss a zzzz' => '11:42:13 AM GMT+0:00' |
|
206 | + * long: e.g. 'h:mm:ss a z' => '11:42:13 AM GMT' |
|
207 | + * medium: e.g. 'h:mm:ss a' => '11:42:13 AM' |
|
208 | + * short: e.g. 'h:mm a' => '11:42 AM' |
|
209 | + * The exact format is dependent on the language |
|
210 | + * @param \DateTimeZone $timeZone The timezone to use |
|
211 | + * @param \OCP\IL10N $l The locale to use |
|
212 | + * @return string Formatted time string |
|
213 | + */ |
|
214 | + public function formatTime($timestamp, $format = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
215 | + return $this->format($timestamp, 'time', $format, $timeZone, $l); |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * Gives the relative past time of the timestamp |
|
220 | - * |
|
221 | - * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
222 | - * @param int|\DateTime $baseTimestamp Timestamp to compare $timestamp against, defaults to current time |
|
223 | - * @return string Dates returned are: |
|
224 | - * < 60 sec => seconds ago |
|
225 | - * < 1 hour => n minutes ago |
|
226 | - * < 1 day => n hours ago |
|
227 | - * < 1 month => Yesterday, n days ago |
|
228 | - * < 13 month => last month, n months ago |
|
229 | - * >= 13 month => last year, n years ago |
|
230 | - * @param \OCP\IL10N $l The locale to use |
|
231 | - * @return string Formatted time span |
|
232 | - */ |
|
233 | - public function formatTimeSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l = null) { |
|
234 | - $l = $this->getLocale($l); |
|
235 | - $timestamp = $this->getDateTime($timestamp); |
|
236 | - if ($baseTimestamp === null) { |
|
237 | - $baseTimestamp = time(); |
|
238 | - } |
|
239 | - $baseTimestamp = $this->getDateTime($baseTimestamp); |
|
218 | + /** |
|
219 | + * Gives the relative past time of the timestamp |
|
220 | + * |
|
221 | + * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
222 | + * @param int|\DateTime $baseTimestamp Timestamp to compare $timestamp against, defaults to current time |
|
223 | + * @return string Dates returned are: |
|
224 | + * < 60 sec => seconds ago |
|
225 | + * < 1 hour => n minutes ago |
|
226 | + * < 1 day => n hours ago |
|
227 | + * < 1 month => Yesterday, n days ago |
|
228 | + * < 13 month => last month, n months ago |
|
229 | + * >= 13 month => last year, n years ago |
|
230 | + * @param \OCP\IL10N $l The locale to use |
|
231 | + * @return string Formatted time span |
|
232 | + */ |
|
233 | + public function formatTimeSpan($timestamp, $baseTimestamp = null, \OCP\IL10N $l = null) { |
|
234 | + $l = $this->getLocale($l); |
|
235 | + $timestamp = $this->getDateTime($timestamp); |
|
236 | + if ($baseTimestamp === null) { |
|
237 | + $baseTimestamp = time(); |
|
238 | + } |
|
239 | + $baseTimestamp = $this->getDateTime($baseTimestamp); |
|
240 | 240 | |
241 | - $diff = $timestamp->diff($baseTimestamp); |
|
242 | - if ($diff->y > 0 || $diff->m > 0 || $diff->d > 0) { |
|
243 | - return $this->formatDateSpan($timestamp, $baseTimestamp, $l); |
|
244 | - } |
|
241 | + $diff = $timestamp->diff($baseTimestamp); |
|
242 | + if ($diff->y > 0 || $diff->m > 0 || $diff->d > 0) { |
|
243 | + return $this->formatDateSpan($timestamp, $baseTimestamp, $l); |
|
244 | + } |
|
245 | 245 | |
246 | - if ($diff->h > 0) { |
|
247 | - if ($timestamp > $baseTimestamp) { |
|
248 | - return $l->n('in %n hour', 'in %n hours', $diff->h); |
|
249 | - } else { |
|
250 | - return $l->n('%n hour ago', '%n hours ago', $diff->h); |
|
251 | - } |
|
252 | - } else if ($diff->i > 0) { |
|
253 | - if ($timestamp > $baseTimestamp) { |
|
254 | - return $l->n('in %n minute', 'in %n minutes', $diff->i); |
|
255 | - } else { |
|
256 | - return $l->n('%n minute ago', '%n minutes ago', $diff->i); |
|
257 | - } |
|
258 | - } |
|
259 | - if ($timestamp > $baseTimestamp) { |
|
260 | - return $l->t('in a few seconds'); |
|
261 | - } else { |
|
262 | - return $l->t('seconds ago'); |
|
263 | - } |
|
264 | - } |
|
246 | + if ($diff->h > 0) { |
|
247 | + if ($timestamp > $baseTimestamp) { |
|
248 | + return $l->n('in %n hour', 'in %n hours', $diff->h); |
|
249 | + } else { |
|
250 | + return $l->n('%n hour ago', '%n hours ago', $diff->h); |
|
251 | + } |
|
252 | + } else if ($diff->i > 0) { |
|
253 | + if ($timestamp > $baseTimestamp) { |
|
254 | + return $l->n('in %n minute', 'in %n minutes', $diff->i); |
|
255 | + } else { |
|
256 | + return $l->n('%n minute ago', '%n minutes ago', $diff->i); |
|
257 | + } |
|
258 | + } |
|
259 | + if ($timestamp > $baseTimestamp) { |
|
260 | + return $l->t('in a few seconds'); |
|
261 | + } else { |
|
262 | + return $l->t('seconds ago'); |
|
263 | + } |
|
264 | + } |
|
265 | 265 | |
266 | - /** |
|
267 | - * Formats the date and time of the given timestamp |
|
268 | - * |
|
269 | - * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
270 | - * @param string $formatDate See formatDate() for description |
|
271 | - * @param string $formatTime See formatTime() for description |
|
272 | - * @param \DateTimeZone $timeZone The timezone to use |
|
273 | - * @param \OCP\IL10N $l The locale to use |
|
274 | - * @return string Formatted date and time string |
|
275 | - */ |
|
276 | - public function formatDateTime($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
277 | - return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l); |
|
278 | - } |
|
266 | + /** |
|
267 | + * Formats the date and time of the given timestamp |
|
268 | + * |
|
269 | + * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
270 | + * @param string $formatDate See formatDate() for description |
|
271 | + * @param string $formatTime See formatTime() for description |
|
272 | + * @param \DateTimeZone $timeZone The timezone to use |
|
273 | + * @param \OCP\IL10N $l The locale to use |
|
274 | + * @return string Formatted date and time string |
|
275 | + */ |
|
276 | + public function formatDateTime($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
277 | + return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l); |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Formats the date and time of the given timestamp |
|
282 | - * |
|
283 | - * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
284 | - * @param string $formatDate See formatDate() for description |
|
285 | - * Uses 'Today', 'Yesterday' and 'Tomorrow' when applicable |
|
286 | - * @param string $formatTime See formatTime() for description |
|
287 | - * @param \DateTimeZone $timeZone The timezone to use |
|
288 | - * @param \OCP\IL10N $l The locale to use |
|
289 | - * @return string Formatted relative date and time string |
|
290 | - */ |
|
291 | - public function formatDateTimeRelativeDay($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
292 | - if (substr($formatDate, -1) !== '^' && substr($formatDate, -1) !== '*') { |
|
293 | - $formatDate .= '^'; |
|
294 | - } |
|
280 | + /** |
|
281 | + * Formats the date and time of the given timestamp |
|
282 | + * |
|
283 | + * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
284 | + * @param string $formatDate See formatDate() for description |
|
285 | + * Uses 'Today', 'Yesterday' and 'Tomorrow' when applicable |
|
286 | + * @param string $formatTime See formatTime() for description |
|
287 | + * @param \DateTimeZone $timeZone The timezone to use |
|
288 | + * @param \OCP\IL10N $l The locale to use |
|
289 | + * @return string Formatted relative date and time string |
|
290 | + */ |
|
291 | + public function formatDateTimeRelativeDay($timestamp, $formatDate = 'long', $formatTime = 'medium', \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
292 | + if (substr($formatDate, -1) !== '^' && substr($formatDate, -1) !== '*') { |
|
293 | + $formatDate .= '^'; |
|
294 | + } |
|
295 | 295 | |
296 | - return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l); |
|
297 | - } |
|
296 | + return $this->format($timestamp, 'datetime', $formatDate . '|' . $formatTime, $timeZone, $l); |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Formats the date and time of the given timestamp |
|
301 | - * |
|
302 | - * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
303 | - * @param string $type One of 'date', 'datetime' or 'time' |
|
304 | - * @param string $format Format string |
|
305 | - * @param \DateTimeZone $timeZone The timezone to use |
|
306 | - * @param \OCP\IL10N $l The locale to use |
|
307 | - * @return string Formatted date and time string |
|
308 | - */ |
|
309 | - protected function format($timestamp, $type, $format, \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
310 | - $l = $this->getLocale($l); |
|
311 | - $timeZone = $this->getTimeZone($timeZone); |
|
312 | - $timestamp = $this->getDateTime($timestamp, $timeZone); |
|
299 | + /** |
|
300 | + * Formats the date and time of the given timestamp |
|
301 | + * |
|
302 | + * @param int|\DateTime $timestamp Either a Unix timestamp or DateTime object |
|
303 | + * @param string $type One of 'date', 'datetime' or 'time' |
|
304 | + * @param string $format Format string |
|
305 | + * @param \DateTimeZone $timeZone The timezone to use |
|
306 | + * @param \OCP\IL10N $l The locale to use |
|
307 | + * @return string Formatted date and time string |
|
308 | + */ |
|
309 | + protected function format($timestamp, $type, $format, \DateTimeZone $timeZone = null, \OCP\IL10N $l = null) { |
|
310 | + $l = $this->getLocale($l); |
|
311 | + $timeZone = $this->getTimeZone($timeZone); |
|
312 | + $timestamp = $this->getDateTime($timestamp, $timeZone); |
|
313 | 313 | |
314 | - return $l->l($type, $timestamp, array( |
|
315 | - 'width' => $format, |
|
316 | - )); |
|
317 | - } |
|
314 | + return $l->l($type, $timestamp, array( |
|
315 | + 'width' => $format, |
|
316 | + )); |
|
317 | + } |
|
318 | 318 | } |
@@ -26,48 +26,48 @@ |
||
26 | 26 | use OCP\Command\ICommand; |
27 | 27 | |
28 | 28 | class QueueBus implements IBus { |
29 | - /** |
|
30 | - * @var ICommand[]|callable[] |
|
31 | - */ |
|
32 | - private $queue = []; |
|
29 | + /** |
|
30 | + * @var ICommand[]|callable[] |
|
31 | + */ |
|
32 | + private $queue = []; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Schedule a command to be fired |
|
36 | - * |
|
37 | - * @param \OCP\Command\ICommand | callable $command |
|
38 | - */ |
|
39 | - public function push($command) { |
|
40 | - $this->queue[] = $command; |
|
41 | - } |
|
34 | + /** |
|
35 | + * Schedule a command to be fired |
|
36 | + * |
|
37 | + * @param \OCP\Command\ICommand | callable $command |
|
38 | + */ |
|
39 | + public function push($command) { |
|
40 | + $this->queue[] = $command; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Require all commands using a trait to be run synchronous |
|
45 | - * |
|
46 | - * @param string $trait |
|
47 | - */ |
|
48 | - public function requireSync($trait) { |
|
49 | - } |
|
43 | + /** |
|
44 | + * Require all commands using a trait to be run synchronous |
|
45 | + * |
|
46 | + * @param string $trait |
|
47 | + */ |
|
48 | + public function requireSync($trait) { |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param \OCP\Command\ICommand | callable $command |
|
53 | - */ |
|
54 | - private function runCommand($command) { |
|
55 | - if ($command instanceof ICommand) { |
|
56 | - // ensure the command can be serialized |
|
57 | - $serialized = serialize($command); |
|
58 | - if(strlen($serialized) > 4000) { |
|
59 | - throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)'); |
|
60 | - } |
|
61 | - $unserialized = unserialize($serialized); |
|
62 | - $unserialized->handle(); |
|
63 | - } else { |
|
64 | - $command(); |
|
65 | - } |
|
66 | - } |
|
51 | + /** |
|
52 | + * @param \OCP\Command\ICommand | callable $command |
|
53 | + */ |
|
54 | + private function runCommand($command) { |
|
55 | + if ($command instanceof ICommand) { |
|
56 | + // ensure the command can be serialized |
|
57 | + $serialized = serialize($command); |
|
58 | + if(strlen($serialized) > 4000) { |
|
59 | + throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)'); |
|
60 | + } |
|
61 | + $unserialized = unserialize($serialized); |
|
62 | + $unserialized->handle(); |
|
63 | + } else { |
|
64 | + $command(); |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - public function run() { |
|
69 | - while ($command = array_shift($this->queue)) { |
|
70 | - $this->runCommand($command); |
|
71 | - } |
|
72 | - } |
|
68 | + public function run() { |
|
69 | + while ($command = array_shift($this->queue)) { |
|
70 | + $this->runCommand($command); |
|
71 | + } |
|
72 | + } |
|
73 | 73 | } |
@@ -33,121 +33,121 @@ |
||
33 | 33 | |
34 | 34 | class Http extends BaseHttp { |
35 | 35 | |
36 | - private $server; |
|
37 | - private $protocolVersion; |
|
38 | - protected $headers; |
|
39 | - |
|
40 | - /** |
|
41 | - * @param array $server $_SERVER |
|
42 | - * @param string $protocolVersion the http version to use defaults to HTTP/1.1 |
|
43 | - */ |
|
44 | - public function __construct($server, $protocolVersion='HTTP/1.1') { |
|
45 | - $this->server = $server; |
|
46 | - $this->protocolVersion = $protocolVersion; |
|
47 | - |
|
48 | - $this->headers = array( |
|
49 | - self::STATUS_CONTINUE => 'Continue', |
|
50 | - self::STATUS_SWITCHING_PROTOCOLS => 'Switching Protocols', |
|
51 | - self::STATUS_PROCESSING => 'Processing', |
|
52 | - self::STATUS_OK => 'OK', |
|
53 | - self::STATUS_CREATED => 'Created', |
|
54 | - self::STATUS_ACCEPTED => 'Accepted', |
|
55 | - self::STATUS_NON_AUTHORATIVE_INFORMATION => 'Non-Authorative Information', |
|
56 | - self::STATUS_NO_CONTENT => 'No Content', |
|
57 | - self::STATUS_RESET_CONTENT => 'Reset Content', |
|
58 | - self::STATUS_PARTIAL_CONTENT => 'Partial Content', |
|
59 | - self::STATUS_MULTI_STATUS => 'Multi-Status', // RFC 4918 |
|
60 | - self::STATUS_ALREADY_REPORTED => 'Already Reported', // RFC 5842 |
|
61 | - self::STATUS_IM_USED => 'IM Used', // RFC 3229 |
|
62 | - self::STATUS_MULTIPLE_CHOICES => 'Multiple Choices', |
|
63 | - self::STATUS_MOVED_PERMANENTLY => 'Moved Permanently', |
|
64 | - self::STATUS_FOUND => 'Found', |
|
65 | - self::STATUS_SEE_OTHER => 'See Other', |
|
66 | - self::STATUS_NOT_MODIFIED => 'Not Modified', |
|
67 | - self::STATUS_USE_PROXY => 'Use Proxy', |
|
68 | - self::STATUS_RESERVED => 'Reserved', |
|
69 | - self::STATUS_TEMPORARY_REDIRECT => 'Temporary Redirect', |
|
70 | - self::STATUS_BAD_REQUEST => 'Bad request', |
|
71 | - self::STATUS_UNAUTHORIZED => 'Unauthorized', |
|
72 | - self::STATUS_PAYMENT_REQUIRED => 'Payment Required', |
|
73 | - self::STATUS_FORBIDDEN => 'Forbidden', |
|
74 | - self::STATUS_NOT_FOUND => 'Not Found', |
|
75 | - self::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
76 | - self::STATUS_NOT_ACCEPTABLE => 'Not Acceptable', |
|
77 | - self::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
78 | - self::STATUS_REQUEST_TIMEOUT => 'Request Timeout', |
|
79 | - self::STATUS_CONFLICT => 'Conflict', |
|
80 | - self::STATUS_GONE => 'Gone', |
|
81 | - self::STATUS_LENGTH_REQUIRED => 'Length Required', |
|
82 | - self::STATUS_PRECONDITION_FAILED => 'Precondition failed', |
|
83 | - self::STATUS_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
84 | - self::STATUS_REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
85 | - self::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
86 | - self::STATUS_REQUEST_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
87 | - self::STATUS_EXPECTATION_FAILED => 'Expectation Failed', |
|
88 | - self::STATUS_IM_A_TEAPOT => 'I\'m a teapot', // RFC 2324 |
|
89 | - self::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity', // RFC 4918 |
|
90 | - self::STATUS_LOCKED => 'Locked', // RFC 4918 |
|
91 | - self::STATUS_FAILED_DEPENDENCY => 'Failed Dependency', // RFC 4918 |
|
92 | - self::STATUS_UPGRADE_REQUIRED => 'Upgrade required', |
|
93 | - self::STATUS_PRECONDITION_REQUIRED => 'Precondition required', // draft-nottingham-http-new-status |
|
94 | - self::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests', // draft-nottingham-http-new-status |
|
95 | - self::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large', // draft-nottingham-http-new-status |
|
96 | - self::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
97 | - self::STATUS_NOT_IMPLEMENTED => 'Not Implemented', |
|
98 | - self::STATUS_BAD_GATEWAY => 'Bad Gateway', |
|
99 | - self::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable', |
|
100 | - self::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout', |
|
101 | - self::STATUS_HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version not supported', |
|
102 | - self::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates', |
|
103 | - self::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage', // RFC 4918 |
|
104 | - self::STATUS_LOOP_DETECTED => 'Loop Detected', // RFC 5842 |
|
105 | - self::STATUS_BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded', // non-standard |
|
106 | - self::STATUS_NOT_EXTENDED => 'Not extended', |
|
107 | - self::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required', // draft-nottingham-http-new-status |
|
108 | - ); |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * Gets the correct header |
|
114 | - * @param int Http::CONSTANT $status the constant from the Http class |
|
115 | - * @param \DateTime $lastModified formatted last modified date |
|
116 | - * @param string $ETag the etag |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - public function getStatusHeader($status, \DateTime $lastModified=null, |
|
120 | - $ETag=null) { |
|
121 | - |
|
122 | - if(!is_null($lastModified)) { |
|
123 | - $lastModified = $lastModified->format(\DateTime::RFC2822); |
|
124 | - } |
|
125 | - |
|
126 | - // if etag or lastmodified have not changed, return a not modified |
|
127 | - if ((isset($this->server['HTTP_IF_NONE_MATCH']) |
|
128 | - && trim(trim($this->server['HTTP_IF_NONE_MATCH']), '"') === (string)$ETag) |
|
129 | - |
|
130 | - || |
|
131 | - |
|
132 | - (isset($this->server['HTTP_IF_MODIFIED_SINCE']) |
|
133 | - && trim($this->server['HTTP_IF_MODIFIED_SINCE']) === |
|
134 | - $lastModified)) { |
|
135 | - |
|
136 | - $status = self::STATUS_NOT_MODIFIED; |
|
137 | - } |
|
138 | - |
|
139 | - // we have one change currently for the http 1.0 header that differs |
|
140 | - // from 1.1: STATUS_TEMPORARY_REDIRECT should be STATUS_FOUND |
|
141 | - // if this differs any more, we want to create childclasses for this |
|
142 | - if($status === self::STATUS_TEMPORARY_REDIRECT |
|
143 | - && $this->protocolVersion === 'HTTP/1.0') { |
|
144 | - |
|
145 | - $status = self::STATUS_FOUND; |
|
146 | - } |
|
147 | - |
|
148 | - return $this->protocolVersion . ' ' . $status . ' ' . |
|
149 | - $this->headers[$status]; |
|
150 | - } |
|
36 | + private $server; |
|
37 | + private $protocolVersion; |
|
38 | + protected $headers; |
|
39 | + |
|
40 | + /** |
|
41 | + * @param array $server $_SERVER |
|
42 | + * @param string $protocolVersion the http version to use defaults to HTTP/1.1 |
|
43 | + */ |
|
44 | + public function __construct($server, $protocolVersion='HTTP/1.1') { |
|
45 | + $this->server = $server; |
|
46 | + $this->protocolVersion = $protocolVersion; |
|
47 | + |
|
48 | + $this->headers = array( |
|
49 | + self::STATUS_CONTINUE => 'Continue', |
|
50 | + self::STATUS_SWITCHING_PROTOCOLS => 'Switching Protocols', |
|
51 | + self::STATUS_PROCESSING => 'Processing', |
|
52 | + self::STATUS_OK => 'OK', |
|
53 | + self::STATUS_CREATED => 'Created', |
|
54 | + self::STATUS_ACCEPTED => 'Accepted', |
|
55 | + self::STATUS_NON_AUTHORATIVE_INFORMATION => 'Non-Authorative Information', |
|
56 | + self::STATUS_NO_CONTENT => 'No Content', |
|
57 | + self::STATUS_RESET_CONTENT => 'Reset Content', |
|
58 | + self::STATUS_PARTIAL_CONTENT => 'Partial Content', |
|
59 | + self::STATUS_MULTI_STATUS => 'Multi-Status', // RFC 4918 |
|
60 | + self::STATUS_ALREADY_REPORTED => 'Already Reported', // RFC 5842 |
|
61 | + self::STATUS_IM_USED => 'IM Used', // RFC 3229 |
|
62 | + self::STATUS_MULTIPLE_CHOICES => 'Multiple Choices', |
|
63 | + self::STATUS_MOVED_PERMANENTLY => 'Moved Permanently', |
|
64 | + self::STATUS_FOUND => 'Found', |
|
65 | + self::STATUS_SEE_OTHER => 'See Other', |
|
66 | + self::STATUS_NOT_MODIFIED => 'Not Modified', |
|
67 | + self::STATUS_USE_PROXY => 'Use Proxy', |
|
68 | + self::STATUS_RESERVED => 'Reserved', |
|
69 | + self::STATUS_TEMPORARY_REDIRECT => 'Temporary Redirect', |
|
70 | + self::STATUS_BAD_REQUEST => 'Bad request', |
|
71 | + self::STATUS_UNAUTHORIZED => 'Unauthorized', |
|
72 | + self::STATUS_PAYMENT_REQUIRED => 'Payment Required', |
|
73 | + self::STATUS_FORBIDDEN => 'Forbidden', |
|
74 | + self::STATUS_NOT_FOUND => 'Not Found', |
|
75 | + self::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
76 | + self::STATUS_NOT_ACCEPTABLE => 'Not Acceptable', |
|
77 | + self::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
78 | + self::STATUS_REQUEST_TIMEOUT => 'Request Timeout', |
|
79 | + self::STATUS_CONFLICT => 'Conflict', |
|
80 | + self::STATUS_GONE => 'Gone', |
|
81 | + self::STATUS_LENGTH_REQUIRED => 'Length Required', |
|
82 | + self::STATUS_PRECONDITION_FAILED => 'Precondition failed', |
|
83 | + self::STATUS_REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
84 | + self::STATUS_REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
85 | + self::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
86 | + self::STATUS_REQUEST_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
87 | + self::STATUS_EXPECTATION_FAILED => 'Expectation Failed', |
|
88 | + self::STATUS_IM_A_TEAPOT => 'I\'m a teapot', // RFC 2324 |
|
89 | + self::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity', // RFC 4918 |
|
90 | + self::STATUS_LOCKED => 'Locked', // RFC 4918 |
|
91 | + self::STATUS_FAILED_DEPENDENCY => 'Failed Dependency', // RFC 4918 |
|
92 | + self::STATUS_UPGRADE_REQUIRED => 'Upgrade required', |
|
93 | + self::STATUS_PRECONDITION_REQUIRED => 'Precondition required', // draft-nottingham-http-new-status |
|
94 | + self::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests', // draft-nottingham-http-new-status |
|
95 | + self::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large', // draft-nottingham-http-new-status |
|
96 | + self::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
97 | + self::STATUS_NOT_IMPLEMENTED => 'Not Implemented', |
|
98 | + self::STATUS_BAD_GATEWAY => 'Bad Gateway', |
|
99 | + self::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable', |
|
100 | + self::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout', |
|
101 | + self::STATUS_HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version not supported', |
|
102 | + self::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates', |
|
103 | + self::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage', // RFC 4918 |
|
104 | + self::STATUS_LOOP_DETECTED => 'Loop Detected', // RFC 5842 |
|
105 | + self::STATUS_BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth Limit Exceeded', // non-standard |
|
106 | + self::STATUS_NOT_EXTENDED => 'Not extended', |
|
107 | + self::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required', // draft-nottingham-http-new-status |
|
108 | + ); |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * Gets the correct header |
|
114 | + * @param int Http::CONSTANT $status the constant from the Http class |
|
115 | + * @param \DateTime $lastModified formatted last modified date |
|
116 | + * @param string $ETag the etag |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + public function getStatusHeader($status, \DateTime $lastModified=null, |
|
120 | + $ETag=null) { |
|
121 | + |
|
122 | + if(!is_null($lastModified)) { |
|
123 | + $lastModified = $lastModified->format(\DateTime::RFC2822); |
|
124 | + } |
|
125 | + |
|
126 | + // if etag or lastmodified have not changed, return a not modified |
|
127 | + if ((isset($this->server['HTTP_IF_NONE_MATCH']) |
|
128 | + && trim(trim($this->server['HTTP_IF_NONE_MATCH']), '"') === (string)$ETag) |
|
129 | + |
|
130 | + || |
|
131 | + |
|
132 | + (isset($this->server['HTTP_IF_MODIFIED_SINCE']) |
|
133 | + && trim($this->server['HTTP_IF_MODIFIED_SINCE']) === |
|
134 | + $lastModified)) { |
|
135 | + |
|
136 | + $status = self::STATUS_NOT_MODIFIED; |
|
137 | + } |
|
138 | + |
|
139 | + // we have one change currently for the http 1.0 header that differs |
|
140 | + // from 1.1: STATUS_TEMPORARY_REDIRECT should be STATUS_FOUND |
|
141 | + // if this differs any more, we want to create childclasses for this |
|
142 | + if($status === self::STATUS_TEMPORARY_REDIRECT |
|
143 | + && $this->protocolVersion === 'HTTP/1.0') { |
|
144 | + |
|
145 | + $status = self::STATUS_FOUND; |
|
146 | + } |
|
147 | + |
|
148 | + return $this->protocolVersion . ' ' . $status . ' ' . |
|
149 | + $this->headers[$status]; |
|
150 | + } |
|
151 | 151 | |
152 | 152 | |
153 | 153 | } |
@@ -25,11 +25,11 @@ |
||
25 | 25 | * @since 13.0.0 |
26 | 26 | */ |
27 | 27 | interface IInstanceFactory { |
28 | - /** |
|
29 | - * @param string $url |
|
30 | - * @return IInstance |
|
31 | - * |
|
32 | - * @since 13.0.0 |
|
33 | - */ |
|
34 | - public function getInstance($url); |
|
28 | + /** |
|
29 | + * @param string $url |
|
30 | + * @return IInstance |
|
31 | + * |
|
32 | + * @since 13.0.0 |
|
33 | + */ |
|
34 | + public function getInstance($url); |
|
35 | 35 | } |
@@ -30,118 +30,118 @@ |
||
30 | 30 | * Provides some basic info about a remote Nextcloud instance |
31 | 31 | */ |
32 | 32 | class Instance implements IInstance { |
33 | - /** @var string */ |
|
34 | - private $url; |
|
33 | + /** @var string */ |
|
34 | + private $url; |
|
35 | 35 | |
36 | - /** @var ICache */ |
|
37 | - private $cache; |
|
36 | + /** @var ICache */ |
|
37 | + private $cache; |
|
38 | 38 | |
39 | - /** @var IClientService */ |
|
40 | - private $clientService; |
|
39 | + /** @var IClientService */ |
|
40 | + private $clientService; |
|
41 | 41 | |
42 | - /** @var array|null */ |
|
43 | - private $status; |
|
42 | + /** @var array|null */ |
|
43 | + private $status; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $url |
|
47 | - * @param ICache $cache |
|
48 | - * @param IClientService $clientService |
|
49 | - */ |
|
50 | - public function __construct($url, ICache $cache, IClientService $clientService) { |
|
51 | - $url = str_replace('https://', '', $url); |
|
52 | - $this->url = str_replace('http://', '', $url); |
|
53 | - $this->cache = $cache; |
|
54 | - $this->clientService = $clientService; |
|
55 | - } |
|
45 | + /** |
|
46 | + * @param string $url |
|
47 | + * @param ICache $cache |
|
48 | + * @param IClientService $clientService |
|
49 | + */ |
|
50 | + public function __construct($url, ICache $cache, IClientService $clientService) { |
|
51 | + $url = str_replace('https://', '', $url); |
|
52 | + $this->url = str_replace('http://', '', $url); |
|
53 | + $this->cache = $cache; |
|
54 | + $this->clientService = $clientService; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @return string The url of the remote server without protocol |
|
59 | - */ |
|
60 | - public function getUrl() { |
|
61 | - return $this->url; |
|
62 | - } |
|
57 | + /** |
|
58 | + * @return string The url of the remote server without protocol |
|
59 | + */ |
|
60 | + public function getUrl() { |
|
61 | + return $this->url; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @return string The of of the remote server with protocol |
|
66 | - */ |
|
67 | - public function getFullUrl() { |
|
68 | - return $this->getProtocol() . '://' . $this->getUrl(); |
|
69 | - } |
|
64 | + /** |
|
65 | + * @return string The of of the remote server with protocol |
|
66 | + */ |
|
67 | + public function getFullUrl() { |
|
68 | + return $this->getProtocol() . '://' . $this->getUrl(); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @return string The full version string in '13.1.2.3' format |
|
73 | - */ |
|
74 | - public function getVersion() { |
|
75 | - $status = $this->getStatus(); |
|
76 | - return $status['version']; |
|
77 | - } |
|
71 | + /** |
|
72 | + * @return string The full version string in '13.1.2.3' format |
|
73 | + */ |
|
74 | + public function getVersion() { |
|
75 | + $status = $this->getStatus(); |
|
76 | + return $status['version']; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return string 'http' or 'https' |
|
81 | - */ |
|
82 | - public function getProtocol() { |
|
83 | - $status = $this->getStatus(); |
|
84 | - return $status['protocol']; |
|
85 | - } |
|
79 | + /** |
|
80 | + * @return string 'http' or 'https' |
|
81 | + */ |
|
82 | + public function getProtocol() { |
|
83 | + $status = $this->getStatus(); |
|
84 | + return $status['protocol']; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Check that the remote server is installed and not in maintenance mode |
|
89 | - * |
|
90 | - * @return bool |
|
91 | - */ |
|
92 | - public function isActive() { |
|
93 | - $status = $this->getStatus(); |
|
94 | - return $status['installed'] && !$status['maintenance']; |
|
95 | - } |
|
87 | + /** |
|
88 | + * Check that the remote server is installed and not in maintenance mode |
|
89 | + * |
|
90 | + * @return bool |
|
91 | + */ |
|
92 | + public function isActive() { |
|
93 | + $status = $this->getStatus(); |
|
94 | + return $status['installed'] && !$status['maintenance']; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @return array |
|
99 | - * @throws NotFoundException |
|
100 | - * @throws \Exception |
|
101 | - */ |
|
102 | - private function getStatus() { |
|
103 | - if ($this->status) { |
|
104 | - return $this->status; |
|
105 | - } |
|
106 | - $key = 'remote/' . $this->url . '/status'; |
|
107 | - $httpsKey = 'remote/' . $this->url . '/https'; |
|
108 | - $status = $this->cache->get($key); |
|
109 | - if (!$status) { |
|
110 | - $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); |
|
111 | - $protocol = 'https'; |
|
112 | - if (!$response) { |
|
113 | - if ($status = $this->cache->get($httpsKey)) { |
|
114 | - throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https'); |
|
115 | - } |
|
116 | - $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); |
|
117 | - $protocol = 'http'; |
|
118 | - } else { |
|
119 | - $this->cache->set($httpsKey, true, 60 * 60 * 24 * 365); |
|
120 | - } |
|
121 | - $status = json_decode($response, true); |
|
122 | - if ($status) { |
|
123 | - $status['protocol'] = $protocol; |
|
124 | - } |
|
125 | - if ($status) { |
|
126 | - $this->cache->set($key, $status, 5 * 60); |
|
127 | - $this->status = $status; |
|
128 | - } else { |
|
129 | - throw new NotFoundException('Remote server not found at address ' . $this->url); |
|
130 | - } |
|
131 | - } |
|
132 | - return $status; |
|
133 | - } |
|
97 | + /** |
|
98 | + * @return array |
|
99 | + * @throws NotFoundException |
|
100 | + * @throws \Exception |
|
101 | + */ |
|
102 | + private function getStatus() { |
|
103 | + if ($this->status) { |
|
104 | + return $this->status; |
|
105 | + } |
|
106 | + $key = 'remote/' . $this->url . '/status'; |
|
107 | + $httpsKey = 'remote/' . $this->url . '/https'; |
|
108 | + $status = $this->cache->get($key); |
|
109 | + if (!$status) { |
|
110 | + $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); |
|
111 | + $protocol = 'https'; |
|
112 | + if (!$response) { |
|
113 | + if ($status = $this->cache->get($httpsKey)) { |
|
114 | + throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https'); |
|
115 | + } |
|
116 | + $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); |
|
117 | + $protocol = 'http'; |
|
118 | + } else { |
|
119 | + $this->cache->set($httpsKey, true, 60 * 60 * 24 * 365); |
|
120 | + } |
|
121 | + $status = json_decode($response, true); |
|
122 | + if ($status) { |
|
123 | + $status['protocol'] = $protocol; |
|
124 | + } |
|
125 | + if ($status) { |
|
126 | + $this->cache->set($key, $status, 5 * 60); |
|
127 | + $this->status = $status; |
|
128 | + } else { |
|
129 | + throw new NotFoundException('Remote server not found at address ' . $this->url); |
|
130 | + } |
|
131 | + } |
|
132 | + return $status; |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * @param string $url |
|
137 | - * @return bool|string |
|
138 | - */ |
|
139 | - private function downloadStatus($url) { |
|
140 | - try { |
|
141 | - $request = $this->clientService->newClient()->get($url); |
|
142 | - return $request->getBody(); |
|
143 | - } catch (\Exception $e) { |
|
144 | - return false; |
|
145 | - } |
|
146 | - } |
|
135 | + /** |
|
136 | + * @param string $url |
|
137 | + * @return bool|string |
|
138 | + */ |
|
139 | + private function downloadStatus($url) { |
|
140 | + try { |
|
141 | + $request = $this->clientService->newClient()->get($url); |
|
142 | + return $request->getBody(); |
|
143 | + } catch (\Exception $e) { |
|
144 | + return false; |
|
145 | + } |
|
146 | + } |
|
147 | 147 | } |
@@ -28,14 +28,14 @@ |
||
28 | 28 | use OCP\Remote\IInstance; |
29 | 29 | |
30 | 30 | class ApiFactory implements IApiFactory { |
31 | - /** @var IClientService */ |
|
32 | - private $clientService; |
|
31 | + /** @var IClientService */ |
|
32 | + private $clientService; |
|
33 | 33 | |
34 | - public function __construct(IClientService $clientService) { |
|
35 | - $this->clientService = $clientService; |
|
36 | - } |
|
34 | + public function __construct(IClientService $clientService) { |
|
35 | + $this->clientService = $clientService; |
|
36 | + } |
|
37 | 37 | |
38 | - public function getApiCollection(IInstance $instance, ICredentials $credentials) { |
|
39 | - return new ApiCollection($instance, $credentials, $this->clientService); |
|
40 | - } |
|
38 | + public function getApiCollection(IInstance $instance, ICredentials $credentials) { |
|
39 | + return new ApiCollection($instance, $credentials, $this->clientService); |
|
40 | + } |
|
41 | 41 | } |
@@ -28,24 +28,24 @@ |
||
28 | 28 | use OCP\Remote\IInstance; |
29 | 29 | |
30 | 30 | class ApiCollection implements IApiCollection { |
31 | - /** @var IInstance */ |
|
32 | - private $instance; |
|
33 | - /** @var ICredentials */ |
|
34 | - private $credentials; |
|
35 | - /** @var IClientService */ |
|
36 | - private $clientService; |
|
31 | + /** @var IInstance */ |
|
32 | + private $instance; |
|
33 | + /** @var ICredentials */ |
|
34 | + private $credentials; |
|
35 | + /** @var IClientService */ |
|
36 | + private $clientService; |
|
37 | 37 | |
38 | - public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { |
|
39 | - $this->instance = $instance; |
|
40 | - $this->credentials = $credentials; |
|
41 | - $this->clientService = $clientService; |
|
42 | - } |
|
38 | + public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { |
|
39 | + $this->instance = $instance; |
|
40 | + $this->credentials = $credentials; |
|
41 | + $this->clientService = $clientService; |
|
42 | + } |
|
43 | 43 | |
44 | - public function getCapabilitiesApi() { |
|
45 | - return new OCS($this->instance, $this->credentials, $this->clientService); |
|
46 | - } |
|
44 | + public function getCapabilitiesApi() { |
|
45 | + return new OCS($this->instance, $this->credentials, $this->clientService); |
|
46 | + } |
|
47 | 47 | |
48 | - public function getUserApi() { |
|
49 | - return new OCS($this->instance, $this->credentials, $this->clientService); |
|
50 | - } |
|
48 | + public function getUserApi() { |
|
49 | + return new OCS($this->instance, $this->credentials, $this->clientService); |
|
50 | + } |
|
51 | 51 | } |
@@ -26,72 +26,72 @@ |
||
26 | 26 | use OCP\Remote\IInstance; |
27 | 27 | |
28 | 28 | class ApiBase { |
29 | - /** @var IInstance */ |
|
30 | - private $instance; |
|
31 | - /** @var ICredentials */ |
|
32 | - private $credentials; |
|
33 | - /** @var IClientService */ |
|
34 | - private $clientService; |
|
29 | + /** @var IInstance */ |
|
30 | + private $instance; |
|
31 | + /** @var ICredentials */ |
|
32 | + private $credentials; |
|
33 | + /** @var IClientService */ |
|
34 | + private $clientService; |
|
35 | 35 | |
36 | - public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { |
|
37 | - $this->instance = $instance; |
|
38 | - $this->credentials = $credentials; |
|
39 | - $this->clientService = $clientService; |
|
40 | - } |
|
36 | + public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { |
|
37 | + $this->instance = $instance; |
|
38 | + $this->credentials = $credentials; |
|
39 | + $this->clientService = $clientService; |
|
40 | + } |
|
41 | 41 | |
42 | - protected function getHttpClient() { |
|
43 | - return $this->clientService->newClient(); |
|
44 | - } |
|
42 | + protected function getHttpClient() { |
|
43 | + return $this->clientService->newClient(); |
|
44 | + } |
|
45 | 45 | |
46 | - protected function addDefaultHeaders(array $headers) { |
|
47 | - return array_merge([ |
|
48 | - 'OCS-APIREQUEST' => 'true', |
|
49 | - 'Accept' => 'application/json' |
|
50 | - ], $headers); |
|
51 | - } |
|
46 | + protected function addDefaultHeaders(array $headers) { |
|
47 | + return array_merge([ |
|
48 | + 'OCS-APIREQUEST' => 'true', |
|
49 | + 'Accept' => 'application/json' |
|
50 | + ], $headers); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @param string $method |
|
55 | - * @param string $url |
|
56 | - * @param array $body |
|
57 | - * @param array $query |
|
58 | - * @param array $headers |
|
59 | - * @return resource|string |
|
60 | - * @throws \InvalidArgumentException |
|
61 | - */ |
|
62 | - protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
63 | - $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
|
64 | - $options = [ |
|
65 | - 'query' => $query, |
|
66 | - 'headers' => $this->addDefaultHeaders($headers), |
|
67 | - 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()] |
|
68 | - ]; |
|
69 | - if ($body) { |
|
70 | - $options['body'] = $body; |
|
71 | - } |
|
53 | + /** |
|
54 | + * @param string $method |
|
55 | + * @param string $url |
|
56 | + * @param array $body |
|
57 | + * @param array $query |
|
58 | + * @param array $headers |
|
59 | + * @return resource|string |
|
60 | + * @throws \InvalidArgumentException |
|
61 | + */ |
|
62 | + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { |
|
63 | + $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; |
|
64 | + $options = [ |
|
65 | + 'query' => $query, |
|
66 | + 'headers' => $this->addDefaultHeaders($headers), |
|
67 | + 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()] |
|
68 | + ]; |
|
69 | + if ($body) { |
|
70 | + $options['body'] = $body; |
|
71 | + } |
|
72 | 72 | |
73 | - $client = $this->getHttpClient(); |
|
73 | + $client = $this->getHttpClient(); |
|
74 | 74 | |
75 | - switch ($method) { |
|
76 | - case 'get': |
|
77 | - $response = $client->get($fullUrl, $options); |
|
78 | - break; |
|
79 | - case 'post': |
|
80 | - $response = $client->post($fullUrl, $options); |
|
81 | - break; |
|
82 | - case 'put': |
|
83 | - $response = $client->put($fullUrl, $options); |
|
84 | - break; |
|
85 | - case 'delete': |
|
86 | - $response = $client->delete($fullUrl, $options); |
|
87 | - break; |
|
88 | - case 'options': |
|
89 | - $response = $client->options($fullUrl, $options); |
|
90 | - break; |
|
91 | - default: |
|
92 | - throw new \InvalidArgumentException('Invalid method ' . $method); |
|
93 | - } |
|
75 | + switch ($method) { |
|
76 | + case 'get': |
|
77 | + $response = $client->get($fullUrl, $options); |
|
78 | + break; |
|
79 | + case 'post': |
|
80 | + $response = $client->post($fullUrl, $options); |
|
81 | + break; |
|
82 | + case 'put': |
|
83 | + $response = $client->put($fullUrl, $options); |
|
84 | + break; |
|
85 | + case 'delete': |
|
86 | + $response = $client->delete($fullUrl, $options); |
|
87 | + break; |
|
88 | + case 'options': |
|
89 | + $response = $client->options($fullUrl, $options); |
|
90 | + break; |
|
91 | + default: |
|
92 | + throw new \InvalidArgumentException('Invalid method ' . $method); |
|
93 | + } |
|
94 | 94 | |
95 | - return $response->getBody(); |
|
96 | - } |
|
95 | + return $response->getBody(); |
|
96 | + } |
|
97 | 97 | } |