Imaulle Posted April 7, 2010 Share Posted April 7, 2010 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 More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 it's like the html stuff gets printed but then the rest of the PHP stuff keeps going and it doesnt wait for the password that I need to input. Is it possible to do this all in one file? Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038180 Share on other sites More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 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 More sharing options...
five Posted April 7, 2010 Share Posted April 7, 2010 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 More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 okay thanks! I'll try that way now... I've gotten the 3 https commands to work. the db/user and permissions all work correctly. However it seems the way the db is imported in from the file doesnt work correctly... Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038225 Share on other sites More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 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 More sharing options...
five Posted April 7, 2010 Share Posted April 7, 2010 and the error is? Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038240 Share on other sites More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 I'm not quite sure... the website doesn't work the way it's supposed to. but when I clear the database and manually import database.sql from phpMyadmin it works correctly. Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038251 Share on other sites More sharing options...
five Posted April 7, 2010 Share Posted April 7, 2010 okay. probably the explode is simplistic. are you sure that in your sql file every query ends with ; ? like if there is a query that ends with a line break phpMyAdmin may be parsing it correctly.. but your explode will put two queries (or more) as one query. Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038276 Share on other sites More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 grrrr I can't tell... I compared a dump of the working SQL db to the dump of the db I'm trying to upload from the setup.php and it looks the same to me. It's like it's only a few things that are broken from the setup.php db import :[ Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038310 Share on other sites More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 whoa! okay yeah I exported the broken db and it was 94 kb. but the export of the working db was 132 kb crap... Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038313 Share on other sites More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 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 More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 oh crap I figured out why the explode isn't working. The db has some stuff in it that has ; so it isn't getting the entire query!!! no clue how to fix that now lol Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038332 Share on other sites More sharing options...
five Posted April 7, 2010 Share Posted April 7, 2010 i think phpMyAdmin is open source. not sure though.. another option that might work on your .sql file may be exploding in ";\n" Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038338 Share on other sites More sharing options...
Imaulle Posted April 7, 2010 Author Share Posted April 7, 2010 I was able to just remove the ; that were not at the end of the queries. Ok so now... How to make the https commands into just one line? btw thanks again for your help! Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038636 Share on other sites More sharing options...
five Posted April 8, 2010 Share Posted April 8, 2010 $b = file_get_contents("https://".$username.":".$password."@localhost:2083/frontend/x3/sql/addb.html?db=archive"); Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038809 Share on other sites More sharing options...
Imaulle Posted April 8, 2010 Author Share Posted April 8, 2010 hrrrm. okay thanks! Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1038864 Share on other sites More sharing options...
Imaulle Posted April 8, 2010 Author Share Posted April 8, 2010 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 More sharing options...
Imaulle Posted April 8, 2010 Author Share Posted April 8, 2010 nevermind I'm a moron! lol Link to comment https://forums.phpfreaks.com/topic/197831-php-help-please/#findComment-1039211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.