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\ActivityHistory;
9:
10: use Aurora\Api;
11: use Aurora\Modules\ActivityHistory\Models\ActivityHistory;
12:
13: /**
14: * System module provides hash-based object storage.
15: *
16: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
17: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
18: * @copyright Copyright (c) 2023, Afterlogic Corp.
19: *
20: * @property Settings $oModuleSettings
21: *
22: * @package Modules
23: */
24: class Module extends \Aurora\System\Module\AbstractModule
25: {
26: public $oManager = null;
27:
28: /***** private functions *****/
29: /**
30: * Initializes module.
31: *
32: * @ignore
33: */
34: public function init()
35: {
36: $this->oManager = new Manager($this);
37: $this->subscribeEvent('AddToActivityHistory', array($this, 'onAddToActivityHistory'));
38: $this->subscribeEvent('Files::Delete::after', array($this, 'onAfterFilesDelete'));
39: $this->subscribeEvent('Files::DeletePublicLink::after', array($this, 'onAfterFilesDeletePublicLink'));
40: $this->subscribeEvent('CreatePublicLink::after', array($this, 'onAfterFilesCreatePublicLink'));
41: $this->subscribeEvent('OpenPgpFilesWebclient::ValidatePublicLinkPassword::after', array($this, 'onAfterValidatePublicLinkPassword'));
42: $this->subscribeEvent('Core::DeleteUser::after', array($this, 'onAfterDeleteUser'));
43: $this->aDeniedMethodsByWebApi = [];
44: }
45:
46: /**
47: * @return Module
48: */
49: public static function getInstance()
50: {
51: return parent::getInstance();
52: }
53:
54: /**
55: * @return Module
56: */
57: public static function Decorator()
58: {
59: return parent::Decorator();
60: }
61:
62: /**
63: * @return Settings
64: */
65: public function getModuleSettings()
66: {
67: return $this->oModuleSettings;
68: }
69:
70: public function onAddToActivityHistory($aParams, &$mResult)
71: {
72: $iUserId = 0;
73: if (is_numeric($aParams['UserId'])) {
74: $iUserId = $aParams['UserId'];
75: } else {
76: $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUserByPublicId($aParams['UserId']);
77: if ($oUser) {
78: $iUserId = $oUser->Id;
79: }
80: }
81: $sGuestPublicId = isset($aParams['GuestPublicId']) ? $aParams['GuestPublicId'] : null;
82: $this->Create($iUserId, $aParams['ResourceType'], $aParams['ResourceId'], $aParams['Action'], $sGuestPublicId);
83: }
84:
85: public function onAfterFilesDelete(&$aArgs, &$mResult)
86: {
87: $iUserId = $aArgs['UserId'];
88: $sStorage = $aArgs['Type'];
89: $aItems = $aArgs['Items'];
90:
91: foreach ($aItems as $aItem) {
92: $sResourceId = $sStorage . '/' . \ltrim(\ltrim($aItem['Path'], '/') . '/' . \ltrim($aItem['Name'], '/'), '/');
93: $this->Delete($iUserId, 'file', $sResourceId);
94: }
95: }
96:
97: public function onAfterFilesCreatePublicLink(&$aArgs, &$mResult)
98: {
99: $iUserId = $aArgs['UserId'];
100: $sStorage = $aArgs['Type'];
101:
102: $sResourceId = $sStorage . '/' . \ltrim(\ltrim($aArgs['Path'], '/') . '/' . \ltrim($aArgs['Name'], '/'), '/');
103: $this->Create($iUserId, 'file', $sResourceId, 'create-public-link');
104: }
105:
106: public function onAfterValidatePublicLinkPassword(&$aArgs, &$mResult)
107: {
108: if (!$mResult) {
109: $this->CreateFromHash($aArgs['Hash'], 'wrong-password');
110: }
111: }
112:
113: public function onAfterFilesDeletePublicLink(&$aArgs, &$mResult)
114: {
115: $iUserId = $aArgs['UserId'];
116: $sStorage = $aArgs['Type'];
117:
118: $sResourceId = $sStorage . '/' . \ltrim(\ltrim($aArgs['Path'], '/') . '/' . \ltrim($aArgs['Name'], '/'), '/');
119: $this->Delete($iUserId, 'file', $sResourceId);
120: }
121:
122: public function onAfterDeleteUser($aArgs, &$mResult)
123: {
124: if ($mResult) {
125: ActivityHistory::where('UserId', $aArgs['UserId'])->delete();
126: }
127: }
128: /***** private functions *****/
129:
130: /***** public functions might be called with web API *****/
131: /**
132: */
133: public function Create($UserId, $ResourceType, $ResourceId, $Action, $GuestPublicId = null)
134: {
135: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
136:
137: $IpAddress = "";
138: if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
139: $IpAddress = $_SERVER['HTTP_CLIENT_IP'];
140: } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
141: $IpAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
142: } else {
143: $IpAddress = $_SERVER['REMOTE_ADDR'];
144: }
145:
146: if (!isset($GuestPublicId)) {
147: $oUser = \Aurora\System\Api::getAuthenticatedUser();
148: if ($oUser) {
149: $GuestPublicId = $oUser->PublicId;
150: } else {
151: $GuestPublicId = '';
152: }
153: }
154: return $this->oManager->Create($UserId, $ResourceType, $ResourceId, $IpAddress, $Action, time(), $GuestPublicId);
155: }
156:
157: public function CreateFromHash($Hash, $EventName)
158: {
159: $oMin = \Aurora\Modules\Min\Module::getInstance();
160: $mMin = $oMin->GetMinByHash($Hash);
161: if (isset($mMin['UserId']) && isset($mMin['Type']) && isset($mMin['Path']) && isset($mMin['Name'])) {
162: $mUserId = $mMin['UserId'];
163: if (is_string($mUserId)) {
164: $oUser = \Aurora\Modules\Core\Module::getInstance()->GetUserByPublicId($mUserId);
165: if ($oUser instanceof \Aurora\Modules\Core\Models\User) {
166: $mUserId = $oUser->Id;
167: }
168: }
169: if (is_int($mUserId)) {
170: $sStorage = $mMin['Type'];
171: $sResourceId = $sStorage . '/' . \ltrim(\ltrim($mMin['Path'], '/') . '/' . \ltrim($mMin['Name'], '/'), '/');
172: $this->Create($mUserId, 'file', $sResourceId, $EventName);
173: }
174: }
175: }
176:
177: /**
178: */
179: public function GetList($UserId, $ResourceType, $ResourceId, $Offset = 0, $Limit = 0)
180: {
181: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
182: Api::CheckAccess($UserId);
183: return [
184: 'Items' => $this->oManager->GetList($UserId, $ResourceType, $ResourceId, $Offset, $Limit)->all(),
185: 'Count' => $this->oManager->GetListCount($UserId, $ResourceType, $ResourceId)
186: ];
187: }
188:
189: /**
190: */
191: public function Delete($UserId, $ResourceType, $ResourceId)
192: {
193: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
194: Api::CheckAccess($UserId);
195: return $this->oManager->Delete($UserId, $ResourceType, $ResourceId);
196: }
197: /***** public functions might be called with web API *****/
198: }
199: