This repository was archived by the owner on Jul 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqo.class
More file actions
168 lines (148 loc) · 6.58 KB
/
qo.class
File metadata and controls
168 lines (148 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace CG;
use CG as r;
use CG\DB as db;
use CG\Util as u;
/**
* @file
* QID object class (alphabetically coded account number or account/agent pair)
* Strategy: Store only whatever is easiest. Figure out the rest as needed.
*/
define('QR_FMTS', '1/2 1/3 2/2 2/3 3/2 3/3 3/4 4/4 4/5');
class Qo {
private $main; // mainQid or main may be empty, but not both
private $mainQid;
private $agent; // empty if proSe (otherwise agentQid or agentCode or agent must be non-empty)
private $agentQid;
private $agentCode;
/**
* Create a QID object from the given qid or uid and optional agent qid or uid
*/
function __construct($main, $agent = '') {
$qo = $this;
if (is_numeric($main)) return list ($qo->main, $qo->agent) = [$main, $agent]; // agent may be empty
if (substr($main, 0, 1) == '.') return $qo->mainQid = R_SERVER_ID . substr($main, 1); // abbreviated
if (strpos($main, ':') !== FALSE) { // old style relation
if (substr($main, 0, 1) == ':') $main = R_SERVER_ID . $main; // unabbreviate
$reid = qo(str_replace(':', '', $main))->id;
$res = r\relation('main,other', $reid);
u\EXPECT((bool) $res, 'non-existent old-style relation qid: ' . $main);
return list ($qo->main, $qo->agent) = [$res['main'], $res['other']];
}
$main = str_replace('.', '', $main); // old style proSe (sent by old app, for example)
if ($i = strpos($main, AGT_MARK)) {
u\EXPECT(!$agent, 'agent specified with compound main QID');
list ($qo->mainQid, $qo->agentCode) = explode(AGT_MARK, $main);
} else list ($qo->mainQid, $qo->agentQid) = [$main, $agent];
}
/**
* Return a pseudo-member
*/
function __get($field) {
$qo = $this;
if ($field == 'id') return $qo->id();
if ($field == 'agent') return $qo->agent();
if ($field == 'ia') return [$qo->id(), $qo->agent()];
if ($field == 'rel') return (@$qo->agent or @$qo->agentQid or @$qo->agentCode);
if ($field == 'proSe') return !$qo->rel;
if ($field == 'agentCode') return $qo->agentCode();
if ($field == 'agentNum') return $qo->rel ? u\a2n($qo->agentCode()) : 0;
if ($field == 'agentQid') return $qo->agentQid ?: $qo->agentQid = qid($qo->agent);
if (!$qo->mainQid) $qo->mainQid = qid($qo->main); // these next fields require mainQid
if ($field == 'mainQid') return $qo->mainQid;
if ($field == 'qid') return @$qo->mainQid . ($qo->rel ? AGT_MARK . $qo->agentCode() : '');
if ($field == 'region') return substr($qo->mainQid, 0, strlen($qo->mainQid) > 7 ? 4 : 3);
if ($field == 'tail') return substr($qo->mainQid, strlen($qo->mainQid) > 7 ? 4 : 3);
u\EXPECT(FALSE, 'bad qid field: ' . $field);
}
private function id() {
$qo = $this;
if ($qo->main) return $qo->main; else $qid = $qo->mainQid;
if (substr($qid, 0, 1) == '!') {
if (strlen($qid) < 6) $qid .= 'AAA';
return $qo->main = -qo(substr($qid, 1))->id;
} else return $qo->main = -r\serverUid($qo->region) + u\a2n($qo->tail);
}
private function agent() {
$qo = $this;
if ($qo->rel) {
if ($agt = @$qo->agent) return $agt;
if (@$qo->agentQid) return $qo->agent = qo($qo->agentQid)->id;
list ($main, $num) = [$qo->id, u\a2n($qo->agentCode) + 1]; // agentNum 1 is A
return $qo->agent = r\relation('other', 'main=:main AND otherNum=:num', compact('main', 'num'));
} else return $qo->id();
}
private function agentCode() {
$qo = $this;
if ($code = @$qo->agentCode) return $code;
if ($qo->proSe) return '';
if (!$num = r\relation('otherNum', $qo->id(), $qo->agent())) return '';
return $qo->agentCode = u\n2a($num - 1, -1); // agent num 1 is A
}
/**
* Return a more tightly-encoded qid for use in a QR code or magnetic stripe (radix 36 instead of 26)
* The first character tells the length of region, account in region, and agentCode, as follows:
* 0-3: 1/2/n, n=c (where c is a radix-36 digit)
* 4-7: 1/3/n, n=c-4
* 8-B: 2/2/n, n=c-8
* C-F: 2/3/n, n=c-C (meaning c minus twelve)
* G-J: 3/2/n, n=c-G, etc.
* K-N: 3/3/n
* O-R: 3/4/n
* S-V: 4/4/n
* W-Z: 4/5/n
* @return [fmt, region, tail, agentCode], where fmt is the format character
*/
public function qr() {
$qo = $this;
$regLen = strlen($region = u\n2a(u\a2n($qo->region), -1, 36));
$tailLen = strlen($tail = u\n2a(u\a2n($qo->tail), $regLen > 3 ? -4 : -2, 36));
$codeLen = ($agentCode = $qo->agentCode) ? strlen($agentCode = u\n2a(u\a2n($agentCode), -1, 36)) : 0;
$i = array_search($regLen . $tailLen, ray('12 13 22 23 32 33 34 44 45'));
u\EXPECT($i !== FALSE, 'bad piece lengths in qo->qr mainQid=' . $qo->mainQid);
u\EXPECT($codeLen < 4, 'agentCode too long: ' . $qo->agentCode);
$fmt = u\n2a($i * 4 + $codeLen, 1, 36);
return [$fmt, $region, $tail, $agentCode];
}
/**
* Convert uid to qid
* Call by:
* qid(uid)
* qid(uid, agent)
* @param int uid: record id of the account
* @param int agent: uid of agent (defaults to uid)
* @return the corresponding qid
*/
public static function uid2($uid, $agent = '') {
if (is_numeric($uid) and (!$agent or $agent == $uid)) { // proSe uid
if ($uid < 0) return '!' . qid(-$uid);
$region = r\uidRegion($uid);
$regionNum = (-$region - 1) / R_REGION_MAX;
return u\n2a($regionNum, -3) . u\n2a($uid - -$region, -3);
} else return qo($uid, $agent)->qid; // more complicated
}
/**
* Convert qr to qid
* @param string qr: qr-style account code
* @return the corresponding qid (FALSE if none)
*/
public static function qr2($qr) {
$fmt = substr($qr, 0, 1);
$c = u\a2n($fmt, 36);
$fmts = ray(QR_FMTS);
list ($regionLen, $acctLen) = explode('/', $fmts[$c / 4]);
$agentLen = $c % 4;
/// debug(compact(ray('qr fmt c fmts regionLen acctLen agentLen')) + ['thefmt' => $fmts[$c / 4]]);
if (strlen($qr) != 1 + $regionLen + $acctLen + $agentLen) return FALSE;
$region = u\n2a(u\a2n(substr($qr, 1, $regionLen), 36), -3);
$acct = u\n2a(u\a2n(substr($qr, 1 + $regionLen, $acctLen), 36), -3);
$agent = $agentLen ? '-' . u\n2a(u\a2n(substr($qr, -$agentLen), 36), -1) : '';
/// debug(compact(ray('qr fmt c regionLen acctLen agentLen region acct agent')));
return $region . $acct . $agent;
}
public function parse() {return [$this->region, $this->tail, $this->agentCode];}
// public function fmt() {return substr($this->qid, 0, 3) . ' ' . substr($this->qid, 3);}
public function fmt() {return $this->qid;} // no spaces or punc
} // end of class
function qo($main, $agent = '') {return new r\Qo($main, $agent);}
function qid($main, $agent = '') {return r\Qo::uid2($main, $agent);}