PERL6 114
Dolibarr.pl Guest on 7th May 2020 10:17:33 AM
  1. #----------------------------------------------------------------------------
  2. # \file         dolibarr.pl
  3. # \brief        Dolibarr script install for Virtualmin Pro
  4. # \author       (c)2009-2019 Regis Houssin  <[email protected]>
  5. #----------------------------------------------------------------------------
  6.  
  7.  
  8. # script_dolibarr_desc()
  9. sub script_dolibarr_desc
  10. {
  11. return "Dolibarr";
  12. }
  13.  
  14. sub script_dolibarr_uses
  15. {
  16. return ( "php" );
  17. }
  18.  
  19. # script_dolibarr_longdesc()
  20. sub script_dolibarr_longdesc
  21. {
  22. return "Dolibarr ERP/CRM is a powerful Open Source software to manage a professional or foundation activity (small and medium enterprises, freelancers).";
  23. }
  24.  
  25. sub script_dolibarr_author
  26. {
  27. return "Regis Houssin";
  28. }
  29.  
  30. # script_dolibarr_versions()
  31. sub script_dolibarr_versions
  32. {
  33. return ( "10.0.0", "9.0.3", "8.0.5", "7.0.5", "6.0.8" );
  34. }
  35.  
  36. sub script_dolibarr_release
  37. {
  38. return 2;       # for mysqli fix
  39. }
  40.  
  41. sub script_dolibarr_category
  42. {
  43. return "Commerce";
  44. }
  45.  
  46. sub script_dolibarr_php_vers
  47. {
  48. return ( 5 );
  49. }
  50.  
  51. sub script_dolibarr_php_modules
  52. {
  53. local ($d, $ver, $phpver, $opts) = @_;
  54. local ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
  55. return $dbtype eq "mysql" ? ("mysql") : ("pgsql");
  56. }
  57.  
  58. sub script_dolibarr_dbs
  59. {
  60. local ($d, $ver) = @_;
  61. return ("mysql", "postgres");
  62. }
  63.  
  64. # script_dolibarr_depends(&domain, version)
  65. sub script_dolibarr_depends
  66. {
  67. local ($d, $ver, $sinfo, $phpver) = @_;
  68. local @rv;
  69.  
  70. if ($ver >= 3.6) {
  71.         # Check for PHP 5.3+
  72.         local $phpv = &get_php_version($phpver || 5, $d);
  73.         if (!$phpv) {
  74.                 push(@rv, "Could not work out exact PHP version");
  75.                 }
  76.         elsif ($phpv < 5.3) {
  77.                 push(@rv, "Dolibarr requires PHP version 5.3 or later");
  78.                 }
  79.         }
  80.  
  81. return @rv;
  82. }
  83.  
  84. # script_dolibarr_params(&domain, version, &upgrade-info)
  85. # Returns HTML for table rows for options for installing dolibarr
  86. sub script_dolibarr_params
  87. {
  88. local ($d, $ver, $upgrade) = @_;
  89. local $rv;
  90. local $hdir = &public_html_dir($d, 1);
  91. if ($upgrade) {
  92.         # Options are fixed when upgrading
  93.         local ($dbtype, $dbname) = split(/_/, $upgrade->{'opts'}->{'db'}, 2);
  94.         $rv .= &ui_table_row("Database for Dolibarr tables", $dbname);
  95.         local $dir = $upgrade->{'opts'}->{'dir'};
  96.         $dir =~ s/^$d->{'home'}\///;
  97.         $rv .= &ui_table_row("Install directory", $dir);
  98.         }
  99. else {
  100.         # Show editable install options
  101.         local @dbs = &domain_databases($d, [ "mysql"]);
  102.         $rv .= &ui_table_row("Database for Dolibarr tables",
  103.                      &ui_database_select("db", undef, \@dbs, $d, "dolibarr"));
  104.         $rv .= &ui_table_row("Install sub-directory under <tt>$hdir</tt>",
  105.                              &ui_opt_textbox("dir", &substitute_scriptname_template("dolibarr", $d), 30, "At top level"));
  106.         if ($d->{'ssl'} && $ver >= 3.0) {
  107.                 $rv .= &ui_table_row("Force https connection?",
  108.                                      &ui_yesno_radio("forcehttps", 0));
  109.                 }
  110.         }
  111. return $rv;
  112. }
  113.  
  114. # script_dolibarr_parse(&domain, version, &in, &upgrade-info)
  115. # Returns either a hash ref of parsed options, or an error string
  116. sub script_dolibarr_parse
  117. {
  118. local ($d, $ver, $in, $upgrade) = @_;
  119. if ($upgrade) {
  120.         # Options are always the same
  121.         return $upgrade->{'opts'};
  122.         }
  123. else {
  124.         local $hdir = &public_html_dir($d, 0);
  125.         $in{'dir_def'} || $in{'dir'} =~ /\S/ && $in{'dir'} !~ /\.\./ ||
  126.                 return "Missing or invalid installation directory";
  127.         local $dir = $in{'dir_def'} ? $hdir : "$hdir/$in{'dir'}";
  128.         local ($newdb) = ($in->{'db'} =~ s/^\*//);
  129.         return { 'db' => $in->{'db'},
  130.                  'newdb' => $newdb,
  131.                  'dir' => $dir,
  132.                  'path' => $in->{'dir_def'} ? "/" : "/$in->{'dir'}",
  133.                  'forcehttps' => $in->{'forcehttps'}, };
  134.         }
  135. }
  136.  
  137. # script_dolibarr_check(&domain, version, &opts, &upgrade-info)
  138. # Returns an error message if a required option is missing or invalid
  139. sub script_dolibarr_check
  140. {
  141. local ($d, $ver, $opts, $upgrade) = @_;
  142. $opts->{'dir'} =~ /^\// || return "Missing or invalid install directory";
  143. $opts->{'db'} || return "Missing database";
  144. if (-r "$opts->{'dir'}/conf/conf.php") {
  145.         return "Dolibarr appears to be already installed in the selected directory";
  146.         }
  147. local ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
  148. local $clash = &find_database_table($dbtype, $dbname, "llx_.*");
  149. $clash && return "Dolibarr appears to be already using the selected database (table $clash)";
  150. return undef;
  151. }
  152.  
  153. # script_dolibarr_files(&domain, version, &opts, &upgrade-info)
  154. # Returns a list of files needed by dolibarr, each of which is a hash ref
  155. # containing a name, filename and URL
  156. sub script_dolibarr_files
  157. {
  158. local ($d, $ver, $opts, $upgrade) = @_;
  159. local @files = ( { 'name' => "source",
  160.            'file' => "Dolibarr_$ver.tar.gz",
  161.            'url' => "http://prdownloads.sourceforge.net/dolibarr/dolibarr-$ver.tgz" } );
  162. return @files;
  163. }
  164.  
  165. sub script_dolibarr_commands
  166. {
  167. return ("tar", "gunzip");
  168. }
  169.  
  170. # script_dolibarr_install(&domain, version, &opts, &files, &upgrade-info)
  171. # Actually installs dolibarr, and returns either 1 and an informational
  172. # message, or 0 and an error
  173. sub script_dolibarr_install
  174. {
  175. local ($d, $version, $opts, $files, $upgrade, $domuser, $dompass) = @_;
  176.  
  177. local ($out, $ex);
  178. if ($opts->{'newdb'} && !$upgrade) {
  179.         local $err = &create_script_database($d, $opts->{'db'});
  180.         return (0, "Database creation failed : $err") if ($err);
  181.         }
  182. local ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
  183. local $dbuser = $dbtype eq "mysql" ? &mysql_user($d) : &postgres_user($d);
  184. local $dbpass = $dbtype eq "mysql" ? &mysql_pass($d) : &postgres_pass($d, 1);
  185. local $dbphptype = $dbtype eq "mysql" && $version < 3.6 ? "mysql" :
  186.                    $dbtype eq "mysql" ? "mysqli" : "pgsql";
  187. local $dbhost = &get_database_host($dbtype, $d);
  188. local $dberr = &check_script_db_connection($dbtype, $dbname, $dbuser, $dbpass);
  189. return (0, "Database connection failed : $dberr") if ($dberr);
  190.  
  191. # Extract tar file to temp dir and copy to target
  192. local $temp = &transname();
  193. local $err = &extract_script_archive($files->{'source'}, $temp, $d,
  194.                              $opts->{'dir'}, "dolibarr-$ver/htdocs");
  195. $err && return (0, "Failed to extract source : $err");
  196.  
  197. # Add config file
  198. local $cfiledir = "$opts->{'dir'}/conf/";
  199. local $docdir = "$opts->{'dir'}/documents";
  200. local $altdir = "$opts->{'dir'}/custom";
  201. local $cfile = $cfiledir."conf.php";
  202. local $oldcfile = &transname();
  203. local $olddocdir = &transname();
  204. local $oldaltdir = &transname();
  205. local $url;
  206.  
  207. $tmpl = &get_template($d->{'template'});
  208. $mycharset = $tmpl->{'mysql_charset'};
  209. $mycollate = $tmpl->{'mysql_collate'};
  210. $pgcharset = $tmpl->{'postgres_encoding'};
  211. $charset = $dbtype eq "mysql" ? $mycharset : $pgcharset;
  212. $collate = $dbtype eq "mysql" ? $mycollate : "C";
  213.  
  214. $path = &script_path_url($d, $opts);
  215. if ($path =~ /^https:/ || $d->{'ssl'}) {
  216.         $url = "https://$d->{'dom'}";
  217. }
  218. else {
  219.         $url = "http://$d->{'dom'}";
  220. }
  221. if ($opts->{'path'} =~ /\w/) {
  222.         $url .= $opts->{'path'};
  223. }
  224.  
  225. if (!$upgrade) {
  226.         local $cdef = "$opts->{'dir'}/conf/conf.php.example";
  227.         &copy_source_dest_as_domain_user($d, $cdef, $cfile);
  228.         &set_permissions_as_domain_user($d, 0777, $cfiledir);
  229.         &copy_source_dest_as_domain_user($d, $cfile);
  230.         &run_as_domain_user($d, "mkdir ".quotemeta($docdir));
  231.         &set_permissions_as_domain_user($d, 0777, $docdir);
  232. }
  233. else {
  234.         # Preserve old config file, documents and custom directory
  235.         &copy_source_dest($cfile, $oldcfile);
  236.         &copy_source_dest($docdir, $olddocdir);
  237.         &copy_source_dest($altdir, $oldaltdir);
  238. }
  239.  
  240. if ($upgrade) {
  241.         # Put back original config file, documents and custom directory
  242.         &copy_source_dest_as_domain_user($d, $oldcfile, $cfile);
  243.         &copy_source_dest_as_domain_user($d, $olddocdir, $docdir);
  244.         &copy_source_dest_as_domain_user($d, $oldaltdir, $altdir);
  245.        
  246.         # First page (Update database schema)
  247.         local @params = ( [ "action", "upgrade" ],
  248.                           [ "versionfrom", $upgrade->{'version'} ],
  249.                           [ "versionto", $ver ],
  250.                          );
  251.         local $err = &call_dolibarr_wizard_page(\@params, "upgrade", $d, $opts);
  252.         return (-1, "Dolibarr wizard failed : $err") if ($err);
  253.        
  254.         # Second page (Migrate some data)
  255.         local @params = ( [ "action", "upgrade" ],
  256.                           [ "versionfrom", $upgrade->{'version'} ],
  257.                           [ "versionto", $ver ],
  258.                          );
  259.         local $err = &call_dolibarr_wizard_page(\@params, "upgrade2", $d, $opts);
  260.         return (-1, "Dolibarr wizard failed : $err") if ($err);
  261.        
  262.         # Third page (Update version number)
  263.         local @params = ( [ "action", "upgrade" ],
  264.                           [ "versionfrom", $upgrade->{'version'} ],
  265.                           [ "versionto", $ver ],
  266.                           [ "installlock", "444" ],
  267.                          );
  268.         local $p = $ver >= 3.8 ? "step5" : "etape5";
  269.         local $err = &call_dolibarr_wizard_page(\@params, $p, $d, $opts);
  270.         return (-1, "Dolibarr wizard failed : $err") if ($err);
  271.        
  272.         # Remove the installation directory. (deprecated)
  273.         # local $dinstall = "$opts->{'dir'}/install";
  274.         # $dinstall  =~ s/\/$//;
  275.         # $out = &run_as_domain_user($d, "rm -rf ".quotemeta($dinstall));
  276.        
  277.         }
  278. else {
  279.         # First page (Db connection and config file creation)
  280.         local @params = ( [ "main_dir", $opts->{'dir'} ],
  281.                           [ "main_data_dir", $opts->{'dir'}."/documents" ],
  282.                           [ "main_url", $url ],
  283.                           [ "db_type", $dbphptype ],
  284.                           [ "db_host", $dbhost ],
  285.                           [ "db_name", $dbname ],
  286.                           [ "db_user", $dbuser ],
  287.                           [ "db_pass", $dbpass ],
  288.                           [ "action", "set" ],
  289.                           [ "main_force_https", $opts->{'forcehttps'} ],
  290.                           [ "dolibarr_main_db_character_set", $charset ],
  291.                           [ "dolibarr_main_db_collation", $collate ],
  292.                           [ "usealternaterootdir", "1" ],
  293.                           [ "main_alt_dir_name", "custom" ],
  294.                          );
  295.         local $p = $ver >= 3.8 ? "step1" : "etape1";
  296.         local $err = &call_dolibarr_wizard_page(\@params, $p, $d, $opts);
  297.         return (-1, "Dolibarr wizard failed : $err") if ($err);
  298.        
  299.         # Second page (Populate database)
  300.         local @params = ( [ "action", "set" ] );
  301.         local $p = $ver >= 3.8 ? "step2" : "etape2";
  302.         local $err = &call_dolibarr_wizard_page(\@params, $p, $d, $opts);
  303.         return (-1, "Dolibarr wizard failed : $err") if ($err);
  304.        
  305.         # Third page (Add administrator account)
  306.         local @params = ( [ "action", "set" ],
  307.                           [ "login", "admin" ],
  308.                           [ "pass", $dompass ],
  309.                           [ "pass_verif", $dompass ],
  310.                           [ "installlock", "444" ],
  311.                          );
  312.         local $p = $ver >= 3.8 ? "step5" : "etape5";
  313.         local $err = &call_dolibarr_wizard_page(\@params, $p, $d, $opts);
  314.         return (-1, "Dolibarr wizard failed : $err") if ($err);
  315.        
  316.         # Remove the installation directory (deprecated)
  317.         # local $dinstall = "$opts->{'dir'}/install";
  318.         # $dinstall  =~ s/\/$//;
  319.         # $out = &run_as_domain_user($d, "rm -rf ".quotemeta($dinstall));
  320.        
  321.         # Protect config file
  322.         &set_permissions_as_domain_user($d, 0644, $cfile);
  323.         &set_permissions_as_domain_user($d, 0755, $cfiledir);
  324.         }
  325.  
  326. # Return a URL for the user
  327. local $rp = $opts->{'dir'};
  328. $rp =~ s/^$d->{'home'}\///;
  329. local $adminurl = $url;
  330. return (1, "Dolibarr installation complete. Go to <a target=_new href='$url'>$url</a> to use it.", "Under $rp using $dbtype database $dbname", $url, 'admin', $dompass);
  331. }
  332.  
  333. # call_dolibarr_wizard_page(&parameters, step-no, &domain, &opts)
  334. sub call_dolibarr_wizard_page
  335. {
  336. local ($params, $page, $d, $opts) = @_;
  337. local $params = join("&", map { $_->[0]."=".&urlize($_->[1]) } @$params );
  338. local $ipage = $opts->{'path'}."/install/".$page.".php";
  339. local ($iout, $ierror);
  340. &post_http_connection($d, $ipage, $params, \$iout, \$ierror);
  341. if ($ierror) {
  342.         return $ierror;
  343.         }
  344. return undef;
  345. }
  346.  
  347. # script_dolibarr_uninstall(&domain, version, &opts)
  348. # Un-installs a dolibarr installation, by deleting the directory.
  349. # Returns 1 on success and a message, or 0 on failure and an error
  350. sub script_dolibarr_uninstall
  351. {
  352. local ($d, $version, $opts) = @_;
  353.  
  354. # Remove the contents of the target directory
  355. local $derr = &delete_script_install_directory($d, $opts);
  356. return (0, $derr) if ($derr);
  357.  
  358. # Remove all llx_ tables from the database
  359. # 10 times because of constraints
  360. for(my $i=0; $i<10; $i++) {
  361.         &cleanup_script_database($d, $opts->{'db'}, "llx_");
  362.         }
  363.  
  364. # Take out the DB
  365. if ($opts->{'newdb'}) {
  366.         &delete_script_database($d, $opts->{'db'});
  367.         }
  368.  
  369. return (1, "Dolibarr directory and tables deleted.");
  370. }
  371.  
  372. # script_dolibarr_realversion(&domain, &opts)
  373. # Returns the real version number of some script install, or undef if unknown
  374. sub script_dolibarr_realversion
  375. {
  376. local ($d, $opts, $sinfo) = @_;
  377. local $lref = &read_file_lines("$opts->{'dir'}/filefunc.inc.php", 1);
  378. foreach my $l (@$lref) {
  379.                 if ($l =~ /'DOL_VERSION','([0-9a-z\.\-]+)'/) {
  380.                 return $1;
  381.                 }
  382.         }
  383. return undef;
  384. }
  385.  
  386. # script_dolibarr_check_latest(version)
  387. # Checks if some version is the latest for this project, and if not returns
  388. # a newer one. Otherwise returns undef.
  389. sub script_dolibarr_check_latest
  390. {
  391. local ($ver) = @_;
  392. local @vers = &osdn_package_versions("dolibarr",
  393.                                 $ver >= 10.0 ? "dolibarr\\-(10\\.0\\.[0-9\\.]+)\\.tgz" :
  394.                                 $ver >= 9.0 ? "dolibarr\\-(9\\.0\\.[0-9\\.]+)\\.tgz" :
  395.                                 $ver >= 8.0 ? "dolibarr\\-(8\\.0\\.[0-9\\.]+)\\.tgz" :
  396.                                 $ver >= 7.0 ? "dolibarr\\-(7\\.0\\.[0-9\\.]+)\\.tgz" :
  397.                                 $ver >= 6.0 ? "dolibarr\\-(6\\.0\\.[0-9\\.]+)\\.tgz" :
  398.                                 $ver >= 5.0 ? "dolibarr\\-(5\\.0\\.[0-9\\.]+)\\.tgz" :
  399.                                 $ver >= 4.0 ? "dolibarr\\-(4\\.0\\.[0-9\\.]+)\\.tgz" :
  400.                                 $ver >= 3.9 ? "dolibarr\\-(3\\.9\\.[0-9\\.]+)\\.tgz" :
  401.                                 $ver >= 3.8 ? "dolibarr\\-(3\\.8\\.[0-9\\.]+)\\.tgz" :
  402.                                 $ver >= 3.7 ? "dolibarr\\-(3\\.7\\.[0-9\\.]+)\\.tgz" :
  403.                                 $ver >= 3.6 ? "dolibarr\\-(3\\.6\\.[0-9\\.]+)\\.tgz" :
  404.                                 $ver >= 3.5 ? "dolibarr\\-(3\\.5\\.[0-9\\.]+)\\.tgz" :
  405.                                 $ver >= 2.9 ? "dolibarr\\-(2\\.9\\.[0-9\\.]+)\\.tgz" :
  406.                               "dolibarr\\-(2\\.8\\.[0-9\\.]+)\\.tgz");
  407. return "Failed to find versions" if (!@vers);
  408. return $ver eq $vers[0] ? undef : $vers[0];
  409. }
  410.  
  411. sub script_dolibarr_site
  412. {
  413. return 'http://www.dolibarr.org/';
  414. }
  415.  
  416. sub script_dolibarr_passmode
  417. {
  418. return 2;
  419. }
  420.  
  421. 1;

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.