Php Vcard shows empty on outlook - php

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

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' );

Come back to the line at the end of a cell in a csv file with php

I would like to return to the line in my csv file, automatically if is end of cell of the csv file, not in the end of data.
I don't want to use fputcsv function ...
$csv = utf8_decode('Export csv
Extrait du SIR Web - le '.date('d/m/Y').'
Pour l\'année : '.$annee.'
Auditeur : '.$auditeur.'
Zone audit : '.$zoneaudit.'
Dpt : '.$departement.'
');
$csv .= utf8_decode('Code centre;Agrément;Auditeur;Zone audit;Date audit;Numéro NC;Code NC;Délai AC;Délai Rép;Validée;Date Validation;Info val au centre;Date rel 1;Date rel 2;Dernier délai;Observations;Date codir;Sanction codir;Clôture d\'Audit;Observations;Action corrective');
foreach ($aResultStats as $row){
$csv .= utf8_decode($row['data_a']).';';
$csv .= utf8_decode($row['data_b']).';';
$csv .= utf8_decode($row['data_c']).';';
$csv .= utf8_decode($row['data_d']).';';
$csv .= utf8_decode($row['data_e']).';';
$csv .= '="'.utf8_decode($row['data_f']).'";';
$csv .= utf8_decode(trim($row['data_g'])).';';
$csv .= utf8_decode($row['data_h']).';';
$csv .= utf8_decode($row['data_i']).';';
$csv .= utf8_decode($row['data_j']).';';
$csv .= utf8_decode($row['data_k']).';';
$csv .= utf8_decode($row['data_l']).';';
$csv .= utf8_decode($row['data_m']).';';
$csv .= utf8_decode($row['data_n']).';';
$csv .= utf8_decode($row['data_o']).';';
$csv .= utf8_decode($row['data_p']).';';
$csv .= utf8_decode($row['data_q']).';';
$csv .= utf8_decode($row['data_r']).';';
$csv .= utf8_decode($row['data_s']).';';
$csv .= utf8_decode(str_replace(array("\r\n","\n"),' ',$row['data_t'])).';
';
/*$csv .= utf8_decode(str_replace(array("\\r\", "\", "\\r"),array(" ", " ", " "),$row['data_t'])).';';*/
$csv .= utf8_decode(str_replace(array("\r\n","\n"),' ',$row['data_u'])).'
';
}
$filename = 'export_' . date('Ymd') .'_' . date('His');
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$filename.".csv");
echo $csv;
In this picture, T and U are two columns, the data of the T columns overflows on the U columns and more...
thanks for help
You have a break line character in the middle of the row content, right after the last delimiter. This may cause an unexpected behavior.
$csv .= utf8_decode(str_replace(array("\r\n","\n"),' ',$row['data_t'])).';
';
It should be something like this
$csv .= utf8_decode(str_replace(array("\r\n","\n"),' ',$row['data_t'])).';';
It is always better in CSV to wrap in quotations when handling UGC, rather than relying on special characters removal.
$csv .= '"' . str_replace('"', '""', utf8_decode($row['data_t']) . '";';
$csv .= '"' . str_replace('"', '""', utf8_decode($row['data_u']) . "\"\n";
You have set an incorrect Content-Type for CSV file. It should be text/csv or you can just leave it as text/plain.

Warnings preventing file_get_content from reading properly

In appending an attachment I am receiving a warning message preventing me attaching an existing pdf file to my e-mail. I know it isn't my headers since the script is successfully attaching a vcard generated by a string earlier within it.
Since I am not editing the file, TDPDF & FPDF shouldn't be required. (not 100% on that though) Here is the code I've been working with. I've included my output testing lines & comments.
//File Definition
$filepath = "docs/";
//$filepath = "./docs/"; //tried: same result
$fullpath = $filepath . $filename; //$filename defined earlier
//File Manipulation
$file = fopen($fullpath,'rb');
$pdfdata = fread($file, filesize($fullpath));
fclose($file);
//Testing
echo $fullpath . "<br />\n";
echo "Filename: " .$filename . "<br />\n";
echo "Filesize: " .filesize($fullpath). "<br />\n";
echo "String Length: " .strlen($pdfdata). "<br />\n";
//The Following line proved the variable is dumping properly,
//but its content cannot be used for file_get_contents...huh?
//var_dump($pdfdata); //Only used for proofing
echo "Probable Errors for file_get_contents<br />\n";
$data = file_get_contents($pdfdata);
// The following line: Sends File, but is 0 bytes
//$attachment = chunk_split(base64_encode($pdfdata));
//default
$attachment = chunk_split(base64_encode(file_get_contents($pdfdata)));
This outputs:
docs/pdf-to-send.pdf
Filename: pdf-to-send.pdf
Filesize: 37907
String Length: 37907
Probable Errors for file_get_contents
Warning: file_get_contents(%PDF-1.5 % ... (truncated by me)
... ) [function.file-get-contents]: failed to open stream: No such file or directory in /my/hosting/directory/mailer.php on line 337
Warning: file_get_contents(%PDF-1.5 % ... (truncated by me )
... ) [function.file-get-contents]: failed to open stream: No such file or directory in /my/hosting/directory/mailer.php on line 339
Its telling me the file size, that can be found in 2 different variables: $pdfdata & $filesize. They match up. I will mention that the response that I truncated (due to the charset) is already truncated by the server. Its why I started checking the length.
Finally, just in case it could possibly be my headers since I was able to successfully send a 0 byte file, here are those lines...
$body .= "--". $mime_boundary. "\r\n" ;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"". "\r\n";
$body .= "Content-Transfer-Encoding: base64" . "\r\n";
$body .= "Content-Disposition: attachment;" . "\r\n";
$body .= "filename=\"".$filename."\"" . "\r\n\n";
$body .= $attachment . "\n\n";
I am aware that I can change (and have tried) the "Content-Type" to "application/pdf".
My charset is UTF-8. I may be misunderstanding "binary-safe" descriptions for fopen() & fread(), but that shouldn't cause the script to fail. Should it?
Any help resolving this would be greatly appreciated.
Ok. I fixed it. Oddly enough it WAS in my headers.
The header command I posted was actually correct. Sadly I posted my vcard attachment body ammendment. So the question already had my answer in it. ::bonk::
This line is correct.
$body .= "filename=\"".$filename."\"" . "\r\n\n";
$body .= $attachment . "\n\n";
This is what I actually had.
$body .= "filename=\"".$filename."\"" . "\r\n";
This explains why I had a 0 byte attachment.
And, just to show the snippet that actually worked:
//File Manipulation
$file = fopen($fullpath,'rb');
$pdfdata = fread($file, filesize($fullpath));
fclose($file);
//Testing
// echo $fullpath . "<br />\n";
//echo "Filename: " .$filename . "<br />\n";
//echo "Filesize: " .filesize($fullpath). "<br />\n";
// echo "String Length: " .strlen($pdfdata). "<br />\n";
//var_dump($pdfdata); // Don't remove this (as a comment) again. LOL
// echo "Probable Errors for file_get_contents<br />\n";
//$data = file_get_contents($pdfdata, true);
$attachment = chunk_split(base64_encode($pdfdata));
//$attachment = chunk_split(base64_encode(file_get_contents($pdfdata)));
Sorry if I wasted anyone's time.

Using PHP to generate an XML file

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]

Categories