Jump to content

php help please


Imaulle

Recommended Posts

okay this is some code I've put together to help automate some website installs. Basically I'll ftp and upload 3 files archive.zip, database.sql, and this setup.php

 

Then I will run http://55.55.55.55/~user/setup.php

 

 

The first part works, the trouble is when it I try to get it to ask me for the password to use, so there is something wrong with my HTML code. Then I'm not positive but I think the sql stuff at the bottom might not work either lol. I'm really good with C++ but this php/sql stuff is new to me.

 

thanks for any input you can give me :)

 

<?php

ob_start();
system('unzip archive.zip');
system('chmod 777 -R archive/images/');
system('chmod 777 -R archive/music/');
ob_end_clean();

$server = $_SERVER['SERVER_ADDR'];
$getURL = explode("/",$_SERVER['PHP_SELF']);
$username = substr($getURL[1], 1);

$addInfo="<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

define ('DB_USER', '".$username."_user');
define ('DB_PASSWORD', 'z)4LC.%1qiT-');
define ('DB_HOST', 'localhost');
define ('DB_NAME', '".$username."_archive');";

$dbinfoFile = "archive/admin/php/dbinfo.php";
$contents = file_get_contents($dbinfoFile);
$contents = $addInfo.$contents;
$fp = fopen($dbinfoFile,"w");
fwrite($fp,$contents);
fclose($fp);
?>

<html>
<body>
<form name ="input">
Password: 
<input type="text" name="cpass"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

<?php

$password = $_POST["cpass"];

//CREATE DATABASE
$command="https://".$username.":".$password."@".$server.":2083/frontend/x3/sql/addb.html?db=archive";
$b = file_get_contents($command);

if (!$b){
//echo "CPanel - Error creating database.";
}
else{
//echo "CPanel - Database created.";
}

//CREATE USER FOR DATABASE
$command = "https://".$username.":".$password."@".$server.":2083/frontend/x3/sql/adduser.html?user=user&pass=pass";
$b = file_get_contents($command);

if (!$b){
//echo "CPanel - Error creating database user.";
}
else{
//echo "CPanel - Database user created.";
}

//GIVE DATABASE USER PERMISSIONS
$command = "https://".$username.":".$password."@".$server.":2083/frontend/x3/sql/addusertodb.html?user=user&db=archive&ALL=ALL";
$b = file_get_contents($command);

if (!$b){
//echo "CPanel - Error assigning user to database.";
}
else{
//echo "CPanel - User permissions set.";
}

ob_start();

$mlink = @mysql_connect($server, $username."_user", "pass");
mysql_select_db($username."_user", $mlink);

$file = file_get_contents("database.sql");
echo $file."<p>";
$queries = explode(";",$file);
for ($i=0,$c=count($queries);$i<$c;$i++){
if (!mysql_query($queries[$i],$mlink)){
//echo "Error during database import:";
}
}
ob_end_clean();

//SET CLIENT PASSWORD
define ('DB_USER', $username.'_user');
define ('DB_PASSWORD', 'z)4LC.%1qiT-');
define ('DB_HOST', 'localhost');
define ('DB_NAME', $username.'_archive');

$newPass =  crypt($password, '$1$rasmusle$');
$newPass = stripslashes($newPass);
$Query = "UPDATE Login SET Password='".$newPass."' WHERE LoginID='1'";
$Result = mysql_query($Query)
    or die ("<login><pass><msg>".$Query."</msg></pass></login>");


unlink("archive.zip");
unlink("database.sql");
unlink("setup.php");

echo "done.";
?>

Link to comment
https://forums.phpfreaks.com/topic/197831-php-help-please/
Share on other sites

hmmm. and I think this line

 

$command="https://".$username.":".$password."@".$server.":2083/frontend/x3/sql/addb.html?db=archive";

 

 

will not work if its https correct? do I need to use cURL ? or is there another way to do this. I'm guessing it wont work because the cert is invalid or something like that...

 

Link to comment
https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038204
Share on other sites

i think it would be better to use something like

 

<?php
if (!isset($_POST['cpass'])) {
?>

html form here 
<form name="myform" method="post" action="setup.php">
...
</form>

<?php
} else {
   $pass = $_POST['cpass'];
   // now do stuff
}

 

since what you want to do is use the same file, the way you have it it will always try to get the password from $_POST, even if that is not set as when the user sees the form. Also a minor fix in your form tag so to specify you need post and to use setup.php as the processing file of the form.

 

also are you sure it connects to mysql ?

Link to comment
https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038223
Share on other sites

okay cool! that worked ty!!!!

 

this is the sql stuff that is being weird now....


$mlink = @mysql_connect('localhost', $username."_user", "password");
mysql_select_db($username."_archive", $mlink);

$file = file_get_contents("database.sql");
echo $file."<p>";
$queries = explode(";",$file);
for ($i=0,$c=count($queries);$i<$c;$i++){
if (!mysql_query($queries[$i],$mlink)){
//echo "Error during database import:";
}
}

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038231
Share on other sites

new question... with this

 

$command="https://".$username.":".$password."@localhost:2083/frontend/x3/sql/addb.html?db=archive";
$b = file_get_contents($command);

if (!$b){
//echo "CPanel - Error creating database.";
}
else{
//echo "CPanel - Database created.";
}

 

 

is there a way to make this into one line? just the https command that I want to run? I don't want the error checking really...

 

Link to comment
https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038331
Share on other sites

okay not sure what I did lol. but now it seems the file that I'm trying to import gets echo too. but everything does seem to work correctly.

 

$mlink = @mysql_connect("localhost",$username."_user","password");
mysql_select_db($username."_archive", $mlink);

$file = file_get_contents("database.sql");
echo $file."<p>";
$queries = explode(";",$file);

for ($i=0,$c=count($queries);$i<$c;$i++){
mysql_query($queries[$i],$mlink);
}

$newPass = stripslashes(crypt($password,'$1$rasmusle$'));
mysql_query("UPDATE Login SET Password='".$newPass."' WHERE LoginID='1'",$mlink);

 

 

I'm going to guess it's the echo $file."<p>"; line.

 

is that even needed? lol

Link to comment
https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1039210
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.