PHP script to manage webapp settings
-
We want our users to start using the webapp but don’t want them to go through all the settings before using it. It would really help making it as easy as possible for them when switching from the Outlook client to the webapp. It still amazes me that there is little to no tooling available to manage the webapp/deskapp. Are there plans for an administration portal of some sort to manage these settings?
We have a few default settings we want to apply for all users (like default font, hotkeys, theme etc). I feel most comfortable using PHP when scripting. I was wondering, is there a PHP example on how to apply default webapp settings for multiple users?
-
Well, I did some digging around myself so here’s what I came up with, might be useful for someone else. It’s a little rough around the edges but I think the idea is clear:
<?php include('/usr/share/kopano/php/mapi/mapi.util.php'); include('/usr/share/kopano/php/mapi/mapidefs.php'); include('/usr/share/kopano/php/mapi/mapicode.php'); include('/usr/share/kopano/php/mapi/mapitags.php'); include('/usr/share/kopano/php/mapi/mapiguid.php'); class WebAppSettings { public $settingsfound; private $webappsettingsarray; private $storehandle; function __construct($storehandle){ $this->storehandle = $storehandle; $storeprops = mapi_getprops($this->storehandle, array(PR_EC_WEBACCESS_SETTINGS_JSON)); $firstkey = key($storeprops); $loadedsettingsarray = json_decode($storeprops[$firstkey], true); if(array_key_exists(PR_EC_WEBACCESS_SETTINGS_JSON, $storeprops)){ $this->webappsettingsarray = $loadedsettingsarray; $this->settingsfound = true; } else{ //no settings found, set default settings $this->set_default(); $this->settingsfound = false; } } public function set_default(){ //plugin settings $this->webappsettingsarray["settings"]["zarafa"]["v1"]["plugins"]["spellchecker"]["spellchecker_languages"] = "nl_NL"; //calendar settings $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["calendar"]["default_zoom_level"] = 30; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["calendar"]["datepicker_show_busy"] = true; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["hierarchy"]["show_default_favorites"] = false; //mail settings $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["close_on_respond"] = true; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["dialogs"]["mailcreate"]["use_html_editor"] = true; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["always_request_readreceipt"] = false; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["autosave_time"] = 60; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["readreceipt_handling"] = "ask"; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["readflag_time_enable"] = true; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["readflag_time"] = 3; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["enable_live_scroll"] = true; //window state settings $this->webappsettingsarray["settings"]["zarafa"]["v1"]["state"]["sidebars"]["todaybar"]["collapsed"] = true; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["state"]["contexts"]["mail"]["current_view_mode"] = 1; //general settings $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["language"] = "nl_NL.UTF-8"; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["default_context"] = "mail"; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["active_theme"] = "basic"; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["start_working_hour"] = 510; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["end_working_hour"] = 1020; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["week_start"] = 1; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["working_days"][0] = 1; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["working_days"][1] = 5; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["working_days"][2] = 2; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["working_days"][3] = 3; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["working_days"][4] = 4; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["show_welcome"] = false; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["keycontrols"] = "basic"; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["default_font"] = "verdana,geneva"; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["base_content_layer"] = "tabs"; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["page_size"] = 50; } public function clear_settings(){ unset($this->webappsettingsarray["settings"]); } public function set_language($language){ $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["language"] = $language; } public function set_startupfolder($startupfolder){ $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["default_context"] = $startupfolder; } public function set_theme($theme){ $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["active_theme"] = $theme; } public function set_firstdayoftheweek($firstdayoftheweek){ $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["week_start"] = $firstdayoftheweek; } public function set_startofworkday($startofworkday){ $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["start_working_hour"] = $startofworkday; } public function set_endofworkday($endofworkday){ $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["end_working_hour"] = $endofworkday; } public function set_calendarresolution($calendarresolution){ $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["calendar"]["default_zoom_level"] = $calendarresolution; } public function set_workingdays($workingdays){ $daycount = 1; for($i=0;$i==6;$i++){ if(isset($workingdays[$i])){ $this->webappsettingsarray["settings"]["zarafa"]["v1"]["main"]["working_days"][$i] = $daycount; $daycount++; } } } public function getJSON(){ return json_encode($this->webappsettingsarray); } public function getArray(){ return $this->webappsettingsarray; } public function save(){ $props = Array(PR_EC_WEBACCESS_SETTINGS_JSON => $this->getJSON()); // Set props mapi_setprops($this->storehandle, $props); // Save changes mapi_savechanges($this->storehandle); } public function set_signature($signaturename, $signature){ //generate id for new signature: $signatureid = str_replace(".","",sprintf('%.3f', microtime(true))); $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["signatures"]["all"][$signatureid]["name"] = $signaturename; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["signatures"]["all"][$signatureid]["content"] = $signature; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["signatures"]["all"][$signatureid]["isHTML"] = true; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["signatures"]["new_message"] = $signatureid; $this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["signatures"]["replyforward_message"] = $signatureid; } public function delete_property(){ $props = Array(PR_EC_WEBACCESS_SETTINGS_JSON => null); // Set props mapi_setprops($this->storehandle, $props); // Save changes mapi_savechanges($this->storehandle); } public function delete_signatures(){ unset($this->webappsettingsarray["settings"]["zarafa"]["v1"]["contexts"]["mail"]["signatures"]); } } //login to the server $session = mapi_logon_zarafa('username','password',"https://yourserver.yourdomain.yoursuffix:237/zarafa"); //get a store handle $msgstorestable = mapi_getmsgstorestable($session); $msgstores = mapi_table_queryallrows($msgstorestable); $userstore = mapi_openmsgstore($session, $msgstores[0][PR_ENTRYID]); $webappsettings = new WebAppSettings($userstore); //echo current settings echo "<pre>"; print_r($webappsettings->getArray()); echo "</pre>"; //delete existing signatures and set a new one $webappsettings->delete_signatures(); $webappsettings->set_signature("Default signature", "Greetings,<br />Name") $webappsettings->save(); //set default settings $webappsettings->set_default(); $webappsettings->save(); //change theme $webappsettings->set_theme("dark"); $webappsettings->save(); ?>
-
Hi @kopiko ,
congrats to the nice script. In case you want to upload it somewhere you could also add a link to https://stash.z-hub.io/projects/COM/repos/projects-and-resources/browse so others could find it more easily.
I actually meant to write you earlier and propose a different route to solve the problem you wanted to solve. Just for completeness this is the route I would have suggested.
At https://stash.kopano.io/projects/KSC/repos/webapp-tools/browse you can find a small python script to read the json settings object for a given user and also write those.
So you could download the settings object for a template user, modify it to you needs and just push it back up to a user of you choice.
This could even be automated with a small wrapper script in/etc/kopano/userscripts
(have a look at00createstore
for an example).