/** * 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); } }
/** * @return array */ public function getResults() { $cachedResults = $this->cache->get(self::CACHE_KEY); if (!is_null($cachedResults)) { return json_decode($cachedResults, true); } return json_decode($this->config->getAppValue('core', self::CACHE_KEY, '{}'), true); }
/** * @param string $key * @return mixed|null */ public function getFromCache($key) { if (is_null($this->cache) || !$this->isCached($key)) { return null; } $key = $this->getCacheKey($key); return json_decode(base64_decode($this->cache->get($key))); }
/** * @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; } }
/** * 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; } }
/** * 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()); }
/** * Fetches an object from the API. * If the object is cached already or a * failed "doesn't exist" response was cached, * that one will be returned. * * @param string $path * @return \OpenCloud\OpenStack\ObjectStorage\Resource\DataObject|bool object * or false if the object did not exist */ private function fetchObject($path) { if ($this->objectCache->hasKey($path)) { // might be "false" if object did not exist from last check return $this->objectCache->get($path); } try { $object = $this->getContainer()->getPartialObject($path); $this->objectCache->set($path, $object); return $object; } catch (ClientErrorResponseException $e) { // this exception happens when the object does not exist, which // is expected in most cases $this->objectCache->set($path, false); return false; } catch (ClientErrorResponseException $e) { // Expected response is "404 Not Found", so only log if it isn't if ($e->getResponse()->getStatusCode() !== 404) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); } return false; } }
/** * 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 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']; }
/** * Do what's needed to get the image from storage * depending on the type. * After this method is called $this->image must hold an * instance of \OCP\Image. */ protected function processImage() { $this->image = new Image(); $this->image->loadFromData($this->cache->get($this->key)); }
/** * 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']; }