4 if (!isset($_SESSION['id']))
6 require_once('common_api.php');
7 header("Content-type: application/json; charset=utf-8");
9 if (!current_user_can(PERMISSION_MANAGE_PLUGINS
))
10 die(json_encode(['error' => "Access denied"]));
12 die(json_encode($config['third-party-plugins']['data']));
14 elseif(isset($_GET['install']))
16 install_plugin($_GET['install']);
18 elseif (isset($_GET['uninstall']))
20 uninstall_plugin($_GET['uninstall']);
23 function uninstall_plugin($name)
26 if (!Plugins
::plugin_exists($name))
27 die(json_encode(['error' => "Plugin not loaded"]));
29 foreach($config['plugins'] as $k => $v)
31 unset($config['plugins'][$k]);
34 deleteDirectory(UPATH
."/plugins/$name");
35 die(json_encode(["success
" => "Plugin was deleted successfully
"]));
38 /**Attempt to install the plugin
39 * @param string $name name of the plugin
42 function install_plugin($name)
45 if (in_array($name, $config['plugins']))
46 die(json_encode(["error
" => "Plugin already installed
"]));
47 $url = get_plugin_install_path_from_name($name);
48 $pluginfile = file_get_contents($url);
49 if (!is_dir(UPATH."/data
/tmp
"))
50 mkdir(UPATH."/data
/tmp
");
52 $path = UPATH."/data
/tmp
/";
53 $file = $path.md5(time()).".tmp
";
54 if (!file_put_contents($file, $pluginfile))
55 die(json_encode(["error
" => "Cannot write to directory
: Need write permission
"]));
59 $zip = new ZipArchive;
60 $res = $zip->open($file);
63 die(json_encode(["error
" => "Could not open file we just wrote lol
"]));
66 // ensure we have no conflicts
67 $extractPath = UPATH."/plugins
/$name";
68 // lazy upgrade for now.
69 if (is_dir($extractPath))
71 deleteDirectory($extractPath);
74 $zip->extractTo($extractPath);
77 //clear up our temp shit
82 // load it in the config
83 $config['plugins'][] = $name;
87 die(json_encode(['success' => "Installation was complete"]));
91 * @param string $name Name of plugin
92 * @return NULL|string Path or NULL
94 function get_plugin_install_path_from_name($name)
97 $list = $config['third-party-plugins']['data']->list;
100 if (!strcmp($p->name
,$name))
101 return $p->download_link
;
106 function deleteDirectory($dir) {
107 if (!file_exists($dir)) {
115 foreach (scandir($dir) as $item) {
116 if ($item == '.' || $item == '..') {
120 if (!deleteDirectory($dir . DIRECTORY_SEPARATOR
. $item)) {