Tool.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\utils;
  10. use mall\basic\Setting;
  11. use think\facade\Config;
  12. use think\facade\Request;
  13. class Tool {
  14. public static function thumb($image="",$prefix=null,$root=false){
  15. if(Check::url($image)) return $image;
  16. $domain = $root ? trim(Request::domain(),"/") : "";
  17. if(empty($image) || !file_exists(self::getRootPath() . "public/" . trim($image,"/"))){
  18. return $domain . "/static/images/default.jpg";
  19. }
  20. $image = "/" . substr($image,stripos($image,"uploads"));
  21. return $domain . $image;
  22. }
  23. public static function removeContentAttr($content){
  24. return preg_replace([
  25. '/(<img [^<>]*?)width=.+?[\'|\"]/',
  26. '/(<img.*?)((height)=[\'"]+[0-9|%]+[\'"]+)/',
  27. '/(<img.*?)((style)=[\'"]+(.*?)+[\'"]+)/',
  28. '/(<img.*?)((alt)=[\'"]+(.*?)+[\'"]+)/'
  29. ],'$1',$content);
  30. }
  31. public static function replaceContentImage($content){
  32. $matches = [];
  33. preg_match_all('/<img([ ]+)src="([^\"]+)"/i', $content, $matches);
  34. $array = [];
  35. foreach($matches[2] as $val){
  36. if(!in_array($val,$array)){
  37. $array[] = $val;
  38. // $content = str_replace($val, trim(Request::domain(),'/') . '/' . trim($val,"/") . '" style="max-width: 100%; display: block; margin:0 auto;', $content);
  39. $content = str_replace($val, Tool::thumb($val,"",true) . '" style="max-width: 100%; display: block; margin:0 auto;', $content);
  40. }
  41. }
  42. return $content;
  43. }
  44. public static function descarte($arr, $tmp = []) {
  45. static $n_arr = [];
  46. foreach (array_shift($arr) as $v) {
  47. $tmp[] = $v;
  48. if ($arr) {
  49. self::descarte($arr, $tmp);
  50. } else {
  51. $n_arr[] = $tmp;
  52. }
  53. array_pop($tmp);
  54. }
  55. return $n_arr;
  56. }
  57. public static function getDirSize($directory) {
  58. if(!file_exists($directory)){
  59. return 0;
  60. }
  61. $dir_size = 0;
  62. if (($dir_handle = opendir($directory)) != false) {
  63. while (($filename = readdir($dir_handle)) != false) {
  64. if ($filename != '.' && $filename != '..') {
  65. $subFile = $directory . '/' . $filename;
  66. if (is_dir($subFile))
  67. $dir_size += self::getDirSize($subFile);
  68. if (is_file($subFile))
  69. $dir_size += filesize($subFile);
  70. }
  71. }
  72. closedir($dir_handle);
  73. return $dir_size;
  74. }
  75. }
  76. public static function convert($size) {
  77. if($size == 0){
  78. return "0 Byte";
  79. }
  80. $unit = array('Byte', 'KB', 'MB', 'GB', 'TB', 'PB');
  81. return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
  82. }
  83. public static function deleteFile($file) {
  84. if (empty($file) || !file_exists($file)) {
  85. return false;
  86. }
  87. if (is_file($file)) {
  88. return unlink($file);
  89. }
  90. $ret = true;
  91. if (($handle = opendir($file)) != false) {
  92. while (($filename = readdir($handle)) != false) {
  93. if ($filename == '.' || $filename == '..') continue;
  94. if (!self::deleteFile($file . '/' . $filename)) $ret = false;
  95. }
  96. } else {
  97. $ret = false;
  98. }
  99. closedir($handle);
  100. if (file_exists($file) && !rmdir($file)) {
  101. $ret = false;
  102. }
  103. return $ret;
  104. }
  105. public static function getRandPrize($data) {
  106. $index = 0;
  107. $sum = array_sum($data);
  108. foreach ($data as $key => $item) {
  109. $num = mt_rand(1, $sum);
  110. if ($num <= $item) {
  111. $index = $key;
  112. break;
  113. } else {
  114. $sum -= $item;
  115. }
  116. }
  117. return $index;
  118. }
  119. public static function editor($content=""){
  120. if(empty($content)){
  121. return $content;
  122. }
  123. $runtime_path = self::getRootPath();
  124. $path = $runtime_path . 'runtime/htmlpurifier/';
  125. if(!file_exists($path)){
  126. mkdir($path,0777,true);
  127. }
  128. $config = \HTMLPurifier_Config::createDefault();
  129. $config->set('Cache.SerializerPath',$path);
  130. $config->set('HTML.SafeEmbed',true);
  131. $config->set('HTML.SafeObject',true);
  132. $config->set('HTML.SafeIframe',true);
  133. $config->set('Output.FlashCompat',true);
  134. $config->set('Attr.EnableID',true);
  135. //$config->set('HTML.AllowedElements',array('div'=>true,'table'=>true,'tr'=>true,'td'=>true,'br'=>true));
  136. $config->set('Core.Encoding','UTF-8');
  137. $def = $config->getHTMLDefinition(true);
  138. $def->addAttribute('a', 'target', 'Enum#_blank,_self,_target,_top');
  139. $def->addAttribute('iframe', 'src',"URI");
  140. $purifier = new \HTMLPurifier($config);
  141. return $purifier->purify($content);
  142. }
  143. public static function prefix(){
  144. $config = Config::get("database");
  145. return $config["connections"][$config["default"]]["prefix"];
  146. }
  147. public static function moneyPrefix($price=0){
  148. return Config::get("base.money_prefix") . $price;
  149. }
  150. public static function getRootPath(): string {
  151. $path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR;
  152. return $path;
  153. }
  154. public static function odd($num){
  155. return (is_numeric($num)&($num&1));
  156. }
  157. public static function even($num){
  158. return (is_numeric($num)&(!($num&1)));
  159. }
  160. }