PluginProbe
Awesome Support – WordPress HelpDesk & Support Plugin / trunk
Awesome Support – WordPress HelpDesk & Support Plugin vtrunk
6.4.0 trunk 3.0.0 3.0.1 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 All 95 releases
awesome-support / includes / admin / functions-log-viewer.php

functions-log-viewer.php in Awesome Support – WordPress HelpDesk & Support Plugin trunk, at includes/admin/functions-log-viewer.php

302 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Created by PhpStorm.
4 * User: Robert
5 * Date: 6/5/2017
6 * Time: 8:34 PM
7 */
8
9 /*
10 log viewer - just a couple of minor changes:
11 1. Don't show the logs folder or show it at the bottom of the screen.
12 2. Make the main background color gray and the viewer itself black text on white background.
13 3. Remove the horizontal lines across the top - looks too busy with them there. This is a simple interface so lets make it less busy by doing that.
14 4. If possible, add an option to turn off wordwrap.
15
16 */
17
18 /*
19 * Actions
20 * --------------------------------
21 * wpas_tools_log_viewer_view
22 * wpas_tools_log_viewer_download
23 * wpas_tools_log_viewer_delete
24 *
25 */
26
27 function get_logs_path() {
28
29 $log = new WPAS_Logger( '' );
30 $base_path = $log->get_logs_base_path() . '/';
31
32 return $base_path;
33
34 }
35
36 function get_logs_url() {
37 return WPAS_URL . 'logs/';
38 }
39
40 /*
41 * AJAX handler function
42 */
43 function wpas_tools_log_viewer_view() {
44
45 if ( ! current_user_can( 'administrator' ) ) {
46 wp_send_json_error( array( 'error' => esc_html__( 'Not found', 'awesome-support' ) ) );
47 }
48
49 check_ajax_referer( 'wpas_tools_log_viewer_view', 'nonce' );
50
51 if( ! isset( $_POST[ 'file' ] ) ) {
52 wp_send_json_error( array( 'error' => esc_html__( 'No file given', 'awesome-support' ) ) );
53 }
54
55 // Default number of lines to return
56 $lines = 100;
57 $file = basename( sanitize_text_field( wp_unslash( $_POST[ 'file' ] )) );
58
59 // Get posted number of lines
60 if( isset( $_POST[ 'lines' ] ) ) {
61 $lines = sanitize_text_field( wp_unslash( $_POST[ 'lines' ] ));
62 }
63
64 wp_send_json_success( wpas_log_viewer_read_last_lines( $file, $lines ) );
65
66 }
67 add_action( 'wp_ajax_wpas_tools_log_viewer_view', 'wpas_tools_log_viewer_view', 10, 0 );
68
69
70 /*
71 * AJAX handler function
72 */
73 function wpas_tools_log_viewer_download() {
74
75 if ( ! current_user_can( 'administrator' ) ) {
76 wp_send_json_error( array( 'error' => esc_html__( 'Not found', 'awesome-support' ) ) );
77 }
78
79 check_ajax_referer( 'wpas_tools_log_viewer_download', 'nonce' );
80
81 if( ! isset( $_POST[ 'file' ] ) ) {
82 wp_send_json_error( array( 'error' => esc_html__( 'No file given', 'awesome-support' ) ) );
83 }
84
85 $file = basename( sanitize_text_field( wp_unslash( $_POST[ 'file' ] )) );
86
87 $content = array(
88 'status' => array(
89 'code' => '200',
90 'message' => 'Downloading...'
91 ),
92 'url' => get_logs_url() . $file
93 );
94
95 wp_send_json_success( $content );
96 }
97 add_action( 'wp_ajax_wpas_tools_log_viewer_download', 'wpas_tools_log_viewer_download', 10, 0 );
98
99
100 /*
101 * AJAX handler function
102 */
103 function wpas_tools_log_viewer_delete() {
104
105 if ( ! current_user_can( 'administrator' ) ) {
106 wp_send_json_error( array( 'error' => esc_html__( 'Not found', 'awesome-support' ) ) );
107 }
108
109 check_ajax_referer( 'wpas_tools_log_viewer_delete', 'nonce' );
110
111 if( ! isset( $_POST[ 'file' ] ) ) {
112 echo json_encode( array( 'error' => esc_html__( 'No file given', 'awesome-support' ) ) );
113 wp_die();
114 }
115
116 $file = basename( sanitize_text_field( wp_unslash( $_POST[ 'file' ] )) );
117
118 wp_send_json_success( wpas_log_viewer_delete_file( $file ) );
119
120 }
121 add_action( 'wp_ajax_wpas_tools_log_viewer_delete', 'wpas_tools_log_viewer_delete', 10, 0 );
122
123
124 /**
125 * @param $file
126 */
127 function wpas_log_viewer_delete_file( $file ) {
128
129 if( unlink( get_logs_path() . $file ) ) {
130 $code = '200';
131 $content = "Deleted " . $file . " successfully.";
132 }
133 else {
134 $code = '404';
135 $content = "Delete " . $file . " failed.";
136 }
137
138 $json = array(
139 'status' => array(
140 'code' => $code,
141 'message' => $content
142 ),
143 'url' => get_logs_url() . $file
144 );
145
146 wp_send_json_success( $json );
147
148 }
149
150
151 /* Function read X last lines from file*/
152 function wpas_log_viewer_read_last_lines( $file, $lines ) {
153
154 if( 'All' === $lines ) {
155 return wpas_log_viewer_read_full_file( $file );
156 }
157
158 $result = [];
159 $file_path = get_logs_path() . $file;
160
161 global $wp_filesystem;
162 // Initialize the filesystem
163 if (empty($wp_filesystem)) {
164 require_once(ABSPATH . '/wp-admin/includes/file.php');
165 WP_Filesystem();
166 }
167
168 $handle = $wp_filesystem->get_contents($file_path);
169 if ($handle !== false) {
170 $lines_array = explode("\n", $handle); // Diviser le contenu du fichier en lignes
171 $total_lines = count($lines_array);
172
173 $linecounter = $lines; // Le nombre de lignes à lire
174 $text = array();
175
176 while ($linecounter > 0 && $total_lines > 0) {
177 if(isset($lines_array[$total_lines - $linecounter])){
178 $text[] = $lines_array[$total_lines - $linecounter];
179 }
180 $linecounter--;
181 }
182
183 foreach ($text as $line) {
184 if($line){
185 $result[] = $line;
186 }
187 }
188
189 // translators: %d is the number of lines, %s is the source.
190 $x_content = __( 'Read %1$d lines from %2$s', 'awesome-support' );
191
192 return array(
193 'status' => array(
194 'code' => '200',
195 'message' => sprintf( $x_content, count($result), esc_html( $file )),
196 ),
197 'fileinfo' => array(
198 'created' => gmdate ("F d Y H:i:s", filectime($file_path)),
199 'lastmodified' => gmdate ("F d Y H:i:s", filemtime($file_path)),
200 'filesize' => wpas_formatbytes(filesize($file_path)),
201 ),
202 'data' => $result,
203 );
204 }
205 else {
206 //return printf( __( "Couldn't open the file %s. Make sure file is exists or is readable.", 'error-log-viewer' ), esc_html( $file ) );
207
208 // translators: %s is the file name that couldn't be opened.
209 $x_content = __( "Couldn't open the file %s. Make sure the file exists and is readable.", 'awesome-support' );
210
211
212 return array(
213 'status' => array(
214 'code' => '404',
215 'message' => sprintf($x_content, esc_html( $file )),
216 ),
217 'data' => [],
218 );
219 }
220
221 }
222
223
224 /* Function read full file */
225 function wpas_log_viewer_read_full_file( $file ) {
226
227 $file_path = get_logs_path() . $file;
228
229 global $wp_filesystem;
230 // Initialize the filesystem
231 if (empty($wp_filesystem)) {
232 require_once(ABSPATH . '/wp-admin/includes/file.php');
233 WP_Filesystem();
234 }
235 $handle = $wp_filesystem->get_contents($file_path);
236 $result = [];
237 if ($handle !== false) {
238 $lines_array = explode("\n", $handle); // Diviser le contenu du fichier en lignes
239 foreach ($lines_array as $line) {
240 $result[] = $line;
241 }
242 // translators: %1$d is the number of lines, %2$s is the source.
243 $x_content = __( 'Read %1$d lines from %2$s', 'awesome-support' );
244
245 return array(
246 'status' => array(
247 'code' => '200',
248 //'message' => '',
249 'message' => sprintf($x_content, count($result), esc_html( $file )),
250 ),
251 'fileinfo' => array(
252 'created' => gmdate ("F d Y H:i:s", filectime($file_path)),
253 'lastmodified' => gmdate ("F d Y H:i:s", filemtime($file_path)),
254 'filesize' => wpas_formatbytes(filesize($file_path)),
255 ),
256 'data' => $result,
257 );
258 }
259 else {
260 //return printf( __( "Couldn't open the file %s. Make sure file is exists or is readable.", 'error-log-viewer' ), esc_html( $file ) );
261 // translators: %s is the file name that couldn't be opened.
262 $x_content = __( "Couldn't open the file %s. Make sure the file exists and is readable.", 'awesome-support' );
263 return array(
264 'status' => array(
265 'code' => '404',
266 'message' => sprintf( $x_content, esc_html( $file )),
267 ),
268 'data' => [],
269 );
270 }
271 }
272
273 function wpas_formatbytes($val, $digits = 3, $mode = "SI", $bB = "B")
274 {
275 $si = array("", "k", "M", "G", "T", "P", "E", "Z", "Y");
276 $iec = array("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi");
277
278 switch(strtoupper($mode))
279 {
280 case "SI" : $factor = 1000; $symbols = $si; break;
281 case "IEC" : $factor = 1024; $symbols = $iec; break;
282 default : $factor = 1000; $symbols = $si; break;
283 }
284
285 switch($bB)
286 {
287 case "b" : $val *= 8; break;
288 default : $bB = "B"; break;
289 }
290
291 for($i=0;$i<count($symbols)-1 && $val>=$factor;$i++)
292 $val /= $factor;
293
294 $p = strpos($val, ".");
295 if($p !== false && $p > $digits)
296 $val = round($val);
297 elseif($p !== false)
298 $val = round($val, $digits-$p);
299
300 return round($val, $digits) . " " . $symbols[$i] . $bB;
301 }
302