php.eval.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Contains rules integration for the php module needed during evaluation.
  5. *
  6. * @addtogroup rules
  7. * @{
  8. */
  9. /**
  10. * A class implementing a rules input evaluator processing PHP.
  11. */
  12. class RulesPHPEvaluator extends RulesDataInputEvaluator {
  13. public static function access() {
  14. return user_access('use PHP for settings');
  15. }
  16. public static function getUsedVars($text, $var_info) {
  17. if (strpos($text, '<?') !== FALSE) {
  18. $used_vars = array();
  19. foreach ($var_info as $name => $info) {
  20. if (strpos($text, '$' . $name) !== FALSE) {
  21. $used_vars[] = $name;
  22. }
  23. }
  24. return $used_vars;
  25. }
  26. }
  27. public function prepare($text, $var_info) {
  28. // A returned NULL skips the evaluator.
  29. $this->setting = self::getUsedVars($text, $var_info);
  30. }
  31. /**
  32. * Evaluates PHP code contained in $text. This doesn't apply $options, thus
  33. * the PHP code is responsible for behaving appropriately.
  34. */
  35. public function evaluate($text, $options, RulesState $state) {
  36. $vars['eval_options'] = $options;
  37. foreach ($this->setting as $key => $var_name) {
  38. $vars[$var_name] = $state->get($var_name);
  39. }
  40. return rules_php_eval($text, rules_unwrap_data($vars));
  41. }
  42. public static function help($var_info) {
  43. module_load_include('inc', 'rules', 'rules/modules/php.rules');
  44. $render = array(
  45. '#type' => 'fieldset',
  46. '#title' => t('PHP Evaluation'),
  47. '#collapsible' => TRUE,
  48. '#collapsed' => TRUE,
  49. ) + rules_php_evaluator_help($var_info);
  50. return $render;
  51. }
  52. }
  53. /**
  54. * A data processor using PHP.
  55. */
  56. class RulesPHPDataProcessor extends RulesDataProcessor {
  57. protected static function form($settings, $var_info) {
  58. $settings += array('code' => '');
  59. $form = array(
  60. '#type' => 'fieldset',
  61. '#title' => t('PHP evaluation'),
  62. '#collapsible' => TRUE,
  63. '#collapsed' => empty($settings['code']),
  64. '#description' => t('Enter PHP code to process the selected argument value.'),
  65. );
  66. $form['code'] = array(
  67. '#type' => 'textarea',
  68. '#title' => t('Code'),
  69. '#description' => t('Enter PHP code without &lt;?php ?&gt; delimiters that returns the processed value. The selected value is available in the variable $value. Example: %code', array('%code' => 'return $value + 1;')),
  70. '#default_value' => $settings['code'],
  71. '#weight' => 5,
  72. );
  73. return $form;
  74. }
  75. public static function access() {
  76. return user_access('use PHP for settings');
  77. }
  78. public function process($value, $info, RulesState $state, RulesPlugin $element) {
  79. $value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element) : $value;
  80. return rules_php_eval_return($this->setting['code'], array('value' => $value));
  81. }
  82. }
  83. /**
  84. * Action and condition callback: Execute PHP code.
  85. */
  86. function rules_execute_php_eval($code, $settings, $state, $element) {
  87. $data = array();
  88. if (!empty($settings['used_vars'])) {
  89. foreach ($settings['used_vars'] as $key => $var_name) {
  90. $data[$var_name] = $state->get($var_name);
  91. }
  92. }
  93. return rules_php_eval_return($code, rules_unwrap_data($data));
  94. }
  95. /**
  96. * Evalutes the given PHP code, with the given variables defined.
  97. *
  98. * @param $code
  99. * The PHP code to run, with <?php ?>
  100. * @param $arguments
  101. * Array containing variables to be extracted to the code.
  102. *
  103. * @return
  104. * The output of the php code.
  105. */
  106. function rules_php_eval($code, $arguments = array()) {
  107. extract($arguments);
  108. ob_start();
  109. print eval('?>'. $code);
  110. $output = ob_get_contents();
  111. ob_end_clean();
  112. return $output;
  113. }
  114. /**
  115. * Evalutes the given PHP code, with the given variables defined. This is like
  116. * rules_php_eval() but does return the returned data from the PHP code.
  117. *
  118. * @param $code
  119. * The PHP code to run, without <?php ?>
  120. * @param $arguments
  121. * Array containing variables to be extracted to the code.
  122. *
  123. * @return
  124. * The return value of the evaled code.
  125. */
  126. function rules_php_eval_return($code, $arguments = array()) {
  127. extract($arguments);
  128. return eval($code);
  129. }
  130. /**
  131. * @}
  132. */