PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 7.13
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v7.13
7.13 7.12 trunk 1.1 2.1.1 5.9 6.0 6.1 6.10 6.11 6.12 6.12.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9 7.0 7.0.1 7.1 7.10 All 34 releases
wp-database-backup / includes / admin / Destination / Google / google-api-php-client / src / external / URITemplateParser.php

URITemplateParser.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 7.13, at includes/admin/Destination/Google/google-api-php-client/src/external/URITemplateParser.php

211 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile -- third party library
3 /*
4 Copyright (c) 2010 Kevin M Burns Jr, http://kevburnsjr.com/
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * A URI Template Parser which is used by the apiREST class to resolve the REST requests
28 * Blogpost: http://lab.kevburnsjr.com/php-uri-template-parser
29 * Source: http://github.com/KevBurnsJr/php-uri-template-parser
30 */
31 class URI_Template_Parser {
32
33 public static $operators = array('+', ';', '?', '/', '.');
34 public static $reserved_operators = array('|', '!', '@');
35 public static $explode_modifiers = array('+', '*');
36 public static $partial_modifiers = array(':', '^');
37
38 public static $gen_delims = array(':', '/', '?', '#', '[', ']', '@');
39 public static $gen_delims_pct = array('%3A', '%2F', '%3F', '%23', '%5B', '%5D', '%40');
40 public static $sub_delims = array('!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=');
41 public static $sub_delims_pct = array('%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D');
42 public static $reserved;
43 public static $reserved_pct;
44
45 public function __construct($template) {
46 self::$reserved = array_merge(self::$gen_delims, self::$sub_delims);
47 self::$reserved_pct = array_merge(self::$gen_delims_pct, self::$sub_delims_pct);
48 $this->template = $template;
49 }
50
51 public function expand($data) {
52 // Modification to make this a bit more performant (since gettype is very slow)
53 if (! is_array($data)) {
54 $data = (array)$data;
55 }
56 /*
57 // Original code, which uses a slow gettype() statement, kept in place for if the assumption that is_array always works here is incorrect
58 switch (gettype($data)) {
59 case "boolean":
60 case "integer":
61 case "double":
62 case "string":
63 case "object":
64 $data = (array)$data;
65 break;
66 }
67 */
68
69 // Resolve template vars
70 preg_match_all('/\{([^\}]*)\}/', $this->template, $em);
71
72 foreach ($em[1] as $i => $bare_expression) {
73 preg_match('/^([\+\;\?\/\.]{1})?(.*)$/', $bare_expression, $lm);
74 $exp = new StdClass();
75 $exp->expression = $em[0][$i];
76 $exp->operator = $lm[1];
77 $exp->variable_list = $lm[2];
78 $exp->varspecs = explode(',', $exp->variable_list);
79 $exp->vars = array();
80 foreach ($exp->varspecs as $varspec) {
81 preg_match('/^([a-zA-Z0-9_]+)([\*\+]{1})?([\:\^][0-9-]+)?(\=[^,]+)?$/', $varspec, $vm);
82 $var = new StdClass();
83 $var->name = $vm[1];
84 $var->modifier = isset($vm[2]) && $vm[2] ? $vm[2] : null;
85 $var->modifier = isset($vm[3]) && $vm[3] ? $vm[3] : $var->modifier;
86 $var->default = isset($vm[4]) ? substr($vm[4], 1) : null;
87 $exp->vars[] = $var;
88 }
89
90 // Add processing flags
91 $exp->reserved = false;
92 $exp->prefix = '';
93 $exp->delimiter = ',';
94 switch ($exp->operator) {
95 case '+':
96 $exp->reserved = 'true';
97 break;
98 case ';':
99 $exp->prefix = ';';
100 $exp->delimiter = ';';
101 break;
102 case '?':
103 $exp->prefix = '?';
104 $exp->delimiter = '&';
105 break;
106 case '/':
107 $exp->prefix = '/';
108 $exp->delimiter = '/';
109 break;
110 case '.':
111 $exp->prefix = '.';
112 $exp->delimiter = '.';
113 break;
114 }
115 $expressions[] = $exp;
116 }
117
118 // Expansion
119 $this->expansion = $this->template;
120
121 foreach ($expressions as $exp) {
122 $part = $exp->prefix;
123 $exp->one_var_defined = false;
124 foreach ($exp->vars as $var) {
125 $val = '';
126 if ($exp->one_var_defined && isset($data[$var->name])) {
127 $part .= $exp->delimiter;
128 }
129 // Variable present
130 if (isset($data[$var->name])) {
131 $exp->one_var_defined = true;
132 $var->data = $data[$var->name];
133
134 $val = self::val_from_var($var, $exp);
135
136 // Variable missing
137 } else {
138 if ($var->default) {
139 $exp->one_var_defined = true;
140 $val = $var->default;
141 }
142 }
143 $part .= $val;
144 }
145 if (! $exp->one_var_defined) $part = '';
146 $this->expansion = str_replace($exp->expression, $part, $this->expansion);
147 }
148
149 return $this->expansion;
150 }
151
152 private function val_from_var($var, $exp) {
153 $val = '';
154 if (is_array($var->data)) {
155 $i = 0;
156 if ($exp->operator == '?' && ! $var->modifier) {
157 $val .= $var->name . '=';
158 }
159 foreach ($var->data as $k => $v) {
160 $del = $var->modifier ? $exp->delimiter : ',';
161 $ek = rawurlencode($k);
162 $ev = rawurlencode($v);
163
164 // Array
165 if ($k !== $i) {
166 if ($var->modifier == '+') {
167 $val .= $var->name . '.';
168 }
169 if ($exp->operator == '?' && $var->modifier || $exp->operator == ';' && $var->modifier == '*' || $exp->operator == ';' && $var->modifier == '+') {
170 $val .= $ek . '=';
171 } else {
172 $val .= $ek . $del;
173 }
174
175 // List
176 } else {
177 if ($var->modifier == '+') {
178 if ($exp->operator == ';' && $var->modifier == '*' || $exp->operator == ';' && $var->modifier == '+' || $exp->operator == '?' && $var->modifier == '+') {
179 $val .= $var->name . '=';
180 } else {
181 $val .= $var->name . '.';
182 }
183 }
184 }
185 $val .= $ev . $del;
186 $i ++;
187 }
188 $val = trim($val, $del);
189
190 // Strings, numbers, etc.
191 } else {
192 if ($exp->operator == '?') {
193 $val = $var->name . (isset($var->data) ? '=' : '');
194 } else if ($exp->operator == ';') {
195 $val = $var->name . ($var->data ? '=' : '');
196 }
197 $val .= rawurlencode($var->data);
198 if ($exp->operator == '+') {
199 $val = str_replace(self::$reserved_pct, self::$reserved, $val);
200 }
201 }
202 return $val;
203 }
204
205 public function match($uri) {}
206
207 public function __toString() {
208 return $this->template;
209 }
210 }
211