Initial commit.
This commit is contained in:
134
lib/php/monica/rmofile.inc.php
Normal file
134
lib/php/monica/rmofile.inc.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
// File name: rmofile.inc.php
|
||||
// Description: PHP subroutine to remove the old page before creating a new one
|
||||
// Date: 2003-03-28
|
||||
// Author: imacat <imacat@pristine.com.tw>
|
||||
// Copyright: Copyright (C) 2003-2007 Pristine Communications
|
||||
|
||||
// Set the include path
|
||||
if (!defined("INCPATH_SET")) {
|
||||
require_once dirname(__FILE__) . "/incpath.inc.php";
|
||||
}
|
||||
// Referenced subroutines
|
||||
require_once "monica/lninfo.inc.php";
|
||||
require_once "monica/rel2abs.inc.php";
|
||||
require_once "monica/requri.inc.php";
|
||||
require_once "monica/xfileio.inc.php";
|
||||
|
||||
// rmoldpage: Remove the old page before creating a new one
|
||||
// Input: The old page path $oldpage to be removed, and an optional new page
|
||||
// path $newpage to be compared with. It won't check at all.
|
||||
// Path is without DOC_ROOT
|
||||
function rmoldpage($oldpage, $newpage = null)
|
||||
{
|
||||
// Get the available languages list
|
||||
global $ALL_LINGUAS;
|
||||
// Standardize it
|
||||
$oldpage = stdpath($oldpage);
|
||||
if (substr($oldpage, -1) == "/") {
|
||||
$oldpage .= "index.html";
|
||||
}
|
||||
// The new file is supplied to be compared with
|
||||
if (!is_null($newpage)) {
|
||||
// Standardize it
|
||||
$newpage = stdpath($newpage);
|
||||
if (substr($newpage, -1) == "/") {
|
||||
$newpage .= "index.html";
|
||||
}
|
||||
// Return if unchanged
|
||||
if ($oldpage == $newpage) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Unilingual
|
||||
if (count($ALL_LINGUAS) == 1) {
|
||||
$oldfile = DOC_ROOT . $oldpage;
|
||||
if (!is_null($newpage)) {
|
||||
$newfile = DOC_ROOT . $newpage;
|
||||
rmoldfile($oldfile, $newfile);
|
||||
} else {
|
||||
rmoldfile($oldfile);
|
||||
}
|
||||
|
||||
// Multilingual
|
||||
} else {
|
||||
for ($l = 0; $l < count($ALL_LINGUAS); $l++) {
|
||||
$langfile = ln($ALL_LINGUAS[$l], LN_FILENAME);
|
||||
$oldfile = DOC_ROOT . "/$langfile$oldpage";
|
||||
if (!is_null($newpage)) {
|
||||
$newfile = DOC_ROOT . "/$langfile$newpage";
|
||||
rmoldfile($oldfile, $newfile);
|
||||
} else {
|
||||
rmoldfile($oldfile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// rmoldfile: the real function
|
||||
function rmoldfile($oldfile, $newfile = null)
|
||||
{
|
||||
// Obtain the parent directory
|
||||
$parent = dirname($oldfile);
|
||||
// Return if its parent is not a directory
|
||||
if (!is_dir($parent)) {
|
||||
return;
|
||||
}
|
||||
// Remove the file and its variants
|
||||
$files = preg_match("/\.html$/", $oldfile)?
|
||||
array($oldfile, "$oldfile.html", "$oldfile.xhtml"): array($oldfile);
|
||||
foreach ($files as $file) {
|
||||
if (!is_file($file) && !is_link($file)) {
|
||||
continue;
|
||||
}
|
||||
// We can delete this file
|
||||
if (is_writable($parent)) {
|
||||
unlink($file);
|
||||
// We cannot delete the file -- Empty it if possible
|
||||
} else {
|
||||
if (is_file($file) && is_writable($file)) {
|
||||
xfwrite($file, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the parents as much as possible and necessary
|
||||
while ($parent != "/") {
|
||||
$dir = $parent;
|
||||
// Coincident with the new path ends
|
||||
if (!is_null($newfile) && substr($newfile, 0, strlen($dir) + 1) == $dir . "/") {
|
||||
return;
|
||||
}
|
||||
// Obtain the parent directory
|
||||
$parent = dirname($dir);
|
||||
// Skip to the next parent if directory not exists
|
||||
if (!file_exists($dir)) {
|
||||
continue;
|
||||
}
|
||||
// We cannot remove, or the directory is not empty
|
||||
if (!is_writable($parent) || !is_dir($dir) || _rmofile_dir_not_empty($dir)) {
|
||||
return;
|
||||
}
|
||||
// Remove this directory
|
||||
rmdir($dir);
|
||||
}
|
||||
}
|
||||
|
||||
// _rmofile_dir_not_empty: Check if a directory is empty
|
||||
function _rmofile_dir_not_empty($dir)
|
||||
{
|
||||
$dh = opendir($dir);
|
||||
while (($ent = readdir($dh)) !== false) {
|
||||
// A real entry is found
|
||||
if ($ent != "." && $ent != "..") {
|
||||
closedir($dh);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// No real entry was found
|
||||
closedir($dh);
|
||||
return false;
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user