Git.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin;
  4. use DirectoryIterator;
  5. use PhpMyAdmin\Utils\HttpRequest;
  6. use stdClass;
  7. use const DIRECTORY_SEPARATOR;
  8. use const PHP_EOL;
  9. use function array_key_exists;
  10. use function array_shift;
  11. use function basename;
  12. use function bin2hex;
  13. use function count;
  14. use function date;
  15. use function explode;
  16. use function fclose;
  17. use function file_exists;
  18. use function file_get_contents;
  19. use function fopen;
  20. use function fread;
  21. use function fseek;
  22. use function function_exists;
  23. use function gzuncompress;
  24. use function implode;
  25. use function in_array;
  26. use function intval;
  27. use function is_dir;
  28. use function is_file;
  29. use function json_decode;
  30. use function ord;
  31. use function preg_match;
  32. use function str_replace;
  33. use function strlen;
  34. use function strpos;
  35. use function strtolower;
  36. use function substr;
  37. use function trim;
  38. use function unpack;
  39. use function is_bool;
  40. /**
  41. * Git class to manipulate Git data
  42. */
  43. class Git
  44. {
  45. /**
  46. * Build a Git class
  47. *
  48. * @var Config
  49. */
  50. private $config;
  51. public function __construct(Config $config)
  52. {
  53. $this->config = $config;
  54. }
  55. /**
  56. * detects if Git revision
  57. *
  58. * @param string $git_location (optional) verified git directory
  59. */
  60. public function isGitRevision(&$git_location = null): bool
  61. {
  62. // PMA config check
  63. if (! $this->config->get('ShowGitRevision')) {
  64. return false;
  65. }
  66. // caching
  67. if (isset($_SESSION['is_git_revision'])
  68. && array_key_exists('git_location', $_SESSION)
  69. ) {
  70. // Define location using cached value
  71. $git_location = $_SESSION['git_location'];
  72. return $_SESSION['is_git_revision'];
  73. }
  74. // find out if there is a .git folder
  75. // or a .git file (--separate-git-dir)
  76. $git = '.git';
  77. if (is_dir($git)) {
  78. if (! @is_file($git . '/config')) {
  79. $_SESSION['git_location'] = null;
  80. $_SESSION['is_git_revision'] = false;
  81. return false;
  82. }
  83. $git_location = $git;
  84. } elseif (is_file($git)) {
  85. $contents = (string) file_get_contents($git);
  86. $gitmatch = [];
  87. // Matches expected format
  88. if (! preg_match(
  89. '/^gitdir: (.*)$/',
  90. $contents,
  91. $gitmatch
  92. )) {
  93. $_SESSION['git_location'] = null;
  94. $_SESSION['is_git_revision'] = false;
  95. return false;
  96. }
  97. if (! @is_dir($gitmatch[1])) {
  98. $_SESSION['git_location'] = null;
  99. $_SESSION['is_git_revision'] = false;
  100. return false;
  101. }
  102. //Detected git external folder location
  103. $git_location = $gitmatch[1];
  104. } else {
  105. $_SESSION['git_location'] = null;
  106. $_SESSION['is_git_revision'] = false;
  107. return false;
  108. }
  109. // Define session for caching
  110. $_SESSION['git_location'] = $git_location;
  111. $_SESSION['is_git_revision'] = true;
  112. return true;
  113. }
  114. private function readPackFile(string $packFile, int $packOffset): ?string
  115. {
  116. // open pack file
  117. $packFileRes = fopen(
  118. $packFile,
  119. 'rb'
  120. );
  121. if ($packFileRes === false) {
  122. return null;
  123. }
  124. // seek to start
  125. fseek($packFileRes, $packOffset);
  126. // parse header
  127. $headerData = fread($packFileRes, 1);
  128. if ($headerData === false) {
  129. return null;
  130. }
  131. $header = ord($headerData);
  132. $type = ($header >> 4) & 7;
  133. $hasnext = ($header & 128) >> 7;
  134. $size = $header & 0xf;
  135. $offset = 4;
  136. while ($hasnext) {
  137. $readData = fread($packFileRes, 1);
  138. if ($readData === false) {
  139. return null;
  140. }
  141. $byte = ord($readData);
  142. $size |= ($byte & 0x7f) << $offset;
  143. $hasnext = ($byte & 128) >> 7;
  144. $offset += 7;
  145. }
  146. // we care only about commit objects
  147. if ($type != 1) {
  148. return null;
  149. }
  150. // read data
  151. $commit = fread($packFileRes, $size);
  152. fclose($packFileRes);
  153. if ($commit === false) {
  154. return null;
  155. }
  156. return $commit;
  157. }
  158. private function getPackOffset(string $packFile, string $hash): ?int
  159. {
  160. // load index
  161. $index_data = @file_get_contents(
  162. $packFile
  163. );
  164. if ($index_data === false) {
  165. return null;
  166. }
  167. // check format
  168. if (substr($index_data, 0, 4) != "\377tOc") {
  169. return null;
  170. }
  171. // check version
  172. $version = unpack('N', substr($index_data, 4, 4));
  173. if ($version[1] != 2) {
  174. return null;
  175. }
  176. // parse fanout table
  177. $fanout = unpack(
  178. 'N*',
  179. substr($index_data, 8, 256 * 4)
  180. );
  181. // find where we should search
  182. $firstbyte = intval(substr($hash, 0, 2), 16);
  183. // array is indexed from 1 and we need to get
  184. // previous entry for start
  185. if ($firstbyte == 0) {
  186. $start = 0;
  187. } else {
  188. $start = $fanout[$firstbyte];
  189. }
  190. $end = $fanout[$firstbyte + 1];
  191. // stupid linear search for our sha
  192. $found = false;
  193. $offset = 8 + (256 * 4);
  194. for ($position = $start; $position < $end; $position++) {
  195. $sha = strtolower(
  196. bin2hex(
  197. substr($index_data, $offset + ($position * 20), 20)
  198. )
  199. );
  200. if ($sha == $hash) {
  201. $found = true;
  202. break;
  203. }
  204. }
  205. if (! $found) {
  206. return null;
  207. }
  208. // read pack offset
  209. $offset = 8 + (256 * 4) + (24 * $fanout[256]);
  210. $packOffsets = unpack(
  211. 'N',
  212. substr($index_data, $offset + ($position * 4), 4)
  213. );
  214. return $packOffsets[1];
  215. }
  216. /**
  217. * Un pack a commit with gzuncompress
  218. *
  219. * @param string $gitFolder The Git folder
  220. * @param string $hash The commit hash
  221. *
  222. * @return array|false|null
  223. */
  224. private function unPackGz(string $gitFolder, string $hash)
  225. {
  226. $commit = false;
  227. $gitFileName = $gitFolder . '/objects/'
  228. . substr($hash, 0, 2) . '/' . substr($hash, 2);
  229. if (@file_exists($gitFileName)) {
  230. $commit = @file_get_contents($gitFileName);
  231. if ($commit === false) {
  232. $this->config->set('PMA_VERSION_GIT', 0);
  233. return null;
  234. }
  235. $commitData = gzuncompress($commit);
  236. if ($commitData === false) {
  237. return null;
  238. }
  239. $commit = explode("\0", $commitData, 2);
  240. $commit = explode("\n", $commit[1]);
  241. $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
  242. } else {
  243. $pack_names = [];
  244. // work with packed data
  245. $packs_file = $gitFolder . '/objects/info/packs';
  246. $packs = '';
  247. if (@file_exists($packs_file)) {
  248. $packs = @file_get_contents($packs_file);
  249. }
  250. if ($packs) {
  251. // File exists. Read it, parse the file to get the names of the
  252. // packs. (to look for them in .git/object/pack directory later)
  253. foreach (explode("\n", $packs) as $line) {
  254. // skip blank lines
  255. if (strlen(trim($line)) == 0) {
  256. continue;
  257. }
  258. // skip non pack lines
  259. if ($line[0] !== 'P') {
  260. continue;
  261. }
  262. // parse names
  263. $pack_names[] = substr($line, 2);
  264. }
  265. } else {
  266. // '.git/objects/info/packs' file can be missing
  267. // (at least in mysGit)
  268. // File missing. May be we can look in the .git/object/pack
  269. // directory for all the .pack files and use that list of
  270. // files instead
  271. $dirIterator = new DirectoryIterator(
  272. $gitFolder . '/objects/pack'
  273. );
  274. foreach ($dirIterator as $file_info) {
  275. $file_name = $file_info->getFilename();
  276. // if this is a .pack file
  277. if (! $file_info->isFile() || substr($file_name, -5) !== '.pack'
  278. ) {
  279. continue;
  280. }
  281. $pack_names[] = $file_name;
  282. }
  283. }
  284. $hash = strtolower($hash);
  285. foreach ($pack_names as $pack_name) {
  286. $index_name = str_replace('.pack', '.idx', $pack_name);
  287. $packOffset = $this->getPackOffset($gitFolder . '/objects/pack/' . $index_name, $hash);
  288. if ($packOffset === null) {
  289. continue;
  290. }
  291. $commit = $this->readPackFile($gitFolder . '/objects/pack/' . $pack_name, $packOffset);
  292. if ($commit !== null) {
  293. $commit = gzuncompress($commit);
  294. if ($commit !== false) {
  295. $commit = explode("\n", $commit);
  296. }
  297. }
  298. $_SESSION['PMA_VERSION_COMMITDATA_' . $hash] = $commit;
  299. }
  300. }
  301. return $commit;
  302. }
  303. /**
  304. * Extract committer, author and message from commit body
  305. *
  306. * @param array $commit The commit body
  307. *
  308. * @return array<int,array<string,string>|string>
  309. */
  310. private function extractDataFormTextBody(array $commit): array
  311. {
  312. $author = [
  313. 'name' => '',
  314. 'email' => '',
  315. 'date' => '',
  316. ];
  317. $committer = [
  318. 'name' => '',
  319. 'email' => '',
  320. 'date' => '',
  321. ];
  322. do {
  323. $dataline = array_shift($commit);
  324. $datalinearr = explode(' ', $dataline, 2);
  325. $linetype = $datalinearr[0];
  326. if (! in_array($linetype, ['author', 'committer'])) {
  327. continue;
  328. }
  329. $user = $datalinearr[1];
  330. preg_match('/([^<]+)<([^>]+)> ([0-9]+)( [^ ]+)?/', $user, $user);
  331. $user2 = [
  332. 'name' => trim($user[1]),
  333. 'email' => trim($user[2]),
  334. 'date' => date('Y-m-d H:i:s', (int) $user[3]),
  335. ];
  336. if (isset($user[4])) {
  337. $user2['date'] .= $user[4];
  338. }
  339. $$linetype = $user2;
  340. } while ($dataline != '');
  341. $message = trim(implode(' ', $commit));
  342. return [$author, $committer, $message];
  343. }
  344. /**
  345. * Is the commit remote
  346. *
  347. * @param mixed $commit The commit
  348. * @param bool $isRemoteCommit Is the commit remote ?, will be modified by reference
  349. * @param string $hash The commit hash
  350. *
  351. * @return stdClass|null The commit body from the GitHub API
  352. */
  353. private function isRemoteCommit(&$commit, bool &$isRemoteCommit, string $hash): ?stdClass
  354. {
  355. $httpRequest = new HttpRequest();
  356. // check if commit exists in Github
  357. if ($commit !== false
  358. && isset($_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash])
  359. ) {
  360. $isRemoteCommit = $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash];
  361. return null;
  362. }
  363. $link = 'https://www.phpmyadmin.net/api/commit/' . $hash . '/';
  364. $is_found = $httpRequest->create($link, 'GET');
  365. if ($is_found === false) {
  366. $isRemoteCommit = false;
  367. $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = false;
  368. return null;
  369. }
  370. if ($is_found === null) {
  371. // no remote link for now, but don't cache this as GitHub is down
  372. $isRemoteCommit = false;
  373. return null;
  374. }
  375. $isRemoteCommit = true;
  376. $_SESSION['PMA_VERSION_REMOTECOMMIT_' . $hash] = true;
  377. if ($commit === false) {
  378. // if no local commit data, try loading from Github
  379. return json_decode((string) $is_found);
  380. }
  381. return null;
  382. }
  383. private function getHashFromHeadRef(string $gitFolder, string $refHead): array
  384. {
  385. $branch = false;
  386. // are we on any branch?
  387. if (strpos($refHead, '/') === false) {
  388. return [trim($refHead), $branch];
  389. }
  390. // remove ref: prefix
  391. $refHead = substr(trim($refHead), 5);
  392. if (strpos($refHead, 'refs/heads/') === 0) {
  393. $branch = substr($refHead, 11);
  394. } else {
  395. $branch = basename($refHead);
  396. }
  397. $refFile = $gitFolder . '/' . $refHead;
  398. if (@file_exists($refFile)) {
  399. $hash = @file_get_contents($refFile);
  400. if ($hash === false) {
  401. $this->config->set('PMA_VERSION_GIT', 0);
  402. return [null, null];
  403. }
  404. return [trim($hash), $branch];
  405. }
  406. // deal with packed refs
  407. $packedRefs = @file_get_contents($gitFolder . '/packed-refs');
  408. if ($packedRefs === false) {
  409. $this->config->set('PMA_VERSION_GIT', 0);
  410. return [null, null];
  411. }
  412. // split file to lines
  413. $refLines = explode(PHP_EOL, $packedRefs);
  414. foreach ($refLines as $line) {
  415. // skip comments
  416. if ($line[0] === '#') {
  417. continue;
  418. }
  419. // parse line
  420. $parts = explode(' ', $line);
  421. // care only about named refs
  422. if (count($parts) != 2) {
  423. continue;
  424. }
  425. // have found our ref?
  426. if ($parts[1] == $refHead) {
  427. $hash = $parts[0];
  428. break;
  429. }
  430. }
  431. if (! isset($hash)) {
  432. $this->config->set('PMA_VERSION_GIT', 0);
  433. // Could not find ref
  434. return [null, null];
  435. }
  436. return [$hash, $branch];
  437. }
  438. private function getCommonDirContents(string $gitFolder): ?string
  439. {
  440. if (! is_file($gitFolder . '/commondir')) {
  441. return null;
  442. }
  443. $commonDirContents = @file_get_contents($gitFolder . '/commondir');
  444. if ($commonDirContents === false) {
  445. return null;
  446. }
  447. return trim($commonDirContents);
  448. }
  449. /**
  450. * detects Git revision, if running inside repo
  451. */
  452. public function checkGitRevision(): ?array
  453. {
  454. // find out if there is a .git folder
  455. $gitFolder = '';
  456. if (! $this->isGitRevision($gitFolder)) {
  457. $this->config->set('PMA_VERSION_GIT', 0);
  458. return null;
  459. }
  460. $ref_head = @file_get_contents($gitFolder . '/HEAD');
  461. if (! $ref_head) {
  462. $this->config->set('PMA_VERSION_GIT', 0);
  463. return null;
  464. }
  465. $commonDirContents = $this->getCommonDirContents($gitFolder);
  466. if ($commonDirContents !== null) {
  467. $gitFolder .= DIRECTORY_SEPARATOR . $commonDirContents;
  468. }
  469. [$hash, $branch] = $this->getHashFromHeadRef($gitFolder, $ref_head);
  470. if ($hash === null) {
  471. return null;
  472. }
  473. $commit = false;
  474. if (! preg_match('/^[0-9a-f]{40}$/i', $hash)) {
  475. $commit = false;
  476. } elseif (isset($_SESSION['PMA_VERSION_COMMITDATA_' . $hash])) {
  477. $commit = $_SESSION['PMA_VERSION_COMMITDATA_' . $hash];
  478. } elseif (function_exists('gzuncompress')) {
  479. $commit = $this->unPackGz($gitFolder, $hash);
  480. if ($commit === null) {
  481. return null;
  482. }
  483. }
  484. $is_remote_commit = false;
  485. $commit_json = $this->isRemoteCommit(
  486. $commit, // Will be modified if necessary by the function
  487. $is_remote_commit, // Will be modified if necessary by the function
  488. $hash
  489. );
  490. $is_remote_branch = false;
  491. if ($is_remote_commit && $branch !== false) {
  492. // check if branch exists in Github
  493. if (isset($_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash])) {
  494. $is_remote_branch = $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash];
  495. } else {
  496. $httpRequest = new HttpRequest();
  497. $link = 'https://www.phpmyadmin.net/api/tree/' . $branch . '/';
  498. $is_found = $httpRequest->create($link, 'GET', true);
  499. if (is_bool($is_found)) {
  500. $is_remote_branch = $is_found;
  501. $_SESSION['PMA_VERSION_REMOTEBRANCH_' . $hash] = $is_found;
  502. }
  503. if ($is_found === null) {
  504. // no remote link for now, but don't cache this as Github is down
  505. $is_remote_branch = false;
  506. }
  507. }
  508. }
  509. if ($commit !== false) {
  510. [$author, $committer, $message] = $this->extractDataFormTextBody($commit);
  511. } elseif (isset($commit_json->author, $commit_json->committer, $commit_json->message)) {
  512. $author = [
  513. 'name' => $commit_json->author->name,
  514. 'email' => $commit_json->author->email,
  515. 'date' => $commit_json->author->date,
  516. ];
  517. $committer = [
  518. 'name' => $commit_json->committer->name,
  519. 'email' => $commit_json->committer->email,
  520. 'date' => $commit_json->committer->date,
  521. ];
  522. $message = trim($commit_json->message);
  523. } else {
  524. $this->config->set('PMA_VERSION_GIT', 0);
  525. return null;
  526. }
  527. $this->config->set('PMA_VERSION_GIT', 1);
  528. return [
  529. 'hash' => $hash,
  530. 'branch' => $branch,
  531. 'message' => $message,
  532. 'author' => $author,
  533. 'committer' => $committer,
  534. 'is_remote_commit' => $is_remote_commit,
  535. 'is_remote_branch' => $is_remote_branch,
  536. ];
  537. }
  538. }