2018-09-16 01:49:48 +08:00
|
|
|
<?php
|
|
|
|
class Plugin {
|
|
|
|
|
2018-09-16 22:01:26 +08:00
|
|
|
public $plugins;
|
|
|
|
|
2018-09-16 01:49:48 +08:00
|
|
|
public function __construct() {
|
|
|
|
if(!file_exists("plugins/")) {
|
|
|
|
mkdir("plugins/", 0775);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function load($path = "./") {
|
|
|
|
$realpath = realpath($path);
|
|
|
|
$handle = opendir($path);
|
|
|
|
while($file = readdir($handle)) {
|
2018-09-16 21:51:17 +08:00
|
|
|
if($file !== "." && $file !== "..") {
|
|
|
|
if(is_dir("{$realpath}/{$file}/")) {
|
|
|
|
if(file_exists("{$realpath}/{$file}/{$file}.php") && file_exists("{$realpath}/{$file}/{$file}.json")) {
|
|
|
|
$files = "{$file}.php";
|
|
|
|
$target = pathinfo($files)['filename'] . ".json";
|
|
|
|
$data = file_get_contents("{$realpath}/{$file}/{$target}");
|
|
|
|
$info = json_decode($data, true);
|
|
|
|
if(!$info) {
|
|
|
|
PHPMC::Error()->Println("Error when load plugin: " . $files . ": No such plugin info file: " . $target . "<br>" . $data);
|
|
|
|
}
|
2018-09-16 22:01:26 +08:00
|
|
|
if(stristr($this->plugins, $info['package'] . ";")) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-16 21:51:17 +08:00
|
|
|
include("{$realpath}/{$file}/{$files}");
|
2018-09-16 22:01:26 +08:00
|
|
|
$this->plugin .= $info['package'] . ";";
|
2018-09-16 21:51:17 +08:00
|
|
|
eval('$' . $info['main'] . ' = new ' . $info['main'] . '();');
|
|
|
|
eval('$' . $info['main'] . '->onload();');
|
|
|
|
} else {
|
|
|
|
echo "Error: {$realpath}/{$file}/{$file}.php";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(pathinfo($file)['extension'] == "php") {
|
|
|
|
$target = pathinfo($file)['filename'] . ".json";
|
|
|
|
$data = file_get_contents("{$realpath}/{$target}");
|
|
|
|
$info = json_decode($data, true);
|
|
|
|
if(!$info) {
|
|
|
|
PHPMC::Error()->Println("Error when load plugin: " . $file . ": No such plugin info file: " . $target . "<br>" . $data);
|
|
|
|
}
|
2018-09-16 22:01:26 +08:00
|
|
|
if(stristr($this->plugins, $info['package'] . ";")) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-16 21:51:17 +08:00
|
|
|
include("{$realpath}/{$file}");
|
2018-09-16 22:01:26 +08:00
|
|
|
$this->plugin .= $info['package'] . ";";
|
2018-09-16 21:51:17 +08:00
|
|
|
eval('$' . $info['main'] . ' = new ' . $info['main'] . '();');
|
|
|
|
eval('$' . $info['main'] . '->onload();');
|
|
|
|
}
|
2018-09-16 01:49:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function pluginReader($name) {
|
|
|
|
$file = fopen($name, "r") or die("Unable to open file!");
|
|
|
|
$data = fread($file, filesize($name));
|
|
|
|
fclose($file);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|