[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/administrator/components/com_templates/ -> admin.templates.php (source)

   1  <?php
   2  /**
   3  * @package Mambo
   4  * @subpackage Templates
   5  * @author Mambo Foundation Inc see README.php
   6  * @copyright (C) 2000 - 2009 Mambo Foundation Inc.
   7  * See COPYRIGHT.php for copyright notices and details.
   8  * @license GNU/GPL Version 2, see LICENSE.php
   9  *
  10  * Redistributions of files must retain the above copyright notice.
  11  *
  12  * Mambo is free software; you can redistribute it and/or
  13  * modify it under the terms of the GNU General Public License
  14  * as published by the Free Software Foundation; version 2 of the License.
  15  */
  16  
  17  /** ensure this file is being included by a parent file */
  18  defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  19  
  20  // ensure user has access to this function
  21  if (!$acl->acl_check( 'administration', 'manage', 'users', $GLOBALS['my']->usertype, 'components', 'com_templates' )) {
  22      mosRedirect( 'index2.php', T_('You are not authorized to view this resource.') );
  23  }
  24  
  25  require_once( $mainframe->getPath( 'admin_html' ) );
  26  require_once ( $mosConfig_absolute_path .'/administrator/components/com_templates/admin.templates.class.php' );
  27  
  28  $task = trim( strtolower( mosGetParam( $_REQUEST, "task", "" ) ) );
  29  $cid = mosGetParam( $_REQUEST, "cid", array(0) );
  30  $client = mosGetParam( $_REQUEST, 'client', '' );
  31  
  32  if (!is_array( $cid )) {
  33      $cid = array(0);
  34  }
  35  
  36  switch ($task) {
  37      case 'new':
  38          mosRedirect ( 'index2.php?option=com_installer&element=template&client='. $client );
  39          break;
  40  
  41      case 'edit_source':
  42          editTemplateSource( $cid[0], $option, $client );
  43          break;
  44  
  45      case 'save_source':
  46          saveTemplateSource( $option, $client );
  47          break;
  48  
  49      case 'edit_css':
  50          editTemplateCSS( $cid[0], $option, $client );
  51          break;
  52  
  53      case 'save_css':
  54          saveTemplateCSS( $option, $client );
  55          break;
  56  
  57      case 'remove':
  58          removeTemplate( $cid[0], $option, $client );
  59          break;
  60  
  61      case 'publish':
  62          defaultTemplate( $cid[0], $option, $client );
  63          break;
  64  
  65      case 'default':
  66          defaultTemplate( $cid[0], $option, $client );
  67          break;
  68  
  69      case 'assign':
  70          assignTemplate( $cid[0], $option, $client );
  71          break;
  72  
  73      case 'save_assign':
  74          saveTemplateAssign( $option, $client );
  75          break;
  76  
  77      case 'cancel':
  78          mosRedirect( 'index2.php?option='. $option .'&client='. $client );
  79          break;
  80  
  81      case 'positions':
  82          editPositions( $option );
  83          break;
  84  
  85      case 'save_positions':
  86          savePositions( $option );
  87          break;
  88  
  89      default:
  90          viewTemplates( $option, $client );
  91          break;
  92  }
  93  
  94  
  95  /**
  96  * Compiles a list of installed, version 4.5+ templates
  97  *
  98  * Based on xml files found.  If no xml file found the template
  99  * is ignored
 100  */
 101  function viewTemplates( $option, $client ) {
 102      global $database, $mainframe;
 103      global $mosConfig_absolute_path, $mosConfig_list_limit;
 104  
 105      $limit = $mainframe->getUserStateFromRequest( 'viewlistlimit', 'limit', $mosConfig_list_limit );
 106      $limitstart = $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 );
 107  
 108      if ($client == 'admin') {
 109          $templateBaseDir = mosPathName( $mosConfig_absolute_path . '/administrator/templates' );
 110      } else {
 111          $templateBaseDir = mosPathName( $mosConfig_absolute_path . '/templates' );
 112      }
 113  
 114      $rows = array();
 115      // Read the template dir to find templates
 116      $templateDirs        = mosReadDirectory($templateBaseDir);
 117  
 118      $id = intval( $client == 'admin' );
 119  
 120      if ($client=='admin') {
 121          // @RawSQLUse, trivial_implementation, SELECT
 122          $database->setQuery( "SELECT template FROM #__templates_menu WHERE client_id='1' AND menuid='0'" );
 123      } else {
 124          // @RawSQLUse, trivial_implementation, SELECT
 125          $database->setQuery( "SELECT template FROM #__templates_menu WHERE client_id='0' AND menuid='0'" );
 126      }
 127      $cur_template = $database->loadResult();
 128  
 129      $rowid = 0;
 130      // Check that the directory contains an xml file
 131      foreach($templateDirs as $templateDir) {
 132          $dirName = mosPathName($templateBaseDir . $templateDir);
 133          $xmlFilesInDir = mosReadDirectory($dirName,'.xml$');
 134  
 135          foreach($xmlFilesInDir as $xmlfile) {
 136              // Read the file to see if it's a valid template XML file
 137              $parser =& new mosXMLDescription($dirName.$xmlfile);
 138              if ($parser->getType() != 'template') continue;
 139              $row = new StdClass();
 140              $row->id = $rowid;
 141              $row->directory = $templateDir;
 142  
 143              $row->creationdate = $parser->getCreationDate('template');
 144              $row->name = $parser->getName('template');
 145              $row->author = $parser->getAuthor('template');
 146              $row->copyright = $parser->getCopyright('template');
 147              $row->authorEmail = $parser->getAuthorEmail('template');
 148              $row->authorUrl = $parser->getAuthorUrl('template');
 149              $row->version = $parser->getVersion('template');
 150  /*
 151              $element = &$xmlDoc->getElementsByPath('name', 1 );
 152              $row->name = $element->getText();
 153  
 154              $element = &$xmlDoc->getElementsByPath('creationDate', 1);
 155              $row->creationdate = $element ? $element->getText() : 'Unknown';
 156  
 157              $element = &$xmlDoc->getElementsByPath('author', 1);
 158              $row->author = $element ? $element->getText() : 'Unknown';
 159  
 160              $element = &$xmlDoc->getElementsByPath('copyright', 1);
 161              $row->copyright = $element ? $element->getText() : '';
 162  
 163              $element = &$xmlDoc->getElementsByPath('authorEmail', 1);
 164              $row->authorEmail = $element ? $element->getText() : '';
 165  
 166              $element = &$xmlDoc->getElementsByPath('authorUrl', 1);
 167              $row->authorUrl = $element ? $element->getText() : '';
 168  
 169              $element = &$xmlDoc->getElementsByPath('version', 1);
 170              $row->version = $element ? $element->getText() : '';
 171  */
 172              // Get info from db
 173              if ($cur_template == $templateDir) {
 174                  $row->published    = 1;
 175              } else {
 176                  $row->published = 0;
 177              }
 178  
 179              $row->checked_out = 0;
 180              $row->mosname = strtolower( str_replace( ' ', '_', $row->name ) );
 181  
 182              // check if template is assigned
 183              $database->setQuery( "SELECT count(*) FROM #__templates_menu WHERE client_id='0' AND template='$row->directory' AND menuid<>'0'" );
 184              $row->assigned = $database->loadResult() ? 1 : 0;
 185  
 186              $rows[] = $row;
 187              $rowid++;
 188          }
 189      }
 190  
 191      require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
 192      $pageNav = new mosPageNav( count( $rows ), $limitstart, $limit );
 193  
 194      $rows = array_slice( $rows, $pageNav->limitstart, $pageNav->limit );
 195  
 196      HTML_templates::showTemplates( $rows, $pageNav, $option, $client );
 197  }
 198  
 199  
 200  /**
 201  * Publish, or make current, the selected template
 202  */
 203  function defaultTemplate( $p_tname, $option, $client ) {
 204      global $database;
 205  
 206      if ($client=='admin') {
 207          // @RawSQLUse, trivial_implementation, DELETE
 208          $database->setQuery("DELETE FROM #__templates_menu WHERE client_id='1' AND menuid='0'");
 209          $database->query();
 210  
 211          // @RawSQLUse, trivial_implementation, DELETE
 212          $database->setQuery("INSERT INTO #__templates_menu SET client_id='1', template='$p_tname', menuid='0'");
 213          $database->query();
 214      } else {
 215          // @RawSQLUse, trivial_implementation, DELETE
 216          $database->setQuery("DELETE FROM #__templates_menu WHERE client_id='0' AND menuid='0'");
 217          $database->query();
 218  
 219          // @RawSQLUse, trivial_implementation, DELETE
 220          $database->setQuery("INSERT INTO #__templates_menu SET client_id='0', template='$p_tname', menuid='0'");
 221          $database->query();
 222  
 223          $_SESSION['cur_template'] = $p_tname;
 224      }
 225  
 226      mosRedirect('index2.php?option='. $option .'&client='. $client);
 227  }
 228  
 229  /**
 230  * Remove the selected template
 231  */
 232  function removeTemplate( $cid, $option, $client ) {
 233      global $database;
 234  
 235      $client_id = $client=='admin' ? 1 : 0;
 236  
 237      // @RawSQLUse, trivial_implementation, SELECT
 238      $database->setQuery("SELECT template FROM #__templates_menu WHERE client_id='$client_id' AND menuid='0'");
 239      $cur_template = $database->loadResult();
 240  
 241      if ($cur_template == $cid) {
 242          echo "<script>alert('".T_('You can not delete template in use.')."'); window.history.go(-1); </script>\n";
 243          exit();
 244      }
 245  
 246      // Un-assign
 247  
 248      $database->setQuery( "DELETE FROM #__templates_menu WHERE template='$cid' AND client_id='$client_id' AND menuid<>'0'" );
 249      $database->query();
 250  
 251      mosRedirect( 'index2.php?option=com_installer&element=template&client='. $client .'&task=remove&cid[]='. $cid );
 252  }
 253  
 254  function editTemplateSource( $p_tname, $option, $client ) {
 255      global $mosConfig_absolute_path;
 256  
 257      if ( $client == 'admin' ) {
 258          $file = $mosConfig_absolute_path .'/administrator/templates/'. $p_tname .'/index.php';
 259      } else {
 260          $file = $mosConfig_absolute_path .'/templates/'. $p_tname .'/index.php';
 261      }
 262  
 263      if ( $fp = fopen( $file, 'r' ) ) {
 264          $content = fread( $fp, filesize( $file ) );
 265          $content = htmlspecialchars( $content );
 266  
 267          HTML_templates::editTemplateSource( $p_tname, $content, $option, $client );
 268      } else {
 269          mosRedirect( 'index2.php?option='. $option .'&client='. $client, sprintf(T_('Operation Failed: Could not open %s' ), $file) );
 270      }
 271  }
 272  
 273  
 274  function saveTemplateSource( $option, $client ) {
 275      global $mosConfig_absolute_path;
 276  
 277      $template = mosGetParam( $_POST, 'template', '' );
 278      $filecontent = mosGetParam( $_POST, 'filecontent', '', _MOS_ALLOWHTML );
 279  
 280      if ( !$template ) {
 281          mosRedirect( 'index2.php?option='. $option .'&client='. $client, T_('Operation failed: No template specified.') );
 282      }
 283      if ( !$filecontent ) {
 284          mosRedirect( 'index2.php?option='. $option .'&client='. $client, T_('Operation failed: Content empty.') );
 285      }
 286  
 287      if ( $client == 'admin' ) {
 288          $file = $mosConfig_absolute_path .'/administrator/templates/'. $template .'/index.php';
 289      } else {
 290          $file = $mosConfig_absolute_path .'/templates/'. $template .'/index.php';
 291      }
 292  
 293      $enable_write = mosGetParam($_POST,'enable_write',0);
 294      $oldperms = fileperms($file);
 295      if ($enable_write) @chmod($file, $oldperms | 0222);
 296  
 297      clearstatcache();
 298      if ( is_writable( $file ) == false ) {
 299          mosRedirect( 'index2.php?option='. $option , sprintf(T_('Operation failed: %s is not writable.'), $file) );
 300      }
 301  
 302      if ( $fp = fopen ($file, 'w' ) ) {
 303          fputs( $fp, stripslashes( $filecontent ), strlen( $filecontent ) );
 304          fclose( $fp );
 305          if ($enable_write) {
 306              @chmod($file, $oldperms);
 307          } else {
 308              if (mosGetParam($_POST,'disable_write',0))
 309                  @chmod($file, $oldperms & 0777555);
 310          } // if
 311          mosRedirect( 'index2.php?option='. $option .'&client='. $client );
 312      } else {
 313          if ($enable_write) @chmod($file, $oldperms);
 314          mosRedirect( 'index2.php?option='. $option .'&client='. $client, T_('Operation failed: Failed to open file for writing.') );
 315      }
 316  
 317  }
 318  
 319  function editTemplateCSS( $p_tname, $option, $client ) {
 320      global $mosConfig_absolute_path;
 321  
 322      if ( $client == 'admin' ) {
 323          $file = $mosConfig_absolute_path .'/administrator/templates/'. $p_tname .'/css/template_css.css';
 324      } else {
 325          $file = $mosConfig_absolute_path .'/templates/'. $p_tname .'/css/template_css.css';
 326      }
 327  
 328      if ($fp = fopen( $file, 'r' )) {
 329          $content = fread( $fp, filesize( $file ) );
 330          $content = htmlspecialchars( $content );
 331  
 332          HTML_templates::editCSSSource( $p_tname, $content, $option, $client );
 333      } else {
 334          mosRedirect( 'index2.php?option='. $option .'&client='. $client, sprintf(T_('Operation Failed: Could not open %s' ), $file) );
 335      }
 336  }
 337  
 338  
 339  function saveTemplateCSS( $option, $client ) {
 340      global $mosConfig_absolute_path;
 341      $template = trim( mosGetParam( $_POST, 'template', '' ) );
 342      $filecontent = mosGetParam( $_POST, 'filecontent', '', _MOS_ALLOWHTML );
 343  
 344      if ( !$template ) {
 345          mosRedirect( 'index2.php?option='. $option .'&client='. $client, T_('Operation failed: No template specified.') );
 346      }
 347  
 348      if ( !$filecontent ) {
 349          mosRedirect( 'index2.php?option='. $option .'&client='. $client, T_('Operation failed: Content empty.') );
 350      }
 351  
 352      if ( $client == 'admin' ) {
 353          $file = $mosConfig_absolute_path .'/administrator/templates/'. $template .'/css/template_css.css';
 354      } else {
 355          $file = $mosConfig_absolute_path .'/templates/'. $template .'/css/template_css.css';
 356      }
 357  
 358      $enable_write = mosGetParam($_POST,'enable_write',0);
 359      $oldperms = fileperms($file);
 360      if ($enable_write) @chmod($file, $oldperms | 0222);
 361  
 362      clearstatcache();
 363      if ( is_writable( $file ) == false ) {
 364          mosRedirect( 'index2.php?option='. $option .'&client='. $client, T_('Operation failed: The file is not writable.') );
 365      }
 366  
 367      if ($fp = fopen ($file, 'w')) {
 368          fputs( $fp, stripslashes( $filecontent ) );
 369          fclose( $fp );
 370          if ($enable_write) {
 371              @chmod($file, $oldperms);
 372          } else {
 373              if (mosGetParam($_POST,'disable_write',0))
 374                  @chmod($file, $oldperms & 0777555);
 375          } // if
 376          mosRedirect( 'index2.php?option='. $option );
 377      } else {
 378          if ($enable_write) @chmod($file, $oldperms);
 379          mosRedirect( 'index2.php?option='. $option .'&client='. $client, T_('Operation failed: Failed to open file for writing.') );
 380      }
 381  
 382  }
 383  
 384  
 385  function assignTemplate( $p_tname, $option, $client ) {
 386      global $database;
 387  
 388      // get selected pages for $menulist
 389      if ( $p_tname ) {
 390          // @RawSQLUse, trivial_implementation, SELECT
 391          $database->setQuery( "SELECT menuid AS value FROM #__templates_menu WHERE client_id='0' AND template='$p_tname'" );
 392          $lookup = $database->loadObjectList();
 393      }
 394  
 395      // build the html select list
 396      $menulist = mosAdminMenus::MenuLinks( $lookup, 0, 1 );
 397  
 398      HTML_templates::assignTemplate( $p_tname, $menulist, $option, $client );
 399  }
 400  
 401  
 402  function saveTemplateAssign( $option, $client ) {
 403      global $database;
 404  
 405      $menus = mosGetParam( $_POST, 'selections', array() );
 406      $template = mosGetParam( $_POST, 'template', '' );
 407  
 408      $database->setQuery( "DELETE FROM #__templates_menu WHERE client_id='0' AND template='$template' AND menuid<>'0'" );
 409      $database->query();
 410  
 411      if ( !in_array( '', $menus ) ) {
 412          foreach ( $menus as $menuid ){
 413              // If 'None' is not in array
 414              if ( $menuid <> -999 ) {
 415                  // check if there is already a template assigned to this menu item
 416                  // @RawSQLUse, trivial_implementation, DELETE
 417                  $database->setQuery( "DELETE FROM #__templates_menu WHERE client_id='0' AND menuid='$menuid'" );
 418                  $database->query();
 419                  // @RawSQLUse, trivial_implementation, INSERT
 420                  $database->setQuery( "INSERT INTO #__templates_menu SET client_id='0', template='$template', menuid='$menuid'" );
 421                  $database->query();
 422              }
 423          }
 424      }
 425  
 426      mosRedirect( 'index2.php?option='. $option .'&client='. $client );
 427  }
 428  
 429  
 430  /**
 431  */
 432  function editPositions( $option ) {
 433      global $database;
 434  
 435      // @RawSQLUse, trivial_implementation, SELECT
 436      $database->setQuery( "SELECT * FROM #__template_positions" );
 437      $positions = $database->loadObjectList();
 438  
 439      HTML_templates::editPositions( $positions, $option );
 440  }
 441  
 442  /**
 443  */
 444  function savePositions( $option ) {
 445      global $database;
 446  
 447      $positions = mosGetParam( $_POST, 'position', array() );
 448      $descriptions = mosGetParam( $_POST, 'description', array() );
 449  
 450      // @RawSQLUse, trivial_implementation, DELETE
 451      $query = 'DELETE FROM #__template_positions';
 452      $database->setQuery( $query );
 453      $database->query();
 454  
 455      foreach ($positions as $id=>$position) {
 456          $position = trim( $database->getEscaped( $position ) );
 457          $description = mosGetParam( $descriptions, $id, '' );
 458          if ($position != '') {
 459              $id = intval( $id );
 460              // @RawSQLUse, trivial_implementation, INSERT
 461              $query = "INSERT INTO #__template_positions"
 462                  . "\nVALUES ($id,'$position','$description')";
 463              $database->setQuery( $query );
 464              $database->query();
 465          }
 466      }
 467      mosRedirect( 'index2.php?option='. $option .'&task=positions', T_('Positions saved') );
 468  }
 469  ?>