| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace Wikimedia\Reflection; |
| 22 | |
| 23 | /** |
| 24 | * Trait for accessing "ghost fields". |
| 25 | * |
| 26 | * Ghost fields are fields that have been created in an object instance |
| 27 | * by PHP's unserialize() mechanism, though they no longer exist |
| 28 | * in the current version of the corresponding class. |
| 29 | * |
| 30 | * Accessing non-public ghost fields offers some challenges due to |
| 31 | * how they are handled by PHP internally. |
| 32 | * |
| 33 | * @see https://www.php.net/manual/en/language.types.array.php#language.types.array.casting |
| 34 | * @since 1.36 |
| 35 | */ |
| 36 | trait GhostFieldAccessTrait { |
| 37 | |
| 38 | /** |
| 39 | * Get the value of the ghost field named $name, |
| 40 | * or null if the field does not exist. |
| 41 | * |
| 42 | * @param string $name |
| 43 | * @param class-string ...$aliases |
| 44 | * @return mixed|null |
| 45 | */ |
| 46 | private function getGhostFieldValue( string $name, string ...$aliases ) { |
| 47 | if ( isset( $this->$name ) ) { |
| 48 | return $this->$name; |
| 49 | } |
| 50 | |
| 51 | $data = (array)$this; |
| 52 | |
| 53 | // Protected variables have a '*' prepended to the variable name. |
| 54 | // These prepended values have null bytes on either side. |
| 55 | $protectedName = "\x00*\x00{$name}"; |
| 56 | if ( isset( $data[$protectedName] ) ) { |
| 57 | return $data[$protectedName]; |
| 58 | } |
| 59 | |
| 60 | // Private variables have the class name prepended to the variable name. |
| 61 | // These prepended values have null bytes on either side. |
| 62 | $thisClass = get_class( $this ); |
| 63 | $privateName = "\x00{$thisClass}\x00{$name}"; |
| 64 | if ( isset( $data[$privateName] ) ) { |
| 65 | return $data[$privateName]; |
| 66 | } |
| 67 | |
| 68 | // Check old aliased class names as well. |
| 69 | foreach ( $aliases as $thisClass ) { |
| 70 | $privateName = "\x00{$thisClass}\x00{$name}"; |
| 71 | if ( isset( $data[$privateName] ) ) { |
| 72 | return $data[$privateName]; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * In PHP <= 8.1, private fields of a class are not properly restored |
| 81 | * when the class is aliased to a new name, since the private fields are |
| 82 | * prefixed with the *old* name of the class. This method can be used |
| 83 | * to restore them if present after deserialization. |
| 84 | * |
| 85 | * @param string $name |
| 86 | * @param class-string ...$aliases |
| 87 | */ |
| 88 | private function restoreAliasedGhostField( string $name, string ...$aliases ): void { |
| 89 | $data = (array)$this; |
| 90 | foreach ( $aliases as $thisClass ) { |
| 91 | $privateName = "\x00{$thisClass}\x00{$name}"; |
| 92 | if ( isset( $data[$privateName] ) ) { |
| 93 | $this->$name = $data[$privateName]; |
| 94 | unset( $data[$privateName] ); |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |