Re: generate_uid
"Jeroen van Meeuwen (Kolab Systems)" <[email protected]>
| Newsgroups | gmane.comp.kde.devel.kolab,gmane.comp.kde.kolab.devel |
|---|---|
| Message-ID | <[email protected]> |
On 2013-02-05 13:56, furlot wrote: > Hello, > > I would like to personalize the uid auto-generation, is there a > reason that generate_uid don't use an uid policy like primary_mail or > secondary_mail? > Yes, but none of them truly nor soundly technical in nature. Please find attached a nice little script I've authored as an exercise to see what I could do if I set my mind to it. Place this in the top-level directory of the kolab-webadmin installation (usually /usr/share/kolab-webadmin/). Execute it with (use a user that has read access to /etc/kolab/kolab.conf): $ php test-generate_uid.php Kind regards, Jeroen van Meeuwen -- Systems Architect, Kolab Systems AG e: vanmeeuwen at kolabsys.com m: +44 74 2516 3817 w: http://www.kolabsys.com pgp: 9342 BF08 _______________________________________________ Kolab-devel mailing list [email protected] https://www.intevation.de/mailman/listinfo/kolab-devel
test-generate_uid.php
(text/x-php, 11.3 KB)
<?php
require_once("lib/functions.php");
$attributes = Array(
"auto_form_fields" => Array(
"cn" => Array(
"data" => Array(
"givenname",
"sn",
),
),
"displayname" => Array(
"data" => Array(
"givenname",
"sn",
),
),
"mail" => Array(
"data" => Array(
"givenname",
"preferredlanguage",
"sn",
),
),
"mailalternateaddress" => Array(
"data" => Array(
"givenname",
"preferredlanguage",
"sn",
),
"optional" => true,
),
"mailhost" => Array(
"optional" => true,
),
"uid" => Array(
"data" => Array(
"givenname",
"preferredlanguage",
"sn",
),
),
"userpassword" => Array(
"optional" => true,
),
),
"form_fields" => Array(
/*
"c" => Array(
"type" => "select",
"optional" => true,
),
*/
"givenname" => Array(),
"initials" => Array(
"optional" => true,
),
"kolabdelegate" => Array(
"type" => "list",
"autocomplete" => true,
"optional" => true,
),
"kolabinvitationpolicy" => Array(
"type" => "select",
"values" => Array(
"",
"ACT_MANUAL",
"ACT_REJECT",
),
"optional" => true,
),
"kolaballowsmtprecipient" => Array(
"type" => "list",
"optional" => true,
),
"kolaballowsmtpsender" => Array(
"type" => "list",
"optional" => true,
),
"l" => Array(
"optional" => true,
),
"mailalternateaddress" => Array(
"type" => "list",
"optional" => true,
),
"mailquota" => Array(
"optional" => true,
),
"mobile" => Array(
"optional" => true,
),
"nsroledn" => Array(
"type" => "list",
"autocomplete" => true,
"optional" => true
),
"o" => Array(
"optional" => true,
),
"ou" => Array(
"type" => "select",
),
"pager" => Array(
"optional" => true,
),
"postalcode" => Array(
"optional" => true,
),
"preferredlanguage" => Array(
"type" => "select",
),
"sn" => Array(),
"street" => Array(
"optional" => true,
),
"telephonenumber" => Array(
"optional" => true,
),
"title" => Array(
"optional" => true,
),
"userpassword" => Array(
"optional" => true,
),
),
"fields" => Array(
"objectclass" => Array(
"top",
"inetorgperson",
"kolabinetorgperson",
"mailrecipient",
"organizationalperson",
"person",
),
),
);
$uid_policies = array(
"%(surname)s.lower()",
'"{0}.{1}": \'%(givenname)s\'[0:1].capitalize(), \'%(surname)s\'',
'"{0}.{1}": \'%(givenname)s\'.lower(), \'%(surname)s\'[0:1].lower()',
);
$tests = array();
$tests[] = array(
'preferredlanguage' => 'en_US',
'givenname' => 'Jeroen',
'sn' => 'van Meeuwen',
);
$tests[] = array(
'preferredlanguage' => 'de_CH',
'givenname' => 'Thomas',
'sn' => 'Brüderli',
);
$tests[] = array(
'preferredlanguage' => 'en_US',
'givenname' => 'Thomas',
'sn' => 'Brüderli',
);
foreach ($uid_policies as $uid_policy) {
foreach ($tests as $test) {
print generate_uid($uid_policy, $test, $attributes) . "\n";
}
}
function generate_uid($policy_uid, $userdata, $attribs = array())
{
$functions = array(
'\'*(\w+)\'*\.capitalize\(\)' => 'strtoupper(substr("${1}", 0, 1)) . strtolower(substr("${1}", 1))',
'\'*(\w+)\'*\.lower\(\)' => 'strtolower("${1}")',
'\'*(\w+)\'*\.upper\(\)' => 'strtoupper("${1}")',
);
if (isset($attribs['auto_form_fields']) && isset($attribs['auto_form_fields']['uid'])) {
// Use Data Please
foreach ($attribs['auto_form_fields']['uid']['data'] as $key) {
if (!isset($userdata[$key])) {
throw new Exception("Key not set: " . $key, 12356);
}
}
// TODO: Use preferredlanguage
if (isset($userdata['preferredlanguage'])) {
//console("Using locale for " . $postdata['preferredlanguage']);
setlocale(LC_ALL, $userdata['preferredlanguage']);
}
// Transliterate all attributes in $postdata
$userdata = kolab_recipient_policy::normalize_userdata($userdata);
// var_dump($policy_uid);
$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
);
}
} else {
//console("Key " . $substrings[1][$x] . " does not exist in \$userdata");
}
}
preg_match_all('/.*\'(.*)\'\[(\d+):(\d+)\].*/', $policy_uid, $substrings);
// var_dump($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);
// var_dump($policy_uid);
}
foreach ($functions as $match => $replace) {
// print "Running with match: $match and replace: $replace\n";
if (preg_match('/' . $match . '/', $policy_uid, $strings)) {
$policy_uid = preg_replace('/' . $match . '/', $replace, $policy_uid);
// var_dump($policy_uid);
}
}
$formatted_policy_uid = explode(":", $policy_uid);
if (is_array($formatted_policy_uid) && count($formatted_policy_uid) == 2) {
// print "\$policy_uid = sprintf(" . $formatted_policy_uid[0] . ", " . $formatted_policy_uid[1] . ");\n";
eval("\$policy_uid = sprintf(" . $formatted_policy_uid[0] . ", " . $formatted_policy_uid[1] . ");");
} else {
eval("\$policy_uid = " . $policy_uid . ";");
}
// var_dump($policy_uid);
return $policy_uid;
} else {
print "Error\n";
}
}
?>