BlueGit

timfa posprint

Files

posprint / lib / php / posprint_url.php

posprint_url.php

posprint_url.php 1.2 KiB Download Raw
1<?php
2/**
3 * Build a posprint:// URL from a .pos document (array or JSON string).
4 *
5 * Encoding: UTF-8 JSON → raw deflate (gzdeflate) → base64url (no padding).
6 *
7 * @param array|string $pos PosFile as associative array or JSON string
8 * @param array{noconfirm?: bool} $options Optional URL flags
9 * @return string posprint://print?v=1&d=…
10 * @throws InvalidArgumentException
11 */
12function posprint_url($pos, array $options = []): string
13{
14 if (is_array($pos)) {
15 $json = json_encode($pos, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
16 if ($json === false) {
17 throw new InvalidArgumentException('json_encode failed: ' . json_last_error_msg());
18 }
19 } elseif (is_string($pos)) {
20 $json = $pos;
21 } else {
22 throw new InvalidArgumentException('pos must be array or JSON string');
23 }
24
25 $compressed = gzdeflate($json, 9);
26 if ($compressed === false) {
27 throw new InvalidArgumentException('gzdeflate failed');
28 }
29
30 $b64 = rtrim(strtr(base64_encode($compressed), '+/', '-_'), '=');
31 $url = 'posprint://print?v=1&d=' . $b64;
32
33 if (!empty($options['noconfirm'])) {
34 $url .= '&noconfirm=1';
35 }
36
37 return $url;
38}