人人商城

file.func.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function file_write($filename, $data) {
  8. global $_W;
  9. $filename = ATTACHMENT_ROOT . '/' . $filename;
  10. mkdirs(dirname($filename));
  11. file_put_contents($filename, $data);
  12. @chmod($filename, $_W['config']['setting']['filemode']);
  13. return is_file($filename);
  14. }
  15. function file_read($filename) {
  16. global $_W;
  17. $filename = ATTACHMENT_ROOT . '/' . $filename;
  18. if (!is_file($filename)) {
  19. return false;
  20. }
  21. return file_get_contents($filename);
  22. }
  23. function file_move($filename, $dest) {
  24. global $_W;
  25. mkdirs(dirname($dest));
  26. if (is_uploaded_file($filename)) {
  27. move_uploaded_file($filename, $dest);
  28. } else {
  29. rename($filename, $dest);
  30. }
  31. @chmod($filename, $_W['config']['setting']['filemode']);
  32. return is_file($dest);
  33. }
  34. function file_tree($path, $include = array()) {
  35. $files = array();
  36. if (!empty($include)) {
  37. $ds = glob($path . '/{' . implode(',', $include) . '}', GLOB_BRACE);
  38. } else {
  39. $ds = glob($path . '/*');
  40. }
  41. if (is_array($ds)) {
  42. foreach ($ds as $entry) {
  43. if (is_file($entry)) {
  44. $files[] = $entry;
  45. }
  46. if (is_dir($entry)) {
  47. $rs = file_tree($entry);
  48. foreach ($rs as $f) {
  49. $files[] = $f;
  50. }
  51. }
  52. }
  53. }
  54. return $files;
  55. }
  56. function file_tree_limit($path, $limit = 0, $acquired_files_count = 0) {
  57. $files = array();
  58. if (is_dir($path)){
  59. if ($dir = opendir($path)){
  60. while (($file = readdir($dir)) !== false){
  61. if (in_array($file, array('.', '..'))) {
  62. continue;
  63. }
  64. if (is_file($path . '/' . $file)) {
  65. $files[] = $path . '/' . $file;
  66. $acquired_files_count++;
  67. if ($limit > 0 && $acquired_files_count >= $limit) {
  68. closedir($dir);
  69. return $files;
  70. }
  71. }
  72. if (is_dir($path . '/' . $file)) {
  73. $rs = file_tree_limit($path . '/' . $file, $limit, $acquired_files_count);
  74. foreach ($rs as $f) {
  75. $files[] = $f;
  76. $acquired_files_count++;
  77. if ($limit > 0 && $acquired_files_count >= $limit) {
  78. closedir($dir);
  79. return $files;
  80. }
  81. }
  82. }
  83. }
  84. closedir($dir);
  85. }
  86. }
  87. return $files;
  88. }
  89. function file_dir_exist_image($path) {
  90. if (is_dir($path)){
  91. if ($dir = opendir($path)){
  92. while (($file = readdir($dir)) !== false){
  93. if (in_array($file, array('.', '..'))) {
  94. continue;
  95. }
  96. if (is_file($path . '/' . $file) && file_is_image($path . '/' . $file)) {
  97. if (strpos($path, ATTACHMENT_ROOT) === 0) {
  98. $attachment = str_replace(ATTACHMENT_ROOT . 'images/', '', $path . '/' .$file);
  99. list($file_account) = explode('/', $attachment);
  100. if ($file_account == 'global') {
  101. continue;
  102. }
  103. }
  104. closedir($dir);
  105. return true;
  106. }
  107. if (is_dir($path . '/' . $file) && file_dir_exist_image($path . '/' . $file)) {
  108. closedir($dir);
  109. return true;
  110. }
  111. }
  112. closedir($dir);
  113. }
  114. }
  115. return false;
  116. }
  117. function mkdirs($path) {
  118. if (!is_dir($path)) {
  119. mkdirs(dirname($path));
  120. mkdir($path);
  121. }
  122. return is_dir($path);
  123. }
  124. function file_copy($src, $des, $filter) {
  125. $dir = opendir($src);
  126. @mkdir($des);
  127. while (false !== ($file = readdir($dir))) {
  128. if (($file != '.') && ($file != '..')) {
  129. if (is_dir($src . '/' . $file)) {
  130. file_copy($src . '/' . $file, $des . '/' . $file, $filter);
  131. } elseif (!in_array(substr($file, strrpos($file, '.') + 1), $filter)) {
  132. copy($src . '/' . $file, $des . '/' . $file);
  133. }
  134. }
  135. }
  136. closedir($dir);
  137. }
  138. function rmdirs($path, $clean = false) {
  139. if (!is_dir($path)) {
  140. return false;
  141. }
  142. $files = glob($path . '/*');
  143. if ($files) {
  144. foreach ($files as $file) {
  145. is_dir($file) ? rmdirs($file) : @unlink($file);
  146. }
  147. }
  148. return $clean ? true : @rmdir($path);
  149. }
  150. function file_upload($file, $type = 'image', $name = '', $compress = false) {
  151. $harmtype = array('asp', 'php', 'jsp', 'js', 'css', 'php3', 'php4', 'php5', 'ashx', 'aspx', 'exe', 'cgi');
  152. if (empty($file)) {
  153. return error(-1, '没有上传内容');
  154. }
  155. if (!in_array($type, array('image', 'thumb', 'voice', 'video', 'audio'))) {
  156. return error(-2, '未知的上传类型');
  157. }
  158. global $_W;
  159. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  160. $ext = strtolower($ext);
  161. $setting = setting_load('upload');
  162. switch ($type) {
  163. case 'image':
  164. case 'thumb':
  165. $allowExt = array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'ico');
  166. $limit = $setting['upload']['image']['limit'];
  167. break;
  168. case 'voice':
  169. case 'audio':
  170. $allowExt = array('mp3', 'wma', 'wav', 'amr');
  171. $limit = $setting['upload']['audio']['limit'];
  172. break;
  173. case 'video':
  174. $allowExt = array('rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4');
  175. $limit = $setting['upload']['audio']['limit'];
  176. break;
  177. }
  178. $type = $type == 'image' ? 'image' : 'audio';
  179. $setting = $_W['setting']['upload'][$type];
  180. if (!empty($setting['extentions'])) {
  181. $allowExt = $setting['extentions'];
  182. }
  183. if (!in_array(strtolower($ext), $allowExt) || in_array(strtolower($ext), $harmtype)) {
  184. return error(-3, '不允许上传此类文件');
  185. }
  186. if (!empty($limit) && $limit * 1024 < filesize($file['tmp_name'])) {
  187. return error(-4, "上传的文件超过大小限制,请上传小于 {$limit}k 的文件");
  188. }
  189. $result = array();
  190. if (empty($name) || $name == 'auto') {
  191. $uniacid = intval($_W['uniacid']);
  192. $path = "{$type}s/{$uniacid}/" . date('Y/m/');
  193. mkdirs(ATTACHMENT_ROOT . '/' . $path);
  194. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $path, $ext);
  195. $result['path'] = $path . $filename;
  196. } else {
  197. mkdirs(dirname(ATTACHMENT_ROOT . '/' . $name));
  198. if (!strexists($name, $ext)) {
  199. $name .= '.' . $ext;
  200. }
  201. $result['path'] = $name;
  202. }
  203. $save_path = ATTACHMENT_ROOT . '/' . $result['path'];
  204. if (!file_move($file['tmp_name'], $save_path)) {
  205. return error(-1, '保存上传文件失败');
  206. }
  207. if ($type == 'image' && $compress) {
  208. file_image_quality($save_path, $save_path, $ext);
  209. }
  210. if (file_is_uni_attach($save_path)) {
  211. $check_result = file_check_uni_space($save_path);
  212. if (is_error($check_result)) {
  213. @unlink($save_path);
  214. return $check_result;
  215. }
  216. file_change_uni_attchsize($save_path);
  217. }
  218. $result['success'] = true;
  219. return $result;
  220. }
  221. function file_wechat_upload($file, $type = 'image', $name = '') {
  222. $harmtype = array('asp', 'php', 'jsp', 'js', 'css', 'php3', 'php4', 'php5', 'ashx', 'aspx', 'exe', 'cgi');
  223. if (empty($file)) {
  224. return error(-1, '没有上传内容');
  225. }
  226. if (!in_array($type, array('image', 'thumb', 'voice', 'video', 'audio'))) {
  227. return error(-2, '未知的上传类型');
  228. }
  229. global $_W;
  230. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  231. $ext = strtolower($ext);
  232. if (in_array(strtolower($ext), $harmtype)) {
  233. return error(-3, '不允许上传此类文件');
  234. }
  235. $result = array();
  236. if (empty($name) || $name == 'auto') {
  237. $uniacid = intval($_W['uniacid']);
  238. $path = "{$type}s/{$uniacid}/" . date('Y/m/');
  239. mkdirs(ATTACHMENT_ROOT . '/' . $path);
  240. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $path, $ext);
  241. $result['path'] = $path . $filename;
  242. } else {
  243. mkdirs(dirname(ATTACHMENT_ROOT . '/' . $name));
  244. if (!strexists($name, $ext)) {
  245. $name .= '.' . $ext;
  246. }
  247. $result['path'] = $name;
  248. }
  249. $save_path = ATTACHMENT_ROOT . '/' . $result['path'];
  250. if (!file_move($file['tmp_name'], $save_path)) {
  251. return error(-1, '保存上传文件失败');
  252. }
  253. if ($type == 'image') {
  254. file_image_quality($save_path, $save_path, $ext);
  255. }
  256. $result['success'] = true;
  257. return $result;
  258. }
  259. function file_remote_upload($filename, $auto_delete_local = true) {
  260. global $_W;
  261. if (empty($_W['setting']['remote']['type'])) {
  262. return false;
  263. }
  264. if ($_W['setting']['remote']['type'] == '1') {
  265. load()->library('ftp');
  266. $ftp_config = array(
  267. 'hostname' => $_W['setting']['remote']['ftp']['host'],
  268. 'username' => $_W['setting']['remote']['ftp']['username'],
  269. 'password' => $_W['setting']['remote']['ftp']['password'],
  270. 'port' => $_W['setting']['remote']['ftp']['port'],
  271. 'ssl' => $_W['setting']['remote']['ftp']['ssl'],
  272. 'passive' => $_W['setting']['remote']['ftp']['pasv'],
  273. 'timeout' => $_W['setting']['remote']['ftp']['timeout'],
  274. 'rootdir' => $_W['setting']['remote']['ftp']['dir'],
  275. );
  276. $ftp = new Ftp($ftp_config);
  277. if (true === $ftp->connect()) {
  278. $response = $ftp->upload(ATTACHMENT_ROOT . '/' . $filename, $filename);
  279. if ($auto_delete_local) {
  280. file_delete($filename);
  281. }
  282. if (!empty($response)) {
  283. return true;
  284. } else {
  285. return error(1, '远程附件上传失败,请检查配置并重新上传');
  286. }
  287. } else {
  288. return error(1, '远程附件上传失败,请检查配置并重新上传');
  289. }
  290. } elseif ($_W['setting']['remote']['type'] == '2') {
  291. load()->library('oss');
  292. load()->model('attachment');
  293. $buckets = attachment_alioss_buctkets($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret']);
  294. $host_name = $_W['setting']['remote']['alioss']['internal'] ? '-internal.aliyuncs.com' : '.aliyuncs.com';
  295. $endpoint = 'http://' . $buckets[$_W['setting']['remote']['alioss']['bucket']]['location'] . $host_name;
  296. try {
  297. $ossClient = new \OSS\OssClient($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $endpoint);
  298. $ossClient->uploadFile($_W['setting']['remote']['alioss']['bucket'], $filename, ATTACHMENT_ROOT . $filename);
  299. } catch (\OSS\Core\OssException $e) {
  300. return error(1, $e->getMessage());
  301. }
  302. if ($auto_delete_local) {
  303. file_delete($filename);
  304. }
  305. } elseif ($_W['setting']['remote']['type'] == '3') {
  306. load()->library('qiniu');
  307. $auth = new Qiniu\Auth($_W['setting']['remote']['qiniu']['accesskey'], $_W['setting']['remote']['qiniu']['secretkey']);
  308. $config = new Qiniu\Config();
  309. $uploadmgr = new Qiniu\Storage\UploadManager($config);
  310. $putpolicy = Qiniu\base64_urlSafeEncode(json_encode(array(
  311. 'scope' => $_W['setting']['remote']['qiniu']['bucket'] . ':' . $filename,
  312. )));
  313. $uploadtoken = $auth->uploadToken($_W['setting']['remote']['qiniu']['bucket'], $filename, 3600, $putpolicy);
  314. list($ret, $err) = $uploadmgr->putFile($uploadtoken, $filename, ATTACHMENT_ROOT . '/' . $filename);
  315. if ($auto_delete_local) {
  316. file_delete($filename);
  317. }
  318. if ($err !== null) {
  319. return error(1, '远程附件上传失败,请检查配置并重新上传');
  320. } else {
  321. return true;
  322. }
  323. } elseif ($_W['setting']['remote']['type'] == '4') {
  324. if (!empty($_W['setting']['remote']['cos']['local'])) {
  325. load()->library('cos');
  326. qcloudcos\Cosapi::setRegion($_W['setting']['remote']['cos']['local']);
  327. $uploadRet = qcloudcos\Cosapi::upload($_W['setting']['remote']['cos']['bucket'], ATTACHMENT_ROOT . $filename, '/' . $filename, '', 3 * 1024 * 1024, 0);
  328. } else {
  329. load()->library('cosv3');
  330. $uploadRet = \Qcloud_cos\Cosapi::upload($_W['setting']['remote']['cos']['bucket'], ATTACHMENT_ROOT . $filename, '/' . $filename, '', 3 * 1024 * 1024, 0);
  331. }
  332. if ($uploadRet['code'] != 0) {
  333. switch ($uploadRet['code']) {
  334. case -62:
  335. $message = '输入的appid有误';
  336. break;
  337. case -79:
  338. $message = '输入的SecretID有误';
  339. break;
  340. case -97:
  341. $message = '输入的SecretKEY有误';
  342. break;
  343. case -166:
  344. $message = '输入的bucket有误';
  345. break;
  346. }
  347. return error(-1, $message);
  348. }
  349. if ($auto_delete_local) {
  350. file_delete($filename);
  351. }
  352. }
  353. }
  354. function file_dir_remote_upload($dir_path, $limit = 50) {
  355. global $_W;
  356. if (empty($_W['setting']['remote']['type'])) {
  357. return error(1, '未开启远程附件');
  358. }
  359. $dir_path = safe_gpc_path($dir_path);
  360. if (!empty($dir_path)) {
  361. $local_attachment = file_tree_limit($dir_path, $limit);
  362. } else {
  363. $local_attachment = array();
  364. }
  365. if (is_array($local_attachment) && !empty($local_attachment)) {
  366. foreach ($local_attachment as $attachment) {
  367. $filename = str_replace(ATTACHMENT_ROOT, '', $attachment);
  368. list($image_dir, $file_account) = explode('/', $filename);
  369. if ($file_account == 'global' || !file_is_image($attachment)) {
  370. continue;
  371. }
  372. if (is_numeric($file_account) && is_dir(ATTACHMENT_ROOT . 'images/' . $file_account) && !empty($_W['setting']['remote_complete_info'][$file_account]['type'])) {
  373. $_W['setting']['remote'] = $_W['setting']['remote_complete_info'][$file_account];
  374. } else {
  375. $_W['setting']['remote'] = $_W['setting']['remote_complete_info'];
  376. }
  377. $result = file_remote_upload($filename);
  378. if (is_error($result)) {
  379. return $result;
  380. }
  381. }
  382. }
  383. return true;
  384. }
  385. function file_random_name($dir, $ext) {
  386. do {
  387. $filename = random(30) . '.' . $ext;
  388. } while (file_exists($dir . $filename));
  389. return $filename;
  390. }
  391. function file_delete($file) {
  392. global $_W;
  393. if (empty($file)) {
  394. return false;
  395. }
  396. if (file_exists(ATTACHMENT_ROOT . '/' . $file) && file_is_uni_attach(ATTACHMENT_ROOT . '/' . $file)) {
  397. file_change_uni_attchsize(ATTACHMENT_ROOT . '/' . $file, false);
  398. }
  399. if (file_exists($file)) {
  400. @unlink($file);
  401. }
  402. if (file_exists(ATTACHMENT_ROOT . '/' . $file)) {
  403. @unlink(ATTACHMENT_ROOT . '/' . $file);
  404. }
  405. return true;
  406. }
  407. function file_remote_delete($file) {
  408. global $_W;
  409. if (empty($file)) {
  410. return true;
  411. }
  412. if ($_W['setting']['remote']['type'] == '1') {
  413. load()->library('ftp');
  414. $ftp_config = array(
  415. 'hostname' => $_W['setting']['remote']['ftp']['host'],
  416. 'username' => $_W['setting']['remote']['ftp']['username'],
  417. 'password' => $_W['setting']['remote']['ftp']['password'],
  418. 'port' => $_W['setting']['remote']['ftp']['port'],
  419. 'ssl' => $_W['setting']['remote']['ftp']['ssl'],
  420. 'passive' => $_W['setting']['remote']['ftp']['pasv'],
  421. 'timeout' => $_W['setting']['remote']['ftp']['timeout'],
  422. 'rootdir' => $_W['setting']['remote']['ftp']['dir'],
  423. );
  424. $ftp = new Ftp($ftp_config);
  425. if (true === $ftp->connect()) {
  426. if ($ftp->delete_file($file)) {
  427. return true;
  428. } else {
  429. return error(1, '删除附件失败,请检查配置并重新删除');
  430. }
  431. } else {
  432. return error(1, '删除附件失败,请检查配置并重新删除');
  433. }
  434. } elseif ($_W['setting']['remote']['type'] == '2') {
  435. load()->model('attachment');
  436. load()->library('oss');
  437. $buckets = attachment_alioss_buctkets($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret']);
  438. $endpoint = 'http://' . $buckets[$_W['setting']['remote']['alioss']['bucket']]['location'] . '.aliyuncs.com';
  439. try {
  440. $ossClient = new \OSS\OssClient($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $endpoint);
  441. $ossClient->deleteObject($_W['setting']['remote']['alioss']['bucket'], $file);
  442. } catch (\OSS\Core\OssException $e) {
  443. return error(1, '删除oss远程文件失败');
  444. }
  445. } elseif ($_W['setting']['remote']['type'] == '3') {
  446. load()->library('qiniu');
  447. $auth = new Qiniu\Auth($_W['setting']['remote']['qiniu']['accesskey'], $_W['setting']['remote']['qiniu']['secretkey']);
  448. $bucketMgr = new Qiniu\Storage\BucketManager($auth);
  449. $error = $bucketMgr->delete($_W['setting']['remote']['qiniu']['bucket'], $file);
  450. if ($error instanceof Qiniu\Http\Error) {
  451. if ($error->code() == 612) {
  452. return true;
  453. }
  454. return error(1, '删除七牛远程文件失败');
  455. } else {
  456. return true;
  457. }
  458. } elseif ($_W['setting']['remote']['type'] == '4') {
  459. $bucketName = $_W['setting']['remote']['cos']['bucket'];
  460. $path = '/' . $file;
  461. if (!empty($_W['setting']['remote']['cos']['local'])) {
  462. load()->library('cos');
  463. qcloudcos\Cosapi::setRegion($_W['setting']['remote']['cos']['local']);
  464. $result = qcloudcos\Cosapi::delFile($bucketName, $path);
  465. } else {
  466. load()->library('cosv3');
  467. $result = Qcloud_cos\Cosapi::delFile($bucketName, $path);
  468. }
  469. if (!empty($result['code'])) {
  470. return error(-1, '删除cos远程文件失败');
  471. } else {
  472. return true;
  473. }
  474. }
  475. return true;
  476. }
  477. function file_image_thumb($srcfile, $desfile = '', $width = 0) {
  478. global $_W;
  479. load()->classs('image');
  480. if (intval($width) == 0) {
  481. load()->model('setting');
  482. $width = intval($_W['setting']['upload']['image']['width']);
  483. }
  484. if (empty($desfile)) {
  485. $ext = pathinfo($srcfile, PATHINFO_EXTENSION);
  486. $srcdir = dirname($srcfile);
  487. do {
  488. $desfile = $srcdir . '/' . random(30) . ".{$ext}";
  489. } while (file_exists($desfile));
  490. }
  491. $des = dirname($desfile);
  492. if (!file_exists($des)) {
  493. if (!mkdirs($des)) {
  494. return error('-1', '创建目录失败');
  495. }
  496. } elseif (!is_writable($des)) {
  497. return error('-1', '目录无法写入');
  498. }
  499. $org_info = @getimagesize($srcfile);
  500. if ($org_info) {
  501. if ($width == 0 || $width > $org_info[0]) {
  502. copy($srcfile, $desfile);
  503. return str_replace(ATTACHMENT_ROOT . '/', '', $desfile);
  504. }
  505. }
  506. $scale_org = $org_info[0] / $org_info[1];
  507. $height = $width / $scale_org;
  508. $desfile = Image::create($srcfile)->resize($width, $height)->saveTo($desfile);
  509. if (!$desfile) {
  510. return false;
  511. }
  512. return str_replace(ATTACHMENT_ROOT . '/', '', $desfile);
  513. }
  514. function file_image_crop($src, $desfile, $width = 400, $height = 300, $position = 1) {
  515. load()->classs('image');
  516. $des = dirname($desfile);
  517. if (!file_exists($des)) {
  518. if (!mkdirs($des)) {
  519. return error('-1', '创建目录失败');
  520. }
  521. } elseif (!is_writable($des)) {
  522. return error('-1', '目录无法写入');
  523. }
  524. return Image::create($src)
  525. ->crop($width, $height, $position)
  526. ->saveTo($desfile);
  527. }
  528. function file_lists($filepath, $subdir = 1, $ex = '', $isdir = 0, $md5 = 0, $enforcement = 0) {
  529. static $file_list = array();
  530. if ($enforcement) {
  531. $file_list = array();
  532. }
  533. $flags = $isdir ? GLOB_ONLYDIR : 0;
  534. $list = glob($filepath . '*' . (!empty($ex) && empty($subdir) ? '.' . $ex : ''), $flags);
  535. if (!empty($ex)) {
  536. $ex_num = strlen($ex);
  537. }
  538. foreach ($list as $k => $v) {
  539. $v = str_replace('\\', '/', $v);
  540. $v1 = str_replace(IA_ROOT . '/', '', $v);
  541. if ($subdir && is_dir($v)) {
  542. file_lists($v . '/', $subdir, $ex, $isdir, $md5);
  543. continue;
  544. }
  545. if (!empty($ex) && strtolower(substr($v, -$ex_num, $ex_num)) == $ex) {
  546. if ($md5) {
  547. $file_list[$v1] = md5_file($v);
  548. } else {
  549. $file_list[] = $v1;
  550. }
  551. continue;
  552. } elseif (!empty($ex) && strtolower(substr($v, -$ex_num, $ex_num)) != $ex) {
  553. unset($list[$k]);
  554. continue;
  555. }
  556. }
  557. return $file_list;
  558. }
  559. function file_remote_attach_fetch($url, $limit = 0, $path = '') {
  560. global $_W;
  561. $url = trim($url);
  562. if (empty($url)) {
  563. return error(-1, '文件地址不存在');
  564. }
  565. load()->func('communication');
  566. $resp = ihttp_get($url);
  567. if (is_error($resp)) {
  568. return error(-1, '提取文件失败, 错误信息: ' . $resp['message']);
  569. }
  570. if (intval($resp['code']) != 200) {
  571. return error(-1, '提取文件失败: 未找到该资源文件.');
  572. }
  573. $ext = $type = '';
  574. switch ($resp['headers']['Content-Type']) {
  575. case 'application/x-jpg':
  576. case 'image/jpg':
  577. case 'image/jpeg':
  578. $ext = 'jpg';
  579. $type = 'images';
  580. break;
  581. case 'image/png':
  582. $ext = 'png';
  583. $type = 'images';
  584. break;
  585. case 'image/gif':
  586. $ext = 'gif';
  587. $type = 'images';
  588. break;
  589. case 'video/mp4':
  590. case 'video/mpeg4':
  591. $ext = 'mp4';
  592. $type = 'videos';
  593. break;
  594. case 'video/x-ms-wmv':
  595. $ext = 'wmv';
  596. $type = 'videos';
  597. break;
  598. case 'audio/mpeg':
  599. $ext = 'mp3';
  600. $type = 'audios';
  601. break;
  602. case 'audio/mp4':
  603. $ext = 'mp4';
  604. $type = 'audios';
  605. break;
  606. case 'audio/x-ms-wma':
  607. $ext = 'wma';
  608. $type = 'audios';
  609. break;
  610. default:
  611. return error(-1, '提取资源失败, 资源文件类型错误.');
  612. break;
  613. }
  614. if (empty($path)) {
  615. $path = $type . "/{$_W['uniacid']}/" . date('Y/m/');
  616. } else {
  617. $path = parse_path($path);
  618. }
  619. if (!$path) {
  620. return error(-1, '提取文件失败: 上传路径配置有误.');
  621. }
  622. if (! is_dir(ATTACHMENT_ROOT . $path)) {
  623. if (! mkdirs(ATTACHMENT_ROOT . $path, 0700, true)) {
  624. return error(-1, '提取文件失败: 权限不足.');
  625. }
  626. }
  627. if (!$limit) {
  628. if ($type == 'images') {
  629. $limit = $_W['setting']['upload']['image']['limit'] * 1024;
  630. } else {
  631. $limit = $_W['setting']['upload']['audio']['limit'] * 1024;
  632. }
  633. } else {
  634. $limit = $limit * 1024;
  635. }
  636. if (intval($resp['headers']['Content-Length']) > $limit) {
  637. return error(-1, '上传的媒体文件过大(' . sizecount($resp['headers']['Content-Length']) . ' > ' . sizecount($limit));
  638. }
  639. $filename = file_random_name(ATTACHMENT_ROOT . $path, $ext);
  640. $pathname = $path . $filename;
  641. $fullname = ATTACHMENT_ROOT . $pathname;
  642. if (file_put_contents($fullname, $resp['content']) == false) {
  643. return error(-1, '提取失败.');
  644. }
  645. return $pathname;
  646. }
  647. function file_is_image($url) {
  648. if (!parse_path($url)) {
  649. return false;
  650. }
  651. $pathinfo = pathinfo($url);
  652. $extension = strtolower($pathinfo['extension']);
  653. return !empty($extension) && in_array($extension, array('jpg', 'jpeg', 'gif', 'png'));
  654. }
  655. function file_image_quality($src, $to_path, $ext) {
  656. load()->classs('image');
  657. global $_W;
  658. $quality = intval($_W['setting']['upload']['image']['zip_percentage']);
  659. if ($quality <= 0 || $quality >= 100) {
  660. return;
  661. }
  662. if (filesize($src) / 1024 > 5120) {
  663. return;
  664. }
  665. $result = Image::create($src, $ext)->saveTo($to_path, $quality);
  666. return $result;
  667. }
  668. function file_is_uni_attach($file) {
  669. global $_W;
  670. if (!is_file($file)) {
  671. return error(-1, '未找到的文件。');
  672. }
  673. return strpos($file, "/{$_W['uniacid']}/") > 0;
  674. }
  675. function file_check_uni_space($file) {
  676. global $_W;
  677. if (!is_file($file)) {
  678. return error(-1, '未找到上传的文件。');
  679. }
  680. if (empty($_W['setting']['remote'][$_W['uniacid']]['type'])) {
  681. $uni_setting = uni_setting_load(array('attachment_limit', 'attachment_size'));
  682. $attachment_limit = intval($uni_setting['attachment_limit']);
  683. if ($attachment_limit == 0) {
  684. $upload = setting_load('upload');
  685. $attachment_limit = empty($upload['upload']['attachment_limit']) ? 0 : intval($upload['upload']['attachment_limit']);
  686. }
  687. if ($attachment_limit > 0) {
  688. $file_size = max(1, round(filesize($file) / 1024));
  689. if (($file_size + $uni_setting['attachment_size']) > ($attachment_limit * 1024)) {
  690. return error(-1, '上传失败,可使用的附件空间不足!');
  691. }
  692. }
  693. }
  694. return true;
  695. }
  696. function file_change_uni_attchsize($file, $is_add = true) {
  697. global $_W;
  698. if (!is_file($file)) {
  699. return error(-1, '未找到的文件。');
  700. }
  701. $file_size = round(filesize($file) / 1024);
  702. $file_size = max(1, $file_size);
  703. $result = true;
  704. if (empty($_W['setting']['remote'][$_W['uniacid']]['type'])) {
  705. $uniacid = pdo_getcolumn('uni_settings', array('uniacid' => $_W['uniacid']), 'uniacid');
  706. if (empty($uniacid)) {
  707. $result = pdo_insert('uni_settings', array('attachment_size' => $file_size, 'uniacid' => $_W['uniacid']));
  708. } else {
  709. if (!$is_add) {
  710. $file_size = -$file_size;
  711. }
  712. $result = pdo_update('uni_settings', array('attachment_size +=' => $file_size), array('uniacid' => $_W['uniacid']));
  713. }
  714. $cachekey = cache_system_key('unisetting', array('uniacid' => $uniacid));
  715. $unisetting = cache_load($cachekey);
  716. $unisetting['attachment_size'] += $file_size;
  717. $unisetting['attachment_size'] = max(0, $unisetting['attachment_size']);
  718. cache_write($cachekey, $unisetting);
  719. }
  720. return $result;
  721. }