選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

scriptmanager.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. class ScriptManager
  3. {
  4. private $scriptCode;
  5. # Sections for the script building
  6. private $requires = array('"date"','"relational"','"vacation"');
  7. public function BuildScriptFromParams($params)
  8. {
  9. # Read variables
  10. $message = $params['message'];
  11. # Detect the format
  12. $format = 'text';
  13. if ( strpos('<html', $message) ) $format = 'html';
  14. # Build vacation scripts
  15. $execBlock =
  16. sprintf("## Generated by Vacation Sieve plugin for roundcube, the %s ##\n\n", date('r'));
  17. # Include initial params in the script
  18. $execBlock .= sprintf("## Initial params: ##\n##STARTPARAMS%sENDPARAMS\n\n", json_encode($params));
  19. if($params['enable'])
  20. {
  21. # Add require blocks
  22. if ( $params['appendSubject'] ) $this->requires[] = '"variables"';
  23. $execBlock .= sprintf('require [%s];'."\n\n", join(",",$this->requires));
  24. # Add needed variables
  25. if ( $params['appendSubject'] )
  26. {
  27. $execBlock .= 'set "subject" "";';
  28. $execBlock .= 'if header :matches "subject" "*" { set "subject" "${1}"; }' ;
  29. $execBlock .= "\n\n";
  30. }
  31. # Build conditions
  32. $startDate = preg_replace('#(\d\d)/(\d\d)/(\d\d\d\d)#','$3-$2-$1',$params['start']);
  33. $endDate = preg_replace('#(\d\d)/(\d\d)/(\d\d\d\d)#','$3-$2-$1',$params['end']);
  34. if ( empty($params['starttime']) && empty($params['endtime']) )
  35. {
  36. # Different days, hours not specified
  37. $execBlock .= sprintf('if allof (currentdate :value "ge" "date" "%s",'.
  38. ' currentdate :value "le" "date" "%s")'."\n",
  39. $startDate,
  40. $endDate);
  41. }
  42. elseif ( $startDate == $endDate )
  43. {
  44. # Same day, between two hours
  45. $execBlock .= sprintf(
  46. 'if allof (currentdate :value "eq" "date" "%s", currentdate :value "ge" "hour" "%02d", '.
  47. ' currentdate :value "le" "hour" "%02d")',
  48. $startDate,
  49. $params['starttime'],
  50. $params['endtime']);
  51. }
  52. else
  53. {
  54. # With Hour Selection
  55. # - day is first day and current hour > starttime
  56. # - day is last day and current hour < endtime
  57. # - current day is > startdate and current day is < enddate
  58. if ( empty($params['starttime']) )
  59. $params['starttime'] = '00:00';
  60. if ( empty($params['endtime']) )
  61. $params['starttime'] = '23';
  62. $execBlock .= sprintf(
  63. 'if anyof ( allof (currentdate :value "eq" "date" "%s", currentdate :value "ge" "hour" "%02d"),'.
  64. ' allof (currentdate :value "eq" "date" "%s", currentdate :value "le" "hour" "%02d"),'.
  65. ' allof (currentdate :value "gt" "date" "%s", currentdate :value "lt" "date" "%s")'.
  66. ')',
  67. $startDate,
  68. $params['starttime'],
  69. $endDate,
  70. $params['endtime'],
  71. $startDate,
  72. $endDate);
  73. }
  74. # Start to build the script
  75. $execBlock .= "\n{\n vacation\n";
  76. $execBlock .= sprintf(" :days %d\n", $params['every']);
  77. # Add addresses
  78. if ( is_array($params['addresses']) )
  79. {
  80. $addresses = array();
  81. foreach ( $params['addresses'] as $address )
  82. {
  83. $address = preg_replace('/.*<(.*)>/', '"$1"', $address);
  84. $addresses[] = $address;
  85. }
  86. $execBlock .= sprintf(" :addresses [%s]\n", join(",",$addresses));
  87. }
  88. # Set subject
  89. $subject = str_replace('"', '\\"', $params['subject']);
  90. if ( $params['appendSubject'] )
  91. $execBlock .= sprintf(' :subject "%s: ${subject}"'."\n", $subject);
  92. else
  93. $execBlock .= sprintf(' :subject "%s"'."\n", $subject);
  94. # This regenerate a different handler every time the filter is saved.
  95. $handle = substr(md5(mt_rand()),0,8);
  96. $execBlock .= sprintf(' :handle "%s"'."\n", $handle);
  97. # Add the from address
  98. $sendFrom = preg_replace('/.*<(.*)>/', '$1', $params['sendFrom']);
  99. $execBlock .= sprintf(' :from "%s"'."\n", $sendFrom);
  100. # Add the message in text format
  101. $message = str_replace('"', '\\"', $params['message']);
  102. $message = trim($message);
  103. $execBlock .= sprintf(' "%s";'."\n", $message);
  104. $execBlock .= "}";
  105. }
  106. #header('Content-type: text/plain');
  107. #print($execBlock);
  108. #exit;
  109. return $execBlock;
  110. }
  111. public function LoadParamsFromScript($script)
  112. {
  113. $params = json_decode($script, true);
  114. return $params;
  115. }
  116. }