#native_company# #native_desc#
#native_cta#

Conversion of File String to an Object in different Format

By saravanababu
on March 1, 2002

Version: 1.3

Type: Class

Category: File Management

License: WebSite Only

Description: File functions For
Readig the string insdside the XMl File and
updating with new Tags for Recovery of an Object

<?php
//Replace Session adodb_sess values with some session ,
//home dir refrers to the users folder
//Any error Occurs Then Redirection To Errorpage.html User created page
// Sorting Carried out in writing the user File in XML tags
//XMLObject refers to XMl object created
	/* The FileTerms is used for performing File Handling Operations
		* Author               :  Saravana Babu
		* Date of Creation     :  13-july-2001.
		* Date of Modification :  10-sept-2001.
		* Modified by          :  Saravana Babu
	*/
	class FileTerms
	{
		var $mFcontwr;            //File to write the contents
		var $mContwr;             //Write the contents to the file
		var $mStringwr;           //String to perform the write operation
		var $mHandledir;          //To Open the directory by getting the function parameter
		var $mFilename;           //File name getting from the Handledir string
		var $mFilenamewr;         //Open the file to perform write operation
		var $mFread;              //Opening the  files in array to read
		var $mFilearray =array(); //Read the Files in Array
		var $mFileext;            //shows the file extension
		var $mReturncode;         //returns the object
		var $mi;                   //To increment the files existing in the directory
	 	//Connection Flag
		var $mDBConnectionFlag='false';
		var $mConnection;					//For Back End Connection
		var $mUserRS;
		var $mLocationId;
		var $mFiledir;
		var $mFile;
		var $mdi;
		var $mDirarray=array();   //used to list the categories
		var $mc;				  //To increment the files in the category directory
		var $handle;              //To open and read the files in the corresponding category
		/*
			*	List The File From The Directory and Then Write The Content of Each File into The single XML
			*	File Parameter Passed are Directory Name with path, XML Object, File Opened To write, and
			*	Session Id Of The Browser
		*/

		// The Xml Object is built using the function where the open tags are intalized here

		function getUpper($ROpnfilename,$Sessionid)
		{
			$this->mi=1;
			$this->mFilenamewr = $ROpnfilename;
			$this->mFcontwr = fopen($this->mFilenamewr,"wb+");
			$this->mStringwr = fwrite($this->mFcontwr,"<root>n");
			$this->mStringwr=fwrite($this->mFcontwr,"<sessionID>".$Sessionid."</sessionID>n");
		    $pagerequest="<page_request>".$GLOBALS['page_request']."</page_request>n";

		}

		//Function perform for the  Lower Tags The XML Object 
		function getLower()
		{
			$this->mStringwr = fwrite($this->mFcontwr,"</root>n");
			fclose($this->mFcontwr);
		}
        
         // List for The Files in the directory and Stores in an array for further manipulation of 
		 // xml Object tags for the received xml file 
		function getFileDir($rHandlestr,&$rXmlObj,$rOpnfilename,$Sessionid)
		{
			
			if($rHandlestr!='' && $this->mHandledir =@opendir($rHandlestr))
			{
				$this->getUpper($rOpnfilename,$Sessionid);
				while (false!==($this->mFilename = readdir($this->mHandledir)))
				{
					$this->fileextension=substr($this->mFilename,strlen($this->mFilename)-4);
					if($this->fileextension==".xml")
					{
						$this->mFilearray[$this->mi] =$rHandlestr."/".$this->mFilename;
						$this->mi++;
					}
				}
				$this->noThemernoPer($this->mFilearray[1]);
				closedir($this->mHandledir);
				array_multisort($this->mFilearray,SORT_ASC,SORT_STRING);
				$dummyarr=array();
				$dummyarr=$this->mFilearray;
				for($j=0;$j<$this->mi;$j=$j+2)
				{
					for($h=$j;$h<=$j+1;$h++)
					{
						 $this->mFilearray[$h+1]=$dummyarr[$h];
					}
				}
				for($j=1;$j<$this->mi;$j=$j+2)
				{
					$this->mStringwr=fwrite($this->mFcontwr,"<section ncount='$j'>n");
					for($h=$j;$h<=$j+1;$h++)
					{
						if($this->mFilearray[$h]!="")
						{
							$this->mFread = fopen($this->mFilearray[$h],"rb+");
							$this->mContwr=fread($this->mFread,filesize($this->mFilearray[$h]));
							$this->mStringwr = fwrite($this->mFcontwr,$this->mContwr);
							fclose($this->mFread);
						}
					}
					$this->mStringwr=fwrite($this->mFcontwr,"</section>n");
				}
				$this->getLower();
				$rXmlObj = xmldocfile($this->mFilenamewr);
				if(is_Object($rXmlObj))
				{return $this->mReturncode = $rXmlObj;}
				else
				{
					header("location:ErrorPage.html");
					exit;
				}
			}
			else
			{
				header("location:ErrorPage.html");
				 exit;
			}
			return $this->mReturncode;
		}
		/*	Read a single File and  Then Write The Content of the File into The single XML File Parameter
		*	Passed are Read File Name, XML Object, File Opened To write, and Session Id Of The Browser
		*/
		function getFilenosection($RdFilename,&$rXmlObj,$rOpnfilename,$sessionid)
		{   $this->validate($RdFilename);
			$this->getUpper($rOpnfilename,$sessionid);
			//File To Read and Transfer To content string and then write in a file

			$this->mFread = fopen($RdFilename,"rb");
			$this->mContwr=fread($this->mFread,filesize($RdFilename));
			$this->mStringwr = fwrite($this->mFcontwr,$this->mContwr);
			fclose($this->mFread);
			$this->getLower();
			$rXmlObj = xmldocfile($this->mFilenamewr);
			if(is_Object($rXmlObj))
			{return $this->mReturncode = $rXmlObj;}
			else
			{
				header("location:ErrorPage.html&dir=Invalid Tags&ministryid=".$GLOBALS['ministryid']);
				exit;
			}
		}

		// The Function Which is used to handle only a single file 
		function getFile($RdFilename,&$rXmlObj,$rOpnfilename,$sessionid)
		{   $this->validate($RdFilename);

			$this->getUpper($rOpnfilename,$sessionid);
			$this->mStringwr=fwrite($this->mFcontwr,"<section>n");
			//File To Read and Transfer To content string and then write in a file
			$this->mFread = fopen($RdFilename,"rb");
			$this->mContwr=fread($this->mFread,filesize($RdFilename));
			$this->mStringwr = fwrite($this->mFcontwr,$this->mContwr);
			fclose($this->mFread);
			$this->mStringwr=fwrite($this->mFcontwr,"</section>n");
			$this->getLower();
			$rXmlObj = xmldocfile($this->mFilenamewr);
			if(is_Object($rXmlObj))
			{return $this->mReturncode = $rXmlObj;}
			else
			{
				header("location:ErrorPage.html");
				exit;
			}
		}
		/*
		*	Read a single file and directory then write the content of the file to  one xml file
		*	Parameter passed are dir,filename,XML Obj,File open to write and session ID
		*/
		function getFilewithDir($rHandlestr,$RdFilename,&$rXmlObj,$rOpnfilename,$sessionid)
		{     
			$this->validate($RdFilename);
			if($rHandlestr!='' && $this->mHandledir =@opendir($rHandlestr))
			{
				$this->getUpper($rOpnfilename,$sessionid);
				while (false!=($this->mFilename = readdir($this->mHandledir)))
				{
					//To read the file Category wise for Modify Theme
					if($this->mFilename!='.' && $this->mFilename !='..')
					{
						if(is_dir($rHandlestr."/".$this->mFilename))
						{
							//creating XML Tag to fetch the categories
							$this->mStringwr=fwrite($this->mFcontwr,"<filedir>");
							$this->mDirarray[$this->mc]=$this->mFilename;
							$this->mStringwr=fwrite($this->mFcontwr,"<dircat>".$this->mDirarray[$this->mc]."</dircat>");
							//$this->mc++;
							
							$this->mStringwr=fwrite($this->mFcontwr,"</filedir>");

							//read the XML Files in the corresponding category
							if($GLOBALS['cat']!='' && $this->mFilename==$GLOBALS['cat'])
							{	
									$this->handle=opendir($rHandlestr."/".$GLOBALS['cat']);
									while(false!=($this->mFile=readdir($this->handle)))
									{
										$this->fileextension=substr($this->mFile,strlen($this->mFile)-4);
										if($this->fileextension==".xml")
										{
											$this->mFilearray[$this->mi] =$rHandlestr."/".$this->mFilename."/".$this->mFile;
											$this->mi++;
										}
									}
									closedir($this->handle);
							}
					    }
					}
					/*$this->fileextension=substr($this->mFilename,strlen($this->mFilename)-4);
					if($this->fileextension==".xml" && fileperms($rHandlestr."/".$this->mFilename)=='33279')
					{
						$this->mFilearray[$this->mi] =$rHandlestr."/".$this->mFilename;
						$this->mi++;
					}*/
				}
				//$this->noThemernoPer($this->mFilearray[1]);
				closedir($this->mHandledir);
				/*$arrayvalue=array("Themeaa","Themeba","Themeab","Themeca","Themedd");
				array_multisort($arrayvalue,SORT_ASC,SORT_STRING);
					for($g=0;$g<count($arrayvalue);$g++)
					{
							echo $arrayvalue[$g]."<br/>";
					}*/
				array_multisort($this->mFilearray,SORT_ASC,SORT_STRING);
				$dummyarr=array();
				$dummyarr=$this->mFilearray;
				//write the file section wise to display two templates in a row
				for($j=0;$j<$this->mi;$j=$j+2)
				{
					for($h=$j;$h<=$j+1;$h++)
					{
						 $this->mFilearray[$h+1]=$dummyarr[$h];
					}
				}
				for($j=1;$j<$this->mi;$j=$j+2)
				{
					$this->mStringwr=fwrite($this->mFcontwr,"<section ncount='$j'>n");
					for($h=$j;$h<=$j+1;$h++)
					{
						if($this->mFilearray[$h]!="")
						{
							$this->mFread = fopen($this->mFilearray[$h],"rb");
							$this->mContwr=fread($this->mFread,filesize($this->mFilearray[$h]));
							$this->mStringwr = fwrite($this->mFcontwr,$this->mContwr);
							fclose($this->mFread);
						}
					}
					$this->mStringwr=fwrite($this->mFcontwr,"</section>n");
				}
				//File To Read and Transfer To content string and then write in a file
				$this->mStringwr=fwrite($this->mFcontwr,"<section>n");
				$this->fd4= fopen($RdFilename,"rb");
				$this->content=fread($this->fd4,filesize($RdFilename));
				$this->mStringwr = fwrite($this->mFcontwr,$this->content);
				fclose($this->fd4);
				$this->mStringwr = fwrite($this->mFcontwr,"</section>n");
				$this->getLower();
				$rXmlObj = xmldocfile($this->mFilenamewr);
				if(is_Object($rXmlObj))
				{return $this->mReturncode = $rXmlObj;}
				else
				{
				 	header("location:ErrorPage.html");
					exit;
				}
			}
			else
			{
				 header("location:ErrorPage.html");
				 exit;
			}
			return $this->mReturncode;
		}
		//The File is not found in an array then the user is redirected to the Ministries of Listservices 
		//Home page of the TAGnet
		function noThemernoPer($FileName)
		{
			if($FileName=='')
			{
				 header("location:ErrorPage.html");
				 exit;

				}
		}
		function validate($RdFilename)
		{
            
			if(!file_exists ($RdFilename))
			{
				 
         header("location:MainPage.html");
				 exit;
			}
		}


	  
	}
?>