PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/accounts/ldapadmin/htdocs/template_engine.php

https://github.com/azeckoski/az-php-sandbox
PHP | 1161 lines | 782 code | 261 blank | 118 comment | 219 complexity | d958248e7aac20109f0781ae9f8f3951 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. // $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/template_engine.php,v 1.26.2.34 2006/03/13 23:13:43 wurley Exp $
  3. /**
  4. * Template render engine.
  5. * @param dn $dn DN of the object being edited. (For editing existing entries)
  6. * @param dn $container DN where the new object will be created. (For creating new entries)
  7. * @param string $template to use for new entry. (For creating new entries)
  8. * @todo schema attr keys should be in lowercase.
  9. * @package phpLDAPadmin
  10. * @author The phpLDAPadmin development team
  11. */
  12. /**
  13. */
  14. require './common.php';
  15. if (! $ldapserver->haveAuthInfo())
  16. pla_error(_('Not enough information to login to server. Please check your configuration.'));
  17. $friendly_attrs = process_friendly_attr_table(); // @todo might not need this.
  18. $pjs = array();
  19. # REMOVE THSE @todo
  20. $today = date('U');
  21. $shadow_before_today_attrs = arrayLower(array('shadowLastChange','shadowMin'));
  22. $shadow_after_today_attrs = arrayLower(array('shadowMax','shadowExpire','shadowWarning','shadowInactive'));
  23. $shadow_format_attrs = array_merge($shadow_before_today_attrs,$shadow_after_today_attrs);
  24. # END REMOVE
  25. # If we have a DN, then this is to edit the entry.
  26. if (isset($_REQUEST['dn'])) {
  27. $dn = $_GET['dn'];
  28. $decoded_dn = rawurldecode($dn);
  29. $encoded_dn = rawurlencode($decoded_dn);
  30. if (! $ldapserver->haveAuthInfo())
  31. pla_error(_('Not enough information to login to server. Please check your configuration.'));
  32. $ldapserver->dnExists($dn)
  33. or pla_error(sprintf(_('No such entry: %s'),pretty_print_dn($dn)));
  34. $rdn = get_rdn($dn);
  35. $attrs = $ldapserver->getDNAttrs($dn,false,$config->GetValue('deref','view'));
  36. $modified_attrs = isset($_REQUEST['modified_attrs']) ? $_REQUEST['modified_attrs'] : false;
  37. $show_internal_attrs = isset($_REQUEST['show_internal_attrs']) ? true : false;
  38. # If an entry has more children than this, stop searching and display this amount with a '+'
  39. $max_children = 100;
  40. } else {
  41. $dn = '';
  42. $rdn = '';
  43. $encoded_dn = '';
  44. if ($_REQUEST['template'] == 'custom') {
  45. include TMPLDIR.'template_header.php';
  46. require TMPLDIR.'creation/custom.php';
  47. die();
  48. } else {
  49. $templates = new Templates($ldapserver->server_id);
  50. $template = $templates->GetTemplate($_REQUEST['template']);
  51. }
  52. }
  53. include TMPLDIR.'template_header.php';
  54. /*
  55. * When we get here, (either a new entry, or modifying an existing entry), if the
  56. * empty_attrs array has content, then we need to ask the user for this information.
  57. */
  58. if (isset($template['empty_attrs'])) {
  59. masort($template['empty_attrs'],'page,order',1);
  60. # What page are we working on.
  61. $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
  62. printf('<center><h2>%s</h2></center>',$template['description']);
  63. echo "\n\n";
  64. if (isset($_REQUEST['nextpage']) && ! $_REQUEST['nextpage']) {
  65. $new_dn = sprintf('%s=%s,%s',$template['rdn'],$_REQUEST['form'][$template['rdn']],$_REQUEST['container']);
  66. echo '<form action="create.php" method="post">';
  67. printf('<input type="hidden" name="new_dn" value="%s" />',$new_dn);
  68. } else {
  69. echo '<form action="template_engine.php" method="post" id="template_form" name="template_form" enctype="multipart/form-data">';
  70. }
  71. if (isset($_REQUEST['form'])) {
  72. foreach ($_REQUEST['form'] as $attr => $value) {
  73. # Check for any with post actions.
  74. if (isset($template['attribute'][$attr]['post']) && $_REQUEST['page'] == $template['attribute'][$attr]['page']+1) {
  75. if (preg_match('/^=php\.(\w+)\((.*)\)$/',$template['attribute'][$attr]['post'],$matches)) {
  76. switch ($matches[1]) {
  77. case 'Password' :
  78. preg_match_all('/%(\w+)(\|.+)?(\/[lU])?%/U',$matches[2],$matchall);
  79. $enc = $_REQUEST[$matchall[1][0]];
  80. $password = $_REQUEST['form'][$matchall[1][1]];
  81. if (trim($password)) {
  82. $value = password_hash($password,$enc);
  83. $_REQUEST['form'][$attr] = $value;
  84. }
  85. break;
  86. case 'SambaPassword' :
  87. $matchall = explode(',',$matches[2]);
  88. $attr = preg_replace('/%/','',$matchall[1]);
  89. # If we have no password, then dont hash nothing!
  90. if (! trim($_REQUEST['form'][$attr]))
  91. break;
  92. $sambapassword = new smbHash;
  93. switch ($matchall[0]) {
  94. case 'LM' : $value = $sambapassword->lmhash($_REQUEST['form'][$attr]);
  95. break;
  96. case 'NT' : $value = $sambapassword->nthash($_REQUEST['form'][$attr]);
  97. break;
  98. default :
  99. $value = null;
  100. }
  101. $_REQUEST['form'][$attr] = $value;
  102. break;
  103. case 'Join' :
  104. preg_match_all('/%(\w+)(\|.+)?(\/[lU])?%/U',$matches[2],$matchall);
  105. $matchattrs = explode(',',$matches[2]);
  106. $char = $matchattrs[0];
  107. $values = array();
  108. foreach ($matchall[1] as $joinattr) {
  109. if (isset($_REQUEST['form'][$joinattr]))
  110. $values[] = $_REQUEST['form'][$joinattr];
  111. else if (isset($_REQUEST[$joinattr]))
  112. $values[] = $_REQUEST[$joinattr];
  113. else
  114. pla_error(sprintf(_('Your template is missing variable (%s)'),$joinattr));
  115. }
  116. $value = implode($char,$values);
  117. $_REQUEST['form'][$attr] = $value;
  118. break;
  119. default:
  120. pla_error(sprintf(_('Your template has an unknown post function (%s).'),$matches[1]));
  121. }
  122. }
  123. }
  124. if (is_array($value))
  125. foreach ($value as $item)
  126. printf('<input type="hidden" name="form[%s][]" value="%s" />',$attr,$item);
  127. else
  128. printf('<input type="hidden" name="form[%s]" value="%s" />',$attr,$value);
  129. }
  130. # Have we got a Binary Attribute?
  131. if (isset($_FILES['form']['name']) && is_array($_FILES['form']['name'])) {
  132. foreach ($_FILES['form']['name'] as $attr => $details) {
  133. if (is_uploaded_file($_FILES['form']['tmp_name'][$attr])) {
  134. $file = $_FILES['form']['tmp_name'][$attr];
  135. $f = fopen($file,'r');
  136. $binary_data = fread($f,filesize($file));
  137. fclose($f);
  138. // @todo: This may need to be implemented.
  139. //if (is_binary_option_required($ldapserver,$attr))
  140. // $attr .= ';binary';
  141. $_SESSION['submitform'][$attr] = $binary_data;
  142. printf('<input type="hidden" name="form[%s]" value="" />',$attr);
  143. }
  144. }
  145. }
  146. }
  147. printf('<input type="hidden" name="server_id" value="%s" />',$ldapserver->server_id);
  148. printf('<input type="hidden" name="template" value="%s" />',$_REQUEST['template']);
  149. printf('<input type="hidden" name="object_classes" value="%s" />',rawurlencode(serialize(array_values($template['objectclass']))));
  150. printf('<input type="hidden" name="page" value="%s" />',$page+1);
  151. echo "\n\n";
  152. echo '<center>';
  153. echo '<table class="confirm" border="0">';
  154. echo '<tr class="spacer"><td colspan="3">&nbsp;</td></tr>';
  155. echo "\n\n";
  156. echo '<tr>';
  157. if (isset($template['askcontainer']) && $template['askcontainer'] && $page == 1) {
  158. if (! (isset($template['regexp']) && isset($template['regexp']))) {
  159. echo '<td>&nbsp;</td>';
  160. echo '<td class="heading">Container <acronym title="Distinguished Name">DN</acronym>:</td>';
  161. printf('<td><input type="text" name="container" size="40" value="%s" />&nbsp;',
  162. htmlspecialchars($_REQUEST['container']));
  163. draw_chooser_link('template_form.container');
  164. echo '</td></tr>';
  165. echo '<tr class="spacer"><td colspan="3"></td></tr>';
  166. } else {
  167. echo '<td>&nbsp;</td>';
  168. echo '<td class="heading">Container <acronym title="Distinguished Name">DN</acronym>:</td>';
  169. printf('<td><input type="text" name="container" size="40" value="%s" disabled />',
  170. htmlspecialchars($_REQUEST['container']));
  171. printf('<input type="hidden" name="container" value="%s" /></td></tr>',$_REQUEST['container']);
  172. echo '<tr class="spacer"><td colspan="3"></td></tr>';
  173. }
  174. } else {
  175. printf('<td><input type="hidden" name="container" value="%s" /></td></tr>',$_REQUEST['container']);
  176. }
  177. $count = 0;
  178. $nextpage = 0;
  179. $mustitems = 0;
  180. foreach ($template['empty_attrs'] as $attr => $detail) {
  181. $mustitem = false;
  182. $verifyitem = false;
  183. $type = isset($detail['type']) ? $detail['type'] : 'text';
  184. if (! isset($detail['page']))
  185. $detail['page'] = 1;
  186. $size = isset($detail['size']) ? $detail['size'] : 20;
  187. $maxlength = isset($detail['maxlength']) ? $detail['maxlength'] : null;
  188. $rows = isset($detail['rows']) ? $detail['rows'] : null;
  189. $cols = isset($detail['cols']) ? $detail['cols'] : null;
  190. # Check that the page number is correct.
  191. if ($detail['page'] < $page && ! isset($attr[$attr])) {
  192. # ERROR: This attribute should be set by now.
  193. print "We should have set [$attr] by now.<BR>";
  194. } elseif ($detail['page'] == $page) {
  195. $count++;
  196. echo '<tr>';
  197. # Some conditional checking.
  198. # $detail['must'] & $detail['disable'] cannot be set at the same time.
  199. if (isset($detail['must']) && $detail['must'] && isset($detail['disable']) && $detail['disable'])
  200. pla_error(sprintf(_('Attribute [%s] is a MUST attribute, so it cannot be disabled.'),$attr));
  201. # If this attribute is disabled, go to the next one.
  202. if (isset($detail['disable']) && $detail['disable'])
  203. continue;
  204. # Evaluate our Default Value, if its a function call result.
  205. if (isset($detail['value'])) {
  206. if (is_array($detail['value'])) {
  207. # If value is an array, then it must a select list.
  208. $type = 'select';
  209. $defaultresult = sprintf('<select name="form[%s]" id="%%s" %%s %%s>',$attr);
  210. foreach ($detail['value'] as $key => $value) {
  211. if (preg_match('/^_KEY:/',$key))
  212. $key = preg_replace('/^_KEY:/','',$key);
  213. else
  214. $key = $value;
  215. $defaultresult .= sprintf('<option name="%s" value="%s" %s>%s</option>',$value,$key,
  216. ((isset($detail['default']) && $detail['default'] == $key) ? 'selected' : ''),$value);
  217. }
  218. $defaultresult .= '</select>';
  219. $detail['value'] = $defaultresult;
  220. } else {
  221. $detail['value'] = $templates->EvaluateDefault($ldapserver,$detail['value'],$_REQUEST['container'],null,
  222. (isset($detail['default']) ? $detail['default'] : null));
  223. }
  224. #if the default has a select list, then change the type to select
  225. if (preg_match('/<select .*>/i',$detail['value']))
  226. $type = 'select';
  227. }
  228. # @todo: if value is a select list, then it cannot be hidden.
  229. # If this is a hidden attribute, then set its value.
  230. if (isset($detail['hidden']) && $detail['hidden']) {
  231. if (isset($detail['value'])) {
  232. printf('<input type="%s" name="form[%s]" id="%s" value="%s"/>','hidden',$attr,$attr,$detail['value']);
  233. continue;
  234. } else {
  235. pla_error(sprintf(_('Attribute [%s] is a HIDDEN attribute, however, it is missing a VALUE in your template.'),$attr));
  236. }
  237. }
  238. # This is a displayed attribute.
  239. # Flag it as a must attribute so that we do get a value.
  240. if (isset($detail['must']) && $detail['must'] &&
  241. ! isset($detail['presubmit']) &&
  242. $type != 'select') {
  243. $mustitems++;
  244. $mustitem = true;
  245. }
  246. # Display the icon if one is required.
  247. if (isset($detail['icon']) && trim($detail['icon']))
  248. printf('<td><img src="%s" /></td>',$detail['icon']);
  249. else
  250. printf('<td>&nbsp;</td>');
  251. echo '<td class="heading">';
  252. # Display the label.
  253. if (isset($detail['description']) && (trim($detail['description'])))
  254. printf('<acronym title="%s">%s</acronym>:',$detail['description'],$detail['display']);
  255. elseif (isset($detail['display']))
  256. printf('%s:',$detail['display']);
  257. else
  258. printf('%s:',_('No DISPLAY/DESCRIPTION attribute in template file'));
  259. echo '</td>';
  260. # Calculate the events.
  261. # @todo: Need to change js so that if a must attr is auto populated, it decrements the total and enables the submit.
  262. if (isset($detail['onchange'])) {
  263. if (is_array($detail['onchange'])) {
  264. foreach ($detail['onchange'] as $value)
  265. $templates->OnChangeAdd($ldapserver,$attr,$value);
  266. } else {
  267. $templates->OnChangeAdd($ldapserver,$attr,$detail['onchange']);
  268. }
  269. }
  270. # Display the input box.
  271. echo '<td>';
  272. # Is this a binary attribute
  273. if ($ldapserver->isAttrBinary($attr)) {
  274. printf('<input type="file" name="form[%s]" size="20" />',$attr);
  275. if (! ini_get('file_uploads'))
  276. printf('<br /><small><b>%s</b></small><br />',
  277. _('Your PHP configuration has disabled file uploads. Please check php.ini before proceeding.'));
  278. else
  279. printf('<br /><small><b>%s: %s</b></small><br />',
  280. _('Maximum file size'),ini_get('upload_max_filesize'));
  281. } elseif (in_array($type,array('text','password'))) {
  282. printf('<input type="%s" size="%s" name="form[%s]%s" id="%s" value="%s" %s%s%s />',
  283. $type,$size,$attr,(isset($detail['array']) && ($detail['array'] > 1) ? '[]' : ''),$attr,
  284. (isset($detail['value']) ? $detail['value'] : ''),
  285. "onBlur=\"fill('$attr', this.value);\"",
  286. (isset($detail['disable']) ? 'disabled' : ''),
  287. ($maxlength ? sprintf(' maxlength="%s" ',$maxlength) : ''));
  288. } elseif ($type == 'textarea') {
  289. printf('<textarea size="%s" name="form[%s]%s" id="%s" value="%s" cols="%s" rows="%s" %s%s ></textarea>',
  290. $size,$attr,(isset($detail['array']) && ($detail['array'] > 1) ? '[]' : ''),$attr,
  291. (isset($detail['value']) ? $detail['value'] : ''),
  292. ($cols ? $cols : 35),
  293. ($rows ? $rows : 4),
  294. "onBlur=\"fill('$attr', this.value);\"",
  295. (isset($detail['disable']) ? 'disabled' : ''));
  296. } elseif ($type == 'select') {
  297. printf($detail['value'],$attr,
  298. "onBlur=\"fill('$attr', this.value);\"",
  299. (isset($detail['disable']) ? 'disabled' : ' '));
  300. }
  301. # Disabled items dont get submitted.
  302. # @todo need to add some js to enable them on submit, or add them as hidden items.
  303. if ($mustitem)
  304. echo '&nbsp;*';
  305. # Do we have a helper, and is it configured for the side.
  306. if (isset($detail['helper']) && isset($detail['helper']['location'])
  307. && $detail['helper']['location'] == 'side' && isset($detail['helper']['value'])) {
  308. printf('&nbsp;%s',$templates->HelperValue($detail['helper']['value'],
  309. (isset($detail['helper']['id']) ? $detail['helper']['id'] : ''),$_REQUEST['container'],$ldapserver,null,
  310. isset($detail['helper']['default']) ? $detail['helper']['default'] : ''));
  311. }
  312. if (isset($detail['hint']) && (trim($detail['hint'])))
  313. printf('&nbsp;<span class="hint">(hint: %s)</span></td>',$detail['hint']);
  314. else
  315. echo '</td>';
  316. echo '</tr>'."\n";
  317. # Do we have a verify attribute?
  318. if (isset($detail['verify']) && ($detail['verify'])) {
  319. $verifyitems = true;
  320. echo '<tr><td>&nbsp;</td><td class="heading">';
  321. # Display the label.
  322. if (isset($detail['description']) && (trim($detail['description'])))
  323. printf('<acronym title="%s">%s %s</acronym>:',_('Verify'),$detail['description'],$detail['display']);
  324. else
  325. printf('%s %s:',_('Verify'),$detail['display']);
  326. echo '</td><td>';
  327. if (in_array($type,array('text','password'))) {
  328. printf('<input type="%s" name="%s" id="%s" value="%s" %s/>',
  329. $type,$attr."V",$attr."V",(isset($detail['value']) ? $detail['value'] : ''),
  330. sprintf('onBlur="check(form.%s,form.%sV)"',$attr,$attr));
  331. }
  332. echo '</td></tr>'."\n";
  333. }
  334. # Is this a multiarray input?
  335. if (isset($detail['array']) && ($detail['array'])) {
  336. for ($i=2; $i <= $detail['array']; $i++) {
  337. echo '<tr><td>&nbsp;</td><td>&nbsp;</td>';
  338. printf('<td><input type="%s" name="form[%s][]" id="%s" value="%s" %s %s />',
  339. $type,$attr,$attr.$i,(isset($detail['value']) ? $detail['value'] : ''),
  340. "onBlur=\"fill('$attr', this.value);\"",
  341. isset($detail['disable']) ? 'disabled' : '');
  342. if (isset($detail['helper']) && isset($detail['helper']['location'])
  343. && $detail['helper']['location'] == 'side' && isset($detail['helper']['value'])) {
  344. printf('&nbsp;%s',$templates->HelperValue($detail['helper']['value'],
  345. (isset($detail['helper']['id']) ? $detail['helper']['id'] : ''),$_REQUEST['container'],$ldapserver,$i));
  346. }
  347. echo '</td></tr>'."\n";
  348. }
  349. }
  350. # Do we have a helper.
  351. # Side helpers are handled above.
  352. # @todo: Helpers must have an onchange or onsubmit.
  353. # @todo: Helpers must have an id field.
  354. # @todo: Helpers must have an post field.
  355. if (isset($detail['helper']) && (! isset($detail['helper']['location']) || $detail['helper']['location'] != 'side')) {
  356. echo '<tr><td>&nbsp;</td>';
  357. echo '<td class="heading">';
  358. # Display the label.
  359. if (isset($detail['helper']['description']) && (trim($detail['helper']['description'])))
  360. printf('<acronym title="%s">%s</acronym>:',$detail['helper']['description'],$detail['helper']['display']);
  361. else
  362. printf('%s:',$detail['helper']['display']);
  363. echo '</td>';
  364. printf('<td>%s</td>',$templates->HelperValue($detail['helper']['value'],$detail['helper']['id']));
  365. }
  366. if (isset($detail['spacer']) && $detail['spacer'])
  367. echo '<tr class="spacer"><td colspan="3"></td></tr>';
  368. # See if there are any future ones - if there are and we dont ask any this round, then thats an error.
  369. } elseif ($detail['page'] > $page) {
  370. $nextpage++;
  371. }
  372. }
  373. # @todo: Proper error message required.
  374. if ($nextpage && ! $count)
  375. pla_error(sprintf(_('We are missing a page for [%s] attributes.'),$nextpage));
  376. # If there is no count, display the summary
  377. if (! $count) {
  378. printf('<tr><td><img src="%s" /></td><td><span class="x-small">%s :</span></td><td><b>%s</b></td></tr>',
  379. $template['icon'],_('Create Object'),htmlspecialchars($new_dn));
  380. echo '<tr class="spacer"><td colspan="3"></td></tr>';
  381. $counter = 0;
  382. foreach ($_REQUEST['form'] as $attr => $value) {
  383. # Remove blank attributes.
  384. if (! is_array($_REQUEST['form'][$attr]) && trim($_REQUEST['form'][$attr]) == '') {
  385. unset($_REQUEST['form'][$attr]);
  386. continue;
  387. }
  388. $attrs[] = $attr;
  389. printf('<tr class="%s"><td colspan=2>',($counter++%2==0?'even':'odd'));
  390. printf('<input type="hidden" name="attrs[]" value="%s" />',$attr);
  391. if (is_array($value))
  392. foreach ($value as $item) {
  393. if ($item && ! isset($unique[$item])) {
  394. $unique[$item] = 1;
  395. printf('<input type="hidden" name="vals[%s][]" value="%s" />',
  396. array_search($attr,$attrs),$item);
  397. printf('%s</td><td><b>%s</b></td></tr>',$attr,htmlspecialchars($item));
  398. }
  399. }
  400. else {
  401. $display = $value;
  402. if (isset($template['attribute'][$attr]['type']) && $template['attribute'][$attr]['type'] == 'password')
  403. if (obfuscate_password_display($_REQUEST['enc']))
  404. $display = '********';
  405. printf('<input type="hidden" name="vals[]" value="%s" />',$value);
  406. printf('%s</td><td><b>%s</b></td></tr>',$attr,htmlspecialchars($display));
  407. }
  408. }
  409. if (isset($_SESSION['submitform'])) {
  410. echo '<tr class="spacer"><td colspan="3"></td></tr>';
  411. foreach (array_keys($_SESSION['submitform']) as $attr) {
  412. printf('<tr class="%s"><td colspan=2>%s</td><td><b>%s</b>',
  413. ($counter++%2==0?'even':'odd'),$attr,_('Binary value not displayed'));
  414. printf('<input type="hidden" name="attrs[]" value="%s" /></td></tr>',$attr);
  415. }
  416. }
  417. }
  418. echo '<tr class="spacer"><td colspan="3"></td></tr>';
  419. if (! $nextpage && isset($_REQUEST['nextpage']) && ! $_REQUEST['nextpage']) {
  420. # Look for any presubmit functions.
  421. foreach ($template['empty_attrs'] as $attr => $detail) {
  422. if (isset($template['attribute'][$attr]['presubmit']) && ! isset($_REQUEST['form'][$attr])) {
  423. printf('<tr class="%s"><td colspan=2>%s</td><td><b>%s</b></td></tr>',
  424. ($counter++%2==0?'even':'odd'),$attr,htmlspecialchars(_('(Auto evaluated on submission.)')));
  425. printf('<input type="hidden" name="presubmit[]" value="%s" />',$attr);
  426. }
  427. }
  428. printf('<tr><td colspan="3"><center><br /><input type="submit" name="submit" value="%s" %s /></center></td></tr>',
  429. _('Create Object'),$mustitems ? 'disabled' : '');
  430. } elseif ($nextpage) {
  431. printf('<tr><td colspan="3"><center><br /><input type="submit" name="submit" value="%s" %s /></center></td></tr>',
  432. _('Next Page'),$mustitems ? 'disabled' : '');
  433. } else {
  434. printf('<tr><td colspan="3"><center><br /><input type="submit" name="submit" value="%s" %s /></center></td></tr>',
  435. _('Proceed >>'),$mustitems ? 'disabled' : '');
  436. }
  437. echo '</table>';
  438. echo '</center>';
  439. if ($mustitems)
  440. printf('<input type="hidden" name="mustitems" value="%s" />',$mustitems);
  441. printf('<input type="hidden" name="nextpage" value="%s" />',$nextpage);
  442. echo '</form>'."\n\n";
  443. printf('<span class="hint">'._('Page %d').'</span>',$page);
  444. echo "\n\n";
  445. if ($mustitems) {
  446. $jstext = '
  447. <script type="text/javascript" language="javascript">
  448. var reduced = new Array();
  449. var form = document.getElementById("template_form");
  450. function reduceMust(attrname){
  451. attr = document.getElementById(attrname);
  452. if (attr.value.length > 0) {
  453. if (! reduced[attrname]) {
  454. reduced[attrname] = 1;
  455. form.mustitems.value--;
  456. }
  457. if (form.mustitems.value < 0) {
  458. form.mustitems.value = 0;
  459. }
  460. if (form.mustitems.value == 0) {
  461. form.submit.disabled = false;
  462. }
  463. } else {
  464. if (reduced[attrname]) {
  465. reduced[attrname] = 0;
  466. form.mustitems.value++;
  467. }
  468. if (form.mustitems.value > 0) {
  469. form.submit.disabled = true;
  470. }
  471. }
  472. }
  473. var attrTrace;
  474. function fill(id, value) {
  475. attrTrace = new Array();
  476. fillRec(id, value);
  477. }
  478. function fillRec(id, value) {
  479. if (attrTrace[id] == 1)
  480. return;
  481. else {
  482. attrTrace[id] = 1;
  483. document.getElementById(id).value = value;
  484. // here comes template-specific implementation, generated by php
  485. if (false) {}';
  486. foreach ($template['empty_attrs'] as $attr => $detail) {
  487. $jstext .= "\t\t\telse if (id == '$attr') {\n";
  488. if (isset($detail['must']))
  489. $jstext .= "\t\t\t\treduceMust('$attr');\n";
  490. $hash = $templates->getJsHash();
  491. if (isset($hash['autoFill'.$attr])) {
  492. $jstext .= $hash['autoFill'.$attr];
  493. }
  494. $jstext .= "\t\t\t}\n";
  495. }
  496. $jstext .= '}}</script>';
  497. $pjs[] = $jstext;
  498. }
  499. if (isset($verifyitems) && $verifyitems) {
  500. //@todo: Return focus to the first item.
  501. $pjs[] = '
  502. <script type="text/javascript" language="javascript">
  503. function check(a,b){
  504. if (a.value != b.value){
  505. alert(\'Values dont compare\')
  506. }
  507. }
  508. </script>';
  509. }
  510. # User needs to submit form to continue.
  511. foreach ($pjs as $script)
  512. echo $script;
  513. die();
  514. }
  515. if (! isset($template))
  516. $template['attrs'] = $attrs;
  517. # If we get here - we are displaying/editing the entry.
  518. # Sort these entries.
  519. uksort($template['attrs'],'sortAttrs');
  520. $js_date_attrs = $config->GetValue('appearance','date_attrs');
  521. $js[] = sprintf('<script type="text/javascript" language="javascript">var defaults = new Array();var default_date_format = "%s";</script>',$config->GetValue('appearance','date'));
  522. foreach ($template['attrs'] as $attr => $vals) {
  523. if (! is_array($vals))
  524. $vals = array($vals);
  525. flush();
  526. $schema_attr = $ldapserver->getSchemaAttribute($attr,$dn);
  527. if ($schema_attr)
  528. $attr_syntax = $schema_attr->getSyntaxOID();
  529. else
  530. $attr_syntax = null;
  531. if (! strcasecmp($attr,'dn'))
  532. continue;
  533. # has the config.php specified that this attribute is to be hidden or shown?
  534. if ($ldapserver->isAttrHidden($attr))
  535. continue;
  536. # Setup the $attr_note, which will be displayed to the right of the attr name (if any)
  537. $attr_note = '';
  538. # is there a user-friendly translation available for this attribute?
  539. if (isset($friendly_attrs[ strtolower($attr) ])) {
  540. $attr_display = $friendly_attrs[ strtolower($attr) ];
  541. $attr_note = "<acronym title=\"" . sprintf(_('Note: \'%s\' is an alias for \'%s\''),$attr_display,$attr) . "\">alias</acronym>";
  542. } else {
  543. $attr_display = $attr;
  544. }
  545. # is this attribute required by an objectClass?
  546. $required_by = '';
  547. if ($schema_attr)
  548. foreach ($schema_attr->getRequiredByObjectClasses() as $required) {
  549. if (isset($attrs['objectClass']) && ! is_array($attrs['objectClass']))
  550. $attrs['objectClass'] = array($attrs['objectClass']);
  551. if (isset($attrs['objectClass']) && in_array(strtolower($required),arrayLower($attrs['objectClass'])))
  552. $required_by .= $required . ' ';
  553. # It seems that some LDAP servers (Domino) returns attributes in lower case?
  554. elseif (isset($attrs['objectclass']) && in_array(strtolower($required),arrayLower($attrs['objectclass'])))
  555. $required_by .= $required . ' ';
  556. }
  557. if ($required_by) {
  558. if (trim($attr_note))
  559. $attr_note .= ', ';
  560. $attr_note .= "<acronym title=\"" . sprintf(_('Required attribute for objectClass(es) %s'),$required_by) . "\">" . _('required') . "</acronym>&nbsp;";
  561. }
  562. # is this attribute required because its the RDN
  563. if (preg_match("/^${attr}=/",$rdn)) {
  564. if (trim($attr_note))
  565. $attr_note .= ', ';
  566. $attr_note .= "&nbsp;<acronym title=\"" . _('This attribute is required for the RDN.') . "\">" . 'rdn' . "</acronym>&nbsp;";
  567. }
  568. if (is_array($modified_attrs) && in_array($attr,$modified_attrs))
  569. $is_modified_attr = true;
  570. else
  571. $is_modified_attr = false;
  572. if ($is_modified_attr)
  573. echo '<tr class="updated_attr">';
  574. else
  575. echo '<tr>';
  576. echo '<td class="attr">';
  577. $schema_href = sprintf('schema.php?server_id=%s&amp;view=attributes&amp;viewvalue=%s',
  578. $ldapserver->server_id,real_attr_name($attr));
  579. printf('<b><a title="'._('Click to view the schema defintion for attribute type \'%s\'').'" href="%s">%s</a></b>',$attr,$schema_href,$attr_display);
  580. echo '</td>';
  581. echo '<td class="attr_note">';
  582. if ($attr_note)
  583. printf('<sup><small>%s</small></sup>',$attr_note);
  584. if ($ldapserver->isAttrReadOnly($attr))
  585. printf('<small>(<acronym title="%s">%s</acronym>)</small>',_('This attribute has been flagged as read only by the phpLDAPadmin administrator'),_('read only'));
  586. echo '</td>';
  587. echo '</tr>';
  588. if ($is_modified_attr)
  589. echo '<tr class="updated_attr">';
  590. else
  591. echo '<tr>';
  592. echo '<td class="val" colspan="2">';
  593. /*
  594. * Is this attribute a jpegPhoto?
  595. */
  596. if ($ldapserver->isJpegPhoto($attr)) {
  597. /* Don't draw the delete buttons if there is more than one jpegPhoto
  598. (phpLDAPadmin can't handle this case yet) */
  599. if ($ldapserver->isReadOnly() || $ldapserver->isAttrReadOnly($attr))
  600. draw_jpeg_photos($ldapserver,$dn,$attr,false);
  601. else
  602. draw_jpeg_photos($ldapserver,$dn,$attr,true);
  603. # proceed to the next attribute
  604. echo '</td></tr>';
  605. if ($is_modified_attr)
  606. echo '<tr class="updated_attr"><td class="bottom" colspan="2"></td></tr>';
  607. continue;
  608. }
  609. /*
  610. * Is this attribute binary?
  611. */
  612. if ($ldapserver->isAttrBinary($attr)) {
  613. $href = sprintf('download_binary_attr.php?server_id=%s&amp;dn=%s&amp;attr=%s',
  614. $ldapserver->server_id,$encoded_dn,$attr);
  615. echo '<small>';
  616. echo _('Binary value');
  617. if (! strcasecmp($attr,'objectSid'))
  618. printf(' (%s)',binSIDtoText($vals[0]));
  619. echo '<br />';
  620. if (count($vals) > 1) {
  621. for ($i=1; $i<=count($vals); $i++)
  622. printf('<a href="%s&amp;value_num=%s"><img src="images/save.png" /> %s(%s)</a><br />',
  623. $href,$i,_('download value'),$i);
  624. } else {
  625. printf('<a href="%s"><img src="images/save.png" /> %s</a><br />',$href,_('download value'));
  626. }
  627. if (! $ldapserver->isReadOnly() && ! $ldapserver->isAttrReadOnly($attr))
  628. printf('<a href="javascript:deleteAttribute(\'%s\');" style="color:red;"><img src="images/trash.png" /> %s</a>',
  629. $attr,_('delete attribute'));
  630. echo '</small>';
  631. echo '</td>';
  632. echo '</tr>';
  633. if ($is_modified_attr)
  634. echo '<tr class="updated_attr"><td class="bottom" colspan="2"></td></tr>';
  635. continue;
  636. }
  637. /*
  638. * Note: at this point,the attribute must be text-based (not binary or jpeg)
  639. */
  640. # If this is the userPassword attribute, add the javascript so we can call check password later.
  641. if (! strcasecmp($attr,'userPassword')) {
  642. $js[] = '
  643. <script type="text/javascript" language="javascript">
  644. <!--
  645. function passwordComparePopup(hash) {
  646. mywindow = open(\'password_checker.php\',\'myname\',\'resizable=no,width=450,height=200,scrollbars=1\');
  647. mywindow.location.href = \'password_checker.php?hash=\'+hash+\'&base64=true\';
  648. if (mywindow.opener == null) mywindow.opener = self;
  649. }
  650. -->
  651. </script>';
  652. }
  653. /*
  654. * If this server is in read-only mode or this attribute is configured as read_only,
  655. * simply draw the attribute values and continue.
  656. */
  657. if ($ldapserver->isReadOnly() || $ldapserver->isAttrReadOnly($attr) || (preg_match("/^${attr}=/",$rdn))) {
  658. if (is_array($vals)) {
  659. foreach ($vals as $i => $val) {
  660. if (trim($val) == '')
  661. printf('<span style="color:red">[%s]</span><br />',_('empty'));
  662. elseif (! strcasecmp($attr,'userPassword') && $config->GetValue('appearance','obfuscate_password_display'))
  663. echo preg_replace('/./','*',$val).'<br />';
  664. elseif (in_array(strtolower($attr),$shadow_format_attrs)) {
  665. $shadow_date = shadow_date($attrs,$attr);
  666. echo htmlspecialchars($val).'&nbsp;';
  667. echo '<small>';
  668. if (($today < $shadow_date) && in_array(strtolower($attr),$shadow_before_today_attrs))
  669. echo '<span style="color:red">'.htmlspecialchars("(".strftime($config->GetValue('appearance','date'),$shadow_date).")").'</span>';
  670. elseif ($today > $shadow_date && in_array(strtolower($attr),$shadow_after_today_attrs))
  671. echo '<span style="color:red">'.htmlspecialchars("(".strftime($config->GetValue('appearance','date'),$shadow_date).")").'</span>';
  672. else
  673. echo htmlspecialchars("(".strftime($config->GetValue('appearance','date'),shadow_date($attrs,$attr)).")");
  674. echo '</small>';
  675. } else
  676. echo htmlspecialchars($val).'<br />';
  677. }
  678. //@todo: redundant?
  679. } else {
  680. if (! strcasecmp($attr,'userPassword') && obfuscate_password_display())
  681. echo preg_replace('/./','*',$vals).'<br />';
  682. else
  683. echo $vals.'<br />';
  684. }
  685. if (! strcasecmp($attr,'userPassword'))
  686. printf('<small><a href="javascript:passwordComparePopup(\'%s\')">%s</a></small>',base64_encode($user_password),_('Check password...'));
  687. if (preg_match("/^${attr}=/",$rdn))
  688. printf('<small>(<a href="%s">%s</a>)</small>',$rename_href,_('rename'));
  689. echo '</td>';
  690. echo '</tr>';
  691. if ($is_modified_attr)
  692. echo '<tr class="updated_attr"><td class="bottom" colspan="2"></td></tr>';
  693. continue;
  694. }
  695. /*
  696. * Is this a userPassword attribute?
  697. */
  698. if (0 == strcasecmp($attr,'userpassword')) {
  699. foreach ($vals as $user_password) {
  700. $enc_type = get_enc_type($user_password);
  701. # Set the default hashing type if the password is blank (must be newly created)
  702. if ($user_password == '')
  703. $enc_type = get_default_hash($ldapserver->server_id);
  704. printf('<input type="hidden" name="old_values[userpassword][]" value="%s" />',htmlspecialchars($user_password));
  705. echo '<!-- Special case of enc_type to detect changes when user changes enc_type but not the password value -->';
  706. printf('<input size="38" type="hidden" name="old_enc_type[]" value="%s" />',($enc_type == '' ? 'clear' : $enc_type));
  707. if (obfuscate_password_display($enc_type))
  708. echo htmlspecialchars(preg_replace('/./','*',$user_password));
  709. else
  710. echo htmlspecialchars($user_password);
  711. echo '<br />';
  712. printf('<input style="width: 260px" type="%s" name="new_values[userpassword][]" value="" />',
  713. (obfuscate_password_display($enc_type) ? 'password' : 'text'));
  714. echo enc_type_select_list($enc_type);
  715. echo '<br />';
  716. printf('<small><a href="javascript:passwordComparePopup(\'%s\')">%s</a></small>',base64_encode($user_password),_('Check password...'));
  717. echo '<br />';
  718. }
  719. /* Draw the "add value" link under the list of values for this attributes */
  720. if (! $ldapserver->isReadOnly() && ($schema_attr = $ldapserver->getSchemaAttribute($attr,$dn)) &&
  721. ! $schema_attr->getIsSingleValue()) {
  722. $add_href = sprintf('add_value_form.php?server_id=%s&amp;dn=%s&amp;attr=%s',
  723. $ldapserver->server_id,$encoded_dn,rawurlencode($attr));
  724. printf('<div class="add_value">(<a href="%s" title="%s">%s</a>)</div>',
  725. $add_href,sprintf(_('Add an additional value to attribute \'%s\''),$attr),_('add value'));
  726. }
  727. echo '</td>';
  728. echo '</tr>';
  729. if ($is_modified_attr)
  730. echo '<tr class="updated_attr"><td class="bottom" colspan="2"></td></tr>';
  731. continue;
  732. }
  733. /*
  734. * Is this a boolean attribute?
  735. */
  736. if ($ldapserver->isAttrBoolean($attr)) {
  737. $val = $vals[0];
  738. printf('<input type="hidden" name="old_values[%s][]" value="%s" />',htmlspecialchars($attr),htmlspecialchars($val));
  739. printf('<select name="new_values[%s][]">',htmlspecialchars($attr));
  740. printf('<option value="TRUE" %s>%s</option>',($val=='TRUE' ? ' selected' : ''),_('true'));
  741. printf('<option value="FALSE" %s>%s</option>',($val=='FALSE' ? ' selected' : ''),_('false'));
  742. printf('<option value="">(%s)</option>',_('none, remove value'));
  743. echo '</select>';
  744. echo '</td>';
  745. echo '</tr>';
  746. if ($is_modified_attr)
  747. echo '<tr class="updated_attr"><td class="bottom" colspan="2"></td></tr>';
  748. continue;
  749. }
  750. /*
  751. * Is this a date type attribute?
  752. */
  753. if (in_array_ignore_case($attr,array_keys($js_date_attrs))) {
  754. $val = $vals[0];
  755. printf('<input type="hidden" name="old_values[%s][]" value="%s" />',htmlspecialchars($attr),htmlspecialchars($val));
  756. printf('<nobr><input type="text" size="30" id="f_date_%s" name="new_values[%s][0]" value="%s" />&nbsp;',
  757. $attr,htmlspecialchars($attr),htmlspecialchars($val));
  758. draw_date_selector_link($attr);
  759. echo '</nobr></td>';
  760. echo '</tr>';
  761. $js[] = sprintf('<script type="text/javascript" language="javascript">defaults[\'f_date_%s\'] = \'%s\';</script>',$attr,$js_date_attrs[$attr]);
  762. if ($is_modified_attr)
  763. echo '<tr class="updated_attr"><td class="bottom" colspan="2"></td></tr>';
  764. continue;
  765. }
  766. /*
  767. * End of special case attributes (non plain text).
  768. */
  769. /*
  770. * This is a plain text attribute, to be displayed and edited in plain text.
  771. */
  772. foreach ($vals as $i => $val) {
  773. $input_name = sprintf('new_values[%s][%s]',htmlspecialchars($attr),$i);
  774. /* We smack an id="..." tag in here that doesn't have [][] in it to allow the
  775. draw_chooser_link() to identify it after the user clicks. */
  776. $input_id = sprintf('new_values_%s_%s',htmlspecialchars($attr),$i);
  777. /* The old_values array will let update.php know if the entry contents changed
  778. between the time the user loaded this page and saved their changes. */
  779. printf('<input type="hidden" name="old_values[%s][%s]" value="%s" />',
  780. htmlspecialchars($attr),$i,htmlspecialchars($val));
  781. # Is this value is a structural objectClass, make it read-only
  782. if (! strcasecmp($attr,'objectClass')) {
  783. printf('<a title="%s" href="schema.php?server_id=%s&amp;view=objectClasses&amp;viewvalue=%s"><img src="images/info.png" /></a>&nbsp;',
  784. _('View the schema description for this objectClass'),$ldapserver->server_id,htmlspecialchars($val));
  785. $schema_object = $ldapserver->getSchemaObjectClass($val);
  786. # This should be an object, but we'll test it anyway
  787. if (is_object($schema_object) && $schema_object->getType() == 'structural') {
  788. printf(' %s <small>(<acronym title="%s">%s</acronym>)</small><br />',
  789. $val,_('This is a structural ObjectClass and cannot be removed.'),_('structural'));
  790. printf('<input type="hidden" name="%s" id="%s" value="%s" />',$input_name,$input_id,htmlspecialchars($val));
  791. continue;
  792. }
  793. }
  794. if (is_dn_string($val) || $ldapserver->isDNAttr($attr))
  795. if ($ldapserver->dnExists($val)) {
  796. printf('<a title="'._('Go to %s').'" href="template_engine.php?server_id=%s&amp;dn=%s"><img style="vertical-align: top" src="images/go.png" /></a>&nbsp;',
  797. htmlspecialchars($val),$ldapserver->server_id,rawurlencode($val));
  798. } else {
  799. printf('<a title="'._('DN not available %s').'"><img style="vertical-align: top" src="images/nogo.png" /></a>&nbsp;',
  800. htmlspecialchars($val),$ldapserver->server_id,rawurlencode($val));
  801. }
  802. elseif (is_mail_string($val))
  803. printf('<a href="mailto:%s"><img style="vertical-align: center" src="images/mail.png" /></a>&nbsp;',htmlspecialchars($val));
  804. elseif (is_url_string($val))
  805. printf('<a href="%s" target="new"><img style="vertical-align: center" src="images/dc.png" /></a>&nbsp;',htmlspecialchars($val));
  806. if ($ldapserver->isMultiLineAttr($attr,$val))
  807. printf('<textarea class="val" rows="3" cols="50" name="%s" id="%s">%s</textarea>',$input_name,$input_id,htmlspecialchars($val));
  808. else
  809. printf('<input type="text" class="val" name="%s" id="%s" value="%s" />&nbsp;',$input_name,$input_id,htmlspecialchars($val));
  810. /* draw a link for popping up the entry browser if this is the type of attribute
  811. that houses DNs. */
  812. if ($ldapserver->isDNAttr($attr))
  813. draw_chooser_link("edit_form.$input_id",false);
  814. echo '<br />';
  815. # If this is a gidNumber on a non-PosixGroup entry, lookup its name and description for convenience
  816. if (! strcasecmp($attr,'gidNumber') &&
  817. ! in_array_ignore_case('posixGroup',$ldapserver->getDNAttr($dn,'objectClass'))) {
  818. $gid_number = $val;
  819. $search_group_filter = "(&(objectClass=posixGroup)(gidNumber=$val))";
  820. $group = $ldapserver->search(null,null,$search_group_filter,array('dn','description'));
  821. if (count($group) > 0) {
  822. echo '<br />';
  823. $group = array_pop($group);
  824. $group_dn = $group['dn'];
  825. $group_name = explode('=',get_rdn($group_dn));
  826. $group_name = $group_name[1];
  827. $href = sprintf('template_engine.php?server_id=%s&amp;dn=%s',$ldapserver->server_id,urlencode($group_dn));
  828. echo '<small>';
  829. printf('<a href="%s">%s</a>',$href,htmlspecialchars($group_name));
  830. $description = isset($group['description']) ? $group['description'] : null;
  831. if ($description)
  832. printf(' (%s)',htmlspecialchars($description));
  833. echo '</small>';
  834. }
  835. }
  836. # Show the dates for all the shadow attributes.
  837. if (in_array(strtolower($attr),$shadow_format_attrs)) {
  838. if (($shadow_date = shadow_date($attrs,$attr)) !== false) {
  839. echo '<br />';
  840. echo '<small>';
  841. if (($today < $shadow_date) && in_array(strtolower($attr),$shadow_before_today_attrs))
  842. echo '<span style="color:red">'.htmlspecialchars(strftime($config->GetValue('appearance','date'),$shadow_date)).'</span>';
  843. elseif ($today > $shadow_date && in_array(strtolower($attr),$shadow_after_today_attrs))
  844. echo '<span style="color:red">'.htmlspecialchars(strftime($config->GetValue('appearance','date'),$shadow_date)).'</span>';
  845. else
  846. echo htmlspecialchars(strftime($config->GetValue('appearance','date'),$shadow_date));
  847. echo '</small>';
  848. }
  849. }
  850. } /* end foreach value */
  851. /* Draw the "add value" link under the list of values for this attributes */
  852. if (! $ldapserver->isReadOnly() && ($schema_attr = $ldapserver->getSchemaAttribute($attr,$dn)) &&
  853. ! $schema_attr->getIsSingleValue()) {
  854. $add_href = sprintf('add_value_form.php?server_id=%s&amp;dn=%s&amp;attr=%s',
  855. $ldapserver->server_id,$encoded_dn,rawurlencode($attr));
  856. printf('<div class="add_value">(<a href="%s" title="%s">%s</a>)</div>',
  857. $add_href,sprintf(_('Add an additional value to attribute \'%s\''),$attr),_('add value'));
  858. }
  859. echo '</td>';
  860. echo '</tr>';
  861. if ($is_modified_attr)
  862. echo '<tr class="updated_attr"><td class="bottom" colspan="2"></td></tr>';
  863. echo "\n";
  864. flush();
  865. } /* End foreach ($attrs as $attr => $vals) */
  866. if (! $ldapserver->isReadOnly())
  867. printf('<tr><td colspan="2"><center><input type="submit" value="%s" /></center></td></tr></table></form>',
  868. _('Save Changes'));
  869. else
  870. printf('</table>');
  871. ?>
  872. <!-- This form is submitted by JavaScript when the user clicks "Delete attribute" on a binary attribute -->
  873. <form name="delete_attribute_form" action="delete_attr.php" method="post">
  874. <input type="hidden" name="server_id" value="<?php echo $ldapserver->server_id; ?>" />
  875. <input type="hidden" name="dn" value="<?php echo $dn; ?>" />
  876. <input type="hidden" name="attr" value="FILLED IN BY JAVASCRIPT" />
  877. </form>
  878. <?php
  879. foreach ($js as $script)
  880. echo $script;
  881. ?>
  882. <!-- If this entry has a binary attribute, we need to provide a form for it to submit when deleting it. -->
  883. <script type="text/javascript" language="javascript">
  884. <!--
  885. function deleteAttribute(attrName)
  886. {
  887. if (confirm("<?php echo _('Really delete attribute'); ?> '" + attrName + "'?")) {
  888. document.delete_attribute_form.attr.value = attrName;
  889. document.delete_attribute_form.submit();
  890. }
  891. }
  892. -->
  893. </script>
  894. </body>
  895. </html>