/usr/share/owncloud/apps/mozilla_sync/lib/urlparser.php

https://gitlab.com/thiagotalma/stalag13 · PHP · 218 lines · 87 code · 37 blank · 94 comment · 15 complexity · ca9197b834670189e68c4703e38e2c5e MD5 · raw file

  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michal Jaskurzynski
  6. * @author Oliver Gasser
  7. * @copyright 2012 Michal Jaskurzynski mjaskurzynski@gmail.com
  8. *
  9. */
  10. namespace OCA\mozilla_sync;
  11. /**
  12. * Class for parsing Mozilla URL Semantics
  13. *
  14. * For example:
  15. * <version>/<username>/<further instruction>
  16. *
  17. */
  18. class UrlParser {
  19. /**
  20. * Constructor, parse given url.
  21. *
  22. * @param string $url Mozilla storage URL, for example /1.0/username/storage/history
  23. */
  24. public function __construct($url) {
  25. // Parser is valid at the begining
  26. $this->parseValidFlag = true;
  27. // Parse URL
  28. $components = parse_url($url);
  29. // For seriously malformed URLs false is returned
  30. if ($components === false) {
  31. $this->parseValidFlag = false;
  32. return;
  33. }
  34. // Get URL path
  35. $url = $components["path"];
  36. // Remove '/' from beginning and end
  37. $url = trim($url, '/');
  38. $urlArray = explode('/', $url);
  39. // There should be at least 2 arguments: version, username
  40. if (count($urlArray) < 2) {
  41. $this->parseValidFlag = false;
  42. Utils::writeLog("URL: Found only " . count($urlArray) . " arguments, but need at least 2 in URL "
  43. . Utils::getSyncUrl() . ": " . var_export($urlArray, true));
  44. return;
  45. }
  46. // Parse version
  47. $this->version = array_shift($urlArray);
  48. // Ignore CAPTCHA request
  49. if ($this->version === 'misc') {
  50. $this->parseValidFlag = false;
  51. return;
  52. } else if (($this->version != '1.0') &&
  53. ($this->version != '1.1') &&
  54. ($this->version != '2.0')) {
  55. $this->parseValidFlag = false;
  56. Utils::writeLog("URL: Illegal version " . $this->version . " found.");
  57. return;
  58. }
  59. // Parse sync hash
  60. $this->syncHash = array_shift($urlArray);
  61. // Parse commands
  62. $this->commandsArray = $urlArray;
  63. // Get URL params (everything after the '?')
  64. if (isset($components["query"])) {
  65. $params = $components["query"];
  66. $params = trim($params, '&');
  67. $this->params = explode('&', $params);
  68. } else {
  69. $this->params = null;
  70. }
  71. }
  72. /**
  73. * @brief Verifies whether the URL is valid.
  74. *
  75. * @return bool True if URL is valid, false otherwise.
  76. */
  77. public function isValid() {
  78. return $this->parseValidFlag;
  79. }
  80. /**
  81. * @brief Return version of the service requested in the URL.
  82. *
  83. * @return string Version of the service requested in the URL.
  84. */
  85. public function getVersion() {
  86. return $this->version;
  87. }
  88. /**
  89. * @brief Return Mozilla Sync user hash from the URL.
  90. *
  91. * @return string Sync hash from the URL.
  92. */
  93. public function getSyncHash() {
  94. return $this->syncHash;
  95. }
  96. /**
  97. * @brief Return command by number, starting from 0.
  98. *
  99. * @param integer $commandNumber The number of the command that will be
  100. * returned, starting from 0.
  101. * @return string The command at the requested index.
  102. */
  103. public function getCommand($commandNumber) {
  104. return $this->commandsArray[$commandNumber];
  105. }
  106. /**
  107. * @brief Return modifiers array, i.e. URL parameters.
  108. *
  109. * Example:
  110. * tabs?full=1&ids=1,2,3
  111. *
  112. * @return array Modifiers for the corresponding command.
  113. */
  114. public function getCommandModifiers() {
  115. $resultArray = array();
  116. // Return an empty array for no parameters
  117. if (is_null($this->params)) {
  118. return $resultArray;
  119. }
  120. // Iterate over all URL params
  121. foreach ($this->params as $value) {
  122. $tmpArray = explode('=', $value);
  123. if (count($tmpArray) != 2) {
  124. continue;
  125. }
  126. $key = $tmpArray[0];
  127. // Split argument list, important for IDs
  128. if (strpos($tmpArray[1], ',') === false) {
  129. $value = $tmpArray[1];
  130. } else {
  131. $value = explode(',', $tmpArray[1]);
  132. }
  133. $resultArray[$key] = $value;
  134. }
  135. return $resultArray;
  136. }
  137. /**
  138. * @brief Return command array.
  139. *
  140. * @return array Commands in URL.
  141. */
  142. public function getCommands() {
  143. return $this->commandsArray;
  144. }
  145. /**
  146. * @brief Return number of commands.
  147. *
  148. * @return integer Number of commands in URL.
  149. */
  150. public function commandCount() {
  151. return count($this->commandsArray);
  152. }
  153. /**
  154. * @brief Check if command string matches given pattern.
  155. *
  156. * @param string $pattern Pattern to mach command string against.
  157. * @return boolean True if command string matches the pattern, false
  158. * otherwise.
  159. */
  160. public function commandMatch($pattern) {
  161. $commandString = implode('/', $this->commandsArray);
  162. return preg_match($pattern, $commandString);
  163. }
  164. /**
  165. * Flag for checking parsing result
  166. */
  167. private $parseValidFlag;
  168. /**
  169. * Mozilla storage API version
  170. */
  171. private $version;
  172. /**
  173. * Mozilla Sync user hash
  174. */
  175. private $syncHash;
  176. /**
  177. * Further commands array
  178. */
  179. private $commandsArray;
  180. }
  181. /* vim: set ts=4 sw=4 tw=80 noet : */