PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/megathrone86/SoftwareProjects
PHP | 381 lines | 279 code | 5 blank | 97 comment | 65 complexity | 4370eb4884ef91c6623fe3a75b83b77d MD5 | raw file
  1. <?php
  2. class utilsBup {
  3. static public function jsonEncode($arr) {
  4. return (is_array($arr) || is_object($arr)) ? json_encode_utf_normal($arr) : json_encode_utf_normal(array());
  5. }
  6. static public function jsonDecode($str) {
  7. if(is_array($str))
  8. return $str;
  9. if(is_object($str))
  10. return (array)$str;
  11. return empty($str) ? array() : json_decode($str, true);
  12. }
  13. static public function unserialize($data) {
  14. return unserialize($data);
  15. }
  16. static public function serialize($data) {
  17. return serialize($data);
  18. }
  19. static public function createDir($path, $params = array('chmod' => NULL, 'httpProtect' => false)) {
  20. if(@mkdir($path)) {
  21. if(!is_null($params['chmod'])) {
  22. @chmod($path, $params['chmod']);
  23. }
  24. //if(!empty($params['httpProtect'])) {
  25. if($params['httpProtect'] == 1) {
  26. self::httpProtectDir($path);
  27. }
  28. if($params['httpProtect'] == 2) {
  29. self::httpProtectDir($path, true);
  30. }
  31. return true;
  32. }
  33. return false;
  34. }
  35. static public function httpProtectDir($path, $c = NULL) {
  36. $eol = "\n";
  37. $all = 'DENY FROM ALL';
  38. $custom = '<Files ~ "\.(php|html)$">'.$eol.'DENY FROM ALL'.$eol.'</Files>';
  39. $content = $c ? $custom : $all;
  40. if(strrpos($path, DS) != strlen($path))
  41. $path .= DS;
  42. if(file_put_contents($path. '.htaccess', $content)) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. /**
  48. * Copy all files from one directory ($source) to another ($destination)
  49. * @param string $source path to source directory
  50. * @params string $destination path to destination directory
  51. */
  52. static public function copyDirectories($source, $destination) {
  53. if(is_dir($source)) {
  54. @mkdir($destination);
  55. $directory = dir($source);
  56. while ( FALSE !== ( $readdirectory = $directory->read() ) ) {
  57. if ( $readdirectory == '.' || $readdirectory == '..' ) {
  58. continue;
  59. }
  60. $PathDir = $source . '/' . $readdirectory;
  61. if (is_dir($PathDir)) {
  62. utilsBup::copyDirectories( $PathDir, $destination . '/' . $readdirectory );
  63. continue;
  64. }
  65. copy( $PathDir, $destination . '/' . $readdirectory );
  66. }
  67. $directory->close();
  68. } else {
  69. copy( $source, $destination );
  70. }
  71. }
  72. static public function getIP() {
  73. return (empty($_SERVER['HTTP_CLIENT_IP']) ? (empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']) : $_SERVER['HTTP_CLIENT_IP']);
  74. }
  75. /**
  76. * Parse xml file into simpleXML object
  77. * @param string $path path to xml file
  78. * @return mixed object SimpleXMLElement if success, else - false
  79. */
  80. static public function getXml($path) {
  81. if(is_file($path)) {
  82. return simplexml_load_file($path);
  83. }
  84. return false;
  85. }
  86. /**
  87. * Check if the element exists in array
  88. * @param array $param
  89. */
  90. static public function xmlAttrToStr($param, $element) {
  91. if (isset($param[$element])) {
  92. // convert object element to string
  93. return (string)$param[$element];
  94. } else {
  95. return '';
  96. }
  97. }
  98. static public function xmlNodeAttrsToArr($node) {
  99. $arr = array();
  100. foreach($node->attributes() as $a => $b) {
  101. $arr[$a] = utilsBup::xmlAttrToStr($node, $a);
  102. }
  103. return $arr;
  104. }
  105. static public function deleteFile($str) {
  106. return @unlink($str);
  107. }
  108. static public function deleteDir($str){
  109. if(is_file($str)){
  110. return self::deleteFile($str);
  111. }
  112. elseif(is_dir($str)){
  113. $scan = glob(rtrim($str,'/').'/*');
  114. foreach($scan as $index=>$path){
  115. utilsBup::deleteDir($path);
  116. }
  117. return @rmdir($str);
  118. }
  119. }
  120. /**
  121. * Retrives list of directories ()
  122. */
  123. static public function getDirList($path) {
  124. $res = array();
  125. if(is_dir($path)){
  126. $files = scandir($path);
  127. foreach($files as $f) {
  128. if($f == '.' || $f == '..' || $f == '.svn') continue;
  129. if(!is_dir($path. $f)) continue;
  130. $res[$f] = array('path' => $path. $f. DS);
  131. }
  132. }
  133. return $res;
  134. }
  135. /**
  136. * Retrives list of files
  137. */
  138. static public function getFilesList($path) {
  139. $files = array();
  140. if(is_dir($path)){
  141. $dirHandle = opendir($path);
  142. while(($file = readdir($dirHandle)) !== false) {
  143. if($file != '.' && $file != '..' && $f != '.svn' && is_file($path. DS. $file)) {
  144. $files[] = $file;
  145. }
  146. }
  147. }
  148. return $files;
  149. }
  150. /**
  151. * Check if $var is object or something another in future
  152. */
  153. static public function is($var, $what = '') {
  154. if (!is_object($var)) {
  155. return false;
  156. }
  157. if(get_class($var) == $what) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. /**
  163. * Get array with all monthes of year, uses in paypal pro and sagepay payment modules for now, than - who knows)
  164. * @return array monthes
  165. */
  166. static public function getMonthesArray() {
  167. static $monthsArray = array();
  168. //Some cache
  169. if(!empty($monthsArray))
  170. return $monthsArray;
  171. for ($i=1; $i<13; $i++) {
  172. $monthsArray[sprintf('%02d', $i)] = strftime('%B', mktime(0,0,0,$i,1,2000));
  173. }
  174. return $monthsArray;
  175. }
  176. /**
  177. * Get an array with years range from current year
  178. * @param int $from - how many years from today ago
  179. * @param int $to - how many years in future
  180. * @param $formatKey - format for keys in array, @see strftime
  181. * @param $formatVal - format for values in array, @see strftime
  182. * @return array - years
  183. */
  184. static public function getYearsArray($from, $to, $formatKey = '%Y', $formatVal = '%Y') {
  185. $today = getdate();
  186. $yearsArray = array();
  187. for ($i=$today['year']-$from; $i <= $today['year']+$to; $i++) {
  188. $yearsArray[strftime($formatKey,mktime(0,0,0,1,1,$i))] = strftime($formatVal,mktime(0,0,0,1,1,$i));
  189. }
  190. return $yearsArray;
  191. }
  192. /**
  193. * Make replacement in $text, where it will be find all keys with prefix ":" and replace it with corresponding value
  194. * @see email_templatesModel::renderContent()
  195. * @see checkoutView::getSuccessPage()
  196. */
  197. static public function makeVariablesReplacement($text, $variables) {
  198. if(!empty($text) && !empty($variables) && is_array($variables)) {
  199. foreach($variables as $k => $v) {
  200. $text = str_replace(':'. $k, $v, $text);
  201. }
  202. return $text;
  203. }
  204. return false;
  205. }
  206. static public function getCurrentWPThemeDir() {
  207. static $themePath;
  208. if(empty($themePath)) {
  209. $themePath = get_theme_root(). DS. utilsBup::getCurrentWPThemeCode(). DS;
  210. }
  211. return $themePath;
  212. }
  213. /**
  214. * Retrive full directory of plugin
  215. * @param string $name - plugin name
  216. * @return string full path in file system to plugin directory
  217. */
  218. static public function getPluginDir($name = '') {
  219. return WP_PLUGIN_DIR. DS. $name. DS;
  220. }
  221. static public function getPluginPath($name = '') {
  222. return WP_PLUGIN_URL. '/'. $name. '/';
  223. }
  224. static public function getExtModDir($plugName) {
  225. return self::getPluginDir($plugName);
  226. }
  227. static public function getExtModPath($plugName) {
  228. return self::getPluginPath($plugName);
  229. }
  230. static public function getCurrentWPThemePath() {
  231. return get_template_directory_uri();
  232. }
  233. static public function getCurrentWPThemeCode() {
  234. static $activeThemeName;
  235. if(empty($activeThemeName)) {
  236. $wpTheme = wp_get_theme();
  237. $activeThemeName = $wpTheme->template;
  238. }
  239. return $activeThemeName;
  240. }
  241. static public function isThisCommercialEdition() {
  242. /*$commercialModules = array('rating');
  243. foreach($commercialModules as $m) {
  244. if(!frameBup::_()->getModule($m))
  245. return false;
  246. if(!is_dir(frameBup::_()->getModule($m)->getModDir()))
  247. return false;
  248. }
  249. return true;*/
  250. foreach(frameBup::_()->getModules() as $m) {
  251. if(is_object($m) && $m->isExternal()) // Should be at least one external module
  252. return true;
  253. }
  254. return false;
  255. }
  256. static public function checkNum($val, $default = 0) {
  257. if(!empty($val) && is_numeric($val))
  258. return $val;
  259. return $default;
  260. }
  261. static public function checkString($val, $default = '') {
  262. if(!empty($val) && is_string($val))
  263. return $val;
  264. return $default;
  265. }
  266. /**
  267. * Retrives extension of file
  268. * @param string $path - path to a file
  269. * @return string - file extension
  270. */
  271. static public function getFileExt($path) {
  272. return strtolower( pathinfo($path, PATHINFO_EXTENSION) );
  273. }
  274. static public function getRandStr($length = 10, $allowedChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890') {
  275. $result = '';
  276. $allowedCharsLen = strlen($allowedChars);
  277. while(strlen($result) < $length) {
  278. $result .= substr($allowedChars, rand(0, $allowedCharsLen), 1);
  279. }
  280. return $result;
  281. }
  282. /**
  283. * Get current host location
  284. * @return string host string
  285. */
  286. static public function getHost() {
  287. return $_SERVER['HTTP_HOST'];
  288. }
  289. /**
  290. * Check if device is mobile
  291. * @return bool true if user are watching this site from mobile device
  292. */
  293. static public function isMobile() {
  294. return mobileDetect::_()->isMobile();
  295. }
  296. /**
  297. * Check if device is tablet
  298. * @return bool true if user are watching this site from tablet device
  299. */
  300. static public function isTablet() {
  301. return mobileDetect::_()->isTablet();
  302. }
  303. static public function getWidgetCategory($callback) {
  304. $categoryToId = array(
  305. 'products' => array('toealsopurchasedwidget', 'toebestsellerswidget', 'toefpwidget', 'toemostviewedwidget', 'toerecentproductswidget', 'toespwidget'),
  306. 'shopping' => array('toebcwidget', 'toecurrencywidget', 'toeshoppingcartwidget'),
  307. 'additional' => array('toebrcwidget', 'toecommentswidget', 'toesearchwidget', 'toesliderwidget', 'toetwitterwidget'),
  308. );
  309. foreach($categoryToId as $cat => $ids) {
  310. if(in_array($callback, $ids))
  311. return $cat;
  312. }
  313. return false;
  314. }
  315. static public function getUploadsDir() {
  316. $uploadDir = wp_upload_dir();
  317. return $uploadDir['basedir'];
  318. }
  319. static public function getUploadsPath() {
  320. $uploadDir = wp_upload_dir();
  321. return $uploadDir['baseurl'];
  322. }
  323. static public function arrToCss($data) {
  324. $res = '';
  325. if(!empty($data)) {
  326. foreach($data as $k => $v) {
  327. $res .= $k. ':'. $v. ';';
  328. }
  329. }
  330. return $res;
  331. }
  332. /**
  333. * Activate all BUP Plugins
  334. *
  335. * @return NULL Check if it's site or multisite and activate.
  336. */
  337. static public function activatePlugin() {
  338. /*global $wpdb;
  339. if (function_exists('is_multisite') && is_multisite()) {
  340. $orig_id = $wpdb->blogid;
  341. $blog_id = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
  342. foreach ($blog_id as $id) {
  343. if (switch_to_blog($id)) {
  344. installerBup::init();
  345. }
  346. }
  347. switch_to_blog($orig_id);
  348. return;
  349. } else {
  350. installerBup::init();
  351. }*/
  352. if(BUP_TEST_MODE) {
  353. add_action('activated_plugin', array(frameBup::_(), 'savePluginActivationErrors'));
  354. }
  355. installerBup::init();
  356. }
  357. /**
  358. * Deactivate All BUP Plugins
  359. *
  360. * @return NULL Check if it's site or multisite and decativate it.
  361. */
  362. public static function deletePlugin()
  363. {
  364. installerBup::delete();
  365. }
  366. static public function checkPRO() {
  367. return frameBup::_()->getModule('license') ? true : false;
  368. }
  369. static public function isSessionStarted() {
  370. if(version_compare(PHP_VERSION, '5.4.0') >= 0 && function_exists('session_status')) {
  371. return !(session_status() == PHP_SESSION_NONE);
  372. } else {
  373. return !(session_id() == '');
  374. }
  375. }
  376. }