Using PHP to generate an XML file - php

I currently have a PHP script which when ran outputs an XML file (however as feed.php not feed.xml) I have been told to add this to my .htaccess and it should fix it:
AddType application/x-httpd-php .xml
However for some reason that isn't working.
What I'd ideally like the script to do - when ran is to generate a feed.xml file with the output of its contents, rather than just outputting the contents on the php page.
Here is my code:
<?PHP
include("../config.php");
#// Timetable Clearup Variabls
$yesterday = strtotime('yesterday');
$yesterdow = date('l',$yesterday);
$order = "SELECT * FROM timetable WHERE day = '$yesterdow' ORDER BY time";
$result = mysql_query($order);
$yesterdayd = date('F jS, Y', time()-86400);
//SET XML HEADER
header('Content-type: text/xml');
//CONSTRUCT RSS FEED HEADERS
$output = '<rss version="2.0">';
$output .= '<channel>';
$output .= "<title>Timetable - {$yesterdayd} </title>";
$output .= '<description>Timetable.</description>';
$output .= '<link>http://site.com/</link>';
### $output .= '<copyright>Your copyright details</copyright>';
while ($row = mysql_fetch_array($result)) {
//BODY OF RSS FEED
$output .= '<item>';
$output .= "<description><td>" . htmlspecialchars($row['username']) . "</td><td>" . htmlspecialchars($row['time']) . "</td></description>";
$output .= '</item> ';
}
//CLOSE RSS FEED
$output .= '</channel>';
$output .= '</rss>';
//SEND COMPLETE RSS FEED TO BROWSER
echo($output);
?>

I would suggest not adding .xml in .htaccess to be processed using PHP compiler.
Rather use mod_rewrite to redirect requests to .xml files to php script.
For example:
RewriteEngine On
RewriteRule ^(.*)\.xml$ /rss_feed.php [L,QSA]

Related

PHP generating a .php.vcf file instead of .vcf file

I have a feature in my PHP website where a user can download a vcard file for an employee, however the file being generated is .php.vcf and isn't supported on mobile.How can i change the file format to .vcf, The simplified PHP code for the vcf generation is below.
function generate_vcard($fn,$ln){
$text = 'BEGIN:VCARD' . "\r\n";
$text .= 'VERSION:3' . "\r\n";
$text .= 'Firstname:'.$fn . "\r\n";
$text .= 'Firstname:'.$fn . "\r\n";
$text .= "END:VCARD"."\r\n";
return $text;
}
$generated_text = generate_vcard('John', 'Smith');
header('Content-Type: text/vcard');
echo $generated_text;
The vcard is downloading, the problem is with the extension as mentioned above that is '.php.vcf' is being generated instead of '.vcf'.
You may try to add content disposition and put filename there:
header('Content-Disposition: attachment; filename="filename.vcf"');
Look at header() documentation, specifically in the "Example #1"

How do you restrict users to upload only pure html files

how can I restrict users to upload files that are Pure HTML Only.
I'm currently creating an online editor in PHP that would produce a web page which can be printed to PDF. As such, I have some HTML files that need to be dynamically included in the web page. This is because manually adding them through the online editor would take a long time as they include a lot of content, plus they contain static data which need not be changed. Think of them as presets.
Now the problem occurs when I let the users upload their own presets. As far as I know, any file with the .html extension can hold PHP code. How can I make sure that no PHP code lies within the uploaded file? Is it even possible to escape PHP tags using PHP?
Code example:
//User fills the PDF editor form and submits it.
is_logged(); // Login check and setting user info variables, redirects if not logged in.
if ( !isset( $_POST['gen-page'] ) {
custom_redirect( MAIN_PATH );
}
$content .= "<html>";
$content .= "<head>";
$content .= "<meta charset='utf-8'>";
$content .= "<meta name=viewport' content='width=device-width, initial-scale=1'>";
$content .= "<title>"$user_name"</title>";
$content .= file_get_contents( MAIN_PATH . '/uploads/' . $user_id . '/' . $user_style . '.html' ); // Contains style details.
$content .= "</head>";
$content .= "<body>";
for ($inputs as $input) { // Inputs is an array that was posted by the user.
$content .= "<p>" . $input . "</p>";
}
$content .= file_get_contents( MAIN_PATH . '/uploads/' . $user_id . '/' . $user_file . '.html' ); // The preset that the user has uploaded.
$content .= '</body>';
$filename = MAIN_PATH.'viewable/view-' . $user_id . '.html';
file_put_contents( $filename, $content );
custom_redirect( MAIN_PATH.'viewable/view-' . $user_id . '.html' );

Php Vcard shows empty on outlook

I am trying to create a vcard link download that creates the vcard on-the-fly.
if(isset($_POST)){
header("Content-type: text/vcard");
header("Content-Disposition: attachment; filename=" . get_the_title() . ".vcf");
$content = "\rBEGIN:VCARD\r";
$content .= "VERSION:2.1\r";
$content .= "N:Vicon;Barry;S.\r";
$content .= "FN:Barry S. Vicon\r";
$content .= "ORG:sample\r";
$content .= "TITLE:Partner\r";
$content .= "NOTE;ENCODING=QUOTED-PRINTABLE: =0D=0A\r";
$content .= "TEL;WORK;VOICE:(516) 999-9999\r";
$content .= "TEL;WORK;FAX:(516) 999-9999\r";
$content .= "URL;WORK:http://www.sample.com\r";
$content .= "EMAIL;PREF;INTERNET:bcohen#sample.com\r";
$content .= "REV:TZ\r";
$content .= "END:VCARD";
echo $content;
}
the file downloads fine and the file format is correct as well but when opened in outlook, not a single info is imported.
here is the output file of the_title.vcf
BEGIN:VCARD
VERSION:2.1
N:Vicon;Barry;S.
FN:Barry S. Vicon
ORG:sample
TITLE:Partner
NOTE;ENCODING=QUOTED-PRINTABLE: =0D=0A
TEL;WORK;VOICE:(516) 999-9999
TEL;WORK;FAX:(516) 999-9999
URL;WORK:http://www.sample.com
EMAIL;PREF;INTERNET:bcohen#sample.com
REV:TZ
END:VCARD
another pre-existing vcard has the exact same info and everything seems to be working when outlook opens that file.
what am I missing here?
$content = "BEGIN:VCARD\n\r";
Solves the issue. thanks to Marc's comment

How do I correctly structure this xml with variables?

How do I correctly structure this XML document. This document will be embedded within a PHP script and send orders to a folder on the server.
Also, can you please look at the FOR EACH loop for products?
Thanks for your help!
(Variables from order form)
$order_id = '1234';
$product_id = '5678';
$prodduct_sku = '0123';
$product_retail = '123.45';
define("FILENAME", "orders/order" . $order_id . ".xml");
$xml_output = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$xml_output .= "<document type="850X-02" timestamp= $timestamp id="123456789>";
$xml_output .= "<order id=" . $order_id .>";
for ($x=0; $x<=100; $x++)
{
$xml_output .= "<line id=" . $x . "quantity=" . $quantity . />";
$xml_output .= "<product id=" . $product_id . "supplier_sku=" . $product_sku . "retail_price=" . $product_retail . />";
}
$xml_output .= "</document>';
// CONVERT THE ARRAY TO A STRING
$str = implode($xml_output);
// WRITE IT INTO THE FILE SYSTEM
file_put_contents(FILENAME, $str);
You don't close your order tag. Add </order> somewhere after the line loop.
Beyond that, you may consider using an IDE that provides robust xml formatting and validation, or you can use a web-based xml validator to verify that the xml you create is valid.
Another tip for testing xml generation code is to get a valid xml sample for the target system, then refine your code until you can generate that example text. Using file diff tools you can quickly identify things like missing tags, elements instead of attributes, or other common errors.

How to replace invalid characters in XML using Javascript or PhP

Need help here for the following:
Running PhP, javascript, MySQL, XML.
1) Retrieving file from MySQL and stored it onto XML file.
2) Use javascript function to load XML file (that stored those data).
3) It produces invalid characters in XML file.
STEP 1 : Sample of the code in PhP -> Loading MySQL DB to store data onto XML file
$file= fopen("MapDeals2.xml", "w");
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
$_xml .="<MAP>\n";
while($row1_ThisWeek = mysql_fetch_array($result1_ThisWeek)) {
$rRName = $row1_ThisWeek['Retailer_Name'];
$rRAddress = $row1_ThisWeek['Retailer_Address1'];
$rRAddressPostCode = $row1_ThisWeek['Retailer_AddressPostCode1'];
//} commented out from the original
$_xml .= "<DEAL>\n";
$_xml .= "<DealDescription>" . $d_Description . "</DealDescription>\n";
$_xml .= "<DealURL>" . $d_URL . "</DealURL>\n";
$_xml .= "<DealRName>" . $rRName . "</DealRName>\n";
$_xml .= "<DealRAddress>" . $rRAddress . "</DealRAddress>\n";
$_xml .= "<DealRPostCode>" . $rRAddressPostCode . "</DealRPostCode>\n";
$_xml .= "</DEAL>\n";
}
//} commented out from the original
$_xml .="</MAP>\n";
fwrite($file, $_xml);
fclose($file);
STEP 2 : Sample of the code in Javscript -> Loading XML file
xhttp.open("GET","Test2.xml", false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
var x=xmlDoc.getElementsByTagName("Employee");
parser = new DOMParser();
xmlDoc = parser.parseFromString("MapDeals2.xml", "text/xml");
for (i=0;i<x.length;i++)
{
// alert ('Generating FOR loop');
var d1 = x[i].getElementsByTagName("EmployeeDescription")[0].childNodes[0].nodeValue;
var e1 = "<br></br>";
.
.
.
}
Is there a solution for the above? Looking forward to hear from you soon.
Cheers
It probably is a charset problem. Your MySQL connection has encoding iso-8859-1 or something like that, while the XML is expected to be in UTF8. You have to convert it somewhere.

Categories