@@ -30,128 +30,128 @@ |
||
30 | 30 | use OCP\ILogger; |
31 | 31 | |
32 | 32 | class ChangesCheck { |
33 | - /** @var IClientService */ |
|
34 | - protected $clientService; |
|
35 | - /** @var ChangesMapper */ |
|
36 | - private $mapper; |
|
37 | - /** @var ILogger */ |
|
38 | - private $logger; |
|
33 | + /** @var IClientService */ |
|
34 | + protected $clientService; |
|
35 | + /** @var ChangesMapper */ |
|
36 | + private $mapper; |
|
37 | + /** @var ILogger */ |
|
38 | + private $logger; |
|
39 | 39 | |
40 | - const RESPONSE_NO_CONTENT = 0; |
|
41 | - const RESPONSE_USE_CACHE = 1; |
|
42 | - const RESPONSE_HAS_CONTENT = 2; |
|
40 | + const RESPONSE_NO_CONTENT = 0; |
|
41 | + const RESPONSE_USE_CACHE = 1; |
|
42 | + const RESPONSE_HAS_CONTENT = 2; |
|
43 | 43 | |
44 | - public function __construct(IClientService $clientService, ChangesMapper $mapper, ILogger $logger) { |
|
45 | - $this->clientService = $clientService; |
|
46 | - $this->mapper = $mapper; |
|
47 | - $this->logger = $logger; |
|
48 | - } |
|
44 | + public function __construct(IClientService $clientService, ChangesMapper $mapper, ILogger $logger) { |
|
45 | + $this->clientService = $clientService; |
|
46 | + $this->mapper = $mapper; |
|
47 | + $this->logger = $logger; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @throws \Exception |
|
52 | - */ |
|
53 | - public function check(string $uri, string $version): array { |
|
54 | - try { |
|
55 | - $version = $this->normalizeVersion($version); |
|
56 | - $changesInfo = $this->mapper->getChanges($version); |
|
57 | - if($changesInfo->getLastCheck() + 1800 > time()) { |
|
58 | - return json_decode($changesInfo->getData(), true); |
|
59 | - } |
|
60 | - } catch (DoesNotExistException $e) { |
|
61 | - $changesInfo = new ChangesResult(); |
|
62 | - } |
|
50 | + /** |
|
51 | + * @throws \Exception |
|
52 | + */ |
|
53 | + public function check(string $uri, string $version): array { |
|
54 | + try { |
|
55 | + $version = $this->normalizeVersion($version); |
|
56 | + $changesInfo = $this->mapper->getChanges($version); |
|
57 | + if($changesInfo->getLastCheck() + 1800 > time()) { |
|
58 | + return json_decode($changesInfo->getData(), true); |
|
59 | + } |
|
60 | + } catch (DoesNotExistException $e) { |
|
61 | + $changesInfo = new ChangesResult(); |
|
62 | + } |
|
63 | 63 | |
64 | - $response = $this->queryChangesServer($uri, $changesInfo); |
|
64 | + $response = $this->queryChangesServer($uri, $changesInfo); |
|
65 | 65 | |
66 | - switch($this->evaluateResponse($response)) { |
|
67 | - case self::RESPONSE_NO_CONTENT: |
|
68 | - return []; |
|
69 | - case self::RESPONSE_USE_CACHE: |
|
70 | - return json_decode($changesInfo->getData(), true); |
|
71 | - case self::RESPONSE_HAS_CONTENT: |
|
72 | - default: |
|
73 | - $data = $this->extractData($response->getBody()); |
|
74 | - $changesInfo->setData(json_encode($data)); |
|
75 | - $changesInfo->setEtag($response->getHeader('Etag')); |
|
76 | - $this->cacheResult($changesInfo, $version); |
|
66 | + switch($this->evaluateResponse($response)) { |
|
67 | + case self::RESPONSE_NO_CONTENT: |
|
68 | + return []; |
|
69 | + case self::RESPONSE_USE_CACHE: |
|
70 | + return json_decode($changesInfo->getData(), true); |
|
71 | + case self::RESPONSE_HAS_CONTENT: |
|
72 | + default: |
|
73 | + $data = $this->extractData($response->getBody()); |
|
74 | + $changesInfo->setData(json_encode($data)); |
|
75 | + $changesInfo->setEtag($response->getHeader('Etag')); |
|
76 | + $this->cacheResult($changesInfo, $version); |
|
77 | 77 | |
78 | - return $data; |
|
79 | - } |
|
80 | - } |
|
78 | + return $data; |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - protected function evaluateResponse(IResponse $response): int { |
|
83 | - if($response->getStatusCode() === 304) { |
|
84 | - return self::RESPONSE_USE_CACHE; |
|
85 | - } else if($response->getStatusCode() === 404) { |
|
86 | - return self::RESPONSE_NO_CONTENT; |
|
87 | - } else if($response->getStatusCode() === 200) { |
|
88 | - return self::RESPONSE_HAS_CONTENT; |
|
89 | - } |
|
90 | - $this->logger->debug('Unexpected return code {code} from changelog server', [ |
|
91 | - 'app' => 'core', |
|
92 | - 'code' => $response->getStatusCode(), |
|
93 | - ]); |
|
94 | - return self::RESPONSE_NO_CONTENT; |
|
95 | - } |
|
82 | + protected function evaluateResponse(IResponse $response): int { |
|
83 | + if($response->getStatusCode() === 304) { |
|
84 | + return self::RESPONSE_USE_CACHE; |
|
85 | + } else if($response->getStatusCode() === 404) { |
|
86 | + return self::RESPONSE_NO_CONTENT; |
|
87 | + } else if($response->getStatusCode() === 200) { |
|
88 | + return self::RESPONSE_HAS_CONTENT; |
|
89 | + } |
|
90 | + $this->logger->debug('Unexpected return code {code} from changelog server', [ |
|
91 | + 'app' => 'core', |
|
92 | + 'code' => $response->getStatusCode(), |
|
93 | + ]); |
|
94 | + return self::RESPONSE_NO_CONTENT; |
|
95 | + } |
|
96 | 96 | |
97 | - protected function cacheResult(ChangesResult $entry, string $version) { |
|
98 | - if($entry->getVersion() === $version) { |
|
99 | - $this->mapper->update($entry); |
|
100 | - } else { |
|
101 | - $entry->setVersion($version); |
|
102 | - $this->mapper->insert($entry); |
|
103 | - } |
|
104 | - } |
|
97 | + protected function cacheResult(ChangesResult $entry, string $version) { |
|
98 | + if($entry->getVersion() === $version) { |
|
99 | + $this->mapper->update($entry); |
|
100 | + } else { |
|
101 | + $entry->setVersion($version); |
|
102 | + $this->mapper->insert($entry); |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @throws \Exception |
|
108 | - */ |
|
109 | - protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse { |
|
110 | - $headers = []; |
|
111 | - if($entry->getEtag() !== '') { |
|
112 | - $headers['If-None-Match'] = [$entry->getEtag()]; |
|
113 | - } |
|
106 | + /** |
|
107 | + * @throws \Exception |
|
108 | + */ |
|
109 | + protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse { |
|
110 | + $headers = []; |
|
111 | + if($entry->getEtag() !== '') { |
|
112 | + $headers['If-None-Match'] = [$entry->getEtag()]; |
|
113 | + } |
|
114 | 114 | |
115 | - $entry->setLastCheck(time()); |
|
116 | - $client = $this->clientService->newClient(); |
|
117 | - return $client->get($uri, [ |
|
118 | - 'headers' => $headers, |
|
119 | - ]); |
|
120 | - } |
|
115 | + $entry->setLastCheck(time()); |
|
116 | + $client = $this->clientService->newClient(); |
|
117 | + return $client->get($uri, [ |
|
118 | + 'headers' => $headers, |
|
119 | + ]); |
|
120 | + } |
|
121 | 121 | |
122 | - protected function extractData($body):array { |
|
123 | - $data = []; |
|
124 | - if ($body) { |
|
125 | - $loadEntities = libxml_disable_entity_loader(true); |
|
126 | - $xml = @simplexml_load_string($body); |
|
127 | - libxml_disable_entity_loader($loadEntities); |
|
128 | - if ($xml !== false) { |
|
129 | - $data['changelogURL'] = (string)$xml->changelog['href']; |
|
130 | - $data['whatsNew'] = []; |
|
131 | - foreach($xml->whatsNew as $infoSet) { |
|
132 | - $data['whatsNew'][(string)$infoSet['lang']] = [ |
|
133 | - 'regular' => (array)$infoSet->regular->item, |
|
134 | - 'admin' => (array)$infoSet->admin->item, |
|
135 | - ]; |
|
136 | - } |
|
137 | - } else { |
|
138 | - libxml_clear_errors(); |
|
139 | - } |
|
140 | - } |
|
141 | - return $data; |
|
142 | - } |
|
122 | + protected function extractData($body):array { |
|
123 | + $data = []; |
|
124 | + if ($body) { |
|
125 | + $loadEntities = libxml_disable_entity_loader(true); |
|
126 | + $xml = @simplexml_load_string($body); |
|
127 | + libxml_disable_entity_loader($loadEntities); |
|
128 | + if ($xml !== false) { |
|
129 | + $data['changelogURL'] = (string)$xml->changelog['href']; |
|
130 | + $data['whatsNew'] = []; |
|
131 | + foreach($xml->whatsNew as $infoSet) { |
|
132 | + $data['whatsNew'][(string)$infoSet['lang']] = [ |
|
133 | + 'regular' => (array)$infoSet->regular->item, |
|
134 | + 'admin' => (array)$infoSet->admin->item, |
|
135 | + ]; |
|
136 | + } |
|
137 | + } else { |
|
138 | + libxml_clear_errors(); |
|
139 | + } |
|
140 | + } |
|
141 | + return $data; |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * returns a x.y.z form of the provided version. Extra numbers will be |
|
146 | - * omitted, missing ones added as zeros. |
|
147 | - */ |
|
148 | - protected function normalizeVersion(string $version): string { |
|
149 | - $versionNumbers = array_slice(explode('.', $version), 0, 3); |
|
150 | - $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input |
|
151 | - while(count($versionNumbers) < 3) { |
|
152 | - // changelog server expects x.y.z, pad 0 if it is too short |
|
153 | - $versionNumbers[] = 0; |
|
154 | - } |
|
155 | - return implode('.', $versionNumbers); |
|
156 | - } |
|
144 | + /** |
|
145 | + * returns a x.y.z form of the provided version. Extra numbers will be |
|
146 | + * omitted, missing ones added as zeros. |
|
147 | + */ |
|
148 | + protected function normalizeVersion(string $version): string { |
|
149 | + $versionNumbers = array_slice(explode('.', $version), 0, 3); |
|
150 | + $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input |
|
151 | + while(count($versionNumbers) < 3) { |
|
152 | + // changelog server expects x.y.z, pad 0 if it is too short |
|
153 | + $versionNumbers[] = 0; |
|
154 | + } |
|
155 | + return implode('.', $versionNumbers); |
|
156 | + } |
|
157 | 157 | } |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | try { |
55 | 55 | $version = $this->normalizeVersion($version); |
56 | 56 | $changesInfo = $this->mapper->getChanges($version); |
57 | - if($changesInfo->getLastCheck() + 1800 > time()) { |
|
57 | + if ($changesInfo->getLastCheck() + 1800 > time()) { |
|
58 | 58 | return json_decode($changesInfo->getData(), true); |
59 | 59 | } |
60 | 60 | } catch (DoesNotExistException $e) { |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | |
64 | 64 | $response = $this->queryChangesServer($uri, $changesInfo); |
65 | 65 | |
66 | - switch($this->evaluateResponse($response)) { |
|
66 | + switch ($this->evaluateResponse($response)) { |
|
67 | 67 | case self::RESPONSE_NO_CONTENT: |
68 | 68 | return []; |
69 | 69 | case self::RESPONSE_USE_CACHE: |
@@ -80,11 +80,11 @@ discard block |
||
80 | 80 | } |
81 | 81 | |
82 | 82 | protected function evaluateResponse(IResponse $response): int { |
83 | - if($response->getStatusCode() === 304) { |
|
83 | + if ($response->getStatusCode() === 304) { |
|
84 | 84 | return self::RESPONSE_USE_CACHE; |
85 | - } else if($response->getStatusCode() === 404) { |
|
85 | + } else if ($response->getStatusCode() === 404) { |
|
86 | 86 | return self::RESPONSE_NO_CONTENT; |
87 | - } else if($response->getStatusCode() === 200) { |
|
87 | + } else if ($response->getStatusCode() === 200) { |
|
88 | 88 | return self::RESPONSE_HAS_CONTENT; |
89 | 89 | } |
90 | 90 | $this->logger->debug('Unexpected return code {code} from changelog server', [ |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | } |
96 | 96 | |
97 | 97 | protected function cacheResult(ChangesResult $entry, string $version) { |
98 | - if($entry->getVersion() === $version) { |
|
98 | + if ($entry->getVersion() === $version) { |
|
99 | 99 | $this->mapper->update($entry); |
100 | 100 | } else { |
101 | 101 | $entry->setVersion($version); |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | */ |
109 | 109 | protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse { |
110 | 110 | $headers = []; |
111 | - if($entry->getEtag() !== '') { |
|
111 | + if ($entry->getEtag() !== '') { |
|
112 | 112 | $headers['If-None-Match'] = [$entry->getEtag()]; |
113 | 113 | } |
114 | 114 | |
@@ -126,12 +126,12 @@ discard block |
||
126 | 126 | $xml = @simplexml_load_string($body); |
127 | 127 | libxml_disable_entity_loader($loadEntities); |
128 | 128 | if ($xml !== false) { |
129 | - $data['changelogURL'] = (string)$xml->changelog['href']; |
|
129 | + $data['changelogURL'] = (string) $xml->changelog['href']; |
|
130 | 130 | $data['whatsNew'] = []; |
131 | - foreach($xml->whatsNew as $infoSet) { |
|
132 | - $data['whatsNew'][(string)$infoSet['lang']] = [ |
|
133 | - 'regular' => (array)$infoSet->regular->item, |
|
134 | - 'admin' => (array)$infoSet->admin->item, |
|
131 | + foreach ($xml->whatsNew as $infoSet) { |
|
132 | + $data['whatsNew'][(string) $infoSet['lang']] = [ |
|
133 | + 'regular' => (array) $infoSet->regular->item, |
|
134 | + 'admin' => (array) $infoSet->admin->item, |
|
135 | 135 | ]; |
136 | 136 | } |
137 | 137 | } else { |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | protected function normalizeVersion(string $version): string { |
149 | 149 | $versionNumbers = array_slice(explode('.', $version), 0, 3); |
150 | 150 | $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input |
151 | - while(count($versionNumbers) < 3) { |
|
151 | + while (count($versionNumbers) < 3) { |
|
152 | 152 | // changelog server expects x.y.z, pad 0 if it is too short |
153 | 153 | $versionNumbers[] = 0; |
154 | 154 | } |
@@ -30,28 +30,28 @@ |
||
30 | 30 | use OCP\IDBConnection; |
31 | 31 | |
32 | 32 | class ChangesMapper extends QBMapper { |
33 | - const TABLE_NAME = 'whats_new'; |
|
33 | + const TABLE_NAME = 'whats_new'; |
|
34 | 34 | |
35 | - public function __construct(IDBConnection $db) { |
|
36 | - parent::__construct($db, self::TABLE_NAME); |
|
37 | - } |
|
35 | + public function __construct(IDBConnection $db) { |
|
36 | + parent::__construct($db, self::TABLE_NAME); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @throws DoesNotExistException |
|
41 | - */ |
|
42 | - public function getChanges(string $version): ChangesResult { |
|
43 | - /* @var $qb IQueryBuilder */ |
|
44 | - $qb = $this->db->getQueryBuilder(); |
|
45 | - $result = $qb->select('*') |
|
46 | - ->from(self::TABLE_NAME) |
|
47 | - ->where($qb->expr()->eq('version', $qb->createNamedParameter($version))) |
|
48 | - ->execute(); |
|
39 | + /** |
|
40 | + * @throws DoesNotExistException |
|
41 | + */ |
|
42 | + public function getChanges(string $version): ChangesResult { |
|
43 | + /* @var $qb IQueryBuilder */ |
|
44 | + $qb = $this->db->getQueryBuilder(); |
|
45 | + $result = $qb->select('*') |
|
46 | + ->from(self::TABLE_NAME) |
|
47 | + ->where($qb->expr()->eq('version', $qb->createNamedParameter($version))) |
|
48 | + ->execute(); |
|
49 | 49 | |
50 | - $data = $result->fetch(); |
|
51 | - $result->closeCursor(); |
|
52 | - if ($data === false) { |
|
53 | - throw new DoesNotExistException('Changes info is not present'); |
|
54 | - } |
|
55 | - return ChangesResult::fromRow($data); |
|
56 | - } |
|
50 | + $data = $result->fetch(); |
|
51 | + $result->closeCursor(); |
|
52 | + if ($data === false) { |
|
53 | + throw new DoesNotExistException('Changes info is not present'); |
|
54 | + } |
|
55 | + return ChangesResult::fromRow($data); |
|
56 | + } |
|
57 | 57 | } |
@@ -31,95 +31,95 @@ |
||
31 | 31 | |
32 | 32 | class VersionCheck { |
33 | 33 | |
34 | - /** @var IClientService */ |
|
35 | - private $clientService; |
|
34 | + /** @var IClientService */ |
|
35 | + private $clientService; |
|
36 | 36 | |
37 | - /** @var IConfig */ |
|
38 | - private $config; |
|
39 | - |
|
40 | - /** |
|
41 | - * @param IClientService $clientService |
|
42 | - * @param IConfig $config |
|
43 | - */ |
|
44 | - public function __construct(IClientService $clientService, |
|
45 | - IConfig $config) { |
|
46 | - $this->clientService = $clientService; |
|
47 | - $this->config = $config; |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Check if a new version is available |
|
53 | - * |
|
54 | - * @return array|bool |
|
55 | - */ |
|
56 | - public function check() { |
|
57 | - // Look up the cache - it is invalidated all 30 minutes |
|
58 | - if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
59 | - return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true); |
|
60 | - } |
|
61 | - |
|
62 | - $updaterUrl = $this->config->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/'); |
|
63 | - |
|
64 | - $this->config->setAppValue('core', 'lastupdatedat', time()); |
|
65 | - |
|
66 | - if ($this->config->getAppValue('core', 'installedat', '') === '') { |
|
67 | - $this->config->setAppValue('core', 'installedat', microtime(true)); |
|
68 | - } |
|
69 | - |
|
70 | - $version = Util::getVersion(); |
|
71 | - $version['installed'] = $this->config->getAppValue('core', 'installedat'); |
|
72 | - $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat'); |
|
73 | - $version['updatechannel'] = \OC_Util::getChannel(); |
|
74 | - $version['edition'] = ''; |
|
75 | - $version['build'] = \OC_Util::getBuild(); |
|
76 | - $version['php_major'] = PHP_MAJOR_VERSION; |
|
77 | - $version['php_minor'] = PHP_MINOR_VERSION; |
|
78 | - $version['php_release'] = PHP_RELEASE_VERSION; |
|
79 | - $versionString = implode('x', $version); |
|
80 | - |
|
81 | - //fetch xml data from updater |
|
82 | - $url = $updaterUrl . '?version=' . $versionString; |
|
83 | - |
|
84 | - $tmp = []; |
|
85 | - try { |
|
86 | - $xml = $this->getUrlContent($url); |
|
87 | - } catch (\Exception $e) { |
|
88 | - return false; |
|
89 | - } |
|
90 | - |
|
91 | - if ($xml) { |
|
92 | - $loadEntities = libxml_disable_entity_loader(true); |
|
93 | - $data = @simplexml_load_string($xml); |
|
94 | - libxml_disable_entity_loader($loadEntities); |
|
95 | - if ($data !== false) { |
|
96 | - $tmp['version'] = (string)$data->version; |
|
97 | - $tmp['versionstring'] = (string)$data->versionstring; |
|
98 | - $tmp['url'] = (string)$data->url; |
|
99 | - $tmp['web'] = (string)$data->web; |
|
100 | - $tmp['changes'] = isset($data->changes) ? (string)$data->changes : ''; |
|
101 | - $tmp['autoupdater'] = (string)$data->autoupdater; |
|
102 | - $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0'; |
|
103 | - } else { |
|
104 | - libxml_clear_errors(); |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - // Cache the result |
|
109 | - $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp)); |
|
110 | - return $tmp; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @codeCoverageIgnore |
|
115 | - * @param string $url |
|
116 | - * @return resource|string |
|
117 | - * @throws \Exception |
|
118 | - */ |
|
119 | - protected function getUrlContent($url) { |
|
120 | - $client = $this->clientService->newClient(); |
|
121 | - $response = $client->get($url); |
|
122 | - return $response->getBody(); |
|
123 | - } |
|
37 | + /** @var IConfig */ |
|
38 | + private $config; |
|
39 | + |
|
40 | + /** |
|
41 | + * @param IClientService $clientService |
|
42 | + * @param IConfig $config |
|
43 | + */ |
|
44 | + public function __construct(IClientService $clientService, |
|
45 | + IConfig $config) { |
|
46 | + $this->clientService = $clientService; |
|
47 | + $this->config = $config; |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Check if a new version is available |
|
53 | + * |
|
54 | + * @return array|bool |
|
55 | + */ |
|
56 | + public function check() { |
|
57 | + // Look up the cache - it is invalidated all 30 minutes |
|
58 | + if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
59 | + return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true); |
|
60 | + } |
|
61 | + |
|
62 | + $updaterUrl = $this->config->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/'); |
|
63 | + |
|
64 | + $this->config->setAppValue('core', 'lastupdatedat', time()); |
|
65 | + |
|
66 | + if ($this->config->getAppValue('core', 'installedat', '') === '') { |
|
67 | + $this->config->setAppValue('core', 'installedat', microtime(true)); |
|
68 | + } |
|
69 | + |
|
70 | + $version = Util::getVersion(); |
|
71 | + $version['installed'] = $this->config->getAppValue('core', 'installedat'); |
|
72 | + $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat'); |
|
73 | + $version['updatechannel'] = \OC_Util::getChannel(); |
|
74 | + $version['edition'] = ''; |
|
75 | + $version['build'] = \OC_Util::getBuild(); |
|
76 | + $version['php_major'] = PHP_MAJOR_VERSION; |
|
77 | + $version['php_minor'] = PHP_MINOR_VERSION; |
|
78 | + $version['php_release'] = PHP_RELEASE_VERSION; |
|
79 | + $versionString = implode('x', $version); |
|
80 | + |
|
81 | + //fetch xml data from updater |
|
82 | + $url = $updaterUrl . '?version=' . $versionString; |
|
83 | + |
|
84 | + $tmp = []; |
|
85 | + try { |
|
86 | + $xml = $this->getUrlContent($url); |
|
87 | + } catch (\Exception $e) { |
|
88 | + return false; |
|
89 | + } |
|
90 | + |
|
91 | + if ($xml) { |
|
92 | + $loadEntities = libxml_disable_entity_loader(true); |
|
93 | + $data = @simplexml_load_string($xml); |
|
94 | + libxml_disable_entity_loader($loadEntities); |
|
95 | + if ($data !== false) { |
|
96 | + $tmp['version'] = (string)$data->version; |
|
97 | + $tmp['versionstring'] = (string)$data->versionstring; |
|
98 | + $tmp['url'] = (string)$data->url; |
|
99 | + $tmp['web'] = (string)$data->web; |
|
100 | + $tmp['changes'] = isset($data->changes) ? (string)$data->changes : ''; |
|
101 | + $tmp['autoupdater'] = (string)$data->autoupdater; |
|
102 | + $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0'; |
|
103 | + } else { |
|
104 | + libxml_clear_errors(); |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + // Cache the result |
|
109 | + $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp)); |
|
110 | + return $tmp; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @codeCoverageIgnore |
|
115 | + * @param string $url |
|
116 | + * @return resource|string |
|
117 | + * @throws \Exception |
|
118 | + */ |
|
119 | + protected function getUrlContent($url) { |
|
120 | + $client = $this->clientService->newClient(); |
|
121 | + $response = $client->get($url); |
|
122 | + return $response->getBody(); |
|
123 | + } |
|
124 | 124 | } |
125 | 125 |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | */ |
56 | 56 | public function check() { |
57 | 57 | // Look up the cache - it is invalidated all 30 minutes |
58 | - if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
58 | + if (((int) $this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
59 | 59 | return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true); |
60 | 60 | } |
61 | 61 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | $versionString = implode('x', $version); |
80 | 80 | |
81 | 81 | //fetch xml data from updater |
82 | - $url = $updaterUrl . '?version=' . $versionString; |
|
82 | + $url = $updaterUrl.'?version='.$versionString; |
|
83 | 83 | |
84 | 84 | $tmp = []; |
85 | 85 | try { |
@@ -93,13 +93,13 @@ discard block |
||
93 | 93 | $data = @simplexml_load_string($xml); |
94 | 94 | libxml_disable_entity_loader($loadEntities); |
95 | 95 | if ($data !== false) { |
96 | - $tmp['version'] = (string)$data->version; |
|
97 | - $tmp['versionstring'] = (string)$data->versionstring; |
|
98 | - $tmp['url'] = (string)$data->url; |
|
99 | - $tmp['web'] = (string)$data->web; |
|
100 | - $tmp['changes'] = isset($data->changes) ? (string)$data->changes : ''; |
|
101 | - $tmp['autoupdater'] = (string)$data->autoupdater; |
|
102 | - $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0'; |
|
96 | + $tmp['version'] = (string) $data->version; |
|
97 | + $tmp['versionstring'] = (string) $data->versionstring; |
|
98 | + $tmp['url'] = (string) $data->url; |
|
99 | + $tmp['web'] = (string) $data->web; |
|
100 | + $tmp['changes'] = isset($data->changes) ? (string) $data->changes : ''; |
|
101 | + $tmp['autoupdater'] = (string) $data->autoupdater; |
|
102 | + $tmp['eol'] = isset($data->eol) ? (string) $data->eol : '0'; |
|
103 | 103 | } else { |
104 | 104 | libxml_clear_errors(); |
105 | 105 | } |
@@ -40,22 +40,22 @@ |
||
40 | 40 | * @method void setData(string $data) |
41 | 41 | */ |
42 | 42 | class ChangesResult extends Entity { |
43 | - /** @var string */ |
|
44 | - protected $version = ''; |
|
43 | + /** @var string */ |
|
44 | + protected $version = ''; |
|
45 | 45 | |
46 | - /** @var string */ |
|
47 | - protected $etag = ''; |
|
46 | + /** @var string */ |
|
47 | + protected $etag = ''; |
|
48 | 48 | |
49 | - /** @var int */ |
|
50 | - protected $lastCheck = 0; |
|
49 | + /** @var int */ |
|
50 | + protected $lastCheck = 0; |
|
51 | 51 | |
52 | - /** @var string */ |
|
53 | - protected $data = ''; |
|
52 | + /** @var string */ |
|
53 | + protected $data = ''; |
|
54 | 54 | |
55 | - public function __construct() { |
|
56 | - $this->addType('version', 'string'); |
|
57 | - $this->addType('etag', 'string'); |
|
58 | - $this->addType('lastCheck', 'int'); |
|
59 | - $this->addType('data', 'string'); |
|
60 | - } |
|
55 | + public function __construct() { |
|
56 | + $this->addType('version', 'string'); |
|
57 | + $this->addType('etag', 'string'); |
|
58 | + $this->addType('lastCheck', 'int'); |
|
59 | + $this->addType('data', 'string'); |
|
60 | + } |
|
61 | 61 | } |
@@ -41,516 +41,516 @@ |
||
41 | 41 | */ |
42 | 42 | class Factory implements IFactory { |
43 | 43 | |
44 | - /** @var string */ |
|
45 | - protected $requestLanguage = ''; |
|
46 | - |
|
47 | - /** |
|
48 | - * cached instances |
|
49 | - * @var array Structure: Lang => App => \OCP\IL10N |
|
50 | - */ |
|
51 | - protected $instances = []; |
|
52 | - |
|
53 | - /** |
|
54 | - * @var array Structure: App => string[] |
|
55 | - */ |
|
56 | - protected $availableLanguages = []; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var array Structure: string => callable |
|
60 | - */ |
|
61 | - protected $pluralFunctions = []; |
|
62 | - |
|
63 | - const COMMON_LANGUAGE_CODES = [ |
|
64 | - 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
|
65 | - 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
|
66 | - ]; |
|
67 | - |
|
68 | - /** @var IConfig */ |
|
69 | - protected $config; |
|
70 | - |
|
71 | - /** @var IRequest */ |
|
72 | - protected $request; |
|
73 | - |
|
74 | - /** @var IUserSession */ |
|
75 | - protected $userSession; |
|
76 | - |
|
77 | - /** @var string */ |
|
78 | - protected $serverRoot; |
|
79 | - |
|
80 | - /** |
|
81 | - * @param IConfig $config |
|
82 | - * @param IRequest $request |
|
83 | - * @param IUserSession $userSession |
|
84 | - * @param string $serverRoot |
|
85 | - */ |
|
86 | - public function __construct(IConfig $config, |
|
87 | - IRequest $request, |
|
88 | - IUserSession $userSession, |
|
89 | - $serverRoot) { |
|
90 | - $this->config = $config; |
|
91 | - $this->request = $request; |
|
92 | - $this->userSession = $userSession; |
|
93 | - $this->serverRoot = $serverRoot; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get a language instance |
|
98 | - * |
|
99 | - * @param string $app |
|
100 | - * @param string|null $lang |
|
101 | - * @return \OCP\IL10N |
|
102 | - */ |
|
103 | - public function get($app, $lang = null) { |
|
104 | - $app = \OC_App::cleanAppId($app); |
|
105 | - if ($lang !== null) { |
|
106 | - $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
107 | - } |
|
108 | - |
|
109 | - $forceLang = $this->config->getSystemValue('force_language', false); |
|
110 | - if (is_string($forceLang)) { |
|
111 | - $lang = $forceLang; |
|
112 | - } |
|
113 | - |
|
114 | - if ($lang === null || !$this->languageExists($app, $lang)) { |
|
115 | - $lang = $this->findLanguage($app); |
|
116 | - } |
|
117 | - |
|
118 | - if (!isset($this->instances[$lang][$app])) { |
|
119 | - $this->instances[$lang][$app] = new L10N( |
|
120 | - $this, $app, $lang, |
|
121 | - $this->getL10nFilesForApp($app, $lang) |
|
122 | - ); |
|
123 | - } |
|
124 | - |
|
125 | - return $this->instances[$lang][$app]; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Find the best language |
|
130 | - * |
|
131 | - * @param string|null $app App id or null for core |
|
132 | - * @return string language If nothing works it returns 'en' |
|
133 | - */ |
|
134 | - public function findLanguage($app = null) { |
|
135 | - $forceLang = $this->config->getSystemValue('force_language', false); |
|
136 | - if (is_string($forceLang)) { |
|
137 | - $this->requestLanguage = $forceLang; |
|
138 | - } |
|
139 | - |
|
140 | - if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
141 | - return $this->requestLanguage; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * At this point Nextcloud might not yet be installed and thus the lookup |
|
146 | - * in the preferences table might fail. For this reason we need to check |
|
147 | - * whether the instance has already been installed |
|
148 | - * |
|
149 | - * @link https://github.com/owncloud/core/issues/21955 |
|
150 | - */ |
|
151 | - if ($this->config->getSystemValue('installed', false)) { |
|
152 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
153 | - if (!is_null($userId)) { |
|
154 | - $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
155 | - } else { |
|
156 | - $userLang = null; |
|
157 | - } |
|
158 | - } else { |
|
159 | - $userId = null; |
|
160 | - $userLang = null; |
|
161 | - } |
|
162 | - |
|
163 | - if ($userLang) { |
|
164 | - $this->requestLanguage = $userLang; |
|
165 | - if ($this->languageExists($app, $userLang)) { |
|
166 | - return $userLang; |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - try { |
|
171 | - // Try to get the language from the Request |
|
172 | - $lang = $this->getLanguageFromRequest($app); |
|
173 | - if ($userId !== null && $app === null && !$userLang) { |
|
174 | - $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
175 | - } |
|
176 | - return $lang; |
|
177 | - } catch (LanguageNotFoundException $e) { |
|
178 | - // Finding language from request failed fall back to default language |
|
179 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
180 | - if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
181 | - return $defaultLanguage; |
|
182 | - } |
|
183 | - } |
|
184 | - |
|
185 | - // We could not find any language so fall back to english |
|
186 | - return 'en'; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Find all available languages for an app |
|
191 | - * |
|
192 | - * @param string|null $app App id or null for core |
|
193 | - * @return array an array of available languages |
|
194 | - */ |
|
195 | - public function findAvailableLanguages($app = null) { |
|
196 | - $key = $app; |
|
197 | - if ($key === null) { |
|
198 | - $key = 'null'; |
|
199 | - } |
|
200 | - |
|
201 | - // also works with null as key |
|
202 | - if (!empty($this->availableLanguages[$key])) { |
|
203 | - return $this->availableLanguages[$key]; |
|
204 | - } |
|
205 | - |
|
206 | - $available = ['en']; //english is always available |
|
207 | - $dir = $this->findL10nDir($app); |
|
208 | - if (is_dir($dir)) { |
|
209 | - $files = scandir($dir); |
|
210 | - if ($files !== false) { |
|
211 | - foreach ($files as $file) { |
|
212 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
213 | - $available[] = substr($file, 0, -5); |
|
214 | - } |
|
215 | - } |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - // merge with translations from theme |
|
220 | - $theme = $this->config->getSystemValue('theme'); |
|
221 | - if (!empty($theme)) { |
|
222 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
223 | - |
|
224 | - if (is_dir($themeDir)) { |
|
225 | - $files = scandir($themeDir); |
|
226 | - if ($files !== false) { |
|
227 | - foreach ($files as $file) { |
|
228 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
229 | - $available[] = substr($file, 0, -5); |
|
230 | - } |
|
231 | - } |
|
232 | - } |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - $this->availableLanguages[$key] = $available; |
|
237 | - return $available; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param string|null $app App id or null for core |
|
242 | - * @param string $lang |
|
243 | - * @return bool |
|
244 | - */ |
|
245 | - public function languageExists($app, $lang) { |
|
246 | - if ($lang === 'en') {//english is always available |
|
247 | - return true; |
|
248 | - } |
|
249 | - |
|
250 | - $languages = $this->findAvailableLanguages($app); |
|
251 | - return array_search($lang, $languages) !== false; |
|
252 | - } |
|
253 | - |
|
254 | - public function iterateLanguage(bool $reset = false): string { |
|
255 | - static $i = 0; |
|
256 | - if($reset) { |
|
257 | - $i = 0; |
|
258 | - } |
|
259 | - switch($i) { |
|
260 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
261 | - case 0: |
|
262 | - $i++; |
|
263 | - $forcedLang = $this->config->getSystemValue('force_language', false); |
|
264 | - if(is_string($forcedLang)) { |
|
265 | - return $forcedLang; |
|
266 | - } |
|
267 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
268 | - case 1: |
|
269 | - $i++; |
|
270 | - $user = $this->userSession->getUser(); |
|
271 | - if($user instanceof IUser) { |
|
272 | - $userLang = $this->config->getUserValue($user->getUID(), 'core', 'lang', null); |
|
273 | - if(is_string($userLang)) { |
|
274 | - return $userLang; |
|
275 | - } |
|
276 | - } |
|
277 | - case 2: |
|
278 | - $i++; |
|
279 | - return $this->config->getSystemValue('default_language', 'en'); |
|
280 | - default: |
|
281 | - return 'en'; |
|
282 | - } |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * @param string|null $app |
|
287 | - * @return string |
|
288 | - * @throws LanguageNotFoundException |
|
289 | - */ |
|
290 | - private function getLanguageFromRequest($app) { |
|
291 | - $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
292 | - if ($header !== '') { |
|
293 | - $available = $this->findAvailableLanguages($app); |
|
294 | - |
|
295 | - // E.g. make sure that 'de' is before 'de_DE'. |
|
296 | - sort($available); |
|
297 | - |
|
298 | - $preferences = preg_split('/,\s*/', strtolower($header)); |
|
299 | - foreach ($preferences as $preference) { |
|
300 | - list($preferred_language) = explode(';', $preference); |
|
301 | - $preferred_language = str_replace('-', '_', $preferred_language); |
|
302 | - |
|
303 | - foreach ($available as $available_language) { |
|
304 | - if ($preferred_language === strtolower($available_language)) { |
|
305 | - return $this->respectDefaultLanguage($app, $available_language); |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - // Fallback from de_De to de |
|
310 | - foreach ($available as $available_language) { |
|
311 | - if (substr($preferred_language, 0, 2) === $available_language) { |
|
312 | - return $available_language; |
|
313 | - } |
|
314 | - } |
|
315 | - } |
|
316 | - } |
|
317 | - |
|
318 | - throw new LanguageNotFoundException(); |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * if default language is set to de_DE (formal German) this should be |
|
323 | - * preferred to 'de' (non-formal German) if possible |
|
324 | - * |
|
325 | - * @param string|null $app |
|
326 | - * @param string $lang |
|
327 | - * @return string |
|
328 | - */ |
|
329 | - protected function respectDefaultLanguage($app, $lang) { |
|
330 | - $result = $lang; |
|
331 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
332 | - |
|
333 | - // use formal version of german ("Sie" instead of "Du") if the default |
|
334 | - // language is set to 'de_DE' if possible |
|
335 | - if (is_string($defaultLanguage) && |
|
336 | - strtolower($lang) === 'de' && |
|
337 | - strtolower($defaultLanguage) === 'de_de' && |
|
338 | - $this->languageExists($app, 'de_DE') |
|
339 | - ) { |
|
340 | - $result = 'de_DE'; |
|
341 | - } |
|
342 | - |
|
343 | - return $result; |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Checks if $sub is a subdirectory of $parent |
|
348 | - * |
|
349 | - * @param string $sub |
|
350 | - * @param string $parent |
|
351 | - * @return bool |
|
352 | - */ |
|
353 | - private function isSubDirectory($sub, $parent) { |
|
354 | - // Check whether $sub contains no ".." |
|
355 | - if (strpos($sub, '..') !== false) { |
|
356 | - return false; |
|
357 | - } |
|
358 | - |
|
359 | - // Check whether $sub is a subdirectory of $parent |
|
360 | - if (strpos($sub, $parent) === 0) { |
|
361 | - return true; |
|
362 | - } |
|
363 | - |
|
364 | - return false; |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * Get a list of language files that should be loaded |
|
369 | - * |
|
370 | - * @param string $app |
|
371 | - * @param string $lang |
|
372 | - * @return string[] |
|
373 | - */ |
|
374 | - // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
375 | - // FIXME This is also the reason, why it is not in the public interface |
|
376 | - public function getL10nFilesForApp($app, $lang) { |
|
377 | - $languageFiles = []; |
|
378 | - |
|
379 | - $i18nDir = $this->findL10nDir($app); |
|
380 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
381 | - |
|
382 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
383 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
384 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
385 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
386 | - ) |
|
387 | - && file_exists($transFile)) { |
|
388 | - // load the translations file |
|
389 | - $languageFiles[] = $transFile; |
|
390 | - } |
|
391 | - |
|
392 | - // merge with translations from theme |
|
393 | - $theme = $this->config->getSystemValue('theme'); |
|
394 | - if (!empty($theme)) { |
|
395 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
396 | - if (file_exists($transFile)) { |
|
397 | - $languageFiles[] = $transFile; |
|
398 | - } |
|
399 | - } |
|
400 | - |
|
401 | - return $languageFiles; |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * find the l10n directory |
|
406 | - * |
|
407 | - * @param string $app App id or empty string for core |
|
408 | - * @return string directory |
|
409 | - */ |
|
410 | - protected function findL10nDir($app = null) { |
|
411 | - if (in_array($app, ['core', 'lib', 'settings'])) { |
|
412 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
413 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
414 | - } |
|
415 | - } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
416 | - // Check if the app is in the app folder |
|
417 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
418 | - } |
|
419 | - return $this->serverRoot . '/core/l10n/'; |
|
420 | - } |
|
421 | - |
|
422 | - |
|
423 | - /** |
|
424 | - * Creates a function from the plural string |
|
425 | - * |
|
426 | - * Parts of the code is copied from Habari: |
|
427 | - * https://github.com/habari/system/blob/master/classes/locale.php |
|
428 | - * @param string $string |
|
429 | - * @return string |
|
430 | - */ |
|
431 | - public function createPluralFunction($string) { |
|
432 | - if (isset($this->pluralFunctions[$string])) { |
|
433 | - return $this->pluralFunctions[$string]; |
|
434 | - } |
|
435 | - |
|
436 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
437 | - // sanitize |
|
438 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
439 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
440 | - |
|
441 | - $body = str_replace( |
|
442 | - array( 'plural', 'n', '$n$plurals', ), |
|
443 | - array( '$plural', '$n', '$nplurals', ), |
|
444 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
445 | - ); |
|
446 | - |
|
447 | - // add parents |
|
448 | - // important since PHP's ternary evaluates from left to right |
|
449 | - $body .= ';'; |
|
450 | - $res = ''; |
|
451 | - $p = 0; |
|
452 | - $length = strlen($body); |
|
453 | - for($i = 0; $i < $length; $i++) { |
|
454 | - $ch = $body[$i]; |
|
455 | - switch ( $ch ) { |
|
456 | - case '?': |
|
457 | - $res .= ' ? ('; |
|
458 | - $p++; |
|
459 | - break; |
|
460 | - case ':': |
|
461 | - $res .= ') : ('; |
|
462 | - break; |
|
463 | - case ';': |
|
464 | - $res .= str_repeat( ')', $p ) . ';'; |
|
465 | - $p = 0; |
|
466 | - break; |
|
467 | - default: |
|
468 | - $res .= $ch; |
|
469 | - } |
|
470 | - } |
|
471 | - |
|
472 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
473 | - $function = create_function('$n', $body); |
|
474 | - $this->pluralFunctions[$string] = $function; |
|
475 | - return $function; |
|
476 | - } else { |
|
477 | - // default: one plural form for all cases but n==1 (english) |
|
478 | - $function = create_function( |
|
479 | - '$n', |
|
480 | - '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
481 | - ); |
|
482 | - $this->pluralFunctions[$string] = $function; |
|
483 | - return $function; |
|
484 | - } |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * returns the common language and other languages in an |
|
489 | - * associative array |
|
490 | - * |
|
491 | - * @return array |
|
492 | - */ |
|
493 | - public function getLanguages() { |
|
494 | - $forceLanguage = $this->config->getSystemValue('force_language', false); |
|
495 | - if ($forceLanguage !== false) { |
|
496 | - return []; |
|
497 | - } |
|
498 | - |
|
499 | - $languageCodes = $this->findAvailableLanguages(); |
|
500 | - |
|
501 | - $commonLanguages = []; |
|
502 | - $languages = []; |
|
503 | - |
|
504 | - foreach($languageCodes as $lang) { |
|
505 | - $l = $this->get('lib', $lang); |
|
506 | - // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
|
507 | - $potentialName = (string) $l->t('__language_name__'); |
|
508 | - if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
509 | - $ln = array( |
|
510 | - 'code' => $lang, |
|
511 | - 'name' => $potentialName |
|
512 | - ); |
|
513 | - } else if ($lang === 'en') { |
|
514 | - $ln = array( |
|
515 | - 'code' => $lang, |
|
516 | - 'name' => 'English (US)' |
|
517 | - ); |
|
518 | - } else {//fallback to language code |
|
519 | - $ln = array( |
|
520 | - 'code' => $lang, |
|
521 | - 'name' => $lang |
|
522 | - ); |
|
523 | - } |
|
524 | - |
|
525 | - // put appropriate languages into appropriate arrays, to print them sorted |
|
526 | - // common languages -> divider -> other languages |
|
527 | - if (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
|
528 | - $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln; |
|
529 | - } else { |
|
530 | - $languages[] = $ln; |
|
531 | - } |
|
532 | - } |
|
533 | - |
|
534 | - ksort($commonLanguages); |
|
535 | - |
|
536 | - // sort now by displayed language not the iso-code |
|
537 | - usort( $languages, function ($a, $b) { |
|
538 | - if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
539 | - // If a doesn't have a name, but b does, list b before a |
|
540 | - return 1; |
|
541 | - } |
|
542 | - if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
543 | - // If a does have a name, but b doesn't, list a before b |
|
544 | - return -1; |
|
545 | - } |
|
546 | - // Otherwise compare the names |
|
547 | - return strcmp($a['name'], $b['name']); |
|
548 | - }); |
|
549 | - |
|
550 | - return [ |
|
551 | - // reset indexes |
|
552 | - 'commonlanguages' => array_values($commonLanguages), |
|
553 | - 'languages' => $languages |
|
554 | - ]; |
|
555 | - } |
|
44 | + /** @var string */ |
|
45 | + protected $requestLanguage = ''; |
|
46 | + |
|
47 | + /** |
|
48 | + * cached instances |
|
49 | + * @var array Structure: Lang => App => \OCP\IL10N |
|
50 | + */ |
|
51 | + protected $instances = []; |
|
52 | + |
|
53 | + /** |
|
54 | + * @var array Structure: App => string[] |
|
55 | + */ |
|
56 | + protected $availableLanguages = []; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var array Structure: string => callable |
|
60 | + */ |
|
61 | + protected $pluralFunctions = []; |
|
62 | + |
|
63 | + const COMMON_LANGUAGE_CODES = [ |
|
64 | + 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
|
65 | + 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
|
66 | + ]; |
|
67 | + |
|
68 | + /** @var IConfig */ |
|
69 | + protected $config; |
|
70 | + |
|
71 | + /** @var IRequest */ |
|
72 | + protected $request; |
|
73 | + |
|
74 | + /** @var IUserSession */ |
|
75 | + protected $userSession; |
|
76 | + |
|
77 | + /** @var string */ |
|
78 | + protected $serverRoot; |
|
79 | + |
|
80 | + /** |
|
81 | + * @param IConfig $config |
|
82 | + * @param IRequest $request |
|
83 | + * @param IUserSession $userSession |
|
84 | + * @param string $serverRoot |
|
85 | + */ |
|
86 | + public function __construct(IConfig $config, |
|
87 | + IRequest $request, |
|
88 | + IUserSession $userSession, |
|
89 | + $serverRoot) { |
|
90 | + $this->config = $config; |
|
91 | + $this->request = $request; |
|
92 | + $this->userSession = $userSession; |
|
93 | + $this->serverRoot = $serverRoot; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get a language instance |
|
98 | + * |
|
99 | + * @param string $app |
|
100 | + * @param string|null $lang |
|
101 | + * @return \OCP\IL10N |
|
102 | + */ |
|
103 | + public function get($app, $lang = null) { |
|
104 | + $app = \OC_App::cleanAppId($app); |
|
105 | + if ($lang !== null) { |
|
106 | + $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
107 | + } |
|
108 | + |
|
109 | + $forceLang = $this->config->getSystemValue('force_language', false); |
|
110 | + if (is_string($forceLang)) { |
|
111 | + $lang = $forceLang; |
|
112 | + } |
|
113 | + |
|
114 | + if ($lang === null || !$this->languageExists($app, $lang)) { |
|
115 | + $lang = $this->findLanguage($app); |
|
116 | + } |
|
117 | + |
|
118 | + if (!isset($this->instances[$lang][$app])) { |
|
119 | + $this->instances[$lang][$app] = new L10N( |
|
120 | + $this, $app, $lang, |
|
121 | + $this->getL10nFilesForApp($app, $lang) |
|
122 | + ); |
|
123 | + } |
|
124 | + |
|
125 | + return $this->instances[$lang][$app]; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Find the best language |
|
130 | + * |
|
131 | + * @param string|null $app App id or null for core |
|
132 | + * @return string language If nothing works it returns 'en' |
|
133 | + */ |
|
134 | + public function findLanguage($app = null) { |
|
135 | + $forceLang = $this->config->getSystemValue('force_language', false); |
|
136 | + if (is_string($forceLang)) { |
|
137 | + $this->requestLanguage = $forceLang; |
|
138 | + } |
|
139 | + |
|
140 | + if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
141 | + return $this->requestLanguage; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * At this point Nextcloud might not yet be installed and thus the lookup |
|
146 | + * in the preferences table might fail. For this reason we need to check |
|
147 | + * whether the instance has already been installed |
|
148 | + * |
|
149 | + * @link https://github.com/owncloud/core/issues/21955 |
|
150 | + */ |
|
151 | + if ($this->config->getSystemValue('installed', false)) { |
|
152 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
153 | + if (!is_null($userId)) { |
|
154 | + $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
155 | + } else { |
|
156 | + $userLang = null; |
|
157 | + } |
|
158 | + } else { |
|
159 | + $userId = null; |
|
160 | + $userLang = null; |
|
161 | + } |
|
162 | + |
|
163 | + if ($userLang) { |
|
164 | + $this->requestLanguage = $userLang; |
|
165 | + if ($this->languageExists($app, $userLang)) { |
|
166 | + return $userLang; |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + try { |
|
171 | + // Try to get the language from the Request |
|
172 | + $lang = $this->getLanguageFromRequest($app); |
|
173 | + if ($userId !== null && $app === null && !$userLang) { |
|
174 | + $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
175 | + } |
|
176 | + return $lang; |
|
177 | + } catch (LanguageNotFoundException $e) { |
|
178 | + // Finding language from request failed fall back to default language |
|
179 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
180 | + if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
181 | + return $defaultLanguage; |
|
182 | + } |
|
183 | + } |
|
184 | + |
|
185 | + // We could not find any language so fall back to english |
|
186 | + return 'en'; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Find all available languages for an app |
|
191 | + * |
|
192 | + * @param string|null $app App id or null for core |
|
193 | + * @return array an array of available languages |
|
194 | + */ |
|
195 | + public function findAvailableLanguages($app = null) { |
|
196 | + $key = $app; |
|
197 | + if ($key === null) { |
|
198 | + $key = 'null'; |
|
199 | + } |
|
200 | + |
|
201 | + // also works with null as key |
|
202 | + if (!empty($this->availableLanguages[$key])) { |
|
203 | + return $this->availableLanguages[$key]; |
|
204 | + } |
|
205 | + |
|
206 | + $available = ['en']; //english is always available |
|
207 | + $dir = $this->findL10nDir($app); |
|
208 | + if (is_dir($dir)) { |
|
209 | + $files = scandir($dir); |
|
210 | + if ($files !== false) { |
|
211 | + foreach ($files as $file) { |
|
212 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
213 | + $available[] = substr($file, 0, -5); |
|
214 | + } |
|
215 | + } |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + // merge with translations from theme |
|
220 | + $theme = $this->config->getSystemValue('theme'); |
|
221 | + if (!empty($theme)) { |
|
222 | + $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
223 | + |
|
224 | + if (is_dir($themeDir)) { |
|
225 | + $files = scandir($themeDir); |
|
226 | + if ($files !== false) { |
|
227 | + foreach ($files as $file) { |
|
228 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
229 | + $available[] = substr($file, 0, -5); |
|
230 | + } |
|
231 | + } |
|
232 | + } |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + $this->availableLanguages[$key] = $available; |
|
237 | + return $available; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param string|null $app App id or null for core |
|
242 | + * @param string $lang |
|
243 | + * @return bool |
|
244 | + */ |
|
245 | + public function languageExists($app, $lang) { |
|
246 | + if ($lang === 'en') {//english is always available |
|
247 | + return true; |
|
248 | + } |
|
249 | + |
|
250 | + $languages = $this->findAvailableLanguages($app); |
|
251 | + return array_search($lang, $languages) !== false; |
|
252 | + } |
|
253 | + |
|
254 | + public function iterateLanguage(bool $reset = false): string { |
|
255 | + static $i = 0; |
|
256 | + if($reset) { |
|
257 | + $i = 0; |
|
258 | + } |
|
259 | + switch($i) { |
|
260 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
261 | + case 0: |
|
262 | + $i++; |
|
263 | + $forcedLang = $this->config->getSystemValue('force_language', false); |
|
264 | + if(is_string($forcedLang)) { |
|
265 | + return $forcedLang; |
|
266 | + } |
|
267 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
268 | + case 1: |
|
269 | + $i++; |
|
270 | + $user = $this->userSession->getUser(); |
|
271 | + if($user instanceof IUser) { |
|
272 | + $userLang = $this->config->getUserValue($user->getUID(), 'core', 'lang', null); |
|
273 | + if(is_string($userLang)) { |
|
274 | + return $userLang; |
|
275 | + } |
|
276 | + } |
|
277 | + case 2: |
|
278 | + $i++; |
|
279 | + return $this->config->getSystemValue('default_language', 'en'); |
|
280 | + default: |
|
281 | + return 'en'; |
|
282 | + } |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * @param string|null $app |
|
287 | + * @return string |
|
288 | + * @throws LanguageNotFoundException |
|
289 | + */ |
|
290 | + private function getLanguageFromRequest($app) { |
|
291 | + $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
292 | + if ($header !== '') { |
|
293 | + $available = $this->findAvailableLanguages($app); |
|
294 | + |
|
295 | + // E.g. make sure that 'de' is before 'de_DE'. |
|
296 | + sort($available); |
|
297 | + |
|
298 | + $preferences = preg_split('/,\s*/', strtolower($header)); |
|
299 | + foreach ($preferences as $preference) { |
|
300 | + list($preferred_language) = explode(';', $preference); |
|
301 | + $preferred_language = str_replace('-', '_', $preferred_language); |
|
302 | + |
|
303 | + foreach ($available as $available_language) { |
|
304 | + if ($preferred_language === strtolower($available_language)) { |
|
305 | + return $this->respectDefaultLanguage($app, $available_language); |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + // Fallback from de_De to de |
|
310 | + foreach ($available as $available_language) { |
|
311 | + if (substr($preferred_language, 0, 2) === $available_language) { |
|
312 | + return $available_language; |
|
313 | + } |
|
314 | + } |
|
315 | + } |
|
316 | + } |
|
317 | + |
|
318 | + throw new LanguageNotFoundException(); |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * if default language is set to de_DE (formal German) this should be |
|
323 | + * preferred to 'de' (non-formal German) if possible |
|
324 | + * |
|
325 | + * @param string|null $app |
|
326 | + * @param string $lang |
|
327 | + * @return string |
|
328 | + */ |
|
329 | + protected function respectDefaultLanguage($app, $lang) { |
|
330 | + $result = $lang; |
|
331 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
332 | + |
|
333 | + // use formal version of german ("Sie" instead of "Du") if the default |
|
334 | + // language is set to 'de_DE' if possible |
|
335 | + if (is_string($defaultLanguage) && |
|
336 | + strtolower($lang) === 'de' && |
|
337 | + strtolower($defaultLanguage) === 'de_de' && |
|
338 | + $this->languageExists($app, 'de_DE') |
|
339 | + ) { |
|
340 | + $result = 'de_DE'; |
|
341 | + } |
|
342 | + |
|
343 | + return $result; |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Checks if $sub is a subdirectory of $parent |
|
348 | + * |
|
349 | + * @param string $sub |
|
350 | + * @param string $parent |
|
351 | + * @return bool |
|
352 | + */ |
|
353 | + private function isSubDirectory($sub, $parent) { |
|
354 | + // Check whether $sub contains no ".." |
|
355 | + if (strpos($sub, '..') !== false) { |
|
356 | + return false; |
|
357 | + } |
|
358 | + |
|
359 | + // Check whether $sub is a subdirectory of $parent |
|
360 | + if (strpos($sub, $parent) === 0) { |
|
361 | + return true; |
|
362 | + } |
|
363 | + |
|
364 | + return false; |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * Get a list of language files that should be loaded |
|
369 | + * |
|
370 | + * @param string $app |
|
371 | + * @param string $lang |
|
372 | + * @return string[] |
|
373 | + */ |
|
374 | + // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
375 | + // FIXME This is also the reason, why it is not in the public interface |
|
376 | + public function getL10nFilesForApp($app, $lang) { |
|
377 | + $languageFiles = []; |
|
378 | + |
|
379 | + $i18nDir = $this->findL10nDir($app); |
|
380 | + $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
381 | + |
|
382 | + if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
383 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
384 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
385 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
386 | + ) |
|
387 | + && file_exists($transFile)) { |
|
388 | + // load the translations file |
|
389 | + $languageFiles[] = $transFile; |
|
390 | + } |
|
391 | + |
|
392 | + // merge with translations from theme |
|
393 | + $theme = $this->config->getSystemValue('theme'); |
|
394 | + if (!empty($theme)) { |
|
395 | + $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
396 | + if (file_exists($transFile)) { |
|
397 | + $languageFiles[] = $transFile; |
|
398 | + } |
|
399 | + } |
|
400 | + |
|
401 | + return $languageFiles; |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * find the l10n directory |
|
406 | + * |
|
407 | + * @param string $app App id or empty string for core |
|
408 | + * @return string directory |
|
409 | + */ |
|
410 | + protected function findL10nDir($app = null) { |
|
411 | + if (in_array($app, ['core', 'lib', 'settings'])) { |
|
412 | + if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
413 | + return $this->serverRoot . '/' . $app . '/l10n/'; |
|
414 | + } |
|
415 | + } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
416 | + // Check if the app is in the app folder |
|
417 | + return \OC_App::getAppPath($app) . '/l10n/'; |
|
418 | + } |
|
419 | + return $this->serverRoot . '/core/l10n/'; |
|
420 | + } |
|
421 | + |
|
422 | + |
|
423 | + /** |
|
424 | + * Creates a function from the plural string |
|
425 | + * |
|
426 | + * Parts of the code is copied from Habari: |
|
427 | + * https://github.com/habari/system/blob/master/classes/locale.php |
|
428 | + * @param string $string |
|
429 | + * @return string |
|
430 | + */ |
|
431 | + public function createPluralFunction($string) { |
|
432 | + if (isset($this->pluralFunctions[$string])) { |
|
433 | + return $this->pluralFunctions[$string]; |
|
434 | + } |
|
435 | + |
|
436 | + if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
437 | + // sanitize |
|
438 | + $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
439 | + $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
440 | + |
|
441 | + $body = str_replace( |
|
442 | + array( 'plural', 'n', '$n$plurals', ), |
|
443 | + array( '$plural', '$n', '$nplurals', ), |
|
444 | + 'nplurals='. $nplurals . '; plural=' . $plural |
|
445 | + ); |
|
446 | + |
|
447 | + // add parents |
|
448 | + // important since PHP's ternary evaluates from left to right |
|
449 | + $body .= ';'; |
|
450 | + $res = ''; |
|
451 | + $p = 0; |
|
452 | + $length = strlen($body); |
|
453 | + for($i = 0; $i < $length; $i++) { |
|
454 | + $ch = $body[$i]; |
|
455 | + switch ( $ch ) { |
|
456 | + case '?': |
|
457 | + $res .= ' ? ('; |
|
458 | + $p++; |
|
459 | + break; |
|
460 | + case ':': |
|
461 | + $res .= ') : ('; |
|
462 | + break; |
|
463 | + case ';': |
|
464 | + $res .= str_repeat( ')', $p ) . ';'; |
|
465 | + $p = 0; |
|
466 | + break; |
|
467 | + default: |
|
468 | + $res .= $ch; |
|
469 | + } |
|
470 | + } |
|
471 | + |
|
472 | + $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
473 | + $function = create_function('$n', $body); |
|
474 | + $this->pluralFunctions[$string] = $function; |
|
475 | + return $function; |
|
476 | + } else { |
|
477 | + // default: one plural form for all cases but n==1 (english) |
|
478 | + $function = create_function( |
|
479 | + '$n', |
|
480 | + '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
481 | + ); |
|
482 | + $this->pluralFunctions[$string] = $function; |
|
483 | + return $function; |
|
484 | + } |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * returns the common language and other languages in an |
|
489 | + * associative array |
|
490 | + * |
|
491 | + * @return array |
|
492 | + */ |
|
493 | + public function getLanguages() { |
|
494 | + $forceLanguage = $this->config->getSystemValue('force_language', false); |
|
495 | + if ($forceLanguage !== false) { |
|
496 | + return []; |
|
497 | + } |
|
498 | + |
|
499 | + $languageCodes = $this->findAvailableLanguages(); |
|
500 | + |
|
501 | + $commonLanguages = []; |
|
502 | + $languages = []; |
|
503 | + |
|
504 | + foreach($languageCodes as $lang) { |
|
505 | + $l = $this->get('lib', $lang); |
|
506 | + // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
|
507 | + $potentialName = (string) $l->t('__language_name__'); |
|
508 | + if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
509 | + $ln = array( |
|
510 | + 'code' => $lang, |
|
511 | + 'name' => $potentialName |
|
512 | + ); |
|
513 | + } else if ($lang === 'en') { |
|
514 | + $ln = array( |
|
515 | + 'code' => $lang, |
|
516 | + 'name' => 'English (US)' |
|
517 | + ); |
|
518 | + } else {//fallback to language code |
|
519 | + $ln = array( |
|
520 | + 'code' => $lang, |
|
521 | + 'name' => $lang |
|
522 | + ); |
|
523 | + } |
|
524 | + |
|
525 | + // put appropriate languages into appropriate arrays, to print them sorted |
|
526 | + // common languages -> divider -> other languages |
|
527 | + if (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
|
528 | + $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln; |
|
529 | + } else { |
|
530 | + $languages[] = $ln; |
|
531 | + } |
|
532 | + } |
|
533 | + |
|
534 | + ksort($commonLanguages); |
|
535 | + |
|
536 | + // sort now by displayed language not the iso-code |
|
537 | + usort( $languages, function ($a, $b) { |
|
538 | + if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
539 | + // If a doesn't have a name, but b does, list b before a |
|
540 | + return 1; |
|
541 | + } |
|
542 | + if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
543 | + // If a does have a name, but b doesn't, list a before b |
|
544 | + return -1; |
|
545 | + } |
|
546 | + // Otherwise compare the names |
|
547 | + return strcmp($a['name'], $b['name']); |
|
548 | + }); |
|
549 | + |
|
550 | + return [ |
|
551 | + // reset indexes |
|
552 | + 'commonlanguages' => array_values($commonLanguages), |
|
553 | + 'languages' => $languages |
|
554 | + ]; |
|
555 | + } |
|
556 | 556 | } |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | * @link https://github.com/owncloud/core/issues/21955 |
150 | 150 | */ |
151 | 151 | if ($this->config->getSystemValue('installed', false)) { |
152 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
152 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
153 | 153 | if (!is_null($userId)) { |
154 | 154 | $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
155 | 155 | } else { |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | // merge with translations from theme |
220 | 220 | $theme = $this->config->getSystemValue('theme'); |
221 | 221 | if (!empty($theme)) { |
222 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
222 | + $themeDir = $this->serverRoot.'/themes/'.$theme.substr($dir, strlen($this->serverRoot)); |
|
223 | 223 | |
224 | 224 | if (is_dir($themeDir)) { |
225 | 225 | $files = scandir($themeDir); |
@@ -253,24 +253,24 @@ discard block |
||
253 | 253 | |
254 | 254 | public function iterateLanguage(bool $reset = false): string { |
255 | 255 | static $i = 0; |
256 | - if($reset) { |
|
256 | + if ($reset) { |
|
257 | 257 | $i = 0; |
258 | 258 | } |
259 | - switch($i) { |
|
259 | + switch ($i) { |
|
260 | 260 | /** @noinspection PhpMissingBreakStatementInspection */ |
261 | 261 | case 0: |
262 | 262 | $i++; |
263 | 263 | $forcedLang = $this->config->getSystemValue('force_language', false); |
264 | - if(is_string($forcedLang)) { |
|
264 | + if (is_string($forcedLang)) { |
|
265 | 265 | return $forcedLang; |
266 | 266 | } |
267 | 267 | /** @noinspection PhpMissingBreakStatementInspection */ |
268 | 268 | case 1: |
269 | 269 | $i++; |
270 | 270 | $user = $this->userSession->getUser(); |
271 | - if($user instanceof IUser) { |
|
271 | + if ($user instanceof IUser) { |
|
272 | 272 | $userLang = $this->config->getUserValue($user->getUID(), 'core', 'lang', null); |
273 | - if(is_string($userLang)) { |
|
273 | + if (is_string($userLang)) { |
|
274 | 274 | return $userLang; |
275 | 275 | } |
276 | 276 | } |
@@ -377,12 +377,12 @@ discard block |
||
377 | 377 | $languageFiles = []; |
378 | 378 | |
379 | 379 | $i18nDir = $this->findL10nDir($app); |
380 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
380 | + $transFile = strip_tags($i18nDir).strip_tags($lang).'.json'; |
|
381 | 381 | |
382 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
383 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
384 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
385 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
382 | + if (($this->isSubDirectory($transFile, $this->serverRoot.'/core/l10n/') |
|
383 | + || $this->isSubDirectory($transFile, $this->serverRoot.'/lib/l10n/') |
|
384 | + || $this->isSubDirectory($transFile, $this->serverRoot.'/settings/l10n/') |
|
385 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app).'/l10n/') |
|
386 | 386 | ) |
387 | 387 | && file_exists($transFile)) { |
388 | 388 | // load the translations file |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | // merge with translations from theme |
393 | 393 | $theme = $this->config->getSystemValue('theme'); |
394 | 394 | if (!empty($theme)) { |
395 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
395 | + $transFile = $this->serverRoot.'/themes/'.$theme.substr($transFile, strlen($this->serverRoot)); |
|
396 | 396 | if (file_exists($transFile)) { |
397 | 397 | $languageFiles[] = $transFile; |
398 | 398 | } |
@@ -409,14 +409,14 @@ discard block |
||
409 | 409 | */ |
410 | 410 | protected function findL10nDir($app = null) { |
411 | 411 | if (in_array($app, ['core', 'lib', 'settings'])) { |
412 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
413 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
412 | + if (file_exists($this->serverRoot.'/'.$app.'/l10n/')) { |
|
413 | + return $this->serverRoot.'/'.$app.'/l10n/'; |
|
414 | 414 | } |
415 | 415 | } else if ($app && \OC_App::getAppPath($app) !== false) { |
416 | 416 | // Check if the app is in the app folder |
417 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
417 | + return \OC_App::getAppPath($app).'/l10n/'; |
|
418 | 418 | } |
419 | - return $this->serverRoot . '/core/l10n/'; |
|
419 | + return $this->serverRoot.'/core/l10n/'; |
|
420 | 420 | } |
421 | 421 | |
422 | 422 | |
@@ -433,15 +433,15 @@ discard block |
||
433 | 433 | return $this->pluralFunctions[$string]; |
434 | 434 | } |
435 | 435 | |
436 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
436 | + if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
437 | 437 | // sanitize |
438 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
439 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
438 | + $nplurals = preg_replace('/[^0-9]/', '', $matches[1]); |
|
439 | + $plural = preg_replace('#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2]); |
|
440 | 440 | |
441 | 441 | $body = str_replace( |
442 | - array( 'plural', 'n', '$n$plurals', ), |
|
443 | - array( '$plural', '$n', '$nplurals', ), |
|
444 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
442 | + array('plural', 'n', '$n$plurals',), |
|
443 | + array('$plural', '$n', '$nplurals',), |
|
444 | + 'nplurals='.$nplurals.'; plural='.$plural |
|
445 | 445 | ); |
446 | 446 | |
447 | 447 | // add parents |
@@ -450,9 +450,9 @@ discard block |
||
450 | 450 | $res = ''; |
451 | 451 | $p = 0; |
452 | 452 | $length = strlen($body); |
453 | - for($i = 0; $i < $length; $i++) { |
|
453 | + for ($i = 0; $i < $length; $i++) { |
|
454 | 454 | $ch = $body[$i]; |
455 | - switch ( $ch ) { |
|
455 | + switch ($ch) { |
|
456 | 456 | case '?': |
457 | 457 | $res .= ' ? ('; |
458 | 458 | $p++; |
@@ -461,7 +461,7 @@ discard block |
||
461 | 461 | $res .= ') : ('; |
462 | 462 | break; |
463 | 463 | case ';': |
464 | - $res .= str_repeat( ')', $p ) . ';'; |
|
464 | + $res .= str_repeat(')', $p).';'; |
|
465 | 465 | $p = 0; |
466 | 466 | break; |
467 | 467 | default: |
@@ -469,7 +469,7 @@ discard block |
||
469 | 469 | } |
470 | 470 | } |
471 | 471 | |
472 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
472 | + $body = $res.'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
473 | 473 | $function = create_function('$n', $body); |
474 | 474 | $this->pluralFunctions[$string] = $function; |
475 | 475 | return $function; |
@@ -501,7 +501,7 @@ discard block |
||
501 | 501 | $commonLanguages = []; |
502 | 502 | $languages = []; |
503 | 503 | |
504 | - foreach($languageCodes as $lang) { |
|
504 | + foreach ($languageCodes as $lang) { |
|
505 | 505 | $l = $this->get('lib', $lang); |
506 | 506 | // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
507 | 507 | $potentialName = (string) $l->t('__language_name__'); |
@@ -534,7 +534,7 @@ discard block |
||
534 | 534 | ksort($commonLanguages); |
535 | 535 | |
536 | 536 | // sort now by displayed language not the iso-code |
537 | - usort( $languages, function ($a, $b) { |
|
537 | + usort($languages, function($a, $b) { |
|
538 | 538 | if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
539 | 539 | // If a doesn't have a name, but b does, list b before a |
540 | 540 | return 1; |
@@ -25,49 +25,49 @@ |
||
25 | 25 | * @since 8.2.0 |
26 | 26 | */ |
27 | 27 | interface IFactory { |
28 | - /** |
|
29 | - * Get a language instance |
|
30 | - * |
|
31 | - * @param string $app |
|
32 | - * @param string|null $lang |
|
33 | - * @return \OCP\IL10N |
|
34 | - * @since 8.2.0 |
|
35 | - */ |
|
36 | - public function get($app, $lang = null); |
|
28 | + /** |
|
29 | + * Get a language instance |
|
30 | + * |
|
31 | + * @param string $app |
|
32 | + * @param string|null $lang |
|
33 | + * @return \OCP\IL10N |
|
34 | + * @since 8.2.0 |
|
35 | + */ |
|
36 | + public function get($app, $lang = null); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Find the best language |
|
40 | - * |
|
41 | - * @param string|null $app App id or null for core |
|
42 | - * @return string language If nothing works it returns 'en' |
|
43 | - * @since 9.0.0 |
|
44 | - */ |
|
45 | - public function findLanguage($app = null); |
|
38 | + /** |
|
39 | + * Find the best language |
|
40 | + * |
|
41 | + * @param string|null $app App id or null for core |
|
42 | + * @return string language If nothing works it returns 'en' |
|
43 | + * @since 9.0.0 |
|
44 | + */ |
|
45 | + public function findLanguage($app = null); |
|
46 | 46 | |
47 | - /** |
|
48 | - * Find all available languages for an app |
|
49 | - * |
|
50 | - * @param string|null $app App id or null for core |
|
51 | - * @return string[] an array of available languages |
|
52 | - * @since 9.0.0 |
|
53 | - */ |
|
54 | - public function findAvailableLanguages($app = null); |
|
47 | + /** |
|
48 | + * Find all available languages for an app |
|
49 | + * |
|
50 | + * @param string|null $app App id or null for core |
|
51 | + * @return string[] an array of available languages |
|
52 | + * @since 9.0.0 |
|
53 | + */ |
|
54 | + public function findAvailableLanguages($app = null); |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param string|null $app App id or null for core |
|
58 | - * @param string $lang |
|
59 | - * @return bool |
|
60 | - * @since 9.0.0 |
|
61 | - */ |
|
62 | - public function languageExists($app, $lang); |
|
56 | + /** |
|
57 | + * @param string|null $app App id or null for core |
|
58 | + * @param string $lang |
|
59 | + * @return bool |
|
60 | + * @since 9.0.0 |
|
61 | + */ |
|
62 | + public function languageExists($app, $lang); |
|
63 | 63 | |
64 | - /** |
|
65 | - * iterate through language settings (if provided) in this order: |
|
66 | - * 1. returns the forced language or: |
|
67 | - * 2. returns the user language or: |
|
68 | - * 3. returns the system default language or: |
|
69 | - * 4+∞. returns 'en' |
|
70 | - * @since 14.0.0 |
|
71 | - */ |
|
72 | - public function iterateLanguage(bool $reset = false): string; |
|
64 | + /** |
|
65 | + * iterate through language settings (if provided) in this order: |
|
66 | + * 1. returns the forced language or: |
|
67 | + * 2. returns the user language or: |
|
68 | + * 3. returns the system default language or: |
|
69 | + * 4+∞. returns 'en' |
|
70 | + * @since 14.0.0 |
|
71 | + */ |
|
72 | + public function iterateLanguage(bool $reset = false): string; |
|
73 | 73 | } |
@@ -29,59 +29,59 @@ |
||
29 | 29 | use OC\Updater\VersionCheck; |
30 | 30 | |
31 | 31 | class UpdateChecker { |
32 | - /** @var VersionCheck */ |
|
33 | - private $updater; |
|
34 | - /** @var ChangesCheck */ |
|
35 | - private $changesCheck; |
|
32 | + /** @var VersionCheck */ |
|
33 | + private $updater; |
|
34 | + /** @var ChangesCheck */ |
|
35 | + private $changesCheck; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param VersionCheck $updater |
|
39 | - */ |
|
40 | - public function __construct(VersionCheck $updater, ChangesCheck $changesCheck) { |
|
41 | - $this->updater = $updater; |
|
42 | - $this->changesCheck = $changesCheck; |
|
43 | - } |
|
37 | + /** |
|
38 | + * @param VersionCheck $updater |
|
39 | + */ |
|
40 | + public function __construct(VersionCheck $updater, ChangesCheck $changesCheck) { |
|
41 | + $this->updater = $updater; |
|
42 | + $this->changesCheck = $changesCheck; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function getUpdateState(): array { |
|
49 | - $data = $this->updater->check(); |
|
50 | - $result = []; |
|
45 | + /** |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function getUpdateState(): array { |
|
49 | + $data = $this->updater->check(); |
|
50 | + $result = []; |
|
51 | 51 | |
52 | - if (isset($data['version']) && $data['version'] !== '' && $data['version'] !== []) { |
|
53 | - $result['updateAvailable'] = true; |
|
54 | - $result['updateVersion'] = $data['versionstring']; |
|
55 | - $result['updaterEnabled'] = $data['autoupdater'] === '1'; |
|
56 | - $result['versionIsEol'] = $data['eol'] === '1'; |
|
57 | - if (strpos($data['web'], 'https://') === 0) { |
|
58 | - $result['updateLink'] = $data['web']; |
|
59 | - } |
|
60 | - if (strpos($data['url'], 'https://') === 0) { |
|
61 | - $result['downloadLink'] = $data['url']; |
|
62 | - } |
|
63 | - if (strpos($data['changes'], 'https://') === 0) { |
|
64 | - try { |
|
65 | - $result['changes'] = $this->changesCheck->check($data['changes'], $data['version']); |
|
66 | - } catch (\Exception $e) { |
|
67 | - // no info, not a problem |
|
68 | - } |
|
69 | - } |
|
52 | + if (isset($data['version']) && $data['version'] !== '' && $data['version'] !== []) { |
|
53 | + $result['updateAvailable'] = true; |
|
54 | + $result['updateVersion'] = $data['versionstring']; |
|
55 | + $result['updaterEnabled'] = $data['autoupdater'] === '1'; |
|
56 | + $result['versionIsEol'] = $data['eol'] === '1'; |
|
57 | + if (strpos($data['web'], 'https://') === 0) { |
|
58 | + $result['updateLink'] = $data['web']; |
|
59 | + } |
|
60 | + if (strpos($data['url'], 'https://') === 0) { |
|
61 | + $result['downloadLink'] = $data['url']; |
|
62 | + } |
|
63 | + if (strpos($data['changes'], 'https://') === 0) { |
|
64 | + try { |
|
65 | + $result['changes'] = $this->changesCheck->check($data['changes'], $data['version']); |
|
66 | + } catch (\Exception $e) { |
|
67 | + // no info, not a problem |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | - return $result; |
|
72 | - } |
|
71 | + return $result; |
|
72 | + } |
|
73 | 73 | |
74 | - return []; |
|
75 | - } |
|
74 | + return []; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @param array $data |
|
79 | - */ |
|
80 | - public function populateJavaScriptVariables(array $data) { |
|
81 | - $data['array']['oc_updateState'] = json_encode([ |
|
82 | - 'updateAvailable' => true, |
|
83 | - 'updateVersion' => $this->getUpdateState()['updateVersion'], |
|
84 | - 'updateLink' => $this->getUpdateState()['updateLink'] ?? '', |
|
85 | - ]); |
|
86 | - } |
|
77 | + /** |
|
78 | + * @param array $data |
|
79 | + */ |
|
80 | + public function populateJavaScriptVariables(array $data) { |
|
81 | + $data['array']['oc_updateState'] = json_encode([ |
|
82 | + 'updateAvailable' => true, |
|
83 | + 'updateVersion' => $this->getUpdateState()['updateVersion'], |
|
84 | + 'updateLink' => $this->getUpdateState()['updateLink'] ?? '', |
|
85 | + ]); |
|
86 | + } |
|
87 | 87 | } |
@@ -37,159 +37,159 @@ |
||
37 | 37 | use OCP\Util; |
38 | 38 | |
39 | 39 | class Admin implements ISettings { |
40 | - /** @var IConfig */ |
|
41 | - private $config; |
|
42 | - /** @var UpdateChecker */ |
|
43 | - private $updateChecker; |
|
44 | - /** @var IGroupManager */ |
|
45 | - private $groupManager; |
|
46 | - /** @var IDateTimeFormatter */ |
|
47 | - private $dateTimeFormatter; |
|
48 | - /** @var IUserSession */ |
|
49 | - private $session; |
|
50 | - /** @var IFactory */ |
|
51 | - private $l10nFactory; |
|
52 | - |
|
53 | - public function __construct( |
|
54 | - IConfig $config, |
|
55 | - UpdateChecker $updateChecker, |
|
56 | - IGroupManager $groupManager, |
|
57 | - IDateTimeFormatter $dateTimeFormatter, |
|
58 | - IUserSession $session, |
|
59 | - IFactory $l10nFactory |
|
60 | - ) { |
|
61 | - $this->config = $config; |
|
62 | - $this->updateChecker = $updateChecker; |
|
63 | - $this->groupManager = $groupManager; |
|
64 | - $this->dateTimeFormatter = $dateTimeFormatter; |
|
65 | - $this->session = $session; |
|
66 | - $this->l10nFactory = $l10nFactory; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @return TemplateResponse |
|
71 | - */ |
|
72 | - public function getForm(): TemplateResponse { |
|
73 | - $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat'); |
|
74 | - $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp); |
|
75 | - |
|
76 | - $channels = [ |
|
77 | - 'daily', |
|
78 | - 'beta', |
|
79 | - 'stable', |
|
80 | - 'production', |
|
81 | - ]; |
|
82 | - $currentChannel = Util::getChannel(); |
|
83 | - if ($currentChannel === 'git') { |
|
84 | - $channels[] = 'git'; |
|
85 | - } |
|
86 | - |
|
87 | - $updateState = $this->updateChecker->getUpdateState(); |
|
88 | - |
|
89 | - $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true); |
|
90 | - |
|
91 | - $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/'; |
|
92 | - $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL); |
|
93 | - |
|
94 | - $params = [ |
|
95 | - 'isNewVersionAvailable' => !empty($updateState['updateAvailable']), |
|
96 | - 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0, |
|
97 | - 'lastChecked' => $lastUpdateCheck, |
|
98 | - 'currentChannel' => $currentChannel, |
|
99 | - 'channels' => $channels, |
|
100 | - 'newVersionString' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'], |
|
101 | - 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'], |
|
102 | - 'changes' => $this->filterChanges($updateState['changes'] ?? []), |
|
103 | - 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'], |
|
104 | - 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'], |
|
105 | - 'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL, |
|
106 | - 'updateServerURL' => $updateServerURL, |
|
107 | - 'notifyGroups' => $this->getSelectedGroups($notifyGroups), |
|
108 | - ]; |
|
109 | - |
|
110 | - $params = [ |
|
111 | - 'json' => json_encode($params), |
|
112 | - ]; |
|
113 | - |
|
114 | - return new TemplateResponse('updatenotification', 'admin', $params, ''); |
|
115 | - } |
|
116 | - |
|
117 | - protected function filterChanges(array $changes) { |
|
118 | - $filtered = []; |
|
119 | - if(isset($changes['changelogURL'])) { |
|
120 | - $filtered['changelogURL'] = $changes['changelogURL']; |
|
121 | - } |
|
122 | - if(!isset($changes['whatsNew'])) { |
|
123 | - return $filtered; |
|
124 | - } |
|
125 | - |
|
126 | - $isFirstCall = true; |
|
127 | - do { |
|
128 | - $lang = $this->l10nFactory->iterateLanguage($isFirstCall); |
|
129 | - if($this->findWhatsNewTranslation($lang, $filtered, $changes['whatsNew'])) { |
|
130 | - return $filtered; |
|
131 | - } |
|
132 | - $isFirstCall = false; |
|
133 | - } while($lang !== 'en'); |
|
134 | - |
|
135 | - return $filtered; |
|
136 | - } |
|
137 | - |
|
138 | - protected function getLangTrunk(string $lang):string { |
|
139 | - $pos = strpos($lang, '_'); |
|
140 | - if($pos !== false) { |
|
141 | - $lang = substr($lang, 0, $pos); |
|
142 | - } |
|
143 | - return $lang; |
|
144 | - } |
|
145 | - |
|
146 | - protected function findWhatsNewTranslation(string $lang, array &$result, array $whatsNew): bool { |
|
147 | - if(isset($whatsNew[$lang])) { |
|
148 | - $result['whatsNew'] = $whatsNew[$lang]; |
|
149 | - return true; |
|
150 | - } |
|
151 | - $trunkedLang = $this->getLangTrunk($lang); |
|
152 | - if($trunkedLang !== $lang && isset($whatsNew[$trunkedLang])) { |
|
153 | - $result['whatsNew'] = $whatsNew[$trunkedLang]; |
|
154 | - return true; |
|
155 | - } |
|
156 | - return false; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param array $groupIds |
|
161 | - * @return array |
|
162 | - */ |
|
163 | - protected function getSelectedGroups(array $groupIds): array { |
|
164 | - $result = []; |
|
165 | - foreach ($groupIds as $groupId) { |
|
166 | - $group = $this->groupManager->get($groupId); |
|
167 | - |
|
168 | - if ($group === null) { |
|
169 | - continue; |
|
170 | - } |
|
171 | - |
|
172 | - $result[] = ['value' => $group->getGID(), 'label' => $group->getDisplayName()]; |
|
173 | - } |
|
174 | - |
|
175 | - return $result; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @return string the section ID, e.g. 'sharing' |
|
180 | - */ |
|
181 | - public function getSection(): string { |
|
182 | - return 'overview'; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @return int whether the form should be rather on the top or bottom of |
|
187 | - * the admin section. The forms are arranged in ascending order of the |
|
188 | - * priority values. It is required to return a value between 0 and 100. |
|
189 | - * |
|
190 | - * E.g.: 70 |
|
191 | - */ |
|
192 | - public function getPriority(): int { |
|
193 | - return 11; |
|
194 | - } |
|
40 | + /** @var IConfig */ |
|
41 | + private $config; |
|
42 | + /** @var UpdateChecker */ |
|
43 | + private $updateChecker; |
|
44 | + /** @var IGroupManager */ |
|
45 | + private $groupManager; |
|
46 | + /** @var IDateTimeFormatter */ |
|
47 | + private $dateTimeFormatter; |
|
48 | + /** @var IUserSession */ |
|
49 | + private $session; |
|
50 | + /** @var IFactory */ |
|
51 | + private $l10nFactory; |
|
52 | + |
|
53 | + public function __construct( |
|
54 | + IConfig $config, |
|
55 | + UpdateChecker $updateChecker, |
|
56 | + IGroupManager $groupManager, |
|
57 | + IDateTimeFormatter $dateTimeFormatter, |
|
58 | + IUserSession $session, |
|
59 | + IFactory $l10nFactory |
|
60 | + ) { |
|
61 | + $this->config = $config; |
|
62 | + $this->updateChecker = $updateChecker; |
|
63 | + $this->groupManager = $groupManager; |
|
64 | + $this->dateTimeFormatter = $dateTimeFormatter; |
|
65 | + $this->session = $session; |
|
66 | + $this->l10nFactory = $l10nFactory; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @return TemplateResponse |
|
71 | + */ |
|
72 | + public function getForm(): TemplateResponse { |
|
73 | + $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat'); |
|
74 | + $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp); |
|
75 | + |
|
76 | + $channels = [ |
|
77 | + 'daily', |
|
78 | + 'beta', |
|
79 | + 'stable', |
|
80 | + 'production', |
|
81 | + ]; |
|
82 | + $currentChannel = Util::getChannel(); |
|
83 | + if ($currentChannel === 'git') { |
|
84 | + $channels[] = 'git'; |
|
85 | + } |
|
86 | + |
|
87 | + $updateState = $this->updateChecker->getUpdateState(); |
|
88 | + |
|
89 | + $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true); |
|
90 | + |
|
91 | + $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/'; |
|
92 | + $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL); |
|
93 | + |
|
94 | + $params = [ |
|
95 | + 'isNewVersionAvailable' => !empty($updateState['updateAvailable']), |
|
96 | + 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0, |
|
97 | + 'lastChecked' => $lastUpdateCheck, |
|
98 | + 'currentChannel' => $currentChannel, |
|
99 | + 'channels' => $channels, |
|
100 | + 'newVersionString' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'], |
|
101 | + 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'], |
|
102 | + 'changes' => $this->filterChanges($updateState['changes'] ?? []), |
|
103 | + 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'], |
|
104 | + 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'], |
|
105 | + 'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL, |
|
106 | + 'updateServerURL' => $updateServerURL, |
|
107 | + 'notifyGroups' => $this->getSelectedGroups($notifyGroups), |
|
108 | + ]; |
|
109 | + |
|
110 | + $params = [ |
|
111 | + 'json' => json_encode($params), |
|
112 | + ]; |
|
113 | + |
|
114 | + return new TemplateResponse('updatenotification', 'admin', $params, ''); |
|
115 | + } |
|
116 | + |
|
117 | + protected function filterChanges(array $changes) { |
|
118 | + $filtered = []; |
|
119 | + if(isset($changes['changelogURL'])) { |
|
120 | + $filtered['changelogURL'] = $changes['changelogURL']; |
|
121 | + } |
|
122 | + if(!isset($changes['whatsNew'])) { |
|
123 | + return $filtered; |
|
124 | + } |
|
125 | + |
|
126 | + $isFirstCall = true; |
|
127 | + do { |
|
128 | + $lang = $this->l10nFactory->iterateLanguage($isFirstCall); |
|
129 | + if($this->findWhatsNewTranslation($lang, $filtered, $changes['whatsNew'])) { |
|
130 | + return $filtered; |
|
131 | + } |
|
132 | + $isFirstCall = false; |
|
133 | + } while($lang !== 'en'); |
|
134 | + |
|
135 | + return $filtered; |
|
136 | + } |
|
137 | + |
|
138 | + protected function getLangTrunk(string $lang):string { |
|
139 | + $pos = strpos($lang, '_'); |
|
140 | + if($pos !== false) { |
|
141 | + $lang = substr($lang, 0, $pos); |
|
142 | + } |
|
143 | + return $lang; |
|
144 | + } |
|
145 | + |
|
146 | + protected function findWhatsNewTranslation(string $lang, array &$result, array $whatsNew): bool { |
|
147 | + if(isset($whatsNew[$lang])) { |
|
148 | + $result['whatsNew'] = $whatsNew[$lang]; |
|
149 | + return true; |
|
150 | + } |
|
151 | + $trunkedLang = $this->getLangTrunk($lang); |
|
152 | + if($trunkedLang !== $lang && isset($whatsNew[$trunkedLang])) { |
|
153 | + $result['whatsNew'] = $whatsNew[$trunkedLang]; |
|
154 | + return true; |
|
155 | + } |
|
156 | + return false; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param array $groupIds |
|
161 | + * @return array |
|
162 | + */ |
|
163 | + protected function getSelectedGroups(array $groupIds): array { |
|
164 | + $result = []; |
|
165 | + foreach ($groupIds as $groupId) { |
|
166 | + $group = $this->groupManager->get($groupId); |
|
167 | + |
|
168 | + if ($group === null) { |
|
169 | + continue; |
|
170 | + } |
|
171 | + |
|
172 | + $result[] = ['value' => $group->getGID(), 'label' => $group->getDisplayName()]; |
|
173 | + } |
|
174 | + |
|
175 | + return $result; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @return string the section ID, e.g. 'sharing' |
|
180 | + */ |
|
181 | + public function getSection(): string { |
|
182 | + return 'overview'; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @return int whether the form should be rather on the top or bottom of |
|
187 | + * the admin section. The forms are arranged in ascending order of the |
|
188 | + * priority values. It is required to return a value between 0 and 100. |
|
189 | + * |
|
190 | + * E.g.: 70 |
|
191 | + */ |
|
192 | + public function getPriority(): int { |
|
193 | + return 11; |
|
194 | + } |
|
195 | 195 | } |
@@ -116,40 +116,40 @@ |
||
116 | 116 | |
117 | 117 | protected function filterChanges(array $changes) { |
118 | 118 | $filtered = []; |
119 | - if(isset($changes['changelogURL'])) { |
|
119 | + if (isset($changes['changelogURL'])) { |
|
120 | 120 | $filtered['changelogURL'] = $changes['changelogURL']; |
121 | 121 | } |
122 | - if(!isset($changes['whatsNew'])) { |
|
122 | + if (!isset($changes['whatsNew'])) { |
|
123 | 123 | return $filtered; |
124 | 124 | } |
125 | 125 | |
126 | 126 | $isFirstCall = true; |
127 | 127 | do { |
128 | 128 | $lang = $this->l10nFactory->iterateLanguage($isFirstCall); |
129 | - if($this->findWhatsNewTranslation($lang, $filtered, $changes['whatsNew'])) { |
|
129 | + if ($this->findWhatsNewTranslation($lang, $filtered, $changes['whatsNew'])) { |
|
130 | 130 | return $filtered; |
131 | 131 | } |
132 | 132 | $isFirstCall = false; |
133 | - } while($lang !== 'en'); |
|
133 | + } while ($lang !== 'en'); |
|
134 | 134 | |
135 | 135 | return $filtered; |
136 | 136 | } |
137 | 137 | |
138 | 138 | protected function getLangTrunk(string $lang):string { |
139 | 139 | $pos = strpos($lang, '_'); |
140 | - if($pos !== false) { |
|
140 | + if ($pos !== false) { |
|
141 | 141 | $lang = substr($lang, 0, $pos); |
142 | 142 | } |
143 | 143 | return $lang; |
144 | 144 | } |
145 | 145 | |
146 | 146 | protected function findWhatsNewTranslation(string $lang, array &$result, array $whatsNew): bool { |
147 | - if(isset($whatsNew[$lang])) { |
|
147 | + if (isset($whatsNew[$lang])) { |
|
148 | 148 | $result['whatsNew'] = $whatsNew[$lang]; |
149 | 149 | return true; |
150 | 150 | } |
151 | 151 | $trunkedLang = $this->getLangTrunk($lang); |
152 | - if($trunkedLang !== $lang && isset($whatsNew[$trunkedLang])) { |
|
152 | + if ($trunkedLang !== $lang && isset($whatsNew[$trunkedLang])) { |
|
153 | 153 | $result['whatsNew'] = $whatsNew[$trunkedLang]; |
154 | 154 | return true; |
155 | 155 | } |
@@ -28,42 +28,42 @@ |
||
28 | 28 | use OCP\Migration\SimpleMigrationStep; |
29 | 29 | |
30 | 30 | class Version14000Date20180626223656 extends SimpleMigrationStep { |
31 | - public function changeSchema(\OCP\Migration\IOutput $output, \Closure $schemaClosure, array $options) { |
|
32 | - /** @var ISchemaWrapper $schema */ |
|
33 | - $schema = $schemaClosure(); |
|
34 | - if(!$schema->hasTable('whats_new')) { |
|
35 | - $table = $schema->createTable('whats_new'); |
|
36 | - $table->addColumn('id', 'integer', [ |
|
37 | - 'autoincrement' => true, |
|
38 | - 'notnull' => true, |
|
39 | - 'length' => 4, |
|
40 | - 'unsigned' => true, |
|
41 | - ]); |
|
42 | - $table->addColumn('version', 'string', [ |
|
43 | - 'notnull' => true, |
|
44 | - 'length' => 64, |
|
45 | - 'default' => '11', |
|
46 | - ]); |
|
47 | - $table->addColumn('etag', 'string', [ |
|
48 | - 'notnull' => true, |
|
49 | - 'length' => 64, |
|
50 | - 'default' => '', |
|
51 | - ]); |
|
52 | - $table->addColumn('last_check', 'integer', [ |
|
53 | - 'notnull' => true, |
|
54 | - 'length' => 4, |
|
55 | - 'unsigned' => true, |
|
56 | - 'default' => 0, |
|
57 | - ]); |
|
58 | - $table->addColumn('data', 'text', [ |
|
59 | - 'notnull' => true, |
|
60 | - 'default' => '', |
|
61 | - ]); |
|
62 | - $table->setPrimaryKey(['id']); |
|
63 | - $table->addUniqueIndex(['version']); |
|
64 | - $table->addIndex(['version', 'etag'], 'version_etag_idx'); |
|
65 | - } |
|
31 | + public function changeSchema(\OCP\Migration\IOutput $output, \Closure $schemaClosure, array $options) { |
|
32 | + /** @var ISchemaWrapper $schema */ |
|
33 | + $schema = $schemaClosure(); |
|
34 | + if(!$schema->hasTable('whats_new')) { |
|
35 | + $table = $schema->createTable('whats_new'); |
|
36 | + $table->addColumn('id', 'integer', [ |
|
37 | + 'autoincrement' => true, |
|
38 | + 'notnull' => true, |
|
39 | + 'length' => 4, |
|
40 | + 'unsigned' => true, |
|
41 | + ]); |
|
42 | + $table->addColumn('version', 'string', [ |
|
43 | + 'notnull' => true, |
|
44 | + 'length' => 64, |
|
45 | + 'default' => '11', |
|
46 | + ]); |
|
47 | + $table->addColumn('etag', 'string', [ |
|
48 | + 'notnull' => true, |
|
49 | + 'length' => 64, |
|
50 | + 'default' => '', |
|
51 | + ]); |
|
52 | + $table->addColumn('last_check', 'integer', [ |
|
53 | + 'notnull' => true, |
|
54 | + 'length' => 4, |
|
55 | + 'unsigned' => true, |
|
56 | + 'default' => 0, |
|
57 | + ]); |
|
58 | + $table->addColumn('data', 'text', [ |
|
59 | + 'notnull' => true, |
|
60 | + 'default' => '', |
|
61 | + ]); |
|
62 | + $table->setPrimaryKey(['id']); |
|
63 | + $table->addUniqueIndex(['version']); |
|
64 | + $table->addIndex(['version', 'etag'], 'version_etag_idx'); |
|
65 | + } |
|
66 | 66 | |
67 | - return $schema; |
|
68 | - } |
|
67 | + return $schema; |
|
68 | + } |
|
69 | 69 | } |
@@ -31,7 +31,7 @@ |
||
31 | 31 | public function changeSchema(\OCP\Migration\IOutput $output, \Closure $schemaClosure, array $options) { |
32 | 32 | /** @var ISchemaWrapper $schema */ |
33 | 33 | $schema = $schemaClosure(); |
34 | - if(!$schema->hasTable('whats_new')) { |
|
34 | + if (!$schema->hasTable('whats_new')) { |
|
35 | 35 | $table = $schema->createTable('whats_new'); |
36 | 36 | $table->addColumn('id', 'integer', [ |
37 | 37 | 'autoincrement' => true, |