SiteService.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * 站点设置.
  4. *
  5. * @author ningque
  6. * @date 2020-01-22 11:36:11
  7. */
  8. declare(strict_types=1);
  9. namespace Modules\Api\Services;
  10. class SiteService
  11. {
  12. public function makeFilename($rootPath, $id = 1, $depth = 2)
  13. {
  14. $filename = '';
  15. for ($i = 1; $i <= $depth; $i++) {
  16. $str = random(2);
  17. $rootPath .= '/' . $str;
  18. $filename .= $str;
  19. }
  20. $filename = $id . $depth . $filename . md5(microtime()) . mt_rand(1000, 9999) . '.mp4';
  21. $this->mkDirs($rootPath);
  22. return [
  23. 'filename' => $filename,
  24. 'path' => $rootPath,
  25. ];
  26. }
  27. public function mkDirs($dir, $mode = 0777)
  28. {
  29. if (is_dir($dir) || @mkdir($dir, $mode)) {
  30. return true;
  31. }
  32. if (!$this->mkDirs(dirname($dir), $mode)) {
  33. return false;
  34. }
  35. return @mkdir($dir, $mode);
  36. }
  37. public function writeLog($filename, $msg)
  38. {
  39. $file = public_path('/logs/' . $filename . date('Ymd') . '.log');
  40. !is_dir(dirname($file)) && mkdir(dirname($file), 0777, true);
  41. $handle = fopen($file, 'a');
  42. flock($handle, LOCK_EX);
  43. fwrite($handle, sprintf("%s\r\n%s\r\n", date('Y-m-d H:i:s', time()), $msg));
  44. flock($handle, LOCK_UN);
  45. fclose($handle);
  46. }
  47. public function encryptImage($cover, $path)
  48. {
  49. $path = '/var/www/resource/wp/ig/' . $path . '/'. date('mdH', time());
  50. $name = $path . '/' .substr(md5(microtime()) . mt_rand(1000, 9999), 0, 16);
  51. $this->mkDirs($path);
  52. $encryptImage = aes_encrypt(file_get_contents($cover), 'wPK8CxWaOwPuVzgs', '', 'AES-128-ECB');
  53. file_put_contents($name. '.ceb', $encryptImage);
  54. file_put_contents($name. '.png', file_get_contents($cover));
  55. $name = str_replace('/var/www/resource/wp', '', $name);
  56. return $name. '.ceb';
  57. }
  58. }