Ejemplo n.º 1
0
Archivo: wi3.php Proyecto: azuya/Wi3
 public function init()
 {
     // Load session handler
     $this->session = Session::instance();
     // Set cache handler
     $this->cache = Wi3TikoCache::instance();
     // Define APPRELATIVEPATH, which is the path to the application relative to the web root
     // We can retrieve this by using the SCRIPT_NAME from the front-controller ({pathfromroot}/app/index.php), and extracting the path-from-root
     define("APPRELATIVEPATH", substr($_SERVER["SCRIPT_NAME"], 0, strpos($_SERVER["SCRIPT_NAME"], "/app/index.php")) . "/app/latest/");
     // Define document root
     // TODO: Add support for ISS (see http://www.helicron.net/php/)
     define("DOCUMENTROOT", $_SERVER["DOCUMENT_ROOT"] . "/");
     // Determine language
     $lang = Cookie::get('lang');
     if ($lang !== NULL) {
         if (!in_array($lang, array('nl-nl', 'en-us'))) {
             // Check the allowed languages, and force the default
             $lang = 'nl-nl';
         }
     } else {
         // Language not set in cookie. Get default language from i18n file.
         $i18nfiles = Kohana::find_file("config", "i18n");
         if (!empty($i18nfiles)) {
             $i18nsettings = Kohana::load($i18nfiles[0]);
             $lang = $i18nsettings["lang"];
         } else {
             $lang = 'nl-nl';
             // Fall back to default
         }
         // Save loaded language in cookie
         Cookie::set('lang', $lang);
     }
     // Set the target language
     i18n::lang($lang);
     // Set the source language to some non-existing language to prevent Kohana skipping translation lookup if source and target language are identical
     i18n::$source = "bb-bb";
     // See http://unicode.org/cldr/utility/languageid.jsp?a=bb&l=en for valid tags
     // Load wi3-kohana-specific functions
     $this->kohana = new Wi3_Kohana();
     // XSS Clean all user input!
     // TODO: only do this if the user is not an admin...
     $this->originalpost = $_POST;
     // Save original $_POST
     foreach ($_POST as $key => $val) {
         $_POST[$key] = Security::xss_clean($val);
     }
     $this->originalget = $_GET;
     // Save original $_GET
     foreach ($_GET as $key => $val) {
         $_GET[$key] = Security::xss_clean($val);
     }
     // Load some Wi3 classes
     // Load a global database configuration
     $this->database = new Wi3_Database();
     // Helper functions to create databases etc
     $this->globaldatabase = Wi3_Database::instance("global");
     Event::instance("wi3.init.globaldatabase.loaded")->execute();
     // Get routing, url and path information
     // These classes in turn add a callback to the wi3.init.site.loaded Event, after which they will update with path and urls to the site
     $this->routing = Wi3_Routing::instance();
     Event::instance("wi3.init.routing.loaded")->execute();
     $this->pathof = Wi3_Pathof::instance();
     Event::instance("wi3.init.pathof.loaded")->execute();
     $this->urlof = Wi3_Urlof::instance();
     Event::instance("wi3.init.urlof.loaded")->execute();
     // Load CSS and Javascript 'injectors'
     $this->css = Wi3_Css::instance();
     $this->javascript = Wi3_Javascript::instance();
     // Instantiate the Model class, that is an interface to the 'factory' method for any underlying model-systems
     $this->model = Wi3_Model::inst();
     Event::instance("wi3.init.model.loaded")->execute();
     // Instantiate the form-builder
     $this->formbuilder = Wi3_Formbuilder::inst();
     Event::instance("wi3.init.formbuilder.loaded")->execute();
     // Now find out what is the scope of this request
     // It most often is a site-scope (i.e. the admin or view of a site), but might also be a global scope (i.e. superadmin)
     // This depends on the controller.
     // Pagefiller-specific controllers are always for the the sitearea
     $this->scope = (substr(Request::instance()->controller, 0, 9) == "adminarea" or substr(Request::instance()->controller, 0, 10) == "pagefiller" or Request::instance()->controller == "sitearea") ? "site" : "global";
     if ($this->scope == "site") {
         $this->sitearea = Wi3_Sitearea::inst();
         // Find out what site we are working with
         // Both the admin controller and the site controller need to know this in order to work properly
         // Find the site by apache 'sitename' variable
         if (isset($_SERVER['REDIRECT_SITENAME'])) {
             $sitename = $_SERVER['REDIRECT_SITENAME'];
             // With correct loading, $_SERVER['REDIRECT_SITENAME'] should always be present, as it is set in the vhosts .htaccess that redirect here
             // Global site is the site in the global space, i.e. the Site model in the 'list of sites' that is always accesible
             // ( In the per-site database, there can only exist one Site model )
             $this->sitearea->globalsite = $this->model->factory("site")->set('name', $sitename)->load();
             Event::instance("wi3.init.sitearea.globalsite.loaded")->execute();
             $this->sitearea->site = $this->sitearea->globalsite;
             // This site instance will be replaced by the local user site. The ->name will be added to that local site, since it does not store that in the local db
         }
         // If the sitename not present, the page request came here via some illegal method.
         // If the site was not loaded correctly or is not active, we cannot show the site either
         if (!isset($_SERVER['REDIRECT_SITENAME']) or empty($sitename) or !$this->sitearea->globalsite->loaded() or $this->sitearea->globalsite->active == FALSE) {
             // Site does not exist. Quit.
             throw new Kohana_Exception("site does not exist");
         }
         // Global site has been loaded and it was found to be active
         // Now we load the local site and requested page from within the user Database
         // This requires the inclusion of the site as a module and an init on its database-config
         //
         // First, Include the whole site-tree in the find_file() function
         Kohana::modules(Kohana::modules() + array("site" => APPPATH . "../../sites/" . $sitename . "/"));
         // Because Kohana uses include_once() this will only init the new module, without double-including the others
         // Load the sitedatabase config. It will be fetched from the sites/sitename/config folder since the sites/sitename is now in the Kohana find_file paths
         $siteconfig = Kohana::config('sitedatabase')->site;
         // Set up a site database connection, to be used by the site-based-models like Site_Page, Site_User, File etc
         $this->sitearea->database = Wi3_Database::instance("site", $siteconfig);
         Event::instance("wi3.init.sitearea.database.loaded")->execute();
         // Load the user-site
         $this->sitearea->site = $this->model->factory("site_site")->set('id', 1)->load();
         $this->sitearea->site->name = $sitename;
         // Add name, since this is not stored in the local site tables, but only in the global ones
         Event::instance("wi3.init.sitearea.site.loaded")->execute();
         // Load the pageposition, page and file manager, all within the sitearea
         $this->sitearea->pagepositions = Wi3_Sitearea_Pagepositions::inst();
         $this->sitearea->pages = Wi3_Sitearea_Pages::inst();
         $this->sitearea->files = Wi3_Sitearea_Files::inst();
         $this->sitearea->users = Wi3_Sitearea_Users::inst();
     }
     // Load baseviews that are passed as $this into views in order to enable some in-view functions
     // Different setups are possible with the different parameters supplied
     // An instance is created, so that they can also be referenced simply from again loading e.g. Wi3_Baseview::instance('superadminarea');
     // These instances are used as 'object scope' for the $this variables in views. See i.e. the superadminarea-controller's ->view function and the Baseview->capture() for more details
     $this->baseview_superadminarea = Wi3_Baseview::instance('superadminarea', array('javascript_url' => $this->urlof->appfiles . 'static/javascript/', 'javascript_path' => $this->pathof->app . 'static/javascript/', 'css_url' => $this->urlof->appfiles . 'static/css/', 'css_path' => $this->pathof->app . 'static/css/'));
     //Maybe just define the asset-path(s), from which the URLs are deduced, based on the Wi3::inst()->urlof ?
     $this->baseview_adminarea = Wi3_Baseview::instance('adminarea', array('javascript_url' => $this->urlof->appfiles . 'static/javascript/', 'javascript_path' => $this->pathof->app . 'static/javascript/', 'css_url' => $this->urlof->appfiles . 'static/css/', 'css_path' => $this->pathof->app . 'static/css/'));
     $this->baseview_sitearea = Wi3_Baseview::instance('sitearea', array('javascript_url' => $this->urlof->site . 'static/javascript/', 'javascript_path' => $this->pathof->site . 'static/javascript/', 'css_url' => $this->urlof->site . 'static/css/', 'css_path' => $this->pathof->site . 'static/css/'));
     Event::instance("wi3.init.baseviews.loaded")->execute();
     // Set up an config loader
     $this->configof = Wi3_Configof::instance();
     // Set up auth. This will try to login the current user from either the site db or the global db, based on the scope
     if ($this->scope == "site") {
         $this->sitearea->auth = Wi3_Auth_Site::instance();
     } else {
         // If user is in setup, then don't yet load Auth and Database instances, since they most probably don't yet exist
         if (Request::instance()->controller != "setup") {
             $this->globalauth = Wi3_Auth_Global::instance();
         }
     }
     $this->acl = Wi3_ACL::instance();
     // Load the plugin-manager. The manager will also include the paths to the plugins in the modules-system
     $this->plugins = new Wi3_Plugins();
     if ($this->scope == "site") {
         // Make all the pageversion-plugins to load
         // The versionplugins should respond to this event call, and add them to the $this->versionplugins array
         Event::instance('wi3.sitearea.pages.versionplugins.load')->execute();
     }
 }
Ejemplo n.º 2
0
 public function action_deletesite()
 {
     $this->setview("superadminarea");
     $site = Wi3::inst()->model->factory("site");
     $site->name = $_POST["name"];
     $site->load();
     try {
         ## Delete the global site in the global DB
         $site->delete();
         // The cascading deletion of the corresponding URLs happens in the model itself
         ## Delete the site-DB
         // Set the local site and add a databasesafename so that the DB config can fetch it
         Wi3::inst()->sitearea = Wi3_Sitearea::inst();
         Wi3::inst()->sitearea->site = $site;
         Wi3::inst()->sitearea->site->databasesafename = str_replace(".", "_", $site->name);
         // Load DB config
         $configarray = (include APPPATH . "../../sites/" . $site->name . "/config/sitedatabase.php");
         $dbinstance = Database::instance("site", $configarray["site"]);
         if (isset($configarray["site"]["connection"]["database"]) and !empty($configarray["site"]["connection"]["database"])) {
             @Wi3::inst()->database->delete_database($configarray["site"]["connection"]["database"]);
         }
         ## Delete all files etc for this site
         $sitename = $site->name;
         if (!empty($sitename)) {
             @Wi3::inst()->unlink_recursive(APPPATH . "../../sites/" . $sitename . "/");
         }
     } catch (Exception $e) {
         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"));
 }