1<?php
2/**
3 * Plugin Now: Inserts a timestamp.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Christoph Lang <calbity@gmx.de>
7 */
8
9// based on http://wiki.splitbrain.org/plugin:tutorial
10
11// must be run within Dokuwiki
12if (!defined('DOKU_INC')) die();
13
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
15require_once(DOKU_PLUGIN . 'syntax.php');
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_webthumbs extends DokuWiki_Syntax_Plugin {
22
23   function getInfo() {
24        return array(
25        'author'  => 'Christoph Lang',
26        'email'   => 'calbity@gmx.de',
27        'date'    => '2011-02-21',
28        'name'    => 'Webthumbnails Plugin',
29        'desc'    => 'Shows Thumbnails of Websites.',
30        'url'     => 'https://www.dokuwiki.org/plugin:webthumbs'
31        );
32    }
33
34    function connectTo($mode) {
35        $this->Lexer->addSpecialPattern('\<webthumb\:.*?\>', $mode, 'plugin_webthumbs');
36    }
37
38    function getType() { return 'substition'; }
39
40    function getSort() { return 267; }
41
42    function handle($match, $state, $pos, &$handler) {
43
44			$match = substr($match,10,-1);
45			list($url,$link) = explode('|',$match,2);
46			$url  = trim($url);
47			$link = trim($link);
48			if(!$link)
49				$link = $url;
50
51			return array($url,$link);
52
53    }
54
55    function validateURL($url) {
56    	return filter_var($url, FILTER_VALIDATE_URL);
57    }
58
59    function _preview($data) {
60
61			foreach($data as $url){
62				if(!$this->validateURL($url)){
63					$sError = str_replace("$1",htmlentities($url),$this->getLang('error'));
64					return $sError;
65				}
66			}
67
68			$type = $this->getConf('url');
69			switch($type){
70				case 3: $sWebService = "http://www.artviper.net/screenshots/screener.php?sdx=1024&sdy=768&w=120&h=80&q=100&url="; break;
71				case 2: $sWebService = "http://images.websnapr.com/?size=s&nocache=81&url="; break;
72				case 1: $sWebService = "http://www.thumbshots.de/cgi-bin/show.cgi?url="; break;
73				case 0:
74				default: $sWebService = "http://open.thumbshots.org/image.pxf?url="; break;
75			}
76			$image = array(
77  									'src'   => $sWebService.rawurlencode($data[0]).'&.png',
78  									'cache' => 'nocache',
79  									);
80
81			return array($data[1],$image);
82
83    }
84
85    function render($mode, &$renderer, $data) {
86
87      if ($mode == 'xhtml') {
88      		$response = $this->_preview($data);
89      		if(is_string($response)){
90	      		$renderer->doc .= $response;
91	      	}else{
92
93						list($link,$image) = $response;
94
95	      		$renderer->externallink($link,$image);
96
97	          return true;
98	        }
99      }
100      return false;
101    }
102}
103