Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
leohearts committed Aug 29, 2020
1 parent 0da6393 commit d257fe6
Showing 1 changed file with 92 additions and 9 deletions.
101 changes: 92 additions & 9 deletions watchbird.php
Original file line number Diff line number Diff line change
Expand Up @@ -1618,20 +1618,100 @@ function showlog(){
function install($dir){
$layer_list = scandir($dir);
foreach ($layer_list as $i){
if ($i === '.' || $i == "..") {continue;}
if ($i === '.' || $i === "..") {
continue;
}
$next = $dir . $i;
if (is_dir($next)){
if ($next[strlen($next) - 1] !== '/') {$next .= "/";}
if (is_dir($next)) {
if ($next[strlen($next) - 1] !== '/') {
$next .= "/";
}
install($next);
}
else{
} else {
$ext = end(explode('.', $next));
$php_ext = ["php", "php5", "phtml"];
if (in_array($ext, $php_ext) && strlen($ext) !== strlen($next)){
if (in_array($ext, $php_ext) && strlen($ext) !== strlen($next)) {
$old_file_str = file_get_contents($next);
if (strpos($old_file_str, "<?php") !== false && $next !== __FILE__){
if (strpos($old_file_str, "<?php") !== false && $next !== __FILE__) {
echo $next . "\n";
file_put_contents($next, "<?php include_once '".__FILE__."'; ?>" . $old_file_str);
$start_pos = strpos($old_file_str, "<?php");
if ($start_pos === false) {
return;
}
$first_code_pos1 = strpos($old_file_str, ";", $start_pos);
$first_code_pos2 = strpos($old_file_str, "{", $start_pos);

if ($first_code_pos1 === false) {
$first_code_pos = $first_code_pos2;
} else if ($first_code_pos2 === false) {
$first_code_pos = $first_code_pos1;
} else $first_code_pos = min($first_code_pos1, $first_code_pos2);
if ($first_code_pos === false) {
return;
}
while (strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "/*") !== false || strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "//") !== false || strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "#") !== false) {
if (strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "/*") !== false) {
$start_pos = strpos($old_file_str, "*/", strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "/*") + $start_pos);
if ($start_pos === false) {
return;
}
$first_code_pos1 = strpos($old_file_str, ";", $start_pos);
$first_code_pos2 = strpos($old_file_str, "{", $start_pos);

if ($first_code_pos1 === false) {
$first_code_pos = $first_code_pos2;
} else if ($first_code_pos2 === false) {
$first_code_pos = $first_code_pos1;
} else $first_code_pos = min($first_code_pos1, $first_code_pos2);
if ($first_code_pos === false) {
return;
}
}
if (strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "//") !== false) {
$start_pos = strpos($old_file_str, "\n", strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "//") + $start_pos);
if ($start_pos === false) {
return;
}
$first_code_pos1 = strpos($old_file_str, ";", $start_pos);
$first_code_pos2 = strpos($old_file_str, "{", $start_pos);

if ($first_code_pos1 === false) {
$first_code_pos = $first_code_pos2;
} else if ($first_code_pos2 === false) {
$first_code_pos = $first_code_pos1;
} else $first_code_pos = min($first_code_pos1, $first_code_pos2);
if ($first_code_pos === false) {
return;
}
}

if (strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "#") !== false) {
$start_pos = strpos($old_file_str, "\n", strrpos(substr($old_file_str, $start_pos, $first_code_pos - $start_pos), "#") + $start_pos);
if ($start_pos === false) {
return;
}
$first_code_pos1 = strpos($old_file_str, ";", $start_pos);
$first_code_pos2 = strpos($old_file_str, "{", $start_pos);

if ($first_code_pos1 === false) {
$first_code_pos = $first_code_pos2;
} else if ($first_code_pos2 === false) {
$first_code_pos = $first_code_pos1;
} else $first_code_pos = min($first_code_pos1, $first_code_pos2);
if ($first_code_pos === false) {
return;
}
}
}
if (preg_match("/namespace/i", substr($old_file_str, $start_pos, $first_code_pos - $start_pos)) === 1){
return; // 一般来说, 只要加在入口文件即可
}
else if (preg_match("/declare {0,}\t{0,}\\(/i", substr($old_file_str, $start_pos, $first_code_pos - $start_pos)) === 1){
file_put_contents($next, substr($old_file_str, 0, $first_code_pos + 1)."\ninclude_once '" . __FILE__ . "';\n" . substr($old_file_str, $first_code_pos + 1));
}
else {
file_put_contents($next, "<?php include_once '".__FILE__."'; ?>" . $old_file_str);
}
}
}
}
Expand All @@ -1655,10 +1735,13 @@ function uninstall($dir)
$php_ext = ["php", "php5", "phtml"];
if (in_array($ext, $php_ext) && strlen($ext) !== strlen($next)) {
$old_file_str = file_get_contents($next);
if (strpos($old_file_str, "<?php include_once '" . __FILE__ . "'; ?>") === 0) {
if (strpos(ltrim($old_file_str), "<?php include_once '" . __FILE__ . "'; ?>") === 0) {
echo $next . "\n";
file_put_contents($next, substr($old_file_str, strlen("<?php include_once '" . __FILE__ . "'; ?>")));
}
else {
file_put_contents($next, str_replace("\ninclude_once '" . __FILE__ . "';\n", "", $old_file_str));
}
}
}
}
Expand Down

0 comments on commit d257fe6

Please sign in to comment.