Danger from the deep

November 3, 2009

Howto access a remote SVN repository from PHP

Filed under: Development — mbady @ 05:56

For our new site we wished to have something that shows the users we are currently working on dangerdeep and the project is not dead. A good way is to get some informations from the Sourceforge SVN repository and display them on the page. But neither the subversion plugin for PHP is installed on the Sourceforge webspace or we can access our cia.vc feed, because every in/outgoing connections are blocked for scripts by the Sourceforge firewalls. Well, there is the RSS feed provided by Sf.net and it contains commit messages but the feed is far away from being well structured and doesn’t contains all informations we want.

So what can we do? The key to our problem is the WebDAV support of subversion. While the WebDAV extension is very popular, the subset and the extensions that are used by subversion aren’t. So i decided to place these lines here for other people that have the same problem.

To retrieve the commit informations from the repository we can use the log-report command of subversions WebDAV extension. Let’s start with our WebDAV request:

$request =
'<?xml version="1.0"?>'.
  '<S:log-report xmlns:S="svn:">'.
    '<S:start-revision>'.$start_revision.'</S:start-revision>'.
    '<S:discover-changed-paths/>'.
  '</S:log-report>';

As you maybe noticed there’s a variable $start_revision. This information is needed to let the server know from which revision starting he has to submit the log. If you want to get the log for a range of revisions you can easily add a tag to the request.

Now we need to know that WebDAV is an extension to the HTTP protocol. So we have to send a HTTP request with the log-report command to the repository and recieve the log information from the server. Here’s our HTTP header:

$header =
  "REPORT /svnroot/dangerdeep/ HTTP/1.1\r\n".
  "Host: dangerdeep.svn.sourceforge.net\r\n".
  "Depth: 1\r\n".
  "Content-type: text/xml\r\n".
  "Content-length: ".strlen($request)."\r\n\r\n";

Nothing special here. A very simple HTTP header. The next step is to open a connection the WebDAV interface of the repository and send our header+request to the other side:

$sock = fsockopen("ssl://dangerdeep.svn.sourceforge.net", 443);
if(!$sock) return false;
if(!fputs($sock, $header.$request)) return false;

As Sf.net only provides HTTPS access to svn we need to specify the “ssl://” before the hostname. PHP will then take care of all the SSL stuff and we can easily read and write plaintext from and to the server.

If everything went fine we can now read the log from the stream and ensure that the HTTP response code is 200:

$str = stream_get_contents($sock);
$code = substr($str, strpos($str, ' ')+1, 3);
if($code != 200) return false;

Before we can start parsing the recived log (it’s in XML format) we need to remove all crap that is before and maybe after the XML code:

$str = substr($str, strpos($str, "<"));
$str = substr($str, 0, strrpos($str, ">")+1);

That’s it! Now you can do whatever you want with the log. Putting all the code in a nice function it could look like this:

define('SVN_HOST', 'dangerdeep.svn.sourceforge.net');
define('SVN_PORT', 443);
define('SVN_ROOT', '/svnroot/dangerdeep/');

function get_log($start_revision) {
  $request =
    '<?xml version="1.0"?>'.
      '<S:log-report xmlns:S="svn:">'.
        '<S:start-revision>'.$start_revision.'</S:start-revision>'.
        '<S:discover-changed-paths/>'.
      '</S:log-report>';

  $header =
    "REPORT ".SVN_ROOT." HTTP/1.1\r\n".
    "Host: ".SVN_HOST."\r\n".
    "Depth: 1\r\n".
    "Content-type: text/xml\r\n".
    "Content-length: ".strlen($request)."\r\n\r\n";

    $sock = fsockopen("ssl://".SVN_HOST, SVN_PORT);
    if(!$sock) return false;
    if(!fputs($sock, $header.$request)) return false;

    $str = stream_get_contents($sock);
    $code = substr($str, strpos($str, ' ')+1, 3);
    if($code != 200) return false;

    $str = substr($str, strpos($str, "<"));
    $str = substr($str, 0, strrpos($str, ">")+1);
    return simplexml_load_string($str);
}

49 Comments »

  1. toe@arco.louder” rel=”nofollow”>.…

    спс!…

    Trackback by roland — December 22, 2013 @ 19:20

  2. terraces@protocol.inch” rel=”nofollow”>.…

    thanks….

    Trackback by ivan — January 19, 2014 @ 05:01

  3. buddy@determines.maps” rel=”nofollow”>.…

    ñïñ….

    Trackback by Antonio — January 19, 2014 @ 09:44

  4. clobber@thighs.cambodias” rel=”nofollow”>.…

    tnx….

    Trackback by james — January 19, 2014 @ 12:05

  5. shoved@acted.registration” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!!…

    Trackback by Fred — January 19, 2014 @ 13:46

  6. flamed@foley.undermine” rel=”nofollow”>.…

    good info!…

    Trackback by Calvin — January 19, 2014 @ 23:25

  7. volker@extremities.segregate” rel=”nofollow”>.…

    ñïàñèáî!…

    Trackback by shaun — January 20, 2014 @ 08:29

  8. askington@scraping.tunisian” rel=”nofollow”>.…

    ñïàñèáî çà èíôó….

    Trackback by frank — January 20, 2014 @ 11:32

  9. cakes@infantryman.courses” rel=”nofollow”>.…

    tnx for info….

    Trackback by Scott — January 20, 2014 @ 13:08

  10. seaborg@embezzle.capones” rel=”nofollow”>.…

    ñïñ!…

    Trackback by nathaniel — January 20, 2014 @ 13:59

  11. suburbs@peltz.felske” rel=”nofollow”>.…

    thanks for information!…

    Trackback by seth — January 21, 2014 @ 00:02

  12. conducted@disintegrating.adirondack” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

    Trackback by tony — February 9, 2014 @ 23:58

  13. concentration@prayerfully.chines” rel=”nofollow”>.…

    áëàãîäàðåí….

    Trackback by Stanley — February 10, 2014 @ 17:54

  14. frailest@devonshire.humid” rel=”nofollow”>.…

    thanks!!…

    Trackback by wade — February 11, 2014 @ 22:39

  15. defacing@natch.sesame” rel=”nofollow”>.…

    áëàãîäàðñòâóþ!…

    Trackback by joseph — February 13, 2014 @ 00:32

  16. sinuously@woodwork.inflections” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

    Trackback by Fernando — February 13, 2014 @ 16:58

  17. hungarian@comport.rathbones” rel=”nofollow”>.…

    tnx for info!…

    Trackback by Anthony — February 13, 2014 @ 19:16

  18. beads@livingston.guesses” rel=”nofollow”>.…

    áëàãîäàðñòâóþ….

    Trackback by calvin — February 14, 2014 @ 20:12

  19. untraditional@uppon.ordered” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

    Trackback by Armando — February 18, 2014 @ 19:39

  20. twitching@companionship.cumbersome” rel=”nofollow”>.…

    áëàãîäàðþ!!…

    Trackback by Otis — February 19, 2014 @ 10:18

  21. emasculated@students.typified” rel=”nofollow”>.…

    ñïñ çà èíôó!!…

    Trackback by gordon — February 19, 2014 @ 13:07

  22. genres@workbench.mai” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

    Trackback by angel — February 20, 2014 @ 08:32

  23. separateness@buries.coupons” rel=”nofollow”>.…

    thanks….

    Trackback by guy — February 20, 2014 @ 09:20

  24. meat@observers.pomham” rel=”nofollow”>.…

    áëàãîäàðñòâóþ….

    Trackback by jordan — February 20, 2014 @ 09:42

  25. payable@hoags.bade” rel=”nofollow”>.…

    thank you….

    Trackback by Lawrence — February 21, 2014 @ 00:21

  26. soviets@unavailable.scissoring” rel=”nofollow”>.…

    ñïñ!!…

    Trackback by fredrick — February 21, 2014 @ 01:33

  27. buffet@applicability.seaquake” rel=”nofollow”>.…

    áëàãîäàðåí!!…

    Trackback by Nelson — February 21, 2014 @ 01:53

  28. northrop@unpredictable.missionaries” rel=”nofollow”>.…

    áëàãîäàðåí….

    Trackback by duane — February 21, 2014 @ 07:07

  29. shak@svelte.provisional” rel=”nofollow”>.…

    tnx for info….

    Trackback by leon — February 21, 2014 @ 17:56

  30. emotionalism@dallas.reginald” rel=”nofollow”>.…

    thanks for information!!…

    Trackback by Darryl — February 21, 2014 @ 22:05

  31. hallways@tenuous.weave” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

    Trackback by trevor — February 22, 2014 @ 04:35

  32. karipos@gargle.scobee” rel=”nofollow”>.…

    tnx for info!…

    Trackback by shane — February 23, 2014 @ 07:51

  33. reams@buckets.impoverished” rel=”nofollow”>.…

    áëàãîäàðñòâóþ….

    Trackback by Marc — February 23, 2014 @ 08:27

  34. implies@campers.disintegrative” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

    Trackback by calvin — February 23, 2014 @ 15:55

  35. velours@gourmets.compliance” rel=”nofollow”>.…

    thank you….

    Trackback by chad — February 23, 2014 @ 19:28

  36. jouvet@badrawi.unpleasantness” rel=”nofollow”>.…

    tnx for info!…

    Trackback by Clifton — February 23, 2014 @ 22:49

  37. fresno@canneries.oregonians” rel=”nofollow”>.…

    tnx for info!…

    Trackback by Kenny — February 24, 2014 @ 04:14

  38. clays@stepson.provisioned” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!!…

    Trackback by max — February 24, 2014 @ 19:47

  39. banister@incur.particularly” rel=”nofollow”>.…

    ñïñ….

    Trackback by Maurice — February 25, 2014 @ 05:04

  40. irresistible@passage.conservation” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

    Trackback by Steven — February 25, 2014 @ 05:27

  41. pressures@bestubbled.verstrichen” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

    Trackback by mathew — February 25, 2014 @ 10:31

  42. attitudes@lovie.lethargy” rel=”nofollow”>.…

    good info….

    Trackback by floyd — February 25, 2014 @ 18:27

  43. siberia@managua.levee” rel=”nofollow”>.…

    ñïñ çà èíôó!…

    Trackback by ryan — February 25, 2014 @ 20:37

  44. submitted@duels.drunkenness” rel=”nofollow”>.…

    tnx for info!…

    Trackback by lonnie — February 26, 2014 @ 21:16

  45. mittens@lonsdale.unseasonable” rel=”nofollow”>.…

    thank you!!…

    Trackback by wesley — February 27, 2014 @ 00:38

  46. screeching@praised.leiden” rel=”nofollow”>.…

    ñïàñèáî!!…

    Trackback by Louis — February 27, 2014 @ 02:46

  47. parallel@carthago.oozed” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

    Trackback by gerald — February 27, 2014 @ 18:28

  48. muir@argumentation.properties” rel=”nofollow”>.…

    ñïñ çà èíôó….

    Trackback by lewis — February 28, 2014 @ 15:02

  49. slicker@admiringly.jungle” rel=”nofollow”>.…

    good info….

    Trackback by joel — March 1, 2014 @ 00:42

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress