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