Zdravím zase po delší době,
dávám dohromady třídu, která by mi měla zjednodušit práci s adresářem o určité struktuře. Formát je následující:
directory_name/
directory_name/attachment/
directory_name/cw_codename
directory_name/cw_description
directory_name/cw_tags
Všechno to nějak lepím dohromady, ale uvědomuji si, že za sebou zanechávám spoustu logických chyb a dalších nedodělků. Na konci tohoto postu je zdrojový kód toho co mám v tuto chvíli vytvořené. Nemám žádný konkrétní problém*, spíš bych od vás potřeboval menší kontrolu a úplně nejlépe patche... Pokud to někoho zajímá nebo třeba řeší stejný problém. Beztak na roumingu neni nic nového...
* Ok, kecám... code_get_attachment (); a _read_recursive (); se chovají dementně.
<?php
class CWDataInterface
{
private $working_dir;
private $dd; // directory descriptor for controlled reading
private $read_limit;
private $limits = array
(
"codename_maxlen" => 255,
"codedesc_maxlen" => 1000000,
"codetags_maxlen" => 1000000
);
public function __construct ($working_dir = "")
{
$this->working_dir = $working_dir;
$this->dd = null;
}
public function __destruct ()
{
if ( $this->dd != null )
closedir ($this->dd);
}
private function _clean_dirname ($dirname)
{
if ( preg_match ("/\/$/", $dirname) == 0 )
$dirname .= "/";
if ( !$this->_check_real_path ($dirname) )
return null;
return $dirname;
}
private function _check_real_path ($dirname)
{
if ( ! realpath ($this->working_dir.$dirname) )
return false;
return true;
}
public function code_create ($dirname, $codename, $code_description = "", $code_tags = "", $user_token = "", $attachment = array ())
{
if ( is_dir ($this->working_dir.$dirname) )
return false;
if ( strlen ($codename) == 0 )
return false;
@mkdir ($this->working_dir.$dirname, 0777);
@mkdir ($this->working_dir.$dirname."attachment", 0777);
$fd_codename = @fopen ($this->working_dir.$dirname."cw_codename", "w");
if ( ! $fd_codename )
return false;
@fwrite ($fd_codename, $codename);
@fclose ($fd_codename);
$fd_description = @fopen ($this->working_dir.$dirname."cw_description", "w");
if ( ! $fd_description )
return false;
@fwrite ($fd_description, $code_description);
@fclose ($fd_description);
$fd_tags = @fopen ($this->working_dir.$dirname."cw_tags", "w");
if ( ! $fd_tags )
return false;
@fwrite ($fd_tags, $code_tags);
@fclose ($fd_tags);
$fd_owners = @fopen ($this->working_dir.$dirname."cw_owners", "w");
if ( ! $fd_owners )
return false;
@fwrite ($fd_owners, $user_token);
@fclose ($fd_owners);
foreach ($attachment as $file){
if ( file_exists ($file) ) // dont forget about security issues!!
@copy ($file, $working_dir.$dirname."attachment/");
}
return true;
}
public function code_get_info ($dirname)
{
$dirname = $this->_clean_dirname ($dirname);
if ( $dirname == null )
return null;
if ( !is_dir ($this->working_dir.$dirname) )
return null;
// read codename
$fd_codename = @fopen ($this->working_dir.$dirname."cw_codename", "r");
if ( ! $fd_codename )
return null;
$data["codename"] = @fread ($fd_codename, $this->limits["codename_maxlen"]);
@fclose ($fd_codename);
// read description
$fd_description = @fopen ($this->working_dir.$dirname."cw_description", "r");
if ( ! $fd_description )
return null;
$data["description"] = @fread ($fd_description, $this->limits["codedesc_maxlen"]);
@fclose ($fd_description);
// read tags
$fd_tags = @fopen ($this->working_dir.$dirname."cw_tags", "r");
if ( ! $fd_tags )
return null;
$tags = @fread ($fd_tags, $this->limits["codetags_maxlen"]);
@fclose ($fd_tags);
// read owners
$fd_owners = @fopen ($this->working_dir.$dirname."cw_owners", "r");
if ( ! $fd_owners )
return null;
$data["owner"] = @fread ($fd_owners, $this->limits["codedesc_maxlen"]);
@fclose ($fd_owners);
$arr_tags = explode (" ", $tags);
if ( !empty ($arr_tags) ){
foreach ($arr_tags as $tag){
$data["tags"][] = $tag;
}
}
$data["modified"] = @filectime ($this->working_dir.$dirname);
return $data;
}
public function code_touch ($dirname, $filename, $type = "file", $content = "")
{
if ( !is_dir ($this->working_dir.$dirname) )
return false;
// v== Do something here it may be useful :)
if ( file_exists ($this->working_dir.$dirname."/attachment/".basename ($filename)) )
return false;
if ( $type == "file" ){
$rval = @touch ($this->working_dir.$dirname."/attachment/".basename ($filename));
if ( strlen ($content) > 0 ){
$fd = @fopen ($this->working_dir.$dirname."/attachment/".basename ($filename), "w");
@fwrite ($fd, $content);
@fclose ($fd);
}
} else {
$rval = @mkdir ($this->working_dir.$dirname."/attachment/".basename ($filename), 0777);
}
return $rval;
}
public function code_get_attachment ($dirname, $read_recursive = false, $limit = 100)
{
$dd = null;
$dirs = array ();
$files = array ();
$read_files = 0;
$dirname = $this->_clean_dirname ($dirname);
if ( $dirname == null )
return array ();
$attachment_dir = $this->working_dir.$dirname."attachment/";
$dd = opendir ($attachment_dir);
if ( ! $dd )
return array ();
while ( $f = readdir ($dd) ){
if ( $f == "." || $f == ".." )
continue;
if ( is_dir ($attachment_dir.$f) ){
$dirs[$dirname.$f] = $dirname.$f;
if ( $read_recursive )
$this->_read_recursive ($dirname, $f, $dirs, $files);
} else {
$files[$dirname.$f] = $dirname.$f;
}
}
closedir ($dd);
return array_merge ($dirs, $files);
}
private function _read_recursive ($dirname, $file, &$dirs, &$files)
{
if ( ! is_dir ($this->working_dir.$dirname) )
return null;
$dd = opendir ($this->working_dir.$dirname."attachment/".$file);
if ( ! $dd )
return null;
while ( $f = readdir ($dd) ){
if ( $f == "." || $f == ".." )
continue;
if ( is_dir ($this->working_dir.$dirname."attachment/".$file."/".$f)){
$dirs[$dirname.$file."/".$f] = $dirname.$file."/".$f;
$this->_read_recursive ($dirname, $file."/".$f, $dirs, $files);
} else {
$files[$dirname.$file."/".$f] = $dirname.$file."/".$f;
}
}
closedir ($dd);
return true;
}
public function code_get_attachment_count ($dirname, $files_only = false)
{
$dirname = $this->_clean_dirname ($dirname);
if ( $dirname == null )
return -1;
if ( !is_dir ($this->working_dir.$dirname."attachment/") )
return -1;
$fd = opendir ($this->working_dir.$dirname."attachment/");
$c = 0;
if ( !$fd )
return -1;
while ( $file = readdir ($fd) ){
if ( $file == "." || $file == ".." )
continue;
if ( $files_only && is_dir ($file))
continue;
$c++;
}
closedir ($fd);
return $c;
}
public function code_get_attachment_size ($dirname, $filename)
{
$dirname = $this->_clean_dirname ($dirname);
if ( $dirname == null )
return -1;
if ( ! is_dir ($this->working_dir.$dirname) )
return -1;
if ( ! file_exists ($this->working_dir.$dirname."attachment/".$filename) )
return -1;
return filesize ($this->working_dir.$dirname."attachment/".$filename);
}
public function code_get_attachment_type ($dirname, $filename)
{
$dirname = $this->_clean_dirname ($dirname);
if ( $dirname == null )
return null;
if ( ! file_exists ($this->working_dir.$dirname."attachment/".$filename) )
return null;
if ( is_dir ($this->working_dir.$dirname."attachment/".$filename) )
return "d";
return "f";
}
/*{
$data = Array ();
$resource_name = $this->_resource_name2path ($resource_name);
#echo "resource_name: $resource_name<br />";
if ( ! $this->test_resource ($resource_name) )
return $data;
if ( !($rd = opendir ($resource_name)) )
return $data;
while ( $res = readdir ($rd) ){
if ( $res == "." || $res == ".." )
continue;
$res = $resource_name.$res;
$data[$res] = $res;
if ( is_dir ($res) && $read_recursive ){
$data = array_merge ($data, $this->read ($res, true));
}
}
return $data;
}*/
/*
* #########################################################
* Experimental fuctions
* #########################################################
*
// Desc: Open container (directory) for controlled reading
function container_fopen ($dirname, $read_limit = 0)
{
$this->dd = opendir ($this->working_dir.$dirname);
if ( !$this->dd )
return false;
$this->read_limit = $read_limit;
return true;
}
function container_read ()
{
if ( $this->dd == null )
return array ();
return true;
}
function container_rewind ()
{
if ( $this->dd != null )
rewinddir ($this->dd);
return true;
}
// Just in case.
function container_fclose ()
{
if ( $this->dd != null )
closedir ($this->dd);
return true;
}
*/
};
?>
Thx.