PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.2
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.2.2, at class/class-mainwp-system-utility.php

1,613 lines 53.8 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 $sig_values = array(
335 'sig' => md5( filesize( $fullFile ) ), // NOSONAR - safe for sig file size.
336 'key_value' => $key_value,
337 'hash_key' => wp_hash( $key_value ),
338 );
339 $sig_values = wp_json_encode( $sig_values );
340 $sig_values = rawurlencode( $sig_values );
341 return $sig_values;
342 }
343
344
345 /**
346 * Method valid_download_sig()
347 *
348 * @param string $file File Name.
349 * @param string $sig download.
350 *
351 * @return bool true|false.
352 */
353 public static function valid_download_sig( $file, $sig ) {
354
355 $sig = rawurldecode( $sig );
356 $value = json_decode( $sig, true );
357
358 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.
359 return false;
360 }
361
362 $hash_key = wp_hash( $value['key_value'] );
363 if ( ! hash_equals( $hash_key, $value['hash_key'] ) ) {
364 return false;
365 }
366
367 return true;
368 }
369
370 /**
371 * Method get_mainwp_specific_dir()
372 *
373 * Get MainWP Specific directory,
374 * if it doesn't exist create it.
375 *
376 * Update .htaccess.
377 *
378 * @param null $dir Current MainWP directory.
379 *
380 * @return string $newdir
381 *
382 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
383 */
384 public static function get_mainwp_specific_dir( $dir = null ) { // phpcs:ignore -- NOSONAR - complex.
385 if ( MainWP_System::instance()->is_single_user() ) {
386 $userid = 0;
387 } else {
388
389 /**
390 * Current user global.
391 *
392 * @global string
393 */
394 global $current_user;
395
396 $userid = $current_user->ID;
397 }
398
399 $hasWPFileSystem = static::get_wp_file_system();
400
401 global $wp_filesystem;
402
403 $dirs = static::get_mainwp_dir();
404
405 $newdir = $dirs[0] . $userid;
406 if ( '/' === $dir || null === $dir ) {
407 $newdir .= DIRECTORY_SEPARATOR;
408 } else {
409 $newdir .= DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
410 }
411
412 if ( $hasWPFileSystem && ! empty( $wp_filesystem ) ) {
413
414 if ( ! $wp_filesystem->is_dir( $newdir ) ) {
415 $wp_filesystem->mkdir( $newdir, 0777 );
416 }
417
418 if ( ! empty( $dirs[0] ) . $userid && ! $wp_filesystem->exists( trailingslashit( $dirs[0] . $userid ) . '.htaccess' ) ) {
419 $file_htaccess = trailingslashit( $dirs[0] . $userid ) . '.htaccess';
420 $wp_filesystem->put_contents( $file_htaccess, 'deny from all' );
421 }
422 } else {
423
424 if ( ! file_exists( $newdir ) ) {
425 mkdir( $newdir, 0777, true ); // NOSONAR - @newdir is valid.
426 }
427
428 if ( ! empty( $dirs[0] ) . $userid && ! file_exists( trailingslashit( $dirs[0] . $userid ) . '.htaccess' ) ) {
429 $file = fopen( trailingslashit( $dirs[0] . $userid ) . '.htaccess', 'w+' );
430 fwrite( $file, 'deny from all' );
431 fclose( $file );
432 }
433 }
434
435 return $newdir;
436 }
437
438 /**
439 * Method get_mainwp_specific_url()
440 *
441 * Get MainWP specific URL.
442 *
443 * @param mixed $dir MainWP Directory.
444 *
445 * @return string MainWP URL.
446 *
447 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
448 */
449 public static function get_mainwp_specific_url( $dir ) {
450 if ( MainWP_System::instance()->is_single_user() ) {
451 $userid = 0;
452 } else {
453
454 /**
455 * Current user global.
456 *
457 * @global string
458 */
459 global $current_user;
460
461 $userid = $current_user->ID;
462 }
463 $dirs = static::get_mainwp_dir();
464
465 return $dirs[1] . $userid . '/' . $dir . '/';
466 }
467
468 /**
469 * Method get_mainwp_dir_allow_access()
470 *
471 * Get MainWP specific sub folder allow access.
472 *
473 * @param mixed $sub_dir MainWP Sub Directory.
474 */
475 public static function get_mainwp_dir_allow_access( $sub_dir ) {
476 $dirs = static::get_mainwp_dir( $sub_dir, false );
477 if ( $dirs ) {
478 static::get_wp_file_system();
479 global $wp_filesystem;
480 if ( $wp_filesystem ) {
481 // to fix issue of do not allow access.
482 $newdir = $dirs[0];
483 $content = "Order allow,deny\r\nAllow from all";
484 // check if the htaccess is deny access all.
485 if ( $wp_filesystem->exists( trailingslashit( $newdir ) . '.htaccess' ) ) {
486 if ( $wp_filesystem->size( trailingslashit( $newdir ) . '.htaccess' ) < 25 ) { // 25 bytes: deny from all.
487 // update the htaccess file to allow direct access.
488 $wp_filesystem->put_contents( trailingslashit( $newdir ) . '.htaccess', $content );
489 }
490 } else {
491 // update the htaccess file to allow direct access.
492 $wp_filesystem->put_contents( trailingslashit( $newdir ) . '.htaccess', $content );
493 }
494 }
495 }
496 return $dirs;
497 }
498
499
500 /**
501 * Method get_wp_file_system()
502 *
503 * Get WP file system & define Global Variable FS_METHOD.
504 *
505 * @return boolean $init True.
506 */
507 public static function get_wp_file_system() {
508
509 /**
510 * WordPress files system object.
511 *
512 * @global object
513 */
514 global $wp_filesystem;
515
516 if ( empty( $wp_filesystem ) ) {
517 ob_start();
518 if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) {
519 include_once ABSPATH . '/wp-admin/includes/screen.php'; // NOSONAR - WP compatible.
520 }
521 if ( file_exists( ABSPATH . '/wp-admin/includes/template.php' ) ) {
522 include_once ABSPATH . '/wp-admin/includes/template.php'; // NOSONAR - WP compatible.
523 }
524 include_once ABSPATH . 'wp-admin/includes/file.php'; // NOSONAR - WP compatible.
525
526 if ( ! function_exists( 'wp_create_nonce' ) ) {
527 include_once ABSPATH . WPINC . '/pluggable.php'; // NOSONAR - WP compatible.
528 }
529
530 $creds = request_filesystem_credentials( 'test' );
531 ob_end_clean();
532 if ( empty( $creds ) ) {
533
534 /**
535 * Define WordPress File system.
536 *
537 * @const ( bool ) Default: true
538 * @source https://code-reference.mainwp.com/classes/MainWP.Dashboard.MainWP_System_Utility.html
539 */
540 define( 'FS_METHOD', 'direct' );
541 }
542 $init = \WP_Filesystem( $creds );
543 } else {
544 $init = true;
545 }
546
547 return $init;
548 }
549
550 /**
551 * Method can_edit_website()
552 *
553 * Check if current user can edit Child Site.
554 *
555 * @param mixed $website Child Site.
556 *
557 * @return mixed true|false|userid
558 *
559 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
560 */
561 public static function can_edit_website( &$website ) {
562 if ( empty( $website ) ) {
563 return false;
564 }
565
566 if ( MainWP_System::instance()->is_single_user() ) {
567 return true;
568 }
569
570 /**
571 * Current user global.
572 *
573 * @global string
574 */
575 global $current_user;
576
577 return $website->userid === $current_user->ID;
578 }
579
580 /**
581 * Gets site tags
582 *
583 * @param array $item Array containing child site data.
584 * @param bool $client_tag It is client tags or not.
585 *
586 * @return mixed Single Row Classes Item.
587 */
588 public static function get_site_tags( $item, $client_tag = false ) { // phpcs:ignore -- NOSONAR - complex.
589
590 if ( ! is_array( $item ) || ! isset( $item['wpgroups'] ) ) {
591 return '';
592 }
593
594 $href = 'admin.php?page=managesites&g=';
595 if ( $client_tag ) {
596 $href = 'admin.php?page=ManageClients&tags=';
597 }
598
599 $groups_colors = '';
600 if ( isset( $item['wpgroups_colors'] ) ) {
601 $groups_colors = explode( ',', $item['wpgroups_colors'] );
602 }
603
604 $tags = '';
605 $tags_labels = '';
606
607 if ( isset( $item['wpgroups'] ) && ! empty( $item['wpgroups'] ) ) {
608
609 if ( $client_tag ) {
610 $tags_filter = static::client_tags_filter( $item );
611 $tags = $tags_filter['wpgroups'];
612 $tags_ids = $tags_filter['wpgroupids'];
613 } else {
614 $tags = $item['wpgroups'];
615 $tags = explode( ',', $tags );
616 $tags_ids = $item['wpgroupids'];
617 $tags_ids = explode( ',', $tags_ids );
618 }
619
620 if ( is_array( $tags ) ) {
621 foreach ( $tags as $idx => $tag ) {
622 $tag = trim( $tag );
623 $tagc = '';
624
625 // to improved db query.
626 if ( is_array( $groups_colors ) && isset( $groups_colors[ $idx ] ) ) {
627 $tagc = $groups_colors[ $idx ];
628 } else {
629 $tagx = MainWP_DB_Common::instance()->get_group_by_name( $tag );
630 $tagc = is_object( $tagx ) && '' !== $tagx->color ? $tagx->color : '';
631 }
632
633 if ( '' !== $tagc ) {
634 $tag_a_style = 'style="color:#fff!important;opacity:1;"';
635 $tag_style = 'style="background-color:' . esc_html( $tagc ) . '"';
636 } else {
637 $tag_a_style = '';
638 $tag_style = '';
639 }
640
641 if ( isset( $tags_ids[ $idx ] ) && ! empty( $tags_ids[ $idx ] ) ) {
642 $tag_id = $tags_ids[ $idx ];
643 $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>';
644 } else {
645 $tags_labels .= '<span ' . $tag_style . ' class="ui tag mini label">' . esc_html( $tag ) . '</span>';
646 }
647 }
648 }
649 }
650 return $tags_labels;
651 }
652
653 /**
654 * Gets site tags
655 *
656 * @param array $item Array containing child site data.
657 *
658 * @return mixed Single Row Classes Item.
659 */
660 public static function get_site_tags_belong( $item ) { // phpcs:ignore -- NOSONAR - complex.
661
662 if ( ! is_array( $item ) || ! isset( $item['wpgroups_belong'] ) ) {
663 return static::get_site_tags( $item );
664 }
665
666 $href = 'admin.php?page=managesites&g=';
667
668 $tags = '';
669 $tags_labels = '';
670
671 if ( isset( $item['wpgroups_belong'] ) && ! empty( $item['wpgroups_belong'] ) ) {
672
673 $tags = $item['wpgroups_belong'];
674 $tags = explode( ',', $tags );
675 $tags_ids = $item['wpgroupids_belong'];
676 $tags_ids = explode( ',', $tags_ids );
677
678 $tags_colors = explode( ',', $item['wpgroupcolors_belong'] );
679
680 if ( is_array( $tags ) ) {
681 foreach ( $tags as $idx => $tag ) {
682 $tag = trim( $tag );
683 $tagc = $tags_colors[ $idx ];
684
685 if ( '' !== $tagc ) {
686 $tag_a_style = 'style="color:#fff!important;opacity:1;"';
687 $tag_style = 'style="background-color:' . esc_html( $tagc ) . '"';
688 } else {
689 $tag_a_style = '';
690 $tag_style = '';
691 }
692
693 if ( isset( $tags_ids[ $idx ] ) && ! empty( $tags_ids[ $idx ] ) ) {
694 $tag_id = $tags_ids[ $idx ];
695 $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>';
696 } else {
697 $tags_labels .= '<span ' . $tag_style . ' class="ui tag mini label">' . esc_html( $tag ) . '</span>';
698 }
699 }
700 }
701 }
702 return $tags_labels;
703 }
704
705 /**
706 * Filter client tags
707 *
708 * @param array $item Array containing tags.
709 *
710 * @return mixed Single Row Classes Item.
711 */
712 public static function client_tags_filter( $item ) {
713 $tags = $item['wpgroups'];
714 $tags = explode( ',', $tags );
715 $tags = array_values( array_unique( $tags ) );
716
717 $tags_ids = $item['wpgroupids'];
718 $tags_ids = explode( ',', $tags_ids );
719 $tags_ids = array_values( array_unique( $tags_ids ) );
720
721 $return = array();
722
723 $return['wpgroups'] = $tags;
724 $return['wpgroupids'] = $tags_ids;
725 return $return;
726 }
727
728 /**
729 * Method is_suspended_site()
730 *
731 * Check if enable site.
732 *
733 * @param mixed $website The website.
734 */
735 public static function is_suspended_site( $website = false ) {
736 if ( empty( $website ) ) {
737 return true; // empty so return as suspended.
738 }
739 if ( is_array( $website ) ) {
740 return '1' === $website['suspended'];
741 } elseif ( is_object( $website ) ) {
742 if ( ! property_exists( $website, 'suspended' ) && property_exists( $website, 'id' ) ) {
743 $website = MainWP_DB::instance()->get_website_by_id( $website->id );
744 }
745 if ( property_exists( $website, 'suspended' ) ) {
746 return '1' === $website->suspended;
747 }
748 } elseif ( is_numeric( $website ) ) {
749 $siteId = $website;
750 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
751 if ( $website ) {
752 return static::is_suspended_site( $website );
753 }
754 }
755 return false;
756 }
757
758 /**
759 * Method get_current_wpid()
760 *
761 * Get current Child Site ID.
762 *
763 * @return string $current_user->current_site_id Current Child Site ID.
764 */
765 public static function get_current_wpid() {
766
767 /**
768 * Current user global.
769 *
770 * @global string
771 */
772 global $current_user;
773
774 return $current_user->current_site_id;
775 }
776
777 /**
778 * Method set_current_wpid()
779 *
780 * Set the current Child Site ID.
781 *
782 * @param mixed $wpid Child Site ID.
783 */
784 public static function set_current_wpid( $wpid ) {
785
786 /**
787 * Current user global.
788 *
789 * @global string
790 */
791 global $current_user;
792
793 $current_user->current_site_id = $wpid;
794 }
795
796 /**
797 * Method get_page_id()
798 *
799 * Get current Page ID.
800 *
801 * @param null $screen Current Screen ID.
802 *
803 * @return string $page Current page ID.
804 */
805 public static function get_page_id( $screen = null ) {
806
807 if ( empty( $screen ) ) {
808 $screen = get_current_screen();
809 } elseif ( is_string( $screen ) ) {
810 $screen = convert_to_screen( $screen );
811 }
812
813 if ( ! isset( $screen->id ) ) {
814 return '';
815 }
816
817 return $screen->id;
818 }
819
820 /**
821 * Method get_child_response()
822 *
823 * Get response from Child Site.
824 *
825 * @param mixed $data Data to process.
826 *
827 * @return json $data|true.
828 */
829 public static function get_child_response( $data ) { // phpcs:ignore -- NOSONAR - complex.
830 $resp = json_decode( $data, true );
831
832 if ( is_array( $resp ) ) {
833 if ( isset( $resp['error'] ) ) {
834 $resp['error'] = MainWP_Utility::esc_content( $resp['error'] );
835 }
836
837 if ( isset( $resp['message'] ) && is_string( $resp['message'] ) ) {
838 $resp['message'] = MainWP_Utility::esc_content( $resp['message'] );
839 }
840
841 if ( isset( $resp['error_message'] ) ) {
842 $resp['error_message'] = MainWP_Utility::esc_content( $resp['error_message'] );
843 }
844
845 if ( isset( $resp['notices'] ) ) {
846 if ( is_string( $resp['notices'] ) ) {
847 $resp['notices'] = MainWP_Utility::esc_content( $resp['notices'] );
848 } elseif ( is_array( $resp['notices'] ) ) {
849 $notices = array();
850 foreach ( $resp['notices'] as $noti ) {
851 if ( ! empty( $noti ) && is_string( $noti ) ) {
852 $notices[] = MainWP_Utility::esc_content( $noti );
853 }
854 }
855 if ( ! empty( $notices ) ) {
856 $resp['notices'] = implode( ' || ', $notices );
857 }
858 }
859 }
860 }
861
862 return $resp;
863 }
864
865 /**
866 * Method maybe_unserialyze()
867 *
868 * Check if $data is serialized,
869 * if it isn't then base64_decode it.
870 *
871 * @param mixed $data Data to check.
872 *
873 * @return mixed $data.
874 */
875 public static function maybe_unserialyze( $data ) {
876 if ( empty( $data ) || is_array( $data ) ) {
877 return $data;
878 } elseif ( is_serialized( $data ) ) {
879 // phpcs:ignore -- for compatability.
880 return maybe_unserialize( $data );
881 } else {
882 // phpcs:ignore -- for compatability.
883 return maybe_unserialize( base64_decode( $data ) );
884 }
885 }
886
887 /**
888 * Method get_openssl_conf()
889 *
890 * Get dashboard openssl configuration.
891 */
892 public static function get_openssl_conf() {
893
894 if ( defined( 'MAINWP_CRYPT_RSA_OPENSSL_CONFIG' ) ) {
895 return MAINWP_CRYPT_RSA_OPENSSL_CONFIG;
896 }
897 $lib_loc = get_option( 'mainwp_opensslLibLocation' );
898 return ! empty( $lib_loc ) ? $lib_loc : '';
899 }
900
901 /**
902 * Get tokens of site.
903 *
904 * @param object $site The website.
905 *
906 * @return array Array of tokens.
907 *
908 * @uses \MainWP\Dashboard\MainWP_DB::get_website_option()
909 */
910 public static function get_tokens_site_values( $site ) {
911
912 $tokens_values = array(
913 '[site.name]' => $site->name,
914 '[site.url]' => $site->url,
915 );
916
917 $site_info = MainWP_DB::instance()->get_website_option( $site, 'site_info' );
918 $site_info = ! empty( $site_info ) ? json_decode( $site_info, true ) : array();
919
920 if ( is_array( $site_info ) ) {
921 $map_site_tokens = array(
922 'client.site.version' => 'wpversion', // Displays the WP version of the child site.
923 'client.site.theme' => 'themeactivated', // Displays the currently active theme for the child site.
924 'client.site.php' => 'phpversion', // Displays the PHP version of the child site.
925 'client.site.mysql' => 'mysql_version', // Displays the MySQL version of the child site.
926 );
927 foreach ( $map_site_tokens as $tok => $val ) {
928 $tokens_value[ '[' . $tok . ']' ] = ( is_array( $site_info ) && isset( $site_info[ $val ] ) ) ? $site_info[ $val ] : '';
929 }
930 }
931
932 return $tokens_values;
933 }
934
935 /**
936 *
937 * Replace site tokens.
938 *
939 * @param string $str String data.
940 * @param array $replace_tokens array of tokens.
941 *
942 * @return string content with replaced tokens.
943 */
944 public static function replace_tokens_values( $str, $replace_tokens ) {
945 $tokens = array_keys( $replace_tokens );
946 $values = array_values( $replace_tokens );
947 return str_replace( $tokens, $values, $str );
948 }
949
950 /**
951 *
952 * Set timeout limit.
953 *
954 * @param int $timeout timeout value.
955 */
956 public static function set_time_limit( $timeout = 0 ) {
957 if ( false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) ) {
958 set_time_limit( $timeout );
959 }
960 }
961
962 /**
963 *
964 * Method get_plugin_theme_info().
965 *
966 * Get WordPress plugin/theme info.
967 *
968 * @param string $what 'plugin' or 'theme'.
969 * @param array $params Plugin/Theme info params.
970 */
971 public static function get_plugin_theme_info( $what, $params = array() ) {
972
973 if ( 'plugin' === $what ) {
974 include_once ABSPATH . '/wp-admin/includes/plugin-install.php'; // NOSONAR - WP compatible.
975 return plugins_api(
976 'plugin_information',
977 $params
978 );
979 } elseif ( 'theme' === $what ) {
980 include_once ABSPATH . '/wp-admin/includes/theme-install.php'; // NOSONAR - WP compatible.
981 return themes_api(
982 'theme_information',
983 $params
984 );
985
986 }
987
988 return false;
989 }
990
991 /**
992 * Method update_cached_icons().
993 *
994 * Update cached icons
995 *
996 * @param string $icon The icon.
997 * @param string $slug slug.
998 * @param string $type Type: plugin|theme.
999 * @param bool $custom_icon Custom icon or not. Default: false.
1000 */
1001 public static function update_cached_icons( $icon, $slug, $type, $custom_icon = false ) {
1002
1003 if ( 'plugin' === $type ) {
1004 $option_name = 'plugins_icons';
1005 } elseif ( 'theme' === $type ) {
1006 $option_name = 'themes_icons';
1007 } else {
1008 return false;
1009 }
1010
1011 $cached_icons = MainWP_DB::instance()->get_general_option( $option_name, 'array' );
1012
1013 $icon = apply_filters( 'mainwp_update_cached_icons', $icon, $slug, $type );
1014
1015 if ( isset( $cached_icons[ $slug ] ) ) {
1016 $value = $cached_icons[ $slug ];
1017 } else {
1018 $value = array(
1019 'lasttime_cached' => time(),
1020 'path_custom' => '',
1021 'path' => '',
1022 );
1023 }
1024
1025 $value['lasttime_cached'] = time();
1026
1027 if ( $custom_icon ) {
1028 $value['path_custom'] = $icon;
1029 } else {
1030 $value['path'] = $icon;
1031 }
1032
1033 // update cache.
1034 $cached_icons[ $slug ] = $value;
1035
1036 MainWP_DB::instance()->update_general_option( $option_name, $cached_icons, 'array' );
1037 return true;
1038 }
1039
1040 /**
1041 * Private function Fetch a plugin|theme icon via API from WordPress.org
1042 *
1043 * @param string $slug Plugin|Theme slug.
1044 * @param string $type Plugin|Theme.
1045 */
1046 private static function fetch_wp_org_icons( $slug, $type ) { // phpcs:ignore -- NOSONAR - complex.
1047 if ( 'plugin' === $type ) {
1048 $fields = array(
1049 'tags' => false,
1050 'icons' => true,
1051 'sections' => false,
1052 'description' => false,
1053 'tested' => false,
1054 'requires' => false,
1055 'rating' => false,
1056 'downloaded' => false,
1057 'downloadlink' => false,
1058 'last_updated' => false,
1059 'homepage' => false,
1060 'compatibility' => false,
1061 'ratings' => false,
1062 'added' => false,
1063 'donate_link' => false,
1064 );
1065 } elseif ( 'theme' === $type ) {
1066 $fields = array(
1067 'screenshots' => true,
1068 'screenshot_count' => 5,
1069 'sections' => false,
1070 'rating' => false,
1071 'downloaded' => false,
1072 'download_link' => false,
1073 'last_updated' => false,
1074 'tags' => false,
1075 'template' => false,
1076 'parent' => false,
1077 'screenshot_url' => false,
1078 'homepage' => false,
1079 );
1080
1081 } else {
1082 return false;
1083 }
1084
1085 $icon = '';
1086 if ( 'theme' === $type ) {
1087 // with $fields empty to get screenshot_url of theme.
1088 $info = static::get_plugin_theme_info(
1089 $type,
1090 array(
1091 'slug' => $slug,
1092 'timeout' => 60,
1093 )
1094 );
1095 if ( is_object( $info ) && ! empty( $info->screenshot_url ) ) {
1096 $icon = $info->screenshot_url;
1097 }
1098 }
1099
1100 // if get screenshot_url of theme success.
1101 if ( ! empty( $icon ) ) {
1102 $option_name = 'themes_icons';
1103 } else {
1104 $info = static::get_plugin_theme_info(
1105 $type,
1106 array(
1107 'slug' => $slug,
1108 'fields' => $fields,
1109 'timeout' => 60,
1110 )
1111 );
1112 $option_name = 'plugins_icons';
1113 $icon = '';
1114 if ( 'plugin' === $type ) {
1115 if ( is_object( $info ) && property_exists( $info, 'icons' ) && isset( $info->icons['1x'] ) ) {
1116 $icon = $info->icons['1x'];
1117 }
1118 } else {
1119 if ( is_object( $info ) && property_exists( $info, 'screenshots' ) && isset( $info->screenshots[0] ) ) {
1120 $icon = $info->screenshots[0];
1121 }
1122 $option_name = 'themes_icons';
1123 }
1124 }
1125
1126 $fetched_icon = '';
1127 if ( '' !== $icon ) {
1128 $fetched_icon = rawurlencode( $icon );
1129 }
1130
1131 $cached_icons = MainWP_DB::instance()->get_general_option( $option_name, 'array' );
1132
1133 if ( isset( $cached_icons[ $slug ] ) && '' === $fetched_icon && ! empty( $cached_icons[ $slug ]['path'] ) ) {
1134 // if fetch icon empty then used caching icon.
1135 $fetched_icon = $cached_icons[ $slug ]['path'];
1136 $icon = rawurldecode( $fetched_icon );
1137 }
1138
1139 static::update_cached_icons( $fetched_icon, $slug, $type );
1140
1141 if ( '' !== $icon ) {
1142 return $icon;
1143 }
1144 return false;
1145 }
1146
1147 /**
1148 * Method handle_get_icon()
1149 *
1150 * @param string $slug Plugin slug.
1151 * @param string $type Type: theme|plugin.
1152 */
1153 public static function handle_get_icon( $slug, $type ) {
1154 if ( empty( $slug ) ) {
1155 return false;
1156 }
1157 if ( 'plugin' === $type || 'theme' === $type ) {
1158 return static::fetch_wp_org_icons( $slug, $type );
1159 }
1160 return '';
1161 }
1162
1163
1164 /**
1165 * Gets a plugin icon via API from WordPress.org
1166 *
1167 * @param string $slug Plugin slug.
1168 * @param bool $forced_get Forced get icon, default: false.
1169 */
1170 public static function get_plugin_icon( $slug, $forced_get = false ) { // phpcs:ignore -- NOSONAR - complex.
1171
1172 $icon = apply_filters( 'mainwp_get_plugin_theme_icon', '', $slug, 'plugin' );
1173
1174 if ( ! empty( $icon ) ) {
1175 return $icon;
1176 }
1177
1178 $forced_get = apply_filters( 'mainwp_forced_get_plugin_theme_icon', $forced_get, $slug, 'plugin' );
1179
1180 if ( $forced_get ) {
1181 $fet_icon = static::fetch_wp_org_icons( $slug, 'plugin' );
1182 if ( false !== $fet_icon ) {
1183 $scr = MainWP_Utility::remove_http_prefix( $fet_icon );
1184 return '<img style="display:inline-block" class="ui mini circular image" updated-icon="true" src="' . esc_attr( $scr ) . '" />';
1185 }
1186 return $icon;
1187 }
1188
1189 // checks expired.
1190 $cached_icons = MainWP_DB::instance()->get_general_option( 'plugins_icons', 'array' );
1191
1192 if ( ! empty( $cached_icons ) ) {
1193 $lasttime_clear_cached = MainWP_DB::instance()->get_general_option( 'lasttime_clear_cached_plugins_icon' );
1194 if ( time() > ( intval( $lasttime_clear_cached ) + MONTH_IN_SECONDS ) ) {
1195 $updated = false;
1196 $new_cached = array();
1197 foreach ( $cached_icons as $sl => $val ) {
1198 if ( empty( $val['path_custom'] ) && time() < ( intval( $val['lasttime_cached'] ) + 12 * MONTH_IN_SECONDS ) ) {
1199 $new_cached[ $sl ] = $val; // unset.
1200 $updated = true;
1201 }
1202 }
1203 if ( $updated ) {
1204 MainWP_DB::instance()->update_general_option( 'plugins_icons', $new_cached, 'array' );
1205 }
1206 MainWP_DB::instance()->update_general_option( 'lasttime_clear_cached_plugins_icon', time() );
1207 }
1208 }
1209
1210 return static::get_plugin_theme_icon( $slug, 'plugin' );
1211 }
1212
1213 /**
1214 * Gets a theme icon via API from WordPress.org
1215 *
1216 * @param string $slug Theme slug.
1217 * @param bool $forced_get Forced get icon, default: false.
1218 */
1219 public static function get_theme_icon( $slug, $forced_get = false ) { // phpcs:ignore -- NOSONAR - complex.
1220
1221 $icon = apply_filters( 'mainwp_get_plugin_theme_icon', '', $slug, 'theme' );
1222
1223 if ( ! empty( $icon ) ) {
1224 return $icon;
1225 }
1226
1227 $forced_get = apply_filters( 'mainwp_forced_get_plugin_theme_icon', $forced_get, $slug, 'theme' );
1228
1229 if ( $forced_get ) {
1230 $fet_icon = static::fetch_wp_org_icons( $slug, 'theme' );
1231 if ( false !== $fet_icon ) {
1232 $scr = MainWP_Utility::remove_http_prefix( $fet_icon );
1233 $icon = '<img style="display:inline-block" class="ui mini circular image" updated-icon="true" src="' . esc_attr( $scr ) . '" />';
1234 }
1235 return $icon;
1236 }
1237
1238 // checks expired.
1239 $cached_icons = MainWP_DB::instance()->get_general_option( 'themes_icons', 'array' );
1240
1241 if ( ! empty( $cached_icons ) ) {
1242 $lasttime_clear_cached = MainWP_DB::instance()->get_general_option( 'lasttime_clear_cached_themes_icon' );
1243 if ( time() > ( intval( $lasttime_clear_cached ) + MONTH_IN_SECONDS ) ) {
1244 $updated = false;
1245 $new_cached = array();
1246 foreach ( $cached_icons as $sl => $val ) {
1247 if ( empty( $val['path_custom'] ) && time() < ( intval( $val['lasttime_cached'] ) + 12 * MONTH_IN_SECONDS ) ) {
1248 $new_cached[ $sl ] = $val;
1249 $updated = true;
1250 }
1251 }
1252 if ( $updated ) {
1253 MainWP_DB::instance()->update_general_option( 'themes_icons', $new_cached, 'array' );
1254 }
1255 MainWP_DB::instance()->update_general_option( 'lasttime_clear_cached_themes_icon', time() );
1256 }
1257 }
1258
1259 return static::get_plugin_theme_icon( $slug, 'theme' );
1260 }
1261
1262
1263 /**
1264 * Gets a plugin|theme icon to output.
1265 *
1266 * @param string $slug Plugin|Theme slug.
1267 * @param string $type Type icon, plugin|theme.
1268 */
1269 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.
1270
1271 if ( 'plugin' === $type ) {
1272 $option_name = 'plugins_icons';
1273 } elseif ( 'theme' === $type ) {
1274 $option_name = 'themes_icons';
1275 } else {
1276 return '<i style="font-size: 17px" class="plug circular inverted icon" not-cached-path="true"></i>';
1277 }
1278
1279 $cached_icons = MainWP_DB::instance()->get_general_option( $option_name, 'array' );
1280
1281 $cached_days = apply_filters( 'mainwp_plugin_theme_icon_cache_days', 15, $slug, $type ); // default 15 days.
1282
1283 $attr_slug = ' icon-type="' . esc_attr( $type ) . '" item-slug="' . esc_attr( $slug ) . '" ';
1284 $cls_expired = ' cached-icon-expired ';
1285 $cls_uploadable = ' cached-icon-customable ';
1286
1287 $icon = '';
1288
1289 if ( isset( $cached_icons[ $slug ] ) ) {
1290 $scr = '';
1291 $is_custom_icon = false;
1292 if ( ! empty( $cached_icons[ $slug ]['path_custom'] ) ) {
1293 if ( 'plugin' === $type ) {
1294 $dirs = static::get_mainwp_dir( 'plugin-icons', true );
1295 } elseif ( 'theme' === $type ) {
1296 $dirs = static::get_mainwp_dir( 'theme-icons', true );
1297 }
1298 $scr = $dirs[1] . rawurldecode( $cached_icons[ $slug ]['path_custom'] );
1299 $is_custom_icon = true; // custom icons will not expired.
1300 } elseif ( ! empty( $cached_icons[ $slug ]['path'] ) ) {
1301 $scr = rawurldecode( $cached_icons[ $slug ]['path'] );
1302 $scr = MainWP_Utility::remove_http_prefix( $scr );
1303 }
1304
1305 $set_cached_expired = apply_filters( 'mainwp_cache_icon_expired', false, $slug, 'theme' );
1306 $set_expired = false;
1307
1308 if ( $set_cached_expired && time() > ( intval( $cached_icons[ $slug ]['lasttime_cached'] ) + 15 * MINUTE_IN_SECONDS ) ) {
1309 $set_expired = true;
1310 }
1311
1312 $forced_exprided = 1700238511;
1313 $lasttime_cached = isset( $cached_icons[ $slug ]['lasttime_cached'] ) ? intval( $cached_icons[ $slug ]['lasttime_cached'] ) : 0;
1314
1315 if ( time() > ( $lasttime_cached + $cached_days * DAY_IN_SECONDS ) || $lasttime_cached < $forced_exprided ) { // expired.
1316 if ( ! empty( $scr ) ) {
1317 $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.
1318 } else {
1319 $icon = '<i style="font-size: 17px" class="plug circular inverted icon ' . $cls_expired . $cls_uploadable . '" ' . $attr_slug . '></i>'; // to update expired icon.
1320 }
1321 } elseif ( ! empty( $scr ) ) {
1322 $use_cls_expired = $set_expired ? $cls_expired : '';
1323 $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"/>';
1324 } else {
1325 $icon = '<i style="font-size: 17px" class="plug circular inverted icon ' . ( $set_expired ? $cls_expired : '' ) . $cls_uploadable . '" ' . $attr_slug . ' cached-path-icon="true"></i>';
1326 }
1327 } elseif ( empty( $icon ) ) {
1328 $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.
1329 }
1330 return $icon;
1331 }
1332
1333
1334 /**
1335 * Method handle_upload_image().
1336 *
1337 * Handle upload icons.
1338 *
1339 * @param string $sub_folder The sub folder.
1340 * @param mixed $file_uploader The file uploader.
1341 * @param mixed $file_index The index of file uploader.
1342 * @param bool $file_subindex Is file with sub index.
1343 * @param int $max_width max image width.
1344 * @param int $max_height max image height.
1345 */
1346 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.
1347
1348 $dirs = static::get_mainwp_dir( $sub_folder, true );
1349 $base_dir = $dirs[0];
1350 $base_url = $dirs[1];
1351
1352 $output = array();
1353 $filename = '';
1354 $filepath = '';
1355
1356 $file_types = array(
1357 'image/jpeg',
1358 'image/jpg',
1359 'image/gif',
1360 'image/x-icon',
1361 'image/png',
1362 );
1363
1364 $file_exts = array(
1365 'jpeg',
1366 'jpg',
1367 'gif',
1368 'ico',
1369 'png',
1370 );
1371
1372 $upload_ok = ( false === $file_subindex ) ? ( UPLOAD_ERR_OK === $file_uploader['error'][ $file_index ] ) : ( UPLOAD_ERR_OK === $file_uploader['error'][ $file_index ][ $file_subindex ] );
1373
1374 if ( $upload_ok ) {
1375 $tmp_file = ( false === $file_subindex ) ? ( $file_uploader['tmp_name'][ $file_index ] ) : ( $file_uploader['tmp_name'][ $file_index ][ $file_subindex ] );
1376
1377 if ( is_uploaded_file( $tmp_file ) ) {
1378 if ( false === $file_subindex ) {
1379 $file_size = $file_uploader['size'][ $file_index ];
1380 $file_type = $file_uploader['type'][ $file_index ];
1381 $file_name = $file_uploader['name'][ $file_index ];
1382 } else {
1383 $file_size = $file_uploader['size'][ $file_index ][ $file_subindex ];
1384 $file_type = $file_uploader['type'][ $file_index ][ $file_subindex ];
1385 $file_name = $file_uploader['name'][ $file_index ][ $file_subindex ];
1386 }
1387
1388 $file_extension = strtolower( pathinfo( $file_name, PATHINFO_EXTENSION ) );
1389
1390 if ( $file_size > 500 * 1025 ) {
1391 $output['error'][] = 3;
1392 } elseif ( ! in_array( $file_type, $file_types ) ) {
1393 $output['error'][] = 4;
1394 } elseif ( ! in_array( $file_extension, $file_exts ) ) {
1395 $output['error'][] = 5;
1396 } else {
1397
1398 $dest_file = $base_dir . '/' . $file_name;
1399 $dest_file = dirname( $dest_file ) . '/' . wp_unique_filename( dirname( $dest_file ), basename( $dest_file ) );
1400
1401 if ( move_uploaded_file( $tmp_file, $dest_file ) ) {
1402 if ( file_exists( $dest_file ) ) {
1403 list( $width, $height ) = getimagesize( $dest_file );
1404 }
1405
1406 $resize = false;
1407 if ( $width > $max_width ) {
1408 $dst_width = $max_width;
1409 if ( $height > $max_height ) {
1410 $dst_height = $max_height;
1411 } else {
1412 $dst_height = $height;
1413 }
1414 $resize = true;
1415 } elseif ( $height > $max_height ) {
1416 $dst_width = $width;
1417 $dst_height = $max_height;
1418 $resize = true;
1419 }
1420
1421 if ( $resize ) {
1422 $src = $dest_file;
1423 $cropped_file = wp_crop_image( $src, 0, 0, $width, $height, $dst_width, $dst_height, false );
1424 if ( ! $cropped_file || is_wp_error( $cropped_file ) ) {
1425 $output['error'][] = 9;
1426 } else {
1427 wp_delete_file( $dest_file );
1428 $filename = basename( $cropped_file );
1429 $filepath = $cropped_file;
1430 }
1431 } else {
1432 $filename = basename( $dest_file );
1433 $filepath = $dest_file;
1434 }
1435 } else {
1436 $output['error'][] = 6;
1437 }
1438 }
1439 }
1440 }
1441 $output['fileurl'] = ! empty( $filename ) ? $base_url . '/' . $filename : '';
1442 $output['filepath'] = ! empty( $filepath ) ? $filepath : '';
1443 $output['filename'] = ! empty( $filename ) ? $filename : '';
1444
1445 return $output;
1446 }
1447
1448 /**
1449 * Method disabled_wpcore_update_by().
1450 *
1451 * Get disabled wpcore update by.
1452 *
1453 * @param string $website The website.
1454 */
1455 public static function disabled_wpcore_update_by( $website ) {
1456 $by = static::get_disabled_wpcore_update_host( $website );
1457 if ( 'flywheel' === $by ) {
1458 return esc_html__( 'FlyWheel disables WP core updates. For more information contact FlyWheel support.', 'mainwp' );
1459 } elseif ( 'pressable' === $by ) {
1460 return esc_html__( 'Pressable disables WP core updates. For more information contact Pressable support.', 'mainwp' );
1461 }
1462 return '';
1463 }
1464
1465
1466 /**
1467 * Method get_disabled_wpcore_update_host().
1468 *
1469 * Get wpcore update disabled for the websites on FlyWheel host or Pressable host.
1470 *
1471 * @param mixed $website data.
1472 */
1473 public static function get_disabled_wpcore_update_host( $website ) {
1474 if ( empty( $website ) ) {
1475 return '';
1476 }
1477 $wphost = MainWP_DB::instance()->get_website_option( $website, 'wphost' );
1478 if ( ! empty( $wphost ) && ( 'flywheel' !== $wphost && 'pressable' !== $wphost ) ) {
1479 $wphost = '';
1480 }
1481 return empty( $wphost ) ? '' : $wphost;
1482 }
1483
1484
1485 /**
1486 * Method get_connect_sign_algorithm().
1487 *
1488 * Get supported sign algorithms.
1489 *
1490 * @param mixed $website The Website object.
1491 *
1492 * @return mixed $alg Algorithm connect.
1493 */
1494 public static function get_connect_sign_algorithm( $website ) { // phpcs:ignore -- NOSONAR - complex.
1495 $alg = is_object( $website ) && property_exists( $website, 'signature_algo' ) && ! empty( $website->signature_algo ) ? $website->signature_algo : false;
1496
1497 // to fix.
1498 if ( is_numeric( $alg ) ) {
1499 $alg = intval( $alg );
1500 }
1501
1502 $default_alg = false;
1503 if ( defined( 'OPENSSL_ALGO_SHA256' ) ) {
1504 $default_alg = OPENSSL_ALGO_SHA256;
1505 }
1506
1507 if ( ! empty( $alg ) && 9999 === $alg ) {
1508 $alg = get_option( 'mainwp_connect_signature_algo', $default_alg );
1509 // to fix.
1510 if ( is_numeric( $alg ) ) {
1511 $alg = intval( $alg );
1512 }
1513 }
1514
1515 if ( empty( $alg ) ) {
1516 $site_info = MainWP_DB::instance()->get_website_option( $website, 'site_info' );
1517 $site_info = ! empty( $site_info ) ? json_decode( $site_info, true ) : array();
1518 if ( is_array( $site_info ) && ! empty( $site_info['child_version'] ) && version_compare( $site_info['child_version'], '4.5', '>=' ) ) {
1519 $alg = $default_alg;
1520 }
1521 }
1522
1523 if ( ! static::is_valid_supported_sign_alg( $alg ) ) {
1524 $alg = false;
1525 }
1526
1527 $alg = apply_filters( 'mainwp_connect_sign_algo', $alg, $website );
1528
1529 return $alg;
1530 }
1531
1532 /**
1533 * Method is_valid_supported_sign_alg()
1534 *
1535 * Check if is supported sign algorithms.
1536 *
1537 * @param int $alg The Sign Algo value.
1538 */
1539 public static function is_valid_supported_sign_alg( $alg ) {
1540 $valid = false;
1541 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 ) ) {
1542 $valid = true;
1543 }
1544 return $valid;
1545 }
1546
1547 /**
1548 * Method get_signature_alg()
1549 *
1550 * Get custom signature algorithms.
1551 */
1552 public static function get_open_ssl_sign_algos() {
1553 $values = array();
1554
1555 if ( defined( 'OPENSSL_ALGO_SHA1' ) ) {
1556 $values[ OPENSSL_ALGO_SHA1 ] = 'OPENSSL_ALGO_SHA1';
1557 }
1558 if ( defined( 'OPENSSL_ALGO_SHA224' ) ) {
1559 $values[ OPENSSL_ALGO_SHA224 ] = 'OPENSSL_ALGO_SHA224';
1560 }
1561
1562 if ( defined( 'OPENSSL_ALGO_SHA256' ) ) {
1563 $values[ OPENSSL_ALGO_SHA256 ] = 'OPENSSL_ALGO_SHA256 ' . esc_html__( '(Default)', 'mainwp' );
1564 }
1565
1566 if ( defined( 'OPENSSL_ALGO_SHA384' ) ) {
1567 $values[ OPENSSL_ALGO_SHA384 ] = 'OPENSSL_ALGO_SHA384';
1568 }
1569
1570 if ( defined( 'OPENSSL_ALGO_SHA512' ) ) {
1571 $values[ OPENSSL_ALGO_SHA512 ] = 'OPENSSL_ALGO_SHA512';
1572 }
1573
1574 return $values;
1575 }
1576
1577 /**
1578 * Method get_default_map_site_fields()
1579 *
1580 * Get default map site fields.
1581 */
1582 public static function get_default_map_site_fields() {
1583 return array(
1584 'id',
1585 'url',
1586 'name',
1587 'adminname',
1588 'privkey',
1589 'http_user',
1590 'http_pass',
1591 'ssl_version',
1592 'sync_errors',
1593 'signature_algo',
1594 'verify_method',
1595 );
1596 }
1597
1598 /**
1599 * Method get_staging_options_sites_view_for_current_users()
1600 *
1601 * Get staging options sites view for current users.
1602 *
1603 * @return string Site views.
1604 */
1605 public static function get_staging_options_sites_view_for_current_users() {
1606 $view = apply_filters( 'mainwp_staging_current_user_sites_view', 'undefined' );
1607 if ( 'undefined' === $view ) { // to compatible.
1608 $view = get_user_option( 'mainwp_staging_options_updates_view' );
1609 }
1610 return $view;
1611 }
1612 }
1613