home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / modules / system / admin / tplsets / main.php < prev    next >
Encoding:
PHP Script  |  2007-09-09  |  82.6 KB  |  1,407 lines

  1. <?php
  2. // $Id: main.php 1029 2007-09-09 03:49:25Z phppp $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
  29. // Project: The XOOPS Project                                                //
  30. // ------------------------------------------------------------------------- //
  31.  
  32. if ( !is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
  33.     exit("Access Denied");
  34. } else {
  35.     $op = 'list';
  36.     if (isset($_POST)) {
  37.         foreach ( $_POST as $k => $v ) {
  38.             ${$k} = $v;
  39.         }
  40.     }
  41.     if (isset($_GET['op'])) {
  42.         $op = trim($_GET['op']);
  43.         $id = $moddir = $file = $type = $tplset = null;
  44.         if (isset($_GET['id'])) {
  45.             $id = intval($_GET['id']);
  46.         }
  47.         if (isset($_GET['moddir'])) {
  48.             $moddir = trim($_GET['moddir']);
  49.         }
  50.         if (isset($_GET['file'])) {
  51.             $file = trim($_GET['file']);
  52.         }
  53.         if (isset($_GET['type'])) {
  54.             $type = trim($_GET['type']);
  55.         }
  56.         if (isset($_GET['tplset'])) {
  57.             $tplset = trim($_GET['tplset']);
  58.         }
  59.     }
  60.  
  61.     if ($op == 'edittpl_go') {
  62.         if (isset($previewtpl)) {
  63.             $op = 'previewtpl';
  64.         }
  65.     }
  66.  
  67.     switch ($op) {
  68.     case 'list':
  69.         $tplset_handler =& xoops_gethandler('tplset');
  70.         $tplsets =& $tplset_handler->getObjects();
  71.         xoops_cp_header();
  72.         echo '<h4 style="text-align:left">'._MD_TPLMAIN.'</h4>';
  73.         $installed = array();
  74.         $tpltpl_handler =& xoops_gethandler('tplfile');
  75.         $installed_mods = $tpltpl_handler->getModuleTplCount('default');
  76.         $tcount = count($tplsets);
  77.         echo '<table width="100%" cellspacing="1" class="outer"><tr align="center"><th width="25%">'._MD_THMSETNAME.'</th><th>'._MD_CREATED.'</th><th>'._MD_TEMPLATES.'</th><th>'._MD_ACTION.'</th><th> </th></tr>';
  78.         $class = 'even';
  79.         for ($i = 0; $i < $tcount; $i++) {
  80.             $tplsetname = $tplsets[$i]->getVar('tplset_name');
  81.             $installed_themes[] = $tplsetname;
  82.             $class = ($class == 'even') ? 'odd' : 'even';
  83.             echo '<tr class="'.$class.'" align="center"><td class="head">'.$tplsetname.'<br /><br /><span style="font-weight:normal;">'.$tplsets[$i]->getVar('tplset_desc').'</span></td><td>'.formatTimestamp($tplsets[$i]->getVar('tplset_created'), 's').'</td><td align="left"><ul>';
  84.             $tplstats = $tpltpl_handler->getModuleTplCount($tplsetname);
  85.             if (count($tplstats) > 0) {
  86.                 $module_handler =& xoops_gethandler('module');
  87.                 echo '<ul>';
  88.                 foreach ($tplstats as $moddir => $filecount) {
  89.                     $module =& $module_handler->getByDirname($moddir);
  90.                     if (is_object($module)) {
  91.                         if ($installed_mods[$moddir] > $filecount) {
  92.                             $filecount = '<span style="color:#ff0000;">'.$filecount.'</span>';
  93.                         }
  94.                     echo '<li>'.$module->getVar('name').' [<a href="admin.php?fct=tplsets&op=listtpl&tplset='.$tplsetname.'&moddir='.$moddir.'">'._LIST.'</a> (<b>'.$filecount.'</b>)]</li>';
  95.                     }
  96.                     unset($module);
  97.                 }
  98.                 $not_installed = array_diff(array_keys($installed_mods), array_keys($tplstats));
  99.             } else {
  100.                 $not_installed =& array_keys($installed_mods);
  101.             }
  102.             foreach ($not_installed as $ni) {
  103.                 $module =& $module_handler->getByDirname($ni);
  104.                 echo '<li>'.$module->getVar('name').' [<a href="admin.php?fct=tplsets&op=listtpl&tplset='.$tplsetname.'&moddir='.$ni.'">'._LIST.'</a> (<span style="color:#ff0000; font-weight: bold;">0</span>)] [<a href="admin.php?fct=tplsets&op=generatemod&tplset='.$tplsetname.'&moddir='.$ni.'">'._MD_GENERATE.'</a>]</li>';
  105.             }
  106.             echo '</ul></td><td>';
  107.             echo '[<a href="admin.php?fct=tplsets&op=download&method=tar&tplset='.$tplsetname.'">'._MD_DOWNLOAD.'</a>]<br />[<a href="admin.php?fct=tplsets&op=clone&tplset='.$tplsetname.'">'._CLONE.'</a>]';
  108.             if ($tplsetname != 'default' && $tplsetname != $xoopsConfig['template_set']) {
  109.                 echo '<br />[<a href="admin.php?fct=tplsets&op=delete&tplset='.$tplsetname.'">'._DELETE.'</a>]';
  110.             }
  111.             echo '</td>';
  112.             if ($tplsetname == $xoopsConfig['template_set']) {
  113.                 echo '<td><img src="'.XOOPS_URL.'/modules/system/images/check.gif" alt="'._MD_DEFAULTTHEME.'" /></td>';
  114.             } else {
  115.                 echo '<td> </td>';
  116.             }
  117.             echo '</tr>';
  118.         }
  119.         echo '</table><br />';
  120.  
  121.         include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
  122.         $form = new XoopsThemeForm(_MD_UPLOADTAR, 'tplupload_form', 'admin.php', 'post', true);
  123.         $form->setExtra('enctype="multipart/form-data"');
  124.         $form->addElement(new XoopsFormFile(_MD_CHOOSETAR.'<br /><span style="color:#ff0000;">'._MD_ONLYTAR.'</span>', 'tpl_upload', 1000000));
  125.         $form->addElement(new XoopsFormText(_MD_NTHEMENAME.'<br /><span style="font-weight:normal;">'._MD_ENTERTH.'</span>', 'tplset_name', 20, 50));
  126.         $form->addElement(new XoopsFormHidden('op', 'uploadtar_go'));
  127.         $form->addElement(new XoopsFormHidden('fct', 'tplsets'));
  128.         $form->addElement(new XoopsFormButton('', 'upload_button', _MD_UPLOAD, 'submit'));
  129.         $form->display();
  130.         xoops_cp_footer();
  131.         break;
  132.     case 'listtpl':
  133.         $tplset = trim($_GET['tplset']);
  134.         if ($tplset == '') {
  135.             redirect_header('admin.php?fct=tplsets',1);
  136.         }
  137.         if ($moddir == '') {
  138.             redirect_header('admin.php?fct=tplsets',1);
  139.         }
  140.         xoops_cp_header();
  141.         $module_handler =& xoops_gethandler('module');
  142.         $module =& $module_handler->getByDirname($moddir);
  143.         $modname = $module->getVar('name');
  144.         echo '<a href="admin.php?fct=tplsets">'. _MD_TPLMAIN .'</a> <span style="font-weight:bold;">»»</span> '.$tplset.'<br /><br />';
  145.  
  146.         echo '<h4>'.$modname.'</h4><form action="admin.php" method="post" enctype="multipart/form-data"><table width="100%" class="outer" cellspacing="1"><tr><th width="40%">'._MD_FILENAME.'</th><th>'._MD_LASTMOD.'</th>';
  147.         if ($tplset != 'default') {
  148.             echo '<th>'._MD_LASTIMP.'</th><th colspan="2">'._MD_ACTION.'</th></tr>';
  149.         } else {
  150.             echo '<th>'._MD_ACTION.'</th></tr>';
  151.         }
  152.         $tpltpl_handler =& xoops_gethandler('tplfile');
  153.         // get files that are already installed
  154.         $templates =& $tpltpl_handler->find($tplset, 'module', null, $moddir);
  155.         $inst_files = array();
  156.         $tcount = count($templates);
  157.         for ($i = 0; $i < $tcount; $i++) {
  158.             if ($i % 2 == 0) {
  159.                 $class = 'even';
  160.             } else {
  161.                 $class = 'odd';
  162.             }
  163.             $last_modified = $templates[$i]->getVar('tpl_lastmodified');
  164.             $last_imported = $templates[$i]->getVar('tpl_lastimported');
  165.             $last_imported_f = ($last_imported > 0) ? formatTimestamp($last_imported, 'l') : '';
  166.             echo  '<tr class="'.$class.'"><td class="head">'.$templates[$i]->getVar('tpl_file').'<br /><br /><span style="font-weight:normal;">'.$templates[$i]->getVar('tpl_desc').'</span></td><td>'.formatTimestamp($last_modified, 'l').'</td>';
  167.             $filename = $templates[$i]->getVar('tpl_file');
  168.             if ($tplset != 'default') {
  169.                 $physical_file = XOOPS_THEME_PATH.'/'.$tplset.'/templates/'.$moddir.'/'.$filename;
  170.                 if (file_exists($physical_file)) {
  171.                     $mtime = filemtime($physical_file);
  172.                     if ($last_imported < $mtime) {
  173.                         if ($mtime > $last_modified) {
  174.                             $bg = '#ff9999';
  175.                         } elseif($mtime > $last_imported) {
  176.                             $bg = '#99ff99';
  177.                         }
  178.                         echo '<td style="background-color:'.$bg.';">'.$last_imported_f.' [<a href="admin.php?fct=tplsets&tplset='.$tplset.'&moddir='.$moddir.'&op=importtpl&id='.$templates[$i]->getVar('tpl_id').'">'._MD_IMPORT.'</a>]';
  179.                     } else {
  180.                         echo '<td>'.$last_imported_f;
  181.                     }
  182.                 } else {
  183.                     echo '<td>'.$last_imported_f;
  184.                 }
  185.                 echo '</td><td>[<a href="admin.php?fct=tplsets&op=edittpl&id='.$templates[$i]->getVar('tpl_id').'">'._EDIT.'</a>] [<a href="admin.php?fct=tplsets&op=deletetpl&id='.$templates[$i]->getVar('tpl_id').'">'._DELETE.'</a>] [<a href="admin.php?fct=tplsets&op=downloadtpl&id='.$templates[$i]->getVar('tpl_id').'">'._MD_DOWNLOAD.'</a>]</td><td align="right"><input type="file" name="'.$filename.'" id="'.$filename.'" /><input type="hidden" name="xoops_upload_file[]" id="xoops_upload_file[]" value="'.$filename.'" /><input type="hidden" name="old_template['.$filename.']" value="'.$templates[$i]->getVar('tpl_id').'" /></td>';
  186.             } else {
  187.                 echo '<td>[<a href="admin.php?fct=tplsets&op=edittpl&id='.$templates[$i]->getVar('tpl_id').'">'._MD_VIEW.'</a>] [<a href="admin.php?fct=tplsets&op=downloadtpl&id='.$templates[$i]->getVar('tpl_id').'">'._MD_DOWNLOAD.'</a>]</td>';
  188.             }
  189.             echo '</tr>'."\n";
  190.             $inst_files[] = $filename;
  191.         }
  192.         if ($tplset != 'default') {
  193.             include_once XOOPS_ROOT_PATH.'/class/xoopslists.php';
  194.             // get difference between already installed files and the files under modules directory. which will be recognized as files that are not installed
  195.             $notinst_files = array_diff(XoopsLists::getFileListAsArray(XOOPS_ROOT_PATH.'/modules/'.$moddir.'/templates/'), $inst_files);
  196.             foreach ($notinst_files as $nfile) {
  197.                 if ($nfile != 'index.html') {
  198.                     echo  '<tr><td style="background-color:#FFFF99; padding: 5px;">'.$nfile.'</td><td style="background-color:#FFFF99; padding: 5px;"> </td><td style="background-color:#FFFF99; padding: 5px;">';
  199.                     $physical_file = XOOPS_THEME_PATH.'/'.$tplset.'/templates/'.$moddir.'/'.$nfile;
  200.                     if (file_exists($physical_file)) {
  201.                         echo '[<a href="admin.php?fct=tplsets&moddir='.$moddir.'&tplset='.$tplset.'&op=importtpl&file='.urlencode($nfile).'">'._MD_IMPORT.'</a>]';
  202.                     } else {
  203.                         echo ' ';
  204.                     }
  205.                     echo '</td><td style="background-color:#FFFF99; padding: 5px;">[<a href="admin.php?fct=tplsets&moddir='.$moddir.'&tplset='.$tplset.'&op=generatetpl&type=module&file='.urlencode($nfile).'">'._MD_GENERATE.'</a>]</td><td style="background-color:#FFFF99; padding: 5px; text-align:right;"><input type="file" name="'.$nfile.'" id="'.$nfile.'" /><input type="hidden" name="xoops_upload_file[]" id="xoops_upload_file[]" value="'.$nfile.'" /></td></tr>'."\n";
  206.                 }
  207.             }
  208.         }
  209.         echo '</table><br /><table width="100%" class="outer" cellspacing="1"><tr><th width="40%">'._MD_FILENAME.'</th><th>'._MD_LASTMOD.'</th>';
  210.         if ($tplset != 'default') {
  211.             echo '<th>'._MD_LASTIMP.'</th><th colspan="2">'._MD_ACTION.'</th></tr>';
  212.         } else {
  213.             echo '<th>'._MD_ACTION.'</th></tr>';
  214.         }
  215.         $btemplates =& $tpltpl_handler->find($tplset, 'block', null, $moddir);
  216.         $binst_files = array();
  217.         $btcount = count($btemplates);
  218.         for ($j = 0; $j < $btcount; $j++) {
  219.             $last_imported = $btemplates[$j]->getVar('tpl_lastimported');
  220.             $last_imported_f = ($last_imported > 0) ? formatTimestamp($last_imported, 'l') : '';
  221.             $last_modified = $btemplates[$j]->getVar('tpl_lastmodified');
  222.             if ($j % 2 == 0) {
  223.                 $class = 'even';
  224.             } else {
  225.                 $class = 'odd';
  226.             }
  227.             echo  '<tr class="'.$class.'"><td class="head"><span style="font-weight:bold;">'.$btemplates[$j]->getVar('tpl_file').'</span><br /><br /><span style="font-weight:normal;">'.$btemplates[$j]->getVar('tpl_desc').'</span></td><td>'.formatTimestamp($last_modified, 'l').'</td>';
  228.             $filename = $btemplates[$j]->getVar('tpl_file');
  229.             $physical_file = XOOPS_THEME_PATH.'/'.$tplset.'/templates/'.$moddir.'/blocks/'.$filename;
  230.             if ($tplset != 'default') {
  231.                 if (file_exists($physical_file)) {
  232.                     $mtime = filemtime($physical_file);
  233.                     if ($last_imported < $mtime) {
  234.                         if ($mtime > $last_modified) {
  235.                             $bg = '#ff9999';
  236.                         } elseif($mtime > $last_imported) {
  237.                             $bg = '#99ff99';
  238.                         }
  239.                         echo '<td style="background-color:'.$bg.';">'.$last_imported_f.' [<a href="admin.php?fct=tplsets&tplset='.$tplset.'&op=importtpl&moddir='.$moddir.'&id='.$btemplates[$j]->getVar('tpl_id').'">'._MD_IMPORT.'</a>]';
  240.                     } else {
  241.                         echo '<td>'.$last_imported_f;
  242.                     }
  243.                 } else {
  244.                     echo '<td>'.$last_imported_f;
  245.                 }
  246.                 echo '</td><td>[<a href="admin.php?fct=tplsets&op=edittpl&id='.$btemplates[$j]->getVar('tpl_id').'">'._EDIT.'</a>] [<a href="admin.php?fct=tplsets&op=deletetpl&id='.$btemplates[$j]->getVar('tpl_id').'">'._DELETE.'</a>] [<a href="admin.php?fct=tplsets&op=downloadtpl&id='.$btemplates[$j]->getVar('tpl_id').'">'._MD_DOWNLOAD.'</a>]</td><td align="right"><input type="file" name="'.$filename.'" id="'.$filename.'" /><input type="hidden" name="xoops_upload_file[]" id="xoops_upload_file[]" value="'.$filename.'" /><input type="hidden" name="old_template['.$filename.']" value="'.$btemplates[$j]->getVar('tpl_id').'" /></td>';
  247.             } else {
  248.                 echo '<td>[<a href="admin.php?fct=tplsets&op=edittpl&id='.$btemplates[$j]->getVar('tpl_id').'">'._MD_VIEW.'</a>] [<a href="admin.php?fct=tplsets&op=downloadtpl&id='.$btemplates[$j]->getVar('tpl_id').'">'._MD_DOWNLOAD.'</a>]</td>';
  249.             }
  250.             echo '</tr>'."\n";
  251.             $binst_files[] = $filename;
  252.         }
  253.         if ($tplset != 'default') {
  254.             include_once XOOPS_ROOT_PATH.'/class/xoopslists.php';
  255.             $bnotinst_files = array_diff(XoopsLists::getFileListAsArray(XOOPS_ROOT_PATH.'/modules/'.$moddir.'/templates/blocks/'), $binst_files);
  256.             foreach ($bnotinst_files as $nfile) {
  257.                 if ($nfile != 'index.html') {
  258.                     echo  '<tr style="background-color:#FFFF99;"><td style="background-color:#FFFF99; padding: 5px;">'.$nfile.'</td><td style="background-color:#FFFF99; padding: 5px;"> </td><td style="background-color:#FFFF99; padding: 5px;">';
  259.                     $physical_file = XOOPS_THEME_PATH.'/'.$tplset.'/templates/'.$moddir.'/blocks/'.$nfile;
  260.                     if (file_exists($physical_file)) {
  261.                         echo '[<a href="admin.php?fct=tplsets&moddir='.$moddir.'&tplset='.$tplset.'&op=importtpl&file='.urlencode($nfile).'">'._MD_IMPORT.'</a>]';
  262.                     } else {
  263.                         echo ' ';
  264.                     }
  265.                     echo '</td><td style="background-color:#FFFF99; padding: 5px;">[<a href="admin.php?fct=tplsets&moddir='.$moddir.'&tplset='.$tplset.'&op=generatetpl&type=block&file='.urlencode($nfile).'">'._MD_GENERATE.'</a>]</td><td style="background-color:#FFFF99; padding: 5px; text-align: right"><input type="file" name="'.$nfile.'" id="'.$nfile.'" /><input type="hidden" name="xoops_upload_file[]" id="xoops_upload_file[]" value="'.$nfile.'" /></td></tr>'."\n";
  266.                 }
  267.             }
  268.         }
  269.         echo '</table>';
  270.         if ($tplset != 'default') {
  271.             echo '<div style="text-align: right; margin-top: 5px;"><input type="hidden" name="fct" value="tplsets" /><input type="hidden" name="op" value="update" />'.$GLOBALS['xoopsSecurity']->getTokenHTML().'<input type="hidden" name="moddir" value="'.$moddir.'" /><input type="hidden" name="tplset" value="'.$tplset.'" /><input type="submit" value="'._MD_UPLOAD.'" /></div></form>';
  272.         }
  273.         xoops_cp_footer();
  274.         break;
  275.     case 'edittpl':
  276.         if ($id <= 0) {
  277.             redirect_header('admin.php?fct=tplsets', 1);
  278.         }
  279.         $tpltpl_handler =& xoops_gethandler('tplfile');
  280.         $tplfile =& $tpltpl_handler->get($id, true);
  281.         if (is_object($tplfile)) {
  282.             $tplset = $tplfile->getVar('tpl_tplset');
  283.             $tform = array('tpl_tplset' => $tplset, 'tpl_id' => $id, 'tpl_file' => $tplfile->getVar('tpl_file'), 'tpl_desc' => $tplfile->getVar('tpl_desc'), 'tpl_lastmodified' => $tplfile->getVar('tpl_lastmodified'), 'tpl_source' => $tplfile->getVar('tpl_source', 'E'), 'tpl_module' => $tplfile->getVar('tpl_module'));
  284.             include_once XOOPS_ROOT_PATH.'/modules/system/admin/tplsets/tplform.php';
  285.             xoops_cp_header();
  286.             echo '<a href="admin.php?fct=tplsets">'. _MD_TPLMAIN .'</a> <span style="font-weight:bold;">»»</span> <a href="./admin.php?fct=tplsets&op=listtpl&moddir='.$tplfile->getVar('tpl_module').'&tplset='.$tplset.'">'.$tplset.'</a> <span style="font-weight:bold;">»»</span> '._MD_EDITTEMPLATE.'<br /><br />';
  287.             $form->display();
  288.             xoops_cp_footer();
  289.             exit();
  290.         } else {
  291.             $err[] = 'Selected template (ID: $id) does not exist';
  292.         }
  293.         xoops_cp_header();
  294.         xoops_error($err);
  295.         echo '<br /><a href="admin.php?fct=tplsets">'._MD_AM_BTOTADMIN.'</a>';
  296.         xoops_cp_footer();
  297.         break;
  298.     case 'edittpl_go':
  299.         if ($id <= 0 | !$GLOBALS['xoopsSecurity']->check()) {
  300.             redirect_header('admin.php?fct=tplsets', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  301.         }
  302.         $tpltpl_handler =& xoops_gethandler('tplfile');
  303.         $tplfile =& $tpltpl_handler->get($id, true);
  304.         $err = array();
  305.         if (!is_object($tplfile)) {
  306.             $err[] = 'Selected template (ID: $id) does not exist';
  307.         } else {
  308.             if ($tplfile->getVar('tpl_tplset') != 'default') {
  309.                 $tplfile->setVar('tpl_source', $html);
  310.                 $tplfile->setVar('tpl_lastmodified', time());
  311.  
  312.                 if (!$tpltpl_handler->insert($tplfile)) {
  313.                     $err[] = 'Could not insert template file to the database.';
  314.                 } else {
  315.                     include_once XOOPS_ROOT_PATH.'/class/template.php';
  316.                     $xoopsTpl = new XoopsTpl();
  317.                     if ($xoopsTpl->is_cached('db:'.$tplfile->getVar('tpl_file'))) {
  318.                         if (!$xoopsTpl->clear_cache('db:'.$tplfile->getVar('tpl_file'))) {
  319.                         }
  320.                     }
  321.                     if ($tplfile->getVar('tpl_tplset') == $xoopsConfig['template_set']) {
  322.                         xoops_template_touch($id);
  323.                     }
  324.                 }
  325.             } else {
  326.                 $err[] = 'Default template files cannot be edited.';
  327.             }
  328.         }
  329.         if (count($err) == 0) {
  330.             if (!empty($moddir)) {
  331.                 redirect_header('admin.php?fct=tplsets&op=edittpl&id='.$tplfile->getVar('tpl_id'), 2, _MD_AM_DBUPDATED);
  332.             } elseif (isset($redirect)) {
  333.                 redirect_header('admin.php?fct=tplsets&tplset='.$tplfile->getVar('tpl_tplset').'&op='.trim($redirect), 2, _MD_AM_DBUPDATED);
  334.             } else {
  335.                 redirect_header('admin.php?fct=tplsets', 2, _MD_AM_DBUPDATED);
  336.             }
  337.         }
  338.         xoops_cp_header();
  339.         xoops_error($err);
  340.         echo '<br /><a href="admin.php?fct=tplsets">'._MD_AM_BTOTADMIN.'</a>';
  341.         xoops_cp_footer();
  342.         break;
  343.     case 'deletetpl':
  344.         xoops_cp_header();
  345.         xoops_confirm(array('id' => $id, 'op' => 'deletetpl_go', 'fct' => 'tplsets'), 'admin.php', _MD_RUSUREDELTPL, _YES);
  346.         xoops_cp_footer();
  347.         break;
  348.     case 'deletetpl_go':
  349.         if ($id <= 0 | !$GLOBALS['xoopsSecurity']->check()) {
  350.             redirect_header('admin.php?fct=tplsets', 1, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  351.         }
  352.         $tpltpl_handler =& xoops_gethandler('tplfile');
  353.         $tplfile =& $tpltpl_handler->get($id);
  354.         $err = array();
  355.         if (!is_object($tplfile)) {
  356.             $err[] = 'Selected template (ID: $id) does not exist';
  357.         } else {
  358.             if ($tplfile->getVar('tpl_tplset') != 'default') {
  359.                 if (!$tpltpl_handler->delete($tplfile)) {
  360.                     $err[] = 'Could not delete '.$tplfile->getVar('tpl_file').' from the database.';
  361.                 } else {
  362.                     // need to compile default xoops template
  363.                     if ($tplfile->getVar('tpl_tplset') == $xoopsConfig['template_set']) {
  364.                         $defaulttpl =& $tpltpl_handler->find('default', $tplfile->getVar('tpl_type'), $tplfile->getVar('tpl_refid'), null, $tplfile->getVar('tpl_file'));
  365.                         if (count($defaulttpl) > 0) {
  366.                             include_once XOOPS_ROOT_PATH.'/class/template.php';
  367.                             xoops_template_touch($defaulttpl[0]->getVar('tpl_id'), true);
  368.                         }
  369.                     }
  370.                 }
  371.             } else {
  372.                 $err[] = 'Default template files cannot be deleted.';
  373.             }
  374.         }
  375.         if (count($err) == 0) {
  376.             redirect_header('admin.php?fct=tplsets&op=listtpl&moddir='.$tplfile->getVar('tpl_module').'&tplset='.urlencode($tplfile->getVar('tpl_tplset')), 2, _MD_AM_DBUPDATED);
  377.         }
  378.         xoops_cp_header();
  379.         xoops_error($err);
  380.         echo '<br /><a href="admin.php?fct=tplsets">'._MD_AM_BTOTADMIN.'</a>';
  381.         xoops_cp_footer();
  382.         break;
  383.     case 'delete':
  384.         xoops_cp_header();
  385.         xoops_confirm(array('tplset' => $tplset, 'op' => 'delete_go', 'fct' => 'tplsets'), 'admin.php', _MD_RUSUREDELTH, _YES);
  386.         xoops_cp_footer();
  387.         break;
  388.     case 'delete_go':
  389.         if (!$GLOBALS['xoopsSecurity']->check()) {
  390.             redirect_header('admin.php?fct=tplsets', 1, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  391.         }
  392.         $msgs = array();
  393.         if ($tplset != 'default' && $tplset != $xoopsConfig['template_set']) {
  394.             $tpltpl_handler =& xoops_gethandler('tplfile');
  395.             $templates =& $tpltpl_handler->getObjects(new Criteria('tpl_tplset', $tplset));
  396.             $tcount = count($templates);
  397.             if ($tcount > 0) {
  398.                 $msgs[] = 'Deleting template files...';
  399.                 for ($i = 0; $i < $tcount; $i++) {
  400.                     if (!$tpltpl_handler->delete($templates[$i])) {
  401.                         $msgs[] = '  <span style="color:#ff0000;">ERROR: Could not delete template <b>'.$templates[$i]->getVar('tpl_file').'</b>. ID: <b>'.$templates[$i]->getVar('tpl_id').'</b></span>';
  402.                     } else {
  403.                         $msgs[] = '  Template <b>'.$templates[$i]->getVar('tpl_file').'</b> deleted. ID: <b>'.$templates[$i]->getVar('tpl_id').'</b>';
  404.                     }
  405.                 }
  406.             }
  407. /*
  408.             $image_handler =& xoops_gethandler('imagesetimg');
  409.             $imagefiles =& $image_handler->getObjects(new Criteria('tplset_name', $tplset));
  410.             $icount = count($imagefiles);
  411.             if ($icount > 0) {
  412.                 $msgs[] = 'Deleting image files...';
  413.                 for ($i = 0; $i < $icount; $i++) {
  414.                     if (!$image_handler->delete($imagefiles[$i])) {
  415.                         $msgs[] = '  <span style="color:#ff0000;">ERROR: Could not remove file <b>'.$imagefiles[$i]->getVar('imgsetimg_file').'</b> from the database (ID: <b>'.$imagefiles[$i]->getVar('imgsetimg_id').'</b>)</span>';
  416.                     } else {
  417.                         $msgs[] = '  File <b>'.$imagefiles[$i]->getVar('imgsetimg_file').'</b> deleted from the database (ID: <b>'.$imagefiles[$i]->getVar('imgsetimg_id').'</b>)</span>';
  418.                     }
  419.                 }
  420.             }
  421.             $imageset_handler =& xoops_gethandler('imageset');
  422.             $imagesets =& $imageset_handler->getObjects(new Criteria('tplset_name', $tplset));
  423.             $scount = count($imagesets);
  424.             if ($scount > 0) {
  425.                 $msgs[] = 'Deleting image set data...';
  426.                 for ($i = 0; $i < $scount; $i++) {
  427.                     if (!$imageset_handler->unlinktplset($imagesets[$i]->getVar('imgset_id'), $tplset)) {
  428.                         $msgs[] = '  <span style="color:#ff0000;">ERROR: Could not remove link between <b>'.$imagesets[$i]->getVar('imgset_name').'</b> (ID: <b>'.$imagesets[$i]->getVar('imgset_id').'</b>) and '.$tplset.' from the database.</span>';
  429.                     } else {
  430.                         $msgs[] = '  Link between <b>'.$imagesets[$i]->getVar('imgset_name').'</b> (ID: <b>'.$imagesets[$i]->getVar('imgset_id').'</b>) and <b>'.$tplset.'</b> removed from the database.</span>';
  431.                     }
  432.                     if (!$imageset_handler->delete($imagesets[$i])) {
  433.                         $msgs[] = '  <span style="color:#ff0000;">ERROR: Could not delete image set <b>'.$imagesets[$i]->getVar('imgset_name').'</b> (ID: <b>'.$imagesets[$i]->getVar('imgset_id').'</b>) from the database.</span>';
  434.                     } else {
  435.                         $msgs[] = '  Image set <b>'.$imagesets[$i]->getVar('imgset_name').'</b> (ID: <b>'.$imagesets[$i]->getVar('imgset_id').'</b>) removed from the database.</span>';
  436.                     }
  437.                 }
  438.             }
  439. */
  440.             $tplset_handler =& xoops_gethandler('tplset');
  441.             $tplsets =& $tplset_handler->getObjects(new Criteria('tplset_name', $tplset));
  442.             if (count($tplsets) > 0 && is_object($tplsets[0])) {
  443.                 $msgs[] = 'Deleting template set data...';
  444.                 if (!$tplset_handler->delete($tplsets[0])) {
  445.                     $msgs[] = '  <span style="color:#ff0000;">ERROR: Template set '.$tplset.' could not be deleted.</span>';
  446.                 } else {
  447.                     $msgs[] = '  Template set data removed from the database.';
  448.                 }
  449.             }
  450.         } else {
  451.             $msgs[] = '<span style="color:#ff0000;">ERROR: Default template files cannot be deleted</span>';
  452.         }
  453.         xoops_cp_header();
  454.         foreach ($msgs as $msg) {
  455.             echo '<code>'.$msg.'</code><br />';
  456.         }
  457.         echo '<br /><a href="admin.php?fct=tplsets">'._MD_AM_BTOTADMIN.'</a>';
  458.         xoops_cp_footer();
  459.         break;
  460.     case 'clone':
  461.         include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
  462.         $form = new XoopsThemeForm(_MD_CLONETHEME, 'template_form', 'admin.php', 'post', true);
  463.         $form->addElement(new XoopsFormLabel(_MD_THEMENAME, $tplset));
  464.         $form->addElement(new XoopsFormText(_MD_NEWNAME, 'newtheme', 30, 50), true);
  465.         $form->addElement(new XoopsFormHidden('tplset', $tplset));
  466.         $form->addElement(new XoopsFormHidden('op', 'clone_go'));
  467.         $form->addElement(new XoopsFormHidden('fct', 'tplsets'));
  468.         $form->addElement(new XoopsFormButton('', 'tpl_button', _SUBMIT, 'submit'));
  469.         xoops_cp_header();
  470.         echo '<a href="admin.php?fct=tplsets">'. _MD_TPLMAIN .'</a> <span style="font-weight:bold;">»»</span> '._MD_CLONETHEME.'<br /><br />';
  471.         $form->display();
  472.         xoops_cp_footer();
  473.         break;
  474.     case 'clone_go':
  475.         if (!$GLOBALS['xoopsSecurity']->check()) {
  476.             redirect_header('admin.php?fct=tplsets', 1, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  477.         }
  478.         $msgs = array();
  479.         $tplset = trim($tplset);
  480.         $newtheme = trim($newtheme);
  481.         $tpltpl_handler =& xoops_gethandler('tplfile');
  482.         xoops_cp_header();
  483.         if ($tplset == $newtheme) {
  484.             xoops_error('Template set name must be a different name.');
  485.         } elseif ($tpltpl_handler->getCount(new Criteria('tpl_tplset', $newtheme)) > 0) {
  486.             xoops_error('Template set <b>'.$newtheme.'</b> already exists.');
  487.         } else {
  488.             $tplset_handler =& xoops_gethandler('tplset');
  489.             $tplsetobj =& $tplset_handler->create();
  490.             $tplsetobj->setVar('tplset_name', $newtheme);
  491.             $tplsetobj->setVar('tplset_created', time());
  492.             if (!$tplset_handler->insert($tplsetobj)) {
  493.                 $msgs[] = '<span style="color:#ff0000;">ERROR: Could not create template set <b>'.$newtheme.'</b>.</span><br />';
  494.             } else {
  495.                 $tplsetid = $tplsetobj->getVar('tplset_id');
  496.                 $templates =& $tpltpl_handler->getObjects(new Criteria('tpl_tplset', $tplset), true);
  497.                 $tcount = count($templates);
  498.                 if ($tcount > 0) {
  499.                     $msgs[] = 'Copying template files...';
  500.                     for ($i = 0; $i < $tcount; $i++) {
  501.                         $newtpl =& $templates[$i]->xoopsClone();
  502.                         $newtpl->setVar('tpl_tplset', $newtheme);
  503.                         $newtpl->setVar('tpl_id', 0);
  504.                         $newtpl->setVar('tpl_lastimported', 0);
  505.                         $newtpl->setVar('tpl_lastmodified', time());
  506.                         if (!$tpltpl_handler->insert($newtpl)) {
  507.                             $msgs[] = '  <span style="color:#ff0000;">ERROR: Failed copying template <b>'.$templates[$i]->getVar('tpl_file').'</b>. ID: <b>'.$templates[$i]->getVar('tpl_id').'</b></span>';
  508.                         } else {
  509.                             $msgs[] = '  Template <b>'.$templates[$i]->getVar('tpl_file').'</b> copied. ID: <b>'.$newtpl->getVar('tpl_id').'</b>';
  510.                         }
  511.                         unset($newtpl);
  512.                     }
  513. /*
  514.                     $imageset_handler =& xoops_gethandler('imageset');
  515.                     $orig_imgset =& $imageset_handler->getObjects(new Criteria('tplset_name', $tplset));
  516.                     $msgs[] = 'Copying image files...';
  517.                     $imgsetcount = count($orig_imgset);
  518.                     for ($i = 0; $i < $imgsetcount; $i++) {
  519.                         if ($orig_imgset[$i]->getVar('imgset_refid') == 0) {
  520.                             $new_imgset =& $orig_imgset[$i]->xoopsClone();
  521.                             $new_imgset->setVar('imgset_id', 0);
  522.                             $new_imgset->setVar('imgset_name', $newtheme);
  523.                             if (!$imageset_handler->insert($new_imgset)) {
  524.                                 $msgs[] = '  <span style="color:#ff0000;">ERROR: Failed copying template image set data.</span>';
  525.                             } else {
  526.                                 $new_imgsetid = $new_imgset->getVar('imgset_id');
  527.                                 $msgs[] = '  Template image set data copied. (Name: <b>'.$newtheme.'</b> ID: <b>'.$new_imgsetid.'</b>)</span>';
  528.                                 $image_handler = xoops_gethandler('imagesetimg');
  529.                                 $orig_images =& $image_handler->getByImageset($orig_imgset[$i]->getVar('imgset_id'));
  530.                                 $imgcount = count($orig_images);
  531.                                 for ($j = 0; $j < $imgcount; $j++) {
  532.                                     $new_image =& $orig_images[$j]->xoopsClone();
  533.                                     $new_image->setVar('imgsetimg_id', 0);
  534.                                     $new_image->setVar('imgsetimg_imgset', $new_imgsetid);
  535.                                     if (!$image_handler->insert($new_image)) {
  536.                                         $msgs[] = '  <span style="color:#ff0000;">ERROR: Failed copying data for image file <b>'.$orig_images[$j]->getVar('imgsetimg_file').'</b>.</span>';
  537.                                     } else {
  538.                                         $thisimage = $orig_images[$j]->getVar('imgsetimg_file');
  539.                                         $msgs[] = '  Data for image file <b>'.$thisimage.'</b> copied.</span>';
  540.                                     }
  541.                                 }
  542.                                 if (!$imageset_handler->linktplset($new_imgsetid, $newtheme)) {
  543.                                     $msgs[] = '  <span style="color:#ff0000;">ERROR: Failed creating link between template image set (ID : <b>'.$new_imgsetid.'</b>) and template set <b>'.$newtheme.'</b>.</span>';
  544.                                 } else {
  545.                                     $msgs[] = '  Template image set (ID: <b>'.$new_imgsetid.'</b>) and template set <b>'.$newtheme.'</b> linked.</span>';
  546.                                 }
  547.                             }
  548.                         } else {
  549.                             // module image set, so just create another link to the new template set
  550.                             if (!$imageset_handler->linktplset($orig_imgset[$i]->getVar('imgset_id'), $newtheme)) {
  551.                                 $msgs[] = '  <span style="color:#ff0000;">ERROR: Failed creating link between module image set  <b>'.$orig_imgset[$i]->getVar('imgset_name').'</b> (ID <b>'.$orig_imgset[$i]->getVar('imgset_id').'</b>) and template set <b>'.$newtheme.'</b>.</span>';
  552.                             } else {
  553.                                 $msgs[] = '  Module image set <b>'.$orig_imgset[$i]->getVar('imgset_name').'</b> (ID <b>'.$orig_imgset[$i]->getVar('imgset_id').'</b>) and template set <b>'.$newtheme.'</b> linked.';
  554.                             }
  555.                         }
  556.                     }
  557. */
  558.                     $msgs[] = 'Template set <b>'.htmlspecialchars($newtheme, ENT_QUOTES).'</b> created. (ID: <b>'.$tplsetid.'</b>)<br />';
  559.                 } else {
  560.                     $msgs[] = '<span style="color:#ff0000;">ERROR: Template files for '.$theme.' do not exist</span>';
  561.                 }
  562.             }
  563.         }
  564.         foreach ($msgs as $msg) {
  565.             echo '<code>'.$msg.'</code><br />';
  566.         }
  567.         echo '<br /><a href="admin.php?fct=tplsets">'._MD_AM_BTOTADMIN.'</a>';
  568.         xoops_cp_footer();
  569.         break;
  570. /*
  571.     case 'editimage':
  572.         xoops_cp_header();
  573.         echo '<a href="admin.php?fct=tplsets">'. _MD_TPLMAIN .'</a> <span style="font-weight:bold;">»»</span> '._MD_EDITSKINIMG.' ('.$tplset.')<br /><br />';
  574.         include XOOPS_ROOT_PATH.'/modules/system/admin/tplsets/tplimgform.php';
  575.         xoops_cp_footer();
  576.         break;
  577.     case 'updateimage':
  578.  
  579.         $tplset = trim($tplset);
  580.         $err = array();
  581.         if ($tplset != 'default') {
  582.             include_once XOOPS_ROOT_PATH.'/class/uploader.php';
  583.             $mimetypes = array('image/gif', "image/pjpeg", "image/jpeg", "image/jpeg", "image/jpeg", "image/png", 'image/x-png', "application/x-shockwave-flash", "image/tiff", "image/tiff", 'image/bmp');
  584.             if ($tplset == $xoopsConfig['template_set']) {
  585.                 //directly upload to cache to reduce one step ;-)
  586.                 $uploader = new XoopsMediaUploader(XOOPS_CACHE_PATH, $mimetypes, 500000);
  587.             } else {
  588.                 $uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH, $mimetypes, 500000);
  589.             }
  590.             $image_handler =& xoops_gethandler('imagesetimg');
  591.             foreach ($imgids as $id) {
  592.                 if (isset($imgfiles[$id]) && trim($imgfiles[$id]) != '') {
  593.                     if ($uploader->fetchMedia('imgfiles', $id)) {
  594.                         $image =& $image_handler->get($id);
  595.                         $uploader->setTargetFileName($image->getVar('imgsetimg_file'));
  596.                         if (!$uploader->upload()) {
  597.                             $err[] = $uploader->getErrors();
  598.                         } else {
  599.                             $fp = @fopen($uploader->getSavedDestination(), 'rb');
  600.                             $image->setVar('imgsetimg_body', @fread($fp, filesize($uploader->getSavedDestination())), true);
  601.                             @fclose($fp);
  602.                             if ($tplset != $xoopsConfig['template_set']) {
  603.                                 @unlink($uploader->getSavedDestination());
  604.                             }
  605.                             if (!$image_handler->insert($image)) {
  606.                                 $err[] = 'Could not save '.$image->getVar('imgsetimg_file');
  607.                             }
  608.                         }
  609.                     } else {
  610.                         $err[] = $uploader->getErrors();
  611.                     }
  612.                 } elseif (!empty($imgdelete[$id])) {
  613.                     $image =& $image_handler->get($id);
  614.                     if (!$image_handler->delete($image)) {
  615.                         $err[] = 'Could not remove image file '.$image->getVar('imgsetimg_file');
  616.                     } else {
  617.                         if ($tplset == $xoopsConfig['template_set']) {
  618.                             @unlink(XOOPS_CACHE_PATH.'/'.$image->getVar('imgsetimg_file'));
  619.                         }
  620.                     }
  621.                 }
  622.             }
  623.         } else {
  624.             $err[] = 'Cannot change XOOPS system default theme set images';
  625.         }
  626.         // delete image set if no more images
  627.         $current_imgs =& $image_handler->getByImageset($imgset);
  628.         if (count($current_imgs) == 0) {
  629.             $imageset_handler =& xoops_gethandler('imageset');
  630.             $imgset =& $imageset_handler->get($imgset);
  631.             if (!$imageset_handler->delete($imgset)) {
  632.                 $err[] = 'Could not remove image set '.$imgset->getVar('imgset_name');
  633.             }
  634.         }
  635.         if (count($err) > 0) {
  636.             xoops_cp_header();
  637.             xoops_error($err);
  638.             xoops_cp_footer();
  639.         } else {
  640.             redirect_header('admin.php?fct=tplsets&op=editimage&tplset='.$tplset, 2, _MD_AM_DBUPDATED);
  641.         }
  642.         break;
  643.     case 'addimage':
  644.         $tplset = trim($tplset);
  645.         $err = array();
  646.         if ($tplset != 'default') {
  647.             include_once XOOPS_ROOT_PATH.'/class/uploader.php';
  648.             $mimetypes = array('image/gif', "image/pjpeg", "image/jpeg", "image/jpeg", "image/jpeg", "image/png", 'image/x-png', "application/x-shockwave-flash", "image/tiff", "image/tiff", 'image/bmp');
  649.             if ($tplset == $xoopsConfig['template_set']) {
  650.                 //directly upload to cache to reduce one step ;-)
  651.                 $uploader = new XoopsMediaUploader(XOOPS_CACHE_PATH, $mimetypes, 500000);
  652.             } else {
  653.                 $uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH, $mimetypes, 500000);
  654.             }
  655.             $image_handler =& xoops_gethandler('imagesetimg');
  656.             if ($uploader->fetchMedia('imgfile')) {
  657.  
  658.                 if (!empty($imgset)) {
  659.                     //check if an image with the same name exists
  660.                     if ($image_handler->imageExists($uploader->getMediaName(), $imgset)) {
  661.                         $err[] = 'Image file '.$uploader->getMediaName().' already exists';
  662.                     }
  663.                 }
  664.                 if (empty($err)) {
  665.                     $image =& $image_handler->create();
  666.                     if (!$uploader->upload()) {
  667.                         $err[] = $uploader->getErrors();
  668.                     } else {
  669.                         if (!$fp = @fopen($uploader->getSavedDestination(), 'rb')) {
  670.                             $err[] = 'Could not read '.$uploader->getSavedFileName();
  671.                         } else {
  672.                             $image->setVar('imgsetimg_body', @fread($fp, filesize($uploader->getSavedDestination())), true);
  673.                             @fclose($fp);
  674.                             if ($tplset != $xoopsConfig['template_set']) {
  675.                                 @unlink($uploader->getSavedDestination());
  676.                             }
  677.                             $image->setVar('imgsetimg_file', $uploader->getSavedFileName());
  678.                             if (!empty($imgset)) {
  679.                                 $image->setVar('imgsetimg_imgset', $imgset);
  680.                             } else {
  681.                                 $imageset_handler =& xoops_gethandler('imageset');
  682.                                 $imgset =& $imageset_handler->create();
  683.                                 $imgset->setVar('imgset_name', $tplset);
  684.                                 $imgset->setVar('imgset_refid', 0);
  685.                                 if (!$imageset_handler->insert($imgset)) {
  686.                                     $err[] = 'Could not create image set.';
  687.                                 } else {
  688.                                     $newimgsetid = $imgset->getVar('imgset_id');
  689.                                     $image->setVar('imgsetimg_imgset', $newimgsetid);
  690.                                     if (!$imageset_handler->linktplset($newimgsetid, $tplset)) {
  691.                                         $err[] = 'Failed linking image set to template set '.$tplset;
  692.                                     }
  693.                                 }
  694.                             }
  695.                             if (count($err) == 0) {
  696.                                 if (!$image_handler->insert($image)) {
  697.                                     $err[] = 'Could not save '.$image->getVar('imgsetimg_file');
  698.                                 }
  699.                             }
  700.                         }
  701.                     }
  702.                 }
  703.             } else {
  704.                 $err[] = $uploader->getErrors();
  705.             }
  706.         }
  707.         if (count($err) > 0) {
  708.             xoops_cp_header();
  709.             xoops_error($err);
  710.             xoops_cp_footer();
  711.         } else {
  712.             redirect_header('admin.php?fct=tplsets&op=editimage&tplset='.$tplset, 2, _MD_AM_DBUPDATED);
  713.         }
  714.         break;
  715.     case 'showimage':
  716.         $image_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  717.         if (empty($image_id)) {
  718.             header('Content-type: image/gif');
  719.             readfile(XOOPS_UPLOAD_PATH.'/blank.gif');
  720.             exit();
  721.         }
  722.         $image_handler =& xoops_gethandler('imagesetimg');
  723.         $image =& $image_handler->getObjects(new Criteria('imgsetimg_id', $image_id));
  724.         if (count($image) > 0) {
  725.             $mimetypes = array('gif' => 'image/gif', "jpe"=>"image/jpeg", "jpeg"=>"image/jpeg", "jpg"=>"image/jpeg", "png"=>"image/png", "swf"=>"application/x-shockwave-flash", "tif"=>"image/tiff", "tiff"=>"image/tiff", "bmp" => 'image/bmp');
  726.             $ext = substr(strtolower(strrchr($image[0]->getVar('imgsetimg_file'), '.')), 1);
  727.             if (in_array($ext, array_keys($mimetypes))) {
  728.                 header('Content-type: '.$mimetypes[$ext]);
  729.             }
  730.             header('Cache-control: max-age=31536000');
  731.             header('Expires: '.gmdate("D, d M Y H:i:s",time()+31536000).'GMT');
  732.             header('Content-disposition: filename='.$image[0]->getVar('imgsetimg_file'));
  733.             header('Content-Length: '.strlen($image[0]->getVar('imgsetimg_body')));
  734.             header('Last-Modified: '.gmdate("D, d M Y H:i:s", time()).'GMT');
  735.             echo $image[0]->getVar('imgsetimg_body');
  736.         } else {
  737.             header('Content-type: image/gif');
  738.             readfile(XOOPS_UPLOAD_PATH.'/blank.gif');
  739.         }
  740.         break;
  741. */
  742.     case 'viewdefault':
  743.         $tpltpl_handler =& xoops_gethandler('tplfile');
  744.         $tplfile =& $tpltpl_handler->get($id);
  745.         $default =& $tpltpl_handler->find('default', $tplfile->getVar('tpl_type'), $tplfile->getVar('tpl_refid'), null, $tplfile->getVar('tpl_file'));
  746.         echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
  747.         echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'._LANGCODE.'" lang="'._LANGCODE.'">
  748.         <head>
  749.         <meta http-equiv="content-type" content="text/html; charset='._CHARSET.'" />
  750.         <meta http-equiv="content-language" content="'._LANGCODE.'" />
  751.         <title>'.htmlspecialchars($xoopsConfig['sitename']).' Administration</title>
  752.         <link rel="stylesheet" type="text/css" media="all" href="'.XOOPS_URL.'/xoops.css" />
  753.             <link rel="stylesheet" type="text/css" media="all" href="'.XOOPS_URL.'/modules/system/style.css" />
  754.         </head><body>';
  755.         if (is_object($default[0])) {
  756.             $tpltpl_handler->loadSource($default[0]);
  757.             $last_modified = $default[0]->getVar('tpl_lastmodified');
  758.             $last_imported = $default[0]->getVar('tpl_lastimported');
  759.             if ($default[0]->getVar('tpl_type') == 'block') {
  760.                 $path = XOOPS_ROOT_PATH.'/modules/'.$default[0]->getVar('tpl_module').'/blocks/'.$default[0]->getVar('tpl_file');
  761.             } else {
  762.                 $path = XOOPS_ROOT_PATH.'/modules/'.$default[0]->getVar('tpl_module').'/'.$default[0]->getVar('tpl_file');
  763.             }
  764.             $colorchange = '';
  765.             if (!file_exists($path)) {
  766.                 $filemodified_date = _MD_NOFILE;
  767.                 $lastimported_date = _MD_NOFILE;
  768.             } else {
  769.                 $tpl_modified = filemtime($path);
  770.                 $filemodified_date = formatTimestamp($tpl_modified, 'l');
  771.                 if ($tpl_modified > $last_imported) {
  772.                     $colorchange = ' bgcolor="#ffCC99"';
  773.                 }
  774.                 $lastimported_date = formatTimestamp($last_imported, 'l');
  775.             }
  776.             include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
  777.             $form = new XoopsThemeForm(_MD_VIEWDEFAULT, 'template_form', 'admin.php');
  778.             $form->addElement(new XoopsFormTextArea(_MD_FILEHTML, 'html', $default[0]->getVar('tpl_source'), 25));
  779.             $form->display();
  780.         } else {
  781.             echo 'Selected file does not exist';
  782.         }
  783.         echo '<div style="text-align:center;">[<a href="#" onclick="javascript:window.close();">'._CLOSE.'</a>]</div></body></html>';
  784.         break;
  785.     case 'downloadtpl':
  786.         $tpltpl_handler =& xoops_gethandler('tplfile');
  787.         $tpl =& $tpltpl_handler->get(intval($id), true);
  788.         if (is_object($tpl)) {
  789.             $output = $tpl->getVar('tpl_source');
  790.             strlen($output);
  791.             header('Cache-Control: no-cache, must-revalidate');
  792.             header('Pragma: no-cache');
  793.             header('Content-Type: application/force-download');
  794.             if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  795.                 header('Content-Disposition: filename='.$tpl->getVar('tpl_file'));
  796.             } else {
  797.                 header('Content-Disposition: attachment; filename='.$tpl->getVar('tpl_file'));
  798.             }
  799.             header('Content-length: '.strlen($output));
  800.             echo $output;
  801.         }
  802.         break;
  803.     case 'uploadtpl':
  804.         $tpltpl_handler =& xoops_gethandler('tplfile');
  805.         $id = intval($_GET['id']);
  806.         $tpl =& $tpltpl_handler->get($id);
  807.         xoops_cp_header();
  808.         echo '<a href="admin.php?fct=tplsets">'. _MD_TPLMAIN .'</a> <span style="font-weight:bold;">»»</span> <a href="./admin.php?fct=tplsets&op=listtpl&moddir='.$tpl->getVar('tpl_module').'&tplset='.$tpl->getVar('tpl_tplset').'">'.$tpl->getVar('tpl_tplset').'</a> <span style="font-weight:bold;">»»</span> '._MD_UPLOAD.'<br /><br />';
  809.         if (is_object($tpl)) {
  810.             include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
  811.             $form = new XoopsThemeForm(_MD_UPLOAD, 'tplupload_form', 'admin.php', 'post', true);
  812.             $form->setExtra('enctype="multipart/form-data"');
  813.             $form->addElement(new XoopsFormLabel(_MD_FILENAME, $tpl->getVar('tpl_file').' ('.$tpl->getVar('tpl_tplset').')'));
  814.             $form->addElement(new XoopsFormFile(_MD_CHOOSEFILE.'<br /><span style="color:#ff0000;">'._MD_UPWILLREPLACE.'</span>', 'tpl_upload', 200000), true);
  815.             $form->addElement(new XoopsFormHidden('tpl_id', $id));
  816.             $form->addElement(new XoopsFormHidden('op', 'uploadtpl_go'));
  817.             $form->addElement(new XoopsFormHidden('fct', 'tplsets'));
  818.             $form->addElement(new XoopsFormButton('', 'upload_button', _MD_UPLOAD, 'submit'));
  819.             $form->display();
  820.             xoops_cp_footer();
  821.             exit();
  822.         } else {
  823.             echo 'Selected template does not exist';
  824.         }
  825.         xoops_cp_footer();
  826.         break;
  827.     case 'uploadtpl_go':
  828.         if (!$GLOBALS['xoopsSecurity']->check()) {
  829.             redirect_header('admin.php?fct=tplsets', 1, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  830.         }
  831.         $tpltpl_handler =& xoops_gethandler('tplfile');
  832.         $tpl =& $tpltpl_handler->get($tpl_id);
  833.         if (is_object($tpl)) {
  834.             include_once XOOPS_ROOT_PATH.'/class/uploader.php';
  835.             $uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH, array('text/html', 'application/x-cdf', 'text/plain'), 200000);
  836.             $uploader->setPrefix('tmp');
  837.             if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
  838.                 if (!$uploader->upload()) {
  839.                     $err = $uploader->getErrors();
  840.                 } else {
  841.                     $tpl->setVar('tpl_lastmodified', time());
  842.                     $fp = @fopen($uploader->getSavedDestination(), 'r');
  843.                     $fsource = @fread($fp, filesize($uploader->getSavedDestination()));
  844.                     @fclose($fp);
  845.                     $tpl->setVar('tpl_source', $fsource, true);
  846.                     @unlink($uploader->getSavedDestination());
  847.                     if (!$tpltpl_handler->insert($tpl)) {
  848.                         $err = 'Failed inserting data to database';
  849.                     } else {
  850.                         if ($tpl->getVar('tpl_tplset') == $xoopsConfig['template_set']) {
  851.                             include_once XOOPS_ROOT_PATH.'/class/template.php';
  852.                             xoops_template_touch($tpl_id, true);
  853.                         }
  854.                     }
  855.                 }
  856.             } else {
  857.                 $err = implode('<br />', $uploader->getErrors(false));
  858.             }
  859.             if (isset($err)) {
  860.                 xoops_cp_header(false);
  861.                 xoops_error($err);
  862.                 xoops_cp_footer();
  863.                 exit();
  864.             }
  865.             redirect_header('admin.php?fct=tplsets&op=listtpl&moddir='.$tpl->getVar('tpl_module').'&tplset='.urlencode($tpl->getVar('tpl_tplset')), 2, _MD_AM_DBUPDATED);
  866.         }
  867.         break;
  868.     // upload new file
  869.     case 'uploadtpl2':
  870.         xoops_cp_header();
  871.         $tplset = htmlspecialchars($tplset);
  872.         $moddir = htmlspecialchars($moddir);
  873.         echo '<a href="admin.php?fct=tplsets">'. _MD_TPLMAIN .'</a> <span style="font-weight:bold;">»»</span> <a href="./admin.php?fct=tplsets&op=listtpl&moddir='.$moddir.'&tplset='.$tplset.'">'.$tplset.'</a> <span style="font-weight:bold;">»»</span> '._MD_UPLOAD.'<br /><br />';
  874.         include_once XOOPS_ROOT_PATH.'/class/xoopsformloader.php';
  875.         $form = new XoopsThemeForm(_MD_UPLOAD, 'tplupload_form', 'admin.php', 'post', true);
  876.         $form->setExtra('enctype="multipart/form-data"');
  877.         $form->addElement(new XoopsFormLabel(_MD_FILENAME, $file));
  878.         $form->addElement(new XoopsFormFile(_MD_CHOOSEFILE.'<br /><span style="color:#ff0000;">'._MD_UPWILLREPLACE.'</span>', 'tpl_upload', 200000), true);
  879.         $form->addElement(new XoopsFormHidden('moddir', $moddir));
  880.         $form->addElement(new XoopsFormHidden('tplset', $tplset));
  881.         $form->addElement(new XoopsFormHidden('file', $file));
  882.         $form->addElement(new XoopsFormHidden('type', $type));
  883.         $form->addElement(new XoopsFormHidden('op', 'uploadtpl2_go'));
  884.         $form->addElement(new XoopsFormHidden('fct', 'tplsets'));
  885.         $form->addElement(new XoopsFormButton('', 'ploadtarupload_button', _MD_UPLOAD, 'submit'));
  886.         $form->display();
  887.         xoops_cp_footer();
  888.         break;
  889.     case 'uploadtpl2_go':
  890.         if (!$GLOBALS['xoopsSecurity']->check()) {
  891.             redirect_header('admin.php?fct=tplsets', 1, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  892.         }
  893.         include_once XOOPS_ROOT_PATH.'/class/uploader.php';
  894.         $uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH, array('text/html', 'application/x-cdf', 'text/plain'), 200000);
  895.         $uploader->setPrefix('tmp');
  896.         if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
  897.             if (!$uploader->upload()) {
  898.                 $err = $uploader->getErrors();
  899.             } else {
  900.                 $tpltpl_handler =& xoops_gethandler('tplfile');
  901.                 $tplfile =& $tpltpl_handler->find('default', $type, null, $moddir, $file);
  902.                 if (is_array($tplfile)) {
  903.                     $tpl =& $tplfile[0]->xoopsClone();
  904.                     $tpl->setVar('tpl_id', 0);
  905.                     $tpl->setVar('tpl_tplset', $tplset);
  906.                     $tpl->setVar('tpl_lastmodified', time());
  907.                     $fp = @fopen($uploader->getSavedDestination(), 'r');
  908.                     $fsource = @fread($fp, filesize($uploader->getSavedDestination()));
  909.                     @fclose($fp);
  910.                     $tpl->setVar('tpl_source', $fsource, true);
  911.                     @unlink($uploader->getSavedDestination());
  912.                     if (!$tpltpl_handler->insert($tpl)) {
  913.                         $err = 'Failed inserting data to database';
  914.                     } else {
  915.                         if ($tplset == $xoopsConfig['template_set']) {
  916.                             include_once XOOPS_ROOT_PATH.'/class/template.php';
  917.                             xoops_template_touch($tpl->getVar('tpl_id'), true);
  918.                         }
  919.                     }
  920.                 } else {
  921.                     $err = 'This template file does not need to be installed (PHP files using this template file does not exist)';
  922.                 }
  923.             }
  924.         } else {
  925.             $err = implode('<br />', $uploader->getErrors(false));
  926.         }
  927.         if (isset($err)) {
  928.             xoops_cp_header(false);
  929.             xoops_error($err);
  930.             xoops_cp_footer();
  931.             exit();
  932.         }
  933.         redirect_header('admin.php?fct=tplsets&op=listtpl&moddir='.$moddir.'&tplset='.urlencode($tplset), 2, _MD_AM_DBUPDATED);
  934.         break;
  935.     case 'download':
  936.         if (isset($tplset)) {
  937.             if (false != extension_loaded('zlib')) {
  938.                 if (isset($_GET['method']) && $_GET['method'] == 'tar') {
  939.                     if (@function_exists('gzencode')) {
  940.                         require_once(XOOPS_ROOT_PATH.'/class/tardownloader.php');
  941.                         $downloader = new XoopsTarDownloader();
  942.                     }
  943.                 } else {
  944.                     if (@function_exists('gzcompress')) {
  945.                         require_once(XOOPS_ROOT_PATH.'/class/zipdownloader.php');
  946.                         $downloader = new XoopsZipDownloader();
  947.                     }
  948.                 }
  949.                 $tplset_handler =& xoops_gethandler('tplset');
  950.                 $tplsetobj =& $tplset_handler->getByName($tplset);
  951.                 $xml = "<"."?xml version=\"1.0\"?".">\r\n<tplset>\r\n  <name>".$tplset."</name>\r\n  <dateCreated>".$tplsetobj->getVar('tplset_created')."</dateCreated>\r\n  <credits>\r\n".$tplsetobj->getVar('tplset_credits')."\r\n  </credits>\r\n  <generator>".XOOPS_VERSION."</generator>\r\n  <templates>";
  952.                 $tpltpl_handler =& xoops_gethandler('tplfile');
  953.                 $files =& $tpltpl_handler->getObjects(new Criteria('tpl_tplset', $tplset), true);
  954.                 $fcount = count($files);
  955.                 if ($fcount > 0) {
  956.                     for ($i = 0; $i < $fcount; $i++) {
  957.                         if ($files[$i]->getVar('tpl_type') == 'block') {
  958.                             $path = $tplset.'/templates/'.$files[$i]->getVar('tpl_module').'/blocks/'.$files[$i]->getVar('tpl_file');
  959.                             $xml .= "\r\n    <template name=\"".$files[$i]->getVar('tpl_file')."\">\r\n      <module>".$files[$i]->getVar('tpl_module')."</module>\r\n      <type>block</type>\r\n      <lastModified>".$files[$i]->getVar('tpl_lastmodified')."</lastModified>\r\n    </template>";
  960.                         } elseif ($files[$i]->getVar('tpl_type') == 'module') {
  961.                             $path = $tplset.'/templates/'.$files[$i]->getVar('tpl_module').'/'.$files[$i]->getVar('tpl_file');
  962.                             $xml .= "\r\n    <template name=\"".$files[$i]->getVar('tpl_file')."\">\r\n      <module>".$files[$i]->getVar('tpl_module')."</module>\r\n      <type>module</type>\r\n      <lastModified>".$files[$i]->getVar('tpl_lastmodified')."</lastModified>\r\n    </template>";
  963.                         }
  964.                         $downloader->addFileData($files[$i]->getVar('tpl_source'), $path, $files[$i]->getVar('tpl_lastmodified'));
  965.                     }
  966.  
  967.                     $xml .= "\r\n  </templates>";
  968. /*
  969.                     $xml ." "\r\n  <images>";
  970.                     $image_handler =& xoops_gethandler('imagesetimg');
  971.                     $criteria = new CriteriaCompo(new Criteria('l.tplset_name', $tplset));
  972.                     $criteria->add(new Criteria('s.imgset_refid', 0));
  973.                     $ifiles =& $image_handler->getObjects($criteria);
  974.                     $fcount = count($ifiles);
  975.                     for ($i = 0; $i < $fcount; $i++) {
  976.                         $dummyimage = XOOPS_CACHE_PATH.'/_dummyimage'.$i.time();
  977.                         $fp = @fopen($dummyimage, 'wb');
  978.                         @fwrite($fp, $ifiles[$i]->getVar('imgsetimg_body'));
  979.                         @fclose($fp);
  980.                         $downloader->addBinaryFile($dummyimage, $tplset.'/images/'.$ifiles[$i]->getVar('imgsetimg_file'));
  981.                         @unlink($dummyimage);
  982.                         $xml .= " \r\n   <image name=\"".$ifiles[$i]->getVar('imgsetimg_file')."\"></image>";
  983.                     }
  984. */
  985.                 }
  986.                 //$xml .= "\r\n  </images>
  987.                 $xml .= "\r\n</tplset>";
  988.                 $downloader->addFileData($xml, $tplset.'/tplset.xml', time());
  989.                 echo $downloader->download($tplset, true);
  990.             } else {
  991.                 xoops_cp_header();
  992.                 xoops_error(_MD_NOZLIB);
  993.                 xoops_cp_footer();
  994.             }
  995.         }
  996.         break;
  997.     case 'generatetpl':
  998.         xoops_cp_header();
  999.         xoops_confirm(array('tplset' => $tplset, 'moddir' => $moddir, 'file' => $file, 'type' => $type, 'op' => 'generatetpl_go', 'fct' => 'tplsets'), 'admin.php', _MD_PLZGENERATE, _MD_GENERATE);
  1000.         xoops_cp_footer();
  1001.         break;
  1002.     case 'generatetpl_go':
  1003.         if (!$GLOBALS['xoopsSecurity']->check()) {
  1004.             redirect_header('admin.php?fct=tplsets', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  1005.         }
  1006.         $tpltpl_handler =& xoops_gethandler('tplfile');
  1007.         $tplfile =& $tpltpl_handler->find('default', $type, null, $moddir, $file, true);
  1008.         if (count($tplfile) > 0) {
  1009.             $newtpl =& $tplfile[0]->xoopsClone();
  1010.             $newtpl->setVar('tpl_id', 0);
  1011.             $newtpl->setVar('tpl_tplset', $tplset);
  1012.             $newtpl->setVar('tpl_lastmodified', time());
  1013.             $newtpl->setVar('tpl_lastimported', 0);
  1014.             if (!$tpltpl_handler->insert($newtpl)) {
  1015.                 $err = 'ERROR: Could not insert template <b>'.$tplfile[0]->getVar('tpl_file').'</b> to the database.';
  1016.             } else {
  1017.                 if ($tplset == $xoopsConfig['template_set']) {
  1018.                     include_once XOOPS_ROOT_PATH.'/class/template.php';
  1019.                     xoops_template_touch($newtpl->getVar('tpl_id'));
  1020.                 }
  1021.             }
  1022.         } else {
  1023.             $err = 'Selected file does not exist)';
  1024.         }
  1025.         if (!isset($err)) {
  1026.             redirect_header('admin.php?fct=tplsets&op=listtpl&moddir='.$newtpl->getVar('tpl_module').'&tplset='.urlencode($newtpl->getVar('tpl_tplset')), 2, _MD_AM_DBUPDATED);
  1027.         }
  1028.         xoops_cp_header();
  1029.         xoops_error($err);
  1030.         echo '<br /><a href="admin.php?fct=tplsets">'._MD_AM_BTOTADMIN.'</a>';
  1031.         xoops_cp_footer();
  1032.         break;
  1033.     case 'generatemod':
  1034.         xoops_cp_header();
  1035.         xoops_confirm(array('tplset' => $tplset, 'op' => 'generatemod_go', 'fct' => 'tplsets', 'moddir' => $moddir), 'admin.php', _MD_PLZGENERATE, _MD_GENERATE);
  1036.         xoops_cp_footer();
  1037.         break;
  1038.     case 'generatemod_go':
  1039.         if (!$GLOBALS['xoopsSecurity']->check()) {
  1040.             redirect_header('admin.php?fct=tplsets', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  1041.         }
  1042.         $tpltpl_handler =& xoops_gethandler('tplfile');
  1043.         xoops_cp_header();
  1044.         echo '<code>';
  1045.         $tplfiles =& $tpltpl_handler->find('default', 'module', null, $moddir, null, true);
  1046.         $fcount = count($tplfiles);
  1047.         if ($fcount > 0) {
  1048.             echo 'Installing module template files for template set '.$tplset.'...<br />';
  1049.             for ($i = 0; $i < $fcount; $i++) {
  1050.                 $newtpl =& $tplfiles[$i]->xoopsClone();
  1051.                 $newtpl->setVar('tpl_id', 0);
  1052.                 $newtpl->setVar('tpl_tplset', $tplset);
  1053.                 $newtpl->setVar('tpl_lastmodified', time());
  1054.                 $newtpl->setVar('tpl_lastimported', 0);
  1055.                 if (!$tpltpl_handler->insert($newtpl)) {
  1056.                     echo '  <span style="color:#ff0000;">ERROR: Could not insert template <b>'.$file.'</b> to the database.</span><br />';
  1057.                 } else {
  1058.                     if ($tplset == $xoopsConfig['template_set']) {
  1059.                         include_once XOOPS_ROOT_PATH.'/class/template.php';
  1060.                         xoops_template_touch($newtpl->getVar('tpl_id'));
  1061.                     }
  1062.                     echo '  Template <b>'.$tplfiles[$i]->getVar('tpl_file').'</b> added to the database.<br />';
  1063.                 }
  1064.             }
  1065.             flush();
  1066.             unset($newtpl);
  1067.         }
  1068.         unset($files);
  1069.         $tplfiles =& $tpltpl_handler->find('default', 'block', null, $moddir, null, true);
  1070.         $fcount = count($tplfiles);
  1071.         if ($fcount > 0) {
  1072.             echo '  Installing block template files...<br />';
  1073.             for ($i = 0; $i < $fcount; $i++) {
  1074.                 $newtpl =& $tplfiles[$i]->xoopsClone();
  1075.                 $newtpl->setVar('tpl_id', 0);
  1076.                 $newtpl->setVar('tpl_tplset', $tplset);
  1077.                 $newtpl->setVar('tpl_lastmodified', time());
  1078.                 $newtpl->setVar('tpl_lastimported', 0);
  1079.                 if (!$tpltpl_handler->insert($newtpl)) {
  1080.                     echo '    <span style="color:#ff0000;">ERROR: Could not insert block template <b>'.$tplfiles[$i]->getVar('tpl_file').'</b> to the database.</span><br />';echo $newtpl->getHtmlErrors();
  1081.                 } else {
  1082.                     if ($tplset == $xoopsConfig['template_set']) {
  1083.                         include_once XOOPS_ROOT_PATH.'/class/template.php';
  1084.                         xoops_template_touch($newtpl->getVar('tpl_id'));
  1085.                     }
  1086.                     echo '    Block template <b>'.$tplfiles[$i]->getVar('tpl_file').'</b> added to the database.<br />';
  1087.                 }
  1088.             }
  1089.             flush();
  1090.             unset($newtpl);
  1091.         }
  1092.         echo '<br />Module template files for template set <b>'.$tplset.'</b> generated and installed.<br /></code><br /><a href="admin.php?fct=tplsets">'._MD_AM_BTOTADMIN.'</a>';
  1093.         xoops_cp_footer();
  1094.         break;
  1095.     case 'uploadtar_go':
  1096.         if (!$GLOBALS['xoopsSecurity']->check()) {
  1097.             redirect_header('admin.php?fct=tplsets', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  1098.         }
  1099.         include_once XOOPS_ROOT_PATH.'/class/uploader.php';
  1100.         $uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH, array('application/x-gzip', 'application/gzip', 'application/gzip-compressed', 'application/x-gzip-compressed', 'application/x-tar', 'application/x-tar-compressed', 'application/octet-stream'), 1000000);
  1101.         $uploader->setPrefix('tmp');
  1102.         xoops_cp_header();
  1103.         echo '<code>';
  1104.         if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
  1105.             if (!$uploader->upload()) {
  1106.                 xoops_error($uploader->getErrors());
  1107.             } else {
  1108.                 include_once XOOPS_ROOT_PATH.'/class/class.tar.php';
  1109.                 $tar = new tar();
  1110.                 $tar->openTar($uploader->getSavedDestination());
  1111.                 @unlink($uploader->getSavedDestination());
  1112.                 $themefound = false;
  1113.                 foreach ($tar->files as $id => $info) {
  1114.                     $infoarr = explode('/', str_replace("\\", '/', $info['name']));
  1115.                     if (!isset($tplset_name)) {
  1116.                         $tplset_name = trim($infoarr[0]);
  1117.                     } else {
  1118.                         $tplset_name = trim($tplset_name);
  1119.                         if ($tplset_name == '') {
  1120.                             $tplset_name = trim($infoarr[0]);
  1121.                         }
  1122.                     }
  1123.                     if ($tplset_name != '') {
  1124.                         break;
  1125.                     }
  1126.                 }
  1127.                 if ($tplset_name == '') {
  1128.                     echo '<span style="color:#ff0000;">ERROR: Template file not found</span><br />';
  1129.                 } elseif  (preg_match('/['.preg_quote('\/:*?"<>|','/').']/', $tplset_name)) {
  1130.                     echo '<span style="color:#ff0000;">ERROR: Invalid Template Set Name</span><br />';
  1131.                 } else {
  1132.                     $tplset_handler =& xoops_gethandler('tplset');
  1133.                     if ($tplset_handler->getCount(new Criteria('tplset_name', $tplset_name)) > 0) {
  1134.                         echo '<span style="color:#ff0000;">ERROR: Template set <b>'.htmlspecialchars($tplset_name, ENT_QUOTES).'</b> already exists.</span><br />';
  1135.                     } else {
  1136.                         $tplset =& $tplset_handler->create();
  1137.                         $tplset->setVar('tplset_name', $tplset_name);
  1138.                         $tplset->setVar('tplset_created', time());
  1139.                         if (!$tplset_handler->insert($tplset)) {
  1140.                             echo '<span style="color:#ff0000;">ERROR: Could not create template set <b>'.htmlspecialchars($tplset_name, ENT_QUOTES).'</b>.</span><br />';
  1141.                         } else {
  1142.                             $tplsetid = $tplset->getVar('tplset_id');
  1143.                             echo 'Template set <b>'.htmlspecialchars($tplset_name, ENT_QUOTES).'</b> created. (ID: <b>'.$tplsetid.'</b>)</span><br />';
  1144.                             $tpltpl_handler = xoops_gethandler('tplfile');
  1145.                             $themeimages = array();
  1146.                             foreach ($tar->files as $id => $info) {
  1147.                                 $infoarr = explode('/', str_replace("\\", '/', $info['name']));
  1148.                                 if (isset($infoarr[3]) && trim($infoarr[3]) == 'blocks') {
  1149.                                     $default =& $tpltpl_handler->find('default', 'block', null, trim($infoarr[2]), trim($infoarr[4]));
  1150.                                 } elseif ((!isset($infoarr[4]) || trim($infoarr[4]) == '') && $infoarr[1] == 'templates') {
  1151.                                     $default =& $tpltpl_handler->find('default', 'module', null, trim($infoarr[2]), trim($infoarr[3]));
  1152.                                 } elseif (isset($infoarr[3]) && trim($infoarr[3]) == 'images') {
  1153.                                     $infoarr[2] = trim($infoarr[2]);
  1154.                                     if (preg_match("/(.*)\.(gif|jpg|jpeg|png)$/i", $infoarr[2], $match)) {
  1155.                                         $themeimages[] = array('name' => $infoarr[2], 'content' => $info['file']);
  1156.                                     }
  1157.                                 }
  1158.                                 if (isset($default) && count($default) > 0) {
  1159.                                     $newtpl =& $default[0]->xoopsClone();
  1160.                                     $newtpl->setVar('tpl_id', 0);
  1161.                                     $newtpl->setVar('tpl_tplset', $tplset_name);
  1162.                                     $newtpl->setVar('tpl_source', $info['file'], true);
  1163.                                     $newtpl->setVar('tpl_lastmodified', time());
  1164.                                     if (!$tpltpl_handler->insert($newtpl)) {
  1165.                                         echo '  <span style="color:#ff0000;">ERROR: Could not insert <b>'.$info['name'].'</b> to the database.</span><br />';
  1166.                                     } else {
  1167.                                         echo '  <b>'.$info['name'].'</b> inserted to the database.<br />';
  1168.                                     }
  1169.                                     unset($default);
  1170.                                 }
  1171.                                 unset($info);
  1172.                             }
  1173.                             $icount = count($themeimages);
  1174.                             if ($icount > 0) {
  1175.                                 $imageset_handler =& xoops_gethandler('imageset');
  1176.                                 $imgset =& $imageset_handler->create();
  1177.                                 $imgset->setVar('imgset_name', $tplset_name);
  1178.                                 $imgset->setVar('imgset_refid', 0);
  1179.                                 if (!$imageset_handler->insert($imgset)) {
  1180.                                     echo '  <span style="color:#ff0000;">ERROR: Could not create image set.</span><br />';
  1181.                                 } else {
  1182.                                     $newimgsetid = $imgset->getVar('imgset_id');
  1183.                                     echo '  Image set <b>'.htmlspecialchars($tplset_name, ENT_QUOTES).'</b> created. (ID: <b>'.$newimgsetid.'</b>)<br />';
  1184.                                     if (!$imageset_handler->linktplset($newimgsetid, $tplset_name)) {
  1185.                                         echo '  <span style="color:#ff0000;">ERROR: Failed linking image set to template set <b>'.htmlspecialchars($tplset_name, ENT_QUOTES).'</b></span><br />';
  1186.                                     }
  1187.                                     $image_handler =& xoops_gethandler('imagesetimg');
  1188.                                     for ($i = 0; $i < $icount; $i++) {
  1189.                                         if (isset($themeimages[$i]['name']) && $themeimages[$i]['name'] != '') {
  1190.                                             $image =& $image_handler->create();
  1191.                                             $image->setVar('imgsetimg_file', $themeimages[$i]['name']);
  1192.                                             $image->setVar('imgsetimg_imgset', $newimgsetid);
  1193.                                             $image->setVar('imgsetimg_body', $themeimages[$i]['content'], true);
  1194.                                             if (!$image_handler->insert($image)) {
  1195.                                                 echo '  <span style="color:#ff0000;">ERROR: Failed storing image file data to database.</span><br />';
  1196.                                             } else {
  1197.                                                 echo '  Image file data stored into database. (ID: <b>'.$image->getVar('imgsetimg_id').'</b>)<br />';
  1198.                                             }
  1199.                                         }
  1200.                                     }
  1201.                                 }
  1202.                             }
  1203.                         }
  1204.                     }
  1205.                 }
  1206.             }
  1207.         } else {
  1208.             $err = implode('<br />', $uploader->getErrors(false));
  1209.             echo $err;
  1210.         }
  1211.         echo '</code><br /><a href="admin.php?fct=tplsets">'._MD_AM_BTOTADMIN.'</a>';
  1212.         xoops_cp_footer();
  1213.         break;
  1214.     case 'previewtpl':
  1215.         if (!$GLOBALS['xoopsSecurity']->check()) {
  1216.             redirect_header('admin.php?fct=tplsets', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  1217.         }
  1218.         require_once XOOPS_ROOT_PATH.'/class/template.php';
  1219.         $myts =& MyTextsanitizer::getInstance();
  1220.         $html = $myts->stripSlashesGPC($html);
  1221.         $tpltpl_handler =& xoops_gethandler('tplfile');
  1222.         $tplfile =& $tpltpl_handler->get($id, true);
  1223.         $xoopsTpl = new XoopsTpl();
  1224.  
  1225.         if (is_object($tplfile)) {
  1226.             $dummylayout = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1227.             <html><head><meta http-equiv="content-type" content="text/html; charset='._CHARSET.'" />
  1228.             <meta http-equiv="content-language" content="'._LANGCODE.'" />
  1229.             <title>'.$xoopsConfig['sitename'].'</title>
  1230.             <link rel="stylesheet" type="text/css" media="screen" href="' .XOOPS_URL.'/xoops.css" />
  1231.             <link rel="stylesheet" type="text/css" media="screen" href="' .getcss($xoopsConfig['theme_set']) .'" />';
  1232.  
  1233.             $css =& $tpltpl_handler->find($xoopsConfig['template_set'], 'css', 0, null, null, true);
  1234.             $csscount = count($css);
  1235.             for ($i = 0; $i < $csscount; $i++) {
  1236.                 $dummylayout .= "\n".$css[$i]->getVar('tpl_source');
  1237.             }
  1238.             $dummylayout .= "\n".'</style></head><body><div id="xo-canvas"><{$content}></div></body></html>';
  1239.             if ($tplfile->getVar('tpl_type') == 'block') {
  1240.                 include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php';
  1241.                 $block = new XoopsBlock($tplfile->getVar('tpl_refid'));
  1242.                 $xoopsTpl->assign('block', $block->buildBlock());
  1243.             }
  1244.             $dummytpl = '_dummytpl_'.time().'.html';
  1245.             $fp = fopen(XOOPS_CACHE_PATH.'/'.$dummytpl, 'w');
  1246.             fwrite($fp, $html);
  1247.             fclose($fp);
  1248.             $xoopsTpl->assign('content', $xoopsTpl->fetch('file:'.XOOPS_CACHE_PATH.'/'.$dummytpl));
  1249.             $xoopsTpl->clear_compiled_tpl('file:'.XOOPS_CACHE_PATH.'/'.$dummytpl);
  1250.             unlink(XOOPS_CACHE_PATH.'/'.$dummytpl);
  1251.             $dummyfile = '_dummy_'.time().'.html';
  1252.             $fp = fopen(XOOPS_CACHE_PATH.'/'.$dummyfile, 'w');
  1253.             fwrite($fp, $dummylayout);
  1254.             fclose($fp);
  1255.             $tplset= $tplfile->getVar('tpl_tplset');
  1256.             $tform = array('tpl_tplset' => $tplset, 'tpl_id' => $id, 'tpl_file' => $tplfile->getVar('tpl_file'), 'tpl_desc' => $tplfile->getVar('tpl_desc'), 'tpl_lastmodified' => $tplfile->getVar('tpl_lastmodified'), 'tpl_source' => htmlspecialchars($html, ENT_QUOTES), 'tpl_module' => $moddir);
  1257.             include_once XOOPS_ROOT_PATH.'/modules/system/admin/tplsets/tplform.php';
  1258.             xoops_cp_header();
  1259.             echo '<a href="admin.php?fct=tplsets">'. _MD_TPLMAIN .'</a> <span style="font-weight:bold;">»»</span> <a href="./admin.php?fct=tplsets&op=listtpl&moddir='.$moddir.'&tplset='.urlencode($tplset).'">'.htmlspecialchars($tplset, ENT_QUOTES).'</a> <span style="font-weight:bold;">»»</span> '._MD_EDITTEMPLATE.'<br /><br />';
  1260.             $form->display();
  1261.             xoops_cp_footer();
  1262.             echo '<script type="text/javascript">
  1263.             <!--//
  1264.             preview_window = openWithSelfMain("", "popup", 680, 450, true);
  1265.             preview_window.document.clear();
  1266.             ';
  1267.             $lines = preg_split("/(\r\n|\r|\n)( *)/", $xoopsTpl->fetch('file:'.XOOPS_CACHE_PATH.'/'.$dummyfile));
  1268.             $xoopsTpl->clear_compiled_tpl('file:'.XOOPS_CACHE_PATH.'/'.$dummyfile);
  1269.             unlink(XOOPS_CACHE_PATH.'/'.$dummyfile);
  1270.             foreach ($lines as $line) {
  1271.                 echo 'preview_window.document.writeln("'.str_replace('"', '\"', $line).'");';
  1272.             }
  1273.             echo '
  1274.             preview_window.focus();
  1275.             preview_window.document.close();
  1276.             //-->
  1277.             </script>';
  1278.  
  1279.         }
  1280.         break;
  1281.     case 'update':
  1282.         if (!$GLOBALS['xoopsSecurity']->check()) {
  1283.             redirect_header('admin.php?fct=tplsets', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  1284.         }
  1285.         include_once XOOPS_ROOT_PATH.'/class/uploader.php';
  1286.         $uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH, array('text/html', 'application/x-cdf'), 200000);
  1287.         $uploader->setPrefix('tmp');
  1288.         $msg = array();
  1289.         foreach ($_POST['xoops_upload_file'] as $upload_file) {
  1290.             // '.' is converted to '_' when upload
  1291.             $upload_file2 = str_replace('.', '_', $upload_file);
  1292.             if ($uploader->fetchMedia($upload_file2)) {
  1293.                 if (!$uploader->upload()) {
  1294.                     $msg[] = $uploader->getErrors();
  1295.                 } else {
  1296.                     $tpltpl_handler =& xoops_gethandler('tplfile');
  1297.                     if (!isset($old_template[$upload_file])) {
  1298.                         $tplfile =& $tpltpl_handler->find('default', null, null, $moddir, $upload_file);
  1299.                         if (count($tplfile) > 0) {
  1300.                             $tpl =& $tplfile[0]->xoopsClone();
  1301.                             $tpl->setVar('tpl_id', 0);
  1302.                             $tpl->setVar('tpl_tplset', $tplset);
  1303.                         } else {
  1304.                             $msg[] = 'Template file <b>'.$upload_file.'</b> does not need to be installed (PHP files using this template file does not exist)';
  1305.                             continue;
  1306.                         }
  1307.                     } else {
  1308.                         $tpl =& $tpltpl_handler->get($old_template[$upload_file]);
  1309.                     }
  1310.                     $tpl->setVar('tpl_lastmodified', time());
  1311.                     $fp = @fopen($uploader->getSavedDestination(), 'r');
  1312.                     $fsource = @fread($fp, filesize($uploader->getSavedDestination()));
  1313.                     @fclose($fp);
  1314.                     $tpl->setVar('tpl_source', $fsource, true);
  1315.                     @unlink($uploader->getSavedDestination());
  1316.                     if (!$tpltpl_handler->insert($tpl)) {
  1317.                         $msg[] = 'Failed inserting data for '.$upload_file.' to database';
  1318.                     } else {
  1319.                         $msg[] = 'Template file <b>'.$upload_file.'</b> updated.';
  1320.                         if ($tplset == $xoopsConfig['template_set']) {
  1321.                             include_once XOOPS_ROOT_PATH.'/class/template.php';
  1322.                             if (xoops_template_touch($tpl->getVar('tpl_id'), true)) {
  1323.                                 $msg[] = 'Template file <b>'.$upload_file.'</b> compiled.';
  1324.                             }
  1325.  
  1326.                         }
  1327.                     }
  1328.                 }
  1329.             } else {
  1330.                 if ($uploader->getMediaName() == '') {
  1331.                     continue;
  1332.                 } else {
  1333.                     $msg[] = $uploader->getErrors();
  1334.                 }
  1335.             }
  1336.         }
  1337.         xoops_cp_header();
  1338.         echo '<code>';
  1339.         foreach ($msg as $m) {
  1340.             echo $m.'<br />';
  1341.         }
  1342.         echo '</code><br /><a href="admin.php?fct=tplsets&op=listtpl&tplset='.urlencode($tplset).'&moddir='.$moddir.'">'._MD_AM_BTOTADMIN.'</a>';
  1343.         xoops_cp_footer();
  1344.         break;
  1345.     case 'importtpl':
  1346.         xoops_cp_header();
  1347.         if (!empty($id)) {
  1348.             xoops_confirm(array('tplset' => $tplset, 'moddir' => $moddir, 'id' => $id, 'op' => 'importtpl_go', 'fct' => 'tplsets'), 'admin.php', _MD_RUSUREIMPT, _MD_IMPORT);
  1349.         } elseif (isset($file)) {
  1350.             xoops_confirm(array('tplset' => $tplset, 'moddir' => $moddir, 'file' => $file, 'op' => 'importtpl_go', 'fct' => 'tplsets'), 'admin.php', _MD_RUSUREIMPT, _MD_IMPORT);
  1351.         }
  1352.         xoops_cp_footer();
  1353.         break;
  1354.     case 'importtpl_go':
  1355.         if (!$GLOBALS['xoopsSecurity']->check()) {
  1356.             redirect_header('admin.php?fct=tplsets', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
  1357.         }
  1358.         $tpltpl_handler =& xoops_gethandler('tplfile');
  1359.         $tplfile = '';
  1360.         if (!empty($id)) {
  1361.             $tplfile =& $tpltpl_handler->get($id, true);
  1362.         } else {
  1363.             $tplfiles =& $tpltpl_handler->find('default', null, null, null, trim($file), true);
  1364.             $tplfile = (count($tplfiles) > 0) ? $tplfiles[0] : '';
  1365.         }
  1366.         $error = true;
  1367.         if (is_object($tplfile)) {
  1368.             switch ($tplfile->getVar('tpl_type')) {
  1369.                 case 'module':
  1370.                     $filepath = XOOPS_THEME_PATH.'/'.$tplset.'/templates/'.$tplfile->getVar('tpl_module').'/'.$tplfile->getVar('tpl_file');
  1371.                     break;
  1372.                 case 'block':
  1373.                     $filepath = XOOPS_THEME_PATH.'/'.$tplset.'/templates/'.$tplfile->getVar('tpl_module').'/blocks/'.$tplfile->getVar('tpl_file');
  1374.                     break;
  1375.                 default:
  1376.                     break;
  1377.             }
  1378.             if (file_exists($filepath)) {
  1379.                 if (false != $fp = fopen($filepath, 'r')) {
  1380.                     $filesource = fread($fp, filesize($filepath));
  1381.                     fclose($fp);
  1382.                     $tplfile->setVar('tpl_source', $filesource, true);
  1383.                     $tplfile->setVar('tpl_tplset', $tplset);
  1384.                     $tplfile->setVar('tpl_lastmodified', time());
  1385.                     $tplfile->setVar('tpl_lastimported', time());
  1386.                     if (!$tpltpl_handler->insert($tplfile)) {
  1387.                     } else {
  1388.                         $error = false;
  1389.                     }
  1390.                 }
  1391.             }
  1392.         }
  1393.         if (false != $error) {
  1394.             xoops_cp_header();
  1395.             xoops_error('Could not import file '.$filepath);
  1396.             echo '<br /><a href="admin.php?fct=tplsets&op=listtpl&tplset='.urlencode($tplset).'&moddir='.$moddir.'">'._MD_AM_BTOTADMIN.'</a>';
  1397.             xoops_cp_footer();
  1398.             exit();
  1399.         }
  1400.         redirect_header('admin.php?fct=tplsets&op=listtpl&moddir='.$tplfile->getVar('tpl_module').'&tplset='.urlencode($tplfile->getVar('tpl_tplset')), 2, _MD_AM_DBUPDATED);
  1401.         break;
  1402.     default:
  1403.         break;
  1404.     }
  1405. }
  1406. ?>
  1407.