kopano-mr-process 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env php
  2. <?php
  3. # -*- Mode: php -*-
  4. /*
  5. * Copyright 2005 - 2015 Zarafa B.V.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. include('mapi/mapi.util.php');
  21. include('mapi/mapidefs.php');
  22. include('mapi/mapicode.php');
  23. include('mapi/mapitags.php');
  24. include('mapi/mapiguid.php');
  25. include('mapi/class.meetingrequest.php');
  26. include('mapi/class.recurrence.php');
  27. include('mapi/class.freebusypublish.php');
  28. define('RECURRENCE_AVAILABILITY_RANGE', 60 * 60 * 24 * 180); // 180 days
  29. $DEBUG = 1;
  30. function parseConfig($configfile)
  31. {
  32. $fp = fopen($configfile, "rt");
  33. if(!$fp)
  34. return false;
  35. $settings = array();
  36. while($line = fgets($fp)) {
  37. if($line[0] == '#')
  38. continue;
  39. $pos = strpos($line, "=");
  40. if($pos) {
  41. $key = trim(substr($line, 0, $pos));
  42. $value = trim(substr($line, $pos+1));
  43. $settings[$key] = $value;
  44. }
  45. }
  46. return $settings;
  47. }
  48. function u2w($s)
  49. {
  50. return $s;
  51. }
  52. if(!function_exists('hex2bin')){
  53. function hex2bin($data)
  54. {
  55. return pack("H*", $data);
  56. }
  57. }
  58. function debugLog($message)
  59. {
  60. global $DEBUG;
  61. if($DEBUG) {
  62. print($message);
  63. }
  64. }
  65. /**
  66. * Auto-process a meeting request, response or cancellation
  67. */
  68. function autoProcess($session, $store, $entryid)
  69. {
  70. debugLog("Processing item with entryid " . bin2hex($entryid) . "\n");
  71. $message = mapi_msgstore_openentry($store, $entryid);
  72. if(!$message) {
  73. debugLog("Unable to open item with entryid " . bin2hex($entryid) . "\n");
  74. return false;
  75. }
  76. $mr = new Meetingrequest($store, $message, $session);
  77. if($mr->isMeetingRequest()) {
  78. // Check general policy settings
  79. $mr->doAccept(true, false, false);
  80. return true;
  81. } else if($mr->isMeetingRequestResponse()) {
  82. $mr->processMeetingRequestResponse();
  83. return true;
  84. } else if($mr->isMeetingCancellation()) {
  85. $mr->processMeetingCancellation();
  86. return true;
  87. }
  88. }
  89. // Since the username we are getting from the commandline is always in utf8, we have
  90. // to force LC_CTYPE to an UTF-8 language. This makes sure that opening the user's store
  91. // will always open the correct user's store.
  92. forceUTF8(LC_CTYPE);
  93. forceUTF8(LC_MESSAGES);
  94. textdomain("kopano");
  95. if(count($argv) != 4) {
  96. print "Usage: " . $argv[0] . " <username> <path/to/dagent.cfg> <entryid>\n";
  97. print
  98. print "If <entryid> is not specified, all unresponded MR's in the inbox are processed\n";
  99. exit(1);
  100. }
  101. $username = $argv[1];
  102. $config = $argv[2];
  103. $entryid = $argv[3];
  104. $settings = parseConfig($config);
  105. if(!$settings || !isset($settings["server_socket"])) {
  106. $settings["server_socket"] = "default:";
  107. }
  108. if(isset($settings["sslkey_file"]) && isset($settings["sslkey_pass"]))
  109. $session = mapi_logon_zarafa($username, "", $settings["server_socket"], $settings["sslkey_file"], $settings["sslkey_pass"]);
  110. else
  111. $session = mapi_logon_zarafa($username, "", $settings["server_socket"]);
  112. $store = GetDefaultStore($session);
  113. $inbox = mapi_msgstore_getreceivefolder($store);
  114. autoProcess($session, $store, hex2bin($entryid));
  115. $storeprops = mapi_getprops($store, array(PR_MAILBOX_OWNER_ENTRYID));
  116. $fb = new FreeBusyPublish($session, $store, getCalendar($store), $storeprops[PR_MAILBOX_OWNER_ENTRYID]);
  117. $fb->PublishFB(time() - (7 * 24 * 60 * 60), 6 * 30 * 24 * 60 * 60); // publish from one week ago, 6 months ahead
  118. exit(0);
  119. ?>