1<?php
2/**
3* Plugin Google Adsense2: include ADS into wiki content.
4*
5* @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6* @author     Dirk Moeller <dirk@systemengineers.de>
7*/
8
9//--- Exported code
10include_once(DOKU_PLUGIN.'googleads/code.php');
11//--- Exported code
12
13// must be run within Dokuwiki
14if(!defined('DOKU_INC')) die();
15
16if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17require_once(DOKU_PLUGIN.'syntax.php');
18
19/**
20* All DokuWiki plugins to extend the parser/rendering mechanism
21* need to inherit from this class
22*/
23class syntax_plugin_googleads extends DokuWiki_Syntax_Plugin {
24	/**
25	* return some info
26	*/
27	function getInfo(){
28		return array(
29                        'author' => 'Dirk Moeller',
30                        'email'  => 'dirk@systemengineers.de',
31                        'date'   => '2007-08-14',
32                        'name'   => 'Google Adsense Plugin',
33                        'desc'   => 'Plugin to embed your Google Adsense code in your site. Based on code from Bernd Zeimetz and Terence J.',
34                        'url'    => 'http://www.systemengineers.de/plugins/googleads',
35                );
36	}
37	function getType(){ return 'substition'; }
38	function getPType(){ return 'block'; }
39	function getSort(){ return 160; }
40	function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{googleads>[^}]*\}\}',$mode,'plugin_googleads'); }
41
42	/**
43	* Handle the match
44	*/
45	function handle($match, $state, $pos, &$handler){
46		$match = substr($match,12,-2); // Strip markup
47		return array($state,explode('|',$match));
48
49//////////////////
50/*
51		switch ($state) {
52			case DOKU_LEXER_ENTER:
53			$data = $this->_getstyle(trim(substr($match, 4, -1)));
54			if (substr($match, -1) == '|') {
55				$this->title_mode = true;
56				return array('title_open',$data);
57			} else {
58				return array('box_open',$data);
59			}
60
61			case DOKU_LEXER_MATCHED:
62				if ($this->title_mode) {
63				$this->title_mode = false;
64				return array('box_open','');
65			} else {
66				return array('data', $match);
67			}
68
69			case DOKU_LEXER_UNMATCHED:
70				return array('data', $match);
71
72			case DOKU_LEXER_EXIT:
73				$data = trim(substr($match, 5, -1));
74				$title =  ($data && $data{0} == "|") ? substr($data,1) : '';
75
76				return array('box_close', $title);
77		}
78		return false;
79*/
80//////////////////
81
82	}
83
84	/**
85	* Create output
86	*/
87	function render($mode, &$renderer, $data) {
88		if($mode == 'xhtml'){
89
90                        // prevent caching (to ensure ACL conformity)
91                        // TODO sometimes: make more intelligent
92                        $renderer->info['cache'] = false;
93
94			list($state, $match) = $data;
95			list($id,$param1) = $match;
96
97			//$style = $this->_getstyle(trim($param1));
98			$style = gads_getstyle(trim($param1));
99
100			//$renderer->doc .= 'debug(id=' . $id . ' param1=' . $param1 . ')<br />';
101			//$renderer->doc .= '<br />';
102
103			$renderer->doc .= gads_showADS($id, $style);
104			return true;
105		}
106		return false;
107	}
108/*
109	function _getstyle($str) {
110		if (!strlen($str)) return array();
111
112		$styles = array();
113
114		$tokens = preg_split('/\s+/', $str, 9);                 // limit is defensive
115		foreach ($tokens as $token) {
116			if (preg_match('/^\d*\.?\d+(%|px|em|ex|pt|cm|mm|pi|in)$/', $token)) {
117				$styles['width'] = $token;
118				continue;
119			}
120
121		// restrict token (class names) characters to prevent any malicious data
122		if (preg_match('/[^A-Za-z0-9_-]/',$token)) continue;
123		$styles['class'] = (isset($styles['class']) ? $styles['class'].' ' : '').$token;
124		}
125
126		return $styles;
127	}
128*/
129}
130?>
131