Back to index

d-push  2.0
listfolders.php
Go to the documentation of this file.
00001 #!/usr/bin/php
00002 <?php
00003 /***********************************************
00004 * File      :   listfolders.php
00005 * Project   :   Z-Push
00006 * Descr     :   This is a small command line
00007 *               tool to list folders of a user
00008 *               store or public folder available
00009 *               for synchronization.
00010 *
00011 * Created   :   06.05.2011
00012 *
00013 * Copyright 2007 - 2011 Zarafa Deutschland GmbH
00014 *
00015 * This program is free software: you can redistribute it and/or modify
00016 * it under the terms of the GNU Affero General Public License, version 3,
00017 * as published by the Free Software Foundation with the following additional
00018 * term according to sec. 7:
00019 *
00020 * According to sec. 7 of the GNU Affero General Public License, version 3,
00021 * the terms of the AGPL are supplemented with the following terms:
00022 *
00023 * "Zarafa" is a registered trademark of Zarafa B.V.
00024 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00025 * The licensing of the Program under the AGPL does not imply a trademark license.
00026 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00027 *
00028 * However, if you propagate an unmodified version of the Program you are
00029 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00030 * Furthermore you may use our trademarks where it is necessary to indicate
00031 * the intended purpose of a product or service provided you use it in accordance
00032 * with honest practices in industrial or commercial matters.
00033 * If you want to propagate modified versions of the Program under the name "Z-Push",
00034 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00035 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00036 *
00037 * This program is distributed in the hope that it will be useful,
00038 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00039 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00040 * GNU Affero General Public License for more details.
00041 *
00042 * You should have received a copy of the GNU Affero General Public License
00043 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00044 *
00045 * Consult LICENSE file for details
00046 ************************************************/
00047 
00048 define("PHP_MAPI_PATH", "/usr/share/php/mapi/");
00049 define('MAPI_SERVER', 'file:///var/run/zarafa');
00050 
00051 $supported_classes = array (
00052     "IPF.Note"          => "SYNC_FOLDER_TYPE_USER_MAIL",
00053     "IPF.Task"          => "SYNC_FOLDER_TYPE_USER_TASK",
00054     "IPF.Appointment"   => "SYNC_FOLDER_TYPE_USER_APPOINTMENT",
00055     "IPF.Contact"       => "SYNC_FOLDER_TYPE_USER_CONTACT",
00056     "IPF.StickyNote"    => "SYNC_FOLDER_TYPE_USER_NOTE"
00057 );
00058 
00059 main();
00060 
00061 function main() {
00062     listfolders_configure();
00063     listfolders_handle();
00064 }
00065 
00066 function listfolders_configure() {
00067 
00068     if (!isset($_SERVER["TERM"]) || !isset($_SERVER["LOGNAME"])) {
00069         echo "This script should not be called in a browser.\n";
00070         exit(1);
00071     }
00072 
00073     if (!function_exists("getopt")) {
00074         echo "PHP Function 'getopt()' not found. Please check your PHP version and settings.\n";
00075         exit(1);
00076     }
00077 
00078     require(PHP_MAPI_PATH.'mapi.util.php');
00079     require(PHP_MAPI_PATH.'mapidefs.php');
00080     require(PHP_MAPI_PATH.'mapicode.php');
00081     require(PHP_MAPI_PATH.'mapitags.php');
00082     require(PHP_MAPI_PATH.'mapiguid.php');
00083 }
00084 
00085 function listfolders_handle() {
00086     $shortoptions = "l:h:u:p:";
00087     $options = getopt($shortoptions);
00088 
00089     $mapi = MAPI_SERVER;
00090     $user = "SYSTEM";
00091     $pass = "";
00092 
00093     if (isset($options['h']))
00094         $mapi = $options['h'];
00095 
00096     if (isset($options['u']) && isset($options['p'])) {
00097         $user = $options['u'];
00098         $pass = $options['p'];
00099     }
00100 
00101     $zarafaAdmin = listfolders_zarafa_admin_setup($mapi, $user, $pass);
00102     if (isset($zarafaAdmin['adminStore']) && isset($options['l'])) {
00103         listfolders_getlist($zarafaAdmin['adminStore'], $zarafaAdmin['session'], trim($options['l']));
00104     }
00105     else {
00106         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";
00107     }
00108 }
00109 
00110 function listfolders_zarafa_admin_setup ($mapi, $user, $pass) {
00111     $session = @mapi_logon_zarafa($user, $pass, $mapi);
00112 
00113     if (!$session) {
00114         echo "User '$user' could not login. The script will exit. Errorcode: 0x". sprintf("%x", mapi_last_hresult()) . "\n";
00115         exit(1);
00116     }
00117 
00118     $stores = @mapi_getmsgstorestable($session);
00119     $storeslist = @mapi_table_queryallrows($stores);
00120     $adminStore = @mapi_openmsgstore($session, $storeslist[0][PR_ENTRYID]);
00121 
00122     $zarafauserinfo = @mapi_zarafa_getuser_by_name($adminStore, $user);
00123     $admin = (isset($zarafauserinfo['admin']) && $zarafauserinfo['admin'])?true:false;
00124 
00125     if (!$stores || !$storeslist || !$adminStore || !$admin) {
00126         echo "There was error trying to log in as admin or retrieving admin info. The script will exit.\n";
00127         exit(1);
00128     }
00129 
00130     return array("session" => $session, "adminStore" => $adminStore);
00131 }
00132 
00133 
00134 function listfolders_getlist ($adminStore, $session, $user) {
00135     global $supported_classes;
00136 
00137     if (strtoupper($user) == 'SYSTEM') {
00138         // Find the public store store
00139         $storestables = @mapi_getmsgstorestable($session);
00140         $result = @mapi_last_hresult();
00141 
00142         if ($result == NOERROR){
00143             $rows = @mapi_table_queryallrows($storestables, array(PR_ENTRYID, PR_MDB_PROVIDER));
00144 
00145             foreach($rows as $row) {
00146                 if (isset($row[PR_MDB_PROVIDER]) && $row[PR_MDB_PROVIDER] == ZARAFA_STORE_PUBLIC_GUID) {
00147                     if (!isset($row[PR_ENTRYID])) {
00148                         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";
00149                         exit (1);
00150                     }
00151                     $entryid = $row[PR_ENTRYID];
00152                     break;
00153                 }
00154             }
00155         }
00156     }
00157     else
00158         $entryid = @mapi_msgstore_createentryid($adminStore, $user);
00159 
00160     $userStore = @mapi_openmsgstore($session, $entryid);
00161     $hresult = mapi_last_hresult();
00162 
00163     // Cache the store for later use
00164     if($hresult != NOERROR) {
00165         echo "Could not open store for '$user'. The script will exit.\n";
00166         exit (1);
00167     }
00168 
00169     $folder = @mapi_msgstore_openentry($userStore);
00170     $h_table = @mapi_folder_gethierarchytable($folder, CONVENIENT_DEPTH);
00171     $subfolders = @mapi_table_queryallrows($h_table, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_CONTAINER_CLASS, PR_SOURCE_KEY));
00172 
00173     echo "Available folders in store '$user':\n" . str_repeat("-", 50) . "\n";
00174     foreach($subfolders as $folder) {
00175         if (isset($folder[PR_CONTAINER_CLASS]) && array_key_exists($folder[PR_CONTAINER_CLASS], $supported_classes)) {
00176             echo "Folder name:\t". $folder[PR_DISPLAY_NAME] . "\n";
00177             echo "Folder ID:\t". bin2hex($folder[PR_SOURCE_KEY]) . "\n";
00178             echo "Type:\t\t". $supported_classes[$folder[PR_CONTAINER_CLASS]] . "\n";
00179             echo "\n";
00180         }
00181     }
00182 }
00183 
00184 ?>