64 lines
2.1 KiB
PHP
64 lines
2.1 KiB
PHP
<?php
|
|
// File name: chkwrite.inc.php
|
|
// Description: PHP subroutine to check the permission to write to a file
|
|
// Date: 2002-04-16
|
|
// Author: imacat <imacat@pristine.com.tw>
|
|
// Copyright: Copyright (C) 2002-2007 Pristine Communications
|
|
|
|
// Set the include path
|
|
if (!defined("INCPATH_SET")) {
|
|
require_once dirname(__FILE__) . "/incpath.inc.php";
|
|
}
|
|
// Referenced subroutines
|
|
require_once "monica/gettext.inc.php";
|
|
require_once "monica/rel2abs.inc.php";
|
|
|
|
// check_writable: Check the permission to write to a file.
|
|
// Input: File full pathname $file.
|
|
// Output: true if success, false if failed.
|
|
function check_writable($file)
|
|
{
|
|
// Standardize it
|
|
$file = stdpath($file);
|
|
// File exists.
|
|
if (file_exists($file)) {
|
|
// If it is a file
|
|
if (!is_file($file)) {
|
|
return array("msg"=>NC_("%s: It is not a file."),
|
|
"margs"=>array($file));
|
|
}
|
|
// If it is writable
|
|
if (!is_writable($file)) {
|
|
return array("msg"=>NC_("%s: You have no permission to overwrite this file."),
|
|
"margs"=>array($file));
|
|
}
|
|
|
|
// Not an existing file. See if we can create it.
|
|
} else {
|
|
$parent = $file;
|
|
// Find the nearest existing parent
|
|
while ($parent != "" && !file_exists($parent)) {
|
|
$parent = dirname($parent);
|
|
}
|
|
// Creat files from root --- You are insane
|
|
if ($parent == "") {
|
|
return array("msg"=>NC_("%s: You cannot create anything under the root directory."),
|
|
"margs"=>array($file));
|
|
}
|
|
// This parent is not a directory
|
|
if (!is_dir($parent)) {
|
|
return array("msg"=>NC_("%s: One of the parents of this file (%s) is not a directory. You cannot create any new file inside."),
|
|
"margs"=>array($file, $parent));
|
|
}
|
|
// If it is possible to create entries in this directory
|
|
if (!is_writable($parent)) {
|
|
return array("msg"=>NC_("%s: You have no permission to create any file under %s."),
|
|
"margs"=>array($file, $parent));
|
|
}
|
|
}
|
|
// OK
|
|
return null;
|
|
}
|
|
|
|
?>
|