PageRenderTime 21ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/google-maps-easy/functions.php

https://gitlab.com/pankajmohale/chef2go
PHP | 316 lines | 250 code | 1 blank | 65 comment | 58 complexity | 9bccfe2842ec598580b95c371e10f4d1 MD5 | raw file
  1. <?php
  2. /**
  3. * Set first leter in a string as UPPERCASE
  4. * @param string $str string to modify
  5. * @return string string with first Uppercase letter
  6. */
  7. if(!function_exists('strFirstUp')) {
  8. function strFirstUp($str) {
  9. return strtoupper(substr($str, 0, 1)).strtolower(substr($str, 1, strlen($str)));
  10. }
  11. }
  12. /**
  13. * Deprecated - class must be created
  14. */
  15. if(!function_exists('dateToTimestampGmp')) {
  16. function dateToTimestampGmp($date) {
  17. if(empty($a))
  18. return false;
  19. $a = explode(GMP_DATE_DL, $date);
  20. return mktime(0, 0, 0, $a[1], $a[0], $a[2]);
  21. }
  22. }
  23. /**
  24. * Generate random string name
  25. * @param int $lenFrom min len
  26. * @param int $lenTo max len
  27. * @return string random string with length from $lenFrom to $lenTo
  28. */
  29. if(!function_exists('getRandName')) {
  30. function getRandName($lenFrom = 6, $lenTo = 9) {
  31. $res = '';
  32. $len = mt_rand($lenFrom, $lenTo);
  33. if($len) {
  34. for($i = 0; $i < $len; $i++) {
  35. $res .= chr(mt_rand(97, 122)); /*rand symbol from a to z*/
  36. }
  37. }
  38. return $res;
  39. }
  40. }
  41. if(!function_exists('importGmp')) {
  42. function importGmp($path) {
  43. if(file_exists($path)) {
  44. require($path);
  45. return true;
  46. }
  47. return false;
  48. }
  49. }
  50. if(!function_exists('setDefaultParams')) {
  51. function setDefaultParams($params, $default) {
  52. foreach($default as $k => $v) {
  53. $params[$k] = isset($params[$k]) ? $params[$k] : $default[$k];
  54. }
  55. return $params;
  56. }
  57. }
  58. if(!function_exists('importClassGmp')) {
  59. function importClassGmp($class, $path = '') {
  60. if(!class_exists($class)) {
  61. if(!$path) {
  62. $classFile = $class;
  63. if(strpos(strtolower($classFile), GMP_CODE) !== false) {
  64. $classFile = preg_replace('/'. GMP_CODE. '/i', '', $classFile);
  65. }
  66. $path = GMP_CLASSES_DIR. $classFile. '.php';
  67. }
  68. return importGmp($path);
  69. } else { //If such class already exist - let's check does this is our plugin class or someone else
  70. /*if(class_exists('ReflectionClass')) { //ReflectionClass supported begining from php5
  71. $reflection = new ReflectionClass($class);
  72. $classFile = $reflection->getFileName();
  73. if(strpos($classFile, GMP_DIR) === false) { //Class is not in our plugin directory
  74. $conflictWith = substr($classFile, strpos($classFile, 'plugins') + strlen('plugins'. DS));
  75. $conflictWith = substr($conflictWith, 0, strpos($conflictWith, DS));
  76. $plugins = get_option('active_plugins');
  77. if(!empty($plugins)) {
  78. for($i = 0; $i < count($plugins); $i++) {
  79. if(strpos($plugins[$i], GMP_PLUG_NAME) !== false) { //Let's remove our plugin from list of active plugins
  80. unset($plugins[$i]);
  81. }
  82. }
  83. update_option( 'active_plugins', $plugins );
  84. }
  85. exit('Sorry, but we have conflict with class name <b style="color: red;">'. $class. '</b> in one of your already installed plugins <b style="color: red;">'. $conflictWith. '</b> located at '. $classFile. '. This means that you can not have both two plugins at one time.');
  86. }
  87. }*/
  88. }
  89. return false;
  90. }
  91. }
  92. /**
  93. * Check if class name exist with prefix or not
  94. * @param strin $class preferred class name
  95. * @return string existing class name
  96. */
  97. if(!function_exists('toeGetClassNameGmp')) {
  98. function toeGetClassNameGmp($class) {
  99. $className = '';
  100. if(class_exists($class. strFirstUp(GMP_CODE)))
  101. $className = $class. strFirstUp(GMP_CODE);
  102. else if(class_exists(GMP_CLASS_PREFIX. $class))
  103. $className = GMP_CLASS_PREFIX. $class;
  104. else
  105. $className = $class;
  106. return $className;
  107. }
  108. }
  109. /**
  110. * Create object of specified class
  111. * @param string $class class that you want to create
  112. * @param array $params array of arguments for class __construct function
  113. * @return object new object of specified class
  114. */
  115. if(!function_exists('toeCreateObjGmp')) {
  116. function toeCreateObjGmp($class, $params) {
  117. $className = toeGetClassNameGmp($class);
  118. $obj = NULL;
  119. if(class_exists('ReflectionClass')) {
  120. $reflection = new ReflectionClass($className);
  121. try {
  122. $obj = $reflection->newInstanceArgs($params);
  123. } catch (ReflectionException $e) { // If class have no constructor
  124. $obj = $reflection->newInstanceArgs();
  125. }
  126. } else {
  127. $obj = new $className();
  128. call_user_func_array(array($obj, '__construct'), $params);
  129. }
  130. return $obj;
  131. }
  132. }
  133. /**
  134. * Redirect user to specified location. Be advised that it should redirect even if headers alredy sent.
  135. * @param string $url where page must be redirected
  136. */
  137. if(!function_exists('redirectGmp')) {
  138. function redirectGmp($url) {
  139. if(headers_sent()) {
  140. echo '<script type="text/javascript"> document.location.href = "'. $url. '"; </script>';
  141. } else {
  142. header('Location: '. $url);
  143. }
  144. exit();
  145. }
  146. }
  147. if(!function_exists('in_array_array')) {
  148. function in_array_array($needle, $haystack) {
  149. if(is_array($needle)) {
  150. foreach($needle as $n) {
  151. if(in_array($n, $haystack))
  152. return true;
  153. }
  154. return false;
  155. } else
  156. return in_array($needle, $haystack);
  157. }
  158. }
  159. if(!function_exists('json_encode_utf_normal')) {
  160. function json_encode_utf_normal($value) {
  161. if (is_int($value)) {
  162. return (string)$value;
  163. } elseif (is_string($value)) {
  164. $value = str_replace(array('\\', '/', '"', "\r", "\n", "\b", "\f", "\t"),
  165. array('\\\\', '\/', '\"', '\r', '\n', '\b', '\f', '\t'), $value);
  166. $convmap = array(0x80, 0xFFFF, 0, 0xFFFF);
  167. $result = "";
  168. for ($i = strlen($value) - 1; $i >= 0; $i--) {
  169. $mb_char = substr($value, $i, 1);
  170. $result = $mb_char . $result;
  171. }
  172. return '"' . $result . '"';
  173. } elseif (is_float($value)) {
  174. return str_replace(",", ".", $value);
  175. } elseif (is_null($value)) {
  176. return 'null';
  177. } elseif (is_bool($value)) {
  178. return $value ? 'true' : 'false';
  179. } elseif (is_array($value)) {
  180. $with_keys = false;
  181. $n = count($value);
  182. for ($i = 0, reset($value); $i < $n; $i++, next($value)) {
  183. if (key($value) !== $i) {
  184. $with_keys = true;
  185. break;
  186. }
  187. }
  188. } elseif (is_object($value)) {
  189. $with_keys = true;
  190. } else {
  191. return '';
  192. }
  193. $result = array();
  194. if ($with_keys) {
  195. foreach ($value as $key => $v) {
  196. $result[] = json_encode_utf_normal((string)$key) . ':' . json_encode_utf_normal($v);
  197. }
  198. return '{' . implode(',', $result) . '}';
  199. } else {
  200. foreach ($value as $key => $v) {
  201. $result[] = json_encode_utf_normal($v);
  202. }
  203. return '[' . implode(',', $result) . ']';
  204. }
  205. }
  206. }
  207. /**
  208. * Prepares the params values to store into db
  209. *
  210. * @param array $d $_POST array
  211. * @return array
  212. */
  213. if(!function_exists('prepareParamsGmp')) {
  214. function prepareParamsGmp(&$d=array(), &$options = array()) {
  215. if (!empty($d['params'])) {
  216. if (isset($d['params']['options'])) {
  217. $options = $d['params']['options'];
  218. //unset($d['params']['options']);
  219. }
  220. if (is_array($d['params'])) {
  221. $params = utilsGmp::jsonEncode($d['params']);
  222. $params = str_replace(array('\n\r', "\n\r", '\n', "\r", '\r', "\r"), '<br />', $params);
  223. $params = str_replace(array('<br /><br />', '<br /><br /><br />'), '<br />', $params);
  224. $d['params'] = $params;
  225. }
  226. } elseif(isset($d['params'])) {
  227. $d['params']['attr']['class'] = '';
  228. $d['params']['attr']['id'] = '';
  229. $params = utilsGmp::jsonEncode($d['params']);
  230. $d['params'] = $params;
  231. }
  232. if(empty($options))
  233. $options = array('value' => array('EMPTY'), 'data' => array());
  234. if(isset($d['code'])) {
  235. if ($d['code'] == '') {
  236. $d['code'] = prepareFieldCodeGmp($d['label']).'_'.rand(0, 9999999);
  237. }
  238. }
  239. return $d;
  240. }
  241. }
  242. if(!function_exists('prepareFieldCodeGmp')) {
  243. function prepareFieldCodeGmp($string) {
  244. $string = preg_replace("/[^a-zA-Z0-9\s]/"," ",$string);
  245. $string = preg_replace("/\s+/", " ", $string);
  246. $string = preg_replace('/ /','',$string);
  247. $code = substr($string, 0, 8);
  248. $code = strtolower($code);
  249. if ($code == '') {
  250. $code = 'field_'.date('dhis');
  251. }
  252. return $code;
  253. }
  254. }
  255. /**
  256. * Recursive implode of array
  257. * @param string $glue imploder
  258. * @param array $array array to implode
  259. * @return string imploded array in string
  260. */
  261. if(!function_exists('recImplode')) {
  262. function recImplode($glue, $array) {
  263. $res = '';
  264. $i = 0;
  265. $count = count($array);
  266. foreach($array as $el) {
  267. $str = '';
  268. if(is_array($el))
  269. $str = recImplode('', $el);
  270. else
  271. $str = $el;
  272. $res .= $str;
  273. if($i < ($count-1))
  274. $res .= $glue;
  275. $i++;
  276. }
  277. return $res;
  278. }
  279. }
  280. if(!function_exists('toeObjectToArray')) {
  281. function toeObjectToArray($data) {
  282. if ((! is_array($data)) and (! is_object($data))) return $data; //$data;
  283. $result = array();
  284. $data = (array) $data;
  285. foreach ($data as $key => $value) {
  286. if (is_object($value)) $value = (array) $value;
  287. if (is_array($value))
  288. $result[$key] = toeObjectToArray($value);
  289. else
  290. $result[$key] = $value;
  291. }
  292. return $result;
  293. }
  294. }
  295. /**
  296. * Correct apply array_map even if array contains sub-arrays
  297. * @param array $array - input array
  298. * @return array - result array with array_map applied
  299. */
  300. if(!function_exists('toeMultArrayMap')) {
  301. function toeMultArrayMap($callback, $array) {
  302. if(is_array($array)) {
  303. foreach($array as $k => $v) {
  304. if(is_array($v)) {
  305. $array[ $k ] = toeMultArrayMap($callback, $v);
  306. } else {
  307. $array[ $k ] = call_user_func($callback, $v);
  308. }
  309. }
  310. } else {
  311. $array = call_user_func($callback, $array);
  312. }
  313. return $array;
  314. }
  315. }