Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
AppHelper.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * Helper class
11  */
12 class AppHelper
13 {
14 
15  /**
16  * Gets the id of a given content id
17  *
18  * @param String $contentId
19  * @return String ObjectId
20  */
21  public static function getObjectIdFromContentId($contentId = null) {
22 
23  if (!is_string($contentId) || AppHelper::isNullOrEmptyString($contentId)) {
24  return false;
25  }
26 
27  $pieces = explode(".", $contentId);
28 
29  if (sizeof($pieces) != 2) {
30  return false;
31  }
32 
33  return $pieces[1];
34  }
35 
36  /**
37  * checks if a given object is a content Id
38  *
39  * @param String $contentId
40  * @return boolean
41  */
42  public static function isContentId($contentId = null) {
43  return is_numeric(AppHelper::getObjectIdFromContentId($contentId));
44  }
45 
46  /**
47  * Checks if a string is null or empty
48  *
49  * @param string $question
50  * @return boolean
51  */
52  public static function isNullOrEmptyString($question = null) {
53  return (!isset($question) || (trim($question) === ''));
54  }
55 
56  public static function mysqlDate($timestamp = null)
57  {
58  return is_null($timestamp) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', $timestamp);
59  }
60 
61  public static function footprint($text)
62  {
63  $engToCyr = array(
64  'a' => 'а',
65  'c' => 'с',
66  'e' => 'е',
67  'o' => 'о',
68  'p' => 'р',
69  'y' => 'у',
70  'H' => 'Н',
71  'T' => 'Т',
72  );
73  $newText = '';
74  foreach(str_split($text) as $char)
75  {
76  if(isset($engToCyr[$char]) && rand() % 2 == 0){
77  $newText.=$engToCyr[$char];
78  }else{
79  $newText.=$char;
80  }
81  }
82  return $newText;
83  }
84 
85  public static function addParamsToUrl($url, $params)
86  {
87  if (($insertPos = strpos($url, '#')) === false) {
88  $insertPos = strlen($url);
89  }
90 
91  if (strpos($url, '?') === false) {
92  $url = substr_replace($url, '?', $insertPos, 0);
93  $insertPos++;
94  } else {
95  $url = substr_replace($url, '&', $insertPos, 0);
96  $insertPos++;
97  }
98  $url = substr_replace($url, http_build_query($params), $insertPos, 0);
99 
100  return $url;
101  }
102 
103  public static function removeParamsFromUrl($url, $params)
104  {
105  $parsed = parse_url($url);
106  parse_str($parsed['query'], $queryString);
107  foreach ($params as $param) {
108  unset($queryString[$param]);
109  }
110  $parsed['query'] = http_build_query($queryString);
111 
112  return http_build_url('', $parsed);
113  }
114 
115  /**
116  * Helper function
117  *
118  * @static
119  * @param $array
120  * @param $searchKey
121  * @param $foundedItems
122  */
123  public static function findItemsWithKey($array, $searchKey, &$foundedItems)
124  {
125  foreach ($array as $key => $item) {
126  if ($key == $searchKey) {
127  $foundedItems[] = $item;
128  }
129  if (is_array($item)) {
130  self::findItemsWithKey($item, $searchKey, $foundedItems);
131  }
132  }
133  }
134 }
135 
136 if (!function_exists('http_build_url')) {
137  define('HTTP_URL_REPLACE', 1); // Replace every part of the first URL when there's one of the second URL
138  define('HTTP_URL_JOIN_PATH', 2); // Join relative paths
139  define('HTTP_URL_JOIN_QUERY', 4); // Join query strings
140  define('HTTP_URL_STRIP_USER', 8); // Strip any user authentication information
141  define('HTTP_URL_STRIP_PASS', 16); // Strip any password authentication information
142  define('HTTP_URL_STRIP_AUTH', 32); // Strip any authentication information
143  define('HTTP_URL_STRIP_PORT', 64); // Strip explicit port numbers
144  define('HTTP_URL_STRIP_PATH', 128); // Strip complete path
145  define('HTTP_URL_STRIP_QUERY', 256); // Strip query string
146  define('HTTP_URL_STRIP_FRAGMENT', 512); // Strip any fragments (#identifier)
147  define('HTTP_URL_STRIP_ALL', 1024); // Strip anything but scheme and host
148 
149  // Build an URL
150  // The parts of the second URL will be merged into the first according to the flags argument.
151  //
152  // @param mixed (Part(s) of) an URL in form of a string or associative array like parse_url() returns
153  // @param mixed Same as the first argument
154  // @param int A bitmask of binary or'ed HTTP_URL constants (Optional)HTTP_URL_REPLACE is the default
155  // @param array If set, it will be filled with the parts of the composed url like parse_url() would return
156  function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = false)
157  {
158  $keys = array('user', 'pass', 'port', 'path', 'query', 'fragment');
159 
160  // HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs
161  if ($flags & HTTP_URL_STRIP_ALL) {
162  $flags |= HTTP_URL_STRIP_USER;
163  $flags |= HTTP_URL_STRIP_PASS;
164  $flags |= HTTP_URL_STRIP_PORT;
165  $flags |= HTTP_URL_STRIP_PATH;
166  $flags |= HTTP_URL_STRIP_QUERY;
167  $flags |= HTTP_URL_STRIP_FRAGMENT;
168  } // HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS
169  else if ($flags & HTTP_URL_STRIP_AUTH) {
170  $flags |= HTTP_URL_STRIP_USER;
171  $flags |= HTTP_URL_STRIP_PASS;
172  }
173 
174  // Parse the original URL
175  $parse_url = parse_url($url);
176 
177  // Scheme and Host are always replaced
178  if (isset($parts['scheme']))
179  $parse_url['scheme'] = $parts['scheme'];
180  if (isset($parts['host']))
181  $parse_url['host'] = $parts['host'];
182 
183  // (If applicable) Replace the original URL with it's new parts
184  if ($flags & HTTP_URL_REPLACE) {
185  foreach ($keys as $key) {
186  if (isset($parts[$key]))
187  $parse_url[$key] = $parts[$key];
188  }
189  } else {
190  // Join the original URL path with the new path
191  if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) {
192  if (isset($parse_url['path']))
193  $parse_url['path'] = rtrim(str_replace(basename($parse_url['path']), '', $parse_url['path']), '/') . '/' . ltrim($parts['path'], '/');
194  else
195  $parse_url['path'] = $parts['path'];
196  }
197 
198  // Join the original query string with the new query string
199  if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) {
200  if (isset($parse_url['query']))
201  $parse_url['query'] .= '&' . $parts['query'];
202  else
203  $parse_url['query'] = $parts['query'];
204  }
205  }
206 
207  // Strips all the applicable sections of the URL
208  // Note: Scheme and Host are never stripped
209  foreach ($keys as $key) {
210  if ($flags & (int)constant('HTTP_URL_STRIP_' . strtoupper($key)))
211  unset($parse_url[$key]);
212  }
213 
214 
215  $new_url = $parse_url;
216 
217  return
218  ((isset($parse_url['scheme'])) ? $parse_url['scheme'] . '://' : '')
219  . ((isset($parse_url['user'])) ? $parse_url['user'] . ((isset($parse_url['pass'])) ? ':' . $parse_url['pass'] : '') . '@' : '')
220  . ((isset($parse_url['host'])) ? $parse_url['host'] : '')
221  . ((isset($parse_url['port'])) ? ':' . $parse_url['port'] : '')
222  . ((isset($parse_url['path'])) ? $parse_url['path'] : '')
223  . ((isset($parse_url['query'])) ? '?' . $parse_url['query'] : '')
224  . ((isset($parse_url['fragment'])) ? '#' . $parse_url['fragment'] : '');
225  }
226 }