2010-12-02 23:34:41 +00:00
< ? php
2012-10-09 12:51:51 -04:00
2010-12-02 23:34:41 +00:00
/**
* class . Installer . php
2012-10-09 12:51:51 -04:00
*
2011-02-01 12:49:40 +00:00
* @ package workflow . engine . ProcessMaker
2010-12-02 23:34:41 +00:00
*
* ProcessMaker Open Source Edition
2011-02-01 12:49:40 +00:00
* Copyright ( C ) 2004 - 2011 Colosa Inc .
2010-12-02 23:34:41 +00:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
2012-10-09 12:51:51 -04:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2010-12-02 23:34:41 +00:00
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
2012-10-09 12:51:51 -04:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2010-12-02 23:34:41 +00:00
*
* For more information , contact Colosa Inc , 2566 Le Jeune Rd . ,
* Coral Gables , FL , 33134 , USA , or email info @ colosa . com .
*
*/
//
// It works with the table CONFIGURATION in a WF dataBase
//
// Copyright (C) 2007 COLOSA
//
// License: LGPL, see LICENSE
////////////////////////////////////////////////////
/**
* Processmaker Installer
2012-10-09 12:51:51 -04:00
*
2011-02-01 12:49:40 +00:00
* @ package workflow . engine . ProcessMaker
2010-12-02 23:34:41 +00:00
* @ author maborak
* @ copyright 2008 COLOSA
*/
class Installer
{
2013-03-14 14:50:58 -04:00
public $options = Array ();
public $result = Array ();
public $error = Array ();
public $report = Array ();
2012-10-09 12:51:51 -04:00
private $connection_database ;
/**
* construct of insert
*
* @ param string $pPRO_UID
* @ return void
*/
2013-03-14 14:50:58 -04:00
public function __construct ()
2012-10-09 12:51:51 -04:00
{
}
/**
* create_site
*
* @ param array $config
* @ param boolean $confirmed
* @ return void
*/
2013-03-14 14:50:58 -04:00
public function create_site ( $config = Array (), $confirmed = false )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
$this -> options = G :: array_concat ( Array ( 'isset' => false , 'password' => G :: generate_password ( 12 ), 'path_data' => @ PATH_DATA , 'path_compiled' => @ PATH_C , 'name' => $config [ 'name' ], 'database' => Array (), 'admin' => Array ( 'username' => 'admin' , 'password' => 'admin'
), 'advanced' => Array ( 'ao_db_wf' => 'wf_' . $config [ 'name' ], 'ao_db_rb' => 'rb_' . $config [ 'name' ], 'ao_db_rp' => 'rp_' . $config [ 'name' ], 'ao_db_drop' => false
)
), $config );
$a = @ explode ( SYSTEM_HASH , G :: decrypt ( HASH_INSTALLATION , SYSTEM_HASH ));
$this -> options [ 'database' ] = G :: array_concat ( Array ( 'username' => @ $a [ 1 ], 'password' => @ $a [ 2 ], 'hostname' => @ $a [ 0 ]
), $this -> options [ 'database' ]);
2012-10-09 12:51:51 -04:00
return ( $confirmed === true ) ? $this -> make_site () : $this -> create_site_test ();
}
/**
* isset_site
*
* @ param string $name Default value " workflow "
* @ return string file_exists ( PATH_DATA . " sites/ " . $name );
*/
2013-03-14 14:50:58 -04:00
public function isset_site ( $name = " workflow " )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
return file_exists ( PATH_DATA . " sites/ " . $name );
2012-10-09 12:51:51 -04:00
}
/**
* create_site_test
*
* @ return void
*/
2013-03-14 14:50:58 -04:00
private function create_site_test ()
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
$name = ( preg_match ( '/^[\w]+$/i' , trim ( $this -> options [ 'name' ]))) ? true : false ;
$result = Array ( 'path_data' => $this -> is_dir_writable ( $this -> options [ 'path_data' ]), 'path_compiled' => $this -> is_dir_writable ( $this -> options [ 'path_compiled' ]), 'database' => $this -> check_connection (), 'access_level' => $this -> cc_status , 'isset' => ( $this -> options [ 'isset' ] == true ) ? $this -> isset_site ( $this -> options [ 'name' ]) : false , 'microtime' => microtime (), 'workspace' => $this -> options [ 'name' ], 'name' => array ( 'status' => $name , 'message' => ( $name ) ? 'PASSED' : 'Workspace name invalid'
), 'admin' => array ( 'username' => ( preg_match ( '/^[\w@\.-]+$/i' , trim ( $this -> options [ 'admin' ][ 'username' ]))) ? true : false , 'password' => (( trim ( $this -> options [ 'admin' ][ 'password' ]) == '' ) ? false : true )
)
2012-10-09 12:51:51 -04:00
);
$result [ 'name' ][ 'message' ] = ( $result [ 'isset' ]) ? 'Workspace already exist' : $result [ 'name' ][ 'message' ];
$result [ 'name' ][ 'status' ] = ( $result [ 'isset' ]) ? false : $result [ 'name' ][ 'status' ];
//print_r($result);
2013-03-14 14:50:58 -04:00
return Array ( 'created' => G :: var_compare ( true , $result [ 'path_data' ], $result [ 'database' ][ 'connection' ], $result [ 'name' ][ 'status' ], $result [ 'database' ][ 'version' ], $result [ 'database' ][ 'ao' ][ 'ao_db_wf' ][ 'status' ], $result [ 'database' ][ 'ao' ][ 'ao_db_rb' ][ 'status' ], $result [ 'database' ][ 'ao' ][ 'ao_db_rp' ][ 'status' ], $result [ 'admin' ][ 'username' ], (( $result [ 'isset' ]) ? false : true ), $result [ 'admin' ][ 'password' ]), 'result' => $result
2012-10-09 12:51:51 -04:00
);
}
/**
* make_site
*
* @ return array $test
*/
2013-03-14 14:50:58 -04:00
private function make_site ()
2012-10-09 12:51:51 -04:00
{
$test = $this -> create_site_test ();
if ( $test [ " created " ] == true || $this -> options [ " advanced " ][ " ao_db_drop " ] == true ) {
/* Check if the hostname is local (localhost or 127.0.0.1) */
2013-03-14 14:50:58 -04:00
$islocal = ( strcmp ( substr ( $this -> options [ 'database' ][ 'hostname' ], 0 , strlen ( 'localhost' )), 'localhost' ) === 0 ) || ( strcmp ( substr ( $this -> options [ 'database' ][ 'hostname' ], 0 , strlen ( '127.0.0.1' )), '127.0.0.1' ) === 0 );
2012-10-09 12:51:51 -04:00
$this -> wf_site_name = $wf = $this -> options [ 'advanced' ][ 'ao_db_wf' ];
$this -> rbac_site_name = $rb = $this -> options [ 'advanced' ][ 'ao_db_rb' ];
$this -> report_site_name = $rp = $this -> options [ 'advanced' ][ 'ao_db_rp' ];
$schema = " schema.sql " ;
$values = " insert.sql " ;
if ( $this -> options [ 'advanced' ][ 'ao_db_drop' ] === true ) {
//Delete workspace directory if exists
//Drop databases
2013-03-14 14:50:58 -04:00
$this -> run_query ( " DROP DATABASE IF EXISTS " . $wf , " Drop database $wf " );
$this -> run_query ( " DROP DATABASE IF EXISTS " . $rb , " Drop database $rb " );
$this -> run_query ( " DROP DATABASE IF EXISTS " . $rp , " Drop database $rp " );
2012-10-09 12:51:51 -04:00
}
2013-03-14 14:50:58 -04:00
$this -> run_query ( " CREATE DATABASE IF NOT EXISTS " . $wf . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci " , " Create database $wf " );
$this -> run_query ( " CREATE DATABASE IF NOT EXISTS " . $rb . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci " , " Create database $rb " );
$this -> run_query ( " CREATE DATABASE IF NOT EXISTS " . $rp . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci " , " Create database $rp " );
2012-10-09 12:51:51 -04:00
if ( $this -> cc_status == 1 ) {
$host = ( $islocal ) ? " localhost " : " % " ;
2013-03-14 14:50:58 -04:00
$this -> run_query ( " GRANT ALL PRIVILEGES ON ` $wf `.* TO $wf @' $host ' IDENTIFIED BY ' { $this -> options [ 'password' ] } ' WITH GRANT OPTION " , " Grant privileges for user $wf on database $wf " );
$this -> run_query ( " GRANT ALL PRIVILEGES ON ` $rb `.* TO $rb @' $host ' IDENTIFIED BY ' { $this -> options [ 'password' ] } ' WITH GRANT OPTION " , " Grant privileges for user $rb on database $rb " );
$this -> run_query ( " GRANT ALL PRIVILEGES ON ` $rp `.* TO $rp @' $host ' IDENTIFIED BY ' { $this -> options [ 'password' ] } ' WITH GRANT OPTION " , " Grant privileges for user $rp on database $rp " );
2012-10-09 12:51:51 -04:00
}
/* Dump schema workflow && data */
2013-03-14 14:50:58 -04:00
$this -> log ( " Import database schema: \n " );
$myPortA = explode ( " : " , $this -> options [ 'database' ][ 'hostname' ]);
if ( count ( $myPortA ) < 2 ) {
2012-10-09 12:51:51 -04:00
$myPortA [ 1 ] = " 3306 " ;
}
$myPort = $myPortA [ 1 ];
$this -> options [ 'database' ][ 'hostname' ] = $myPortA [ 0 ];
2013-03-14 14:50:58 -04:00
mysql_select_db ( $wf , $this -> connection_database );
2012-10-09 12:51:51 -04:00
$pws = PATH_WORKFLOW_MYSQL_DATA . $schema ;
2013-03-14 14:50:58 -04:00
$qws = $this -> query_sql_file ( PATH_WORKFLOW_MYSQL_DATA . $schema , $this -> connection_database );
$this -> log ( $qws , isset ( $qws [ 'errors' ]));
$qwv = $this -> query_sql_file ( PATH_WORKFLOW_MYSQL_DATA . $values , $this -> connection_database );
$this -> log ( $qwv , isset ( $qwv [ 'errors' ]));
2012-10-09 12:51:51 -04:00
/* Dump schema rbac && data */
$pws = PATH_RBAC_MYSQL_DATA . $schema ;
2013-03-14 14:50:58 -04:00
mysql_select_db ( $rb , $this -> connection_database );
$qrs = $this -> query_sql_file ( PATH_RBAC_MYSQL_DATA . $schema , $this -> connection_database );
$this -> log ( $qrs , isset ( $qrs [ 'errors' ]));
$qrv = $this -> query_sql_file ( PATH_RBAC_MYSQL_DATA . $values , $this -> connection_database );
$this -> log ( $qrv , isset ( $qrv [ 'errors' ]));
2012-10-09 12:51:51 -04:00
2013-03-14 14:50:58 -04:00
mysql_select_db ( $wf , $this -> connection_database );
2012-10-09 12:51:51 -04:00
require_once ( " propel/Propel.php " );
require_once ( 'classes/model/AppCacheView.php' );
$appCache = new AppCacheView ();
2013-03-14 14:50:58 -04:00
$appCache -> setPathToAppCacheFiles ( PATH_METHODS . 'setup/setupSchemas/' );
$triggers = $appCache -> getTriggers ( " en " );
$this -> log ( " Create 'cases list cache' triggers " );
2012-10-09 12:51:51 -04:00
foreach ( $triggers as $triggerName => $trigger ) {
2013-03-14 14:50:58 -04:00
$this -> run_query ( $trigger , " -> Trigger $triggerName " );
2012-10-09 12:51:51 -04:00
}
$path_site = $this -> options [ 'path_data' ] . " /sites/ " . $this -> options [ 'name' ] . " / " ;
$db_file = $path_site . " db.php " ;
2013-03-14 14:50:58 -04:00
@ mkdir ( $path_site , 0777 , true );
@ mkdir ( $path_site . " files/ " , 0777 , true );
@ mkdir ( $path_site . " mailTemplates/ " , 0777 , true );
@ mkdir ( $path_site . " public/ " , 0777 , true );
@ mkdir ( $path_site . " reports/ " , 0777 , true );
@ mkdir ( $path_site . " xmlForms " , 0777 , true );
2012-10-09 12:51:51 -04:00
2013-05-08 14:10:43 -04:00
$db_text = " <?php \n " . " // Processmaker configuration \n " . " define ('DB_ADAPTER', 'mysql' ); \n " . " define ('DB_HOST', ' " . $this -> options [ 'database' ][ 'hostname' ] . " : " . $myPort . " ' ); \n " . " define ('DB_NAME', ' " . $wf . " ' ); \n " . " define ('DB_USER', ' " . (( $this -> cc_status == 1 ) ? $wf : $this -> options [ 'database' ][ 'username' ]) . " ' ); \n " . " define ('DB_PASS', ' " . (( $this -> cc_status == 1 ) ? $this -> options [ 'password' ] : $this -> options [ 'database' ][ 'password' ]) . " ' ); \n " . " define ('DB_RBAC_HOST', ' " . $this -> options [ 'database' ][ 'hostname' ] . " : " . $myPort . " ' ); \n " . " define ('DB_RBAC_NAME', ' " . $rb . " ' ); \n " . " define ('DB_RBAC_USER', ' " . (( $this -> cc_status == 1 ) ? $rb : $this -> options [ 'database' ][ 'username' ]) . " ' ); \n " . " define ('DB_RBAC_PASS', ' " . (( $this -> cc_status == 1 ) ? $this -> options [ 'password' ] : $this -> options [ 'database' ][ 'password' ]) . " ' ); \n " . " define ('DB_REPORT_HOST', ' " . $this -> options [ 'database' ][ 'hostname' ] . " : " . $myPort . " ' ); \n " . " define ('DB_REPORT_NAME', ' " . $rp . " ' ); \n " . " define ('DB_REPORT_USER', ' " . (( $this -> cc_status == 1 ) ? $rp : $this -> options [ 'database' ][ 'username' ]) . " ' ); \n " . " define ('DB_REPORT_PASS', ' " . (( $this -> cc_status == 1 ) ? $this -> options [ 'password' ] : $this -> options [ 'database' ][ 'password' ]) . " ' ); \n " ;
2013-03-25 17:29:43 -04:00
if ( defined ( 'PARTNER_FLAG' ) || isset ( $_REQUEST [ 'PARTNER_FLAG' ])) {
2013-05-08 13:10:05 -04:00
$db_text .= " define ('PARTNER_FLAG', " . (( defined ( 'PARTNER_FLAG' )) ? PARTNER_FLAG : (( isset ( $_REQUEST [ 'PARTNER_FLAG' ])) ? $_REQUEST [ 'PARTNER_FLAG' ] : 'false' )) . " ); \n " ;
2013-04-26 12:55:16 -04:00
if ( defined ( 'SYSTEM_NAME' )) {
2013-05-08 13:10:05 -04:00
$db_text .= " define ('SYSTEM_NAME', ' " . SYSTEM_NAME . " '); \n " ;
2013-04-26 11:45:03 -04:00
}
2013-03-19 10:12:35 -04:00
}
2013-05-08 14:10:43 -04:00
$db_text .= " ?> " ;
2013-03-14 14:50:58 -04:00
$fp = @ fopen ( $db_file , " w " );
$this -> log ( " Create: " . $db_file . " => " . (( ! $fp ) ? $fp : " OK " ) . " \n " , $fp === false );
$ff = @ fputs ( $fp , $db_text , strlen ( $db_text ));
$this -> log ( " Write: " . $db_file . " => " . (( ! $ff ) ? $ff : " OK " ) . " \n " , $ff === false );
2012-10-09 12:51:51 -04:00
2013-03-14 14:50:58 -04:00
fclose ( $fp );
2013-04-09 17:45:08 -04:00
$this -> setPartner ();
$this -> setConfiguration ();
$this -> setAdmin ();
2012-10-09 12:51:51 -04:00
}
return $test ;
}
2013-04-09 17:45:08 -04:00
/**
* set_partner
*
* @ return void
*/
public function setPartner ()
{
2013-04-10 10:25:16 -04:00
$partnerFlag = ( defined ( 'PARTNER_FLAG' )) ? PARTNER_FLAG : false ;
if ( $partnerFlag ) {
2013-04-09 17:45:08 -04:00
// Execute sql for partner
$pathMysqlPartner = PATH_CORE . 'data' . PATH_SEP . 'partner' . PATH_SEP . 'mysql' . PATH_SEP ;
if ( G :: verifyPath ( $pathMysqlPartner )) {
$res = array ();
$filesSlq = glob ( $pathMysqlPartner . '*.sql' );
foreach ( $filesSlq as $value ) {
$this -> query_sql_file ( $value , $this -> connection_database );
}
}
// Execute to change of skin
$pathSkinPartner = PATH_CORE . 'data' . PATH_SEP . 'partner' . PATH_SEP . 'skin' . PATH_SEP ;
if ( G :: verifyPath ( $pathSkinPartner )) {
$res = array ();
$fileTar = glob ( $pathSkinPartner . '*.tar' );
foreach ( $fileTar as $value ) {
$dataFile = pathinfo ( $value );
$nameSkinTmp = $dataFile [ 'filename' ];
G :: LoadThirdParty ( 'pear/Archive' , 'Tar' );
$tar = new Archive_Tar ( $value );
$pathSkinTmp = $pathSkinPartner . 'tmp' . PATH_SEP ;
G :: rm_dir ( $pathSkinTmp );
G :: verifyPath ( $pathSkinTmp , true );
chmod ( $pathSkinTmp , 0777 );
$tar -> extract ( $pathSkinTmp );
$pathSkinName = $pathSkinTmp . $nameSkinTmp . PATH_SEP ;
chmod ( $pathSkinName , 0777 );
G :: verifyPath ( PATH_CORE . 'skinEngine' . PATH_SEP . 'tmp' , true );
$skinClassic = PATH_CORE . 'skinEngine' . PATH_SEP . 'tmp' . PATH_SEP ;
if ( is_dir ( $pathSkinName )) {
$this -> copyFile ( $pathSkinName , $skinClassic );
}
G :: rm_dir ( PATH_CORE . 'skinEngine' . PATH_SEP . 'base' );
rename ( PATH_CORE . 'skinEngine' . PATH_SEP . 'tmp' , PATH_CORE . 'skinEngine' . PATH_SEP . 'base' );
G :: rm_dir ( PATH_CORE . 'skinEngine' . PATH_SEP . 'tmp' );
break ;
}
}
}
}
function copyFile ( $fromDir , $toDir , $chmod = 0777 )
{
$errors = array ();
$messages = array ();
if ( ! is_writable ( $toDir )) {
$errors [] = 'target ' . $toDir . ' is not writable' ;
}
if ( ! is_dir ( $toDir )) {
$errors [] = 'target ' . $toDir . ' is not a directory' ;
}
if ( ! is_dir ( $fromDir )) {
$errors [] = 'source ' . $fromDir . ' is not a directory' ;
}
if ( ! empty ( $errors )) {
return false ;
}
$exceptions = array ( '.' , '..' );
$handle = opendir ( $fromDir );
while ( false !== ( $item = readdir ( $handle ))) {
if ( ! in_array ( $item , $exceptions )) {
$from = str_replace ( '//' , '/' , $fromDir . '/' . $item );
$to = str_replace ( '//' , '/' , $toDir . '/' . $item );
if ( is_file ( $from )) {
if ( @ copy ( $from , $to )) {
chmod ( $to , $chmod );
touch ( $to , filemtime ( $from ));
}
}
if ( is_dir ( $from )) {
if ( @ mkdir ( $to )) {
chmod ( $to , $chmod );
}
$this -> copyFile ( $from , $to , $chmod );
}
}
}
closedir ( $handle );
}
2013-04-08 16:48:03 -04:00
/**
* set_configuration
*
* @ return void
*/
2013-04-09 17:45:08 -04:00
public function setConfiguration ()
2013-04-08 16:48:03 -04:00
{
$oConf = new Configuration ();
$dataCondif = $oConf -> getAll ();
if ( count ( $dataCondif )) {
foreach ( $dataCondif as $value ) {
if ( $value [ 'CFG_UID' ] == 'ENVIRONMENT_SETTINGS' ) {
$query = 'INSERT INTO CONFIGURATION (CFG_UID, OBJ_UID, CFG_VALUE, PRO_UID, USR_UID, APP_UID) VALUES' ;
$query .= " (' " .
$value [ 'CFG_UID' ] . " ', ' " .
$value [ 'OBJ_UID' ] . " ', ' " .
$value [ 'CFG_VALUE' ] . " ', ' " .
$value [ 'PRO_UID' ] . " ', ' " .
$value [ 'USR_UID' ] . " ', ' " .
$value [ 'APP_UID' ] . " ') " ;
mysql_select_db ( $this -> wf_site_name , $this -> connection_database );
$this -> run_query ( $query , " Copy configuracion environment " );
break ;
}
}
}
2013-04-09 17:45:08 -04:00
}
2013-04-08 16:48:03 -04:00
2012-10-09 12:51:51 -04:00
/**
* set_admin
*
* @ return void
*/
2013-04-09 17:45:08 -04:00
public function setAdmin ()
2010-12-02 23:34:41 +00:00
{
2013-03-14 14:50:58 -04:00
mysql_select_db ( $this -> wf_site_name , $this -> connection_database );
2012-10-09 12:51:51 -04:00
// The mysql_escape_string function has been DEPRECATED as of PHP 5.3.0.
// $this->run_query('UPDATE USERS SET USR_USERNAME = \''.mysql_escape_string($this->options['admin']['username']).'\', `USR_PASSWORD` = \''.md5($this->options['admin']['password']).'\' WHERE `USR_UID` = \'00000000000000000000000000000001\' LIMIT 1',
// "Add 'admin' user in ProcessMaker (wf)");
2013-03-14 14:50:58 -04:00
$this -> run_query ( 'UPDATE USERS SET USR_USERNAME = \'' . mysql_real_escape_string ( $this -> options [ 'admin' ][ 'username' ]) . '\', ' . ' `USR_PASSWORD` = \'' . md5 ( $this -> options [ 'admin' ][ 'password' ]) . '\' ' . ' WHERE `USR_UID` = \'00000000000000000000000000000001\' LIMIT 1' , " Add 'admin' user in ProcessMaker (wf) " );
mysql_select_db ( $this -> rbac_site_name , $this -> connection_database );
2012-10-09 12:51:51 -04:00
// The mysql_escape_string function has been DEPRECATED as of PHP 5.3.0.
// $this->run_query('UPDATE USERS SET USR_USERNAME = \''.mysql_escape_string($this->options['admin']['username']).'\', `USR_PASSWORD` = \''.md5($this->options['admin']['password']).'\' WHERE `USR_UID` = \'00000000000000000000000000000001\' LIMIT 1',
// "Add 'admin' user in ProcessMaker (rb)");
2013-03-14 14:50:58 -04:00
$this -> run_query ( 'UPDATE USERS SET USR_USERNAME = \'' . mysql_real_escape_string ( $this -> options [ 'admin' ][ 'username' ]) . '\', ' . ' `USR_PASSWORD` = \'' . md5 ( $this -> options [ 'admin' ][ 'password' ]) . '\' ' . ' WHERE `USR_UID` = \'00000000000000000000000000000001\' LIMIT 1' , " Add 'admin' user in ProcessMaker (rb) " );
2012-10-09 12:51:51 -04:00
}
/**
* Run a mysql query on the current database and take care of logging and
* error handling .
*
* @ param string $query SQL command
* @ param string $description Description to log instead of $query
*/
2013-03-14 14:50:58 -04:00
private function run_query ( $query , $description = null )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
$result = @ mysql_query ( $query , $this -> connection_database );
2012-10-09 12:51:51 -04:00
$error = ( $result ) ? false : mysql_error ();
2013-03-14 14:50:58 -04:00
$this -> log (( $description ? $description : $query ) . " => " . (( $error ) ? $error : " OK " ) . " \n " , $error );
2012-10-09 12:51:51 -04:00
}
/**
* query_sql_file
*
* @ param string $file
* @ param string $connection
* @ return array $report
*/
2013-03-14 14:50:58 -04:00
public function query_sql_file ( $file , $connection )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
$lines = file ( $file );
$previous = null ;
2012-10-09 12:51:51 -04:00
$errors = '' ;
2013-03-14 14:50:58 -04:00
@ mysql_query ( " SET NAMES 'utf8'; " );
2012-10-09 12:51:51 -04:00
foreach ( $lines as $j => $line ) {
2013-03-14 14:50:58 -04:00
$line = trim ( $line ); // Remove comments from the script
2012-10-09 12:51:51 -04:00
2013-03-14 14:50:58 -04:00
if ( strpos ( $line , " -- " ) === 0 ) {
$line = substr ( $line , 0 , strpos ( $line , " -- " ));
2012-10-09 12:51:51 -04:00
}
2013-03-14 14:50:58 -04:00
if ( empty ( $line )) {
2012-10-09 12:51:51 -04:00
continue ;
}
2013-03-14 14:50:58 -04:00
if ( strpos ( $line , " # " ) === 0 ) {
$line = substr ( $line , 0 , strpos ( $line , " # " ));
2012-10-09 12:51:51 -04:00
}
2013-03-14 14:50:58 -04:00
if ( empty ( $line )) {
2012-10-09 12:51:51 -04:00
continue ;
}
// Concatenate the previous line, if any, with the current
if ( $previous ) {
$line = $previous . " " . $line ;
}
2013-03-14 14:50:58 -04:00
$previous = null ;
2012-10-09 12:51:51 -04:00
// If the current line doesnt end with ; then put this line together
// with the next one, thus supporting multi-line statements.
2013-03-14 14:50:58 -04:00
if ( strrpos ( $line , " ; " ) != strlen ( $line ) - 1 ) {
2012-10-09 12:51:51 -04:00
$previous = $line ;
continue ;
}
2013-03-14 14:50:58 -04:00
$line = substr ( $line , 0 , strrpos ( $line , " ; " ));
@ mysql_query ( $line , $connection );
2012-10-09 12:51:51 -04:00
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 12:51:51 -04:00
/**
* check_path
*
* @ return void
* @ todo Empty function
*/
2013-03-14 14:50:58 -04:00
private function check_path ()
2012-10-09 12:51:51 -04:00
{
2010-12-02 23:34:41 +00:00
}
2012-10-09 12:51:51 -04:00
/**
* function find_root_path
*
* @ param string $path
* @ return string $path
*/
2013-03-14 14:50:58 -04:00
private function find_root_path ( $path )
2012-10-09 12:51:51 -04:00
{
$i = 0 ; //prevent loop inifinity
2013-03-14 14:50:58 -04:00
while ( ! is_dir ( $path ) && ( $path = dirname ( $path )) && (( strlen ( $path ) > 1 ) && $i < 10 )) {
$i ++ ;
2012-10-09 12:51:51 -04:00
}
return $path ;
2010-12-02 23:34:41 +00:00
}
2012-10-09 12:51:51 -04:00
/**
* file_permisions
*
* @ param string $file
* @ param integer $def default value 777
* @ return integer $def
*/
2013-03-14 14:50:58 -04:00
public function file_permisions ( $file , $def = 777 )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
if ( PHP_OS == 'WINNT' ) {
2012-10-09 12:51:51 -04:00
return $def ;
2013-03-14 14:50:58 -04:00
} else {
return ( int ) substr ( sprintf ( '%o' , @ fileperms ( $file )), - 4 );
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 12:51:51 -04:00
/**
* is_dir_writable
*
* @ param string $dir default value empty
* @ return string $path
*/
2013-03-14 14:50:58 -04:00
public function is_dir_writable ( $dir = '' )
2012-10-09 12:51:51 -04:00
{
if ( PHP_OS == 'WINNT' ) {
2013-03-14 14:50:58 -04:00
$dir = $this -> find_root_path ( $dir );
return file_exists ( $dir );
2012-10-09 12:51:51 -04:00
} else {
2013-03-14 14:50:58 -04:00
$dir = $this -> find_root_path ( $dir );
return ( is_writable ( $dir ) && is_readable ( $dir ));
2012-10-09 12:51:51 -04:00
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 12:51:51 -04:00
/**
* getDirectoryFiles
*
* @ param string $dir default value empty
* @ return string $path
*/
2013-03-14 14:50:58 -04:00
public function getDirectoryFiles ( $dir , $extension )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
$filesArray = array ();
if ( file_exists ( $dir )) {
if ( $handle = opendir ( $dir )) {
while ( false !== ( $file = readdir ( $handle ))) {
$fileParts = explode ( " . " , $file );
if ( $fileParts [ count ( $fileParts ) - 1 ] == $extension ) {
2012-10-09 12:51:51 -04:00
$filesArray [] = $file ;
}
}
2013-03-14 14:50:58 -04:00
closedir ( $handle );
2012-10-09 12:51:51 -04:00
}
}
return $filesArray ;
2010-12-02 23:34:41 +00:00
}
2012-10-09 12:51:51 -04:00
/**
* check_db_empty
*
* @ param string $dbName
* @ return boolean true or false
*/
2013-03-14 14:50:58 -04:00
public function check_db_empty ( $dbName )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
$a = @ mysql_select_db ( $dbName , $this -> connection_database );
if ( ! $a ) {
2012-10-09 12:51:51 -04:00
return true ;
}
2013-03-14 14:50:58 -04:00
$q = @ mysql_query ( 'SHOW TABLES' , $this -> connection_database );
return ( @ mysql_num_rows ( $q ) > 0 ) ? false : true ;
2010-12-02 23:34:41 +00:00
}
2012-10-09 12:51:51 -04:00
/**
* check_db
*
* @ param string $dbName
* @ return Array Array ( 'status' => true or false , 'message' => string )
*/
2013-03-14 14:50:58 -04:00
public function check_db ( $dbName )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
if ( ! $this -> connection_database ) {
2012-10-09 12:51:51 -04:00
//erik: new verification if the mysql extension is enabled
2013-03-14 14:50:58 -04:00
$error = class_exists ( 'mysql_error' ) ? mysql_error () : 'Mysql Module for PHP is not enabled!' ;
return Array ( 'status' => false , 'message' => $error
2012-10-09 12:51:51 -04:00
);
} else {
2013-03-14 14:50:58 -04:00
if ( ! mysql_select_db ( $dbName , $this -> connection_database ) && $this -> cc_status != 1 ) {
return Array ( 'status' => false , 'message' => mysql_error ()
2012-10-09 12:51:51 -04:00
);
} else {
/* var_dump ( $this -> options [ 'advanced' ][ 'ao_db_drop' ], $this -> cc_status , $this -> check_db_empty ( $dbName ));
2013-03-14 14:50:58 -04:00
if (( $this -> options [ 'advanced' ][ 'ao_db_drop' ] === false && $this -> cc_status != 1 && ! $this -> check_db_empty ( $dbName )) )
{
return Array ( 'status' => false , 'message' => 'Database is not empty' );
}
else
{
return Array ( 'status' => true , 'message' => 'OK' );
} */
if ( $this -> options [ 'advanced' ][ 'ao_db_drop' ] === true || $this -> check_db_empty ( $dbName )) {
return Array ( 'status' => true , 'message' => 'PASSED'
2012-10-09 12:51:51 -04:00
);
} else {
2013-03-14 14:50:58 -04:00
return Array ( 'status' => false , 'message' => 'Database is not empty'
2012-10-09 12:51:51 -04:00
);
}
}
2010-12-02 23:34:41 +00:00
}
}
2012-10-09 12:51:51 -04:00
/**
* check_connection
*
* @ return Array $rt
*/
2013-03-14 14:50:58 -04:00
private function check_connection ()
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
if ( ! function_exists ( " mysql_connect " )) {
2012-10-09 12:51:51 -04:00
$this -> cc_status = 0 ;
2013-03-14 14:50:58 -04:00
$rt = Array ( 'connection' => false , 'grant' => 0 , 'version' => false , 'message' => " ERROR: Mysql Module for PHP is not enabled, try install <b>php-mysql</b> package. " , 'ao' => Array ( 'ao_db_wf' => false , 'ao_db_rb' => false , 'ao_db_rp' => false
)
2012-10-09 12:51:51 -04:00
);
} else {
2013-03-14 14:50:58 -04:00
$this -> connection_database = @ mysql_connect ( $this -> options [ 'database' ][ 'hostname' ], $this -> options [ 'database' ][ 'username' ], $this -> options [ 'database' ][ 'password' ]);
$rt = Array ( 'version' => false , 'ao' => Array ( 'ao_db_wf' => false , 'ao_db_rb' => false , 'ao_db_rp' => false
)
2012-10-09 12:51:51 -04:00
);
2013-03-14 14:50:58 -04:00
if ( ! $this -> connection_database ) {
2012-10-09 12:51:51 -04:00
$this -> cc_status = 0 ;
$rt [ 'connection' ] = false ;
$rt [ 'grant' ] = 0 ;
$rt [ 'message' ] = " Mysql error: " . mysql_error ();
} else {
2013-03-14 14:50:58 -04:00
preg_match ( '@[0-9]+\.[0-9]+\.[0-9]+@' , mysql_get_server_info ( $this -> connection_database ), $version );
$rt [ 'version' ] = version_compare ( @ $version [ 0 ], " 4.1.0 " , " >= " );
2012-10-09 12:51:51 -04:00
$rt [ 'connection' ] = true ;
$dbNameTest = " PROCESSMAKERTESTDC " ;
2013-03-14 14:50:58 -04:00
$db = @ mysql_query ( " CREATE DATABASE " . $dbNameTest , $this -> connection_database );
if ( ! $db ) {
2012-10-09 12:51:51 -04:00
$this -> cc_status = 3 ;
$rt [ 'grant' ] = 3 ;
//$rt['message'] = "Db GRANTS error: ".mysql_error();
$rt [ 'message' ] = " Successful connection " ;
} else {
//@mysql_drop_db("processmaker_testGA");
$usrTest = " wfrbtest " ;
$chkG = " GRANT ALL PRIVILEGES ON ` " . $dbNameTest . " `.* TO " . $usrTest . " @'%' IDENTIFIED BY 'sample' WITH GRANT OPTION " ;
2013-03-14 14:50:58 -04:00
$ch = @ mysql_query ( $chkG , $this -> connection_database );
if ( ! $ch ) {
2012-10-09 12:51:51 -04:00
$this -> cc_status = 2 ;
$rt [ 'grant' ] = 2 ;
//$rt['message'] = "USER PRIVILEGES ERROR";
$rt [ 'message' ] = " Successful connection " ;
} else {
$this -> cc_status = 1 ;
2013-03-14 14:50:58 -04:00
@ mysql_query ( " DROP USER " . $usrTest . " @'%' " , $this -> connection_database );
2012-10-09 12:51:51 -04:00
$rt [ 'grant' ] = 1 ;
$rt [ 'message' ] = " Successful connection " ;
}
2013-03-14 14:50:58 -04:00
@ mysql_query ( " DROP DATABASE " . $dbNameTest , $this -> connection_database );
2012-10-09 12:51:51 -04:00
}
// var_dump($wf,$rb,$rp);
}
2010-12-02 23:34:41 +00:00
}
2013-03-14 14:50:58 -04:00
$rt [ 'ao' ][ 'ao_db_wf' ] = $this -> check_db ( $this -> options [ 'advanced' ][ 'ao_db_wf' ]);
$rt [ 'ao' ][ 'ao_db_rb' ] = $this -> check_db ( $this -> options [ 'advanced' ][ 'ao_db_rb' ]);
$rt [ 'ao' ][ 'ao_db_rp' ] = $this -> check_db ( $this -> options [ 'advanced' ][ 'ao_db_rp' ]);
2012-10-09 12:51:51 -04:00
return $rt ;
}
2010-12-02 23:34:41 +00:00
2012-10-09 12:51:51 -04:00
/**
* log
*
* @ param string $text
* @ return void
*/
2013-03-14 14:50:58 -04:00
public function log ( $text , $failed = null )
2012-10-09 12:51:51 -04:00
{
2013-03-14 14:50:58 -04:00
array_push ( $this -> report , $text );
if ( $failed ) {
throw new Exception ( is_string ( $text ) ? $text : var_export ( $text , true ) );
}
2010-12-02 23:34:41 +00:00
}
}
2013-03-14 14:50:58 -04:00