PluginProbe
Gmedia Photo Gallery / trunk
Gmedia Photo Gallery vtrunk
trunk 0.6.3 0.8.6 0.9.23 1.1.1 1.12.4 1.13.4 1.14.7 1.16.4 1.16.6 1.16.7 1.17.6 1.18.0 1.2.6 1.20.0 1.21.6 1.21.8 1.21.9 1.22.0 1.23.0 1.24.0 1.24.1 1.25.0 1.25.1 1.3.7 All 37 releases
grand-media / admin / processor / class.processor.modules.php

class.processor.modules.php in Gmedia Photo Gallery trunk, at admin/processor/class.processor.modules.php

238 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 /**
7 * GmediaProcessor_Modules
8 */
9 class GmediaProcessor_Modules extends GmediaProcessor {
10 private static $me = null;
11
12 public $modules = array();
13
14 public static function getMe() {
15 if ( null === self::$me ) {
16 self::$me = new GmediaProcessor_Modules();
17 }
18
19 return self::$me;
20 }
21
22 protected function processor() {
23 global $gmDB, $gmCore, $gmGallery, $user_ID;
24
25 if ( ! $gmCore->caps['gmedia_gallery_manage'] ) {
26 wp_die( esc_html__( 'You are not allowed to manage gmedia galleries', 'grand-media' ) );
27 }
28 if ( ! $gmCore->caps['gmedia_module_manage'] ) {
29 wp_die( esc_html__( 'You are not allowed to manage gmedia modules', 'grand-media' ) );
30 }
31
32 include_once GMEDIA_ABSPATH . 'admin/pages/modules/functions.php';
33
34 if ( isset( $_POST['module_preset_save'] ) || isset( $_POST['module_preset_save_default'] ) || isset( $_POST['module_preset_save_global'] ) ) {
35 check_admin_referer( 'GmediaGallery' );
36
37 if ( isset( $_POST['preview_bgcolor'] ) ) {
38 $gmGallery->options['preview_bgcolor'] = $gmCore->sanitize_hex_color( $gmCore->_post( 'preview_bgcolor' ), 'ffffff' );
39 update_option( 'gmediaOptions', $gmGallery->options );
40 }
41
42 $edit_preset = (int) $gmCore->_get( 'preset' );
43 do {
44 $term = $gmCore->_post( 'term' );
45
46 if ( isset( $term['query'] ) ) {
47 wp_parse_str( $term['query'], $_query );
48 update_user_option( $user_ID, 'gmedia_preset_demo_query_args', $_query );
49 }
50
51 if ( ( (int) $term['global'] !== $user_ID ) && ! $gmCore->caps['gmedia_edit_others_media'] ) {
52 $this->error[] = esc_html__( 'You are not allowed to edit others media', 'grand-media' );
53 break;
54 }
55
56 if ( empty( $term['module'] ) ) {
57 $this->error[] = esc_html__( 'Something goes wrong... Choose module, please', 'grand-media' );
58 break;
59 }
60
61 $taxonomy = 'gmedia_module';
62 if ( isset( $_POST['module_preset_save_default'] ) || isset( $_POST['module_preset_save_global'] ) ) {
63 $term['name'] = '[' . $term['module'] . ']';
64 if ( current_user_can( 'manage_options' ) && isset( $_POST['module_preset_save_global'] ) ) {
65 $term['global'] = '0';
66 }
67 $edit_preset = $gmDB->term_exists( $term['name'], $taxonomy, $term['global'] );
68 $term['term_id'] = $edit_preset;
69 } else {
70 $term['name'] = trim( $term['name'] );
71 if ( empty( $term['name'] ) ) {
72 $term['name'] = current_time( 'mysql' );
73 }
74 if ( $gmCore->is_digit( $term['name'] ) ) {
75 $this->error[] = esc_html__( "Preset name can't be only digits", 'grand-media' );
76 break;
77 }
78 $term['name'] = '[' . $term['module'] . '] ' . $term['name'];
79
80 if ( $edit_preset && ! $gmDB->term_exists( $edit_preset ) ) {
81 $this->error[] = esc_html__( 'A term with the id provided does not exists', 'grand-media' );
82 $edit_preset = false;
83 }
84 $term_id = $gmDB->term_exists( $term['name'], $taxonomy, $term['global'] );
85 if ( $term_id ) {
86 if ( $term_id !== $edit_preset ) {
87 $this->error[] = esc_html__( 'A term with the name provided already exists', 'grand-media' );
88 break;
89 }
90 }
91 }
92
93 $module_settings = $gmCore->_post( 'module', array() );
94 foreach ( $module_settings as &$setting ) {
95 if ( is_string( $setting ) && 7 === strlen( $setting ) && '#' === $setting[0] ) {
96 $setting = ltrim( $setting, '#' );
97 }
98 }
99 $module_path = $gmCore->get_module_path( $term['module'] );
100 $default_options = array();
101 if ( is_file( $module_path['path'] . '/settings.php' ) ) {
102 /** @noinspection PhpIncludeInspection */
103 include $module_path['path'] . '/settings.php';
104 } else {
105 // translators: module name.
106 $this->error[] = sprintf( esc_html__( 'Can\'t load data from `%s` module' , 'grand-media'), esc_html( $term['module'] ) );
107 break;
108 }
109 $term['description'] = $gmCore->array_replace_recursive( $default_options, $module_settings );
110 $term['status'] = $term['module'];
111
112 if ( $edit_preset ) {
113 $term_id = $gmDB->update_term( $edit_preset, $term );
114 } else {
115 $term_id = $gmDB->insert_term( $term['name'], $taxonomy, $term );
116 }
117 if ( is_wp_error( $term_id ) ) {
118 $this->error[] = $term_id->get_error_message();
119 break;
120 }
121
122 if ( $edit_preset ) {
123 // translators: ID.
124 $this->msg[] = sprintf( esc_html__( 'Preset #%d successfully saved', 'grand-media' ), $term_id );
125 } else {
126 $location = add_query_arg( array( 'preset' => $term_id, 'message' => 'save' ), $this->url );
127 set_transient( 'gmedia_new_preset_id', $term_id, 60 );
128 wp_safe_redirect( $location );
129 exit;
130 }
131 } while ( 0 );
132 }
133 $term_id = $gmCore->_get( 'preset' );
134 if ( ( 'save' === $gmCore->_get( 'message' ) ) && $term_id ) {
135 $gmedia_new_preset_id = get_transient( 'gmedia_new_preset_id' );
136 if ( false !== $gmedia_new_preset_id ) {
137 gmedia_delete_transients( 'gm_cache' );
138 delete_transient( 'gmedia_new_preset_id' );
139 // translators: ID.
140 $this->msg[] = sprintf( esc_html__( 'Preset #%d successfully saved', 'grand-media' ), $term_id );
141 }
142 }
143
144 if ( isset( $_FILES['modulezip']['tmp_name'] ) ) {
145 if ( ! empty( $_FILES['modulezip']['tmp_name'] ) && isset( $_FILES['modulezip']['name'] ) ) {
146 check_admin_referer( 'gmedia_module', '_wpnonce_module' );
147 if ( ! current_user_can( 'manage_options' ) ) {
148 wp_die( esc_html__( 'You are not allowed to install module ZIP', 'grand-media' ) );
149 }
150 $to_folder = $gmCore->upload['path'] . '/' . $gmGallery->options['folder']['module'] . '/';
151 if ( ! wp_mkdir_p( $to_folder ) ) {
152 // translators: dirname.
153 $this->error[] = sprintf( esc_html__( 'Unable to create directory %s. Is its parent directory writable by the server?', 'grand-media' ), esc_html( $to_folder ) );
154
155 return;
156 }
157 if ( ! wp_is_writable( $to_folder ) ) {
158 $gmCore->file_chmod( $to_folder, 0755 );
159 if ( ! wp_is_writable( $to_folder ) ) {
160 //wp_delete_file( $_FILES['modulezip']['tmp_name'] );
161 // translators: dirname.
162 $this->error[] = sprintf( esc_html__( 'Directory %s is not writable by the server.', 'grand-media' ), esc_html( $to_folder ) );
163
164 return;
165 }
166 }
167 $modulezip_name = sanitize_text_field( wp_unslash( $_FILES['modulezip']['name'] ) );
168 $modulezip_tmp_name = sanitize_text_field( wp_unslash( $_FILES['modulezip']['tmp_name'] ) );
169 $filename = wp_unique_filename( $to_folder, $modulezip_name );
170
171 // Move the file to the modules dir.
172 // phpcs:ignore Generic.PHP.ForbiddenFunctions.Found -- This authenticated module installer uses its own directory; retain PHP's genuine HTTP-upload validation when moving the package.
173 if ( false === @move_uploaded_file( $modulezip_tmp_name, $to_folder . $filename ) ) {
174 // translators: path.
175 $this->error[] = sprintf( esc_html__( 'The uploaded file could not be moved to %s', 'grand-media' ), esc_html( $to_folder . $filename ) );
176 } else {
177 global $wp_filesystem;
178 // Is a filesystem accessor setup?
179 if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) {
180 require_once ABSPATH . 'wp-admin/includes/file.php';
181 WP_Filesystem();
182 }
183 if ( ! is_object( $wp_filesystem ) ) {
184 $result = new WP_Error( 'fs_unavailable', esc_html__( 'Could not access filesystem.', 'grand-media' ) );
185 } elseif ( $wp_filesystem->errors->get_error_code() ) {
186 $result = new WP_Error( 'fs_error', esc_html__( 'Filesystem error', 'grand-media' ), $wp_filesystem->errors );
187 } else {
188 $maybe_folder_dir = basename( $modulezip_name, '.zip' );
189 $maybe_folder_dir = sanitize_file_name( $maybe_folder_dir );
190 if ( $maybe_folder_dir && is_dir( $to_folder . $maybe_folder_dir ) ) {
191 $gmCore->delete_folder( $to_folder . $maybe_folder_dir );
192 }
193 $result = unzip_file( $to_folder . $filename, $to_folder );
194 }
195 // Once extracted, delete the package.
196 wp_delete_file( $to_folder . $filename );
197 if ( is_wp_error( $result ) ) {
198 $this->error[] = $result->get_error_message();
199 } else {
200 // translators: filename.
201 $this->msg[] = sprintf( esc_html__( "The `%s` file unzipped to module's directory", 'grand-media' ), esc_html( $filename ) );
202 }
203 }
204 gmedia_delete_transients( 'gm_cache' );
205 } else {
206 $this->error[] = esc_html__( 'No file specified', 'grand-media' );
207 }
208 }
209
210 if ( isset( $_GET['delete_module'] ) ) {
211 if ( $gmCore->_get( '_wpnonce_module_delete' ) ) {
212 $mfold = preg_replace( '/[^a-z0-9_-]+/i', '_', $gmCore->_get( 'delete_module', '' ) );
213 $mpath = "{$gmCore->upload['path']}/{$gmGallery->options['folder']['module']}/{$mfold}";
214 if ( $mfold && file_exists( $mpath ) ) {
215 check_admin_referer( 'gmedia_module_delete', '_wpnonce_module_delete' );
216 $gmCore->delete_folder( $mpath );
217 $location = remove_query_arg( array( '_wpnonce_module_delete' ) );
218 // translators: path.
219 set_transient( 'gmedia_module_deleted', sprintf( esc_html__( 'The `%s` module folder was deleted', 'grand-media' ), esc_html( $mpath ) ), 60 );
220 wp_safe_redirect( $location );
221 }
222 } elseif ( false !== ( $message = get_transient( 'gmedia_module_deleted' ) ) ) {
223 gmedia_delete_transients( 'gm_cache' );
224 delete_transient( 'gmedia_module_deleted' );
225 $this->msg[] = $message;
226 }
227 }
228
229 $this->modules = get_gmedia_modules();
230 wp_clear_scheduled_hook( 'gmedia_modules_update' );
231 wp_schedule_event( time(), 'daily', 'gmedia_modules_update' );
232 $gmCore->modules_update( $this->modules );
233 }
234 }
235
236 global $gmProcessorModules;
237 $gmProcessorModules = GmediaProcessor_Modules::getMe();
238