$t_scrambler = array(); foreach (array('variable', 'function', 'method', 'property', 'class', 'class_constant', 'constant', 'namespace', 'label') as $dummy => $scramble_what) { $t_scrambler[$scramble_what] = new Scrambler($scramble_what, $conf, $process_mode == 'directory' ? $target_directory : null); } $traverser->addVisitor(new MyNodeVisitor()); switch ($process_mode) { case 'file': $obfuscated_str = obfuscate($source_file); if ($obfuscated_str === null) { exit; } if ($target_file === '') { echo $obfuscated_str . PHP_EOL; exit; } file_put_contents($target_file, $obfuscated_str . PHP_EOL); exit; case 'directory': if (isset($conf->t_skip) && is_array($conf->t_skip)) { foreach ($conf->t_skip as $key => $val) { $conf->t_skip[$key] = "{$source_directory}/{$val}"; } } if (isset($conf->t_keep) && is_array($conf->t_keep)) { foreach ($conf->t_keep as $key => $val) { $conf->t_keep[$key] = "{$source_directory}/{$val}"; } } obfuscate_directory($source_directory, "{$target_directory}/yakpro-po/obfuscated"); exit; }
function obfuscate_directory($source_dir, $target_dir, $keep_mode = false) { global $conf; if (!($dp = opendir($source_dir))) { fprintf(STDERR, "Error:\t [%s] directory does not exists!%s", $source_dir, PHP_EOL); exit(-1); } $t_dir = array(); $t_file = array(); while (($entry = readdir($dp)) !== false) { if ($entry == "." || $entry == "..") { continue; } $new_keep_mode = $keep_mode; $source_path = "{$source_dir}/{$entry}"; $source_stat = @lstat($source_path); $target_path = "{$target_dir}/{$entry}"; $target_stat = @lstat($target_path); if ($source_stat === false) { fprintf(STDERR, "Error:\t cannot stat [%s] !%s", $source_path, PHP_EOL); exit(-1); } if (isset($conf->t_skip) && is_array($conf->t_skip) && in_array($source_path, $conf->t_skip)) { continue; } if (is_link($source_path)) { if ($target_stat !== false && is_link($target_path) && $source_stat['mtime'] <= $target_stat['mtime']) { continue; } if ($target_stat !== false) { if (is_dir($target_path)) { directory_remove($target_path); } else { if (unlink($target_path) === false) { fprintf(STDERR, "Error:\t cannot unlink [%s] !%s", $target_path, PHP_EOL); exit(-1); } } } @symlink(readlink($source_path), $target_path); // Do not warn on non existing symbolinc link target! if (strtolower(PHP_OS) == 'linux') { $x = `touch '{$target_path}' --no-dereference --reference='{$source_path}' `; } continue; } if (is_dir($source_path)) { if ($target_stat !== false) { if (!is_dir($target_path)) { if (unlink($target_path) === false) { fprintf(STDERR, "Error:\t cannot unlink [%s] !%s", $target_path, PHP_EOL); exit(-1); } } } if (!file_exists($target_path)) { mkdir($target_path, 0777, true); } if (isset($conf->t_keep) && is_array($conf->t_keep) && in_array($source_path, $conf->t_keep)) { $new_keep_mode = true; } obfuscate_directory($source_path, $target_path, $new_keep_mode); continue; } if (is_file($source_path)) { if ($target_stat !== false && is_dir($target_path)) { directory_remove($target_path); } if ($target_stat !== false && $source_stat['mtime'] <= $target_stat['mtime']) { continue; } // do not process if source timestamp is not greater than target $extension = pathinfo($source_path, PATHINFO_EXTENSION); $keep = $keep_mode; if (isset($conf->t_keep) && is_array($conf->t_keep) && in_array($source_path, $conf->t_keep)) { $keep = true; } if (!in_array($extension, $conf->t_obfuscate_php_extension)) { $keep = true; } if ($keep) { file_put_contents($target_path, file_get_contents($source_path)); } else { $obfuscated_str = obfuscate($source_path); if ($obfuscated_str === null) { if (isset($conf->abort_on_error)) { fprintf(STDERR, "Aborting...%s", PHP_EOL); exit; } } file_put_contents($target_path, $obfuscated_str . PHP_EOL); } if ($keep) { file_put_contents($target_path, file_get_contents($source_path)); } touch($target_path, $source_stat['mtime']); continue; } } closedir($dp); }