<?php
/**
 * DokuWiki Action Plugin MetaHeaders
 * 
 * @license     GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author      Stephen Flitman <sflitman@xenoscience.com>
 */
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');

/**
 * All DokuWiki plugins to extend the admin function
 * need to inherit from this class
 */
class action_plugin_scriptheader extends DokuWiki_Action_Plugin {

    public function getInfo() {
        return array(
                'author' => 'Stephen Flitman',
                'email' => 'sflitman@xenoscience.com',
                'date'   => @file_get_contents(DOKU_PLUGIN.'scriptheader/VERSION'),
                'name' => 'scriptheader',
                'desc' => 'Lets you add <script> headers to your wiki pages.',
                'url' => 'http://dokuwiki.org/plugin:scriptheader'
            );
    }

    public function register(Doku_Event_Handler $controller) {
        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'scriptheader');
    }

    /**
     * Modifies the script headers before their send to the browser.
     *
     * Stephen Flitman <sflitman@xenoscience.com>
     */
    public function scriptheader(&$event, $param) {
       global $ID;
       global $INFO;
       global $ACT;

       if ($ACT != 'show' || !page_exists($ID)) return;

       $headerconf = DOKU_CONF . 'scriptheader.conf';
       if (@file_exists($headerconf)) { 
          $scripts=confToHash($headerconf);
          $except=$scripts['except'];
          if ($except) {
             if (preg_match($except,$ID)) return;          
             unset($scripts['except']);
          }
          foreach ($scripts as $i => $script) {
             $event->data["script"][] = array (
                     "type" => "text/javascript",
                     "src" => $script,
                     "_data" => "",
             );
          }
       }
       return true;
   }
}

