PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/zarafa/listfolders.php

http://github.com/xbgmsharp/sogosync
PHP | 184 lines | 108 code | 29 blank | 47 comment | 29 complexity | 78fc4dd1fc9b47a69272f66bd11450f3 MD5 | raw file
Possible License(s): AGPL-3.0
  1. #!/usr/bin/php
  2. <?php
  3. /***********************************************
  4. * File : listfolders.php
  5. * Project : Z-Push
  6. * Descr : This is a small command line
  7. * tool to list folders of a user
  8. * store or public folder available
  9. * for synchronization.
  10. *
  11. * Created : 06.05.2011
  12. *
  13. * Copyright 2007 - 2011 Zarafa Deutschland GmbH
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation with the following additional
  18. * term according to sec. 7:
  19. *
  20. * According to sec. 7 of the GNU Affero General Public License, version 3,
  21. * the terms of the AGPL are supplemented with the following terms:
  22. *
  23. * "Zarafa" is a registered trademark of Zarafa B.V.
  24. * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
  25. * The licensing of the Program under the AGPL does not imply a trademark license.
  26. * Therefore any rights, title and interest in our trademarks remain entirely with us.
  27. *
  28. * However, if you propagate an unmodified version of the Program you are
  29. * allowed to use the term "Z-Push" to indicate that you distribute the Program.
  30. * Furthermore you may use our trademarks where it is necessary to indicate
  31. * the intended purpose of a product or service provided you use it in accordance
  32. * with honest practices in industrial or commercial matters.
  33. * If you want to propagate modified versions of the Program under the name "Z-Push",
  34. * you may only do so if you have a written permission by Zarafa Deutschland GmbH
  35. * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU Affero General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU Affero General Public License
  43. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  44. *
  45. * Consult LICENSE file for details
  46. ************************************************/
  47. define("PHP_MAPI_PATH", "/usr/share/php/mapi/");
  48. define('MAPI_SERVER', 'file:///var/run/zarafa');
  49. $supported_classes = array (
  50. "IPF.Note" => "SYNC_FOLDER_TYPE_USER_MAIL",
  51. "IPF.Task" => "SYNC_FOLDER_TYPE_USER_TASK",
  52. "IPF.Appointment" => "SYNC_FOLDER_TYPE_USER_APPOINTMENT",
  53. "IPF.Contact" => "SYNC_FOLDER_TYPE_USER_CONTACT",
  54. "IPF.StickyNote" => "SYNC_FOLDER_TYPE_USER_NOTE"
  55. );
  56. main();
  57. function main() {
  58. listfolders_configure();
  59. listfolders_handle();
  60. }
  61. function listfolders_configure() {
  62. if (!isset($_SERVER["TERM"]) || !isset($_SERVER["LOGNAME"])) {
  63. echo "This script should not be called in a browser.\n";
  64. exit(1);
  65. }
  66. if (!function_exists("getopt")) {
  67. echo "PHP Function 'getopt()' not found. Please check your PHP version and settings.\n";
  68. exit(1);
  69. }
  70. require(PHP_MAPI_PATH.'mapi.util.php');
  71. require(PHP_MAPI_PATH.'mapidefs.php');
  72. require(PHP_MAPI_PATH.'mapicode.php');
  73. require(PHP_MAPI_PATH.'mapitags.php');
  74. require(PHP_MAPI_PATH.'mapiguid.php');
  75. }
  76. function listfolders_handle() {
  77. $shortoptions = "l:h:u:p:";
  78. $options = getopt($shortoptions);
  79. $mapi = MAPI_SERVER;
  80. $user = "SYSTEM";
  81. $pass = "";
  82. if (isset($options['h']))
  83. $mapi = $options['h'];
  84. if (isset($options['u']) && isset($options['p'])) {
  85. $user = $options['u'];
  86. $pass = $options['p'];
  87. }
  88. $zarafaAdmin = listfolders_zarafa_admin_setup($mapi, $user, $pass);
  89. if (isset($zarafaAdmin['adminStore']) && isset($options['l'])) {
  90. listfolders_getlist($zarafaAdmin['adminStore'], $zarafaAdmin['session'], trim($options['l']));
  91. }
  92. else {
  93. echo "Usage:\nlistfolders.php [actions] [options]\n\nActions: [-l username]\n\t-l username\tlist folders of user, for public folder use 'SYSTEM'\n\nGlobal options: [-h path] [[-u remoteuser] [-p password]]\n\t-h path\t\tconnect through <path>, e.g. file:///var/run/socket\n\t-u authuser\tlogin as authenticated administration user\n\t-p authpassword\tpassword of the remoteuser\n\n";
  94. }
  95. }
  96. function listfolders_zarafa_admin_setup ($mapi, $user, $pass) {
  97. $session = @mapi_logon_zarafa($user, $pass, $mapi);
  98. if (!$session) {
  99. echo "User '$user' could not login. The script will exit. Errorcode: 0x". sprintf("%x", mapi_last_hresult()) . "\n";
  100. exit(1);
  101. }
  102. $stores = @mapi_getmsgstorestable($session);
  103. $storeslist = @mapi_table_queryallrows($stores);
  104. $adminStore = @mapi_openmsgstore($session, $storeslist[0][PR_ENTRYID]);
  105. $zarafauserinfo = @mapi_zarafa_getuser_by_name($adminStore, $user);
  106. $admin = (isset($zarafauserinfo['admin']) && $zarafauserinfo['admin'])?true:false;
  107. if (!$stores || !$storeslist || !$adminStore || !$admin) {
  108. echo "There was error trying to log in as admin or retrieving admin info. The script will exit.\n";
  109. exit(1);
  110. }
  111. return array("session" => $session, "adminStore" => $adminStore);
  112. }
  113. function listfolders_getlist ($adminStore, $session, $user) {
  114. global $supported_classes;
  115. if (strtoupper($user) == 'SYSTEM') {
  116. // Find the public store store
  117. $storestables = @mapi_getmsgstorestable($session);
  118. $result = @mapi_last_hresult();
  119. if ($result == NOERROR){
  120. $rows = @mapi_table_queryallrows($storestables, array(PR_ENTRYID, PR_MDB_PROVIDER));
  121. foreach($rows as $row) {
  122. if (isset($row[PR_MDB_PROVIDER]) && $row[PR_MDB_PROVIDER] == ZARAFA_STORE_PUBLIC_GUID) {
  123. if (!isset($row[PR_ENTRYID])) {
  124. echo "Public folder are not available.\nIf this is a multi-tenancy system, use -u and -p and login with an admin user of the company.\nThe script will exit.\n";
  125. exit (1);
  126. }
  127. $entryid = $row[PR_ENTRYID];
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. else
  134. $entryid = @mapi_msgstore_createentryid($adminStore, $user);
  135. $userStore = @mapi_openmsgstore($session, $entryid);
  136. $hresult = mapi_last_hresult();
  137. // Cache the store for later use
  138. if($hresult != NOERROR) {
  139. echo "Could not open store for '$user'. The script will exit.\n";
  140. exit (1);
  141. }
  142. $folder = @mapi_msgstore_openentry($userStore);
  143. $h_table = @mapi_folder_gethierarchytable($folder, CONVENIENT_DEPTH);
  144. $subfolders = @mapi_table_queryallrows($h_table, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_CONTAINER_CLASS, PR_SOURCE_KEY));
  145. echo "Available folders in store '$user':\n" . str_repeat("-", 50) . "\n";
  146. foreach($subfolders as $folder) {
  147. if (isset($folder[PR_CONTAINER_CLASS]) && array_key_exists($folder[PR_CONTAINER_CLASS], $supported_classes)) {
  148. echo "Folder name:\t". $folder[PR_DISPLAY_NAME] . "\n";
  149. echo "Folder ID:\t". bin2hex($folder[PR_SOURCE_KEY]) . "\n";
  150. echo "Type:\t\t". $supported_classes[$folder[PR_CONTAINER_CLASS]] . "\n";
  151. echo "\n";
  152. }
  153. }
  154. }
  155. ?>