includes/plugins.php
changeset 507 586fd7d3202d
parent 464 8063eace5b67
child 519 94214ec0871c
equal deleted inserted replaced
506:da0f2a76add5 507:586fd7d3202d
     1 <?php
     1 <?php
     2 
     2 
     3 /*
     3 /*
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     5  * Version 1.1.2 (Caoineag alpha 2)
     5  * Version 1.1.3 (Caoineag alpha 3)
     6  * Copyright (C) 2006-2007 Dan Fuhry
     6  * Copyright (C) 2006-2007 Dan Fuhry
     7  *
     7  *
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    10  *
    10  *
   170   
   170   
   171   function loaded($plugid)
   171   function loaded($plugid)
   172   {
   172   {
   173     return isset( $this->loaded_plugins[$plugid] );
   173     return isset( $this->loaded_plugins[$plugid] );
   174   }
   174   }
       
   175   
       
   176   /**
       
   177    * Parses all special comment blocks in a plugin and returns an array in the format:
       
   178    <code>
       
   179    array(
       
   180        0 => array(
       
   181            'block' => 'upgrade',
       
   182            // parsed from the block's parameters section
       
   183              'release_from' => '1.0b1',
       
   184              'release_to' => '1.0b2',
       
   185            'value' => 'foo'
       
   186          ),
       
   187        1 => array(
       
   188            ...
       
   189          )
       
   190      );
       
   191    </code>
       
   192    * @param string Path to plugin file
       
   193    * @param string Optional. The type of block to fetch. If this is specified, only the block type specified will be read, all others will be discarded.
       
   194    * @return array
       
   195    */
       
   196   
       
   197   public static function parse_plugin_blocks($file, $type = false)
       
   198   {
       
   199     if ( !file_exists($file) )
       
   200     {
       
   201       return array();
       
   202     }
       
   203     $blocks = array();
       
   204     $contents = @file_get_contents($file);
       
   205     if ( empty($contents) )
       
   206     {
       
   207       return array();
       
   208     }
       
   209     
       
   210     $regexp = '#^/\*\*!([a-z0-9_]+)'  // block header and type
       
   211             . '(([\s]+[a-z0-9_]+[\s]*=[\s]*".+?"[\s]*;)*)' // parameters
       
   212             . '[\s]*\*\*' . "\n"      // spacing and header close
       
   213             . '([\w\W]+?)' . "\n"     // value
       
   214             . '\*\*!\*/'              // closing comment
       
   215             . '#m';
       
   216             
       
   217     // Match out all blocks
       
   218     
       
   219     $results = preg_match_all($regexp, $contents, $blocks);
       
   220     
       
   221     $return = array();
       
   222     foreach ( $blocks[0] as $i => $_ )
       
   223     {
       
   224       if ( is_string($type) && $blocks[1][$i] !== $type )
       
   225         continue;
       
   226       
       
   227       $el = self::parse_vars($blocks[2][$i]);
       
   228       $el['block'] = $blocks[1][$i];
       
   229       $el['value'] = $blocks[4][$i];
       
   230       $return[] = $el;
       
   231     }
       
   232     
       
   233     return $return;
       
   234   }
       
   235   
       
   236   private static function parse_vars($var_block)
       
   237   {
       
   238     preg_match_all('/[\s]+([a-z0-9_]+)[\s]*=[\s]*"(.+?)";/', $var_block, $matches);
       
   239     $return = array();
       
   240     foreach ( $matches[0] as $i => $_ )
       
   241     {
       
   242       $return[ $matches[1][$i] ] = $matches[2][$i];
       
   243     }
       
   244     return $return;
       
   245   }
   175 }
   246 }
   176 
   247 
   177 ?>
   248 ?>