Mixin_Url_Manipulation

The NextGEN Gallery Mixin Url Manipulation class.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/router/package.module.router.php  
  1. class Mixin_Url_Manipulation extends Mixin 
  2. public function join_paths() 
  3. $args = func_get_args(); 
  4. $parts = $this->_flatten_array($args); 
  5. foreach ($parts as &$part) { 
  6. $part = trim(str_replace('\\', '/', $part), '/'); 
  7. return implode('/', $parts); 
  8. /** 
  9. * Removes a segment from a url 
  10. * @param string $segment 
  11. * @param string $url 
  12. * @return string 
  13. */ 
  14. public function remove_url_segment($segment, $url) 
  15. $retval = $url; 
  16. $parts = parse_url($url); 
  17. // If the url has a path, then we can remove a segment 
  18. if (isset($parts['path']) && $segment != '/') { 
  19. if (substr($segment, -1) == '/') { 
  20. $segment = substr($segment, -1); 
  21. $segment = preg_quote($segment, '#'); 
  22. if (preg_match("#{$segment}#", $parts['path'], $matches)) { 
  23. $parts['path'] = str_replace('//', '/', str_replace($matches[0], '', $parts['path'])); 
  24. $retval = $this->object->construct_url_from_parts($parts); 
  25. return $retval; 
  26. /** 
  27. * Flattens an array of arrays to a single array 
  28. * @param array $array 
  29. * @param array $parent (optional) 
  30. * @param bool $exclude_duplicates (optional - defaults to TRUE) 
  31. * @return array 
  32. */ 
  33. public function _flatten_array($array, $parent = NULL, $exclude_duplicates = TRUE) 
  34. if (is_array($array)) { 
  35. // We're to add each element to the parent array 
  36. if ($parent) { 
  37. foreach ($array as $index => $element) { 
  38. foreach ($this->_flatten_array($array) as $sub_element) { 
  39. if ($exclude_duplicates) { 
  40. if (!in_array($sub_element, $parent)) { 
  41. $parent[] = $sub_element; 
  42. } else { 
  43. $parent[] = $sub_element; 
  44. $array = $parent; 
  45. } else { 
  46. $index = 0; 
  47. while (isset($array[$index])) { 
  48. $element = $array[$index]; 
  49. if (is_array($element)) { 
  50. $array = $this->_flatten_array($element, $array); 
  51. unset($array[$index]); 
  52. $index += 1; 
  53. $array = array_values($array); 
  54. } else { 
  55. $array = array($array); 
  56. return $array; 
  57. public function join_querystrings() 
  58. $parts = array(); 
  59. $retval = array(); 
  60. $params = func_get_args(); 
  61. $parts = $this->_flatten_array($params); 
  62. foreach ($parts as $part) { 
  63. $part = explode('&', $part); 
  64. foreach ($part as $segment) { 
  65. $segment = explode('=', $segment); 
  66. $key = $segment[0]; 
  67. $value = isset($segment[1]) ? $segment[1] : ''; 
  68. $retval[$key] = $value; 
  69. return $this->object->assoc_array_to_querystring($retval); 
  70. public function assoc_array_to_querystring($arr) 
  71. $retval = array(); 
  72. foreach ($arr as $key => $val) { 
  73. if (strlen($key)) { 
  74. $retval[] = strlen($val) ? "{$key}={$val}" : $key; 
  75. return implode('&', $retval); 
  76. /** 
  77. * Constructs a url from individual parts, created by parse_url 
  78. * @param array $parts 
  79. * @return string 
  80. */ 
  81. public function construct_url_from_parts($parts) 
  82. // let relative paths be relative, and full paths full 
  83. $prefix = ''; 
  84. if (!empty($parts['scheme']) && !empty($parts['host'])) { 
  85. $prefix = $parts['scheme'] . '://' . $parts['host']; 
  86. if (!empty($parts['port'])) { 
  87. $prefix .= ':' . $parts['port']; 
  88. $retval = $this->object->join_paths($prefix, isset($parts['path']) ? str_replace('//', '/', trailingslashit($parts['path'])) : ''); 
  89. if (isset($parts['query']) && $parts['query']) { 
  90. $retval .= untrailingslashit("?{$parts['query']}"); 
  91. return $retval; 
  92. public function get_parameter_segments($request_uri) 
  93. return str_replace($this->strip_param_segments($request_uri), '', $request_uri); 
  94. /** 
  95. * Returns the request uri with the parameter segments stripped 
  96. * @param string $request_uri 
  97. * @return string 
  98. */ 
  99. public function strip_param_segments($request_uri, $remove_slug = TRUE) 
  100. $retval = $request_uri ? $request_uri : '/'; 
  101. $settings = C_NextGen_Settings::get_instance(); 
  102. $sep = preg_quote($settings->router_param_separator, '#'); 
  103. $param_regex = "#((?P<id>\\w+) {$sep})?(?<key>\\w+) {$sep}(?P<value>.+)/?\$#"; 
  104. $slug = $settings->router_param_slug && $remove_slug ? '/' . preg_quote($settings->router_param_slug, '#') : ''; 
  105. $slug_regex = '#' . $slug . '/?$#'; 
  106. // Remove all parameters 
  107. while (@preg_match($param_regex, $retval, $matches)) { 
  108. $match_regex = '#' . preg_quote(array_shift($matches), '#') . '$#'; 
  109. $retval = preg_replace($match_regex, '', $retval); 
  110. // Remove the slug or trailing slash 
  111. if (@preg_match($slug_regex, $retval, $matches)) { 
  112. $match_regex = '#' . preg_quote(array_shift($matches), '#') . '$#'; 
  113. $retval = preg_replace($match_regex, '', $retval); 
  114. // If there's a slug, we can assume everything after is a parameter,  
  115. // even if it's not in our desired format. 
  116. $retval = preg_replace('#' . $slug . '.*$#', '', $retval); 
  117. if (!$retval) { 
  118. $retval = '/'; 
  119. return $retval;