[ Index ]
 

Code source de CMS made simple 1.0.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/ThemeManager/ -> action.importtheme.php (source)

   1  <?php
   2  #-------------------------------------------------------------------------
   3  # Module: ThemeManager - a module for importing and exporting template
   4  #   and stylesheet packages.
   5  # Version: 1.0.6, Robert Campbell <rob@techcom.dyndns.org>
   6  #
   7  #-------------------------------------------------------------------------
   8  # CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
   9  # This project's homepage is: http://www.cmsmadesimple.org
  10  #
  11  #-------------------------------------------------------------------------
  12  #
  13  # This program is free software; you can redistribute it and/or modify
  14  # it under the terms of the GNU General Public License as published by
  15  # the Free Software Foundation; either version 2 of the License, or
  16  # (at your option) any later version.
  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  # You should have received a copy of the GNU General Public License
  23  # along with this program; if not, write to the Free Software
  24  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25  # Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
  26  #
  27  #-------------------------------------------------------------------------
  28  
  29  if (!($this->CheckPermission('Add Stylesheets') &&
  30        $this->CheckPermission('Add Templates') &&
  31        $this->CheckPermission('Add Stylesheet Assoc')))
  32    {
  33      $this->DisplayErrorPage($id, $params, $return_id,
  34                  $this->Lang('accessdenied'));
  35      return;
  36    }
  37  
  38  
  39  /*---------------------------------------------------------
  40   _cleanupReferences
  41   ---------------------------------------------------------*/
  42  function _cleanupReferences( $prefix, $references, &$input )
  43  {
  44    $data = $input;
  45    foreach( $references as $ref )
  46      {
  47        $olddata = $data;
  48        $data = preg_replace( '|'.$ref['location'].'|', $prefix."/".$ref['name'],
  49                  $data );
  50        if( $data == '' )
  51      {
  52        return false;
  53      }
  54      }
  55    $input = $data;
  56    return true;
  57  }
  58  
  59  
  60  //
  61  // Begin
  62  //
  63  global $gCms;
  64  $fieldName=$id.'input_browse';
  65  if (!isset ($_FILES[$fieldName]) || !isset ($_FILES)
  66      || !is_array ($_FILES[$fieldName]) || !$_FILES[$fieldName]['name'])
  67    {
  68      $this->DisplayErrorPage( $id, $params, $returnid, 
  69                   $this->Lang('error_nofilesuploaded'));
  70      return;
  71    }
  72  
  73  $mmmodule = $this->GetModuleInstance('MenuManager');
  74  if( !$mmmodule )
  75    {
  76      // nothing to output
  77      $this->DisplayErrorPage( $id, $params, $returnid, 
  78                   $this->Lang('error_nomenumanager'));
  79      return;
  80    }
  81  
  82  // make sure we have the dtdversion
  83  $havedtdversion = false;
  84  
  85  // normalize the file variable
  86  $file = $_FILES[$fieldName];
  87      
  88  // $file['tmp_name'] is the file we have to parse
  89  $xml = file_get_contents( $file['tmp_name'] );
  90  
  91  // load all the stylesheets
  92  global $gCms;
  93  $styleops =& $gCms->GetStylesheetOperations();
  94  $allstylesheets = $styleops->LoadStylesheets();
  95  
  96  // and the templates
  97  global $gCms;
  98  $templateops =& $gCms->GetTemplateOperations();
  99  $alltemplates = $templateops->LoadTemplates();
 100  
 101  // parse the xml stuff
 102  $parser = xml_parser_create();
 103  xml_parse_into_struct( $parser, $xml, $val, $idx );
 104  xml_parser_free( $parser );
 105  
 106  $theme = '';
 107  $curelement = array();
 108  $templates = array();
 109  $associations = array();
 110  $allreferences = array();
 111  $stylesheets = array();
 112  $mmtemplates = array();
 113  
 114  $onemmtemplate = array();
 115  $onetemplate = new Template();
 116  $onesheet = '';
 117  $onereference = array();
 118  $oneassoc = array();
 119  foreach( $val as $elem )
 120  {
 121    $value = '';
 122    if( isset( $elem['value'] ) ) $value = $elem['value'];
 123    $type = $elem['type'];
 124    switch( $elem['tag'] )
 125      {
 126      case 'NAME':
 127        {
 128      if( $type != 'complete' && $type != 'close' )
 129        {
 130          continue;
 131        }
 132      $theme = $value;
 133      break;
 134        }
 135  
 136      case 'DTDVERSION':
 137        {
 138      if( $type != 'complete' && $type != 'close' )
 139        {
 140          continue;
 141        }
 142      if( $value != DTD_VERSION )
 143        {
 144          $this->DisplayErrorPage( $id, $params, $returnid, 
 145                       $this->Lang('error_dtdmismatch'));
 146          return;
 147        }
 148      $havedtdversion = true;
 149      break;
 150        }
 151      case 'TNAME':
 152        if( $type != 'complete' && $type != 'close' )
 153      {
 154        continue;
 155      }
 156        $onetemplate->name = $theme." : ".$value;
 157        foreach( $alltemplates as $tmpl )
 158      {
 159        if( $tmpl->name == $onetemplate->name )
 160          {
 161            // template name exists
 162            $this->DisplayErrorPage( $id, $params, $returnid, 
 163                         $this->Lang('error_templateexists',$onetemplate->name));
 164            return;
 165          }
 166      }
 167        break;
 168  
 169      case 'TENCODING':
 170        if( $type != 'complete' && $type != 'close' )
 171      {
 172        continue;
 173      }
 174        $onetemplate->encoding = $value;
 175        break;
 176          
 177      case 'TDATA':
 178        if( $type != 'complete' && $type != 'close' )
 179      {
 180        continue;
 181      }
 182        $onetemplate->content = base64_decode($value);
 183        break;
 184          
 185      case 'TEMPLATE':
 186        if( $type != 'close' )
 187      {
 188        continue;
 189      }
 190        $templates[] = $onetemplate;
 191        $onetemplate = new Template();
 192        break;
 193          
 194      case 'CSSNAME':
 195        $onesheet->name = $theme." : ".$value;
 196        foreach( $allstylesheets as $cssobj )
 197      {
 198        if( $cssobj->name == $onesheet->name )
 199          {
 200            // stylesheet name exists
 201            $this->DisplayErrorPage( $id, $params, $returnid, 
 202                         $this->Lang('error_stylesheetexists',$onesheet->name));
 203            return;
 204          }
 205      }
 206        break;
 207  
 208      case 'CSSMEDIATYPE':
 209        $onesheet->media_type = $value;
 210        break;
 211  
 212      case 'CSSDATA':
 213        $onesheet->value = base64_decode($value);
 214        break;
 215  
 216      case 'STYLESHEET':
 217        if( $type == 'open' )
 218      {
 219        $onesheet = new Stylesheet();
 220      }
 221        else if( $type == 'close' )
 222      {
 223        $stylesheets[] = $onesheet;
 224      }
 225  
 226      case 'ASSOC_TNAME':
 227        $oneassoc['tname'] = $value;
 228        break;
 229  
 230      case 'ASSOC_CSSNAME':
 231        $oneassoc['cssname'] = $value;
 232        break;
 233  
 234      case 'ASSOC':
 235        if( $type != 'close' )
 236      {
 237        continue;
 238      }
 239        if( count( $oneassoc ) != 2 )
 240      {
 241        $this->DisplayErrorPage( $id, $params, $returnid, 
 242                     $this->Lang('error_badassoc',$oneassoc['tname']));
 243      }
 244        $associations[] = $oneassoc;
 245        $oneassoc = array();
 246        break;
 247  
 248      case 'REFNAME':
 249        $onereference['name'] = $value;
 250        break;
 251  
 252      case 'REFENCODED':
 253        $onereference['encoded'] = $value;
 254        break;
 255  
 256      case 'REFLOCATION':
 257        $onereference['location'] = $value;
 258        break;
 259  
 260      case 'REFDATA':
 261        $onereference['data'] = $value;
 262        break;
 263  
 264      case 'REFERENCE':
 265        if( $type != 'close' )
 266      {
 267        continue;
 268      }
 269        if( count( $onereference ) != 4 )
 270      {
 271        // stylesheet name exists
 272        $this->DisplayErrorPage( $id, $params, $returnid, 
 273                     $this->Lang('error_badembed',$onesheet->name));
 274      }
 275        $allreferences[] = $onereference;
 276        $onereference = array();
 277        break;
 278  
 279      case 'MMTEMPLATE_NAME':
 280        $onemmtemplate['origname'] = $value;
 281        $t_arr = explode('.tpl',$value);
 282        if( is_array( $t_arr ) )
 283      {
 284        $value = $t_arr[0];
 285      }
 286        $onemmtemplate['name'] = $theme." : ".$value;
 287        break;
 288  
 289      case 'MMTEMPLATE_DATA':
 290        $onemmtemplate['data'] = base64_decode($value);
 291        break;
 292          
 293      case 'MMTEMPLATE':
 294        if( $type != 'close' )
 295      {
 296        continue;
 297      }
 298        if( count( $onemmtemplate ) != 3 )
 299      {
 300        // stylesheet name exists
 301        $this->DisplayErrorPage( $id, $params, $returnid, 
 302                     $this->Lang('error_badxml'));
 303      }
 304        $mmtemplates[] = $onemmtemplate;
 305      }
 306  }
 307  
 308  // check the dtd version one last time.
 309  if( $havedtdversion == false )
 310    {
 311      $this->DisplayErrorPage( $id, $params, $returnid, 
 312                   $this->Lang('error_dtdmismatch'));
 313      return;
 314    }
 315  
 316  
 317  /*---------------------------------------------------------
 318   _saveEncodedFile
 319   ---------------------------------------------------------*/
 320  function _saveEncodedFile( $obj, $prefix, $name, &$location, $encoded, $data )
 321  {
 322    // clean up the location
 323    if( substr($location,0,1) == '/' )
 324      {
 325        $location = substr($location,1);
 326      }
 327      
 328    if( $encoded )
 329      {
 330        $data = base64_decode( $data );
 331      }
 332  
 333    // translate slashes if we have to
 334    $newloc = preg_replace( '|\/|', DIRECTORY_SEPARATOR, $location );
 335  
 336    $dir = $prefix.DIRECTORY_SEPARATOR.dirname( $location );
 337    if( !file_exists( $dir ) )
 338      {
 339        $obj->_mkdirr( $dir );
 340        if( !file_exists( $dir ) )
 341      {
 342        return 0;
 343      }
 344      }
 345  
 346    // and put it out there
 347    global $gCms;
 348    $fn = $prefix.DIRECTORY_SEPARATOR.$newloc;
 349    $fp = fopen( $fn, "w" );
 350    if( !$fp )
 351      {
 352        return 0;
 353      }
 354    fwrite( $fp, $data );
 355    fclose( $fp );
 356    return 1;
 357  }
 358  
 359  
 360  // make sure we have at least one stylesheet
 361  if( count( $stylesheets ) == 0 )
 362    {
 363      $this->DisplayErrorPage( $id, $params, $returnid, 
 364                   $this->Lang('error_nostylesheets'));
 365      return;
 366    }
 367  
 368  // clean up the template and stylesheets and adjust everything to their new locations
 369  $prefix = basename($gCms->config['uploads_url'])."/".$theme;
 370  for( $i = 0; $i < count($stylesheets); $i++ )
 371    {
 372      $result = _cleanupReferences( $prefix, $allreferences, $stylesheets[$i]->value );
 373      if( !$result )
 374        {
 375      $this->DisplayErrorPage( $id, $params, $returnid, 
 376                   $this->Lang('error_editingproblem'));
 377      return;
 378        }
 379    }
 380  
 381  $newtemplates = array();
 382  foreach( $templates as $onetemplate )
 383  {
 384    $result = _cleanupReferences( $prefix, $allreferences, $onetemplate->content );
 385    if( !$result )
 386      {
 387        $this->DisplayErrorPage( $id, $params, $returnid, 
 388                     $this->Lang('error_editingproblem'));
 389        return;
 390      }
 391    $newtemplates[] = $onetemplate;
 392  }
 393  $templates = $newtemplates;
 394      
 395  
 396  // save our references as files
 397  // first we create the destination directory if it doesn't already exist
 398  $dest = $gCms->config['uploads_path'].DIRECTORY_SEPARATOR.$theme;
 399  $this->_mkdirr( $dest );
 400  
 401  foreach( $allreferences as $ref )
 402  {
 403    //$res = _saveEncodedFile( $dest, $ref['name'], $ref['location'],
 404    $res = _saveEncodedFile( $this, $dest, $ref['name'], $ref['name'],
 405                 $ref['encoded'], $ref['data'] );
 406    if( $res != 1 ) 
 407      {
 408        // this could be a problem we could have a bunch o files lying around
 409        $this->DisplayErrorPage( $id, $params, $returnid, 
 410                     $this->Lang('error_problemsavingincludes'));
 411        return;
 412      }
 413  }
 414      
 415      
 416  //
 417  // now we have some nice, clean templates and stylesheets, do our magic
 418  //
 419  
 420  // stylesheets first
 421  $cssids = array();
 422  global $gCms;
 423  $styleops =& $gCms->GetStylesheetOperations();
 424  foreach( $stylesheets as $css )
 425  {
 426    $newid = $styleops->InsertStylesheet( $css );
 427    if( $newid == -1 )
 428      {
 429        // couldn't insert the stylesheet
 430        continue;
 431      }
 432    $cssids[] = array( $css->name, $newid );
 433  }
 434  
 435  // then the menu manager templates
 436  foreach( $mmtemplates as $mmtempl ) 
 437  {
 438    $mmmodule->SetMenuTemplate( $mmtempl['name'], $mmtempl['data'] );
 439  }
 440  
 441  // then templates
 442  foreach( $templates as $onetemplate )
 443  {
 444    // have to clean up the template, and give it the 
 445    // new mmtemplate name if it wants it
 446    // replace {cms_module module=menumanager template=something} 
 447    // with {cms_module module=menumanager template="theme : something"}
 448    //$pattern = "/\{cms_module\s+module\s*=\s*[\\\"']{0,1}menumanager[\\\"']{0,1}\s+template\s*=\s*[\\\"']{0,1}(.*?)[\\\"']{0,1}\}/i";
 449    //$replacement = "{cms_module module='menumanager' template='theme : $1'}";
 450    $newcontent = $onetemplate->content;
 451    foreach( $mmtemplates as $mmtempl )
 452      {
 453        $pattern = "/\{cms_module\s+module\s*=\s*[\\\"']{0,1}menumanager[\\\"']{0,1}\s+template\s*=\s*[\\\"']{0,1}(".$mmtempl['origname'].")[\\\"']{0,1}/i";
 454        $replacement = "{cms_module module='menumanager' template='".$mmtempl['name']."' ";
 455        $newcontent = preg_replace($pattern, $replacement, $newcontent);    
 456      }
 457  
 458    $onetemplate->content = $newcontent;
 459  
 460    // insert the template
 461    global $gCms;
 462    $templateops =& $gCms->GetTemplateOperations();
 463    $styleops =& $gCms->GetStylesheetOperations();
 464    $newtmplid = $templateops->InsertTemplate( $onetemplate );
 465    if( $newtmplid == -1 )
 466      {
 467        $db = $this->GetDb();
 468        echo "DEBUG: ".$db->ErrorMsg()."<br/>";
 469        $this->DisplayErrorPage( $id, $params, $returnid, 
 470                     $this->Lang('error_couldnotcreatetemplate'));
 471        return;
 472      }
 473    else
 474      {
 475        // now associate the stylesheets with this template
 476          
 477        // only add the associations for this template.
 478        foreach( $associations as $assoc )
 479      {
 480        $tmp_name = $theme." : ".$assoc['tname'];
 481        if( $tmp_name != $onetemplate->name )
 482          {
 483            continue;
 484          }
 485          
 486        foreach( $cssids as $rec )
 487          {
 488            $tmp_name2 = $theme." : ".$assoc['cssname'];
 489            if( $tmp_name2 == $rec[0] )
 490          {
 491            // have a templateid and a cssid, so go for it.
 492            if( !$styleops->AssociateStylesheetToTemplate( $rec[1], $newtmplid ) )
 493              {
 494                // really really bad if this didn't work.
 495                // cuz theoretically we would have to clean up.
 496                $this->DisplayErrorPage( $id, $params, $returnid, 
 497                             $this->Lang('error_couldnotassoccss'));
 498                return;
 499              }
 500            $assoc['used'] = 1;
 501          }
 502          }
 503      } // foreach
 504      } // else
 505  } // foreach
 506  
 507  // if we got here, everything should be a-okay
 508      $this->Redirect( $id, 'defaultadmin', $returnid );
 509  ?>


Généré le : Tue Apr 3 18:50:37 2007 par Balluche grâce à PHPXref 0.7