AutoUpdate.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 18/04/2017
  6. * Time: 09:12
  7. */
  8. namespace app\common\services;
  9. use app\frontend\modules\update\models\authModel;
  10. use Illuminate\Filesystem\Filesystem;
  11. use Ixudra\Curl\Facades\Curl;
  12. use vierbergenlars\SemVer\version;
  13. /**
  14. * Auto update class.
  15. *
  16. * update.json
  17. * {
  18. "0.1.0": "http://domain/server/0.1.0.zip",
  19. "0.2.0": "http://domain/server/0.2.0.zip",
  20. "0.2.1": "http://domain/server/0.2.1.zip"
  21. }
  22. */
  23. class AutoUpdate
  24. {
  25. /**
  26. * The latest version.
  27. *
  28. * @var \vierbergenlars\SemVer\version
  29. */
  30. private $_latestVersion = null;
  31. /**
  32. * Updates not yet installed.
  33. *
  34. * @var array
  35. */
  36. private $_updates = null;
  37. /**
  38. * Cache for update requests.
  39. *
  40. */
  41. private $_cache = null;
  42. /**
  43. * Result of simulated install.
  44. *
  45. * @var array
  46. */
  47. private $_simulationResults = array();
  48. /**
  49. * Temporary download directory.
  50. *
  51. * @var string
  52. */
  53. private $_tempDir = '';
  54. /**
  55. * Install directory.
  56. *
  57. * @var string
  58. */
  59. private $_installDir = '';
  60. /**
  61. * Update branch.
  62. *
  63. * @var string
  64. */
  65. private $_branch = '';
  66. /**
  67. * Url to the update folder on the server.
  68. *
  69. * @var string
  70. */
  71. protected $_updateUrl = 'https://example.com/updates/';
  72. /**
  73. * Version filename on the server.
  74. *
  75. * @var string
  76. */
  77. protected $_updateFile = 'check.json';
  78. /**
  79. * Current version.
  80. *
  81. * @var \vierbergenlars\SemVer\version
  82. */
  83. protected $_currentVersion = null;
  84. /**
  85. * Create new folders with this privileges.
  86. *
  87. * @var int
  88. */
  89. public $dirPermissions = 0755;
  90. /**
  91. * Update script filename.
  92. *
  93. * @var string
  94. */
  95. public $updateScriptName = 'update.php';
  96. /**
  97. * Username authentication
  98. *
  99. * @var string
  100. */
  101. private $_username = '';
  102. /**
  103. * Password authentication
  104. *
  105. * @var string
  106. */
  107. private $_password = '';
  108. /*
  109. * Callbacks to be called when each update is finished
  110. */
  111. private $onEachUpdateFinishCallbacks = [];
  112. /*
  113. * Callbacks to be called when all updates are finished
  114. */
  115. private $onAllUpdateFinishCallbacks = [];
  116. /**
  117. * No update available.
  118. */
  119. const NO_UPDATE_AVAILABLE = 0;
  120. /**
  121. * Zip file could not be opened.
  122. */
  123. const ERROR_INVALID_ZIP = 10;
  124. /**
  125. * Could not check for last version.
  126. */
  127. const ERROR_VERSION_CHECK = 20;
  128. /**
  129. * Temp directory does not exist or is not writable.
  130. */
  131. const ERROR_TEMP_DIR = 30;
  132. /**
  133. * Install directory does not exist or is not writable.
  134. */
  135. const ERROR_INSTALL_DIR = 35;
  136. /**
  137. * Could not download update.
  138. */
  139. const ERROR_DOWNLOAD_UPDATE = 40;
  140. /**
  141. * Could not delete zip update file.
  142. */
  143. const ERROR_DELETE_TEMP_UPDATE = 50;
  144. /**
  145. * Error while installing the update.
  146. */
  147. const ERROR_INSTALL = 60;
  148. /**
  149. * Error in simulated install.
  150. */
  151. const ERROR_SIMULATE = 70;
  152. /**
  153. * Create new instance
  154. *
  155. * @param string $tempDir
  156. * @param string $installDir
  157. * @param int $maxExecutionTime
  158. */
  159. public function __construct($tempDir = null, $installDir = null, $maxExecutionTime = 0)
  160. {
  161. // Init logger
  162. $this->_log = app('log');
  163. $this->setTempDir(($tempDir !== null) ? $tempDir : storage_path('app/auto-update/temp'));
  164. $this->setInstallDir(($installDir !== null) ? $installDir : base_path());
  165. $this->_latestVersion = new version('0.0.0');
  166. $this->_currentVersion = new version('0.0.0');
  167. // Init cache
  168. $this->_cache = app('cache');
  169. ini_set('max_execution_time', $maxExecutionTime);
  170. }
  171. /**
  172. * Set the temporary download directory.
  173. *
  174. * @param string $dir
  175. * @return $this|void
  176. */
  177. public function setTempDir($dir)
  178. {
  179. $dir = $this->addTrailingSlash($dir);
  180. if (!is_dir($dir)) {
  181. $this->_log->debug(sprintf('Creating new temporary directory "%s"', $dir));
  182. if (!mkdir($dir, 0755, true)) {
  183. $this->_log->critical(sprintf('Could not create temporary directory "%s"', $dir));
  184. return;
  185. }
  186. }
  187. $this->_tempDir = $dir;
  188. return $this;
  189. }
  190. /**
  191. * Set the install directory.
  192. *
  193. * @param string $dir
  194. * @return $this|void
  195. */
  196. public function setInstallDir($dir)
  197. {
  198. $dir = $this->addTrailingSlash($dir);
  199. if (!is_dir($dir)) {
  200. $this->_log->debug(sprintf('Creating new install directory "%s"', $dir));
  201. if (!mkdir($dir, 0755, true)) {
  202. $this->_log->critical(sprintf('Could not create install directory "%s"', $dir));
  203. return;
  204. }
  205. }
  206. $this->_installDir = $dir;
  207. return $this;
  208. }
  209. /**
  210. * Set the update filename.
  211. *
  212. * @param string $updateFile
  213. * @return $this
  214. */
  215. public function setUpdateFile($updateFile)
  216. {
  217. $this->_updateFile = $updateFile;
  218. return $this;
  219. }
  220. /**
  221. * Set the update filename.
  222. *
  223. * @param string $updateUrl
  224. * @return $this
  225. */
  226. public function setUpdateUrl($updateUrl)
  227. {
  228. $this->_updateUrl = $updateUrl;
  229. return $this;
  230. }
  231. /**
  232. * Set the update branch.
  233. *
  234. * @param string branch
  235. * @return $this
  236. */
  237. public function setBranch($branch)
  238. {
  239. $this->_branch = $branch;
  240. return $this;
  241. }
  242. /**
  243. * Set the version of the current installed software.
  244. *
  245. * @param string $currentVersion
  246. *
  247. * @return bool
  248. */
  249. public function setCurrentVersion($currentVersion)
  250. {
  251. $version = new version($currentVersion);
  252. if ($version->valid() === null) {
  253. $this->_log->error(sprintf('Invalid current version "%s"', $currentVersion));
  254. return false;
  255. }
  256. $this->_currentVersion = $version;
  257. return $this;
  258. }
  259. /**
  260. * Set authentication
  261. * @param $username
  262. * @param $password
  263. */
  264. public function setBasicAuth($username, $password)
  265. {
  266. $this->_username = $username;
  267. $this->_password = $password;
  268. }
  269. /**
  270. * Set authentication in update method of users and password exist
  271. * @return null|resource
  272. */
  273. private function _useBasicAuth()
  274. {
  275. if ($this->_username && $this->_password) {
  276. return stream_context_create(array(
  277. 'http' => array(
  278. 'header' => "Authorization: Basic " . base64_encode("$this->_username:$this->_password")
  279. )
  280. ));
  281. }
  282. return null;
  283. }
  284. /**
  285. * Get the name of the latest version.
  286. *
  287. * @return \vierbergenlars\SemVer\version
  288. */
  289. public function getLatestVersion()
  290. {
  291. return $this->_latestVersion;
  292. }
  293. public function getUpdates()
  294. {
  295. return $this->_updates;
  296. }
  297. /**
  298. * Get an array of versions which will be installed.
  299. *
  300. * @return array
  301. */
  302. public function getVersionsToUpdate()
  303. {
  304. return array_map(function ($update) {
  305. return $update['version'];
  306. }, $this->_updates);
  307. }
  308. /**
  309. * Get the results of the last simulation.
  310. *
  311. * @return array
  312. */
  313. public function getSimulationResults()
  314. {
  315. return $this->_simulationResults;
  316. }
  317. /**
  318. * Remove directory recursively.
  319. *
  320. * @param string $dir
  321. *
  322. * @return void
  323. */
  324. private function _removeDir($dir)
  325. {
  326. $this->_log->debug(sprintf('Remove directory "%s"', $dir));
  327. if (!is_dir($dir)) {
  328. $this->_log->warning(sprintf('"%s" is not a directory!', $dir));
  329. return false;
  330. }
  331. $objects = array_diff(scandir($dir), array('.', '..'));
  332. foreach ($objects as $object) {
  333. if (is_dir($dir . DIRECTORY_SEPARATOR . $object))
  334. $this->_removeDir($dir . DIRECTORY_SEPARATOR . $object);
  335. else
  336. unlink($dir . DIRECTORY_SEPARATOR . $object);
  337. }
  338. return rmdir($dir);
  339. }
  340. /**
  341. * Check for a new version
  342. *
  343. * @return int|bool
  344. * true: New version is available
  345. * false: Error while checking for update
  346. * int: Status code (i.e. AutoUpdate::NO_UPDATE_AVAILABLE)
  347. */
  348. public function checkUpdate()
  349. {
  350. $this->_log->notice('Checking for a new update...');
  351. // Reset previous updates
  352. $this->_latestVersion = new version('0.0.0');
  353. $this->_updates = [];
  354. $versions = null; // $this->_cache->get('update-versions');
  355. // Create absolute url to update file
  356. $updateFile = $this->_updateUrl . '/' . $this->_updateFile;
  357. if (!empty($this->_branch))
  358. $updateFile .= '.' . $this->_branch;
  359. // Check if cache is empty
  360. if ($versions === null || $versions === false) {
  361. $this->_log->debug(sprintf('Get new updates from %s', $updateFile));
  362. // Read update file from update server
  363. //$update = @file_get_contents($updateFile, $this->_useBasicAuth());
  364. $data = [
  365. 'domain' => request()->getHost()
  366. ];
  367. $update = Curl::to($updateFile)
  368. ->withHeader(
  369. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  370. )
  371. ->withData($data)
  372. ->get();
  373. if ($update === false) {
  374. $this->_log->info(sprintf('Could not download update file "%s"!', $updateFile));
  375. return false;
  376. }
  377. // Parse update file
  378. $updateFileExtension = substr(strrchr($this->_updateFile, '.'), 1);
  379. switch ($updateFileExtension) {
  380. case 'ini':
  381. $versions = @parse_ini_string($update, true);
  382. if (!is_array($versions)) {
  383. $this->_log->error('Unable to parse ini update file!');
  384. return false;
  385. }
  386. $versions = array_map(function ($block) {
  387. return isset($block['url']) ? $block['url'] : false;
  388. }, $versions);
  389. break;
  390. case 'json':
  391. $versions = (array)@json_decode($update);
  392. if (!is_array($versions)) {
  393. $this->_log->error('Unable to parse json update file!');
  394. return false;
  395. }
  396. if (isset($versions['result']) && 0 == $versions['result']) {
  397. return $versions;
  398. }
  399. break;
  400. default:
  401. $this->_log->error(sprintf('Unknown file extension "%s"', $updateFileExtension));
  402. return false;
  403. }
  404. $this->_cache->put('update-versions', $versions);
  405. } else {
  406. $this->_log->debug('Got updates from cache');
  407. }
  408. if (!is_array($versions)) {
  409. $this->_log->error(sprintf('Could not read versions from server %s', $updateFile));
  410. return false;
  411. }
  412. // Check for latest version
  413. foreach ($versions as $versionRaw => $updateUrl) {
  414. // $this->checkDomain($updateUrl->domain);
  415. $version = new version($versionRaw);
  416. if ($version->valid() === null) {
  417. $this->_log->info(sprintf('Could not parse version "%s" from update server "%s"', $versionRaw, $updateFile));
  418. continue;
  419. }
  420. if (version::gt($version, $this->_currentVersion)) {
  421. if (version::gt($version, $this->_latestVersion))
  422. $this->_latestVersion = $version;
  423. $this->_updates[] = [
  424. 'version' => $version->getVersion(),
  425. 'url' => $updateUrl->url,
  426. 'description' => $updateUrl->description,
  427. 'created_at' => strtotime($updateUrl->created_at->date),
  428. 'upgrade' => $updateUrl->upgrade,
  429. 'php_version' => $updateUrl->php_version
  430. ];
  431. }
  432. }
  433. // Sort versions to install
  434. usort($this->_updates, function ($a, $b) {
  435. return version::compare($a['version'], $b['version']);
  436. });
  437. if ($this->newVersionAvailable()) {
  438. $this->_log->debug(sprintf('New version "%s" available', $this->_latestVersion));
  439. return true;
  440. } else {
  441. $this->_log->debug('No new version available');
  442. return self::NO_UPDATE_AVAILABLE;
  443. }
  444. }
  445. /*
  446. * 检测指定的 key 和 密钥是否存在
  447. *
  448. *
  449. * @params string $fileName 检查路径
  450. * @params array $keyAndSecret ['key' => string, 'secret' => string]
  451. * @params array $postData post 传参
  452. *
  453. * @return mixed
  454. */
  455. public function isKeySecretExists($fileName, $keyAndSecret, $postData, $message='') {
  456. //dd($fileName . "<br/>" . $keyAndSecret['key'] . '==> ' . $keyAndSecret['secret'] . '<br/>' . $postData . "<br/>" . $message);
  457. $content = Curl::to($fileName)
  458. ->withHeader(
  459. "Authorization: Basic " . base64_encode("{$keyAndSecret['key']}:{$keyAndSecret['secret']}")
  460. )
  461. ->withData($postData)
  462. ->get();
  463. //var_dump($content);exit();
  464. $result = json_decode($content, true);
  465. if(!$result['isExists']) {
  466. $this->_log->error($message . $result['message']);
  467. }
  468. return $result;
  469. }
  470. /**
  471. * Check if a new version is available.
  472. *
  473. * @return bool
  474. */
  475. public function newVersionAvailable()
  476. {
  477. if (!empty($this->_updates) && $this->_updates['upgrade'] != 'master') {
  478. return true;
  479. }
  480. return version::gt($this->_latestVersion, $this->_currentVersion);
  481. }
  482. /**
  483. * Download the update
  484. *
  485. * @param string $updateUrl Url where to download from
  486. * @param string $updateFile Path where to save the download
  487. *
  488. * @return bool
  489. */
  490. protected function _downloadUpdate($updateUrl, $updateFile)
  491. {
  492. $this->_log->info(sprintf('Downloading update "%s" to "%s"', $updateUrl, $updateFile));
  493. return Curl::to($updateUrl)
  494. ->withHeader(
  495. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  496. )
  497. ->withContentType('application/zip, application/octet-stream')
  498. ->withOption('FOLLOWLOCATION',true)
  499. ->withOption('TIMEOUT',100)
  500. ->download($updateFile);
  501. }
  502. protected function _downloadUpdate_v2($updateUrl, $updateFile, $client)
  503. {
  504. $this->_log->info(sprintf('Downloading update "%s" to "%s"', $updateUrl, $updateFile));
  505. //获取文件夹数据
  506. $checkUpdateFileUurl = $updateUrl . '/check/' . $client . '/0';
  507. $files = Curl::to($checkUpdateFileUurl)
  508. ->withHeader(
  509. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  510. )
  511. ->asJsonResponse(true)
  512. ->get();
  513. if (!is_null($files) && !empty($files['result'])) {
  514. $downloadUrl = $updateUrl . '/download/';
  515. foreach ($files['result'] as $item) {
  516. $updateUrl = $downloadUrl . $client . '/' . $item;
  517. $updateFile = $this->_tempDir . $item;
  518. Curl::to($updateUrl)
  519. ->withHeader(
  520. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  521. )
  522. ->withContentType('application/zip, application/octet-stream')
  523. ->withOption('FOLLOWLOCATION',true)
  524. ->withOption('TIMEOUT',100)
  525. ->download($updateFile);
  526. }
  527. }
  528. return true;
  529. }
  530. /**
  531. * Simulate update process.
  532. *
  533. * @param string $updateFile
  534. *
  535. * @return bool
  536. */
  537. protected function _simulateInstall($updateFile)
  538. {
  539. $this->_log->notice('[SIMULATE] Install new version');
  540. clearstatcache();
  541. // Check if zip file could be opened
  542. $zip = zip_open($updateFile);
  543. if (!is_resource($zip)) {
  544. $this->_log->error(sprintf('Could not open zip file "%s", error: %d', $updateFile, $zip));
  545. return false;
  546. }
  547. $i = -1;
  548. $files = [];
  549. $simulateSuccess = true;
  550. while ($file = zip_read($zip)) {
  551. $i++;
  552. $filename = zip_entry_name($file);
  553. $foldername = $this->_installDir . dirname($filename);
  554. $absoluteFilename = $this->_installDir . $filename;
  555. $files[$i] = [
  556. 'filename' => $filename,
  557. 'foldername' => $foldername,
  558. 'absolute_filename' => $absoluteFilename,
  559. ];
  560. $this->_log->debug(sprintf('[SIMULATE] Updating file "%s"', $filename));
  561. // Check if parent directory is writable
  562. if (!is_dir($foldername)) {
  563. $this->_log->debug(sprintf('[SIMULATE] Create directory "%s"', $foldername));
  564. $files[$i]['parent_folder_exists'] = false;
  565. $parent = dirname($foldername);
  566. if(!is_dir($parent)){
  567. if (!mkdir($parent, $this->dirPermissions, true)) {
  568. $files[$i]['parent_folder_writable'] = false;
  569. $simulateSuccess = false;
  570. $this->_log->error(sprintf('Directory "%s" has to be writeable!', $parent));
  571. }
  572. }
  573. if (!is_writable($parent)) {
  574. $files[$i]['parent_folder_writable'] = false;
  575. $simulateSuccess = false;
  576. $this->_log->warning(sprintf('[SIMULATE] Directory "%s" has to be writeable!', $parent));
  577. } else {
  578. $files[$i]['parent_folder_writable'] = true;
  579. }
  580. }
  581. // Skip if entry is a directory
  582. if (substr($filename, -1, 1) == DIRECTORY_SEPARATOR || substr($filename, -1, 1) == '.')
  583. continue;
  584. // Read file contents from archive
  585. $contents = zip_entry_read($file, zip_entry_filesize($file));
  586. if ($contents === false) {
  587. $files[$i]['extractable'] = false;
  588. $simulateSuccess = false;
  589. $this->_log->warning(sprintf('[SIMULATE] Coud not read contents of file "%s" from zip file!', $filename));
  590. }
  591. // Write to file
  592. if (file_exists($absoluteFilename)) {
  593. $files[$i]['file_exists'] = true;
  594. if (!is_writable($absoluteFilename)) {
  595. $files[$i]['file_writable'] = false;
  596. $simulateSuccess = false;
  597. $this->_log->warning(sprintf('[SIMULATE] Could not overwrite "%s"!', $absoluteFilename));
  598. }
  599. } else {
  600. $files[$i]['file_exists'] = false;
  601. if (is_dir($foldername)) {
  602. if (!is_writable($foldername)) {
  603. $files[$i]['file_writable'] = false;
  604. $simulateSuccess = false;
  605. $this->_log->warning(sprintf('[SIMULATE] The file "%s" could not be created!', $absoluteFilename));
  606. } else {
  607. $files[$i]['file_writable'] = true;
  608. }
  609. } else {
  610. $files[$i]['file_writable'] = true;
  611. $this->_log->debug(sprintf('[SIMULATE] The file "%s" could be created', $absoluteFilename));
  612. }
  613. }
  614. if ($filename == $this->updateScriptName) {
  615. $this->_log->debug(sprintf('[SIMULATE] Update script "%s" found', $absoluteFilename));
  616. $files[$i]['update_script'] = true;
  617. } else {
  618. $files[$i]['update_script'] = false;
  619. }
  620. }
  621. $this->_simulationResults = $files;
  622. return $simulateSuccess;
  623. }
  624. protected function _simulateInstall_v2($updateFile, $version, $client = 1)
  625. {
  626. $this->_log->notice('[SIMULATE] Install new version');
  627. clearstatcache();
  628. // Check if zip file could be opened
  629. $dir = $this->_tempDir . $version;
  630. if (!is_dir($dir)) {
  631. $this->_log->error(sprintf('Could not open dir "%s", error: %d', $version, $dir));
  632. return false;
  633. }
  634. $files = [];
  635. $simulateSuccess = true;
  636. if (is_dir($dir)) {
  637. $allfiles = app(Filesystem::class)->allFiles($dir);
  638. foreach ($allfiles as $key => $rows) {
  639. $filename = $rows->getRelativePathname();
  640. $foldername = $this->_installDir . dirname($filename);
  641. $absoluteFilename = $this->_installDir . $filename;
  642. if (2 == $client || (1 == $client && config('app.framework') == 'platform')) {
  643. $foldername = $this->_installDir . 'addons/yun_shop/' . dirname($filename);
  644. $absoluteFilename = $this->_installDir . 'addons/yun_shop/' . $filename;
  645. }
  646. $files[$key] = [
  647. 'filename' => $filename,
  648. 'foldername' => $foldername,
  649. 'absolute_filename' => $absoluteFilename,
  650. ];
  651. $this->_log->debug(sprintf('[SIMULATE] Updating file "%s"', $filename));
  652. // Check if parent directory is writable
  653. if (!is_dir($foldername)) {
  654. $this->_log->debug(sprintf('[SIMULATE] Create directory "%s"', $foldername));
  655. $files[$key]['parent_folder_exists'] = false;
  656. $parent = dirname($foldername);
  657. if(!is_dir($parent)){
  658. if (!mkdir($parent, $this->dirPermissions, true)) {
  659. $files[$key]['parent_folder_writable'] = false;
  660. $simulateSuccess = false;
  661. $this->_log->error(sprintf('Directory "%s" has to be writeable!', $parent));
  662. }
  663. }
  664. if (!is_writable($parent)) {
  665. $files[$key]['parent_folder_writable'] = false;
  666. $simulateSuccess = false;
  667. $this->_log->warning(sprintf('[SIMULATE] Directory "%s" has to be writeable!', $parent));
  668. } else {
  669. $files[$key]['parent_folder_writable'] = true;
  670. }
  671. }
  672. // Skip if entry is a directory
  673. if (substr($filename, -1, 1) == DIRECTORY_SEPARATOR || substr($filename, -1, 1) == '.')
  674. continue;
  675. // Read file contents from archive
  676. $contents = file_get_contents($rows->getPathname());
  677. if ($contents === false) {
  678. $files[$key]['extractable'] = false;
  679. $simulateSuccess = false;
  680. $this->_log->warning(sprintf('[SIMULATE] Coud not read contents of file "%s" from zip file!', $filename));
  681. }
  682. // Write to file
  683. if (file_exists($absoluteFilename)) {
  684. $files[$key]['file_exists'] = true;
  685. if (!is_writable($absoluteFilename)) {
  686. $files[$key]['file_writable'] = false;
  687. $simulateSuccess = false;
  688. $this->_log->warning(sprintf('[SIMULATE] Could not overwrite "%s"!', $absoluteFilename));
  689. }
  690. } else {
  691. $files[$key]['file_exists'] = false;
  692. if (is_dir($foldername)) {
  693. if (!is_writable($foldername)) {
  694. $files[$key]['file_writable'] = false;
  695. $simulateSuccess = false;
  696. $this->_log->warning(sprintf('[SIMULATE] The file "%s" could not be created!', $absoluteFilename));
  697. } else {
  698. $files[$key]['file_writable'] = true;
  699. }
  700. } else {
  701. $files[$key]['file_writable'] = true;
  702. $this->_log->debug(sprintf('[SIMULATE] The file "%s" could be created', $absoluteFilename));
  703. }
  704. }
  705. if ($filename == $this->updateScriptName) {
  706. $this->_log->debug(sprintf('[SIMULATE] Update script "%s" found', $absoluteFilename));
  707. $files[$key]['update_script'] = true;
  708. } else {
  709. $files[$key]['update_script'] = false;
  710. }
  711. }
  712. }
  713. $this->_simulationResults = $files;
  714. return $simulateSuccess;
  715. }
  716. /**
  717. * Install update.
  718. *
  719. * @param string $updateFile Path to the update file
  720. * @param bool $simulateInstall Check for directory and file permissions before copying files
  721. *
  722. * @return bool
  723. */
  724. protected function _install($updateFile, $simulateInstall, $version)
  725. {
  726. $this->_log->notice(sprintf('Trying to install update "%s"', $updateFile));
  727. // Check if install should be simulated
  728. if ($simulateInstall && !$this->_simulateInstall($updateFile)) {
  729. $this->_log->critical('Simulation of update process failed!');
  730. return self::ERROR_SIMULATE;
  731. }
  732. clearstatcache();
  733. // Check if zip file could be opened
  734. $zip = zip_open($updateFile);
  735. if (!is_resource($zip)) {
  736. $this->_log->error(sprintf('Could not open zip file "%s", error: %d', $updateFile, $zip));
  737. return false;
  738. }
  739. // Read every file from archive
  740. while ($file = zip_read($zip)) {
  741. $filename = zip_entry_name($file);
  742. $foldername = $this->_installDir . dirname($filename);
  743. $absoluteFilename = $this->_installDir . $filename;
  744. $this->_log->debug(sprintf('Updating file "%s"', $filename));
  745. if (!is_dir($foldername)) {
  746. if (!mkdir($foldername, $this->dirPermissions, true)) {
  747. $this->_log->error(sprintf('Directory "%s" has to be writeable!', $foldername));
  748. return false;
  749. }
  750. }
  751. // Skip if entry is a directory
  752. if (substr($filename, -1, 1) == '/' || substr($filename, -1, 1) == '\\' || substr($filename, -1, 1) == '.')
  753. continue;
  754. // Read file contents from archive
  755. $contents = zip_entry_read($file, zip_entry_filesize($file));
  756. if ($contents === false) {
  757. $this->_log->error(sprintf('Coud not read zip entry "%s"', $file));
  758. continue;
  759. }
  760. // Write to file
  761. if (file_exists($absoluteFilename)) {
  762. if (!is_writable($absoluteFilename)) {
  763. $this->_log->error('Could not overwrite "%s"!', $absoluteFilename);
  764. zip_close($zip);
  765. return false;
  766. }
  767. } else {
  768. if (!touch($absoluteFilename)) {
  769. $this->_log->error(sprintf('[SIMULATE] The file "%s" could not be created!', $absoluteFilename));
  770. zip_close($zip);
  771. return false;
  772. }
  773. $this->_log->debug(sprintf('File "%s" created', $absoluteFilename));
  774. }
  775. $updateHandle = @fopen($absoluteFilename, 'w');
  776. if (!$updateHandle) {
  777. $this->_log->error(sprintf('Could not open file "%s"!', $absoluteFilename));
  778. zip_close($zip);
  779. return false;
  780. }
  781. if (!empty($contents) && !fwrite($updateHandle, $contents)) {
  782. $this->_log->error(sprintf('Could not write to file "%s"!', $absoluteFilename));
  783. zip_close($zip);
  784. return false;
  785. }
  786. fclose($updateHandle);
  787. //If file is a update script, include
  788. if ($filename == $this->updateScriptName) {
  789. $this->_log->debug(sprintf('Try to include update script "%s"', $absoluteFilename));
  790. require($absoluteFilename);
  791. $this->_log->info(sprintf('Update script "%s" included!', $absoluteFilename));
  792. if (!unlink($absoluteFilename)) {
  793. $this->_log->warning(sprintf('Could not delete update script "%s"!', $absoluteFilename));
  794. }
  795. }
  796. }
  797. zip_close($zip);
  798. // TODO
  799. $this->_log->notice(sprintf('Update "%s" successfully installed', $version));
  800. return true;
  801. }
  802. protected function _install_v2($updateFile, $simulateInstall, $version, $client = 1)
  803. {
  804. $this->_log->notice(sprintf('Trying to install update "%s"', $updateFile));
  805. // Check if install should be simulated
  806. if ($simulateInstall && !$this->_simulateInstall_v2($updateFile, $version, $client)) {
  807. $this->_log->critical('Simulation of update process failed!');
  808. return self::ERROR_SIMULATE;
  809. }
  810. clearstatcache();
  811. // Check if zip file could be opened
  812. $dir = $this->_tempDir . $version;
  813. if (is_dir($dir)) {
  814. $allfiles = app(Filesystem::class)->allFiles($dir);
  815. foreach ($allfiles as $rows) {
  816. $filename = $rows->getRelativePathname();
  817. $foldername = $this->_installDir . dirname($filename);
  818. $absoluteFilename = $this->_installDir . $filename;
  819. if (2 == $client || (1 == $client && config('app.framework') == 'platform')) {
  820. $foldername = $this->_installDir . 'addons/yun_shop/' . dirname($filename);
  821. $absoluteFilename = $this->_installDir . 'addons/yun_shop/' . $filename;
  822. }
  823. $this->_log->debug(sprintf('Updating file "%s"', $filename));
  824. if (!is_dir($foldername)) {
  825. if (!mkdir($foldername, $this->dirPermissions, true)) {
  826. $this->_log->error(sprintf('Directory "%s" has to be writeable!', $foldername));
  827. return false;
  828. }
  829. }
  830. // Skip if entry is a directory
  831. if (substr($filename, -1, 1) == '/' || substr($filename, -1, 1) == '\\' || substr($filename, -1, 1) == '.')
  832. continue;
  833. // Read file contents from archive
  834. $contents = file_get_contents($rows->getPathname());
  835. if ($contents === false) {
  836. $this->_log->error(sprintf('Coud not read zip entry "%s"', $filename));
  837. continue;
  838. }
  839. // Write to file
  840. if (file_exists($absoluteFilename)) {
  841. if (!is_writable($absoluteFilename)) {
  842. $this->_log->error('Could not overwrite "%s"!', $absoluteFilename);
  843. return false;
  844. }
  845. } else {
  846. if (!touch($absoluteFilename)) {
  847. $this->_log->error(sprintf('[SIMULATE] The file "%s" could not be created!', $absoluteFilename));
  848. return false;
  849. }
  850. $this->_log->debug(sprintf('File "%s" created', $absoluteFilename));
  851. }
  852. $updateHandle = @fopen($absoluteFilename, 'w');
  853. if (!$updateHandle) {
  854. $this->_log->error(sprintf('Could not open file "%s"!', $absoluteFilename));
  855. return false;
  856. }
  857. if (!empty($contents) && !fwrite($updateHandle, $contents)) {
  858. $this->_log->error(sprintf('Could not write to file "%s"!', $absoluteFilename));
  859. return false;
  860. }
  861. fclose($updateHandle);
  862. //If file is a update script, include
  863. if ($filename == $this->updateScriptName) {
  864. $this->_log->debug(sprintf('Try to include update script "%s"', $absoluteFilename));
  865. require($absoluteFilename);
  866. $this->_log->info(sprintf('Update script "%s" included!', $absoluteFilename));
  867. if (!unlink($absoluteFilename)) {
  868. $this->_log->warning(sprintf('Could not delete update script "%s"!', $absoluteFilename));
  869. }
  870. }
  871. }
  872. }
  873. // TODO
  874. $this->_log->notice(sprintf('Update "%s" successfully installed', $version));
  875. return true;
  876. }
  877. /**
  878. * Update to the latest version
  879. *
  880. * @param bool $simulateInstall Check for directory and file permissions before copying files (Default: true)
  881. * @param bool $deleteDownload Delete download after update (Default: true)
  882. *
  883. * @return mixed integer|bool
  884. */
  885. public function update($client = 1, $simulateInstall = true, $deleteDownload = true)
  886. {
  887. $this->_log->info('Trying to perform update');
  888. // Check for latest version
  889. if ($this->_latestVersion === null || count($this->_updates) === 0)
  890. $client == 2 ? $this->checkBackUpdate() : $this->checkUpdate();
  891. if ($this->_latestVersion === null || count($this->_updates) === 0) {
  892. $this->_log->error('Could not get latest version from server!');
  893. return self::ERROR_VERSION_CHECK;
  894. }
  895. // Check if current version is up to date
  896. if (!$this->newVersionAvailable()) {
  897. $this->_log->warning('No update available!');
  898. return self::NO_UPDATE_AVAILABLE;
  899. }
  900. rsort($this->_updates);
  901. foreach ($this->_updates as $key => $update) {
  902. if ($key > 0) {
  903. break;
  904. }
  905. $this->_log->debug(sprintf('Update to version "%s"', $update['version']));
  906. // Check for temp directory
  907. if (empty($this->_tempDir) || !is_dir($this->_tempDir) || !is_writable($this->_tempDir)) {
  908. $this->_log->critical(sprintf('Temporary directory "%s" does not exist or is not writeable!', $this->_tempDir));
  909. return self::ERROR_TEMP_DIR;
  910. }
  911. // Check for install directory
  912. if (empty($this->_installDir) || !is_dir($this->_installDir) || !is_writable($this->_installDir)) {
  913. $this->_log->critical(sprintf('Install directory "%s" does not exist or is not writeable!', $this->_installDir));
  914. return self::ERROR_INSTALL_DIR;
  915. }
  916. $updateFile = $this->_tempDir . $update['version'] . '.zip';
  917. // Download update
  918. if (!is_file($updateFile)) {
  919. if (!$this->_downloadUpdate_v2($update['url'], $updateFile, $client)) {
  920. $this->_log->critical(sprintf('Failed to download update from "%s" to "%s"!', $update['url'], $updateFile));
  921. return self::ERROR_DOWNLOAD_UPDATE;
  922. }
  923. $this->_log->debug(sprintf('Latest update downloaded to "%s"', $updateFile));
  924. } else {
  925. $this->_log->info(sprintf('Latest update already downloaded to "%s"', $updateFile));
  926. }
  927. //下载文件MD5校验
  928. if (file_exists($this->_tempDir . 'md5.txt')) {
  929. $error = [];
  930. $md5 = file_get_contents($this->_tempDir . 'md5.txt');
  931. $segment = explode(PHP_EOL, $md5);
  932. foreach ($segment as $val) {
  933. if (!empty($val)) {
  934. $item = str_replace("\r", '', $val);
  935. $rows = explode(':', $item);
  936. $file_md5[$rows[0]] = $rows[1];
  937. }
  938. }
  939. $allfiles = app(Filesystem::class)->allFiles($this->_tempDir);
  940. if (empty($allfiles)) {
  941. return '更新文件不存在';
  942. }
  943. foreach ($allfiles as $file) {
  944. $soure_file[$file->getFilename()] = md5_file($file->getRealPath());
  945. }
  946. foreach ($file_md5 as $k => $v) {
  947. if (!is_null($v) && !empty($soure_file[$k]) && $v != $soure_file[$k]) {
  948. $error[$k] = ['source' => $v, 'destination' => $soure_file[$k]];
  949. }
  950. }
  951. $this->_log->debug('Download zip file successfull');
  952. if (empty($error)) {
  953. $default_dir = $client == 2 ? 'framework_frontend' :'frontend';
  954. // Install update
  955. $yZip = new YZip();
  956. $yZip->unzip($this->_tempDir, $this->_tempDir);
  957. $chk_url = substr(config('auto-update.checkUrl'), strpos(config('auto-update.checkUrl'), '/')+2);
  958. $chk_url = substr($chk_url, 0, strpos($chk_url, '/'));
  959. $cp_source_path = 'app/auto-update/temp/data/wwwroot/' . $chk_url . '/storage/' . $update['upgrade'] . '/' . $default_dir . '/' . $update['version'] . '_source/';
  960. $cp_destination_path = 'app/auto-update/temp/' . $update['version'] . '/';
  961. if (2 == $client) {
  962. //copy 框架index.html到根目录后并删除
  963. if (is_dir(storage_path($cp_source_path))) {
  964. $cp_index_path = $cp_source_path . '/index/admin.html';
  965. $cp_des_path = base_path() . '/admin.html';
  966. app(Filesystem::class)->copy(storage_path($cp_index_path), $cp_des_path);
  967. app(Filesystem::class)->delete(storage_path($cp_index_path));
  968. app(Filesystem::class)->deleteDirectory(storage_path($cp_index_path . '/index'));
  969. }
  970. }
  971. if (is_dir(storage_path($cp_source_path))) {
  972. \Log::debug('copy file start.....', $cp_source_path);
  973. app(Filesystem::class)->copyDirectory(storage_path($cp_source_path),
  974. storage_path($cp_destination_path));
  975. app(Filesystem::class)->copy(storage_path($cp_destination_path . '/index/index.html'), storage_path($cp_destination_path . '/index.html'));
  976. app(Filesystem::class)->delete(storage_path($cp_destination_path . '/index/index.html'));
  977. app(Filesystem::class)->deleteDirectory(storage_path($cp_destination_path . '/index'));
  978. \Log::debug('copy file end.....');
  979. }
  980. $result = $this->_install_v2($updateFile, $simulateInstall, $update['version'], $client);
  981. if ($result === true) {
  982. $this->runOnEachUpdateFinishCallbacks($update['version']);
  983. if ($deleteDownload) {
  984. $this->_log->debug(sprintf('Trying to delete update file "%s" after successfull update', $updateFile));
  985. if (@$this->deldir($this->_tempDir)) {
  986. $this->_log->info(sprintf('Update file "%s" deleted after successfull update', $updateFile));
  987. } else {
  988. $this->_log->error(sprintf('Could not delete update file "%s" after successfull update!', $updateFile));
  989. return self::ERROR_DELETE_TEMP_UPDATE;
  990. }
  991. }
  992. } else {
  993. if ($deleteDownload) {
  994. $this->_log->debug(sprintf('Trying to delete update file "%s" after failed update', $updateFile));
  995. if (@$this->deldir($this->_tempDir)) {
  996. $this->_log->info(sprintf('Update file "%s" deleted after failed update', $updateFile));
  997. } else {
  998. $this->_log->error(sprintf('Could not delete update file "%s" after failed update!', $updateFile));
  999. }
  1000. }
  1001. return $result;
  1002. }
  1003. $this->runOnAllUpdateFinishCallbacks($this->getVersionsToUpdate());
  1004. return true;
  1005. } else {
  1006. \Log::debug('-----下载文件校验失败-----', $error);
  1007. return '下载文件校验失败';
  1008. }
  1009. } else {
  1010. return '校验文件不存在';
  1011. }
  1012. }
  1013. }
  1014. /**
  1015. * Add slash at the end of the path.
  1016. *
  1017. * @param string $dir
  1018. * @return string
  1019. */
  1020. public function addTrailingSlash($dir)
  1021. {
  1022. if (substr($dir, -1) != DIRECTORY_SEPARATOR)
  1023. $dir = $dir . DIRECTORY_SEPARATOR;
  1024. return $dir;
  1025. }
  1026. /**
  1027. * @param array $callback
  1028. */
  1029. public function onEachUpdateFinish($callback)
  1030. {
  1031. $this->onEachUpdateFinishCallbacks[] = $callback;
  1032. }
  1033. /**
  1034. * @param array $callback
  1035. */
  1036. public function setOnAllUpdateFinishCallbacks($callback)
  1037. {
  1038. $this->onAllUpdateFinishCallbacks[] = $callback;
  1039. }
  1040. public function runOnEachUpdateFinishCallbacks($updateVersion)
  1041. {
  1042. foreach ($this->onEachUpdateFinishCallbacks as $callback) {
  1043. call_user_func($callback, $updateVersion);
  1044. }
  1045. }
  1046. public function runOnAllUpdateFinishCallbacks($updatedVersions)
  1047. {
  1048. foreach ($this->onAllUpdateFinishCallbacks as $callback) {
  1049. call_user_func($callback, $updatedVersions);
  1050. }
  1051. }
  1052. public function checkBackUpdate()
  1053. {
  1054. $domain = rtrim(request()->getHost(), '/');
  1055. $this->authCode($domain);
  1056. $code = authModel::orderBy('id', 'desc')->value('code');
  1057. $this->_log->notice('Back Checking for a new update...');
  1058. $versions = null; // $this->_cache->get('update-versions');
  1059. // Create absolute url to update file
  1060. $updateFile = $this->_updateUrl . '/' . $this->_updateFile . '/' . $code;
  1061. // Check if cache is empty
  1062. if ($versions === null || $versions === false) {
  1063. $this->_log->debug(sprintf('Get new updates from %s', $updateFile));
  1064. // Read update file from update server
  1065. //$update = @file_get_contents($updateFile, $this->_useBasicAuth());
  1066. $data = [
  1067. 'vendor' => $this->getDirsByPath('vendor'),
  1068. 'domain' => $domain
  1069. ];
  1070. $update = Curl::to($updateFile)
  1071. ->withHeader(
  1072. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  1073. )
  1074. ->withData($data)
  1075. ->asJsonResponse(true)
  1076. ->get();
  1077. if (is_array($update)) {
  1078. // return $this->checkDomain($update->domain);
  1079. }
  1080. if ($update === false) {
  1081. $this->_log->info(sprintf('Could not download update file "%s"!', $updateFile));
  1082. return false;
  1083. }
  1084. return $update;
  1085. }
  1086. }
  1087. public function checkBackDownload($data)
  1088. {
  1089. $code = authModel::orderBy('id', 'desc')->value('code');
  1090. $this->_log->notice('Back Checking for a new download...');
  1091. $versions = null; // $this->_cache->get('update-versions');
  1092. // Create absolute url to update file
  1093. $updateFile = $this->_updateUrl . '/' . $this->_updateFile . '/' . $code;
  1094. // Check if cache is empty
  1095. if ($versions === null || $versions === false) {
  1096. $this->_log->debug(sprintf('Get new updates from %s', $updateFile));
  1097. $download = Curl::to($updateFile)
  1098. ->withHeader(
  1099. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  1100. )
  1101. ->withData($data)
  1102. ->asJsonResponse(true)
  1103. ->get();
  1104. if ($download === false) {
  1105. $this->_log->info(sprintf('Could not download update file "%s"!', $updateFile));
  1106. return false;
  1107. }
  1108. return $download;
  1109. }
  1110. }
  1111. public function getDirsByPath($path, Filesystem $filesystem = null)
  1112. {
  1113. $dirs = [];
  1114. if (is_null($filesystem)) {
  1115. $filesystem = app(Filesystem::class);
  1116. }
  1117. if ($all_dir = $filesystem->directories(base_path($path))) {
  1118. if (!is_null($all_dir)) {
  1119. foreach ($all_dir as $dir) {
  1120. $dirs[] = substr($dir, strrpos($dir, DIRECTORY_SEPARATOR)+1);
  1121. }
  1122. }
  1123. }
  1124. return $dirs;
  1125. }
  1126. private function checkDomain($domain)
  1127. {
  1128. if (!preg_match($_SERVER['HTTP_HOST'], $domain)) {
  1129. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  1130. return 'unknown';
  1131. }
  1132. redirect(yzWebFullUrl('update.pirate'))->send();
  1133. }
  1134. }
  1135. private function deldir($dir)
  1136. {
  1137. //先删除目录下的文件:
  1138. $dh = opendir($dir);
  1139. while ($file = readdir($dh)) {
  1140. if ($file != "." && $file != "..") {
  1141. $fullpath = $dir . "/" . $file;
  1142. if (!is_dir($fullpath)) {
  1143. unlink($fullpath);
  1144. } else {
  1145. $this->deldir($fullpath);
  1146. }
  1147. }
  1148. }
  1149. closedir($dh);
  1150. //删除当前文件夹:
  1151. if (rmdir($dir)) {
  1152. return true;
  1153. } else {
  1154. return false;
  1155. }
  1156. }
  1157. public function isPluginExists($url, $message = '')
  1158. {
  1159. $content = Curl::to($url)
  1160. ->asJsonResponse(true)
  1161. ->get();
  1162. if(!$content['isExists']) {
  1163. $this->_log->error($message . $content['message']);
  1164. }
  1165. return $content;
  1166. }
  1167. private function authCode($domain)
  1168. {
  1169. $url = $this->_updateUrl . '/code.json/' . $domain;
  1170. $data = [
  1171. 'domain' => rtrim(request()->getHost(), '/')
  1172. ];
  1173. Curl::to($url)
  1174. ->withHeader(
  1175. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  1176. )
  1177. ->withData($data)
  1178. ->asJsonResponse(true)
  1179. ->get();
  1180. }
  1181. public function remoteSystemVersion()
  1182. {
  1183. $updateFile = $this->_updateUrl . '/' . $this->_updateFile;
  1184. $update = Curl::to($updateFile)
  1185. ->withHeader(
  1186. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  1187. )
  1188. ->get();
  1189. return $update;
  1190. }
  1191. public function showLog($page)
  1192. {
  1193. $updateFile = $this->_updateUrl . '/' . $this->_updateFile;
  1194. $log = Curl::to($updateFile)
  1195. ->withHeader(
  1196. "Authorization: Basic " . base64_encode("{$this->_username}:{$this->_password}")
  1197. )
  1198. ->withData(['page' => $page])
  1199. ->get();
  1200. return $log;
  1201. }
  1202. }