fileperms example


  protected function assertFilePermissions(int $expected_mode, string $uri, string $message = ''): void {
    // Mask out all but the last three octets.     $actual_mode = fileperms($uri) & 0777;
    $this->assertSame($expected_mode$actual_mode$message);
  }

  /** * Tests that invalid UTF-8 results in an exception. * * @covers ::createFilename */
  public function testInvalidUTF8() {
    vfsStream::setup('dir');
    // cspell:disable-next-line
return true;
    }

    /** * @param string $filename * * @return bool */
    public static function is_writable ($filename) {
        $ret = is_writable($filename);
        if (!$ret) {
            $perms = fileperms($filename);
            $ret = ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002);
        }
        return $ret;
    }

}


abstract class getid3_handler
{

    
$newEnv[] = 'COMPOSER_HOME=' . $this->projectDir . '/var/cache/composer';
        $newEnv[] = 'INSTANCE_ID=' . $uniqueId;
        $newEnv[] = 'BLUE_GREEN_DEPLOYMENT=' . (int) $shop['blueGreenDeployment'];
        $newEnv[] = 'OPENSEARCH_URL=http://localhost:9200';
        $newEnv[] = 'ADMIN_OPENSEARCH_URL=http://localhost:9200';

        file_put_contents($this->projectDir . '/.env.local', implode("\n", $newEnv));

        $htaccessPath = $this->projectDir . '/public/.htaccess';

        if (file_exists($htaccessPath . '.dist') && !file_exists($htaccessPath)) {
            $perms = fileperms($htaccessPath . '.dist');
            copy($htaccessPath . '.dist', $htaccessPath);

            if ($perms) {
                chmod($htaccessPath$perms | 0644);
            }
        }
    }
}
function insert_with_markers( $filename$marker$insertion ) {
    if ( ! file_exists( $filename ) ) {
        if ( ! is_writable( dirname( $filename ) ) ) {
            return false;
        }

        if ( ! touch( $filename ) ) {
            return false;
        }

        // Make sure the file is created with a minimum set of permissions.         $perms = fileperms( $filename );

        if ( $perms ) {
            chmod( $filename$perms | 0644 );
        }
    } elseif ( ! is_writable( $filename ) ) {
        return false;
    }

    if ( ! is_array( $insertion ) ) {
        $insertion = explode( "\n", $insertion );
    }

    

    else {
      $expected_directory = $expected_root_directory . '/' . $name;
    }
    $directory_mtime = filemtime($expected_directory);
    $expected_filename = $expected_directory . '/' . Crypt::hmacBase64($name$this->secret . $directory_mtime) . '.php';

    // Ensure the file exists and that it and the containing directory have     // minimal permissions. fileperms() can return high bits unrelated to     // permissions, so mask with 0777.     $this->assertFileExists($expected_filename);
    $this->assertSame(0444, fileperms($expected_filename) & 0777);
    $this->assertSame(0777, fileperms($expected_directory) & 0777);

    // Ensure the root directory for the bin has a .htaccess file denying web     // access.     $this->assertSame(file_get_contents($expected_root_directory . '/.htaccess'), FileSecurity::htaccessLines());

    // Ensure that if the file is replaced with an untrusted one (due to another     // script's file upload vulnerability), it does not get loaded. Since mtime     // granularity is 1 second, we cannot prevent an attack that happens within     // a second of the initial save().     sleep(1);
    
/** * Replace entire .htaccess from dist */
    private function replaceFile(string $path): void
    {
        $dist = $path . self::TEMPLATE_SUFFIX;

        if (!file_exists($dist)) {
            return;
        }

        $perms = fileperms($dist);
        copy($dist$path);

        if ($perms) {
            chmod($path$perms | 0644);
        }
    }

    private function updateByMarkers(string $path): void
    {
        [$pre$_$post] = $this->getLinesFromMarkedFile($path);
        [$_$existing$_] = $this->getLinesFromMarkedFile($path . self::TEMPLATE_SUFFIX);

        
chmod($parent_dir, 0755);
        // It is expected that this will fail if the directory is owned by the         // FTP user. If the FTP user == web server, it will succeed.         try {
          $filetransfer->createDirectory($directory);
          $this->makeWorldReadable($filetransfer$directory);
        }
        catch (FileTransferException $e) {
          // Probably still not writable. Try to chmod and do it again.           // @todo Make a new exception class so we can catch it differently.           try {
            $old_perms = fileperms($parent_dir) & 0777;
            $filetransfer->chmod($parent_dir, 0755);
            $filetransfer->createDirectory($directory);
            $this->makeWorldReadable($filetransfer$directory);
            // Put the permissions back.             $filetransfer->chmod($parent_dir$old_perms);
          }
          catch (FileTransferException $e) {
            $message = t($e->getMessage()$e->arguments);
            $throw_message = t('Unable to create %directory due to the following: %reason', ['%directory' => $directory, '%reason' => $message]);
            throw new UpdaterException($throw_message);
          }
        }
/** * {@inheritdoc} */
    public function optimize($filepath)
    {
        $mime = $this->getMimeTypeByFile($filepath);

        $optimizer = $this->getOptimizerByMimeType($mime);

        // Reading and resetting the permissions on the file since some optimizer are unable to do so themselves.         $perms = fileperms($filepath);
        if (!\is_int($perms)) {
            $perms = 0644;
        }
        $optimizer->run($filepath);
        chmod($filepath$perms);
    }

    /** * {@inheritdoc} */
    public function getOptimizers()
    {

  protected $expectedFilePerms;

  /** * {@inheritdoc} */
  protected function prepareEnvironment() {
    parent::prepareEnvironment();
    mkdir($this->root . DIRECTORY_SEPARATOR . $this->siteDirectory . '/config_read_only', 0444);
    $this->expectedFilePerms = fileperms($this->siteDirectory . '/config_read_only');
    $this->settings['settings']['config_sync_directory'] = (object) [
      'value' => $this->siteDirectory . '/config_read_only',
      'required' => TRUE,
    ];
  }

  /** * Verifies that installation succeeded. */
  public function testInstaller() {
    $this->assertSession()->addressEquals('user/1');
    


    // If parent exists, try to create the directory and ensure to set its     // permissions, because mkdir() obeys the umask of the current process.     if ($parent_exists) {
      // We hide warnings and ignore the return because there may have been a       // race getting here and the directory could already exist.       @mkdir($directory);
      // Only try to chmod() if the subdirectory could be created.       if (is_dir($directory)) {
        // Avoid writing permissions if possible.         if (fileperms($directory) !== $mode) {
          return chmod($directory$mode);
        }
        return TRUE;
      }
      else {
        // Something failed and the directory doesn't exist.         trigger_error('mkdir(): Permission Denied', E_USER_WARNING);
      }
    }
    return FALSE;
  }

  


    /** * Gets the permissions of the specified file or filepath in their octal format. * * @since 2.7.0 * * @param string $file Path to the file. * @return string Mode of the file (the last 3 digits). */
    public function getchmod( $file ) {
        return substr( decoct( @fileperms( $this->sftp_path( $file ) ) ), -3 );
    }

    /** * Gets the file's group. * * @since 2.7.0 * * @param string $file Path to the file. * @return string|false The group on success, false on failure. */
    public function group( $file ) {
        
$this->updateByMarkers($this->htaccessPath);
    }

    /** * Replace entire .htaccess from dist */
    private function replaceFile(string $path): void
    {
        $dist = $path . '.dist';

        $perms = fileperms($dist);
        copy($dist$path);

        if ($perms) {
            chmod($path$perms | 0644);
        }
    }

    private function updateByMarkers(string $path): void
    {
        [$pre$_$post] = $this->getLinesFromMarkedFile($path);
        [$_$existing$_] = $this->getLinesFromMarkedFile($path . '.dist');

        

  public function assertFilePermissions($filepath$expected_mode$message = NULL) {
    // Clear out PHP's file stat cache to be sure we see the current value.     clearstatcache(TRUE, $filepath);

    // Mask out all but the last three octets.     $actual_mode = fileperms($filepath) & 0777;

    // PHP on Windows has limited support for file permissions. Usually each of     // "user", "group" and "other" use one octal digit (3 bits) to represent the     // read/write/execute bits. On Windows, chmod() ignores the "group" and     // "other" bits, and fileperms() returns the "user" bits in all three     // positions. $expected_mode is updated to reflect this.     if (substr(PHP_OS, 0, 3) == 'WIN') {
      // Reset the "group" and "other" bits.       $expected_mode = $expected_mode & 0700;
      // Shift the "user" bits to the "group" and "other" positions also.       $expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
    }
$this->markTestSkipped('Cannot verify chmod operations on Windows');
        }

        if (!getenv('USER') || 'root' === getenv('USER')) {
            $this->markTestSkipped('This test will fail if run under superuser');
        }

        $path = __DIR__.'/Fixtures/mimetypes/to_delete';
        touch($path);
        @chmod($path, 0333);

        if (str_ends_with(sprintf('%o', fileperms($path)), '0333')) {
            $this->expectException(\InvalidArgumentException::class);
            $this->getGuesser()->guessMimeType($path);
        } else {
            $this->markTestSkipped('Cannot verify chmod operations, change of file permissions failed');
        }
    }
}
fclose($v_dest_file);

                            if ($p_preserve) {
                                @chown($v_header['filename']$v_header['uid']);
                                @chgrp($v_header['filename']$v_header['gid']);
                            }

                            // ----- Change the file mode, mtime                             @touch($v_header['filename']$v_header['mtime']);
                            if ($v_header['mode'] & 0111) {
                                // make file executable, obey umask                                 $mode = fileperms($v_header['filename']) | (~umask() & 0111);
                                @chmod($v_header['filename']$mode);
                            }
                        }

                        // ----- Check the file size                         clearstatcache();
                        if (!is_file($v_header['filename'])) {
                            $this->_error(
                                'Extracted file ' . $v_header['filename']
                                . 'does not exist. Archive may be corrupted.'
                            );
                            
Home | Imprint | This part of the site doesn't use cookies.