PluginProbe
DoLogin Security / trunk
DoLogin Security vtrunk
5.0.10 4.8.3 trunk 1.0 1.1 1.1.1 1.2 1.2.1 1.2.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 1.6 All 64 releases
dologin / src / f.cls.php

f.cls.php in DoLogin Security trunk, at src/f.cls.php

166 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * File Operator
4 *
5 * @since 1.0
6 */
7 namespace dologin;
8
9 defined( 'WPINC' ) || exit;
10
11 class f {
12 /**
13 * Delete folder
14 *
15 * @since 1.0
16 */
17 public static function rrmdir( $dir ) {
18 $files = array_diff( scandir( $dir ), array( '.', '..' ) );
19
20 foreach ( $files as $file ) {
21 is_dir( "$dir/$file" ) ? self::rrmdir( "$dir/$file" ) : wp_delete_file( "$dir/$file" );
22 }
23
24 return rmdir( $dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- generic filesystem helper for the plugin's own data files.
25 }
26
27 public static function count_lines( $filename ) {
28 if ( ! file_exists( $filename ) ) {
29 return 0;
30 }
31
32 $file = new \SplFileObject( $filename );
33 $file->seek( PHP_INT_MAX );
34 return $file->key() + 1;
35 }
36
37 /**
38 * Read data from file
39 *
40 * @since 1.0
41 */
42 public static function read( $filename, $start_line = null, $lines = null ) {
43 if ( ! file_exists( $filename ) ) {
44 return '';
45 }
46
47 if ( ! is_readable( $filename ) ) {
48 return false;
49 }
50
51 if ( $start_line !== null ) {
52 $res = array();
53 $file = new \SplFileObject( $filename );
54 $file->seek( $start_line );
55
56 if ( $lines === null ) {
57 while ( ! $file->eof() ) {
58 $res[] = rtrim( $file->current(), PHP_EOL );
59 $file->next();
60 }
61 } else {
62 for ( $i = 0; $i < $lines; $i++ ) {
63 if ( $file->eof() ) {
64 break;
65 }
66 $res[] = rtrim( $file->current(), PHP_EOL );
67 $file->next();
68 }
69 }
70
71 unset( $file );
72 return $res;
73 }
74
75 $content = file_get_contents( $filename ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- generic filesystem helper for the plugin's own data files.
76
77 $content = self::remove_zero_space( $content );
78
79 return $content;
80 }
81
82 /**
83 * Append data to file
84 *
85 * @since 1.0
86 * @access public
87 */
88 public static function append( $filename, $data, $mkdir = false, $silence = true ) {
89 return self::save( $filename, $data, $mkdir, true, $silence );
90 }
91
92 /**
93 * Save data to file
94 *
95 * @since 1.0
96 */
97 public static function save( $filename, $data, $mkdir = false, $append = false, $silence = true ) {
98 $error = false;
99 $folder = dirname( $filename );
100
101 // mkdir if folder does not exist
102 if ( ! file_exists( $folder ) ) {
103 if ( ! $mkdir ) {
104 /* translators: %s: Folder path. */
105 return $silence ? false : sprintf( __( 'Folder does not exist: %s', 'dologin' ), $folder );
106 }
107
108 try {
109 mkdir( $folder, 0755, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir -- generic filesystem helper for the plugin's own data files.
110 } catch ( \Exception $ex ) {
111 /* translators: 1: Folder path, 2: Error message. */
112 return $silence ? false : sprintf( __( 'Can not create folder: %1$s. Error: %2$s', 'dologin' ), $folder, $ex->getMessage() );
113 }
114 }
115
116 if ( ! file_exists( $filename ) ) {
117 if ( ! is_writable( $folder ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- generic filesystem helper for the plugin's own data files.
118 /* translators: %s: Folder path. */
119 return $silence ? false : sprintf( __( 'Folder is not writable: %s.', 'dologin' ), $folder );
120 }
121 try {
122 touch( $filename ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch -- generic filesystem helper for the plugin's own data files.
123 } catch ( \Exception $ex ) {
124 /* translators: %s: File path. */
125 return $silence ? false : sprintf( __( 'File %s is not writable.', 'dologin' ), $filename );
126 }
127 } elseif ( ! is_writeable( $filename ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writeable -- generic filesystem helper for the plugin's own data files.
128 /* translators: %s: File path. */
129 return $silence ? false : sprintf( __( 'File %s is not writable.', 'dologin' ), $filename );
130 }
131
132 $data = self::remove_zero_space( $data );
133
134 $ret = file_put_contents( $filename, $data, $append ? FILE_APPEND : LOCK_EX ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- generic filesystem helper for the plugin's own data files.
135 if ( $ret === false ) {
136 /* translators: %s: File path. */
137 return $silence ? false : sprintf( __( 'Failed to write to %s.', 'dologin' ), $filename );
138 }
139
140 return true;
141 }
142
143 /**
144 * Remove Unicode zero-width space <200b><200c>
145 *
146 * @since 1.0
147 */
148 public static function remove_zero_space( $content ) {
149 if ( is_array( $content ) ) {
150 $content = array_map( __CLASS__ . '::remove_zero_space', $content );
151 return $content;
152 }
153
154 // Remove UTF-8 BOM if present
155 if ( substr( $content, 0, 3 ) === "\xEF\xBB\xBF" ) {
156 $content = substr( $content, 3 );
157 }
158
159 $content = str_replace( "\xe2\x80\x8b", '', $content );
160 $content = str_replace( "\xe2\x80\x8c", '', $content );
161 $content = str_replace( "\xe2\x80\x8d", '', $content );
162
163 return $content;
164 }
165 }
166