![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 require_once('PEAR.php'); 00003 if ( !defined('FILE_APPEND') ){ 00004 define('FILE_APPEND', 1); 00005 } 00006 if ( !function_exists('file_put_contents') ) { 00007 00008 function file_put_contents($n, $d, $flag = false) { 00009 $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w'; 00010 $f = @fopen($n, $mode); 00011 if ($f === false) { 00012 return 0; 00013 } else { 00014 if (is_array($d)) $d = implode($d); 00015 $bytes_written = fwrite($f, $d); 00016 fclose($f); 00017 return $bytes_written; 00018 } 00019 } 00020 } 00021 00022 define('DB_HOST', 'localhost'); // This is the host of your mysql dbms 00023 ini_set('include_path','.'.PATH_SEPARATOR.'lib'); 00024 set_time_limit(1500); 00025 class Dataface_Installer { 00026 00027 function createApplicationArchive($conf, $path=null){} 00028 function installApplicationArchive($path){} 00029 function prepareApplicationArchive($path){} 00030 function authenticate(){ 00031 header('WWW-Authenticate: Basic realm="Dataface Installer"'); 00032 header('HTTP/1.0 401 Unauthorized'); 00033 setcookie('logged_in',1); 00034 echo 'Please enter your MySQL Username and password to access this page'; 00035 00036 exit; 00037 00038 } 00039 00040 00041 function logout(){ 00042 //echo "here"; 00043 setcookie("logged_in", "", time() - 3600); 00044 header('Location: '.$_SERVER['PHP_SELF']); 00045 exit; 00046 } 00047 00048 function mainMenu(){ 00049 include('install'.DIRECTORY_SEPARATOR.'mainMenu.inc.php'); 00050 } 00051 00052 00053 function infoLink($id){ 00054 return '<img src="images/info.gif" onclick="fieldInfo(\''.$id.'\');" />'; 00055 00056 } 00057 00058 function archive2app(){ 00059 00060 require_once 'HTML/QuickForm.php'; 00061 $form = new HTML_QuickForm('fromarchive'); 00062 00063 $form->addElement('hidden', '-action', 'archive2app'); 00064 00065 $form->addElement('file','archive', 'Installation Archive'.$this->infoLink('archive2app.archive')); 00066 $form->addElement('text','database_name','Database Name '.$this->infoLink('archive2app.database_name')); 00067 00068 00069 $form->addElement('text','mysql_user', 'MySQL Username '.$this->infoLink('archive2app.mysql_user')); 00070 $form->addElement('password', 'mysql_password', 'MySQL Password'); 00071 $form->addElement('checkbox', 'create_user', 'Create user '.$this->infoLink('archive2app.create_user')); 00072 00073 $form->addElement('select','install_type', 'Installation type '.$this->infoLink('archive2app.install_type'), array( 00074 '' => 'Please select ...', 00075 'download_tarball' => 'Download Tarball', 00076 'ftp_install' => 'Install on server (using FTP)' 00077 ), 00078 array('onchange'=>"listeners.install_type.onchange(this);") 00079 ); 00080 00081 $form->addElement('header', 'ftp_info', 'FTP Connection Info'); 00082 $form->addElement('text', 'ftp_host', 'FTP Host'); 00083 $form->addElement('checkbox', 'ftp_ssl', 'Use SSL'); 00084 $form->addElement('text', 'ftp_path', 'FTP Path'); 00085 $form->addElement('text', 'ftp_username', 'FTP Username'); 00086 $form->addElement('password', 'ftp_password', 'FTP Password'); 00087 00088 $form->addElement('submit','submit','Submit'); 00089 00090 00091 $form->addRule('database_name','Please select a database', 'required', null,'client'); 00092 $form->addRule('mysql_user', 'Please enter a mysql username that the application can connect as.', 'required',null,'client'); 00093 $form->addRule('install_type', 'Please select an installation type and then click submit.', 'required', null, 'client'); 00094 $form->addRule('archive', 'Please choose the application tar.gz file to upload', 'uploadedfile',null,'client'); 00095 00096 $form->setDefaults(array( 00097 'mysql_user'=>$_SERVER['PHP_AUTH_USER'], 00098 'mysql_password'=>$_SERVER['PHP_AUTH_PW'] 00099 ) 00100 ); 00101 00102 if ( $form->validate() ){ 00103 $res = $form->process(array(&$this,'archive2app__process'), true); 00104 if ( PEAR::isError($res) ){ 00105 die($res->getMessage()); 00106 } 00107 } 00108 require_once 'HTML/QuickForm/Renderer/Array.php'; 00109 $renderer = new HTML_QuickForm_Renderer_Array(true,true,true); 00110 $form->accept($renderer); 00111 00112 $context = $renderer->toArray(); 00113 00114 ob_start(); 00115 $form->display(); 00116 $out = ob_get_contents(); 00117 ob_end_clean(); 00118 include 'install'.DIRECTORY_SEPARATOR.'archive2app.inc.php'; 00119 } 00120 00121 function archive2app__process($values){ 00122 require_once 'Archive/Tar.php'; 00123 00124 if ( preg_match('/\.gz$/', $_FILES['archive']['name']) ){ 00125 $compression = 'gz'; 00126 } else { 00127 $compression = null; 00128 } 00129 $archive = new Archive_Tar($_FILES['archive']['tmp_name'], $compression); 00130 $files = $archive->listContent(); 00131 foreach ( $files as $file ){ 00132 if ( !preg_match('/(\.ini)|(\.php)$/', $file['filename']) ){ 00133 continue; 00134 } 00135 $content = $archive->extractInString($file['filename']); 00136 $content = str_replace( 00137 array( 00138 '%%DATAFACE_URL%%', 00139 '%%DATAFACE_PATH%%', 00140 '%%MYSQL_USER%%', 00141 '%%MYSQL_PASSWORD%%', 00142 '%%MYSQL_HOST%%', 00143 '%%MYSQL_DATABASE_NAME%%' 00144 ), 00145 array( 00146 addslashes(dirname($_SERVER['PHP_SELF'])), 00147 addslashes(dirname(__FILE__)), 00148 addslashes($values['mysql_user']), 00149 addslashes($values['mysql_password']), 00150 addslashes(DB_HOST), 00151 addslashes($values['database_name']) 00152 ), 00153 $content 00154 ); 00155 $archive->addString($file['filename'], $content); 00156 00157 } 00158 $root = $files[0]['filename']; 00159 00160 $install = $archive->extractInString($root.'install/install.sql'); 00161 $res = mysql_select_db($values['database_name'], db()); 00162 if ( !$res ){ 00163 $dbname = str_replace('`','',$values['database_name']); 00164 $res = mysql_query("create database `".addslashes($dbname)."`", db()); 00165 if ( !$res ){ 00166 return PEAR::raiseError("Failed to create database '$dbname'"); 00167 } 00168 $res = mysql_select_db($dbname); 00169 if ( !$res ){ 00170 return PEAR::raiseError("Problem selecting database $dbname."); 00171 } 00172 } 00173 00174 if ( $install ){ 00175 $installFile = tempnam(null, 'install.sql'); 00176 file_put_contents($installFile, $install); 00177 00178 00179 $file = file($installFile); 00180 $queries = array(); 00181 $ctr = 0; 00182 foreach ($file as $line){ 00183 00184 if ( isComment($line) ) continue; 00185 $queries[$ctr] .= $line; 00186 $trimmed = trim($line); 00187 if ( $trimmed{strlen($trimmed)-1} == ';' ) $ctr++; 00188 00189 } 00190 00191 //$file = implode("",$out); 00192 foreach ($queries as $query){ 00193 00194 $res = @mysql_query($query, $db); 00195 if ( !$res ){ 00196 $my_errs[] = mysql_error($db); 00197 } 00198 } 00199 } 00200 00201 00202 00203 switch ($values['install_type'] ){ 00204 case 'ftp_install': 00205 //echo 'here'; 00206 require_once 'install/FTPExtractor.class.php'; 00207 $extractor = new FTPExtractor($archive); 00208 $res = $extractor->connect($values['ftp_host'], $values['ftp_username'], $values['ftp_password']); 00209 00210 if ( PEAR::isError($res) ){ 00211 die($res->getMessage()); 00212 } 00213 $res = $extractor->extract($values['ftp_path'],'/'); 00214 //if ( PEAR::isError($res) ){ 00215 // die($res->getMessage()); 00216 //} 00217 $context = array(); 00218 if ( PEAR::isError($res) ){ 00219 $context['result'] = 'Error: '.$res->getMessage(); 00220 } else { 00221 $context = $res; 00222 } 00223 include 'install'.DIRECTORY_SEPARATOR.'archive2app-results.inc.php'; 00224 exit; 00225 00226 default: // download_tarball 00227 $tarpath = $_FILES['archive']['tmp_name']; 00228 if ( $compression == 'gz' ){ 00229 $mimetype = 'application/x-gzip'; 00230 } else { 00231 $mimetype = 'application/x-tar'; 00232 } 00233 header('Content-type: '.$mimetype); 00234 header('Content-Disposition: attachment; filename="'.basename($_FILES['archive']['name']).'.tar.gz"'); 00235 echo file_get_contents($tarpath); 00236 exit; 00237 00238 } 00239 00240 00241 00242 00243 } 00244 00245 function db2app(){ 00246 require_once 'HTML/QuickForm.php'; 00247 $form = new HTML_QuickForm('db2app'); 00248 $res = mysql_list_dbs(db()); 00249 if ( !$res ) trigger_error(mysql_error(db()), E_USER_ERROR); 00250 $options = array('' => 'Please Select Database ...'); 00251 while ( $row = mysql_fetch_row($res) ) $options[$row[0]] = $row[0]; 00252 $form->addElement('hidden','-action','db2app'); 00253 $form->addElement('select', 'database_name','Select Database'.$this->infoLink('archive2app.database_name'), $options, array('onchange'=>'listeners.database_name.onchange(this)')); 00254 $form->addElement('header','db_info','Database connection details'); 00255 //$form->addElement('html', 'this is a test'); 00256 $form->addElement('text', 'mysql_user', 'MySQL Username '.$this->infoLink('archive2app.mysql_user')); 00257 $form->addElement('password', 'mysql_password', 'MySQL Password'); 00258 //$form->addElement('radio','output_format','Output options','Download as tar.gz archive','download'); 00259 //$form->addElement('radio','output_format','','Install on webserver in apps directory','install'); 00260 00261 $form->addElement('select','install_type', 'Installation type '.$this->infoLink('archive2app.install_type'), array( 00262 '' => 'Please select ...', 00263 'download_tarball' => 'Download Tarball', 00264 'ftp_install' => 'Install on server (using FTP)' 00265 ), 00266 00267 array('onchange'=>"listeners.install_type.onchange(this);") 00268 ); 00269 00270 $form->addElement('header', 'ftp_info', 'FTP Connection Info'); 00271 $form->addElement('text', 'ftp_host', 'FTP Host'); 00272 $form->addElement('checkbox', 'ftp_ssl', 'Use SSL'); 00273 $form->setDefaults(array('ftp_host'=>DB_HOST)); 00274 $form->addElement('text', 'ftp_path', 'FTP Path',array('size'=>50)); 00275 $form->setDefaults(array('ftp_path'=>$_SERVER['DOCUMENT_ROOT'])); 00276 $form->addElement('text', 'ftp_username', 'FTP Username'); 00277 $form->addElement('password', 'ftp_password', 'FTP Password'); 00278 00279 00280 $form->addElement('submit','submit','Submit'); 00281 00282 00283 $form->addRule('database_name','Please select a database', 'required', null,'client'); 00284 $form->addRule('mysql_user', 'Please enter a mysql username that the application can connect as.', 'required',null,'client'); 00285 $form->addRule('install_type', 'Please select an installation type and then click submit.', 'required', null, 'client'); 00286 $form->setDefaults(array( 00287 'mysql_user'=>$_SERVER['PHP_AUTH_USER'], 00288 'mysql_password'=>$_SERVER['PHP_AUTH_PW'] 00289 ) 00290 ); 00291 00292 if ( $form->validate() ){ 00293 $tarpath = $form->process(array(&$this,'db2app__process'), true); 00294 header('Content-type: application/x-gzip'); 00295 header('Content-Disposition: attachment; filename="'.basename($tarpath).'.tar.gz"'); 00296 echo file_get_contents($tarpath); 00297 exit; 00298 } 00299 00300 require_once 'HTML/QuickForm/Renderer/Array.php'; 00301 $renderer = new HTML_QuickForm_Renderer_Array(true,true,true); 00302 $form->accept($renderer); 00303 00304 $context = $renderer->toArray(); 00305 //print_r($context); 00306 00307 ob_start(); 00308 $form->display(); 00309 $out = ob_get_contents(); 00310 ob_end_clean(); 00311 include 'install'.DIRECTORY_SEPARATOR.'db2app.inc.php'; 00312 } 00313 00314 function db2app__process($values){ 00315 require_once 'Archive/Tar.php'; 00316 $tarpath = tempnam('/tmp',strval($values['database_name'])); 00317 //echo $tarpath; 00318 $compression='gz'; 00319 $archive = new Archive_Tar($tarpath,$compression); 00320 $path = strval($values['database_name']); 00321 $archive->addString($path.'/.htaccess', '<FilesMatch "\.ini$"> 00322 Deny from all 00323 </FilesMatch>'); 00324 $archive->addString($path.'/Web.config', file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'site_skeleton'.DIRECTORY_SEPARATOR.'Web.config')); 00325 00326 00327 00328 00329 mysql_select_db($values['database_name'], db()); 00330 $res = mysql_query('show tables', db()); 00331 if ( !$res ) trigger_error(mysql_error(db()), E_USER_ERROR); 00332 $tables = array(); 00333 while ( $row = mysql_fetch_row($res) ){ 00334 if ( $row[0]{0} == '_' ) continue; 00335 if ( strpos($row[0], 'dataface_') === 0 ) continue; 00336 if ( preg_match('/__history$/', $row[0]) ) continue; 00337 $tables[] = $row[0].' = "'.ucwords(str_replace('_',' ', $row[0])).'"'; 00338 } 00339 00340 $archive->addString($path.'/conf.ini',';;Configuration settings for application 00341 title="'.addslashes($values['database_name']).'" 00342 00343 [_database] 00344 host="'.DB_HOST.'" 00345 name="'.addslashes($values['database_name']).'" 00346 user="'.addslashes($values['mysql_user']).'" 00347 password="'.addslashes($values['mysql_password']).'" 00348 00349 [_tables] 00350 '.implode("\n",$tables).' 00351 ' 00352 ); 00353 00354 $archive->addString($path.'/index.php','<?php //Main Application access point 00355 require_once "'.addslashes(dirname(__FILE__).DIRECTORY_SEPARATOR.'public-api.php').'"; 00356 df_init(__FILE__, "'.addslashes(dirname($_SERVER['PHP_SELF'])).'")->display(); 00357 ' 00358 ); 00359 00360 00361 switch ($values['install_type'] ){ 00362 case 'ftp_install': 00363 //echo 'here'; 00364 require_once 'install/FTPExtractor.class.php'; 00365 $extractor = new FTPExtractor($archive); 00366 $res = $extractor->connect($values['ftp_host'], $values['ftp_username'], $values['ftp_password']); 00367 00368 if ( PEAR::isError($res) ){ 00369 die($res->getMessage()); 00370 } 00371 00372 00373 $res = $extractor->extract($values['ftp_path'],'/'); 00374 //if ( PEAR::isError($res) ){ 00375 // die($res->getMessage()); 00376 //} 00377 $context = array(); 00378 if ( PEAR::isError($res) ){ 00379 $context['result'] = 'Error: '.$res->getMessage(); 00380 } else { 00381 $context = $res; 00382 00383 00384 } 00385 include 'install'.DIRECTORY_SEPARATOR.'archive2app-results.inc.php'; 00386 exit; 00387 00388 default: // download_tarball 00389 //$tarpath = $_FILES['archive']['tmp_name']; 00390 if ( $compression == 'gz' ){ 00391 $mimetype = 'application/x-gzip'; 00392 } else { 00393 $mimetype = 'application/x-tar'; 00394 } 00395 header('Content-type: '.$mimetype); 00396 header('Content-Disposition: attachment; filename="'.basename($tarpath).'.tar.gz"'); 00397 echo file_get_contents($tarpath); 00398 exit; 00399 00400 } 00401 00402 //return $tarpath; 00403 00404 } 00405 00406 function test_db_access($dbname, $username, $password){ 00407 $db = @mysql_connect(DB_HOST, $username, $password); 00408 if ( !$db ){ 00409 return PEAR::raiseError("Could not connect to the MySQL server with username $username."); 00410 } 00411 00412 $res = mysql_select_db($dbname, $db); 00413 if ( !$res ) return PEAR::raiseError("Could not access the database $dbname as user $username."); 00414 00415 return true; 00416 } 00417 00418 function test_ftp_access($host, $path, $user, $password, $ssl=false){ 00419 require_once 'install/ftp.api.php'; 00420 require_once 'install/ftp.class.php'; 00421 if ( $ssl ){ 00422 $conn = ftp_ssl_connect($host); 00423 } else { 00424 $conn = ftp_connect($host); 00425 } 00426 if ( !$conn ) return PEAR::raiseError("Could not connect to FTP server"); 00427 00428 $res = @ftp_login($conn, $user, $password); 00429 if ( !$res ) return PEAR::raiseError("Failed to login to FTP server with the provided username ($user) and password"); 00430 00431 $res = @ftp_chdir($conn, $path); 00432 if ( !$res ){ 00433 return PEAR::raiseError("Failed: The directory $path on the server $host does not exist."); 00434 00435 } 00436 00437 return true; 00438 00439 } 00440 00441 function testdb(){ 00442 if ( !@$_REQUEST['-dbname'] || !$_REQUEST['-dbuser'] || !isset($_REQUEST['-dbpass']) ){ 00443 trigger_error("Please provide all of -dbname, -dbuser, and -dbpass parameters in the POST variables.", E_USER_ERROR); 00444 00445 } 00446 00447 $res = $this->test_db_access($_REQUEST['-dbname'], $_REQUEST['-dbuser'], $_REQUEST['-dbpass']); 00448 if ( PEAR::isError($res) ){ 00449 $msg = array( 00450 'success' => false, 00451 'message' => $res->getMessage() 00452 ); 00453 00454 00455 } else { 00456 $msg = array( 00457 'success' => true, 00458 'message' => 'Connected to database successfully' 00459 ); 00460 } 00461 00462 header('Content-type: text/json'); 00463 require_once 'Services/JSON.php'; 00464 $json = new Services_JSON; 00465 echo $json->encode($msg); 00466 exit; 00467 } 00468 00469 function testftp(){ 00470 if ( !@$_REQUEST['-ftphost'] || !$_REQUEST['-ftpuser'] || !isset($_REQUEST['-ftppass']) ){ 00471 trigger_error("Please provide all of -ftphost, -ftpuser, and -ftppass parameters in the POST variables.", E_USER_ERROR); 00472 00473 } 00474 00475 $res = $this->test_ftp_access($_REQUEST['-ftphost'], @$_REQUEST['-ftppath'], $_REQUEST['-ftpuser'], $_REQUEST['-ftppass'], @$_REQUEST['-ftpssl']); 00476 if ( PEAR::isError($res) ){ 00477 $msg = array( 00478 'success' => false, 00479 'message' => $res->getMessage() 00480 ); 00481 00482 00483 } else { 00484 $msg = array( 00485 'success' => true, 00486 'message' => 'Connected to FTP server successfully' 00487 ); 00488 } 00489 00490 header('Content-type: text/json'); 00491 require_once 'Services/JSON.php'; 00492 $json = new Services_JSON; 00493 echo $json->encode($msg); 00494 exit; 00495 } 00496 00497 } 00498 //print_r($_SERVER); 00499 function db(){ 00500 static $db=-1; 00501 if ( $db == -1 ){ 00502 if (!@$_SERVER['PHP_AUTH_USER'] || !$_COOKIE['logged_in'] ){ 00503 Dataface_Installer::authenticate(); 00504 } 00505 $db = @mysql_connect(DB_HOST,@$_SERVER['PHP_AUTH_USER'], @$_SERVER['PHP_AUTH_PW']); 00506 if ( !$db ){ 00507 Dataface_Installer::authenticate(); 00508 } 00509 } 00510 return $db; 00511 } 00512 00513 function isComment($line){ 00514 $line = trim($line); 00515 if ( strlen($line) > 1 and $line{0} == '-' and $line{1} == '-') return true; 00516 return false; 00517 } 00518 00519 00520 db(); 00521 00522 00523 $installer = new Dataface_Installer; 00524 switch (@$_REQUEST['-action']){ 00525 case 'testdb': 00526 $installer->testdb(); 00527 break; 00528 00529 case 'testftp': 00530 $installer->testftp(); 00531 break; 00532 00533 case 'logout': 00534 $installer->logout(); 00535 break; 00536 00537 case 'db2app': 00538 $installer->db2app(); 00539 break; 00540 00541 case 'archive2app': 00542 $installer->archive2app(); 00543 break; 00544 00545 default: 00546 Dataface_Installer::mainMenu(); 00547 00548 }