function EvaluateDefault(&$ldapserver, $value, $container, $counter = '', $default = null) { if (DEBUG_ENABLED) { debug_log('%s::EvaluateDefault(): Entered with (%s,%s,%s,%s)', 5, get_class($this), $ldapserver->server_id, $value, $container, $counter); } global $ldapservers; if (preg_match('/^=php\\.(\\w+)\\((.*)\\)$/', $value, $matches)) { $args = preg_split('/,/', $matches[2]); switch ($matches[1]) { case 'GetNextNumber': if ($args[0] == '$') { $args[0] = $ldapservers->GetValue($ldapserver->server_id, 'auto_number', 'search_base'); } $container = $ldapserver->getContainerParent($container, $args[0]); $detail['value'] = get_next_number($ldapserver, $container, $args[1]); break; case 'PickList': $container = $ldapserver->getContainerParent($container, $args[0]); preg_match_all('/%(\\w+)(\\|.+)?(\\/[lU])?%/U', $args[3], $matchall); //print_r($matchall); // -1 = highlevel match, 1 = attr, 2 = subst, 3 = mod $ldap_attrs = $matchall[1]; array_push($ldap_attrs, $args[2]); $picklistvalues = return_ldap_hash($ldapserver, $container, $args[1], $args[2], $ldap_attrs); $detail['value'] = sprintf('<select name="form[%s]" id="%%s" %%s %%s>', isset($args[4]) ? $args[4] : $args[2]); $counter = 0; foreach ($picklistvalues as $key => $values) { $display = $args[3]; foreach ($matchall[1] as $arg) { $display = preg_replace('/%(' . $arg . ')(\\|.+)?(\\/[lU])?%/U', $values[$arg], $display); } if (!isset($picklist[$display])) { $detail['value'] .= sprintf('<option id="%s%s" value="%s" %s>%s</option>', isset($args[4]) ? $args[4] : $args[2], ++$counter, $values[$args[2]], $default == $display ? 'selected' : '', $display); $picklist[$display] = true; } } $detail['value'] .= '</select>'; break; case 'RandomPassword': $detail['value'] = password_generate(); printf('<script type="text/javascript" language="javascript">alert(\'%s:\\n%s\')</script>', _('A random password was generated for you'), $detail['value']); break; case 'DrawChooserLink': $detail['value'] = draw_chooser_link(sprintf('template_form.%s%s', $args[0], $counter), $args[1]); break; case 'Function': # Capture the function name and remove function name from $args $function_name = array_shift($args); $function_args = array(); foreach ($args as $arg) { if (preg_match('/^%(\\w+)(\\|.+)?(\\/[lU])?%/U', $arg, $matches)) { $varname = $matches[1]; if (isset($_POST['form'][$varname])) { $function_args[] = $_POST['form'][$varname]; } else { pla_error(sprintf(_('Your template calls php.Function for a default value, however (%s) is NOT available in the POST FORM variables. The following variables are available [%s].'), $varname, isset($_POST['form']) ? implode('|', array_keys($_POST['form'])) : 'NONE')); } } else { $function_args[] = $arg; } } # Call the PHP function if exists (PHP 4 >= 4.0.4, PHP 5) if (function_exists($function_name)) { $detail['value'] = call_user_func_array($function_name, $function_args); } break; default: $detail['value'] = 'UNKNOWN'; } $return = $detail['value']; } else { $return = $value; } if (DEBUG_ENABLED) { debug_log('%s::EvaluateDefault(): Returning (%s)', 5, get_class($this), $return); } return $return; }
protected function getDefaultAttribute($attribute, $container, $type) { if (DEBUG_ENABLED && (($fargs = func_get_args()) || ($fargs = 'NOARGS'))) { debug_log('Entered (%%)', 129, 0, __FILE__, __LINE__, __METHOD__, $fargs); } switch ($type) { case 'autovalue': $autovalue = $attribute->getAutoValue(); break; case 'helpervalue': $autovalue = $attribute->getHelperValue(); break; default: system_message(array('title' => _('Unknown Default Attribute context'), 'body' => sprintf('%s (<b>%s</b>)', _('A call was made to getDefaultAttribute() with an unkown context'), $type), 'type' => 'warn')); return; } $args = explode(';', $autovalue['args']); $server = $this->getServer(); $vals = ''; switch ($autovalue['function']) { /** * Function enables normal PHP functions to be called to evaluate a value. * eg: =php.Function(date;dmY) * * All arguments will be passed to the function, and its value returned. * If this used used in a POST context, the attribute values can be used as arguments. * * Mandatory Arguments: * * arg 0 * - php Function to call * * Additional arguments will be passed to the function. */ case 'Function': $function = array_shift($args); if (count($args) && count($args) > 1) { system_message(array('title' => _('Too many arguments'), 'body' => sprintf('%s (<b>%s</b>)', _('Function() only takes two arguments and more than two were specified'), count($args)), 'type' => 'warn')); return; } $function_args = explode(',', $args[0]); if (function_exists($function)) { $vals = call_user_func_array($function, $function_args); } else { system_message(array('title' => _('Function doesnt exist'), 'body' => sprintf('%s (<b>%s</b>)', _('An attempt was made to call a function that doesnt exist'), $function), 'type' => 'warn')); } break; /** * GetNextNumber will query the LDAP server and calculate the next number based on the query * eg: <![CDATA[=php.GetNextNumber(/;gidNumber;false;(&(objectClass=posixGroup));*2,+1000)]]> * * Mandatory Arguments: * * arg 0 * - "$" => 'auto_number','search_base' in config file * - "/",".",".." => get container parent as usual * * * arg 1 * - attribute to query for * * Optional Arguments: * * arg 2 (pool mechanism only) * - "true" increments attribute by 1 * - "false" do nothing * * * arg 3 (pool mechanism only) * - ldap filter (must match one entry only in container) * * * arg 4 * - calculus on number, eg: * - *2,+1000 => number = (2*number) + 1000 * * * arg 5 * - Min number */ /** * GetNextNumber will query the LDAP server and calculate the next number based on the query * eg: <![CDATA[=php.GetNextNumber(/;gidNumber;false;(&(objectClass=posixGroup));*2,+1000)]]> * * Mandatory Arguments: * * arg 0 * - "$" => 'auto_number','search_base' in config file * - "/",".",".." => get container parent as usual * * * arg 1 * - attribute to query for * * Optional Arguments: * * arg 2 (pool mechanism only) * - "true" increments attribute by 1 * - "false" do nothing * * * arg 3 (pool mechanism only) * - ldap filter (must match one entry only in container) * * * arg 4 * - calculus on number, eg: * - *2,+1000 => number = (2*number) + 1000 * * * arg 5 * - Min number */ case 'GetNextNumber': # If the attribute already has values, we'll return if ($type == 'autovalue' && $attribute->getValues()) { return; } if ($args[0] == '$') { $args[0] = $server->getValue($this->server_id, 'auto_number', 'search_base'); } $container = $server->getContainerPath($container, $args[0]); $vals = get_next_number($container, $args[1], !empty($args[2]) && $args[2] == 'false' ? false : true, !empty($args[3]) ? $args[3] : false, !empty($args[5]) ? $args[5] : null); # Operate calculus on next number. if (!empty($args[4])) { $mod = explode(',', $args[4]); $next_number = $vals; foreach ($mod as $calc) { $operand = $calc[0]; $operator = substr($calc, 1); switch ($operand) { case '*': $next_number = $next_number * $operator; break; case '+': $next_number = $next_number + $operator; break; case '-': $next_number = $next_number - $operator; break; case '/': $next_number = $next_number / $operator; break; } } $vals = $next_number; } break; /** * PickList will query the LDAP server and provide a select list of values * MultiList will query the LDAP server and provide a multi select list of values * eg: <![CDATA[=php.MultiList(/;(objectClass=posixAccount);uid)]]> * * eg: <![CDATA[=php.MultiList(/;(&(objectClass=posixAccount)(uid=groupA*));uid;%cn/U% (%gidNumber%);memberUid;dmdName=users;root => cn=root,nobody => cn=nobody;gidNumber;10)]]> * * Mandatory Arguments: * * arg 0 * - container, to query from current position * - "/",".",".." => get container parent as usual * * * arg 1 * - LDAP filter. May include '%attr%', it will be expanded. * * * arg2 * - list attribute key * * Optional Arguments: * * arg3 * - select display (plus modifier /C: Capitalize) * - replaced by %arg 2% if not given * * * arg 4 * - the value furnished in output - must be attribute id. replaced by arg 2 if not given * * * arg 5 * - container override * * * arg 6 * - csv list (, separator) of added values. syntax: key => display_attribute=value, key... * * * arg 7 * - csv list (, separator) of sort attributes (less to more important) * * * arg 8 (for MultiList) * - size of displayed list (default: 10 lines) */ /** * PickList will query the LDAP server and provide a select list of values * MultiList will query the LDAP server and provide a multi select list of values * eg: <![CDATA[=php.MultiList(/;(objectClass=posixAccount);uid)]]> * * eg: <![CDATA[=php.MultiList(/;(&(objectClass=posixAccount)(uid=groupA*));uid;%cn/U% (%gidNumber%);memberUid;dmdName=users;root => cn=root,nobody => cn=nobody;gidNumber;10)]]> * * Mandatory Arguments: * * arg 0 * - container, to query from current position * - "/",".",".." => get container parent as usual * * * arg 1 * - LDAP filter. May include '%attr%', it will be expanded. * * * arg2 * - list attribute key * * Optional Arguments: * * arg3 * - select display (plus modifier /C: Capitalize) * - replaced by %arg 2% if not given * * * arg 4 * - the value furnished in output - must be attribute id. replaced by arg 2 if not given * * * arg 5 * - container override * * * arg 6 * - csv list (, separator) of added values. syntax: key => display_attribute=value, key... * * * arg 7 * - csv list (, separator) of sort attributes (less to more important) * * * arg 8 (for MultiList) * - size of displayed list (default: 10 lines) */ case 'MultiList': case 'PickList': # arg5 overrides our container if (empty($args[5])) { $container = $server->getContainerPath($container, $args[0]); } else { $container = $args[5]; } # Process filter (arg 1), eventually replace %attr% by it's value set in a previous page. preg_match_all('/%(\\w+)(\\|.+)?(\\/[lUC])?%/U', $args[1], $filtermatchall); //print_r($matchall); // -1 = highlevel match, 1 = attr, 2 = subst, 3 = mod if (isset($_REQUEST['form'])) { $formvalues = array_change_key_case($_REQUEST['form']); foreach ($filtermatchall[1] as $arg) { $value = $formvalues[strtolower($arg)]; $args[1] = preg_replace("/%({$arg})(\\|.+)?(\\/[lU])?%/U", $value, $args[1]); } } if (empty($args[3])) { $args[3] = "%{$args[2]}%"; } preg_match_all('/%(\\w+)(\\|.+)?(\\/[lUC])?%/U', $args[3], $matchall); //print_r($matchall); // -1 = highlevel match, 1 = attr, 2 = subst, 3 = mod $attrs = array_unique(array_merge($matchall[1], array($args[2]))); # arg7 is sort attributes if (isset($args[7])) { $sort_attrs = explode(',', $args[7]); $attrs = array_unique(array_merge($attrs, $sort_attrs)); } $picklistvalues = return_ldap_hash($container, $args[1], $args[2], $attrs, isset($args[7]) && $args[7] ? $sort_attrs : false); # arg6 is a set of fixed values to add to search result if (isset($args[6])) { $fixedvalues = explode(',', $args[6]); foreach ($fixedvalues as $fixedvalue) { if (empty($fixedvalue)) { continue; } $fixedvalue = preg_split('/=\\>/', $fixedvalue); $displayvalue = explode('=', $fixedvalue[1]); $newvalue[trim($fixedvalue[0])] = array($args[2] => trim($fixedvalue[0]), trim($displayvalue[0]) => trim($displayvalue[1])); $picklistvalues = array_merge($picklistvalues, $newvalue); } } $vals = array(); foreach ($picklistvalues as $key => $values) { $display = $args[3]; foreach ($matchall[1] as $key => $arg) { if (isset($values[$arg])) { $disp_val = $values[$arg]; } else { $disp_val = ''; } if (is_array($disp_val)) { $disp_val = $disp_val[0]; } if ($matchall[3][$key]) { switch ($matchall[3][$key]) { case '/l': # lowercase if (function_exists('mb_convert_case')) { $disp_val = mb_convert_case($disp_val, MB_CASE_LOWER, 'utf-8'); } else { $disp_val = strtolower($disp_val); } break; case '/U': # uppercase if (function_exists('mb_convert_case')) { $disp_val = mb_convert_case($disp_val, MB_CASE_UPPER, 'utf-8'); } else { $disp_val = strtoupper($disp_val); } break; case '/C': # capitalize if (function_exists('mb_convert_case')) { $disp_val = mb_convert_case($disp_val, MB_CASE_TITLE, 'utf-8'); } else { $disp_val = ucfirst($disp_val); } break; default: break; } } # make value a substring of preg_match_all('/^\\|([0-9]*)-([0-9]*)$/', trim($matchall[2][$key]), $substrarray); if (isset($substrarray[1][0]) && $substrarray[1][0] || isset($substrarray[2][0]) && $substrarray[2][0]) { $begin = $substrarray[1][0] ? $substrarray[1][0] : '0'; $end = $substrarray[2][0] ? $substrarray[2][0] : strlen($disp_val); if (function_exists('mb_substr')) { $disp_val = mb_substr($disp_val, $begin, $end, 'utf-8'); } else { $disp_val = substr($disp_val, $begin, $end); } } $display = preg_replace("/%({$arg})(\\|.+)?(\\/[lUC])?%/U", $disp_val, $display); } if (!isset($picklist[$values[$args[2]]])) { $vals[$values[$args[2]]] = $display; $picklist[$values[$args[2]]] = true; } } break; /** * PasswordEncryptionTypes will return a list of our support password encryption types * eg: =php.PasswordEncryptionTypes() * * This function doesnt use any arguments */ /** * PasswordEncryptionTypes will return a list of our support password encryption types * eg: =php.PasswordEncryptionTypes() * * This function doesnt use any arguments */ case 'PasswordEncryptionTypes': $vals = password_types(); break; /** * RandomPassword will create a random password for the value. * eg: =php.RandomPassword() * * When calling the attribute Javascript it will generate a random password. * * This function doesnt use any arguments */ /** * RandomPassword will create a random password for the value. * eg: =php.RandomPassword() * * When calling the attribute Javascript it will generate a random password. * * This function doesnt use any arguments */ case 'RandomPassword': break; } switch ($type) { case 'autovalue': if (!is_array($vals)) { $attribute->autoValue(array($vals)); } else { $attribute->autoValue($vals); } break; case 'helpervalue': return $vals; } }