functions.tools.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /* $Id: functions.tools.php 2355 2007-01-23 17:18:40Z b4rt $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * get list with files + checksums
  17. *
  18. * @param $talk
  19. * @return array
  20. */
  21. function getFileChecksums($talk = false) {
  22. global $cfg, $fileList;
  23. $fileList = array();
  24. $fileList['files'] = array();
  25. $fileList['types'] = array(".php", ".dist", ".pl", ".pm", ".tmpl", ".html", ".js", ".css", ".xml", ".xsd", ".py");
  26. _getFileChecksums(substr($cfg['docroot'], 0 , -1), $talk);
  27. return $fileList['files'];
  28. }
  29. /**
  30. * get list with files + checksums worker
  31. *
  32. * @param $dir
  33. * @param $talk
  34. */
  35. function _getFileChecksums($dir, $talk = false) {
  36. global $cfg, $fileList;
  37. if (!is_dir($dir))
  38. return false;
  39. $dirHandle = opendir($dir);
  40. while ($file = readdir($dirHandle)) {
  41. $fullpath = $dir.'/'.$file;
  42. if (is_dir($fullpath)) {
  43. if ($file{0} != '.')
  44. _getFileChecksums($fullpath, $talk);
  45. } else {
  46. $stringLength = strlen($file);
  47. foreach ($fileList['types'] as $ftype) {
  48. $extLength = strlen($ftype);
  49. if (($stringLength > $extLength) && (strtolower(substr($file, -($extLength))) === ($ftype))) {
  50. $file = str_replace($cfg["docroot"], '', $fullpath);
  51. $fileList['files'][$file] = md5_file($fullpath);
  52. if ($talk)
  53. sendLine('.');
  54. }
  55. }
  56. }
  57. }
  58. closedir($dirHandle);
  59. }
  60. /**
  61. * print file-list
  62. *
  63. * @param $basedir
  64. * @param $type 1 = list, 2 = checksums
  65. * @param $mode 1 = text, 2 = html
  66. */
  67. function printFileList($basedir, $type = 1, $mode = 2) {
  68. global $fileList;
  69. if (((strlen($basedir) > 0)) && (substr($basedir, -1 ) != "/"))
  70. $basedir .= "/";
  71. $dir = substr($basedir, 0 , -1);
  72. if (!is_dir($dir))
  73. return false;
  74. define('_URL_SVNLOG','http://svn.berlios.de/wsvn/tf-b4rt/trunk/?rev=');
  75. define('_URL_SVNLOG_SUFFIX','&sc=1');
  76. define('_URL_SVNFILE','http://svn.berlios.de/wsvn/tf-b4rt/trunk/html/');
  77. define('_URL_SVNFILE_SUFFIX','?op=log&rev=0&sc=0&isdir=0');
  78. $fileList = array();
  79. $fileList['files'] = array();
  80. $fileList['types'] = array(".php", ".dist", ".pl", ".pm", ".tmpl", ".html", ".js", ".css", ".xml", ".xsd", ".py");
  81. $fileList['count'] = 0;
  82. $fileList['size'] = 0;
  83. $fileList['revision'] = 1;
  84. _printFileList($basedir, $dir, $type, $mode);
  85. // footer in html
  86. if (($type == 1) && ($mode == 2)) {
  87. sendLine('<br><strong>Processed '.$fileList['count'].' files. ('.formatHumanSize($fileList['size']).')</strong>');
  88. sendLine('<br><strong>Highest Revision-Number : ');
  89. sendLine('<a href="'._URL_SVNLOG.$fileList['revision']._URL_SVNLOG_SUFFIX.'" target="_blank">'.$fileList['revision'].'</a>');
  90. sendLine('</strong>');
  91. }
  92. }
  93. /**
  94. * print file list worker
  95. *
  96. * @param $basedir
  97. * @param $dir
  98. * @param $type 1 = list, 2 = checksums
  99. * @param $mode 1 = text, 2 = html
  100. * @return revision-list as html-snip
  101. */
  102. function _printFileList($basedir, $dir, $type = 1, $mode = 2) {
  103. global $fileList;
  104. if (!is_dir($dir))
  105. return false;
  106. $dirHandle = opendir($dir);
  107. while ($file = readdir($dirHandle)) {
  108. $fullpath = $dir.'/'.$file;
  109. if (is_dir($fullpath)) {
  110. if ($file{0} != '.')
  111. _printFileList($basedir, $fullpath, $type, $mode);
  112. } else {
  113. $stringLength = strlen($file);
  114. foreach ($fileList['types'] as $ftype) {
  115. $extLength = strlen($ftype);
  116. if (($stringLength > $extLength) && (strtolower(substr($file, -($extLength))) === ($ftype))) {
  117. // count
  118. $fileList['count'] += 1;
  119. // file
  120. $_file = str_replace($basedir, '', $fullpath);
  121. switch ($type) {
  122. default:
  123. case 1:
  124. // vars
  125. $_size = filesize($fullpath);
  126. $_rev = getSVNRevisionFromId($fullpath);
  127. // size
  128. $fileList['size'] += $_size;
  129. // rev
  130. if ($_rev != 'NoID') {
  131. $intrev = intval($_rev);
  132. if ($intrev > $fileList['revision'])
  133. $fileList['revision'] = $intrev;
  134. }
  135. // print
  136. switch ($mode) {
  137. default:
  138. case 1:
  139. echo $_file.';'.$_size.';'.$_rev."\n";
  140. break;
  141. case 2:
  142. $line = '<a href="'._URL_SVNFILE.$_file._URL_SVNFILE_SUFFIX.'" target="_blank">'.$_file.'</a> | ';
  143. $line .= formatHumanSize($_size).' | ';
  144. $line .= ($_rev != 'NoID')
  145. ? '<a href="'._URL_SVNLOG.$_rev._URL_SVNLOG_SUFFIX.'" target="_blank">'.$_rev.'</a>'
  146. : 'NoID';
  147. $line .= '<br>';
  148. sendLine($line);
  149. break;
  150. }
  151. break;
  152. case 2:
  153. // vars
  154. $_md5 = md5_file($fullpath);
  155. // print
  156. switch ($mode) {
  157. default:
  158. case 1:
  159. echo $_file.';'.$_md5."\n";
  160. break;
  161. case 2:
  162. sendLine($_file." ".$_md5."<br>");
  163. break;
  164. }
  165. break;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. closedir($dirHandle);
  172. }
  173. /**
  174. * get svn-revision from id-tag of a file
  175. *
  176. * @param $filename
  177. * @return string
  178. */
  179. function getSVNRevisionFromId($filename) {
  180. $data = file_get_contents($filename);
  181. $len = strlen($data);
  182. for ($i = 0; $i < $len; $i++) {
  183. if ($data{$i} == '$') {
  184. if (($data{$i+1} == 'I') && ($data{$i+2} == 'd')) {
  185. $revision = "";
  186. $j = $i + 3;
  187. while ($j < $len) {
  188. if ($data{$j} == '$') {
  189. $rev = explode(" ", $revision);
  190. return trim($rev[2]);
  191. } else {
  192. $revision .= $data{$j};
  193. }
  194. $j++;
  195. }
  196. }
  197. }
  198. }
  199. return 'NoID';
  200. }
  201. /**
  202. * get data of a url
  203. *
  204. * @param $url the url
  205. * @return data
  206. */
  207. function getDataFromUrl($url) {
  208. ini_set("allow_url_fopen", "1");
  209. ini_set("user_agent", "torrentflux-b4rt/". _VERSION);
  210. if ($urlHandle = @fopen($url, 'r')) {
  211. stream_set_timeout($urlHandle, 15);
  212. $info = stream_get_meta_data($urlHandle);
  213. $data = null;
  214. while ((!feof($urlHandle)) && (!$info['timed_out'])) {
  215. $data .= @fgets($urlHandle, 4096);
  216. $info = stream_get_meta_data($urlHandle);
  217. }
  218. @fclose ($urlHandle);
  219. return $data;
  220. }
  221. }
  222. ?>