| @@ -68,1393 +68,1393 @@ | ||
| 68 | 68 | use OC\AppFramework\Http\Request; | 
| 69 | 69 | |
| 70 | 70 |  class OC_Util { | 
| 71 | - public static $scripts = array(); | |
| 72 | - public static $styles = array(); | |
| 73 | - public static $headers = array(); | |
| 74 | - private static $rootMounted = false; | |
| 75 | - private static $fsSetup = false; | |
| 76 | - | |
| 77 | - /** @var array Local cache of version.php */ | |
| 78 | - private static $versionCache = null; | |
| 79 | - | |
| 80 | -	protected static function getAppManager() { | |
| 81 | - return \OC::$server->getAppManager(); | |
| 82 | - } | |
| 83 | - | |
| 84 | -	private static function initLocalStorageRootFS() { | |
| 85 | - // mount local file backend as root | |
| 86 | -		$configDataDirectory = \OC::$server->getSystemConfig()->getValue("datadirectory", OC::$SERVERROOT . "/data"); | |
| 87 | - //first set up the local "root" storage | |
| 88 | - \OC\Files\Filesystem::initMountManager(); | |
| 89 | -		if (!self::$rootMounted) { | |
| 90 | -			\OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir' => $configDataDirectory), '/'); | |
| 91 | - self::$rootMounted = true; | |
| 92 | - } | |
| 93 | - } | |
| 94 | - | |
| 95 | - /** | |
| 96 | - * mounting an object storage as the root fs will in essence remove the | |
| 97 | - * necessity of a data folder being present. | |
| 98 | - * TODO make home storage aware of this and use the object storage instead of local disk access | |
| 99 | - * | |
| 100 | - * @param array $config containing 'class' and optional 'arguments' | |
| 101 | - * @suppress PhanDeprecatedFunction | |
| 102 | - */ | |
| 103 | -	private static function initObjectStoreRootFS($config) { | |
| 104 | - // check misconfiguration | |
| 105 | -		if (empty($config['class'])) { | |
| 106 | -			\OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); | |
| 107 | - } | |
| 108 | -		if (!isset($config['arguments'])) { | |
| 109 | - $config['arguments'] = array(); | |
| 110 | - } | |
| 111 | - | |
| 112 | - // instantiate object store implementation | |
| 113 | - $name = $config['class']; | |
| 114 | -		if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { | |
| 115 | -			$segments = explode('\\', $name); | |
| 116 | - OC_App::loadApp(strtolower($segments[1])); | |
| 117 | - } | |
| 118 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); | |
| 119 | - // mount with plain / root object store implementation | |
| 120 | - $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; | |
| 121 | - | |
| 122 | - // mount object storage as root | |
| 123 | - \OC\Files\Filesystem::initMountManager(); | |
| 124 | -		if (!self::$rootMounted) { | |
| 125 | - \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); | |
| 126 | - self::$rootMounted = true; | |
| 127 | - } | |
| 128 | - } | |
| 129 | - | |
| 130 | - /** | |
| 131 | - * mounting an object storage as the root fs will in essence remove the | |
| 132 | - * necessity of a data folder being present. | |
| 133 | - * | |
| 134 | - * @param array $config containing 'class' and optional 'arguments' | |
| 135 | - * @suppress PhanDeprecatedFunction | |
| 136 | - */ | |
| 137 | -	private static function initObjectStoreMultibucketRootFS($config) { | |
| 138 | - // check misconfiguration | |
| 139 | -		if (empty($config['class'])) { | |
| 140 | -			\OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); | |
| 141 | - } | |
| 142 | -		if (!isset($config['arguments'])) { | |
| 143 | - $config['arguments'] = array(); | |
| 144 | - } | |
| 145 | - | |
| 146 | - // instantiate object store implementation | |
| 147 | - $name = $config['class']; | |
| 148 | -		if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { | |
| 149 | -			$segments = explode('\\', $name); | |
| 150 | - OC_App::loadApp(strtolower($segments[1])); | |
| 151 | - } | |
| 152 | - | |
| 153 | -		if (!isset($config['arguments']['bucket'])) { | |
| 154 | - $config['arguments']['bucket'] = ''; | |
| 155 | - } | |
| 156 | - // put the root FS always in first bucket for multibucket configuration | |
| 157 | - $config['arguments']['bucket'] .= '0'; | |
| 158 | - | |
| 159 | - $config['arguments']['objectstore'] = new $config['class']($config['arguments']); | |
| 160 | - // mount with plain / root object store implementation | |
| 161 | - $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; | |
| 162 | - | |
| 163 | - // mount object storage as root | |
| 164 | - \OC\Files\Filesystem::initMountManager(); | |
| 165 | -		if (!self::$rootMounted) { | |
| 166 | - \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); | |
| 167 | - self::$rootMounted = true; | |
| 168 | - } | |
| 169 | - } | |
| 170 | - | |
| 171 | - /** | |
| 172 | - * Can be set up | |
| 173 | - * | |
| 174 | - * @param string $user | |
| 175 | - * @return boolean | |
| 176 | - * @description configure the initial filesystem based on the configuration | |
| 177 | - * @suppress PhanDeprecatedFunction | |
| 178 | - * @suppress PhanAccessMethodInternal | |
| 179 | - */ | |
| 180 | -	public static function setupFS($user = '') { | |
| 181 | - //setting up the filesystem twice can only lead to trouble | |
| 182 | -		if (self::$fsSetup) { | |
| 183 | - return false; | |
| 184 | - } | |
| 185 | - | |
| 186 | -		\OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem'); | |
| 187 | - | |
| 188 | - // If we are not forced to load a specific user we load the one that is logged in | |
| 189 | -		if ($user === null) { | |
| 190 | - $user = ''; | |
| 191 | -		} else if ($user == "" && \OC::$server->getUserSession()->isLoggedIn()) { | |
| 192 | - $user = OC_User::getUser(); | |
| 193 | - } | |
| 194 | - | |
| 195 | - // load all filesystem apps before, so no setup-hook gets lost | |
| 196 | -		OC_App::loadApps(array('filesystem')); | |
| 197 | - | |
| 198 | - // the filesystem will finish when $user is not empty, | |
| 199 | - // mark fs setup here to avoid doing the setup from loading | |
| 200 | - // OC_Filesystem | |
| 201 | -		if ($user != '') { | |
| 202 | - self::$fsSetup = true; | |
| 203 | - } | |
| 204 | - | |
| 205 | - \OC\Files\Filesystem::initMountManager(); | |
| 206 | - | |
| 207 | - $prevLogging = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); | |
| 208 | -		\OC\Files\Filesystem::addStorageWrapper('mount_options', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { | |
| 209 | -			if ($storage->instanceOfStorage('\OC\Files\Storage\Common')) { | |
| 210 | - /** @var \OC\Files\Storage\Common $storage */ | |
| 211 | - $storage->setMountOptions($mount->getOptions()); | |
| 212 | - } | |
| 213 | - return $storage; | |
| 214 | - }); | |
| 215 | - | |
| 216 | -		\OC\Files\Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, \OCP\Files\Storage\IStorage $storage, \OCP\Files\Mount\IMountPoint $mount) { | |
| 217 | -			if (!$mount->getOption('enable_sharing', true)) { | |
| 218 | - return new \OC\Files\Storage\Wrapper\PermissionsMask([ | |
| 219 | - 'storage' => $storage, | |
| 220 | - 'mask' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_SHARE | |
| 221 | - ]); | |
| 222 | - } | |
| 223 | - return $storage; | |
| 224 | - }); | |
| 225 | - | |
| 226 | - // install storage availability wrapper, before most other wrappers | |
| 227 | -		\OC\Files\Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, \OCP\Files\Storage\IStorage $storage) { | |
| 228 | -			if (!$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) { | |
| 229 | - return new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]); | |
| 230 | - } | |
| 231 | - return $storage; | |
| 232 | - }); | |
| 233 | - | |
| 234 | -		\OC\Files\Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { | |
| 235 | -			if ($mount->getOption('encoding_compatibility', false) && !$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) { | |
| 236 | - return new \OC\Files\Storage\Wrapper\Encoding(['storage' => $storage]); | |
| 237 | - } | |
| 238 | - return $storage; | |
| 239 | - }); | |
| 240 | - | |
| 241 | -		\OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { | |
| 242 | - // set up quota for home storages, even for other users | |
| 243 | - // which can happen when using sharing | |
| 244 | - | |
| 245 | - /** | |
| 246 | - * @var \OC\Files\Storage\Storage $storage | |
| 247 | - */ | |
| 248 | -			if ($storage->instanceOfStorage('\OC\Files\Storage\Home') | |
| 249 | -				|| $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') | |
| 250 | -			) { | |
| 251 | - /** @var \OC\Files\Storage\Home $storage */ | |
| 252 | -				if (is_object($storage->getUser())) { | |
| 253 | - $user = $storage->getUser()->getUID(); | |
| 254 | - $quota = OC_Util::getUserQuota($user); | |
| 255 | -					if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { | |
| 256 | -						return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files')); | |
| 257 | - } | |
| 258 | - } | |
| 259 | - } | |
| 260 | - | |
| 261 | - return $storage; | |
| 262 | - }); | |
| 263 | - | |
| 264 | -		\OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, \OCP\Files\Storage\IStorage $storage, \OCP\Files\Mount\IMountPoint $mount) { | |
| 265 | - /* | |
| 71 | + public static $scripts = array(); | |
| 72 | + public static $styles = array(); | |
| 73 | + public static $headers = array(); | |
| 74 | + private static $rootMounted = false; | |
| 75 | + private static $fsSetup = false; | |
| 76 | + | |
| 77 | + /** @var array Local cache of version.php */ | |
| 78 | + private static $versionCache = null; | |
| 79 | + | |
| 80 | +    protected static function getAppManager() { | |
| 81 | + return \OC::$server->getAppManager(); | |
| 82 | + } | |
| 83 | + | |
| 84 | +    private static function initLocalStorageRootFS() { | |
| 85 | + // mount local file backend as root | |
| 86 | +        $configDataDirectory = \OC::$server->getSystemConfig()->getValue("datadirectory", OC::$SERVERROOT . "/data"); | |
| 87 | + //first set up the local "root" storage | |
| 88 | + \OC\Files\Filesystem::initMountManager(); | |
| 89 | +        if (!self::$rootMounted) { | |
| 90 | +            \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir' => $configDataDirectory), '/'); | |
| 91 | + self::$rootMounted = true; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * mounting an object storage as the root fs will in essence remove the | |
| 97 | + * necessity of a data folder being present. | |
| 98 | + * TODO make home storage aware of this and use the object storage instead of local disk access | |
| 99 | + * | |
| 100 | + * @param array $config containing 'class' and optional 'arguments' | |
| 101 | + * @suppress PhanDeprecatedFunction | |
| 102 | + */ | |
| 103 | +    private static function initObjectStoreRootFS($config) { | |
| 104 | + // check misconfiguration | |
| 105 | +        if (empty($config['class'])) { | |
| 106 | +            \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); | |
| 107 | + } | |
| 108 | +        if (!isset($config['arguments'])) { | |
| 109 | + $config['arguments'] = array(); | |
| 110 | + } | |
| 111 | + | |
| 112 | + // instantiate object store implementation | |
| 113 | + $name = $config['class']; | |
| 114 | +        if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { | |
| 115 | +            $segments = explode('\\', $name); | |
| 116 | + OC_App::loadApp(strtolower($segments[1])); | |
| 117 | + } | |
| 118 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); | |
| 119 | + // mount with plain / root object store implementation | |
| 120 | + $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; | |
| 121 | + | |
| 122 | + // mount object storage as root | |
| 123 | + \OC\Files\Filesystem::initMountManager(); | |
| 124 | +        if (!self::$rootMounted) { | |
| 125 | + \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); | |
| 126 | + self::$rootMounted = true; | |
| 127 | + } | |
| 128 | + } | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * mounting an object storage as the root fs will in essence remove the | |
| 132 | + * necessity of a data folder being present. | |
| 133 | + * | |
| 134 | + * @param array $config containing 'class' and optional 'arguments' | |
| 135 | + * @suppress PhanDeprecatedFunction | |
| 136 | + */ | |
| 137 | +    private static function initObjectStoreMultibucketRootFS($config) { | |
| 138 | + // check misconfiguration | |
| 139 | +        if (empty($config['class'])) { | |
| 140 | +            \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR); | |
| 141 | + } | |
| 142 | +        if (!isset($config['arguments'])) { | |
| 143 | + $config['arguments'] = array(); | |
| 144 | + } | |
| 145 | + | |
| 146 | + // instantiate object store implementation | |
| 147 | + $name = $config['class']; | |
| 148 | +        if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) { | |
| 149 | +            $segments = explode('\\', $name); | |
| 150 | + OC_App::loadApp(strtolower($segments[1])); | |
| 151 | + } | |
| 152 | + | |
| 153 | +        if (!isset($config['arguments']['bucket'])) { | |
| 154 | + $config['arguments']['bucket'] = ''; | |
| 155 | + } | |
| 156 | + // put the root FS always in first bucket for multibucket configuration | |
| 157 | + $config['arguments']['bucket'] .= '0'; | |
| 158 | + | |
| 159 | + $config['arguments']['objectstore'] = new $config['class']($config['arguments']); | |
| 160 | + // mount with plain / root object store implementation | |
| 161 | + $config['class'] = '\OC\Files\ObjectStore\ObjectStoreStorage'; | |
| 162 | + | |
| 163 | + // mount object storage as root | |
| 164 | + \OC\Files\Filesystem::initMountManager(); | |
| 165 | +        if (!self::$rootMounted) { | |
| 166 | + \OC\Files\Filesystem::mount($config['class'], $config['arguments'], '/'); | |
| 167 | + self::$rootMounted = true; | |
| 168 | + } | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | + * Can be set up | |
| 173 | + * | |
| 174 | + * @param string $user | |
| 175 | + * @return boolean | |
| 176 | + * @description configure the initial filesystem based on the configuration | |
| 177 | + * @suppress PhanDeprecatedFunction | |
| 178 | + * @suppress PhanAccessMethodInternal | |
| 179 | + */ | |
| 180 | +    public static function setupFS($user = '') { | |
| 181 | + //setting up the filesystem twice can only lead to trouble | |
| 182 | +        if (self::$fsSetup) { | |
| 183 | + return false; | |
| 184 | + } | |
| 185 | + | |
| 186 | +        \OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem'); | |
| 187 | + | |
| 188 | + // If we are not forced to load a specific user we load the one that is logged in | |
| 189 | +        if ($user === null) { | |
| 190 | + $user = ''; | |
| 191 | +        } else if ($user == "" && \OC::$server->getUserSession()->isLoggedIn()) { | |
| 192 | + $user = OC_User::getUser(); | |
| 193 | + } | |
| 194 | + | |
| 195 | + // load all filesystem apps before, so no setup-hook gets lost | |
| 196 | +        OC_App::loadApps(array('filesystem')); | |
| 197 | + | |
| 198 | + // the filesystem will finish when $user is not empty, | |
| 199 | + // mark fs setup here to avoid doing the setup from loading | |
| 200 | + // OC_Filesystem | |
| 201 | +        if ($user != '') { | |
| 202 | + self::$fsSetup = true; | |
| 203 | + } | |
| 204 | + | |
| 205 | + \OC\Files\Filesystem::initMountManager(); | |
| 206 | + | |
| 207 | + $prevLogging = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); | |
| 208 | +        \OC\Files\Filesystem::addStorageWrapper('mount_options', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { | |
| 209 | +            if ($storage->instanceOfStorage('\OC\Files\Storage\Common')) { | |
| 210 | + /** @var \OC\Files\Storage\Common $storage */ | |
| 211 | + $storage->setMountOptions($mount->getOptions()); | |
| 212 | + } | |
| 213 | + return $storage; | |
| 214 | + }); | |
| 215 | + | |
| 216 | +        \OC\Files\Filesystem::addStorageWrapper('enable_sharing', function ($mountPoint, \OCP\Files\Storage\IStorage $storage, \OCP\Files\Mount\IMountPoint $mount) { | |
| 217 | +            if (!$mount->getOption('enable_sharing', true)) { | |
| 218 | + return new \OC\Files\Storage\Wrapper\PermissionsMask([ | |
| 219 | + 'storage' => $storage, | |
| 220 | + 'mask' => \OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_SHARE | |
| 221 | + ]); | |
| 222 | + } | |
| 223 | + return $storage; | |
| 224 | + }); | |
| 225 | + | |
| 226 | + // install storage availability wrapper, before most other wrappers | |
| 227 | +        \OC\Files\Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, \OCP\Files\Storage\IStorage $storage) { | |
| 228 | +            if (!$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) { | |
| 229 | + return new \OC\Files\Storage\Wrapper\Availability(['storage' => $storage]); | |
| 230 | + } | |
| 231 | + return $storage; | |
| 232 | + }); | |
| 233 | + | |
| 234 | +        \OC\Files\Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { | |
| 235 | +            if ($mount->getOption('encoding_compatibility', false) && !$storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage') && !$storage->isLocal()) { | |
| 236 | + return new \OC\Files\Storage\Wrapper\Encoding(['storage' => $storage]); | |
| 237 | + } | |
| 238 | + return $storage; | |
| 239 | + }); | |
| 240 | + | |
| 241 | +        \OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { | |
| 242 | + // set up quota for home storages, even for other users | |
| 243 | + // which can happen when using sharing | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * @var \OC\Files\Storage\Storage $storage | |
| 247 | + */ | |
| 248 | +            if ($storage->instanceOfStorage('\OC\Files\Storage\Home') | |
| 249 | +                || $storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage') | |
| 250 | +            ) { | |
| 251 | + /** @var \OC\Files\Storage\Home $storage */ | |
| 252 | +                if (is_object($storage->getUser())) { | |
| 253 | + $user = $storage->getUser()->getUID(); | |
| 254 | + $quota = OC_Util::getUserQuota($user); | |
| 255 | +                    if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) { | |
| 256 | +                        return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files')); | |
| 257 | + } | |
| 258 | + } | |
| 259 | + } | |
| 260 | + | |
| 261 | + return $storage; | |
| 262 | + }); | |
| 263 | + | |
| 264 | +        \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, \OCP\Files\Storage\IStorage $storage, \OCP\Files\Mount\IMountPoint $mount) { | |
| 265 | + /* | |
| 266 | 266 | * Do not allow any operations that modify the storage | 
| 267 | 267 | */ | 
| 268 | -			if ($mount->getOption('readonly', false)) { | |
| 269 | - return new \OC\Files\Storage\Wrapper\PermissionsMask([ | |
| 270 | - 'storage' => $storage, | |
| 271 | - 'mask' => \OCP\Constants::PERMISSION_ALL & ~( | |
| 272 | - \OCP\Constants::PERMISSION_UPDATE | | |
| 273 | - \OCP\Constants::PERMISSION_CREATE | | |
| 274 | - \OCP\Constants::PERMISSION_DELETE | |
| 275 | - ), | |
| 276 | - ]); | |
| 277 | - } | |
| 278 | - return $storage; | |
| 279 | - }); | |
| 280 | - | |
| 281 | -		OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user)); | |
| 282 | - | |
| 283 | - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($prevLogging); | |
| 284 | - | |
| 285 | - //check if we are using an object storage | |
| 286 | -		$objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null); | |
| 287 | -		$objectStoreMultibucket = \OC::$server->getSystemConfig()->getValue('objectstore_multibucket', null); | |
| 288 | - | |
| 289 | - // use the same order as in ObjectHomeMountProvider | |
| 290 | -		if (isset($objectStoreMultibucket)) { | |
| 291 | - self::initObjectStoreMultibucketRootFS($objectStoreMultibucket); | |
| 292 | -		} elseif (isset($objectStore)) { | |
| 293 | - self::initObjectStoreRootFS($objectStore); | |
| 294 | -		} else { | |
| 295 | - self::initLocalStorageRootFS(); | |
| 296 | - } | |
| 297 | - | |
| 298 | -		if ($user != '' && !\OC::$server->getUserManager()->userExists($user)) { | |
| 299 | -			\OC::$server->getEventLogger()->end('setup_fs'); | |
| 300 | - return false; | |
| 301 | - } | |
| 302 | - | |
| 303 | - //if we aren't logged in, there is no use to set up the filesystem | |
| 304 | -		if ($user != "") { | |
| 305 | - | |
| 306 | - $userDir = '/' . $user . '/files'; | |
| 307 | - | |
| 308 | - //jail the user into his "home" directory | |
| 309 | - \OC\Files\Filesystem::init($user, $userDir); | |
| 310 | - | |
| 311 | -			OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir)); | |
| 312 | - } | |
| 313 | -		\OC::$server->getEventLogger()->end('setup_fs'); | |
| 314 | - return true; | |
| 315 | - } | |
| 316 | - | |
| 317 | - /** | |
| 318 | - * check if a password is required for each public link | |
| 319 | - * | |
| 320 | - * @return boolean | |
| 321 | - * @suppress PhanDeprecatedFunction | |
| 322 | - */ | |
| 323 | -	public static function isPublicLinkPasswordRequired() { | |
| 324 | -		$enforcePassword = \OC::$server->getConfig()->getAppValue('core', 'shareapi_enforce_links_password', 'no'); | |
| 325 | - return $enforcePassword === 'yes'; | |
| 326 | - } | |
| 327 | - | |
| 328 | - /** | |
| 329 | - * check if sharing is disabled for the current user | |
| 330 | - * @param IConfig $config | |
| 331 | - * @param IGroupManager $groupManager | |
| 332 | - * @param IUser|null $user | |
| 333 | - * @return bool | |
| 334 | - */ | |
| 335 | -	public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) { | |
| 336 | -		if ($config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { | |
| 337 | -			$groupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', ''); | |
| 338 | - $excludedGroups = json_decode($groupsList); | |
| 339 | -			if (is_null($excludedGroups)) { | |
| 340 | -				$excludedGroups = explode(',', $groupsList); | |
| 341 | - $newValue = json_encode($excludedGroups); | |
| 342 | -				$config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); | |
| 343 | - } | |
| 344 | - $usersGroups = $groupManager->getUserGroupIds($user); | |
| 345 | -			if (!empty($usersGroups)) { | |
| 346 | - $remainingGroups = array_diff($usersGroups, $excludedGroups); | |
| 347 | - // if the user is only in groups which are disabled for sharing then | |
| 348 | - // sharing is also disabled for the user | |
| 349 | -				if (empty($remainingGroups)) { | |
| 350 | - return true; | |
| 351 | - } | |
| 352 | - } | |
| 353 | - } | |
| 354 | - return false; | |
| 355 | - } | |
| 356 | - | |
| 357 | - /** | |
| 358 | - * check if share API enforces a default expire date | |
| 359 | - * | |
| 360 | - * @return boolean | |
| 361 | - * @suppress PhanDeprecatedFunction | |
| 362 | - */ | |
| 363 | -	public static function isDefaultExpireDateEnforced() { | |
| 364 | -		$isDefaultExpireDateEnabled = \OC::$server->getConfig()->getAppValue('core', 'shareapi_default_expire_date', 'no'); | |
| 365 | - $enforceDefaultExpireDate = false; | |
| 366 | -		if ($isDefaultExpireDateEnabled === 'yes') { | |
| 367 | -			$value = \OC::$server->getConfig()->getAppValue('core', 'shareapi_enforce_expire_date', 'no'); | |
| 368 | - $enforceDefaultExpireDate = $value === 'yes'; | |
| 369 | - } | |
| 370 | - | |
| 371 | - return $enforceDefaultExpireDate; | |
| 372 | - } | |
| 373 | - | |
| 374 | - /** | |
| 375 | - * Get the quota of a user | |
| 376 | - * | |
| 377 | - * @param string $userId | |
| 378 | - * @return float Quota bytes | |
| 379 | - */ | |
| 380 | -	public static function getUserQuota($userId) { | |
| 381 | - $user = \OC::$server->getUserManager()->get($userId); | |
| 382 | -		if (is_null($user)) { | |
| 383 | - return \OCP\Files\FileInfo::SPACE_UNLIMITED; | |
| 384 | - } | |
| 385 | - $userQuota = $user->getQuota(); | |
| 386 | -		if($userQuota === 'none') { | |
| 387 | - return \OCP\Files\FileInfo::SPACE_UNLIMITED; | |
| 388 | - } | |
| 389 | - return OC_Helper::computerFileSize($userQuota); | |
| 390 | - } | |
| 391 | - | |
| 392 | - /** | |
| 393 | - * copies the skeleton to the users /files | |
| 394 | - * | |
| 395 | - * @param string $userId | |
| 396 | - * @param \OCP\Files\Folder $userDirectory | |
| 397 | - * @throws \OCP\Files\NotFoundException | |
| 398 | - * @throws \OCP\Files\NotPermittedException | |
| 399 | - * @suppress PhanDeprecatedFunction | |
| 400 | - */ | |
| 401 | -	public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) { | |
| 402 | - | |
| 403 | -		$plainSkeletonDirectory = \OC::$server->getConfig()->getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton'); | |
| 404 | - $userLang = \OC::$server->getL10NFactory()->findLanguage(); | |
| 405 | -		$skeletonDirectory = str_replace('{lang}', $userLang, $plainSkeletonDirectory); | |
| 406 | - | |
| 407 | -		if (!file_exists($skeletonDirectory)) { | |
| 408 | - $dialectStart = strpos($userLang, '_'); | |
| 409 | -			if ($dialectStart !== false) { | |
| 410 | -				$skeletonDirectory = str_replace('{lang}', substr($userLang, 0, $dialectStart), $plainSkeletonDirectory); | |
| 411 | - } | |
| 412 | -			if ($dialectStart === false || !file_exists($skeletonDirectory)) { | |
| 413 | -				$skeletonDirectory = str_replace('{lang}', 'default', $plainSkeletonDirectory); | |
| 414 | - } | |
| 415 | -			if (!file_exists($skeletonDirectory)) { | |
| 416 | - $skeletonDirectory = ''; | |
| 417 | - } | |
| 418 | - } | |
| 419 | - | |
| 420 | -		$instanceId = \OC::$server->getConfig()->getSystemValue('instanceid', ''); | |
| 421 | - | |
| 422 | -		if ($instanceId === null) { | |
| 423 | -			throw new \RuntimeException('no instance id!'); | |
| 424 | - } | |
| 425 | - $appdata = 'appdata_' . $instanceId; | |
| 426 | -		if ($userId === $appdata) { | |
| 427 | -			throw new \RuntimeException('username is reserved name: ' . $appdata); | |
| 428 | - } | |
| 429 | - | |
| 430 | -		if (!empty($skeletonDirectory)) { | |
| 431 | - \OCP\Util::writeLog( | |
| 432 | - 'files_skeleton', | |
| 433 | -				'copying skeleton for '.$userId.' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'), | |
| 434 | - ILogger::DEBUG | |
| 435 | - ); | |
| 436 | - self::copyr($skeletonDirectory, $userDirectory); | |
| 437 | - // update the file cache | |
| 438 | -			$userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE); | |
| 439 | - } | |
| 440 | - } | |
| 441 | - | |
| 442 | - /** | |
| 443 | - * copies a directory recursively by using streams | |
| 444 | - * | |
| 445 | - * @param string $source | |
| 446 | - * @param \OCP\Files\Folder $target | |
| 447 | - * @return void | |
| 448 | - */ | |
| 449 | -	public static function copyr($source, \OCP\Files\Folder $target) { | |
| 450 | - $logger = \OC::$server->getLogger(); | |
| 451 | - | |
| 452 | - // Verify if folder exists | |
| 453 | - $dir = opendir($source); | |
| 454 | -		if($dir === false) { | |
| 455 | -			$logger->error(sprintf('Could not opendir "%s"', $source), ['app' => 'core']); | |
| 456 | - return; | |
| 457 | - } | |
| 458 | - | |
| 459 | - // Copy the files | |
| 460 | -		while (false !== ($file = readdir($dir))) { | |
| 461 | -			if (!\OC\Files\Filesystem::isIgnoredDir($file)) { | |
| 462 | -				if (is_dir($source . '/' . $file)) { | |
| 463 | - $child = $target->newFolder($file); | |
| 464 | - self::copyr($source . '/' . $file, $child); | |
| 465 | -				} else { | |
| 466 | - $child = $target->newFile($file); | |
| 467 | - $sourceStream = fopen($source . '/' . $file, 'r'); | |
| 468 | -					if($sourceStream === false) { | |
| 469 | -						$logger->error(sprintf('Could not fopen "%s"', $source . '/' . $file), ['app' => 'core']); | |
| 470 | - closedir($dir); | |
| 471 | - return; | |
| 472 | - } | |
| 473 | -					stream_copy_to_stream($sourceStream, $child->fopen('w')); | |
| 474 | - } | |
| 475 | - } | |
| 476 | - } | |
| 477 | - closedir($dir); | |
| 478 | - } | |
| 479 | - | |
| 480 | - /** | |
| 481 | - * @return void | |
| 482 | - * @suppress PhanUndeclaredMethod | |
| 483 | - */ | |
| 484 | -	public static function tearDownFS() { | |
| 485 | - \OC\Files\Filesystem::tearDown(); | |
| 486 | - \OC::$server->getRootFolder()->clearCache(); | |
| 487 | - self::$fsSetup = false; | |
| 488 | - self::$rootMounted = false; | |
| 489 | - } | |
| 490 | - | |
| 491 | - /** | |
| 492 | - * get the current installed version of ownCloud | |
| 493 | - * | |
| 494 | - * @return array | |
| 495 | - */ | |
| 496 | -	public static function getVersion() { | |
| 497 | - OC_Util::loadVersion(); | |
| 498 | - return self::$versionCache['OC_Version']; | |
| 499 | - } | |
| 500 | - | |
| 501 | - /** | |
| 502 | - * get the current installed version string of ownCloud | |
| 503 | - * | |
| 504 | - * @return string | |
| 505 | - */ | |
| 506 | -	public static function getVersionString() { | |
| 507 | - OC_Util::loadVersion(); | |
| 508 | - return self::$versionCache['OC_VersionString']; | |
| 509 | - } | |
| 510 | - | |
| 511 | - /** | |
| 512 | - * @deprecated the value is of no use anymore | |
| 513 | - * @return string | |
| 514 | - */ | |
| 515 | -	public static function getEditionString() { | |
| 516 | - return ''; | |
| 517 | - } | |
| 518 | - | |
| 519 | - /** | |
| 520 | - * @description get the update channel of the current installed of ownCloud. | |
| 521 | - * @return string | |
| 522 | - */ | |
| 523 | -	public static function getChannel() { | |
| 524 | - OC_Util::loadVersion(); | |
| 525 | -		return \OC::$server->getConfig()->getSystemValue('updater.release.channel', self::$versionCache['OC_Channel']); | |
| 526 | - } | |
| 527 | - | |
| 528 | - /** | |
| 529 | - * @description get the build number of the current installed of ownCloud. | |
| 530 | - * @return string | |
| 531 | - */ | |
| 532 | -	public static function getBuild() { | |
| 533 | - OC_Util::loadVersion(); | |
| 534 | - return self::$versionCache['OC_Build']; | |
| 535 | - } | |
| 536 | - | |
| 537 | - /** | |
| 538 | - * @description load the version.php into the session as cache | |
| 539 | - * @suppress PhanUndeclaredVariable | |
| 540 | - */ | |
| 541 | -	private static function loadVersion() { | |
| 542 | -		if (self::$versionCache !== null) { | |
| 543 | - return; | |
| 544 | - } | |
| 545 | - | |
| 546 | - $timestamp = filemtime(OC::$SERVERROOT . '/version.php'); | |
| 547 | - require OC::$SERVERROOT . '/version.php'; | |
| 548 | - /** @var $timestamp int */ | |
| 549 | - self::$versionCache['OC_Version_Timestamp'] = $timestamp; | |
| 550 | - /** @var $OC_Version string */ | |
| 551 | - self::$versionCache['OC_Version'] = $OC_Version; | |
| 552 | - /** @var $OC_VersionString string */ | |
| 553 | - self::$versionCache['OC_VersionString'] = $OC_VersionString; | |
| 554 | - /** @var $OC_Build string */ | |
| 555 | - self::$versionCache['OC_Build'] = $OC_Build; | |
| 556 | - | |
| 557 | - /** @var $OC_Channel string */ | |
| 558 | - self::$versionCache['OC_Channel'] = $OC_Channel; | |
| 559 | - } | |
| 560 | - | |
| 561 | - /** | |
| 562 | - * generates a path for JS/CSS files. If no application is provided it will create the path for core. | |
| 563 | - * | |
| 564 | - * @param string $application application to get the files from | |
| 565 | - * @param string $directory directory within this application (css, js, vendor, etc) | |
| 566 | - * @param string $file the file inside of the above folder | |
| 567 | - * @return string the path | |
| 568 | - */ | |
| 569 | -	private static function generatePath($application, $directory, $file) { | |
| 570 | -		if (is_null($file)) { | |
| 571 | - $file = $application; | |
| 572 | - $application = ""; | |
| 573 | - } | |
| 574 | -		if (!empty($application)) { | |
| 575 | - return "$application/$directory/$file"; | |
| 576 | -		} else { | |
| 577 | - return "$directory/$file"; | |
| 578 | - } | |
| 579 | - } | |
| 580 | - | |
| 581 | - /** | |
| 582 | - * add a javascript file | |
| 583 | - * | |
| 584 | - * @param string $application application id | |
| 585 | - * @param string|null $file filename | |
| 586 | - * @param bool $prepend prepend the Script to the beginning of the list | |
| 587 | - * @return void | |
| 588 | - */ | |
| 589 | -	public static function addScript($application, $file = null, $prepend = false) { | |
| 590 | - $path = OC_Util::generatePath($application, 'js', $file); | |
| 591 | - | |
| 592 | - // core js files need separate handling | |
| 593 | -		if ($application !== 'core' && $file !== null) { | |
| 594 | - self::addTranslations ( $application ); | |
| 595 | - } | |
| 596 | - self::addExternalResource($application, $prepend, $path, "script"); | |
| 597 | - } | |
| 598 | - | |
| 599 | - /** | |
| 600 | - * add a javascript file from the vendor sub folder | |
| 601 | - * | |
| 602 | - * @param string $application application id | |
| 603 | - * @param string|null $file filename | |
| 604 | - * @param bool $prepend prepend the Script to the beginning of the list | |
| 605 | - * @return void | |
| 606 | - */ | |
| 607 | -	public static function addVendorScript($application, $file = null, $prepend = false) { | |
| 608 | - $path = OC_Util::generatePath($application, 'vendor', $file); | |
| 609 | - self::addExternalResource($application, $prepend, $path, "script"); | |
| 610 | - } | |
| 611 | - | |
| 612 | - /** | |
| 613 | - * add a translation JS file | |
| 614 | - * | |
| 615 | - * @param string $application application id | |
| 616 | - * @param string|null $languageCode language code, defaults to the current language | |
| 617 | - * @param bool|null $prepend prepend the Script to the beginning of the list | |
| 618 | - */ | |
| 619 | -	public static function addTranslations($application, $languageCode = null, $prepend = false) { | |
| 620 | -		if (is_null($languageCode)) { | |
| 621 | - $languageCode = \OC::$server->getL10NFactory()->findLanguage($application); | |
| 622 | - } | |
| 623 | -		if (!empty($application)) { | |
| 624 | - $path = "$application/l10n/$languageCode"; | |
| 625 | -		} else { | |
| 626 | - $path = "l10n/$languageCode"; | |
| 627 | - } | |
| 628 | - self::addExternalResource($application, $prepend, $path, "script"); | |
| 629 | - } | |
| 630 | - | |
| 631 | - /** | |
| 632 | - * add a css file | |
| 633 | - * | |
| 634 | - * @param string $application application id | |
| 635 | - * @param string|null $file filename | |
| 636 | - * @param bool $prepend prepend the Style to the beginning of the list | |
| 637 | - * @return void | |
| 638 | - */ | |
| 639 | -	public static function addStyle($application, $file = null, $prepend = false) { | |
| 640 | - $path = OC_Util::generatePath($application, 'css', $file); | |
| 641 | - self::addExternalResource($application, $prepend, $path, "style"); | |
| 642 | - } | |
| 643 | - | |
| 644 | - /** | |
| 645 | - * add a css file from the vendor sub folder | |
| 646 | - * | |
| 647 | - * @param string $application application id | |
| 648 | - * @param string|null $file filename | |
| 649 | - * @param bool $prepend prepend the Style to the beginning of the list | |
| 650 | - * @return void | |
| 651 | - */ | |
| 652 | -	public static function addVendorStyle($application, $file = null, $prepend = false) { | |
| 653 | - $path = OC_Util::generatePath($application, 'vendor', $file); | |
| 654 | - self::addExternalResource($application, $prepend, $path, "style"); | |
| 655 | - } | |
| 656 | - | |
| 657 | - /** | |
| 658 | - * add an external resource css/js file | |
| 659 | - * | |
| 660 | - * @param string $application application id | |
| 661 | - * @param bool $prepend prepend the file to the beginning of the list | |
| 662 | - * @param string $path | |
| 663 | - * @param string $type (script or style) | |
| 664 | - * @return void | |
| 665 | - */ | |
| 666 | -	private static function addExternalResource($application, $prepend, $path, $type = "script") { | |
| 667 | - | |
| 668 | -		if ($type === "style") { | |
| 669 | -			if (!in_array($path, self::$styles)) { | |
| 670 | -				if ($prepend === true) { | |
| 671 | - array_unshift ( self::$styles, $path ); | |
| 672 | -				} else { | |
| 673 | - self::$styles[] = $path; | |
| 674 | - } | |
| 675 | - } | |
| 676 | -		} elseif ($type === "script") { | |
| 677 | -			if (!in_array($path, self::$scripts)) { | |
| 678 | -				if ($prepend === true) { | |
| 679 | - array_unshift ( self::$scripts, $path ); | |
| 680 | -				} else { | |
| 681 | - self::$scripts [] = $path; | |
| 682 | - } | |
| 683 | - } | |
| 684 | - } | |
| 685 | - } | |
| 686 | - | |
| 687 | - /** | |
| 688 | - * Add a custom element to the header | |
| 689 | - * If $text is null then the element will be written as empty element. | |
| 690 | - * So use "" to get a closing tag. | |
| 691 | - * @param string $tag tag name of the element | |
| 692 | - * @param array $attributes array of attributes for the element | |
| 693 | - * @param string $text the text content for the element | |
| 694 | - * @param bool $prepend prepend the header to the beginning of the list | |
| 695 | - */ | |
| 696 | -	public static function addHeader($tag, $attributes, $text = null, $prepend = false) { | |
| 697 | - $header = array( | |
| 698 | - 'tag' => $tag, | |
| 699 | - 'attributes' => $attributes, | |
| 700 | - 'text' => $text | |
| 701 | - ); | |
| 702 | -		if ($prepend === true) { | |
| 703 | - array_unshift (self::$headers, $header); | |
| 704 | - | |
| 705 | -		} else { | |
| 706 | - self::$headers[] = $header; | |
| 707 | - } | |
| 708 | - } | |
| 709 | - | |
| 710 | - /** | |
| 711 | - * check if the current server configuration is suitable for ownCloud | |
| 712 | - * | |
| 713 | - * @param \OC\SystemConfig $config | |
| 714 | - * @return array arrays with error messages and hints | |
| 715 | - */ | |
| 716 | -	public static function checkServer(\OC\SystemConfig $config) { | |
| 717 | -		$l = \OC::$server->getL10N('lib'); | |
| 718 | - $errors = array(); | |
| 719 | -		$CONFIG_DATADIRECTORY = $config->getValue('datadirectory', OC::$SERVERROOT . '/data'); | |
| 720 | - | |
| 721 | -		if (!self::needUpgrade($config) && $config->getValue('installed', false)) { | |
| 722 | - // this check needs to be done every time | |
| 723 | - $errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY); | |
| 724 | - } | |
| 725 | - | |
| 726 | - // Assume that if checkServer() succeeded before in this session, then all is fine. | |
| 727 | -		if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) { | |
| 728 | - return $errors; | |
| 729 | - } | |
| 730 | - | |
| 731 | - $webServerRestart = false; | |
| 732 | - $setup = new \OC\Setup( | |
| 733 | - $config, | |
| 734 | - \OC::$server->getIniWrapper(), | |
| 735 | -			\OC::$server->getL10N('lib'), | |
| 736 | - \OC::$server->query(\OCP\Defaults::class), | |
| 737 | - \OC::$server->getLogger(), | |
| 738 | - \OC::$server->getSecureRandom(), | |
| 739 | - \OC::$server->query(\OC\Installer::class) | |
| 740 | - ); | |
| 741 | - | |
| 742 | - $urlGenerator = \OC::$server->getURLGenerator(); | |
| 743 | - | |
| 744 | - $availableDatabases = $setup->getSupportedDatabases(); | |
| 745 | -		if (empty($availableDatabases)) { | |
| 746 | - $errors[] = array( | |
| 747 | -				'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'), | |
| 748 | - 'hint' => '' //TODO: sane hint | |
| 749 | - ); | |
| 750 | - $webServerRestart = true; | |
| 751 | - } | |
| 752 | - | |
| 753 | - // Check if config folder is writable. | |
| 754 | -		if(!OC_Helper::isReadOnlyConfigEnabled()) { | |
| 755 | -			if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) { | |
| 756 | - $errors[] = array( | |
| 757 | -					'error' => $l->t('Cannot write into "config" directory'), | |
| 758 | -					'hint' => $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s', | |
| 759 | -						[ $urlGenerator->linkToDocs('admin-dir_permissions') ]) . '. ' | |
| 760 | -						. $l->t('Or, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it. See %s', | |
| 761 | -						[ $urlGenerator->linkToDocs('admin-config') ] ) | |
| 762 | - ); | |
| 763 | - } | |
| 764 | - } | |
| 765 | - | |
| 766 | - // Check if there is a writable install folder. | |
| 767 | -		if ($config->getValue('appstoreenabled', true)) { | |
| 768 | - if (OC_App::getInstallPath() === null | |
| 769 | - || !is_writable(OC_App::getInstallPath()) | |
| 770 | - || !is_readable(OC_App::getInstallPath()) | |
| 771 | -			) { | |
| 772 | - $errors[] = array( | |
| 773 | -					'error' => $l->t('Cannot write into "apps" directory'), | |
| 774 | -					'hint' => $l->t('This can usually be fixed by giving the webserver write access to the apps directory' | |
| 775 | - . ' or disabling the appstore in the config file. See %s', | |
| 776 | -						[$urlGenerator->linkToDocs('admin-dir_permissions')]) | |
| 777 | - ); | |
| 778 | - } | |
| 779 | - } | |
| 780 | - // Create root dir. | |
| 781 | -		if ($config->getValue('installed', false)) { | |
| 782 | -			if (!is_dir($CONFIG_DATADIRECTORY)) { | |
| 783 | - $success = @mkdir($CONFIG_DATADIRECTORY); | |
| 784 | -				if ($success) { | |
| 785 | - $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); | |
| 786 | -				} else { | |
| 787 | - $errors[] = [ | |
| 788 | -						'error' => $l->t('Cannot create "data" directory'), | |
| 789 | -						'hint' => $l->t('This can usually be fixed by giving the webserver write access to the root directory. See %s', | |
| 790 | -							[$urlGenerator->linkToDocs('admin-dir_permissions')]) | |
| 791 | - ]; | |
| 792 | - } | |
| 793 | -			} else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) { | |
| 794 | - // is_writable doesn't work for NFS mounts, so try to write a file and check if it exists. | |
| 795 | -				$testFile = sprintf('%s/%s.tmp', $CONFIG_DATADIRECTORY, uniqid('data_dir_writability_test_')); | |
| 796 | - $handle = fopen($testFile, 'w'); | |
| 797 | -				if (!$handle || fwrite($handle, 'Test write operation') === FALSE) { | |
| 798 | -					$permissionsHint = $l->t('Permissions can usually be fixed by giving the webserver write access to the root directory. See %s.', | |
| 799 | -						[$urlGenerator->linkToDocs('admin-dir_permissions')]); | |
| 800 | - $errors[] = [ | |
| 801 | - 'error' => 'Your data directory is not writable', | |
| 802 | - 'hint' => $permissionsHint | |
| 803 | - ]; | |
| 804 | -				} else { | |
| 805 | - fclose($handle); | |
| 806 | - unlink($testFile); | |
| 807 | - } | |
| 808 | -			} else { | |
| 809 | - $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); | |
| 810 | - } | |
| 811 | - } | |
| 812 | - | |
| 813 | -		if (!OC_Util::isSetLocaleWorking()) { | |
| 814 | - $errors[] = array( | |
| 815 | -				'error' => $l->t('Setting locale to %s failed', | |
| 816 | -					array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/' | |
| 817 | - . 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')), | |
| 818 | -				'hint' => $l->t('Please install one of these locales on your system and restart your webserver.') | |
| 819 | - ); | |
| 820 | - } | |
| 821 | - | |
| 822 | - // Contains the dependencies that should be checked against | |
| 823 | - // classes = class_exists | |
| 824 | - // functions = function_exists | |
| 825 | - // defined = defined | |
| 826 | - // ini = ini_get | |
| 827 | - // If the dependency is not found the missing module name is shown to the EndUser | |
| 828 | - // When adding new checks always verify that they pass on Travis as well | |
| 829 | - // for ini settings, see https://github.com/owncloud/administration/blob/master/travis-ci/custom.ini | |
| 830 | - $dependencies = array( | |
| 831 | - 'classes' => array( | |
| 832 | - 'ZipArchive' => 'zip', | |
| 833 | - 'DOMDocument' => 'dom', | |
| 834 | - 'XMLWriter' => 'XMLWriter', | |
| 835 | - 'XMLReader' => 'XMLReader', | |
| 836 | - ), | |
| 837 | - 'functions' => [ | |
| 838 | - 'xml_parser_create' => 'libxml', | |
| 839 | - 'mb_strcut' => 'mbstring', | |
| 840 | - 'ctype_digit' => 'ctype', | |
| 841 | - 'json_encode' => 'JSON', | |
| 842 | - 'gd_info' => 'GD', | |
| 843 | - 'gzencode' => 'zlib', | |
| 844 | - 'iconv' => 'iconv', | |
| 845 | - 'simplexml_load_string' => 'SimpleXML', | |
| 846 | - 'hash' => 'HASH Message Digest Framework', | |
| 847 | - 'curl_init' => 'cURL', | |
| 848 | - 'openssl_verify' => 'OpenSSL', | |
| 849 | - ], | |
| 850 | - 'defined' => array( | |
| 851 | - 'PDO::ATTR_DRIVER_NAME' => 'PDO' | |
| 852 | - ), | |
| 853 | - 'ini' => [ | |
| 854 | - 'default_charset' => 'UTF-8', | |
| 855 | - ], | |
| 856 | - ); | |
| 857 | - $missingDependencies = array(); | |
| 858 | - $invalidIniSettings = []; | |
| 859 | -		$moduleHint = $l->t('Please ask your server administrator to install the module.'); | |
| 860 | - | |
| 861 | - $iniWrapper = \OC::$server->getIniWrapper(); | |
| 862 | -		foreach ($dependencies['classes'] as $class => $module) { | |
| 863 | -			if (!class_exists($class)) { | |
| 864 | - $missingDependencies[] = $module; | |
| 865 | - } | |
| 866 | - } | |
| 867 | -		foreach ($dependencies['functions'] as $function => $module) { | |
| 868 | -			if (!function_exists($function)) { | |
| 869 | - $missingDependencies[] = $module; | |
| 870 | - } | |
| 871 | - } | |
| 872 | -		foreach ($dependencies['defined'] as $defined => $module) { | |
| 873 | -			if (!defined($defined)) { | |
| 874 | - $missingDependencies[] = $module; | |
| 875 | - } | |
| 876 | - } | |
| 877 | -		foreach ($dependencies['ini'] as $setting => $expected) { | |
| 878 | -			if (is_bool($expected)) { | |
| 879 | -				if ($iniWrapper->getBool($setting) !== $expected) { | |
| 880 | - $invalidIniSettings[] = [$setting, $expected]; | |
| 881 | - } | |
| 882 | - } | |
| 883 | -			if (is_int($expected)) { | |
| 884 | -				if ($iniWrapper->getNumeric($setting) !== $expected) { | |
| 885 | - $invalidIniSettings[] = [$setting, $expected]; | |
| 886 | - } | |
| 887 | - } | |
| 888 | -			if (is_string($expected)) { | |
| 889 | -				if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) { | |
| 890 | - $invalidIniSettings[] = [$setting, $expected]; | |
| 891 | - } | |
| 892 | - } | |
| 893 | - } | |
| 894 | - | |
| 895 | -		foreach($missingDependencies as $missingDependency) { | |
| 896 | - $errors[] = array( | |
| 897 | -				'error' => $l->t('PHP module %s not installed.', array($missingDependency)), | |
| 898 | - 'hint' => $moduleHint | |
| 899 | - ); | |
| 900 | - $webServerRestart = true; | |
| 901 | - } | |
| 902 | -		foreach($invalidIniSettings as $setting) { | |
| 903 | -			if(is_bool($setting[1])) { | |
| 904 | - $setting[1] = $setting[1] ? 'on' : 'off'; | |
| 905 | - } | |
| 906 | - $errors[] = [ | |
| 907 | -				'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]), | |
| 908 | -				'hint' =>  $l->t('Adjusting this setting in php.ini will make Nextcloud run again') | |
| 909 | - ]; | |
| 910 | - $webServerRestart = true; | |
| 911 | - } | |
| 912 | - | |
| 913 | - /** | |
| 914 | - * The mbstring.func_overload check can only be performed if the mbstring | |
| 915 | - * module is installed as it will return null if the checking setting is | |
| 916 | - * not available and thus a check on the boolean value fails. | |
| 917 | - * | |
| 918 | - * TODO: Should probably be implemented in the above generic dependency | |
| 919 | - * check somehow in the long-term. | |
| 920 | - */ | |
| 921 | -		if($iniWrapper->getBool('mbstring.func_overload') !== null && | |
| 922 | -			$iniWrapper->getBool('mbstring.func_overload') === true) { | |
| 923 | - $errors[] = array( | |
| 924 | -				'error' => $l->t('mbstring.func_overload is set to "%s" instead of the expected value "0"', [$iniWrapper->getString('mbstring.func_overload')]), | |
| 925 | -				'hint' => $l->t('To fix this issue set <code>mbstring.func_overload</code> to <code>0</code> in your php.ini') | |
| 926 | - ); | |
| 927 | - } | |
| 928 | - | |
| 929 | -		if(function_exists('xml_parser_create') && | |
| 930 | -			LIBXML_LOADED_VERSION < 20700 ) { | |
| 931 | - $version = LIBXML_LOADED_VERSION; | |
| 932 | - $major = floor($version/10000); | |
| 933 | - $version -= ($major * 10000); | |
| 934 | - $minor = floor($version/100); | |
| 935 | - $version -= ($minor * 100); | |
| 936 | - $patch = $version; | |
| 937 | - $errors[] = array( | |
| 938 | -				'error' => $l->t('libxml2 2.7.0 is at least required. Currently %s is installed.', [$major . '.' . $minor . '.' . $patch]), | |
| 939 | -				'hint' => $l->t('To fix this issue update your libxml2 version and restart your web server.') | |
| 940 | - ); | |
| 941 | - } | |
| 942 | - | |
| 943 | -		if (!self::isAnnotationsWorking()) { | |
| 944 | - $errors[] = array( | |
| 945 | -				'error' => $l->t('PHP is apparently set up to strip inline doc blocks. This will make several core apps inaccessible.'), | |
| 946 | -				'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.') | |
| 947 | - ); | |
| 948 | - } | |
| 949 | - | |
| 950 | -		if (!\OC::$CLI && $webServerRestart) { | |
| 951 | - $errors[] = array( | |
| 952 | -				'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'), | |
| 953 | -				'hint' => $l->t('Please ask your server administrator to restart the web server.') | |
| 954 | - ); | |
| 955 | - } | |
| 956 | - | |
| 957 | - $errors = array_merge($errors, self::checkDatabaseVersion()); | |
| 958 | - | |
| 959 | - // Cache the result of this function | |
| 960 | -		\OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0); | |
| 961 | - | |
| 962 | - return $errors; | |
| 963 | - } | |
| 964 | - | |
| 965 | - /** | |
| 966 | - * Check the database version | |
| 967 | - * | |
| 968 | - * @return array errors array | |
| 969 | - */ | |
| 970 | -	public static function checkDatabaseVersion() { | |
| 971 | -		$l = \OC::$server->getL10N('lib'); | |
| 972 | - $errors = array(); | |
| 973 | -		$dbType = \OC::$server->getSystemConfig()->getValue('dbtype', 'sqlite'); | |
| 974 | -		if ($dbType === 'pgsql') { | |
| 975 | - // check PostgreSQL version | |
| 976 | -			try { | |
| 977 | -				$result = \OC_DB::executeAudited('SHOW SERVER_VERSION'); | |
| 978 | - $data = $result->fetchRow(); | |
| 979 | -				if (isset($data['server_version'])) { | |
| 980 | - $version = $data['server_version']; | |
| 981 | -					if (version_compare($version, '9.5.0', '<')) { | |
| 982 | - $errors[] = array( | |
| 983 | -							'error' => $l->t('PostgreSQL >= 9.5 required'), | |
| 984 | -							'hint' => $l->t('Please upgrade your database version') | |
| 985 | - ); | |
| 986 | - } | |
| 987 | - } | |
| 988 | -			} catch (\Doctrine\DBAL\DBALException $e) { | |
| 989 | - $logger = \OC::$server->getLogger(); | |
| 990 | -				$logger->warning('Error occurred while checking PostgreSQL version, assuming >= 9'); | |
| 991 | - $logger->logException($e); | |
| 992 | - } | |
| 993 | - } | |
| 994 | - return $errors; | |
| 995 | - } | |
| 996 | - | |
| 997 | - /** | |
| 998 | - * Check for correct file permissions of data directory | |
| 999 | - * | |
| 1000 | - * @param string $dataDirectory | |
| 1001 | - * @return array arrays with error messages and hints | |
| 1002 | - */ | |
| 1003 | -	public static function checkDataDirectoryPermissions($dataDirectory) { | |
| 1004 | -		if(\OC::$server->getConfig()->getSystemValue('check_data_directory_permissions', true) === false) { | |
| 1005 | - return []; | |
| 1006 | - } | |
| 1007 | -		$l = \OC::$server->getL10N('lib'); | |
| 1008 | - $errors = []; | |
| 1009 | -		$permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory' | |
| 1010 | - . ' cannot be listed by other users.'); | |
| 1011 | - $perms = substr(decoct(@fileperms($dataDirectory)), -3); | |
| 1012 | -		if (substr($perms, -1) !== '0') { | |
| 1013 | - chmod($dataDirectory, 0770); | |
| 1014 | - clearstatcache(); | |
| 1015 | - $perms = substr(decoct(@fileperms($dataDirectory)), -3); | |
| 1016 | -			if ($perms[2] !== '0') { | |
| 1017 | - $errors[] = [ | |
| 1018 | -					'error' => $l->t('Your data directory is readable by other users'), | |
| 1019 | - 'hint' => $permissionsModHint | |
| 1020 | - ]; | |
| 1021 | - } | |
| 1022 | - } | |
| 1023 | - return $errors; | |
| 1024 | - } | |
| 1025 | - | |
| 1026 | - /** | |
| 1027 | - * Check that the data directory exists and is valid by | |
| 1028 | - * checking the existence of the ".ocdata" file. | |
| 1029 | - * | |
| 1030 | - * @param string $dataDirectory data directory path | |
| 1031 | - * @return array errors found | |
| 1032 | - */ | |
| 1033 | -	public static function checkDataDirectoryValidity($dataDirectory) { | |
| 1034 | -		$l = \OC::$server->getL10N('lib'); | |
| 1035 | - $errors = []; | |
| 1036 | -		if ($dataDirectory[0] !== '/') { | |
| 1037 | - $errors[] = [ | |
| 1038 | -				'error' => $l->t('Your data directory must be an absolute path'), | |
| 1039 | -				'hint' => $l->t('Check the value of "datadirectory" in your configuration') | |
| 1040 | - ]; | |
| 1041 | - } | |
| 1042 | -		if (!file_exists($dataDirectory . '/.ocdata')) { | |
| 1043 | - $errors[] = [ | |
| 1044 | -				'error' => $l->t('Your data directory is invalid'), | |
| 1045 | -				'hint' => $l->t('Ensure there is a file called ".ocdata"' . | |
| 1046 | - ' in the root of the data directory.') | |
| 1047 | - ]; | |
| 1048 | - } | |
| 1049 | - return $errors; | |
| 1050 | - } | |
| 1051 | - | |
| 1052 | - /** | |
| 1053 | - * Check if the user is logged in, redirects to home if not. With | |
| 1054 | - * redirect URL parameter to the request URI. | |
| 1055 | - * | |
| 1056 | - * @return void | |
| 1057 | - */ | |
| 1058 | -	public static function checkLoggedIn() { | |
| 1059 | - // Check if we are a user | |
| 1060 | -		if (!\OC::$server->getUserSession()->isLoggedIn()) { | |
| 1061 | -			header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute( | |
| 1062 | - 'core.login.showLoginForm', | |
| 1063 | - [ | |
| 1064 | - 'redirect_url' => \OC::$server->getRequest()->getRequestUri(), | |
| 1065 | - ] | |
| 1066 | - ) | |
| 1067 | - ); | |
| 1068 | - exit(); | |
| 1069 | - } | |
| 1070 | - // Redirect to 2FA challenge selection if 2FA challenge was not solved yet | |
| 1071 | -		if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { | |
| 1072 | -			header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.selectChallenge')); | |
| 1073 | - exit(); | |
| 1074 | - } | |
| 1075 | - } | |
| 1076 | - | |
| 1077 | - /** | |
| 1078 | - * Check if the user is a admin, redirects to home if not | |
| 1079 | - * | |
| 1080 | - * @return void | |
| 1081 | - */ | |
| 1082 | -	public static function checkAdminUser() { | |
| 1083 | - OC_Util::checkLoggedIn(); | |
| 1084 | -		if (!OC_User::isAdminUser(OC_User::getUser())) { | |
| 1085 | -			header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php')); | |
| 1086 | - exit(); | |
| 1087 | - } | |
| 1088 | - } | |
| 1089 | - | |
| 1090 | - /** | |
| 1091 | - * Returns the URL of the default page | |
| 1092 | - * based on the system configuration and | |
| 1093 | - * the apps visible for the current user | |
| 1094 | - * | |
| 1095 | - * @return string URL | |
| 1096 | - * @suppress PhanDeprecatedFunction | |
| 1097 | - */ | |
| 1098 | -	public static function getDefaultPageUrl() { | |
| 1099 | - $urlGenerator = \OC::$server->getURLGenerator(); | |
| 1100 | - // Deny the redirect if the URL contains a @ | |
| 1101 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] | |
| 1102 | -		if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) { | |
| 1103 | - $location = $urlGenerator->getAbsoluteURL(urldecode($_REQUEST['redirect_url'])); | |
| 1104 | -		} else { | |
| 1105 | -			$defaultPage = \OC::$server->getConfig()->getAppValue('core', 'defaultpage'); | |
| 1106 | -			if ($defaultPage) { | |
| 1107 | - $location = $urlGenerator->getAbsoluteURL($defaultPage); | |
| 1108 | -			} else { | |
| 1109 | - $appId = 'files'; | |
| 1110 | - $config = \OC::$server->getConfig(); | |
| 1111 | -				$defaultApps = explode(',', $config->getSystemValue('defaultapp', 'files')); | |
| 1112 | - // find the first app that is enabled for the current user | |
| 1113 | -				foreach ($defaultApps as $defaultApp) { | |
| 1114 | - $defaultApp = OC_App::cleanAppId(strip_tags($defaultApp)); | |
| 1115 | -					if (static::getAppManager()->isEnabledForUser($defaultApp)) { | |
| 1116 | - $appId = $defaultApp; | |
| 1117 | - break; | |
| 1118 | - } | |
| 1119 | - } | |
| 1120 | - | |
| 1121 | -				if($config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true') { | |
| 1122 | -					$location = $urlGenerator->getAbsoluteURL('/apps/' . $appId . '/'); | |
| 1123 | -				} else { | |
| 1124 | -					$location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/'); | |
| 1125 | - } | |
| 1126 | - } | |
| 1127 | - } | |
| 1128 | - return $location; | |
| 1129 | - } | |
| 1130 | - | |
| 1131 | - /** | |
| 1132 | - * Redirect to the user default page | |
| 1133 | - * | |
| 1134 | - * @return void | |
| 1135 | - */ | |
| 1136 | -	public static function redirectToDefaultPage() { | |
| 1137 | - $location = self::getDefaultPageUrl(); | |
| 1138 | -		header('Location: ' . $location); | |
| 1139 | - exit(); | |
| 1140 | - } | |
| 1141 | - | |
| 1142 | - /** | |
| 1143 | - * get an id unique for this instance | |
| 1144 | - * | |
| 1145 | - * @return string | |
| 1146 | - */ | |
| 1147 | -	public static function getInstanceId() { | |
| 1148 | -		$id = \OC::$server->getSystemConfig()->getValue('instanceid', null); | |
| 1149 | -		if (is_null($id)) { | |
| 1150 | - // We need to guarantee at least one letter in instanceid so it can be used as the session_name | |
| 1151 | - $id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); | |
| 1152 | -			\OC::$server->getSystemConfig()->setValue('instanceid', $id); | |
| 1153 | - } | |
| 1154 | - return $id; | |
| 1155 | - } | |
| 1156 | - | |
| 1157 | - /** | |
| 1158 | - * Public function to sanitize HTML | |
| 1159 | - * | |
| 1160 | - * This function is used to sanitize HTML and should be applied on any | |
| 1161 | - * string or array of strings before displaying it on a web page. | |
| 1162 | - * | |
| 1163 | - * @param string|array $value | |
| 1164 | - * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. | |
| 1165 | - */ | |
| 1166 | -	public static function sanitizeHTML($value) { | |
| 1167 | -		if (is_array($value)) { | |
| 1168 | -			$value = array_map(function($value) { | |
| 1169 | - return self::sanitizeHTML($value); | |
| 1170 | - }, $value); | |
| 1171 | -		} else { | |
| 1172 | - // Specify encoding for PHP<5.4 | |
| 1173 | - $value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); | |
| 1174 | - } | |
| 1175 | - return $value; | |
| 1176 | - } | |
| 1177 | - | |
| 1178 | - /** | |
| 1179 | - * Public function to encode url parameters | |
| 1180 | - * | |
| 1181 | - * This function is used to encode path to file before output. | |
| 1182 | - * Encoding is done according to RFC 3986 with one exception: | |
| 1183 | - * Character '/' is preserved as is. | |
| 1184 | - * | |
| 1185 | - * @param string $component part of URI to encode | |
| 1186 | - * @return string | |
| 1187 | - */ | |
| 1188 | -	public static function encodePath($component) { | |
| 1189 | - $encoded = rawurlencode($component); | |
| 1190 | -		$encoded = str_replace('%2F', '/', $encoded); | |
| 1191 | - return $encoded; | |
| 1192 | - } | |
| 1193 | - | |
| 1194 | - | |
| 1195 | -	public function createHtaccessTestFile(\OCP\IConfig $config) { | |
| 1196 | - // php dev server does not support htaccess | |
| 1197 | -		if (php_sapi_name() === 'cli-server') { | |
| 1198 | - return false; | |
| 1199 | - } | |
| 1200 | - | |
| 1201 | - // testdata | |
| 1202 | - $fileName = '/htaccesstest.txt'; | |
| 1203 | - $testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.'; | |
| 1204 | - | |
| 1205 | - // creating a test file | |
| 1206 | -		$testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; | |
| 1207 | - | |
| 1208 | -		if (file_exists($testFile)) {// already running this test, possible recursive call | |
| 1209 | - return false; | |
| 1210 | - } | |
| 1211 | - | |
| 1212 | - $fp = @fopen($testFile, 'w'); | |
| 1213 | -		if (!$fp) { | |
| 1214 | -			throw new OC\HintException('Can\'t create test file to check for working .htaccess file.', | |
| 1215 | - 'Make sure it is possible for the webserver to write to ' . $testFile); | |
| 1216 | - } | |
| 1217 | - fwrite($fp, $testContent); | |
| 1218 | - fclose($fp); | |
| 1219 | - | |
| 1220 | - return $testContent; | |
| 1221 | - } | |
| 1222 | - | |
| 1223 | - /** | |
| 1224 | - * Check if the .htaccess file is working | |
| 1225 | - * @param \OCP\IConfig $config | |
| 1226 | - * @return bool | |
| 1227 | - * @throws Exception | |
| 1228 | - * @throws \OC\HintException If the test file can't get written. | |
| 1229 | - */ | |
| 1230 | -	public function isHtaccessWorking(\OCP\IConfig $config) { | |
| 1231 | - | |
| 1232 | -		if (\OC::$CLI || !$config->getSystemValue('check_for_working_htaccess', true)) { | |
| 1233 | - return true; | |
| 1234 | - } | |
| 1235 | - | |
| 1236 | - $testContent = $this->createHtaccessTestFile($config); | |
| 1237 | -		if ($testContent === false) { | |
| 1238 | - return false; | |
| 1239 | - } | |
| 1240 | - | |
| 1241 | - $fileName = '/htaccesstest.txt'; | |
| 1242 | -		$testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; | |
| 1243 | - | |
| 1244 | - // accessing the file via http | |
| 1245 | - $url = \OC::$server->getURLGenerator()->getAbsoluteURL(OC::$WEBROOT . '/data' . $fileName); | |
| 1246 | -		try { | |
| 1247 | - $content = \OC::$server->getHTTPClientService()->newClient()->get($url)->getBody(); | |
| 1248 | -		} catch (\Exception $e) { | |
| 1249 | - $content = false; | |
| 1250 | - } | |
| 1251 | - | |
| 1252 | - // cleanup | |
| 1253 | - @unlink($testFile); | |
| 1254 | - | |
| 1255 | - /* | |
| 268 | +            if ($mount->getOption('readonly', false)) { | |
| 269 | + return new \OC\Files\Storage\Wrapper\PermissionsMask([ | |
| 270 | + 'storage' => $storage, | |
| 271 | + 'mask' => \OCP\Constants::PERMISSION_ALL & ~( | |
| 272 | + \OCP\Constants::PERMISSION_UPDATE | | |
| 273 | + \OCP\Constants::PERMISSION_CREATE | | |
| 274 | + \OCP\Constants::PERMISSION_DELETE | |
| 275 | + ), | |
| 276 | + ]); | |
| 277 | + } | |
| 278 | + return $storage; | |
| 279 | + }); | |
| 280 | + | |
| 281 | +        OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user)); | |
| 282 | + | |
| 283 | + \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($prevLogging); | |
| 284 | + | |
| 285 | + //check if we are using an object storage | |
| 286 | +        $objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null); | |
| 287 | +        $objectStoreMultibucket = \OC::$server->getSystemConfig()->getValue('objectstore_multibucket', null); | |
| 288 | + | |
| 289 | + // use the same order as in ObjectHomeMountProvider | |
| 290 | +        if (isset($objectStoreMultibucket)) { | |
| 291 | + self::initObjectStoreMultibucketRootFS($objectStoreMultibucket); | |
| 292 | +        } elseif (isset($objectStore)) { | |
| 293 | + self::initObjectStoreRootFS($objectStore); | |
| 294 | +        } else { | |
| 295 | + self::initLocalStorageRootFS(); | |
| 296 | + } | |
| 297 | + | |
| 298 | +        if ($user != '' && !\OC::$server->getUserManager()->userExists($user)) { | |
| 299 | +            \OC::$server->getEventLogger()->end('setup_fs'); | |
| 300 | + return false; | |
| 301 | + } | |
| 302 | + | |
| 303 | + //if we aren't logged in, there is no use to set up the filesystem | |
| 304 | +        if ($user != "") { | |
| 305 | + | |
| 306 | + $userDir = '/' . $user . '/files'; | |
| 307 | + | |
| 308 | + //jail the user into his "home" directory | |
| 309 | + \OC\Files\Filesystem::init($user, $userDir); | |
| 310 | + | |
| 311 | +            OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir)); | |
| 312 | + } | |
| 313 | +        \OC::$server->getEventLogger()->end('setup_fs'); | |
| 314 | + return true; | |
| 315 | + } | |
| 316 | + | |
| 317 | + /** | |
| 318 | + * check if a password is required for each public link | |
| 319 | + * | |
| 320 | + * @return boolean | |
| 321 | + * @suppress PhanDeprecatedFunction | |
| 322 | + */ | |
| 323 | +    public static function isPublicLinkPasswordRequired() { | |
| 324 | +        $enforcePassword = \OC::$server->getConfig()->getAppValue('core', 'shareapi_enforce_links_password', 'no'); | |
| 325 | + return $enforcePassword === 'yes'; | |
| 326 | + } | |
| 327 | + | |
| 328 | + /** | |
| 329 | + * check if sharing is disabled for the current user | |
| 330 | + * @param IConfig $config | |
| 331 | + * @param IGroupManager $groupManager | |
| 332 | + * @param IUser|null $user | |
| 333 | + * @return bool | |
| 334 | + */ | |
| 335 | +    public static function isSharingDisabledForUser(IConfig $config, IGroupManager $groupManager, $user) { | |
| 336 | +        if ($config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { | |
| 337 | +            $groupsList = $config->getAppValue('core', 'shareapi_exclude_groups_list', ''); | |
| 338 | + $excludedGroups = json_decode($groupsList); | |
| 339 | +            if (is_null($excludedGroups)) { | |
| 340 | +                $excludedGroups = explode(',', $groupsList); | |
| 341 | + $newValue = json_encode($excludedGroups); | |
| 342 | +                $config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); | |
| 343 | + } | |
| 344 | + $usersGroups = $groupManager->getUserGroupIds($user); | |
| 345 | +            if (!empty($usersGroups)) { | |
| 346 | + $remainingGroups = array_diff($usersGroups, $excludedGroups); | |
| 347 | + // if the user is only in groups which are disabled for sharing then | |
| 348 | + // sharing is also disabled for the user | |
| 349 | +                if (empty($remainingGroups)) { | |
| 350 | + return true; | |
| 351 | + } | |
| 352 | + } | |
| 353 | + } | |
| 354 | + return false; | |
| 355 | + } | |
| 356 | + | |
| 357 | + /** | |
| 358 | + * check if share API enforces a default expire date | |
| 359 | + * | |
| 360 | + * @return boolean | |
| 361 | + * @suppress PhanDeprecatedFunction | |
| 362 | + */ | |
| 363 | +    public static function isDefaultExpireDateEnforced() { | |
| 364 | +        $isDefaultExpireDateEnabled = \OC::$server->getConfig()->getAppValue('core', 'shareapi_default_expire_date', 'no'); | |
| 365 | + $enforceDefaultExpireDate = false; | |
| 366 | +        if ($isDefaultExpireDateEnabled === 'yes') { | |
| 367 | +            $value = \OC::$server->getConfig()->getAppValue('core', 'shareapi_enforce_expire_date', 'no'); | |
| 368 | + $enforceDefaultExpireDate = $value === 'yes'; | |
| 369 | + } | |
| 370 | + | |
| 371 | + return $enforceDefaultExpireDate; | |
| 372 | + } | |
| 373 | + | |
| 374 | + /** | |
| 375 | + * Get the quota of a user | |
| 376 | + * | |
| 377 | + * @param string $userId | |
| 378 | + * @return float Quota bytes | |
| 379 | + */ | |
| 380 | +    public static function getUserQuota($userId) { | |
| 381 | + $user = \OC::$server->getUserManager()->get($userId); | |
| 382 | +        if (is_null($user)) { | |
| 383 | + return \OCP\Files\FileInfo::SPACE_UNLIMITED; | |
| 384 | + } | |
| 385 | + $userQuota = $user->getQuota(); | |
| 386 | +        if($userQuota === 'none') { | |
| 387 | + return \OCP\Files\FileInfo::SPACE_UNLIMITED; | |
| 388 | + } | |
| 389 | + return OC_Helper::computerFileSize($userQuota); | |
| 390 | + } | |
| 391 | + | |
| 392 | + /** | |
| 393 | + * copies the skeleton to the users /files | |
| 394 | + * | |
| 395 | + * @param string $userId | |
| 396 | + * @param \OCP\Files\Folder $userDirectory | |
| 397 | + * @throws \OCP\Files\NotFoundException | |
| 398 | + * @throws \OCP\Files\NotPermittedException | |
| 399 | + * @suppress PhanDeprecatedFunction | |
| 400 | + */ | |
| 401 | +    public static function copySkeleton($userId, \OCP\Files\Folder $userDirectory) { | |
| 402 | + | |
| 403 | +        $plainSkeletonDirectory = \OC::$server->getConfig()->getSystemValue('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton'); | |
| 404 | + $userLang = \OC::$server->getL10NFactory()->findLanguage(); | |
| 405 | +        $skeletonDirectory = str_replace('{lang}', $userLang, $plainSkeletonDirectory); | |
| 406 | + | |
| 407 | +        if (!file_exists($skeletonDirectory)) { | |
| 408 | + $dialectStart = strpos($userLang, '_'); | |
| 409 | +            if ($dialectStart !== false) { | |
| 410 | +                $skeletonDirectory = str_replace('{lang}', substr($userLang, 0, $dialectStart), $plainSkeletonDirectory); | |
| 411 | + } | |
| 412 | +            if ($dialectStart === false || !file_exists($skeletonDirectory)) { | |
| 413 | +                $skeletonDirectory = str_replace('{lang}', 'default', $plainSkeletonDirectory); | |
| 414 | + } | |
| 415 | +            if (!file_exists($skeletonDirectory)) { | |
| 416 | + $skeletonDirectory = ''; | |
| 417 | + } | |
| 418 | + } | |
| 419 | + | |
| 420 | +        $instanceId = \OC::$server->getConfig()->getSystemValue('instanceid', ''); | |
| 421 | + | |
| 422 | +        if ($instanceId === null) { | |
| 423 | +            throw new \RuntimeException('no instance id!'); | |
| 424 | + } | |
| 425 | + $appdata = 'appdata_' . $instanceId; | |
| 426 | +        if ($userId === $appdata) { | |
| 427 | +            throw new \RuntimeException('username is reserved name: ' . $appdata); | |
| 428 | + } | |
| 429 | + | |
| 430 | +        if (!empty($skeletonDirectory)) { | |
| 431 | + \OCP\Util::writeLog( | |
| 432 | + 'files_skeleton', | |
| 433 | +                'copying skeleton for '.$userId.' from '.$skeletonDirectory.' to '.$userDirectory->getFullPath('/'), | |
| 434 | + ILogger::DEBUG | |
| 435 | + ); | |
| 436 | + self::copyr($skeletonDirectory, $userDirectory); | |
| 437 | + // update the file cache | |
| 438 | +            $userDirectory->getStorage()->getScanner()->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE); | |
| 439 | + } | |
| 440 | + } | |
| 441 | + | |
| 442 | + /** | |
| 443 | + * copies a directory recursively by using streams | |
| 444 | + * | |
| 445 | + * @param string $source | |
| 446 | + * @param \OCP\Files\Folder $target | |
| 447 | + * @return void | |
| 448 | + */ | |
| 449 | +    public static function copyr($source, \OCP\Files\Folder $target) { | |
| 450 | + $logger = \OC::$server->getLogger(); | |
| 451 | + | |
| 452 | + // Verify if folder exists | |
| 453 | + $dir = opendir($source); | |
| 454 | +        if($dir === false) { | |
| 455 | +            $logger->error(sprintf('Could not opendir "%s"', $source), ['app' => 'core']); | |
| 456 | + return; | |
| 457 | + } | |
| 458 | + | |
| 459 | + // Copy the files | |
| 460 | +        while (false !== ($file = readdir($dir))) { | |
| 461 | +            if (!\OC\Files\Filesystem::isIgnoredDir($file)) { | |
| 462 | +                if (is_dir($source . '/' . $file)) { | |
| 463 | + $child = $target->newFolder($file); | |
| 464 | + self::copyr($source . '/' . $file, $child); | |
| 465 | +                } else { | |
| 466 | + $child = $target->newFile($file); | |
| 467 | + $sourceStream = fopen($source . '/' . $file, 'r'); | |
| 468 | +                    if($sourceStream === false) { | |
| 469 | +                        $logger->error(sprintf('Could not fopen "%s"', $source . '/' . $file), ['app' => 'core']); | |
| 470 | + closedir($dir); | |
| 471 | + return; | |
| 472 | + } | |
| 473 | +                    stream_copy_to_stream($sourceStream, $child->fopen('w')); | |
| 474 | + } | |
| 475 | + } | |
| 476 | + } | |
| 477 | + closedir($dir); | |
| 478 | + } | |
| 479 | + | |
| 480 | + /** | |
| 481 | + * @return void | |
| 482 | + * @suppress PhanUndeclaredMethod | |
| 483 | + */ | |
| 484 | +    public static function tearDownFS() { | |
| 485 | + \OC\Files\Filesystem::tearDown(); | |
| 486 | + \OC::$server->getRootFolder()->clearCache(); | |
| 487 | + self::$fsSetup = false; | |
| 488 | + self::$rootMounted = false; | |
| 489 | + } | |
| 490 | + | |
| 491 | + /** | |
| 492 | + * get the current installed version of ownCloud | |
| 493 | + * | |
| 494 | + * @return array | |
| 495 | + */ | |
| 496 | +    public static function getVersion() { | |
| 497 | + OC_Util::loadVersion(); | |
| 498 | + return self::$versionCache['OC_Version']; | |
| 499 | + } | |
| 500 | + | |
| 501 | + /** | |
| 502 | + * get the current installed version string of ownCloud | |
| 503 | + * | |
| 504 | + * @return string | |
| 505 | + */ | |
| 506 | +    public static function getVersionString() { | |
| 507 | + OC_Util::loadVersion(); | |
| 508 | + return self::$versionCache['OC_VersionString']; | |
| 509 | + } | |
| 510 | + | |
| 511 | + /** | |
| 512 | + * @deprecated the value is of no use anymore | |
| 513 | + * @return string | |
| 514 | + */ | |
| 515 | +    public static function getEditionString() { | |
| 516 | + return ''; | |
| 517 | + } | |
| 518 | + | |
| 519 | + /** | |
| 520 | + * @description get the update channel of the current installed of ownCloud. | |
| 521 | + * @return string | |
| 522 | + */ | |
| 523 | +    public static function getChannel() { | |
| 524 | + OC_Util::loadVersion(); | |
| 525 | +        return \OC::$server->getConfig()->getSystemValue('updater.release.channel', self::$versionCache['OC_Channel']); | |
| 526 | + } | |
| 527 | + | |
| 528 | + /** | |
| 529 | + * @description get the build number of the current installed of ownCloud. | |
| 530 | + * @return string | |
| 531 | + */ | |
| 532 | +    public static function getBuild() { | |
| 533 | + OC_Util::loadVersion(); | |
| 534 | + return self::$versionCache['OC_Build']; | |
| 535 | + } | |
| 536 | + | |
| 537 | + /** | |
| 538 | + * @description load the version.php into the session as cache | |
| 539 | + * @suppress PhanUndeclaredVariable | |
| 540 | + */ | |
| 541 | +    private static function loadVersion() { | |
| 542 | +        if (self::$versionCache !== null) { | |
| 543 | + return; | |
| 544 | + } | |
| 545 | + | |
| 546 | + $timestamp = filemtime(OC::$SERVERROOT . '/version.php'); | |
| 547 | + require OC::$SERVERROOT . '/version.php'; | |
| 548 | + /** @var $timestamp int */ | |
| 549 | + self::$versionCache['OC_Version_Timestamp'] = $timestamp; | |
| 550 | + /** @var $OC_Version string */ | |
| 551 | + self::$versionCache['OC_Version'] = $OC_Version; | |
| 552 | + /** @var $OC_VersionString string */ | |
| 553 | + self::$versionCache['OC_VersionString'] = $OC_VersionString; | |
| 554 | + /** @var $OC_Build string */ | |
| 555 | + self::$versionCache['OC_Build'] = $OC_Build; | |
| 556 | + | |
| 557 | + /** @var $OC_Channel string */ | |
| 558 | + self::$versionCache['OC_Channel'] = $OC_Channel; | |
| 559 | + } | |
| 560 | + | |
| 561 | + /** | |
| 562 | + * generates a path for JS/CSS files. If no application is provided it will create the path for core. | |
| 563 | + * | |
| 564 | + * @param string $application application to get the files from | |
| 565 | + * @param string $directory directory within this application (css, js, vendor, etc) | |
| 566 | + * @param string $file the file inside of the above folder | |
| 567 | + * @return string the path | |
| 568 | + */ | |
| 569 | +    private static function generatePath($application, $directory, $file) { | |
| 570 | +        if (is_null($file)) { | |
| 571 | + $file = $application; | |
| 572 | + $application = ""; | |
| 573 | + } | |
| 574 | +        if (!empty($application)) { | |
| 575 | + return "$application/$directory/$file"; | |
| 576 | +        } else { | |
| 577 | + return "$directory/$file"; | |
| 578 | + } | |
| 579 | + } | |
| 580 | + | |
| 581 | + /** | |
| 582 | + * add a javascript file | |
| 583 | + * | |
| 584 | + * @param string $application application id | |
| 585 | + * @param string|null $file filename | |
| 586 | + * @param bool $prepend prepend the Script to the beginning of the list | |
| 587 | + * @return void | |
| 588 | + */ | |
| 589 | +    public static function addScript($application, $file = null, $prepend = false) { | |
| 590 | + $path = OC_Util::generatePath($application, 'js', $file); | |
| 591 | + | |
| 592 | + // core js files need separate handling | |
| 593 | +        if ($application !== 'core' && $file !== null) { | |
| 594 | + self::addTranslations ( $application ); | |
| 595 | + } | |
| 596 | + self::addExternalResource($application, $prepend, $path, "script"); | |
| 597 | + } | |
| 598 | + | |
| 599 | + /** | |
| 600 | + * add a javascript file from the vendor sub folder | |
| 601 | + * | |
| 602 | + * @param string $application application id | |
| 603 | + * @param string|null $file filename | |
| 604 | + * @param bool $prepend prepend the Script to the beginning of the list | |
| 605 | + * @return void | |
| 606 | + */ | |
| 607 | +    public static function addVendorScript($application, $file = null, $prepend = false) { | |
| 608 | + $path = OC_Util::generatePath($application, 'vendor', $file); | |
| 609 | + self::addExternalResource($application, $prepend, $path, "script"); | |
| 610 | + } | |
| 611 | + | |
| 612 | + /** | |
| 613 | + * add a translation JS file | |
| 614 | + * | |
| 615 | + * @param string $application application id | |
| 616 | + * @param string|null $languageCode language code, defaults to the current language | |
| 617 | + * @param bool|null $prepend prepend the Script to the beginning of the list | |
| 618 | + */ | |
| 619 | +    public static function addTranslations($application, $languageCode = null, $prepend = false) { | |
| 620 | +        if (is_null($languageCode)) { | |
| 621 | + $languageCode = \OC::$server->getL10NFactory()->findLanguage($application); | |
| 622 | + } | |
| 623 | +        if (!empty($application)) { | |
| 624 | + $path = "$application/l10n/$languageCode"; | |
| 625 | +        } else { | |
| 626 | + $path = "l10n/$languageCode"; | |
| 627 | + } | |
| 628 | + self::addExternalResource($application, $prepend, $path, "script"); | |
| 629 | + } | |
| 630 | + | |
| 631 | + /** | |
| 632 | + * add a css file | |
| 633 | + * | |
| 634 | + * @param string $application application id | |
| 635 | + * @param string|null $file filename | |
| 636 | + * @param bool $prepend prepend the Style to the beginning of the list | |
| 637 | + * @return void | |
| 638 | + */ | |
| 639 | +    public static function addStyle($application, $file = null, $prepend = false) { | |
| 640 | + $path = OC_Util::generatePath($application, 'css', $file); | |
| 641 | + self::addExternalResource($application, $prepend, $path, "style"); | |
| 642 | + } | |
| 643 | + | |
| 644 | + /** | |
| 645 | + * add a css file from the vendor sub folder | |
| 646 | + * | |
| 647 | + * @param string $application application id | |
| 648 | + * @param string|null $file filename | |
| 649 | + * @param bool $prepend prepend the Style to the beginning of the list | |
| 650 | + * @return void | |
| 651 | + */ | |
| 652 | +    public static function addVendorStyle($application, $file = null, $prepend = false) { | |
| 653 | + $path = OC_Util::generatePath($application, 'vendor', $file); | |
| 654 | + self::addExternalResource($application, $prepend, $path, "style"); | |
| 655 | + } | |
| 656 | + | |
| 657 | + /** | |
| 658 | + * add an external resource css/js file | |
| 659 | + * | |
| 660 | + * @param string $application application id | |
| 661 | + * @param bool $prepend prepend the file to the beginning of the list | |
| 662 | + * @param string $path | |
| 663 | + * @param string $type (script or style) | |
| 664 | + * @return void | |
| 665 | + */ | |
| 666 | +    private static function addExternalResource($application, $prepend, $path, $type = "script") { | |
| 667 | + | |
| 668 | +        if ($type === "style") { | |
| 669 | +            if (!in_array($path, self::$styles)) { | |
| 670 | +                if ($prepend === true) { | |
| 671 | + array_unshift ( self::$styles, $path ); | |
| 672 | +                } else { | |
| 673 | + self::$styles[] = $path; | |
| 674 | + } | |
| 675 | + } | |
| 676 | +        } elseif ($type === "script") { | |
| 677 | +            if (!in_array($path, self::$scripts)) { | |
| 678 | +                if ($prepend === true) { | |
| 679 | + array_unshift ( self::$scripts, $path ); | |
| 680 | +                } else { | |
| 681 | + self::$scripts [] = $path; | |
| 682 | + } | |
| 683 | + } | |
| 684 | + } | |
| 685 | + } | |
| 686 | + | |
| 687 | + /** | |
| 688 | + * Add a custom element to the header | |
| 689 | + * If $text is null then the element will be written as empty element. | |
| 690 | + * So use "" to get a closing tag. | |
| 691 | + * @param string $tag tag name of the element | |
| 692 | + * @param array $attributes array of attributes for the element | |
| 693 | + * @param string $text the text content for the element | |
| 694 | + * @param bool $prepend prepend the header to the beginning of the list | |
| 695 | + */ | |
| 696 | +    public static function addHeader($tag, $attributes, $text = null, $prepend = false) { | |
| 697 | + $header = array( | |
| 698 | + 'tag' => $tag, | |
| 699 | + 'attributes' => $attributes, | |
| 700 | + 'text' => $text | |
| 701 | + ); | |
| 702 | +        if ($prepend === true) { | |
| 703 | + array_unshift (self::$headers, $header); | |
| 704 | + | |
| 705 | +        } else { | |
| 706 | + self::$headers[] = $header; | |
| 707 | + } | |
| 708 | + } | |
| 709 | + | |
| 710 | + /** | |
| 711 | + * check if the current server configuration is suitable for ownCloud | |
| 712 | + * | |
| 713 | + * @param \OC\SystemConfig $config | |
| 714 | + * @return array arrays with error messages and hints | |
| 715 | + */ | |
| 716 | +    public static function checkServer(\OC\SystemConfig $config) { | |
| 717 | +        $l = \OC::$server->getL10N('lib'); | |
| 718 | + $errors = array(); | |
| 719 | +        $CONFIG_DATADIRECTORY = $config->getValue('datadirectory', OC::$SERVERROOT . '/data'); | |
| 720 | + | |
| 721 | +        if (!self::needUpgrade($config) && $config->getValue('installed', false)) { | |
| 722 | + // this check needs to be done every time | |
| 723 | + $errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY); | |
| 724 | + } | |
| 725 | + | |
| 726 | + // Assume that if checkServer() succeeded before in this session, then all is fine. | |
| 727 | +        if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) { | |
| 728 | + return $errors; | |
| 729 | + } | |
| 730 | + | |
| 731 | + $webServerRestart = false; | |
| 732 | + $setup = new \OC\Setup( | |
| 733 | + $config, | |
| 734 | + \OC::$server->getIniWrapper(), | |
| 735 | +            \OC::$server->getL10N('lib'), | |
| 736 | + \OC::$server->query(\OCP\Defaults::class), | |
| 737 | + \OC::$server->getLogger(), | |
| 738 | + \OC::$server->getSecureRandom(), | |
| 739 | + \OC::$server->query(\OC\Installer::class) | |
| 740 | + ); | |
| 741 | + | |
| 742 | + $urlGenerator = \OC::$server->getURLGenerator(); | |
| 743 | + | |
| 744 | + $availableDatabases = $setup->getSupportedDatabases(); | |
| 745 | +        if (empty($availableDatabases)) { | |
| 746 | + $errors[] = array( | |
| 747 | +                'error' => $l->t('No database drivers (sqlite, mysql, or postgresql) installed.'), | |
| 748 | + 'hint' => '' //TODO: sane hint | |
| 749 | + ); | |
| 750 | + $webServerRestart = true; | |
| 751 | + } | |
| 752 | + | |
| 753 | + // Check if config folder is writable. | |
| 754 | +        if(!OC_Helper::isReadOnlyConfigEnabled()) { | |
| 755 | +            if (!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) { | |
| 756 | + $errors[] = array( | |
| 757 | +                    'error' => $l->t('Cannot write into "config" directory'), | |
| 758 | +                    'hint' => $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s', | |
| 759 | +                        [ $urlGenerator->linkToDocs('admin-dir_permissions') ]) . '. ' | |
| 760 | +                        . $l->t('Or, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it. See %s', | |
| 761 | +                        [ $urlGenerator->linkToDocs('admin-config') ] ) | |
| 762 | + ); | |
| 763 | + } | |
| 764 | + } | |
| 765 | + | |
| 766 | + // Check if there is a writable install folder. | |
| 767 | +        if ($config->getValue('appstoreenabled', true)) { | |
| 768 | + if (OC_App::getInstallPath() === null | |
| 769 | + || !is_writable(OC_App::getInstallPath()) | |
| 770 | + || !is_readable(OC_App::getInstallPath()) | |
| 771 | +            ) { | |
| 772 | + $errors[] = array( | |
| 773 | +                    'error' => $l->t('Cannot write into "apps" directory'), | |
| 774 | +                    'hint' => $l->t('This can usually be fixed by giving the webserver write access to the apps directory' | |
| 775 | + . ' or disabling the appstore in the config file. See %s', | |
| 776 | +                        [$urlGenerator->linkToDocs('admin-dir_permissions')]) | |
| 777 | + ); | |
| 778 | + } | |
| 779 | + } | |
| 780 | + // Create root dir. | |
| 781 | +        if ($config->getValue('installed', false)) { | |
| 782 | +            if (!is_dir($CONFIG_DATADIRECTORY)) { | |
| 783 | + $success = @mkdir($CONFIG_DATADIRECTORY); | |
| 784 | +                if ($success) { | |
| 785 | + $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); | |
| 786 | +                } else { | |
| 787 | + $errors[] = [ | |
| 788 | +                        'error' => $l->t('Cannot create "data" directory'), | |
| 789 | +                        'hint' => $l->t('This can usually be fixed by giving the webserver write access to the root directory. See %s', | |
| 790 | +                            [$urlGenerator->linkToDocs('admin-dir_permissions')]) | |
| 791 | + ]; | |
| 792 | + } | |
| 793 | +            } else if (!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) { | |
| 794 | + // is_writable doesn't work for NFS mounts, so try to write a file and check if it exists. | |
| 795 | +                $testFile = sprintf('%s/%s.tmp', $CONFIG_DATADIRECTORY, uniqid('data_dir_writability_test_')); | |
| 796 | + $handle = fopen($testFile, 'w'); | |
| 797 | +                if (!$handle || fwrite($handle, 'Test write operation') === FALSE) { | |
| 798 | +                    $permissionsHint = $l->t('Permissions can usually be fixed by giving the webserver write access to the root directory. See %s.', | |
| 799 | +                        [$urlGenerator->linkToDocs('admin-dir_permissions')]); | |
| 800 | + $errors[] = [ | |
| 801 | + 'error' => 'Your data directory is not writable', | |
| 802 | + 'hint' => $permissionsHint | |
| 803 | + ]; | |
| 804 | +                } else { | |
| 805 | + fclose($handle); | |
| 806 | + unlink($testFile); | |
| 807 | + } | |
| 808 | +            } else { | |
| 809 | + $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY)); | |
| 810 | + } | |
| 811 | + } | |
| 812 | + | |
| 813 | +        if (!OC_Util::isSetLocaleWorking()) { | |
| 814 | + $errors[] = array( | |
| 815 | +                'error' => $l->t('Setting locale to %s failed', | |
| 816 | +                    array('en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/' | |
| 817 | + . 'pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8')), | |
| 818 | +                'hint' => $l->t('Please install one of these locales on your system and restart your webserver.') | |
| 819 | + ); | |
| 820 | + } | |
| 821 | + | |
| 822 | + // Contains the dependencies that should be checked against | |
| 823 | + // classes = class_exists | |
| 824 | + // functions = function_exists | |
| 825 | + // defined = defined | |
| 826 | + // ini = ini_get | |
| 827 | + // If the dependency is not found the missing module name is shown to the EndUser | |
| 828 | + // When adding new checks always verify that they pass on Travis as well | |
| 829 | + // for ini settings, see https://github.com/owncloud/administration/blob/master/travis-ci/custom.ini | |
| 830 | + $dependencies = array( | |
| 831 | + 'classes' => array( | |
| 832 | + 'ZipArchive' => 'zip', | |
| 833 | + 'DOMDocument' => 'dom', | |
| 834 | + 'XMLWriter' => 'XMLWriter', | |
| 835 | + 'XMLReader' => 'XMLReader', | |
| 836 | + ), | |
| 837 | + 'functions' => [ | |
| 838 | + 'xml_parser_create' => 'libxml', | |
| 839 | + 'mb_strcut' => 'mbstring', | |
| 840 | + 'ctype_digit' => 'ctype', | |
| 841 | + 'json_encode' => 'JSON', | |
| 842 | + 'gd_info' => 'GD', | |
| 843 | + 'gzencode' => 'zlib', | |
| 844 | + 'iconv' => 'iconv', | |
| 845 | + 'simplexml_load_string' => 'SimpleXML', | |
| 846 | + 'hash' => 'HASH Message Digest Framework', | |
| 847 | + 'curl_init' => 'cURL', | |
| 848 | + 'openssl_verify' => 'OpenSSL', | |
| 849 | + ], | |
| 850 | + 'defined' => array( | |
| 851 | + 'PDO::ATTR_DRIVER_NAME' => 'PDO' | |
| 852 | + ), | |
| 853 | + 'ini' => [ | |
| 854 | + 'default_charset' => 'UTF-8', | |
| 855 | + ], | |
| 856 | + ); | |
| 857 | + $missingDependencies = array(); | |
| 858 | + $invalidIniSettings = []; | |
| 859 | +        $moduleHint = $l->t('Please ask your server administrator to install the module.'); | |
| 860 | + | |
| 861 | + $iniWrapper = \OC::$server->getIniWrapper(); | |
| 862 | +        foreach ($dependencies['classes'] as $class => $module) { | |
| 863 | +            if (!class_exists($class)) { | |
| 864 | + $missingDependencies[] = $module; | |
| 865 | + } | |
| 866 | + } | |
| 867 | +        foreach ($dependencies['functions'] as $function => $module) { | |
| 868 | +            if (!function_exists($function)) { | |
| 869 | + $missingDependencies[] = $module; | |
| 870 | + } | |
| 871 | + } | |
| 872 | +        foreach ($dependencies['defined'] as $defined => $module) { | |
| 873 | +            if (!defined($defined)) { | |
| 874 | + $missingDependencies[] = $module; | |
| 875 | + } | |
| 876 | + } | |
| 877 | +        foreach ($dependencies['ini'] as $setting => $expected) { | |
| 878 | +            if (is_bool($expected)) { | |
| 879 | +                if ($iniWrapper->getBool($setting) !== $expected) { | |
| 880 | + $invalidIniSettings[] = [$setting, $expected]; | |
| 881 | + } | |
| 882 | + } | |
| 883 | +            if (is_int($expected)) { | |
| 884 | +                if ($iniWrapper->getNumeric($setting) !== $expected) { | |
| 885 | + $invalidIniSettings[] = [$setting, $expected]; | |
| 886 | + } | |
| 887 | + } | |
| 888 | +            if (is_string($expected)) { | |
| 889 | +                if (strtolower($iniWrapper->getString($setting)) !== strtolower($expected)) { | |
| 890 | + $invalidIniSettings[] = [$setting, $expected]; | |
| 891 | + } | |
| 892 | + } | |
| 893 | + } | |
| 894 | + | |
| 895 | +        foreach($missingDependencies as $missingDependency) { | |
| 896 | + $errors[] = array( | |
| 897 | +                'error' => $l->t('PHP module %s not installed.', array($missingDependency)), | |
| 898 | + 'hint' => $moduleHint | |
| 899 | + ); | |
| 900 | + $webServerRestart = true; | |
| 901 | + } | |
| 902 | +        foreach($invalidIniSettings as $setting) { | |
| 903 | +            if(is_bool($setting[1])) { | |
| 904 | + $setting[1] = $setting[1] ? 'on' : 'off'; | |
| 905 | + } | |
| 906 | + $errors[] = [ | |
| 907 | +                'error' => $l->t('PHP setting "%s" is not set to "%s".', [$setting[0], var_export($setting[1], true)]), | |
| 908 | +                'hint' =>  $l->t('Adjusting this setting in php.ini will make Nextcloud run again') | |
| 909 | + ]; | |
| 910 | + $webServerRestart = true; | |
| 911 | + } | |
| 912 | + | |
| 913 | + /** | |
| 914 | + * The mbstring.func_overload check can only be performed if the mbstring | |
| 915 | + * module is installed as it will return null if the checking setting is | |
| 916 | + * not available and thus a check on the boolean value fails. | |
| 917 | + * | |
| 918 | + * TODO: Should probably be implemented in the above generic dependency | |
| 919 | + * check somehow in the long-term. | |
| 920 | + */ | |
| 921 | +        if($iniWrapper->getBool('mbstring.func_overload') !== null && | |
| 922 | +            $iniWrapper->getBool('mbstring.func_overload') === true) { | |
| 923 | + $errors[] = array( | |
| 924 | +                'error' => $l->t('mbstring.func_overload is set to "%s" instead of the expected value "0"', [$iniWrapper->getString('mbstring.func_overload')]), | |
| 925 | +                'hint' => $l->t('To fix this issue set <code>mbstring.func_overload</code> to <code>0</code> in your php.ini') | |
| 926 | + ); | |
| 927 | + } | |
| 928 | + | |
| 929 | +        if(function_exists('xml_parser_create') && | |
| 930 | +            LIBXML_LOADED_VERSION < 20700 ) { | |
| 931 | + $version = LIBXML_LOADED_VERSION; | |
| 932 | + $major = floor($version/10000); | |
| 933 | + $version -= ($major * 10000); | |
| 934 | + $minor = floor($version/100); | |
| 935 | + $version -= ($minor * 100); | |
| 936 | + $patch = $version; | |
| 937 | + $errors[] = array( | |
| 938 | +                'error' => $l->t('libxml2 2.7.0 is at least required. Currently %s is installed.', [$major . '.' . $minor . '.' . $patch]), | |
| 939 | +                'hint' => $l->t('To fix this issue update your libxml2 version and restart your web server.') | |
| 940 | + ); | |
| 941 | + } | |
| 942 | + | |
| 943 | +        if (!self::isAnnotationsWorking()) { | |
| 944 | + $errors[] = array( | |
| 945 | +                'error' => $l->t('PHP is apparently set up to strip inline doc blocks. This will make several core apps inaccessible.'), | |
| 946 | +                'hint' => $l->t('This is probably caused by a cache/accelerator such as Zend OPcache or eAccelerator.') | |
| 947 | + ); | |
| 948 | + } | |
| 949 | + | |
| 950 | +        if (!\OC::$CLI && $webServerRestart) { | |
| 951 | + $errors[] = array( | |
| 952 | +                'error' => $l->t('PHP modules have been installed, but they are still listed as missing?'), | |
| 953 | +                'hint' => $l->t('Please ask your server administrator to restart the web server.') | |
| 954 | + ); | |
| 955 | + } | |
| 956 | + | |
| 957 | + $errors = array_merge($errors, self::checkDatabaseVersion()); | |
| 958 | + | |
| 959 | + // Cache the result of this function | |
| 960 | +        \OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0); | |
| 961 | + | |
| 962 | + return $errors; | |
| 963 | + } | |
| 964 | + | |
| 965 | + /** | |
| 966 | + * Check the database version | |
| 967 | + * | |
| 968 | + * @return array errors array | |
| 969 | + */ | |
| 970 | +    public static function checkDatabaseVersion() { | |
| 971 | +        $l = \OC::$server->getL10N('lib'); | |
| 972 | + $errors = array(); | |
| 973 | +        $dbType = \OC::$server->getSystemConfig()->getValue('dbtype', 'sqlite'); | |
| 974 | +        if ($dbType === 'pgsql') { | |
| 975 | + // check PostgreSQL version | |
| 976 | +            try { | |
| 977 | +                $result = \OC_DB::executeAudited('SHOW SERVER_VERSION'); | |
| 978 | + $data = $result->fetchRow(); | |
| 979 | +                if (isset($data['server_version'])) { | |
| 980 | + $version = $data['server_version']; | |
| 981 | +                    if (version_compare($version, '9.5.0', '<')) { | |
| 982 | + $errors[] = array( | |
| 983 | +                            'error' => $l->t('PostgreSQL >= 9.5 required'), | |
| 984 | +                            'hint' => $l->t('Please upgrade your database version') | |
| 985 | + ); | |
| 986 | + } | |
| 987 | + } | |
| 988 | +            } catch (\Doctrine\DBAL\DBALException $e) { | |
| 989 | + $logger = \OC::$server->getLogger(); | |
| 990 | +                $logger->warning('Error occurred while checking PostgreSQL version, assuming >= 9'); | |
| 991 | + $logger->logException($e); | |
| 992 | + } | |
| 993 | + } | |
| 994 | + return $errors; | |
| 995 | + } | |
| 996 | + | |
| 997 | + /** | |
| 998 | + * Check for correct file permissions of data directory | |
| 999 | + * | |
| 1000 | + * @param string $dataDirectory | |
| 1001 | + * @return array arrays with error messages and hints | |
| 1002 | + */ | |
| 1003 | +    public static function checkDataDirectoryPermissions($dataDirectory) { | |
| 1004 | +        if(\OC::$server->getConfig()->getSystemValue('check_data_directory_permissions', true) === false) { | |
| 1005 | + return []; | |
| 1006 | + } | |
| 1007 | +        $l = \OC::$server->getL10N('lib'); | |
| 1008 | + $errors = []; | |
| 1009 | +        $permissionsModHint = $l->t('Please change the permissions to 0770 so that the directory' | |
| 1010 | + . ' cannot be listed by other users.'); | |
| 1011 | + $perms = substr(decoct(@fileperms($dataDirectory)), -3); | |
| 1012 | +        if (substr($perms, -1) !== '0') { | |
| 1013 | + chmod($dataDirectory, 0770); | |
| 1014 | + clearstatcache(); | |
| 1015 | + $perms = substr(decoct(@fileperms($dataDirectory)), -3); | |
| 1016 | +            if ($perms[2] !== '0') { | |
| 1017 | + $errors[] = [ | |
| 1018 | +                    'error' => $l->t('Your data directory is readable by other users'), | |
| 1019 | + 'hint' => $permissionsModHint | |
| 1020 | + ]; | |
| 1021 | + } | |
| 1022 | + } | |
| 1023 | + return $errors; | |
| 1024 | + } | |
| 1025 | + | |
| 1026 | + /** | |
| 1027 | + * Check that the data directory exists and is valid by | |
| 1028 | + * checking the existence of the ".ocdata" file. | |
| 1029 | + * | |
| 1030 | + * @param string $dataDirectory data directory path | |
| 1031 | + * @return array errors found | |
| 1032 | + */ | |
| 1033 | +    public static function checkDataDirectoryValidity($dataDirectory) { | |
| 1034 | +        $l = \OC::$server->getL10N('lib'); | |
| 1035 | + $errors = []; | |
| 1036 | +        if ($dataDirectory[0] !== '/') { | |
| 1037 | + $errors[] = [ | |
| 1038 | +                'error' => $l->t('Your data directory must be an absolute path'), | |
| 1039 | +                'hint' => $l->t('Check the value of "datadirectory" in your configuration') | |
| 1040 | + ]; | |
| 1041 | + } | |
| 1042 | +        if (!file_exists($dataDirectory . '/.ocdata')) { | |
| 1043 | + $errors[] = [ | |
| 1044 | +                'error' => $l->t('Your data directory is invalid'), | |
| 1045 | +                'hint' => $l->t('Ensure there is a file called ".ocdata"' . | |
| 1046 | + ' in the root of the data directory.') | |
| 1047 | + ]; | |
| 1048 | + } | |
| 1049 | + return $errors; | |
| 1050 | + } | |
| 1051 | + | |
| 1052 | + /** | |
| 1053 | + * Check if the user is logged in, redirects to home if not. With | |
| 1054 | + * redirect URL parameter to the request URI. | |
| 1055 | + * | |
| 1056 | + * @return void | |
| 1057 | + */ | |
| 1058 | +    public static function checkLoggedIn() { | |
| 1059 | + // Check if we are a user | |
| 1060 | +        if (!\OC::$server->getUserSession()->isLoggedIn()) { | |
| 1061 | +            header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute( | |
| 1062 | + 'core.login.showLoginForm', | |
| 1063 | + [ | |
| 1064 | + 'redirect_url' => \OC::$server->getRequest()->getRequestUri(), | |
| 1065 | + ] | |
| 1066 | + ) | |
| 1067 | + ); | |
| 1068 | + exit(); | |
| 1069 | + } | |
| 1070 | + // Redirect to 2FA challenge selection if 2FA challenge was not solved yet | |
| 1071 | +        if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { | |
| 1072 | +            header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.selectChallenge')); | |
| 1073 | + exit(); | |
| 1074 | + } | |
| 1075 | + } | |
| 1076 | + | |
| 1077 | + /** | |
| 1078 | + * Check if the user is a admin, redirects to home if not | |
| 1079 | + * | |
| 1080 | + * @return void | |
| 1081 | + */ | |
| 1082 | +    public static function checkAdminUser() { | |
| 1083 | + OC_Util::checkLoggedIn(); | |
| 1084 | +        if (!OC_User::isAdminUser(OC_User::getUser())) { | |
| 1085 | +            header('Location: ' . \OCP\Util::linkToAbsolute('', 'index.php')); | |
| 1086 | + exit(); | |
| 1087 | + } | |
| 1088 | + } | |
| 1089 | + | |
| 1090 | + /** | |
| 1091 | + * Returns the URL of the default page | |
| 1092 | + * based on the system configuration and | |
| 1093 | + * the apps visible for the current user | |
| 1094 | + * | |
| 1095 | + * @return string URL | |
| 1096 | + * @suppress PhanDeprecatedFunction | |
| 1097 | + */ | |
| 1098 | +    public static function getDefaultPageUrl() { | |
| 1099 | + $urlGenerator = \OC::$server->getURLGenerator(); | |
| 1100 | + // Deny the redirect if the URL contains a @ | |
| 1101 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] | |
| 1102 | +        if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) { | |
| 1103 | + $location = $urlGenerator->getAbsoluteURL(urldecode($_REQUEST['redirect_url'])); | |
| 1104 | +        } else { | |
| 1105 | +            $defaultPage = \OC::$server->getConfig()->getAppValue('core', 'defaultpage'); | |
| 1106 | +            if ($defaultPage) { | |
| 1107 | + $location = $urlGenerator->getAbsoluteURL($defaultPage); | |
| 1108 | +            } else { | |
| 1109 | + $appId = 'files'; | |
| 1110 | + $config = \OC::$server->getConfig(); | |
| 1111 | +                $defaultApps = explode(',', $config->getSystemValue('defaultapp', 'files')); | |
| 1112 | + // find the first app that is enabled for the current user | |
| 1113 | +                foreach ($defaultApps as $defaultApp) { | |
| 1114 | + $defaultApp = OC_App::cleanAppId(strip_tags($defaultApp)); | |
| 1115 | +                    if (static::getAppManager()->isEnabledForUser($defaultApp)) { | |
| 1116 | + $appId = $defaultApp; | |
| 1117 | + break; | |
| 1118 | + } | |
| 1119 | + } | |
| 1120 | + | |
| 1121 | +                if($config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true') { | |
| 1122 | +                    $location = $urlGenerator->getAbsoluteURL('/apps/' . $appId . '/'); | |
| 1123 | +                } else { | |
| 1124 | +                    $location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/'); | |
| 1125 | + } | |
| 1126 | + } | |
| 1127 | + } | |
| 1128 | + return $location; | |
| 1129 | + } | |
| 1130 | + | |
| 1131 | + /** | |
| 1132 | + * Redirect to the user default page | |
| 1133 | + * | |
| 1134 | + * @return void | |
| 1135 | + */ | |
| 1136 | +    public static function redirectToDefaultPage() { | |
| 1137 | + $location = self::getDefaultPageUrl(); | |
| 1138 | +        header('Location: ' . $location); | |
| 1139 | + exit(); | |
| 1140 | + } | |
| 1141 | + | |
| 1142 | + /** | |
| 1143 | + * get an id unique for this instance | |
| 1144 | + * | |
| 1145 | + * @return string | |
| 1146 | + */ | |
| 1147 | +    public static function getInstanceId() { | |
| 1148 | +        $id = \OC::$server->getSystemConfig()->getValue('instanceid', null); | |
| 1149 | +        if (is_null($id)) { | |
| 1150 | + // We need to guarantee at least one letter in instanceid so it can be used as the session_name | |
| 1151 | + $id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS); | |
| 1152 | +            \OC::$server->getSystemConfig()->setValue('instanceid', $id); | |
| 1153 | + } | |
| 1154 | + return $id; | |
| 1155 | + } | |
| 1156 | + | |
| 1157 | + /** | |
| 1158 | + * Public function to sanitize HTML | |
| 1159 | + * | |
| 1160 | + * This function is used to sanitize HTML and should be applied on any | |
| 1161 | + * string or array of strings before displaying it on a web page. | |
| 1162 | + * | |
| 1163 | + * @param string|array $value | |
| 1164 | + * @return string|array an array of sanitized strings or a single sanitized string, depends on the input parameter. | |
| 1165 | + */ | |
| 1166 | +    public static function sanitizeHTML($value) { | |
| 1167 | +        if (is_array($value)) { | |
| 1168 | +            $value = array_map(function($value) { | |
| 1169 | + return self::sanitizeHTML($value); | |
| 1170 | + }, $value); | |
| 1171 | +        } else { | |
| 1172 | + // Specify encoding for PHP<5.4 | |
| 1173 | + $value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); | |
| 1174 | + } | |
| 1175 | + return $value; | |
| 1176 | + } | |
| 1177 | + | |
| 1178 | + /** | |
| 1179 | + * Public function to encode url parameters | |
| 1180 | + * | |
| 1181 | + * This function is used to encode path to file before output. | |
| 1182 | + * Encoding is done according to RFC 3986 with one exception: | |
| 1183 | + * Character '/' is preserved as is. | |
| 1184 | + * | |
| 1185 | + * @param string $component part of URI to encode | |
| 1186 | + * @return string | |
| 1187 | + */ | |
| 1188 | +    public static function encodePath($component) { | |
| 1189 | + $encoded = rawurlencode($component); | |
| 1190 | +        $encoded = str_replace('%2F', '/', $encoded); | |
| 1191 | + return $encoded; | |
| 1192 | + } | |
| 1193 | + | |
| 1194 | + | |
| 1195 | +    public function createHtaccessTestFile(\OCP\IConfig $config) { | |
| 1196 | + // php dev server does not support htaccess | |
| 1197 | +        if (php_sapi_name() === 'cli-server') { | |
| 1198 | + return false; | |
| 1199 | + } | |
| 1200 | + | |
| 1201 | + // testdata | |
| 1202 | + $fileName = '/htaccesstest.txt'; | |
| 1203 | + $testContent = 'This is used for testing whether htaccess is properly enabled to disallow access from the outside. This file can be safely removed.'; | |
| 1204 | + | |
| 1205 | + // creating a test file | |
| 1206 | +        $testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; | |
| 1207 | + | |
| 1208 | +        if (file_exists($testFile)) {// already running this test, possible recursive call | |
| 1209 | + return false; | |
| 1210 | + } | |
| 1211 | + | |
| 1212 | + $fp = @fopen($testFile, 'w'); | |
| 1213 | +        if (!$fp) { | |
| 1214 | +            throw new OC\HintException('Can\'t create test file to check for working .htaccess file.', | |
| 1215 | + 'Make sure it is possible for the webserver to write to ' . $testFile); | |
| 1216 | + } | |
| 1217 | + fwrite($fp, $testContent); | |
| 1218 | + fclose($fp); | |
| 1219 | + | |
| 1220 | + return $testContent; | |
| 1221 | + } | |
| 1222 | + | |
| 1223 | + /** | |
| 1224 | + * Check if the .htaccess file is working | |
| 1225 | + * @param \OCP\IConfig $config | |
| 1226 | + * @return bool | |
| 1227 | + * @throws Exception | |
| 1228 | + * @throws \OC\HintException If the test file can't get written. | |
| 1229 | + */ | |
| 1230 | +    public function isHtaccessWorking(\OCP\IConfig $config) { | |
| 1231 | + | |
| 1232 | +        if (\OC::$CLI || !$config->getSystemValue('check_for_working_htaccess', true)) { | |
| 1233 | + return true; | |
| 1234 | + } | |
| 1235 | + | |
| 1236 | + $testContent = $this->createHtaccessTestFile($config); | |
| 1237 | +        if ($testContent === false) { | |
| 1238 | + return false; | |
| 1239 | + } | |
| 1240 | + | |
| 1241 | + $fileName = '/htaccesstest.txt'; | |
| 1242 | +        $testFile = $config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data') . '/' . $fileName; | |
| 1243 | + | |
| 1244 | + // accessing the file via http | |
| 1245 | + $url = \OC::$server->getURLGenerator()->getAbsoluteURL(OC::$WEBROOT . '/data' . $fileName); | |
| 1246 | +        try { | |
| 1247 | + $content = \OC::$server->getHTTPClientService()->newClient()->get($url)->getBody(); | |
| 1248 | +        } catch (\Exception $e) { | |
| 1249 | + $content = false; | |
| 1250 | + } | |
| 1251 | + | |
| 1252 | + // cleanup | |
| 1253 | + @unlink($testFile); | |
| 1254 | + | |
| 1255 | + /* | |
| 1256 | 1256 | * If the content is not equal to test content our .htaccess | 
| 1257 | 1257 | * is working as required | 
| 1258 | 1258 | */ | 
| 1259 | - return $content !== $testContent; | |
| 1260 | - } | |
| 1261 | - | |
| 1262 | - /** | |
| 1263 | - * Check if the setlocal call does not work. This can happen if the right | |
| 1264 | - * local packages are not available on the server. | |
| 1265 | - * | |
| 1266 | - * @return bool | |
| 1267 | - */ | |
| 1268 | -	public static function isSetLocaleWorking() { | |
| 1269 | - \Patchwork\Utf8\Bootup::initLocale(); | |
| 1270 | -		if ('' === basename('§')) { | |
| 1271 | - return false; | |
| 1272 | - } | |
| 1273 | - return true; | |
| 1274 | - } | |
| 1275 | - | |
| 1276 | - /** | |
| 1277 | - * Check if it's possible to get the inline annotations | |
| 1278 | - * | |
| 1279 | - * @return bool | |
| 1280 | - */ | |
| 1281 | -	public static function isAnnotationsWorking() { | |
| 1282 | - $reflection = new \ReflectionMethod(__METHOD__); | |
| 1283 | - $docs = $reflection->getDocComment(); | |
| 1284 | - | |
| 1285 | - return (is_string($docs) && strlen($docs) > 50); | |
| 1286 | - } | |
| 1287 | - | |
| 1288 | - /** | |
| 1289 | - * Check if the PHP module fileinfo is loaded. | |
| 1290 | - * | |
| 1291 | - * @return bool | |
| 1292 | - */ | |
| 1293 | -	public static function fileInfoLoaded() { | |
| 1294 | -		return function_exists('finfo_open'); | |
| 1295 | - } | |
| 1296 | - | |
| 1297 | - /** | |
| 1298 | - * clear all levels of output buffering | |
| 1299 | - * | |
| 1300 | - * @return void | |
| 1301 | - */ | |
| 1302 | -	public static function obEnd() { | |
| 1303 | -		while (ob_get_level()) { | |
| 1304 | - ob_end_clean(); | |
| 1305 | - } | |
| 1306 | - } | |
| 1307 | - | |
| 1308 | - /** | |
| 1309 | - * Checks whether the server is running on Mac OS X | |
| 1310 | - * | |
| 1311 | - * @return bool true if running on Mac OS X, false otherwise | |
| 1312 | - */ | |
| 1313 | -	public static function runningOnMac() { | |
| 1314 | - return (strtoupper(substr(PHP_OS, 0, 6)) === 'DARWIN'); | |
| 1315 | - } | |
| 1316 | - | |
| 1317 | - /** | |
| 1318 | - * Handles the case that there may not be a theme, then check if a "default" | |
| 1319 | - * theme exists and take that one | |
| 1320 | - * | |
| 1321 | - * @return string the theme | |
| 1322 | - */ | |
| 1323 | -	public static function getTheme() { | |
| 1324 | -		$theme = \OC::$server->getSystemConfig()->getValue("theme", ''); | |
| 1325 | - | |
| 1326 | -		if ($theme === '') { | |
| 1327 | -			if (is_dir(OC::$SERVERROOT . '/themes/default')) { | |
| 1328 | - $theme = 'default'; | |
| 1329 | - } | |
| 1330 | - } | |
| 1331 | - | |
| 1332 | - return $theme; | |
| 1333 | - } | |
| 1334 | - | |
| 1335 | - /** | |
| 1336 | - * Normalize a unicode string | |
| 1337 | - * | |
| 1338 | - * @param string $value a not normalized string | |
| 1339 | - * @return bool|string | |
| 1340 | - */ | |
| 1341 | -	public static function normalizeUnicode($value) { | |
| 1342 | -		if(Normalizer::isNormalized($value)) { | |
| 1343 | - return $value; | |
| 1344 | - } | |
| 1345 | - | |
| 1346 | - $normalizedValue = Normalizer::normalize($value); | |
| 1347 | -		if ($normalizedValue === null || $normalizedValue === false) { | |
| 1348 | -			\OC::$server->getLogger()->warning('normalizing failed for "' . $value . '"', ['app' => 'core']); | |
| 1349 | - return $value; | |
| 1350 | - } | |
| 1351 | - | |
| 1352 | - return $normalizedValue; | |
| 1353 | - } | |
| 1354 | - | |
| 1355 | - /** | |
| 1356 | - * A human readable string is generated based on version and build number | |
| 1357 | - * | |
| 1358 | - * @return string | |
| 1359 | - */ | |
| 1360 | -	public static function getHumanVersion() { | |
| 1361 | - $version = OC_Util::getVersionString(); | |
| 1362 | - $build = OC_Util::getBuild(); | |
| 1363 | -		if (!empty($build) and OC_Util::getChannel() === 'daily') { | |
| 1364 | - $version .= ' Build:' . $build; | |
| 1365 | - } | |
| 1366 | - return $version; | |
| 1367 | - } | |
| 1368 | - | |
| 1369 | - /** | |
| 1370 | - * Returns whether the given file name is valid | |
| 1371 | - * | |
| 1372 | - * @param string $file file name to check | |
| 1373 | - * @return bool true if the file name is valid, false otherwise | |
| 1374 | - * @deprecated use \OC\Files\View::verifyPath() | |
| 1375 | - */ | |
| 1376 | -	public static function isValidFileName($file) { | |
| 1377 | - $trimmed = trim($file); | |
| 1378 | -		if ($trimmed === '') { | |
| 1379 | - return false; | |
| 1380 | - } | |
| 1381 | -		if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { | |
| 1382 | - return false; | |
| 1383 | - } | |
| 1384 | - | |
| 1385 | - // detect part files | |
| 1386 | -		if (preg_match('/' . \OCP\Files\FileInfo::BLACKLIST_FILES_REGEX . '/', $trimmed) !== 0) { | |
| 1387 | - return false; | |
| 1388 | - } | |
| 1389 | - | |
| 1390 | -		foreach (str_split($trimmed) as $char) { | |
| 1391 | -			if (strpos(\OCP\Constants::FILENAME_INVALID_CHARS, $char) !== false) { | |
| 1392 | - return false; | |
| 1393 | - } | |
| 1394 | - } | |
| 1395 | - return true; | |
| 1396 | - } | |
| 1397 | - | |
| 1398 | - /** | |
| 1399 | - * Check whether the instance needs to perform an upgrade, | |
| 1400 | - * either when the core version is higher or any app requires | |
| 1401 | - * an upgrade. | |
| 1402 | - * | |
| 1403 | - * @param \OC\SystemConfig $config | |
| 1404 | - * @return bool whether the core or any app needs an upgrade | |
| 1405 | - * @throws \OC\HintException When the upgrade from the given version is not allowed | |
| 1406 | - */ | |
| 1407 | -	public static function needUpgrade(\OC\SystemConfig $config) { | |
| 1408 | -		if ($config->getValue('installed', false)) { | |
| 1409 | -			$installedVersion = $config->getValue('version', '0.0.0'); | |
| 1410 | -			$currentVersion = implode('.', \OCP\Util::getVersion()); | |
| 1411 | - $versionDiff = version_compare($currentVersion, $installedVersion); | |
| 1412 | -			if ($versionDiff > 0) { | |
| 1413 | - return true; | |
| 1414 | -			} else if ($config->getValue('debug', false) && $versionDiff < 0) { | |
| 1415 | - // downgrade with debug | |
| 1416 | -				$installedMajor = explode('.', $installedVersion); | |
| 1417 | - $installedMajor = $installedMajor[0] . '.' . $installedMajor[1]; | |
| 1418 | -				$currentMajor = explode('.', $currentVersion); | |
| 1419 | - $currentMajor = $currentMajor[0] . '.' . $currentMajor[1]; | |
| 1420 | -				if ($installedMajor === $currentMajor) { | |
| 1421 | - // Same major, allow downgrade for developers | |
| 1422 | - return true; | |
| 1423 | -				} else { | |
| 1424 | - // downgrade attempt, throw exception | |
| 1425 | -					throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')'); | |
| 1426 | - } | |
| 1427 | -			} else if ($versionDiff < 0) { | |
| 1428 | - // downgrade attempt, throw exception | |
| 1429 | -				throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')'); | |
| 1430 | - } | |
| 1431 | - | |
| 1432 | - // also check for upgrades for apps (independently from the user) | |
| 1433 | - $apps = \OC_App::getEnabledApps(false, true); | |
| 1434 | - $shouldUpgrade = false; | |
| 1435 | -			foreach ($apps as $app) { | |
| 1436 | -				if (\OC_App::shouldUpgrade($app)) { | |
| 1437 | - $shouldUpgrade = true; | |
| 1438 | - break; | |
| 1439 | - } | |
| 1440 | - } | |
| 1441 | - return $shouldUpgrade; | |
| 1442 | -		} else { | |
| 1443 | - return false; | |
| 1444 | - } | |
| 1445 | - } | |
| 1446 | - | |
| 1447 | - /** | |
| 1448 | - * is this Internet explorer ? | |
| 1449 | - * | |
| 1450 | - * @return boolean | |
| 1451 | - */ | |
| 1452 | -	public static function isIe() { | |
| 1453 | -		if (!isset($_SERVER['HTTP_USER_AGENT'])) { | |
| 1454 | - return false; | |
| 1455 | - } | |
| 1456 | - | |
| 1457 | - return preg_match(Request::USER_AGENT_IE, $_SERVER['HTTP_USER_AGENT']) === 1; | |
| 1458 | - } | |
| 1259 | + return $content !== $testContent; | |
| 1260 | + } | |
| 1261 | + | |
| 1262 | + /** | |
| 1263 | + * Check if the setlocal call does not work. This can happen if the right | |
| 1264 | + * local packages are not available on the server. | |
| 1265 | + * | |
| 1266 | + * @return bool | |
| 1267 | + */ | |
| 1268 | +    public static function isSetLocaleWorking() { | |
| 1269 | + \Patchwork\Utf8\Bootup::initLocale(); | |
| 1270 | +        if ('' === basename('§')) { | |
| 1271 | + return false; | |
| 1272 | + } | |
| 1273 | + return true; | |
| 1274 | + } | |
| 1275 | + | |
| 1276 | + /** | |
| 1277 | + * Check if it's possible to get the inline annotations | |
| 1278 | + * | |
| 1279 | + * @return bool | |
| 1280 | + */ | |
| 1281 | +    public static function isAnnotationsWorking() { | |
| 1282 | + $reflection = new \ReflectionMethod(__METHOD__); | |
| 1283 | + $docs = $reflection->getDocComment(); | |
| 1284 | + | |
| 1285 | + return (is_string($docs) && strlen($docs) > 50); | |
| 1286 | + } | |
| 1287 | + | |
| 1288 | + /** | |
| 1289 | + * Check if the PHP module fileinfo is loaded. | |
| 1290 | + * | |
| 1291 | + * @return bool | |
| 1292 | + */ | |
| 1293 | +    public static function fileInfoLoaded() { | |
| 1294 | +        return function_exists('finfo_open'); | |
| 1295 | + } | |
| 1296 | + | |
| 1297 | + /** | |
| 1298 | + * clear all levels of output buffering | |
| 1299 | + * | |
| 1300 | + * @return void | |
| 1301 | + */ | |
| 1302 | +    public static function obEnd() { | |
| 1303 | +        while (ob_get_level()) { | |
| 1304 | + ob_end_clean(); | |
| 1305 | + } | |
| 1306 | + } | |
| 1307 | + | |
| 1308 | + /** | |
| 1309 | + * Checks whether the server is running on Mac OS X | |
| 1310 | + * | |
| 1311 | + * @return bool true if running on Mac OS X, false otherwise | |
| 1312 | + */ | |
| 1313 | +    public static function runningOnMac() { | |
| 1314 | + return (strtoupper(substr(PHP_OS, 0, 6)) === 'DARWIN'); | |
| 1315 | + } | |
| 1316 | + | |
| 1317 | + /** | |
| 1318 | + * Handles the case that there may not be a theme, then check if a "default" | |
| 1319 | + * theme exists and take that one | |
| 1320 | + * | |
| 1321 | + * @return string the theme | |
| 1322 | + */ | |
| 1323 | +    public static function getTheme() { | |
| 1324 | +        $theme = \OC::$server->getSystemConfig()->getValue("theme", ''); | |
| 1325 | + | |
| 1326 | +        if ($theme === '') { | |
| 1327 | +            if (is_dir(OC::$SERVERROOT . '/themes/default')) { | |
| 1328 | + $theme = 'default'; | |
| 1329 | + } | |
| 1330 | + } | |
| 1331 | + | |
| 1332 | + return $theme; | |
| 1333 | + } | |
| 1334 | + | |
| 1335 | + /** | |
| 1336 | + * Normalize a unicode string | |
| 1337 | + * | |
| 1338 | + * @param string $value a not normalized string | |
| 1339 | + * @return bool|string | |
| 1340 | + */ | |
| 1341 | +    public static function normalizeUnicode($value) { | |
| 1342 | +        if(Normalizer::isNormalized($value)) { | |
| 1343 | + return $value; | |
| 1344 | + } | |
| 1345 | + | |
| 1346 | + $normalizedValue = Normalizer::normalize($value); | |
| 1347 | +        if ($normalizedValue === null || $normalizedValue === false) { | |
| 1348 | +            \OC::$server->getLogger()->warning('normalizing failed for "' . $value . '"', ['app' => 'core']); | |
| 1349 | + return $value; | |
| 1350 | + } | |
| 1351 | + | |
| 1352 | + return $normalizedValue; | |
| 1353 | + } | |
| 1354 | + | |
| 1355 | + /** | |
| 1356 | + * A human readable string is generated based on version and build number | |
| 1357 | + * | |
| 1358 | + * @return string | |
| 1359 | + */ | |
| 1360 | +    public static function getHumanVersion() { | |
| 1361 | + $version = OC_Util::getVersionString(); | |
| 1362 | + $build = OC_Util::getBuild(); | |
| 1363 | +        if (!empty($build) and OC_Util::getChannel() === 'daily') { | |
| 1364 | + $version .= ' Build:' . $build; | |
| 1365 | + } | |
| 1366 | + return $version; | |
| 1367 | + } | |
| 1368 | + | |
| 1369 | + /** | |
| 1370 | + * Returns whether the given file name is valid | |
| 1371 | + * | |
| 1372 | + * @param string $file file name to check | |
| 1373 | + * @return bool true if the file name is valid, false otherwise | |
| 1374 | + * @deprecated use \OC\Files\View::verifyPath() | |
| 1375 | + */ | |
| 1376 | +    public static function isValidFileName($file) { | |
| 1377 | + $trimmed = trim($file); | |
| 1378 | +        if ($trimmed === '') { | |
| 1379 | + return false; | |
| 1380 | + } | |
| 1381 | +        if (\OC\Files\Filesystem::isIgnoredDir($trimmed)) { | |
| 1382 | + return false; | |
| 1383 | + } | |
| 1384 | + | |
| 1385 | + // detect part files | |
| 1386 | +        if (preg_match('/' . \OCP\Files\FileInfo::BLACKLIST_FILES_REGEX . '/', $trimmed) !== 0) { | |
| 1387 | + return false; | |
| 1388 | + } | |
| 1389 | + | |
| 1390 | +        foreach (str_split($trimmed) as $char) { | |
| 1391 | +            if (strpos(\OCP\Constants::FILENAME_INVALID_CHARS, $char) !== false) { | |
| 1392 | + return false; | |
| 1393 | + } | |
| 1394 | + } | |
| 1395 | + return true; | |
| 1396 | + } | |
| 1397 | + | |
| 1398 | + /** | |
| 1399 | + * Check whether the instance needs to perform an upgrade, | |
| 1400 | + * either when the core version is higher or any app requires | |
| 1401 | + * an upgrade. | |
| 1402 | + * | |
| 1403 | + * @param \OC\SystemConfig $config | |
| 1404 | + * @return bool whether the core or any app needs an upgrade | |
| 1405 | + * @throws \OC\HintException When the upgrade from the given version is not allowed | |
| 1406 | + */ | |
| 1407 | +    public static function needUpgrade(\OC\SystemConfig $config) { | |
| 1408 | +        if ($config->getValue('installed', false)) { | |
| 1409 | +            $installedVersion = $config->getValue('version', '0.0.0'); | |
| 1410 | +            $currentVersion = implode('.', \OCP\Util::getVersion()); | |
| 1411 | + $versionDiff = version_compare($currentVersion, $installedVersion); | |
| 1412 | +            if ($versionDiff > 0) { | |
| 1413 | + return true; | |
| 1414 | +            } else if ($config->getValue('debug', false) && $versionDiff < 0) { | |
| 1415 | + // downgrade with debug | |
| 1416 | +                $installedMajor = explode('.', $installedVersion); | |
| 1417 | + $installedMajor = $installedMajor[0] . '.' . $installedMajor[1]; | |
| 1418 | +                $currentMajor = explode('.', $currentVersion); | |
| 1419 | + $currentMajor = $currentMajor[0] . '.' . $currentMajor[1]; | |
| 1420 | +                if ($installedMajor === $currentMajor) { | |
| 1421 | + // Same major, allow downgrade for developers | |
| 1422 | + return true; | |
| 1423 | +                } else { | |
| 1424 | + // downgrade attempt, throw exception | |
| 1425 | +                    throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')'); | |
| 1426 | + } | |
| 1427 | +            } else if ($versionDiff < 0) { | |
| 1428 | + // downgrade attempt, throw exception | |
| 1429 | +                throw new \OC\HintException('Downgrading is not supported and is likely to cause unpredictable issues (from ' . $installedVersion . ' to ' . $currentVersion . ')'); | |
| 1430 | + } | |
| 1431 | + | |
| 1432 | + // also check for upgrades for apps (independently from the user) | |
| 1433 | + $apps = \OC_App::getEnabledApps(false, true); | |
| 1434 | + $shouldUpgrade = false; | |
| 1435 | +            foreach ($apps as $app) { | |
| 1436 | +                if (\OC_App::shouldUpgrade($app)) { | |
| 1437 | + $shouldUpgrade = true; | |
| 1438 | + break; | |
| 1439 | + } | |
| 1440 | + } | |
| 1441 | + return $shouldUpgrade; | |
| 1442 | +        } else { | |
| 1443 | + return false; | |
| 1444 | + } | |
| 1445 | + } | |
| 1446 | + | |
| 1447 | + /** | |
| 1448 | + * is this Internet explorer ? | |
| 1449 | + * | |
| 1450 | + * @return boolean | |
| 1451 | + */ | |
| 1452 | +    public static function isIe() { | |
| 1453 | +        if (!isset($_SERVER['HTTP_USER_AGENT'])) { | |
| 1454 | + return false; | |
| 1455 | + } | |
| 1456 | + | |
| 1457 | + return preg_match(Request::USER_AGENT_IE, $_SERVER['HTTP_USER_AGENT']) === 1; | |
| 1458 | + } | |
| 1459 | 1459 | |
| 1460 | 1460 | } |