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

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