file.func.php 25 KB

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