includes/plugins.php
changeset 507 586fd7d3202d
parent 464 8063eace5b67
child 519 94214ec0871c
--- a/includes/plugins.php	Mon Mar 17 09:47:19 2008 -0400
+++ b/includes/plugins.php	Tue Mar 18 14:32:40 2008 -0400
@@ -2,7 +2,7 @@
 
 /*
  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
- * Version 1.1.2 (Caoineag alpha 2)
+ * Version 1.1.3 (Caoineag alpha 3)
  * Copyright (C) 2006-2007 Dan Fuhry
  *
  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
@@ -172,6 +172,77 @@
   {
     return isset( $this->loaded_plugins[$plugid] );
   }
+  
+  /**
+   * Parses all special comment blocks in a plugin and returns an array in the format:
+   <code>
+   array(
+       0 => array(
+           'block' => 'upgrade',
+           // parsed from the block's parameters section
+             'release_from' => '1.0b1',
+             'release_to' => '1.0b2',
+           'value' => 'foo'
+         ),
+       1 => array(
+           ...
+         )
+     );
+   </code>
+   * @param string Path to plugin file
+   * @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.
+   * @return array
+   */
+  
+  public static function parse_plugin_blocks($file, $type = false)
+  {
+    if ( !file_exists($file) )
+    {
+      return array();
+    }
+    $blocks = array();
+    $contents = @file_get_contents($file);
+    if ( empty($contents) )
+    {
+      return array();
+    }
+    
+    $regexp = '#^/\*\*!([a-z0-9_]+)'  // block header and type
+            . '(([\s]+[a-z0-9_]+[\s]*=[\s]*".+?"[\s]*;)*)' // parameters
+            . '[\s]*\*\*' . "\n"      // spacing and header close
+            . '([\w\W]+?)' . "\n"     // value
+            . '\*\*!\*/'              // closing comment
+            . '#m';
+            
+    // Match out all blocks
+    
+    $results = preg_match_all($regexp, $contents, $blocks);
+    
+    $return = array();
+    foreach ( $blocks[0] as $i => $_ )
+    {
+      if ( is_string($type) && $blocks[1][$i] !== $type )
+        continue;
+      
+      $el = self::parse_vars($blocks[2][$i]);
+      $el['block'] = $blocks[1][$i];
+      $el['value'] = $blocks[4][$i];
+      $return[] = $el;
+    }
+    
+    return $return;
+  }
+  
+  private static function parse_vars($var_block)
+  {
+    preg_match_all('/[\s]+([a-z0-9_]+)[\s]*=[\s]*"(.+?)";/', $var_block, $matches);
+    $return = array();
+    foreach ( $matches[0] as $i => $_ )
+    {
+      $return[ $matches[1][$i] ] = $matches[2][$i];
+    }
+    return $return;
+  }
 }
 
 ?>