]>
Commit | Line | Data |
---|---|---|
fae5cd15 VP |
1 | <?php |
2 | ||
3 | session_start(); | |
4 | if (!isset($_SESSION['id'])) | |
5 | die("Access denied"); | |
6 | require_once('common_api.php'); | |
7 | ||
8 | header("Content-type: application/json; charset=utf-8"); | |
9 | ||
10 | if (!current_user_can(PERMISSION_MANAGE_PLUGINS)) | |
11 | die(json_encode(['error' => "Access denied"])); | |
12 | if (empty($_GET)) | |
13 | die(json_encode($config['third-party-plugins']['data'])); | |
14 | ||
15 | elseif(isset($_GET['install'])) | |
16 | { | |
17 | install_plugin($_GET['install']); | |
18 | } | |
19 | elseif (isset($_GET['uninstall'])) | |
20 | { | |
21 | uninstall_plugin($_GET['uninstall']); | |
22 | } | |
23 | ||
24 | function uninstall_plugin($name) | |
25 | { | |
26 | global $config; | |
27 | if (!Plugins::plugin_exists($name)) | |
28 | die(json_encode(['error' => "Plugin not loaded"])); | |
29 | ||
30 | foreach($config['plugins'] as $k => $v) | |
31 | if ($v == $name) | |
32 | unset($config['plugins'][$k]); | |
33 | write_config(); | |
34 | ||
35 | deleteDirectory(UPATH."/plugins/$name"); | |
36 | die(json_encode(["success" => "Plugin was deleted successfully"])); | |
37 | } | |
38 | ||
39 | /**Attempt to install the plugin | |
40 | * @param string $name name of the plugin | |
41 | * @return void | |
42 | */ | |
43 | function install_plugin($name) | |
44 | { | |
45 | global $config; | |
46 | if (in_array($name, $config['plugins'])) | |
47 | die(json_encode(["error" => "Plugin already installed"])); | |
48 | $url = get_plugin_install_path_from_name($name); | |
49 | $pluginfile = file_get_contents($url); | |
50 | if (!is_dir(UPATH."/data/tmp")) | |
51 | mkdir(UPATH."/data/tmp"); | |
52 | ||
53 | $path = UPATH."/data/tmp/"; | |
54 | $file = $path.md5(time()).".tmp"; | |
55 | if (!file_put_contents($file, $pluginfile)) | |
56 | die(json_encode(["error" => "Cannot write to directory: Need write permission"])); | |
57 | ||
58 | unset($pluginfile); | |
59 | ||
60 | $zip = new ZipArchive; | |
61 | $res = $zip->open($file); | |
62 | if ($res !== true) { | |
63 | unlink($file); | |
64 | die(json_encode(["error" => "Could not open file we just wrote lol"])); | |
65 | } | |
66 | ||
67 | // ensure we have no conflicts | |
68 | $extractPath = UPATH."/plugins/$name"; | |
69 | // lazy upgrade for now. | |
70 | if (is_dir($extractPath)) | |
71 | { | |
72 | deleteDirectory($extractPath); | |
73 | } | |
74 | mkdir($extractPath); | |
75 | $zip->extractTo($extractPath); | |
76 | $zip->close(); | |
77 | ||
78 | //clear up our temp shit | |
79 | unset($zip); | |
80 | unlink($file); | |
81 | unset($res); | |
82 | ||
83 | // load it in the config | |
84 | $config['plugins'][] = $name; | |
85 | write_config(); | |
86 | ||
87 | // wahey | |
88 | die(json_encode(['success' => "Installation was complete"])); | |
89 | } | |
90 | ||
91 | /** | |
92 | * @param string $name Name of plugin | |
93 | * @return NULL|string Path or NULL | |
94 | */ | |
95 | function get_plugin_install_path_from_name($name) | |
96 | { | |
97 | global $config; | |
98 | foreach($config['third-party-plugins']['data'] 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 | } |