Skip to content

Instantly share code, notes, and snippets.

@jelly
Created February 4, 2015 21:12
Show Gist options
  • Save jelly/0ed67279aae61a9dc261 to your computer and use it in GitHub Desktop.
Save jelly/0ed67279aae61a9dc261 to your computer and use it in GitHub Desktop.
Convert webaccess settings to webapp settings (not all settings are supported)
<?php
include('/usr/share/php/mapi/mapi.util.php');
include('/usr/share/php/mapi/mapidefs.php');
include('/usr/share/php/mapi/mapicode.php');
include('/usr/share/php/mapi/mapitags.php');
include('/usr/share/php/mapi/mapiguid.php');
if(count($argv) < 2) {
print "Usage: webaccess2webapp.php username <optional configuration file>\n";
exit(1);
}
$user = $argv[1];
if(count($argv) == 3) {
$config = $argv[2];
}
$session = mapi_logon_zarafa($user, '', 'file:///var/run/zarafa', null, null);
if(!$session) { print "Unable to open session\n"; exit(1); }
$msgstorestable = mapi_getmsgstorestable($session);
if(!$msgstorestable) { print "Unable to open message stores table\n"; exit(1); }
$msgstores = mapi_table_queryallrows($msgstorestable, array(PR_DEFAULT_STORE, PR_ENTRYID));
foreach ($msgstores as $row) {
if($row[PR_DEFAULT_STORE]) {
$storeentryid = $row[PR_ENTRYID];
}
}
if(!$storeentryid) { print "Can't find default store\n"; exit(1); }
$store = mapi_openmsgstore($session, $storeentryid);
if(!$store) { print "Unable to open default store\n"; exit(1); }
$stream = mapi_openpropertytostream($store, PR_EC_WEBACCESS_SETTINGS);
if ($stream === false) {
print "User has no webaccess settings";
exit(1);
}
$stat = mapi_stream_stat($stream);
mapi_stream_seek($stream, 0, STREAM_SEEK_SET);
$settings_string = '';
for($i=0;$i<$stat['cb'];$i+=1024){
$settings_string .= mapi_stream_read($stream, 1024);
}
// suppress php notice in case unserializing fails
$webac_settings = @unserialize($settings_string);
//print_r($webac_settings);
// Basic settings
$webapp_settings = json_decode('{"settings":{"zarafa":{"v1":{"main":{"language":"en_US.UTF-8","default_context":"mail","start_working_hour":540,"end_working_hour":1020,"week_start":1,"show_welcome":false},"contexts":{"mail":{},"calendar":{"default_zoom_level":30,"datepicker_show_busy":true}},"state":{"models":{"note":{"current_data_mode":0}},"contexts":{"mail":{"current_view":0,"current_view_mode":1}}}}}}}', true);
# CALENDAR
$webapp_short_settings = $webapp_settings['settings']['zarafa']['v1'];
$webac_short_settings = $webac_settings['settings'];
if(isset($webac_settings['settings']['calendar'])) {
$webapp_short_settings['contexts']['calendar']['default_reminder'] = $webac_short_settings['calendar']['reminder'];
$webapp_short_settings['contexts']['calendar']['default_reminder_time'] = $webac_short_settings['calendar']['reminder_minutes'];
$webapp_short_settings['main']['end_working_hour'] = $webac_short_settings['calendar']['workdayend'];
$webapp_short_settings['main']['start_working_hour'] = $webac_short_settings['calendar']['workdaystart'];
}
# GENERAL
if(isset($webac_settings['global']['language'])){
$webapp_short_settings['main']['language'] = $webac_settings['global']['language'];
}
# KEYBOARD SHORTCUTS
if(isset($webac_short_settings['global']['shortcuts']['enabled'])) {
$webapp_short_settings['main']['keycontrols_enabled'] = $webac_short_settings['global']['shortcuts']['enabled'];
}
if(isset($webac_short_settings['global']['previewpane'])){
switch($webac_short_settings['global']['previewpane']){
case 'right':
$webapp_short_settings['state']['contexts']['mail']['current_view_mode'] = 1;
case 'bottom':
$webapp_short_settings['state']['contexts']['mail']['current_view_mode'] = 2;
default:
$webapp_short_settings['state']['contexts']['mail']['current_view_mode'] = 0;
}
}
# MAIL
# read reciept: webapp "always/never/ask"
if(isset($webac_short_settings['global']['readreceipt_handling'])){
$webapp_short_settings['contexts']['mail']['readreceipt_handling'] = $webac_short_settings['global']['readreceipt_handling'];
}
if(isset($webac_short_settings['createmail']['mailformat'])){
//$webapp_short_settings['']['mail'][''] = $webac_short_settings['createmail']['mailformat'];
}
# mark mails as read after
if(isset($webac_short_settings['global']['mail_readflagtime'])){
$webapp_short_settings['contexts']['mail']['readflag_time'] = $webac_short_settings['global']['mail_readflagtime'];
}
# autosave email interval
if(isset($webac_short_settings['createmail']['autosave_interval'])){
$webapp_short_settings['contexts']['mail']['autosave_time'] = $webac_short_settings['createmail']['autosave_interval'] * 60;
}
if(isset($webac_short_settings['createmail']['autosave'])){
$webapp_short_settings['contexts']['mail']['autosave_enable'] = $webac_short_settings['createmail']['autosave'];
}
# EDITOR HTML or PLAIN
$webapp_short_settings['contexts']['mail']['dialogs']['mailcreate']['use_html_editor'] = $webac_short_settings['createmail']['mailformat'];
# SIGNATURES
if(isset($webac_short_settings['createmail']['signatures'])){
$ids = explode(';', $webac_short_settings['createmail']['signatures']['id_lookup']);
$webapp_short_settings['contexts']['mail']['signatures']['all'] = "";
$temp = array();
foreach($ids as $id) {
$webac_signature = $webac_short_settings['createmail']['signatures'][$id];
$temp[$id] = array('name' => $webac_signature['name'], 'content' => $webac_signature['content'], 'isHTML' => ($webac_signature['type'] === 'plain' ? 'false' : 'true'));
}
$webapp_short_settings['contexts']['mail']['signatures']['all'] = $temp;
if(isset($webac_short_settings['createmail']['signatures']['select_sig_newmsg'])) {
$webapp_short_settings['contexts']['mail']['signatures']['new_message'] = $webac_short_settings['createmail']['signatures']['select_sig_newmsg'];
}
if(isset($webac_short_settings['createmail']['signatures']['select_sig_replyfwd'])) {
$webapp_short_settings['contexts']['mail']['signatures']['replyforward_message'] = $webac_short_settings['createmail']['signatures']['select_sig_replyfwd'];
}
}
$new_array_settings = array('settings' => array('zarafa' => array('v1' => $webapp_short_settings)));
//print_r($new_array_settings);
$stream = mapi_openproperty($store, PR_EC_WEBACCESS_SETTINGS_JSON, IID_IStream, 0, MAPI_CREATE | MAPI_MODIFY);
$new_settings = json_encode($new_array_settings);
mapi_stream_setsize($stream, strlen($new_settings));
mapi_stream_write($stream, $new_settings);
mapi_stream_commit($stream);
mapi_savechanges($store);
print "Settings converted from webaccess to webapp for user $user\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment