<?php if (!defined('TEMPLATE_CMS_ACCESS')) exit('No direct script access allowed'); // нафик? у тебя всё равно весь код в функциях и ничегно не выполнится
/**
* Filesystem module
* @package TemplateCMS
* @subpackage Engine
* @author Romanenko Sergey / Awilum
* @copyright 2010 Romanenko Sergey / Awilum
* @version SVN: $Id$
* @since 2.0
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* TemplateCMS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYING.txt for copyright notices and details.
* @filesource
*/
/**
* Get list of files in directory
* @param string $dir directory to scan
* @param string $type file type
* @return array $files
*/
function listFiles($dir, $type=NULL) { // есть такая клёва штука как неймспэйсы, чтобы имена не пересекались
// например, templatecms_filesystem_listFile()
$files = array();
if(is_dir($dir)) {
$dir = opendir ($dir);
while (false !== ($file = readdir($dir))) {
if(($file !=".") && ($file !="..")) {
if(isset($type)) {
if (strpos($file, $type,1)) {
$files[] = $file;
}
} else {
$files[] = $file;
}
}
}
closedir($dir);
return $files;
} else {
echo '<span style="color:#9C4F4F;">Error: Wrong directory ( '.$dir.' )</span>'; // здесь должна добавляться ошибка в какой-нить стек, а не делаться сразу вывод пользователю
}
}
/**
* Create new file
* @param string $filename filename
* @param string $content content to save
* @param string $old_filename old filename turn on rename mod
* @todo test it!
*/
function createFile($filename, $content, $old_filename=NULL) {
$space = '-';
if(!empty($old_filename)) {
$new_cl_space = str_replace(" ", $space, $filename);
$new = translitIt($new_cl_space);
if($old_filename !== $new) {
rename($old_filename,$new);
$save_filename = $filename;
} else {
$save_filename = $filename;
}
} else {
$save_filename = $filename;
}
$save_cl_space = str_replace(" ", $space, $save_filename);
$save = translitIt($save_cl_space);
// file_put_contents ???
$handle = fopen ($save,"a");
flock ($handle,LOCK_EX);
ftruncate ($handle,0);
fputs($handle,$content);
fflush ($handle);
flock ($handle,LOCK_UN);
fclose ($handle);
}
/**
* Delete current file
* @param string $filename filename
*/
function deleteFile($filename) {
if(file_exists($filename)) {
unlink($filename);
}
}
/**
* Rename file
* @param string $from Original file location
* @param string $to Desitination location of the file
*/
function renameFile($from, $to) {
if (!file_exists($to)) {
return rename($from, $to);
}
}
function loadFile($file) {
if(file_exists($file)) {
return file_get_contents($file);
}
}
/**
* Updates the file modified time to the current time
* @param string $file filename
* @return boolean
*/
function touchFile($file) {
return touch($file);
}
/**
* Determines if a file is writable
* @param string $file filename
* @return boolean
*/
function writable($file) { // гениально, здесь наверняка глубокий смысл
return is_writable($file);
}
/**
* Determines if a file is readable
* @param string $file filename
* @return boolean
*/
function readable($file) {
return is_readable($file);
}
/**
* Copy file
* @param string $from Original file location
* @param string $to Desitination location of the file
* @todo test it!
*/
function copyFile($from, $to) {
if (file_exists($from) && !file_exists($to)) {
copy($from, $to);
}
}
/**
* Creates a directory
* @param string $dir name of directory to create
*/
function createDir($dir) {
if (!is_dir($dir)) {
mkdir($dir, 0775);
}
}
/**
* Delete directory
* @param string $dir name of directory to delete
* @todo test IT!
*/
function deleteDir($dir) {
if (is_dir($dir)) $dir_handle = opendir($dir);
if ($dir_handle) {
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dir."/".$file)) {
unlink($dir."/".$file);
} else {
delete_directory($dir.'/'.$file);
}
}
}
closedir($dir_handle);
rmdir($dir);
}
}
/**
* Check dir permission
* @param string $dir directory to check
* @return string
*/
function checkDirPerm($dir) {
clearstatcache();
return substr(sprintf('%o', fileperms($dir)), -4);
}
/**
* Extension enables to transparently read or write
* ZIP compressed archives and the files inside them.
*/
$zip = new ZipArchive(); // а вот и всё дерьмо откуда растёт, можно было обойтись templatecms_filesystem_getZip()
// а не гадить в глобальную область видимости
// и не понятно, зачем завязываться на расширение.
/**
* Zip folders
* @global object $zip
* @param string $folder
* @param string $zip_name
*/
function zipFolders($folder, $zip_name=NULL) {
global $zip;
$files = array();
$interator = new RecursiveDirectoryIterator($folder);
foreach(new RecursiveIteratorIterator($interator) as $file) {
$files[] = $file;
}
if(!empty($zip_name)) {
$folder = $zip_name;
}
if ($zip->open($folder.'.zip', ZipArchive::CREATE) === true) {
foreach($files as $file) {
$zip->addFile($file, $file);
}
$zip->close();
}
}
/**
* Zip file
* @global object $zip
* @param string $file
* @param string $zip_name
*/
function zipFile($file, $zip_name=NULL) {
global $zip;
if(!empty($zip_name)) {
$file = $zip_name;
}
if ($zip->open($file.'.zip', ZipArchive::CREATE) === true) {
$zip->addFile($file, $file);
$zip->close();
}
}
/**
* Unpack zip
* @global object $zip
* @param string $file
*/
function unZip($file) {
global $zip;
if($zip->open($file) === true) {
$zip->extractTo(basename($file,'.zip'));
$zip->close();
}
}
?>