PluginProbe
Softaculous / trunk
Softaculous vtrunk
2.4.1 2.4.0 trunk 2.0.7 2.2.1 2.2.2 2.2.4 2.2.7 2.3.9
softaculous / actions.php

actions.php in Softaculous trunk, at actions.php

357 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')){
4 exit;
5 }
6
7 function softaculous_site_actions(){
8 global $softaculous_lang, $softaculous_error, $softaculous_wp_config;
9
10 $return = array();
11
12 $request = softaculous_optREQ('request');
13
14 if(empty($request)){
15 $return['error'] = $softaculous_lang['no_req_post'];
16 echo wp_json_encode($return);
17 die();
18 }
19
20 if($request == 'update_website'){
21 $source = urldecode(softaculous_optREQ('source'));
22
23 include_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php');
24 include_once(ABSPATH.'wp-admin/includes/update.php');
25 include_once(ABSPATH.'wp-admin/includes/misc.php');
26
27 global $wp_filesystem;
28
29 $upgrade_error = array();
30
31 $wp_upgrader_skin = new WP_Upgrader_Skin();
32 $wp_upgrader_skin->done_header = true;
33
34 $wp_upgrader = new WP_Upgrader($wp_upgrader_skin);
35
36 $res = $wp_upgrader->fs_connect(array(get_home_path(), WP_CONTENT_DIR));
37 if (!$res || is_wp_error($res)){
38 $upgrade_error[] = $res;
39 }
40
41 $download = $wp_upgrader->download_package($source);
42 if (is_wp_error($download)){
43 $upgrade_error[] = $download;
44 }
45
46 $working_dir = $wp_upgrader->unpack_package($download);
47 if (is_wp_error($working_dir)){
48 $upgrade_error[] = $working_dir;
49 }
50
51 $wp_dir = trailingslashit($wp_filesystem->abspath());
52
53 if (!$wp_filesystem->copy($working_dir.'/wordpress/wp-admin/includes/update-core.php', $wp_dir.'wp-admin/includes/update-core.php', true)){
54 $wp_filesystem->delete($working_dir, true);
55
56 $upgrade_error[] = $softaculous_lang['copy_fail'];
57 }
58
59 $wp_filesystem->chmod($wp_dir.'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
60 include_once(get_home_path().'wp-admin/includes/update-core.php');
61
62 if(!function_exists('update_core')){
63 $upgrade_error[] = $softaculous_lang['call_update_fail'];
64 }
65
66 $result = update_core($working_dir, $wp_dir);
67 if(is_wp_error($result)){
68 $upgrade_error[] = $result->get_error_code();
69 }
70
71 if(!empty($upgrade_error)){
72 $return['error'] = 'error: '.implode("\n", $upgrade_error);
73 }
74
75 $return['updatedto'] = softaculous_version_wp();
76 }
77
78 if($request == 'create_post'){
79 // Create post object
80 $my_post = array(
81 'post_title' => $_POST['post_title'], //WP handles sanitization in wp_insert_post fn
82 'post_content' => $_POST['post_content'],
83 'post_status' => 'publish',
84 'post_author' => 1
85 );
86
87 // Insert the post into the database
88 $create_post_response = wp_insert_post($my_post);
89
90 $post_featured_image = softaculous_optPOST('featured_image');
91
92 if(!empty($create_post_response) && !empty($post_featured_image)){
93
94 $image_id = media_sideload_image($post_featured_image, $create_post_response, null, 'id');
95
96 if (!is_wp_error($image_id)) {
97 set_post_thumbnail($create_post_response, $image_id);
98 }
99
100 }
101
102 $return['create_post_response'] = $create_post_response;
103 }
104
105 if($request == 'delete_post'){
106
107 $post_id = softaculous_optREQ('del_post');
108
109 // Delete the post from the database
110 $return['delete_post_response'] = wp_delete_post($post_id);
111 }
112
113 if($request == 'publish_post'){
114
115 $post_id = softaculous_optREQ('post_id');
116
117 $post_data = array('ID' => $post_id, 'post_status' => 'publish');
118
119 // Delete the post from the database
120 $return['publish_post_response'] = wp_update_post($post_data);
121 }
122
123 if(softaculous_optGET('plugins') || softaculous_optGET('plugin')){
124 $plugins = urldecode(softaculous_optREQ('plugins'));
125 $arr_plugins = explode(',', $plugins);
126
127 if($request == 'activate'){//Activate
128
129 $res = softaculous_activate_plugin($arr_plugins);
130 if(!$res){
131 $return['error'] = $softaculous_lang['err_activating_pl'];
132 }
133 }elseif($request == 'deactivate'){//Deactivate
134
135 $res = softaculous_deactivate_plugin($arr_plugins);
136 if(!$res){
137 $return['error'] = $softaculous_lang['err_deactivating_pl'];
138 }
139 }elseif($request == 'delete'){//Deactivate and then Delete
140
141 $act_res = softaculous_deactivate_plugin($arr_plugins);
142 if(!$act_res){
143 $return['error'] = $softaculous_lang['err_deactivating_del_pl'];
144 }
145
146 // Delete the old list of installed plugins (otherwise delete fails)
147 wp_cache_delete('plugins', 'plugins');
148
149 $result = delete_plugins($arr_plugins);
150 if(is_wp_error($result)) {
151 $return['error'] = $result->get_error_message();
152 }elseif($result === false) {
153 $return['error'] = $softaculous_lang['err_deleting_pl'];
154 }
155 }elseif($request == 'install'){//Install Plugins
156
157 $sources = urldecode(softaculous_optREQ('sources'));
158 $arr_sources = explode(',', $sources);
159
160 $all_installed_plugins = array();
161
162 foreach($arr_plugins as $plk => $plval){
163
164 //Skip if the plugin is already installed
165 if(softaculous_is_plugin_installed($plval)){
166 continue;
167 }
168
169 $filename = basename(parse_url($arr_sources[$plk], PHP_URL_PATH));
170
171 $download_dest = $softaculous_wp_config['uploads_dir'].'/'.$filename;
172 $unzip_dest = $softaculous_wp_config['plugins_root_dir'];
173
174 softaculous_get_web_file($arr_sources[$plk], $download_dest);
175
176 if(softaculous_sfile_exists($download_dest)){
177 $res = softaculous_unzip($download_dest, $unzip_dest);
178 }
179
180 @softaculous_sunlink($download_dest);
181
182 //Activate the installed plugin(s)
183 $pl_slug = $plval;
184 if(preg_match('/(.*?)\/(.*?)\.php/is', $plval)){
185 softaculous_preg_replace('/(.*?)\/(.*?)\.php/is', $plval, $pl_slug, 1, 1);
186 }
187
188 if(empty($pl_slug)){//This is the case for the default Hello Dolly plugin that comes installed with the initial WP package
189 continue;
190 }
191
192 $all_installed_plugins[] = softaculous_get_plugin_path(ABSPATH.'wp-content/plugins/'.$pl_slug, $pl_slug);
193 }
194
195 // Delete the old list of installed plugins (otherwise activate fails)
196 wp_cache_delete('plugins', 'plugins');
197
198 //Activate the installed plugins
199 softaculous_activate_plugin($all_installed_plugins);
200
201 if(!empty($softaculous_error)){
202 $return['error'] = $softaculous_error;
203 }
204 }elseif($request == 'update'){
205
206 $plugin_name = urldecode(softaculous_optREQ('plugin'));
207 $download_link = urldecode(softaculous_optREQ('source'));
208
209 //For backward compatibility
210 if(!is_array($plugin_name)) $plugin_name = array($plugin_name);
211 if(!is_array($download_link)) $download_link = array($download_link);
212
213 $sources = urldecode(softaculous_optREQ('sources'));
214 $arr_sources = explode(',', $sources);
215
216 $arr_plugins = array_merge($plugin_name, $arr_plugins);
217 $arr_sources = array_merge($download_link, $arr_sources);
218
219 $site_url = urldecode(softaculous_optREQ('siteurl'));
220
221 foreach($arr_plugins as $plk => $plval){
222 $filename = basename(parse_url($arr_sources[$plk], PHP_URL_PATH));
223
224 $download_dest = $softaculous_wp_config['uploads_dir'].'/'.$filename;
225 $unzip_dest = $softaculous_wp_config['plugins_root_dir'];
226
227 softaculous_get_web_file($arr_sources[$plk], $download_dest);
228
229 if(softaculous_sfile_exists($download_dest)){
230 $res = softaculous_unzip($download_dest, $unzip_dest);
231 }
232
233 @softaculous_sunlink($download_dest);
234 }
235
236 // Lets visit the installation once to make the changes in the database
237 $resp = wp_remote_get($site_url);
238
239 if(!empty($softaculous_error)){
240 $return['error'] = $softaculous_error;
241 }
242 }
243 }elseif(softaculous_optGET('themes') || softaculous_optGET('theme')){
244
245 $themes = urldecode(softaculous_optREQ('themes'));
246 $arr_themes = explode(',', $themes);
247
248 $active_theme = array_keys(softaculous_get_active_theme());
249
250 if($request == 'activate' && count($arr_themes) == 1){//Activate
251
252 //Do not activate/delete the theme if it is active
253 if($active_theme[0] != $arr_themes[0]){
254 $res = softaculous_activate_theme($arr_themes);
255 if(!empty($softaculous_error)){
256 $return['error'] = $softaculous_error;
257 }
258 if(!$res){
259 $return['error'] = $softaculous_lang['err_activating_theme'];
260 }
261 }
262
263 }elseif($request == 'delete'){//Delete
264
265 //Do not delete the theme if it is active
266 foreach($arr_themes as $tk => $tv){
267 if($active_theme[0] == $tv){
268 unset($arr_themes[$tk]);
269 }
270 }
271
272 $res = softaculous_delete_theme($arr_themes);
273 if(!empty($softaculous_error)){
274 $return['error'] = $softaculous_error;
275 }
276 if(!$res){
277 $return['error'] = $softaculous_lang['err_deleting_theme'];
278 }
279
280 }elseif($request == 'install'){//Install Themes
281
282 $sources = urldecode(softaculous_optREQ('sources'));
283 $arr_sources = explode(',', $sources);
284
285 foreach($arr_themes as $thk => $thval){
286
287 //Skip if the theme is already installed
288 if(softaculous_is_theme_installed($thval)){
289 continue;
290 }
291
292 $filename = basename(parse_url($arr_sources[$thk], PHP_URL_PATH));
293
294 $download_dest = $softaculous_wp_config['uploads_dir'].'/'.$filename;
295 $unzip_dest = $softaculous_wp_config['themes_root_dir'].'/';
296
297 softaculous_get_web_file($arr_sources[$thk], $download_dest);
298
299 if(softaculous_sfile_exists($download_dest)){
300 $res = softaculous_unzip($download_dest, $unzip_dest);
301 }
302
303 @softaculous_sunlink($download_dest);
304 }
305
306 if(!empty($softaculous_error)){
307 $return['error'] = $softaculous_error;
308 }
309 }elseif($request == 'update'){//Update Theme
310
311 $theme_name = urldecode(softaculous_optREQ('theme'));
312 $download_link = urldecode(softaculous_optREQ('source'));
313
314 //For backward compatibility
315 if(!is_array($theme_name)) $theme_name = array($theme_name);
316 if(!is_array($download_link)) $download_link = array($download_link);
317
318 $sources = urldecode(softaculous_optREQ('sources'));
319 $arr_sources = explode(',', $sources);
320
321 $arr_themes = array_merge($theme_name, $arr_themes);
322 $arr_sources = array_merge($download_link, $arr_sources);
323
324 $site_url = urldecode(softaculous_optREQ('siteurl'));
325
326 foreach($arr_themes as $thk => $thval){
327 $filename = basename(parse_url($arr_sources[$thk], PHP_URL_PATH));
328
329 $download_dest = $softaculous_wp_config['uploads_dir'].'/'.$filename;
330 $unzip_dest = $softaculous_wp_config['themes_root_dir'];
331
332 softaculous_get_web_file($arr_sources[$thk], $download_dest);
333
334 if(softaculous_sfile_exists($download_dest)){
335 $res = softaculous_unzip($download_dest, $unzip_dest);
336 }
337
338 @softaculous_sunlink($download_dest);
339 }
340
341 // Lets visit the installation once to make the changes in the database
342 $resp = wp_remote_get($site_url);
343
344 if(!empty($softaculous_error)){
345 $return['error'] = $softaculous_error;
346 }
347 }
348 }
349
350 if(empty($return['error'])){
351 $return['result'] = 'done';
352 }
353
354 //Using serialize here as all_plugins contains class object which are not json_decoded in Softaculous.
355 echo wp_json_encode($return);
356
357 }