Source for file CMS3_System.php

Documentation is available at CMS3_System.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 CMS3_System
  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. /**
  30. *CMS_System is a system plugin that hosts IProvidesEditor, IConfigure and ISystemCallback
  31. */
  32. {
  33.  
  34.  
  35.     //ISystemCallback implementation
  36.     //used for system callbacks like logout...
  37.  
  38.     
  39.     /**
  40.      * Handles a callback from ajax application.
  41.      *
  42.      * @param string data Data attached to this callback
  43.  
  44.      * @return 
  45.      * @access public
  46.      */
  47.     public function SystemCallback$data ){
  48.             //Perform a logout
  49.         if($data == "Logout"){
  50.             unset($_SESSION["loggedin"]);
  51.             header("Location: " $this->Owner->GetCMS3URL("System/Login");
  52.  
  53.             //Save default Namespace
  54.         }elseif($data == "SaveDefaultNamespace"){
  55.             $Settings $this->LoadSettings();
  56.             $Settings["DefaultNamespace"$_POST["DefaultNamespace"];
  57.             if($this->SaveSettings($Settings)){
  58.                 print "TRUE";
  59.             }else{
  60.                 //Save settings log the error for us.
  61.                 print "Unable to save settings, try again or see system log.";
  62.             }
  63.  
  64.             //Save default OutputHandler
  65.         }elseif($data == "SaveDefaultOutputHandler"){
  66.             $Settings $this->LoadSettings();
  67.             $Settings["DefaultOutputHandler"$_POST["DefaultOutputHandler"];
  68.             if($this->SaveSettings($Settings)){
  69.                 print "TRUE";
  70.             }else{
  71.                 //Save settings log the error for us.
  72.                 print "Unable to save settings, try again or see system log.";
  73.             }
  74.  
  75.             //Don't know what to do, lets just give it a soft 404... 
  76.         }else{
  77.             print "Error: 404, data not found.";
  78.         }
  79.     }
  80.  
  81.  
  82.     //IProvidesEditor implementation
  83.     //is used to create an abstraction of a controlcenter, in other word just a place to list all IConfigure implementations
  84.  
  85.     
  86.     /**
  87.      * Gets an Ajax guide for modification of the content.
  88.     *
  89.     * This implementation of the interface is used to list implemnetations of IConfigure
  90.      *
  91.      * @param IEmbedmentOutputHandler EmbedmentOutputHandler Outputhandler the content MUST be parse to.
  92.      * @return IEmbedmentOutputHandler 
  93.      * @access public
  94.      */
  95.     public function &ModifyContent($Identifier null&$EmbedmentOutputHandler){
  96.         $Configurations $this->Owner->GetImplementations("IConfigure");
  97.         $Content "<center><br /><br /><div class='ControlCenter'>";
  98.         foreach($Configurations as $Configuration){
  99.             $Icon $Configuration->GetControlCenterIcon();
  100.             $Menu $Configuration->GetControlCenterMenuEntry();
  101.             $Link $this->Owner->GetCMS3URL("System/Configure/" $Configuration->GetPluginID();
  102.             $Content .= "<a href='$Link'><img src='$Iconborder='0' /><br />$Menu</a>";
  103.         }
  104.         $Content .= "</div></center>";
  105.         //TODO content, publish list of IConfigure, don't list system cause it leads here.
  106.         $EmbedmentOutputHandler->SetBody($Content);
  107.         $EmbedmentOutputHandler->ExtentHeader("<style>.ControlCenter a {display: block; float: left;border: 1px solid #37abc8; background-color: #87cdde; margin: 10px; text-align: center; padding: 2px; font-size: 10px; text-decoration: none; color: black; width: 100px; height: 92px; padding-top: 5px;}\n .ControlCenter a:hover {background-color: #afdde9; text-decoration: none; color: black;} .ControlCenter {width: 55%;}</style>");
  108.         return $EmbedmentOutputHandler;
  109.     }
  110.  
  111.     /**
  112.     * Gets an absolute address of an icon for this plugin
  113.     */
  114.     public function GetSystemIcon(){
  115.         return $this->Owner->GetCMS3URL("bin/System/preferences-desktop.png";
  116.     }
  117.  
  118.     /**
  119.     * Gets menu entry string
  120.     */
  121.     public function GetSystemMenuEntry(){
  122.         return "ControlCenter";
  123.     }
  124.  
  125.  
  126.     //IConfigure implementation
  127.  
  128.     
  129.     /**
  130.     * Gets an absolute address of an icon for this plugin
  131.     */
  132.     public function GetControlCenterIcon(){
  133.         return $this->Owner->GetCMS3URL("bin/System/preferences-desktop.png";
  134.     }
  135.  
  136.     /**
  137.     * Gets menu entry string
  138.     */
  139.     public function GetControlCenterMenuEntry(){
  140.         //TODO: i18n
  141.         return "System Config";
  142.     }
  143.  
  144.     /**
  145.      * Gets a dojo based ajax guide for configuration of the plugin.
  146.      *
  147.      * @param IEmbedmentOutputHandler OutputHandler
  148.      * @return IEmbedmentOutputHandler 
  149.      * @access public
  150.      */
  151.     public function &Configure($Identifier null&$EmbedmentOutputHandler ){
  152.         //Load templates from files
  153.         $Head file_get_contents($this->Owner->GetCMS3Path("data/" $this->GetPluginID("/ConfigHead.htm");
  154.         $Body file_get_contents($this->Owner->GetCMS3Path("data/" $this->GetPluginID("/ConfigBody.htm");
  155.  
  156.             //Generate content
  157.         //Load settings
  158.         $Settings $this->LoadSettings();
  159.  
  160.         //List all namespaces
  161.         $Query "SELECT Namespace FROM CMS3_Namespaces ORDER BY Namespace";
  162.         $Result mysql_query($Query$this->SystemDatabaseHandle);
  163.         $NSOptions "";
  164.         if(mysql_num_rows($Result!= 0){
  165.             while($Row mysql_fetch_array($ResultMYSQL_ASSOC))
  166.             {
  167.                 if($Settings["DefaultNamespace"== $Row["Namespace"]){
  168.                      $Selected " selected=\"selected\"";
  169.                 }else{
  170.                     $Selected "";
  171.                 }
  172.                 $NSOptions .= "<option value='" $Row["Namespace""'" $Selected ">" $Row["Namespace""</option>\n";
  173.             }
  174.         }
  175.  
  176.         //List OutputHandlers
  177.         $OHOptions "";
  178.         $Selected "";
  179.         $OutputHandlers $this->Owner->GetImplementations("IOutputHandler");
  180.         foreach($OutputHandlers as $OutputHandler){
  181.             if($Settings["DefaultOutputHandler"== $OutputHandler->GetPluginID()){
  182.                  $Selected " selected=\"selected\"";
  183.             }else{
  184.                 $Selected "";
  185.             }
  186.             $OHOptions .= "<option value='" $OutputHandler->GetPluginID("'" $Selected ">" $OutputHandler->GetPluginID("</option>\n";
  187.         }
  188.  
  189.         //Parse dynamic content
  190.         $Body str_replace("CMS3URL",$this->Owner->GetCMS3URL(,$Body);
  191.         $Body str_replace("NSOPTIONS",$NSOptions ,$Body);
  192.         $Body str_replace("OHOPTIONS",$OHOptions ,$Body);
  193.  
  194.         //Parse the output
  195.         $EmbedmentOutputHandler->ExtentHeader($Head);
  196.         $EmbedmentOutputHandler->SetBody($Body);
  197.         return $EmbedmentOutputHandler;
  198.     }
  199.  
  200.     /**
  201.      * Print the page of a given identifier.
  202.      *
  203.      * @param string identifier Identifier of the requested page.
  204.  
  205.      * @param IOutputHandler OutputHandler OutputHandler used the parse the output. You may disable this OutputHandler with
  206.     *  OutputHandler->Disable() and print the content manually.
  207.  
  208.      * @return 
  209.      * @access public
  210.      */
  211.     public function &GetPage$identifier,  &$OutputHandler ){
  212.         if(!$this->Owner->IsAdmin()){
  213.             if($_POST["AjaxRequest"== "true"){
  214.                 //Repond to Ajax callback
  215.                 $OutputHandler->Disable();
  216.                 if($this->Owner->Login($_POST["Username"]$_POST["Password"])){
  217.                     print $this->Owner->GetCMS3URL("System/Welcome";
  218.                 }else{
  219.                     print "Fail";
  220.                 }
  221.             }else{
  222.                 //TODO: Make login site, using outputhandler
  223.                 $OutputHandler->SetTitle("CMS3 System login");
  224.                 $Body file_get_contents($this->Owner->GetCMS3Path("data/System/LoginBody.htm");
  225.                 $Body str_replace("CMS3URL",$this->Owner->GetCMS3URL(,$Body);
  226.                 $OutputHandler->SetBody($Body);
  227.                 $Header file_get_contents($this->Owner->GetCMS3Path("data/System/LoginHeader.htm");
  228.                 $Header str_replace("CMS3URL",$this->Owner->GetCMS3URL(,$Header);
  229.                 $OutputHandler->ExtentHeader($Header);
  230.                 $OutputHandler->Parse();
  231.             }
  232.         }else{
  233.             //We wont be using this...
  234.             $OutputHandler->Disable();
  235.  
  236.             $data explode("/"$identifier3);
  237.             if(sizeof($data== 1){
  238.                 $SysOutput $this->GetSystemOutputHandler();
  239.                 //TODO: i18n and create an overview of what can be done here...
  240.                 $SysOutput->SetBody("Welcome to CMS3 backend, please use the dock for navigation.");
  241.                 $this->ParseSystemOutputHandler($SysOutput);
  242.             }else{
  243.                 //Append empty string, if there's only two
  244.                 if(sizeof($data== 2){
  245.                     $data[2"";
  246.                 }
  247.                 if($data[0== "Configure"){
  248.                     $SysOutput $this->GetSystemOutputHandler();
  249.                     $Configure $this->Owner->GetPlugin($data[1]);
  250.                     $SysOutput $Configure->Configure($data[2]$SysOutput);
  251.                     $this->ParseSystemOutputHandler($SysOutput);
  252.                 }
  253.                 if($data[0== "Edit"){
  254.                     $SysOutput $this->GetSystemOutputHandler();
  255.                     $Editor $this->Owner->GetPlugin($data[1]);
  256.                     $SysOutput $Editor->ModifyContent($data[2]$SysOutput);
  257.                     $this->ParseSystemOutputHandler($SysOutput);
  258.                 }
  259.                 if($data[0== "Callback"){
  260.                     //TODO: Check if interface is implementated
  261.                     $ICallback $this->Owner->GetPlugin($data[1]);
  262.                     $ICallback->SystemCallback($data[2]);
  263.                 }
  264.             }
  265.         }
  266.  
  267.         return $OutputHandler;
  268.     }
  269.  
  270.     /**
  271.     *Gets an instance of SystemOutputHandler
  272.     */
  273.     protected function &GetSystemOutputHandler(){
  274.         require_once($this->Owner->GetCMS3Path("lib/System/CMS3_System_SysOutputHandler.php");
  275.         $SysOutputHandler new CMS3_System_SysOutputHandler();
  276.         $SysOutputHandler->Register($this->Owner);
  277.         return $SysOutputHandler;
  278.     }
  279.  
  280.     /**
  281.     *Parse SysOutputHandler to HTML
  282.     */
  283.     protected function ParseSystemOutputHandler(&$OutputHandler){
  284.         //TODO enable reponse as ajax call, perhaps using hidden http parameter like $_POST["IsAjax"]
  285.         print "<html>";
  286.         print "<head>" $OutputHandler->GetHeader("</head>";
  287.         print "<body>" $OutputHandler->GetContent("</body>";
  288.         print "</html>";
  289.     }
  290.  
  291.     /**
  292.     *Referance to a system database handle
  293.     */
  294.     protected $SystemDatabaseHandle = null;
  295.  
  296.     /**
  297.      * Gets the pluginID of the plugin.
  298.      *
  299.      * @return string 
  300.      * @access public
  301.      */
  302.     public function GetPluginID){
  303.         return "System";
  304.     }
  305.  
  306.     /**
  307.     *Set the system database handle of this object
  308.     *
  309.     *@param resource Handle System database handle
  310.     */
  311.     public function SetSystemDatabaseHandle(&$Handle){
  312.         $this->SystemDatabaseHandle =$Handle;
  313.     }
  314.  
  315.         //Function for saving and loading settings
  316.         /**
  317.     *Opens the settings file for writting
  318.     *
  319.     * NOTE: this method is public because it's linked to CMS3_MainClass
  320.     */
  321.     public function &LoadSettings(){
  322.         //We're return be referance for performance issues...
  323.         $SettingsFile $this->Owner->GetCMS3Path("etc/" $this->GetPluginID("/Settings.bin"//.bin since it a binary serialized string
  324.         if(file_exists($SettingsFile)){
  325.             //Read data from file
  326.             if(($data file_get_contents($SettingsFile)) === FALSE){
  327.                 $this->Owner->Log($this->GetPluginID()"Could not read date from settings file: ./etc/" $this->GetPluginID("/Settings.bin");
  328.                 return array();                
  329.             }
  330.             //Deserialize loaded data
  331.             return unserialize($data);
  332.         }else{
  333.             return array();
  334.         }
  335.     }
  336.  
  337.     /**
  338.     *Save settings to files...
  339.     */
  340.     protected function SaveSettings(&$Settings){
  341.         $SettingsFile $this->Owner->GetCMS3Path("etc/" $this->GetPluginID("/Settings.bin"//.bin since it a binary serialized string
  342.         $SettingsPath $this->Owner->GetCMS3Path("etc/" $this->GetPluginID("/";
  343.         //Create dir if not there
  344.         if(!is_dir($SettingsPath)){
  345.             if(!mkdir($SettingsPath)){
  346.                 //Log error if couldn't be created
  347.                 $this->Owner->Log($this->GetPluginID()"Could not create directory for settings: ./etc/" $this->GetPluginID("/");
  348.                 return false;
  349.             }
  350.         }
  351.         //Check if file is writeable
  352.         if(file_exists($SettingsFile&& !is_writable($SettingsFile)){
  353.             //Log error if file isn't writeable
  354.             $this->Owner->Log($this->GetPluginID()"Settings file is not writeable: ./etc/" $this->GetPluginID("/Settings.bin");
  355.             return false;
  356.         }else{
  357.             //Open the file for writing
  358.             if(!$Handle fopen($SettingsFile"w")){
  359.                 $this->Owner->Log($this->GetPluginID()"Settings file could not be opened: ./etc/" $this->GetPluginID("/Settings.bin");
  360.                 return false;
  361.             }
  362.             //Write data to file
  363.             if(fwrite($Handle,serialize($Settings)) === FALSE){
  364.                 $this->Owner->Log($this->GetPluginID()"Settings could not be written to file: ./etc/" $this->GetPluginID("/Settings.bin");
  365.                 return false;
  366.             }
  367.             //Close filestream/handle
  368.             if(!fclose($Handle)){
  369.                 $this->Owner->Log($this->GetPluginID()"Could not close setting file correctly: ./etc/" $this->GetPluginID("/Settings.bin");
  370.             }
  371.             //Return true since we've saved the settings, doesn't really matter if file wasn't closed correctly 
  372.             return true;
  373.         }
  374.     }
  375. }
  376.  
  377.  
  378. ?>

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