Source for file CMS3_Gallery.php

Documentation is available at CMS3_Gallery.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_Gallery
  22. *
  23. @package    Gallery
  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. * Very simple plugin that allows creation of an image gallery
  31. */
  32. {
  33.  
  34.         //Implementation of IProvidesContent
  35.         /**
  36.      * Print the page of a given identifier.
  37.      *
  38.      * @param string identifier Identifier of the requested page.
  39.  
  40.      * @param IOutputHandler OutputHandler OutputHandler used the parse the output. You may disable this OutputHandler with
  41.     *  OutputHandler->Disable() and print the content manually.
  42.  
  43.      * @return 
  44.      * @access public
  45.      */
  46.     public function &GetPage$identifier,  &$OutputHandler ){
  47.         //Get the image path and URL
  48.         $ImagePath $this->Owner->GetCMS3Path("bin/" $this->GetPluginID("/Images/";
  49.         $URL $this->Owner->GetCMS3URL("bin/" $this->GetPluginID("/Images/";
  50.  
  51.         //Check if null first, gives better performance
  52.         if($identifier != null && file_exists($ImagePath $identifier))
  53.         {
  54.             //Only view this one image
  55.             $body "<h1>Image: " $identifier "</h1><a href='" $URL $identifier "' title='Click here to see this image' target='_blank'><img src='" $URL $identifier ."' border='0' style='max-width: 80%;'></a>";
  56.             $OutputHandler->SetBody($body);
  57.             $OutputHandler->SetTitle("Image: " $identifier);
  58.         }else{
  59.             $bodylist array();
  60.             
  61.             //View URL
  62.             $Vurl $this->Owner->GetCMS3URL($this->Owner->GetNamespace($this->GetPluginID()) "/";
  63.  
  64.             //List all images
  65.             $Images scandir($ImagePath);
  66.             foreach($Images as $Image){
  67.                 if($Image != "." && $Image != ".."){
  68.                     $bodylist[array("Image: " $Image"<a href='" $Vurl $Image "' title='Click here to see this image'><img src='" $URL $Image "' border='0' style=\"max-height: 300px;\"></a>");
  69.                 }
  70.             }
  71.  
  72.             $body "<h1>Gallery</h1><i>Click on the images to enlarge them.</i>";
  73.             $OutputHandler->SetBody($body$bodylist);
  74.             $OutputHandler->SetTitle("Gallery");
  75.         }
  76.  
  77.         //Parse content
  78.         $OutputHandler->Parse();
  79.  
  80.         //TODO show gallery
  81.         return $OutputHandler;
  82.     }
  83.  
  84.  
  85.         //Implementation of IProvidesEditor
  86.         /**
  87.      * Gets an Ajax guide for modification of the content.
  88.      *
  89.      * @param IEmbedmentOutputHandler EmbedmentOutputHandler Outputhandler the content MUST be parse to.
  90.     * @param string Identifier String at the end of the URL
  91.      * @return IEmbedmentOutputHandler 
  92.      * @access public
  93.      */
  94.     public function &ModifyContent($Identifier null&$EmbedmentOutputHandler){
  95.         //Get the image path
  96.         $ImagePath $this->Owner->GetCMS3Path("bin/" $this->GetPluginID("/Images/";
  97.         //Initialize body variable
  98.         $body "";
  99.  
  100.         //Handle possible uploads
  101.         if($Identifier == "Upload"){
  102.             //Check for error on HTTP level
  103.             if ($_FILES["image"]["error"0){
  104.                 //Determine the error:
  105.                         /* this is a modification of similar code found here: http://php.net/manual/en/features.file-upload.errors.php */
  106.                 switch ($_FILES["image"]["error"]{
  107.                     case UPLOAD_ERR_INI_SIZE:
  108.                         $msg "The uploaded file exceeds the upload_max_filesize directive (" ini_get("upload_max_filesize"") in php.ini.";
  109.                         break;
  110.                     case UPLOAD_ERR_FORM_SIZE:
  111.                         $msg "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
  112.                         break;
  113.                     case UPLOAD_ERR_PARTIAL:
  114.                         $msg "The uploaded file was only partially uploaded.";
  115.                         break;
  116.                     case UPLOAD_ERR_NO_FILE:
  117.                         $msg "No file was uploaded.";
  118.                         break;
  119.                     case UPLOAD_ERR_NO_TMP_DIR:
  120.                         $msg "Missing a temporary folder.";
  121.                         break;
  122.                     case UPLOAD_ERR_CANT_WRITE:
  123.                         $msg "Failed to write file to disk";
  124.                         break;
  125.                     default:
  126.                         $msg "Unknown File Error";
  127.                 }
  128.                 //print some info
  129.                 $body .= "<div id='info'><b>Upload failure:</b><br /> Filename: " $_FILES["image"]["name""<br />Filesize: " ($_FILES["image"]["size"1024" KiB<br />"$msg "</div><br />";
  130.                 //Log the error to system log
  131.                 $this->Owner->Log($this->GetPluginID()"Error during upload, HTTP/php error code: " $msg);
  132.             }else{
  133.                 //Delete file if exists
  134.                 if(file_exists($ImagePath $_FILES["image"]["name"])){
  135.                     if(!unlink($ImagePath $_FILES["image"]["name"])){
  136.                         $msg "sdfsd";
  137.                         $body .= "<div id='info'><b>Upload failure:</b><br /> Filename: " $_FILES["image"]["name""<br />Filesize: " ($_FILES["image"]["size"1024" KiB<br />"$msg "</div><br />";
  138.                         //Error deleting file, log it, continue and see what happens :)
  139.                         //Log to system log
  140.                         $this->Owner->Log($this->GetPluginID()"Unable to delete existing file, that prevents upload.");
  141.                     }
  142.                 }
  143.                 //Move uploaded file
  144.                 if(move_uploaded_file($_FILES["image"]["tmp_name"]$ImagePath $_FILES["image"]["name"])){
  145.                     //Print some file info:
  146.                     $body .= "<div id='info'><b>Upload successful:</b><br /> Filename: " $_FILES["image"]["name""<br />Filesize: " ($_FILES["image"]["size"1024" KiB</div><br />";
  147.                 }else{
  148.                     //Handle possible error
  149.                     $body .= "<div id='info'><b>Upload failure:</b><br /> Filename: " $_FILES["image"]["name""<br />Filesize: " ($_FILES["image"]["size"1024" KiB</div><br />";
  150.                     //Save to system log
  151.                     $this->Owner->Log($this->GetPluginID()"Unable to move uploaded file to image directory.");
  152.                 }
  153.             }
  154.         //Handle possible deletes
  155.         }elseif(strstr($Identifier,"/")){
  156.             //Find filename to delete
  157.             $eID explode("/" $Identifier2);
  158.             $File $eID[1];
  159.  
  160.             //Try to delete the file
  161.             if(unlink($ImagePath $File)){
  162.                 //Print  a message
  163.                 $body .= "<div id='info'><b>Deletion successfyl:</b><br />The file: " $File " has been deleted.</div>";
  164.             }else{
  165.                 //print an error message
  166.                 $body .= "<div id='info'><b>Deletion failure:</b><br />Deletion of the file " $File " has failed.</div>";
  167.                 //Write an error to system log
  168.                 $this->Owner->Log($this->GetPluginID()"Failed to delete " $File " on request by user.");
  169.             }
  170.         }
  171.  
  172.         //Create the form:
  173.         $body .= "<form method='post' enctype=\"multipart/form-data\" action='" $this->Owner->GetCMS3URL("System/Edit/" $this->GetPluginID("/Upload" "'><b>Upload:</b><br /><blockquote><input type='file' name='image' /> <input type='submit' value='Upload' /><br /><i>Settings in php.ini limits the filesize to " ini_get("upload_max_filesize""</i></blockquote><br /><b>Delete:</b><br /><blockquote>Click on an image to delete it.</blockquote></form><span id='images'>";
  174.  
  175.             //List images
  176.         $ShowURL $this->Owner->GetCMS3URL("bin/" $this->GetPluginID("/Images/";
  177.         $DelURL $this->Owner->GetCMS3URL("System/Edit/" $this->GetPluginID("/Delete/";
  178.         $Images scandir($ImagePath);
  179.         foreach($Images as $Image){
  180.             if($Image != "." && $Image != ".."){
  181.                  $body .= "<a href='" $DelURL $Image "' title='Click here to delete this image'><img src='" $ShowURL $Image ."' border='0' class='Gallery'></a>";
  182.             }
  183.         }
  184.         $body .= "</span>";
  185.  
  186.         //Create a header
  187.         $Head "<style>#info {border: 1px solid #37abc8; background-color: #87cdde; padding: 3px; margin: 5px;}\n #images a:hover img {background-color: #87cdde;}\n #images img {float: left; max-width: 200px; max-height: 200px; margin: 5px; padding: 3px;}form blockquote i {font-size: 12px;}</style>";
  188.         $Head .= "<script type=\"text/javascript\">";
  189.         $Head .= "dojo.require(\"dojo.lfx.*\");";
  190.         $Head .= "function HideStatus(){setTimeout(\"dojo.lfx.html.fadeOut('info', 2000).play()\", 3000);}";
  191.         $Head .= "dojo.addOnLoad(HideStatus);";
  192.         $Head .= "</script>";
  193.         
  194.         //Parse content
  195.         $EmbedmentOutputHandler->ExtentHeader($Head);
  196.         $EmbedmentOutputHandler->SetBody($body);
  197.  
  198.         //Return OutputHandler
  199.         return $EmbedmentOutputHandler;
  200.     }
  201.  
  202.     /**
  203.     * Gets an absolute address of an icon for this plugin
  204.     */
  205.     public function GetSystemIcon(){
  206.         return $this->Owner->GetCMS3URL("bin/" $this->GetPluginID("/icon.png";
  207.     }
  208.  
  209.     /**
  210.     * Gets menu entry string
  211.     */
  212.     public function GetSystemMenuEntry(){
  213.         return "Gallery";
  214.     }
  215.  
  216.  
  217.         //Implementation of IPlugin
  218.  
  219.     
  220.     /**
  221.      * Gets the pluginID of the plugin.
  222.      *
  223.      * @return string 
  224.      * @access public
  225.      */
  226.     public function GetPluginID){
  227.         return "Gallery";
  228.     }
  229.  
  230.  
  231.  
  232.         //IConfigure implementation
  233.  
  234.     
  235.     /**
  236.     * Gets an absolute address of an icon for this plugin
  237.     */
  238.     public function GetControlCenterIcon(){
  239.         return $this->Owner->GetCMS3URL("bin/" $this->GetPluginID("/icon.png";
  240.     }
  241.  
  242.     /**
  243.     * Gets menu entry string
  244.     */
  245.     public function GetControlCenterMenuEntry(){
  246.         //TODO: i18n
  247.         return "Configure " $this->GetPluginID();
  248.     }
  249.  
  250.     /**
  251.      * Gets a dojo based ajax guide for configuration of the plugin.
  252.      *
  253.      * @param IEmbedmentOutputHandler OutputHandler
  254.      * @return IEmbedmentOutputHandler 
  255.      * @access public
  256.      */
  257.     public function &Configure($Identifier null&$EmbedmentOutputHandler ){
  258.         //Load templates from files
  259.         $Head file_get_contents($this->Owner->GetCMS3Path("data/" $this->GetPluginID("/ConfigHead.htm");
  260.         $Body file_get_contents($this->Owner->GetCMS3Path("data/" $this->GetPluginID("/ConfigBody.htm");
  261.  
  262.         $Body str_replace("CMS3URL",$this->Owner->GetCMS3URL(,$Body);
  263.         $Body str_replace("NAMESPACE",$this->Owner->GetNamespace($this->GetPluginID()) ,$Body);
  264.  
  265.         //Parse the output
  266.         $EmbedmentOutputHandler->ExtentHeader($Head);
  267.         $EmbedmentOutputHandler->SetBody($Body);
  268.         return $EmbedmentOutputHandler;
  269.     }
  270.  
  271.         //ISystemCallback implementation
  272.  
  273.     
  274.     /**
  275.      * Handles a callback from ajax application.
  276.      *
  277.      * @param string data Data attached to this callback
  278.      * @return 
  279.      * @access public
  280.      */
  281.     public function SystemCallback$data ){
  282.         if($data == "SaveNamespace"){
  283.             //Find old namespace
  284.             $OldNS $this->Owner->GetNamespace($this->GetPluginID());
  285.  
  286.             //Does old namespace exist?
  287.             if(!$OldNS){
  288.                 //If does not exist
  289.  
  290.                 //TODO: remove illegal chars...
  291.                 //Try to register new namespace
  292.                 if($this->Owner->RegisterNamespace($_POST["Namespace"]$this->GetPluginID())){
  293.                     print "TRUE";
  294.                 }else{
  295.                         //TODO i18n
  296.                     print "Registration failed, make sure you have chosen a unique namespace.";
  297.                 }
  298.             }else{
  299.                 //If does exist
  300.                 
  301.                 //Unregister current namespace (should not be able to fail)
  302.                 if($this->Owner->UnregisterNamespace($OldNS)) //NOTE: a plugin can't have two namespaces due to database layout.
  303.                 {
  304.                     //TODO: remove illegal chars...
  305.                     //Try to register new namespace
  306.                     if($this->Owner->RegisterNamespace($_POST["Namespace"]$this->GetPluginID())){
  307.                         print "TRUE";
  308.                     }else{
  309.                         //Try to reregister old namespace
  310.                         if($this->Owner->RegisterNamespace($OldNS$this->GetPluginID())){
  311.                                 //TODO i18n
  312.                             print "Registration failed, make sure you have chosen a unique namespace. You still have the old namespace.";
  313.                         }else{
  314.                                 //TODO i18n
  315.                             print "Registration failed, make sure you have chosen a unique namespace. You don't have any namespace now.";
  316.                             //Not owning any namespace
  317.                         }
  318.                     }
  319.                 }else{
  320.                     //Could not unregister current namespace
  321.                         //TODO: i18n
  322.                     $msg "Unregistration of current namespace failed."
  323.                     print $msg;
  324.                     $this->Owner->Log($this->GetPluginID()$msg);
  325.                 }
  326.             }
  327.         }else{
  328.             //If identifier doesn't identify the request just give a soft 404..
  329.             print "Error: 404, data not found.";
  330.         }
  331.     }
  332.  
  333.  
  334.     //Implementation of ILinkable
  335.  
  336.     
  337.     /**
  338.      * Gets a list of links that the plugins exposes
  339.      * 
  340.      * Each entry in the array is an array with 3 fields, one for the display name, one for the URL of the page, and one for an array of sub entries. Each sub entry consits of 3 fields, just like the toplevel entries.
  341.      * 
  342.      * @access public
  343.      * @return array 
  344.      */
  345.     public function GetLinks){
  346.         //Get out namespace
  347.         if($Namespace $this->Owner->GetNamespace($this->GetPluginID())){
  348.             //Get a URL
  349.             $URL $this->Owner->GetCMS3URL($Namespace "/";
  350.  
  351.             //Create an plugin entry
  352.             $Links array($Namespace,$URLarray());
  353.  
  354.             //Get the image path
  355.             $ImagePath $this->Owner->GetCMS3Path("bin/" $this->GetPluginID("/Images/";
  356.  
  357.             //List images
  358.             $Images scandir($ImagePath);
  359.             foreach($Images as $Image){
  360.                 if($Image != "." && $Image != ".."){
  361.                      $Links[2][=array($Image$URL $Image null)
  362.                 }
  363.             }
  364.  
  365.             //Add toplevel array and return it
  366.             return array($Links);
  367.         }else{
  368.             return array(array($this->GetPluginID()nullnull));
  369.         }
  370.     }
  371.  
  372.     //Implementation of IRemove
  373.  
  374.     
  375.     /**
  376.      * Unregisters namespace
  377.      *
  378.      * @return bool True/false depending in success
  379.      * @access public
  380.      */
  381.     public function Remove)
  382.     {
  383.         $this->Owner->UnregisterNamespace($this->Owner->GetNamespace($this->GetPluginID()));
  384.         return true;
  385.     }
  386. }
  387.  
  388.  
  389. ?>

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