1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\Min;
9:
10: use Aurora\Modules\Min\Models\MinHash;
11: use Illuminate\Database\Capsule\Manager as Capsule;
12:
13: /**
14: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
15: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
16: * @copyright Copyright (c) 2023, Afterlogic Corp.
17: *
18: * @property Module $oModule
19: */
20: class Manager extends \Aurora\System\Managers\AbstractManager
21: {
22: public function __construct(\Aurora\System\Module\AbstractModule $oModule = null)
23: {
24: parent::__construct($oModule);
25: }
26:
27: /**
28: * @param string $sHashID
29: * @param array $aParams
30: *
31: * @return string|bool
32: */
33: public function createMin($sHashID, $aParams, $iUserId = null, $iExpireDate = null)
34: {
35: $mResult = false;
36: $sNewMin = '';
37:
38: if (is_string($sHashID) && 0 < strlen($sHashID) && false !== $this->getMinByID($sHashID)) {
39: return false;
40: }
41:
42: while (true) {
43: $sNewMin = \Illuminate\Support\Str::random(10);
44: if (false === $this->getMinByHash($sNewMin)) {
45: break;
46: }
47: }
48:
49: if (0 < strlen($sNewMin)) {
50: $aParams['__hash_id__'] = $sHashID;
51: $aParams['__hash__'] = $sNewMin;
52: $aParams['__time__'] = time();
53: $aParams['__time_update__'] = time();
54:
55: if (MinHash::create([
56: 'Hash' => $sNewMin,
57: 'HashId' => md5($sHashID),
58: 'UserId' => $iUserId,
59: 'Data' => @\json_encode($aParams),
60: 'ExpireDate' => $iExpireDate
61: ])) {
62: $mResult = $sNewMin;
63: }
64: }
65:
66: return $mResult;
67: }
68:
69: /**
70: * @param Models\MinHash $oMin
71: *
72: * @return array|bool
73: */
74: private function parseGetMinDbResult($oMin)
75: {
76: $mResult = false;
77:
78: if ($oMin && !empty($oMin->Data)) {
79: $aData = @\json_decode($oMin->Data, true);
80: if (is_array($aData) && 0 < count($aData)) {
81: $mResult = $aData;
82:
83: if ($oMin->ExpireDate) {
84: $mResult['ExpireDate'] = $oMin->ExpireDate;
85: }
86: }
87: }
88:
89: return $mResult;
90: }
91:
92: /**
93: * @param string $sHashID
94: *
95: * @return array|bool
96: */
97: public function getMinByID($sHashID)
98: {
99: $oMin = MinHash::firstWhere('HashId', \md5($sHashID));
100: return $this->parseGetMinDbResult($oMin);
101: }
102:
103: /**
104: * @param string $sHash
105: *
106: * @return array|bool
107: */
108: public function getMinByHash($sHash)
109: {
110: $oMin = MinHash::where(Capsule::connection()->raw('BINARY `Hash`'), $sHash)->first();
111: return $this->parseGetMinDbResult($oMin);
112: }
113:
114: /**
115: * @deprecated since version 9.7.3
116: * @param int $iUserId
117: *
118: * @return array|bool
119: */
120: public function getMinListByUserId($iUserId)
121: {
122: return MinHash::where('UserId', $iUserId)->get();
123: }
124:
125: /**
126: * @param string $sHashID
127: *
128: * @return bool
129: */
130: public function deleteMinByID($sHashID)
131: {
132: return !!MinHash::where('HashId', \md5($sHashID))->delete();
133: }
134:
135: /**
136: * @param string $sHash
137: *
138: * @return bool
139: */
140: public function deleteMinByHash($sHash)
141: {
142: return !!MinHash::where(Capsule::connection()->raw('BINARY `Hash`'), $sHash)->delete();
143: }
144:
145: /**
146: * @param string $sHashID
147: * @param array $aParams
148: * @param string $sNewHashID Default value is **null**
149: *
150: * @return bool
151: */
152: public function updateMinByID($sHashID, $aParams, $sNewHashID = null)
153: {
154: $aPrevParams = $this->getMinByID($sHashID);
155: if (isset($aPrevParams['__hash__'])) {
156: $aParams['__hash__'] = $aPrevParams['__hash__'];
157: }
158: if (!empty($sNewHashID)) {
159: $aParams['__hash_id__'] = $sNewHashID;
160: }
161: if (isset($aPrevParams['__time__'])) {
162: $aParams['__time__'] = $aPrevParams['__time__'];
163: }
164:
165: $aParams['__time_update__'] = time();
166: $aMergedParams = array_merge($aPrevParams, $aParams);
167:
168: $aUpdate = [
169: 'Data' => @\json_encode($aMergedParams)
170: ];
171: if (!empty($sNewHashID)) {
172: $aUpdate['HashId'] = \md5($sNewHashID);
173: }
174:
175: return MinHash::where('HashId', \md5($sHashID))->update($aUpdate);
176: }
177:
178: /**
179: * @param string $sHash
180: * @param array $aParams
181: * @param string $sNewHashID Default value is **null**
182: *
183: * @return bool
184: */
185: public function updateMinByHash($sHash, $aParams, $sNewHashID = null)
186: {
187: $aPrevParams = $this->getMinByHash($sHash);
188: if (isset($aPrevParams['__hash_id__'])) {
189: $aParams['__hash_id__'] = $aPrevParams['__hash_id__'];
190: }
191: if (!empty($sNewHashID)) {
192: $aParams['__hash_id__'] = $sNewHashID;
193: }
194: if (isset($aPrevParams['__time__'])) {
195: $aParams['__time__'] = $aPrevParams['__time__'];
196: }
197:
198: $aParams['__time_update__'] = time();
199:
200: $aUpdate = [
201: 'Data' => @\json_encode($aParams)
202: ];
203: if (!empty($sNewHashID)) {
204: $aUpdate['HashId'] = \md5($sNewHashID);
205: }
206:
207: return MinHash::where('Hash', $sHash)->update($aUpdate);
208: }
209: }
210: