Skip to content

Developer notes

app/addons/kv_vendor_domains/
addon.xml, init.php, func.php
Tygh/Addons/KvVendorDomains/
Hosts.php pure host, URL and dispatch helpers (unit-tested)
Rules.php pure status rules: P/W/A/R/S/D transitions, primary host, plan limits (unit-tested)
Dns.php TXT ownership + CNAME/A routing check with an injectable resolver (unit-tested)
Config.php settings, main host, base domain
Domains.php records, plan rules, cached host map, DNS checks, cron re-check
Runtime.php vendor host recognition, routing, url_post, canonical URLs
Seo.php sitemap.xml and robots.txt of a vendor host
License/Client.php, License/Ed25519.php, Upgrade/Synchronizer.php (generated)
controllers/backend/kv_vendor_domains.php admin and vendor pages, cron mode "recheck"
controllers/backend/vendor_plans.post.php domain rule for the plan editor
controllers/frontend/kv_vendor_domains_public.php to_main, robots, sitemap (vendor hosts), add (marketplace domain)
schemas/{menu,permissions,block_manager}/ menu, privileges, block cache scope
design/backend/templates/addons/kv_vendor_domains/ manage, plans, update, status component, plan editor hook
var/themes_repository/responsive/templates/addons/kv_vendor_domains/hooks/index/scripts.post.tpl
js/addons/kv_vendor_domains/func.js storefront script (vendor hosts only)
Table Columns
cscart_kv_vendor_domains domain_id, company_id, type (S subdomain, C custom), host (UNIQUE, lower case, no port), status (P pending DNS, W waiting approval, A active, R rejected, S suspended, D disabled), is_primary, token, txt_ok, route_ok, fail_count, last_check, last_detail, reason, created_at, updated_at, approved_at, approved_by
cscart_kv_vendor_domains_plan_rules plan_id, allow_subdomain, allow_custom, max_domains

Uninstall drops both tables, the two privileges and the language variables.

  1. init.php calls Runtime::boot(). Add-ons initialize inside CS-Cart’s init stack, after the session and before routing. boot() returns at once for the admin, the API, the console and for requests whose host equals the storefront host. Otherwise it looks the host up in the cached host map (Domains::hostMap(), Registry cache tagged with the domain, rule, company, vendor plan and settings tables), checks the license for the main host and then rewrites config.http_host / https_host / current_host / *_location. From here on every fn_url() link, SEO redirect and asset URL carries the vendor host. Without this the core would 301 the request to the default storefront (fn_dispatch).
  2. get_route (priority 1900, after the SEO add-on at 1800) maps /robots.txt and /sitemap(-N).xml to the public controller, / to companies.view or companies.products of the vendor, redirects other vendors’ pages and products to the same path on the marketplace, and redirects main-only dispatches. GET and HEAD get a 302; POST gets a 307 and AJAX gets force_redirection from the public controller (to_main), with the ajax parameters stripped.
  3. get_products_pre forces company_id of the vendor in every product list on a vendor host. (categories, products.search, features, blocks.)
  4. url_post (1900): links of main-only dispatches and links that the core built for the main host with the vendor’s company_id are rewritten (main-only to the marketplace, vendor links to the vendor host).
  5. dispatch_before_display (1900): seo_canonical, seo_alt_hreflangs_list and schema_org_markup_items are rewritten from the hosts of the page to the target origin (see Configuration, Canonical URLs). The scripts.post.tpl hook prints the script configuration on vendor hosts only.

Hosts are trusted only when they equal an active record exactly (case-insensitive, port ignored) of an active vendor whose plan allows the type. HTTP_HOST/X-Forwarded-Host values that match nothing are handled by the core as before.

CS-Cart builds the session before add-ons initialize. Its name carries md5(http_location) of the main host and its cookie domain is derived from the main host. Consequences, and why the add-on never relies on them:

  • Subdomain of the marketplace host (acme.example.com with the marketplace on example.com): the cookie domain .example.com is sent to vendor subdomains, so the session may be shared. If the marketplace runs on www.example.com the cookie domain is .www.example.com and it is not shared.
  • Custom domain: the browser drops a cookie for .example.com set by shop.acme.com; every request starts a guest session that is never reused.
  • Therefore everything that needs a session runs on the marketplace domain: links are rewritten, add-to-cart / wishlist / comparison buttons are re-pointed by the script (a top-level GET navigation to the marketplace, see below), and requests that still arrive on a vendor host are redirected (302, 307 for POST, force_redirection for AJAX). Login, registration, checkout, payment callbacks and the account never run on a vendor host.
  • Add to cart. The core session cookie is SameSite=Lax, so a cross-site POST from a vendor’s own domain would not carry it (every add would start a new guest session and replace the visitor’s cookie). The storefront script therefore takes the click over and opens kv_vendor_domains_public.add on the marketplace domain with a top-level GET navigation, which carries the Lax cookie; the product form is in the query string. The endpoint adds directly only when the Referer is an active vendor host and every product belongs to that vendor (then the checks of core checkout.add apply: anonymous shopping, only the product of the pressed button via extra, amount 1 when empty; it adds with fn_add_product_to_cart(), recalculates with 'E', saves the cart and opens the cart page). In every other case (no Referer, for example under a no-referrer policy, a foreign site, the marketplace host itself, or a product of another vendor) nothing is added: a confirmation page on the marketplace lists the products and amounts, and its form posts to kv_vendor_domains_public.confirm, which adds only with a session-bound token (HMAC of the session id with the store crypt key; checked by the add-on itself, so it also holds when the core storefront CSRF check is off, as with the full page cache). The visitor makes one extra click. Core checkout.add is not used here because on 4.17 it answers AJAX requests only. The wishlist button (a cm-submit link with data-ca-dispatch) uses the same endpoint with to=wishlist (core wishlist.add answers POST only). Comparison links (cm-ajax links to product_features.add_product, already pointing to the marketplace through url_post) would be a cross-origin XHR without the cookie, so the script opens them as a top-level navigation and lands on the marketplace comparison list. Tested in a real browser with the marketplace on localhost and the vendor on a .test domain: two adds in a row, add after login, wishlist, comparison and a no-referrer page (confirmation, then add) all stay in one session and keep the login.
  • Cookies written with fn_set_cookie() (language, currency) are host-only (no domain) and therefore per vendor host.
  • The mini-cart block on a vendor host shows that host’s session (empty for custom domains); the cart link opens the marketplace cart.
  • Consequence for CSRF: storefront POSTs do not use the security hash (config.tweaks.anti_csrf is forced off with the full page cache).

Untested. The points below are the design; no full page cache or Varnish run was made.

  • Output of vendor hosts depends on the host only: no session or visitor data, no cookies. Varnish’s default vcl_hash hashes URL and Host, so every host gets its own cached objects. Do not normalize or strip the Host header in your VCL; add hash_data(req.http.host); if you replaced vcl_hash.
  • The public controller (kv_vendor_domains_public) is not in the cacheable dispatch list of schemas/full_page_cache/varnish.php; sitemap output is cached inside CS-Cart (Registry cache tagged with products, seo_names, …).
  • ESI (esi.php) requests keep the Host of the page, so session-dependent blocks (cart_content, my_account) render for the vendor host and show a guest state on custom domains.
  • Cache tags: page tags come from the tables read while rendering. A domain change touches kv_vendor_domains, which is not a global tag; pages are not affected by it, only the host map is.

The core’s block cache key has no host. On a vendor host blocks list only that vendor’s products and carry the host in their links, so schemas/block_manager/block_cache_properties.post.php adds the callable handler fn_kv_vendor_domains_scope_key ('' on the marketplace host, h<host> on a vendor host) to the key of every cached block, and kv_vendor_domains to the update handlers (adding, disabling or changing a domain refreshes cached blocks).

Tygh\Addons\KvVendorDomains\License\Client verifies the license against the Kavio Labs license server.

  • Gate: Runtime::licensed($mainHost) (License\Client::isActive() for the marketplace host) in Runtime::boot(). Vendor hosts are covered by the marketplace license. When it is false the add-on does not recognize vendor hosts, so the core redirects them to the marketplace; sitemap and robots disappear with them. Admin and vendor pages are not gated.
  • State: kv_license_kv_vendor_domains (?:storage_data), or var/kv_storage/kv_vendor_domains/license.json while the full page cache is active, protected by an HMAC with the store’s crypt key.
  • Cron: php admin.php --dispatch=kv_vendor_domains_license.check.
  • Development stores: the constants KV_LICENSE_SERVER_URL, KV_LICENSE_TEST_HOST, KV_LICENSE_TEST_TIME_OFFSET (ignored on production hosts).
  • Admin license UI (generated): card, badge, status bar (first line of the mainbox of manage, plans and update), license.less.
Hook Handler Purpose
get_route (1900) fn_kv_vendor_domains_get_route Routing on vendor hosts
url_post (1900) fn_kv_vendor_domains_url_post Link rewriting
get_products_pre fn_kv_vendor_domains_get_products_pre Vendor-only product lists
dispatch_before_display (1900) fn_kv_vendor_domains_dispatch_before_display Canonical, hreflang, JSON-LD
vendor_plan_after_save / _after_delete Plan rule from the plan editor
delete_company Removes the vendor’s domains
init_templater_post Upgrade synchronizer (only while an upgrade is pending)

Arguments are identical in 4.17.2 and 4.21.2.

Hook Where Purpose
index:scripts (post) storefront Script configuration on vendor hosts
vendor_plans:update_restrictions (post) admin Domain rule fields in the plan editor
  • Controllers or dispatches that must stay on the marketplace domain are a setting, not code.
  • To run DNS checks against your own resolver, set \Tygh\Addons\KvVendorDomains\Dns::setResolver(callable $resolver) from your add-on (callable(string $name, string $type): string[], types TXT, CNAME, A, AAAA).
  • Domains::hostMap() returns ['hosts' => host => [company_id, type], 'primary' => company_id => host, 'all' => company_id => hosts] for served domains only.

Tygh/Addons/KvVendorDomains/License/Ed25519.php (checks the signature of the license server’s answers on servers whose PHP has no sodium extension) is derived from paragonie/sodium_compat, ISC License, Copyright (c) 2016-2023 Paragon Initiative Enterprises and Copyright (c) 2013-2019 Frank Denis. The full license notice is in the file header.

  • Vendor hosts render the default storefront (the host is unknown to the core, which falls back to it before add-ons initialize): layout, categories and currency of other storefronts on Multi-Vendor Ultimate are not used.
  • Master (common) products: lists on a vendor host are limited to company_id of the vendor; master products (company_id 0) with an offer of the vendor are not listed, and a master product page redirects to the marketplace.
  • Custom domains: the session cookie is not accepted there, so every request starts a guest session. 4.21 starts sessions lazily; on 4.17 and 4.18 a session row is written per request, crawlers included.
  • DNS resolution uses the system resolver (dns_get_record). A failed lookup is distinguished from a confirmed miss with a control lookup of the CNAME target (or the marketplace host).