Download Monitor - Version 4.6.3

Version Description

  • 03.10.2022 = Fixed: Double sized downloads ( #1059 ) Fixed: Failsafe logging for unsuccessful database upgrade ( #1060 ) Fixed: Password protected Downloads could not be opened ( #1055 )
Download this release

Release Info

Developer raldea89
Plugin Icon 128x128 Download Monitor
Version 4.6.3
Comparing to
See all releases

Code changes from version 4.6.2 to 4.6.3

assets/js/database-upgrader.js CHANGED
@@ -27,7 +27,7 @@
27
  $(document).on('click', 'button#dlm-upgrade-db,a.dlm-db-upgrade-link', function (e) {
28
  e.preventDefault();
29
  $(this).prop('disabled', true);
30
- $(this).parents('.dlm-upgrade-db-notice').addClass('started');
31
 
32
  const opts = {
33
  url: dlmDBUpgrader.ajax,
27
  $(document).on('click', 'button#dlm-upgrade-db,a.dlm-db-upgrade-link', function (e) {
28
  e.preventDefault();
29
  $(this).prop('disabled', true);
30
+ $('body').find('.dlm-upgrade-db-notice').addClass('started');
31
 
32
  const opts = {
33
  url: dlmDBUpgrader.ajax,
assets/js/database-upgrader.min.js CHANGED
@@ -1 +1 @@
1
- !function(a){"use strict";var t={counts:0,upgraderResumeOffset:0,completed:0,ajaxRequests:[],ajaxStarted:1,ajaxTimeout:null,ajax:ajaxurl,entries:0,requestsNumber:0,init:function(){a(document).on("click","button#dlm-upgrade-db,a.dlm-db-upgrade-link",function(e){e.preventDefault(),a(this).prop("disabled",!0),a(this).parents(".dlm-upgrade-db-notice").addClass("started");e={url:t.ajax,type:"post",async:!0,cache:!1,dataType:"json",data:{action:"dlm_db_log_entries",nonce:dlm_upgrader.nonce},success:function(e){"0"!==e&&"0"!==e.entries?(t.entries=e.entries,e.offset,0!==parseInt(e.offset)&&(t.upgraderResumeOffset=parseInt(e.offset),e={url:t.ajax,type:"post",async:!0,cache:!1,dataType:"json",data:{action:"dlm_upgrade_db_clear_offset",nonce:dlm_upgrader.nonce,offset:parseInt(e.offset)},success:function(){console.log("previous offset cleared")}},a.ajax(e))):t.entries=0,t.processAjax(),s.init(Math.ceil(1e6*t.upgraderResumeOffset/t.entries))}};a.ajax(e)})},processAjax:function(){if(0<t.entries-1e4*t.upgraderResumeOffset){t.requestsNumber=1e4<=t.entries?parseInt(Math.ceil(t.entries/1e4)):1;for(let e=0;e<=t.requestsNumber-t.upgraderResumeOffset;e++){var a={url:t.ajax,type:"post",async:!0,cache:!1,dataType:"json",data:{action:"dlm_upgrade_db",nonce:dlm_upgrader.nonce,offset:t.counts+t.upgraderResumeOffset},success:function(){t.ajaxStarted=t.ajaxStarted-1,t.completed=t.completed+1,s.progressHandler(100*(t.completed+t.upgraderResumeOffset)/t.requestsNumber)}};t.counts+=1,t.ajaxRequests.push(a)}}var e={url:t.ajax,type:"post",async:!0,cache:!1,dataType:"json",data:{action:"dlm_alter_download_log",nonce:dlm_upgrader.nonce},success:function(){t.ajaxStarted=t.ajaxStarted-1,t.completed=t.completed+1,0<t.entries?s.progressHandler(100*(t.completed+t.upgraderResumeOffset)/t.requestsNumber):s.progressHandler(100*(t.completed+t.upgraderResumeOffset))}};t.ajaxRequests.push(e),t.runAjaxs()},runAjaxs:function(){for(var e;t.ajaxStarted<2&&0<t.ajaxRequests.length;)t.ajaxStarted=t.ajaxStarted+1,e=t.ajaxRequests.shift(),a.ajax(e);0<t.ajaxRequests.length&&(t.ajaxTimeout=setTimeout(function(){console.log("Delayed 1s"),t.runAjaxs()},1e3))}};const s={el:{},label:{},init:(e=0)=>{s.el=jQuery("#dlm_progress-bar"),s.label=jQuery("#dlm_progress-bar").parent().find(".dlm-progress-label"),s.label.text(Math.ceil(e)+"%"),s.el.progressbar({value:e,change:()=>{s.label.text(s.el.progressbar("value")+"%")},complete:()=>{setTimeout(function(){s.label.text("Complete! Page will be reloaded in 5 seconds."),s.el.addClass("completed"),setTimeout(function(){window.location.reload(!1)},5e3)},3e3)}})},progressHandler:e=>{s.el.progressbar("value",Math.ceil(e))}};a(document).ready(function(){t.init()})}(jQuery);
1
+ !function(a){"use strict";var t={counts:0,upgraderResumeOffset:0,completed:0,ajaxRequests:[],ajaxStarted:1,ajaxTimeout:null,ajax:ajaxurl,entries:0,requestsNumber:0,init:function(){a(document).on("click","button#dlm-upgrade-db,a.dlm-db-upgrade-link",function(e){e.preventDefault(),a(this).prop("disabled",!0),a("body").find(".dlm-upgrade-db-notice").addClass("started");e={url:t.ajax,type:"post",async:!0,cache:!1,dataType:"json",data:{action:"dlm_db_log_entries",nonce:dlm_upgrader.nonce},success:function(e){"0"!==e&&"0"!==e.entries?(t.entries=e.entries,e.offset,0!==parseInt(e.offset)&&(t.upgraderResumeOffset=parseInt(e.offset),e={url:t.ajax,type:"post",async:!0,cache:!1,dataType:"json",data:{action:"dlm_upgrade_db_clear_offset",nonce:dlm_upgrader.nonce,offset:parseInt(e.offset)},success:function(){console.log("previous offset cleared")}},a.ajax(e))):t.entries=0,t.processAjax(),s.init(Math.ceil(1e6*t.upgraderResumeOffset/t.entries))}};a.ajax(e)})},processAjax:function(){if(0<t.entries-1e4*t.upgraderResumeOffset){t.requestsNumber=1e4<=t.entries?parseInt(Math.ceil(t.entries/1e4)):1;for(let e=0;e<=t.requestsNumber-t.upgraderResumeOffset;e++){var a={url:t.ajax,type:"post",async:!0,cache:!1,dataType:"json",data:{action:"dlm_upgrade_db",nonce:dlm_upgrader.nonce,offset:t.counts+t.upgraderResumeOffset},success:function(){t.ajaxStarted=t.ajaxStarted-1,t.completed=t.completed+1,s.progressHandler(100*(t.completed+t.upgraderResumeOffset)/t.requestsNumber)}};t.counts+=1,t.ajaxRequests.push(a)}}var e={url:t.ajax,type:"post",async:!0,cache:!1,dataType:"json",data:{action:"dlm_alter_download_log",nonce:dlm_upgrader.nonce},success:function(){t.ajaxStarted=t.ajaxStarted-1,t.completed=t.completed+1,0<t.entries?s.progressHandler(100*(t.completed+t.upgraderResumeOffset)/t.requestsNumber):s.progressHandler(100*(t.completed+t.upgraderResumeOffset))}};t.ajaxRequests.push(e),t.runAjaxs()},runAjaxs:function(){for(var e;t.ajaxStarted<2&&0<t.ajaxRequests.length;)t.ajaxStarted=t.ajaxStarted+1,e=t.ajaxRequests.shift(),a.ajax(e);0<t.ajaxRequests.length&&(t.ajaxTimeout=setTimeout(function(){console.log("Delayed 1s"),t.runAjaxs()},1e3))}};const s={el:{},label:{},init:(e=0)=>{s.el=jQuery("#dlm_progress-bar"),s.label=jQuery("#dlm_progress-bar").parent().find(".dlm-progress-label"),s.label.text(Math.ceil(e)+"%"),s.el.progressbar({value:e,change:()=>{s.label.text(s.el.progressbar("value")+"%")},complete:()=>{setTimeout(function(){s.label.text("Complete! Page will be reloaded in 5 seconds."),s.el.addClass("completed"),setTimeout(function(){window.location.reload(!1)},5e3)},3e3)}})},progressHandler:e=>{s.el.progressbar("value",Math.ceil(e))}};a(document).ready(function(){t.init()})}(jQuery);
changelog.txt CHANGED
@@ -1,3 +1,8 @@
 
 
 
 
 
1
  = 4.6.2 - 29.09.2022 =
2
  Fixed: WP VIP servers bug ( [#1052](https://github.com/WPChill/download-monitor/issues/1052) ) thanks to @jesusfreak3
3
  Fixed: Progress loader for external sources ( [#1051](https://github.com/WPChill/download-monitor/issues/1051) )
1
+ = 4.6.3 - 03.10.2022 =
2
+ Fixed: Double sized downloads ( [#1059](https://github.com/WPChill/download-monitor/issues/1059) )
3
+ Fixed: Failsafe logging for unsuccessful database upgrade ( [#1060](https://github.com/WPChill/download-monitor/issues/1060) )
4
+ Fixed: Password protected Downloads could not be opened ( [#1055](https://github.com/WPChill/download-monitor/issues/1055) )
5
+
6
  = 4.6.2 - 29.09.2022 =
7
  Fixed: WP VIP servers bug ( [#1052](https://github.com/WPChill/download-monitor/issues/1052) ) thanks to @jesusfreak3
8
  Fixed: Progress loader for external sources ( [#1051](https://github.com/WPChill/download-monitor/issues/1051) )
download-monitor.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Download Monitor
4
  Plugin URI: https://www.download-monitor.com
5
  Description: A full solution for managing and selling downloadable files, monitoring downloads and outputting download links and file information on your WordPress powered site.
6
- Version: 4.6.2
7
  Author: WPChill
8
  Author URI: https://wpchill.com
9
  Requires at least: 5.4
@@ -34,7 +34,7 @@ if ( ! defined( 'ABSPATH' ) ) {
34
 
35
  // Define DLM Version
36
 
37
- define( 'DLM_VERSION', '4.6.2' );
38
  define( 'DLM_UPGRADER_VERSION', '4.6.0' );
39
 
40
  // Define DLM FILE
3
  Plugin Name: Download Monitor
4
  Plugin URI: https://www.download-monitor.com
5
  Description: A full solution for managing and selling downloadable files, monitoring downloads and outputting download links and file information on your WordPress powered site.
6
+ Version: 4.6.3
7
  Author: WPChill
8
  Author URI: https://wpchill.com
9
  Requires at least: 5.4
34
 
35
  // Define DLM Version
36
 
37
+ define( 'DLM_VERSION', '4.6.3' );
38
  define( 'DLM_UPGRADER_VERSION', '4.6.0' );
39
 
40
  // Define DLM FILE
includes/backwards-compatibility/class-dlm-backwards-compatibility.php CHANGED
@@ -151,6 +151,7 @@ class DLM_Backwards_Compatibility {
151
 
152
  $this->filters = $filters;
153
  add_filter( 'dlm_admin_sort_columns', array( $this, 'query_args_download_count_compatibility' ), 60 );
 
154
  add_filter( 'posts_join', array( $this, 'join_download_count_compatibility' ) );
155
  add_filter( 'posts_where', array( $this, 'where_download_count_compatibility' ) );
156
  add_filter( 'posts_groupby', array( $this, 'groupby_download_count_compatibility' ) );
151
 
152
  $this->filters = $filters;
153
  add_filter( 'dlm_admin_sort_columns', array( $this, 'query_args_download_count_compatibility' ), 60 );
154
+ add_filter( 'dlm_query_args_filter', array( $this, 'query_args_download_count_compatibility' ), 60 );
155
  add_filter( 'posts_join', array( $this, 'join_download_count_compatibility' ) );
156
  add_filter( 'posts_where', array( $this, 'where_download_count_compatibility' ) );
157
  add_filter( 'posts_groupby', array( $this, 'groupby_download_count_compatibility' ) );
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: wpchill, silkalns, barrykooij, mikejolley
3
  Tags: download manager, document management, file manager, digital store, ecommerce, document management plugin, download monitor, download counter, password protection, download protection, password, protect downloads, tracker, sell, shop, ecommerce, paypal
4
  Requires at least: 5.4
5
  Tested up to: 6.0
6
- Stable tag: 4.6.2
7
  License: GPLv3
8
  Text Domain: -
9
  Requires PHP: 5.6
@@ -121,6 +121,11 @@ More documentation can be found in our [Knowledge Base](https://www.download-mon
121
 
122
  == Changelog ==
123
 
 
 
 
 
 
124
  = 4.6.2 - 29.09.2022 =
125
  Fixed: WP VIP servers bug ( [#1052](https://github.com/WPChill/download-monitor/issues/1052) ) thanks to @jesusfreak3
126
  Fixed: Progress loader for external sources ( [#1051](https://github.com/WPChill/download-monitor/issues/1051) )
3
  Tags: download manager, document management, file manager, digital store, ecommerce, document management plugin, download monitor, download counter, password protection, download protection, password, protect downloads, tracker, sell, shop, ecommerce, paypal
4
  Requires at least: 5.4
5
  Tested up to: 6.0
6
+ Stable tag: 4.6.3
7
  License: GPLv3
8
  Text Domain: -
9
  Requires PHP: 5.6
121
 
122
  == Changelog ==
123
 
124
+ = 4.6.3 - 03.10.2022 =
125
+ Fixed: Double sized downloads ( [#1059](https://github.com/WPChill/download-monitor/issues/1059) )
126
+ Fixed: Failsafe logging for unsuccessful database upgrade ( [#1060](https://github.com/WPChill/download-monitor/issues/1060) )
127
+ Fixed: Password protected Downloads could not be opened ( [#1055](https://github.com/WPChill/download-monitor/issues/1055) )
128
+
129
  = 4.6.2 - 29.09.2022 =
130
  Fixed: WP VIP servers bug ( [#1052](https://github.com/WPChill/download-monitor/issues/1052) ) thanks to @jesusfreak3
131
  Fixed: Progress loader for external sources ( [#1051](https://github.com/WPChill/download-monitor/issues/1051) )
src/Admin/Reports/Page.php CHANGED
@@ -368,7 +368,7 @@ class DLM_Reports_Page {
368
  <div class="wrap">
369
  <hr class="wp-header-end">
370
  <div class="main">
371
- <h3><?php esc_html_e( 'Please upgrade the database in order to further use Download Monitor\'s Reports page.', 'download-monitor' ); ?></h3>
372
  </div>
373
  </div>
374
  <?php
368
  <div class="wrap">
369
  <hr class="wp-header-end">
370
  <div class="main">
371
+ <h3><?php echo __( 'Please <a href="#" class="dlm-db-upgrade-link">upgrade the database</a> in order to further use Download Monitor\'s Reports page.', 'download-monitor' ); ?></h3>
372
  </div>
373
  </div>
374
  <?php
src/DownloadHandler.php CHANGED
@@ -292,7 +292,22 @@ class DLM_Download_Handler {
292
 
293
  // Action on found download
294
  if ( $download->exists() ) {
 
 
 
 
 
 
 
 
 
 
 
295
  if ( post_password_required( $download_id ) ) {
 
 
 
 
296
  wp_die( get_the_password_form( $download_id ), esc_html__( 'Password Required', 'download-monitor' ) );
297
  }
298
 
@@ -321,16 +336,6 @@ class DLM_Download_Handler {
321
  */
322
  private function trigger( $download ) {
323
 
324
- // Check and see if this is an XHR request or a classic request.
325
- if ( isset( $_SERVER['HTTP_DLM_XHR_REQUEST'] ) && 'dlm_XMLHttpRequest' === $_SERVER['HTTP_DLM_XHR_REQUEST'] ) {
326
-
327
- if ( ! isset( $_REQUEST['nonce'] ) ) {
328
- wp_send_json_error( array( 'error' => 'missing_nonce' ) );
329
- }
330
- wp_verify_nonce( $_REQUEST['nonce'], 'dlm_ajax_nonce' );
331
- define( 'DLM_DOING_XHR', true );
332
- }
333
-
334
  // Download is triggered. First thing we do, send no cache headers.
335
  $this->cache_headers();
336
 
@@ -339,11 +344,15 @@ class DLM_Download_Handler {
339
 
340
  /** @var array $file_paths */
341
  $file_paths = $version->get_mirrors();
342
- // Set required headers used by XHR download
343
- $this->set_required_xhr_headers( $download, $version );
 
 
 
 
344
  // Check if we got files in this version.
345
  if ( empty( $file_paths ) ) {
346
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
347
  header( 'DLM-Error: ' . esc_html__( 'No file paths defined.', 'download-monitor' ) );
348
  http_response_code( 404 );
349
  exit;
@@ -358,7 +367,7 @@ class DLM_Download_Handler {
358
 
359
  // Check if we actually got a path.
360
  if ( ! $file_path ) {
361
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
362
  header( 'DLM-Error: ' . esc_html__( 'No file path defined.', 'download-monitor' ) );
363
  http_response_code( 404 );
364
  exit;
@@ -374,7 +383,7 @@ class DLM_Download_Handler {
374
  // The return of the get_secure_path function is an array that consists of the path ( string ), remote file ( bool ) and restriction ( bool ).
375
  // If the path is false it means that the file is restricted, so don't download it or redirect to it.
376
  if ( $restriction ) {
377
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
378
  header( 'DLM-Error: ' . esc_html__( 'Access denied to this file.', 'download-monitor' ) );
379
  http_response_code( 403 );
380
  exit;
@@ -382,16 +391,17 @@ class DLM_Download_Handler {
382
  header( 'Status: 403 Access denied, file not in allowed paths.' );
383
  wp_die( esc_html__( 'Access denied to this file', 'download-monitor' ) . ' <a href="' . esc_url( home_url() ) . '">' . esc_html__( 'Go to homepage &rarr;', 'download-monitor' ) . '</a>', esc_html__( 'Download Error', 'download-monitor' ) );
384
  }
385
-
386
- // Set extra headers for XHR download.
387
- $this->set_extra_xhr_headers( $file_path, $download, $version );
 
388
 
389
  // Check Access.
390
- if ( ! apply_filters( 'dlm_can_download', true, $download, $version, $_REQUEST, ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) ) ) {
391
 
392
  // Check if we need to redirect if visitor don't have access to file.
393
  if ( $redirect = apply_filters( 'dlm_access_denied_redirect', false ) ) {
394
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
395
  header( 'DLM-Redirect: ' . $redirect );
396
  header( 'DLM-No-Access: true' );
397
  exit;
@@ -428,7 +438,7 @@ class DLM_Download_Handler {
428
  $no_access_permalink = add_query_arg( 'version', $download->get_version()->get_version(), $no_access_permalink );
429
  }
430
 
431
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
432
  header( 'DLM-Redirect: ' . $no_access_permalink );
433
  header( 'DLM-No-Access: true' );
434
  exit;
@@ -440,7 +450,7 @@ class DLM_Download_Handler {
440
  }
441
  }
442
 
443
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
444
  header( 'DLM-Error: ' . esc_html__( 'Access denied. You do not have permission to download this file.', 'download-monitor' ) );
445
  exit;
446
  }
@@ -465,7 +475,7 @@ class DLM_Download_Handler {
465
 
466
  // Redirect to the file...
467
  if ( $download->is_redirect_only() || apply_filters( 'dlm_do_not_force', false, $download, $version ) ) {
468
- if ( ! ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) ) {
469
  $this->dlm_logging->log( $download, $version, 'redirect' );
470
  }
471
 
@@ -479,7 +489,7 @@ class DLM_Download_Handler {
479
  $file_path = str_replace( trailingslashit( $correct_path ), site_url( '/', $scheme ), $file_path );
480
  }
481
 
482
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
483
  header( 'DLM-Redirect: ' . $file_path );
484
  exit;
485
  }
@@ -496,7 +506,7 @@ class DLM_Download_Handler {
496
 
497
  if ( '1' === get_option( 'dlm_xsendfile_enabled' ) ) {
498
  if ( function_exists( 'apache_get_modules' ) && in_array( 'mod_xsendfile', apache_get_modules() ) ) {
499
- if ( ! ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) ) {
500
  $this->dlm_logging->log( $download, $version, 'completed' );
501
  }
502
 
@@ -505,7 +515,7 @@ class DLM_Download_Handler {
505
 
506
  } elseif ( stristr( getenv( 'SERVER_SOFTWARE' ), 'lighttpd' ) ) {
507
 
508
- if ( ! ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) ) {
509
  $this->dlm_logging->log( $download, $version, 'completed' );
510
  }
511
 
@@ -557,14 +567,19 @@ class DLM_Download_Handler {
557
  $range = false;
558
  }
559
 
560
- if ( $this->readfile_chunked( $file_path, false, $range ) ) {
561
- if ( ! ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) ) {
 
 
 
 
 
562
  $this->dlm_logging->log( $download, $version, 'completed' );
563
  }
564
- $this->readfile_chunked( $file_path, false, $range );
565
  } elseif ( $remote_file ) {
566
  // Redirect - we can't track if this completes or not.
567
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
568
  header( 'DLM-Redirect: ' . $file_path );
569
  exit;
570
  }
@@ -574,7 +589,7 @@ class DLM_Download_Handler {
574
 
575
  } else {
576
 
577
- if ( defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR ) {
578
  header( 'DLM-Error: ' . esc_html__( 'File not found.', 'download-monitor' ) );
579
  exit;
580
  }
@@ -739,7 +754,7 @@ class DLM_Download_Handler {
739
  $chunksize = 1 * ( 1024 * 1024 );
740
  $buffer = '';
741
  $cnt = 0;
742
- $handle = fopen( $file, 'rb' );
743
 
744
  if ( $handle === false ) {
745
  return false;
@@ -767,4 +782,13 @@ class DLM_Download_Handler {
767
 
768
  return $status;
769
  }
 
 
 
 
 
 
 
 
 
770
  }
292
 
293
  // Action on found download
294
  if ( $download->exists() ) {
295
+
296
+ // Check and see if this is an XHR request or a classic request.
297
+ if ( isset( $_SERVER['HTTP_DLM_XHR_REQUEST'] ) && 'dlm_XMLHttpRequest' === $_SERVER['HTTP_DLM_XHR_REQUEST'] ) {
298
+
299
+ if ( ! isset( $_REQUEST['nonce'] ) ) {
300
+ wp_send_json_error( array( 'error' => 'missing_nonce' ) );
301
+ }
302
+ wp_verify_nonce( $_REQUEST['nonce'], 'dlm_ajax_nonce' );
303
+ define( 'DLM_DOING_XHR', true );
304
+ }
305
+
306
  if ( post_password_required( $download_id ) ) {
307
+ if ( $this->check_for_xhr() ) {
308
+ header( 'DLM-Redirect: ' . $download->get_the_download_link() );
309
+ exit;
310
+ }
311
  wp_die( get_the_password_form( $download_id ), esc_html__( 'Password Required', 'download-monitor' ) );
312
  }
313
 
336
  */
337
  private function trigger( $download ) {
338
 
 
 
 
 
 
 
 
 
 
 
339
  // Download is triggered. First thing we do, send no cache headers.
340
  $this->cache_headers();
341
 
344
 
345
  /** @var array $file_paths */
346
  $file_paths = $version->get_mirrors();
347
+
348
+ if ( $this->check_for_xhr() ) {
349
+ // Set required headers used by XHR download
350
+ $this->set_required_xhr_headers( $download, $version );
351
+ }
352
+
353
  // Check if we got files in this version.
354
  if ( empty( $file_paths ) ) {
355
+ if ( $this->check_for_xhr() ) {
356
  header( 'DLM-Error: ' . esc_html__( 'No file paths defined.', 'download-monitor' ) );
357
  http_response_code( 404 );
358
  exit;
367
 
368
  // Check if we actually got a path.
369
  if ( ! $file_path ) {
370
+ if ( $this->check_for_xhr() ) {
371
  header( 'DLM-Error: ' . esc_html__( 'No file path defined.', 'download-monitor' ) );
372
  http_response_code( 404 );
373
  exit;
383
  // The return of the get_secure_path function is an array that consists of the path ( string ), remote file ( bool ) and restriction ( bool ).
384
  // If the path is false it means that the file is restricted, so don't download it or redirect to it.
385
  if ( $restriction ) {
386
+ if ( $this->check_for_xhr() ) {
387
  header( 'DLM-Error: ' . esc_html__( 'Access denied to this file.', 'download-monitor' ) );
388
  http_response_code( 403 );
389
  exit;
391
  header( 'Status: 403 Access denied, file not in allowed paths.' );
392
  wp_die( esc_html__( 'Access denied to this file', 'download-monitor' ) . ' <a href="' . esc_url( home_url() ) . '">' . esc_html__( 'Go to homepage &rarr;', 'download-monitor' ) . '</a>', esc_html__( 'Download Error', 'download-monitor' ) );
393
  }
394
+ if ( $this->check_for_xhr() ) {
395
+ // Set extra headers for XHR download.
396
+ $this->set_extra_xhr_headers( $file_path, $download, $version );
397
+ }
398
 
399
  // Check Access.
400
+ if ( ! apply_filters( 'dlm_can_download', true, $download, $version, $_REQUEST, $this->check_for_xhr() ) ) {
401
 
402
  // Check if we need to redirect if visitor don't have access to file.
403
  if ( $redirect = apply_filters( 'dlm_access_denied_redirect', false ) ) {
404
+ if ( $this->check_for_xhr() ) {
405
  header( 'DLM-Redirect: ' . $redirect );
406
  header( 'DLM-No-Access: true' );
407
  exit;
438
  $no_access_permalink = add_query_arg( 'version', $download->get_version()->get_version(), $no_access_permalink );
439
  }
440
 
441
+ if ( $this->check_for_xhr() ) {
442
  header( 'DLM-Redirect: ' . $no_access_permalink );
443
  header( 'DLM-No-Access: true' );
444
  exit;
450
  }
451
  }
452
 
453
+ if ( $this->check_for_xhr() ) {
454
  header( 'DLM-Error: ' . esc_html__( 'Access denied. You do not have permission to download this file.', 'download-monitor' ) );
455
  exit;
456
  }
475
 
476
  // Redirect to the file...
477
  if ( $download->is_redirect_only() || apply_filters( 'dlm_do_not_force', false, $download, $version ) ) {
478
+ if ( ! $this->check_for_xhr() ) {
479
  $this->dlm_logging->log( $download, $version, 'redirect' );
480
  }
481
 
489
  $file_path = str_replace( trailingslashit( $correct_path ), site_url( '/', $scheme ), $file_path );
490
  }
491
 
492
+ if ( $this->check_for_xhr() ) {
493
  header( 'DLM-Redirect: ' . $file_path );
494
  exit;
495
  }
506
 
507
  if ( '1' === get_option( 'dlm_xsendfile_enabled' ) ) {
508
  if ( function_exists( 'apache_get_modules' ) && in_array( 'mod_xsendfile', apache_get_modules() ) ) {
509
+ if ( ! $this->check_for_xhr() ) {
510
  $this->dlm_logging->log( $download, $version, 'completed' );
511
  }
512
 
515
 
516
  } elseif ( stristr( getenv( 'SERVER_SOFTWARE' ), 'lighttpd' ) ) {
517
 
518
+ if ( ! $this->check_for_xhr() ) {
519
  $this->dlm_logging->log( $download, $version, 'completed' );
520
  }
521
 
567
  $range = false;
568
  }
569
 
570
+ // Add our file contents to the output buffer, so we can set the cookie for the 60 seconds download.
571
+ ob_start();
572
+ $this->readfile_chunked( $file_path, false, $range );
573
+ $contents = ob_get_clean();
574
+
575
+ if ( $contents ) {
576
+ if ( ! $this->check_for_xhr() ) {
577
  $this->dlm_logging->log( $download, $version, 'completed' );
578
  }
579
+ echo $contents;
580
  } elseif ( $remote_file ) {
581
  // Redirect - we can't track if this completes or not.
582
+ if ( $this->check_for_xhr() ) {
583
  header( 'DLM-Redirect: ' . $file_path );
584
  exit;
585
  }
589
 
590
  } else {
591
 
592
+ if ( $this->check_for_xhr() ) {
593
  header( 'DLM-Error: ' . esc_html__( 'File not found.', 'download-monitor' ) );
594
  exit;
595
  }
754
  $chunksize = 1 * ( 1024 * 1024 );
755
  $buffer = '';
756
  $cnt = 0;
757
+ $handle = fopen( $file, 'r' );
758
 
759
  if ( $handle === false ) {
760
  return false;
782
 
783
  return $status;
784
  }
785
+
786
+ /**
787
+ * Check if this is an XHR request or not
788
+ *
789
+ * @return bool
790
+ */
791
+ private function check_for_xhr(){
792
+ return defined( 'DLM_DOING_XHR' ) && DLM_DOING_XHR;
793
+ }
794
  }
src/Logs/LogItem.php CHANGED
@@ -312,12 +312,16 @@ class DLM_Log_Item {
312
 
313
  $download_date = current_time( 'mysql' );
314
 
315
- $result = $wpdb->insert(
316
- "{$wpdb->download_log}",
 
 
 
 
 
317
  array(
318
  'user_id' => absint( $this->get_user_id() ),
319
  'user_ip' => $this->get_user_ip(),
320
- 'uuid' => $this->get_user_uuid(),
321
  'user_agent' => $this->get_user_agent(),
322
  'download_id' => absint( $this->get_download_id() ),
323
  'version_id' => absint( $this->get_version_id() ),
@@ -325,13 +329,18 @@ class DLM_Log_Item {
325
  'download_date' => sanitize_text_field( $download_date ),
326
  'download_status' => $this->get_download_status(),
327
  'download_status_message' => $this->get_download_status_message(),
328
- 'download_location' => $this->get_current_url(),
329
- 'download_category' => $this->get_download_categories( $this->get_download_id() ),
330
  ),
 
 
 
 
 
 
 
 
331
  array(
332
  '%d',
333
  '%s',
334
- '%s',
335
  '%d',
336
  '%d',
337
  '%s',
@@ -339,9 +348,14 @@ class DLM_Log_Item {
339
  '%s',
340
  '%s',
341
  '%s',
342
- '%s',
343
- '%s'
344
- )
 
 
 
 
 
345
  );
346
 
347
  do_action( 'dlm_increase_download_count', $this );
312
 
313
  $download_date = current_time( 'mysql' );
314
 
315
+ // Add filters for download_log column entries, so in case the upgrader failed we can still log the download.
316
+ /**
317
+ * Filter for the download_log columns
318
+ * @hooked: ( DLM_Logging, log_entries ) Adds uuid, download_category and download_location
319
+ */
320
+ $log_entries = apply_filters(
321
+ 'dlm_log_entries',
322
  array(
323
  'user_id' => absint( $this->get_user_id() ),
324
  'user_ip' => $this->get_user_ip(),
 
325
  'user_agent' => $this->get_user_agent(),
326
  'download_id' => absint( $this->get_download_id() ),
327
  'version_id' => absint( $this->get_version_id() ),
329
  'download_date' => sanitize_text_field( $download_date ),
330
  'download_status' => $this->get_download_status(),
331
  'download_status_message' => $this->get_download_status_message(),
 
 
332
  ),
333
+ $this
334
+ );
335
+ /**
336
+ * Filter for the download_log columns types
337
+ * @hooked: ( DLM_Logging, log_values )
338
+ */
339
+ $log_values = apply_filters(
340
+ 'dlm_log_values',
341
  array(
342
  '%d',
343
  '%s',
 
344
  '%d',
345
  '%d',
346
  '%s',
348
  '%s',
349
  '%s',
350
  '%s',
351
+ ),
352
+ $this
353
+ );
354
+
355
+ $result = $wpdb->insert(
356
+ "{$wpdb->download_log}",
357
+ $log_entries,
358
+ $log_values
359
  );
360
 
361
  do_action( 'dlm_increase_download_count', $this );
src/Logs/Logging.php CHANGED
@@ -26,6 +26,8 @@ class DLM_Logging {
26
  public function __construct() {
27
  add_action( 'wp_ajax_log_dlm_xhr_download', array( $this, 'xhr_log_download' ), 15 );
28
  add_action( 'wp_ajax_nopriv_log_dlm_xhr_download', array( $this, 'xhr_log_download' ), 15 );
 
 
29
  }
30
 
31
  /**
@@ -193,5 +195,59 @@ class DLM_Logging {
193
  download_monitor()->service( 'log_item_repository' )->persist( $log_item );
194
  }
195
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  }
197
 
26
  public function __construct() {
27
  add_action( 'wp_ajax_log_dlm_xhr_download', array( $this, 'xhr_log_download' ), 15 );
28
  add_action( 'wp_ajax_nopriv_log_dlm_xhr_download', array( $this, 'xhr_log_download' ), 15 );
29
+ add_filter( 'dlm_log_entries', array( $this, 'log_entries' ), 15, 2 );
30
+ add_filter( 'dlm_log_values', array( $this, 'log_values' ), 15, 1 );
31
  }
32
 
33
  /**
195
  download_monitor()->service( 'log_item_repository' )->persist( $log_item );
196
  }
197
  }
198
+
199
+ /**
200
+ * Failproof way to log a download if DB upgrade did not proceed correctly
201
+ *
202
+ * @param $entries
203
+ * @param $item
204
+ *
205
+ * @return void
206
+ * @since 40.6.3
207
+ */
208
+ public function log_entries( $entries, $item ) {
209
+ global $wpdb;
210
+
211
+ if ( isset( $wpdb->download_log ) && DLM_Utils::column_checker( $wpdb->download_log, 'uuid' ) ) {
212
+ $entries['uuid'] = $item->get_user_uuid();
213
+ }
214
+
215
+ if ( isset( $wpdb->download_log ) && DLM_Utils::column_checker( $wpdb->download_log, 'download_location' ) ) {
216
+ $entries['download_location'] = $item->get_current_url();
217
+ }
218
+
219
+ if ( isset( $wpdb->download_log ) && DLM_Utils::column_checker( $wpdb->download_log, 'download_category' ) ) {
220
+ $entries['download_category'] = $item->get_download_categories( $item->get_download_id() );
221
+ }
222
+
223
+ return $entries;
224
+ }
225
+
226
+ /**
227
+ * Failproof way to log a download if DB upgrade did not proceed correctly
228
+ *
229
+ * @param $entries
230
+ * @param $item
231
+ *
232
+ * @return void
233
+ * @since 4.6.3
234
+ */
235
+ public function log_values( $entries ) {
236
+ global $wpdb;
237
+
238
+ if ( isset( $wpdb->download_log ) && DLM_Utils::column_checker( $wpdb->download_log, 'uuid' ) ) {
239
+ $entries[] = '%s';
240
+ }
241
+
242
+ if ( isset( $wpdb->download_log ) && DLM_Utils::column_checker( $wpdb->download_log, 'download_location' ) ) {
243
+ $entries[] = '%s';
244
+ }
245
+
246
+ if ( isset( $wpdb->download_log ) && DLM_Utils::column_checker( $wpdb->download_log, 'download_category' ) ) {
247
+ $entries[] = '%s';
248
+ }
249
+
250
+ return $entries;
251
+ }
252
  }
253
 
src/Logs/WordPressLogItemRepository.php CHANGED
@@ -144,55 +144,60 @@ class DLM_WordPress_Log_Item_Repository implements DLM_Log_Item_Repository {
144
 
145
  // Set the log date. Should be current date, as logs will be separated by dates.
146
  $log_date = date( 'Y-m-d' );
147
- $today = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->dlm_reports} WHERE date = %s;", $log_date ), ARRAY_A );
148
 
149
- // check if entry exists.
150
- if ( null !== $today && ! empty( $today ) ) {
151
 
152
- $downloads = json_decode( $today[0]['download_ids'], ARRAY_A );
153
 
154
- if ( isset( $downloads[ $download_id ] ) ) {
155
- $downloads[ $download_id ]['downloads'] = $downloads[ $download_id ]['downloads'] + 1;
156
- } else {
157
- $downloads[ $download_id ] = array(
158
- 'downloads' => 1,
159
- 'title' => get_the_title( $download_id ),
160
- );
161
- }
162
 
163
- $result = $wpdb->update(
164
- "{$wpdb->dlm_reports}",
165
- array(
166
- 'download_ids' => wp_json_encode( $downloads ),
167
- ),
168
- array(
169
- 'date' => $log_date,
170
- )
171
- );
172
-
173
- if ( false === $result ) {
174
- throw new Exception( 'Unable to insert log item in WordPress database' );
175
- }
176
- } else {
177
-
178
- // insert row.
179
- $result = $wpdb->insert(
180
- "{$wpdb->dlm_reports}",
181
- array(
182
- 'date' => $log_date,
183
- 'download_ids' => wp_json_encode(
184
- array(
185
- $download_id => array(
186
- 'downloads' => 1,
187
- 'title' => get_the_title( $download_id ),
188
- ),
189
- )
190
  ),
191
- )
192
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
194
- if ( false === $result ) {
195
- throw new Exception( 'Unable to insert log item in WordPress database' );
 
196
  }
197
  }
198
 
144
 
145
  // Set the log date. Should be current date, as logs will be separated by dates.
146
  $log_date = date( 'Y-m-d' );
 
147
 
148
+ //Check first if the table exists, db upgrade process might have failed.
149
+ if ( DLM_Utils::table_checker( $wpdb->dlm_reports ) ) {
150
 
151
+ $today = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->dlm_reports} WHERE date = %s;", $log_date ), ARRAY_A );
152
 
153
+ // check if entry exists.
154
+ if ( null !== $today && ! empty( $today ) ) {
 
 
 
 
 
 
155
 
156
+ $downloads = json_decode( $today[0]['download_ids'], ARRAY_A );
157
+
158
+ if ( isset( $downloads[ $download_id ] ) ) {
159
+ $downloads[ $download_id ]['downloads'] = $downloads[ $download_id ]['downloads'] + 1;
160
+ } else {
161
+ $downloads[ $download_id ] = array(
162
+ 'downloads' => 1,
163
+ 'title' => get_the_title( $download_id ),
164
+ );
165
+ }
166
+
167
+ $result = $wpdb->update(
168
+ "{$wpdb->dlm_reports}",
169
+ array(
170
+ 'download_ids' => wp_json_encode( $downloads ),
 
 
 
 
 
 
 
 
 
 
 
 
171
  ),
172
+ array(
173
+ 'date' => $log_date,
174
+ )
175
+ );
176
+
177
+ if ( false === $result ) {
178
+ throw new Exception( 'Unable to insert log item in WordPress database' );
179
+ }
180
+ } else {
181
+
182
+ // insert row.
183
+ $result = $wpdb->insert(
184
+ "{$wpdb->dlm_reports}",
185
+ array(
186
+ 'date' => $log_date,
187
+ 'download_ids' => wp_json_encode(
188
+ array(
189
+ $download_id => array(
190
+ 'downloads' => 1,
191
+ 'title' => get_the_title( $download_id ),
192
+ ),
193
+ )
194
+ ),
195
+ )
196
+ );
197
 
198
+ if ( false === $result ) {
199
+ throw new Exception( 'Unable to insert log item in WordPress database' );
200
+ }
201
  }
202
  }
203