2 require_once(
'PEAR.php');
3 if ( !defined(
'FILE_APPEND') ){
4 define(
'FILE_APPEND', 1);
6 if ( !function_exists(
'file_put_contents') ) {
8 function file_put_contents($n, $d, $flag =
false) {
9 $mode = ($flag == FILE_APPEND || strtoupper($flag) ==
'FILE_APPEND') ?
'a' :
'w';
10 $f = @fopen($n, $mode);
14 if (is_array($d)) $d = implode($d);
15 $bytes_written = fwrite(
$f, $d);
17 return $bytes_written;
22 define(
'DB_HOST',
'localhost');
23 ini_set(
'include_path',
'.'.PATH_SEPARATOR.
'lib');
31 header(
'WWW-Authenticate: Basic realm="Dataface Installer"');
32 header(
'HTTP/1.0 401 Unauthorized');
33 setcookie(
'logged_in',1);
34 echo
'Please enter your MySQL Username and password to access this page';
43 setcookie(
"logged_in",
"", time() - 3600);
44 header(
'Location: '.
$_SERVER[
'PHP_SELF']);
49 include(
'install'.DIRECTORY_SEPARATOR.
'mainMenu.inc.php');
54 return '<img src="images/info.gif" onclick="fieldInfo(\''.$id.
'\');
" />';
58 function archive2app(){
60 require_once 'HTML/QuickForm.php';
61 $form = new HTML_QuickForm('fromarchive');
63 $form->addElement('hidden', '-action', 'archive2app');
65 $form->addElement('file','archive', 'Installation Archive'.$this->infoLink('archive2app.archive'));
66 $form->addElement('text','database_name','Database Name '.$this->infoLink('archive2app.database_name'));
69 $form->addElement('text','mysql_user', 'MySQL Username '.$this->infoLink('archive2app.mysql_user'));
70 $form->addElement('password', 'mysql_password', 'MySQL Password');
71 $form->addElement('checkbox', 'create_user', 'Create user '.$this->infoLink('archive2app.create_user'));
73 $form->addElement('select','install_type', 'Installation type '.$this->infoLink('archive2app.install_type'), array(
74 '' => 'Please select ...',
75 'download_tarball' => 'Download Tarball',
76 'ftp_install' => 'Install on server (using FTP)'
78 array('onchange'=>"listeners.install_type.onchange(
this);
")
81 $form->addElement('header', 'ftp_info', 'FTP Connection Info');
82 $form->addElement('text', 'ftp_host', 'FTP Host');
83 $form->addElement('checkbox', 'ftp_ssl', 'Use SSL');
84 $form->addElement('text', 'ftp_path', 'FTP Path');
85 $form->addElement('text', 'ftp_username', 'FTP Username');
86 $form->addElement('password', 'ftp_password', 'FTP Password');
88 $form->addElement('submit','submit','Submit');
91 $form->addRule('database_name','Please select a database', 'required', null,'client');
92 $form->addRule('mysql_user', 'Please enter a mysql username that the application can connect as.', 'required',null,'client');
93 $form->addRule('install_type', 'Please select an installation type and then click submit.', 'required', null, 'client');
94 $form->addRule('archive', 'Please choose the application tar.gz file to upload', 'uploadedfile',null,'client');
96 $form->setDefaults(array(
97 'mysql_user'=>$_SERVER['PHP_AUTH_USER'],
98 'mysql_password'=>$_SERVER['PHP_AUTH_PW']
102 if ( $form->validate() ){
103 $res = $form->process(array(&$this,'archive2app__process'), true);
104 if ( PEAR::isError($res) ){
105 die($res->getMessage());
108 require_once 'HTML/QuickForm/Renderer/Array.php';
109 $renderer = new HTML_QuickForm_Renderer_Array(true,true,true);
110 $form->accept($renderer);
112 $context = $renderer->toArray();
116 $out = ob_get_contents();
118 include 'install'.DIRECTORY_SEPARATOR.'archive2app.inc.php';
121 function archive2app__process($values){
122 require_once 'Archive/Tar.php';
124 if ( preg_match('/\.gz$/', $_FILES['archive']['name']) ){
129 $archive = new Archive_Tar($_FILES['archive']['tmp_name'], $compression);
130 $files = $archive->listContent();
131 foreach ( $files as $file ){
132 if ( !preg_match('/(\.ini)|(\.php)$/', $file['filename']) ){
135 $content = $archive->extractInString($file['filename']);
136 $content = str_replace(
141 '%%MYSQL_PASSWORD%%',
143 '%%MYSQL_DATABASE_NAME%%'
146 addslashes(dirname($_SERVER['PHP_SELF'])),
147 addslashes(dirname(__FILE__)),
148 addslashes($values['mysql_user']),
149 addslashes($values['mysql_password']),
151 addslashes($values['database_name'])
155 $archive->addString($file['filename'], $content);
158 $root = $files[0]['filename'];
160 $install = $archive->extractInString($root.'install/install.sql');
161 $res = mysql_select_db($values['database_name'], db());
163 $dbname = str_replace('`','',$values['database_name']);
164 $res = mysql_query("create database `
".addslashes($dbname)."`
", db());
166 return PEAR::raiseError("Failed to create database
'$dbname'");
168 $res = mysql_select_db($dbname);
170 return PEAR::raiseError("Problem selecting database $dbname.
");
175 $installFile = tempnam(null, 'install.sql');
176 file_put_contents($installFile, $install);
179 $file = file($installFile);
182 foreach ($file as $line){
184 if ( isComment($line) ) continue;
185 $queries[$ctr] .= $line;
186 $trimmed = trim($line);
187 if ( $trimmed{strlen($trimmed)-1} == ';' ) $ctr++;
191 //$file = implode("",$out);
192 foreach ($queries as $query){
194 $res = @mysql_query($query, $db);
196 $my_errs[] = mysql_error($db);
203 switch ($values['install_type'] ){
206 require_once 'install/FTPExtractor.class.php';
207 $extractor = new FTPExtractor($archive);
208 $res = $extractor->connect($values['ftp_host'], $values['ftp_username'], $values['ftp_password']);
210 if ( PEAR::isError($res) ){
211 die($res->getMessage());
213 $res = $extractor->extract($values['ftp_path'],'/');
214 //if ( PEAR::isError($res) ){
215 // die($res->getMessage());
218 if ( PEAR::isError($res) ){
219 $context['result'] = 'Error: '.$res->getMessage();
223 include 'install'.DIRECTORY_SEPARATOR.'archive2app-results.inc.php';
226 default: // download_tarball
227 $tarpath = $_FILES['archive']['tmp_name'];
228 if ( $compression == 'gz' ){
229 $mimetype = 'application/x-gzip';
231 $mimetype = 'application/x-tar';
233 header('Content-type: '.$mimetype);
234 header('Content-Disposition: attachment; filename="'.basename($_FILES['archive
']['name
']).'.tar.gz
"');
235 echo file_get_contents($tarpath);
246 require_once 'HTML/QuickForm.php';
247 $form = new HTML_QuickForm('db2app');
248 $res = mysql_list_dbs(db());
249 if ( !$res ) trigger_error(mysql_error(db()), E_USER_ERROR);
250 $options = array('' => 'Please Select Database ...');
251 while ( $row = mysql_fetch_row($res) ) $options[$row[0]] = $row[0];
252 $form->addElement('hidden','-action','db2app');
253 $form->addElement('select', 'database_name','Select Database'.$this->infoLink('archive2app.database_name'), $options, array('onchange'=>'listeners.database_name.onchange(this)'));
254 $form->addElement('header','db_info','Database connection details');
255 //$form->addElement('html', 'this is a test');
256 $form->addElement('text', 'mysql_user', 'MySQL Username '.$this->infoLink('archive2app.mysql_user'));
257 $form->addElement('password', 'mysql_password', 'MySQL Password');
258 //$form->addElement('radio','output_format','Output options','Download as tar.gz archive','download');
259 //$form->addElement('radio','output_format','','Install on webserver in apps directory','install');
261 $form->addElement('select','install_type', 'Installation type '.$this->infoLink('archive2app.install_type'), array(
262 '' => 'Please select ...',
263 'download_tarball' => 'Download Tarball',
264 'ftp_install' => 'Install on server (using FTP)'
267 array('onchange'=>"listeners.install_type.onchange(
this);
")
270 $form->addElement('header', 'ftp_info', 'FTP Connection Info');
271 $form->addElement('text', 'ftp_host', 'FTP Host');
272 $form->addElement('checkbox', 'ftp_ssl', 'Use SSL');
273 $form->setDefaults(array('ftp_host'=>DB_HOST));
274 $form->addElement('text', 'ftp_path', 'FTP Path',array('size'=>50));
275 $form->setDefaults(array('ftp_path'=>$_SERVER['DOCUMENT_ROOT']));
276 $form->addElement('text', 'ftp_username', 'FTP Username');
277 $form->addElement('password', 'ftp_password', 'FTP Password');
280 $form->addElement('submit','submit','Submit');
283 $form->addRule('database_name','Please select a database', 'required', null,'client');
284 $form->addRule('mysql_user', 'Please enter a mysql username that the application can connect as.', 'required',null,'client');
285 $form->addRule('install_type', 'Please select an installation type and then click submit.', 'required', null, 'client');
286 $form->setDefaults(array(
287 'mysql_user'=>$_SERVER['PHP_AUTH_USER'],
288 'mysql_password'=>$_SERVER['PHP_AUTH_PW']
292 if ( $form->validate() ){
293 $tarpath = $form->process(array(&$this,'db2app__process'), true);
294 header('Content-type: application/x-gzip');
295 header('Content-Disposition: attachment; filename="'.basename($tarpath).'.tar.gz
"');
296 echo file_get_contents($tarpath);
300 require_once 'HTML/QuickForm/Renderer/Array.php';
301 $renderer = new HTML_QuickForm_Renderer_Array(true,true,true);
302 $form->accept($renderer);
304 $context = $renderer->toArray();
309 $out = ob_get_contents();
311 include 'install'.DIRECTORY_SEPARATOR.'db2app.inc.php';
314 function db2app__process($values){
315 require_once 'Archive/Tar.php';
316 $tarpath = tempnam('/tmp',strval($values['database_name']));
319 $archive = new Archive_Tar($tarpath,$compression);
320 $path = strval($values['database_name']);
321 $archive->addString($path.'/.htaccess', '<FilesMatch "\.ini$
">
324 $archive->addString($path.'/Web.config', file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'site_skeleton'.DIRECTORY_SEPARATOR.'Web.config'));
329 mysql_select_db($values['database_name'], db());
330 $res = mysql_query('show tables', db());
331 if ( !$res ) trigger_error(mysql_error(db()), E_USER_ERROR);
333 while ( $row = mysql_fetch_row($res) ){
334 if ( $row[0]{0} == '_' ) continue;
335 if ( strpos($row[0], 'dataface_') === 0 ) continue;
336 if ( preg_match('/__history$/', $row[0]) ) continue;
337 $tables[] = $row[0].' = "'.ucwords(str_replace('_
',' ', $row[0])).'"';
340 $archive->addString($path.'/conf.ini',';;Configuration settings for application
341 title="'.addslashes($values['database_name
']).'"
345 name="'.addslashes($values['database_name
']).'"
346 user="'.addslashes($values['mysql_user
']).'"
347 password="'.addslashes($values['mysql_password
']).'"
350 '.implode("\n
",$tables).'
354 $archive->addString($path.'/index.php','<?php //Main Application access point
355 require_once "'.addslashes(dirname(__FILE__).DIRECTORY_SEPARATOR.'public-api.php
').'";
356 df_init(__FILE__, "'.addslashes(dirname($_SERVER['PHP_SELF
'])).'")->display();
361 switch ($values['install_type'] ){
364 require_once 'install/FTPExtractor.class.php';
365 $extractor = new FTPExtractor($archive);
366 $res = $extractor->connect($values['ftp_host'], $values['ftp_username'], $values['ftp_password']);
368 if ( PEAR::isError($res) ){
369 die($res->getMessage());
373 $res = $extractor->extract($values['ftp_path'],'/');
374 //if ( PEAR::isError($res) ){
375 // die($res->getMessage());
378 if ( PEAR::isError($res) ){
379 $context['result'] = 'Error: '.$res->getMessage();
385 include 'install'.DIRECTORY_SEPARATOR.'archive2app-results.inc.php';
388 default: // download_tarball
389 //$tarpath = $_FILES['archive']['tmp_name'];
390 if ( $compression == 'gz' ){
391 $mimetype = 'application/x-gzip';
393 $mimetype = 'application/x-tar';
395 header('Content-type: '.$mimetype);
396 header('Content-Disposition: attachment; filename="'.basename($tarpath).'.tar.gz
"');
397 echo file_get_contents($tarpath);
406 function test_db_access($dbname, $username, $password){
407 $db = @mysql_connect(DB_HOST, $username, $password);
409 return PEAR::raiseError("Could not connect to the MySQL server with username $username.
");
412 $res = mysql_select_db($dbname, $db);
413 if ( !$res ) return PEAR::raiseError("Could not access the database $dbname as user $username.
");
418 function test_ftp_access($host, $path, $user, $password, $ssl=false){
419 require_once 'install/ftp.api.php';
420 require_once 'install/ftp.class.php';
422 $conn = ftp_ssl_connect($host);
424 $conn = ftp_connect($host);
426 if ( !$conn ) return PEAR::raiseError("Could not connect to
FTP server
");
428 $res = @ftp_login($conn, $user, $password);
429 if ( !$res ) return PEAR::raiseError("Failed to login to
FTP server with the provided username ($user) and password");
431 $res = @ftp_chdir($conn,
$path);
433 return PEAR::raiseError(
"Failed: The directory $path on the server $host does not exist.");
442 if ( !@$_REQUEST[
'-dbname'] || !$_REQUEST[
'-dbuser'] || !isset($_REQUEST[
'-dbpass']) ){
443 trigger_error(
"Please provide all of -dbname, -dbuser, and -dbpass parameters in the POST variables.", E_USER_ERROR);
447 $res = $this->
test_db_access($_REQUEST[
'-dbname'], $_REQUEST[
'-dbuser'], $_REQUEST[
'-dbpass']);
451 'message' => $res->getMessage()
458 'message' =>
'Connected to database successfully'
462 header(
'Content-type: text/json');
463 require_once
'Services/JSON.php';
464 $json =
new Services_JSON;
465 echo $json->encode($msg);
470 if ( !@$_REQUEST[
'-ftphost'] || !$_REQUEST[
'-ftpuser'] || !isset($_REQUEST[
'-ftppass']) ){
471 trigger_error(
"Please provide all of -ftphost, -ftpuser, and -ftppass parameters in the POST variables.", E_USER_ERROR);
475 $res = $this->
test_ftp_access($_REQUEST[
'-ftphost'], @$_REQUEST[
'-ftppath'], $_REQUEST[
'-ftpuser'], $_REQUEST[
'-ftppass'], @$_REQUEST[
'-ftpssl']);
479 'message' => $res->getMessage()
486 'message' =>
'Connected to FTP server successfully'
490 header(
'Content-type: text/json');
491 require_once
'Services/JSON.php';
492 $json =
new Services_JSON;
493 echo $json->encode($msg);
503 if (!@
$_SERVER[
'PHP_AUTH_USER'] || !$_COOKIE[
'logged_in'] ){
516 if ( strlen($line) > 1 and $line{0} ==
'-' and $line{1} ==
'-')
return true;
525 switch (@$_REQUEST[
'-action']){