[ Index ]
 

Code source de CMS made simple 1.0.5

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

title

Body

[fermer]

/lib/classes/ -> class.moduleoperations.inc.php (source)

   1  <?php
   2  # CMS - CMS Made Simple
   3  # (c)2004-6 by Ted Kulp (ted@cmsmadesimple.org)
   4  # This project's homepage is: http://cmsmadesimple.org
   5  #
   6  # This program is free software; you can redistribute it and/or modify
   7  # it under the terms of the GNU General Public License as published by
   8  # the Free Software Foundation; either version 2 of the License, or
   9  # (at your option) any later version.
  10  #
  11  # This program is distributed in the hope that it will be useful,
  12  # BUT withOUT ANY WARRANTY; without even the implied warranty of
  13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
  14  # GNU General Public License for more details.
  15  # You should have received a copy of the GNU General Public License
  16  # along with this program; if not, write to the Free Software
  17  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA    02111-1307    USA
  18  #
  19  #$Id$
  20  
  21  /**
  22   * "Static" module functions for internal use and module development.  CMSModule
  23   * extends this so that it has internal access to the functions.
  24   *
  25   * @since        0.9
  26   * @package        CMS
  27   */
  28  define( "MODULE_DTD_VERSION", "1.3" );
  29  
  30  require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'class.module.inc.php');
  31  
  32  class ModuleOperations
  33  {
  34    /**
  35     * A member to hold an error string
  36     */
  37    var $error;
  38  
  39    /**
  40     * A member to hold the id of the active tab
  41     */
  42    var $mActiveTab = '';
  43  
  44    /**
  45     * ------------------------------------------------------------------
  46     * Error Functions
  47     * ------------------------------------------------------------------
  48     */
  49  
  50    /**
  51     * Set an error condition
  52     */
  53    function SetError($str = '')
  54    {
  55      global $gCms;
  56      $gCms->variables['error'] = $str;
  57    }
  58  
  59    /**
  60     * Return the last error
  61     */
  62    function GetLastError()
  63    {
  64      global $gCms;
  65      if( isset( $gCms->variables['error'] ) )
  66        return $gCms->variables['error'];
  67      return "";
  68    }
  69  
  70      /**
  71       * Creates an xml data package from the module directory.
  72       */
  73  	function CreateXMLPackage( &$modinstance, &$message, &$filecount )
  74      {
  75        // get a file list
  76        $filecount = 0;
  77        $dir = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR."modules".DIRECTORY_SEPARATOR.$modinstance->GetName();
  78        $files = get_recursive_file_list( $dir, $modinstance->xml_exclude_files );
  79  
  80        $xmltxt  = '<?xml version="1.0" encoding="ISO-8859-1"?>';
  81        $xmltxt .= $modinstance->xmldtd."\n";
  82        $xmltxt .= "<module>\n";
  83            $xmltxt .= "    <dtdversion>".MODULE_DTD_VERSION."</dtdversion>\n";
  84        $xmltxt .= "    <name>".$modinstance->GetName()."</name>\n";
  85        $xmltxt .= "    <version>".$modinstance->GetVersion()."</version>\n";
  86        $xmltxt .= "  <mincmsversion>".$modinstance->MinimumCMSVersion()."</mincmsversion>\n";
  87        $xmltxt .= "    <help>".base64_encode($modinstance->GetHelpPage())."</help>\n";
  88        $xmltxt .= "    <about>".base64_encode($modinstance->GetAbout())."</about>\n";
  89        $desc = $modinstance->GetAdminDescription();
  90        if( $desc != '' )
  91          {
  92            $xmltxt .= "    <description>".$desc."</description>\n";
  93          }
  94        $depends = $modinstance->GetDependencies();
  95        foreach( $depends as $key=>$val )
  96          {
  97            $xmltxt .= "    <requires>\n";
  98                $xmltxt .= "      <requiredname>$key</requiredname>\n";
  99                $xmltxt .= "      <requiredversion>$val</requiredversion>\n";
 100            $xmltxt .= "    </requires>\n";
 101          }
 102        foreach( $files as $file )
 103          {
 104            // strip off the beginning
 105            if (substr($file,0,strlen($dir)) == $dir)
 106               {
 107               $file = substr($file,strlen($dir));
 108               }
 109            if( $file == '' ) continue;
 110  
 111            $xmltxt .= "    <file>\n";
 112            $filespec = $dir.DIRECTORY_SEPARATOR.$file;
 113            $xmltxt .= "      <filename>$file</filename>\n";
 114            if( @is_dir( $filespec ) )
 115          {
 116            $xmltxt .= "      <isdir>1</isdir>\n";
 117          }
 118            else
 119          {
 120            $xmltxt .= "      <isdir>0</isdir>\n";
 121            $data = base64_encode(file_get_contents($filespec));
 122            $xmltxt .= "      <data><![CDATA[".$data."]]></data>\n";
 123          }
 124  
 125            $xmltxt .= "    </file>\n";
 126            ++$filecount;
 127          }
 128            $xmltxt .= "</module>\n";
 129        $message = 'XML package of '.strlen($xmltxt).' bytes created for '.$modinstance->GetName();
 130        $message .= ' including '.$filecount.' files';
 131        return $xmltxt;
 132      }
 133  
 134    /**
 135     * Unpackage a module from an xml string
 136     * does not touch the database
 137     */
 138    function ExpandXMLPackage( $xml, $overwrite = 0, $brief = 0 )
 139    {
 140      global $gCms;
 141  
 142      // first make sure that we can actually write to the module directory
 143      $dir = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR."modules";
 144  
 145      if( !is_writable( $dir ) && $brief != 0 )
 146        {
 147          // directory not writable
 148          ModuleOperations::SetError( lang( 'errordirectorynotwritable' ) );
 149          return false;
 150        }
 151  
 152      // start parsing xml
 153      $parser = xml_parser_create();
 154      $ret = xml_parse_into_struct( $parser, $xml, $val, $xt );
 155      xml_parser_free( $parser );
 156  
 157      if( $ret == 0 )
 158        {
 159          ModuleOperations::SetError( lang( 'errorcouldnotparsexml' ) );
 160          return false;
 161        }
 162  
 163      ModuleOperations::SetError( "" );
 164      $havedtdversion = false;
 165      $moduledetails = array();
 166      $moduledetails['size'] = strlen($xml);
 167      $required = array();
 168      foreach( $val as $elem )
 169        {
 170          $value = (isset($elem['value'])?$elem['value']:'');
 171          $type = (isset($elem['type'])?$elem['type']:'');
 172          switch( $elem['tag'] )
 173            {
 174            case 'NAME':
 175          {
 176            if( $type != 'complete' && $type != 'close' )
 177              {
 178                continue;
 179              }
 180            // check if this module is already installed
 181            if( isset( $gCms->modules[$value] ) && $overwrite == 0 && $brief == 0 )
 182              {
 183                ModuleOperations::SetError( lang( 'moduleinstalled' ) );
 184                return false;
 185              }
 186            $moduledetails['name'] = $value;
 187            break;
 188          }
 189  
 190            case 'DTDVERSION':
 191          {
 192            if( $type != 'complete' && $type != 'close' )
 193              {
 194                continue;
 195              }
 196            if( $value != MODULE_DTD_VERSION )
 197              {
 198                ModuleOperations::SetError( lang( 'errordtdmismatch' ) );
 199                return false;
 200              }
 201            $havedtdversion = true;
 202            break;
 203          }
 204  
 205            case 'VERSION':
 206          {
 207            if( $type != 'complete' && $type != 'close' )
 208              {
 209                continue;
 210              }
 211            $moduledetails['version'] = $value;
 212            if( isset( $gCms->modules[$moduledetails['name']] ) )
 213              {
 214                $version = $gCms->modules[$moduledetails['name']]['object']->GetVersion();
 215                if( $moduledetails['version'] < $version && $brief == 0)
 216              {
 217                ModuleOperations::SetError( lang('errorattempteddowngrade') );
 218                return false;
 219              }
 220                else if ($moduledetails['version'] == $version && $brief == 0 )
 221              {
 222                ModuleOperations::SetError( lang('moduleinstalled') );
 223                return false;
 224              }
 225              }
 226            break;
 227          }
 228          
 229            case 'MINCMSVERSION':
 230          {
 231            if( $type != 'complete' && $type != 'close' )
 232              {
 233                continue;
 234              }
 235            $moduledetails['mincmsversion'] = $value;
 236            break;
 237          }
 238          
 239            case 'MAXCMSVERSION':
 240          {
 241            if( $type != 'complete' && $type != 'close' )
 242              {
 243                continue;
 244              }
 245            $moduledetails['maxcmsversion'] = $value;
 246            break;
 247          }
 248          
 249            case 'DESCRIPTION':
 250          {
 251            if( $type != 'complete' && $type != 'close' )
 252              {
 253                continue;
 254              }
 255            $moduledetails['description'] = $value;
 256            break;
 257          }
 258          
 259            case 'HELP':
 260          {
 261            if( $type != 'complete' && $type != 'close' )
 262              {
 263                continue;
 264              }
 265            $moduledetails['help'] = base64_decode($value);
 266            break;
 267          }
 268          
 269            case 'ABOUT':
 270          {
 271            if( $type != 'complete' && $type != 'close' )
 272              {
 273                continue;
 274              }
 275            $moduledetails['about'] = base64_decode($value);
 276            break;
 277          }
 278          
 279            case 'REQUIRES':
 280          {
 281            if( $type != 'complete' && $type != 'close' )
 282              {
 283                continue;
 284              }
 285            if( count($requires) != 2 )
 286              {
 287                continue;
 288              }
 289            if( !isset( $moduledetails['requires'] ) )
 290              {
 291                $moduledetails['requires'] = array();
 292              }
 293            $moduledetails['requires'][] = $requires;
 294            $requires = array();
 295          }
 296          
 297            case 'REQUIREDNAME':
 298          $requires['name'] = $value;
 299          break;
 300  
 301            case 'REQUIREDVERSION':
 302          $requires['version'] = $value;
 303          break;
 304  
 305            case 'FILE':
 306          {
 307            if( $type != 'complete' && $type != 'close' )
 308              {
 309                continue;
 310              }
 311            if( $brief != 0 )
 312              {
 313                continue;
 314              }
 315  
 316            // finished a first file
 317            if( !isset( $moduledetails['name'] )       || !isset( $moduledetails['version'] ) ||
 318                !isset( $moduledetails['filename'] ) || !isset( $moduledetails['isdir'] ) )
 319              {
 320                print_r( $moduledetails );
 321                ModuleOperations::SetError( lang('errorincompletexml') );
 322                return false;
 323              }
 324  
 325            // ready to go
 326            $moduledir=$dir.DIRECTORY_SEPARATOR.$moduledetails['name'];
 327            $filename=$moduledir.$moduledetails['filename'];
 328            if( !file_exists( $moduledir ) )
 329              {
 330                if( !@mkdir( $moduledir ) && !is_dir( $moduledir ) )
 331              {
 332                ModuleOperations::SetError(lang('errorcantcreatefile').' '.$moduledir);
 333                break;
 334              }
 335              }
 336            else if( $moduledetails['isdir'] )
 337              {
 338                if( !@mkdir( $filename ) && !is_dir( $filename ) )
 339              {
 340                ModuleOperations::SetError(lang('errorcantcreatefile').' '.$filename);
 341                break;
 342              }
 343              }
 344            else
 345              {
 346                $data = $moduledetails['filedata'];
 347                if( strlen( $data ) )
 348              {
 349                $data = base64_decode( $data );
 350              }
 351                $fp = @fopen( $filename, "w" );
 352                if( !$fp )
 353              {
 354                ModuleOperations::SetError(lang('errorcantcreatefile').' '.$filename);
 355              }
 356                if( strlen( $data ) )
 357              {
 358                @fwrite( $fp, $data );
 359              }
 360                @fclose( $fp );
 361              }
 362            unset( $moduledetails['filedata'] );
 363            unset( $moduledetails['filename'] );
 364            unset( $moduledetails['isdir'] );
 365          }
 366  
 367            case 'FILENAME':
 368          $moduledetails['filename'] = $value;
 369          break;
 370  
 371            case 'ISDIR':
 372          $moduledetails['isdir'] = $value;
 373          break;
 374  
 375            case 'DATA':
 376          if( $type != 'complete' && $type != 'close' )
 377            {
 378              continue;
 379            }
 380          $moduledetails['filedata'] = $value;
 381          break;
 382            }
 383        } // foreach
 384  
 385      if( $havedtdversion == false )
 386        {
 387          ModuleOperations::SetError( lang( 'errordtdmismatch' ) );
 388        }
 389  
 390      // we've created the module's directory
 391      unset( $moduledetails['filedata'] );
 392      unset( $moduledetails['filename'] );
 393      unset( $moduledetails['isdir'] );
 394  
 395      if( ModuleOperations::GetLastError() != "" )
 396        {
 397          return false;
 398        }
 399      return $moduledetails;
 400    }
 401  
 402  
 403    /**
 404     * Install a module into the database
 405     */
 406    function InstallModule($module, $loadifnecessary = false)
 407    {
 408      global $gCms;
 409      if( !isset( $gCms->modules[$module] ) )
 410        {
 411      if( $loadifnecessary == false )
 412        {
 413          return array(false,lang('errormodulenotloaded'));
 414        }
 415      else
 416        {
 417          if( !ModuleOperations::LoadNewModule( $module ) )
 418            {
 419          return array(false,lang('errormodulewontload'));
 420            }
 421        }
 422        }
 423   
 424      $db =& $gCms->GetDb();
 425      if (isset($gCms->modules[$module]))
 426        {
 427      $modinstance =& $gCms->modules[$module]['object'];
 428      $result = $modinstance->Install();
 429      
 430          #now insert a record
 431      if (!isset($result) || $result === FALSE)
 432        {
 433          $query = "INSERT INTO ".cms_db_prefix()."modules (module_name, version, status, admin_only, active) VALUES (?,?,'installed',?,?)";
 434          $db->Execute($query, array($module,$modinstance->GetVersion(),($modinstance->IsAdminOnly()==true?1:0),1));
 435          
 436              #and insert any dependancies
 437          if (count($modinstance->GetDependencies()) > 0) #Check for any deps
 438            {
 439                  #Now check to see if we can satisfy any deps
 440          foreach ($modinstance->GetDependencies() as $onedepkey=>$onedepvalue)
 441            {
 442              $time = $db->DBTimeStamp(time());
 443              $query = "INSERT INTO ".cms_db_prefix()."module_deps (parent_module, child_module, minimum_version, create_date, modified_date) VALUES (?,?,?,".$time.",".$time.")";
 444              $db->Execute($query, array($onedepkey, $module, $onedepvalue));
 445            }
 446            }
 447          
 448              #send an event saying the module has been installed
 449          Events::SendEvent('Core', 'ModuleInstalled', array('name' => &$module, 'version' => $modinstance->GetVersion()));
 450          
 451          // and we're done
 452          return array(true);
 453        }
 454      else
 455        {
 456          if( trim($result) == "" )
 457            {
 458          $result = lang('errorinstallfailed');
 459            }
 460          return array(false,$result);
 461        }
 462        }    
 463      else
 464        {
 465      return array(false,lang('errormodulenotfound'));
 466        }
 467    }
 468  
 469  
 470    /**
 471     * Load a single module from the filesystem
 472     */
 473    function LoadNewModule( $modulename )
 474    {
 475      global $gCms;
 476      $db =& $gCms->GetDb();
 477      $cmsmodules = &$gCms->modules;
 478      
 479      $dir = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR."modules";
 480  
 481      if (@is_file("$dir/$modulename/$modulename.module.php"))
 482        {
 483      // question: is this a potential XSS vulnerability
 484      include("$dir/$modulename/$modulename.module.php");
 485      if( !class_exists( $modulename ) )
 486        {
 487          return false;
 488        }
 489  
 490      $newmodule =& new $modulename;
 491      $name = $newmodule->GetName();
 492      $cmsmodules[$name]['object'] =& $newmodule;
 493      $cmsmodules[$name]['installed'] = false;
 494      $cmsmodules[$name]['active'] = false;
 495        }
 496      else
 497        {
 498      unset($cmsmodules[$modulename]);
 499      return false;
 500        }
 501      return true;
 502    }
 503  
 504    /**
 505     * Upgrade a module
 506     */
 507    function UpgradeModule( $module, $oldversion, $newversion )
 508    {
 509      global $gCms;
 510      $db =& $gCms->GetDb();
 511  
 512      if (!isset($gCms->modules[$module]))
 513        {
 514      return false;
 515        }
 516  
 517      $modinstance = $gCms->modules[$module]['object'];
 518      $result = $modinstance->Upgrade($oldversion, $newversion);
 519      if( $result === FALSE )
 520        {
 521      return false;
 522        }
 523  
 524      $query = "UPDATE ".cms_db_prefix()."modules SET version = ?, admin_only = ? WHERE module_name = ?";
 525      $db->Execute($query,array($newversion,($modinstance->IsAdminOnly()==true?1:0),$module));
 526      
 527      Events::SendEvent('Core', 'ModuleUpgraded', array('name' => $module, 
 528                                'oldversion' => $oldversion, 
 529                                'newversion' => $oldversion));
 530  
 531      return true;
 532    }
 533  
 534      /**
 535       * Returns a hash of all loaded modules.  This will include all
 536       * modules loaded by LoadModules, which could either be all or them,
 537       * or just ones that are active and installed.
 538       */
 539      function & GetAllModules()
 540      {
 541          global $gCms;
 542          $cmsmodules = &$gCms->modules;
 543          return $cmsmodules;
 544      }
 545  
 546  }


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