diff --git a/bmlt-meeting-list.php b/bmlt-meeting-list.php index caacaf9..a671fc2 100644 --- a/bmlt-meeting-list.php +++ b/bmlt-meeting-list.php @@ -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); @@ -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(); @@ -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) { @@ -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']; @@ -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); @@ -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); @@ -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(); @@ -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(), @@ -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 = [ @@ -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 = [ @@ -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']); @@ -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) { diff --git a/composer.json b/composer.json index 4c4959a..13d3813 100644 --- a/composer.json +++ b/composer.json @@ -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.*" diff --git a/composer.lock b/composer.lock index 1b3e9d5..744260e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,89 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f72ff6c9db3f69402e7c06a485f7e5bc", + "content-hash": "346c81652c44ad94b4fef1668a17a791", "packages": [ + { + "name": "monolog/monolog", + "version": "2.1.0", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "38914429aac460e8e4616c8cb486ecb40ec90bb1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/38914429aac460e8e4616c8cb486ecb40ec90bb1", + "reference": "38914429aac460e8e4616c8cb486ecb40ec90bb1", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "psr/log": "^1.0.1" + }, + "provide": { + "psr/log-implementation": "1.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^2.4.9 || ^3.0", + "doctrine/couchdb": "~1.0@dev", + "elasticsearch/elasticsearch": "^6.0", + "graylog2/gelf-php": "^1.4.2", + "php-amqplib/php-amqplib": "~2.4", + "php-console/php-console": "^3.1.3", + "php-parallel-lint/php-parallel-lint": "^1.0", + "phpspec/prophecy": "^1.6.1", + "phpunit/phpunit": "^8.5", + "predis/predis": "^1.1", + "rollbar/rollbar": "^1.3", + "ruflin/elastica": ">=0.90 <3.0", + "swiftmailer/swiftmailer": "^5.3|^6.0" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-mbstring": "Allow to work properly with unicode symbols", + "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)", + "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", + "php-console/php-console": "Allow sending log messages to Google Chrome", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "http://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "time": "2020-05-22T08:12:19+00:00" + }, { "name": "mpdf/mpdf", "version": "v8.0.1", diff --git a/readme.txt b/readme.txt index 1d1a7aa..754911a 100644 --- a/readme.txt +++ b/readme.txt @@ -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. @@ -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