Jump to content

preg_match_all not working! reading and writing the connection string in the global include via php.


Greywacke

Recommended Posts

DEPRECATED - PLEASE READ NEXT POSThi there,i have an idea more or less of how this can be done... but the finer details puzzle me slightly, i have - for the following first 7 lines of the include:

<?php/*MYSQL CONNECTION INCLUDEVersion 2.2.5*///$conn = mysql_connect("localhost", "root", "");$conn = mysql_connect("fakeserver", "fakeusername", "fakepassword");

sofar written a rough draft of the following two functions, both to return the data set in the file and the data retrieved from the file, the first function to write to the file with the new data posted.

function putdbconfig($file = "") {	if ($_SERVER['HTTPS'] == "on") {		$connstr = file_get_contents($file);		// get data to be replaced		preg_match_all("/$\$conn = mysql_connect\((\"[^\"]*\"), (\"[^\"]*\"), (\"[^\"]*\")\);/",$connstr,$arr);		// replace the data in $arr[0] with the following requested connection data		$arr[0][0] = "\"".$_POST["svr"]."\"";		$arr[0][1] = "\"".$_POST["usr"]."\"";		$arr[0][2] = "\"".$_POST["pwd"]."\"";		// replace existing connection data with requested connection data		preg_replace($arr[1],$arr[0],$connstr);		// overwrite $file with replaced connection data		file_put_contents($file, $connstr);		// write data that was written to xml:		if (preg_match_all("/$\$conn = mysql_connect(\"([^\"]*)\", \"([^\"]*)\", \"([^\"]*)\");/",$connstr,$arr)) {			echo "	<conn server=\"".$arr[1][0]."\" user=\"".$arr[1][1]."\" pass=\"".$arr[1][2]."\" />\n";		}	}}function getdbconfig($file = "") {	if ($_SERVER['HTTPS'] == "on") {		$connstr = file_get_contents($file);		if (preg_match_all("/$\$conn = mysql_connect(\"([^\"]*)\", \"([^\"]*)\", \"([^\"]*)\");/",$connstr,$arr)) {			echo "	<conn server=\"".$arr[1][0]."\" user=\"".$arr[1][1]."\" pass=\"".$arr[1][2]."\" />\n";		}	}}

the end goal is to allow only via an ssl/tls ajax query (thus only making accessible to ssl ajax requests), the retrieval of the database connection settings - also allowing setting of it via the admin interface developed.is this a good idea? am i going about it in a feasible fashion? i do struggle somewhat with regular expressions... any opinions, suggestions, ideas etc (feedback) is 100% welcome.

Link to comment
Share on other sites

DEPRECATED - PLEASE READ NEXT POSToops - i realised that the server was the same for wherever. it was the db that is connected to that differs across sites, not the server - and this is on line 361 of the include.

mysql_select_db("dbowner_5683194816_prod", $conn);

therefore i have changed the php functions as follows:

function putdbconfig($file = "") {	if ($_SERVER['HTTPS'] == "on") {		$connstr = file_get_contents($file);		// get data to be replaced		preg_match_all(array("/$mysql_select_db\(\"([^\"]*)\", \$conn\);/","/$\$conn = mysql_connect(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\");/"),$connstr,$arr);		// replace the data in $arr[1] with the following posted array		$arr[0][0] = $_POST["text_dbname"];		$arr[0][1] = $_POST["text_dbuser"];		$arr[0][2] = $_POST["text_dbpass"];		preg_replace($arr[1],$arr[0],$connstr);		// overwrite $file with replaced data		file_put_contents($file, $connstr, FILE_USE_INCLUDE_PATH);		// write data that was written below:		if (preg_match_all(array("/$mysql_select_db\(\"([^\"]*)\", \$conn\);/","/$\$conn = mysql_connect(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\");/"),$connstr,$arr)) {			echo "	<conn dbname=\"".$arr[1][0]."\" user=\"".$arr[1][1]."\" pass=\"".$arr[1][2]."\" />\n";		}	}}function getdbconfig($file = "") {	if ($_SERVER['HTTPS'] == "on") {		$connstr = file_get_contents($file, FILE_USE_INCLUDE_PATH);		if (preg_match_all(array("/$mysql_select_db\(\"([^\"]*)\", \$conn\);/","/$\$conn = mysql_connect(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\");/"),$connstr,$arr)) {			echo "	<conn dbname=\"".$arr[1][0]."\" user=\"".$arr[1][1]."\" pass=\"".$arr[1][2]."\" />\n";		}	}}

i'm thinking i need to replace all the elements in the arrays with / beginning and / ending within the strings, if i am to use preg_replace. any recommendations?

Link to comment
Share on other sites

DEPRECATED - PLEASE READ NEXT POSTokay *blushes*preg_match_all does not support use of arrays, so therefore i have updated the functions as follows:

function putdbconfig($file = "") {	//if ($_SERVER['HTTPS'] == "on") {		$connstr = file_get_contents($file);		// get data to be replaced		preg_match_all("/$mysql_select_db\(\"([^\"]*)\", \$conn\);|$\$conn = mysql_connect(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\");/",$connstr,$arr);		// replace the data in $arr[1] with the following posted array		$arr[0][0] = $_POST["text_dbuser"];		$arr[0][1] = $_POST["text_dbpass"];		$arr[0][2] = $_POST["text_dbname"];		preg_replace($arr[1],$arr[0],$connstr);		// overwrite $file with replaced data		file_put_contents($file, $connstr, FILE_USE_INCLUDE_PATH);		// write data that was written below:		if (preg_match_all("/$mysql_select_db\(\"([^\"]*)\", \$conn\);|$\$conn = mysql_connect(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\");/",$connstr,$arr)) {			echo "	<conn dbname=\"".$arr[1][2]."\" user=\"".$arr[1][0]."\" pass=\"".$arr[1][1]."\" />\n";		}	//}}function getdbconfig($file = "") {	//if ($_SERVER['HTTPS'] == "on") {		$connstr = file_get_contents($file, FILE_USE_INCLUDE_PATH);		if (preg_match_all("/$mysql_select_db\(\"([^\"]*)\", \$conn\);|$\$conn = mysql_connect(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\");/",$connstr,$arr)) {			echo "	<conn dbname=\"".$arr[1][2]."\" user=\"".$arr[1][0]."\" pass=\"".$arr[1][1]."\" />\n";		}	//}}

only question now, is how to enclose the individual returned array element strings in / - this is a perl regular expression (preg). any help here? sure in arr[0] it's easy - just concat a "/". around the posted values, but as far as i know it is not required in the replacement - only in the match - eg $arr[1]. the normal way to do it just seems awefully long winded. maybe i should write a function to go through an array and set them... :)

Link to comment
Share on other sites

would it not be possible to match two sets of strings inside a single php document's contents in one try? i tried using what i thought to be the OR clause in perl regular expressions (|).

"/[color="#9ACD32"][\r\n]+[/color]mysql_select_db\(\"([^\"]*)\", \$conn\);[color="#FF8C00"][\r\n]+[/color][color="#FF0000"]|[/color][color="#9ACD32"][\r\n]+[/color]\$conn = mysql_connect\(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\"\);[color="#FF8C00"][\r\n]+[/color]/"

how would i match the connection details that exist in these two seperate places - stipulated below (the db user, db password and db name).somebody please help! :) has been pretty useless in finding this - as it keeps returning articles on pregnant girls - even searching preg_ :)the typical contents of the page that is loaded into $connstr, is as follows:

<?php...//$conn = mysql_connect("localhost", "root", "");line   7: $conn = mysql_connect("localhost", "[b]testuser[/b]", "[b]testpassword[/b]");...line 361: mysql_select_db("[b]rootuser_5683194816_prod[/b]", $conn);...?>

hence selecting [\n\r]+ as beginning and end of lines - as there are almost duplicate lines that are commented out (preceeded by //). but this does not match on either of these :)... = other arbitrary code to the purpose of what we are dealing with here.somebody please help! :(it is still in connection with the putdbconfig and getdbconfig functions, but i cannot seem to find the problem - even on gingko giloba with an influx of water 0o really frustrating this :/

function putdbconfig($file = "") {	//if ($_SERVER['HTTPS'] == "on") {		$pattern = "/[color="#9ACD32"][\r\n]+[/color]mysql_select_db\(\"([^\"]*)\", \$conn\);[color="#FF8C00"][\r\n]+[/color][color="#FF0000"]|[/color][color="#9ACD32"][\r\n]+[/color]\$conn = mysql_connect\(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\"\);[color="#FF8C00"][\r\n]+[/color]/";		$connstr = file_get_contents($file, FILE_USE_INCLUDE_PATH);		echo "<test>".$connstr."</test>\n";		// get data to be replaced		preg_match_all($pattern,$connstr,$arr,PREG_PATTERN_ORDER);		// replace the data in $arr[1] with the following posted array		$arr[0][0] = $_POST["text_dbuser"];		$arr[0][1] = $_POST["text_dbpass"];		$arr[0][2] = $_POST["text_dbname"];		preg_replace(preg_prepare($arr[1]),$arr[0],$connstr);		// overwrite $file with replaced data		file_put_contents($file, $connstr, FILE_USE_INCLUDE_PATH);		// write data that was written below:		if (preg_match_all($pattern,$connstr,$arr,PREG_PATTERN_ORDER)) {			echo "	<conn dbname=\"".$arr[1][2]."\" user=\"".$arr[1][0]."\" pass=\"".$arr[1][1]."\" />\n";		}	//}}function getdbconfig($file = "") {	//if ($_SERVER['HTTPS'] == "on") {		$pattern = "/[color="#9ACD32"][\r\n]+[/color]mysql_select_db\(\"([^\"]*)\", \$conn\);[color="#FF8C00"][\r\n]+[/color][color="#FF0000"]|[/color][color="#9ACD32"][\r\n]+[/color]\$conn = mysql_connect\(\"[^\"]*\", \"([^\"]*)\", \"([^\"]*)\"\);[color="#FF8C00"][\r\n]+[/color]/";		$connstr = file_get_contents($file, FILE_USE_INCLUDE_PATH);		echo "<test>".$connstr."</test>\n";		if (preg_match_all($pattern,$connstr,$arr,PREG_PATTERN_ORDER)) {			echo "	<conn dbname=\"".$arr[1][2]."\" user=\"".$arr[1][0]."\" pass=\"".$arr[1][1]."\" />\n";		}	//}}

i have printed the $connstr string - and it is the full content of the connection include. why will preg_match_all not work? <_<ps: the commented checking for https is while the isp sort out the configuration on the domain.

Link to comment
Share on other sites

ok i have modified the $pattern to read as follows.

$pattern = '/^\$conn = mysql_connect\("[a-zA-Z._0-9\-]+", "([a-zA-Z_0-9]+)", "([a-zA-Z_0-9]+)"\);$|^mysql_select_db\("([a-zA-Z_0-9]+)", \$conn\);$/m';

but unfortunately it still only returns the database user! :)

<conn dbname="" user="testuser" pass="" />

regular expressions are giving me a headache again! :/

Link to comment
Share on other sites

ahhh success at last ^^ decided to rather go with str_replace, matching and replacing on the enclosing quotes to be safe. also, i looked at the returned array via print_r - and it was not as expected at all. also added retrieval and setting of server and timezone, along with the user, pass and db from this connection include :)

function strenclose($str, $chars) {	return $chars.$str.$chars;}function putdbconfig($file = "") {	//if ($_SERVER['HTTPS'] == "on") {		$pattern = '/^\$conn = mysql_connect\("([a-zA-Z._0-9\-]+)", "([a-zA-Z_0-9]+)", "([a-zA-Z_0-9]+)"\);$'.				   '|^mysql_select_db\("([a-zA-Z_0-9]+)", \$conn\);$'.				   '|^date_default_timezone_set\([\'"]([A-Za-z0-9\/+-]+)[\'"]\);$/m';		$connstr = file_get_contents($file, FILE_USE_INCLUDE_PATH);		//echo "<test>".$connstr."</test>\n";		// get data to be replaced		preg_match_all($pattern,$connstr,$arr,PREG_PATTERN_ORDER);		// replace the data in $arr[1] with the following posted array		$arrget = array(						strenclose($arr[1][0],"\""),			// old dbsrvr						strenclose($arr[4][1],"\""),			// old dbname						strenclose($arr[2][0],"\""),			// old dbuser						strenclose($arr[3][0],"\""),			// old dbpass						strenclose($arr[5][2],"\"")				// old php_tz				);		$arrput = array(						strenclose($_POST["text_dbsrvr"],"\""),	// new dbsrvr						strenclose($_POST["text_dbname"],"\""),	// new dbname						strenclose($_POST["text_dbuser"],"\""),	// new dbuser						strenclose($_POST["text_dbpass"],"\""),	// new dbpass						strenclose($_POST["text_php_tz"],"\"")	// new php_tz				);		$connstr = str_replace($arrget,$arrput,$connstr);		// overwrite $file with replaced data		$ret = file_put_contents($file, $connstr, FILE_USE_INCLUDE_PATH | LOCK_EX);		//echo "<test>".$ret."</test>\n";		$connstr = file_get_contents($file, FILE_USE_INCLUDE_PATH);		// write data that was written below:		if (preg_match_all($pattern,$connstr,$arr,PREG_PATTERN_ORDER)) {			echo "<test>";			print_r($arr);			echo "</test>\n";			echo "	<conn dbsrvr=\"".$arr[1][0]."\" dbname=\"".$arr[4][1]."\" user=\"".$arr[2][0]."\" pass=\"".$arr[3][0]."\" php_tz=\"".$arr[5][2]."\" />\n";		}	//}}function getdbconfig($file = "") {	//if ($_SERVER['HTTPS'] == "on") {		$pattern = '/^\$conn = mysql_connect\("([a-zA-Z._0-9\-]+)", "([a-zA-Z_0-9]+)", "([a-zA-Z_0-9]+)"\);$'.				   '|^mysql_select_db\("([a-zA-Z_0-9]+)", \$conn\);$'.				   '|^date_default_timezone_set\([\'"]([A-Za-z0-9\/+\-]+)[\'"]\);$/m';		$connstr = file_get_contents($file, FILE_USE_INCLUDE_PATH);		//echo "<test>".$connstr."</test>\n";		if (preg_match_all($pattern,$connstr,$arr,PREG_PATTERN_ORDER)) {			echo "<test>";			print_r($arr);			echo "</test>\n";			echo "	<conn dbsrvr=\"".$arr[1][0]."\" dbname=\"".$arr[4][1]."\" user=\"".$arr[2][0]."\" pass=\"".$arr[3][0]."\" php_tz=\"".$arr[5][2]."\" />\n";		}	//}}

this issue has now been RESOLVED (google is prolly tired of my regex searches by now tee hee) :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...