identity.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. require_once __DIR__ . '/db.php';
  3. // Resolve the current actor (who is making this request).
  4. // - ICG pages call current_actor('ICG').
  5. // - Vendor pages call current_actor_from_vendor($_GET['v'] ?? '').
  6. // - AJAX endpoints read both an explicit ?actor=ICG flag (set by PDQ.php) and a
  7. // ?v=<slug> flag (set by vendor.php). The actor is whichever side called us.
  8. // This matches the existing trust model: access to PDQ.php vs vendor.php IS the
  9. // only access control we have. Don't trust the actor value with anything you
  10. // wouldn't already trust the URL with.
  11. function current_actor(string $expected): string {
  12. if ($expected !== 'ICG') {
  13. throw new InvalidArgumentException("current_actor expects 'ICG'");
  14. }
  15. return 'ICG';
  16. }
  17. function current_actor_from_vendor(string $slug): array {
  18. $v = find_vendor_by_slug($slug);
  19. if (!$v) {
  20. http_response_code(404);
  21. echo "Unknown vendor: " . htmlspecialchars($slug);
  22. exit;
  23. }
  24. return $v;
  25. }
  26. // Resolve the actor for an AJAX endpoint based on POST/GET fields:
  27. // actor=ICG -> 'ICG'
  28. // v=<vendor-slug> -> vendor slug
  29. // Returns [actor_label, vendor_id_or_null].
  30. function resolve_request_actor(): array {
  31. $params = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
  32. if (($params['actor'] ?? '') === 'ICG') {
  33. return ['ICG', null];
  34. }
  35. if (!empty($params['v'])) {
  36. $v = find_vendor_by_slug($params['v']);
  37. if ($v) return [$v['slug'], (int) $v['id']];
  38. }
  39. http_response_code(400);
  40. echo 'No actor specified';
  41. exit;
  42. }