Example #1
0
 /**
  * Rotate log file if size specified in config is reached.
  * Also if `rotate` count is reached oldest file is removed.
  *
  * @return mixed True if rotated successfully or false in case of error.
  *   Void if file doesn't need to be rotated.
  */
 protected function _rotateLines()
 {
     $filepath = $this->_path . $this->_file;
     $file = new File($filepath);
     $fileContent = $file->read();
     $fileLines = explode(PHP_EOL, $fileContent);
     if (!$file->exists() and count($fileLines) < $this->_maxLine) {
         return;
     }
     while (count($fileLines) >= $this->_maxLine) {
         array_shift($fileLines);
     }
     return $file->write(implode(PHP_EOL, $fileLines));
 }
Example #2
0
 /**
  * Update the applications plugins.php file.
  *
  * @param string $plugin Name of plugin.
  * @return bool If modify passed.
  */
 protected function _modifyBootstrap($plugin)
 {
     $finder = "/\nPluginLoader::load\\((.|.\n|\n\\s\\s|\n\t|)+'{$plugin}'(.|.\n|)+\\);\n/";
     $plugins = new File($this->_pluginsFile, false);
     $contents = $plugins->read();
     if (!preg_match("@\n\\s*nPluginLoader::loadAll@", $contents)) {
         $contents = preg_replace($finder, "", $contents);
         $plugins->write($contents);
         $this->out('');
         $this->out(sprintf('%s modified', $this->_pluginsFile));
         return true;
     }
     return false;
 }
Example #3
0
 /**
  * Update the applications plugins.php file.
  *
  * @param string $plugin Name of plugin.
  * @param bool $hasBootstrap Whether or not bootstrap should be loaded.
  * @param bool $hasRoutes Whether or not routes should be loaded.
  * @param bool $hasAutoloader Whether or not there is an autoloader configured for
  * the plugin.
  * @return bool If modify passed.
  */
 protected function _modifyBootstrap($plugin, $hasBootstrap, $hasRoutes, $hasAutoloader)
 {
     $bootstrap = new File($this->_pluginsFile, false);
     $contents = $bootstrap->read();
     if (!preg_match("@\n\\s*PluginLoader::loadAll@", $contents)) {
         $autoloadString = $hasAutoloader ? "'autoload' => true" : '';
         $bootstrapString = $hasBootstrap ? "'bootstrap' => true" : '';
         $routesString = $hasRoutes ? "'routes' => true" : '';
         $append = "\\PluginLoader::load('%s', [%s]);\n";
         $options = implode(', ', array_filter([$autoloadString, $bootstrapString, $routesString]));
         $bootstrap->append(str_replace(', []', '', sprintf($append, $plugin, $options)));
         $this->out('');
         $this->out(sprintf('%s modified', $this->_pluginsFile));
         return true;
     }
     return false;
 }
Example #4
0
 /**
  * Constructor
  *
  * @param string $path Path to file
  * @param bool $create Create file if it does not exist (if true)
  * @param int $mode Mode to apply to the folder holding the file
  */
 public function __construct($path, $create = false, $mode = 0755)
 {
     parent::__construct($path, $create, $mode);
     if (!static::$_ffprobe) {
         static::$_ffprobe = new FFProbe();
     }
     $this->_driver = new FFMpegDriver();
 }
Example #5
0
 /**
  * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
  *
  * The file object is only created when the uploaded file is valid (i.e. when the
  * isValid() method returns true). Otherwise the only methods that could be called
  * on an UploadedFile instance are:
  *
  *   * getClientOriginalName,
  *   * getClientMimeType,
  *   * isValid,
  *   * getError.
  *
  * Calling any other method on an non-valid instance will cause an unpredictable result.
  *
  * @param string $path         The full temporary path to the file
  * @param string $originalName The original file name
  * @param string $mimeType     The type of the file as provided by PHP
  * @param int    $size         The file size
  * @param int    $error        The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants)
  * @param bool   $test         Whether the test mode is active
  */
 public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
 {
     $this->_originalName = $originalName;
     $this->_mimeType = $mimeType ?: 'application/octet-stream';
     $this->_size = $size;
     $this->_error = $error ?: UPLOAD_ERR_OK;
     $this->_test = (bool) $test;
     parent::__construct($path);
 }
Example #6
0
 /**
  * Setup for display or download the given file.
  *
  * If $_SERVER['HTTP_RANGE'] is set a slice of the file will be
  * returned instead of the entire file.
  *
  * ### Options keys
  *
  * - name: Alternate download name
  * - download: If `true` sets download header and forces file to be downloaded rather than displayed in browser
  *
  * @param string $path Path to file.
  * @param array $options Options See above.
  * @return void
  * @throws NotFoundException
  */
 public function file($path, array $options = [])
 {
     $options += ['name' => null, 'download' => null];
     if (strpos($path, '../') !== false || strpos($path, '..\\') !== false) {
         throw new NotFoundException('The requested file contains `..` and will not be read.');
     }
     $file = new File($path);
     if (!$file->exists() || !$file->readable()) {
         if (Configure::read('debug')) {
             throw new NotFoundException(sprintf('The requested file %s was not found or not readable', $path));
         }
         throw new NotFoundException('The requested file was not found');
     }
     $extension = strtolower($file->extension());
     $download = $options['download'];
     if ((!$extension || $this->contentType($extension) === false) && $download === null) {
         $download = true;
     }
     $fileSize = $file->size();
     if ($download) {
         if ($options['name'] === null) {
             $name = $file->name;
         } else {
             $name = $options['name'];
         }
         $this->header('Content-Disposition', 'attachment; filename="' . $name . '"');
         $this->header('Content-Transfer-Encoding', 'binary');
     }
     $this->header('Accept-Ranges', 'bytes');
     $this->header('Content-Length', $fileSize);
     ob_get_clean();
     $this->_file = $file;
 }
Example #7
0
 /**
  * testPermissions method
  *
  * @return void
  */
 public function testPermissions()
 {
     $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
     $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'permissions' . DIRECTORY_SEPARATOR;
     $old = umask();
     umask(02);
     $filename = $dir . 'permission_' . uniqid();
     $expecting = decoct(0664 & ~umask());
     $file = new File($filename, true);
     $fileInfo = new FileInfo($file);
     $result = $fileInfo->permissions();
     $this->assertEquals($expecting, $result);
     $file->delete();
     umask(022);
     $filename = $dir . 'permission_' . uniqid();
     $expecting = decoct(0644 & ~umask());
     $file = new File($filename, true);
     $fileInfo = new FileInfo($file);
     $result = $fileInfo->permissions();
     $this->assertEquals($expecting, $result);
     $file->delete();
     umask(0422);
     $filename = $dir . 'permission_' . uniqid();
     $expecting = decoct(0244 & ~umask());
     $file = new File($filename, true);
     $fileInfo = new FileInfo($file);
     $result = $fileInfo->permissions();
     $this->assertEquals($expecting, $result);
     $file->delete();
     umask(0444);
     $filename = $dir . 'permission_' . uniqid();
     $expecting = decoct(0222 & ~umask());
     $file = new File($filename, true);
     $fileInfo = new FileInfo($file);
     $result = $fileInfo->permissions();
     $this->assertEquals($expecting, $result);
     $file->delete();
     umask($old);
 }
Example #8
0
 /**
  * testReplaceText method
  *
  * @return void
  */
 public function testReplaceText()
 {
     $TestFile = new File(TEST_ROOT . '/Fixture/FileTest.txt');
     $TmpFile = new File(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'cakephp.file.test.tmp', true);
     // Copy the test file to the temporary location
     $TestFile->copy($TmpFile->getPath(), true);
     // Replace the contents of the temporary file
     $result = $TmpFile->replaceText('welcome.php', 'welcome.tmp');
     $this->assertTrue($result);
     // Double check
     $expected = 'This is the welcome.tmp file in vendors directory';
     $contents = $TmpFile->read();
     $this->assertContains($expected, $contents);
     $search = ['This is the', 'welcome.php file', 'in tmp directory'];
     $replace = ['This should be a', 'welcome.tmp file', 'in the Lib directory'];
     // Replace the contents of the temporary file
     $result = $TmpFile->replaceText($search, $replace);
     $this->assertTrue($result);
     // Double check
     $expected = 'This should be a welcome.tmp file in vendors directory';
     $contents = $TmpFile->read();
     $this->assertContains($expected, $contents);
     $TmpFile->delete();
 }
Example #9
0
 /**
  * testChmod method
  *
  * @return void
  */
 public function testChmod()
 {
     $this->skipIf(DS === '\\', 'Folder permissions tests not supported on Windows.');
     $path = TMP . 'tests/';
     $Folder = new Folder($path);
     $subdir = 'test_folder_new';
     $new = $path . $subdir;
     $this->assertTrue($Folder->create($new));
     $this->assertTrue($Folder->create($new . DS . 'test1'));
     $this->assertTrue($Folder->create($new . DS . 'test2'));
     $filePath = $new . DS . 'test1.php';
     $File = new File($filePath);
     $this->assertTrue($File->create());
     $filePath = $new . DS . 'skip_me.php';
     $File = new File($filePath);
     $this->assertTrue($File->create());
     $this->assertTrue($Folder->chmod($new, 0755, true));
     $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
     $this->assertEquals('0755', $perms);
     $this->assertTrue($Folder->chmod($new, 0744, true, ['skip_me.php', 'test2']));
     $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
     $this->assertEquals('0755', $perms);
     $perms = substr(sprintf('%o', fileperms($new . DS . 'test1')), -4);
     $this->assertEquals('0744', $perms);
 }