95 lines
2.2 KiB
PHP
Executable File
95 lines
2.2 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
$runfile = "/var/run/cdn-pending-bot.run";
|
|
|
|
if(file_exists($runfile)){
|
|
die("Bot is running now!\n");
|
|
}
|
|
|
|
file_put_contents($runfile, time());
|
|
class Config{
|
|
public static $pending;
|
|
public static $dest;
|
|
public static $key;
|
|
public static function load(){
|
|
$config = @json_decode(@file_get_contents(__DIR__ . "/config.json"), true);
|
|
if ($config === null) throw new RuntimeException("Failed to read config");
|
|
|
|
$ref = new ReflectionClass(self::class);
|
|
foreach($ref->getStaticProperties() as $key=>$value){
|
|
if(!isset($config[$key])) throw new RuntimeException("Config doesnt contain field ".$key);
|
|
self::$$key = $config[$key];
|
|
}
|
|
}
|
|
}
|
|
|
|
Config::load();
|
|
|
|
interface IRule
|
|
{
|
|
|
|
public function getMatchingExtensions(): array;
|
|
|
|
public function processFile(string $file);
|
|
}
|
|
|
|
/**
|
|
* @var $rules IRule[]
|
|
*/
|
|
$rules = [];
|
|
|
|
/**
|
|
* @param string $ext
|
|
* @return IRule|null
|
|
*/
|
|
function get_rule_by_ext(string $ext)
|
|
{
|
|
global $rules;
|
|
foreach ($rules as $rule) {
|
|
foreach ($rule->getMatchingExtensions() as $re) {
|
|
if ($re === $ext) return $rule;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
foreach (new IteratorIterator(new DirectoryIterator(__DIR__ . "/rules")) as $file) {
|
|
/**
|
|
* @var $file SplFileInfo
|
|
*/
|
|
|
|
if ($file->getExtension() == "php") {
|
|
include $file->getPathname();
|
|
}
|
|
}
|
|
|
|
foreach (get_declared_classes() as $class) {
|
|
try {
|
|
$ref = new ReflectionClass($class);
|
|
if (in_array(IRule::class, $ref->getInterfaceNames())) $rules[] = new $class;
|
|
} catch (ReflectionException $e) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
//List files
|
|
|
|
foreach (new IteratorIterator(new DirectoryIterator(Config::$pending)) as $file) {
|
|
clearstatcache();
|
|
if (!file_exists($file->getPathname())) continue;
|
|
/**
|
|
* @var $file SplFileInfo
|
|
*/
|
|
$ext = strtolower($file->getExtension());
|
|
if($ext == "part") continue;
|
|
$rule = get_rule_by_ext($ext);
|
|
if ($rule !== null) {
|
|
echo "Applying rule ".get_class($rule)." to file ".$file->getBasename()."\r\n";
|
|
$rule->processFile($file->getPathname());
|
|
}
|
|
if (file_exists($file->getPathname())) @unlink($file->getPathname());
|
|
}
|
|
|
|
unlink($runfile);
|