Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
AppHelper.php
1 <?php
2 /**
3  *
4  */
5 class AppHelper
6 {
7  public static function mysqlDate($timestamp = null)
8  {
9  return is_null($timestamp) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', $timestamp);
10  }
11 
12  public static function footprint($text)
13  {
14  $engToCyr = array(
15  'a' => 'а',
16  'c' => 'с',
17  'e' => 'е',
18  'o' => 'о',
19  'p' => 'р',
20  'y' => 'у',
21  'H' => 'Н',
22  'T' => 'Т',
23  );
24  $newText = '';
25  foreach(str_split($text) as $char)
26  {
27  if(isset($engToCyr[$char]) && rand() % 2 == 0){
28  $newText.=$engToCyr[$char];
29  }else{
30  $newText.=$char;
31  }
32  }
33  return $newText;
34  }
35 
36  public static function addParamsToUrl($url, $params)
37  {
38  if (($insertPos = strpos($url, '#')) === false) {
39  $insertPos = strlen($url);
40  }
41 
42  if (strpos($url, '?') === false) {
43  $url = substr_replace($url, '?', $insertPos, 0);
44  $insertPos++;
45  } else {
46  $url = substr_replace($url, '&', $insertPos, 0);
47  $insertPos++;
48  }
49  $url = substr_replace($url, http_build_query($params), $insertPos, 0);
50 
51  return $url;
52  }
53 
54  public static function removeParamsFromUrl($url, $params)
55  {
56  $parsed = parse_url($url);
57  parse_str($parsed['query'], $queryString);
58  foreach ($params as $param) {
59  unset($queryString[$param]);
60  }
61  $parsed['query'] = http_build_query($queryString);
62 
63  return http_build_url('', $parsed);
64  }
65 
66  /**
67  * Helper function
68  *
69  * @static
70  * @param $array
71  * @param $searchKey
72  * @param $foundedItems
73  */
74  public static function findItemsWithKey($array, $searchKey, &$foundedItems)
75  {
76  foreach ($array as $key => $item) {
77  if ($key == $searchKey) {
78  $foundedItems[] = $item;
79  }
80  if (is_array($item)) {
81  self::findItemsWithKey($item, $searchKey, $foundedItems);
82  }
83  }
84  }
85 }
86 
87 if (!function_exists('http_build_url')) {
88  define('HTTP_URL_REPLACE', 1); // Replace every part of the first URL when there's one of the second URL
89  define('HTTP_URL_JOIN_PATH', 2); // Join relative paths
90  define('HTTP_URL_JOIN_QUERY', 4); // Join query strings
91  define('HTTP_URL_STRIP_USER', 8); // Strip any user authentication information
92  define('HTTP_URL_STRIP_PASS', 16); // Strip any password authentication information
93  define('HTTP_URL_STRIP_AUTH', 32); // Strip any authentication information
94  define('HTTP_URL_STRIP_PORT', 64); // Strip explicit port numbers
95  define('HTTP_URL_STRIP_PATH', 128); // Strip complete path
96  define('HTTP_URL_STRIP_QUERY', 256); // Strip query string
97  define('HTTP_URL_STRIP_FRAGMENT', 512); // Strip any fragments (#identifier)
98  define('HTTP_URL_STRIP_ALL', 1024); // Strip anything but scheme and host
99 
100  // Build an URL
101  // The parts of the second URL will be merged into the first according to the flags argument.
102  //
103  // @param mixed (Part(s) of) an URL in form of a string or associative array like parse_url() returns
104  // @param mixed Same as the first argument
105  // @param int A bitmask of binary or'ed HTTP_URL constants (Optional)HTTP_URL_REPLACE is the default
106  // @param array If set, it will be filled with the parts of the composed url like parse_url() would return
107  function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = false)
108  {
109  $keys = array('user', 'pass', 'port', 'path', 'query', 'fragment');
110 
111  // HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs
112  if ($flags & HTTP_URL_STRIP_ALL) {
113  $flags |= HTTP_URL_STRIP_USER;
114  $flags |= HTTP_URL_STRIP_PASS;
115  $flags |= HTTP_URL_STRIP_PORT;
116  $flags |= HTTP_URL_STRIP_PATH;
117  $flags |= HTTP_URL_STRIP_QUERY;
118  $flags |= HTTP_URL_STRIP_FRAGMENT;
119  } // HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS
120  else if ($flags & HTTP_URL_STRIP_AUTH) {
121  $flags |= HTTP_URL_STRIP_USER;
122  $flags |= HTTP_URL_STRIP_PASS;
123  }
124 
125  // Parse the original URL
126  $parse_url = parse_url($url);
127 
128  // Scheme and Host are always replaced
129  if (isset($parts['scheme']))
130  $parse_url['scheme'] = $parts['scheme'];
131  if (isset($parts['host']))
132  $parse_url['host'] = $parts['host'];
133 
134  // (If applicable) Replace the original URL with it's new parts
135  if ($flags & HTTP_URL_REPLACE) {
136  foreach ($keys as $key) {
137  if (isset($parts[$key]))
138  $parse_url[$key] = $parts[$key];
139  }
140  } else {
141  // Join the original URL path with the new path
142  if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) {
143  if (isset($parse_url['path']))
144  $parse_url['path'] = rtrim(str_replace(basename($parse_url['path']), '', $parse_url['path']), '/') . '/' . ltrim($parts['path'], '/');
145  else
146  $parse_url['path'] = $parts['path'];
147  }
148 
149  // Join the original query string with the new query string
150  if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) {
151  if (isset($parse_url['query']))
152  $parse_url['query'] .= '&' . $parts['query'];
153  else
154  $parse_url['query'] = $parts['query'];
155  }
156  }
157 
158  // Strips all the applicable sections of the URL
159  // Note: Scheme and Host are never stripped
160  foreach ($keys as $key) {
161  if ($flags & (int)constant('HTTP_URL_STRIP_' . strtoupper($key)))
162  unset($parse_url[$key]);
163  }
164 
165 
166  $new_url = $parse_url;
167 
168  return
169  ((isset($parse_url['scheme'])) ? $parse_url['scheme'] . '://' : '')
170  . ((isset($parse_url['user'])) ? $parse_url['user'] . ((isset($parse_url['pass'])) ? ':' . $parse_url['pass'] : '') . '@' : '')
171  . ((isset($parse_url['host'])) ? $parse_url['host'] : '')
172  . ((isset($parse_url['port'])) ? ':' . $parse_url['port'] : '')
173  . ((isset($parse_url['path'])) ? $parse_url['path'] : '')
174  . ((isset($parse_url['query'])) ? '?' . $parse_url['query'] : '')
175  . ((isset($parse_url['fragment'])) ? '#' . $parse_url['fragment'] : '');
176  }
177 }