人人商城

file.func.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. //载入日志函数
  205. load()->func('logging');
  206. //记录文本日志
  207. logging_run('记录字符串日志数据');
  208. //记录数组数据
  209. logging_run(array('$save_path' => $save_path, 'age' => '18'));
  210. if (!file_move($file['tmp_name'], $save_path)) {
  211. return error(-1, '保存上传文件失败');
  212. }
  213. if ($type == 'image' && $compress) {
  214. file_image_quality($save_path, $save_path, $ext);
  215. }
  216. if (file_is_uni_attach($save_path)) {
  217. $check_result = file_check_uni_space($save_path);
  218. if (is_error($check_result)) {
  219. @unlink($save_path);
  220. return $check_result;
  221. }
  222. file_change_uni_attchsize($save_path);
  223. }
  224. $result['success'] = true;
  225. return $result;
  226. }
  227. function file_wechat_upload($file, $type = 'image', $name = '') {
  228. $harmtype = array('asp', 'php', 'jsp', 'js', 'css', 'php3', 'php4', 'php5', 'ashx', 'aspx', 'exe', 'cgi');
  229. if (empty($file)) {
  230. return error(-1, '没有上传内容');
  231. }
  232. if (!in_array($type, array('image', 'thumb', 'voice', 'video', 'audio'))) {
  233. return error(-2, '未知的上传类型');
  234. }
  235. global $_W;
  236. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  237. $ext = strtolower($ext);
  238. if (in_array(strtolower($ext), $harmtype)) {
  239. return error(-3, '不允许上传此类文件');
  240. }
  241. $result = array();
  242. if (empty($name) || $name == 'auto') {
  243. $uniacid = intval($_W['uniacid']);
  244. $path = "{$type}s/{$uniacid}/" . date('Y/m/');
  245. mkdirs(ATTACHMENT_ROOT . '/' . $path);
  246. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $path, $ext);
  247. $result['path'] = $path . $filename;
  248. } else {
  249. mkdirs(dirname(ATTACHMENT_ROOT . '/' . $name));
  250. if (!strexists($name, $ext)) {
  251. $name .= '.' . $ext;
  252. }
  253. $result['path'] = $name;
  254. }
  255. $save_path = ATTACHMENT_ROOT . '/' . $result['path'];
  256. if (!file_move($file['tmp_name'], $save_path)) {
  257. return error(-1, '保存上传文件失败');
  258. }
  259. if ($type == 'image') {
  260. file_image_quality($save_path, $save_path, $ext);
  261. }
  262. $result['success'] = true;
  263. return $result;
  264. }
  265. function file_remote_upload($filename, $auto_delete_local = true) {
  266. global $_W;
  267. if (empty($_W['setting']['remote']['type'])) {
  268. return false;
  269. }
  270. if ($_W['setting']['remote']['type'] == '1') {
  271. load()->library('ftp');
  272. $ftp_config = array(
  273. 'hostname' => $_W['setting']['remote']['ftp']['host'],
  274. 'username' => $_W['setting']['remote']['ftp']['username'],
  275. 'password' => $_W['setting']['remote']['ftp']['password'],
  276. 'port' => $_W['setting']['remote']['ftp']['port'],
  277. 'ssl' => $_W['setting']['remote']['ftp']['ssl'],
  278. 'passive' => $_W['setting']['remote']['ftp']['pasv'],
  279. 'timeout' => $_W['setting']['remote']['ftp']['timeout'],
  280. 'rootdir' => $_W['setting']['remote']['ftp']['dir'],
  281. );
  282. $ftp = new Ftp($ftp_config);
  283. if (true === $ftp->connect()) {
  284. $response = $ftp->upload(ATTACHMENT_ROOT . '/' . $filename, $filename);
  285. if ($auto_delete_local) {
  286. file_delete($filename);
  287. }
  288. if (!empty($response)) {
  289. return true;
  290. } else {
  291. return error(1, '远程附件上传失败,请检查配置并重新上传');
  292. }
  293. } else {
  294. return error(1, '远程附件上传失败,请检查配置并重新上传');
  295. }
  296. } elseif ($_W['setting']['remote']['type'] == '2') {
  297. load()->library('oss');
  298. load()->model('attachment');
  299. $buckets = attachment_alioss_buctkets($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret']);
  300. $host_name = $_W['setting']['remote']['alioss']['internal'] ? '-internal.aliyuncs.com' : '.aliyuncs.com';
  301. $endpoint = 'http://' . $buckets[$_W['setting']['remote']['alioss']['bucket']]['location'] . $host_name;
  302. try {
  303. $ossClient = new \OSS\OssClient($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $endpoint);
  304. $ossClient->uploadFile($_W['setting']['remote']['alioss']['bucket'], $filename, ATTACHMENT_ROOT . $filename);
  305. } catch (\OSS\Core\OssException $e) {
  306. return error(1, $e->getMessage());
  307. }
  308. if ($auto_delete_local) {
  309. file_delete($filename);
  310. }
  311. } elseif ($_W['setting']['remote']['type'] == '3') {
  312. load()->library('qiniu');
  313. $auth = new Qiniu\Auth($_W['setting']['remote']['qiniu']['accesskey'], $_W['setting']['remote']['qiniu']['secretkey']);
  314. $config = new Qiniu\Config();
  315. $uploadmgr = new Qiniu\Storage\UploadManager($config);
  316. $putpolicy = Qiniu\base64_urlSafeEncode(json_encode(array(
  317. 'scope' => $_W['setting']['remote']['qiniu']['bucket'] . ':' . $filename,
  318. )));
  319. $uploadtoken = $auth->uploadToken($_W['setting']['remote']['qiniu']['bucket'], $filename, 3600, $putpolicy);
  320. list($ret, $err) = $uploadmgr->putFile($uploadtoken, $filename, ATTACHMENT_ROOT . '/' . $filename);
  321. if ($auto_delete_local) {
  322. file_delete($filename);
  323. }
  324. if ($err !== null) {
  325. return error(1, '远程附件上传失败,请检查配置并重新上传');
  326. } else {
  327. return true;
  328. }
  329. } elseif ($_W['setting']['remote']['type'] == '4') {
  330. if (!empty($_W['setting']['remote']['cos']['local'])) {
  331. load()->library('cos');
  332. qcloudcos\Cosapi::setRegion($_W['setting']['remote']['cos']['local']);
  333. $uploadRet = qcloudcos\Cosapi::upload($_W['setting']['remote']['cos']['bucket'], ATTACHMENT_ROOT . $filename, '/' . $filename, '', 3 * 1024 * 1024, 0);
  334. } else {
  335. load()->library('cosv3');
  336. $uploadRet = \Qcloud_cos\Cosapi::upload($_W['setting']['remote']['cos']['bucket'], ATTACHMENT_ROOT . $filename, '/' . $filename, '', 3 * 1024 * 1024, 0);
  337. }
  338. if ($uploadRet['code'] != 0) {
  339. switch ($uploadRet['code']) {
  340. case -62:
  341. $message = '输入的appid有误';
  342. break;
  343. case -79:
  344. $message = '输入的SecretID有误';
  345. break;
  346. case -97:
  347. $message = '输入的SecretKEY有误';
  348. break;
  349. case -166:
  350. $message = '输入的bucket有误';
  351. break;
  352. }
  353. return error(-1, $message);
  354. }
  355. if ($auto_delete_local) {
  356. file_delete($filename);
  357. }
  358. }
  359. }
  360. function file_dir_remote_upload($dir_path, $limit = 50) {
  361. global $_W;
  362. if (empty($_W['setting']['remote']['type'])) {
  363. return error(1, '未开启远程附件');
  364. }
  365. $dir_path = safe_gpc_path($dir_path);
  366. if (!empty($dir_path)) {
  367. $local_attachment = file_tree_limit($dir_path, $limit);
  368. } else {
  369. $local_attachment = array();
  370. }
  371. if (is_array($local_attachment) && !empty($local_attachment)) {
  372. foreach ($local_attachment as $attachment) {
  373. $filename = str_replace(ATTACHMENT_ROOT, '', $attachment);
  374. list($image_dir, $file_account) = explode('/', $filename);
  375. if ($file_account == 'global' || !file_is_image($attachment)) {
  376. continue;
  377. }
  378. if (is_numeric($file_account) && is_dir(ATTACHMENT_ROOT . 'images/' . $file_account) && !empty($_W['setting']['remote_complete_info'][$file_account]['type'])) {
  379. $_W['setting']['remote'] = $_W['setting']['remote_complete_info'][$file_account];
  380. } else {
  381. $_W['setting']['remote'] = $_W['setting']['remote_complete_info'];
  382. }
  383. $result = file_remote_upload($filename);
  384. if (is_error($result)) {
  385. return $result;
  386. }
  387. }
  388. }
  389. return true;
  390. }
  391. function file_random_name($dir, $ext) {
  392. do {
  393. $filename = random(30) . '.' . $ext;
  394. } while (file_exists($dir . $filename));
  395. return $filename;
  396. }
  397. function file_delete($file) {
  398. global $_W;
  399. if (empty($file)) {
  400. return false;
  401. }
  402. if (file_exists(ATTACHMENT_ROOT . '/' . $file) && file_is_uni_attach(ATTACHMENT_ROOT . '/' . $file)) {
  403. file_change_uni_attchsize(ATTACHMENT_ROOT . '/' . $file, false);
  404. }
  405. if (file_exists($file)) {
  406. @unlink($file);
  407. }
  408. if (file_exists(ATTACHMENT_ROOT . '/' . $file)) {
  409. @unlink(ATTACHMENT_ROOT . '/' . $file);
  410. }
  411. return true;
  412. }
  413. function file_remote_delete($file) {
  414. global $_W;
  415. if (empty($file)) {
  416. return true;
  417. }
  418. if ($_W['setting']['remote']['type'] == '1') {
  419. load()->library('ftp');
  420. $ftp_config = array(
  421. 'hostname' => $_W['setting']['remote']['ftp']['host'],
  422. 'username' => $_W['setting']['remote']['ftp']['username'],
  423. 'password' => $_W['setting']['remote']['ftp']['password'],
  424. 'port' => $_W['setting']['remote']['ftp']['port'],
  425. 'ssl' => $_W['setting']['remote']['ftp']['ssl'],
  426. 'passive' => $_W['setting']['remote']['ftp']['pasv'],
  427. 'timeout' => $_W['setting']['remote']['ftp']['timeout'],
  428. 'rootdir' => $_W['setting']['remote']['ftp']['dir'],
  429. );
  430. $ftp = new Ftp($ftp_config);
  431. if (true === $ftp->connect()) {
  432. if ($ftp->delete_file($file)) {
  433. return true;
  434. } else {
  435. return error(1, '删除附件失败,请检查配置并重新删除');
  436. }
  437. } else {
  438. return error(1, '删除附件失败,请检查配置并重新删除');
  439. }
  440. } elseif ($_W['setting']['remote']['type'] == '2') {
  441. load()->model('attachment');
  442. load()->library('oss');
  443. $buckets = attachment_alioss_buctkets($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret']);
  444. $endpoint = 'http://' . $buckets[$_W['setting']['remote']['alioss']['bucket']]['location'] . '.aliyuncs.com';
  445. try {
  446. $ossClient = new \OSS\OssClient($_W['setting']['remote']['alioss']['key'], $_W['setting']['remote']['alioss']['secret'], $endpoint);
  447. $ossClient->deleteObject($_W['setting']['remote']['alioss']['bucket'], $file);
  448. } catch (\OSS\Core\OssException $e) {
  449. return error(1, '删除oss远程文件失败');
  450. }
  451. } elseif ($_W['setting']['remote']['type'] == '3') {
  452. load()->library('qiniu');
  453. $auth = new Qiniu\Auth($_W['setting']['remote']['qiniu']['accesskey'], $_W['setting']['remote']['qiniu']['secretkey']);
  454. $bucketMgr = new Qiniu\Storage\BucketManager($auth);
  455. $error = $bucketMgr->delete($_W['setting']['remote']['qiniu']['bucket'], $file);
  456. if ($error instanceof Qiniu\Http\Error) {
  457. if ($error->code() == 612) {
  458. return true;
  459. }
  460. return error(1, '删除七牛远程文件失败');
  461. } else {
  462. return true;
  463. }
  464. } elseif ($_W['setting']['remote']['type'] == '4') {
  465. $bucketName = $_W['setting']['remote']['cos']['bucket'];
  466. $path = '/' . $file;
  467. if (!empty($_W['setting']['remote']['cos']['local'])) {
  468. load()->library('cos');
  469. qcloudcos\Cosapi::setRegion($_W['setting']['remote']['cos']['local']);
  470. $result = qcloudcos\Cosapi::delFile($bucketName, $path);
  471. } else {
  472. load()->library('cosv3');
  473. $result = Qcloud_cos\Cosapi::delFile($bucketName, $path);
  474. }
  475. if (!empty($result['code'])) {
  476. return error(-1, '删除cos远程文件失败');
  477. } else {
  478. return true;
  479. }
  480. }
  481. return true;
  482. }
  483. function file_image_thumb($srcfile, $desfile = '', $width = 0) {
  484. global $_W;
  485. load()->classs('image');
  486. if (intval($width) == 0) {
  487. load()->model('setting');
  488. $width = intval($_W['setting']['upload']['image']['width']);
  489. }
  490. if (empty($desfile)) {
  491. $ext = pathinfo($srcfile, PATHINFO_EXTENSION);
  492. $srcdir = dirname($srcfile);
  493. do {
  494. $desfile = $srcdir . '/' . random(30) . ".{$ext}";
  495. } while (file_exists($desfile));
  496. }
  497. $des = dirname($desfile);
  498. if (!file_exists($des)) {
  499. if (!mkdirs($des)) {
  500. return error('-1', '创建目录失败');
  501. }
  502. } elseif (!is_writable($des)) {
  503. return error('-1', '目录无法写入');
  504. }
  505. $org_info = @getimagesize($srcfile);
  506. if ($org_info) {
  507. if ($width == 0 || $width > $org_info[0]) {
  508. copy($srcfile, $desfile);
  509. return str_replace(ATTACHMENT_ROOT . '/', '', $desfile);
  510. }
  511. }
  512. $scale_org = $org_info[0] / $org_info[1];
  513. $height = $width / $scale_org;
  514. $desfile = Image::create($srcfile)->resize($width, $height)->saveTo($desfile);
  515. if (!$desfile) {
  516. return false;
  517. }
  518. return str_replace(ATTACHMENT_ROOT . '/', '', $desfile);
  519. }
  520. function file_image_crop($src, $desfile, $width = 400, $height = 300, $position = 1) {
  521. load()->classs('image');
  522. $des = dirname($desfile);
  523. if (!file_exists($des)) {
  524. if (!mkdirs($des)) {
  525. return error('-1', '创建目录失败');
  526. }
  527. } elseif (!is_writable($des)) {
  528. return error('-1', '目录无法写入');
  529. }
  530. return Image::create($src)
  531. ->crop($width, $height, $position)
  532. ->saveTo($desfile);
  533. }
  534. function file_lists($filepath, $subdir = 1, $ex = '', $isdir = 0, $md5 = 0, $enforcement = 0) {
  535. static $file_list = array();
  536. if ($enforcement) {
  537. $file_list = array();
  538. }
  539. $flags = $isdir ? GLOB_ONLYDIR : 0;
  540. $list = glob($filepath . '*' . (!empty($ex) && empty($subdir) ? '.' . $ex : ''), $flags);
  541. if (!empty($ex)) {
  542. $ex_num = strlen($ex);
  543. }
  544. foreach ($list as $k => $v) {
  545. $v = str_replace('\\', '/', $v);
  546. $v1 = str_replace(IA_ROOT . '/', '', $v);
  547. if ($subdir && is_dir($v)) {
  548. file_lists($v . '/', $subdir, $ex, $isdir, $md5);
  549. continue;
  550. }
  551. if (!empty($ex) && strtolower(substr($v, -$ex_num, $ex_num)) == $ex) {
  552. if ($md5) {
  553. $file_list[$v1] = md5_file($v);
  554. } else {
  555. $file_list[] = $v1;
  556. }
  557. continue;
  558. } elseif (!empty($ex) && strtolower(substr($v, -$ex_num, $ex_num)) != $ex) {
  559. unset($list[$k]);
  560. continue;
  561. }
  562. }
  563. return $file_list;
  564. }
  565. function file_remote_attach_fetch($url, $limit = 0, $path = '') {
  566. global $_W;
  567. $url = trim($url);
  568. if (empty($url)) {
  569. return error(-1, '文件地址不存在');
  570. }
  571. load()->func('communication');
  572. $resp = ihttp_get($url);
  573. if (is_error($resp)) {
  574. return error(-1, '提取文件失败, 错误信息: ' . $resp['message']);
  575. }
  576. if (intval($resp['code']) != 200) {
  577. return error(-1, '提取文件失败: 未找到该资源文件.');
  578. }
  579. $ext = $type = '';
  580. switch ($resp['headers']['Content-Type']) {
  581. case 'application/x-jpg':
  582. case 'image/jpg':
  583. case 'image/jpeg':
  584. $ext = 'jpg';
  585. $type = 'images';
  586. break;
  587. case 'image/png':
  588. $ext = 'png';
  589. $type = 'images';
  590. break;
  591. case 'image/gif':
  592. $ext = 'gif';
  593. $type = 'images';
  594. break;
  595. case 'video/mp4':
  596. case 'video/mpeg4':
  597. $ext = 'mp4';
  598. $type = 'videos';
  599. break;
  600. case 'video/x-ms-wmv':
  601. $ext = 'wmv';
  602. $type = 'videos';
  603. break;
  604. case 'audio/mpeg':
  605. $ext = 'mp3';
  606. $type = 'audios';
  607. break;
  608. case 'audio/mp4':
  609. $ext = 'mp4';
  610. $type = 'audios';
  611. break;
  612. case 'audio/x-ms-wma':
  613. $ext = 'wma';
  614. $type = 'audios';
  615. break;
  616. default:
  617. return error(-1, '提取资源失败, 资源文件类型错误.');
  618. break;
  619. }
  620. if (empty($path)) {
  621. $path = $type . "/{$_W['uniacid']}/" . date('Y/m/');
  622. } else {
  623. $path = parse_path($path);
  624. }
  625. if (!$path) {
  626. return error(-1, '提取文件失败: 上传路径配置有误.');
  627. }
  628. if (! is_dir(ATTACHMENT_ROOT . $path)) {
  629. if (! mkdirs(ATTACHMENT_ROOT . $path, 0700, true)) {
  630. return error(-1, '提取文件失败: 权限不足.');
  631. }
  632. }
  633. if (!$limit) {
  634. if ($type == 'images') {
  635. $limit = $_W['setting']['upload']['image']['limit'] * 1024;
  636. } else {
  637. $limit = $_W['setting']['upload']['audio']['limit'] * 1024;
  638. }
  639. } else {
  640. $limit = $limit * 1024;
  641. }
  642. if (intval($resp['headers']['Content-Length']) > $limit) {
  643. return error(-1, '上传的媒体文件过大(' . sizecount($resp['headers']['Content-Length']) . ' > ' . sizecount($limit));
  644. }
  645. $filename = file_random_name(ATTACHMENT_ROOT . $path, $ext);
  646. $pathname = $path . $filename;
  647. $fullname = ATTACHMENT_ROOT . $pathname;
  648. if (file_put_contents($fullname, $resp['content']) == false) {
  649. return error(-1, '提取失败.');
  650. }
  651. return $pathname;
  652. }
  653. function file_is_image($url) {
  654. if (!parse_path($url)) {
  655. return false;
  656. }
  657. $pathinfo = pathinfo($url);
  658. $extension = strtolower($pathinfo['extension']);
  659. return !empty($extension) && in_array($extension, array('jpg', 'jpeg', 'gif', 'png'));
  660. }
  661. function file_image_quality($src, $to_path, $ext) {
  662. load()->classs('image');
  663. global $_W;
  664. $quality = intval($_W['setting']['upload']['image']['zip_percentage']);
  665. if ($quality <= 0 || $quality >= 100) {
  666. return;
  667. }
  668. if (filesize($src) / 1024 > 5120) {
  669. return;
  670. }
  671. $result = Image::create($src, $ext)->saveTo($to_path, $quality);
  672. return $result;
  673. }
  674. function file_is_uni_attach($file) {
  675. global $_W;
  676. if (!is_file($file)) {
  677. return error(-1, '未找到的文件。');
  678. }
  679. return strpos($file, "/{$_W['uniacid']}/") > 0;
  680. }
  681. function file_check_uni_space($file) {
  682. global $_W;
  683. if (!is_file($file)) {
  684. return error(-1, '未找到上传的文件。');
  685. }
  686. if (empty($_W['setting']['remote'][$_W['uniacid']]['type'])) {
  687. $uni_setting = uni_setting_load(array('attachment_limit', 'attachment_size'));
  688. $attachment_limit = intval($uni_setting['attachment_limit']);
  689. if ($attachment_limit == 0) {
  690. $upload = setting_load('upload');
  691. $attachment_limit = empty($upload['upload']['attachment_limit']) ? 0 : intval($upload['upload']['attachment_limit']);
  692. }
  693. if ($attachment_limit > 0) {
  694. $file_size = max(1, round(filesize($file) / 1024));
  695. if (($file_size + $uni_setting['attachment_size']) > ($attachment_limit * 1024)) {
  696. return error(-1, '上传失败,可使用的附件空间不足!');
  697. }
  698. }
  699. }
  700. return true;
  701. }
  702. function file_change_uni_attchsize($file, $is_add = true) {
  703. global $_W;
  704. if (!is_file($file)) {
  705. return error(-1, '未找到的文件。');
  706. }
  707. $file_size = round(filesize($file) / 1024);
  708. $file_size = max(1, $file_size);
  709. $result = true;
  710. if (empty($_W['setting']['remote'][$_W['uniacid']]['type'])) {
  711. $uniacid = pdo_getcolumn('uni_settings', array('uniacid' => $_W['uniacid']), 'uniacid');
  712. if (empty($uniacid)) {
  713. $result = pdo_insert('uni_settings', array('attachment_size' => $file_size, 'uniacid' => $_W['uniacid']));
  714. } else {
  715. if (!$is_add) {
  716. $file_size = -$file_size;
  717. }
  718. $result = pdo_update('uni_settings', array('attachment_size +=' => $file_size), array('uniacid' => $_W['uniacid']));
  719. }
  720. $cachekey = cache_system_key('unisetting', array('uniacid' => $uniacid));
  721. $unisetting = cache_load($cachekey);
  722. $unisetting['attachment_size'] += $file_size;
  723. $unisetting['attachment_size'] = max(0, $unisetting['attachment_size']);
  724. cache_write($cachekey, $unisetting);
  725. }
  726. return $result;
  727. }