public function action_insertfield() { // TODO: (?) use generic class functions and consolidate with pagefiller-fieldcreation at runtime // TODO: per-user checking for editing-access to this page // an admin-role is assumed for bare login-access to the adminarea, other roles should define access to individual pages $page = Wi3::inst()->model->factory("site_page")->set("id", $_POST["pageid"])->load(); Wi3::inst()->sitearea->setpage($page); // Create a new field, belonging to this page $fieldtype = $_POST["fieldtype"]; $field = Wi3::inst()->model->factory("site_field")->setref($page)->set("type", $fieldtype)->create(); if (!isset($this::$responseoptions["inserttype"])) { $this->responseoptions["inserttype"] = "insertbefore"; } // Return a render of the field $html = Pagefiller_default::view("fieldrender_edit")->set("field", $field)->render(); // Return success echo json_encode(array("scriptsbefore" => array("0" => "wi3.pagefillers.default.edittoolbar.insertFieldHtml(\"" . $field->id . "\", \"" . base64_encode($html) . "\", \"" . $this::$responseoptions["inserttype"] . "\")"), "scriptsafter" => array("0" => "wi3.pagefillers.default.edittoolbar.saveAllEditableBlocks()"))); }
<form id='wi3_adminarea_menu_addpageform' onSubmit=''> <label>Pagetitle:</label> <input name='longtitle'/> <?php $versionhtml = ""; foreach (Wi3::inst()->sitearea->pages->versionplugins() as $plugin) { $versionhtml .= $plugin->versionhtmlforaddpage(); } if (!empty($versionhtml)) { echo $versionhtml; } echo "<div id='menu_addpageoptions'>"; // Get pagefillers, and display the addpageoptions of the default one, along with a choice to pick another $pagefillers = Wi3::inst()->configof->wi3->pagefillers->pagefillers; if (isset($pagefillers->default)) { // Display the addpageoptions of the default pagefiller // $pagefillerpath = $pagefillers->default->path."classes/pagefiller/default.php"; // include($pagefillerpath); $pagefiller = new Pagefiller_default(); echo $pagefiller->pageoptionshtml(); } echo "</div>"; ?> </form> <button style='width: 100%; margin-bottom: 25px;' onClick='adminarea.addpageposition();'>Create</button>
public function action_createsite() { $this->setview("superadminarea"); // name should not start with a dot, to prevent issues with a) overwriting the .template folder, and b) hidden folders if (substr($_POST["name"], 0, 1) == ".") { echo "<p>site kon niet aangemaakt worden!</p>"; echo "<p>Sitenaam mag niet beginnen met een punt (.)!</p>"; return; } ### # Database settings ### if (isset($_POST["dbusername"]) and isset($_POST["dbpassword"]) and isset($_POST["dbexistingornew"]) and isset($_POST["dbname"])) { // Create or use Database! $dbname = $_POST["dbname"]; $dbokay = TRUE; for ($i = 0; $i < 1; $i++) { // Try connection @($con = mysql_connect("localhost", $_POST["dbusername"], $_POST["dbpassword"])); if (!$con) { $dbokay = FALSE; $message = __("Connection to database could not be established. Please try again."); break; } // Save the grants of the current user $result = mysql_query("SHOW GRANTS FOR CURRENT_USER"); $grants = array(); while ($row = mysql_fetch_array($result)) { $grants[] = $row; } $hasallprivileges = FALSE; foreach ($grants as $grant) { if (strpos($grant[0], "GRANT ALL PRIVILEGES ON *.* TO ") === 0) { // User has all privileges for all dbs, so that's fine $hasallprivileges = TRUE; break; // break from foreach } } if ($_POST["dbexistingornew"] == "existing") { // Try if existing db exists $db_selected = mysql_select_db($dbname, $con); if ($db_selected == FALSE) { $dbokay = FALSE; $message = __("Database '" . $dbname . "' does not exist. Please try again."); break; } // Now check whether we have the rights to create tables in the db $hasprivileges = FALSE; if ($hasallprivileges) { $hasprivileges = TRUE; } else { foreach ($grants as $grant) { if (strpos($grant[0], "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER") === 0 and strpos($grant[0], "ON `" . $dbname . "`") > 0) { // User has privileges for the $dbname db, so that's fine $hasprivileges = TRUE; break; // break from foreach } } } // Final check for db privileges if ($hasprivileges === FALSE) { $dbokay = FALSE; $message = __("User does not have the proper rights to use database '" . $dbname . "'. Please try again."); break; } } else { // Check if we can create the new DB if ($hasallprivileges) { if (!mysql_query("CREATE DATABASE " . $dbname, $con)) { $dbokay = FALSE; // Check if there was an error because the db already existed $db_selected = mysql_select_db($dbname, $con); if ($db_selected) { $message = __("User was unable to create database '" . $dbname . "' because it already exists. Please delete the db manually or select the 'existing' option to use the existing database."); } else { $message = __("User was unable to create database '" . $dbname . "', despite having the rights to do so. Please try again."); } break; } } else { $dbokay = FALSE; $message = "User does not have the proper rights to create database '" . $dbname . "'. Please try again."; break; } } } if (!$dbokay) { echo "<p>" . $message . "</p>"; return; } } ### # Site creation ### $site = Wi3::inst()->model->factory("site"); $site->active = $_POST["active"]; $site->name = $_POST["name"]; $site->title = $_POST["title"]; // Sitefolder is currently always the same as the sitename try { $site->create(); // Second create the site folder (with config files etc). Do this by copying the .template folder. Wi3::inst()->copy_recursive(APPPATH . "../../sites/.template", APPPATH . "../../sites/" . $site->name); // Save the DB configuration file by loading the example file and set the correct values $wi3databaseconfig = file_get_contents(APPPATH . "../../sites/" . $site->name . "/config/sitedatabase.php.example"); $wi3databaseconfig = preg_replace("@\\'username\\'.*@", "'username' => '" . $_POST["dbusername"] . "',", $wi3databaseconfig); $wi3databaseconfig = preg_replace("@\\'password\\'.*@", "'password' => '" . $_POST["dbpassword"] . "',", $wi3databaseconfig); $wi3databaseconfig = preg_replace("@\\'database\\'.*@", "'database' => '" . $_POST["dbname"] . "',", $wi3databaseconfig); $wi3databaseconfig = preg_replace("@dbname\\=\\w*@", "dbname=" . $_POST["dbname"], $wi3databaseconfig); file_put_contents(APPPATH . "../../sites/" . $site->name . "/config/sitedatabase.php", $wi3databaseconfig); // Now load the newly created database-config file for this specific site $configarray = (include APPPATH . "../../sites/" . $site->name . "/config/sitedatabase.php"); $dbinstance = Database::instance("site", $configarray["site"]); // Now create all user tables in the site-space. They will use the 'site' DB instance automatically (as this is set in the Model->_db setting) Wi3::inst()->database->create_table_from_sprig_model("site_site"); Wi3::inst()->database->create_table_from_sprig_model("site_pageposition"); Wi3::inst()->database->create_table_from_sprig_model("site_page"); Wi3::inst()->database->create_table_from_sprig_model("site_array"); Wi3::inst()->database->create_table_from_sprig_model("site_arraydata"); Wi3::inst()->database->create_table_from_sprig_model("site_data"); Wi3::inst()->database->create_table_from_sprig_model("site_file"); // Setup the Auth classes Wi3::inst()->database->create_table_from_sprig_model("site_user"); Wi3::inst()->database->create_table_from_sprig_model("site_user_token"); Wi3::inst()->database->create_table_from_sprig_model("site_role"); // Now create the admin user $m = Wi3::inst()->model->factory("site_user"); $m->username = "******"; $m->email = "*****@*****.**"; $m->password = "******"; $m->password_confirm = "admin"; $m->create(); // Now create roles $role = Wi3::inst()->model->factory("site_role"); $role->name = "login"; $role->description = "login role"; $role->users = $m->id; $role->create(); // Admin role $role = Wi3::inst()->model->factory("site_role"); $role->name = "admin"; $role->description = "admin role for this site"; $role->users = $m->id; $role->create(); // Finally, loop over all the pagefillers, and check whether they want to insert any tables etc // TODO: all pagefillers please Pagefiller_default::event("site_created", FALSE); } catch (Exception $e) { echo "<p>site kon niet aangemaakt worden!</p>"; echo Kohana::debug($e); return; } // Redirect to get rid of the superadminarea/someaction URL and to prevent POST issues Request::instance()->redirect(Wi3::inst()->urlof->controller("superadminarea")); }