/** * Discover the actual data and do some naive caching to ensure that the data * is not requested multiple times. * * If no valid discovery data is found the ownCloud defaults are returned. * * @param string $remote * @return array */ private function discover($remote) { // Check if something is in the cache if ($cacheData = $this->cache->get($remote)) { return json_decode($cacheData, true); } // Default response body $discoveredServices = ['webdav' => '/public.php/webdav', 'share' => '/ocs/v1.php/cloud/shares']; // Read the data from the response body try { $response = $this->client->get($remote . '/ocs-provider/'); if ($response->getStatusCode() === 200) { $decodedService = json_decode($response->getBody(), true); if (is_array($decodedService)) { $endpoints = ['webdav', 'share']; foreach ($endpoints as $endpoint) { if (isset($decodedService['services']['FEDERATED_SHARING']['endpoints'][$endpoint])) { $endpointUrl = (string) $decodedService['services']['FEDERATED_SHARING']['endpoints'][$endpoint]; if ($this->isSafeUrl($endpointUrl)) { $discoveredServices[$endpoint] = $endpointUrl; } } } } } } catch (ClientException $e) { // Don't throw any exception since exceptions are handled before } catch (ConnectException $e) { // Don't throw any exception since exceptions are handled before } // Write into cache $this->cache->set($remote, json_encode($discoveredServices)); return $discoveredServices; }
/** * Generate url based on $name and $parameters * * @param string $name Name of the route to use. * @param array $parameters Parameters for the route * @param bool $absolute * @return string */ public function generate($name, $parameters = array(), $absolute = false) { asort($parameters); $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . json_encode($parameters) . intval($absolute); if ($this->cache->hasKey($key)) { return $this->cache->get($key); } else { $url = parent::generate($name, $parameters, $absolute); $this->cache->set($key, $url, 3600); return $url; } }
/** * @NoAdminRequired * * @param array $crop * @return DataResponse */ public function postCroppedAvatar($crop) { $userId = $this->userSession->getUser()->getUID(); if (is_null($crop)) { return new DataResponse(['data' => ['message' => $this->l->t("No crop data provided")]], Http::STATUS_BAD_REQUEST); } if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { return new DataResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], Http::STATUS_BAD_REQUEST); } $tmpAvatar = $this->cache->get('tmpAvatar'); if (is_null($tmpAvatar)) { return new DataResponse(['data' => ['message' => $this->l->t("No temporary profile picture available, try again")]], Http::STATUS_BAD_REQUEST); } $image = new \OC_Image($tmpAvatar); $image->crop($crop['x'], $crop['y'], round($crop['w']), round($crop['h'])); try { $avatar = $this->avatarManager->getAvatar($userId); $avatar->set($image); // Clean up $this->cache->remove('tmpAvatar'); return new DataResponse(['status' => 'success']); } catch (\OC\NotSquareException $e) { return new DataResponse(['data' => ['message' => $this->l->t('Crop is not square')]], Http::STATUS_BAD_REQUEST); } catch (\Exception $e) { return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_BAD_REQUEST); } }
public function clearCache() { if (is_null($this->cache)) { return; } $this->cache->clear($this->getCacheKey(null)); }
/** * @param ISubscription $subscription * @return string */ protected function getData(ISubscription $subscription) { $id = $subscription->getId(); $url = $subscription->getUrl(); $cacheId = implode('::', [$id, $url]); if ($this->cache->hasKey($cacheId)) { return $this->cache->get($cacheId); } else { $curl = curl_init(); $data = null; $this->prepareRequest($curl, $url); $this->getRequestData($curl, $data); $this->validateRequest($curl); $this->cache->set($cacheId, $data, 60 * 60 * 2); return $data; } }
public function testListCategoriesNotCachedWithAppStore() { $expected = [['id' => 0, 'ident' => 'enabled', 'displayName' => 'Enabled'], ['id' => 1, 'ident' => 'disabled', 'displayName' => 'Not enabled'], ['id' => 0, 'ident' => 'tools', 'displayName' => 'Tools'], ['id' => 1, 'ident' => 'games', 'displayName' => 'Games'], ['id' => 2, 'ident' => 'productivity', 'displayName' => 'Productivity'], ['id' => 3, 'ident' => 'multimedia', 'displayName' => 'Multimedia']]; $this->cache->expects($this->once())->method('get')->with('listCategories')->will($this->returnValue(null)); $this->cache->expects($this->once())->method('set')->with('listCategories', $expected, 3600); $this->ocsClient->expects($this->once())->method('isAppStoreEnabled')->will($this->returnValue(true)); $this->ocsClient->expects($this->once())->method('getCategories')->will($this->returnValue(['ownCloud Tools', 'Games', 'ownCloud Productivity', 'Multimedia'])); $this->assertSame($expected, $this->appSettingsController->listCategories()); }
/** * Get cache info * @NoAdminRequired */ public function fromCache() { if ($this->cache) { $cachedFriends = json_decode($this->cache->get($this->cacheKey), true); return !empty($cachedFriends) && is_array($cachedFriends); } else { return false; } }
public function testListCategoriesNotCachedWithAppStore() { $expected = [ [ 'id' => 0, 'displayName' => 'Enabled', ], [ 'id' => 1, 'displayName' => 'Not enabled', ], [ 'id' => 0, 'displayName' => 'Tools', ], [ 'id' => 1, 'displayName' => 'Awesome Games', ], [ 'id' => 2, 'displayName' => 'PIM', ], [ 'id' => 3, 'displayName' => 'Papershop', ], ]; $this->cache ->expects($this->once()) ->method('get') ->with('listCategories') ->will($this->returnValue(null)); $this->cache ->expects($this->once()) ->method('set') ->with('listCategories', $expected, 3600); $this->ocsClient ->expects($this->once()) ->method('isAppStoreEnabled') ->will($this->returnValue(true)); $this->ocsClient ->expects($this->once()) ->method('getCategories') ->will($this->returnValue( [ 'ownCloud Tools', 'Awesome Games', 'ownCloud PIM', 'Papershop', ] )); $this->assertSame($expected, $this->appSettingsController->listCategories()); }
/** * Stores the results in the app config as well as cache * * @param string $scope * @param array $result */ private function storeResults($scope, array $result) { $resultArray = $this->getResults(); unset($resultArray[$scope]); if (!empty($result)) { $resultArray[$scope] = $result; } $this->config->setAppValue('core', self::CACHE_KEY, json_encode($resultArray)); $this->cache->set(self::CACHE_KEY, json_encode($resultArray)); }
public function writeBack($tmpFile) { if (!isset(self::$tmpFiles[$tmpFile])) { return false; } $fileData = fopen($tmpFile, 'r'); $this->getContainer()->uploadObject(self::$tmpFiles[$tmpFile], $fileData); // invalidate target object to force repopulation on fetch $this->objectCache->remove(self::$tmpFiles[$tmpFile]); unlink($tmpFile); }
public function testWithMaliciousEndpointCached() { $response = $this->getMock('\\OCP\\Http\\Client\\IResponse'); $response->expects($this->once())->method('getStatusCode')->willReturn(200); $response->expects($this->once())->method('getBody')->willReturn('{"version":2,"services":{"PRIVATE_DATA":{"version":1,"endpoints":{"store":"\\/ocs\\/v2.php\\/privatedata\\/setattribute","read":"\\/ocs\\/v2.php\\/privatedata\\/getattribute","delete":"\\/ocs\\/v2.php\\/privatedata\\/deleteattribute"}},"SHARING":{"version":1,"endpoints":{"share":"\\/ocs\\/v2.php\\/apps\\/files_sharing\\/api\\/v1\\/shares"}},"FEDERATED_SHARING":{"version":1,"endpoints":{"share":"\\/ocs\\/v2.php\\/cl@oud\\/MyCustomShareEndpoint","webdav":"\\/public.php\\/MyC:ustomEndpoint\\/"}},"ACTIVITY":{"version":1,"endpoints":{"list":"\\/ocs\\/v2.php\\/cloud\\/activity"}},"PROVISIONING":{"version":1,"endpoints":{"user":"******","groups":"\\/ocs\\/v2.php\\/cloud\\/groups","apps":"\\/ocs\\/v2.php\\/cloud\\/apps"}}}}'); $this->client->expects($this->once())->method('get')->with('https://myhost.com/ocs-provider/', [])->willReturn($response); $this->cache->expects($this->at(0))->method('get')->with('https://myhost.com')->willReturn(null); $this->cache->expects($this->at(1))->method('set')->with('https://myhost.com', '{"webdav":"\\/public.php\\/webdav","share":"\\/ocs\\/v1.php\\/cloud\\/shares"}'); $this->cache->expects($this->at(2))->method('get')->with('https://myhost.com')->willReturn('{"webdav":"\\/public.php\\/webdav","share":"\\/ocs\\/v1.php\\/cloud\\/shares"}'); $this->assertSame('/public.php/webdav', $this->discoveryManager->getWebDavEndpoint('https://myhost.com')); $this->assertSame('/ocs/v1.php/cloud/shares', $this->discoveryManager->getShareEndpoint('https://myhost.com')); }
/** * Save image data to cache and return the key * * @return string */ private function cachePhoto() { if ($this->cached) { return; } if (!$this->image instanceof Image) { $this->processImage(); } $this->normalizePhoto(); $data = $this->image->data(); $this->key = uniqid('photo-'); $this->cache->set($this->key, $data, 600); }
/** * Load the slicemap for a given mailbox. The slicemap contains * the uidvalidity information, the UIDs->slice lookup table, and any * metadata that needs to be saved for the mailbox. * * @param string $mailbox The mailbox. * @param integer $uidvalid The IMAP uidvalidity value of the mailbox. */ protected function _loadSliceMap($mailbox, $uidvalid = null) { if (!isset($this->_slicemap[$mailbox]) && ($data = $this->_cache->get($this->_getCid($mailbox, 'slicemap'), 0)) !== false && ($slice = @unserialize($data)) && is_array($slice)) { $this->_slicemap[$mailbox] = $slice; } if (isset($this->_slicemap[$mailbox])) { $ptr =& $this->_slicemap[$mailbox]; if (is_null($ptr['d']['uidvalid'])) { $ptr['d']['uidvalid'] = $uidvalid; return; } elseif (!is_null($uidvalid) && $ptr['d']['uidvalid'] != $uidvalid) { $this->_deleteMailbox($mailbox); } else { return; } } $this->_slicemap[$mailbox] = array('c' => 0, 'd' => array('uidvalid' => $uidvalid), 'i' => 0, 's' => array()); }
/** * * Clean previous results for a proper rescanning. Otherwise */ private function cleanResults() { $this->config->deleteAppValue('core', self::CACHE_KEY); $this->cache->remove(self::CACHE_KEY); }
protected function expectClearCache() { $this->cache->expects($this->once())->method('clear')->with('listApps'); }
/** * Get all available apps in a category * * @param int $category * @return array */ public function listApps($category = 0) { if (!is_null($this->cache->get('listApps-' . $category))) { $apps = $this->cache->get('listApps-' . $category); } else { switch ($category) { // installed apps case 0: $apps = $this->getInstalledApps(); usort($apps, function ($a, $b) { $a = (string) $a['name']; $b = (string) $b['name']; if ($a === $b) { return 0; } return $a < $b ? -1 : 1; }); break; // not-installed apps // not-installed apps case 1: $apps = \OC_App::listAllApps(true); $apps = array_filter($apps, function ($app) { return !$app['active']; }); usort($apps, function ($a, $b) { $a = (string) $a['name']; $b = (string) $b['name']; if ($a === $b) { return 0; } return $a < $b ? -1 : 1; }); break; default: $filter = $this->config->getSystemValue('appstore.experimental.enabled', false) ? 'all' : 'approved'; $apps = \OC_App::getAppstoreApps($filter, $category); if (!$apps) { $apps = array(); } else { // don't list installed apps $installedApps = $this->getInstalledApps(); $installedApps = array_map(function ($app) { if (isset($app['ocsid'])) { return $app['ocsid']; } return $app['id']; }, $installedApps); $apps = array_filter($apps, function ($app) use($installedApps) { return !in_array($app['id'], $installedApps); }); } // sort by score usort($apps, function ($a, $b) { $a = (int) $a['score']; $b = (int) $b['score']; if ($a === $b) { return 0; } return $a > $b ? -1 : 1; }); break; } } // fix groups to be an array $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n); $apps = array_map(function ($app) use($dependencyAnalyzer) { // fix groups $groups = array(); if (is_string($app['groups'])) { $groups = json_decode($app['groups']); } $app['groups'] = $groups; $app['canUnInstall'] = !$app['active'] && $app['removable']; // fix licence vs license if (isset($app['license']) && !isset($app['licence'])) { $app['licence'] = $app['license']; } // analyse dependencies $missing = $dependencyAnalyzer->analyze($app); $app['canInstall'] = empty($missing); $app['missingDependencies'] = $missing; return $app; }, $apps); $this->cache->set('listApps-' . $category, $apps, 300); return ['apps' => $apps, 'status' => 'success']; }
/** * Get all available apps in a category * * @param string $category * @param bool $includeUpdateInfo Should we check whether there is an update * in the app store? * @return array */ public function listApps($category = '', $includeUpdateInfo = true) { $category = $this->getCategory($category); $cacheName = 'listApps-' . $category . '-' . (int) $includeUpdateInfo; if (!is_null($this->cache->get($cacheName))) { $apps = $this->cache->get($cacheName); } else { switch ($category) { // installed apps case 0: $apps = $this->getInstalledApps($includeUpdateInfo); usort($apps, function ($a, $b) { $a = (string) $a['name']; $b = (string) $b['name']; if ($a === $b) { return 0; } return $a < $b ? -1 : 1; }); $version = \OCP\Util::getVersion(); foreach ($apps as $key => $app) { if (!array_key_exists('level', $app) && array_key_exists('ocsid', $app)) { $remoteAppEntry = $this->ocsClient->getApplication($app['ocsid'], $version); if (is_array($remoteAppEntry) && array_key_exists('level', $remoteAppEntry)) { $apps[$key]['level'] = $remoteAppEntry['level']; } } } break; // not-installed apps // not-installed apps case 1: $apps = \OC_App::listAllApps(true, $includeUpdateInfo, $this->ocsClient); $apps = array_filter($apps, function ($app) { return !$app['active']; }); $version = \OCP\Util::getVersion(); foreach ($apps as $key => $app) { if (!array_key_exists('level', $app) && array_key_exists('ocsid', $app)) { $remoteAppEntry = $this->ocsClient->getApplication($app['ocsid'], $version); if (is_array($remoteAppEntry) && array_key_exists('level', $remoteAppEntry)) { $apps[$key]['level'] = $remoteAppEntry['level']; } } } usort($apps, function ($a, $b) { $a = (string) $a['name']; $b = (string) $b['name']; if ($a === $b) { return 0; } return $a < $b ? -1 : 1; }); break; default: $filter = $this->config->getSystemValue('appstore.experimental.enabled', false) ? 'all' : 'approved'; $apps = \OC_App::getAppstoreApps($filter, $category, $this->ocsClient); if (!$apps) { $apps = array(); } else { // don't list installed apps $installedApps = $this->getInstalledApps(false); $installedApps = array_map(function ($app) { if (isset($app['ocsid'])) { return $app['ocsid']; } return $app['id']; }, $installedApps); $apps = array_filter($apps, function ($app) use($installedApps) { return !in_array($app['id'], $installedApps); }); // show tooltip if app is downloaded from remote server $inactiveApps = $this->getInactiveApps(); foreach ($apps as &$app) { $app['needsDownload'] = !in_array($app['id'], $inactiveApps); } } // sort by score usort($apps, function ($a, $b) { $a = (int) $a['score']; $b = (int) $b['score']; if ($a === $b) { return 0; } return $a > $b ? -1 : 1; }); break; } } // fix groups to be an array $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n); $apps = array_map(function ($app) use($dependencyAnalyzer) { // fix groups $groups = array(); if (is_string($app['groups'])) { $groups = json_decode($app['groups']); } $app['groups'] = $groups; $app['canUnInstall'] = !$app['active'] && $app['removable']; // fix licence vs license if (isset($app['license']) && !isset($app['licence'])) { $app['licence'] = $app['license']; } // analyse dependencies $missing = $dependencyAnalyzer->analyze($app); $app['canInstall'] = empty($missing); $app['missingDependencies'] = $missing; $app['missingMinOwnCloudVersion'] = !isset($app['dependencies']['owncloud']['@attributes']['min-version']); $app['missingMaxOwnCloudVersion'] = !isset($app['dependencies']['owncloud']['@attributes']['max-version']); return $app; }, $apps); $this->cache->set($cacheName, $apps, 300); return ['apps' => $apps, 'status' => 'success']; }
/** * Get all available categories * @param int $category * @return array */ public function listApps($category = 0) { if (!is_null($this->cache->get('listApps-' . $category))) { $apps = $this->cache->get('listApps-' . $category); } else { switch ($category) { // installed apps case 0: $apps = \OC_App::listAllApps(true); $apps = array_filter($apps, function ($app) { return $app['active']; }); break; // not-installed apps // not-installed apps case 1: $apps = \OC_App::listAllApps(true); $apps = array_filter($apps, function ($app) { return !$app['active']; }); break; default: if ($category === 2) { $apps = \OC_App::getAppstoreApps('approved'); $apps = array_filter($apps, function ($app) { return isset($app['internalclass']) && $app['internalclass'] === 'recommendedapp'; }); } else { $apps = \OC_App::getAppstoreApps('approved', $category); } if (!$apps) { $apps = array(); } usort($apps, function ($a, $b) { $a = (int) $a['score']; $b = (int) $b['score']; if ($a === $b) { return 0; } return $a > $b ? -1 : 1; }); break; } } // fix groups to be an array $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n); $apps = array_map(function ($app) use($dependencyAnalyzer) { // fix groups $groups = array(); if (is_string($app['groups'])) { $groups = json_decode($app['groups']); } $app['groups'] = $groups; $app['canUnInstall'] = !$app['active'] && $app['removable']; // fix licence vs license if (isset($app['license']) && !isset($app['licence'])) { $app['licence'] = $app['license']; } // analyse dependencies $missing = $dependencyAnalyzer->analyze($app); $app['canInstall'] = empty($missing); $app['missingDependencies'] = $missing; return $app; }, $apps); $this->cache->set('listApps-' . $category, $apps, 300); return ['apps' => $apps, 'status' => 'success']; }