Permalink
Browse files

Initial commit of files

  • Loading branch information...
1 parent 32a9083 commit 8b7de321bd1494837f21eb8a7dcd7da561228518 @kemayo committed May 5, 2010
View
@@ -0,0 +1,6 @@
+._*
+*~
+comics/*.gif
+comics/*.png
+comics/*.jpg
+include/config.php
View
@@ -0,0 +1,11 @@
+<IfModule mod_rewrite.c>
+ RewriteEngine on
+ RewriteRule ^(.*/)?\.git/ - [F,L]
+ RewriteRule ^(.*/)?include/ - [F,L]
+ RewriteRule ^(.*/)?template/.+\.php$ - [F,L]
+
+ # Rewrite current-style URLs of the form 'index.php?q=x'.
+ RewriteCond %{REQUEST_FILENAME} !-f
+ RewriteCond %{REQUEST_FILENAME} !-d
+ RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
+</IfModule>
View
@@ -0,0 +1 @@
+Deny from all
View
No changes.
View
@@ -0,0 +1,135 @@
+<?php
+switch($request[1]) {
+case 'comic':
+ // editing an individual comic
+ $comic = array();
+ if($request[2] != 'new') {
+ $comic = $db->fetch_first("SELECT * FROM comics WHERE comicid = %d", $request[2]);
+ if($comic) {
+ $comic['text'] = fetch_text($comic['comicid']);
+ }
+ }
+ if(isset($_POST['submit'])) {
+ // this would need proper errors...
+ $pub_date = strtotime(date('Y-m-d H:i:s', strtotime($_POST['pub_date'])));
+ if(!$pub_date) {
+ die("Bad date");
+ }
+ if($comic) {
+ $comicid = $comic['comicid'];
+ $db->query("UPDATE comics SET title=%s, pub_date=%d, filename=%s, chapterid=%d WHERE comicid=%d",
+ array($_POST['title'], $pub_date, $_POST['filename'], $_POST['chapterid'], $comicid));
+ } else {
+ if(isset($_POST['filename']) && $_POST['filename']) {
+ if(!file_exists(BASEDIR.$config['comicpath'].'/'.$_POST['filename'])) {
+ die("Comic file does not exist");
+ }
+ $filename = $_POST['filename'];
+ } else {
+ if($_FILES['comicfile']['error'] == UPLOAD_ERR_NO_FILE) {
+ die("No file uploaded");
+ }
+ $uploadpath = BASEDIR.$config['comicpath'].'/'.basename($_FILES['comicfile']['name']);
+ if(file_exists($uploadpath)) {
+ die("File exists");
+ }
+ if(!move_uploaded_file($_FILES['comicfile']['tmp_name'], $uploadpath)) {
+ die("Couldn't move uploaded file");
+ }
+ $filename = basename($_FILES['comicfile']['name']);
+ }
+ if(!$filename) {
+ die("No filename");
+ }
+ $comicid = $db->insert_id(
+ "INSERT INTO comics (title, pub_date, filename, chapterid) VALUES (%s, %d, %s, %d)",
+ array($_POST['title'], $pub_date, $filename, $_POST['chapterid']));
+ }
+ $db->query(
+ "REPLACE INTO comics_text (comicid, description, transcript, alt_text) VALUES (%d, %s, %s, %s)",
+ array($comicid, $_POST['description'], $_POST['transcript'], $_POST['alt_text']));
+
+ redirect("/admin/comic/".$comicid);
+ die;
+ }
+ $comic['chapters'] = $db->fetch("SELECT * FROM chapters ORDER BY `order` DESC");
+ template('admin_comic', $comic);
+break;
+case 'chapter':
+ $chapter = false;
+ if($request[2] != 'new') {
+ $chapter = $db->fetch_first("SELECT * FROM chapters WHERE chapterid = %d", $request[2]);
+ if($chapter) {
+ $chapter['description'] = $db->quick("SELECT description FROM chapters_text WHERE chapterid = %d", $request[2]);
+ }
+ }
+ if(isset($_POST['submit'])) {
+ if($chapter) {
+ $chapterid = $chapter['chapterid'];
+ $db->query("UPDATE chapters SET title = %s, slug = %s WHERE chapterid = %d",
+ array($_POST['title'], $_POST['slug'], $chapterid));
+ } else {
+ $order = $db->quick("SELECT MAX(order) + 1 FROM chapters");
+ $chapterid = $db->insert_id(
+ "INSERT INTO chapters (`title`, `slug`, `order`) VALUES (%s, %s, %d)",
+ array($_POST['title'], $_POST['slug'], $order));
+ }
+ if(!$chapterid) {
+ die("Error saving");
+ }
+ $db->query(
+ "REPLACE INTO chapters_text (chapterid, description) VALUES (%d, %s)",
+ array($chapterid, $_POST['description']));
+
+ redirect("/admin/chapter/".$chapterid);
+ die;
+ }
+ template('admin_chapter', $chapter);
+break;
+case 'rant':
+ $rant = false;
+ if($request[2] != 'new') {
+ $rant = $db->fetch_first("SELECT * FROM rants WHERE rantid = %d", $request[2]);
+ if($rant) {
+ $rant['text'] = $db->quick("SELECT text FROM rants_text WHERE rantid = %d", $request[2]);
+ }
+ }
+ if(isset($_POST['submit'])) {
+ $pub_date = strtotime(date('Y-m-d H:i:s', strtotime($_POST['pub_date'])));
+ if(!$pub_date) {
+ die("Bad date");
+ }
+ if($rant) {
+ $rantid = $rant['rantid'];
+ $db->query("UPDATE rants SET title = %s, pub_date = %s WHERE rantid = %d",
+ array($_POST['title'], $pub_date, $rantid));
+ } else {
+ $rantid = $db->insert_id(
+ "INSERT INTO rants (`title`, `pub_date`) VALUES (%s, %s)",
+ array($_POST['title'], $pub_date));
+ }
+ if(!$rantid) {
+ die("Error saving");
+ }
+ $db->query(
+ "REPLACE INTO rants_text (rantid, text) VALUES (%d, %s)",
+ array($rantid, $_POST['text']));
+
+ redirect("/admin/rant/".$rantid);
+ die;
+ }
+ template('admin_rant', $rant);
+ break;
+default:
+ // index
+ $comics = $db->fetch("SELECT comicid, title, pub_date FROM comics ORDER BY pub_date DESC");
+ $chapters = $db->fetch("SELECT * from chapters ORDER BY `order` DESC");
+ $rants = $db->fetch("SELECT * from rants ORDER BY pub_date DESC");
+ template('admin', array(
+ 'comics' => $comics,
+ 'chapters' => $chapters,
+ 'rants' => $rants,
+ ));
+break;
+}
+?>
View
@@ -0,0 +1,110 @@
+<?php
+
+// Code taken from the PHP.net section on disabling magic_quotes.
+// Makes sure that magic_quotes is, effectively, always off.
+if (get_magic_quotes_gpc()) {
+ function stripslashes_deep($value)
+ {
+ $value = is_array($value) ?
+ array_map('stripslashes_deep', $value) :
+ stripslashes($value);
+ return $value;
+ }
+ $_POST = array_map('stripslashes_deep', $_POST);
+ $_GET = array_map('stripslashes_deep', $_GET);
+ $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
+ $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
+}
+
+/**
+ * Includes a template
+ *
+ * @param string $name
+ * @param array $vars
+ * @return void
+ */
+function template($name, $vars = false) {
+ global $config, $page;
+ if($vars) {
+ extract($vars);
+ }
+ $template = BASEDIR . '/template/' . $config['template'] . '/' . $name . '.php';
+ if(file_exists($template)) {
+ include $template;
+ } else {
+ include BASEDIR . '/template/default/' . $name . '.php';
+ }
+}
+
+function redirect($where) {
+ if(!preg_match("/^[^:]+:\/\//", $where)) {
+ $where = BASEURL . $where;
+ }
+ ob_end_clean();
+ if(DEBUG) {
+ echo '<p>Redirect to: <a href="', $where, '">', $where, '</a></p>';
+ template('foot');
+ die;
+ }
+ header("Location:{$where}");
+ die;
+}
+
+function debug($key, $details) {
+ if(!DEBUG) {
+ // no point in accumulating
+ return;
+ }
+ global $page;
+ $page['debug'][$key] = $details;
+}
+
+function ob_template($name, $vars = false) {
+ ob_start();
+ template($name, $vars);
+ return ob_get_clean();
+}
+
+function fetch_navigation($comic) {
+ global $db;
+ $comics_today = $db->quick("SELECT COUNT(*) FROM comics WHERE pub_date = %d", $comic['pub_date']);
+ return array(
+ 'current' => $comic['comicid'],
+ 'prev' => $db->quick("SELECT comicid FROM comics WHERE pub_date < %d AND pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date DESC LIMIT 1", $comic['pub_date']),
+ 'next' => $db->quick("SELECT comicid FROM comics WHERE pub_date > %d AND pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date ASC LIMIT 1", $comic['pub_date']),
+ 'first' => $db->quick("SELECT comicid FROM comics WHERE pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date ASC LIMIT 1"),
+ 'last' => $db->quick("SELECT comicid FROM comics WHERE pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date DESC LIMIT 1"),
+ );
+}
+
+function fetch_text($comicid) {
+ global $db;
+ $text = $db->fetch_first("SELECT description, transcript, alt_text FROM comics_text WHERE comicid = %d", $comicid);
+ if(!$text) {
+ return array(
+ 'description' => '',
+ 'transcript' => '',
+ 'alt_text' => '',
+ );
+ }
+ return $text;
+}
+
+function fetch_recent_updates() {
+ global $db;
+ $rants = $db->fetch("SELECT 'rant' as type, rantid AS id, title, pub_date FROM rants WHERE pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date DESC LIMIT 5");
+ $comics = $db->fetch("SELECT 'comic' as type, comicid AS id, title, pub_date FROM comics WHERE pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date DESC LIMIT 5");
+ $updates = array_merge($rants, $comics);
+ uasort($updates, 'compare_updates');
+
+ return array_slice($updates, 0, 5);
+}
+
+function compare_updates($a, $b) {
+ if ($a['pub_date'] == $b['pub_date']) {
+ return 0;
+ }
+ return ($a['pub_date'] > $b['pub_date']) ? -1 : 1;
+}
+
+?>
@@ -0,0 +1,16 @@
+<?php
+
+define('BASEURL', ''); // no trailing slash
+define('DEBUG', false);
+
+$config = array(
+ 'title' => "A Simple Comic",
+ 'comicpath' => "/comics",
+ 'template' => "default",
+ 'adminuser' => "admin",
+ 'adminpass' => "admin",
+);
+
+$db = new DataBase('localhost', 'database', 'username', 'password', DEBUG);
+
+?>
View
@@ -0,0 +1,105 @@
+<?php
+
+class DataBase {
+ private $conn;
+ private $debug;
+ public $debug_output = array();
+
+ function __construct($dbhost, $dbname, $dbuser, $dbpass, $debug = false) {
+ $this->conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
+ if(!$this->conn) {
+ die("Database unavailable.");
+ }
+ mysqli_set_charset($this->conn, 'utf8');
+ $this->debug = $debug;
+ }
+
+ function debug($sql) {
+ if($this->debug) {
+ $this->debug_output[] = $sql;
+ }
+ }
+
+ function query($sql, $values = false) {
+ if($values) {
+ if(!is_array($values)) {
+ $values = array($values);
+ }
+ $values = array_map("db_quoter", $values);
+ $sql = vsprintf($sql, $values);
+ }
+ $this->debug($sql);
+ return mysqli_query($this->conn, $sql);
+ }
+
+ function fetch($sql, $values = false) {
+ $q = $this->query($sql, $values);
+ if(!$q) {
+ return array();
+ }
+ $results = array();
+ while($r = mysqli_fetch_array($q, MYSQLI_ASSOC)) {
+ $results[] = $r;
+ }
+ mysqli_free_result($q);
+ return $results;
+ }
+
+ function fetch_first($sql, $values = false) {
+ $q = $this->query($sql, $values);
+ if(!$q) {
+ return false;
+ }
+ $r = mysqli_fetch_array($q, MYSQLI_ASSOC);
+ mysqli_free_result($q);
+ return $r;
+ }
+
+ function quick($sql, $values = false) {
+ $q = $this->query($sql, $values);
+ if(!$q) {
+ return false;
+ }
+ $r = mysqli_fetch_array($q, MYSQLI_NUM);
+ mysqli_free_result($q);
+ if($r) {
+ return current($r);
+ }
+ }
+
+ function insert_id($sql, $values = false) {
+ $q = $this->query($sql, $values);
+ if(!$q) {
+ return false;
+ }
+ $id = mysqli_insert_id($this->conn);
+ mysqli_free_result($q);
+ return $id;
+ }
+}
+
+function db_quoter($s) {
+ if(!is_numeric($s)) {
+ $s = "'".addslashes($s)."'";
+ }
+ return $s;
+}
+
+/*
+function query($sql, $values = null, $howmany = QUERY_ALL, $result_type = MYSQLI_ASSOC) {
+ if($q) {
+ $results = false;
+ } elseif($howmany == QUERY_SINGLEVALUE) {
+ if($r = mysqli_fetch_array($q, $result_type)) {
+ $results = current($r);
+ }
+ } elseif($howmany == QUERY_SINGLEVALUE_ARRAY) {
+ $results = array();
+ while($r = mysqli_fetch_array($q, $result_type)) {
+ $results[] = current($r);
+ }
+ }
+ }
+}
+*/
+?>
Oops, something went wrong.

0 comments on commit 8b7de32

Please sign in to comment.