<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#----------------------------------------------------------------------------
# \file         dolibarr.pl
# \brief        Dolibarr script install for Virtualmin Pro
# \author       (c)2009-2020 Regis Houssin  &lt;regis.houssin@inodbox.com&gt;
#----------------------------------------------------------------------------


# script_dolibarr_desc()
sub script_dolibarr_desc
{
return "Dolibarr";
}

sub script_dolibarr_uses
{
return ( "php" );
}

# script_dolibarr_longdesc()
sub script_dolibarr_longdesc
{
return "Dolibarr ERP/CRM is a powerful Open Source software to manage a professional or foundation activity (small and medium enterprises, freelancers).";
}

sub script_dolibarr_author
{
return "Regis Houssin";
}

# script_dolibarr_versions()
sub script_dolibarr_versions
{
return ( "14.0.5", "13.0.5", "12.0.5", "11.0.5", "10.0.7", "9.0.4", "8.0.6", "7.0.5" );
}

sub script_dolibarr_release
{
return 2;	# for mysqli fix
}

sub script_dolibarr_category
{
return "Commerce";
}

sub script_dolibarr_php_vers
{
return ( 5 );
}

sub script_dolibarr_php_modules
{
local ($d, $ver, $phpver, $opts) = @_;
local ($dbtype, $dbname) = split(/_/, $opts-&gt;{'db'}, 2);
return $dbtype eq "mysql" ? ("mysql") : ("pgsql");
}

sub script_dolibarr_dbs
{
local ($d, $ver) = @_;
return ("mysql", "postgres");
}

# script_dolibarr_depends(&amp;domain, version)
sub script_dolibarr_depends
{
local ($d, $ver, $sinfo, $phpver) = @_;
local @rv;

if ($ver &gt;= 3.6) {
	# Check for PHP 5.3+
	local $phpv = &amp;get_php_version($phpver || 5, $d);
	if (!$phpv) {
		push(@rv, "Could not work out exact PHP version");
		}
	elsif ($phpv &lt; 5.3) {
		push(@rv, "Dolibarr requires PHP version 5.3 or later");
		}
	}
if ($ver &gt;= 12.0) {
	# Check for PHP 5.6+
	local $phpv = &amp;get_php_version($phpver || 5, $d);
	if (!$phpv) {
		push(@rv, "Could not work out exact PHP version");
		}
	elsif ($phpv &lt; 5.6) {
		push(@rv, "Dolibarr requires PHP version 5.6 or later");
		}
	}

return @rv;
}

# script_dolibarr_params(&amp;domain, version, &amp;upgrade-info)
# Returns HTML for table rows for options for installing dolibarr
sub script_dolibarr_params
{
local ($d, $ver, $upgrade) = @_;
local $rv;
local $hdir = &amp;public_html_dir($d, 1);
if ($upgrade) {
	# Options are fixed when upgrading
	local ($dbtype, $dbname) = split(/_/, $upgrade-&gt;{'opts'}-&gt;{'db'}, 2);
	$rv .= &amp;ui_table_row("Database for Dolibarr tables", $dbname);
	local $dir = $upgrade-&gt;{'opts'}-&gt;{'dir'};
	$dir =~ s/^$d-&gt;{'home'}\///;
	$rv .= &amp;ui_table_row("Install directory", $dir);
	}
else {
	# Show editable install options
	local @dbs = &amp;domain_databases($d, [ "mysql"]);
	$rv .= &amp;ui_table_row("Database for Dolibarr tables",
		     &amp;ui_database_select("db", undef, \@dbs, $d, "dolibarr"));
	$rv .= &amp;ui_table_row("Install sub-directory under &lt;tt&gt;$hdir&lt;/tt&gt;",
			     &amp;ui_opt_textbox("dir", &amp;substitute_scriptname_template("dolibarr", $d), 30, "At top level"));
	if ($d-&gt;{'ssl'} &amp;&amp; $ver &gt;= 3.0) {
		$rv .= &amp;ui_table_row("Force https connection?",
				     &amp;ui_yesno_radio("forcehttps", 0));
		}
	}
return $rv;
}

# script_dolibarr_parse(&amp;domain, version, &amp;in, &amp;upgrade-info)
# Returns either a hash ref of parsed options, or an error string
sub script_dolibarr_parse
{
local ($d, $ver, $in, $upgrade) = @_;
if ($upgrade) {
	# Options are always the same
	return $upgrade-&gt;{'opts'};
	}
else {
	local $hdir = &amp;public_html_dir($d, 0);
	$in{'dir_def'} || $in{'dir'} =~ /\S/ &amp;&amp; $in{'dir'} !~ /\.\./ ||
		return "Missing or invalid installation directory";
	local $dir = $in{'dir_def'} ? $hdir : "$hdir/$in{'dir'}";
	local ($newdb) = ($in-&gt;{'db'} =~ s/^\*//);
	return { 'db' =&gt; $in-&gt;{'db'},
		 'newdb' =&gt; $newdb,
		 'dir' =&gt; $dir,
		 'path' =&gt; $in-&gt;{'dir_def'} ? "/" : "/$in-&gt;{'dir'}",
		 'forcehttps' =&gt; $in-&gt;{'forcehttps'}, };
	}
}

# script_dolibarr_check(&amp;domain, version, &amp;opts, &amp;upgrade-info)
# Returns an error message if a required option is missing or invalid
sub script_dolibarr_check
{
local ($d, $ver, $opts, $upgrade) = @_;
$opts-&gt;{'dir'} =~ /^\// || return "Missing or invalid install directory";
$opts-&gt;{'db'} || return "Missing database";
if (-r "$opts-&gt;{'dir'}/conf/conf.php") {
	return "Dolibarr appears to be already installed in the selected directory";
	}
local ($dbtype, $dbname) = split(/_/, $opts-&gt;{'db'}, 2);
local $clash = &amp;find_database_table($dbtype, $dbname, "llx_.*");
$clash &amp;&amp; return "Dolibarr appears to be already using the selected database (table $clash)";
return undef;
}

# script_dolibarr_files(&amp;domain, version, &amp;opts, &amp;upgrade-info)
# Returns a list of files needed by dolibarr, each of which is a hash ref
# containing a name, filename and URL
sub script_dolibarr_files
{
local ($d, $ver, $opts, $upgrade) = @_;
local @files = ( { 'name' =&gt; "source",
	   'file' =&gt; "Dolibarr_$ver.tar.gz",
	   'url' =&gt; "http://prdownloads.sourceforge.net/dolibarr/dolibarr-$ver.tgz" } );
return @files;
}

sub script_dolibarr_commands
{
return ("tar", "gunzip");
}

# script_dolibarr_install(&amp;domain, version, &amp;opts, &amp;files, &amp;upgrade-info)
# Actually installs dolibarr, and returns either 1 and an informational
# message, or 0 and an error
sub script_dolibarr_install
{
local ($d, $version, $opts, $files, $upgrade, $domuser, $dompass) = @_;

local ($out, $ex);
if ($opts-&gt;{'newdb'} &amp;&amp; !$upgrade) {
        local $err = &amp;create_script_database($d, $opts-&gt;{'db'});
        return (0, "Database creation failed : $err") if ($err);
        }
local ($dbtype, $dbname) = split(/_/, $opts-&gt;{'db'}, 2);
local $dbuser = $dbtype eq "mysql" ? &amp;mysql_user($d) : &amp;postgres_user($d);
local $dbpass = $dbtype eq "mysql" ? &amp;mysql_pass($d) : &amp;postgres_pass($d, 1);
local $dbphptype = $dbtype eq "mysql" &amp;&amp; $version &lt; 3.6 ? "mysql" :
		   $dbtype eq "mysql" ? "mysqli" : "pgsql";
local $dbhost = &amp;get_database_host($dbtype, $d);
local $dberr = &amp;check_script_db_connection($dbtype, $dbname, $dbuser, $dbpass);
return (0, "Database connection failed : $dberr") if ($dberr);

# Extract tar file to temp dir and copy to target
local $temp = &amp;transname();
local $err = &amp;extract_script_archive($files-&gt;{'source'}, $temp, $d,
			     $opts-&gt;{'dir'}, "dolibarr-$ver/htdocs");
$err &amp;&amp; return (0, "Failed to extract source : $err");

# Add config file
local $cfiledir = "$opts-&gt;{'dir'}/conf/";
local $docdir = "$opts-&gt;{'dir'}/documents";
local $altdir = "$opts-&gt;{'dir'}/custom";
local $cfile = $cfiledir."conf.php";
local $oldcfile = &amp;transname();
local $olddocdir = &amp;transname();
local $oldaltdir = &amp;transname();
local $url;

$tmpl = &amp;get_template($d-&gt;{'template'});
$mycharset = $tmpl-&gt;{'mysql_charset'};
$mycollate = $tmpl-&gt;{'mysql_collate'};
$pgcharset = $tmpl-&gt;{'postgres_encoding'};
$charset = $dbtype eq "mysql" ? $mycharset : $pgcharset;
$collate = $dbtype eq "mysql" ? $mycollate : "C";

$path = &amp;script_path_url($d, $opts);
if ($path =~ /^https:/ || $d-&gt;{'ssl'}) {
        $url = "https://$d-&gt;{'dom'}";
}
else {
        $url = "http://$d-&gt;{'dom'}";
}
if ($opts-&gt;{'path'} =~ /\w/) {
	$url .= $opts-&gt;{'path'};
}

if (!$upgrade) {
	local $cdef = "$opts-&gt;{'dir'}/conf/conf.php.example";
	&amp;copy_source_dest_as_domain_user($d, $cdef, $cfile);
	&amp;set_permissions_as_domain_user($d, 0777, $cfiledir);
	&amp;copy_source_dest_as_domain_user($d, $cfile);
	&amp;run_as_domain_user($d, "mkdir ".quotemeta($docdir));
	&amp;set_permissions_as_domain_user($d, 0777, $docdir);
}
else {
	# Preserve old config file, documents and custom directory
	&amp;copy_source_dest($cfile, $oldcfile);
	&amp;copy_source_dest($docdir, $olddocdir);
	&amp;copy_source_dest($altdir, $oldaltdir);
}

if ($upgrade) {
	# Put back original config file, documents and custom directory
	&amp;copy_source_dest_as_domain_user($d, $oldcfile, $cfile);
	&amp;copy_source_dest_as_domain_user($d, $olddocdir, $docdir);
	&amp;copy_source_dest_as_domain_user($d, $oldaltdir, $altdir);
	
	# First page (Update database schema)
	local @params = ( [ "action", "upgrade" ],
			  [ "versionfrom", $upgrade-&gt;{'version'} ],
			  [ "versionto", $ver ],
	 		 );
	local $err = &amp;call_dolibarr_wizard_page(\@params, "upgrade", $d, $opts);
	return (-1, "Dolibarr wizard failed : $err") if ($err);
	
	# Second page (Migrate some data)
	local @params = ( [ "action", "upgrade" ],
			  [ "versionfrom", $upgrade-&gt;{'version'} ],
			  [ "versionto", $ver ],
			 );
	local $err = &amp;call_dolibarr_wizard_page(\@params, "upgrade2", $d, $opts);
	return (-1, "Dolibarr wizard failed : $err") if ($err);
	
	# Third page (Update version number)
	local @params = ( [ "action", "upgrade" ],
			  [ "versionfrom", $upgrade-&gt;{'version'} ],
			  [ "versionto", $ver ],
			  [ "installlock", "444" ],
			 );
	local $p = $ver &gt;= 3.8 ? "step5" : "etape5";
	local $err = &amp;call_dolibarr_wizard_page(\@params, $p, $d, $opts);
	return (-1, "Dolibarr wizard failed : $err") if ($err);
	
	# Remove the installation directory. (deprecated)
	# local $dinstall = "$opts-&gt;{'dir'}/install";
	# $dinstall  =~ s/\/$//;
	# $out = &amp;run_as_domain_user($d, "rm -rf ".quotemeta($dinstall));
	
	}
else {
	# First page (Db connection and config file creation)
	local @params = ( [ "main_dir", $opts-&gt;{'dir'} ],
			  [ "main_data_dir", $opts-&gt;{'dir'}."/documents" ],
			  [ "main_url", $url ],
			  [ "db_type", $dbphptype ],
			  [ "db_host", $dbhost ],
			  [ "db_name", $dbname ],
			  [ "db_user", $dbuser ],
			  [ "db_pass", $dbpass ],
			  [ "action", "set" ],
			  [ "main_force_https", $opts-&gt;{'forcehttps'} ],
			  [ "dolibarr_main_db_character_set", $charset ],
			  [ "dolibarr_main_db_collation", $collate ],
			  [ "usealternaterootdir", "1" ],
			  [ "main_alt_dir_name", "custom" ],
			 );
	local $p = $ver &gt;= 3.8 ? "step1" : "etape1";
	local $err = &amp;call_dolibarr_wizard_page(\@params, $p, $d, $opts);
	return (-1, "Dolibarr wizard failed : $err") if ($err);
	
	# Second page (Populate database)
	local @params = ( [ "action", "set" ] );
	local $p = $ver &gt;= 3.8 ? "step2" : "etape2";
	local $err = &amp;call_dolibarr_wizard_page(\@params, $p, $d, $opts);
	return (-1, "Dolibarr wizard failed : $err") if ($err);
	
	# Third page (Add administrator account)
	local @params = ( [ "action", "set" ],
			  [ "login", "admin" ],
			  [ "pass", $dompass ],
			  [ "pass_verif", $dompass ],
			  [ "installlock", "444" ],
	 		 );
	local $p = $ver &gt;= 3.8 ? "step5" : "etape5";
	local $err = &amp;call_dolibarr_wizard_page(\@params, $p, $d, $opts);
	return (-1, "Dolibarr wizard failed : $err") if ($err);
	
	# Remove the installation directory (deprecated)
	# local $dinstall = "$opts-&gt;{'dir'}/install";
	# $dinstall  =~ s/\/$//;
	# $out = &amp;run_as_domain_user($d, "rm -rf ".quotemeta($dinstall));
	
	# Protect config file
	&amp;set_permissions_as_domain_user($d, 0644, $cfile);
	&amp;set_permissions_as_domain_user($d, 0755, $cfiledir);
	}
 
# Return a URL for the user
local $rp = $opts-&gt;{'dir'};
$rp =~ s/^$d-&gt;{'home'}\///;
local $adminurl = $url;
return (1, "Dolibarr installation complete. Go to &lt;a target=_new href='$url'&gt;$url&lt;/a&gt; to use it.", "Under $rp using $dbtype database $dbname", $url, 'admin', $dompass);
}

# call_dolibarr_wizard_page(&amp;parameters, step-no, &amp;domain, &amp;opts)
sub call_dolibarr_wizard_page
{
local ($params, $page, $d, $opts) = @_;
local $params = join("&amp;", map { $_-&gt;[0]."=".&amp;urlize($_-&gt;[1]) } @$params );
local $ipage = $opts-&gt;{'path'}."/install/".$page.".php";
local ($iout, $ierror);
&amp;post_http_connection($d, $ipage, $params, \$iout, \$ierror);
if ($ierror) {
	return $ierror;
	}
return undef;
}

# script_dolibarr_uninstall(&amp;domain, version, &amp;opts)
# Un-installs a dolibarr installation, by deleting the directory.
# Returns 1 on success and a message, or 0 on failure and an error
sub script_dolibarr_uninstall
{
local ($d, $version, $opts) = @_;

# Remove the contents of the target directory
local $derr = &amp;delete_script_install_directory($d, $opts);
return (0, $derr) if ($derr);

# Remove all llx_ tables from the database
# 10 times because of constraints
for(my $i=0; $i&lt;10; $i++) {
	&amp;cleanup_script_database($d, $opts-&gt;{'db'}, "llx_");
	}

# Take out the DB
if ($opts-&gt;{'newdb'}) {
        &amp;delete_script_database($d, $opts-&gt;{'db'});
        }

return (1, "Dolibarr directory and tables deleted.");
}

# script_dolibarr_realversion(&amp;domain, &amp;opts)
# Returns the real version number of some script install, or undef if unknown
sub script_dolibarr_realversion
{
local ($d, $opts, $sinfo) = @_;
local $lref = &amp;read_file_lines("$opts-&gt;{'dir'}/filefunc.inc.php", 1);
foreach my $l (@$lref) {
		if ($l =~ /'DOL_VERSION',\s?'([0-9a-z\.\-]+)'/) {
                return $1;
                }
        }
return undef;
}

# script_dolibarr_check_latest(version)
# Checks if some version is the latest for this project, and if not returns
# a newer one. Otherwise returns undef.
sub script_dolibarr_check_latest
{
local ($ver) = @_;
local @vers = &amp;osdn_package_versions("dolibarr",
				$ver &gt;= 14.0 ? "dolibarr\\-(12\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 13.0 ? "dolibarr\\-(12\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 12.0 ? "dolibarr\\-(12\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 11.0 ? "dolibarr\\-(11\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 10.0 ? "dolibarr\\-(10\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 9.0 ? "dolibarr\\-(9\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 8.0 ? "dolibarr\\-(8\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 7.0 ? "dolibarr\\-(7\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 6.0 ? "dolibarr\\-(6\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 5.0 ? "dolibarr\\-(5\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 4.0 ? "dolibarr\\-(4\\.0\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 3.9 ? "dolibarr\\-(3\\.9\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 3.8 ? "dolibarr\\-(3\\.8\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 3.7 ? "dolibarr\\-(3\\.7\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 3.6 ? "dolibarr\\-(3\\.6\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 3.5 ? "dolibarr\\-(3\\.5\\.[0-9\\.]+)\\.tgz" :
				$ver &gt;= 2.9 ? "dolibarr\\-(2\\.9\\.[0-9\\.]+)\\.tgz" :
                              "dolibarr\\-(2\\.8\\.[0-9\\.]+)\\.tgz");
return "Failed to find versions" if (!@vers);
return $ver eq $vers[0] ? undef : $vers[0];
}

sub script_dolibarr_site
{
return 'https://www.dolibarr.org/';
}

sub script_dolibarr_passmode
{
return 2;
}

1;
</pre></body></html>