From 3d728542fbdec6ef2ddc3c30efddf3152aef2310 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 26 Jan 2017 20:59:04 +0000
Subject: [PATCH] More user preferences stuff for #128

---
 build/index.php                      | 42 +++++++++++++++++++++++++++-
 core.php                             |  2 +-
 module_index.json                    |  4 +--
 modules/feature-user-preferences.php | 36 ++++++++++++++++++++++++
 4 files changed, 80 insertions(+), 4 deletions(-)

diff --git a/build/index.php b/build/index.php
index ff27089..d964768 100644
--- a/build/index.php
+++ b/build/index.php
@@ -884,7 +884,7 @@ function render_editor($editorName)
 
 /**
  * Saves the currently logged in uesr's data back to peppermint.json.
- * @return bool Whethert he user's data was saved successfully. Returns false if the user isn't logged in.
+ * @return bool Whether the user's data was saved successfully. Returns false if the user isn't logged in.
  */
 function save_userdata()
 {
@@ -3129,6 +3129,8 @@ class search
 		// Loop over each nterm and find it in the source
 		foreach($nterms as $nterm)
 		{
+			if(in_array($nterm, static::$stop_words))
+				continue;
 			$all_offsets = mb_stripos_all($source, $nterm);
 			// Skip over adding matches if there aren't any
 			if($all_offsets === false)
@@ -3208,6 +3210,8 @@ class search
 		
 		foreach($qterms as $qterm)
 		{
+			if(in_array($qterm, static::$stop_words))
+				continue;
 			// From http://stackoverflow.com/a/2483859/1460422
 			$context = preg_replace("/" . str_replace("/", "\/", preg_quote($qterm)) . "/i", "<strong class='search-term-highlight'>$0</strong>", $context);
 		}
@@ -3851,6 +3855,42 @@ register_module([
 			exit(page_renderer::render_main("User Preferences - $settings->sitename", $content));
 		});
 		
+		add_action("save-preferences", function() {
+			global $env, $settings;
+			
+			if(!$env->is_logged_in)
+			{
+				http_response_code(400);
+				exit(page_renderer::render_main("Error Saving Preferences - $settings->sitename", "<p>You aren't logged in, so you can't save your preferences. Try <a href='?action=login&returnto=" . rawurlencode("?action=user-preferences") . "'>logging in</a> first.</p>"));
+			}
+			
+			if(isset($_POST["email-address"]))
+			{
+				if(mb_strlen($_POST["email-address"]) > 320)
+				{
+					http_response_code(413);
+					exit(page_renderer::render_main("Error Saving Email Address - $settings->sitename", "<p>The email address you supplied (<code>{$_POST['email-address']}</code>) is too long. Email addresses can only be 320 characters long. <a href='javascript:window.history.back();'>Go back</a>."));
+				}
+				
+				if(mb_strpos($_POST["email-address"], "@") === false)
+				{
+					http_response_code(422);
+					exit(page_renderer::render_main("Error Saving Email Address - $settings->sitename", "<p>The email address you supplied (<code>{$_POST['email-address']}</code>) doesn't appear to be valid. <a href='javascript:window.history.back();'>Go back</a>."));
+				}
+				
+				$env->user_data->emailAddress = $_POST["email-address"];
+			}
+			
+			if(!save_userdata())
+			{
+				http_response_code(503);
+				exit(page_renderer::render_main("Error Saving Preferences - $settings->sitename", "<p>$settings->sitename had some trouble saving your preferences! Please contact $settings->admindetails_name, $settings->sitename's administrator and tell them about this error if it still occurs in 5 minutes. They can be contacted by email at this address: <a href='mailto:" . hide_email($settings->admindetails_email) . "'>" . hide_email($settings->admindetails_email) . "</a>.</p>"));
+			}
+			
+			
+			exit(page_renderer::render_main("Preferences Saved Successfully - $settings->sitename", "<p></p>"));
+		});
+		
 		/**
 		 * @api	{post}	?action=change-password	Change your password
 		 * @apiName			ChangePassword
diff --git a/core.php b/core.php
index cf8043d..0fcf18e 100644
--- a/core.php
+++ b/core.php
@@ -566,7 +566,7 @@ function render_editor($editorName)
 
 /**
  * Saves the currently logged in uesr's data back to peppermint.json.
- * @return bool Whethert he user's data was saved successfully. Returns false if the user isn't logged in.
+ * @return bool Whether the user's data was saved successfully. Returns false if the user isn't logged in.
  */
 function save_userdata()
 {
diff --git a/module_index.json b/module_index.json
index 3ed2b96..c2c11b4 100755
--- a/module_index.json
+++ b/module_index.json
@@ -86,7 +86,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
         "id": "feature-search",
-        "lastupdate": 1482008539,
+        "lastupdate": 1485464071,
         "optional": false
     },
     {
@@ -104,7 +104,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds a user preferences page, letting pople do things like change their email address and password.",
         "id": "feature-user-preferences",
-        "lastupdate": 1483389541,
+        "lastupdate": 1485463715,
         "optional": false
     },
     {
diff --git a/modules/feature-user-preferences.php b/modules/feature-user-preferences.php
index 16b2ff5..6a5af80 100644
--- a/modules/feature-user-preferences.php
+++ b/modules/feature-user-preferences.php
@@ -63,6 +63,42 @@ register_module([
 			exit(page_renderer::render_main("User Preferences - $settings->sitename", $content));
 		});
 		
+		add_action("save-preferences", function() {
+			global $env, $settings;
+			
+			if(!$env->is_logged_in)
+			{
+				http_response_code(400);
+				exit(page_renderer::render_main("Error Saving Preferences - $settings->sitename", "<p>You aren't logged in, so you can't save your preferences. Try <a href='?action=login&returnto=" . rawurlencode("?action=user-preferences") . "'>logging in</a> first.</p>"));
+			}
+			
+			if(isset($_POST["email-address"]))
+			{
+				if(mb_strlen($_POST["email-address"]) > 320)
+				{
+					http_response_code(413);
+					exit(page_renderer::render_main("Error Saving Email Address - $settings->sitename", "<p>The email address you supplied (<code>{$_POST['email-address']}</code>) is too long. Email addresses can only be 320 characters long. <a href='javascript:window.history.back();'>Go back</a>."));
+				}
+				
+				if(mb_strpos($_POST["email-address"], "@") === false)
+				{
+					http_response_code(422);
+					exit(page_renderer::render_main("Error Saving Email Address - $settings->sitename", "<p>The email address you supplied (<code>{$_POST['email-address']}</code>) doesn't appear to be valid. <a href='javascript:window.history.back();'>Go back</a>."));
+				}
+				
+				$env->user_data->emailAddress = $_POST["email-address"];
+			}
+			
+			if(!save_userdata())
+			{
+				http_response_code(503);
+				exit(page_renderer::render_main("Error Saving Preferences - $settings->sitename", "<p>$settings->sitename had some trouble saving your preferences! Please contact $settings->admindetails_name, $settings->sitename's administrator and tell them about this error if it still occurs in 5 minutes. They can be contacted by email at this address: <a href='mailto:" . hide_email($settings->admindetails_email) . "'>" . hide_email($settings->admindetails_email) . "</a>.</p>"));
+			}
+			
+			
+			exit(page_renderer::render_main("Preferences Saved Successfully - $settings->sitename", "<p></p>"));
+		});
+		
 		/**
 		 * @api	{post}	?action=change-password	Change your password
 		 * @apiName			ChangePassword
