Advertisement
Guest User

Untitled

a guest
Jan 8th, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 10.92 KB | None | 0 0
  1.  
  2. our (%in, %config);
  3.  
  4. # script_wordpress_desc()
  5. sub script_wordpress_desc
  6. {
  7. return "WordPress";
  8. }
  9.  
  10. sub script_wordpress_uses
  11. {
  12. return ( "php" );
  13. }
  14.  
  15. sub script_wordpress_longdesc
  16. {
  17. return "A semantic personal publishing platform with a focus on aesthetics, web standards, and usability.";
  18. }
  19.  
  20. # script_wordpress_versions()
  21. sub script_wordpress_versions
  22. {
  23. return ( "4.9.1" );
  24. }
  25.  
  26. sub script_wordpress_category
  27. {
  28. return ("Blog", "CMS");
  29. }
  30.  
  31. sub script_wordpress_php_vers
  32. {
  33. my ($d, $ver) = @_;
  34. if ($ver >= 3.2) {
  35.     return ( 5 );
  36.     }
  37. else {
  38.     return ( 4, 5 );
  39.     }
  40. }
  41.  
  42. sub script_wordpress_php_modules
  43. {
  44. return ("mysql", "gd");
  45. }
  46.  
  47. sub script_wordpress_php_optional_modules
  48. {
  49. return ("curl");
  50. }
  51.  
  52. sub script_wordpress_dbs
  53. {
  54. return ("mysql");
  55. }
  56.  
  57. sub script_wordpress_release
  58. {
  59. return 4;   # For wp-cli support
  60. }
  61.  
  62. # script_wordpress_depends(&domain, version)
  63. sub script_wordpress_depends
  64. {
  65. my ($d, $ver, $sinfo, $phpver) = @_;
  66. my @rv;
  67.  
  68. # Check for MySQL 4+
  69. require_mysql();
  70. if (mysql::get_mysql_version() < 4) {
  71.     push(@rv, "WordPress requires MySQL version 4 or higher");
  72.     }
  73.  
  74. # Check for PHP 5.2+
  75. my $phpv = get_php_version($phpver || 5, $d);
  76. if (!$phpv) {
  77.     push(@rv, "Could not work out exact PHP version");
  78.     }
  79. elsif ($phpv < 5.2) {
  80.     push(@rv, "Wordpress requires PHP version 5.2 or later");
  81.     }
  82.  
  83. return @rv;
  84. }
  85.  
  86. # script_wordpress_params(&domain, version, &upgrade-info)
  87. # Returns HTML for table rows for options for installing Wordpress
  88. sub script_wordpress_params
  89. {
  90. my ($d, $ver, $upgrade) = @_;
  91. my $rv;
  92. my $hdir = public_html_dir($d, 1);
  93. if ($upgrade) {
  94.     # Options are fixed when upgrading
  95.     my ($dbtype, $dbname) = split(/_/, $upgrade->{'opts'}->{'db'}, 2);
  96.     $rv .= ui_table_row("Database for WordPress tables", $dbname);
  97.     my $prefix = $upgrade->{'opts'}->{'prefix'};
  98.     my $dir = $upgrade->{'opts'}->{'dir'};
  99.     $dir =~ s/^$d->{'home'}\///;
  100.     $rv .= ui_table_row("Install directory", $dir);
  101.     }
  102. else {
  103.     # Show editable install options
  104.     my @dbs = domain_databases($d, [ "mysql" ]);
  105.     $rv .= ui_table_row("Database for WordPress tables",
  106.              ui_database_select("db", undef, \@dbs, $d, "wordpress"));
  107.     $rv .= ui_table_row("Table Prefix",
  108.                ui_opt_textbox("prefix", &substitute_scriptname_template("wp_", $d), 30, "Default (wp_)"));
  109.     $rv .= ui_table_row("Install sub-directory under <tt>$hdir</tt>",
  110.                ui_opt_textbox("dir", &substitute_scriptname_template("wordpress", $d), 30, "At top level"));
  111.     if (&has_wordpress_cli()) {
  112.         # Can select the blog title
  113.         $rv .= ui_table_row("WordPress Blog title",
  114.             ui_textbox("title", $d->{'owner'}, 40));
  115.         }
  116.     }
  117. return $rv;
  118. }
  119.  
  120. # script_wordpress_parse(&domain, version, &in, &upgrade-info)
  121. # Returns either a hash ref of parsed options, or an error string
  122. sub script_wordpress_parse
  123. {
  124. my ($d, $ver, $in, $upgrade) = @_;
  125. if ($upgrade) {
  126.     # Options are always the same
  127.     return $upgrade->{'opts'};
  128.     }
  129. else {
  130.     my $hdir = public_html_dir($d, 0);
  131.     $in{'dir_def'} || $in{'dir'} =~ /\S/ && $in{'dir'} !~ /\.\./ ||
  132.         return "Missing or invalid installation directory";
  133.     my $dir = $in{'dir_def'} ? $hdir : "$hdir/$in{'dir'}";
  134.     my ($newdb) = ($in->{'db'} =~ s/^\*//);
  135.     return { 'db' => $in->{'db'},
  136.          'newdb' => $newdb,
  137.          'dir' => $dir,
  138.          'path' => $in{'dir_def'} ? "/" : "/$in{'dir'}",
  139.          'title' => $in{'title'} };
  140.     }
  141. }
  142.  
  143. # script_wordpress_check(&domain, version, &opts, &upgrade-info)
  144. # Returns an error message if a required option is missing or invalid
  145. sub script_wordpress_check
  146. {
  147. local ($d, $ver, $opts, $upgrade) = @_;
  148. $opts->{'dir'} =~ /^\// || return "Missing or invalid install directory";
  149. $opts->{'db'} || return "Missing database";
  150. if (-r "$opts->{'dir'}/wp-login.php") {
  151.     return "WordPress appears to be already installed in the selected directory";
  152.     }
  153. my ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
  154. my $clash = find_database_table($dbtype, $dbname, "wp_.*");
  155. $clash && return "WordPress appears to be already using the selected database (table $clash)";
  156. return undef;
  157. }
  158.  
  159. # script_wordpress_files(&domain, version, &opts, &upgrade-info)
  160. # Returns a list of files needed by Wordpress, each of which is a hash ref
  161. # containing a name, filename and URL
  162. sub script_wordpress_files
  163. {
  164. my ($d, $ver, $opts, $upgrade) = @_;
  165. if ($d && &has_wordpress_cli()) {
  166.     # Nothing to download
  167.     return ( );
  168.     }
  169. my @files = ( { 'name' => "source",
  170.        'file' => "wordpress-$ver.zip",
  171.        'url' => "http://wordpress.org/latest.zip",
  172.        'virtualmin' => 1,
  173.        'nocache' => 1 } );
  174. return @files;
  175. }
  176.  
  177. sub script_wordpress_commands
  178. {
  179. return ("unzip");
  180. }
  181.  
  182. # script_wordpress_install(&domain, version, &opts, &files, &upgrade-info)
  183. # Actually installs WordPress, and returns either 1 and an informational
  184. # message, or 0 and an error
  185. sub script_wordpress_install
  186. {
  187. local ($d, $version, $opts, $files, $upgrade, $domuser, $dompass) = @_;
  188. my ($out, $ex);
  189. if ($opts->{'newdb'} && !$upgrade) {
  190.         my $err = create_script_database($d, $opts->{'db'});
  191.         return (0, "Database creation failed : $err") if ($err);
  192.         }
  193. my ($dbtype, $dbname) = split(/_/, $opts->{'db'}, 2);
  194. my $dbuser = $dbtype eq "mysql" ? mysql_user($d) : postgres_user($d);
  195. my $dbpass = $dbtype eq "mysql" ? mysql_pass($d) : postgres_pass($d, 1);
  196. my $dbphptype = $dbtype eq "mysql" ? "mysql" : "psql";
  197. my $dbhost = get_database_host($dbtype, $d);
  198. my $dberr = check_script_db_connection($dbtype, $dbname, $dbuser, $dbpass);
  199. return (0, "Database connection failed : $dberr") if ($dberr);
  200.  
  201. if (&has_wordpress_cli()) {
  202.     if (!$upgrade) {
  203.         # Execute the download command
  204.         &make_dir_as_domain_user($d, $opts->{'dir'}, 0755);
  205.         my $wp = "cd ".quotemeta($opts->{'dir'})." && ".&has_command("wp");
  206.         my $out = &run_as_domain_user($d, "$wp core download --version=$version 2>&1");
  207.         if ($?) {
  208.             return (-1, "wp core download failed : $out");
  209.             }
  210.  
  211.         # Configure the database
  212.         my $out = &run_as_domain_user($d,
  213.             "$wp config create --dbname=".quotemeta($dbname).
  214.             " --dbuser=".quotemeta($dbuser)." --dbpass=".quotemeta($dbpass).
  215.             " --dbhost=".quotemeta($dbhost)." 2>&1");
  216.         if ($?) {
  217.             return (-1, "wp config create failed : $out");
  218.             }
  219.  
  220.         # Do the install
  221.         my $out = &run_as_domain_user($d,
  222.             "$wp core install --url=$d->{'dom'}$opts->{'path'}".
  223.             " --title=".quotemeta($opts->{'title'} || $d->{'owner'}).
  224.             " --admin_user=".quotemeta($domuser).
  225.             " --admin_password=".quotemeta($dompass).
  226.             " --admin_email=".quotemeta($d->{'emailto'})." 2>&1");
  227.         if ($?) {
  228.             return (-1, "wp core install failed : $out");
  229.             }
  230.         }
  231.     else {
  232.         # Do the upgrade
  233.         my $out = &run_as_domain_user($d,
  234.                         "$wp core upgrade --version=$version");
  235.         if ($?) {
  236.             return (-1, "wp core upgrade failed : $out");
  237.             }
  238.         }
  239.     }
  240. else {
  241.     # Extract tar file to temp dir and copy to target
  242.     my $verdir = "wordpress";
  243.     my $temp = transname();
  244.     my $err = extract_script_archive($files->{'source'}, $temp, $d,
  245.                          $opts->{'dir'}, $verdir);
  246.     $err && return (0, "Failed to extract source : $err");
  247.     my $cfileorig = "$opts->{'dir'}/wp-config-sample.php";
  248.     my $cfile = "$opts->{'dir'}/wp-config.php";
  249.  
  250.     # Create the 'wordpress' virtuser, if missing
  251.     if ($config{'mail'} && $d->{'mail'}) {
  252.         my ($wpvirt) = grep { $_->{'from'} eq 'wordpress@'.$d->{'dom'} }
  253.                        list_virtusers();
  254.         if (!$wpvirt) {
  255.             $wpvirt = { 'from' => 'wordpress@'.$d->{'dom'},
  256.                     'to' => [ $d->{'emailto_addr'} ] };
  257.             create_virtuser($wpvirt);
  258.             }
  259.         }
  260.  
  261.     # Copy and update the config file
  262.     if (!-r $cfile) {
  263.         run_as_domain_user($d, "cp ".quotemeta($cfileorig)." ".
  264.                           quotemeta($cfile));
  265.         my $prefix = $opts->{'prefix'};
  266.         my $lref = read_file_lines_as_domain_user($d, $cfile);
  267.         foreach my $l (@$lref) {
  268.             if ($l =~ /^define\('DB_NAME',/) {
  269.                 $l = "define('DB_NAME', '$dbname');";
  270.                 }
  271.             if ($l =~ /^define\('DB_USER',/) {
  272.                 $l = "define('DB_USER', '$dbuser');";
  273.                 }
  274.             if ($l =~ /^define\('DB_HOST',/) {
  275.                 $l = "define('DB_HOST', '$dbhost');";
  276.                 }
  277.             if ($l =~ /^define\('DB_PASSWORD',/) {
  278.                 $l = "define('DB_PASSWORD', '".
  279.                      php_quotemeta($dbpass, 1)."');";
  280.                 }
  281.             if ($l =~ /define\('(AUTH_KEY|SECURE_AUTH_KEY|LOGGED_IN_KEY|NONCE_KEY|AUTH_SALT|SECURE_AUTH_SALT|LOGGED_IN_SALT|NONCE_SALT)'/) {
  282.                 my $salt = random_password(64);
  283.                 $l = "define('$1', '$salt');";
  284.                 }
  285.             if ($l =~ /^define\('WP_AUTO_UPDATE_CORE',/) {
  286.                 $l = "define('WP_AUTO_UPDATE_CORE', false);";
  287.                 }
  288.             if ($l =~ /^\$table_prefix  = /) {
  289.                 $l = "\$table_prefix  = '$prefix';";
  290.                 }
  291.         }
  292.         flush_file_lines_as_domain_user($d, $cfile);
  293.         }
  294.     }
  295.  
  296. # Make content directory writable, for uploads
  297. make_file_php_writable($d, "$opts->{'dir'}/wp-content", 0);
  298.  
  299. if (&has_wordpress_cli()) {
  300.     # Install is all done, return the base URL
  301.     my $url = script_path_url($d, $opts);
  302.     my $rp = $opts->{'dir'};
  303.     $rp =~ s/^$d->{'home'}\///;
  304.     return (1, "WordPress installation complete. It can be accessed at <a target=_blank href='$url'>$url</a>.", "Under $rp using $dbphptype database $dbname", $url, $domuser, $dompass);
  305.     }
  306. else {
  307.     # Return a URL to complete the install
  308.     my $url = script_path_url($d, $opts).
  309.              ($upgrade ? "wp-admin/upgrade.php" : "wp-admin/install.php");
  310.     my $userurl = script_path_url($d, $opts);
  311.     my $rp = $opts->{'dir'};
  312.     $rp =~ s/^$d->{'home'}\///;
  313.     return (1, "WordPress initial installation complete. It can be completed at <a target=_blank href='$url'>$url</a>.", "Under $rp using $dbphptype database $dbname", $userurl);
  314.     }
  315. }
  316.  
  317. # script_wordpress_uninstall(&domain, version, &opts)
  318. # Un-installs a Wordpress installation, by deleting the directory and database.
  319. # Returns 1 on success and a message, or 0 on failure and an error
  320. sub script_wordpress_uninstall
  321. {
  322. my ($d, $version, $opts) = @_;
  323.  
  324. # Remove the contents of the target directory
  325. my $derr = delete_script_install_directory($d, $opts);
  326. return (0, $derr) if ($derr);
  327.  
  328. # Remove all wp_ tables from the database
  329. cleanup_script_database($d, $opts->{'db'}, "wp_");
  330.  
  331. # Take out the DB
  332. if ($opts->{'newdb'}) {
  333.         delete_script_database($d, $opts->{'db'});
  334.         }
  335.  
  336. return (1, "WordPress directory and tables deleted.");
  337. }
  338.  
  339. # script_wordpress_realversion(&domain, &opts)
  340. # Returns the real version number of some script install, or undef if unknown
  341. sub script_wordpress_realversion
  342. {
  343. my ($d, $opts, $sinfo) = @_;
  344. my $lref = read_file_lines("$opts->{'dir'}/wp-includes/version.php", 1);
  345. foreach my $l (@$lref) {
  346.     if ($l =~ /wp_version\s*=\s*'([0-9\.]+)'/) {
  347.         return $1;
  348.         }
  349.     }
  350. return undef;
  351. }
  352.  
  353. # script_wordpress_latest(version)
  354. # Returns a URL and regular expression or callback func to get the version
  355. sub script_wordpress_latest
  356. {
  357. my ($ver) = @_;
  358. return ( "http://wordpress.org/download/",
  359.      "Version\\s+([0-9\\.]+)" );
  360. }
  361.  
  362. sub script_wordpress_site
  363. {
  364. return 'http://wordpress.org/';
  365. }
  366.  
  367. sub script_wordpress_gpl
  368. {
  369. return 1;
  370. }
  371.  
  372. sub script_wordpress_passmode
  373. {
  374. return &has_wordpress_cli() ? 1 : 0;
  375. }
  376.  
  377. sub has_wordpress_cli
  378. {
  379. return &has_command("wp");
  380. }
  381.  
  382. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement