XML
|
|
|
|
<?php
/* --=[ By Zubair Saiyed ]=--
--=[ zubair_911@hotmail.com ]=--
--=[ Dt : 4th June 2002 ]=--
--=[ Dt : 7th June 2002 ]=--
--=[ -Prototype One- ]=-- */
// Working Function of XML Parsing \
/***** Include Section *****/
include ("imap.class");
/***** Global Variable Section *****/
//$File = "Mail_Header.xml"; // this can be any xml file
$FileOut = "MailLog.xml" ;
$Buffer = array ( ) ;
$Mirror = array ( ) ;
$Position = array ( ) ;
$PosCount = 0 ;
$Count = 0 ;
$String ;
$MainString ;
$Header ;
//$XSLFile ;
$Result ;
if ( ! ($Flag = $imap->open("username","password","host","143" )))
die ( "Can not Connect to Imap Server !!!! " ) ;
/***** Parsing Function *****/
function MailXMLParser( $File , $XSLFile )
{
global $FileOut ;
global $Buffer, $Mirror, $Position, $PosCount ;
global $Count, $String, $MainString , $Header ;
/***** Opening the Needed XML File *****/
if ( ! ( $Fi = fopen ( $File , "r" ) ) )
die ( " Could not able to Open File" . $File ) ;
/* XSL Content Reader */
if ( ! ( $Fx = fopen ( $XSLFile , "r" ) ) )
die ( " Could not able to Open File" . $File ) ;
else
$XSLFile = fread ( $Fx,filesize($XSLFile));
fclose ( $Fx ) ;
/* End-XSL-Reader*/
/* Important : Header Extraction Function Excluded Here */
/***** Reading File and Storing into the Buffer *****/
while ( ! feof ( $Fi ) )
{
$Buffer [ $Count ] = fgetc ( $Fi ) ;
$Count++ ;
}
$Total = $Count ;
for ( $Count = 0 ; $Count < $Total ; $Count++ )
{
if ( $Buffer [ $Count ] == "<" && $Buffer [ $Count+1 ] == "?" )
{ //Marking the Position //
$Position [ $PosCount ] = $Count ;
$PosCount++ ;
}
if ( $Buffer [ $Count ] == "?" && $Buffer [ $Count+1 ] == ">" )
{ //Marking the Position //
$Position [ $PosCount ] = $Count ;
$PosCount++ ;
}
}
for ( $i = 0 ; $i < $PosCount ; $i++ )
{
if ( ( $i + 1 ) % 2 != 0 ) // Starting Pos to Copy into String
{
$Start = $Position [ $i ] ;
$End = $Position [ $i +1 ] ;
for ( ; $Start < $End + 2 ; $Start++ )
{
$String .= $Buffer [ $Start ] ;
}
$Header .= $String ;
$Header .= "n" ;
$String = ""; // Set String to NULL //
}
}
fclose ( $Fi ) ;
/**** Closing the file as it has been Parsed to Check The Headers of the XML *****/
/***** Create the Parser *****/
$Parser = xml_parser_create ( ) ;
/***** Registering the Handlers *****/
// -=[ Dt: 7th June 2002 ]=- For Case Folding Option //
xml_parser_set_option ($Parser,XML_OPTION_CASE_FOLDING,FALSE);
xml_set_element_handler ( $Parser , "startElementHandler" , "endElementHandler" ) ;
xml_set_character_data_handler ( $Parser , "cdataHandler" ) ;
/***** Opening File Again to Use XML_Parse *****/
/// An Inclusion of File That will be created in between the Process ///
if ( ! ( $Fp = fopen ( $File , "r" ) ) )
die ( " Could not able to Open File" . $File ) ;
while ( $Data = fread ( $Fp , 4096 ) )
{
if ( !xml_parse ( $Parser , $Data , feof ( $Fp ) ) )
{
die ( "Error in Xml Document" ) ;
}
}
if ( !xml_parser_free( $Parser ) )
die("Fatal Error :: Can not Free Parser");
/***** Finally Writing to Output File *****/
/***** Closing the Files *****/
fclose ( $Fp ) ;
/***** Printing Directed to Browser ****/ // May 25th Edition
$MainString = $Header . $MainString ;
CreateXSLT($MainString,$XSLFile ) ;
// we have to pass this string as XML in xslt_process //
/***** Fuctions that is Required to Generate the Dynamic XML File *****/
}
function startElementHandler ( $Parser , $Name , $Attrib )
{
$String = "<". $Name . ">" ;
// --=[ if Some of the Tag can be Looped ]=-- //
$String = CheckTags ( $String ) ;
WriteFile ( $String ) ;
}
function endElementHandler ( $Parser , $Name )
{
$String = "</". $Name . ">n" ;
WriteFile ( $String ) ;
//echo ( "</$Name> <br>" ) ;
}
function cdataHandler ( $Parser , $Data )
{
$String = $Data ;
if ( $String != "n" )
WriteFile ( $Data ) ;
else
WriteFile ( $String ) ;
//echo ("$Data<br>" ) ;
}
/***** Function That Writes In the MainString such that contain all Remaing XML Tags *****/
function WriteFile ( $String )
{
global $MainString ;
$MainString .= $String . " n" ;
}
/***** Function that can Loop Specific Tags *****/
function CheckTags ( $String )
{ global $Result ;
global $imap ;
switch ( $String )
{
// No of Cases Made Here !! //
case "<Mail_Header>":
$String = MailHeaders();
return $String ;
case "<Mail_Footer>": return $String ;
case "<Mail_Box>": return $String ;
} // Switch Ends
} // Fuction ends
/***** Function that returns the Header of Mails *****/
function MailHeaders ( )
{
global $imap ;
$Result = $imap->mail_headers_list();
$Counter = count($Result)-1;
while ( list($Key,$Value) = each($Result) )
{
if ( $Key < $Counter)
{
$String .= "<Mail_Header>"."n";
$String .= $Value ."n"."</Mail_Header>"."n" ;
}
if ( $Key == $Counter)
{
$String .= "<Mail_Header>"."n";
$String .= $Value ;
}
} // while ends
return $String ;
}
/***** Function that Generates the HTML For Particular XML/XSL *****/
function CreateXSLT ( $MainString , $XSLFile )
{ global $FileOut ;
if ( ! ( $Fp = fopen ( $FileOut , "w" ) ) )
die ( " Could not able to Open File" . $File ) ;
$arguments = array('/_xml' => $MainString ,'/_xsl' => $XSLFile );
$xsltproc = xslt_create();
$html = xslt_process( $xsltproc,'arg:/_xml','arg:/_xsl',NULL,$arguments);
if (!$html) die('XSLT processing error: '.xslt_error($xsltproc));
xslt_free($xsltproc);
echo $html ;
fputs($Fp,$MainString);
fclose($Fp);
}
MailXMLParser( "Mail_Header.xml", "Mail_Header.xsl" ) ;
// here Pass any Standard XML/XSL file //
// Regards -- Zubair Saiyed //
?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|