Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Logging - turn off output buffering #147

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 56 additions & 17 deletions bmlt-meeting-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
Description: Maintains and generates a PDF Meeting List from BMLT.
Author: bmlt-enabled
Author URI: https://bmlt.app
Version: 2.4.2
Version: 2.5.0
*/
/* Disallow direct access to the plugin file */
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Mpdf\Mpdf;
use function DeepCopy\deep_copy;
error_reporting(1);
Expand All @@ -21,6 +23,7 @@
class Bread {
var $lang = '';
var $mpdf = '';
var $logger = false;
var $meeting_count = 0;
var $formats_used = '';
var $formats_by_key = array();
Expand Down Expand Up @@ -86,7 +89,10 @@ class Bread {
var $maxSetting = 1;
var $loaded_setting = 1;
var $authors_safe = array();

function log($level, $message) {
if ($this->logger==false) return;
$this->logger->log($level, $message);
}
function loadAllSettings() {
$this->allSettings = get_option( Bread::SETTINGS );
if ($this->allSettings === false) {
Expand Down Expand Up @@ -736,10 +742,30 @@ function bmlt_meeting_list($atts = null, $content = null) {
}
$mpdf_init_options['restrictColorSpace'] = $this->options['colorspace'];
$mpdf_init_options = array_merge($mpdf_init_options, $page_type_settings);
ob_clean();
ob_end_clean();
$this->mpdf = new mPDF($mpdf_init_options);
$this->mpdf->setAutoBottomMargin = 'pad';

$this->mpdf->setAutoBottomMargin = 'pad';

if (isset($_GET['log-level'])) {
$level = Logger::DEBUG;
switch ($_GET['log-level']) {
case 'ERROR':
$level = Logger::ERROR;
break;
default:
break;
}
// create a log channel
$this->logger = new Logger('Bread');
$temp_dir = get_temp_dir();
if (isset($_GET['log-location'])) {
$temp_dir = ABSPATH.$_GET['log-location'].'/';
}
$this->logger->pushHandler(new StreamHandler($temp_dir.'mpdf.log', $level));
$this->mpdf->setLogger($this->logger);
$this->mpdf->debug = true;
$this->log(Logger::INFO,"Begin Logging: current-meeting-list=".$_REQUEST['current-meeting-list']);
}
// TODO: Adding a page number really could just be an option or tag.
if ( $this->options['page_fold'] == 'half' || $this->options['page_fold'] == 'full' ) {
$page_string = $this->translate[$this->options['weekday_language']]['PAGE'];
Expand Down Expand Up @@ -797,7 +823,7 @@ function bmlt_meeting_list($atts = null, $content = null) {
]);

$this->mpdf_column->WriteHTML($html);
$FilePath = ABSPATH . "column_tmp_".strtolower( date ( "njYghis" ) ).".pdf";
$FilePath = get_temp_dir() . "column_tmp_".strtolower( date ( "njYghis" ) ).".pdf";
$this->mpdf_column->Output($FilePath,'F');
$pagecount = $this->mpdf->SetSourceFile($FilePath);
$tplId = $this->mpdf->importPage($pagecount);
Expand Down Expand Up @@ -835,6 +861,7 @@ function bmlt_meeting_list($atts = null, $content = null) {
"[service_body_5]" => strtoupper($this->options['service_body_5']),

);
$this->log(Logger::INFO,"Begin Logging: current-meeting-list=".$_REQUEST['current-meeting-list']);
$this->unique_areas = $this->get_areas();
// Extensions
$this->section_shortcodes = apply_filters("Bread_Section_Shortcodes",$this->section_shortcodes, $this->unique_areas, $this->formats_used);
Expand Down Expand Up @@ -975,9 +1002,9 @@ function bmlt_meeting_list($atts = null, $content = null) {
$this->mpdf->SetDefaultBodyCSS('font-size', $this->options['content_font_size'] . 'pt');
$this->mpdf->SetDefaultBodyCSS('line-height', $this->options['content_line_height']);
$this->upgradeHeaderData();

$this->log(Logger::INFO,"Before writeMeetings");
$this->writeMeetings($result_meetings,$this->options['meeting_template_content'],$this->options['weekday_language'],$this->options['include_asm']==0 ? -1 : 0);

$this->log(Logger::INFO,"After writeMeetings");
if ( $this->options['page_fold'] !== 'half' && $this->options['page_fold'] !== 'full' ) {
$this->write_custom_section();
$this->write_front_page();
Expand All @@ -986,11 +1013,13 @@ function bmlt_meeting_list($atts = null, $content = null) {
$this->write_last_page();
}
}
$this->log(Logger::INFO,"Generation complete...need to repage now.");
$this->mpdf->SetDisplayMode('fullpage','two');
$upload_dir = wp_upload_dir();
$FilePath = ABSPATH . "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
$FilePath = get_temp_dir(). "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
if ( $this->options['page_fold'] == 'half' ) {
$this->log(Logger::INFO,"Before Output half-fold");
$this->mpdf->Output($FilePath,'F');
$this->log(Logger::INFO,"After Output half-fold");
$mpdfOptions = [
'mode' => $mode,
'tempDir' => get_temp_dir(),
Expand Down Expand Up @@ -1031,8 +1060,9 @@ function bmlt_meeting_list($atts = null, $content = null) {
}
}
unlink($FilePath);
$FilePath = ABSPATH . "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
$FilePath = get_temp_dir() . "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
$this->mpdf = $this->mpdftmp;
if ($this->logger) $this->mpdf->setLogger($this->logger);
} else if ($this->options['page_fold'] == 'full' && $this->options['booklet_pages']) {
$this->mpdf->Output($FilePath,'F');
$mpdfOptions = [
Expand Down Expand Up @@ -1065,8 +1095,9 @@ function bmlt_meeting_list($atts = null, $content = null) {
$tplIdx = $this->mpdftmp->ImportPage($np);
$this->mpdftmp->UseTemplate($tplIdx);
unlink($FilePath);
$FilePath = ABSPATH . "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
$FilePath = get_temp_dir() . "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
$this->mpdf = $this->mpdftmp;
if ($this->logger) $this->mpdf->setLogger($this->logger);
} else if ($this->options['page_fold'] == 'flyer' ) {
$this->mpdf->Output($FilePath,'F');
$mpdfOptions = [
Expand Down Expand Up @@ -1102,9 +1133,11 @@ function bmlt_meeting_list($atts = null, $content = null) {
$this->mpdftmp->UseTemplate($tplIdx,$fw+$fw,0);
$this->addColumnSeparators($oh);
unlink($FilePath);
$FilePath = ABSPATH . "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
$FilePath = get_temp_dir() . "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
$this->mpdf = $this->mpdftmp;
if ($this->logger) $this->mpdf->setLogger($this->logger);
}
$this->log(Logger::INFO,"Repaging done.");
if ( $this->options['include_protection'] == 1 ) {
// 'copy','print','modify','annot-forms','fill-forms','extract','assemble','print-highres'
$this->mpdf->SetProtection(array('copy','print','print-highres'), '', $this->options['protection_password']);
Expand All @@ -1114,10 +1147,16 @@ function bmlt_meeting_list($atts = null, $content = null) {
$content = bin2hex($content);
$transient_key = 'bmlt_ml_'.md5($this->options['root_server'].$services);
set_transient( $transient_key, $content, intval($this->options['cache_time']) * HOUR_IN_SECONDS );
}
$FilePath = "current_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";

$this->mpdf->Output($FilePath,'I');
}
$FilePath = "final_meeting_list_".strtolower( date ( "njYghis" ) ).".pdf";
$this->log(Logger::INFO,"FilePath=$FilePath");
$this->mpdf->debug = true;
try {
$this->mpdf->Output($FilePath,'I');
} catch (MpdfException $e) { // Note: safer fully qualified exception name used for catch
$this->log(Logger::INFO,$e->getMessage());
}
$this->log(Logger::INFO,"Success!");
exit;
}
function orderByWeekdayStart(&$result_meetings) {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"require": {
"mpdf/mpdf": "8.0.1",
"mpdf/qrcode": "^1.0",
"myclabs/deep-copy": "1.8.1"
"myclabs/deep-copy": "1.8.1",
"monolog/monolog": "^2.1"
},
"require-dev": {
"squizlabs/php_codesniffer": "3.*"
Expand Down
83 changes: 82 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: meeting list, bmlt, narcotics anonymous, na
Requires at least: 4.0
Requires PHP: 5.6
Tested up to: 5.4.0
Stable tag: 2.4.2
Stable tag: 2.5.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
A web-based tool that creates, maintains and generates a PDF meeting list from BMLT.
Expand Down Expand Up @@ -53,6 +53,11 @@ Follow all these steps, keep in mind that once you start using bread, it's not g
- Read here for more information: https://github.com/bmlt-enabled/bread/blob/unstable/contribute.md

== Changelog ==
= 2.5.0 =
* Added logging during PDF generation https://na-site.org?current-meeting-list=1&log-level=DEBUG
Using Monolog.
* Prevent erroroneous bytes in output buffer from corrupting PDF file

= 2.4.0 =
* Added QR Code generation from shortcode
* Made User-Agent confifurable
Expand Down