master

laravel/framework

Last updated at: 29/12/2023 09:20

Recaller.php

TLDR

This file contains the Recaller class, which is responsible for managing the "recaller" or "remember me" functionality in the authentication system.

Methods

__construct

This method is the constructor of the Recaller class. It creates a new instance of the Recaller class and sets the internal $recaller property based on the provided $recaller argument.

id

This method returns the user ID extracted from the $recaller string.

token

This method returns the "remember token" extracted from the $recaller string.

hash

This method returns the password hash extracted from the $recaller string.

valid

This method determines if the $recaller string is valid by checking if it is a proper string and if it has all the required segments.

properString

This protected method checks if the $recaller property is a string and contains the "|" character.

hasAllSegments

This protected method checks if the $recaller property has all the required segments by splitting it into segments and verifying their count and content.

segments

This method returns an array of segments extracted from the $recaller property.

<?php

namespace Illuminate\Auth;

class Recaller
{
    /**
     * The "recaller" / "remember me" cookie string.
     *
     * @var string
     */
    protected $recaller;

    /**
     * Create a new recaller instance.
     *
     * @param  string  $recaller
     * @return void
     */
    public function __construct($recaller)
    {
        $this->recaller = @unserialize($recaller, ['allowed_classes' => false]) ?: $recaller;
    }

    /**
     * Get the user ID from the recaller.
     *
     * @return string
     */
    public function id()
    {
        return explode('|', $this->recaller, 3)[0];
    }

    /**
     * Get the "remember token" token from the recaller.
     *
     * @return string
     */
    public function token()
    {
        return explode('|', $this->recaller, 3)[1];
    }

    /**
     * Get the password from the recaller.
     *
     * @return string
     */
    public function hash()
    {
        return explode('|', $this->recaller, 4)[2];
    }

    /**
     * Determine if the recaller is valid.
     *
     * @return bool
     */
    public function valid()
    {
        return $this->properString() && $this->hasAllSegments();
    }

    /**
     * Determine if the recaller is an invalid string.
     *
     * @return bool
     */
    protected function properString()
    {
        return is_string($this->recaller) && str_contains($this->recaller, '|');
    }

    /**
     * Determine if the recaller has all segments.
     *
     * @return bool
     */
    protected function hasAllSegments()
    {
        $segments = explode('|', $this->recaller);

        return count($segments) >= 3 && trim($segments[0]) !== '' && trim($segments[1]) !== '';
    }

    /**
     * Get the recaller's segments.
     *
     * @return array
     */
    public function segments()
    {
        return explode('|', $this->recaller);
    }
}