Guest User

Untitled

a guest
Aug 10th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 37.88 KB | None | 0 0
  1. <?php
  2. class Plugins_lib {
  3.     /**
  4.      * Instance of this class (CI basically initiates all models/libs as
  5.      * singletons, so essentially, this is a reference to a singleton
  6.      * @var object
  7.      */
  8.     public static $instance;
  9.  
  10.     /**
  11.      * Static CI reference property
  12.      * @var object
  13.      */
  14.     private static $CI;
  15.  
  16.     /**
  17.      * Plugins Model
  18.      * @var object
  19.      */
  20.     private static $PM;
  21.  
  22.     /**
  23.      * Plugin Path - Specified in config plugin_path
  24.      * @var string
  25.      */
  26.     private static $plugin_path;
  27.  
  28.     /**
  29.      * Plugin messages (Errors, debugging, warnings)
  30.      * @var array
  31.      */
  32.     private static $messages;
  33.  
  34.     /**
  35.      * Plugins List
  36.      * @var  array
  37.      */
  38.     public static $plugins;
  39.  
  40.     /**
  41.      * Array of active plugins - This is a reference of $plugins['enabled']
  42.      * @var  array
  43.      */
  44.     public static $enabled_plugins;
  45.  
  46.     /**
  47.      * Actions/Filters to run
  48.      * @var array
  49.      */
  50.     public static $actions = array();
  51.  
  52.     /**
  53.      * Current tag being executed
  54.      * @var  string
  55.      */
  56.     public static $current_action;
  57.  
  58.     /**
  59.      * Previously executed tags
  60.      * @var array
  61.      */
  62.     public static $run_actions = array();
  63.  
  64.     // ------------------------------------------------------------------------
  65.  
  66.     /**
  67.      * Plugin Constructor
  68.      *
  69.      * Constructor performs...
  70.      *  - Set $CI Reference
  71.      *  - Load Plugins Model
  72.      *  - Set the plugin directory
  73.      *  - Get plugins from DB and include enabled plugins
  74.      *  - Load the plugins by initializing the plugin Classes
  75.      *
  76.      * @access public
  77.      */
  78.     public function __construct()
  79.     {
  80.         // Codeigniter instance
  81.         static::$CI =& get_instance();
  82.  
  83.         // Set a static handler for the instance (So it can be called outside of CI classes, IE: below)
  84.         // No real need to setup a singleton with CI....
  85.         static::$instance = $this;
  86.  
  87.         // Load the plugins model, if loaded by autoloader, it
  88.         static::$CI->load->model('Plugins_model');
  89.  
  90.         // Set a shorter handle for the plugins model
  91.         static::$PM = static::$CI->Plugins_model;
  92.  
  93.         static::$messages = array(
  94.             'error' => [],
  95.             'debug' => [],
  96.             'warn'  => []
  97.         );
  98.  
  99.         // Set plugin path
  100.         $this->set_plugin_dir();
  101.  
  102.         // Get all activated plugins
  103.         $this->get_plugins();
  104.  
  105.         // Include plugins
  106.         $this->include_enabled_plugins();
  107.  
  108.         // Load them
  109.         $this->load_enabled_plugins();
  110.     }
  111.  
  112.     // ------------------------------------------------------------------------
  113.  
  114.     /**
  115.      * Set Plugin Directory
  116.      *
  117.      * Set the plugins directory that contains all the plugin folders, at the local
  118.      * private property: static::$plugin_path
  119.      *
  120.      * @param   str $dir Directory, an ending slash is appended if not found, and any
  121.      *                   accidental double slashes are replaced with single slashes
  122.      * @access private
  123.      */
  124.     private function set_plugin_dir()
  125.     {
  126.         if($path = static::$CI->config->item('plugin_path'))
  127.         {
  128.             $this->debug("Plugin path set to {$path} via config setting");
  129.         }
  130.         elseif(defined('PLUGIN_DIR'))
  131.         {
  132.             $this->debug("Plugin path set to ".PLUGIN_DIR."via constant PLUGIN_DIR");
  133.  
  134.             $path = PLUGIN_DIR;
  135.         }
  136.         else
  137.         {
  138.             $path = FCPATH . 'plugins/';
  139.  
  140.             $this->debug("Plugin path defaulted to {$path}");
  141.  
  142.             $this->warn("No plugin path specified in CI settings or PLUGIN_DIR constant, defaulting to {$path}");
  143.         }
  144.  
  145.         // Make sure it ends in /
  146.         if( ! preg_match('%/$%', $path))
  147.         {
  148.             $path = "{$path}/";
  149.         }
  150.  
  151.         static::$plugin_path = str_replace('//','/',$path);
  152.     }
  153.  
  154.     // ------------------------------------------------------------------------
  155.  
  156.     /**
  157.      * View Controller
  158.      *
  159.      * View the controller from the plugin.
  160.      *
  161.      * @param   string  $plugin         Systen name of plugin
  162.      * @param   mixed   $plugin_data    Any data to hand down to the plugin (Anything
  163.      *                                  processed from the controller, etc)
  164.      * @access  public
  165.      * @return  string  Whatever content is returned from the plugins controller (which
  166.      *                  is usually HTML from the settings form)
  167.      */
  168.     public function view_controller($plugin, $plugin_data = NULL)
  169.     {
  170.         if( ! isset(static::$plugins[$plugin]))
  171.         {
  172.             Logger_lib::error("The plugin {$plugin} was not found");
  173.  
  174.             return FALSE;
  175.         }
  176.         elseif( ! method_exists(static::$plugins[$plugin]['handler'], 'controller'))
  177.         {
  178.             App_lib::add_notification('error','Plugin Error',"The plugin {$plugin} does not have a controller", TRUE);
  179.  
  180.             return FALSE;
  181.         }
  182.  
  183.         return call_user_func(array(static::$plugins[$plugin]['handler'], 'controller'), $plugin_data);
  184.     }
  185.  
  186.     // ------------------------------------------------------------------------
  187.  
  188.     /**
  189.      * Plugin Error Collector
  190.      *
  191.      * Push an error message into the messages array
  192.      *
  193.      * @param   str     $message        Message to push
  194.      * @access  private
  195.      */
  196.     private function error($message)
  197.     {
  198.         log_message('error', 'PLUGIN-ERROR: ' . $message);
  199.  
  200.         array_push(static::$messages['error'], $message);
  201.     }
  202.  
  203.     // ------------------------------------------------------------------------
  204.  
  205.     /**
  206.      * Plugin Debug Collector
  207.      *
  208.      * Push a debug message into the messages array
  209.      *
  210.      * @param   str     $message        Message to push
  211.      * @access  private
  212.      */
  213.     private function debug($message)
  214.     {
  215.         log_message('debug', 'PLUGIN-DEBUG: ' . $message);
  216.  
  217.         array_push(static::$messages['debug'], $message);
  218.     }
  219.  
  220.     // ------------------------------------------------------------------------
  221.  
  222.     /**
  223.      * Plugin Warn Collector
  224.      *
  225.      * Push a warn message into the messages array
  226.      *
  227.      * @param   str     $message        Message to push
  228.      * @access  private
  229.      */
  230.     private function warn($message)
  231.     {
  232.         log_message('error', 'PLUGIN-WARN: ' . $message);
  233.  
  234.         array_push(static::$messages['warn'], $message);
  235.     }
  236.  
  237.     // ------------------------------------------------------------------------
  238.  
  239.     /**
  240.      * Enable Plugin
  241.      *
  242.      * Enable a plugin by setting the plugins.status to 1 in the plugins table
  243.      *
  244.      * @oaram   string  $plugin     Plugin Name
  245.      * @param   mixed   $data       Any data that should be handed down to the plugins deactivate method (optional)
  246.      * @access  public
  247.      * @return  bool
  248.      */
  249.     public function enable_plugin($plugin, $data = NULL)
  250.     {
  251.         // Run the plugins activation method to run anything required by the plugin
  252.         call_user_func(array(static::$plugins[$plugin]['handler'], 'activate'), $data);
  253.  
  254.         // Enable it in the database
  255.         return static::$PM->set_status($plugin, 1);
  256.     }
  257.  
  258.     // ------------------------------------------------------------------------
  259.  
  260.     /**
  261.      * Disable Plugin
  262.      *
  263.      * Disable a plugin by setting the plugins.status to 0 in the plugins table
  264.      *
  265.      * @oaram   string  $plugin     Plugin Name
  266.      * @param   mixed   $data       Any data that should be handed down to the plugins activate method (optional)
  267.      * @access  public
  268.      * @return  bool
  269.      */
  270.     public function disable_plugin($plugin, $data = NULL)
  271.     {
  272.         // Run the plugins deactivation method to run anything required by the plugin
  273.         call_user_func(array(static::$plugins[$plugin]['handler'], 'deactivate'), $data);
  274.  
  275.         // Disable it in the database
  276.         return static::$PM->set_status($plugin, 0);
  277.     }
  278.  
  279.     // ------------------------------------------------------------------------
  280.  
  281.     /**
  282.      * Install Plugin
  283.      *
  284.      * Install a plugin by adding it to the database and executing any installation code thats in
  285.      * the plugins install method
  286.      *
  287.      * @param   string  $plugin     Plugins system name (Folder name)
  288.      * @access  public
  289.      * @param   boolean
  290.      */
  291.     public function install_plugin($plugin, $data = NULL)
  292.     {
  293.         // System name for folder and file
  294.         $system_name = strtolower($plugin);
  295.  
  296.         // Class name is just system name with ucfirst
  297.         $class_name = ucfirst($system_name);
  298.  
  299.         $plugin_path = static::$plugin_path . "{$system_name}/{$system_name}.php";
  300.  
  301.         // If the plugins class hasnt been loaded...
  302.         if( ! class_exists($class_name))
  303.         {
  304.             // Make sure a valid plugin file by the same name as the folder exists
  305.             if (file_exists($plugin_path))
  306.             {
  307.                 if( ! include_once $plugin_path)
  308.                 {
  309.                     $this->error("Failed to install {$plugin}, there was an error loading the plugin file {$plugin_path}, is it readable?");
  310.                 }
  311.                 else
  312.                 {
  313.                     $this->debug("Successfully loaded the plugin file {$plugin_path}");
  314.                 }
  315.             }
  316.             else
  317.             {
  318.                 $this->error("Failed to install {$plugin}, unable to find the file {$plugin_path}");
  319.             }
  320.         }
  321.  
  322.         // Execute the plugin installation
  323.         return call_user_func("{$class_name}::install", $data);
  324.     }
  325.  
  326.     // ------------------------------------------------------------------------
  327.  
  328.     /**
  329.      * Plugin Details
  330.      *
  331.      * Retrieve the details of a plugin from the database
  332.      *
  333.      * @param   string  $plugin Plugin system name
  334.      * @access  public
  335.      * @return  object|bool
  336.      */
  337.     public function plugin_details($plugin)
  338.     {
  339.         return static::$PM->get_plugin($plugin);
  340.     }
  341.  
  342.     // ------------------------------------------------------------------------
  343.  
  344.     /**
  345.      * Get Enabled Plugins
  346.      *
  347.      * Retrieve the enabled plugins from the database and load them into the enabled_plugins
  348.      * array. This does not initiate the plugins, thats done in a different method
  349.      *
  350.      * @access private
  351.      */
  352.     private function get_plugins()
  353.     {
  354.         // Fetch all plugins
  355.         if( ! $plugins = static::$PM->get_plugins())
  356.         {
  357.             return FALSE;
  358.         }
  359.  
  360.         // Load the plugins
  361.         foreach($plugins as $p)
  362.         {
  363.             if( ! isset( static::$plugins[ $p->system_name ] ) )
  364.             {
  365.                 $this->debug( "Adding plugin {$p->system_name}" );
  366.  
  367.                 static::$plugins[$p->system_name] = array(
  368.                     'data' =>  $p->data
  369.                 );
  370.  
  371.                 // If its enabled, add it to $enabled_plugins referencing the plugin in $plugins
  372.                 if($p->status == '1')
  373.                 {
  374.                     $this->debug( "Enabling plugin {$p->system_name}" );
  375.  
  376.                     static::$enabled_plugins[ $p->system_name ] = &static::$plugins[$p->system_name];
  377.                 }
  378.             }
  379.         }
  380.     }
  381.  
  382.     // ------------------------------------------------------------------------
  383.  
  384.     /**
  385.      * Print Plugins
  386.      *
  387.      * Retrieve all plugin information from the database
  388.      *
  389.      * @access public
  390.      * @return array
  391.      */
  392.     public function retrieve_plugins()
  393.     {
  394.         return static::$PM->get_plugins();
  395.     }
  396.  
  397.     // ------------------------------------------------------------------------
  398.  
  399.     /**
  400.      * Include Enabled Plugins
  401.      *
  402.      * Iterate through the enabled_plugins property array and include the actual plugin files
  403.      *
  404.      * @access private
  405.      */
  406.     private function include_enabled_plugins()
  407.     {
  408.         if(empty(static::$enabled_plugins))
  409.         {
  410.             $this->error("Unable to include enabled plugin files, enabled plugins not retrieved");
  411.  
  412.             return FALSE;
  413.         }
  414.  
  415.         foreach(static::$enabled_plugins as $name => $p)
  416.         {
  417.             $plugin_path = static::$plugin_path . "{$name}/{$name}.php";
  418.  
  419.             // Make sure a valid plugin file by the same name as the folder exists
  420.             if (file_exists($plugin_path))
  421.             {
  422.                 if( ! include_once $plugin_path)
  423.                 {
  424.                     $this->error("There was an error loading the plugin file {$plugin_path}");
  425.                 }
  426.                 else
  427.                 {
  428.                     $this->debug("Successfully loaded the plugin file {$plugin_path}");
  429.                 }
  430.             }
  431.             else
  432.             {
  433.                 $this->error("Failed to include the plugin {$name}, unable to find the file {$plugin_path}");
  434.             }
  435.         }
  436.     }
  437.  
  438.     // ------------------------------------------------------------------------
  439.  
  440.     /**
  441.      * Load Enabled Plugins
  442.      *
  443.      * Load the enabled plugins into objects, store the objects into the loaded plugins array
  444.      *
  445.      * @access private
  446.      */
  447.     private function load_enabled_plugins()
  448.     {
  449.         foreach(static::$enabled_plugins as $name => $p)
  450.         {
  451.             new $name;
  452.         }
  453.     }
  454.  
  455.     // ------------------------------------------------------------------------
  456.  
  457.     /**
  458.      * Add Action
  459.      *
  460.      * Assign a function to be executed by a certain tag. An action will just fire off a function
  461.      * when the tag is called, a filter will parse data and return the modified data
  462.      *
  463.      * @param string        $tag        Tag to add filter to
  464.      * @param string|array  $function   Either a string (function), or an array (Object, method)
  465.      * @param int           $priority   Priority
  466.      * @param string        $type       Either action or filter
  467.      * @access public
  468.      * @return bool
  469.      */
  470.     public function add_action($tag, $function, $priority = 10, $type = 'action')
  471.     {
  472.         if(is_array($function))
  473.         {
  474.             if(count($function) < 2)
  475.             {
  476.                 // If its an array of one element, then just add the first element
  477.                 $function = $function[0];
  478.             }
  479.             elseif( ! is_object($function[0]))
  480.             {
  481.                 $this->error("Failing to add method '" . implode('::', $function) . "'' as {$type} to tag {$tag}, an array was given, first element was not an object");
  482.  
  483.                 return FALSE;
  484.             }
  485.             elseif( ! method_exists($function[0], $function[1]))
  486.             {
  487.                 $this->error("Failing to add method '" . get_class($function[0]) . "::{$function[1]}' as {$type} to tag {$tag}, the method does not exist");
  488.  
  489.                 return FALSE;
  490.             }
  491.         }
  492.  
  493.         // Execute is_array again, since the above could have converted it to an array
  494.         if( ! is_array($function))
  495.         {
  496.             if( ! function_exists($function))
  497.             {
  498.                 $this->error("Failing to add function {$function} as {$type} to tag {$tag}, the function does not exist");
  499.  
  500.                 return FALSE;
  501.             }
  502.         }
  503.  
  504.         if( ! in_array($type, ['action','filter']))
  505.         {
  506.             $this->error("Unknown type '{$type}', must be 'filter' or 'action'");
  507.  
  508.             return FALSE;
  509.         }
  510.  
  511.         static::$actions[$tag][$priority][] = array(
  512.             'function' => $function,
  513.             'type'  => $type
  514.         );
  515.  
  516.         return TRUE;
  517.     }
  518.  
  519.     // ------------------------------------------------------------------------
  520.  
  521.     /**
  522.      * Add Filter
  523.      *
  524.      * Just a wrapper for add_function except adds it as type 'filter'. Filters will
  525.      * take in data and perform an action on it, then return it, actions will just
  526.      * fire off a function
  527.      *
  528.      * @param string        $tag        Tag to add filter to
  529.      * @param string|array  $function   Either a string (function), or an array (Object, method)
  530.      * @param int           $priority   Priority
  531.      * @access public
  532.      * @return bool
  533.      */
  534.     public function add_filter($tag, $function, $priority = 10)
  535.     {
  536.         return $this->add_action($tag, $function, $priority, 'filter');
  537.     }
  538.  
  539.     // ------------------------------------------------------------------------
  540.  
  541.     /**
  542.      * Get Actions
  543.      *
  544.      * Get actions....
  545.      *
  546.      * @access public
  547.      * @return array
  548.      */
  549.     public function get_actions()
  550.     {
  551.         foreach(static::$actions as $k => $a)
  552.             ksort(static::$actions[$k]);
  553.  
  554.         return static::$actions;
  555.     }
  556.  
  557.     // ------------------------------------------------------------------------
  558.  
  559.     /**
  560.      * Do Action
  561.      *
  562.      * Execute a specific action, pass optional arguments to it
  563.      * @param string    $tag    Tag to execute
  564.      * @param null      $args   Arguments to hand to functions assigned to tag
  565.      * @access public
  566.      * @return mixed    Returns whatever the type of $args is
  567.      * @todo This seems broken, when asset.(edit|create).post array is filtered, it returns a single value
  568.      */
  569.     public function do_action($tag, $args = NULL)
  570.     {
  571.         static::$current_action = $tag;
  572.  
  573.         array_push(static::$run_actions, $tag);
  574.  
  575.         if( ! isset(static::$actions[$tag]))
  576.         {
  577.             $this->debug("No actions found for tag {$tag}");
  578.  
  579.             return $args;
  580.         }
  581.  
  582.         ksort(static::$actions[$tag]);
  583.  
  584.         //die('<pre>' . print_r(static::$actions, TRUE));
  585.  
  586.         foreach(static::$actions[$tag] as $actions)
  587.         {
  588.             foreach($actions as $a)
  589.             {
  590.                 // Make sure the function or method exists
  591.                 if(is_array($a['function']))
  592.                 {
  593.                     // Methods are setup as an array, [0] is the object/class, [1] is the method
  594.                     if( ! method_exists($a['function'][0], $a['function'][1]))
  595.                     {
  596.                         $this->error("Unable to execute method '" . get_class($a['function'][0]) . "::{$a['function'][1]}' for action {$tag}, the method doesn't exist");
  597.  
  598.                         return $args;
  599.                     }
  600.                 }
  601.                 else
  602.                 {
  603.                     // Strings are just functions
  604.                     if( ! function_exists($a['function']))
  605.                     {
  606.                         $this->error("Unable to execute function '{$a['function']}' for action {$tag}, the function doesn't exist");
  607.  
  608.                         return $args;
  609.                     }
  610.                 }
  611.  
  612.                 // Actions
  613.                 if($a['type'] == 'action')
  614.                 {
  615.                     // No arguments/null
  616.                     if(is_null($args))
  617.                     {
  618.                         call_user_func( $a['function'] );
  619.                     }
  620.                     // Single argument
  621.                     else
  622.                     {
  623.                         call_user_func( $a['function'], $args );
  624.                     }
  625.                 }
  626.                 // Filters
  627.                 else
  628.                 {
  629.                     // No arguments/null
  630.                     if(is_null($args))
  631.                     {
  632.                         $args = call_user_func( $a['function'] );
  633.                     }
  634.                     else
  635.                     {
  636.                         $args = call_user_func( $a['function'], $args );
  637.                     }
  638.                 }
  639.             }
  640.         }
  641.  
  642.         static::$current_action = NULL;
  643.  
  644.         // Be polite, return it as you found it
  645.         settype($args, gettype($args));
  646.  
  647.         return $args;
  648.     }
  649.  
  650.     // ------------------------------------------------------------------------
  651.  
  652.     /**
  653.      * Remove Action
  654.      *
  655.      * Remove a function from an action
  656.      *
  657.      * @param string $tag Tag to check in
  658.      * @param mixed  $function Function to be removed
  659.      * @param int    $priority Priority to check for function
  660.      * @todo This was copied from the old POS plugins library, where the functions were strings, now they are arrays of objects/functions, so this needs to check for that Maybe it should check if $function is array, if so, iterate through $actions, if($a[0] instanceof $function[0] and $a[1] == $function[1], remove
  661.      * @access public
  662.      * @return bool
  663.      */
  664.     public function remove_action($tag, $function, $priority = 10)
  665.     {
  666.         if (isset(static::$actions[$tag][$priority][$function]))
  667.         {
  668.             // Remove the action hook from our hooks array
  669.             unset(static::$actions[$tag][$priority][$function]);
  670.         }
  671.  
  672.         return TRUE;
  673.  
  674.     }
  675.  
  676.     // ------------------------------------------------------------------------
  677.  
  678.     /**
  679.      * Current Action
  680.      *
  681.      * Set the currently running action
  682.      *
  683.      * @access public
  684.      * @return string
  685.      */
  686.     public function current_action()
  687.     {
  688.         return static::$current_action;
  689.     }
  690.  
  691.     // ------------------------------------------------------------------------
  692.  
  693.     /**
  694.      * Has Run
  695.      *
  696.      * See if a particular action has ran yet
  697.      *
  698.      * @param  string  $action  Action to check for
  699.      * @access public
  700.      * @return boolean
  701.      */
  702.     public function has_run($action)
  703.     {
  704.         if (isset(static::$run_actions[$action]))
  705.         {
  706.             return true;
  707.         }
  708.         else
  709.         {
  710.             return false;
  711.         }
  712.     }
  713.  
  714.     // ------------------------------------------------------------------------
  715.  
  716.     /**
  717.      * Update Plugin Headers
  718.      *
  719.      * Parse a given plugins PHP file for the header information in the comments, and update the database info
  720.      * accordingly
  721.      *
  722.      * @param   string  $plugin Plugin System Name to check
  723.      * @access  public
  724.      * @todo    Try to retrieve only the top X lines of the file, not the whole file
  725.      * @return  TRUE Always true
  726.      */
  727.     public function update_plugin_headers($plugin)
  728.     {
  729.         if (isset(static::$plugins[$plugin]))
  730.         {
  731.             $arr = array();
  732.  
  733.             $plugin_data = file_get_contents(static::$plugin_path.$plugin."/".$plugin.".php"); // Load the plugin we want
  734.  
  735.             preg_match ('|Plugin Name:(.*)$|mi', $plugin_data, $name);
  736.             preg_match ('|Plugin URI:(.*)$|mi', $plugin_data, $uri);
  737.             preg_match ('|Version:(.*)|i', $plugin_data, $version);
  738.             preg_match ('|Description:(.*)$|mi', $plugin_data, $description);
  739.             preg_match ('|Author:(.*)$|mi', $plugin_data, $author_name);
  740.             preg_match ('|Author URI:(.*)$|mi', $plugin_data, $author_uri);
  741.  
  742.             if (isset($name[1]))
  743.             {
  744.                 $arr['name'] = trim($name[1]);
  745.             }
  746.  
  747.             if (isset($uri[1]))
  748.             {
  749.                 $arr['uri'] = trim($uri[1]);
  750.             }
  751.  
  752.             if (isset($version[1]))
  753.             {
  754.                 $arr['version'] = trim($version[1]);
  755.             }
  756.  
  757.             if (isset($description[1]))
  758.             {
  759.                 $arr['description'] = trim($description[1]);
  760.             }
  761.  
  762.             if (isset($author_name[1]))
  763.             {
  764.                 $arr['author'] = trim($author_name[1]);
  765.             }
  766.  
  767.             if (isset($author_uri[1]))
  768.             {
  769.                 $arr['author_uri'] = trim($author_uri[1]);
  770.             }
  771.  
  772.             if(empty($arr))
  773.             {
  774.                 $this->warn("Skipping header update for {$plugin}, no headers matched");
  775.             }
  776.             elseif(self::$PM->update_plugin_info($plugin, $arr))
  777.             {
  778.                 $this->debug("Updated plugin headers for {$plugin}: " . serialize($arr));
  779.             }
  780.             else
  781.             {
  782.                 $this->error("Failed to update plugin headers for {$plugin}: " . serialize($arr));
  783.             }
  784.         }
  785.  
  786.         return TRUE;
  787.     }
  788.  
  789.     // ------------------------------------------------------------------------
  790.  
  791.     /**
  792.      * Update All Plugin Headers
  793.      *
  794.      * Execute self::update_plugin_headers for each plugin found in static::$plugins
  795.      *
  796.      * @access public
  797.      * @return boolean
  798.      */
  799.     public function update_all_plugin_headers()
  800.     {
  801.         if(empty(static::$plugins))
  802.         {
  803.             $this->warn("No plugins to update headers for");
  804.  
  805.             return TRUE;
  806.         }
  807.  
  808.         foreach(static::$plugins as $name => $plugin)
  809.         {
  810.             $this->debug("Updating plugin headers for {$name}");
  811.  
  812.             if( ! $this->update_plugin_headers($name))
  813.             {
  814.                 return FALSE;
  815.             }
  816.         }
  817.  
  818.         return TRUE;
  819.     }
  820.  
  821.     // ------------------------------------------------------------------------
  822.  
  823.     /**
  824.      * Doing Action
  825.      *
  826.      * If the param is NULL, then this will return what action is being executed,
  827.      * if an action is supplied, then it will return boolean based on if that action
  828.      * is being executed or not
  829.      *
  830.      * @param null $action  Action to check for
  831.      */
  832.     public function doing_action($action = NULL)
  833.     {
  834.         if(is_null($action))
  835.         {
  836.             return static::$current_action;
  837.         }
  838.         else
  839.         {
  840.             return $action === static::$current_action;
  841.         }
  842.     }
  843.  
  844.     // ------------------------------------------------------------------------
  845.  
  846.     /**
  847.      * Did Action
  848.      *
  849.      * Returns if a tag has been fired or not
  850.      *
  851.      * @param $tag
  852.      */
  853.     public function did_action($tag)
  854.     {
  855.         return in_array($tag, static::$run_actions);
  856.     }
  857.  
  858.     // ------------------------------------------------------------------------
  859.  
  860.     /**
  861.      * Get Class Usages
  862.      *
  863.      * Parse through the plugins .php code to check for any classes that it
  864.      * attempts to use, returns an array of class names
  865.      *
  866.      * @param string    $plugin     System name of plugin (NOT display name)
  867.      * @access public
  868.      * @return array
  869.      * @todo This needs to be finished and more solid/stable
  870.      */
  871.     public function get_class_usages($plugin)
  872.     {
  873.         // Get the base path of the plugin
  874.         $plugin_path = "/" . static::$plugin_path . "/{$plugin}/";
  875.  
  876.         // Should get re-defined once class_profiles.php loads
  877.         $classes = NULL;
  878.  
  879.         // An array of class names and their data
  880.         require_once APPPATH . 'libraries/class_profiles.php';
  881.  
  882.         //die('CLASSES:<pre>' . print_r($classes, TRUE));
  883.  
  884.         $files = array();
  885.  
  886.         if( ! $dir = @new RecursiveDirectoryIterator( realpath($plugin_path) ))
  887.         {
  888.             Logger_lib::error("Failed to read from plugin path: {$plugin_path}", TRUE, TRUE);
  889.  
  890.             return FALSE;
  891.         }
  892.  
  893.         $objects = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
  894.  
  895.         foreach($objects as $name => $object)
  896.         {
  897.             if(preg_match('/\.php$/', $name))
  898.             {
  899.                 array_push($files, $name);
  900.             }
  901.         }
  902.  
  903.         $matches = array();
  904.  
  905.         foreach($files as $f)
  906.         {
  907.             $line_num = 0;
  908.  
  909.             if( ! $fh = @fopen($f, 'r'))
  910.             {
  911.                 Logger_lib::error("Failed to open plugin file for parsing: {$f}", TRUE, TRUE);
  912.  
  913.                 return FALSE;
  914.             }
  915.  
  916.             $file_lines = array();
  917.  
  918.             while ( ! feof($fh))
  919.             {
  920.                 $line_num++;
  921.  
  922.                 $line = fgets($fh, 4096);
  923.  
  924.                 $file_lines[] = trim($line);
  925.  
  926.                 foreach($classes as $c => $d) // Classes is defined in class_profiles.php
  927.                 {
  928.                     if (preg_match('/' . $d['regex'] . '/', $line)) {
  929.                         $matches[$c][] = array(
  930.                             'file' => $f,
  931.                             'class' => $d['class'],
  932.                             'line_num' => $line_num,
  933.                             'line' => trim($line)
  934.                         );
  935.                     }
  936.                 }
  937.             }
  938.             fclose($fh);
  939.         }
  940.  
  941.         return $matches;
  942.     }
  943.  
  944.     // ------------------------------------------------------------------------
  945.  
  946.     /**
  947.      * Get Orphaned Plugins
  948.      *
  949.      * Look in the plugin directory for any folders that do not have an associated entry
  950.      * in the plugins table
  951.      *
  952.      * @access public
  953.      * @return array|bool   If no orphaned plugins are found, return false
  954.      */
  955.     public function get_orphaned_plugins()
  956.     {
  957.         $files = scandir(static::$plugin_path);
  958.  
  959.         $plugins = static::$PM->get_plugins();
  960.  
  961.         $orphaned = array();
  962.  
  963.         foreach($files as $f)
  964.         {
  965.             // Skip directories
  966.             if(in_array($f, ['.','..'])) continue;
  967.  
  968.             if( ! isset($plugins[$f]))
  969.             {
  970.                 array_push($orphaned, $f);
  971.             }
  972.         }
  973.  
  974.         return (count($orphaned) > 0 ? $orphaned : FALSE);
  975.     }
  976.  
  977.     // ------------------------------------------------------------------------
  978.  
  979.     /**
  980.      * Get Messages
  981.      *
  982.      * Get all messages, or a specific type of message
  983.      *
  984.      * @param   string  $type   Type of error to retrieve (error, debug, warn)
  985.      * @access  public
  986.      * @return  array|bool
  987.      */
  988.     public function get_messages($type = NULL)
  989.     {
  990.         if( ! $type)
  991.         {
  992.             return static::$messages;
  993.         }
  994.         elseif( ! isset(static::$messages[ strtolower($type) ]))
  995.         {
  996.             $this->error("Failed to retrieve error type '{$type}', no such type found. Use 'error', 'warn' or 'debug'");
  997.  
  998.             return FALSE;
  999.         }
  1000.  
  1001.         return static::$messages[strtolower($type)];
  1002.     }
  1003.  
  1004.     // ------------------------------------------------------------------------
  1005.  
  1006.     /**
  1007.      * Print Messages
  1008.      *
  1009.      * Print all messages, or messages of a certain type
  1010.      *
  1011.      * @param   string  $type   Type of error to display (error, debug, warn)
  1012.      * @access  public
  1013.      * @return  array|bool
  1014.      */
  1015.     public function print_messages($type = NULL)
  1016.     {
  1017.         if($type)
  1018.         {
  1019.             if(@empty(static::$messages[ strtolower($type) ]) || ! isset(static::$messages[ strtolower($type) ]))
  1020.             {
  1021.                 echo "{$type} IS EMPTY\n";
  1022.                 return TRUE;
  1023.             }
  1024.  
  1025.             echo "<h3>Plugin Messages - <strong>" . ucfirst($type) . "</strong></h3>\n";
  1026.  
  1027.             echo "<ol>\n";
  1028.  
  1029.             foreach(static::$messages[ strtolower($type) ] as $m)
  1030.             {
  1031.                 echo "<li>$m</li>\n";
  1032.             }
  1033.  
  1034.             echo "</ol>\n</hr>\n";
  1035.  
  1036.             return TRUE;
  1037.         }
  1038.  
  1039.         foreach(static::$messages as $type => $messages)
  1040.         {
  1041.             if(@empty($messages))
  1042.             {
  1043.                 echo "{$type} IS EMPTY\n";
  1044.                 continue;
  1045.             }
  1046.  
  1047.             echo "<h3>Plugin Messages - <strong>" . ucfirst($type) . "</strong></h3>\n";
  1048.  
  1049.             echo "<ol>\n";
  1050.  
  1051.             foreach($messages as $m)
  1052.             {
  1053.                 echo "<li>$m</li>\n";
  1054.             }
  1055.  
  1056.             echo "</ol>\n</hr>\n";
  1057.         }
  1058.  
  1059.         return TRUE;
  1060.     }
  1061. }
  1062.  
  1063. // ------------------------------------------------------------------------
  1064.  
  1065. /**
  1066.  * Shortcut to Plugins_lib::update_all_plugin_headers()
  1067.  *
  1068.  * Executes update_plugin_headers() for all plugins in database
  1069.  *
  1070.  * @return bool
  1071.  */
  1072. function update_all_plugin_headers()
  1073. {
  1074.     return Plugins_lib::$instance->update_all_plugin_headers();
  1075. }
  1076.  
  1077. // ------------------------------------------------------------------------
  1078.  
  1079. /**
  1080.  * Shortcut to Plugins_lib::update_plugin_headers()
  1081.  *
  1082.  * Updates the plugin headers for a specified plugin based on the plugins .php file comments
  1083.  *
  1084.  * @param   string  $plugin Plugin system name
  1085.  * @return  bool
  1086.  */
  1087. function update_plugin_headers($plugin)
  1088. {
  1089.     return Plugins_lib::$instance->update_plugin_headers($plugin);
  1090. }
  1091.  
  1092. // ------------------------------------------------------------------------
  1093.  
  1094. /**
  1095.  * Shortcut to Plugins_lib::install_plugin()
  1096.  *
  1097.  * Executes whatevers in the plugins install method
  1098.  *
  1099.  * @param   string  $plugin Plugin system name
  1100.  * @return  bool
  1101.  */
  1102. function install_plugin($plugin, $data = NULL)
  1103. {
  1104.     return Plugins_lib::$instance->install_plugin($plugin, $data);
  1105. }
  1106.  
  1107. // ------------------------------------------------------------------------
  1108.  
  1109. /**
  1110.  * Shortcut to Plugins_lib::enable_plugin()
  1111.  *
  1112.  * Enable a specified plugin by setting the database status value to 1
  1113.  *
  1114.  * @param   string  $plugin Plugin system name
  1115.  * @param   mixed   $data   Any data that should be handed down to the plugins activation method (optional)
  1116.  * @return  bool
  1117.  */
  1118. function enable_plugin($plugin, $data = NULL)
  1119. {
  1120.     return Plugins_lib::$instance->enable_plugin($plugin, $data);
  1121. }
  1122.  
  1123. // ------------------------------------------------------------------------
  1124.  
  1125. /**
  1126.  * Shortcut to Plugins_lib::disable_plugin()
  1127.  *
  1128.  * Disable a specified plugin by setting the database status value to 0
  1129.  *
  1130.  * @param   string  $plugin Plugin system name
  1131.  * @param   mixed   $data   Any data that should be handed down to the plugins deactivate method (optional)
  1132.  * @return  bool
  1133.  */
  1134. function disable_plugin($plugin, $data = NULL)
  1135. {
  1136.     return Plugins_lib::$instance->disable_plugin($plugin, $data);
  1137. }
  1138.  
  1139. // ------------------------------------------------------------------------
  1140.  
  1141. /**
  1142.  * Shortcut to Plugins_lib::plugin_details()
  1143.  *
  1144.  * Return the details of a plugin from the plugins database table
  1145.  *
  1146.  * @param   string  $plugin Plugin system name
  1147.  * @return  array
  1148.  */
  1149. function plugin_details($plugin)
  1150. {
  1151.     return Plugins_lib::$instance->plugin_details($plugin);
  1152. }
  1153.  
  1154. // ------------------------------------------------------------------------
  1155.  
  1156. /**
  1157.  * Shortcut to Plugins_lib::get_messages()
  1158.  *
  1159.  * Gets all the plugin messages thus far (errors, debug messages, warnings)
  1160.  *
  1161.  * @param   string  $type   Specific type to retrieve, if NULL, all are returned
  1162.  * @return  array
  1163.  */
  1164. function get_messages($type = NULL)
  1165. {
  1166.     return Plugins_lib::$instance->get_messages($type);
  1167. }
  1168.  
  1169. // ------------------------------------------------------------------------
  1170.  
  1171. /**
  1172.  * Shortcut to Plugins_lib::print_messages()
  1173.  *
  1174.  * Displays all the plugin messages thus far (errors, debug messages, warnings)
  1175.  *
  1176.  * @param   string  $type   Specific type to retrieve, if NULL, all are printed
  1177.  * @return  array
  1178.  */
  1179. function print_messages($type = NULL)
  1180. {
  1181.     return Plugins_lib::$instance->print_messages($type);
  1182. }
  1183.  
  1184. // ------------------------------------------------------------------------
  1185.  
  1186. /**
  1187.  * Shortcut to Plugins_lib::get_orphaned_plugins()
  1188.  *
  1189.  * See if there are any plugins in the plugins directory that arent in the database
  1190.  *
  1191.  * @return  array
  1192.  */
  1193. function get_orphaned_plugins()
  1194. {
  1195.     return Plugins_lib::$instance->get_orphaned_plugins();
  1196. }
  1197.  
  1198. // ------------------------------------------------------------------------
  1199.  
  1200. /**
  1201.  * Shortcut to Plugins_lib::add_action()
  1202.  *
  1203.  * Add an action - a function that will fire off when a tag/action is executed (NOT
  1204.  * the same as add_filter - which will return a value
  1205.  *
  1206.  * @param   string          $tag        Tag/Action thats being executed
  1207.  * @param   string|array    $function   Either a single function (string), or a class and method (array)
  1208.  * @param   int             $priority   Priority of this action
  1209.  * @return  boolean
  1210.  */
  1211. function add_action($tag, $function, $priority = 10)
  1212. {
  1213.     return Plugins_lib::$instance->add_action($tag, $function, $priority);
  1214. }
  1215.  
  1216. // ------------------------------------------------------------------------
  1217.  
  1218. /**
  1219.  * Shortcut to Plugins_lib::add_filter()
  1220.  *
  1221.  * Add a filter - a function that can be used to effect/parse/filter out some content (NOT
  1222.  * the same as add_action - which will just fire off a function
  1223.  *
  1224.  * @param   string          $tag        Tag/Action thats being executed
  1225.  * @param   string|array    $function   Either a single function (string), or a class and method (array)
  1226.  * @param   int             $priority   Priority of this action
  1227.  * @return  boolean
  1228.  */
  1229. function add_filter($tag, $function, $priority = 10)
  1230. {
  1231.     return Plugins_lib::$instance->add_filter($tag, $function, $priority);
  1232. }
  1233.  
  1234. // ------------------------------------------------------------------------
  1235.  
  1236. /**
  1237.  * Shortcut to Plugins_lib::get_actions()
  1238.  *
  1239.  * Retrieve all actions/filters that are assigned to actions/tags
  1240.  *
  1241.  * @return  array
  1242.  */
  1243. function get_actions()
  1244. {
  1245.     return Plugins_lib::$instance->get_actions();
  1246. }
  1247.  
  1248. // ------------------------------------------------------------------------
  1249.  
  1250. /**
  1251.  * Shortcut to Plugins_lib::retrieve_plugins()
  1252.  *
  1253.  * Retrieve all plugins
  1254.  *
  1255.  * @return  array
  1256.  */
  1257. function retrieve_plugins()
  1258. {
  1259.     return Plugins_lib::$instance->retrieve_plugins();
  1260. }
  1261.  
  1262. // ------------------------------------------------------------------------
  1263.  
  1264. /**
  1265.  * Shortcut to Plugins_lib::do_action()
  1266.  *
  1267.  * Execute all plugin functions tied to a specific tag
  1268.  *
  1269.  * @param   string  $tag    Tag to execute
  1270.  * @param   mixed   $args   Arguments to hand to plugin (Can be anything)
  1271.  * @return  mixed
  1272.  */
  1273. function do_action($tag, $args = NULL)
  1274. {
  1275.     return Plugins_lib::$instance->do_action($tag, $args);
  1276. }
  1277.  
  1278. // ------------------------------------------------------------------------
  1279.  
  1280. /**
  1281.  * Shortcut to Plugins_lib::remove_action()
  1282.  *
  1283.  * Remove a specific plugin function assigned to execute on a specific tag at a specific priority
  1284.  *
  1285.  * @param   string          $tag        Tag to clear actions from
  1286.  * @param   string|array    $function   Function or object/method to remove from tag
  1287.  * @param   int             $priority   Priority to clear
  1288.  * @return  boolean
  1289.  */
  1290. function remove_action($tag, $function, $priority = 10)
  1291. {
  1292.     return Plugins_lib::$instance->remove_action($tag, $function, $priority);
  1293. }
  1294.  
  1295. // ------------------------------------------------------------------------
  1296.  
  1297. /**
  1298.  * Shortcut to Plugins_lib::current_action()
  1299.  *
  1300.  * Get the current plugin action being executed
  1301.  *
  1302.  * @return  string
  1303.  */
  1304. function current_action()
  1305. {
  1306.     return Plugins_lib::$instance->current_action();
  1307. }
  1308.  
  1309. // ------------------------------------------------------------------------
  1310.  
  1311. /**
  1312.  * Shortcut to Plugins_lib::has_run()
  1313.  *
  1314.  * See if an action has run or not
  1315.  *
  1316.  * @param   string  $action     Tag/action to check
  1317.  * @return  boolean
  1318.  */
  1319. function has_run($action = NULL)
  1320. {
  1321.     return Plugins_lib::$instance->has_run($action);
  1322. }
  1323.  
  1324. // ------------------------------------------------------------------------
  1325.  
  1326. /**
  1327.  * Shortcut to Plugins_lib::doing_action()
  1328.  *
  1329.  * If no action is specified, then the current action being executed will be returned, if
  1330.  * an action is specified, then TRUE/FALSE will be returned based on if the action is
  1331.  * being executed or not
  1332.  *
  1333.  * @oaran   string  $action     Action to check
  1334.  * @return  boolean|string
  1335.  */
  1336. function doing_action($action = NULL)
  1337. {
  1338.     return Plugins_lib::$instance->doing_action($action);
  1339. }
  1340.  
  1341. // ------------------------------------------------------------------------
  1342.  
  1343. /**
  1344.  * Shortcut to Plugins_lib::did_action()
  1345.  *
  1346.  * Check if an action/tag has been executed or not
  1347.  *
  1348.  * @param   string  $tag    Tag/action to check
  1349.  * @return  boolean
  1350.  */
  1351. function did_action($tag)
  1352. {
  1353.     return Plugins_lib::$instance->did_action($tag);
  1354. }
Advertisement
Add Comment
Please, Sign In to add comment