PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Bk/StartPage.php

https://bitbucket.org/abtris/phpframeworks
PHP | 519 lines | 351 code | 11 blank | 157 comment | 62 complexity | 772a30597bb14ceed03fec14a968c49d MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Library class for startpage
  4. * @author Ladislav Prskavec <ladislav@prskavec.net>
  5. * @copyright Copyright (c) 2007-2008
  6. * @package startpage
  7. * @version $Id$
  8. */
  9. define('REP_BROWSER','');
  10. define('BUG_URL', '');
  11. /**
  12. * Class startpage
  13. * @static
  14. * @package startpage
  15. */
  16. class Bk_StartPage
  17. {
  18. /**
  19. * konstruktor
  20. */
  21. private function __construct ()
  22. { // zabranime vytvoreni tridy
  23. }
  24. /**
  25. * Apache info
  26. *
  27. * These functions are only available when running PHP as an Apache module.
  28. * It depends on settings in httpd.conf ServerTokens
  29. * ServerTokens Minimal are recommended. File is at Ubuntu /etc/apache2/conf.d/security
  30. *
  31. * @return string
  32. */
  33. public static function apacheInfo ()
  34. {
  35. if (function_exists('apache_get_version')) {
  36. $a = apache_get_version();
  37. $b = substr($a, strpos($a, "/") + 1, strlen($a));
  38. return $b;
  39. } else {
  40. return "N/A";
  41. }
  42. }
  43. /**
  44. * Mysql info
  45. * @return string
  46. */
  47. public static function mysqlInfo ()
  48. {
  49. $link = @mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
  50. if (! $link) {
  51. return "N/A";
  52. }
  53. $a = mysql_get_server_info();
  54. $b = substr($a, 0, strpos($a, "-"));
  55. return $b;
  56. }
  57. /**
  58. * Sqlite info
  59. * @return string
  60. */
  61. public static function sqliteInfo ()
  62. {
  63. try {
  64. $dbh = new PDO('sqlite:' . $_SERVER['DOCUMENT_ROOT'] . SQLITE_TEST_DB); // success
  65. foreach ($dbh->query('SELECT sqlite_version()') as $row) {
  66. return ($row[0]);
  67. }
  68. } catch (Exception $e) {
  69. //echo $e->getMessage();
  70. return "N/A";
  71. }
  72. }
  73. /**
  74. * get PEAR package version
  75. * @param string $packageName
  76. * @return string
  77. */
  78. public static function getPearPackageVersion($packageName)
  79. {
  80. // pear info phpDocumentor | grep 'Release Version' | awk '{print $3}'
  81. $out = shell_exec("pear info $packageName | grep 'Release Version' | awk '{print $3}'");
  82. return $out;
  83. }
  84. /**
  85. * Get version
  86. * @return unknown_type
  87. */
  88. public static function getPhpUnitVersion()
  89. {
  90. $out = system("phpunit --version | grep PHPUnit | awk '{print $2}'");
  91. return $out;
  92. }
  93. /**
  94. * Print_d
  95. * @param mixed $var
  96. * @return void
  97. */
  98. public static function printDebug ($var)
  99. {
  100. echo "<pre>";
  101. if (is_array($var) || is_object($var)) {
  102. print_r($var);
  103. } else {
  104. var_dump($var);
  105. }
  106. echo "</pre>";
  107. }
  108. /**
  109. * svnVersion - from bash
  110. * @return string
  111. */
  112. public static function svnVersion()
  113. {
  114. $output = shell_exec('svn --version | grep "svn, version"');
  115. // svn, version 1.5.1 (r32289)
  116. $pattern = '/(\d.\d.\d)/';
  117. preg_match($pattern, $output, $matches, PREG_OFFSET_CAPTURE, 3);
  118. if (!empty($matches[0][0])) {
  119. return $matches[0][0];
  120. } else {
  121. return "N/A";
  122. }
  123. }
  124. /**
  125. * Message replace
  126. * @param string $msg
  127. * @return string
  128. */
  129. public static function messageReplace ($msg)
  130. {
  131. // Ticket XXXX nahradit Ticket <a href='https://helpdesk.cvut.cz/ets/0/showTicket?id=XXXX'>XXXX</a>
  132. $string = $msg;
  133. $pattern = '/Ticket: (\d+)/';
  134. $replacement = "<strong><a href='" . BUG_URL . "$1'>Ticket: $1</a></strong>";
  135. return preg_replace($pattern, $replacement, $string);
  136. }
  137. /**
  138. * Check date range for display another color today and yesterday
  139. * @param string $date1 in DATE_ISO8601
  140. * @param int days
  141. * return bool
  142. */
  143. public static function dateRange ($date1, $range)
  144. {
  145. $date1 = date_parse($date1);
  146. $date2 = date(DATE_ISO8601); // actual date
  147. $date2 = date_parse($date2);
  148. if ($date1['year'] == $date2['year']) {
  149. if ($date1['month'] == $date2['month']) {
  150. if ($date1['day'] == $date2['day'] || $date1['day'] + $range == $date2['day']) {
  151. return true;
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. /**
  158. * Date replace
  159. *
  160. * Zmena formatovani z ISO na cesky zvyk DD.MM.RRRR
  161. *
  162. * @param string $date
  163. * @return string
  164. */
  165. public static function dateReplace ($date)
  166. {
  167. $date = date_parse($date);
  168. $date = sprintf("%02d.%02d.%04d %02d:%02d:%02d", $date['day'], $date['month'], $date['year'], $date['hour'], $date['minute'], $date['second']);
  169. return $date;
  170. }
  171. /**
  172. * Truncate
  173. * @param string $text Text ke zkrácení
  174. * @param int $numb Kolik znak? je platných
  175. * @param string $etc='...' Zp?sob zakon?ení
  176. * @return string
  177. */
  178. public static function truncate ($text, $numb, $etc = "...")
  179. {
  180. if (strlen($text) > $numb) {
  181. $text = substr($text, 0, $numb);
  182. $text .= $etc;
  183. }
  184. return $text;
  185. }
  186. /**
  187. * Changelog
  188. *
  189. * Changelog structure
  190. * <code>
  191. * YYYY-MM-DD John Doe johndoe@example.com
  192. *
  193. * * myfile.ext (myfunction): my changes made
  194. * additional changes
  195. *
  196. * * anotherfile.ext (somefunction): more changes
  197. * </code>
  198. * XML output from SVN
  199. * <code>
  200. * <log>
  201. * <logentry revision="165">
  202. * <author>abtris</author>
  203. * <date>2008-03-26T13:36:35.586000Z</date>
  204. * <msg>
  205. * Ticket: 000041
  206. * Napojeni nazvu ve filtru na db, table status
  207. * </msg>
  208. * </logentry>
  209. * </log>
  210. * </code>
  211. * @param string $path
  212. * @param bool $last
  213. * @param string $id
  214. */
  215. public static function changeLog ($path, $last, $id = NULL)
  216. {
  217. // Changelog
  218. if (!$last) {
  219. // REP exits
  220. $repExists = 0;
  221. }
  222. // XML log exists
  223. if (file_exists($path)) {
  224. // kontrola validity XML
  225. $xml = @simplexml_load_file($path);
  226. if (! $xml) {
  227. if (! $last)
  228. echo "<h3 class='error'>Changelog in $path isn't valid XML file.</h3>";
  229. return false;
  230. }
  231. } else {
  232. // if file not exists
  233. echo "<h3>Failed to open " . $path . "</h3";
  234. return false;
  235. //exit();
  236. }
  237. // last revision
  238. if ($last) {
  239. if (isset($_GET['rev']) && $path == $_GET['file']) {
  240. $keys = array();
  241. foreach ($xml->children() as $i) {
  242. array_push($keys, (int) $i['revision']);
  243. }
  244. $key = array_search($_GET['rev'], $keys);
  245. $lastkey = end(array_keys($keys));
  246. $val = array_values($keys);
  247. if ($_GET['pos'] == "next") {
  248. if ($key > 0) {
  249. $rev = $val[$key - 1];
  250. } else {
  251. $rev = $val[$key];
  252. }
  253. }
  254. if ($_GET['pos'] == "prev") {
  255. if ($key < $lastkey) {
  256. $rev = $val[$key + 1];
  257. } else {
  258. $rev = $val[$lastkey];
  259. }
  260. }
  261. // use xpath lookfor rev in XML
  262. if (! empty($rev)) {
  263. $xpath = "//logentry[@revision=" . $rev . "]";
  264. foreach ($xml->xpath($xpath) as $res) {
  265. // print
  266. echo "<td class='msg'>";
  267. echo "<strong>";
  268. printf("R%d %s %s", $res['revision'], self::dateReplace($res->date), $res->author);
  269. echo "</strong>";
  270. echo "&nbsp;<a href='?file={$path}&rev={$res['revision']}&pos=prev#$id'>";
  271. echo "&laquo;</a>{$res['revision']}<a href='?file={$path}" . "&rev={$res['revision']}&pos=next#$id'>&raquo;</a>";
  272. echo "<br />";
  273. echo nl2br(self::messageReplace($res->msg));
  274. echo "<br />";
  275. echo "</td>";
  276. }
  277. }
  278. } else {
  279. echo "<td class='msg'>";
  280. echo "<strong>";
  281. // comapre revision for git
  282. if (strlen((string)$xml->logentry['revision'])==strlen((int)$xml->logentry['revision'])) {
  283. printf("R%d %s %s", $xml->logentry['revision'], self::dateReplace($xml->logentry->date), $xml->logentry->author);
  284. } else {
  285. printf("%s %s", self::dateReplace($xml->logentry->date), $xml->logentry->author);
  286. }
  287. echo "</strong>";
  288. echo "&nbsp;<a href='?file={$path}&rev={$xml->logentry['revision']}";
  289. echo "&pos=prev#$id'>&laquo;</a>{$xml->logentry['revision']}";
  290. echo "<a href='?file={$path}&rev={$xml->logentry['revision']}&pos=next#$id'>&raquo;</a>";
  291. echo "<br />";
  292. echo nl2br(self::messageReplace($xml->logentry->msg));
  293. echo "<br />";
  294. echo "</td>";
  295. }
  296. } else {
  297. // all revisions
  298. echo "<table id='changelog'>";
  299. echo "<tr><th>Revision</th><th>Message</th><th>Date</th><th>Author</th></tr>";
  300. foreach ($xml->children() as $i) {
  301. if (self::dateRange($i->date, 0)) {
  302. echo "<tr class='thisday'>";
  303. } elseif (self::dateRange($i->date, 1)) {
  304. echo "<tr class='dayback'>";
  305. } else {
  306. echo "<tr>";
  307. }
  308. // revision
  309. echo "<td>" . $i['revision'] . "</td>";
  310. // msg
  311. echo "<td class='msg'>" . nl2br(self::messageReplace(htmlspecialchars($i->msg)));
  312. // path
  313. if (! empty($i->paths->path)) {
  314. echo "<div class='path'>";
  315. foreach ($i->paths->path as $k) {
  316. if (REP_BROWSER != "" && $repExists == 1) {
  317. echo $k['action'] . " - <a href='" . REP_BROWSER . "{$repName}{$k}'>" . self::truncate($k, 78) . "</a><br />";
  318. } elseif (isset($k['action'])) {
  319. echo $k['action'] . " - " . self::truncate($k, 78) . "<br />";
  320. }
  321. // detect hg or git
  322. if (!isset($k['action'])) {
  323. $files = explode(" ",$k);
  324. foreach ($files as $file) {
  325. echo $file."<br />";
  326. }
  327. }
  328. // for tags and branches
  329. if ( isset($k['copyfrom-path']) && isset($k['copyfrom-rev']) ) {
  330. echo "<strong>";
  331. echo "Copy from: {$k['copyfrom-path']} (";
  332. echo "R{$k['copyfrom-rev']})";
  333. echo "</strong><br />";
  334. }
  335. }
  336. echo "</div>";
  337. }
  338. echo "</td>";
  339. // date
  340. echo "<td>";
  341. echo self::dateReplace($i->date);
  342. echo "</td>";
  343. // author
  344. echo "<td>" . $i->author . "</td>";
  345. echo "</tr>";
  346. }
  347. echo "</table>";
  348. }
  349. } // end changelog
  350. /**
  351. * Etc/hosts
  352. * @return array
  353. */
  354. public static function hosts ()
  355. {
  356. // detekce OS
  357. if (eregi("linux", $_SERVER['HTTP_USER_AGENT'])) {
  358. $os = "linux";
  359. } elseif (eregi("win32", $_SERVER['HTTP_USER_AGENT'])) {
  360. $os = "win";
  361. } else {
  362. $os = "win";
  363. }
  364. if ($os == "win") {
  365. $filename = $_SERVER['WINDIR'] . "\system32\drivers\etc\hosts";
  366. } else {
  367. $filename = "/etc/hosts";
  368. }
  369. if (file_exists($filename)) {
  370. $obsah = file_get_contents($filename);
  371. // var_dump($obsah);
  372. $reg = '/^(\d+.\d+.\d+.\d+)\s+(\S+)/m';
  373. preg_match_all($reg, $obsah, $matches, PREG_SET_ORDER);
  374. return $matches;
  375. }
  376. }
  377. /**
  378. * Apache error log
  379. */
  380. public static function showErrorLog ()
  381. {
  382. echo "<div id='main'><h1>Apache error log</h1>";
  383. if (file_exists(APACHELOG_PATH)) {
  384. // errorlog
  385. echo "<table id='changelog'>";
  386. echo "<tr><th>Date</th><th>Message</th></tr>";
  387. $path = APACHELOG_PATH;
  388. $output = shell_exec('tail '.$path);
  389. $pole = explode("\n",$output);
  390. // parse lines
  391. foreach($pole as $line) {
  392. if (!empty($line)) {
  393. preg_match('~^\[(.*?)\]~', $line, $date);
  394. if (empty($date[1])) {
  395. continue;
  396. }
  397. //preg_match('~\] \[([a-z]*?)\] \[~', $line, $type);
  398. //preg_match('~\] \[client ([0-9\.]*)\]~', $line, $client);
  399. preg_match('~\] (.*)$~', $line, $message);
  400. echo "<tr>";
  401. echo "<td>$date[1]</td>";
  402. //echo "<td>$type[1]</td>";
  403. //echo "<td>$client[1]</td>";
  404. echo "<td style='text-align:left'>$message[1]</td>";
  405. echo "</tr>";
  406. }
  407. }
  408. echo "</table></div>";
  409. return true;
  410. } else {
  411. echo "Error log not found in ".APACHELOG_PATH;
  412. return false;
  413. }
  414. }
  415. /**
  416. * Infoini
  417. * @param string $string
  418. * @return string
  419. */
  420. public static function infoIni ($string)
  421. {
  422. if (ini_get($string) == 0) {
  423. return "Off";
  424. } else {
  425. return "On";
  426. }
  427. }
  428. /**
  429. * Check extension
  430. * @param $string
  431. * @return unknown_type
  432. */
  433. public static function checkExt ($string)
  434. {
  435. $j = 0;
  436. $pole = get_loaded_extensions();
  437. foreach ($pole as $i) {
  438. if ($i == $string)
  439. $j = 1;
  440. }
  441. if ($j == 1) {
  442. return "<strong>Installed</strong>";
  443. } else {
  444. return "<strong>Not available</strong>";
  445. }
  446. }
  447. /**
  448. * Lists of projects, in $root directory can used some mask
  449. * @param string $root
  450. * @return mixed
  451. */
  452. public function listProject ($root, $url)
  453. {
  454. echo "<p><strong>Working copies at $root</strong></p>";
  455. echo "<table id='wc'>";
  456. echo "<tr class='firstrow'><th>Name</th><th>SCM</th><th>Links</th>
  457. <th>Last record from history</th></tr>";
  458. foreach (glob($root . PROJECT_FILTER, GLOB_ONLYDIR) as $dirs) {
  459. echo "<tr>";
  460. echo "<td class='name' id='" . basename($dirs) . "'>
  461. <a href='". $url . basename($dirs) . "'>" . basename($dirs) . "</a></td>";
  462. // xmllog is name with path etc. /tmp/start_page_cheangelog.xml
  463. // build by hook post-commit script
  464. $xmllog = $dirs . XMLLOGNAME;
  465. echo "<td class='log'>";
  466. if (file_exists($dirs.'/.svn')) {
  467. $output = shell_exec("svn info $dirs | grep 'URL' | awk '{print $2}'");
  468. echo "<a href='".$output."'>SVN</a>";
  469. }
  470. if (file_exists($dirs.'/.git')) {
  471. echo "GIT";
  472. }
  473. if (file_exists($dirs.'/.hg')) {
  474. if (file_exists($dirs.'/.hg/hgrc')) {
  475. $hgrc = parse_ini_file($dirs.'/.hg/hgrc');
  476. echo "<a href='".$hgrc['default']."'>HG</a>";
  477. } else {
  478. echo "HG";
  479. }
  480. }
  481. echo "</td>";
  482. if (file_exists($xmllog)) {
  483. echo "<td class='log'>";
  484. echo "<a href='" . BASE_URL . "changelog.php?file=$xmllog'>Log</a> ";
  485. echo "<a href='" . WIKI_LINK . basename($dirs) . "'>Wiki</a>";
  486. //echo "<a href='changelog.php?rep=".substr($dirs, strpos($dirs, "/")+1,strlen($dirs))."'>Log</a>
  487. echo "</td>";
  488. } else {
  489. echo "<td></td>";
  490. }
  491. // This part is for last revision (display rev and message)
  492. if (file_exists($xmllog)) {
  493. StartPage::changeLog($xmllog, true, basename($dirs));
  494. }
  495. //
  496. echo "</tr>";
  497. }
  498. echo "</table>";
  499. }
  500. public function listProjects ($root)
  501. {
  502. $urls = explode('|',PROJECT_URL);
  503. $roots = explode('|',$root);
  504. for ($i=0;$i<count($roots);$i++){
  505. self::listProject($roots[$i],$urls[$i]);
  506. }
  507. }
  508. } // end class