forked from Konzertheld/addoninstaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addoninstaller.plugin.php
200 lines (177 loc) · 5.66 KB
/
addoninstaller.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Habari;
class AddonInstaller extends Plugin
{
/** @var array $_types Default addon types */
protected $_types = array(
'theme' => 'Themes',
'plugin' => 'Plugins',
'locale' => 'Locales',
'core' => 'Core',
);
/** @var array $_types Subdirectories for default addon types */
protected $_type_subdirs = array(
'theme' => 'themes',
'plugin' => 'plugins',
'locale' => 'locale',
);
/**
* Plugin initialization. Add our templates and rules so they can be used.
*/
public function action_init()
{
$this->add_template( 'addon_preview', dirname(__FILE__) . '/templates/addon_preview.php' );
$this->add_template( 'addon', dirname(__FILE__) . '/templates/addon.php' );
$this->add_rule('"retrieve_addonlist"', 'retrieve_addonlist');
$this->add_rule('"install_addons"', 'install_addons');
}
public function action_admin_theme_get_dashboard($handler, $theme)
{
if(isset($_SESSION['install_addons'])) {
$this->notice_installation();
}
}
/**
* Display installable addons in the plugin list
*/
public function filter_plugin_loader($existing_loader, Theme $theme)
{
// Notify about problems with the tempdir
$tmpdir = $this->tempdir();
if(!$tmpdir) {
Session::error(_t("You have no writable temporary directory. Your webserver needs to have write access to either your server's tempdir or Habari's user directory."));
}
$data = Session::get_set('install_addons', false);
// Check if we need to do something before displaying the list
$action = $_GET['action'];
switch($action)
{
case "install":
$addon = $_GET['addon'];
if(!array_key_exists($addon, $data)) {
// What did you pass us there?
Session::error("There was a problem handling the addon identifier. Maybe you used a wrong link?");
break;
}
// Get the addon!
$request = new RemoteRequest($data[$addon]->download_url);
$request->execute();
file_put_contents($tmpdir . "/download.zip", $request->get_response_body());
// Extract it to the real directory
$zip = new \ZipArchive();
$zipstatus = $zip->open($tmpdir . "/download.zip");
if($zipstatus === true) {
$addonpath = Site::get_dir("user") . '/' . $this->_type_subdirs[$data[$addon]->type] . '/' . $data[$addon]->name;
if(!is_dir($addonpath)) {
mkdir($addonpath);
}
if($zip->extractTo($addonpath)) {
$this->remove_from_set($addon);
Session::notice("Plugin now available for activation");
Utils::redirect(URL::get( 'admin', array("page" => "plugins")));
}
else {
Session::error("There was a problem extracting the zip file. Maybe your user directory is no writable?");
}
$zip->close();
}
else {
switch($zipstatus) {
case \ZipArchive::ER_NOZIP:
Session::error(_t("There was a problem retrieving the zip file"));
break;
default:
Session::error(_t("There was a problem opening the zip file"));
break;
}
}
break;
}
// Pass the addon list to the theme
$addons = array();
foreach($data as $addon) {
// Insert other checks here
$addon->habari_compatible = (version_compare(Version::get_habariversion(), $addon->habari_version) == -1) ? false : true;
if($addon->habari_compatible && $tmpdir) {
$actions = array("install" => URL::get( 'admin', array("page" => "plugins", "action" => "install", "addon" => $addon->slug)));
$addon->actions = $actions;
}
$addons[$addon->type][] = $addon;
}
$theme->addon_types = $this->_types;
$theme->addons = $addons;
$loader = $theme->fetch('addon_preview');
return $existing_loader . $loader;
}
/**
* Grab calls to yoursite.tld/install_addons
* This is where the catalog redirects to
*/
public function theme_route_install_addons($theme, $params)
{
if(isset($_POST['payload']) && !empty($_POST['payload'])) {
$payload = json_decode($_POST->raw('payload'));
foreach($payload as $addondata) {
Session::add_to_set('install_addons', $addondata, $addondata->slug);
}
}
if(!User::identify()->loggedin) {
Session::notice('You have to login to install addons');
Utils::redirect(Site::get_url('login'));
}
else {
$this->notice_installation();
}
Utils::redirect(URL::get('admin', array('page' => 'plugins')) . '#for_installation');
}
/**
* Prepare the template for the new pages we added in filter_adminhandler_post_loadplugins_main_menu
* It will automatically be included by the filename
*/
public function action_admin_theme_get_addon_preview($handler, $theme)
{
}
/**
* Determine a usable temporary directory
*/
function tempdir()
{
if(is_writable(sys_get_temp_dir())) {
return sys_get_temp_dir();
}
else if(is_writable(Site::get_dir('user'))) {
if(is_dir(Site::get_dir('user') . "/hpm_tmp")) {
return Site::get_dir('user') . "/hpm_tmp";
}
else {
mkdir(Site::get_dir('user') . "/hpm_tmp", 0755, true);
if(is_dir(Site::get_dir('user') . "/hpm_tmp")) {
return Site::get_dir('user') . "/hpm_tmp";
}
else return false;
}
}
else return false;
}
/**
* Helper function to easily get rid of a single entry in the Session
*/
function remove_from_set($slug)
{
$data = Session::get_set("install_addons");
foreach($data as $index => $entry) {
if($index == $slug) {
continue;
}
Session::add_to_set("install_addons", $entry, $index);
}
}
/**
* Helper function to avoid multiple places where one translated string is used
*/
function notice_installation()
{
Session::notice(_t("You have addons ready for installation.", __CLASS__) . " <a href='" . URL::get( 'admin', array("page" => "plugins")) . "#for_installation'>" . _t("Go to list", __CLASS__) . "</a>", 'addons_installnotice');
}
}
?>