Mpdf library won't output the PDF in browser rather always download - mpdf

I am not sure why it is not opening in any browser and always download it.
<?php
use Mpdf\Mpdf;
require __DIR__ . '/vendor/autoload.php';
// Grab variables
$fname = $_POST[ 'fname' ];
$lname = $_POST[ 'lname' ];
$email = $_POST[ 'email' ];
$phone = $_POST[ 'phone' ];
$message = $_POST[ 'message' ];
$filename = 'invoice.pdf';
ob_clean();
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
$mpdf = new Mpdf();
//
// create pdf
$data = '<h1>' . $fname . ' ' . $lname . '\'s Details</h1>';
$data .= '<table>';
$data .= '<tr><th>First Name: </th><td>' . $fname . '</td><td class="price">220,25kr</td></tr>';
$data .= '<tr><th>Last Name: </th><td>' . $lname . '</td><td class="price">1257,50kr</td></tr>';
$data .= '<tr><th>Email: </th><td>' . $email . '</td><td class="price">25,35kr</td></tr>';
$data .= '<tr><th>Phone: </th><td>' . $phone . '</td><td class="price">532,00kr</td></tr>';
if ($message) {
$data .= '<tr><th>Message: </th><td colspan="2">' . $message . '</td></tr>';
}
$data .= '<tr><th colspan="2">Total </th><td class="price">7,251,90kr</td></tr>';
$data .= '</table>';
// write pdf
$stylesheet = file_get_contents('invoice.css');
$mpdf->shrink_tables_to_fit = 1;
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($data);
// output to browser
$mpdf->Output($filename, 'I');
ob_end_flush();
?>

It was a Download Manager extension that was taking over the behavior. I have disabled, and everything works great.

Related

WP_mail attachment: An attachment was sent but it was not in the media library

I've a code snippet that builds an html email and send it towards a batch of users.
The email can contain one or multiple attachments.
Previously, my code was working fine, but since a few months the attachments are not sent anymore. More concrete, wordpress mail loggers tell me:
An attachment was sent but it was not in the media library
Below the code that handles the attachment, the rest of the email is working fine, headers are set to text/html
If I dump the path of the attachment in the mail an example is:
/home/lwk/domains/lintsewindklievers.be/public_html/wp-content/uploads/2022/10/CaveTriathlon_2021_111_RUN.gpx
I've no idea why this isn't working anymore..
Someone who might know what's going wrong?
function sentMail($rit, $maillist){
global $wpdb;
global $today;
$today = new DateTime();
$today = $today->format('Y-m-d');
$checkMail = FALSE;
$attachments = array();
// components for our email
$subject = "LWK Rit | ".date("d-m-Y h:i", strtotime($rit->datum_rit))." | ".$rit->post_title;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "'Content-Type: text/html; charset=UTF-8'";
//$headers .= "'Content-type: multipart/mixed; charset=iso-8859-1";
$headers .= "'From: De Lintse Windklievers <mail#lintsewindklievers.be>'";
$headers .= "'Reply-To: secretaris#lintsewindklievers.be'";
$message = '<html><body>';
$message .= '<img width="150" height="78" src="https://lintsewindklievers.be/wp-content/uploads/2021/05/Logo-LWK-scaled.jpg" alt="Website Change Request" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Title</strong> </td><td>" . $rit->post_title . "</td></tr>";
$message .= "<tr><td><strong>Datum</strong> </td><td>" . date("d-m-Y h:i", strtotime($rit->datum_rit)) . "</td></tr>";
$message .= "<tr><td><strong>Afstand</strong> </td><td>" . $rit->afstand . "</td></tr>";
$message .= "<tr><td><strong>Organistie</strong> </td><td>" . $rit->organisatie . "</td></tr>";
$message .= "<tr><td><strong>Vertrekplaats:</strong> </td><td>" . $rit->vertrekplaats . "</td></tr>";
$message .= "<tr><td><strong>Stopplaats:</strong> </td><td>" . $rit->stopplaats . "</td></tr>";
$message .= "<tr><td><strong>Opmerkingen:</strong> </td><td>" . $rit->opmerkingen . "</td></tr>";
if (isset($rit->gpx_route)){
$aGPX = unserialize($rit->gpx_route);
foreach ( $aGPX as &$file){
$url = wp_get_attachment_url( $file );
$name = get_the_title($file);
$sGPX = "<a href='". $url ."'>" . $name . ".gpx</a>\n\n";
$message .= "<tr><td><strong>GPX:</strong> </td><td>" . $sGPX . "</td></tr>";
//echo $url;
$parsed = parse_url( wp_get_attachment_url( $file ) );
$urlx = dirname( $parsed [ 'path' ] ) . '/' . rawurlencode( basename( $parsed[ 'path' ] ) );
$urlx = substr($urlx, strpos($urlx, '/', 1));
array_push($attachments, WP_CONTENT_DIR . $urlx );
//$message .= "<tr><td><strong>GPX attachment:</strong> </td><td>" . WP_CONTENT_DIR . $urlx . "</td></tr>";
}
}
$message .= "</table>";
$message .= "</body></html>";
add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
foreach( $maillist as $recepient ){
if(wp_mail( $recepient, $subject, $message, $headers, $attachments )){
$checkMail = TRUE;
}else{
$checkMail = FALSE;
}
}
if($checkMail){
$wpdb->update('wp_custom_ritten', array('notif_mail'=>$today), array('id'=>$rit->ID));
}
// Reset content-type to avoid conflicts -- https://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
}
function wpdocs_set_html_mail_content_type() {
return 'text/html';
}

meta_key and meta_value put it in an array WP_User_Query in Wordpress

I want to display some information about the user using function new WP_User_Query
"umeta_id": "606",
"user_id": "18",
"meta_key": "facebook_login_data",
"meta_value": "a:24:{s:10:\"identifier\";s:16:\"XXXXX\";s:10:\"webSiteURL\";s:0:\"\";s:10:\"profileURL\";s:61:\"https://www.facebook.com/app_scoped_user_id/XXXXXX/\";s:8:\"photoURL\";s:72:\"https://graph.facebook.com/XXXXXX/picture?width=150&height=150\";s:11:\"displayName\";s:14:\"Osadchiy Yuriy\";s:11:\"description\";s:0:\"\";s:9:\"firstName\";s:5:\"Yuriy\";s:8:\"lastName\";s:8:\"Osadchiy\";s:6:\"gender\";s:4:\"male\";s:8:\"language\";s:5:\"ru_RU\";s:3:\"age\";N;s:8:\"birthDay\";i:25;s:10:\"birthMonth\";i:11;s:9:\"birthYear\";i:1988;s:5:\"email\";s:15:\"XXXX.com\";s:13:\"emailVerified\";s:15:\"XXXX.com\";s:5:\"phone\";N;s:7:\"address\";N;s:7:\"country\";N;s:6:\"region\";s:0:\"\";s:4:\"city\";N;s:3:\"zip\";N;s:9:\"job_title\";N;s:17:\"organization_name\";N;}"
How can I get the avatar of the user photoURL
I use this code:
$args = array(
'role' => 'Customer'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->ID . '</p>';
echo '<p>' . $user->display_name . '</p>';
echo '<p>' . $user->user_email . '</p>';
echo '<p>' . $user->billing_phone . '</p>';
echo '<p>' . $user->billing_first_name . '</p>';
echo '<p>' . $user->billing_last_name . '</p>';
echo '<p>' . $user->facebook_login_data
}
}
I would be grateful for the help.
My decision:
$args = array(
'role' => 'Customer'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->ID . '</p>';
echo '<p>' . $user->display_name . '</p>';
echo '<p>' . $user->user_email . '</p>';
echo '<p>' . $user->billing_phone . '</p>';
echo '<p>' . $user->billing_first_name . '</p>';
echo '<p>' . $user->billing_last_name . '</p>';
if ($user->facebook_login_data){
echo '<p>' . $user->facebook_login_data["profileURL"] .'</p>';
}
}
}

Escaping untrusted data in php for my wordPress theme

I have got a comment from someone superior to me who wants me to escape some comment in two php snippets which I have posted below. The problem is I don't know how to do that. Can anyone help me by modifying the snippets.
Comment I got:
Comment #1:
Validate and/or sanitize untrusted data before entering into the database. All untrusted data should be escaped before output.
Code Snippet #1:
<?php
if ( get_header_image() && !('blank' == get_header_textcolor()) ) {
echo '<div class="site-branding header-background-image" style="background-image: url(' . get_header_image() . ')">';
} else {
echo '<div class="site-branding">';
}
?>
Code Snippet #2:
<?php
printf(
/* translators: %1$s = text link: sangeet, URL: http://wordpress.org/themes/sangeet/, %2$s = text link: Kiran Kumar Dash, URL: https://twitter.com/TheKiranDash */
__( 'Theme: %1$s by %2$s', 'sangeet' ),
'' . esc_attr( 'sangeet', 'sangeet' ) . '',
'' . esc_attr__( 'Kiran Kumar Dash', 'sangeet' ) . '' );
?>
Comment #2:
esc all get_permalink() in content.php
Code snippet #3
<?php
if ( !is_single() ) {
echo '<div class="index-box">';
if ( has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . get_permalink() . '" title="' . __('Read ', 'sangeet') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
My approach:
I used esc_url to esc the get_permalink() in the snippet. Shall I use esc_all? Or esc_url is just fine.
<?php
if ( !is_single() ) {
echo '<div class="index-box">';
if ( has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . esc_url(get_permalink()) . '" title="' . __('Read ', 'sangeet') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
As requested:
PS. I didnt write this, only advanced it.
Usage:
$user = sanctify($_GET['user']);
Function:
function sanctify($data){
// Fix &entity\n;
$data = str_replace(array('&','<','>'), array('&amp;','&lt;','&gt;'), $data);
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
$data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);
// Remove javascript: and vbscript: protocols
$data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);
// Remove namespaced elements (we do not need them)
$data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);
$data = str_replace('"','',str_replace("'","",$data));
do{
// Remove really unwanted tags
$old_data = $data;
$data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
}
while ($old_data !== $data);
// we are done...
return $data;
}

Comparing post publish date to "one month ago" unix time

I'm stuck on a problem and thought maybe I'm missing something basic. I'm making a function in WordPress that lists posts with a publish date of less than one month from "now". I do this by subtracting 2419200 (30 days) from the current time, using time(). Then I get posts which are > 30 days ago. However, my function interprets this range incorrectly and only serves up posts which are within a range of 2 weeks!
There is a lot going on in this function in addition to this problem, so I apologize, but follow the variables $range, $limit, and $publish_date for this issue. Notice that for testing purposes I set my variable to "monthly" manually at the start.
function send_archive() {
$range = 'monthly';
$now = time();
switch ($range) {
case 'monthly' :
$limit = ($now - 2419200); // 30 days
break;
case 'weekly' :
$limit = ($now - 604800);
break;
case 'daily' :
$limit = ($now - 86400);
break;
}
$post_types = get_post_types();
$posts = get_posts(array('post_type' => $post_types));
$all_users = get_users();
foreach ($all_users as $user) {
$excluded = excluded_admin( $user->user_login );
echo 'excluded is: ' . $excluded . '<br />';
echo 'user_login is: ' . $user->user_login . '<br />';
if ( $excluded != 'excluded' ) {
$frequency = get_user_meta($user->ID, 'iw_notify_frequency', true);
echo 'frequency is: ' . $frequency . '<br />';
echo 'Range is: ' . $range . '<br />';
echo 'Limit is: ' . date('D, d M Y H:i:s',$limit) . '<br />';
$posts_in_range = '';
$counter = 0;
$to = '';
$subject = '';
$headers = '';
if ($frequency == $range) {
$posts_in_range = get_option('iw-notify-greeting');
$posts_in_range .= '<p>' . strtoupper($range) . ' digest of posts from the City of Mukilteo</p>';
foreach ($posts as $post) {
$published_date = strtotime( $post->post_date_gmt );
$posts_in_range .= 'published_date: ' . $published_date. '<br />';
$posts_in_range .= 'limit: ' . $limit . '<br />';
$posts_in_range .= 'title: ' . $post->post_title . '<br />';
if ($published_date > $limit ) {
$match = has_user_selected_terms($user->ID, $post->ID);
if ($match != '') {
$headers = 'Content-type: text/html;charset=utf-8' . "\r\n";
$posts_in_range .= $post->post_title;
$posts_in_range .= ' - published ' . date('M, d', $published_date) . '<br />';
$posts_in_range .= $post->post_excerpt . '<br />';
$posts_in_range .= get_permalink($post->ID);
$posts_in_range .= '<br /><br />';
$counter++;
}
}
}
}
$posts_in_range .= get_option('iw-notify-unsubscribe') . '<br />----<br />';
if ( $counter != 0 ) {
$to = $user->user_email;
$subject = ucfirst($range) . ' archive from the City of Mukilteo';
$headers = 'Content-type: text/html;charset=utf-8';
$headers .= 'From: ' . get_option('from-name') . '<' . get_option('from-email') . '>' . "\r\n";
$content = $posts_in_range;
wp_mail($to, $subject, $content, $headers);
echo "Email sent to " . $user->display_name . '<br />';
echo $to . '<br />';
echo $subject .'<br />';
echo $headers . '<br />';
echo $posts_in_range . '<br />';
//echo $user->display_name . '<br />';
//echo $posts_in_range;
}
}
}
}
Instead of going all over your posts you can run a query using a date range
$args = array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => '1 month ago',
)
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
//DO YOUR STUFF WITH THE RESULTS
This query should return the post created in the last month.
More info about Date Parameters

How to upload file to custom folder and send attachment to email in wordpress

I have a some trouble with understanding in uploading and sending attachment.
For example:
$zip_code = $_POST['zip_code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$attachments = '';
if (!empty($_FILES['attachment']['tmp_name'])) {
$path = $_FILES['attachment']['name'];
if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}
$subject = 'You have message from ' . get_bloginfo('name');
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Zip Code:</b></td><td>' . $zip_code . '</td></tr>';
$message .= '<tr><td width="150px"><b>Phone:</b></td><td>' . $phone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Attachments:</b></td><td>' . $attachments . '</td></tr>';
$message .= '</table>';
//php mailer variables
$to = get_option('admin_email');
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $attachments . "\"\n\n";
$sent = wp_mail($to, $subject, $message, $headers, $attachments);
This code work not correctly. Email and attachment sending correctly, but file upload in root directory. And when I upload the same files, new file overwrite old file. I don't understand how I can add some hash tag to file name.
Maybe U can help me? Any ideas or where I can read how to resolve this issue.
Thanks!
What is not working? To help you find out what is not working, try this:
$zip_code = $_POST['zip_code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$attachments = '';
if (!empty($_FILES['attachment']['tmp_name'])) {
$path = $_FILES['attachment']['name'];
if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}
$subject = 'You have message from ' . get_bloginfo('name');
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Zip Code:</b></td><td>' . $zip_code . '</td></tr>';
$message .= '<tr><td width="150px"><b>Phone:</b></td><td>' . $phone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Attachments:</b></td><td>' . $attachments . '</td></tr>';
$message .= '</table>';
//php mailer variables
$to = get_option('admin_email');
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $attachments . "\"\n\n";
echo 'Headers: . $headers . '<br>/r/n';
echo 'To: ' . $to .'<br>/r/n';
echo 'Subject: ' . $subject . '<br>/r/n';
echo 'Message: ' . $message . '<br>/r/n';
echo 'Image: <img src="' . $attachments .'" />';
// $sent = wp_mail($to, $subject, $message, $headers, $attachments);
You'll be able to see what is missing if there is anything missing, and you'll be able to rectify your code this way.

Resources