2002-03-03 22:47:30 +01:00
< ? php
/************************************************************************** \
2003-10-22 13:28:17 +02:00
* phpGroupWare - Setup *
* http :// www . phpgroupware . org *
* -------------------------------------------- *
* This file written by Joseph Engo < jengo @ phpgroupware . org > *
* and Dan Kuykendall < seek3r @ phpgroupware . org > *
* and Mark Peters < skeeter @ phpgroupware . org > *
* and Miles Lott < milosch @ phpgroupware . org > *
* -------------------------------------------- *
2002-03-03 22:47:30 +01:00
* This program is free software ; you can redistribute it and / or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation ; either version 2 of the License , or ( at your *
2003-10-22 13:28:17 +02:00
* option ) any later version . *
2002-03-03 22:47:30 +01:00
\ **************************************************************************/
/* $Id$ */
class setup
{
var $db ;
2002-03-04 05:49:42 +01:00
var $oProc ;
2002-03-03 22:47:30 +01:00
var $detection = '' ;
var $process = '' ;
var $lang = '' ;
var $html = '' ;
var $appreg = '' ;
2003-10-22 13:28:17 +02:00
/* table name vars */
2003-10-05 04:06:11 +02:00
var $tbl_apps ;
var $tbl_config ;
var $tbl_hooks ;
2002-03-03 22:47:30 +01:00
function setup ( $html = False , $translation = False )
{
$this -> detection = CreateObject ( 'phpgwapi.setup_detection' );
$this -> process = CreateObject ( 'phpgwapi.setup_process' );
$this -> appreg = CreateObject ( 'phpgwapi.app_registry' );
/* The setup application needs these */
2003-10-22 13:28:17 +02:00
$this -> html = $html ? CreateObject ( 'phpgwapi.setup_html' ) : '' ;
2002-03-03 22:47:30 +01:00
$this -> translation = $translation ? CreateObject ( 'phpgwapi.setup_translation' ) : '' ;
2003-10-05 04:06:11 +02:00
// $this->tbl_apps = $this->get_apps_table_name();
// $this->tbl_config = $this->get_config_table_name();
$this -> tbl_hooks = $this -> get_hooks_table_name ();
2002-03-03 22:47:30 +01:00
}
/*!
@ function loaddb
@ abstract include api db class for the ConfigDomain and connect to the db
*/
function loaddb ()
{
2004-01-06 14:38:45 +01:00
if ( ! isset ( $this -> ConfigDomain ) || empty ( $this -> ConfigDomain ))
{
$this -> ConfigDomain = get_var ( 'ConfigDomain' , array ( 'COOKIE' , 'POST' ), $_POST [ 'FormDomain' ]);
}
2002-03-03 22:47:30 +01:00
2004-01-06 14:38:45 +01:00
$GLOBALS [ 'phpgw_info' ][ 'server' ][ 'db_type' ] = $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'db_type' ];
2002-03-03 22:47:30 +01:00
2003-10-22 13:28:17 +02:00
$this -> db = CreateObject ( 'phpgwapi.db' );
2004-01-06 14:38:45 +01:00
$this -> db -> Host = $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'db_host' ];
2004-01-18 03:24:42 +01:00
$this -> db -> Port = $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'db_port' ];
2004-01-06 14:38:45 +01:00
$this -> db -> Type = $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'db_type' ];
$this -> db -> Database = $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'db_name' ];
$this -> db -> User = $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'db_user' ];
$this -> db -> Password = $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'db_pass' ];
}
/**
* Set the domain used for cookies
*
* @ return string domain
*/
function set_cookiedomain ()
{
$dom = $_SERVER [ 'HTTP_HOST' ];
if ( preg_match ( " /^(.*):(.*) $ / " , $dom , $arr ))
{
$dom = $arr [ 1 ];
}
$parts = explode ( '.' , $dom );
if ( count ( $parts ) > 2 )
{
if ( ! ereg ( '[0-9]+' , $parts [ 1 ]))
{
for ( $i = 1 ; $i < count ( $parts ); $i ++ )
{
$this -> cookie_domain .= '.' . $parts [ $i ];
}
}
else
{
$this -> cookie_domain = '' ;
}
}
else
{
$this -> cookie_domain = '' ;
}
}
/**
* Set a cookie
*
* @ param string $cookiename name of cookie to be set
* @ param string $cookievalue value to be used , if unset cookie is cleared ( optional )
* @ param int $cookietime when cookie should expire , 0 for session only ( optional )
*/
function set_cookie ( $cookiename , $cookievalue = '' , $cookietime = 0 )
{
if ( ! $this -> cookie_domain )
{
$this -> set_cookiedomain ();
}
setcookie ( $cookiename , $cookievalue , $cookietime , '/' , $this -> cookie_domain );
2002-03-03 22:47:30 +01:00
}
/*!
@ function auth
@ abstract authenticate the setup user
@ param $auth_type ? ? ?
*/
2002-03-06 03:47:47 +01:00
function auth ( $auth_type = 'Config' )
2002-03-03 22:47:30 +01:00
{
2003-09-28 17:45:07 +02:00
#phpinfo();
2004-01-18 22:03:56 +01:00
#$remoteip = $_SERVER['REMOTE_ADDR'];
2002-05-11 21:30:10 +02:00
2004-01-18 22:03:56 +01:00
$FormLogout = get_var ( 'FormLogout' , array ( 'GET' , 'POST' ));
2004-01-06 14:38:45 +01:00
if ( ! $FormLogout )
2002-05-11 21:30:10 +02:00
{
2004-01-06 14:38:45 +01:00
$ConfigLogin = get_var ( 'ConfigLogin' , array ( 'POST' ));
$HeaderLogin = get_var ( 'HeaderLogin' , array ( 'POST' ));
$FormDomain = get_var ( 'FormDomain' , array ( 'POST' ));
2004-01-18 22:03:56 +01:00
$FormUser = get_var ( 'FormUser' , array ( 'POST' ));
2004-01-06 14:38:45 +01:00
$FormPW = get_var ( 'FormPW' , array ( 'POST' ));
2002-05-11 21:30:10 +02:00
2004-01-06 14:38:45 +01:00
$this -> ConfigDomain = get_var ( 'ConfigDomain' , array ( 'POST' , 'COOKIE' ));
2004-01-18 22:03:56 +01:00
$ConfigUser = get_var ( 'ConfigUser' , array ( 'POST' , 'COOKIE' ));
2004-01-06 14:38:45 +01:00
$ConfigPW = get_var ( 'ConfigPW' , array ( 'POST' , 'COOKIE' ));
2004-01-18 22:03:56 +01:00
$HeaderUser = get_var ( 'HeaderUser' , array ( 'POST' , 'COOKIE' ));
2004-01-06 14:38:45 +01:00
$HeaderPW = get_var ( 'HeaderPW' , array ( 'POST' , 'COOKIE' ));
$ConfigLang = get_var ( 'ConfigLang' , array ( 'POST' , 'COOKIE' ));
2004-01-18 22:03:56 +01:00
/* Setup defaults to aid in header upgrade to version 1.26 .
* This was the first version to include the following values .
*/
2004-01-20 01:52:11 +01:00
if ( !@ isset ( $GLOBALS [ 'phpgw_domain' ][ $FormDomain ][ 'config_user' ]) && isset ( $GLOBALS [ 'phpgw_domain' ][ $FormDomain ]))
2004-01-18 22:03:56 +01:00
{
@ $GLOBALS [ 'phpgw_domain' ][ $FormDomain ][ 'config_user' ] = 'admin' ;
}
if ( !@ isset ( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'header_admin_user' ]))
{
@ $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'header_admin_user' ] = 'admin' ;
}
2004-01-06 14:38:45 +01:00
}
2002-03-04 03:55:53 +01:00
2004-01-06 14:38:45 +01:00
/* if(!empty($remoteip) && !$this->checkip($remoteip)) { return False; } */
2002-03-06 03:40:30 +01:00
2004-01-06 14:38:45 +01:00
/* If FormLogout is set, simply invalidate the cookies (LOGOUT) */
switch ( strtolower ( $FormLogout ))
2002-03-03 22:47:30 +01:00
{
2004-01-06 14:38:45 +01:00
case 'config' :
2002-03-04 03:55:53 +01:00
/* config logout */
2004-01-06 14:38:45 +01:00
$expire = time () - 86400 ;
2004-01-18 22:03:56 +01:00
$this -> set_cookie ( 'ConfigUser' , '' , $expire , '/' );
2004-01-06 14:38:45 +01:00
$this -> set_cookie ( 'ConfigPW' , '' , $expire , '/' );
$this -> set_cookie ( 'ConfigDomain' , '' , $expire , '/' );
$this -> set_cookie ( 'ConfigLang' , '' , $expire , '/' );
2003-08-28 16:31:11 +02:00
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'LastDomain' ] = $_COOKIE [ 'ConfigDomain' ];
2002-03-04 04:08:47 +01:00
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'ConfigLoginMSG' ] = lang ( 'You have successfully logged out' );
2002-03-03 22:47:30 +01:00
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'HeaderLoginMSG' ] = '' ;
return False ;
2004-01-06 14:38:45 +01:00
case 'header' :
2002-03-04 03:55:53 +01:00
/* header admin logout */
2004-01-06 14:38:45 +01:00
$expire = time () - 86400 ;
2004-01-18 22:03:56 +01:00
$this -> set_cookie ( 'HeaderUser' , '' , $expire , '/' );
2004-01-06 14:38:45 +01:00
$this -> set_cookie ( 'HeaderPW' , '' , $expire , '/' );
$this -> set_cookie ( 'ConfigLang' , '' , $expire , '/' );
2002-03-04 04:08:47 +01:00
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'HeaderLoginMSG' ] = lang ( 'You have successfully logged out' );
2002-03-04 03:55:53 +01:00
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'ConfigLoginMSG' ] = '' ;
return False ;
2002-03-03 22:47:30 +01:00
}
2004-01-06 14:38:45 +01:00
/* We get here if FormLogout is not set (LOGIN or subsequent pages) */
/* Expire login if idle for 20 minutes. The cookies are updated on every page load. */
$expire = ( int )( time () + ( 1200 * 9 ));
switch ( strtolower ( $auth_type ))
2002-03-03 22:47:30 +01:00
{
2004-01-06 14:38:45 +01:00
case 'header' :
if ( ! empty ( $HeaderLogin ))
{
/* header admin login */
2004-01-18 22:03:56 +01:00
/* New test is md5, cleartext version is for header < 1.26 */
if ( $FormUser == stripslashes ( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'header_admin_user' ]) &&
( md5 ( $FormPW ) == stripslashes ( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'header_admin_password' ]) ||
$FormPW == stripslashes ( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'header_admin_password' ]))
)
2004-01-06 14:38:45 +01:00
{
2004-01-18 22:03:56 +01:00
$this -> set_cookie ( 'HeaderUser' , " $FormUser " , $expire , '/' );
2004-01-06 14:38:45 +01:00
$this -> set_cookie ( 'HeaderPW' , " $FormPW " , $expire , '/' );
$this -> set_cookie ( 'ConfigLang' , " $ConfigLang " , $expire , '/' );
return True ;
}
else
{
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'HeaderLoginMSG' ] = lang ( 'Invalid password' );
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'ConfigLoginMSG' ] = '' ;
return False ;
}
}
elseif ( ! empty ( $HeaderPW ) && $auth_type == 'Header' )
{
// Returning after login to header admin
2004-01-18 22:03:56 +01:00
/* New test is md5, cleartext version is for header < 1.26 */
if ( $HeaderUser == stripslashes ( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'header_admin_user' ]) &&
( md5 ( $HeaderPW ) == stripslashes ( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'header_admin_password' ]) ||
$HeaderPW == stripslashes ( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'header_admin_password' ]))
)
2004-01-06 14:38:45 +01:00
{
2004-01-18 22:03:56 +01:00
$this -> set_cookie ( 'HeaderUser' , " $HeaderUser " , $expire , '/' );
2004-01-06 14:38:45 +01:00
$this -> set_cookie ( 'HeaderPW' , " $HeaderPW " , $expire , '/' );
$this -> set_cookie ( 'ConfigLang' , " $ConfigLang " , $expire , '/' );
return True ;
}
else
{
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'HeaderLoginMSG' ] = lang ( 'Invalid password' );
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'ConfigLoginMSG' ] = '' ;
return False ;
}
}
break ;
case 'config' :
if ( ! empty ( $ConfigLogin ))
{
/* config login */
2004-01-18 22:03:56 +01:00
/* New test is md5, cleartext version is for header < 1.26 */
if ( isset ( $GLOBALS [ 'phpgw_domain' ][ $FormDomain ]) &&
$FormUser == stripslashes ( @ $GLOBALS [ 'phpgw_domain' ][ $FormDomain ][ 'config_user' ]) &&
( md5 ( $FormPW ) == stripslashes ( @ $GLOBALS [ 'phpgw_domain' ][ $FormDomain ][ 'config_passwd' ]) ||
$FormPW == stripslashes ( @ $GLOBALS [ 'phpgw_domain' ][ $FormDomain ][ 'config_passwd' ]))
)
2004-01-06 14:38:45 +01:00
{
2004-01-18 22:03:56 +01:00
$this -> set_cookie ( 'ConfigUser' , " $FormUser " , $expire , '/' );
2004-01-06 14:38:45 +01:00
$this -> set_cookie ( 'ConfigPW' , " $FormPW " , $expire , '/' );
$this -> set_cookie ( 'ConfigDomain' , " $FormDomain " , $expire , '/' );
/* Set this now since the cookie will not be available until the next page load */
$this -> ConfigDomain = " $FormDomain " ;
$this -> set_cookie ( 'ConfigLang' , " $ConfigLang " , $expire , '/' );
return True ;
}
else
{
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'ConfigLoginMSG' ] = lang ( 'Invalid password' );
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'HeaderLoginMSG' ] = '' ;
return False ;
}
}
elseif ( ! empty ( $ConfigPW ))
{
// Returning after login to config
2004-01-18 22:03:56 +01:00
/* New test is md5, cleartext version is for header < 1.26 */
if ( $ConfigUser == stripslashes ( $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'config_user' ]) &&
( md5 ( $ConfigPW ) == stripslashes ( $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'config_passwd' ]) ||
$ConfigPW == stripslashes ( $GLOBALS [ 'phpgw_domain' ][ $this -> ConfigDomain ][ 'config_passwd' ]))
)
2004-01-06 14:38:45 +01:00
{
2004-01-18 22:03:56 +01:00
$this -> set_cookie ( 'ConfigUser' , " $ConfigUser " , $expire , '/' );
2004-01-06 14:38:45 +01:00
$this -> set_cookie ( 'ConfigPW' , " $ConfigPW " , $expire , '/' );
$this -> set_cookie ( 'ConfigDomain' , $this -> ConfigDomain , $expire , '/' );
$this -> set_cookie ( 'ConfigLang' , " $ConfigLang " , $expire , '/' );
return True ;
}
else
{
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'ConfigLoginMSG' ] = lang ( 'Invalid password' );
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'HeaderLoginMSG' ] = '' ;
return False ;
}
}
break ;
2002-03-03 22:47:30 +01:00
}
2004-01-06 14:38:45 +01:00
return False ;
2002-03-03 22:47:30 +01:00
}
2002-05-11 21:30:10 +02:00
function checkip ( $remoteip = '' )
{
$allowed_ips = split ( ',' , $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'setup_acl' ]);
if ( is_array ( $allowed_ips ))
{
$foundip = False ;
while ( list (, $value ) = @ each ( $allowed_ips ))
{
$test = split ( " \ . " , $value );
if ( count ( $test ) < 3 )
{
$value .= " .0.0 " ;
$tmp = split ( " \ . " , $remoteip );
$tmp [ 2 ] = 0 ;
$tmp [ 3 ] = 0 ;
$testremoteip = join ( '.' , $tmp );
}
elseif ( count ( $test ) < 4 )
{
$value .= " .0 " ;
$tmp = split ( " \ . " , $remoteip );
$tmp [ 3 ] = 0 ;
$testremoteip = join ( '.' , $tmp );
}
elseif ( count ( $test ) == 4 &&
2003-11-04 15:14:57 +01:00
( int ) $test [ 3 ] == 0 )
2002-05-11 21:30:10 +02:00
{
$tmp = split ( " \ . " , $remoteip );
$tmp [ 3 ] = 0 ;
$testremoteip = join ( '.' , $tmp );
}
else
{
$testremoteip = $remoteip ;
}
//echo '<br>testing: ' . $testremoteip . ' compared to ' . $value;
if ( $testremoteip == $value )
{
//echo ' - PASSED!';
$foundip = True ;
}
}
if ( ! $foundip )
{
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'HeaderLoginMSG' ] = '' ;
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'ConfigLoginMSG' ] = lang ( 'Invalid IP address' );
return False ;
}
}
return True ;
}
2002-03-03 22:47:30 +01:00
/*!
@ function get_major
@ abstract Return X . X . X major version from X . X . X . X versionstring
@ param $
*/
function get_major ( $versionstring )
{
if ( ! $versionstring )
{
return False ;
}
2003-08-28 16:31:11 +02:00
$version = str_replace ( 'pre' , '.' , $versionstring );
2002-03-03 22:47:30 +01:00
$varray = explode ( '.' , $version );
$major = implode ( '.' , array ( $varray [ 0 ], $varray [ 1 ], $varray [ 2 ]));
return $major ;
}
/*!
@ function clear_session_cache
@ abstract Clear system / user level cache so as to have it rebuilt with the next access
@ param None
*/
function clear_session_cache ()
{
2002-04-10 01:48:36 +02:00
$tables = Array ();
2002-04-17 13:58:09 +02:00
$tablenames = $this -> db -> table_names ();
2003-08-28 16:31:11 +02:00
foreach ( $tablenames as $key => $val )
2002-03-03 22:47:30 +01:00
{
$tables [] = $val [ 'table_name' ];
}
if ( in_array ( 'phpgw_app_sessions' , $tables ))
{
$this -> db -> lock ( array ( 'phpgw_app_sessions' ));
@ $this -> db -> query ( " DELETE FROM phpgw_app_sessions WHERE sessionid = '0' and loginid = '0' and app = 'phpgwapi' and location = 'config' " , __LINE__ , __FILE__ );
@ $this -> db -> query ( " DELETE FROM phpgw_app_sessions WHERE app = 'phpgwapi' and location = 'phpgw_info_cache' " , __LINE__ , __FILE__ );
$this -> db -> unlock ();
}
}
/*!
@ function register_app
@ abstract Add an application to the phpgw_applications table
@ param $appname Application 'name' with a matching $setup_info [ $appname ] array slice
@ param $enable optional , set to True / False to override setup . inc . php setting
*/
function register_app ( $appname , $enable = 99 )
{
$setup_info = $GLOBALS [ 'setup_info' ];
if ( ! $appname )
{
return False ;
}
if ( $enable == 99 )
{
$enable = $setup_info [ $appname ][ 'enable' ];
}
2003-11-04 15:14:57 +01:00
$enable = ( int ) $enable ;
2002-03-03 22:47:30 +01:00
/*
2003-08-28 16:31:11 +02:00
Use old applications table if the currentver is less than 0.9 . 10 pre8 ,
but not if the currentver = '' , which probably means new install .
2002-03-03 22:47:30 +01:00
*/
if ( $this -> alessthanb ( $setup_info [ 'phpgwapi' ][ 'currentver' ], '0.9.10pre8' ) && ( $setup_info [ 'phpgwapi' ][ 'currentver' ] != '' ))
{
$appstbl = 'applications' ;
}
else
{
$appstbl = 'phpgw_applications' ;
if ( $this -> amorethanb ( $setup_info [ 'phpgwapi' ][ 'currentver' ], '0.9.13.014' ))
{
$use_appid = True ;
}
}
if ( $GLOBALS [ 'DEBUG' ])
{
echo '<br>register_app(): ' . $appname . ', version: ' . $setup_info [ $appname ][ 'version' ] . ', table: ' . $appstbl . '<br>' ;
// _debug_array($setup_info[$appname]);
}
if ( $setup_info [ $appname ][ 'version' ])
{
if ( $setup_info [ $appname ][ 'tables' ])
{
$tables = implode ( ',' , $setup_info [ $appname ][ 'tables' ]);
}
2003-08-28 16:31:11 +02:00
if ( $setup_info [ $appname ][ 'tables_use_prefix' ] == True )
{
echo $setup_info [ $appname ][ 'name' ] . ' uses tables_use_prefix, storing '
2003-10-22 13:28:17 +02:00
. $setup_info [ $appname ][ 'tables_prefix' ]
2003-08-28 16:31:11 +02:00
. ' as prefix for ' . $setup_info [ $appname ][ 'name' ] . " tables \n " ;
2003-10-22 13:28:17 +02:00
2003-08-28 16:31:11 +02:00
$sql = " INSERT INTO phpgw_config (config_app,config_name,config_value) "
. " VALUES (' " . $setup_info [ $appname ][ 'name' ] . " ',' "
. $appname . " _tables_prefix',' " . $setup_info [ $appname ][ 'tables_prefix' ] . " '); " ;
$this -> db -> query ( $sql , __LINE__ , __FILE__ );
}
2002-03-03 22:47:30 +01:00
if ( $use_appid )
{
$this -> db -> query ( " SELECT MAX(app_id) FROM $appstbl " );
$this -> db -> next_record ();
if ( $this -> db -> f ( 0 ))
{
$app_id = ( $this -> db -> f ( 0 ) + 1 ) . ',' ;
$app_idstr = 'app_id,' ;
}
else
{
srand ( 100000 );
$app_id = rand ( 1 , 100000 ) . ',' ;
$app_idstr = 'app_id,' ;
}
}
$this -> db -> query ( " INSERT INTO $appstbl "
2002-10-27 00:39:19 +02:00
. " ( $app_idstr app_name,app_enabled,app_order,app_tables,app_version) "
2002-03-03 22:47:30 +01:00
. " VALUES ( "
. $app_id
. " ' " . $setup_info [ $appname ][ 'name' ] . " ', "
. $enable . " , "
2003-11-04 15:14:57 +01:00
. ( int ) $setup_info [ $appname ][ 'app_order' ] . " , "
2002-03-03 22:47:30 +01:00
. " ' " . $tables . " ', "
. " ' " . $setup_info [ $appname ][ 'version' ] . " '); "
);
$this -> clear_session_cache ();
}
}
/*!
@ function app_registered
@ abstract Check if an application has info in the db
@ param $appname Application 'name' with a matching $setup_info [ $appname ] array slice
@ param $enabled optional , set to False to not enable this app
*/
function app_registered ( $appname )
{
$setup_info = $GLOBALS [ 'setup_info' ];
if ( ! $appname )
{
return False ;
}
if ( $this -> alessthanb ( $setup_info [ 'phpgwapi' ][ 'currentver' ], '0.9.10pre8' ) && ( $setup_info [ 'phpgwapi' ][ 'currentver' ] != '' ))
{
$appstbl = 'applications' ;
}
else
{
$appstbl = 'phpgw_applications' ;
}
if ( @ $GLOBALS [ 'DEBUG' ])
{
echo '<br>app_registered(): checking ' . $appname . ', table: ' . $appstbl ;
// _debug_array($setup_info[$appname]);
}
$this -> db -> query ( " SELECT COUNT(app_name) FROM $appstbl WHERE app_name=' " . $appname . " ' " );
$this -> db -> next_record ();
if ( $this -> db -> f ( 0 ))
{
if ( @ $GLOBALS [ 'DEBUG' ])
{
echo '... app previously registered.' ;
}
return True ;
}
if ( @ $GLOBALS [ 'DEBUG' ])
{
echo '... app not registered' ;
}
return False ;
}
/*!
@ function update_app
@ abstract Update application info in the db
@ param $appname Application 'name' with a matching $setup_info [ $appname ] array slice
@ param $enabled optional , set to False to not enable this app
*/
function update_app ( $appname )
{
$setup_info = $GLOBALS [ 'setup_info' ];
if ( ! $appname )
{
return False ;
}
if ( $this -> alessthanb ( $setup_info [ 'phpgwapi' ][ 'currentver' ], '0.9.10pre8' ) && ( $setup_info [ 'phpgwapi' ][ 'currentver' ] != '' ))
{
$appstbl = 'applications' ;
}
else
{
$appstbl = 'phpgw_applications' ;
}
if ( $GLOBALS [ 'DEBUG' ])
{
echo '<br>update_app(): ' . $appname . ', version: ' . $setup_info [ $appname ][ 'currentver' ] . ', table: ' . $appstbl . '<br>' ;
// _debug_array($setup_info[$appname]);
}
$this -> db -> query ( " SELECT COUNT(app_name) FROM $appstbl WHERE app_name=' " . $appname . " ' " );
$this -> db -> next_record ();
if ( ! $this -> db -> f ( 0 ))
{
return False ;
}
if ( $setup_info [ $appname ][ 'version' ])
{
//echo '<br>' . $setup_info[$appname]['version'];
if ( $setup_info [ $appname ][ 'tables' ])
{
$tables = implode ( ',' , $setup_info [ $appname ][ 'tables' ]);
}
$sql = " UPDATE $appstbl "
. " SET app_name=' " . $setup_info [ $appname ][ 'name' ] . " ', "
2003-11-04 15:14:57 +01:00
. " app_enabled= " . ( int ) $setup_info [ $appname ][ 'enable' ] . " , "
. " app_order= " . ( int ) $setup_info [ $appname ][ 'app_order' ] . " , "
2002-03-03 22:47:30 +01:00
. " app_tables=' " . $tables . " ', "
. " app_version=' " . $setup_info [ $appname ][ 'version' ] . " ' "
. " WHERE app_name=' " . $appname . " ' " ;
//echo $sql; exit;
$this -> db -> query ( $sql );
}
}
/*!
@ function update_app_version
@ abstract Update application version in applications table , post upgrade
@ param $setup_info Array of application information ( multiple apps or single )
@ param $appname Application 'name' with a matching $setup_info [ $appname ] array slice
@ param $tableschanged ? ? ?
*/
function update_app_version ( $setup_info , $appname , $tableschanged = True )
{
if ( ! $appname )
{
return False ;
}
if ( $this -> alessthanb ( $setup_info [ 'phpgwapi' ][ 'currentver' ], '0.9.10pre8' ) && ( $setup_info [ 'phpgwapi' ][ 'currentver' ] != '' ))
{
$appstbl = 'applications' ;
}
else
{
$appstbl = 'phpgw_applications' ;
}
if ( $tableschanged == True )
{
$GLOBALS [ 'phpgw_info' ][ 'setup' ][ 'tableschanged' ] = True ;
}
if ( $setup_info [ $appname ][ 'currentver' ])
{
$this -> db -> query ( " UPDATE $appstbl SET app_version=' " . $setup_info [ $appname ][ 'currentver' ] . " ' WHERE app_name=' " . $appname . " ' " );
}
return $setup_info ;
}
/*!
@ function deregister_app
@ abstract de - Register an application
@ param $appname Application 'name' with a matching $setup_info [ $appname ] array slice
*/
function deregister_app ( $appname )
{
if ( ! $appname )
{
return False ;
}
$setup_info = $GLOBALS [ 'setup_info' ];
if ( $this -> alessthanb ( $setup_info [ 'phpgwapi' ][ 'currentver' ], '0.9.10pre8' ) && ( $setup_info [ 'phpgwapi' ][ 'currentver' ] != '' ))
{
$appstbl = 'applications' ;
}
else
{
$appstbl = 'phpgw_applications' ;
}
//echo 'DELETING application: ' . $appname;
$this -> db -> query ( " DELETE FROM $appstbl WHERE app_name=' " . $appname . " ' " );
$this -> clear_session_cache ();
}
/*!
@ function register_hooks
@ abstract Register an application ' s hooks
@ param $appname Application 'name' with a matching $setup_info [ $appname ] array slice
*/
function register_hooks ( $appname )
{
$setup_info = $GLOBALS [ 'setup_info' ];
if ( ! $appname )
{
return False ;
}
if ( $this -> alessthanb ( $setup_info [ 'phpgwapi' ][ 'currentver' ], '0.9.8pre5' ) && ( $setup_info [ 'phpgwapi' ][ 'currentver' ] != '' ))
{
/* No phpgw_hooks table yet. */
return False ;
}
2003-04-28 00:26:04 +02:00
if ( ! is_object ( $this -> hooks ))
2002-03-03 22:47:30 +01:00
{
2003-04-28 00:26:04 +02:00
$this -> hooks = CreateObject ( 'phpgwapi.hooks' , $this -> db );
2002-03-03 22:47:30 +01:00
}
2003-04-28 00:26:04 +02:00
$this -> hooks -> register_hooks ( $appname , $setup_info [ $appname ][ 'hooks' ]);
2002-03-03 22:47:30 +01:00
}
/*!
@ function update_hooks
@ abstract Update an application ' s hooks
@ param $appname Application 'name' with a matching $setup_info [ $appname ] array slice
*/
function update_hooks ( $appname )
{
2003-04-28 00:26:04 +02:00
$this -> register_hooks ( $appname );
2002-03-03 22:47:30 +01:00
}
/*!
@ function deregister_hooks
@ abstract de - Register an application ' s hooks
@ param $appname Application 'name' with a matching $setup_info [ $appname ] array slice
*/
function deregister_hooks ( $appname )
{
if ( $this -> alessthanb ( $setup_info [ 'phpgwapi' ][ 'currentver' ], '0.9.8pre5' ))
{
/* No phpgw_hooks table yet. */
return False ;
}
if ( ! $appname )
{
return False ;
}
2003-04-28 00:26:04 +02:00
2002-03-03 22:47:30 +01:00
//echo "DELETING hooks for: " . $setup_info[$appname]['name'];
2003-04-28 00:26:04 +02:00
if ( ! is_object ( $this -> hooks ))
{
$this -> hooks = CreateObject ( 'phpgwapi.hooks' , $this -> db );
}
$this -> hooks -> register_hooks ( $appname );
2002-03-03 22:47:30 +01:00
}
/*!
2003-08-28 16:31:11 +02:00
@ function hook
@ abstract call the hooks for a single application
@ param $location hook location - required
@ param $appname application name - optional
*/
2002-03-03 22:47:30 +01:00
function hook ( $location , $appname = '' )
{
2003-04-28 00:26:04 +02:00
if ( ! is_object ( $this -> hooks ))
2002-03-03 22:47:30 +01:00
{
2003-04-28 00:26:04 +02:00
$this -> hooks = CreateObject ( 'phpgwapi.hooks' , $this -> db );
2002-03-03 22:47:30 +01:00
}
2003-04-28 10:25:59 +02:00
return $this -> hooks -> single ( $location , $appname , True , True );
2002-03-03 22:47:30 +01:00
}
/*
@ function alessthanb
@ abstract phpgw version checking , is param 1 < param 2 in phpgw versionspeak ?
@ param $a phpgw version number to check if less than $b
@ param $b phpgw version number to check $a against
#return True if $a < $b
*/
function alessthanb ( $a , $b , $DEBUG = False )
{
$num = array ( '1st' , '2nd' , '3rd' , '4th' );
if ( $DEBUG )
{
echo '<br>Input values: '
. 'A="' . $a . '", B="' . $b . '"' ;
}
2003-11-04 15:14:57 +01:00
$newa = str_replace ( 'pre' , '.' , $a );
$newb = str_replace ( 'pre' , '.' , $b );
2002-03-03 22:47:30 +01:00
$testa = explode ( '.' , $newa );
if ( @ $testa [ 1 ] == '' )
{
$testa [ 1 ] = 0 ;
}
if ( @ $testa [ 3 ] == '' )
{
$testa [ 3 ] = 0 ;
}
$testb = explode ( '.' , $newb );
if ( @ $testb [ 1 ] == '' )
{
$testb [ 1 ] = 0 ;
}
if ( @ $testb [ 3 ] == '' )
{
$testb [ 3 ] = 0 ;
}
$less = 0 ;
for ( $i = 0 ; $i < count ( $testa ); $i ++ )
{
2003-11-04 15:14:57 +01:00
if ( $DEBUG ) { echo '<br>Checking if ' . ( int ) $testa [ $i ] . ' is less than ' . ( int ) $testb [ $i ] . ' ...' ; }
if (( int ) $testa [ $i ] < ( int ) $testb [ $i ])
2002-03-03 22:47:30 +01:00
{
if ( $DEBUG ) { echo ' yes.' ; }
$less ++ ;
if ( $i < 3 )
{
/* Ensure that this is definitely smaller */
if ( $DEBUG ) { echo " This is the $num[$i] octet, so A is definitely less than B. " ; }
$less = 5 ;
break ;
}
}
2003-11-04 15:14:57 +01:00
elseif (( int ) $testa [ $i ] > ( int ) $testb [ $i ])
2002-03-03 22:47:30 +01:00
{
if ( $DEBUG ) { echo ' no.' ; }
$less -- ;
if ( $i < 2 )
{
/* Ensure that this is definitely greater */
if ( $DEBUG ) { echo " This is the $num[$i] octet, so A is definitely greater than B. " ; }
$less = - 5 ;
break ;
}
}
else
{
if ( $DEBUG ) { echo ' no, they are equal.' ; }
$less = 0 ;
}
}
if ( $DEBUG ) { echo '<br>Check value is: "' . $less . '"' ; }
if ( $less > 0 )
{
if ( $DEBUG ) { echo '<br>A is less than B' ; }
return True ;
}
elseif ( $less < 0 )
{
if ( $DEBUG ) { echo '<br>A is greater than B' ; }
return False ;
}
else
{
if ( $DEBUG ) { echo '<br>A is equal to B' ; }
return False ;
}
}
/*!
@ function amorethanb
@ abstract phpgw version checking , is param 1 > param 2 in phpgw versionspeak ?
@ param $a phpgw version number to check if more than $b
@ param $b phpgw version number to check $a against
#return True if $a < $b
*/
function amorethanb ( $a , $b , $DEBUG = False )
{
$num = array ( '1st' , '2nd' , '3rd' , '4th' );
if ( $DEBUG )
{
echo '<br>Input values: '
. 'A="' . $a . '", B="' . $b . '"' ;
}
2003-11-04 15:14:57 +01:00
$newa = str_replace ( 'pre' , '.' , $a );
$newb = str_replace ( 'pre' , '.' , $b );
2002-03-03 22:47:30 +01:00
$testa = explode ( '.' , $newa );
if ( $testa [ 3 ] == '' )
{
$testa [ 3 ] = 0 ;
}
$testb = explode ( '.' , $newb );
if ( $testb [ 3 ] == '' )
{
$testb [ 3 ] = 0 ;
}
$less = 0 ;
for ( $i = 0 ; $i < count ( $testa ); $i ++ )
{
2003-11-04 15:14:57 +01:00
if ( $DEBUG ) { echo '<br>Checking if ' . ( int ) $testa [ $i ] . ' is more than ' . ( int ) $testb [ $i ] . ' ...' ; }
if (( int ) $testa [ $i ] > ( int ) $testb [ $i ])
2002-03-03 22:47:30 +01:00
{
if ( $DEBUG ) { echo ' yes.' ; }
$less ++ ;
if ( $i < 3 )
{
/* Ensure that this is definitely greater */
if ( $DEBUG ) { echo " This is the $num[$i] octet, so A is definitely greater than B. " ; }
$less = 5 ;
break ;
}
}
2003-11-04 15:14:57 +01:00
elseif (( int ) $testa [ $i ] < ( int ) $testb [ $i ])
2002-03-03 22:47:30 +01:00
{
if ( $DEBUG ) { echo ' no.' ; }
$less -- ;
if ( $i < 2 )
{
/* Ensure that this is definitely smaller */
if ( $DEBUG ) { echo " This is the $num[$i] octet, so A is definitely less than B. " ; }
$less = - 5 ;
break ;
}
}
else
{
if ( $DEBUG ) { echo ' no, they are equal.' ; }
$less = 0 ;
}
}
if ( $DEBUG ) { echo '<br>Check value is: "' . $less . '"' ; }
if ( $less > 0 )
{
if ( $DEBUG ) { echo '<br>A is greater than B' ; }
return True ;
}
elseif ( $less < 0 )
{
if ( $DEBUG ) { echo '<br>A is less than B' ; }
return False ;
}
else
{
if ( $DEBUG ) { echo '<br>A is equal to B' ; }
return False ;
}
}
2003-09-11 20:13:49 +02:00
function get_hooks_table_name ()
{
2003-11-04 15:14:57 +01:00
if ( @ $this -> alessthanb ( $GLOBALS [ 'setup_info' ][ 'phpgwapi' ][ 'currentver' ], '0.9.8pre5' ) &&
@ $GLOBALS [ 'setup_info' ][ 'phpgwapi' ][ 'currentver' ] != '' )
2003-08-28 16:31:11 +02:00
{
/* No phpgw_hooks table yet. */
return False ;
}
2003-09-11 20:13:49 +02:00
return 'phpgw_hooks' ;
}
2003-12-29 22:05:06 +01:00
function setup_account_object ()
{
if ( ! is_object ( $GLOBALS [ 'phpgw' ] -> accounts ))
{
if ( ! is_object ( $this -> db ))
{
$this -> loaddb ();
}
/* Load up some configured values */
$this -> db -> query ( " SELECT config_name,config_value FROM phpgw_config " .
" WHERE config_name LIKE 'ldap%' OR config_name LIKE 'account_%' " , __LINE__ , __FILE__ );
while ( $this -> db -> next_record ())
{
$GLOBALS [ 'phpgw_info' ][ 'server' ][ $this -> db -> f ( 'config_name' )] = $this -> db -> f ( 'config_value' );
}
if ( ! is_object ( $GLOBALS [ 'phpgw' ]))
{
$GLOBALS [ 'phpgw' ] = CreateObject ( 'phpgwapi.phpgw' );
}
copyobj ( $this -> db , $GLOBALS [ 'phpgw' ] -> db );
$GLOBALS [ 'phpgw' ] -> common = CreateObject ( 'phpgwapi.common' );
$GLOBALS [ 'phpgw' ] -> accounts = CreateObject ( 'phpgwapi.accounts' );
if (( $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'account_repository' ] == 'ldap' ) &&
! $GLOBALS [ 'phpgw' ] -> accounts -> ds )
{
printf ( " <b>Error: Error connecting to LDAP server %s!</b><br> " , $GLOBALS [ 'phpgw_info' ][ 'server' ][ 'ldap_host' ]);
exit ;
}
}
}
/*!
@ function add_account
@ abstract add an user account or a user group
@ param username string alphanumerical username or groupname ( account_lid )
@ param first , last string first / last name
@ param $passwd string cleartext pw
@ param $group string / boolean Groupname for users primary group or False for a group , default 'Default'
@ param $changepw boolean user has right to change pw , default False
@ returns the numerical user - id
@ note if the $username already exists , only the id is returned , no new user / group gets created
*/
function add_account ( $username , $first , $last , $passwd , $group = 'default' , $changepw = False )
{
$this -> setup_account_object ();
2004-01-26 04:26:11 +01:00
$groupid = $group ? $GLOBALS [ 'phpgw' ] -> accounts -> name2id ( $group ) : False ;
2003-12-29 22:05:06 +01:00
2004-01-26 04:26:11 +01:00
if ( ! ( $accountid = $GLOBALS [ 'phpgw' ] -> accounts -> name2id ( $username )))
2003-12-29 22:05:06 +01:00
{
2004-01-26 04:26:11 +01:00
$accountid = $accountid ? $accountid : $GLOBALS [ 'phpgw' ] -> accounts -> create ( array (
2003-12-29 22:05:06 +01:00
'account_type' => $group ? 'u' : 'g' ,
'account_lid' => $username ,
'account_passwd' => $passwd ,
'account_firstname' => $first ,
'account_lastname' => $last ,
'account_status' => 'A' ,
'account_primary_group' => $groupid ,
'account_expires' => - 1
));
}
2004-01-26 04:26:11 +01:00
if ( $groupid )
2003-12-29 22:05:06 +01:00
{
2004-01-26 04:26:11 +01:00
$this -> add_acl ( 'phpgw_group' ,( int ) $groupid ,( int ) $accountid );
2003-12-29 22:05:06 +01:00
}
2004-01-26 04:26:11 +01:00
$this -> add_acl ( 'preferences' , 'changepassword' ,( int ) $accountid , ( boolean ) $changepw );
2003-12-29 22:05:06 +01:00
return $accountid ;
}
/*!
@ function add_acl
@ abstract Add ACL rights
@ param $app string / array with app - names
@ param $locations string eg . run
@ param $account int / string accountid or account_lid
@ param $rights int rights to set , default 1
*/
function add_acl ( $apps , $location , $account , $rights = 1 )
{
if ( ! is_int ( $account ))
{
$this -> setup_account_object ();
$account = $GLOBALS [ 'phpgw' ] -> accounts -> name2id ( $account );
}
if ( ! is_object ( $this -> db )) $this -> loaddb ();
if ( ! is_array ( $apps )) $apps = array ( $apps );
foreach ( $apps as $app )
{
$this -> db -> query ( " INSERT INTO phpgw_acl(acl_appname,acl_location,acl_account,acl_rights) VALUES(' $app ',' $location ', $account , $rights ) " );
}
}
2002-03-03 22:47:30 +01:00
}
?>