PDA

View Full Version : remotely create a new database



mercury1231
09-22-2008, 11:58 PM
I'm trying to access to my MySQL database. After I added my IP address to the whitelist and turned on remote MySQL access, I was able to connect to the database (using third-party applications such as MySQL Query Browser). However, it's weird that I could not create a new database by executing SQL queries. It worked when I created a new database in the cPanel. I'm confused. Please help me. I've got to do this since this is part of an application that I need to implement.

shadmego
09-23-2008, 06:20 AM
The only way I am aware of being able to actually create the database remotely is by running the installations from SimpleScripts or Fantastico. This might happen because their scripts use a provided user that has permissions to create databases.

I don't know how HM has the database set up, but I am willing to but a beer on the fact that they don't let regular accounts create databases remotely. You have to log into your CPanel account and create the database there, then you can modify the thing remotely with your MySQL Query Browser.

You should verify this with support though.

There also *might* be a way to get around this by searching for a script that will create the database automatically. Much like the way you can run a script to create subdomains and email addresses based on a form being filled out, there might be a similar way to create the database from *outside* CPanel by sending the supplied information *through* the CPanel parsing engine.

I'm not at all strong enough with php to help with this, but if it's possible, I know a few people that would probably be willing to give it a shot.

~NOTE~ If it is against HM policy to remotely create databases, then you should not attempt what I suggested. Again, I urge you to verify this issue with Support and read the TOS.

~regards, and welcome to the community

RDM
09-23-2008, 08:23 AM
I just recently wanted to do the same thing so I wrote this to do exactly that:



include('curl.class.php');
$curl = new Curl();

//cPanel Info
$box_num = 000;
$cPanel_user = 'CPANEL_USERNAME';
$cPanel_pass = 'CPANEL_PASSWORD';

//Database to create
$db = 'NEW_DB';
$db_user = 'DB_USER';
$db_pass = 'DB_PASS';

//Permission must stay in uppercase. This will give the user full permissions. Remove the ones that you don't want.
$permissions = array('SELECT' => 'SELECT', 'INSERT' => 'INSERT', 'UPDATE' => 'UPDATE', 'DELETE' => 'DELETE',
'CREATE' => 'CREATE', 'ALTER' => 'ALTER', 'DROP' => 'DROP', 'LOCKTABLES' => 'LOCK',
'INDEX' => 'INDEX', 'REFERENCES' => 'REFERENCES', 'CREATETEMPORARYTABLES' => 'TEMPORARY',
'CREATEROUTINE' => 'CREATEROUTINE');


//Do It
$host = 'http://' . $cPanel_user . ':' . $cPanel_pass . '@host' . $box_num . '.hostmonster.com';
$url = $host . ':2082/frontend/hostmonster/sql/addb.html';
$data = array('db' => $db);
$results = $curl->post($url,$data);
if (empty($results['error'])) {
echo 'Database created<br/>';
$url = $host . ':2082/frontend/hostmonster/sql/adduser.html';
$data = array('user' => $db_user, 'pass' => $db_pass, 'pass2' => $db_pass);
$results = $curl->post($url,$data);
if (empty($results['error'])) {
echo 'User created<br/>';
$url = $host . ':2082/frontend/hostmonster/sql/addusertodb.html';
$data_user = array('user' => $cPanel_user . '_' . $db_user,'db' => $cPanel_user . '_' . $db);
$data = array_merge($data_user,$permissions);
$results = $curl->post($url,$data);
if (empty($results['error'])) {
echo 'User added to database';
}
else {
die('There was and error adding the user to the database');
}
}
else {
die('There was and error creating the user');
}
}
else {
die('There was and error creating the database');
}
You will also need this Curl class:

class Curl {
public $m_caseless;
public $m_handle;
public $m_header;
public $m_options;
public $m_status;
public $m_followed;

public function __construct() {}

public function setCurl($theURL=null) {
if (!function_exists('curl_init')) {
trigger_error('PHP was not built with --with-curl, rebuild PHP to use the curl class.', E_USER_ERROR);
}

$this->m_handle = curl_init();

$this->m_caseless = null;
$this->m_header = null;
$this->m_options = null;
$this->m_status = null;
$this->m_followed = null;

if (!empty($theURL)) {
$this->setopt(CURLOPT_URL, $theURL);
}

$this->setopt(CURLOPT_HEADER, false);
$this->setopt(CURLOPT_RETURNTRANSFER, true);
}

public function post($url,$post) {
$this->setCurl($url);

$this->setopt(CURLOPT_FOLLOWLOCATION, true);
$this->setopt(CURLOPT_POST, true);
$this->setopt(CURLOPT_POSTFIELDS, $this->asPostString($post));

$results = array();
$results['exec'] = $this->exec();
$results['error'] = $this->hasError();

$this->close();

return $results;
}

function get($url,$get) {
$this->setCurl(urldecode($url . '?' . $this->asPostString($get)));

$this->setopt(CURLOPT_FOLLOWLOCATION, true);
$this->setopt(CURLOPT_HTTPHEADER, array("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
"Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3",
"Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 300")
);
$this->setopt(CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
$this->setopt(CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

$results = array();
$results['exec'] = $this->exec();
$results['error'] = $this->hasError();
$this->close();

return $results;
}

public function close() {
curl_close($this->m_handle);
$this->m_handle = null;
}

public function exec() {
$theReturnValue = curl_exec($this->m_handle);

$this->m_status = curl_getinfo($this->m_handle);
$this->m_status['errno'] = curl_errno($this->m_handle);
$this->m_status['error'] = curl_error($this->m_handle);

$this->m_header = null;

if ($this->m_status['errno']) {
return '';
}

if ($this->getOption(CURLOPT_HEADER)) {

$this->m_followed = array();
$rv = $theReturnValue;

while (count($this->m_followed) <= $this->m_status['redirect_count']) {
$theArray = preg_split("/(\r\n){2,2}/", $rv, 2);
$this->m_followed[] = $theArray[0];
$rv = $theArray[1];
}

$this->parseHeader($theArray[0]);

return $theArray[1];
}
else {
return $theReturnValue;
}
}

public function getHeader($theHeader=null) {
if (empty($this->m_header)) {
return false;
}

if (empty($theHeader)) {
return $this->m_header;
}
else {
$theHeader = strtoupper($theHeader);
if (isset($this->m_caseless[$theHeader])) {
return $this->m_header[$this->m_caseless[$theHeader]];
}
else {
return false;
}
}
}

public function getOption($theOption) {
if (isset($this->m_options[$theOption])) {
return $this->m_options[$theOption];
}

return null;
}

public function hasError() {
if (isset($this->m_status['error'])) {
return (empty($this->m_status['error']) ? false : $this->m_status['error']);
}
else {
return false;
}
}

public function parseHeader($theHeader) {
$this->m_caseless = array();

$theArray = preg_split("/(\r\n)+/", $theHeader);

if (preg_match('/^HTTP/', $theArray[0])) {
$theArray = array_slice($theArray, 1);
}

foreach ($theArray as $theHeaderString) {
$theHeaderStringArray = preg_split("/\s*:\s*/", $theHeaderString, 2);

$theCaselessTag = strtoupper($theHeaderStringArray[0]);

if (!isset($this->m_caseless[$theCaselessTag])) {
$this->m_caseless[$theCaselessTag] = $theHeaderStringArray[0];
}

$this->m_header[$this->m_caseless[$theCaselessTag]][] = $theHeaderStringArray[1];
}
}

public function getStatus($theField=null) {
if (empty($theField)) {
return $this->m_status;
}
else {
if (isset($this->m_status[$theField])) {
return $this->m_status[$theField];
}
else {
return false;
}
}
}

public function setopt($theOption, $theValue) {
curl_setopt($this->m_handle, $theOption, $theValue);
$this->m_options[$theOption] = $theValue;
}

public function &fromPostString(&$thePostString) {
$return = array();
$fields = explode('&', $thePostString);
foreach($fields as $aField) {
$xxx = explode('=', $aField);
$return[$xxx[0]] = urldecode($xxx[1]);
}

return $return;
}

public function &asPostString(&$theData, $theName = NULL) {
$thePostString = '';
$thePrefix = $theName;

if (is_array($theData)) {
foreach ($theData as $theKey => $theValue) {
if ($thePrefix === NULL) {
$thePostString .= '&' . curl::asPostString($theValue, $theKey);
}
else {
$thePostString .= '&' . curl::asPostString($theValue, $thePrefix . '[' . $theKey . ']');
}
}
}
else {
$thePostString .= '&' . urlencode((string)$thePrefix) . '=' . urlencode($theData);
}

$xxx =& substr($thePostString, 1);

return $xxx;
}

public function getFollowedHeaders() {
$theHeaders = array();
if ($this->m_followed) {
foreach ( $this->m_followed as $aHeader) {
$theHeaders[] = explode( "\r\n", $aHeader);
}
return $theHeaders;
}

return $theHeaders;
}
}

mercury1231
09-23-2008, 03:27 PM
thanks a lot for the help!! I really appreciate them.

sayad_sandrine
10-11-2008, 06:02 AM
Hello,
I get an error can you help me please

Parse error: syntax error, unexpected '=', expecting ')' in /home2/courssco/public_html/cpanel.php on line 16