Checks if a file is both readable and writable.

@param string 	$filename Name of a file
@param boolean 	&$error Error flag
@param string 	&$error_str Error description
@return int	1 - is readable and writable; 0 - is not
485816
static function IsReadableAndWritable($filename,&$error = null,&$error_str = null){
		$error = false;
		$error_str = "";

		settype($filename,"string");

		if(!file_exists($filename)){
			$error = true;
			$error_str = "file does't exist";
			return 0;
		}

		$_UID_ = posix_getuid();
		$_FILE_OWNER = fileowner($filename);
		$_FILE_PERMS = fileperms($filename);
		if(!(
			(($_FILE_OWNER!=$_UID_) && (((int)$_FILE_PERMS&(int)bindec("110")))==(int)bindec("110")) ||
			(($_FILE_OWNER==$_UID_) && (((int)$_FILE_PERMS&(int)bindec("110000000"))==(int)bindec("110000000")))
		)){
			return 0;
		}
		return 1;
	}
104422
protected function validateFilePath($fileName) {
		if (!file_exists($fileName)) {
			throw new IOException("File $fileName does not exists");
		}
		if (!is_file($fileName)) {
			throw new IOException("$fileName must be a file");
		}
		if (!is_readable($fileName)) {
			throw new IOException("File $fileName is not readable");
		}
		return true;
	}
285968
protected function checkFile($writable = true)
    {
        if(!$this->fileExists()) throw NonExistentFileException::createForFileDoesNotExists($this->getFilePath());
        if(!is_readable($this->file_path)) throw NotReadableFileException::createForNotReadable($this->getFilePath());
        if($writable)
        {
            if(!is_writable($this->file_path)) throw NotWritableFileException::createForNotWritable($this->getFilePath());
        }
    }
733296
public static function valid($filename = null)
	{
		if (!$filename)
		{
			return false;
		}

		if (preg_match("/^\s*http[s]{0,1}:/i", $filename))
		{
			return false;
		}
		if (preg_match("/^\s*[\/]{0,1}index.php\?/i", $filename))
		{
			return false;
		}
		// Disallow windows drive letter
		if (preg_match("/^\s*[.]:/", $filename))
		{
			return false;
		}
		// Disallow \
		if (strpos($filename, '\\'))
		{
			return false;
		}
		// Disallow ..
		if (strpos($filename, '..'))
		{
			return false;
		}

		return true;
	}
1200920
public static function _uploadError ($error)
	{
		
		// Return an error dependent on the error number
		
		switch ($error)
		{
			
			case UPLOAD_ERR_INI_SIZE:
				
				return 'The filesize cannot be more than ' . ini_get ('post_max_size');
				break;
				
			case UPLOAD_ERR_FORM_SIZE:
				
				return 'The filesize cannot be more than ' . $_POST['MAX_FILE_SIZE'];
				break;
				
			case UPLOAD_ERR_PARTIAL:
				
				return 'The file was only partially uploaded. Please try again.';
				break;
				
			case UPLOAD_ERR_NO_FILE:
				
				return 'No file was selected to upload!';
				break;
				
			case UPLOAD_ERR_NO_TMP_DIR:
				
				return 'There is no temporary folder to upload the file to. Please inform the site administrator.';
				break;
				
			case UPLOAD_ERR_CANT_WRITE:
				
				return 'Failed to write the file to disk.';
				break;
				
			default:
				
				return 'Unknown error.';
				break;
				
		}
		
	}
448001
function _read_file($filename)
	{
		if ( file_exists($filename) && ($fd = @fopen($filename, 'rb')) ) {
			$contents = '';
			while (!feof($fd)) {
				$contents .= fread($fd, 8192);
			}
			fclose($fd);
			return $contents;
		} else {
			return false;
		}
	}
686133
static function GetFileContent($filename,&$error = null,&$error_str = null){
		$error = false;
		$error_str = "";

		$out = "";
		
		settype($filename,"string");

		if(!is_file($filename)){
			$error = true;
			$error_str = "$filename is not a file";
			return null;		
		}

		$filesize = filesize($filename);
		if($filesize==0){ return ""; }

		$f = fopen($filename,"r");
		if(!$f){
			$error = false;
			$error_str = "can't open file $filename for writing";
			return $out;
		}
		$out = fread($f,$filesize);
		fclose($f);

		return $out;
	}
506673
public static function canOpenArchive($fileName)
    {
        self::checkRequirements();

        $type = self::detectArchiveType($fileName);
        if ($type !== false && self::canOpenType($type)) {
            return true;
        }

        return false;
    }
1301552
private function _log_to_file($error_text)
    {
        try {
            $dir_name = 'logs';
            if (!is_dir($dir_name)) {
                mkdir($dir_name);
            }
            $fileName = $dir_name.'/'.__CLASS__.'-'.date('Y-m-d').'.txt';
            $myFile = fopen($fileName, 'a+');
            $date = '============[Date]============';
            $date .= "\n";
            $date .= '[ '.date('Y-m-d H:i:s  e').' ] ';
            fwrite($myFile, $date.$error_text."\n\n");
            fclose($myFile);
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }
1052649
public function canWrite()
    {
        $ht = $this->getHTaccessFilename();
        $fs = $this->getFilesystem();

        return (bool) (@$fs->isFile($ht) ? @$fs->isWritable($ht) : @$fs->isWritable(@dirname($ht)));
    }
91010
public static function set_writable($filename, $writable = true)
    {
        $stat = @stat($filename);

        if ($stat === false) {
            return false;
        }

        // We're on Windows
        if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
            return true;
        }

        list($myuid, $mygid) = array(posix_geteuid(), posix_getgid());

        if ($writable) {
            // Set only the user writable bit (file is owned by us)
            if ($stat['uid'] == $myuid) {
                return chmod($filename, fileperms($filename) | 0200);
            }

            // Set only the group writable bit (file group is the same as us)
            if ($stat['gid'] == $mygid) {
                return chmod($filename, fileperms($filename) | 0220);
            }

            // Set the world writable bit (file isn't owned or grouped by us)
            return chmod($filename, fileperms($filename) | 0222);
        } else {
            // Set only the user writable bit (file is owned by us)
            if ($stat['uid'] == $myuid) {
                return chmod($filename, (fileperms($filename) | 0222) ^ 0222);
            }

            // Set only the group writable bit (file group is the same as us)
            if ($stat['gid'] == $mygid) {
                return chmod($filename, (fileperms($filename) | 0222) ^ 0022);
            }

            // Set the world writable bit (file isn't owned or grouped by us)
            return chmod($filename, (fileperms($filename) | 0222) ^ 0002);
        }
    }
1109283
public function output(string $filename = null): int
    {
        if (null !== $filename) {
            $this->sendHeaders($filename);
        }
        $input_bom = $this->getInputBOM();
        $this->document->rewind();
        $this->document->fseek(strlen($input_bom));
        echo $this->output_bom;

        return strlen($this->output_bom) + $this->document->fpassthru();
    }
995923
protected function checkValidAttachment($filename, &$error) {
    if (!is_string($filename)) {
      $error = sprintf("Filename must be a string but was type %s",
                       gettype($filename));
      return false;
    }

    $path_parts = pathinfo($filename);
    if (isset($path_parts['extension'])) {
      if (in_array($path_parts['extension'], self::$extension_blacklist)) {
        $error = sprintf("'%s' is a blacklisted file extension.",
                         $path_parts['extension']);
        return false;
      }
    }

    return true;
  }
1335616
protected function isValidFilename($filename)
    {
        $extension = strtolower(File::get_file_extension($filename));

        // Validate true if within the list of allowed extensions
        $allowed = $this->getAllowedExtensions();
        if ($allowed) {
            return in_array($extension, $allowed);
        }

        // If no extensions are configured, fallback to global list
        $globalList = File::getAllowedExtensions();
        if (in_array($extension, $globalList)) {
            return true;
        }

        // Only admins can bypass global rules
        return !File::config()->apply_restrictions_to_admin && Permission::check('ADMIN');
    }
201851
static function is_writable($name)
    {
        if (file_exists(DIR_CONFIG.$name.'.php')){
            return is_writable(DIR_CONFIG.$name.'.php');
        }
        return is_writable(DIR_CONFIG);
    }
71671
protected function checkFileName($fileName)
    {
        $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

        $allow = $this->getWhitelistedExtensions();
        $block = $this->getBlacklistedExtensions();

        return  (   (false === $allow || in_array($extension, $allow))
                &&  (false === $block || ! in_array($extension, $block))
                );
    }
870206
function verifyFileName (fileName)
{
    // if fileName undefined or null, throw exception
    if ((fileName === undefined) || (fileName === null)) {
        var message1 = format (cc.MSG_INVALID_NAME);
        throw new SafeFileError (cc.INVALID_NAME, message1);
    }
}