1 <?php
2 /**
3  * Plugin Flickr: Embedding Flickr image
4  *
5  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6  * @author     Norbert Csík <norbert.csik@gmail.com>
7  * @version    0.2.0
8  */
9 
10 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
11 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12 require_once(DOKU_PLUGIN.'syntax.php');
13 
14 /**
15  * All DokuWiki plugins to extend the parser/rendering mechanism
16  * need to inherit from this class
17  */
18 class syntax_plugin_flickr extends DokuWiki_Syntax_Plugin {
19 
20     /**
21      * return some info
22      */
23     function getInfo(){
24         return array(
25             'author' => 'Norbert Csík',
26             'email'  => 'norbert.csik@gmail.com',
27             'date'   => '2006-06-06',
28             'name'   => 'Flickr Plugin',
29             'desc'   => 'Embedding Flickr image',
30             'url'    => 'http://www.csik.net/project:flickr',
31         );
32     }
33 
34     /**
35      * What kind of syntax are we?
36      */
37     function getType(){
38         return 'substition';
39     }
40 
41     /**
42      * What kind of syntax do we allow (optional)
43      */
44 //    function getAllowedTypes() {
45 //        return array();
46 //    }
47 
48     /**
49      * What about paragraphs? (optional)
50      */
51 //    function getPType(){
52 //        return 'normal';
53 //    }
54 
55     /**
56      * Where to sort in?
57      */
58     function getSort(){
59         return 299;
60     }
61 
62 
63     /**
64      * Connect pattern to lexer
65      */
66     function connectTo($mode) {
67       $this->Lexer->addSpecialPattern('\{\{flickr>.*?\}\}',$mode,'plugin_flickr');
68     }
69 
70     /**
71      * Handle the match
72      */
73     function handle($match, $state, $pos, &$handler){
74         switch ($state) {
75           case DOKU_LEXER_ENTER :
76             break;
77           case DOKU_LEXER_MATCHED :
78             break;
79           case DOKU_LEXER_UNMATCHED :
80             break;
81           case DOKU_LEXER_EXIT :
82             break;
83           case DOKU_LEXER_SPECIAL :
84             return $match;
85             break;
86         }
87         return array();
88     }
89 
90     /**
91      * Create output
92      */
93     function render($mode, &$renderer, $data) {
94         if($mode == 'xhtml'){
95         	global $conf;
96 
97 			// strip flickr> from start
98 			$data = "{{".substr($data,9,-2)."}}";
99 
100 			// get flickr image id
101 			$start = strrpos($data, "/")+1;
102 			$end = strpos($data, "_", $start);
103 			$imageid = substr($data, $start, $end-$start);
104 
105     		// parse the media az an external media
106     		$p = Doku_Handler_Parse_Media($data);
107 
108 			// render the media as a flickr external media
109 			$link = array();
110 			$link['class']  = 'media';
111 			$link['style']  = '';
112 			$link['pre']    = '';
113 			$link['suf']    = '';
114 			$link['more']   = '';
115 			$link['target'] = $conf['target']['media'];
116 
117 			$link['title']  = $p['title'];
118 			$link['url']    = "http://www.flickr.com/".$conf['plugin']['flickr']['accounturl']."/".$imageid."/";
119 			$link['name']   = $renderer->_media ($p['src'], $p['title'], $p['align'], $p['width'], $p['height'], $p['cache']);
120 
121 			list($ext,$mime) = mimetype($p['src']);
122 			if(substr($mime,0,5) == 'image'){
123 				// link only jpeg images
124 				// if ($ext != 'jpg' && $ext != 'jpeg') $noLink = TRUE;
125 			}elseif($mime == 'application/x-shockwave-flash'){
126 				// don't link flash movies
127 				$noLink = TRUE;
128 			}else{
129 				// add file icons
130 				$link['class'] .= ' mediafile mf_'.$ext;
131 			}
132 
133 			//output formatted
134 			$renderer->doc .= $renderer->_formatLink($link);
135 
136             return true;
137         }
138         return false;
139     }
140 }
141