PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/githelper.php

https://github.com/phybros/git-center
PHP | 121 lines | 94 code | 23 blank | 4 comment | 20 complexity | 533e135c0e48bf85ed789f6a849ba8cf MD5 | raw file
  1. <?php
  2. function getRepoDescription($repopath) {
  3. $descriptionpath = $repopath . '/description';
  4. return file_exists($descriptionpath) ? file_get_contents($repopath . '/description') : "Error: Repository does not exist";
  5. }
  6. function getLastUpdate($repopath) {
  7. if(file_exists($repopath)) {
  8. $repo = new Git($repopath);
  9. $branch_hash = $repo->getTip('master');
  10. $branch = $repo->getObject($branch_hash);
  11. return date("D M jS, Y H:i:s", $branch->committer->time + $branch->committer->offset);
  12. } else {
  13. return "Error: Repository does not exist";
  14. }
  15. }
  16. function isTag($tags, $commithash) {
  17. foreach($tags as $tag => $commit) {
  18. if($commit->getName() == $commithash) return $tag;
  19. }
  20. }
  21. function sha1_short($sha1_long) {
  22. return substr($sha1_long, 0, 10);
  23. }
  24. function getTotalCommits($repopath) {
  25. $repo = new Git($repopath);
  26. $master_name = $repo->getTip('master');
  27. $master = $repo->getObject($master_name);
  28. return count($master->getHistory());
  29. }
  30. function isBinaryData($data) {
  31. $sampleSize = 100;
  32. $max = 5;
  33. $badChars = 0;
  34. $isBinary = false;
  35. if(strlen($data) > $sampleSize) {
  36. $data = substr($data, 0, $sampleSize);
  37. }
  38. $chars = str_split($data);
  39. foreach($chars as $char) {
  40. if($char != " " && $char != " " && $char != "\n" && $char != "\r" && ctype_cntrl($char)) {
  41. $badChars++;
  42. }
  43. if($badChars >= $max) {
  44. $isBinary = true;
  45. }
  46. }
  47. return $isBinary;
  48. }
  49. function hasBom($data) {
  50. if(strlen($data) < 3) return false;
  51. return (substr($data, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf));
  52. }
  53. function __computeUnsignedChecksum($bytestring) {
  54. $unsigned_chksum = 0;
  55. for($i=0; $i<512; $i++)
  56. $unsigned_chksum += ord($bytestring[$i]);
  57. for($i=0; $i<8; $i++)
  58. $unsigned_chksum -= ord($bytestring[148 + $i]);
  59. $unsigned_chksum += ord(" ") * 8;
  60. return $unsigned_chksum;
  61. }
  62. function tarSection($Name, $Data, $information=NULL) {
  63. // Generate the TAR header for this file
  64. $header = "";
  65. $header .= str_pad($Name,100,chr(0));
  66. $header .= str_pad("777",7,"0",STR_PAD_LEFT) . chr(0);
  67. $header .= str_pad(decoct($information["user_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  68. $header .= str_pad(decoct($information["group_id"]),7,"0",STR_PAD_LEFT) . chr(0);
  69. $header .= str_pad(decoct(strlen($Data)),11,"0",STR_PAD_LEFT) . chr(0);
  70. $header .= str_pad(decoct(time(0)),11,"0",STR_PAD_LEFT) . chr(0);
  71. $header .= str_repeat(" ",8);
  72. $header .= "0";
  73. $header .= str_repeat(chr(0),100);
  74. $header .= str_pad("ustar",6,chr(32));
  75. $header .= chr(32) . chr(0);
  76. $header .= str_pad($information["user_name"],32,chr(0));
  77. $header .= str_pad($information["group_name"],32,chr(0));
  78. $header .= str_repeat(chr(0),8);
  79. $header .= str_repeat(chr(0),8);
  80. $header .= str_repeat(chr(0),155);
  81. $header .= str_repeat(chr(0),12);
  82. // Compute header checksum
  83. $checksum = str_pad(decoct(__computeUnsignedChecksum($header)),6,"0",STR_PAD_LEFT);
  84. for($i=0; $i<6; $i++) {
  85. $header[(148 + $i)] = substr($checksum,$i,1);
  86. }
  87. $header[154] = chr(0);
  88. $header[155] = chr(32);
  89. // Pad file contents to byte count divisible by 512
  90. $file_contents = str_pad($Data,(ceil(strlen($Data) / 512) * 512),chr(0));
  91. // Add new tar formatted data to tar file contents
  92. $tar_file = $header . $file_contents;
  93. return $tar_file;
  94. }
  95. function getDataUri($imagedata, $mime = '') {
  96. return 'data:'.$mime.';base64,'.base64_encode($imagedata);
  97. }
  98. ?>