|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +*Apache |
| 5 | +*NGINX |
| 6 | +*other environments |
| 7 | +*/ |
| 8 | + |
| 9 | +function htaccess_rules() { |
| 10 | + |
| 11 | + -> better-wp-security/core/class-itsec-files.php |
| 12 | + |
| 13 | +define() custom HTTP header to get IP default:'X-CLUSTER-CLIENT-IP' |
| 14 | +preg_quote $host_ip |
| 15 | +"SetEnvIF " . array( 'REMOTE_ADDR', 'X-FORWARDED-FOR' ) $deny_env; |
| 16 | +# Apache < 2.3, # Apache ≥ 2.3 |
| 17 | +<IfModule !mod_authz_core.c> |
| 18 | + Order allow,deny |
| 19 | + Deny from env=$deny_env |
| 20 | +# Deny from $host_ip |
| 21 | + Allow from all |
| 22 | + Satisfy All |
| 23 | +</IfModule> |
| 24 | +<IfModule mod_authz_core.c> |
| 25 | + Require not env $deny_env |
| 26 | +# Require not ip $host_ip |
| 27 | +</IfModule> |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +function singleton_put_contents( $path, $marker, $content, $timeout ) { |
| 32 | + |
| 33 | + // wp-admin/includes/misc.php |
| 34 | + save_mod_rewrite_rules() |
| 35 | + clone: insert_with_markers() |
| 36 | + - gethome(htaccess) normal/subdir install |
| 37 | + |
| 38 | + //1. exists, writeable ... |
| 39 | + ! fstat()/mod -> then @chmod( $htaccess, 0664 ); |
| 40 | + |
| 41 | + //2. lock || wait and loop |
| 42 | + "If the file has been locked with LOCK_EX in another process, the CALL WILL BLOCK UNTIL ALL OTHER LOCKS have been released." |
| 43 | + // must use "@" |
| 44 | + $fp = @fopen( $path, 'w' ) |
| 45 | + check $fp |
| 46 | +
|
| 47 | + $give_up = time() + 30; |
| 48 | + while ( ! flock( $file_handle, LOCK_EX | LOCK_NB ) ) { |
| 49 | + //Lock not acquired, try again in: |
| 50 | + usleep( round( rand( 0, 100 ) * 1000 ) ); |
| 51 | + if ( $give_up >= time() ) |
| 52 | + } |
| 53 | +
|
| 54 | + //3. read rules |
| 55 | + $file_stat = fstat( $handle ); |
| 56 | + $contents = fread( $handle, $file_stat['size'] ); |
| 57 | + // line ends |
| 58 | + $contents = preg_replace( '/\n|\r\n?/', PHP_EOL, $contents ); |
| 59 | + //already contains? |
| 60 | + preg_match multiline "# BEGIN " . $marker . PHP_EOL -> "# END " . $marker . PHP_EOL; |
| 61 | + |
| 62 | + //4. ftruncate($fp, 0); write new rules in one go |
| 63 | + |
| 64 | + // 5. release lock |
| 65 | + fflush |
| 66 | + flock( $handle, LOCK_UN ); |
| 67 | + fclose |
| 68 | +} |
0 commit comments