source: trunk/zpush/backend/zarafa/listfolders.php @ 7589

Revision 7589, 7.1 KB checked in by douglas, 11 years ago (diff)

Ticket #3209 - Integrar módulo de sincronização Z-push ao Expresso

  • Property svn:executable set to *
Line 
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 - 2012 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
48define("PHP_MAPI_PATH", "/usr/share/php/mapi/");
49define('MAPI_SERVER', 'file:///var/run/zarafa');
50
51$supported_classes = array (
52    "IPF.Note"          => "SYNC_FOLDER_TYPE_USER_MAIL",
53    "IPF.Task"          => "SYNC_FOLDER_TYPE_USER_TASK",
54    "IPF.Appointment"   => "SYNC_FOLDER_TYPE_USER_APPOINTMENT",
55    "IPF.Contact"       => "SYNC_FOLDER_TYPE_USER_CONTACT",
56    "IPF.StickyNote"    => "SYNC_FOLDER_TYPE_USER_NOTE"
57);
58
59main();
60
61function main() {
62    listfolders_configure();
63    listfolders_handle();
64}
65
66function listfolders_configure() {
67
68    if (!isset($_SERVER["TERM"]) || !isset($_SERVER["LOGNAME"])) {
69        echo "This script should not be called in a browser.\n";
70        exit(1);
71    }
72
73    if (!function_exists("getopt")) {
74        echo "PHP Function 'getopt()' not found. Please check your PHP version and settings.\n";
75        exit(1);
76    }
77
78    require(PHP_MAPI_PATH.'mapi.util.php');
79    require(PHP_MAPI_PATH.'mapidefs.php');
80    require(PHP_MAPI_PATH.'mapicode.php');
81    require(PHP_MAPI_PATH.'mapitags.php');
82    require(PHP_MAPI_PATH.'mapiguid.php');
83}
84
85function listfolders_handle() {
86    $shortoptions = "l:h:u:p:";
87    $options = getopt($shortoptions);
88
89    $mapi = MAPI_SERVER;
90    $user = "SYSTEM";
91    $pass = "";
92
93    if (isset($options['h']))
94        $mapi = $options['h'];
95
96    if (isset($options['u']) && isset($options['p'])) {
97        $user = $options['u'];
98        $pass = $options['p'];
99    }
100
101    $zarafaAdmin = listfolders_zarafa_admin_setup($mapi, $user, $pass);
102    if (isset($zarafaAdmin['adminStore']) && isset($options['l'])) {
103        listfolders_getlist($zarafaAdmin['adminStore'], $zarafaAdmin['session'], trim($options['l']));
104    }
105    else {
106        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";
107    }
108}
109
110function listfolders_zarafa_admin_setup ($mapi, $user, $pass) {
111    $session = @mapi_logon_zarafa($user, $pass, $mapi);
112
113    if (!$session) {
114        echo "User '$user' could not login. The script will exit. Errorcode: 0x". sprintf("%x", mapi_last_hresult()) . "\n";
115        exit(1);
116    }
117
118    $stores = @mapi_getmsgstorestable($session);
119    $storeslist = @mapi_table_queryallrows($stores);
120    $adminStore = @mapi_openmsgstore($session, $storeslist[0][PR_ENTRYID]);
121
122    $zarafauserinfo = @mapi_zarafa_getuser_by_name($adminStore, $user);
123    $admin = (isset($zarafauserinfo['admin']) && $zarafauserinfo['admin'])?true:false;
124
125    if (!$stores || !$storeslist || !$adminStore || !$admin) {
126        echo "There was error trying to log in as admin or retrieving admin info. The script will exit.\n";
127        exit(1);
128    }
129
130    return array("session" => $session, "adminStore" => $adminStore);
131}
132
133
134function listfolders_getlist ($adminStore, $session, $user) {
135    global $supported_classes;
136
137    if (strtoupper($user) == 'SYSTEM') {
138        // Find the public store store
139        $storestables = @mapi_getmsgstorestable($session);
140        $result = @mapi_last_hresult();
141
142        if ($result == NOERROR){
143            $rows = @mapi_table_queryallrows($storestables, array(PR_ENTRYID, PR_MDB_PROVIDER));
144
145            foreach($rows as $row) {
146                if (isset($row[PR_MDB_PROVIDER]) && $row[PR_MDB_PROVIDER] == ZARAFA_STORE_PUBLIC_GUID) {
147                    if (!isset($row[PR_ENTRYID])) {
148                        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";
149                        exit (1);
150                    }
151                    $entryid = $row[PR_ENTRYID];
152                    break;
153                }
154            }
155        }
156    }
157    else
158        $entryid = @mapi_msgstore_createentryid($adminStore, $user);
159
160    $userStore = @mapi_openmsgstore($session, $entryid);
161    $hresult = mapi_last_hresult();
162
163    // Cache the store for later use
164    if($hresult != NOERROR) {
165        echo "Could not open store for '$user'. The script will exit.\n";
166        exit (1);
167    }
168
169    $folder = @mapi_msgstore_openentry($userStore);
170    $h_table = @mapi_folder_gethierarchytable($folder, CONVENIENT_DEPTH);
171    $subfolders = @mapi_table_queryallrows($h_table, array(PR_ENTRYID, PR_DISPLAY_NAME, PR_CONTAINER_CLASS, PR_SOURCE_KEY));
172
173    echo "Available folders in store '$user':\n" . str_repeat("-", 50) . "\n";
174    foreach($subfolders as $folder) {
175        if (isset($folder[PR_CONTAINER_CLASS]) && array_key_exists($folder[PR_CONTAINER_CLASS], $supported_classes)) {
176            echo "Folder name:\t". $folder[PR_DISPLAY_NAME] . "\n";
177            echo "Folder ID:\t". bin2hex($folder[PR_SOURCE_KEY]) . "\n";
178            echo "Type:\t\t". $supported_classes[$folder[PR_CONTAINER_CLASS]] . "\n";
179            echo "\n";
180        }
181    }
182}
183
184?>
Note: See TracBrowser for help on using the repository browser.