From: Benjamin Auder <benjamin.a@mailoo.org>
Date: Thu, 5 Feb 2015 15:16:00 +0000 (+0100)
Subject: first commit after cleaning
X-Git-Url: https://git.auder.net/variants/current/doc/css/R.css?a=commitdiff_plain;h=3e641f63d7206b50c3eebecc53023117b7185dac;p=sview.git

first commit after cleaning
---

3e641f63d7206b50c3eebecc53023117b7185dac
diff --git a/.htaccess b/.htaccess
new file mode 100644
index 0000000..6c33b67
--- /dev/null
+++ b/.htaccess
@@ -0,0 +1,13 @@
+<IfModule mod_rewrite.c>
+	RewriteEngine on
+
+	# indicate the path of your website relatively to the web root
+	RewriteBase /
+
+	# the two following conditions say "if the requested resource exist, just serve it"
+	RewriteCond %{REQUEST_FILENAME} !-f
+	RewriteCond %{REQUEST_FILENAME} !-d
+	
+	# redirect anything else to index.php (which will analyze the URI)
+	RewriteRule . index.php
+</IfModule>
diff --git a/HOWTO.md b/HOWTO.md
new file mode 100644
index 0000000..88fe191
--- /dev/null
+++ b/HOWTO.md
@@ -0,0 +1,112 @@
+## 0. Installation
+
+In the following, I assume your website is located under http\[s\]://domain/topic/ 
+and is named "website" (adapt to your case). For example, in https://github.com/blog/ 
+domain = github.com and topic = blog.
+
+Get the source code either with `git clone` command or using a zip archive. 
+Copy all folder contents in the website/ folder : 
+<pre>website/
+    a/
+    f/
+    site/
+    .htaccess
+    common.php
+    defaults.php
+    index.php
+    s.php
+</pre>
+- **a/** (for "assets") is the folder for CSS files, images and javascript codes. 
+I like to put them respectively in css/, img/ and js/ folders, but the choice is yours.
+- **f/** (for "files") is the folder for any downloadable (or browsable) file you may upload. 
+- **site/** is the main folder containing all your website pages. Three are already there :
+  - **404.php** : the 404 error page;
+  - **dl.php** : a script to download binary files;
+  - **home.php** : the specifications for the welcome page.
+- **.htaccess** : its main job consists in routing everything that is not a resource 
+to the index.php file. 
+- **common.php** contains shared variables and functions to be used by at least two different pages.
+- **defaults.php** defines default variables for any web page, like the title or javascripts block.
+- **index.php** contains your website template, which is rendered for any web page 
+(and filled with specific values defined in pages under site/ folder; anything can be customized).
+- **s.php** consists in the framework code, loaded at the beginning of index.php.
+
+Now (online), in the .htaccess file, change the line `RewriteBase /` to `RewriteBase /topic`.
+
+## 1. Set default contents
+
+Edit the file defaults.php with
+- A global title to your website; this title can later be mixed with a more specific 
+page-based title, or be replaced.
+- A list of references to CSS style sheets and pre-rendering javascript, like 
+`<link rel="stylesheet" href="http://cran.r-project.org/R.css"/>`. 
+We will see later how to refer to local style sheets (under a/css).
+- Some javascript code which will be loaded by default after every page loads
+(e.g. [jquery](http://jquery.com/)).
+
+Each variable name is prepended with "b\_" to avoid potential conflicts with your own variables.
+
+## 2. Complete main pages
+
+### index.php
+
+Complete
+- The menu (at commented location)
+- The banner (near the menu, if you want one)
+- The footer (if you don't want one, just drop it).
+
+You can also change the \<meta\> tags if needed.
+
+### site/home.php
+
+The welcome page. You can choose a title ($s\_title) or use the default one 
+(by not specifying anything). Style sheets and javascripts can be customized, ...etc. 
+Any default variable can be used to define a specific variable (prepended with "s\_"). 
+
+### site/404.php
+
+Customize it; it is probably viewed more often than you think ;-)
+
+## 3. Write all other pages
+
+All pages are under site/ folder, and you can nest them in any directory tree.
+
+**Hint** : if you don't want to load the main template, just end any site file 
+with a PHP `exit` directive.
+
+Now we will see how to access pages and resources (images, CSS, files, javascript).
+
+<p>&nbsp;</p>
+--------------------------------------------------
+
+## How to view a web page ?
+
+The page at physical location site/some\_folder/mypage.php is viewed in the web browser at the URL 
+http\[s\]://domain/topic/website/some\_folder/mypage (thanks to URL rewriting defined in 
+the .htaccess file). 
+
+Any page can be linked internally using the `r()` PHP function ('r' for "resource"), like in 
+the following : `<a href="<?php echo r('some_folder/mypage'); ?>">'`. This function determines 
+the nesting level and output the appropriate path.
+
+## How to access...
+
+*A CSS style sheet* : its path is given by the following PHP function call 
+`r('a/css/name_of_the_file.css')` from within any site file (assuming you place all CSS files 
+under a/css/. They may be inside a nested folder structure).
+
+*An image* : same as above, with `r('a/img/name_of_the_image.xxx')`.
+
+*A javascript file* : same as above, with `r('a/js/name_of_the_file.js')`.
+
+## How to give a download link ?
+
+Just use a regular link pointing to `r('dl/?f=name_of_the_file.xxx')`, anywhere you want.
+
+<p>&nbsp;</p>
+--------------------------------------------------
+
+## Usual workflow
+
+Just add pages under site/ folder, and potential resources and files under a/ and f/. 
+All other files will not change a lot.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..73c5672
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2014 Benjamin Auder
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..48ab862
--- /dev/null
+++ b/README.md
@@ -0,0 +1,17 @@
+# sview : tiny PHP web framework.
+
+It is inspired by [a similar framework](http://shortphp.com) written by Arnór Heiðar Sigurðsson. 
+In the same spirit but more advanced and more complete, see also [nanoc](http://nanoc.ws/) - in Ruby.
+
+sview is designed to organize essentially static websites. 
+It does not offer the features you would expect from a complete MVC framework - 
+actually, it has only the "V" part. 
+If you want a more demanding dynamic website, consider using an appropriate tool, 
+[Symfony](http://symfony.com/) for example.
+
+## How to use it ?
+
+The documentation is [right 
+here](http://git.redua.net/?p=sview.git;a=blob;f=HOWTO.md;hb=HEAD), 
+and a full website example is [located 
+here](http://git.redua.net/?p=sview.git;a=blob;f=sample-website.tar.xz;hb=HEAD).
diff --git a/a/.gitkeep b/a/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/common.php b/common.php
new file mode 100644
index 0000000..c257ed3
--- /dev/null
+++ b/common.php
@@ -0,0 +1,3 @@
+<?php
+
+//Shared variables and/or functions go here
diff --git a/defaults.php b/defaults.php
new file mode 100644
index 0000000..b938847
--- /dev/null
+++ b/defaults.php
@@ -0,0 +1,9 @@
+<?php
+
+//Set default values here; 'b' stands for "base"
+
+$b_title = 'My website title';
+
+$b_header = '';
+
+$b_javascripts = '';
diff --git a/f/.gitkeep b/f/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..57141bd
--- /dev/null
+++ b/index.php
@@ -0,0 +1,30 @@
+<?php include 's.php'; ?>
+
+<!DOCTYPE html>
+<html>
+
+<head>
+	<meta charset="UTF-8"/>
+	<title><?php echo isset($s_title)?$s_title:$b_title; ?></title>
+	<!--CSS styles-->
+	<?php echo isset($s_header)?$s_header:$b_header; ?>
+</head>
+
+<body>
+
+<!--Insert code for the menu here (or after the banner)-->
+
+<!--Some website banner (could also come before the menu)-->
+
+<div class="container">
+	<!--A content is always page-specific, thus always defined, and has no default value-->
+	<?php echo $content; ?>
+</div>
+
+<!--Write the footer here. It is not supposed to be redefined.-->
+
+<?php echo isset($s_javascripts)?$s_javascripts:$b_javascripts; ?>
+
+</body>
+
+</html>
diff --git a/s.php b/s.php
new file mode 100644
index 0000000..f0ca57d
--- /dev/null
+++ b/s.php
@@ -0,0 +1,57 @@
+<?php
+
+//Function to get resources (using $nlevels below)
+function r($toppath) {
+	global $nlevels;
+	$res = '';
+	for ($i=0; $i<$nlevels; $i++)
+		$res .= '../';
+	$res .= $toppath;
+	return $res;
+}
+
+//////////////////////////////////////////////////
+
+//Start buffering output
+ob_start();
+
+//Report any error
+error_reporting(E_ALL);
+
+//Sub-path path to the currently running script. If we're directly on top of a
+//domain, this will probably be empty, else for instance: /john/mysite
+$subpath = str_replace('/index.php', '', $_SERVER['PHP_SELF']);
+
+//Turn a request like /john/mysite/get/page.php?a=32 into /get/page.php?a=32
+$request = str_replace($subpath, '', $_SERVER['REQUEST_URI']);
+
+//Turn a request like /get/page.php?a=32 into get/page?a=32
+$request = str_replace('.php', '', $request);
+
+//Keep only what stands before '?' : get/page
+$location = trim(explode('?', $request)[0], '/');
+//[HACK] The following works better on my university server
+//~ $interrmark = strrpos($request, '?');
+//~ $location = trim(($interrmark!==FALSE ? substr($request,0,$interrmark) : $request), '/');
+
+//If the user asked for main index.php, consider he wanted home page
+if ($location == 'index') $location = '';
+
+//[HACK] Count the number of sub-levels to go up to meet assets folder
+$nlevels = substr_count($location, '/');
+
+//Turn get/page into site/get/page.php
+$phpfile = 'site/'.(strlen($location)>0?$location:'home').'.php';
+
+//Include default values for title, headers, javascripts...
+include 'defaults.php';
+
+//Include common PHP code (functions and constants)
+include 'common.php';
+
+//Finally, include the PHP file
+include (file_exists($phpfile) ? $phpfile : 'site/404.php');
+
+//regular template: flush output into $content variable
+$content = ob_get_contents();
+ob_end_clean();
diff --git a/sample-website.tar.xz b/sample-website.tar.xz
new file mode 100644
index 0000000..5a91c82
Binary files /dev/null and b/sample-website.tar.xz differ
diff --git a/site/404.php b/site/404.php
new file mode 100644
index 0000000..fb6d1bd
--- /dev/null
+++ b/site/404.php
@@ -0,0 +1,8 @@
+<?php
+	$s_title = 'Page not found';
+	header('HTTP/1.0 404 Not Found');
+?>
+
+<div>
+	<p>The requested page does not exist</p>
+</div>
diff --git a/site/dl.php b/site/dl.php
new file mode 100644
index 0000000..441b1c3
--- /dev/null
+++ b/site/dl.php
@@ -0,0 +1,20 @@
+<?php
+
+//Generic binary file downloading (when file exists)
+
+$filename = r('f/'.$_GET['f']);
+
+if (file_exists($filename)) {
+	header('Content-Description: File Transfer');
+	header('Content-Type: application/octet-stream');
+	header('Content-Disposition: attachment; filename='.basename($filename));
+	header('Expires: 0');
+	header('Cache-Control: must-revalidate');
+	header('Pragma: public');
+	header('Content-Length: ' . filesize($filename));
+	ob_end_flush();
+	readfile($filename);
+	exit;
+}
+
+else header('Location: '.r('404'));
diff --git a/site/home.php b/site/home.php
new file mode 100644
index 0000000..6f0e47a
--- /dev/null
+++ b/site/home.php
@@ -0,0 +1,7 @@
+<?php
+	$s_title = 'Home page';
+?>
+
+<div>
+	<p>Some content</p>
+</div>