PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/kz40_site/www/wp/wp-content/plugins/backup-by-supsystic/functions.php

https://gitlab.com/megathrone86/SoftwareProjects
PHP | 378 lines | 306 code | 11 blank | 61 comment | 59 complexity | 88a255a031045f2f8c385b5e0e77286c 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('dateToTimestampBup')) {
  16. function dateToTimestampBup($date) {
  17. if(empty($a))
  18. return false;
  19. $a = explode(BUP_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('import')) {
  42. function import($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('importClassBup')) {
  59. function importClassBup($class, $path = '') {
  60. if(!class_exists($class)) {
  61. if(!$path) {
  62. $classFile = $class;
  63. if(strpos(strtolower($classFile), BUP_CODE) !== false) {
  64. $classFile = preg_replace('/'. BUP_CODE. '$/i', '', $classFile);
  65. }
  66. $path = BUP_CLASSES_DIR. $classFile. '.php';
  67. }
  68. return import($path);
  69. }
  70. return false;
  71. }
  72. }
  73. /**
  74. * Check if class name exist with prefix or not
  75. * @param strin $class preferred class name
  76. * @return string existing class name
  77. */
  78. if(!function_exists('toeGetClassNameBup')) {
  79. function toeGetClassNameBup($class) {
  80. $className = '';
  81. if(class_exists($class. strFirstUp(BUP_CODE)))
  82. $className = $class. strFirstUp(BUP_CODE);
  83. else if(class_exists(BUP_CLASS_PREFIX. $class))
  84. $className = BUP_CLASS_PREFIX. $class;
  85. else
  86. $className = $class;
  87. return $className;
  88. }
  89. }
  90. /**
  91. * Create object of specified class
  92. * @param string $class class that you want to create
  93. * @param array $params array of arguments for class __construct function
  94. * @return object new object of specified class
  95. */
  96. if(!function_exists('toeCreateObjBup')) {
  97. function toeCreateObjBup($class, $params) {
  98. $className = toeGetClassNameBup($class);
  99. $obj = NULL;
  100. if(class_exists('ReflectionClass')) {
  101. $reflection = new ReflectionClass($className);
  102. try {
  103. $obj = $reflection->newInstanceArgs($params);
  104. } catch (ReflectionException $e) { // If class have no constructor
  105. $obj = $reflection->newInstanceArgs();
  106. }
  107. } else {
  108. $obj = new $className();
  109. call_user_func_array(array($obj, '__construct'), $params);
  110. }
  111. return $obj;
  112. }
  113. }
  114. /**
  115. * Redirect user to specified location. Be advised that it should redirect even if headers alredy sent.
  116. * @param string $url where page must be redirected
  117. */
  118. if(!function_exists('redirectBup')) {
  119. function redirectBup($url) {
  120. if(headers_sent()) {
  121. echo '<script type="text/javascript"> document.location.href = "'. $url. '"; </script>';
  122. } else {
  123. header('Location: '. $url);
  124. }
  125. exit();
  126. }
  127. }
  128. if(!function_exists('in_array_array')) {
  129. function in_array_array($needle, $haystack) {
  130. if(is_array($needle)) {
  131. foreach($needle as $n) {
  132. if(in_array($n, $haystack))
  133. return true;
  134. }
  135. return false;
  136. } else
  137. return in_array_array($needle, $haystack);
  138. }
  139. }
  140. if(!function_exists('json_encode_utf_normal')) {
  141. function json_encode_utf_normal($value) {
  142. if (is_int($value)) {
  143. return (string)$value;
  144. } elseif (is_string($value)) {
  145. $value = str_replace(array('\\', '/', '"', "\r", "\n", "\b", "\f", "\t"),
  146. array('\\\\', '\/', '\"', '\r', '\n', '\b', '\f', '\t'), $value);
  147. $convmap = array(0x80, 0xFFFF, 0, 0xFFFF);
  148. $result = "";
  149. for ($i = strlen($value) - 1; $i >= 0; $i--) {
  150. $mb_char = substr($value, $i, 1);
  151. $result = $mb_char . $result;
  152. }
  153. return '"' . $result . '"';
  154. } elseif (is_float($value)) {
  155. return str_replace(",", ".", $value);
  156. } elseif (is_null($value)) {
  157. return 'null';
  158. } elseif (is_bool($value)) {
  159. return $value ? 'true' : 'false';
  160. } elseif (is_array($value)) {
  161. $with_keys = false;
  162. $n = count($value);
  163. for ($i = 0, reset($value); $i < $n; $i++, next($value)) {
  164. if (key($value) !== $i) {
  165. $with_keys = true;
  166. break;
  167. }
  168. }
  169. } elseif (is_object($value)) {
  170. $with_keys = true;
  171. } else {
  172. return '';
  173. }
  174. $result = array();
  175. if ($with_keys) {
  176. foreach ($value as $key => $v) {
  177. $result[] = json_encode_utf_normal((string)$key) . ':' . json_encode_utf_normal($v);
  178. }
  179. return '{' . implode(',', $result) . '}';
  180. } else {
  181. foreach ($value as $key => $v) {
  182. $result[] = json_encode_utf_normal($v);
  183. }
  184. return '[' . implode(',', $result) . ']';
  185. }
  186. }
  187. }
  188. if(!function_exists('prepareFieldCodeBup')) {
  189. function prepareFieldCodeBup($string) {
  190. $string = preg_replace("/[^a-zA-Z0-9\s]/"," ",$string);
  191. $string = preg_replace("/\s+/", " ", $string);
  192. $string = ereg_replace(' ','',$string);
  193. $code = substr($string, 0, 8);
  194. $code = strtolower($code);
  195. if ($code == '') {
  196. $code = 'field_'.date('dhis');
  197. }
  198. return $code;
  199. }
  200. }
  201. /**
  202. * Recursive implode of array
  203. * @param string $glue imploder
  204. * @param array $array array to implode
  205. * @return string imploded array in string
  206. */
  207. if(!function_exists('recImplode')) {
  208. function recImplode($glue, $array) {
  209. $res = '';
  210. $i = 0;
  211. $count = count($array);
  212. foreach($array as $el) {
  213. $str = '';
  214. if(is_array($el))
  215. $str = recImplode('', $el);
  216. else
  217. $str = $el;
  218. $res .= $str;
  219. if($i < ($count-1))
  220. $res .= $glue;
  221. $i++;
  222. }
  223. return $res;
  224. }
  225. }
  226. if(!function_exists('toeObjectToArray')) {
  227. function toeObjectToArray($data) {
  228. if ((! is_array($data)) and (! is_object($data))) return $data; //$data;
  229. $result = array();
  230. $data = (array) $data;
  231. foreach ($data as $key => $value) {
  232. if (is_object($value)) $value = (array) $value;
  233. if (is_array($value))
  234. $result[$key] = toeObjectToArray($value);
  235. else
  236. $result[$key] = $value;
  237. }
  238. return $result;
  239. }
  240. }
  241. /**
  242. * Correct apply array_map even if array contains sub-arrays
  243. * @param array $array - input array
  244. * @return array - result array with array_map applied
  245. */
  246. if(!function_exists('toeMultArrayMap')) {
  247. function toeMultArrayMap($callback, $array) {
  248. if(is_array($array)) {
  249. foreach($array as $k => $v) {
  250. if(is_array($v)) {
  251. $array[ $k ] = toeMultArrayMap($callback, $v);
  252. } else {
  253. $array[ $k ] = call_user_func($callback, $v);
  254. }
  255. }
  256. } else {
  257. $array = call_user_func($callback, $array);
  258. }
  259. return $array;
  260. }
  261. }
  262. if(!function_exists('toeVarDump')) {
  263. /**
  264. * Make var_dump(); with <pre> tag to display data more clear
  265. */
  266. function toeVarDump() {
  267. echo '<pre>';
  268. call_user_func_array('var_dump', func_get_args());
  269. echo '</pre>';
  270. }
  271. }
  272. /**
  273. * Prepares the params values to store into db
  274. *
  275. * @param array $d $_POST array
  276. * @return array
  277. */
  278. if(!function_exists('prepareParamsBup')) {
  279. function prepareParamsBup(&$d=array(), &$options = array()) {
  280. if (!empty($d['params'])) {
  281. if (isset($d['params']['options'])) {
  282. $options = $d['params']['options'];
  283. //unset($d['params']['options']);
  284. }
  285. if (is_array($d['params'])) {
  286. $params = utilsCsp::jsonEncode($d['params']);
  287. $params = str_replace(array('\n\r', "\n\r", '\n', "\r", '\r', "\r"), '<br />', $params);
  288. $params = str_replace(array('<br /><br />', '<br /><br /><br />'), '<br />', $params);
  289. $d['params'] = $params;
  290. }
  291. } elseif(isset($d['params'])) {
  292. $d['params']['attr']['class'] = '';
  293. $d['params']['attr']['id'] = '';
  294. $params = utilsCsp::jsonEncode($d['params']);
  295. $d['params'] = $params;
  296. }
  297. if(empty($options))
  298. $options = array('value' => array('EMPTY'), 'data' => array());
  299. if(isset($d['code'])) {
  300. if ($d['code'] == '') {
  301. $d['code'] = prepareFieldCodeBup($d['label']).'_'.rand(0, 9999999);
  302. }
  303. }
  304. return $d;
  305. }
  306. if(!function_exists('getGoogleClientApiConfig')) {
  307. function getGoogleClientApiConfig(){
  308. $apiConfig = array(
  309. // True if objects should be returned by the service classes.
  310. // False if associative arrays should be returned (default behavior).
  311. 'use_objects' => false,
  312. // The application_name is included in the User-Agent HTTP header.
  313. 'application_name' => '',
  314. // OAuth2 Settings, you can get these keys at https://code.google.com/apis/console
  315. 'oauth2_client_id' => '',
  316. 'oauth2_client_secret' => '',
  317. 'oauth2_redirect_uri' => '',
  318. // The developer key, you get this at https://code.google.com/apis/console
  319. 'developer_key' => '',
  320. // Site name to show in the Google's OAuth 1 authentication screen.
  321. 'site_name' => 'www.example.org',
  322. // Which Authentication, Storage and HTTP IO classes to use.
  323. 'authClass' => 'Google_OAuth2',
  324. 'ioClass' => 'Google_CurlIO',
  325. 'cacheClass' => 'Google_FileCache',
  326. // Don't change these unless you're working against a special development or testing environment.
  327. 'basePath' => 'https://www.googleapis.com',
  328. // IO Class dependent configuration, you only have to configure the values
  329. // for the class that was configured as the ioClass above
  330. 'ioFileCache_directory' =>
  331. (function_exists('sys_get_temp_dir') ?
  332. sys_get_temp_dir() . '/Google_Client' :
  333. '/tmp/Google_Client'),
  334. // Definition of service specific values like scopes, oauth token URLs, etc
  335. 'services' => array(
  336. 'analytics' => array('scope' => 'https://www.googleapis.com/auth/analytics.readonly'),
  337. 'calendar' => array(
  338. 'scope' => array(
  339. "https://www.googleapis.com/auth/calendar",
  340. "https://www.googleapis.com/auth/calendar.readonly",
  341. )
  342. ),
  343. 'books' => array('scope' => 'https://www.googleapis.com/auth/books'),
  344. 'latitude' => array(
  345. 'scope' => array(
  346. 'https://www.googleapis.com/auth/latitude.all.best',
  347. 'https://www.googleapis.com/auth/latitude.all.city',
  348. )
  349. ),
  350. 'moderator' => array('scope' => 'https://www.googleapis.com/auth/moderator'),
  351. 'oauth2' => array(
  352. 'scope' => array(
  353. 'https://www.googleapis.com/auth/userinfo.profile',
  354. 'https://www.googleapis.com/auth/userinfo.email',
  355. )
  356. ),
  357. 'plus' => array('scope' => 'https://www.googleapis.com/auth/plus.login'),
  358. 'siteVerification' => array('scope' => 'https://www.googleapis.com/auth/siteverification'),
  359. 'tasks' => array('scope' => 'https://www.googleapis.com/auth/tasks'),
  360. 'urlshortener' => array('scope' => 'https://www.googleapis.com/auth/urlshortener')
  361. )
  362. );
  363. return $apiConfig;
  364. }
  365. }
  366. }
  367. ?>