Jump to content

PHP + Curl on Windows - problem with spaces in Command Prompt


davil
 Share

Recommended Posts

Hi all,

I'm having problems here at work - I'm trying to setup a registry information reporting system that reports info on each client PCs registry back to a Web Server and into a MySQL database.

This seems to be working fine except for some info from the registry that can have spaces, i.e. longdate from the registry comes out as "dd MMMM yyyy" or similar... This is fine if you paste it into a url in firefox or whatever, like test.php?longdate=dd MMMM yyyy but command prompt / dos doesn't like this behaviour (I'm using batch files + CURL on each client PC to send the info to the server).

There's probably a quick fix to solve this problem using FOR but I can't figure it out.. I tried replacing the spaces with dashes but that will only work if the info in the registry string is as above, so as it can change I need a more flexible solution.

at the minute I'm just echoing the SQL query rather than running it but you still get the idea.

Here's go.bat that I use to start the process

@ECHO OFF
cls
setlocal


oldat03wupdatesNOW.EXE > %TMP%NOW.TXT

for /F "tokens=1,2,3,4,5 delims=/ " %%i in (%TMP%NOW.TXT) do set d=%%i %%j %%k %%m
for /F "tokens=1,2,3,4,5 delims=/ " %%i in (%TMP%NOW.TXT) do set day=%%i
for /F "tokens=1,2,3,4,5 delims=/ " %%i in (%TMP%NOW.TXT) do set month=%%j
for /F "tokens=1,2,3,4,5 delims=/ " %%i in (%TMP%NOW.TXT) do set dayofmonth=%%k
for /F "tokens=1,2,3,4,5 delims=/ " %%i in (%TMP%NOW.TXT) do set timey=%%l
for /F "tokens=1,2,3,4,5 delims=/ " %%i in (%TMP%NOW.TXT) do set year=%%m

DEL %TMP%NOW.TXT

if /i [%month%] == [Jan] set month=01
if /i [%month%] == [Feb] set month=02
if /i [%month%] == [Mar] set month=03
if /i [%month%] == [Apr] set month=04
if /i [%month%] == [May] set month=05
if /i [%month%] == [Jun] set month=06
if /i [%month%] == [Jul] set month=07
if /i [%month%] == [Aug] set month=08
if /i [%month%] == [Sep] set month=09
if /i [%month%] == [Oct] set month=10
if /i [%month%] == [Nov] set month=11
if /i [%month%] == [Dec] set month=12




FOR /F "tokens=2* delims=     " %%A IN ('oldat03wupdatesreg.exe QUERY "HKCUControl PanelInternational" /v sCountry') DO SET Country=%%B
FOR /F "tokens=2* delims=     " %%A IN ('oldat03wupdatesreg.exe QUERY "HKCUControl PanelInternational" /v sCurrency') DO SET Currency=%%B
FOR /F "tokens=2* delims=     " %%A IN ('oldat03wupdatesreg.exe QUERY "HKCUControl PanelInternational" /v sLongDate') DO SET LongDate=%%B
FOR /F "tokens=2* delims=     " %%A IN ('oldat03wupdatesreg.exe QUERY "HKCUControl PanelInternational" /v sShortDate') DO SET ShortDate=%%B
FOR /F "tokens=2* delims=     " %%A IN ('oldat03wupdatesreg.exe QUERY "HKCUControl PanelInternational" /v sTimeFormat') DO SET TimeFormat=%%B

FOR /F "tokens=2* delims=     " %%A IN ('oldat03wupdatesreg.exe QUERY "HKEY_CURRENT_USERControl PanelPowerCfg" /v CurrentPowerPolicy') DO SET PowerPolicy=%%B

FOR /F "tokens=2* delims=     " %%A IN ('oldat03wupdatesreg.exe QUERY "HKLMSOFTWARENetwork AssociatesTVDShared ComponentsVirusScan Engine4.0.xx" /v "szVirDefVer"') DO SET VirusDefs=%%B
FOR /F "tokens=2* delims=     " %%A IN ('oldat03wupdatesreg.exe QUERY "HKLMSOFTWARENetwork AssociatesTVDShared ComponentsVirusScan Engine4.0.xx" /v "szEngineVer"') DO SET VirusEngine=%%B



echo LONG DATE in batch file=%longdate%
echo SHORT DATE in batch file=%shortdate%
echo.
echo.


SET URL="http://localhost/it2/test.php?datestamp=%year%%month%%dayofmonth%&timestamp=%timey%&hostname=%computername%&username=%username%&country=%Country%&currency=%Currency%&longdate=%LongDate%&shortdate=%ShortDate%&timeformat=%TimeFormat%&powerpolicy=%PowerPolicy%&virusdefs=%VirusDefs%&virusengine=%VirusEngine%&end=1"

"oldat03wupdatescurl.exe" -B -0 %URL%


:end

here's test.php

<?php
require("config.php");
$tablename="reginfo";

echo "LONGDATE IN PHP=".$_REQUEST['longdate']."nn";
echo "SHORTDATE IN PHP=".$_REQUEST['shortdate']."nn";



if ( ($_REQUEST['currency'] == "€")or ($_REQUEST['currency'] == "Ç") ){$_REQUEST['currency']="€";}


echo "<PRE>nn";


$sqltest = "UPDATE `$tablename` SET ";
foreach ($_REQUEST as $key => $value) {$sqltest.= "`$key` = '$value'n,";}
$sqltest= rtrim ($sqltest,",");
$sqltest.= " WHERE `macaddress` = '".$_REQUEST['macaddress']."' LIMIT 1";    


echo $sqltest;
?>

Here's the output in Firefox / IE if I just paste the url into the address bar, which works fine:

LONGDATE IN PHP=dd MMMM yyyy SHORTDATE IN PHP=dd/MM/yyyy

UPDATE `reginfo` SET `datestamp` = '20070719'
,`timestamp` = '14:42:18'
,`hostname` = 'mypcname'
,`username` = 'jsmith'
,`country` = 'Ireland'
,`currency` = '€'
,`longdate` = 'dd MMMM yyyy'
,`shortdate` = 'dd/MM/yyyy'
,`timeformat` = 'HH:mm:ss'
,`powerpolicy` = '3'
,`virusdefs` = '4.0.5070'
,`virusengine` = '5.1.00'
,`end` = '1'
,`PHPSESSID` = '8l3f649g8aihsf8bleaeqloos6'
 WHERE `macaddress` = '' LIMIT 1

Here's the output in command prompt when I use go.bat

LONG DATE in batch file=dd MMMM yyyy
SHORT DATE in batch file=dd/MM/yyyy


LONGDATE IN PHP=dd

SHORTDATE IN PHP=

<PRE>

UPDATE `reginfo` SET `datestamp` = '20070719'
,`timestamp` = '15:40:16'
,`hostname` = 'mypcname'
,`username` = 'jsmith'
,`country` = 'Ireland'
,`currency` = 'Ç'
,`longdate` = 'dd'
 WHERE `macaddress` = '' LIMIT 1

Now I have tried adding inverted commas around the URL, like this:

"oldat03wupdatescurl.exe" -B -0 "%URL%"

but that didn't work - obviously DOS doesn't like the ampersand so I tried replacing every ampersand with caret + ampersand( ^& ) but that didn't work either.

Is there any way to get the Command prompt to ignore the spaces (if there are any spaces)

If anybody can point me in the right direction, any help would be much appreciated.

I'm using curl for windows, here's the Version output

curl 7.16.0 (i586-pc-mingw32msvc) libcurl/7.16.0 zlib/1.2.2

Protocols: tftp ftp telnet dict ldap http file

Features: Largefile libz

[edit] Obviously I intend to add a hell of a lot more registry info to this when I get the space problem sorted [/edit]

Link to comment
Share on other sites

Oh yeah, I forgot to mention I even tried replacing spaces with %20 but that didn't work either

set longdate=dd%20mm%20yyyy

gives the following output:

`longdate` = 'dd0mm0yyyy'

I could write PHP to strip the zeroes but again this doesn't get my original REG string into the MySQL database, which is what I want to do.

I could write loads of checking PHP and batch but I'm sure there's a better/more efficient way to do this.

Any ideas?

Link to comment
Share on other sites

ahah.... maybe the best way is to replace spaces with something like £-£ or something that shouldn't already be in the text and then replacing that again in the PHP with spaces... it would work but it's not great programming - I mean what happens when some crazy fool edits their registry and puts in £-£ - stranger things have happened.

[edit]I'll probably write a quick EXE in Freebasic to do some of the replacing work but it just seems a bit messy[/edit]

Link to comment
Share on other sites

No I haven't tried that yet I'm only getting to grips with this batch programming (I've been at it for years but only started using FOR this year)

to be honest I'm not sure what you mean by escaping the characters. I know it has something to do with getting rid of them and I've heard the term loads of times around the internet but I'm not sure how to do it. is it something to do with caret ^ symbol?

Link to comment
Share on other sites

No I haven't tried that yet I'm only getting to grips with this batch programming (I've been at it for years but only started using FOR this year)

to be honest I'm not sure what you mean by escaping the characters. I know it has something to do with getting rid of them and I've heard the term loads of times around the internet but I'm not sure how to do it. is it something to do with caret ^ symbol?

http://support.microsoft.com/kb/75634

The caret symbol only escape these characters: & | > < ^

The percentage sign escapes the percentage sign. An example:

SET A="Blah"

ECHO %A

REM it should now print Blah

ECHO %%A

REM It should now print %A

Link to comment
Share on other sites

Nice!! Thanks very much! I've just tried that and all I have to do is put in %%20 instead of %20 - thanks Coop!

I'll just put in a "FOR" statement that replaces all spaces with %%20 - you wouldn't believe how much you've helped me.

Thanks again for everyones help

Link to comment
Share on other sites

TEXTools (TCL.exe) - found at http://www.fireflysoftware.com/recommended/text_editors.htm seems to be the man for the job..

Here are some examples of what TCL (Textools command line) can do:

Removing a Section from an .INI File

Translate quoted comma-delimited to fixed-width

Translate fixed-width to quoted, comma-delimited

Replace XML attribute values

Selective Updates Using an Isolation Block

Search and replace carriage returns

Help screen to C++ string

Help screen to Delphi string

Create mailing labels from a list of addresses

Cull unused database table fields

I found a load of info at ezinearticles dot com but I can't post the link because the URL is blacklisted by Hak.5 -

Should I be alarmed?

Anyway hopefully the above tool should help solve my problem.

I will report back if I get it to work..

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...