Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,7 @@ paket-files/
wordpress/wp-content/plugins/akismet
wordpress/wp-content/plugins/hello.php
wordpress/wp-content/themes/twentysixteen
wordpress/wp-content/uploads/
wordpress/wp-content/uploads/

# Sqlite Database
app/Database/*
5 changes: 5 additions & 0 deletions PeachPied.WordPress.AspNetCore/RequestDelegateExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ private static IApplicationBuilder InstallWordPress(this IApplicationBuilder app
WpStandard.DB_PASSWORD = options.DbPassword;
WpStandard.DB_USER = options.DbUser;

// SQLITE
WpStandard.USE_MYSQL = options.UseMySQL;
WpStandard.DB_DIR = options.SQLiteFolder;
WpStandard.DB_FILE = options.SQLiteFileName;

//
WpStandard.WP_DEBUG = options.Debug || (app.ApplicationServices.TryGetService<IWebHostEnvironment>(out var env) && env.IsDevelopment());

Expand Down
18 changes: 18 additions & 0 deletions PeachPied.WordPress.AspNetCore/WordPressConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ public class WordPressConfig
/// <summary>MySQL host.</summary>
public string DbHost { get; set; } = "localhost";


/// <summary>Use MySQL, disabled use SQLite</summary>
public bool UseMySQL { get; set; } = true;
/// <summary>SQLite Database Folder</summary>
public string SQLiteFolder { get; set; } = "./Database/";
/// <summary>SQLite Database Filename</summary>
public string SQLiteFileName { get; set; } = "database.sqlite";

/// <summary>Autoinstall Wordpress if database dont exists - SQLite only</summary>
public bool WPInstall { get; set; } = true;
/// <summary>Autoinstall Wordpress Weblog Title</summary>
public string WPInstallWeblogTitle { get; set; } = "Wordpress";
/// <summary>Autoinstall Wordpress admin Username</summary>
public string WPInstallUserName { get; set; } = "admin";
/// <summary>Autoinstall Wordpress admin Password</summary>
public string WPInstallAdminPassword { get; set; } = "admin";
/// <summary>Autoinstall Wordpress admin eMail</summary>
public string WPInstallAdminEmail { get; set; } = "[email protected]";
//

/// <summary>WordPress Database Table prefix.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ void DashboardRightNow(WpApp app, TextWriter output)
var frameworkVersion =
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription ??
Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
string databaseSystem = "MySQL";
if (!WpStandard.USE_MYSQL)
{
databaseSystem = "SQLite (Path: " + WpStandard.DB_DIR + WpStandard.DB_FILE + ")";
}
var process = System.Diagnostics.Process.GetCurrentProcess();

string licenseRow = "";
Expand All @@ -268,7 +273,8 @@ void DashboardRightNow(WpApp app, TextWriter output)
<tr>
<td><img src=""{LogoUrl}"" style=""width:76px;margin:8px;display:inline;""></td>
<td>
<b>Your WordPress runs on {frameworkVersion}</b>
<b>Your WordPress runs on {frameworkVersion}</b><br/>
<b title=""Database System"">Database:</b> {databaseSystem}
<div>
<b title=""Memory allocated by the whole process."">Memory usage:</b> {process.WorkingSet64 / 1024 / 1024} MB<br/>
<b title=""CPU time consumed by the whole process."">CPU usage:</b> {process.TotalProcessorTime:c}<br/>
Expand Down
9 changes: 9 additions & 0 deletions PeachPied.WordPress.Standard/WpStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public static class WpStandard
/// <summary>MySQL hostname</summary>
public static string DB_HOST { get; set; } = "localhost";

/// <summary>Use MySQL, disabled use SQLite</summary>
public static bool USE_MYSQL { get; set; } = true;

/// <summary>SQLite Database Folder</summary>
public static string DB_DIR { get; set; } = "./Database/";

/// <summary>SQLite Database Filename</summary>
public static string DB_FILE { get; set; } = "database.sqlite";

/// <summary>Database Charset to use in creating database tables</summary>
public const string DB_CHARSET = "utf8";

Expand Down
96 changes: 96 additions & 0 deletions app/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
* Demo application with WordPress.
*/

using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;

namespace peachserver
Expand All @@ -27,6 +35,66 @@ static void Main(string[] args)

class Startup
{
public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

// Autoinstall Wordpress Database on first init with Username: admin, Password: admin
// Íf WPInstall is set to false or UseMySQL is true this is getting ignored open /wp-admin/install.php and install manually
public class InitWordpressDatabase : BackgroundService
{
private readonly IServiceProvider _services;
private readonly IHostApplicationLifetime _lifetime;
private readonly IConfiguration _configuration;

public InitWordpressDatabase(IServiceProvider services, IHostApplicationLifetime lifetime, IConfiguration configuration)
{
_services = services;
_lifetime = lifetime;
_configuration=configuration;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var multiForm = new MultipartFormDataContent();
multiForm.Add(new StringContent(_configuration.GetValue<string>("WordPress:WPInstallWeblogTitle")), "weblog_title");
multiForm.Add(new StringContent(_configuration.GetValue<string>("WordPress:WPInstallUserName")), "user_name");
multiForm.Add(new StringContent(_configuration.GetValue<string>("WordPress:WPInstallAdminPassword")), "admin_password");
multiForm.Add(new StringContent(_configuration.GetValue<string>("WordPress:WPInstallAdminPassword")), "admin_password2");
multiForm.Add(new StringContent(_configuration.GetValue<string>("WordPress:WPInstallAdminEmail")), "admin_email");
multiForm.Add(new StringContent("Install+WordPress"), "Submit");

int port = 80;
Uri uri = new Uri("http://127.0.0.1/");
string url = uri.ToString();

foreach (var address in _services.GetService<IServer>().Features.Get<IServerAddressesFeature>().Addresses)
{
uri = new Uri(address.Replace("*", "127.0.0.1"));
port = uri.Port;
}

if (port != 80)
{
url = $"{uri.ToString()}";
}
var response = await new HttpClient().PostAsync(url + "wp-admin/install.php?step=2", multiForm);
Console.WriteLine("--------------------------------------------------------------------");
Console.WriteLine("\tWordpress Database created");
Console.WriteLine("");
Console.WriteLine("\tWordpress Weblog Title:" + _configuration.GetValue<string>("WordPress:WPInstallWeblogTitle"));
Console.WriteLine("\tWordpress Admin User Name:" + _configuration.GetValue<string>("WordPress:WPInstallUserName"));
Console.WriteLine("\tWordpress Admin Password:" + _configuration.GetValue<string>("WordPress:WPInstallAdminPassword"));
Console.WriteLine("\tWordpress Admin eMail:" + _configuration.GetValue<string>("WordPress:WPInstallAdminEmail"));
Console.WriteLine("");
Console.WriteLine("\tPlease open: " + url);
Console.WriteLine("--------------------------------------------------------------------");
}
}

public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression();
Expand All @@ -35,6 +103,34 @@ public void ConfigureServices(IServiceCollection services)
{
//
});

// Check if we want to use SQLite, if so check if database folders exists and database is not empty, trigger install if set in config
bool needDatabaseInit = false;
if (!Configuration.GetValue<bool>("WordPress:UseMySQL") && Configuration.GetValue<bool>("WordPress:WPInstall"))
{
if (!Directory.Exists(Configuration.GetValue<string>("WordPress:SQLiteFolder")))
{
Directory.CreateDirectory(Configuration.GetValue<string>("WordPress:SQLiteFolder"));
}

if (File.Exists(Configuration.GetValue<string>("WordPress:SQLiteFolder") + Configuration.GetValue<string>("WordPress:SQLiteFileName")))
{
FileInfo fi = new FileInfo(Configuration.GetValue<string>("WordPress:SQLiteFolder") + Configuration.GetValue<string>("WordPress:SQLiteFileName"));
if (fi.Length < 100000)
{
needDatabaseInit = true;
}
} else
{
needDatabaseInit = true;
}

if (needDatabaseInit)
{
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddHostedService<InitWordpressDatabase>();
}
}
}

public void Configure(IApplicationBuilder app, IHostEnvironment env, IConfiguration configuration)
Expand Down
8 changes: 8 additions & 0 deletions app/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"dbpassword": "password",
"dbuser": "root",
"dbname": "wordpress",
"UseMySQL": true,
"SQLiteFolder": "./Database/",
"SQLiteFileName": "database.sqlite",
"WPInstall": true,
"WPInstallWeblogTitle": "Wordpress",
"WPInstallUserName": "admin",
"WPInstallAdminPassword": "admin",
"WPInstallAdminEmail": "[email protected]",
"LegacyPluginAssemblies": [],
"SALT": {
"AUTH_KEY": "*Sr748b66z3R+(v%1z;|SCtBZz/cEvo1)mo|F&EO>5a^1aF6@C9^KIzG&MD?=Zmt",
Expand Down
1 change: 1 addition & 0 deletions wordpress/PeachPied.WordPress.msbuildproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
<!--PackageReference Include="Peachpie.RequestHandler" Version="$(PeachpieVersion)" /-->
<ProjectReference Include="..\PeachPied.WordPress.Standard\PeachPied.WordPress.Standard.csproj" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Peachpie.Library.PDO.Sqlite" Version="$(PeachpieVersion)" />
</ItemGroup>
</Project>
Loading