~ubuntu-branches/ubuntu/maverick/mahara/maverick-updates

« back to all changes in this revision

Viewing changes to htdocs/lib/htmlpurifier/HTMLPurifier/URI.php

  • Committer: Bazaar Package Importer
  • Author(s): Nigel McNie
  • Date: 2008-04-29 11:15:39 UTC
  • Revision ID: james.westby@ubuntu.com-20080429111539-b28eqkagavaub2zr
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
require_once 'HTMLPurifier/URIParser.php';
 
4
require_once 'HTMLPurifier/URIFilter.php';
 
5
 
 
6
/**
 
7
 * HTML Purifier's internal representation of a URI
 
8
 */
 
9
class HTMLPurifier_URI
 
10
{
 
11
    
 
12
    public $scheme, $userinfo, $host, $port, $path, $query, $fragment;
 
13
    
 
14
    /**
 
15
     * @note Automatically normalizes scheme and port
 
16
     */
 
17
    public function __construct($scheme, $userinfo, $host, $port, $path, $query, $fragment) {
 
18
        $this->scheme = is_null($scheme) || ctype_lower($scheme) ? $scheme : strtolower($scheme);
 
19
        $this->userinfo = $userinfo;
 
20
        $this->host = $host;
 
21
        $this->port = is_null($port) ? $port : (int) $port;
 
22
        $this->path = $path;
 
23
        $this->query = $query;
 
24
        $this->fragment = $fragment;
 
25
    }
 
26
    
 
27
    /**
 
28
     * Retrieves a scheme object corresponding to the URI's scheme/default
 
29
     * @param $config Instance of HTMLPurifier_Config
 
30
     * @param $context Instance of HTMLPurifier_Context
 
31
     * @return Scheme object appropriate for validating this URI
 
32
     */
 
33
    public function getSchemeObj($config, $context) {
 
34
        $registry = HTMLPurifier_URISchemeRegistry::instance();
 
35
        if ($this->scheme !== null) {
 
36
            $scheme_obj = $registry->getScheme($this->scheme, $config, $context);
 
37
            if (!$scheme_obj) return false; // invalid scheme, clean it out
 
38
        } else {
 
39
            // no scheme: retrieve the default one
 
40
            $def = $config->getDefinition('URI');
 
41
            $scheme_obj = $registry->getScheme($def->defaultScheme, $config, $context);
 
42
            if (!$scheme_obj) {
 
43
                // something funky happened to the default scheme object
 
44
                trigger_error(
 
45
                    'Default scheme object "' . $def->defaultScheme . '" was not readable',
 
46
                    E_USER_WARNING
 
47
                );
 
48
                return false;
 
49
            }
 
50
        }
 
51
        return $scheme_obj;
 
52
    }
 
53
    
 
54
    /**
 
55
     * Generic validation method applicable for all schemes
 
56
     * @param $config Instance of HTMLPurifier_Config
 
57
     * @param $context Instance of HTMLPurifier_Context
 
58
     * @return True if validation/filtering succeeds, false if failure
 
59
     */
 
60
    public function validate($config, $context) {
 
61
        
 
62
        // validate host
 
63
        if (!is_null($this->host)) {
 
64
            $host_def = new HTMLPurifier_AttrDef_URI_Host();
 
65
            $this->host = $host_def->validate($this->host, $config, $context);
 
66
            if ($this->host === false) $this->host = null;
 
67
        }
 
68
        
 
69
        // validate port
 
70
        if (!is_null($this->port)) {
 
71
            if ($this->port < 1 || $this->port > 65535) $this->port = null;
 
72
        }
 
73
        
 
74
        // query and fragment are quite simple in terms of definition:
 
75
        // *( pchar / "/" / "?" ), so define their validation routines
 
76
        // when we start fixing percent encoding
 
77
        
 
78
        // path gets to be validated against a hodge-podge of rules depending
 
79
        // on the status of authority and scheme, but it's not that important,
 
80
        // esp. since it won't be applicable to everyone
 
81
        
 
82
        return true;
 
83
        
 
84
    }
 
85
    
 
86
    /**
 
87
     * Convert URI back to string
 
88
     * @return String URI appropriate for output
 
89
     */
 
90
    public function toString() {
 
91
        // reconstruct authority
 
92
        $authority = null;
 
93
        if (!is_null($this->host)) {
 
94
            $authority = '';
 
95
            if(!is_null($this->userinfo)) $authority .= $this->userinfo . '@';
 
96
            $authority .= $this->host;
 
97
            if(!is_null($this->port))     $authority .= ':' . $this->port;
 
98
        }
 
99
        
 
100
        // reconstruct the result
 
101
        $result = '';
 
102
        if (!is_null($this->scheme))    $result .= $this->scheme . ':';
 
103
        if (!is_null($authority))       $result .=  '//' . $authority;
 
104
        $result .= $this->path;
 
105
        if (!is_null($this->query))     $result .= '?' . $this->query;
 
106
        if (!is_null($this->fragment))  $result .= '#' . $this->fragment;
 
107
        
 
108
        return $result;
 
109
    }
 
110
    
 
111
    /**
 
112
     * Returns a copy of the URI object
 
113
     */
 
114
    public function copy() {
 
115
        return unserialize(serialize($this));
 
116
    }
 
117
    
 
118
}
 
119