4 if (!isset($_SESSION['id']))
6 require_once('common_api.php');
8 if (!current_user_can(PERMISSION_MANAGE_PLUGINS
))
9 die(json_encode(['error' => "Access denied"]));
11 die(json_encode($config['third-party-plugins']['data']));
13 elseif(isset($_GET['install']))
15 install_plugin($_GET['install']);
17 elseif (isset($_GET['uninstall']))
19 uninstall_plugin($_GET['uninstall']);
22 function uninstall_plugin($name)
25 if (!Plugins
::plugin_exists($name))
26 die(json_encode(['error' => "Plugin not loaded"]));
28 foreach($config['plugins'] as $k => $v)
30 unset($config['plugins'][$k]);
33 deleteDirectory(UPATH
."/plugins/$name");
34 die(json_encode(["success
" => "Plugin was deleted successfully
"]));
37 /**Attempt to install the plugin
38 * @param string $name name of the plugin
41 function install_plugin($name)
44 if (in_array($name, $config['plugins']))
45 die(json_encode(["error
" => "Plugin already installed
"]));
46 $url = get_plugin_install_path_from_name($name);
47 $pluginfile = file_get_contents($url);
48 if (!is_dir(UPATH."/data
/tmp
"))
49 mkdir(UPATH."/data
/tmp
");
51 $path = UPATH."/data
/tmp
/";
52 $file = $path.md5(time()).".tmp
";
53 if (!file_put_contents($file, $pluginfile))
54 die(json_encode(["error
" => "Cannot write to directory
: Need write permission
"]));
58 $zip = new ZipArchive;
59 $res = $zip->open($file);
62 die(json_encode(["error
" => "Could not open file we just wrote lol
"]));
65 // ensure we have no conflicts
66 $extractPath = UPATH."/plugins
/$name";
67 // lazy upgrade for now.
68 if (is_dir($extractPath))
70 deleteDirectory($extractPath);
73 $zip->extractTo($extractPath);
76 //clear up our temp shit
81 // load it in the config
82 $config['plugins'][] = $name;
86 die(json_encode(['success' => "Installation was complete"]));
90 * @param string $name Name of plugin
91 * @return NULL|string Path or NULL
93 function get_plugin_install_path_from_name($name)
96 $list = $config['third-party-plugins']['data']->list;
99 if (!strcmp($p->name
,$name))
100 return $p->download_link
;
105 function deleteDirectory($dir) {
106 if (!file_exists($dir)) {
114 foreach (scandir($dir) as $item) {
115 if ($item == '.' || $item == '..') {
119 if (!deleteDirectory($dir . DIRECTORY_SEPARATOR
. $item)) {