summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2014-10-27 11:49:36 (GMT)
committerAleksander Machniak <alec@alec.pl>2014-10-27 11:49:36 (GMT)
commite8e352bae686834ba42ebf5418c8e9eacb8e2cee (patch)
tree8269d63c565bbdf62bc5f608321667f1d6b0e3ef
parent49a78e8675819c1194b9579f93bb0d5e1c13c99f (diff)
downloadkolab-wap-e8e352bae686834ba42ebf5418c8e9eacb8e2cee.tar.gz
Support substring modifiers in primary_mail recipient policy (#3553)
-rw-r--r--lib/kolab_recipient_policy.php200
1 files changed, 76 insertions, 124 deletions
diff --git a/lib/kolab_recipient_policy.php b/lib/kolab_recipient_policy.php
index b3cc995..831121e 100644
--- a/lib/kolab_recipient_policy.php
+++ b/lib/kolab_recipient_policy.php
@@ -23,9 +23,10 @@
+--------------------------------------------------------------------------+
*/
-class kolab_recipient_policy {
-
- static function format() {
+class kolab_recipient_policy
+{
+ static function format()
+ {
$_args = func_get_args();
$args = array();
@@ -139,56 +140,9 @@ class kolab_recipient_policy {
}
$primary_mail = $conf->get_raw($userdata['domain'], 'primary_mail');
-
- preg_match_all('/%\((\w+)\)s/', $primary_mail, $substrings);
-
- // Update userdata array
- for ($x = 0; $x < count($substrings[0]); $x++) {
- if (array_key_exists($substrings[1][$x], $userdata)) {
- if (!empty($substrings[2][$x])) {
- if (!empty($substrings[3][$x])) {
- $primary_mail = preg_replace(
- '/%\(' . $substrings[1][$x]. '\)s/',
- substr(
- $userdata[$substrings[1][$x]],
- $substrings[2][$x],
- $substrings[3][$x]
- ),
- $primary_mail
- );
- } else {
- $primary_mail = preg_replace(
- '/%\(' . $substrings[1][$x]. '\)s/',
- substr(
- $userdata[$substrings[1][$x]],
- $substrings[2][$x]
- ),
- $primary_mail
- );
- }
- } elseif (!empty($substrings[3][$x])) {
- $primary_mail = preg_replace(
- '/%\(' . $substrings[1][$x]. '\)s/',
- substr(
- $userdata[$substrings[1][$x]],
- 0,
- $substrings[3][$x]
- ),
- $primary_mail
- );
- } else {
- $primary_mail = preg_replace(
- '/%\(' . $substrings[1][$x]. '\)s/',
- $userdata[$substrings[1][$x]],
- $primary_mail
- );
- }
- } else {
- Log::error("Recipient policy finds that key " . $substrings[1][$x] . " does not exist in \$userdata (primary_mail)");
- }
- }
-
+ $primary_mail = self::apply_formats($primary_mail, $userdata);
$parsed_email = self::parse_email($primary_mail);
+
return $parsed_email;
}
@@ -385,78 +339,7 @@ class kolab_recipient_policy {
);
$policy_uid = preg_replace('/(\{\d+\})/', '%s', $policy_uid);
-
- preg_match_all('/%\((\w+)\)s/', $policy_uid, $substrings);
-
- // Update userdata array
- for ($x = 0; $x < count($substrings[0]); $x++) {
- if (array_key_exists($substrings[1][$x], $userdata)) {
- if (!empty($substrings[2][$x])) {
- if (!empty($substrings[3][$x])) {
- $policy_uid = preg_replace(
- '/%\(' . $substrings[1][$x]. '\)s/',
- substr(
- $userdata[$substrings[1][$x]],
- $substrings[2][$x],
- $substrings[3][$x]
- ),
- $policy_uid
- );
-
- } else {
- $policy_uid = preg_replace(
- '/%\(' . $substrings[1][$x]. '\)s/',
- substr(
- $userdata[$substrings[1][$x]],
- $substrings[2][$x]
- ),
- $policy_uid
- );
-
- }
- } elseif (!empty($substrings[3][$x])) {
- $policy_uid = preg_replace(
- '/%\(' . $substrings[1][$x]. '\)s/',
- substr(
- $userdata[$substrings[1][$x]],
- 0,
- $substrings[3][$x]
- ),
- $policy_uid
- );
-
- } else {
- $policy_uid = preg_replace(
- '/%\(' . $substrings[1][$x]. '\)s/',
- $userdata[$substrings[1][$x]],
- $policy_uid
- );
-
- }
- }
- }
-
- preg_match_all('/.*\'(.*)\'\[(\d+):(\d+)\].*/', $policy_uid, $substrings);
-
- for ($x = 0; $x < count($substrings[0]); $x++) {
- if (!empty($substrings[2][$x])) {
- $start = $substrings[2][$x];
- } else {
- $start = 0;
- }
-
- if (!empty($substrings[3][$x])) {
- $end = $substrings[3][$x];
- } else {
- $end = 0;
- }
-
- $policy_uid = preg_replace(
- "/'" . $substrings[1][$x] . "'\[" . $substrings[2][$x] . ':' . $substrings[3][$x] . "\]/",
- substr($substrings[1][$x], $start, $end),
- $policy_uid
- );
- }
+ $policy_uid = self::apply_formats($policy_uid, $userdata);
foreach ($functions as $match => $replace) {
while (preg_match('/' . $match . '/', $policy_uid, $strings)) {
@@ -510,4 +393,73 @@ class kolab_recipient_policy {
return $email_parts[0] . '@' . $email_parts[1];
}
+ /**
+ * Implements string replacement according to defined format
+ */
+ static private function apply_formats($subject, $userdata)
+ {
+ preg_match_all('/%\((\w+)\)s/', $subject, $substrings);
+
+ // Update userdata array
+ for ($x = 0; $x < count($substrings[0]); $x++) {
+ if (array_key_exists($substrings[1][$x], $userdata)) {
+ if (!empty($substrings[2][$x])) {
+ if (!empty($substrings[3][$x])) {
+ $subject = preg_replace(
+ '/%\(' . $substrings[1][$x]. '\)s/',
+ substr($userdata[$substrings[1][$x]], $substrings[2][$x], $substrings[3][$x]),
+ $subject
+ );
+ }
+ else {
+ $subject = preg_replace(
+ '/%\(' . $substrings[1][$x]. '\)s/',
+ substr($userdata[$substrings[1][$x]], $substrings[2][$x]),
+ $subject
+ );
+ }
+ }
+ else if (!empty($substrings[3][$x])) {
+ $subject = preg_replace(
+ '/%\(' . $substrings[1][$x]. '\)s/',
+ substr($userdata[$substrings[1][$x]], 0, $substrings[3][$x]),
+ $subject
+ );
+ }
+ else {
+ $subject = preg_replace(
+ '/%\(' . $substrings[1][$x]. '\)s/',
+ $userdata[$substrings[1][$x]],
+ $subject
+ );
+ }
+ }
+ }
+
+ preg_match_all('/.*\'(.*)\'\[(\d+):(\d+)\].*/U', $subject, $substrings);
+
+ for ($x = 0; $x < count($substrings[0]); $x++) {
+ if (!empty($substrings[2][$x])) {
+ $start = $substrings[2][$x];
+ }
+ else {
+ $start = 0;
+ }
+
+ if (!empty($substrings[3][$x])) {
+ $end = $substrings[3][$x];
+ }
+ else {
+ $end = 0;
+ }
+
+ $subject = preg_replace(
+ "/'" . $substrings[1][$x] . "'\[" . $substrings[2][$x] . ':' . $substrings[3][$x] . "\]/",
+ substr($substrings[1][$x], $start, $end),
+ $subject
+ );
+ }
+
+ return $subject;
+ }
}