home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / cms / template.php < prev    next >
PHP Script  |  2004-03-08  |  8KB  |  292 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13. //load Group-Office
  14. require("../../Group-Office.php");
  15.  
  16. //load the CMS module class library
  17. require($GO_CONFIG->class_path.'cms.class.inc');
  18. $cms = new cms();
  19.  
  20. //authenticate the user
  21. $GO_SECURITY->authenticate();
  22.  
  23. //see if the user has access to this module
  24. //for this to work there must be a module named 'example'
  25. $GO_MODULES->authenticate('cms');
  26.  
  27. //get the language file
  28. require($GO_LANGUAGE->get_language_file('cms'));
  29.  
  30. $task = isset($_REQUEST['task']) ? $_REQUEST['task'] : '';
  31. $template_id = isset($_REQUEST['template_id']) ? $_REQUEST['template_id'] : 0;
  32. if ($template_id > 0)
  33. {
  34.     $template = $cms->get_template($template_id);
  35.  
  36.     if (!$GO_SECURITY->has_permission($GO_SECURITY->user_id, $template['acl_write']))
  37.     {
  38.         header('Location: '.$GO_CONFIG->host.'error_docs/403.php');
  39.         exit();
  40.     }
  41. }
  42. //create a tab window
  43. $tpl_table = new tabtable('template_tab',$cms_theme,'750','450', '100','', true);
  44.  
  45. switch($task)
  46. {
  47.     case 'save_template':
  48.         if($_SERVER['REQUEST_METHOD'] == 'POST')
  49.         {
  50.             $name = trim($_POST['name']);
  51.             $restrict_editor = isset($_POST['restrict_editor']) ? $_POST['restrict_editor'] : '';
  52.             if ($name == '')
  53.             {
  54.                 $feedback= '<p class="Error">'.$error_missing_field.'</p>';
  55.             }else
  56.             {
  57.                 if (isset($_FILES['additional_style_file']) && is_uploaded_file($_FILES['additional_style_file']['tmp_name']))
  58.                 {
  59.                     $fp = fopen($_FILES['additional_style_file']['tmp_name'], 'r');
  60.                     $additional_style = addslashes(fread($fp, $_FILES['additional_style_file']['size']));
  61.                     fclose($fp);
  62.                     unlink($_FILES['additional_style_file']['tmp_name']);
  63.                 }else
  64.                 {
  65.                     $additional_style = $_POST['additional_style'];
  66.                 }
  67.  
  68.                 if (isset($_FILES['style_file']) && is_uploaded_file($_FILES['style_file']['tmp_name']))
  69.                 {
  70.                     $fp = fopen($_FILES['style_file']['tmp_name'], 'r');
  71.                     $style = addslashes(fread($fp, $_FILES['style_file']['size']));
  72.                     fclose($fp);
  73.                     unlink($_FILES['style_file']['tmp_name']);
  74.                 }else
  75.                 {
  76.                     $style = $_POST['style'];
  77.                 }
  78.  
  79.                 if ($template_id > 0)
  80.                 {
  81.                     $template = $cms->get_template_by_name($GO_SECURITY->user_id, $name);
  82.                     if ($template && $template['id'] != $template_id)
  83.                     {
  84.                         $feedback = '<p class="Error">'.$fbNameExists.'</p>';
  85.                     }else
  86.                     {
  87.                         if (!$cms->update_template($template_id, $name, $style, $additional_style, $restrict_editor))
  88.                         {
  89.                             $feedback = '<p class="Error">'.$strSaveError.'</p>';
  90.                         }else
  91.                         {
  92.                             $template = $cms->get_template($template_id);
  93.                         }
  94.                     }
  95.                 }else
  96.                 {
  97.                     if ($cms->get_template_by_name($GO_SECURITY->user_id, $name))
  98.                     {
  99.                         $feedback = '<p class="Error">'.$fbNameExists.'</p>';
  100.                     }else
  101.                     {
  102.                         if (!$acl_read = $GO_SECURITY->get_new_acl('cms template read: '.$name))
  103.                         {
  104.                             die($strAclError);
  105.                         }
  106.  
  107.                         if (!$acl_write = $GO_SECURITY->get_new_acl('cms template write: '.$name))
  108.                         {
  109.                             $GO_SECURITY->delete_acl($acl_read);
  110.                             die($strAclError);
  111.                         }
  112.  
  113.                         if (!$GO_SECURITY->add_user_to_acl($GO_SECURITY->user_id, $acl_write))
  114.                         {
  115.                             $GO_SECURITY->delete_acl($acl_read);
  116.                             $GO_SECURITY->delete_acl($acl_write);
  117.                             die($strAclError);
  118.                         }
  119.  
  120.                         if(!$template_id = $cms->add_template($GO_SECURITY->user_id, $name, $style, $additional_style, $restrict_editor, $acl_read, $acl_write))
  121.                         {
  122.                             $GO_SECURITY->delete_acl($acl_read);
  123.                             $GO_SECURITY->delete_acl($acl_write);
  124.                             $feedback = '<p class="Error">'.$strSaveError.'</p>';
  125.                         }else
  126.                         {
  127.                             $template = $cms->get_template($template_id);
  128.                         }
  129.                     }
  130.                 }
  131.                 if ($_POST['close'] == 'true')
  132.                 {
  133.                     header('Location: index.php?tabindex=2');
  134.                     exit();
  135.                 }
  136.  
  137.             }
  138.         }
  139.     break;
  140.  
  141.     case 'save_template_item':
  142.         $task='template_item';
  143.         if($_SERVER['REQUEST_METHOD'] == 'POST')
  144.         {
  145.             $name = trim($_POST['name']);
  146.             if ($name == '')
  147.             {
  148.                 $feedback= '<p class="Error">'.$error_missing_field.'</p>';
  149.             }else
  150.             {
  151.                 if (isset($_FILES['content_file']) && is_uploaded_file($_FILES['content_file']['tmp_name']))
  152.                 {
  153.                     $fp = fopen($_FILES['content_file']['tmp_name'], 'r');
  154.                     $content = addslashes(fread($fp, $_FILES['content_file']['size']));
  155.                     fclose($fp);
  156.                     unlink($_FILES['content_file']['tmp_name']);
  157.                 }else
  158.                 {
  159.                         $content = $_POST['content'];
  160.                 }
  161.  
  162.                 $template_item_id = isset($_POST['template_item_id']) ? $_POST['template_item_id'] : 0;
  163.  
  164.                 if ($template_item_id > 0)
  165.                 {
  166.                     if ($template_item = $cms->get_template_by_name($template_id, $name) && $template_item['id'] != $template_item_id)
  167.                     {
  168.                         $feedback = '<p class="Error">'.$fbNameExists.'</p>';
  169.                     }else
  170.                     {
  171.                         if (!$cms->update_template_item($template_item_id, $name, $content))
  172.                         {
  173.                             $feedback = '<p class="Error">'.$strSaveError.'</p>';
  174.                         }
  175.                     }
  176.                 }else
  177.                 {
  178.                     if ($cms->get_template_item_by_name($template_id, $name))
  179.                     {
  180.                         $feedback = '<p class="Error">'.$fbNameExists.'</p>';
  181.                     }else
  182.                     {
  183.                         if(!$template_item_id = $cms->add_template_item($template_id, $name, $content))
  184.                         {
  185.                             $feedback = '<p class="Error">'.$strSaveError.'</p>';
  186.                         }
  187.                     }
  188.                 }
  189.                 if ($cms->get_template_items($template_id) == 1)
  190.                 {
  191.                     $cms->set_main_template_item($template_id, $template_item_id);
  192.                 }
  193.                 if ($_POST['close'] == 'true')
  194.                 {
  195.                     $task = '';
  196.                 }
  197.             }
  198.         }
  199.     break;
  200.  
  201.     case 'upload':
  202.  
  203.         if ($_SERVER['REQUEST_METHOD'] == 'POST')
  204.         {
  205.             $tpl_table->set_active_tab(2);
  206.             $task = 'list';
  207.             if (isset($_FILES['file']))
  208.             {
  209.                 require_once($GO_CONFIG->class_path.'filetypes.class.inc');
  210.                 $filetypes = new filetypes();
  211.                 for ($i=0;$i<count($_FILES['file']);$i++)
  212.                 {
  213.                     if (is_uploaded_file($_FILES['file']['tmp_name'][$i]))
  214.                     {
  215.                         $extension = get_extension($_FILES['file']['name'][$i]);
  216.                         if (!$filetypes->get_type($extension))
  217.                         {
  218.                             $filetypes->add_type($extension, $_FILES['file']['type']);
  219.                         }
  220.  
  221.                         $name = $_FILES['file']['name'][$i];
  222.                         while ($cms->template_file_exists($template_id, $name))
  223.                         {
  224.                             $x++;
  225.                             $name = strip_extension($_FILES['file']['name'][$i]).' ('.$x.').'.get_extension($_FILES['file']['name'][$i]);
  226.                         }
  227.  
  228.                         $fp = fopen($_FILES['file']['tmp_name'][$i], 'r');
  229.                         $content = addslashes(fread($fp, $_FILES['file']['size'][$i]));
  230.                         fclose($fp);
  231.                         unlink($_FILES['file']['tmp_name'][$i]);
  232.                         $file_id = $cms->add_template_file($template_id, $name, $content);
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.     break;
  238.  
  239.     case "files":
  240.         $tpl_table->set_active_tab(2);
  241.     break;
  242.     case 'save_main_template_item':
  243.         $cms->set_main_template_item($template_id, $_POST['main_template_item_id']);
  244.         if ($_POST['close'] == 'true')
  245.         {
  246.             header('Location: index.php?tabindex=2');
  247.             exit();
  248.         }
  249.     break;
  250. }
  251.  
  252. //set the page title for the content file
  253. $page_title = $lang_modules['cms'];
  254.  
  255. //require the content file. This will draw the logo's and the menu
  256. require($GO_THEME->theme_path."header.inc");
  257.  
  258. ?>
  259. <form name="cms" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
  260. <input type="hidden" name="template_id" value="<?php echo $template_id; ?>" />
  261.  
  262. <?php
  263. switch ($task)
  264. {
  265.     case 'upload':
  266.         require('upload_template_file.inc');
  267.     break;
  268.  
  269.     default:
  270.         if ($template_id > 0)
  271.         {
  272.             $tpl_table->add_tab('template.inc', $strProperties);
  273.             $tpl_table->add_tab('template_items.inc', $cms_templates);
  274.             $tpl_table->add_tab('template_files.inc', $cms_files);
  275.             $tpl_table->add_tab('template_read_permissions.inc', $strReadRights);
  276.             $tpl_table->add_tab('template_write_permissions.inc', $strWriteRights);
  277.             $tpl_table->print_head();
  278.             require($tpl_table->get_active_tab_id());
  279.         }else
  280.         {
  281.             $tpl_table->print_head();
  282.             require('template.inc');
  283.         }
  284.         $tpl_table->print_foot();
  285.     break;
  286. }
  287. ?>
  288.  
  289. </form>
  290. <?php
  291. require($GO_THEME->theme_path."footer.inc");
  292. ?>