| 1 | <?php |
| 2 | /** |
| 3 | * Contains a class for dealing with individual log entries |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | * @author Niklas Laxström |
| 22 | * @license GPL-2.0-or-later |
| 23 | * @since 1.19 |
| 24 | */ |
| 25 | |
| 26 | use MediaWiki\Registration\ExtensionRegistry; |
| 27 | |
| 28 | /** |
| 29 | * Extends the LogEntry Interface with some basic functionality |
| 30 | * |
| 31 | * @since 1.19 |
| 32 | */ |
| 33 | abstract class LogEntryBase implements LogEntry { |
| 34 | |
| 35 | public function getFullType() { |
| 36 | return $this->getType() . '/' . $this->getSubtype(); |
| 37 | } |
| 38 | |
| 39 | public function isDeleted( $field ) { |
| 40 | return ( $this->getDeleted() & $field ) === $field; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Whether the parameters for this log are stored in new or |
| 45 | * old format. |
| 46 | * |
| 47 | * @return bool |
| 48 | */ |
| 49 | public function isLegacy() { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create a blob from a parameter array |
| 55 | * |
| 56 | * @since 1.26 |
| 57 | * @param array $params |
| 58 | * @return string |
| 59 | */ |
| 60 | public static function makeParamBlob( $params ) { |
| 61 | return serialize( (array)$params ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Extract a parameter array from a blob |
| 66 | * |
| 67 | * @since 1.26 |
| 68 | * @param string $blob |
| 69 | * @param ?string $logType Full log type of the corresponding log entry (see LogEntryBase::getFullType()) |
| 70 | * @return array|false Array on success, false if the blob is not serialized data |
| 71 | */ |
| 72 | public static function extractParams( $blob, ?string $logType = null ) { |
| 73 | // Don't allow serialized class instances, because log entries can be imported from user input (T422244). |
| 74 | // However, extensions currently may store arbitrary data in log params, so we need a way to allow |
| 75 | // such log entries to be handled. This is discouraged and the option may be removed in the future. |
| 76 | $attribute = ExtensionRegistry::getInstance()->getAttribute( 'LogParamsAllowedClasses' ); |
| 77 | if ( $logType && array_key_exists( $logType, $attribute ) && is_array( $attribute[$logType] ) ) { |
| 78 | $allowedClasses = $attribute[$logType]; |
| 79 | } else { |
| 80 | $allowedClasses = false; |
| 81 | } |
| 82 | // Suppress errors about invalid serialized data, because this is also called |
| 83 | // for legacy log entries, which don't use serialization |
| 84 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 85 | $result = @unserialize( $blob, [ 'allowed_classes' => $allowedClasses ] ); |
| 86 | if ( $result !== false && !is_array( $result ) ) { |
| 87 | // Non-arrays are not allowed here |
| 88 | return false; |
| 89 | } |
| 90 | return $result; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Whether the parameters for this log entry contain forbidden serialized data. |
| 95 | */ |
| 96 | public static function containsUnsafeParams( array $params ): bool { |
| 97 | $result = false; |
| 98 | $params = [ $params ]; |
| 99 | array_walk_recursive( $params, static function ( $val ) use ( &$result ) { |
| 100 | if ( $val instanceof \__PHP_Incomplete_Class ) { |
| 101 | $result = true; |
| 102 | } |
| 103 | } ); |
| 104 | return $result; |
| 105 | } |
| 106 | } |