diff --git a/library/Utils.php b/library/Utils.php deleted file mode 100644 index 7e31d9bfa89b50758af21dad4c4aab729d91bc87..0000000000000000000000000000000000000000 --- a/library/Utils.php +++ /dev/null @@ -1,75 +0,0 @@ -<?php - -class Utils -{ - private function __construct () - { - } - - /** - * @param array $array - * @return array - * Note : Recursively filters out empty values and arrays at any depth. - */ - public static function recursiveArrayFilter (array $array): array - { - return array_filter($array, function ($item) { - if (is_array($item)) { - $item = self::recursiveArrayFilter($item); - } - return !empty($item); - }); - } - - /** - * Find matching keys between 2 lists. - * - * @param array|null $elements - * @param array $keys - * @return array - */ - public static function findMatchingKeys (?array $elements, array $keys): array - { - $matching = []; - - foreach ($elements as $element) { - foreach ($keys as $key) { - if (!empty($element) && array_key_exists($key, $element)) { - $matching[] = $key; - } - } - } - - return $matching; - } - - /** - * @param $array - * @return array - * Note : simply return all values of a multi-dimensional array. - */ - public static function getArrayValuesRecursive ($array) - { - $values = []; - foreach ($array as $value) { - if (is_array($value)) { - // If value is an array, merge its values recursively - $values = array_merge($values, self::getArrayValuesRecursive($value)); - } else { - // If value is not an array, add it to the result - $values[] = $value; - } - } - return $values; - } - - /** - * @param string $text - * @return string - * Note : This come from jwtToken, as it is completely private - it is cloned here for now. - */ - public static function base64urlEncode (string $text): string - { - return str_replace(["+", "/", "="], ["A", "B", ""], base64_encode($text)); - } -} \ No newline at end of file diff --git a/library/MailUtils.php b/library/plugins/MailUtils.php similarity index 70% rename from library/MailUtils.php rename to library/plugins/MailUtils.php index 5fc496a8232f3f09543371c2c027a99333b193ce..87f4c6420bcd265b82ffb32d511bd63f66d79422 100644 --- a/library/MailUtils.php +++ b/library/plugins/MailUtils.php @@ -2,11 +2,11 @@ class MailUtils { - private function __construct () + public function __construct () { } - public static function sendMail ($setFrom, $setBCC, $recipients, $body, $signature, $subject, $receipt, $attachments) + public function sendMail ($setFrom, $setBCC, $recipients, $body, $signature, $subject, $receipt, $attachments) { $mail_controller = new \FusionDirectory\Mail\MailLib($setFrom, $setBCC, @@ -24,7 +24,7 @@ class MailUtils * @return array * Note : A simple retrieval methods of the mail backend configuration set in FusionDirectory */ - public static function getMailObjectConfiguration (TaskGateway $gateway): array + public function getMailObjectConfiguration (TaskGateway $gateway): array { return $gateway->getLdapTasks( "(objectClass=fdTasksConf)", diff --git a/library/TokenUtils.php b/library/plugins/ReminderTokenUtils.php similarity index 77% rename from library/TokenUtils.php rename to library/plugins/ReminderTokenUtils.php index 8552514794e1d306864fbd93aab825d67caaa9d1..4ead9b4d8aad98c93671093f435671c49dcd8879 100644 --- a/library/TokenUtils.php +++ b/library/plugins/ReminderTokenUtils.php @@ -1,8 +1,8 @@ <?php -class TokenUtils +class ReminderTokenUtils { - private function __construct () + public function __construct () { } @@ -12,7 +12,7 @@ class TokenUtils * @return string * @throws Exception */ - public static function generateToken (string $userDN, int $timeStamp, TaskGateway $gateway): string + public function generateToken (string $userDN, int $timeStamp, TaskGateway $gateway): string { $token = NULL; // Salt has been generated with APG. @@ -25,10 +25,10 @@ class TokenUtils $token_hmac = hash_hmac("sha256", $time . $payload, $_ENV["SECRET_KEY"], TRUE); // We need to have a token allowed to be used within an URL. - $token = Utils::base64urlEncode($token_hmac); + $token = $this->base64urlEncode($token_hmac); // Save token within LDAP - self::saveTokenInLdap($userDN, $token, $timeStamp, $gateway); + $this->saveTokenInLdap($userDN, $token, $timeStamp, $gateway); return $token; } @@ -41,7 +41,7 @@ class TokenUtils * @return bool * @throws Exception */ - public static function saveTokenInLdap (string $userDN, string $token, int $days, TaskGateway $gateway): bool + private function saveTokenInLdap (string $userDN, string $token, int $days, TaskGateway $gateway): bool { $result = FALSE; @@ -64,17 +64,17 @@ class TokenUtils // Verify if token ou branch exists - if (!self::tokenBranchExist('ou=tokens' . ',' . $_ENV["LDAP_BASE"], $gateway)) { + if (!$this->tokenBranchExist('ou=tokens' . ',' . $_ENV["LDAP_BASE"], $gateway)) { // Create the branch - self::createBranchToken($gateway); + $this->createBranchToken($gateway); } // The user token DN creation $userTokenDN = 'cn=' . $uid . ',ou=tokens' . ',' . $_ENV["LDAP_BASE"]; // Verify if a token already exists for specified user and remove it to create new one correctly. - if (self::tokenBranchExist($userTokenDN, $gateway)) { + if ($this->tokenBranchExist($userTokenDN, $gateway)) { // Remove the user token - self::removeUserToken($userTokenDN, $gateway); + $this->removeUserToken($userTokenDN, $gateway); } // Add token to LDAP for specific UID @@ -95,7 +95,7 @@ class TokenUtils * @return int * Note : Simply return the difference between first and second call. (First call can be null). */ - public static function getTokenExpiration (int $subTaskCall, int $firstCall, int $secondCall): int + public function getTokenExpiration (int $subTaskCall, int $firstCall, int $secondCall): int { // if firstCall is empty, secondCall is the timestamp expiry for the token. $result = $secondCall; @@ -115,7 +115,7 @@ class TokenUtils * @return void * Note : Simply remove the token for specific user DN */ - public static function removeUserToken ($userTokenDN, TaskGateway $gateway): void + private function removeUserToken ($userTokenDN, TaskGateway $gateway): void { // Add token to LDAP for specific UID try { @@ -130,7 +130,7 @@ class TokenUtils * Create ou=pluginManager LDAP branch * @throws Exception */ - public static function createBranchToken (TaskGateway $gateway): void + private function createBranchToken (TaskGateway $gateway): void { try { ldap_add( @@ -153,7 +153,7 @@ class TokenUtils * @param string $taskDN * @return array */ - public static function generateTokenUrl (string $token, array $mailTemplateForm, string $taskDN): array + public function generateTokenUrl (string $token, array $mailTemplateForm, string $taskDN): array { //Only take the cn of the main task name : preg_match('/cn=([^,]+),ou=/', $taskDN, $matches); @@ -173,7 +173,7 @@ class TokenUtils * @return bool * Note : Simply inspect if the branch for token is existing. */ - public static function tokenBranchExist (string $dn, TaskGateway $gateway): bool + private function tokenBranchExist (string $dn, TaskGateway $gateway): bool { $result = FALSE; @@ -195,4 +195,14 @@ class TokenUtils return $result; } + + /** + * @param string $text + * @return string + * Note : This come from jwtToken, as it is completely private - it is cloned here for now. + */ + private function base64urlEncode (string $text): string + { + return str_replace(["+", "/", "="], ["A", "B", ""], base64_encode($text)); + } } \ No newline at end of file diff --git a/library/plugins/Utils.php b/library/plugins/Utils.php new file mode 100644 index 0000000000000000000000000000000000000000..10eafe626a4d288e6a5a66a80b54102547b05ca5 --- /dev/null +++ b/library/plugins/Utils.php @@ -0,0 +1,59 @@ +<?php + +class Utils +{ + public function __construct () + { + } + + /** + * @param array $array + * @return array + * Note : Recursively filters out empty values and arrays at any depth. + */ + public function recursiveArrayFilter (array $array): array + { + return array_filter($array, function ($item) { + if (is_array($item)) { + $item = $this->recursiveArrayFilter($item); + } + return !empty($item); + }); + } + + /** + * Find matching keys between 2 lists. + * + * @param array|null $elements + * @param array $keys + * @return array + */ + public function findMatchingKeys (?array $elements, array $keys): array + { + $matching = []; + + if (!empty($elements)) { + foreach ($elements as $element) { + foreach ($keys as $key) { + if (!empty($element) && array_key_exists($key, $element)) { + $matching[] = $key; + } + } + } + } + + return $matching; + } + + /** + * @param $array + * @return array + * Note : simply return all values of a multi-dimensional array. + */ + public function getArrayValuesRecursive ($array) + { + return array_reduce($array, function ($carry, $value) { + return array_merge($carry, is_array($value) ? $this->getArrayValuesRecursive($value) : [$value]); + }, []); + } +} \ No newline at end of file diff --git a/plugins/tasks/Audit.php b/plugins/tasks/Audit.php index c4bbe4033affd9813370aa9fb38a51aec03a7f85..94818316140c49b7b8f0ed19d0a8373639baa358 100644 --- a/plugins/tasks/Audit.php +++ b/plugins/tasks/Audit.php @@ -4,10 +4,12 @@ class Audit implements EndpointInterface { private TaskGateway $gateway; + private Utils $utils; public function __construct (TaskGateway $gateway) { $this->gateway = $gateway; + $this->utils = new Utils(); } /** @@ -47,7 +49,7 @@ class Audit implements EndpointInterface $result = $this->processAuditDeletion($this->gateway->getObjectTypeTask('Audit')); // Recursive function to filter out empty arrays at any depth - $nonEmptyResults = Utils::recursiveArrayFilter($result); + $nonEmptyResults = $this->utils->recursiveArrayFilter($result); if (!empty($nonEmptyResults)) { return $nonEmptyResults; diff --git a/plugins/tasks/Notifications.php b/plugins/tasks/Notifications.php index e17ffe3f162bae5357154a4578e333b3bf77e46d..4547b87f2ab471069783e77b48e8f4e960529e5c 100644 --- a/plugins/tasks/Notifications.php +++ b/plugins/tasks/Notifications.php @@ -4,10 +4,12 @@ class Notifications implements EndpointInterface { private TaskGateway $gateway; + private Utils $utils; public function __construct (TaskGateway $gateway) { $this->gateway = $gateway; + $this->utils = new Utils(); } /** @@ -82,7 +84,7 @@ class Notifications implements EndpointInterface $this->gateway->unsetCountKeys($monitoredSupannResource); // Find matching attributes between audited and monitored attributes - $matchingAttrs = Utils::findMatchingKeys($auditAttributes, $monitoredAttrs); + $matchingAttrs = $this->utils->findMatchingKeys($auditAttributes, $monitoredAttrs); // Verify Supann resource state if applicable if ($this->shouldVerifySupannResource($monitoredSupannResource, $auditAttributes)) { @@ -183,7 +185,7 @@ class Notifications implements EndpointInterface } // Get all the values only of a multidimensional array. - $auditedValues = Utils::getArrayValuesRecursive($auditedAttrs); + $auditedValues = $this->utils->getArrayValuesRecursive($auditedAttrs); if (in_array($monitoredSupannState, $auditedValues)) { $result = TRUE; diff --git a/plugins/tasks/Reminder.php b/plugins/tasks/Reminder.php index 1faeff7b2f67e7a6566461740190f9dc77209f20..ceb619a712d9bf9f0608dca0349f53321c5dd949 100644 --- a/plugins/tasks/Reminder.php +++ b/plugins/tasks/Reminder.php @@ -4,10 +4,12 @@ class Reminder implements EndpointInterface { private TaskGateway $gateway; - + private ReminderTokenUtils $reminderTokenUtils; + public function __construct (TaskGateway $gateway) { $this->gateway = $gateway; + $this->reminderTokenUtils = new ReminderTokenUtils(); } /** @@ -109,13 +111,13 @@ class Reminder implements EndpointInterface $reminders[$remindersMainTaskName]['subTask'][$task['cn'][0]]['uid'] = $task['fdtasksgranulardn'][0]; // Create timeStamp expiration for token - $tokenExpire = TokenUtils::getTokenExpiration($task['fdtasksgranularhelper'][0], + $tokenExpire = $this->reminderTokenUtils->getTokenExpiration($task['fdtasksgranularhelper'][0], $remindersMainTask[0]['fdtasksreminderfirstcall'][0], $remindersMainTask[0]['fdtasksremindersecondcall'][0]); // Create token for SubTask - $token = TokenUtils::generateToken($task['fdtasksgranulardn'][0], $tokenExpire, $this->gateway); + $token = $this->reminderTokenUtils->generateToken($task['fdtasksgranulardn'][0], $tokenExpire, $this->gateway); // Edit the mailForm with the url link containing the token - $tokenMailTemplateForm = TokenUtils::generateTokenUrl($token, $mailTemplateForm, $remindersMainTaskName); + $tokenMailTemplateForm = $this->reminderTokenUtils->generateTokenUrl($token, $mailTemplateForm, $remindersMainTaskName); // Recipient email form $reminders[$remindersMainTaskName]['subTask'][$task['cn'][0]]['mail'] = $tokenMailTemplateForm; @@ -136,13 +138,13 @@ class Reminder implements EndpointInterface $reminders[$remindersMainTaskName]['subTask'][$task['cn'][0]]['uid'] = $task['fdtasksgranulardn'][0]; // Create timeStamp expiration for token - $tokenExpire = TokenUtils::getTokenExpiration($task['fdtasksgranularhelper'][0], + $tokenExpire = $this->reminderTokenUtils->getTokenExpiration($task['fdtasksgranularhelper'][0], $remindersMainTask[0]['fdtasksreminderfirstcall'][0], $remindersMainTask[0]['fdtasksremindersecondcall'][0]); // Create token for SubTask - $token = TokenUtils::generateToken($task['fdtasksgranulardn'][0], $tokenExpire, $this->gateway); + $token = $this->reminderTokenUtils->generateToken($task['fdtasksgranulardn'][0], $tokenExpire, $this->gateway); // Edit the mailForm with the url link containing the token - $tokenMailTemplateForm = TokenUtils::generateTokenUrl($token, $mailTemplateForm, $remindersMainTaskName); + $tokenMailTemplateForm = $this->reminderTokenUtils->generateTokenUrl($token, $mailTemplateForm, $remindersMainTaskName); // Recipient email form $reminders[$remindersMainTaskName]['subTask'][$task['cn'][0]]['mail'] = $tokenMailTemplateForm;