<?php if (!defined('STORE_ROOT')) { http_response_code(403); exit; } class PublicId { const PREFIX_STORE = 'ST'; const PREFIX_PRODUCT = 'PD'; const PREFIX_ORDER = 'OR'; const PREFIX_USER = 'US'; const PREFIX_REVIEW = 'RV'; const PREFIX_FAQ = 'FQ'; const PREFIX_TICKET = 'TK'; const PREFIX_CATEGORY = 'CT'; public static function generate($prefix, $length = 6) { $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789'; $maxRetries = 10; for ($attempt = 0; $attempt < $maxRetries; $attempt++) { $id = $prefix . '_'; for ($i = 0; $i < $length; $i++) { $id .= $chars[random_int(0, strlen($chars) - 1)]; } $exists = Database::value( "SELECT COUNT(*) FROM public_ids WHERE public_id = :pid", [':pid' => $id] ); if (!$exists) return $id; } return $prefix . '_' . bin2hex(random_bytes(6)); } public static function create($prefix, $internalId, $entityType) { $publicId = self::generate($prefix); Database::insert( "INSERT INTO public_ids (public_id, internal_id, entity_type, created_at)              VALUES (:pid, :iid, :etype, NOW())", [':pid' => $publicId, ':iid' => $internalId, ':etype' => $entityType] ); return $publicId; } public static function resolve($publicId) { return Database::value( "SELECT internal_id FROM public_ids WHERE public_id = :pid", [':pid' => $publicId] ); } public static function generateSku() { $maxRetries = 20; for ($attempt = 0; $attempt < $maxRetries; $attempt++) { $digits = ''; for ($i = 0; $i < 6; $i++) { $digits .= random_int(0, 9); } $sku = 'SUPS_' . $digits; $exists = Database::value( "SELECT COUNT(*) FROM product_variants WHERE sku = :sku", [':sku' => $sku] ); if (!$exists) { $exists2 = Database::value( "SELECT COUNT(*) FROM products WHERE sku = :sku", [':sku' => $sku] ); if (!$exists2) return $sku; } } return 'SUPS_' . bin2hex(random_bytes(3)); } } 
