PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.3
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.3
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-system-utility.php

class-mainwp-system-utility.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.3, at class/class-mainwp-system-utility.php

1,634 lines 54.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP System Utility Helper
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.WP.AlternativeFunctions, WordPress.PHP.NoSilencedErrors, Generic.Metrics.CyclomaticComplexity -- Using cURL functions.
11
12 /**
13 * Class MainWP_System_Utility
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_System_Utility { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
18
19 /**
20 * Private static variable to hold the single instance of the class.
21 *
22 * @static
23 *
24 * @var mixed Default null
25 */
26 private static $instance = null;
27
28 /**
29 * Method instance()
30 *
31 * Create a public static instance.
32 *
33 * @static
34 * @return MainWP_Post_Handler
35 */
36 public static function instance() {
37 if ( null === static::$instance ) {
38 static::$instance = new self();
39 }
40 return static::$instance;
41 }
42
43
44 /**
45 * Method get_class_name()
46 *
47 * Get Class Name.
48 *
49 * @return object
50 */
51 public static function get_class_name() {
52 return __CLASS__;
53 }
54
55 /**
56 * Method is_admin()
57 *
58 * Check if current user is an administrator.
59 *
60 * @return boolean True|False.
61 */
62 public static function is_admin() {
63
64 /**
65 * Current user global.
66 *
67 * @global string
68 */
69 global $current_user;
70 if ( empty( $current_user->ID ) ) {
71 return false;
72 }
73
74 if ( ( property_exists( $current_user, 'wp_user_level' ) && 10 === (int) $current_user->wp_user_level ) || ( isset( $current_user->user_level ) && 10 === (int) $current_user->user_level ) || static::current_user_has_role( 'administrator' ) ) {
75 return true;
76 }
77
78 return false;
79 }
80
81 /**
82 * Method current_user_has_role()
83 *
84 * Check if the user has role.
85 *
86 * @param array|string $roles role or array of roles to check.
87 * @param object|null $user user check.
88 *
89 * @return bool true|false If the user is administrator (Level 10), return true, if not, return false.
90 */
91 public static function current_user_has_role( $roles, $user = null ) {
92
93 if ( null === $user ) {
94 $user = wp_get_current_user();
95 }
96
97 if ( empty( $user ) || empty( $user->ID ) ) {
98 return false;
99 }
100
101 if ( is_string( $roles ) ) {
102 $allowed_roles = array( $roles );
103 } elseif ( is_array( $roles ) ) {
104 $allowed_roles = $roles;
105 } else {
106 return false;
107 }
108
109 if ( array_intersect( $allowed_roles, $user->roles ) ) {
110 return true;
111 }
112
113 return false;
114 }
115
116 /**
117 * Method get_primary_backup()
118 *
119 * Check if using Legacy Backup Solution.
120 *
121 * @return mixed False|$enable_legacy_backup.
122 */
123 public static function get_primary_backup() {
124 $enable_legacy_backup = get_option( 'mainwp_enableLegacyBackupFeature' );
125 if ( ! $enable_legacy_backup ) {
126 return get_option( 'mainwp_primaryBackup', false );
127 }
128 return false;
129 }
130
131 /**
132 * Method get_notification_email()
133 *
134 * Check if user wants to recieve MainWP Notification Emails.
135 *
136 * @return mixed null|User Email Address.
137 *
138 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
139 */
140 public static function get_notification_email() {
141 return get_option( 'admin_email' );
142 }
143
144 /**
145 * Method get_base_dir()
146 *
147 * Get the base upload directory.
148 *
149 * @return string basedir/
150 */
151 public static function get_base_dir() {
152 $upload_dir = wp_upload_dir();
153
154 return $upload_dir['basedir'] . DIRECTORY_SEPARATOR;
155 }
156
157 /**
158 * Method get_icons_dir()
159 *
160 * Get MainWP icons directory,
161 * if it doesn't exist create it.
162 *
163 * @return array $dir, $url
164 */
165 public static function get_icons_dir() {
166 static::get_wp_file_system();
167
168 /**
169 * WordPress files system object.
170 *
171 * @global object
172 */
173 global $wp_filesystem;
174
175 $dirs = static::get_mainwp_dir();
176 $dir = $dirs[0] . 'icons' . DIRECTORY_SEPARATOR;
177 $url = $dirs[1] . 'icons/';
178 if ( ! $wp_filesystem->exists( $dir ) ) {
179 $wp_filesystem->mkdir( $dir, 0777 );
180 }
181 if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
182 $wp_filesystem->touch( $dir . 'index.php' );
183 }
184 return array( $dir, $url );
185 }
186
187 /**
188 * Method touch().
189 *
190 * If the file does not exist, it will be created.
191 *
192 * @param string $filename File name.
193 */
194 public static function touch( $filename ) {
195 $hasWPFileSystem = static::get_wp_file_system();
196 /**
197 * WordPress files system object.
198 *
199 * @global object
200 */
201 global $wp_filesystem;
202 if ( $hasWPFileSystem && ! empty( $wp_filesystem ) ) {
203 if ( ! $wp_filesystem->exists( $filename ) ) {
204 $wp_filesystem->touch( $filename );
205 }
206 } elseif ( ! file_exists( $filename ) ) { //phpcs:ignore -- ok.
207 touch( $filename ); //phpcs:ignore -- ok.
208 }
209 }
210
211 /**
212 * Method is_writable().
213 *
214 * @param string $file The file.
215 */
216 public static function is_writable( $file ) {
217 $hasWPFileSystem = static::get_wp_file_system();
218 /**
219 * WordPress files system object.
220 *
221 * @global object
222 */
223 global $wp_filesystem;
224
225 $is_writable = true;
226 if ( $hasWPFileSystem && ! empty( $wp_filesystem ) ) {
227 if ( ! $wp_filesystem->is_writable( $file ) ) {
228 $is_writable = false;
229 }
230 } elseif ( ! is_writable( $file ) ) { //phpcs:ignore -- ok.
231 $is_writable = false;
232 }
233 return $is_writable;
234 }
235
236 /**
237 * Method get_mainwp_dir()
238 *
239 * Get the MainWP directory,
240 * if it doesn't exist create it.
241 *
242 * @param string|null $subdir mainwp sub diectories.
243 * @param bool $direct_access Return true if Direct access file system. Default: false.
244 *
245 * @return array $dir, $url
246 */
247 public static function get_mainwp_dir( $subdir = null, $direct_access = false ) {
248 static::get_wp_file_system();
249
250 /**
251 * WordPress files system object.
252 *
253 * @global object
254 */
255 global $wp_filesystem;
256
257 $upload_dir = wp_upload_dir();
258 $dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'mainwp' . DIRECTORY_SEPARATOR;
259 $url = $upload_dir['baseurl'] . '/mainwp/';
260 if ( ! $wp_filesystem->exists( $dir ) ) {
261 $wp_filesystem->mkdir( $dir, 0777 );
262 }
263 if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) {
264 $wp_filesystem->touch( $dir . 'index.php' );
265 }
266
267 if ( ! empty( $subdir ) && ! stristr( $subdir, '..' ) ) {
268 $newdir = $dir . $subdir . DIRECTORY_SEPARATOR;
269 $url = $url . $subdir . '/';
270
271 if ( ! $wp_filesystem->exists( $newdir ) ) {
272 $wp_filesystem->mkdir( $newdir, 0777 );
273 }
274
275 if ( $direct_access ) {
276 if ( ! $wp_filesystem->exists( trailingslashit( $newdir ) . 'index.php' ) ) {
277 $wp_filesystem->touch( trailingslashit( $newdir ) . 'index.php' );
278 }
279 if ( $wp_filesystem->exists( trailingslashit( $newdir ) . '.htaccess' ) ) {
280 $wp_filesystem->delete( trailingslashit( $newdir ) . '.htaccess' );
281 }
282 } elseif ( ! $wp_filesystem->exists( trailingslashit( $newdir ) . '.htaccess' ) ) {
283 $wp_filesystem->put_contents( trailingslashit( $newdir ) . '.htaccess', 'deny from all' );
284 }
285 return array( $newdir, $url );
286 }
287
288 return array( $dir, $url );
289 }
290
291 /**
292 * Method get_mainwp_sub_dir()
293 *
294 * Get the MainWP directory,
295 * if it doesn't exist create it.
296 *
297 * @param string|null $subdir mainwp sub diectories.
298 * @param bool $direct_access Return true if Direct access file system. Default: false.
299 *
300 * @return string $dir mainwp sub-directory.
301 */
302 public static function get_mainwp_sub_dir( $subdir = null, $direct_access = false ) {
303 $dirs = static::get_mainwp_dir( $subdir, $direct_access );
304 return $dirs[0];
305 }
306
307 /**
308 * Method get_download_dir()
309 *
310 * @param mixed $what What url.
311 * @param mixed $filename File Name.
312 *
313 * @return string Download URL.
314 */
315 public static function get_download_url( $what, $filename ) {
316 $specificDir = static::get_mainwp_specific_dir( $what );
317 $mwpDir = static::get_mainwp_dir();
318 $mwpDir = $mwpDir[0];
319 $fullFile = $specificDir . $filename;
320
321 return admin_url( '?sig=' . static::get_download_sig( $fullFile ) . '&mwpdl=' . rawurlencode( str_replace( $mwpDir, '', $fullFile ) ) );
322 }
323
324
325 /**
326 * Method get_download_sig()
327 *
328 * @param string $fullFile File Name.
329 *
330 * @return string Sig Download URL.
331 */
332 public static function get_download_sig( $fullFile ) {
333 $key_value = uniqid( 'sig_', true ) . filesize( $fullFile ) . time();
334 $secret_value = uniqid( 'sig_secret_', true ) . filesize( $fullFile ) . time();
335
336 $hashkey = wp_hash( $key_value );
337
338 $sig_values = array(
339 'sig' => md5( filesize( $fullFile ) ), // NOSONAR - safe for sig file size.
340 'key_value' => $key_value,
341 'hash_key' => $secret_value,
342 );
343
344 set_site_transient( 'mainwp_fdl_' . $hashkey, $secret_value, 3 * HOUR_IN_SECONDS );
345
346 $sig_values = wp_json_encode( $sig_values );
347 $sig_values = rawurlencode( $sig_values );
348 return $sig_values;
349 }
350
351
352 /**
353 * Method valid_download_sig()
354 *
355 * @param string $file File Name.
356 * @param string $sig download.
357 *
358 * @return bool true|false.
359 */
360 public static function valid_download_sig( $file, $sig ) {
361
362 $sig = rawurldecode( $sig );
363 $value = json_decode( $sig, true );
364
365 if ( ! is_array( $value ) || empty( $value['key_value'] ) || empty( $value['sig'] ) || md5( filesize( $file ) ) !== $value['sig'] ) { // NOSONAR - it's safe for size matching, file in uploads folder.
366 return false;
367 }
368
369 $hash_key = wp_hash( $value['key_value'] );
370 $secure_key = get_site_transient( 'mainwp_fdl_' . $hash_key );
371
372 if ( empty( $secure_key ) || empty( $value['hash_key'] ) || ! hash_equals( $secure_key, $value['hash_key'] ) ) {
373 return false;
374 }
375
376 return true;
377 }
378
379 /**
380 * Method get_mainwp_specific_dir()
381 *
382 * Get MainWP Specific directory,
383 * if it doesn't exist create it.
384 *
385 * Update .htaccess.
386 *
387 * @param null $dir Current MainWP directory.
388 *
389 * @return string $newdir
390 *
391 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
392 */
393 public static function get_mainwp_specific_dir( $dir = null ) { // phpcs:ignore -- NOSONAR - complex.
394 if ( MainWP_System::instance()->is_single_user() ) {
395 $userid = 0;
396 } else {
397
398 /**
399 * Current user global.
400 *
401 * @global string
402 */
403 global $current_user;
404
405 $userid = $current_user->ID;
406 }
407
408 $hasWPFileSystem = static::get_wp_file_system();
409
410 global $wp_filesystem;
411
412 $dirs = static::get_mainwp_dir();
413
414 $userdir = $dirs[0] . $userid;
415 $newdir = $userdir;
416
417 if ( '/' === $dir || null === $dir ) {
418 $newdir .= DIRECTORY_SEPARATOR;
419 } else {
420 $newdir .= DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
421 }
422
423 if ( $hasWPFileSystem && ! empty( $wp_filesystem ) ) {
424
425 // need to check user dir first.
426 if ( ! $wp_filesystem->is_dir( $userdir ) ) {
427 $wp_filesystem->mkdir( $userdir, 0777 );
428 }
429
430 if ( ! $wp_filesystem->is_dir( $newdir ) ) {
431 $wp_filesystem->mkdir( $newdir, 0777 );
432 }
433
434 if ( ! empty( $dirs[0] ) . $userid && ! $wp_filesystem->exists( trailingslashit( $dirs[0] . $userid ) . '.htaccess' ) ) {
435 $file_htaccess = trailingslashit( $dirs[0] . $userid ) . '.htaccess';
436 $wp_filesystem->put_contents( $file_htaccess, 'deny from all' );
437 }
438 } else {
439
440 // need to check user dir first.
441 if ( ! file_exists( $userdir ) ) {
442 mkdir( $userdir, 0777, true ); // NOSONAR - @newdir is valid.
443 }
444
445 if ( ! file_exists( $newdir ) ) {
446 mkdir( $newdir, 0777, true ); // NOSONAR - @newdir is valid.
447 }
448
449 if ( ! empty( $dirs[0] ) . $userid && ! file_exists( trailingslashit( $dirs[0] . $userid ) . '.htaccess' ) ) {
450 $file = fopen( trailingslashit( $dirs[0] . $userid ) . '.htaccess', 'w+' );
451 fwrite( $file, 'deny from all' );
452 fclose( $file );
453 }
454 }
455
456 return $newdir;
457 }
458
459 /**
460 * Method get_mainwp_specific_url()
461 *
462 * Get MainWP specific URL.
463 *
464 * @param mixed $dir MainWP Directory.
465 *
466 * @return string MainWP URL.
467 *
468 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
469 */
470 public static function get_mainwp_specific_url( $dir ) {
471 if ( MainWP_System::instance()->is_single_user() ) {
472 $userid = 0;
473 } else {
474
475 /**
476 * Current user global.
477 *
478 * @global string
479 */
480 global $current_user;
481
482 $userid = $current_user->ID;
483 }
484 $dirs = static::get_mainwp_dir();
485
486 return $dirs[1] . $userid . '/' . $dir . '/';
487 }
488
489 /**
490 * Method get_mainwp_dir_allow_access()
491 *
492 * Get MainWP specific sub folder allow access.
493 *
494 * @param mixed $sub_dir MainWP Sub Directory.
495 */
496 public static function get_mainwp_dir_allow_access( $sub_dir ) {
497 $dirs = static::get_mainwp_dir( $sub_dir, false );
498 if ( $dirs ) {
499 static::get_wp_file_system();
500 global $wp_filesystem;
501 if ( $wp_filesystem ) {
502 // to fix issue of do not allow access.
503 $newdir = $dirs[0];
504 $content = "Order allow,deny\r\nAllow from all";
505 // check if the htaccess is deny access all.
506 if ( $wp_filesystem->exists( trailingslashit( $newdir ) . '.htaccess' ) ) {
507 if ( $wp_filesystem->size( trailingslashit( $newdir ) . '.htaccess' ) < 25 ) { // 25 bytes: deny from all.
508 // update the htaccess file to allow direct access.
509 $wp_filesystem->put_contents( trailingslashit( $newdir ) . '.htaccess', $content );
510 }
511 } else {
512 // update the htaccess file to allow direct access.
513 $wp_filesystem->put_contents( trailingslashit( $newdir ) . '.htaccess', $content );
514 }
515 }
516 }
517 return $dirs;
518 }
519
520
521 /**
522 * Method get_wp_file_system()
523 *
524 * Get WP file system & define Global Variable FS_METHOD.
525 *
526 * @return boolean $init True.
527 */
528 public static function get_wp_file_system() {
529
530 /**
531 * WordPress files system object.
532 *
533 * @global object
534 */
535 global $wp_filesystem;
536
537 if ( empty( $wp_filesystem ) ) {
538 ob_start();
539 if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) {
540 include_once ABSPATH . '/wp-admin/includes/screen.php'; // NOSONAR - WP compatible.
541 }
542 if ( file_exists( ABSPATH . '/wp-admin/includes/template.php' ) ) {
543 include_once ABSPATH . '/wp-admin/includes/template.php'; // NOSONAR - WP compatible.
544 }
545 include_once ABSPATH . 'wp-admin/includes/file.php'; // NOSONAR - WP compatible.
546
547 if ( ! function_exists( 'wp_create_nonce' ) ) {
548 include_once ABSPATH . WPINC . '/pluggable.php'; // NOSONAR - WP compatible.
549 }
550
551 $creds = request_filesystem_credentials( 'test' );
552 ob_end_clean();
553 if ( empty( $creds ) ) {
554
555 /**
556 * Define WordPress File system.
557 *
558 * @const ( bool ) Default: true
559 * @source https://code-reference.mainwp.com/classes/MainWP.Dashboard.MainWP_System_Utility.html
560 */
561 define( 'FS_METHOD', 'direct' );
562 }
563 $init = \WP_Filesystem( $creds );
564 } else {
565 $init = true;
566 }
567
568 return $init;
569 }
570
571 /**
572 * Method can_edit_website()
573 *
574 * Check if current user can edit Child Site.
575 *
576 * @param mixed $website Child Site.
577 *
578 * @return mixed true|false|userid
579 *
580 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
581 */
582 public static function can_edit_website( &$website ) {
583 if ( empty( $website ) ) {
584 return false;
585 }
586
587 if ( MainWP_System::instance()->is_single_user() ) {
588 return true;
589 }
590
591 /**
592 * Current user global.
593 *
594 * @global string
595 */
596 global $current_user;
597
598 return $website->userid === $current_user->ID;
599 }
600
601 /**
602 * Gets site tags
603 *
604 * @param array $item Array containing child site data.
605 * @param bool $client_tag It is client tags or not.
606 *
607 * @return mixed Single Row Classes Item.
608 */
609 public static function get_site_tags( $item, $client_tag = false ) { // phpcs:ignore -- NOSONAR - complex.
610
611 if ( ! is_array( $item ) || ! isset( $item['wpgroups'] ) ) {
612 return '';
613 }
614
615 $href = 'admin.php?page=managesites&g=';
616 if ( $client_tag ) {
617 $href = 'admin.php?page=ManageClients&tags=';
618 }
619
620 $groups_colors = '';
621 if ( isset( $item['wpgroups_colors'] ) ) {
622 $groups_colors = explode( ',', $item['wpgroups_colors'] );
623 }
624
625 $tags = '';
626 $tags_labels = '';
627
628 if ( isset( $item['wpgroups'] ) && ! empty( $item['wpgroups'] ) ) {
629
630 if ( $client_tag ) {
631 $tags_filter = static::client_tags_filter( $item );
632 $tags = $tags_filter['wpgroups'];
633 $tags_ids = $tags_filter['wpgroupids'];
634 } else {
635 $tags = $item['wpgroups'];
636 $tags = explode( ',', $tags );
637 $tags_ids = $item['wpgroupids'];
638 $tags_ids = explode( ',', $tags_ids );
639 }
640
641 if ( is_array( $tags ) ) {
642 foreach ( $tags as $idx => $tag ) {
643 $tag = trim( $tag );
644 $tagc = '';
645
646 // to improved db query.
647 if ( is_array( $groups_colors ) && isset( $groups_colors[ $idx ] ) ) {
648 $tagc = $groups_colors[ $idx ];
649 } else {
650 $tagx = MainWP_DB_Common::instance()->get_group_by_name( $tag );
651 $tagc = is_object( $tagx ) && '' !== $tagx->color ? $tagx->color : '';
652 }
653
654 if ( '' !== $tagc ) {
655 $tag_a_style = 'style="color:#fff!important;opacity:1;"';
656 $tag_style = 'style="background-color:' . esc_html( $tagc ) . '"';
657 } else {
658 $tag_a_style = '';
659 $tag_style = '';
660 }
661
662 if ( isset( $tags_ids[ $idx ] ) && ! empty( $tags_ids[ $idx ] ) ) {
663 $tag_id = $tags_ids[ $idx ];
664 $tags_labels .= '<span ' . $tag_style . ' class="ui tag mini label"><a ' . $tag_a_style . ' href="' . esc_url( $href . $tag_id ) . '">' . esc_html( $tag ) . '</a></span>';
665 } else {
666 $tags_labels .= '<span ' . $tag_style . ' class="ui tag mini label">' . esc_html( $tag ) . '</span>';
667 }
668 }
669 }
670 }
671 return $tags_labels;
672 }
673
674 /**
675 * Gets site tags
676 *
677 * @param array $item Array containing child site data.
678 *
679 * @return mixed Single Row Classes Item.
680 */
681 public static function get_site_tags_belong( $item ) { // phpcs:ignore -- NOSONAR - complex.
682
683 if ( ! is_array( $item ) || ! isset( $item['wpgroups_belong'] ) ) {
684 return static::get_site_tags( $item );
685 }
686
687 $href = 'admin.php?page=managesites&g=';
688
689 $tags = '';
690 $tags_labels = '';
691
692 if ( isset( $item['wpgroups_belong'] ) && ! empty( $item['wpgroups_belong'] ) ) {
693
694 $tags = $item['wpgroups_belong'];
695 $tags = explode( ',', $tags );
696 $tags_ids = $item['wpgroupids_belong'];
697 $tags_ids = explode( ',', $tags_ids );
698
699 $tags_colors = explode( ',', $item['wpgroupcolors_belong'] );
700
701 if ( is_array( $tags ) ) {
702 foreach ( $tags as $idx => $tag ) {
703 $tag = trim( $tag );
704 $tagc = $tags_colors[ $idx ];
705
706 if ( '' !== $tagc ) {
707 $tag_a_style = 'style="color:#fff!important;opacity:1;"';
708 $tag_style = 'style="background-color:' . esc_html( $tagc ) . '"';
709 } else {
710 $tag_a_style = '';
711 $tag_style = '';
712 }
713
714 if ( isset( $tags_ids[ $idx ] ) && ! empty( $tags_ids[ $idx ] ) ) {
715 $tag_id = $tags_ids[ $idx ];
716 $tags_labels .= '<span ' . $tag_style . ' class="ui tag mini label"><a ' . $tag_a_style . ' href="' . esc_url( $href . $tag_id ) . '">' . esc_html( $tag ) . '</a></span>';
717 } else {
718 $tags_labels .= '<span ' . $tag_style . ' class="ui tag mini label">' . esc_html( $tag ) . '</span>';
719 }
720 }
721 }
722 }
723 return $tags_labels;
724 }
725
726 /**
727 * Filter client tags
728 *
729 * @param array $item Array containing tags.
730 *
731 * @return mixed Single Row Classes Item.
732 */
733 public static function client_tags_filter( $item ) {
734 $tags = $item['wpgroups'];
735 $tags = explode( ',', $tags );
736 $tags = array_values( array_unique( $tags ) );
737
738 $tags_ids = $item['wpgroupids'];
739 $tags_ids = explode( ',', $tags_ids );
740 $tags_ids = array_values( array_unique( $tags_ids ) );
741
742 $return = array();
743
744 $return['wpgroups'] = $tags;
745 $return['wpgroupids'] = $tags_ids;
746 return $return;
747 }
748
749 /**
750 * Method is_suspended_site()
751 *
752 * Check if enable site.
753 *
754 * @param mixed $website The website.
755 */
756 public static function is_suspended_site( $website = false ) {
757 if ( empty( $website ) ) {
758 return true; // empty so return as suspended.
759 }
760 if ( is_array( $website ) ) {
761 return '1' === $website['suspended'];
762 } elseif ( is_object( $website ) ) {
763 if ( ! property_exists( $website, 'suspended' ) && property_exists( $website, 'id' ) ) {
764 $website = MainWP_DB::instance()->get_website_by_id( $website->id );
765 }
766 if ( property_exists( $website, 'suspended' ) ) {
767 return '1' === $website->suspended;
768 }
769 } elseif ( is_numeric( $website ) ) {
770 $siteId = $website;
771 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
772 if ( $website ) {
773 return static::is_suspended_site( $website );
774 }
775 }
776 return false;
777 }
778
779 /**
780 * Method get_current_wpid()
781 *
782 * Get current Child Site ID.
783 *
784 * @return string $current_user->current_site_id Current Child Site ID.
785 */
786 public static function get_current_wpid() {
787
788 /**
789 * Current user global.
790 *
791 * @global string
792 */
793 global $current_user;
794
795 return $current_user->current_site_id;
796 }
797
798 /**
799 * Method set_current_wpid()
800 *
801 * Set the current Child Site ID.
802 *
803 * @param mixed $wpid Child Site ID.
804 */
805 public static function set_current_wpid( $wpid ) {
806
807 /**
808 * Current user global.
809 *
810 * @global string
811 */
812 global $current_user;
813
814 $current_user->current_site_id = $wpid;
815 }
816
817 /**
818 * Method get_page_id()
819 *
820 * Get current Page ID.
821 *
822 * @param null $screen Current Screen ID.
823 *
824 * @return string $page Current page ID.
825 */
826 public static function get_page_id( $screen = null ) {
827
828 if ( empty( $screen ) ) {
829 $screen = get_current_screen();
830 } elseif ( is_string( $screen ) ) {
831 $screen = convert_to_screen( $screen );
832 }
833
834 if ( ! isset( $screen->id ) ) {
835 return '';
836 }
837
838 return $screen->id;
839 }
840
841 /**
842 * Method get_child_response()
843 *
844 * Get response from Child Site.
845 *
846 * @param mixed $data Data to process.
847 *
848 * @return json $data|true.
849 */
850 public static function get_child_response( $data ) { // phpcs:ignore -- NOSONAR - complex.
851 $resp = json_decode( $data, true );
852
853 if ( is_array( $resp ) ) {
854 if ( isset( $resp['error'] ) ) {
855 $resp['error'] = MainWP_Utility::esc_content( $resp['error'] );
856 }
857
858 if ( isset( $resp['message'] ) && is_string( $resp['message'] ) ) {
859 $resp['message'] = MainWP_Utility::esc_content( $resp['message'] );
860 }
861
862 if ( isset( $resp['error_message'] ) ) {
863 $resp['error_message'] = MainWP_Utility::esc_content( $resp['error_message'] );
864 }
865
866 if ( isset( $resp['notices'] ) ) {
867 if ( is_string( $resp['notices'] ) ) {
868 $resp['notices'] = MainWP_Utility::esc_content( $resp['notices'] );
869 } elseif ( is_array( $resp['notices'] ) ) {
870 $notices = array();
871 foreach ( $resp['notices'] as $noti ) {
872 if ( ! empty( $noti ) && is_string( $noti ) ) {
873 $notices[] = MainWP_Utility::esc_content( $noti );
874 }
875 }
876 if ( ! empty( $notices ) ) {
877 $resp['notices'] = implode( ' || ', $notices );
878 }
879 }
880 }
881 }
882
883 return $resp;
884 }
885
886 /**
887 * Method maybe_unserialyze()
888 *
889 * Check if $data is serialized,
890 * if it isn't then base64_decode it.
891 *
892 * @param mixed $data Data to check.
893 *
894 * @return mixed $data.
895 */
896 public static function maybe_unserialyze( $data ) {
897 if ( empty( $data ) || is_array( $data ) ) {
898 return $data;
899 } elseif ( is_serialized( $data ) ) {
900 // phpcs:ignore -- for compatability.
901 return maybe_unserialize( $data );
902 } else {
903 // phpcs:ignore -- for compatability.
904 return maybe_unserialize( base64_decode( $data ) );
905 }
906 }
907
908 /**
909 * Method get_openssl_conf()
910 *
911 * Get dashboard openssl configuration.
912 */
913 public static function get_openssl_conf() {
914
915 if ( defined( 'MAINWP_CRYPT_RSA_OPENSSL_CONFIG' ) ) {
916 return MAINWP_CRYPT_RSA_OPENSSL_CONFIG;
917 }
918 $lib_loc = get_option( 'mainwp_opensslLibLocation' );
919 return ! empty( $lib_loc ) ? $lib_loc : '';
920 }
921
922 /**
923 * Get tokens of site.
924 *
925 * @param object $site The website.
926 *
927 * @return array Array of tokens.
928 *
929 * @uses \MainWP\Dashboard\MainWP_DB::get_website_option()
930 */
931 public static function get_tokens_site_values( $site ) {
932
933 $tokens_values = array(
934 '[site.name]' => $site->name,
935 '[site.url]' => $site->url,
936 );
937
938 $site_info = MainWP_DB::instance()->get_website_option( $site, 'site_info' );
939 $site_info = ! empty( $site_info ) ? json_decode( $site_info, true ) : array();
940
941 if ( is_array( $site_info ) ) {
942 $map_site_tokens = array(
943 'client.site.version' => 'wpversion', // Displays the WP version of the child site.
944 'client.site.theme' => 'themeactivated', // Displays the currently active theme for the child site.
945 'client.site.php' => 'phpversion', // Displays the PHP version of the child site.
946 'client.site.mysql' => 'mysql_version', // Displays the MySQL version of the child site.
947 );
948 foreach ( $map_site_tokens as $tok => $val ) {
949 $tokens_value[ '[' . $tok . ']' ] = ( is_array( $site_info ) && isset( $site_info[ $val ] ) ) ? $site_info[ $val ] : '';
950 }
951 }
952
953 return $tokens_values;
954 }
955
956 /**
957 *
958 * Replace site tokens.
959 *
960 * @param string $str String data.
961 * @param array $replace_tokens array of tokens.
962 *
963 * @return string content with replaced tokens.
964 */
965 public static function replace_tokens_values( $str, $replace_tokens ) {
966 $tokens = array_keys( $replace_tokens );
967 $values = array_values( $replace_tokens );
968 return str_replace( $tokens, $values, $str );
969 }
970
971 /**
972 *
973 * Set timeout limit.
974 *
975 * @param int $timeout timeout value.
976 */
977 public static function set_time_limit( $timeout = 0 ) {
978 if ( false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) ) {
979 set_time_limit( $timeout );
980 }
981 }
982
983 /**
984 *
985 * Method get_plugin_theme_info().
986 *
987 * Get WordPress plugin/theme info.
988 *
989 * @param string $what 'plugin' or 'theme'.
990 * @param array $params Plugin/Theme info params.
991 */
992 public static function get_plugin_theme_info( $what, $params = array() ) {
993
994 if ( 'plugin' === $what ) {
995 include_once ABSPATH . '/wp-admin/includes/plugin-install.php'; // NOSONAR - WP compatible.
996 return plugins_api(
997 'plugin_information',
998 $params
999 );
1000 } elseif ( 'theme' === $what ) {
1001 include_once ABSPATH . '/wp-admin/includes/theme-install.php'; // NOSONAR - WP compatible.
1002 return themes_api(
1003 'theme_information',
1004 $params
1005 );
1006
1007 }
1008
1009 return false;
1010 }
1011
1012 /**
1013 * Method update_cached_icons().
1014 *
1015 * Update cached icons
1016 *
1017 * @param string $icon The icon.
1018 * @param string $slug slug.
1019 * @param string $type Type: plugin|theme.
1020 * @param bool $custom_icon Custom icon or not. Default: false.
1021 */
1022 public static function update_cached_icons( $icon, $slug, $type, $custom_icon = false ) {
1023
1024 if ( 'plugin' === $type ) {
1025 $option_name = 'plugins_icons';
1026 } elseif ( 'theme' === $type ) {
1027 $option_name = 'themes_icons';
1028 } else {
1029 return false;
1030 }
1031
1032 $cached_icons = MainWP_DB::instance()->get_general_option( $option_name, 'array' );
1033
1034 $icon = apply_filters( 'mainwp_update_cached_icons', $icon, $slug, $type );
1035
1036 if ( isset( $cached_icons[ $slug ] ) ) {
1037 $value = $cached_icons[ $slug ];
1038 } else {
1039 $value = array(
1040 'lasttime_cached' => time(),
1041 'path_custom' => '',
1042 'path' => '',
1043 );
1044 }
1045
1046 $value['lasttime_cached'] = time();
1047
1048 if ( $custom_icon ) {
1049 $value['path_custom'] = $icon;
1050 } else {
1051 $value['path'] = $icon;
1052 }
1053
1054 // update cache.
1055 $cached_icons[ $slug ] = $value;
1056
1057 MainWP_DB::instance()->update_general_option( $option_name, $cached_icons, 'array' );
1058 return true;
1059 }
1060
1061 /**
1062 * Private function Fetch a plugin|theme icon via API from WordPress.org
1063 *
1064 * @param string $slug Plugin|Theme slug.
1065 * @param string $type Plugin|Theme.
1066 */
1067 private static function fetch_wp_org_icons( $slug, $type ) { // phpcs:ignore -- NOSONAR - complex.
1068 if ( 'plugin' === $type ) {
1069 $fields = array(
1070 'tags' => false,
1071 'icons' => true,
1072 'sections' => false,
1073 'description' => false,
1074 'tested' => false,
1075 'requires' => false,
1076 'rating' => false,
1077 'downloaded' => false,
1078 'downloadlink' => false,
1079 'last_updated' => false,
1080 'homepage' => false,
1081 'compatibility' => false,
1082 'ratings' => false,
1083 'added' => false,
1084 'donate_link' => false,
1085 );
1086 } elseif ( 'theme' === $type ) {
1087 $fields = array(
1088 'screenshots' => true,
1089 'screenshot_count' => 5,
1090 'sections' => false,
1091 'rating' => false,
1092 'downloaded' => false,
1093 'download_link' => false,
1094 'last_updated' => false,
1095 'tags' => false,
1096 'template' => false,
1097 'parent' => false,
1098 'screenshot_url' => false,
1099 'homepage' => false,
1100 );
1101
1102 } else {
1103 return false;
1104 }
1105
1106 $icon = '';
1107 if ( 'theme' === $type ) {
1108 // with $fields empty to get screenshot_url of theme.
1109 $info = static::get_plugin_theme_info(
1110 $type,
1111 array(
1112 'slug' => $slug,
1113 'timeout' => 60,
1114 )
1115 );
1116 if ( is_object( $info ) && ! empty( $info->screenshot_url ) ) {
1117 $icon = $info->screenshot_url;
1118 }
1119 }
1120
1121 // if get screenshot_url of theme success.
1122 if ( ! empty( $icon ) ) {
1123 $option_name = 'themes_icons';
1124 } else {
1125 $info = static::get_plugin_theme_info(
1126 $type,
1127 array(
1128 'slug' => $slug,
1129 'fields' => $fields,
1130 'timeout' => 60,
1131 )
1132 );
1133 $option_name = 'plugins_icons';
1134 $icon = '';
1135 if ( 'plugin' === $type ) {
1136 if ( is_object( $info ) && property_exists( $info, 'icons' ) && isset( $info->icons['1x'] ) ) {
1137 $icon = $info->icons['1x'];
1138 }
1139 } else {
1140 if ( is_object( $info ) && property_exists( $info, 'screenshots' ) && isset( $info->screenshots[0] ) ) {
1141 $icon = $info->screenshots[0];
1142 }
1143 $option_name = 'themes_icons';
1144 }
1145 }
1146
1147 $fetched_icon = '';
1148 if ( '' !== $icon ) {
1149 $fetched_icon = rawurlencode( $icon );
1150 }
1151
1152 $cached_icons = MainWP_DB::instance()->get_general_option( $option_name, 'array' );
1153
1154 if ( isset( $cached_icons[ $slug ] ) && '' === $fetched_icon && ! empty( $cached_icons[ $slug ]['path'] ) ) {
1155 // if fetch icon empty then used caching icon.
1156 $fetched_icon = $cached_icons[ $slug ]['path'];
1157 $icon = rawurldecode( $fetched_icon );
1158 }
1159
1160 static::update_cached_icons( $fetched_icon, $slug, $type );
1161
1162 if ( '' !== $icon ) {
1163 return $icon;
1164 }
1165 return false;
1166 }
1167
1168 /**
1169 * Method handle_get_icon()
1170 *
1171 * @param string $slug Plugin slug.
1172 * @param string $type Type: theme|plugin.
1173 */
1174 public static function handle_get_icon( $slug, $type ) {
1175 if ( empty( $slug ) ) {
1176 return false;
1177 }
1178 if ( 'plugin' === $type || 'theme' === $type ) {
1179 return static::fetch_wp_org_icons( $slug, $type );
1180 }
1181 return '';
1182 }
1183
1184
1185 /**
1186 * Gets a plugin icon via API from WordPress.org
1187 *
1188 * @param string $slug Plugin slug.
1189 * @param bool $forced_get Forced get icon, default: false.
1190 */
1191 public static function get_plugin_icon( $slug, $forced_get = false ) { // phpcs:ignore -- NOSONAR - complex.
1192
1193 $icon = apply_filters( 'mainwp_get_plugin_theme_icon', '', $slug, 'plugin' );
1194
1195 if ( ! empty( $icon ) ) {
1196 return $icon;
1197 }
1198
1199 $forced_get = apply_filters( 'mainwp_forced_get_plugin_theme_icon', $forced_get, $slug, 'plugin' );
1200
1201 if ( $forced_get ) {
1202 $fet_icon = static::fetch_wp_org_icons( $slug, 'plugin' );
1203 if ( false !== $fet_icon ) {
1204 $scr = MainWP_Utility::remove_http_prefix( $fet_icon );
1205 return '<img style="display:inline-block" class="ui mini circular image" updated-icon="true" src="' . esc_attr( $scr ) . '" />';
1206 }
1207 return $icon;
1208 }
1209
1210 // checks expired.
1211 $cached_icons = MainWP_DB::instance()->get_general_option( 'plugins_icons', 'array' );
1212
1213 if ( ! empty( $cached_icons ) ) {
1214 $lasttime_clear_cached = MainWP_DB::instance()->get_general_option( 'lasttime_clear_cached_plugins_icon' );
1215 if ( time() > ( intval( $lasttime_clear_cached ) + MONTH_IN_SECONDS ) ) {
1216 $updated = false;
1217 $new_cached = array();
1218 foreach ( $cached_icons as $sl => $val ) {
1219 if ( empty( $val['path_custom'] ) && time() < ( intval( $val['lasttime_cached'] ) + 12 * MONTH_IN_SECONDS ) ) {
1220 $new_cached[ $sl ] = $val; // unset.
1221 $updated = true;
1222 }
1223 }
1224 if ( $updated ) {
1225 MainWP_DB::instance()->update_general_option( 'plugins_icons', $new_cached, 'array' );
1226 }
1227 MainWP_DB::instance()->update_general_option( 'lasttime_clear_cached_plugins_icon', time() );
1228 }
1229 }
1230
1231 return static::get_plugin_theme_icon( $slug, 'plugin' );
1232 }
1233
1234 /**
1235 * Gets a theme icon via API from WordPress.org
1236 *
1237 * @param string $slug Theme slug.
1238 * @param bool $forced_get Forced get icon, default: false.
1239 */
1240 public static function get_theme_icon( $slug, $forced_get = false ) { // phpcs:ignore -- NOSONAR - complex.
1241
1242 $icon = apply_filters( 'mainwp_get_plugin_theme_icon', '', $slug, 'theme' );
1243
1244 if ( ! empty( $icon ) ) {
1245 return $icon;
1246 }
1247
1248 $forced_get = apply_filters( 'mainwp_forced_get_plugin_theme_icon', $forced_get, $slug, 'theme' );
1249
1250 if ( $forced_get ) {
1251 $fet_icon = static::fetch_wp_org_icons( $slug, 'theme' );
1252 if ( false !== $fet_icon ) {
1253 $scr = MainWP_Utility::remove_http_prefix( $fet_icon );
1254 $icon = '<img style="display:inline-block" class="ui mini circular image" updated-icon="true" src="' . esc_attr( $scr ) . '" />';
1255 }
1256 return $icon;
1257 }
1258
1259 // checks expired.
1260 $cached_icons = MainWP_DB::instance()->get_general_option( 'themes_icons', 'array' );
1261
1262 if ( ! empty( $cached_icons ) ) {
1263 $lasttime_clear_cached = MainWP_DB::instance()->get_general_option( 'lasttime_clear_cached_themes_icon' );
1264 if ( time() > ( intval( $lasttime_clear_cached ) + MONTH_IN_SECONDS ) ) {
1265 $updated = false;
1266 $new_cached = array();
1267 foreach ( $cached_icons as $sl => $val ) {
1268 if ( empty( $val['path_custom'] ) && time() < ( intval( $val['lasttime_cached'] ) + 12 * MONTH_IN_SECONDS ) ) {
1269 $new_cached[ $sl ] = $val;
1270 $updated = true;
1271 }
1272 }
1273 if ( $updated ) {
1274 MainWP_DB::instance()->update_general_option( 'themes_icons', $new_cached, 'array' );
1275 }
1276 MainWP_DB::instance()->update_general_option( 'lasttime_clear_cached_themes_icon', time() );
1277 }
1278 }
1279
1280 return static::get_plugin_theme_icon( $slug, 'theme' );
1281 }
1282
1283
1284 /**
1285 * Gets a plugin|theme icon to output.
1286 *
1287 * @param string $slug Plugin|Theme slug.
1288 * @param string $type Type icon, plugin|theme.
1289 */
1290 private static function get_plugin_theme_icon( $slug, $type ) { // phpcs:ignore -- NOSONAR -Current complexity is the only way to achieve desired results, pull request solutions appreciated.
1291
1292 if ( 'plugin' === $type ) {
1293 $option_name = 'plugins_icons';
1294 } elseif ( 'theme' === $type ) {
1295 $option_name = 'themes_icons';
1296 } else {
1297 return '<i style="font-size: 17px" class="plug circular inverted icon" not-cached-path="true"></i>';
1298 }
1299
1300 $cached_icons = MainWP_DB::instance()->get_general_option( $option_name, 'array' );
1301
1302 $cached_days = apply_filters( 'mainwp_plugin_theme_icon_cache_days', 15, $slug, $type ); // default 15 days.
1303
1304 $attr_slug = ' icon-type="' . esc_attr( $type ) . '" item-slug="' . esc_attr( $slug ) . '" ';
1305 $cls_expired = ' cached-icon-expired ';
1306 $cls_uploadable = ' cached-icon-customable ';
1307
1308 $icon = '';
1309
1310 if ( isset( $cached_icons[ $slug ] ) ) {
1311 $scr = '';
1312 $is_custom_icon = false;
1313 if ( ! empty( $cached_icons[ $slug ]['path_custom'] ) ) {
1314 if ( 'plugin' === $type ) {
1315 $dirs = static::get_mainwp_dir( 'plugin-icons', true );
1316 } elseif ( 'theme' === $type ) {
1317 $dirs = static::get_mainwp_dir( 'theme-icons', true );
1318 }
1319 $scr = $dirs[1] . rawurldecode( $cached_icons[ $slug ]['path_custom'] );
1320 $is_custom_icon = true; // custom icons will not expired.
1321 } elseif ( ! empty( $cached_icons[ $slug ]['path'] ) ) {
1322 $scr = rawurldecode( $cached_icons[ $slug ]['path'] );
1323 $scr = MainWP_Utility::remove_http_prefix( $scr );
1324 }
1325
1326 $set_cached_expired = apply_filters( 'mainwp_cache_icon_expired', false, $slug, 'theme' );
1327 $set_expired = false;
1328
1329 if ( $set_cached_expired && time() > ( intval( $cached_icons[ $slug ]['lasttime_cached'] ) + 15 * MINUTE_IN_SECONDS ) ) {
1330 $set_expired = true;
1331 }
1332
1333 $forced_exprided = 1700238511;
1334 $lasttime_cached = isset( $cached_icons[ $slug ]['lasttime_cached'] ) ? intval( $cached_icons[ $slug ]['lasttime_cached'] ) : 0;
1335
1336 if ( time() > ( $lasttime_cached + $cached_days * DAY_IN_SECONDS ) || $lasttime_cached < $forced_exprided ) { // expired.
1337 if ( ! empty( $scr ) ) {
1338 $icon = '<img style="display:inline-block" class="ui mini circular image ' . ( $is_custom_icon ? $cls_uploadable : $cls_expired ) . '" ' . $attr_slug . 'src="' . esc_attr( $scr ) . '" alt="Icon"/>'; // to update expired icon.
1339 } else {
1340 $icon = '<i style="font-size: 17px" class="plug circular inverted icon ' . $cls_expired . $cls_uploadable . '" ' . $attr_slug . '></i>'; // to update expired icon.
1341 }
1342 } elseif ( ! empty( $scr ) ) {
1343 $use_cls_expired = $set_expired ? $cls_expired : '';
1344 $icon = '<img style="display:inline-block" class="ui mini circular image ' . ( $is_custom_icon ? $cls_uploadable : $use_cls_expired ) . '" ' . $attr_slug . ' cached-path-icon="true" src="' . esc_attr( $scr ) . '" alt="Icon"/>';
1345 } else {
1346 $icon = '<i style="font-size: 17px" class="plug circular inverted icon ' . ( $set_expired ? $cls_expired : '' ) . $cls_uploadable . '" ' . $attr_slug . ' cached-path-icon="true"></i>';
1347 }
1348 } elseif ( empty( $icon ) ) {
1349 $icon = '<i style="font-size: 17px" class="plug circular inverted icon ' . $cls_expired . '" ' . $attr_slug . ' not-cached-path="true"></i>'; // not upload when not existed in the cached.
1350 }
1351 return $icon;
1352 }
1353
1354
1355 /**
1356 * Method handle_upload_image().
1357 *
1358 * Handle upload icons.
1359 *
1360 * @param string $sub_folder The sub folder.
1361 * @param mixed $file_uploader The file uploader.
1362 * @param mixed $file_index The index of file uploader.
1363 * @param bool $file_subindex Is file with sub index.
1364 * @param int $max_width max image width.
1365 * @param int $max_height max image height.
1366 */
1367 public static function handle_upload_image( $sub_folder, $file_uploader, $file_index = 0, $file_subindex = false, $max_width = 300, $max_height = 300 ) { // phpcs:ignore -- NOSONAR - complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
1368
1369 $dirs = static::get_mainwp_dir( $sub_folder, true );
1370 $base_dir = $dirs[0];
1371 $base_url = $dirs[1];
1372
1373 $output = array();
1374 $filename = '';
1375 $filepath = '';
1376
1377 $file_types = array(
1378 'image/jpeg',
1379 'image/jpg',
1380 'image/gif',
1381 'image/x-icon',
1382 'image/png',
1383 );
1384
1385 $file_exts = array(
1386 'jpeg',
1387 'jpg',
1388 'gif',
1389 'ico',
1390 'png',
1391 );
1392
1393 $upload_ok = ( false === $file_subindex ) ? ( UPLOAD_ERR_OK === $file_uploader['error'][ $file_index ] ) : ( UPLOAD_ERR_OK === $file_uploader['error'][ $file_index ][ $file_subindex ] );
1394
1395 if ( $upload_ok ) {
1396 $tmp_file = ( false === $file_subindex ) ? ( $file_uploader['tmp_name'][ $file_index ] ) : ( $file_uploader['tmp_name'][ $file_index ][ $file_subindex ] );
1397
1398 if ( is_uploaded_file( $tmp_file ) ) {
1399 if ( false === $file_subindex ) {
1400 $file_size = $file_uploader['size'][ $file_index ];
1401 $file_type = $file_uploader['type'][ $file_index ];
1402 $file_name = $file_uploader['name'][ $file_index ];
1403 } else {
1404 $file_size = $file_uploader['size'][ $file_index ][ $file_subindex ];
1405 $file_type = $file_uploader['type'][ $file_index ][ $file_subindex ];
1406 $file_name = $file_uploader['name'][ $file_index ][ $file_subindex ];
1407 }
1408
1409 $file_extension = strtolower( pathinfo( $file_name, PATHINFO_EXTENSION ) );
1410
1411 if ( $file_size > 500 * 1025 ) {
1412 $output['error'][] = 3;
1413 } elseif ( ! in_array( $file_type, $file_types ) ) {
1414 $output['error'][] = 4;
1415 } elseif ( ! in_array( $file_extension, $file_exts ) ) {
1416 $output['error'][] = 5;
1417 } else {
1418
1419 $dest_file = $base_dir . '/' . $file_name;
1420 $dest_file = dirname( $dest_file ) . '/' . wp_unique_filename( dirname( $dest_file ), basename( $dest_file ) );
1421
1422 if ( move_uploaded_file( $tmp_file, $dest_file ) ) {
1423 if ( file_exists( $dest_file ) ) {
1424 list( $width, $height ) = getimagesize( $dest_file );
1425 }
1426
1427 $resize = false;
1428 if ( $width > $max_width ) {
1429 $dst_width = $max_width;
1430 if ( $height > $max_height ) {
1431 $dst_height = $max_height;
1432 } else {
1433 $dst_height = $height;
1434 }
1435 $resize = true;
1436 } elseif ( $height > $max_height ) {
1437 $dst_width = $width;
1438 $dst_height = $max_height;
1439 $resize = true;
1440 }
1441
1442 if ( $resize ) {
1443 $src = $dest_file;
1444 $cropped_file = wp_crop_image( $src, 0, 0, $width, $height, $dst_width, $dst_height, false );
1445 if ( ! $cropped_file || is_wp_error( $cropped_file ) ) {
1446 $output['error'][] = 9;
1447 } else {
1448 wp_delete_file( $dest_file );
1449 $filename = basename( $cropped_file );
1450 $filepath = $cropped_file;
1451 }
1452 } else {
1453 $filename = basename( $dest_file );
1454 $filepath = $dest_file;
1455 }
1456 } else {
1457 $output['error'][] = 6;
1458 }
1459 }
1460 }
1461 }
1462 $output['fileurl'] = ! empty( $filename ) ? $base_url . '/' . $filename : '';
1463 $output['filepath'] = ! empty( $filepath ) ? $filepath : '';
1464 $output['filename'] = ! empty( $filename ) ? $filename : '';
1465
1466 return $output;
1467 }
1468
1469 /**
1470 * Method disabled_wpcore_update_by().
1471 *
1472 * Get disabled wpcore update by.
1473 *
1474 * @param string $website The website.
1475 */
1476 public static function disabled_wpcore_update_by( $website ) {
1477 $by = static::get_disabled_wpcore_update_host( $website );
1478 if ( 'flywheel' === $by ) {
1479 return esc_html__( 'FlyWheel disables WP core updates. For more information contact FlyWheel support.', 'mainwp' );
1480 } elseif ( 'pressable' === $by ) {
1481 return esc_html__( 'Pressable disables WP core updates. For more information contact Pressable support.', 'mainwp' );
1482 }
1483 return '';
1484 }
1485
1486
1487 /**
1488 * Method get_disabled_wpcore_update_host().
1489 *
1490 * Get wpcore update disabled for the websites on FlyWheel host or Pressable host.
1491 *
1492 * @param mixed $website data.
1493 */
1494 public static function get_disabled_wpcore_update_host( $website ) {
1495 if ( empty( $website ) ) {
1496 return '';
1497 }
1498 $wphost = MainWP_DB::instance()->get_website_option( $website, 'wphost' );
1499 if ( ! empty( $wphost ) && ( 'flywheel' !== $wphost && 'pressable' !== $wphost ) ) {
1500 $wphost = '';
1501 }
1502 return empty( $wphost ) ? '' : $wphost;
1503 }
1504
1505
1506 /**
1507 * Method get_connect_sign_algorithm().
1508 *
1509 * Get supported sign algorithms.
1510 *
1511 * @param mixed $website The Website object.
1512 *
1513 * @return mixed $alg Algorithm connect.
1514 */
1515 public static function get_connect_sign_algorithm( $website ) { // phpcs:ignore -- NOSONAR - complex.
1516 $alg = is_object( $website ) && property_exists( $website, 'signature_algo' ) && ! empty( $website->signature_algo ) ? $website->signature_algo : false;
1517
1518 // to fix.
1519 if ( is_numeric( $alg ) ) {
1520 $alg = intval( $alg );
1521 }
1522
1523 $default_alg = false;
1524 if ( defined( 'OPENSSL_ALGO_SHA256' ) ) {
1525 $default_alg = OPENSSL_ALGO_SHA256;
1526 }
1527
1528 if ( ! empty( $alg ) && 9999 === $alg ) {
1529 $alg = get_option( 'mainwp_connect_signature_algo', $default_alg );
1530 // to fix.
1531 if ( is_numeric( $alg ) ) {
1532 $alg = intval( $alg );
1533 }
1534 }
1535
1536 if ( empty( $alg ) ) {
1537 $site_info = MainWP_DB::instance()->get_website_option( $website, 'site_info' );
1538 $site_info = ! empty( $site_info ) ? json_decode( $site_info, true ) : array();
1539 if ( is_array( $site_info ) && ! empty( $site_info['child_version'] ) && version_compare( $site_info['child_version'], '4.5', '>=' ) ) {
1540 $alg = $default_alg;
1541 }
1542 }
1543
1544 if ( ! static::is_valid_supported_sign_alg( $alg ) ) {
1545 $alg = false;
1546 }
1547
1548 $alg = apply_filters( 'mainwp_connect_sign_algo', $alg, $website );
1549
1550 return $alg;
1551 }
1552
1553 /**
1554 * Method is_valid_supported_sign_alg()
1555 *
1556 * Check if is supported sign algorithms.
1557 *
1558 * @param int $alg The Sign Algo value.
1559 */
1560 public static function is_valid_supported_sign_alg( $alg ) {
1561 $valid = false;
1562 if ( ( defined( 'OPENSSL_ALGO_SHA1' ) && OPENSSL_ALGO_SHA1 === $alg ) || ( defined( 'OPENSSL_ALGO_SHA224' ) && OPENSSL_ALGO_SHA224 === $alg ) || ( defined( 'OPENSSL_ALGO_SHA256' ) && OPENSSL_ALGO_SHA256 === $alg ) || ( defined( 'OPENSSL_ALGO_SHA384' ) && OPENSSL_ALGO_SHA384 === $alg ) || ( defined( 'OPENSSL_ALGO_SHA512' ) && OPENSSL_ALGO_SHA512 === $alg ) ) {
1563 $valid = true;
1564 }
1565 return $valid;
1566 }
1567
1568 /**
1569 * Method get_signature_alg()
1570 *
1571 * Get custom signature algorithms.
1572 */
1573 public static function get_open_ssl_sign_algos() {
1574 $values = array();
1575
1576 if ( defined( 'OPENSSL_ALGO_SHA1' ) ) {
1577 $values[ OPENSSL_ALGO_SHA1 ] = 'OPENSSL_ALGO_SHA1';
1578 }
1579 if ( defined( 'OPENSSL_ALGO_SHA224' ) ) {
1580 $values[ OPENSSL_ALGO_SHA224 ] = 'OPENSSL_ALGO_SHA224';
1581 }
1582
1583 if ( defined( 'OPENSSL_ALGO_SHA256' ) ) {
1584 $values[ OPENSSL_ALGO_SHA256 ] = 'OPENSSL_ALGO_SHA256 ' . esc_html__( '(Default)', 'mainwp' );
1585 }
1586
1587 if ( defined( 'OPENSSL_ALGO_SHA384' ) ) {
1588 $values[ OPENSSL_ALGO_SHA384 ] = 'OPENSSL_ALGO_SHA384';
1589 }
1590
1591 if ( defined( 'OPENSSL_ALGO_SHA512' ) ) {
1592 $values[ OPENSSL_ALGO_SHA512 ] = 'OPENSSL_ALGO_SHA512';
1593 }
1594
1595 return $values;
1596 }
1597
1598 /**
1599 * Method get_default_map_site_fields()
1600 *
1601 * Get default map site fields.
1602 */
1603 public static function get_default_map_site_fields() {
1604 return array(
1605 'id',
1606 'url',
1607 'name',
1608 'adminname',
1609 'privkey',
1610 'http_user',
1611 'http_pass',
1612 'ssl_version',
1613 'sync_errors',
1614 'signature_algo',
1615 'verify_method',
1616 );
1617 }
1618
1619 /**
1620 * Method get_staging_options_sites_view_for_current_users()
1621 *
1622 * Get staging options sites view for current users.
1623 *
1624 * @return string Site views.
1625 */
1626 public static function get_staging_options_sites_view_for_current_users() {
1627 $view = apply_filters( 'mainwp_staging_current_user_sites_view', 'undefined' );
1628 if ( 'undefined' === $view ) { // to compatible.
1629 $view = get_user_option( 'mainwp_staging_options_updates_view' );
1630 }
1631 return $view;
1632 }
1633 }
1634