Documentation is available at viewer.php
- <?php
- /* Processing and output, called by the "component" interface class */
- class midgard_admin_sitewizard_viewer {
- var $_debug_prefix;
- var $_form_prefix;
- var $_config;
- var $_topic;
- var $_current_view;
- var $_views;
- var $_templates;
- var $_styles;
- var $_host;
- var $_style;
- var $_sitegroup;
- var $_datamanager;
- var $_schema;
- var $_message;
- var $errcode;
- var $errstr;
- function midgard_admin_sitewizard_viewer($topic, $config)
- {
- global $midgard;
- $this->_debug_prefix = "midgard.admin.sitewizard viewer::";
- $this->_form_prefix = "midgard_admin_sitewizard_";
- $this->_config = $config;
- $this->_topic = $topic;
- $this->_current_view = false;
- $this->_host = null;
- $this->_style = null;
- $this->_sitegroup = null;
- $this->_datamanager = null;
- $this->_schema = null;
- $this->errcode = null;
- $this->errstr = null;
- $this->_message = null;
- $GLOBALS["midgard_admin_sitewizard_errstr"] = "";
- // get l10n libraries
- $i18n =& $GLOBALS["midcom"]->get_service("i18n");
- $GLOBALS["view_l10n"] = $i18n->get_l10n("midgard.admin.sitewizard");
- $GLOBALS["view_l10n_midcom"] = $i18n->get_l10n("midcom");
- // Cache never
- $GLOBALS["midcom"]->cache->content->no_cache();
- $this->_views = array(
- "select_sitegroup" => array(
- "name" => $GLOBALS["view_l10n"]->get("select organization"),
- "visible" => true,
- ),
- "create_host" => array(
- "name" => $GLOBALS["view_l10n"]->get("create website"),
- "visible" => true,
- ),
- "configure_host" => array(
- "name" => $GLOBALS["view_l10n"]->get("configure website"),
- "visible" => true,
- ),
- "select_style" => array(
- "name" => $GLOBALS["view_l10n"]->get("setup style"),
- "visible" => true,
- ),
- "configure_style" => array(
- "name" => $GLOBALS["view_l10n"]->get("configure style"),
- "visible" => false,
- ),
- "finish" => array(
- "name" => $GLOBALS["view_l10n"]->get("finish"),
- "visible" => true,
- ),
- );
- $this->_templates = array();
- // Root pages of different template apps
- $root_pages = mgd_list_pages(0);
- if ($root_pages)
- {
- while ($root_pages->fetch())
- {
- if (substr($root_pages->name,0,9) == "template_")
- {
- $template = mgd_get_page($root_pages->id);
- $this->_templates[$template->name] = $template;
- }
- }
- }
- $openpsa = mgd_get_object_by_guid("d29ac1bb81da41d7cebf7fc1d06e7746");
- if ($openpsa)
- {
- $this->_templates["openpsa"] = $openpsa;
- }
- $this->_styles = array();
- // Template styles
- $styles = mgd_list_styles();
- if ($styles)
- {
- while ($styles->fetch())
- {
- if (substr($styles->name,0,9) == "template_")
- {
- $this->_styles[$styles->name] = $styles;
- }
- }
- }
- }
- function _getSitegroup($id) {
- $this->_sitegroup = mgd_get_sitegroup($id);
- if ($this->_sitegroup) {
- return true;
- }
- return false;
- }
- function _createSitegroup($sitegroup_name,$admin_username,$admin_password,$quota=false) {
- $sitegroup = mgd_get_sitegroup();
- $sitegroup->name = $sitegroup_name;
- $sitegroup_created = $sitegroup->create();
- if ($sitegroup_created) {
- $sitegroup = mgd_get_sitegroup($sitegroup_created);
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("created sitegroup \"%s\" (#%d) successfully"),$sitegroup->name,$sitegroup->id));
- if ($_MIDGARD["config"]["quota"] && $quota) {
- $quota_kb = $quota * 1000;
- $quota_created = mgd_create_quota($sitegroup->id, 'wholesg', '', 0, $quota_kb);
- if ($quota_created) {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("quota set to %dKB for sitegroup \"%s\""),$quota,$sitegroup->name));
- }
- }
- $admin_group = mgd_get_group(); $admin_group->name = sprintf($GLOBALS["view_l10n"]->get("%s administrator group"),$sitegroup->name); $admin_group_created = $admin_group->create(); if ($admin_group_created) { $admin_group = mgd_get_group($admin_group_created);
- //$admin_group->setsitegroup($sitegroup->id);
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("created group \"%s\" (#%d, SG%d) successfully"),$admin_group->name,$admin_group->id,$admin_group->sitegroup));
- $admin_user = mgd_get_person(); $admin_user->firstname = $admin_username; $admin_user_created = $admin_user->create(); if ($admin_user_created) {
- $admin_user = mgd_get_person($admin_user_created);
- //$admin_user->setsitegroup($sitegroup->id);
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("created user \"%s\" (#%d, SG%d) successfully"),$admin_user->name,$admin_user->id,$admin_user->sitegroup)); $account_created = mgd_update_password_plain($admin_user->id,$admin_username,$admin_password);
- if ($account_created) {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("set username \"%s\" for user \"%s\" successfully"),$admin_username,$admin_user->name));
- if (version_compare(mgd_version(),"1.7","<"))
- {
- // Totally ugly hack here. This has been fixed in Midgard 1.7 by fixing mgd_create_member() in Midgard Core
- // See the definition of auth_hack_session in snippet /Nemein_Authentication/Functions
- global $auth_userdata_pad, $auth_conf;
- error_reporting(E_WARNING);
- $auth_data = auth_hack_session();
- $username = OTP_decrypt($auth_data["username"],$auth_userdata_pad);
- $password = OTP_decrypt($auth_data["passwd"],$auth_userdata_pad);
- error_reporting(E_ALL);
- // Change the user and group to SGn so we can see them
- $admin_group->setsitegroup($sitegroup->id);
- $admin_user->setsitegroup($sitegroup->id);
- // Reauthenticate
- mgd_auth_midgard($username."*".$sitegroup->name,$password,0);
- $midgard = mgd_get_midgard();
- if ($midgard->sitegroup == $sitegroup->id) {
- $this->_addMessage("Changed authentication from ROOT to sitegroup #".$midgard->sitegroup);
- } else {
- $this->_addMessage("Changing authentication session from ROOT to sitegroup #".$sitegroup->id." failed, reason ".mgd_errstr());
- }
- // End the ugly hack
- }
- $membership_created = mgd_create_member($admin_user->id,$admin_group->id,sprintf($GLOBALS["view_l10n"]->get("created by %s on %s"),$GLOBALS["view_l10n"]->get("midgard.admin.sitewizard"),strftime("%x %X",time())));
- $membership_error = mgd_errstr();
- if ($membership_created) {
- $membership = mgd_get_member($membership_created);
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("added user \"%s\" successfully to group \"%s\""),$admin_user->name,$admin_group->name));
- $admin_group->setsitegroup($sitegroup->id);
- $sitegroup->admingroup = $admin_group->id;
- $admin_user->setsitegroup($sitegroup->id);
- $membership->setsitegroup($sitegroup->id);
- $admin_group_assigned = $sitegroup->update();
- if ($admin_group_assigned) {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("assigned group \"%s\" as organization \"%s\" administrators"),$admin_group->name,$sitegroup->name));
- $sitegroup = mgd_get_sitegroup($sitegroup->id);
- // All done, redirect
- return $sitegroup->id;
- } else {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed assign group as organization administrators, reason %s"),mgd_errstr()),"crit");
- midcom_helper_purge_object($membership->guid());
- midcom_helper_purge_object($admin_user->guid());
- midcom_helper_purge_object($admin_group->guid());
- midcom_helper_purge_object($sitegroup->guid());
- return false;
- }
- } else {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to add user \"%s\" to group \"%s\", reason %s"),$admin_user->name,$admin_group->name,$membership_error),"crit");
- midcom_helper_purge_object($admin_user->guid());
- midcom_helper_purge_object($admin_group->guid());
- midcom_helper_purge_object($sitegroup->guid());
- }
- } else {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create account for admin user, reason %s"),mgd_errstr()),"crit");
- midcom_helper_purge_object($admin_user->guid());
- midcom_helper_purge_object($admin_group->guid());
- midcom_helper_purge_object($sitegroup->guid());
- return false;
- }
- } else {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create admin user, reason %s"),mgd_errstr()),"crit");
- midcom_helper_purge_object($admin_group->guid());
- midcom_helper_purge_object($sitegroup->guid());
- return false;
- }
- } else {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create admin group, reason %s"),mgd_errstr()),"crit");
- midcom_helper_purge_object($sitegroup->guid());
- return false;
- }
- } else {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create organization, reason %s"),mgd_errstr()),"crit");
- return false;
- }
- }
- function _getHost($id) {
- $host = mgd_get_host($id);
- if ($host->sitegroup == $this->_sitegroup->id) {
- $this->_host = $host;
- $style = mgd_get_style($host->style);
- $this->_style = $style;
- return true;
- }
- return false;
- }
- function _addMessage($message, $type = "info") {
- $this->_message[] = array(
- "message" => $message,
- "type" => $type,
- );
- }
- function can_handle($argc, $argv) {
- debug_push($this->_debug_prefix . "can_handle");
- $this->_argv = $argv;
- // Autoload CSS
- $GLOBALS["midcom"]->add_link_head(array(
- 'rel' => 'stylesheet',
- 'type' => 'text/css',
- 'href' => MIDCOM_STATIC_URL."/midgard.admin.sitewizard/sitewizard.css",
- ));
- if ($argc == 0) {
- // Welcome page, ask for sitegroup
- return true;
- } elseif ($argc == 1) {
- // First argument is sitegroup ID
- if ($this->_getSitegroup($argv[0])) {
- // Sitegroup chosen, ask for host
- return true;
- } else {
- return false;
- // TODO: redirect to selector?
- }
- } elseif ($argc == 2) {
- // Second argument is host ID
- $this->_getSitegroup($argv[0]);
- $host_found = $this->_getHost($argv[1]);
- if ($host_found) {
- $this->_current_view = "finish";
- return true;
- }
- } elseif ($argc == 3) {
- // Third argument is selected view
- // Host chosen, ask for style
- $this->_getSitegroup($argv[0]);
- $host_found = $this->_getHost($argv[1]);
- if ($host_found) {
- $GLOBALS["midgard_admin_sitewizard_nap_activeid"] = $this->_host->id;
- if ($argv[2] == "configure_host") {
- $host_schema = mgd_get_page_element_by_name($this->_host->root,"__schema");
- if ($host_schema) {
- // Load schema
- eval ("\$schema = Array(\n" . $host_schema->value . "\n);");
- if (is_array($schema)) {
- $this->_schema = $schema;
- $this->_current_view = "configure_host";
- $this->_views["configure_host"]["visible"] = true;
- return true;
- } else {
- // TODO: Schema problem, complain
- }
- }
- } elseif ($argv[2] == "select_style") {
- $this->_current_view = "select_style";
- return true;
- } elseif ($argv[2] == "configure_style") {
- if ($this->_style) {
- $style_schema = mgd_get_element_by_name($this->_style->up,"__schema");
- if ($style_schema) {
- // Load schema
- eval ("\$schema = Array(\n" . $style_schema->value . "\n);");
- if (is_array($schema)) {
- $this->_schema = $schema;
- $this->_current_view = "configure_style";
- $this->_views["configure_style"]["visible"] = true;
- return true;
- } else {
- // TODO: Schema problem, complain
- }
- }
- }
- }
- }
- return false;
- }
- debug_pop();
- return false;
- }
- function handle() {
- debug_push($this->_debug_prefix . "handle");
- // Walk through the wizard steps to see where we are
- if (!$this->_sitegroup) {
- // Sitegroup hasn't been created or selected yet
- if (isset($_POST[$this->_form_prefix."process"])) {
- // User pressed "Next", process the form
- if ($_POST[$this->_form_prefix."sitegroup_action"] == "create_new") {
- if ($_MIDGARD["config"]["quota"] && isset($_POST[$this->_form_prefix."sitegroup"]["quota"])) {
- $sitegroup_created = $this->_createSitegroup($_POST[$this->_form_prefix."sitegroup"]["name"],$_POST[$this->_form_prefix."sitegroup"]["admin"],$_POST[$this->_form_prefix."sitegroup"]["password"],$_POST[$this->_form_prefix."sitegroup"]["quota"]);
- } else {
- $sitegroup_created = $this->_createSitegroup($_POST[$this->_form_prefix."sitegroup"]["name"],$_POST[$this->_form_prefix."sitegroup"]["admin"],$_POST[$this->_form_prefix."sitegroup"]["password"]);
- }
- if ($sitegroup_created) {
- // Successfully created, redirect to host creation
- $GLOBALS["midcom"]->relocate($sitegroup_created."/");
- }
- } elseif ($_POST[$this->_form_prefix."sitegroup_action"] == "use_existing") {
- // Existing sitegroup selected
- if ($this->_getSitegroup($_POST[$this->_form_prefix."sitegroup"]["id"])) {
- // Redirect to host setup for sitegroup
- $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/");
- } else {
- // TODO: raise error
- return false;
- }
- }
- }
- // Sitegroup creation and selection screen
- $this->_current_view = "select_sitegroup";
- } elseif (!$this->_host) {
- if (isset($_POST[$this->_form_prefix."process"])) {
- // User pressed "Next", process the form
- $host = new midgard_admin_sitewizard_host(false,$this->_sitegroup->id);
- $host->name = $_POST[$this->_form_prefix."host"]["name"];
- $host->root = $this->_templates["template_midcom"]->id;
- if (isset($_POST[$this->_form_prefix."advanced"]) && $_POST[$this->_form_prefix."advanced"] == "on") {
- // Advanced mode
- $host->port = $_POST[$this->_form_prefix."host"]["port"];
- $host->prefix = $_POST[$this->_form_prefix."host"]["prefix"];
- }
- $host_created = $host->create();
- if ($host_created) {
- $this->_getHost($host_created);
- // Initialize
- $host_init = mgd_get_page_element_by_name($host->root,"__init");
- if ($host_init) {
- $GLOBALS["midgard_admin_sitewizard_init_errstr"] = "";
- eval ('?>' . $host_init->value . '<?');
- if (function_exists('template_initialize')) {
- // Function is defined by template
- $initialized = template_initialize($this->_host,$this->_sitegroup->id);
- if (!$initialized) {
- $this->_addMessage($GLOBALS["midgard_admin_sitewizard_init_errstr"],"crit");
- midcom_helper_purge_object($this->_host->guid());
- $this->_current_view = "create_host";
- return true;
- }
- }
- }
- // Check where to go next
- $host_schema = mgd_get_page_element_by_name($this->_host->root,"__schema");
- if ($host_schema) {
- // Website is configurable
- $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$host_created."/configure_host.html");
- } else {
- // Go to select style
- $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$host_created."/select_style.html");
- }
- } else {
- $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create website, reason %s"),mgd_errstr()),"crit");
- }
- }
- // Host creation screen
- $this->_current_view = "create_host";
- } else {
- // We have SG and host, handle regular config situations
- if ($this->_current_view == "configure_host") {
- $this->_datamanager = new midcom_helper_datamanager($this->_schema);
- // Figure out storage location
- $root_page = mgd_get_page($this->_host->root);
- $storage_location = $root_page->parameter("midgard.admin.sitewizard","config_location");
- if ($storage_location == "sitegroup") {
- $this->_datamanager->init($this->_sitegroup);
- } else {
- // Default to host, as in midcom-template
- $this->_datamanager->init($this->_host);
- }
- switch ($this->_datamanager->process_form()) {
- case MIDCOM_DATAMGR_SAVED:
- $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$this->_host->id."/select_style.html");
- case MIDCOM_DATAMGR_EDITING:
- case MIDCOM_DATAMGR_CANCELLED:
- // We stay here whatever happens here, at least as long as
- // there is no fatal error.
- break;
- case MIDCOM_DATAMGR_FAILED:
- $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
- $this->errcode = MIDCOM_ERRCRIT;
- debug_pop();
- return false;
- }
- } elseif ($this->_current_view == "select_style") {
- if (isset($_POST[$this->_form_prefix."process"])) {
- // User pressed "Next", process the form
- $selected_style = $_POST[$this->_form_prefix."select_style"];
- if (array_key_exists($selected_style,$this->_styles)) {
- $substyle_created = mgd_create_style($this->_styles[$selected_style]->id,sprintf($GLOBALS["view_l10n"]->get("style \"%s\" for \"%s\""),$selected_style,$this->_sitegroup->name),$this->_sitegroup->admingroup);
- if ($substyle_created) {
- $substyle = mgd_get_style($substyle_created);
- $substyle->setsitegroup($this->_sitegroup->id);
- $this->_host->style = $substyle->id;
- if ($this->_host->update()) {
- // Check where to go next
- $style_schema = mgd_get_element_by_name($this->_styles[$selected_style]->id,"__schema");
- if ($style_schema) {
- // Style is configurable
- $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$this->_host->id."/configure_style.html");
- } else {
- // All done
- $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$this->_host->id."/");