PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 1.4.6
Backup Migration v1.4.6
2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / check / checker.php
backup-backup / includes / check Last commit date
checker.php 1 year ago compatibility.php 1 year ago system_info.php 1 year ago
checker.php
128 lines
1 <?php
2
3 // Namespace
4 namespace BMI\Plugin\Checker;
5
6 // Use
7 use BMI\Plugin\BMI_Logger AS Logger;
8 use BMI\Plugin\Progress\BMI_ZipProgress AS Progress;
9 use BMI\Plugin\Backup_Migration_Plugin as BMP;
10
11 // Exit on direct access
12 if (!defined('ABSPATH')) exit;
13
14 /**
15 * BMI_Checker
16 */
17 class BMI_Checker {
18
19 public $issues = array();
20 public $progress;
21
22 function __construct($progress = false) {
23
24 $this->progress = $progress;
25
26 }
27
28 public function logs($log, $status = 'INFO') {
29
30 if ($this->progress) {
31 $this->progress->log($log, $status);
32 }
33
34 }
35
36 public function is_enabled($func) {
37
38 $disabled = explode(',', ini_get('disable_functions'));
39 $isDisabled = in_array($func, $disabled);
40 if (!$isDisabled && function_exists($func)) return true;
41 else return false;
42
43 }
44
45 public function check_free_space($size, $hideRequire = false) {
46
47 if (!$hideRequire) {
48 $this->logs(__('Requires at least ', 'backup-backup') . $size . __(' bytes.', 'backup-backup') . ' [' . BMP::humanSize($size) . ']');
49 }
50
51 $maxTime = 60;
52 if ($this->is_enabled('get_ini')) {
53 $maxTime = @ini_get('max_execution_time');
54 if ($this->is_enabled('ini_set')) @ini_set('max_execution_time', '259200');
55 }
56
57 $shouldUseDiskFreeSpaceIfAvailable = false;
58
59 // If free disk space is larger lower than 50 GBs
60 // OR {
61 // If there is low execution time use space check, as the other may take too much time
62 // If size of the backup is larger than 3 GBs (as it may be to slow to check)
63 // }
64 if ($this->is_enabled('disk_free_space') && (disk_free_space(BMI_BACKUPS) < 1024*1024*1024*50 || ($size > 1024*1024*1024*3 && $maxTime <= 60)))
65 $shouldUseDiskFreeSpaceIfAvailable = true;
66
67 if ($this->is_enabled('disk_free_space') && intval(disk_free_space(BMI_BACKUPS)) > 100 && $shouldUseDiskFreeSpaceIfAvailable) {
68
69 $this->logs(__('Disk free space function is not disabled - using it...', 'backup-backup'));
70 $this->logs(__('Checking this path/partition: ', 'backup-backup') . BMI_BACKUPS);
71 $free = intval(disk_free_space(BMI_BACKUPS));
72 $this->logs(__('There is ', 'backup-backup') . number_format($free / 1024 / 1024, 2) . __(' MB free.', 'backup-backup') . ' [' . BMP::humanSize($free) . ']', 'SUCCESS');
73 if ($free > $size) {
74 $this->logs(__('Great! We have enough space.', 'backup-backup'), 'SUCCESS');
75 return true;
76 } else {
77 return false;
78 }
79
80 } else {
81
82 // Log
83 $this->logs(__('Disk free space function is disabled by hosting.', 'backup-backup'));
84 $this->logs(__('Using dummy file to check free space (it can take some time).', 'backup-backup'));
85
86 // TMP Filename
87 $file = BMI_BACKUPS . '/' . '.space_check';
88 try {
89
90 // 2 GB = (1024 * 1024 * 1024 * 2)
91 $total = $size;
92
93 $fh = fopen($file, 'w');
94 $chunk = 1024;
95 while ($size > 0) {
96 fputs($fh, str_pad('', min($chunk, $size)));
97 $size -= $chunk;
98 }
99 fclose($fh);
100
101 $fs = filesize($file);
102 @unlink($file);
103
104 if ($fs > ($total - 100)) return true;
105 else return false;
106
107 } catch (\Exception $e) {
108
109 Logger::error($e);
110 if (file_exists($file)) unlink($file);
111
112 return false;
113
114 } catch (\Throwable $e) {
115
116 Logger::error($e);
117 if (file_exists($file)) unlink($file);
118
119 return false;
120
121 }
122
123 }
124
125 }
126
127 }
128