Source for file default.php

Documentation is available at default.php

  1. <?php
  2. //          (F)
  3. // CMS3 - A Three Content Management System.
  4. // Copyright (C) 2007  Jop... (Jonas F. Jensen).
  5. // 
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. // 
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  19.  
  20. /**
  21. *This file defines and executes the CMS3_MainClass
  22. *
  23. @package    System
  24. @author    Jonas F. Jensen <jopsen@gmail.com>
  25. @copyright    2007 Jonas F. Jensen.
  26. @license    http://www.gnu.org/licenses/gpl.txt
  27. */
  28.  
  29. //Start session
  30.  
  31. /**
  32. *The CMS3 main class, this class hostes all plugins and initiate everything.
  33. */
  34. {
  35.     protected $Plugins = array();
  36.  
  37.     /**
  38.     * Constructor, loads all plugins
  39.     */
  40.     function __construct(){
  41.         //Include all shared interfaces
  42.         $InShare scandir("./share/");
  43.         foreach($InShare as $Shared){
  44.             if($Shared != "." && $Shared != ".."){
  45.                 $InShared scandir("./share/" $Shared "/")//$InShare
  46.                 foreach($InShared as $SharedInterface){
  47.                     if($SharedInterface != "." && $SharedInterface != ".."){
  48.                         //Use output control to avoid problems from bad plugins
  49.                         ob_start();
  50.                         require_once($this->GetCMS3Path("share/"$Shared ."/" $SharedInterface);
  51.                         ob_end_clean();
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.  
  57.         $InLib scandir("./lib/");
  58.         foreach($InLib as $Plugin){
  59.             //Avoid trying to load .. and .
  60.             if($Plugin != "." && $Plugin != ".."){
  61.                 //You could do some testing of is_dir etc. but DONT
  62.                 $this->Plugins[$Plugin$this->LoadPlugin($Plugin);
  63.             }
  64.         }
  65.     }
  66.  
  67.     /**
  68.     * Destructor, closes Database connections
  69.     */
  70.     function __destruct({
  71.         if(isset($this->UserSpaceDatabaseHandle)){
  72.             if(!mysql_close($this->UserSpaceDatabaseHandle)){
  73.                 $this->LogMySQL("System""Error closing userspace MySQL connection",$this->UserSpaceDatabaseHandle);
  74.             }
  75.         }
  76.         if(isset($this->SystemDatabaseHandle)){
  77.             if(!mysql_close($this->SystemDatabaseHandle)){
  78.                 $this->LogMySQL("System""Error closing system MySQL connection",$this->SystemDatabaseHandle);
  79.             }
  80.         }
  81.     }
  82.  
  83.     /**
  84.     * Main functions start the application
  85.     */
  86.     static function Main($Args){
  87.         $MainClass new CMS3_MainClass();
  88.         $MainClass->ResolveIdentifier($Args);
  89.     }
  90.  
  91.     /**
  92.      * Gets the username of the current user, resturns null if user not loggedin.
  93.      *
  94.      * @return string 
  95.      * @access public
  96.      */
  97.     public function GetUserName{
  98.         if($_SESSION["loggedin"== "TruE"){
  99.             return $_SESSION["UserName"];
  100.         }else{
  101.             return false;
  102.         }
  103.     // end of member function GetUserName
  104.  
  105.     
  106.     /**
  107.     *Log the user in
  108.     *
  109.     *@param string Username Username of the user
  110.     *@param string Password The users password
  111.     *@return bool true if login was successfull
  112.     */
  113.     public function Login($Username$Password){
  114.         //Calculate hash
  115.         $Password sha1($Password);
  116.         $Query "Select BackendAccess from CMS3_Users where Username = '$UsernameAND Password = '$Passwordlimit 1";
  117.         $Result mysql_query($Query$this->GetSystemDatabaseHandle());
  118.         if(mysql_num_rows($Result!= 0){
  119.             $_SESSION["loggedin""TruE";
  120.             $_SESSION["UserName"$Username;
  121.             //TODO cache backend access value, since it's usefull
  122.             return true;
  123.         }else{
  124.             return false;
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Returns true if current user is administrator.
  130.      *
  131.      * @return true/false 
  132.      * @access public
  133.      */
  134.     public function IsAdmin{
  135.         //As of development just return true
  136.         //return true;
  137.         //We are using sha1 since it's not completly broken yet.
  138.         if($UserName $this->GetUserName()){
  139.             $DB $this->GetSystemDatabaseHandle();
  140.             $Result mysql_query("Select BackendAccess from CMS3_Users where Username = '$UserNamelimit 1");
  141.             //We don't have to check password that should be done in GetUserName
  142.             //AND Password = '"  . $_COOKIE["CMS3_PasswordHash"] . "' limit 1",);
  143.             if($Result){
  144.                 $BackendAccess mysql_fetch_row($Result);
  145.                 if($BackendAccess[0== "1"){
  146.                     return true;
  147.                 }else{
  148.                     return false;
  149.                 }
  150.             }else{
  151.                 return false;
  152.             }
  153.         }
  154.     // end of member function IsAdmin
  155.  
  156.     
  157.     /**
  158.      * Gets the server path of the CMS3 installation.
  159.      *
  160.      * @return string 
  161.      * @access public
  162.      */
  163.     public function GetCMS3Path{
  164.         //As of development
  165.         return $_SERVER["DOCUMENT_ROOT""/";
  166.     // end of member function GetCMS3Path
  167.  
  168.     
  169.     /**
  170.      * Gets the URL of the CMS3 toplevel namespace.
  171.      *
  172.      * @return string 
  173.      * @access public
  174.      */
  175.     public function GetCMS3URL{
  176.         //As of development
  177.         return "http://" $_SERVER["HTTP_HOST""/";
  178.     // end of member function GetCMS3URL
  179.  
  180.     
  181.     /**
  182.     * Load a plugin
  183.     *
  184.     * @param string Plugin PluginID of the plugin you wish to load
  185.     * @return object Returns the plugin you requested
  186.     */
  187.     public function &LoadPlugin($Plugin){
  188.         //Note this could also be done with autoload, but this will cause less conflicts
  189.  
  190.         //Use output control to avoid problems from bad plugins
  191.         ob_start();
  192.         require_once($this->GetCMS3Path("lib/$Plugin/CMS3_$Plugin.php");
  193.         ob_end_clean();
  194.         $IPlugin = eval("return new CMS3_$Plugin();");
  195.         $IPlugin->Register($this);
  196.         return $IPlugin;
  197.     }
  198.  
  199.     /**
  200.     * Gets an instance of a plugin, this returns the one loaded at initialization
  201.     *
  202.     * @param string Plugin PluginID of the plugin you wish to load
  203.     * @return object The plugin you requested, or false if not there.
  204.     */
  205.     public function &GetPlugin($Plugin){
  206.         //Determine if plugin exists
  207.         if(array_key_exists($Plugin$this->Plugins))
  208.         {
  209.             return $this->Plugins[$Plugin];
  210.         }else{
  211.             return false;
  212.         }
  213.     }
  214.  
  215.     /**
  216.     * Gets all plugins that implements an interface
  217.     *
  218.     *@param string Interface Interface the plugins must implement
  219.     *@return array Array of objects that implements the interface
  220.     */
  221.     public function &GetImplementations($Interface){
  222.         $Implementations array();
  223.         foreach($this->Plugins as $Plugin){
  224.             if(in_array($Interface,class_implements($Plugin))){
  225.                 $Implementations[$Plugin;
  226.             }
  227.         }
  228.         return $Implementations;
  229.     }
  230.  
  231.     /**
  232.     *Call a method on all plugins that implements an interface
  233.     *
  234.     *@param string Interface Interface the plugins must implement to get this call
  235.     *@param string Method Method to be called
  236.     *@param array Arguments Array of arguments to be passed to the method.
  237.     *@return array Returns an array of return values from the different methods
  238.     */
  239.     public function &CallImplementations($Interface$Method$Arguments array()){
  240.         $Returns array();
  241.         $StrArgs "";
  242.         for($i 0$i <= sizeof($Arguments)$i++){
  243.             if(StrArgs == ""){
  244.                 $StrArgs .= "\$Arguments['" $i "']";
  245.             }else{
  246.                 $StrArgs .= ", \$Arguments['" $i "']";
  247.             }
  248.         }
  249.         $Implementations $this->GetImplementations($Interface);
  250.         foreach($Implementations as $Implementation){
  251.             $Returns[= eval("return " $Implementation .  "->" $Method "($Args)");
  252.         }
  253.         return $Returns;
  254.     }
  255.  
  256.  
  257.     /**
  258.     * Registers a namespace, the plugin that owns this MUST implement IProvidesContent
  259.     * 
  260.     * @param string Namespace String representation of the namespace, max 255 characters.
  261.     * @param string PluginID PluginID of the owner plugin, this must implement IProvidesContent
  262.     * @access public
  263.     * @return bool Returns true if registration was successfull.
  264.     */
  265.     public function RegisterNamespace($Namespace$PluginID){
  266.         //System is an integrated part of the CMS3 system, special treatment of it, since it's hardlinked into the system.
  267.         if($Namespace == "System"){
  268.             return false;
  269.         }
  270.         //We don't have to check if it's already registered, since it's defines as primary key
  271.         $Query "INSERT INTO CMS3_Namespaces (NamespaceOwnerVALUES ('$Namespace','$PluginID')";
  272.         return mysql_query($Query$this->GetSystemDatabaseHandle());
  273.     }
  274.  
  275.     /**
  276.     * Unregisters a namespace
  277.     * 
  278.     * @param string Namespace String representation of the namespace.
  279.     * @access public
  280.     * @return bool Returns true if unregistration was successfull.
  281.     */
  282.     public function UnregisterNamespace($Namespace){
  283.         $Query "DELETE FROM CMS3_Namespaces WHERE Namespace = '$Namespacelimit 1";
  284.         return mysql_query($Query$this->GetSystemDatabaseHandle());
  285.     }
  286.  
  287.     /**
  288.     * Get namespace from owner
  289.     * 
  290.     * @param string PluginID PluginID of the owner plugin
  291.     * @access public
  292.     * @return string Namespace of the plugin or false if nothing was registered.
  293.     */
  294.     public function GetNamespace($PluginID){
  295.         $Query "SELECT Namespace FROM CMS3_Namespaces  WHERE Owner='$PluginIDlimit 1";
  296.         if(!$Result mysql_query($Query$this->GetSystemDatabaseHandle())){
  297.             $this->LogMySQL("System""Could not resolve namespace for $PluginIDmysqlerror",  $this->GetSystemDatabaseHandle());
  298.             return false;
  299.         }else{
  300.             if(mysql_num_rows($Result!= 0){
  301.                 $Row mysql_fetch_row($Result);
  302.                 return $Row[0];
  303.             }else{
  304.                 return false;
  305.             }
  306.         }
  307.     }
  308.  
  309.     /**
  310.     * Handle for the logfile, not always loaded
  311.     * 
  312.     * @access protected
  313.     */
  314.     protected $LogFileHandle;
  315.  
  316.     /**
  317.      * Print a message to the logfile, useful for debuggin information.
  318.      *
  319.      * Message are both printed to custom logfile in ./data/System/System.log and send to syslogd local0 savedin /var/log/CMS3.log according to installation notes.
  320.      *
  321.      * @param string PluginID PluginID of the plugin that wrote this log message.
  322.      * @param string msg Message to print to the log. Note: Date and time is printed automaticly.
  323.      * @access public
  324.      */
  325.     public function Log$PluginID,  $msg {
  326.         //Write to custom logfile
  327.         if(!isset($this->LogFileHandle)){
  328.             $this->LogFileHandle = fopen($this->GetCMS3Path("/data/System/System.log","a+");
  329.         }
  330.         fwrite($this->LogFileHandle,"\n" date(DATE_RFC822)  ." - "$PluginID ": " $msg);
  331.  
  332.         //Write to syslog
  333.         define_syslog_variables();
  334.         //Open connection to syslogd
  335.         if(openlog("CMS3" LOG_CONS LOG_PID LOG_LOCAL0)){
  336.             if(!syslog(LOG_ERR "CMS3 - " $PluginID " | " $msg)){
  337.                 fwrite($this->LogFileHandle,"\n" date(DATE_RFC822)  ." - System: Could not send message to syslogd.");
  338.             }
  339.             //Close connection to syslogd:
  340.             closelog();
  341.         }else{
  342.             fwrite($this->LogFileHandle,"\n" date(DATE_RFC822)  ." - System: Could not connect to syslogd.");
  343.         }
  344.     // end of member function Log
  345.  
  346.     
  347.     /**
  348.      * Print a message of an mysql error to the logfile, useful for debuggin information.
  349.      *
  350.      * @param string PluginID PluginID of the plugin that wrote this log message.
  351.      * @param string msg Message to print to the log. Note: Date and time is printed automaticly.
  352.      * @param resource mysql resource that the error accoured on.
  353.      * @access public
  354.      */
  355.     public function LogMySQL$PluginID,  $msg &$res{
  356.         $this->Log($PluginID$msg ": \n" mysql_errno($res": " .  mysql_error($res));
  357.     // end of member function Log
  358.  
  359.     
  360.     /**
  361.     * Handle for the userspace database, not always initialized
  362.     * 
  363.     * @access protected
  364.     */
  365.     protected $UserSpaceDatabaseHandle;
  366.  
  367.     /**
  368.     * Gets a handle for the userspace database
  369.     * 
  370.     * @access public
  371.     * @return Returns a MySQL database connection.
  372.     */
  373.     public function &GetDatabaseHandle(){
  374.         if(!isset($this->UserSpaceDatabaseHandle)){
  375.             require_once($this->GetCMS3Path("/etc/System/mysql.conf.php");
  376.             $this->UserSpaceDatabaseHandle = mysql_connect(
  377.                             CMS3_System_UserSpaceDatabaseServer
  378.                             CMS3_System_UserSpaceDatabaseUser,
  379.                             CMS3_System_UserSpaceDatabasePassword);
  380.             if(!$this->UserSpaceDatabaseHandle)
  381.             {
  382.                 $this->LogMySQL("System""Could not connect to userspace database host, mysqlerror"$this->UserSpaceDatabaseHandle);
  383.                 return false;
  384.             }
  385.         }
  386.  
  387.         //Make sure it always called...
  388.         if(!mysql_select_db(CMS3_System_UserSpaceDatabasePrefix "CMS3_Userspace"$this->UserSpaceDatabaseHandle)){
  389.             $this->LogMySQL("System""Could not select to userspace database, mysqlerror"$this->UserSpaceDatabaseHandle);
  390.             return false;
  391.         }    
  392.         return $this->UserSpaceDatabaseHandle;
  393.     }
  394.  
  395.     /**
  396.     * Closes the application, with an FatalError log, and print the log messsage to user.
  397.     * 
  398.     * @access public
  399.     */
  400.     public function FatalError($PluginID$msg "Fatal Error, closing the application."){
  401.         $this->Log($PluginID$msg);
  402.         //TODO: Try using outputhandlers print Error: 503
  403.         print "\nFatal Error: \n" $PluginID ": " $msg;
  404.         //Destruct everything we can:
  405.         $this->__destruct();
  406.  
  407.         //Exit and kill all remaining code.
  408.         exit;
  409.     }
  410.  
  411.     /**
  412.     * Handle for the system database, not always initialized
  413.     * 
  414.     * @access private
  415.     */
  416.     private $SystemDatabaseHandle;
  417.  
  418.     /**
  419.     * Gets a handle for the system database
  420.     * 
  421.     * @access protected
  422.     * @return Returns a MySQL database connection.
  423.     */
  424.     protected function &GetSystemDatabaseHandle(){
  425.         if(!isset($this->SystemDatabaseHandle)){
  426.             require_once($this->GetCMS3Path("/etc/System/mysql.conf.php");
  427.             $this->SystemDatabaseHandle = mysql_connect(
  428.                             CMS3_System_SystemDatabaseServer
  429.                             CMS3_System_SystemDatabaseUser,
  430.                             CMS3_System_SystemDatabasePassword);
  431.             if(!$this->SystemDatabaseHandle)
  432.             {
  433.                 $this->LogMySQL("System""Could not connect to system database host, mysqlerror"$this->SystemDatabaseHandle);
  434.                 $this->FatalError("System""Mysql connection problems");
  435.             }        
  436.         }
  437.         //Make sure it always called...
  438.         if(!mysql_select_db(CMS3_System_SystemDatabasePrefix "CMS3_System",$this->SystemDatabaseHandle)){
  439.             $this->LogMySQL("System""Could not select to system database, mysqlerror"$this->SystemDatabaseHandle);
  440.             $this->FatalError("System""Mysql connection problems");
  441.         }
  442.         return $this->SystemDatabaseHandle;
  443.     }
  444.  
  445.     /**
  446.      * Resoles an print the content of an identifier.
  447.      *
  448.      * @param string Identifier Identifier, including namespace etc.
  449.  
  450.      * @return bool True/false depending on success of failure
  451.      * @access public
  452.      */
  453.     public function ResolveIdentifier$Identifier {
  454.         //Load system settings and parse the system database handle to System plugin
  455.         $System $this->GetPlugin("System");
  456.         $System->SetSystemDatabaseHandle($this->GetSystemDatabaseHandle());
  457.         $SysSettings $System->LoadSettings();
  458.  
  459.         //If identifier is zero restore default namespace from CMS3_System
  460.         if($Identifier == ""){
  461.             $Identifier $SysSettings["DefaultNamespace"];
  462.         }
  463.  
  464.         //Modify identifier
  465.         $data explode("/"$Identifier2);
  466.         $Namespace $data[0];
  467.         $PluginSpecificData $data[1];
  468.  
  469.         //Load outputhandler
  470.         if(!($OutputHandler $this->GetPlugin($SysSettings["DefaultOutputHandler"])))
  471.         {
  472.             //Select a random OutputHandler
  473.             $OutputHandlers $this->GetImplementations("IOutputHandler");
  474.             $OutputHandler =$OutputHandlers[0];    
  475.         }
  476.  
  477.         //Set identifier
  478.         $OutputHandler->SetIdentifier($Identifier);
  479.  
  480.         //get owner of the namespace
  481.         $Query "SELECT Owner from CMS3_Namespaces where Namespace = '" $Namespace "' limit 1";
  482.         if($Result mysql_query($Query$this->GetSystemDatabaseHandle()))
  483.         {
  484.             if(mysql_num_rows($Result!= 0){
  485.                 $Row mysql_fetch_row($Result);
  486.                 $PluginID $Row[0];
  487.                 $IProvidesContent $this->GetPlugin($PluginID);
  488.                 $IProvidesContent->GetPage($PluginSpecificData$OutputHandler);
  489.                 return true;
  490.             }else{
  491.                 //If namespace wasn't found
  492.                 $OutputHandler->PrintError("Namespace not found.""404");
  493.                 return false;
  494.             }
  495.         }else{
  496.             $OutputHandler->PrintError("Namespace not found.""404");
  497.             return false;
  498.         }
  499.     }
  500.  
  501.     /**
  502.      * Gets an embedment from it unique identifier, returns embedable HTML.
  503.      *
  504.      * @param string Identifier Unique identifier.
  505.  
  506.      * @return string 
  507.      * @access public
  508.      */
  509.     public function GetEmbedment$Identifier {
  510.         //$data = explode("/", $Identifier, 2);
  511.         //TODO: make this work, remember classes  are note coded at singleton
  512.         /*$PluginID = $data[0];
  513.         $PluginSpecificData = $data[1];
  514.         require_once("OutputHandlers/CMS3_HTMLOutputHandler.php");
  515.         $IEmbedable = $this->LoadPlugin($PluginID);
  516.         
  517.         return $IEmbedable->GetEmbedments($PluginSpecificData,$OutputHandler);*/
  518.  
  519.         return $identifier;
  520.     // end of member function GetEmbedment
  521.  
  522. }
  523.  
  524.  
  525. CMS3_MainClass::Main($_GET["i"])
  526.  
  527. ?>

Documentation generated on Mon, 30 Apr 2007 01:59:13 +0200 by phpDocumentor 1.3.1