C:\xampp\htdocs\landing\wp-content\plugins\amp\includes\sanitizers\class-amp-form-sanitizer.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
 * Class AMP_Form_Sanitizer.
 *
 * @package AMP
 * @since 0.7
 */

use AmpProject\DevMode;
use 
AmpProject\Dom\Document;

/**
 * Class AMP_Form_Sanitizer
 *
 * Strips and corrects attributes in forms.
 *
 * @since 0.7
 * @internal
 */
class AMP_Form_Sanitizer extends AMP_Base_Sanitizer {

    
/**
     * Tag.
     *
     * @var string HTML <form> tag to identify and process.
     *
     * @since 0.7
     */
    
public static $tag 'form';

    
/**
     * Sanitize the <form> elements from the HTML contained in this instance's Dom\Document.
     *
     * @link https://www.ampproject.org/docs/reference/components/amp-form
     * @since 0.7
     */
    
public function sanitize() {

        
/**
         * Node list.
         *
         * @var DOMNodeList $nodes
         */
        
$nodes     $this->dom->getElementsByTagNameself::$tag );
        
$num_nodes $nodes->length;

        if ( 
=== $num_nodes ) {
            return;
        }

        for ( 
$i $num_nodes 1$i >= 0$i-- ) {
            
$node $nodes->item$i );
            if ( ! 
$node instanceof DOMElement || DevMode::hasExemptionForNode$node ) ) {
                continue;
            }

            
// In HTML, the default method is 'get'.
            
$method 'get';
            if ( 
$node->getAttribute'method' ) ) {
                
$method strtolower$node->getAttribute'method' ) );
            } else {
                
$node->setAttribute'method'$method );
            }

            
$action_url $this->get_action_url$node->getAttribute'action' ) );

            
$xhr_action $node->getAttribute'action-xhr' );

            
// Make HTTP URLs protocol-less, since HTTPS is required for forms.
            
if ( 'http://' === strtolowersubstr$action_url0) ) ) {
                
$action_url substr$action_url);
            }

            
/*
             * According to the AMP spec:
             * For GET submissions, provide at least one of action or action-xhr.
             * This attribute is required for method=GET. For method=POST, the
             * action attribute is invalid, use action-xhr instead.
             */
            
if ( 'get' === $method ) {
                if ( 
$action_url !== $node->getAttribute'action' ) ) {
                    
$node->setAttribute'action'$action_url );
                }
            } elseif ( 
'post' === $method ) {
                
$node->removeAttribute'action' );
                if ( ! 
$xhr_action ) {
                    
// Record that action was converted to action-xhr.
                    
$action_url add_query_argAMP_HTTP::ACTION_XHR_CONVERTED_QUERY_VAR1$action_url );
                    if ( ! 
amp_is_canonical() ) {
                        
$action_url add_query_argamp_get_slug(), ''$action_url );
                    }
                    
$node->setAttribute'action-xhr'$action_url );
                    
// Append success/error handlers if not found.
                    
$this->ensure_response_message_elements$node );
                } elseif ( 
'http://' === substr$xhr_action0) ) {
                    
$node->setAttribute'action-xhr'substr$xhr_action) );
                }
            }

            
/*
             * The target "indicates where to display the form response after submitting the form.
             * The value must be _blank or _top". The _self and _parent values are treated
             * as synonymous with _top, and anything else is treated like _blank.
             */
            
$target $node->getAttribute'target' );
            if ( 
'_top' !== $target ) {
                if ( ! 
$target || in_array$target, [ '_self''_parent' ], true ) ) {
                    
$node->setAttribute'target''_top' );
                } elseif ( 
'_blank' !== $target ) {
                    
$node->setAttribute'target''_blank' );
                }
            }
        }
    }

    
/**
     * Get the action URL for the form element.
     *
     * @param string $action_url Action URL.
     * @return string Action URL.
     */
    
protected function get_action_url$action_url ) {
        
/*
         * In HTML, the default action is just the current URL that the page is served from.
         * The action "specifies a server endpoint to handle the form input. The value must be an
         * https URL and must not be a link to a CDN".
         */
        
if ( ! $action_url ) {
            return 
esc_url_raw'//' $_SERVER['HTTP_HOST'] . wp_unslash$_SERVER['REQUEST_URI'] ) );
        }

        
$parsed_url wp_parse_url$action_url );

        if (
            
// Ignore a malformed URL - it will be later sanitized.
            
false === $parsed_url
            
||
            
// Ignore HTTPS URLs, because there is nothing left to do.
            
( isset( $parsed_url['scheme'] ) && 'https' === $parsed_url['scheme'] )
            ||
            
// Ignore protocol-relative URLs, because there is also nothing left to do.
            
( ! isset( $parsed_url['scheme'] ) && isset( $parsed_url['host'] ) )
        ) {
            return 
$action_url;
        }

        
// Make URL protocol relative.
        
$parsed_url['scheme'] = '//';

        
// Set an empty path if none is defined but there is a host.
        
if ( ! isset( $parsed_url['path'] ) && isset( $parsed_url['host'] ) ) {
            
$parsed_url['path'] = '';
        }

        if ( ! isset( 
$parsed_url['host'] ) ) {
            
$parsed_url['host'] = $_SERVER['HTTP_HOST'];
        }

        if ( ! isset( 
$parsed_url['path'] ) ) {
            
// If there is action URL path, use the one from the request.
            
$parsed_url['path'] = trailingslashitwp_unslash$_SERVER['REQUEST_URI'] ) );
        } elseif ( 
'' !== $parsed_url['path'] && '/' !== $parsed_url['path'][0] ) {
            
// If the path is relative, append it to the current request path.
            
$parsed_url['path'] = trailingslashitwp_unslash$_SERVER['REQUEST_URI'] ) ) . trailingslashit$parsed_url['path'] );
        }

        
// Rebuild the URL.
        
$action_url $parsed_url['scheme'];
        if ( isset( 
$parsed_url['user'] ) ) {
            
$action_url .= $parsed_url['user'];
            if ( isset( 
$parsed_url['pass'] ) ) {
                
$action_url .= ':' $parsed_url['pass'];
            }
            
$action_url .= '@';
        }
        
$action_url .= $parsed_url['host'];
        if ( isset( 
$parsed_url['port'] ) ) {
            
$action_url .= ':' $parsed_url['port'];
        }
        
$action_url .= $parsed_url['path'];
        if ( isset( 
$parsed_url['query'] ) ) {
            
$action_url .= '?' $parsed_url['query'];
        }
        if ( isset( 
$parsed_url['fragment'] ) ) {
            
$action_url .= '#' $parsed_url['fragment'];
        }

        return 
esc_url_raw$action_url );
    }

    
/**
     * Ensure that the form has a submit-success and submit-error element templates.
     *
     * @link https://www.ampproject.org/docs/reference/components/amp-form#success/error-response-rendering
     * @since 1.2
     *
     * @param DOMElement $form The form node to check.
     */
    
public function ensure_response_message_elements$form ) {
        
$elements = [
            
'submit-error'   => null,
            
'submit-success' => null,
            
'submitting'     => null,
        ];

        
$templates $this->dom->xpath->queryDocument::XPATH_MUSTACHE_TEMPLATE_ELEMENTS_QUERY$form );
        foreach ( 
$templates as $template ) {
            
$parent $template->parentNode;
            if ( 
$parent instanceof DOMElement ) {
                foreach ( 
array_keys$elements ) as $attribute ) {
                    if ( 
$parent->hasAttribute$attribute ) ) {
                        
$elements$attribute ] = $parent;
                    }
                }
            }
        }

        foreach ( 
$elements as $attribute => $element ) {
            if ( 
$element ) {
                continue;
            }
            
$div      $this->dom->createElement'div' );
            
$template $this->dom->createElement'template' );
            
$div->setAttribute'class''amp-wp-default-form-message' );
            if ( 
'submitting' === $attribute ) {
                
$p $this->dom->createElement'p' );
                
$p->appendChild$this->dom->createTextNode__'Submitting…''amp' ) ) );
                
$template->appendChild$p );
            } else {
                
$p $this->dom->createElement'p' );
                
$p->setAttribute'class''{{#redirecting}}amp-wp-form-redirecting{{/redirecting}}' );
                
$p->appendChild$this->dom->createTextNode'{{#message}}{{{message}}}{{/message}}' ) );

                
// Show generic message for HTTP success/failure.
                
$p->appendChild$this->dom->createTextNode'{{^message}}' ) );
                if ( 
'submit-error' === $attribute ) {
                    
$p->appendChild$this->dom->createTextNode__'Your submission failed.''amp' ) ) );
                    
/* translators: %1$s: HTTP status text, %2$s: HTTP status code */
                    
$reason sprintf__'The server responded with %1$s (code %2$s).''amp' ), '{{status_text}}''{{status_code}}' );
                } else {
                    
$p->appendChild$this->dom->createTextNode__'It appears your submission was successful.''amp' ) ) );
                    
$reason __'Even though the server responded OK, it is possible the submission was not processed.''amp' );
                }
                
$reason .= ' ' __'Please contact the developer of this form processor to improve this message.''amp' );

                
$p->appendChild$this->dom->createTextNode' ' ) );
                
$small $this->dom->createElement'small' );
                
$small->appendChild$this->dom->createTextNode$reason ) );
                
$small->appendChild$this->dom->createTextNode' ' ) );
                
$link $this->dom->createElement'a' );
                
$link->setAttribute'href''https://amp-wp.org/?p=5463' );
                
$link->setAttribute'target''_blank' );
                
$link->appendChild$this->dom->createTextNode__'Learn More''amp' ) ) );
                
$small->appendChild$link );
                
$p->appendChild$small );

                
$p->appendChild$this->dom->createTextNode'{{/message}}' ) );
                
$template->appendChild$p );
            }
            
$div->setAttribute$attribute'' );
            
$template->setAttribute'type''amp-mustache' );
            
$div->appendChild$template );
            
$form->appendChild$div );
        }
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586