$ git clone http://thingshare.ion.nu/thingshare.git
commit ac0bba7f420728a31135d2f94cef853c2e4912a6
Author: Alicia <...>
Date:   Sat Mar 14 14:18:12 2020 +0100

    Fixes and improvements for the initial setup process.

diff --git a/genpreviews.php b/genpreviews.php
index 4474627..5c8c713 100755
--- a/genpreviews.php
+++ b/genpreviews.php
@@ -27,7 +27,7 @@ if(!flock($lock, LOCK_EX|LOCK_NB)){die("Lock failed");}
 include_once('db.php');
 include_once('config.php');
 include_once('files.php');
-mkdir(TMPDIR, 755);
+mkdir(TMPDIR, 0755);
 $res=mysqli_query($db, 'select distinct lower(substring_index(name,".",-1)) as type, hash from files');
 while($row=mysqli_fetch_assoc($res))
 {
@@ -141,6 +141,7 @@ while($row=mysqli_fetch_assoc($res))
           $c=sprintf('#%02x%02x%02x', PREVIEW_COLOR[0], PREVIEW_COLOR[1], PREVIEW_COLOR[2]);
           copy($file, TMPDIR.'/thingsharepreview.'.$row['type']);
           file_put_contents(TMPDIR.'/thingsharepreview.scad', 'color("'.$c.'")import("thingsharepreview.'.str_replace('"', '', $row['type']).'");');
+          if(getenv('DISPLAY')==''){putenv('DISPLAY=:0');} // Default X server
           system('openscad --imgsize '.(int)PREVIEW_SIZE[0].','.(int)PREVIEW_SIZE[1].' -o '.escapeshellarg($file.'.png').' '.escapeshellarg(TMPDIR.'/thingsharepreview.scad'));
           unlink(TMPDIR.'/thingsharepreview.'.$row['type']);
           unlink(TMPDIR.'/thingsharepreview.scad');
diff --git a/index.php b/index.php
index 55eb09d..45eec92 100644
--- a/index.php
+++ b/index.php
@@ -17,6 +17,7 @@
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
+// <h1>If you're seeing this, PHP isn't set up correctly</h1><!--
 // Upgrade HTTP requests to HTTPS if the client supports/requests it
 if(isset($_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS']) &&
    in_array(strtolower($_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS']), Array('1','true','yes','on')) &&
diff --git a/setup.php b/setup.php
index ea43d7b..db08ffd 100644
--- a/setup.php
+++ b/setup.php
@@ -18,8 +18,18 @@
     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
 if(file_exists('config.php')){die('Already set up');}
-// TODO: Check that mod_rewrite/equivalent works? Might need to come after the data collection, considering domain and path
-// TODO: Check that session.gc_maxlifetime is set to at least a few hours? (with a warning, not an error)
+if(substr($_SERVER['REQUEST_URI'], -16)=='/rpc/rewritetest'){header('Content-type: image/png');readfile('icons/find.png');exit();}
+// Step 0: Warnings (not mandatory, but persistent through the process which also works as doublechecking if solved)
+print('<p><img src="icons/find.png" height="0" alt="The \'icons\' directory appears to be inaccessible. This could indicate filesystem permissions issues or a webserver directory alias (e.g. icons for directory listings)" /></p>');
+print('<p><img src="rpc/rewritetest" height="0" alt="mod_rewrite+.htaccess (or equivalent) does not appear to be working correctly (or not yet added. At time of writing there is only Apache mod_rewrite+.htaccess)" /></p>');
+$sessiontime=ini_get('session.gc_maxlifetime');
+if($sessiontime<3*3600)
+{
+  if($sessiontime%3600==0){$humantime=($sessiontime/3600).' hours';}
+  elseif($sessiontime%60==0){$humantime=($sessiontime/60).' minutes';}
+  else{$humantime=$sessiontime.' seconds';}
+  print('<p>Warning! session.gc_maxlifetime in php.ini is set to '.$sessiontime.' seconds ('.$humantime.') which means sessions will end after just '.$humantime.' of inactivity. Over '.(4*3600).' (4 hours) is recommended</p>');
+}
 // Step 1: PHP Modules
 $modules=Array();
 if(!function_exists('curl_exec')){$modules[]='curl';}
@@ -45,7 +55,30 @@ if(!touch('configtest'))
   exit();
 }
 unlink('configtest');
-// Step 3: Gather information to write a config.php
+// Step 3: Check that things are installed and accessible
+$deps='';
+$mandatory=false;
+if(substr_count(shell_exec('assimp --help'), 'listext')<1)
+{
+  $deps.='<li>The program assimp appears to be missing. It is required to convert between 3D model filetypes for previews. On Debian it is provided by the package \'assimp-utils\'</li>';
+  $mandatory=true;
+}
+if(!is_dir('parsedown'))
+{
+  $deps.='<li>The PHP library Parsedown appears to be missing. It is required to render markdown formatted text into HTML. See <a href="Dependencies">Dependencies</a> for how to obtain it</li>';
+  $mandatory=true;
+}
+if(!is_dir('mdjs')){$deps.='<li>The javascript library mdjs appears to be missing. It can optionally be used to generate live previews of markdown formatted text. See <a href="Dependencies">Dependencies</a> for how to obtain it</li>';}
+if($deps!='')
+{
+  print('<h1>Dependencies</h1><ul>'.$deps.'</ul>');
+  if($mandatory)
+  {
+    print('<form><button>Continue</button></form>');
+    exit();
+  }
+}
+// Step 4: Gather information to write a config.php
 if(!isset($_POST['DOMAIN']))
 {
 // TODO: Handle preview color and size
@@ -88,7 +121,7 @@ if(!isset($_POST['DOMAIN']))
   }
   print('<button>Continue</button></form>');
 }else{
-  // Step 4: Write config.php based on config.php.sample and the submitted values
+  // Step 5: Write config.php based on config.php.sample and the submitted values
 // TODO: Sanity check on the provided values (database info, tmpdir)
   $config=file_get_contents('config.php.sample');
   foreach($_POST as $key=>$value)
@@ -99,9 +132,9 @@ if(!isset($_POST['DOMAIN']))
   print('<h1>Done!</h1>');
   print('Your Thingshare instance is now live.<br />');
   print('<a href="'.$_POST['BASEURL'].'/register">Click here</a> to register the first account, which will get the administrative privileges');
-  // Step 5: Initiate database
+  // Step 6: Initiate database
   include_once('db.php');
-  create_db_tables();
+  db_create_tables();
   // Generate and store key, used to verify remote activities of local users
   $config = array(
     'digest_alg'=>'sha512', // TODO: Is there a better digest for this? Would using HASH from config.php be a good idea? (I suspect not)