ip = $ip; $this->type = self::TYPE_REGISTERED; $this->email = $email; $this->status = self::STATUS_PENDING; $this->timestamp = time(); return $this->save(); } /** * Checks whether a pending unlock request already exists. * * @param string $ip The IP address to check. * @param string $email The email address to check. * * @return bool */ public static function has_pending_request( $ip, $email ): bool { $model = self::get_orm()->get_repository( self::class ) ->where( 'ip', $ip ) ->where( 'email', $email ) ->where( 'status', self::STATUS_PENDING ) ->first(); return is_object( $model ); } /** * Remove data by given data. * * @param int $timestamp The timestamp to compare against. * @param int $limit The maximum number of records to delete. * * @return void */ public static function remove_records( $timestamp, $limit ) { $orm = self::get_orm(); $orm->get_repository( self::class ) ->where( 'timestamp', '<=', $timestamp ) ->order_by( 'id' ) ->limit( $limit ) ->delete_by_limit(); } /** * Remove all records. * * @return void */ public static function truncate() { $orm = self::get_orm(); $orm->get_repository( self::class )->truncate(); } /** * Retrieves the resolved IP address by the given ID, email, and limit time. * * @param int $id The ID of the record. * @param string $email The email associated with the record. * @param int $limit_time The limit time for the record. * * @return string The resolved IP address if it exists and is not expired, otherwise 'expired' or an empty string. */ public static function get_resolved_ip_by( $id, $email, $limit_time ) { $orm = self::get_orm(); $model = $orm->get_repository( self::class ) ->where( 'id', $id ) ->where( 'email', $email ) ->where( 'status', self::STATUS_PENDING ) ->first(); if ( ! is_object( $model ) ) { return ''; } if ( $model->timestamp > $limit_time ) { $model->status = self::STATUS_RESOLVED; $orm->save( $model ); return $model->ip; } else { return 'expired'; } } }