Source for file viewer.php

Documentation is available at viewer.php

  1. <?php
  2.  
  3. /* Processing and output, called by the "component" interface class */
  4.  
  5. class midgard_admin_sitewizard_viewer {
  6.  
  7. var $_debug_prefix;
  8. var $_form_prefix;
  9.  
  10. var $_config;
  11. var $_topic;
  12. var $_current_view;
  13. var $_views;
  14. var $_templates;
  15. var $_styles;
  16. var $_host;
  17. var $_style;
  18. var $_sitegroup;
  19. var $_datamanager;
  20. var $_schema;
  21. var $_message;
  22. var $errcode;
  23. var $errstr;
  24.  
  25. function midgard_admin_sitewizard_viewer($topic, $config)
  26. {
  27.  
  28. global $midgard;
  29. $this->_debug_prefix = "midgard.admin.sitewizard viewer::";
  30. $this->_form_prefix = "midgard_admin_sitewizard_";
  31.  
  32. $this->_config = $config;
  33. $this->_topic = $topic;
  34. $this->_current_view = false;
  35. $this->_host = null;
  36. $this->_style = null;
  37. $this->_sitegroup = null;
  38. $this->_datamanager = null;
  39. $this->_schema = null;
  40. $this->errcode = null;
  41. $this->errstr = null;
  42. $this->_message = null;
  43.  
  44. $GLOBALS["midgard_admin_sitewizard_errstr"] = "";
  45.  
  46. // get l10n libraries
  47. $i18n =& $GLOBALS["midcom"]->get_service("i18n");
  48. $GLOBALS["view_l10n"] = $i18n->get_l10n("midgard.admin.sitewizard");
  49. $GLOBALS["view_l10n_midcom"] = $i18n->get_l10n("midcom");
  50. // Cache never
  51. $GLOBALS["midcom"]->cache->content->no_cache();
  52.  
  53. $this->_views = array(
  54. "select_sitegroup" => array(
  55. "name" => $GLOBALS["view_l10n"]->get("select organization"),
  56. "visible" => true,
  57. ),
  58. "create_host" => array(
  59. "name" => $GLOBALS["view_l10n"]->get("create website"),
  60. "visible" => true,
  61. ),
  62. "configure_host" => array(
  63. "name" => $GLOBALS["view_l10n"]->get("configure website"),
  64. "visible" => true,
  65. ),
  66. "select_style" => array(
  67. "name" => $GLOBALS["view_l10n"]->get("setup style"),
  68. "visible" => true,
  69. ),
  70. "configure_style" => array(
  71. "name" => $GLOBALS["view_l10n"]->get("configure style"),
  72. "visible" => false,
  73. ),
  74. "finish" => array(
  75. "name" => $GLOBALS["view_l10n"]->get("finish"),
  76. "visible" => true,
  77. ),
  78. );
  79. $this->_templates = array();
  80. // Root pages of different template apps
  81. $root_pages = mgd_list_pages(0);
  82. if ($root_pages)
  83. {
  84. while ($root_pages->fetch())
  85. {
  86. if (substr($root_pages->name,0,9) == "template_")
  87. {
  88. $template = mgd_get_page($root_pages->id);
  89. $this->_templates[$template->name] = $template;
  90. }
  91. }
  92. }
  93. $openpsa = mgd_get_object_by_guid("d29ac1bb81da41d7cebf7fc1d06e7746");
  94. if ($openpsa)
  95. {
  96. $this->_templates["openpsa"] = $openpsa;
  97. }
  98. $this->_styles = array();
  99. // Template styles
  100. $styles = mgd_list_styles();
  101. if ($styles)
  102. {
  103. while ($styles->fetch())
  104. {
  105. if (substr($styles->name,0,9) == "template_")
  106. {
  107. $this->_styles[$styles->name] = $styles;
  108. }
  109. }
  110. }
  111.  
  112. }
  113. function _getSitegroup($id) {
  114. $this->_sitegroup = mgd_get_sitegroup($id);
  115. if ($this->_sitegroup) {
  116. return true;
  117. }
  118. return false;
  119. }
  120. function _createSitegroup($sitegroup_name,$admin_username,$admin_password,$quota=false) {
  121. $sitegroup = mgd_get_sitegroup();
  122. $sitegroup->name = $sitegroup_name;
  123. $sitegroup_created = $sitegroup->create();
  124. if ($sitegroup_created) {
  125.  
  126. $sitegroup = mgd_get_sitegroup($sitegroup_created);
  127. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("created sitegroup \"%s\" (#%d) successfully"),$sitegroup->name,$sitegroup->id));
  128.  
  129. if ($_MIDGARD["config"]["quota"] && $quota) {
  130. $quota_kb = $quota * 1000;
  131. $quota_created = mgd_create_quota($sitegroup->id, 'wholesg', '', 0, $quota_kb);
  132. if ($quota_created) {
  133. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("quota set to %dKB for sitegroup \"%s\""),$quota,$sitegroup->name));
  134. }
  135. }
  136. $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);
  137. //$admin_group->setsitegroup($sitegroup->id);
  138. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("created group \"%s\" (#%d, SG%d) successfully"),$admin_group->name,$admin_group->id,$admin_group->sitegroup));
  139. $admin_user = mgd_get_person(); $admin_user->firstname = $admin_username; $admin_user_created = $admin_user->create(); if ($admin_user_created) {
  140. $admin_user = mgd_get_person($admin_user_created);
  141. //$admin_user->setsitegroup($sitegroup->id);
  142. $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);
  143. if ($account_created) {
  144. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("set username \"%s\" for user \"%s\" successfully"),$admin_username,$admin_user->name));
  145. if (version_compare(mgd_version(),"1.7","<"))
  146. {
  147. // Totally ugly hack here. This has been fixed in Midgard 1.7 by fixing mgd_create_member() in Midgard Core
  148. // See the definition of auth_hack_session in snippet /Nemein_Authentication/Functions
  149. global $auth_userdata_pad, $auth_conf;
  150. error_reporting(E_WARNING);
  151. $auth_data = auth_hack_session();
  152. $username = OTP_decrypt($auth_data["username"],$auth_userdata_pad);
  153. $password = OTP_decrypt($auth_data["passwd"],$auth_userdata_pad);
  154. error_reporting(E_ALL);
  155. // Change the user and group to SGn so we can see them
  156. $admin_group->setsitegroup($sitegroup->id);
  157. $admin_user->setsitegroup($sitegroup->id);
  158. // Reauthenticate
  159. mgd_auth_midgard($username."*".$sitegroup->name,$password,0);
  160. $midgard = mgd_get_midgard();
  161. if ($midgard->sitegroup == $sitegroup->id) {
  162. $this->_addMessage("Changed authentication from ROOT to sitegroup #".$midgard->sitegroup);
  163. } else {
  164. $this->_addMessage("Changing authentication session from ROOT to sitegroup #".$sitegroup->id." failed, reason ".mgd_errstr());
  165. }
  166. // End the ugly hack
  167. }
  168. $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())));
  169. $membership_error = mgd_errstr();
  170. if ($membership_created) {
  171. $membership = mgd_get_member($membership_created);
  172. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("added user \"%s\" successfully to group \"%s\""),$admin_user->name,$admin_group->name));
  173. $admin_group->setsitegroup($sitegroup->id);
  174. $sitegroup->admingroup = $admin_group->id;
  175. $admin_user->setsitegroup($sitegroup->id);
  176. $membership->setsitegroup($sitegroup->id);
  177. $admin_group_assigned = $sitegroup->update();
  178. if ($admin_group_assigned) {
  179.  
  180. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("assigned group \"%s\" as organization \"%s\" administrators"),$admin_group->name,$sitegroup->name));
  181. $sitegroup = mgd_get_sitegroup($sitegroup->id);
  182.  
  183. // All done, redirect
  184. return $sitegroup->id;
  185. } else {
  186. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed assign group as organization administrators, reason %s"),mgd_errstr()),"crit");
  187. midcom_helper_purge_object($membership->guid());
  188. midcom_helper_purge_object($admin_user->guid());
  189. midcom_helper_purge_object($admin_group->guid());
  190. midcom_helper_purge_object($sitegroup->guid());
  191. return false;
  192. }
  193. } else {
  194. $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");
  195. midcom_helper_purge_object($admin_user->guid());
  196. midcom_helper_purge_object($admin_group->guid());
  197. midcom_helper_purge_object($sitegroup->guid());
  198. }
  199. } else {
  200. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create account for admin user, reason %s"),mgd_errstr()),"crit");
  201. midcom_helper_purge_object($admin_user->guid());
  202. midcom_helper_purge_object($admin_group->guid());
  203. midcom_helper_purge_object($sitegroup->guid());
  204. return false;
  205. }
  206. } else {
  207. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create admin user, reason %s"),mgd_errstr()),"crit");
  208. midcom_helper_purge_object($admin_group->guid());
  209. midcom_helper_purge_object($sitegroup->guid());
  210. return false;
  211. }
  212. } else {
  213. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create admin group, reason %s"),mgd_errstr()),"crit");
  214. midcom_helper_purge_object($sitegroup->guid());
  215. return false;
  216. }
  217. } else {
  218. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create organization, reason %s"),mgd_errstr()),"crit");
  219. return false;
  220. }
  221. }
  222.  
  223. function _getHost($id) {
  224. $host = mgd_get_host($id);
  225. if ($host->sitegroup == $this->_sitegroup->id) {
  226. $this->_host = $host;
  227. $style = mgd_get_style($host->style);
  228. $this->_style = $style;
  229. return true;
  230. }
  231. return false;
  232. }
  233. function _addMessage($message, $type = "info") {
  234. $this->_message[] = array(
  235. "message" => $message,
  236. "type" => $type,
  237. );
  238. }
  239.  
  240. function can_handle($argc, $argv) {
  241.  
  242. debug_push($this->_debug_prefix . "can_handle");
  243.  
  244. $this->_argv = $argv;
  245.  
  246. // Autoload CSS
  247. $GLOBALS["midcom"]->add_link_head(array(
  248. 'rel' => 'stylesheet',
  249. 'type' => 'text/css',
  250. 'href' => MIDCOM_STATIC_URL."/midgard.admin.sitewizard/sitewizard.css",
  251. ));
  252. if ($argc == 0) {
  253. // Welcome page, ask for sitegroup
  254. return true;
  255. } elseif ($argc == 1) {
  256. // First argument is sitegroup ID
  257. if ($this->_getSitegroup($argv[0])) {
  258. // Sitegroup chosen, ask for host
  259. return true;
  260. } else {
  261. return false;
  262. // TODO: redirect to selector?
  263. }
  264. } elseif ($argc == 2) {
  265. // Second argument is host ID
  266. $this->_getSitegroup($argv[0]);
  267. $host_found = $this->_getHost($argv[1]);
  268.  
  269. if ($host_found) {
  270. $this->_current_view = "finish";
  271. return true;
  272. }
  273. } elseif ($argc == 3) {
  274. // Third argument is selected view
  275. // Host chosen, ask for style
  276. $this->_getSitegroup($argv[0]);
  277. $host_found = $this->_getHost($argv[1]);
  278. if ($host_found) {
  279. $GLOBALS["midgard_admin_sitewizard_nap_activeid"] = $this->_host->id;
  280. if ($argv[2] == "configure_host") {
  281. $host_schema = mgd_get_page_element_by_name($this->_host->root,"__schema");
  282. if ($host_schema) {
  283. // Load schema
  284. eval ("\$schema = Array(\n" . $host_schema->value . "\n);");
  285. if (is_array($schema)) {
  286. $this->_schema = $schema;
  287. $this->_current_view = "configure_host";
  288. $this->_views["configure_host"]["visible"] = true;
  289. return true;
  290. } else {
  291. // TODO: Schema problem, complain
  292. }
  293. }
  294. } elseif ($argv[2] == "select_style") {
  295. $this->_current_view = "select_style";
  296. return true;
  297. } elseif ($argv[2] == "configure_style") {
  298. if ($this->_style) {
  299. $style_schema = mgd_get_element_by_name($this->_style->up,"__schema");
  300. if ($style_schema) {
  301. // Load schema
  302. eval ("\$schema = Array(\n" . $style_schema->value . "\n);");
  303. if (is_array($schema)) {
  304. $this->_schema = $schema;
  305. $this->_current_view = "configure_style";
  306. $this->_views["configure_style"]["visible"] = true;
  307. return true;
  308. } else {
  309. // TODO: Schema problem, complain
  310. }
  311. }
  312. }
  313. }
  314.  
  315. }
  316.  
  317. return false;
  318. }
  319.  
  320. debug_pop();
  321. return false;
  322. }
  323.  
  324. function handle() {
  325. debug_push($this->_debug_prefix . "handle");
  326. // Walk through the wizard steps to see where we are
  327. if (!$this->_sitegroup) {
  328. // Sitegroup hasn't been created or selected yet
  329. if (isset($_POST[$this->_form_prefix."process"])) {
  330. // User pressed "Next", process the form
  331. if ($_POST[$this->_form_prefix."sitegroup_action"] == "create_new") {
  332. if ($_MIDGARD["config"]["quota"] && isset($_POST[$this->_form_prefix."sitegroup"]["quota"])) {
  333. $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"]);
  334. } else {
  335. $sitegroup_created = $this->_createSitegroup($_POST[$this->_form_prefix."sitegroup"]["name"],$_POST[$this->_form_prefix."sitegroup"]["admin"],$_POST[$this->_form_prefix."sitegroup"]["password"]);
  336. }
  337. if ($sitegroup_created) {
  338.  
  339. // Successfully created, redirect to host creation
  340. $GLOBALS["midcom"]->relocate($sitegroup_created."/");
  341. }
  342. } elseif ($_POST[$this->_form_prefix."sitegroup_action"] == "use_existing") {
  343. // Existing sitegroup selected
  344. if ($this->_getSitegroup($_POST[$this->_form_prefix."sitegroup"]["id"])) {
  345. // Redirect to host setup for sitegroup
  346. $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/");
  347. } else {
  348. // TODO: raise error
  349. return false;
  350. }
  351. }
  352. }
  353. // Sitegroup creation and selection screen
  354. $this->_current_view = "select_sitegroup";
  355. } elseif (!$this->_host) {
  356. if (isset($_POST[$this->_form_prefix."process"])) {
  357. // User pressed "Next", process the form
  358. $host = new midgard_admin_sitewizard_host(false,$this->_sitegroup->id);
  359. $host->name = $_POST[$this->_form_prefix."host"]["name"];
  360. $host->root = $this->_templates["template_midcom"]->id;
  361. if (isset($_POST[$this->_form_prefix."advanced"]) && $_POST[$this->_form_prefix."advanced"] == "on") {
  362. // Advanced mode
  363. $host->port = $_POST[$this->_form_prefix."host"]["port"];
  364. $host->prefix = $_POST[$this->_form_prefix."host"]["prefix"];
  365. }
  366. $host_created = $host->create();
  367. if ($host_created) {
  368. $this->_getHost($host_created);
  369. // Initialize
  370. $host_init = mgd_get_page_element_by_name($host->root,"__init");
  371. if ($host_init) {
  372. $GLOBALS["midgard_admin_sitewizard_init_errstr"] = "";
  373. eval ('?>' . $host_init->value . '<?');
  374. if (function_exists('template_initialize')) {
  375. // Function is defined by template
  376. $initialized = template_initialize($this->_host,$this->_sitegroup->id);
  377. if (!$initialized) {
  378. $this->_addMessage($GLOBALS["midgard_admin_sitewizard_init_errstr"],"crit");
  379. midcom_helper_purge_object($this->_host->guid());
  380. $this->_current_view = "create_host";
  381. return true;
  382. }
  383. }
  384. }
  385. // Check where to go next
  386. $host_schema = mgd_get_page_element_by_name($this->_host->root,"__schema");
  387. if ($host_schema) {
  388. // Website is configurable
  389. $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$host_created."/configure_host.html");
  390. } else {
  391. // Go to select style
  392. $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$host_created."/select_style.html");
  393. }
  394. } else {
  395. $this->_addMessage(sprintf($GLOBALS["view_l10n"]->get("failed to create website, reason %s"),mgd_errstr()),"crit");
  396. }
  397. }
  398. // Host creation screen
  399. $this->_current_view = "create_host";
  400.  
  401. } else {
  402. // We have SG and host, handle regular config situations
  403. if ($this->_current_view == "configure_host") {
  404. $this->_datamanager = new midcom_helper_datamanager($this->_schema);
  405. // Figure out storage location
  406. $root_page = mgd_get_page($this->_host->root);
  407. $storage_location = $root_page->parameter("midgard.admin.sitewizard","config_location");
  408. if ($storage_location == "sitegroup") {
  409. $this->_datamanager->init($this->_sitegroup);
  410. } else {
  411. // Default to host, as in midcom-template
  412. $this->_datamanager->init($this->_host);
  413. }
  414. switch ($this->_datamanager->process_form()) {
  415. case MIDCOM_DATAMGR_SAVED:
  416. $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$this->_host->id."/select_style.html");
  417. case MIDCOM_DATAMGR_EDITING:
  418. case MIDCOM_DATAMGR_CANCELLED:
  419. // We stay here whatever happens here, at least as long as
  420. // there is no fatal error.
  421. break;
  422.  
  423. case MIDCOM_DATAMGR_FAILED:
  424. $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
  425. $this->errcode = MIDCOM_ERRCRIT;
  426. debug_pop();
  427. return false;
  428.  
  429. }
  430. } elseif ($this->_current_view == "select_style") {
  431.  
  432. if (isset($_POST[$this->_form_prefix."process"])) {
  433. // User pressed "Next", process the form
  434. $selected_style = $_POST[$this->_form_prefix."select_style"];
  435. if (array_key_exists($selected_style,$this->_styles)) {
  436. $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);
  437. if ($substyle_created) {
  438. $substyle = mgd_get_style($substyle_created);
  439. $substyle->setsitegroup($this->_sitegroup->id);
  440. $this->_host->style = $substyle->id;
  441. if ($this->_host->update()) {
  442. // Check where to go next
  443. $style_schema = mgd_get_element_by_name($this->_styles[$selected_style]->id,"__schema");
  444. if ($style_schema) {
  445. // Style is configurable
  446. $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$this->_host->id."/configure_style.html");
  447. } else {
  448. // All done
  449. $GLOBALS["midcom"]->relocate($this->_sitegroup->id."/".$this->_host->id."/");