PHP Class kontrola – PHP – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

PHP Class kontrola – PHP – Fórum – Programujte.comPHP Class kontrola – PHP – Fórum – Programujte.com

 

Earl Cash0
Věrný člen
19. 3. 2012   #1
-
0
-

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.

Nahlásit jako SPAM
IP: 46.36.57.–
Oh my goodness, shut me down! Machines making machines. How perverse. (C-3PO)
Earl Cash0
Věrný člen
24. 3. 2012   #2
-
0
-

*SIGH*

Nahlásit jako SPAM
IP: 46.36.57.–
Oh my goodness, shut me down! Machines making machines. How perverse. (C-3PO)
yetty
~ Redaktor
+5
Super člen
24. 3. 2012   #3
-
0
-

Nechce se mi vůbec přemýšlet nad tím, k čemu je to dobré ;)

Ale po letmém zkouknutí: vůbec se mi nelíbí množství potlačování chyb. Pokud něco ukládám do souboru a nevím jestli se to povedlo nebo ne, je to k ničemu. Potřebuji mít jistotu. Jinak řečeno, zavináče bych používal co možná nejméně.

Ještě bych doporučil použití funkcí file_get_contents a file_put_contents.

Nahlásit jako SPAM
IP: 46.23.51.–
yetty
~ Redaktor
+5
Super člen
24. 3. 2012   #4
-
0
-

Na zjišťování jestli je na konci lomítko nebo ne, je regulární výraz celkem brutální 

if ( preg_match ("/\/$/", $dirname) == 0 ) 

Mnohem rychlejší bude něco takového (nezkoušel jsem, možná bude potřeba opravit):

if ( $dirname[strlen($dirname)-1] != '/' )

(Ale možná by nebylo od věci změřit, jak rychle obě varianty běhají. Občas bývají výsledky nečekané.)

Nahlásit jako SPAM
IP: 46.23.51.–
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 33 hostů

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý