1<?php
2
3/**
4*
5* Parses for image placement.
6*
7* @category Text
8*
9* @package Text_Wiki
10*
11* @author Paul M. Jones <pmjones@php.net>
12*
13* @license LGPL
14*
15* @version $Id$
16*
17*/
18
19/**
20*
21* Parses for image placement.
22*
23* @category Text
24*
25* @package Text_Wiki
26*
27* @author Paul M. Jones <pmjones@php.net>
28*
29*/
30
31class Text_Wiki_Parse_Image extends Text_Wiki_Parse {
32
33    /**
34     * URL schemes recognized by this rule.
35     *
36     * @access public
37     * @var array
38    */
39    var $conf = array(
40        'schemes' => 'http|https|ftp|gopher|news',
41        'host_regexp' => '(?:[^.\s/"\'<\\\#delim#\ca-\cz]+\.)*[a-z](?:[-a-z0-9]*[a-z0-9])?\.?',
42        'path_regexp' => '(?:/[^\s"<\\\#delim#\ca-\cz]*)?'
43    );
44
45    /**
46    *
47    * The regular expression used to find source text matching this
48    * rule.
49    *
50    * @access public
51    *
52    * @var string
53    *
54    */
55
56    var $regex = '/(\[\[image\s+)(.+?)(\]\])/i';
57
58
59    /**
60     * The regular expressions used to check ecternal urls
61     *
62     * @access public
63     * @var string
64     * @see parse()
65     */
66    var $url = '';
67
68     /**
69     * Constructor.
70     * We override the constructor to build up the url regex from config
71     *
72     * @param object &$obj the base conversion handler
73     * @return The parser object
74     * @access public
75     */
76    function __construct(&$obj)
77    {
78        $default = $this->conf;
79        parent::__construct($obj);
80
81        // convert the list of recognized schemes to a regex OR,
82        $schemes = $this->getConf('schemes', $default['schemes']);
83        $this->url = str_replace( '#delim#', $this->wiki->delim,
84           '#(?:' . (is_array($schemes) ? implode('|', $schemes) : $schemes) . ')://'
85           . $this->getConf('host_regexp', $default['host_regexp'])
86           . $this->getConf('path_regexp', $default['path_regexp']) .'#');
87    }
88
89    /**
90    *
91    * Generates a token entry for the matched text.  Token options are:
92    *
93    * 'src' => The image source, typically a relative path name.
94    *
95    * 'opts' => Any macro options following the source.
96    *
97    * @access public
98    *
99    * @param array &$matches The array of matches from parse().
100    *
101    * @return A delimited token number to be used as a placeholder in
102    * the source text.
103    *
104    */
105
106    function process(&$matches)
107    {
108        $pos = strpos($matches[2], ' ');
109
110        if ($pos === false) {
111            $options = array(
112                'src' => $matches[2],
113                'attr' => array());
114        } else {
115            // everything after the space is attribute arguments
116            $options = array(
117                'src' => substr($matches[2], 0, $pos),
118                'attr' => $this->getAttrs(substr($matches[2], $pos+1))
119            );
120            // check the scheme case of external link
121            if (array_key_exists('link', $options['attr'])) {
122                // external url ?
123                if (($pos = strpos($options['attr']['link'], '://')) !== false) {
124                    if (!preg_match($this->url, $options['attr']['link'])) {
125                        return $matches[0];
126                    }
127                } elseif (in_array('Wikilink', $this->wiki->disable)) {
128                        return $matches[0]; // Wikilink disabled
129                }
130            }
131        }
132
133        return $this->wiki->addToken($this->rule, $options);
134    }
135}
136?>
137