aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/arrayparser.php
blob: c4ad1264fbb6dbd03297cef78d31915acdf47829 (plain)
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
<?php

/**
 * @author Robin Appelman
 * @copyright 2013 Robin Appelman icewind@owncloud.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OC;

class SyntaxException extends \Exception {
}

class ArrayParser {
	const TYPE_NUM = 1;
	const TYPE_BOOL = 2;
	const TYPE_STRING = 3;
	const TYPE_ARRAY = 4;

	/**
	 * @param string $string
	 */
	function parsePHP($string) {
		$string = $this->stripPHPTags($string);
		$string = $this->stripAssignAndReturn($string);
		return $this->parse($string);
	}

	function stripPHPTags($string) {
		$string = trim($string);
		if (substr($string, 0, 5) === '<?php') {
			$string = substr($string, 5);
		}
		if (substr($string, -2) === '?>') {
			$string = substr($string, 0, -2);
		}
		return $string;
	}

	/**
	 * @param string $string
	 */
	function stripAssignAndReturn($string) {
		$string = trim($string);
		if (substr($string, 0, 6) === 'return') {
			$string = substr($string, 6);
		}
		if (substr($string, 0, 1) === '$') {
			list(, $string) = explode('=', $string, 2);
		}
		return $string;
	}

	function parse($string) {
		$string = trim($string);
		$string = trim($string, ';');
		switch ($this->getType($string)) {
			case self::TYPE_NUM:
				return $this->parseNum($string);
			case self::TYPE_BOOL:
				return $this->parseBool($string);
			case self::TYPE_STRING:
				return $this->parseString($string);
			case self::TYPE_ARRAY:
				return $this->parseArray($string);
		}
		return null;
	}

	/**
	 * @param string $string
	 */
	function getType($string) {
		$string = strtolower($string);
		$first = substr($string, 0, 1);
		$last = substr($string, -1, 1);
		$arrayFirst = substr($string, 0, 5);
		if (($first === '"' or $first === "'") and ($last === '"' or $last === "'")) {
			return self::TYPE_STRING;
		} elseif ($string === 'false' or $string === 'true') {
			return self::TYPE_BOOL;
		} elseif ($arrayFirst === 'array' and $last === ')') {
			return self::TYPE_ARRAY;
		} else {
			return self::TYPE_NUM;
		}
	}

	/**
	 * @param string $string
	 */
	function parseString($string) {
		return substr($string, 1, -1);
	}

	/**
	 * @param string $string
	 */
	function parseNum($string) {
		return intval($string);
	}

	/**
	 * @param string $string
	 */
	function parseBool($string) {
		$string = strtolower($string);
		return $string === 'true';
	}

	/**
	 * @param string $string
	 */
	function parseArray($string) {
		$body = substr($string, 5);
		$body = trim($body);
		$body = substr($body, 1, -1);
		$items = $this->splitArray($body);
		$result = array();
		$lastKey = -1;
		foreach ($items as $item) {
			$item = trim($item);
			if ($item) {
				if (strpos($item, '=>')) {
					list($key, $value) = explode('=>', $item, 2);
					$key = $this->parse($key);
					$value = $this->parse($value);
				} else {
					$key = ++$lastKey;
					$value = $item;
				}

				if (is_numeric($key)) {
					$lastKey = $key;
				}
				$result[$key] = $value;
			}
		}
		return $result;
	}

	/**
	 * @param string $body
	 */
	function splitArray($body) {
		$inSingleQuote = false;//keep track if we are inside quotes
		$inDoubleQuote = false;
		$bracketDepth = 0;//keep track if we are inside brackets
		$parts = array();
		$start = 0;
		$escaped = false;//keep track if we are after an escape character
		$skips = array();//keep track of the escape characters we need to remove from the result
		if (substr($body, -1, 1) !== ',') {
			$body .= ',';
		}
		for ($i = 0; $i < strlen($body); $i++) {
			$char = substr($body, $i, 1);
			if ($char === '\\') {
				if ($escaped) {
					array_unshift($skips, $i - 1);
				}
				$escaped = !$escaped;
			} else {
				if ($char === '"' and !$inSingleQuote) {
					if ($escaped) {
						array_unshift($skips, $i - 1);
					} else {
						$inDoubleQuote = !$inDoubleQuote;
					}
				} elseif ($char === "'" and !$inDoubleQuote) {
					if ($escaped) {
						array_unshift($skips, $i - 1);
					} else {
						$inSingleQuote = !$inSingleQuote;
					}
				} elseif (!$inDoubleQuote and !$inSingleQuote) {
					if ($char === '(') {
						$bracketDepth++;
					} elseif ($char === ')') {
						if ($bracketDepth <= 0) {
							throw new SyntaxException;
						} else {
							$bracketDepth--;
						}
					} elseif ($bracketDepth === 0 and $char === ',') {
						$part = substr($body, $start, $i - $start);
						foreach ($skips as $skip) {
							$part = substr($part, 0, $skip - $start) . substr($part, $skip - $start + 1);
						}
						$parts[] = $part;
						$start = $i + 1;
						$skips = array();
					}
				}
				$escaped = false;
			}
		}
		return $parts;
	}
}
; msgstr "" #: templates/admin.php:187 msgid "" "Please connect to this ownCloud instance via HTTPS to enable or disable the " "SSL enforcement." msgstr "" #: templates/admin.php:197 msgid "Log" msgstr "" #: templates/admin.php:198 msgid "Log level" msgstr "" #: templates/admin.php:229 msgid "More" msgstr "" #: templates/admin.php:230 msgid "Less" msgstr "" #: templates/admin.php:236 templates/personal.php:116 msgid "Version" msgstr "" #: templates/admin.php:240 templates/personal.php:119 msgid "" "Developed by the <a href=\"http://ownCloud.org/contact\" " "target=\"_blank\">ownCloud community</a>, the <a " "href=\"https://github.com/owncloud\" target=\"_blank\">source code</a> is " "licensed under the <a href=\"http://www.gnu.org/licenses/agpl-3.0.html\" " "target=\"_blank\"><abbr title=\"Affero General Public " "License\">AGPL</abbr></a>." msgstr "" #: templates/apps.php:13 msgid "Add your App" msgstr "" #: templates/apps.php:28 msgid "More Apps" msgstr "" #: templates/apps.php:33 msgid "Select an App" msgstr "" #: templates/apps.php:39 msgid "See application page at apps.owncloud.com" msgstr "" #: templates/apps.php:41 msgid "<span class=\"licence\"></span>-licensed by <span class=\"author\"></span>" msgstr "" #: templates/apps.php:43 msgid "Update" msgstr "" #: templates/help.php:4 msgid "User Documentation" msgstr "" #: templates/help.php:6 msgid "Administrator Documentation" msgstr "" #: templates/help.php:9 msgid "Online Documentation" msgstr "" #: templates/help.php:11 msgid "Forum" msgstr "" #: templates/help.php:14 msgid "Bugtracker" msgstr "" #: templates/help.php:17 msgid "Commercial Support" msgstr "" #: templates/personal.php:10 msgid "Get the apps to sync your files" msgstr "" #: templates/personal.php:21 msgid "Show First Run Wizard again" msgstr "" #: templates/personal.php:29 #, php-format msgid "You have used <strong>%s</strong> of the available <strong>%s</strong>" msgstr "" #: templates/personal.php:41 templates/users.php:23 templates/users.php:86 msgid "Password" msgstr "" #: templates/personal.php:42 msgid "Your password was changed" msgstr "" #: templates/personal.php:43 msgid "Unable to change your password" msgstr "" #: templates/personal.php:44 msgid "Current password" msgstr "" #: templates/personal.php:46 msgid "New password" msgstr "" #: templates/personal.php:48 msgid "Change password" msgstr "" #: templates/personal.php:60 templates/users.php:85 msgid "Display Name" msgstr "" #: templates/personal.php:75 msgid "Email" msgstr "" #: templates/personal.php:77 msgid "Your email address" msgstr "" #: templates/personal.php:78 msgid "Fill in an email address to enable password recovery" msgstr "" #: templates/personal.php:87 templates/personal.php:88 msgid "Language" msgstr "" #: templates/personal.php:100 msgid "Help translate" msgstr "" #: templates/personal.php:106 msgid "WebDAV" msgstr "" #: templates/personal.php:108 msgid "Use this address to connect to your ownCloud in your file manager" msgstr "" #: templates/users.php:21 msgid "Login Name" msgstr "" #: templates/users.php:30 msgid "Create" msgstr "" #: templates/users.php:36 msgid "Admin Recovery Password" msgstr "" #: templates/users.php:37 templates/users.php:38 msgid "" "Enter the recovery password in order to recover the users files during " "password change" msgstr "" #: templates/users.php:42 msgid "Default Storage" msgstr "" #: templates/users.php:48 templates/users.php:142 msgid "Unlimited" msgstr "" #: templates/users.php:66 templates/users.php:157 msgid "Other" msgstr "Այլ" #: templates/users.php:84 msgid "Username" msgstr "" #: templates/users.php:91 msgid "Storage" msgstr "" #: templates/users.php:102 msgid "change display name" msgstr "" #: templates/users.php:106 msgid "set new password" msgstr "" #: templates/users.php:137 msgid "Default" msgstr ""