I want to have my PHP application labeled with the revision number which it uses, but I don't want to use CruiseControl or update a file and upload it every time. How should I do it?
SVN keywords is not a good solution. As others pointed out adding $Revision$ in a file only affects the specific file, which may not change for a long time.
Remembering to "edit" a file (by adding or removing a blank line) before every commit is pointless. You could as well just type the revision by hand.
One good way to do it (that I know of) is to have an automated deployment process (which is always a good thing) and using the command svnversion. Here is what I do:
Wherever I need the revision I do an include: <?php include 'version.php'; ?>. This "version.php" file only has the revision number. Moreover it is not part of the repository (it set to be ignored). Here is how I create it:
1) On projects where SVN is installed on the server, I also use it for deployment. Getting the latest version to the server I have a script that among other things does the following (it runs on the server):
cd /var/www/project
svn update
rm version.php
svnversion > version.php
2) On projects where SVN is not installed my deployment script is more complex: it creates the version.php file locally, zips the code, uploads and extracts it
Assuming your webroot is a checked-out copy of the subversion tree, you could parse the /.svn/entries file and hook out the revision number (4th line here)...
In PHP:
$svn = File('.svn/entries');
$svnrev = $svn[3];
unset($svn);
This is how I got it to work.
If your server is setup to allow shell_exec AND you have SVN installed just run:
$revision = `svnversion`;
or
$revision = shell_exec('svnversion');
From this answer:
You can do it by adding the following
anywhere in your code
$Id:$
So for example Jeff did:
<div id="svnrevision">svn revision: $Id:$</div>
and when checked in the
server replaced $Id:$ with the current
revision number. I also found this reference.
There is also $Date:$, $Rev:$,
$Revision:$
Bit late now, but use a Subversion post-commit hook. In your repository's hooks folder, create a shell script like this one:
#!/bin/bash
REPOS="$1"
REV="$2"
cd /web/root
rm -f /web/root/templates/base.html
/usr/bin/svn update
/bin/sed -i s/REVISION/$REV/ /web/root/templates/base.html
This particular example assumes your live site is in /web/root and the development code is held elsewhere. When you commit a dev change, the script deletes the prior live template (to avoid conflict messages), runs the update and replaces occurrences of REVISION in the template with the actual revision number.
More on hooks here
In most cases the code on the server would actually contain an "Export" of the code, not a checkout, and therefore not contain the .svn folders. At least that's the setup I see most often. Do others actually check out their code onto the web server?
You can get close with SVN Keywords. Add $Revision$ where you want the revision to show, but that will only show the last revision that particular file was changed, so you would have to make a change to the file each time. Getting the global revision number isn't possible without some sort of external script, or a post-commit hook.
You could also do it like this:
$status = #shell_exec('svnversion '.realpath(__FILE__));
if ( preg_match('/\d+/', $status, $match) ) {
echo 'Revision: '.$match[0];
}
The easiest way is to use the Subversion "Keyword Substitution". There is a guide here in the SVN book (Version Control with Subversion).
You'll basically just have to add the text $Rev$ somewhere in your file.
Then enable the keyword in your repository. On checkout SVN will substitute the revision number into the file.
See my response to the similar question "Mark" svn export with revision.
If you capture the revision number when you export you can use:
svn export /path/to/repository | grep ^Exported > revision.txt
To strip everything but the revision number, you can pipe it through this sed command:
svn export /path/to/repository | grep ^Exported | sed 's/^[^0-9]\+\([0-9]\+\).*/\1/' > revision.txt
$svn_rev=file_get_contents('/path.to.repository/db/current');
Another possibility to do this is to run a cron that executes the steps described in the "Deploy Process" (assuming it is a *nix/FreeBSD server).
If performance is an issue, then you could do:
exec('svn info /path/to/repository', $output);
$svn_ver = (int) trim(substr($output[4], strpos($output[4], ':')));
This of course depends on your having done a checkout, and the presence of the svn command.
Related
Ok, this is a little difficult to explain so bear with me.
Let's say for example I have this repository I'm working out of
path/to/repos/users
In this repository there is a bunch of different users for example.
*../../../users/me
../../../users/you*
And we're both making commits in TortoiseSVN like usual and for this example let's say revision 10 is the head revision. In the "../me" directory I've only submitted 10, 6, and 5. And the "../you" directory committed everything else.
If I were to use a svn command in that directory = svn update -r PREV and I were to check the version number using svnversion the number I would get is 9. However, I don't want 9. Which brings me to my question, is there any way using the command line to update to previous revisions and get revisions that only "../me" submitted?
Sorry if it is a little hard to understand, if there's anything I can provide to make it a bit clearer just let me know. Thank you.
I figured out my issue this morning, to answer my own question, when working with subfolders out of a checked out repository, every time a svn update -r PREV command is called, it updates to the previous revisions in me directory. However, running svnversion on at that point will give you the previous revision in ../../repos/users to solve this issue, I called svn info . in the me directory and noted that the Last Changed Rev: had the number that I needed. To get this I called svn info (this is in php btw):
exec('svn info .', $info);
Then I used a regular expression to grab the key:
$versionKey = array_keys(preg_grep('/Last Changed Rev/', $info));
And then set the version to that revision number using substr with an offset of 18:
$version = substr($info[$versionKey[0]], 18);
I am working on a php website and it gets regularly infected by Malware. I've gone through all the security steps but failed. But I know how it every time infect my code. It comes at the starting of my php index file as following.
<script>.....</script><?
Can anybody please help me how can I remove the starting block code of every index file at my server folders? I will use a cron for this.
I already gone through regex question for removal of javascript malware but did not found what I want.
You should change FTP password to your website, and also make sure that there are no programs running in background that open TCP connections on your server enabling some remote dude to change your site files. If you are on Linux, check the running processes and kill/delete all that is suspicious.
You can also make all server files ReadOnly with ROOT...
Anyhow, trojan/malware/unautorized ftp access is to blame, not JavaScript.
Also, this is more a SuperUser question...
Clients regularly call me do disinfect their non-backed up, PHP malware infected sites, on host servers they have no control over.
If I can get shell access, here is a script I wrote to run:
( set -x; pwd; date; time grep -rl zend_framework --include=*.php --exclude=*\"* --exclude=*\^* --exclude=*\%* . |perl -lne 'print quotemeta' |xargs -rt -P3 -n4 sed -i.$(date +%Y%m%d.%H%M%S).bak 's/<?php $zend_framework=.*?>//g'; date ; ls -atrFl ) 2>&1 | tee -a ./$(date +%Y%m%d.%H%M%S).$$.log`;
It may take a while but ONLY modifies PHP files containing the trojan's signature <?php $zend_framework=
It makes a backup of the infected .php versions to .bak so that when re-scanned, will skip those.
If I cannot get shell access, eg. FTP only, then I create a short cleaner.php file containing basically that code for php to exec, but often the webserver times out the script execution before it goes through all subdirectories though.
WORKAROUND for your problem:
I put this in a crontab / at job to run eg. every 12 hours if such access to process scheduling directly on the server is possible, otherwise, there are also more convoluted approaches depending on what is permitted, eg. calling the cleaner php from the outside once in a while, but making it start with different folders each time via sort --random (because after 60sec or so it will get terminated by the web server anyway).
Change Database Username Password
Change FTP password
Change WordPress Hash Key.
Download theme + plugins to your computer and scan with UPDATED antivirus specially NOD32.
Don't look for the pattern that tells you it is malware, just patch all your software, close unused ports, follow what people told you here already instead of trying to clean the code with regex or signatures...
I need to find the number of commits for a directory within a repository. I will be dealing with the result within PHP so would probably need to utilise exec(). It will be running on Linux.
I have a slightly quirky repository where the directory structure looks a little like:
/trunk/section1
/trunk/section2
/trunk/section3 (and so on)
I'm going to use the result as part of the version number for each section. As it stands I use the latest revision but ideally would like to have it numbered sequentially instead of 84 -> 120 -> 400 etc.
I plan on counting the commits and using '3' instead of '400', using the above example.
If you really like to count the number of commits just use the following:
svn log -q --stop-on-copy URL/trunk | grep "^r" | wc -l
will printout the number of commits.
I'm not familiar with how to invoke this with php, but you can use the svn log --xml path/to/repository/trunk/section1 command to get the result in xml format. From there it shouldn't be too difficult to count the number of <logentry> nodes in the output.
I have been trying to get my php to be able to create sql procedure files. My page does everything just as it needs to. It even writes to file correctly, but now I need it to add the file to svn and then also commit. I have been trying
system("svn commit", $trash);
but I cannot seem to get it to work. I have been able to get
system("svn add ".$fileName);
to work and i get common "A................fileName added" or what ever it says. I think that when I try to commit the file svn is waiting for authentication or something, I think this because I don't get anything back in the $trash...
All help is appreciated, thanks.
There is a PECL extension for working with SVN:
svn_commit — Sends changes from the local working copy to the repository
Example from Manual:
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, 'Bob');
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, 'abc123');
var_dump(svn_commit('Log message of Bob\'s commit', array(realpath('calculator'))));
There is also a PEAR package:
http://pear.php.net/package/VersionControl_SVN
VersionControl_SVN is a simple OO-style interface for Subversion, the free/open-source version control system. VersionControl_SVN can be used to manage trees of source code, text files, image files -- just about any collection of files.
You can go with a full svn commit command line like :
svn commit --username USER --password PASS
Further options available at : http://svnbook.red-bean.com/en/1.1/re06.html
I want to use PHP writing git hooks but have some problem.
I use Windows 7 LAMP packet and git bash.
So, if I run the next script (pre-commit hook) through git shell:
https://gist.github.com/713716
it works fine and there is 123 on the screen. But if I use:
git commit
I have the next error:
error: cannot spawn .git/hooks/pre-commit: No such file or directory
So, what the problem is?
I rarely work on Windows systems but it might have something to do with either making sure the pre-commit file is executable by every user (per Phil's comment), or something to do with git invoking a php command.
Looks like this problem may exist elsewhere but not easily reproduceable.
It's janky, but it might work if you create a new file somewhere with a php extension and invoke that file from the pre-commit hook.
#pre-commit
C:/WebServers/usr/local/php5/php C:/path/to/123.php
#123.php
<?php echo 123 ?>