Skip to content

Instantly share code, notes, and snippets.

@asimzeeshan
Last active August 27, 2015 15:25
Show Gist options
  • Select an option

  • Save asimzeeshan/882ca003743843e177b0 to your computer and use it in GitHub Desktop.

Select an option

Save asimzeeshan/882ca003743843e177b0 to your computer and use it in GitHub Desktop.
A scriplet to update SVN from web + log the output and client IP (just rename this to some random script)
<?php
# ======================================================================
# Update SVN from web invoke script
# ----------------------------------------------------------------------
# Basic functions
# ======================================================================
function _appendLog($data) {
file_put_contents("svn.log", $data."\n", FILE_APPEND | LOCK_EX);
}
# ======================================================================
# Let's BEGIN
# ======================================================================
// Ensure that SVN credentials are already saved else modify the command below to add those credentials explicitly
exec("svn co http://usvn.vteamslabs.com/svn/asimzeeshan/trunk /home/asim/svn-test", $output);
// if there is output
if (!empty($output)) {
// ensure that you get the IP regardless of local-network, proxy or public ip
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$client_ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (!empty($_SERVER['REMOTE_ADDR'])) {
$client_ip = $_SERVER['REMOTE_ADDR'];
} else { // if executed through php5-cli, none of the above variables will have any data. We need a filler hence the "filler" below
$client_ip = "php5-cli";
}
// prepend the transaction
_appendLog("\nSVN UPDATE INITIATED FROM $client_ip on ".date('Y-m-d H:i:s'));
// add the multiline output
// I hope PHP7 adds support for array() as $data
foreach ($output as $row) {
echo $row."<br />";
_appendLog($row); // log it
}
}
@asimzeeshan
Copy link
Author

Sample output in the logs would be

SVN UPDATE INITIATED FROM php5-cli on 2015-08-27 19:52:57
Checked out revision 450.

@asimzeeshan
Copy link
Author

Linear approach, added a function _appendLog() to centralize the logging

@asimzeeshan
Copy link
Author

Added <br /> instead of \n because in the browser, it will be a mess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment