public function testPublicAdapter()
 {
     $_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Win64) PHP/5.3.13';
     $adapter = new PublicAssetAdapter($this->rootDir);
     $this->assertFileExists($this->rootDir . '/.htaccess');
     $this->assertFileNotExists($this->rootDir . '/web.config');
     $htaccess = $adapter->read('.htaccess');
     $content = $htaccess['contents'];
     // Allowed extensions set
     $this->assertContains('RewriteCond %{REQUEST_URI} !\\.(?i:', $content);
     foreach (File::config()->allowed_extensions as $extension) {
         $this->assertRegExp('/\\b' . preg_quote($extension) . '\\b/', $content);
     }
     // Rewrite rules
     $this->assertContains('RewriteRule .* ../framework/main.php?url=%1 [QSA]', $content);
     $this->assertContains('RewriteRule error[^\\\\/]*\\.html$ - [L]', $content);
     // Test flush restores invalid content
     \file_put_contents($this->rootDir . '/.htaccess', '# broken content');
     $adapter->flush();
     $htaccess2 = $adapter->read('.htaccess');
     $this->assertEquals($content, $htaccess2['contents']);
     // Test URL
     $this->assertEquals('/assets/AssetAdapterTest/file.jpg', $adapter->getPublicUrl('file.jpg'));
 }
 /**
  * Hook to validate this record against a validation result
  *
  * @param ValidationResult $result
  * @param string $filename Optional filename to validate. If omitted, the current value is validated.
  * @return bool Valid flag
  */
 public function validate(ValidationResult $result, $filename = null)
 {
     if (empty($filename)) {
         $filename = $this->getFilename();
     }
     if (empty($filename) || $this->isValidFilename($filename)) {
         return true;
     }
     // Check allowed extensions
     $extensions = $this->getAllowedExtensions();
     if (empty($extensions)) {
         $extensions = File::config()->allowed_extensions;
     }
     sort($extensions);
     $message = _t('File.INVALIDEXTENSION', 'Extension is not allowed (valid: {extensions})', 'Argument 1: Comma-separated list of valid extensions', array('extensions' => wordwrap(implode(', ', $extensions))));
     $result->error($message);
     return false;
 }
 /**
  * Construct a new UploadField instance
  *
  * @param string $name The internal field name, passed to forms.
  * @param string $title The field label.
  * @param SS_List $items If no items are defined, the field will try to auto-detect an existing relation on
  *                       @link $record}, with the same name as the field name.
  */
 public function __construct($name, $title = null, SS_List $items = null)
 {
     // TODO thats the first thing that came to my head, feel free to change it
     $this->addExtraClass('ss-upload');
     // class, used by js
     $this->addExtraClass('ss-uploadfield');
     // class, used by css for uploadfield only
     $this->ufConfig = self::config()->defaultConfig;
     parent::__construct($name, $title);
     if ($items) {
         $this->setItems($items);
     }
     // filter out '' since this would be a regex problem on JS end
     $this->getValidator()->setAllowedExtensions(array_filter(File::config()->allowed_extensions));
     // get the lower max size
     $maxUpload = File::ini2bytes(ini_get('upload_max_filesize'));
     $maxPost = File::ini2bytes(ini_get('post_max_size'));
     $this->getValidator()->setAllowedMaxFileSize(min($maxUpload, $maxPost));
 }
 /**
  * Set this store as the new asset backend
  *
  * @param string $basedir Basedir to store assets, which will be placed beneath 'assets' folder
  */
 public static function activate($basedir)
 {
     // Assign this as the new store
     $publicAdapter = new PublicAssetAdapter(ASSETS_PATH . '/' . $basedir);
     $publicFilesystem = new Filesystem($publicAdapter, ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]);
     $protectedAdapter = new ProtectedAssetAdapter(ASSETS_PATH . '/' . $basedir . '/.protected');
     $protectedFilesystem = new Filesystem($protectedAdapter, ['visibility' => AdapterInterface::VISIBILITY_PRIVATE]);
     $backend = new AssetStoreTest_SpyStore();
     $backend->setPublicFilesystem($publicFilesystem);
     $backend->setProtectedFilesystem($protectedFilesystem);
     Injector::inst()->registerService($backend, 'AssetStore');
     // Assign flysystem backend to generated asset handler at the same time
     $generated = new GeneratedAssetHandler();
     $generated->setFilesystem($publicFilesystem);
     Injector::inst()->registerService($generated, 'GeneratedAssetHandler');
     Requirements::backend()->setAssetHandler($generated);
     // Disable legacy and set defaults
     Config::inst()->remove(get_class(new FlysystemAssetStore()), 'legacy_filenames');
     Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', '/');
     DBFile::config()->force_resample = false;
     File::config()->force_resample = false;
     self::reset();
     self::$basedir = $basedir;
     // Ensure basedir exists
     SSFilesystem::makeFolder(self::base_path());
 }
 /**
  * Construct a new UploadField instance
  *
  * @param string $name The internal field name, passed to forms.
  * @param string $title The field label.
  */
 public function __construct($name, $title = null)
 {
     $this->addExtraClass('ss-upload');
     // class, used by js
     $this->addExtraClass('ss-uploadfield');
     // class, used by css for uploadfield only
     $this->ufConfig = array_merge($this->ufConfig, self::config()->defaultConfig);
     parent::__construct($name, $title);
     // AssetField always uses rename replacement method
     $this->getUpload()->setReplaceFile(false);
     // filter out '' since this would be a regex problem on JS end
     $this->getValidator()->setAllowedExtensions(array_filter(File::config()->allowed_extensions));
     // get the lower max size
     $maxUpload = File::ini2bytes(ini_get('upload_max_filesize'));
     $maxPost = File::ini2bytes(ini_get('post_max_size'));
     $this->getValidator()->setAllowedMaxFileSize(min($maxUpload, $maxPost));
 }
 /**
  * @return Upload
  */
 protected function getUpload()
 {
     $upload = Upload::create();
     $upload->getValidator()->setAllowedExtensions(array_filter(File::config()->get('allowed_extensions')));
     return $upload;
 }
 /**
  * Gets the list of all extensions for testing
  *
  * @return array
  */
 public function allowedExtensions()
 {
     $args = array();
     foreach (array_filter(File::config()->allowed_extensions) as $ext) {
         $args[] = array($ext);
     }
     return $args;
 }
 /**
  * Render server configuration file from a template file
  *
  * @param string $template
  * @return string Rendered results
  */
 protected function renderTemplate($template)
 {
     // Build allowed extensions
     $allowedExtensions = new ArrayList();
     foreach (File::config()->allowed_extensions as $extension) {
         if ($extension) {
             $allowedExtensions->push(new ArrayData(array('Extension' => preg_quote($extension))));
         }
     }
     $viewer = new SSViewer(array($template));
     return (string) $viewer->process(new ArrayData(array('AllowedExtensions' => $allowedExtensions)));
 }