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