]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - api/plugin.php
Add a `Plugins` overview card
[irc/unrealircd/unrealircd-webpanel.git] / api / plugin.php
1 <?php
2
3 session_start();
4 if (!isset($_SESSION['id']))
5 die("Access denied");
6 require_once('common_api.php');
7 header("Content-type: application/json; charset=utf-8");
8
9 if (!current_user_can(PERMISSION_MANAGE_PLUGINS))
10 die(json_encode(['error' => "Access denied"]));
11 if (empty($_GET))
12 die(json_encode($config['third-party-plugins']['data']));
13
14 elseif(isset($_GET['install']))
15 {
16 install_plugin($_GET['install']);
17 }
18 elseif (isset($_GET['uninstall']))
19 {
20 uninstall_plugin($_GET['uninstall']);
21 }
22
23 function uninstall_plugin($name)
24 {
25 global $config;
26 if (!Plugins::plugin_exists($name))
27 die(json_encode(['error' => "Plugin not loaded"]));
28
29 foreach($config['plugins'] as $k => $v)
30 if ($v == $name)
31 unset($config['plugins'][$k]);
32 write_config();
33
34 deleteDirectory(UPATH."/plugins/$name");
35 die(json_encode(["success" => "Plugin was deleted successfully"]));
36 }
37
38 /**Attempt to install the plugin
39 * @param string $name name of the plugin
40 * @return void
41 */
42 function install_plugin($name)
43 {
44 global $config;
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");
51
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"]));
56
57 unset($pluginfile);
58
59 $zip = new ZipArchive;
60 $res = $zip->open($file);
61 if ($res !== true) {
62 unlink($file);
63 die(json_encode(["error" => "Could not open file we just wrote lol"]));
64 }
65
66 // ensure we have no conflicts
67 $extractPath = UPATH."/plugins/$name";
68 // lazy upgrade for now.
69 if (is_dir($extractPath))
70 {
71 deleteDirectory($extractPath);
72 }
73 mkdir($extractPath);
74 $zip->extractTo($extractPath);
75 $zip->close();
76
77 //clear up our temp shit
78 unset($zip);
79 unlink($file);
80 unset($res);
81
82 // load it in the config
83 $config['plugins'][] = $name;
84 write_config();
85
86 // wahey
87 die(json_encode(['success' => "Installation was complete"]));
88 }
89
90 /**
91 * @param string $name Name of plugin
92 * @return NULL|string Path or NULL
93 */
94 function get_plugin_install_path_from_name($name)
95 {
96 global $config;
97 $list = $config['third-party-plugins']['data']->list;
98 foreach($list as $p)
99 {
100 if (!strcmp($p->name,$name))
101 return $p->download_link;
102 }
103 return NULL;
104 }
105
106 function deleteDirectory($dir) {
107 if (!file_exists($dir)) {
108 return true;
109 }
110
111 if (!is_dir($dir)) {
112 return unlink($dir);
113 }
114
115 foreach (scandir($dir) as $item) {
116 if ($item == '.' || $item == '..') {
117 continue;
118 }
119
120 if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
121 return false;
122 }
123
124 }
125
126 return rmdir($dir);
127 }