php.inc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide access control based on evaluated PHP.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t("PHP Code"),
  12. 'description' => t('Control access through arbitrary PHP code.'),
  13. 'callback' => 'ctools_php_ctools_access_check',
  14. 'default' => array('description' => '', 'php' => ''),
  15. 'settings form' => 'ctools_php_ctools_access_settings',
  16. 'summary' => 'ctools_php_ctools_access_summary',
  17. 'all contexts' => TRUE,
  18. );
  19. /**
  20. * Settings form for the 'by perm' access plugin
  21. *
  22. * @todo Need a way to provide a list of all available contexts to be used by
  23. * the eval-ed PHP.
  24. */
  25. function ctools_php_ctools_access_settings($form, &$form_state, $conf) {
  26. $perms = array();
  27. $form['settings']['description'] = array(
  28. '#type' => 'textfield',
  29. '#title' => t('Administrative desc'),
  30. '#default_value' => $conf['description'],
  31. '#description' => t('A description for this test for administrative purposes.'),
  32. );
  33. $form['settings']['php'] = array(
  34. '#type' => 'textarea',
  35. '#title' => t('PHP Code'),
  36. '#default_value' => $conf['php'],
  37. '#description' => t('Access will be granted if the following PHP code returns <code>TRUE</code>. Do not include &lt;?php ?&gt;. Note that executing incorrect PHP-code can break your Drupal site. All contexts will be available in the <em>$contexts</em> variable.'),
  38. );
  39. if (!user_access('use PHP for settings')) {
  40. $form['settings']['php']['#disabled'] = TRUE;
  41. $form['settings']['php']['#value'] = $conf['php'];
  42. $form['settings']['php']['#description'] .= ' ' . t('You do not have sufficient permissions to edit PHP code.');
  43. }
  44. return $form;
  45. }
  46. /**
  47. * Check for access.
  48. */
  49. function ctools_php_ctools_access_check($__conf, $contexts) {
  50. $access = eval($__conf['php']);
  51. return $access;
  52. }
  53. /**
  54. * Provide a summary description based upon the checked roles.
  55. */
  56. function ctools_php_ctools_access_summary($conf, $contexts) {
  57. return !empty($conf['description']) ? check_plain($conf['description']) : t('No description');
  58. }