| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sanitization file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub; |
| 9 |
|
| 10 |
use Activitypub\Model\Blog; |
| 11 |
|
| 12 |
/** |
| 13 |
* Sanitization class. |
| 14 |
*/ |
| 15 |
class Sanitize { |
| 16 |
/** |
| 17 |
* Sanitize a list of URLs. |
| 18 |
* |
| 19 |
* @param string|array $value The value to sanitize. |
| 20 |
* @return array The sanitized list of URLs. |
| 21 |
*/ |
| 22 |
public static function url_list( $value ) { |
| 23 |
if ( ! \is_array( $value ) ) { |
| 24 |
$value = \explode( PHP_EOL, $value ); |
| 25 |
} |
| 26 |
|
| 27 |
$value = \array_filter( $value ); |
| 28 |
$value = \array_map( 'trim', $value ); |
| 29 |
$value = \array_map( 'sanitize_url', $value ); |
| 30 |
$value = \array_unique( $value ); |
| 31 |
|
| 32 |
return \array_values( $value ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Sanitize a list of hosts. |
| 37 |
* |
| 38 |
* @param string $value The value to sanitize. |
| 39 |
* @return string The sanitized list of hosts. |
| 40 |
*/ |
| 41 |
public static function host_list( $value ) { |
| 42 |
$value = \explode( PHP_EOL, $value ); |
| 43 |
$value = \array_map( |
| 44 |
function ( $host ) { |
| 45 |
$host = \trim( $host ); |
| 46 |
$host = \strtolower( $host ); |
| 47 |
$host = \set_url_scheme( $host ); |
| 48 |
$host = \sanitize_url( $host, array( 'http', 'https' ) ); |
| 49 |
|
| 50 |
// Remove protocol. |
| 51 |
if ( \str_contains( $host, 'http' ) ) { |
| 52 |
$host = \wp_parse_url( $host, PHP_URL_HOST ); |
| 53 |
} |
| 54 |
|
| 55 |
return \filter_var( $host, FILTER_VALIDATE_DOMAIN ); |
| 56 |
}, |
| 57 |
$value |
| 58 |
); |
| 59 |
|
| 60 |
return \implode( PHP_EOL, \array_filter( $value ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Sanitize a blog identifier. |
| 65 |
* |
| 66 |
* @param string $value The value to sanitize. |
| 67 |
* @return string The sanitized blog identifier. |
| 68 |
*/ |
| 69 |
public static function blog_identifier( $value ) { |
| 70 |
// Hack to allow dots in the username. |
| 71 |
$parts = \explode( '.', $value ); |
| 72 |
$sanitized = \array_map( 'sanitize_title', $parts ); |
| 73 |
$sanitized = \implode( '.', $sanitized ); |
| 74 |
|
| 75 |
// Check for login or nicename. |
| 76 |
$user = new \WP_User_Query( |
| 77 |
array( |
| 78 |
'search' => $sanitized, |
| 79 |
'search_columns' => array( 'user_login', 'user_nicename' ), |
| 80 |
'number' => 1, |
| 81 |
'hide_empty' => true, |
| 82 |
'fields' => 'ID', |
| 83 |
) |
| 84 |
); |
| 85 |
|
| 86 |
if ( $user->get_results() ) { |
| 87 |
\add_settings_error( |
| 88 |
'activitypub_blog_identifier', |
| 89 |
'activitypub_blog_identifier', |
| 90 |
\esc_html__( 'You cannot use an existing author’s name for the blog profile ID.', 'activitypub' ) |
| 91 |
); |
| 92 |
|
| 93 |
return Blog::get_default_username(); |
| 94 |
} |
| 95 |
|
| 96 |
return $sanitized; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the sanitized value of a constant. |
| 101 |
* |
| 102 |
* @param mixed $value The constant value. |
| 103 |
* |
| 104 |
* @return string The sanitized value. |
| 105 |
*/ |
| 106 |
public static function constant_value( $value ) { |
| 107 |
if ( is_bool( $value ) ) { |
| 108 |
return $value ? 'true' : 'false'; |
| 109 |
} |
| 110 |
|
| 111 |
if ( is_string( $value ) ) { |
| 112 |
return esc_attr( $value ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( is_array( $value ) ) { |
| 116 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 117 |
return print_r( $value, true ); |
| 118 |
} |
| 119 |
|
| 120 |
return $value; |
| 121 |
} |
| 122 |
} |
| 123 |
|