includes/plugins.php
changeset 593 4f9bec0d65c1
parent 590 03a60844c7c5
child 601 9c1f07ae26d5
equal deleted inserted replaced
592:27377179fe58 593:4f9bec0d65c1
   286           if ( !in_array($dh, $restrict) )
   286           if ( !in_array($dh, $restrict) )
   287             continue;
   287             continue;
   288           
   288           
   289         // it's a PHP file, attempt to read metadata
   289         // it's a PHP file, attempt to read metadata
   290         $fullpath = ENANO_ROOT . "/plugins/$dh";
   290         $fullpath = ENANO_ROOT . "/plugins/$dh";
   291         // first can we use cached info?
   291         $plugin_meta = $this->get_plugin_info($fullpath);
   292         if ( isset($plugins_cache[$dh]) && $plugins_cache[$dh]['file md5'] === $this->md5_header($fullpath) )
   292         
       
   293         if ( is_array($plugin_meta) )
   293         {
   294         {
   294           $plugin_meta = $plugins_cache[$dh];
   295           // all checks passed
       
   296           $plugin_list[$dh] = $plugin_meta;
   295         }
   297         }
   296         else
       
   297         {
       
   298           // the cache is out of date if we reached here -- regenerate
       
   299           if ( $use_cache )
       
   300             $this->generate_plugins_cache();
       
   301           
       
   302           // pass 1: try to read a !info block
       
   303           $blockdata = $this->parse_plugin_blocks($fullpath, 'info');
       
   304           if ( empty($blockdata) )
       
   305           {
       
   306             // no !info block, check for old header
       
   307             $fh = @fopen($fullpath, 'r');
       
   308             if ( !$fh )
       
   309               // can't read, bail out
       
   310               continue;
       
   311             $plugin_data = array();
       
   312             for ( $i = 0; $i < 8; $i++ )
       
   313             {
       
   314               $plugin_data[] = @fgets($fh, 8096);
       
   315             }
       
   316             // close our file handle
       
   317             fclose($fh);
       
   318             // is the header correct?
       
   319             if ( trim($plugin_data[0]) != '<?php' || trim($plugin_data[1]) != '/*' )
       
   320             {
       
   321               // nope. get out.
       
   322               continue;
       
   323             }
       
   324             // parse all the variables
       
   325             $plugin_meta = array();
       
   326             for ( $i = 2; $i <= 7; $i++ )
       
   327             {
       
   328               if ( !preg_match('/^([A-z0-9 ]+?): (.+?)$/', trim($plugin_data[$i]), $match) )
       
   329                 continue 2;
       
   330               $plugin_meta[ strtolower($match[1]) ] = $match[2];
       
   331             }
       
   332           }
       
   333           else
       
   334           {
       
   335             // parse JSON block
       
   336             $plugin_data =& $blockdata[0]['value'];
       
   337             $plugin_data = enano_clean_json(enano_trim_json($plugin_data));
       
   338             try
       
   339             {
       
   340               $plugin_meta_uc = enano_json_decode($plugin_data);
       
   341             }
       
   342             catch ( Exception $e )
       
   343             {
       
   344               continue;
       
   345             }
       
   346             // convert all the keys to lowercase
       
   347             $plugin_meta = array();
       
   348             foreach ( $plugin_meta_uc as $key => $value )
       
   349             {
       
   350               $plugin_meta[ strtolower($key) ] = $value;
       
   351             }
       
   352           }
       
   353         }
       
   354         if ( !isset($plugin_meta) || !is_array(@$plugin_meta) )
       
   355         {
       
   356           // parsing didn't work.
       
   357           continue;
       
   358         }
       
   359         // check for required keys
       
   360         $required_keys = array('plugin name', 'plugin uri', 'description', 'author', 'version', 'author uri');
       
   361         foreach ( $required_keys as $key )
       
   362         {
       
   363           if ( !isset($plugin_meta[$key]) )
       
   364             // not set, skip this plugin
       
   365             continue 2;
       
   366         }
       
   367         // decide if it's a system plugin
       
   368         $plugin_meta['system plugin'] = in_array($dh, $this->system_plugins);
       
   369         // reset installed variable
       
   370         $plugin_meta['installed'] = false;
       
   371         $plugin_meta['status'] = 0;
       
   372         // all checks passed
       
   373         $plugin_list[$dh] = $plugin_meta;
       
   374       }
   298       }
   375     }
   299     }
   376     // gather info about installed plugins
   300     // gather info about installed plugins
   377     $q = $db->sql_query('SELECT plugin_id, plugin_filename, plugin_version, plugin_flags FROM ' . table_prefix . 'plugins;');
   301     $q = $db->sql_query('SELECT plugin_id, plugin_filename, plugin_version, plugin_flags FROM ' . table_prefix . 'plugins;');
   378     if ( !$q )
   302     if ( !$q )
   404     ksort($plugin_list);
   328     ksort($plugin_list);
   405     
   329     
   406     // done
   330     // done
   407     return $plugin_list;
   331     return $plugin_list;
   408   }
   332   }
       
   333   
       
   334   /**
       
   335    * Retrieves the metadata block from a plugin file
       
   336    * @param string Path to plugin file (full path)
       
   337    * @return array
       
   338    */
       
   339   
       
   340   function get_plugin_info($fullpath)
       
   341   {
       
   342     global $plugins_cache;
       
   343     $dh = basename($fullpath);
       
   344     
       
   345     // first can we use cached info?
       
   346     if ( isset($plugins_cache[$dh]) && $plugins_cache[$dh]['file md5'] === $this->md5_header($fullpath) )
       
   347     {
       
   348       $plugin_meta = $plugins_cache[$dh];
       
   349     }
       
   350     else
       
   351     {
       
   352       // the cache is out of date if we reached here -- regenerate
       
   353       if ( $use_cache )
       
   354         $this->generate_plugins_cache();
       
   355       
       
   356       // pass 1: try to read a !info block
       
   357       $blockdata = $this->parse_plugin_blocks($fullpath, 'info');
       
   358       if ( empty($blockdata) )
       
   359       {
       
   360         // no !info block, check for old header
       
   361         $fh = @fopen($fullpath, 'r');
       
   362         if ( !$fh )
       
   363           // can't read, bail out
       
   364           return false;
       
   365         $plugin_data = array();
       
   366         for ( $i = 0; $i < 8; $i++ )
       
   367         {
       
   368           $plugin_data[] = @fgets($fh, 8096);
       
   369         }
       
   370         // close our file handle
       
   371         fclose($fh);
       
   372         // is the header correct?
       
   373         if ( trim($plugin_data[0]) != '<?php' || trim($plugin_data[1]) != '/*' )
       
   374         {
       
   375           // nope. get out.
       
   376           return false;
       
   377         }
       
   378         // parse all the variables
       
   379         $plugin_meta = array();
       
   380         for ( $i = 2; $i <= 7; $i++ )
       
   381         {
       
   382           if ( !preg_match('/^([A-z0-9 ]+?): (.+?)$/', trim($plugin_data[$i]), $match) )
       
   383             return false;
       
   384           $plugin_meta[ strtolower($match[1]) ] = $match[2];
       
   385         }
       
   386       }
       
   387       else
       
   388       {
       
   389         // parse JSON block
       
   390         $plugin_data =& $blockdata[0]['value'];
       
   391         $plugin_data = enano_clean_json(enano_trim_json($plugin_data));
       
   392         try
       
   393         {
       
   394           $plugin_meta_uc = enano_json_decode($plugin_data);
       
   395         }
       
   396         catch ( Exception $e )
       
   397         {
       
   398           return false;
       
   399         }
       
   400         // convert all the keys to lowercase
       
   401         $plugin_meta = array();
       
   402         foreach ( $plugin_meta_uc as $key => $value )
       
   403         {
       
   404           $plugin_meta[ strtolower($key) ] = $value;
       
   405         }
       
   406       }
       
   407     }
       
   408     if ( !isset($plugin_meta) || !is_array(@$plugin_meta) )
       
   409     {
       
   410       // parsing didn't work.
       
   411       return false;
       
   412     }
       
   413     // check for required keys
       
   414     $required_keys = array('plugin name', 'plugin uri', 'description', 'author', 'version', 'author uri');
       
   415     foreach ( $required_keys as $key )
       
   416     {
       
   417       if ( !isset($plugin_meta[$key]) )
       
   418         // not set, skip this plugin
       
   419         return false;
       
   420     }
       
   421     // decide if it's a system plugin
       
   422     $plugin_meta['system plugin'] = in_array($dh, $this->system_plugins);
       
   423     // reset installed variable
       
   424     $plugin_meta['installed'] = false;
       
   425     $plugin_meta['status'] = 0;
       
   426     
       
   427     return $plugin_meta;
       
   428   }
       
   429   
   409   
   430   
   410   /**
   431   /**
   411    * Attempts to cache plugin information in a file to speed fetching.
   432    * Attempts to cache plugin information in a file to speed fetching.
   412    */
   433    */
   413   
   434