Source for file mvblog.php
Documentation is available at
mvblog.php
<?php
/**
* MvBlog -- An open source no-nosense blogtool
*
* Copyright (C) 2005-2007, Michiel van Baak
* Michiel van Baak <mvanbaak@users.sourceforge.net>
*
* See http://www.mvblog.org for more information on MvBlog.
* That page also provides Bugtrackers, Filereleases etc.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*
*
@package
MvBlog
*
@author
Michiel van Baak
*
@version
%%VERSION%%
*
@copyright
2005-2007 Michiel van Baak
*/
/*
* Start the autoloader, so we never have to include anything
*/
require_once
(
"mvblog_autoloader.php"
)
;
$mvblog_AutoLoader
=
new
mvblog_AutoLoader
(
)
;
$pathInfo
=
pathinfo
(
__FILE__
)
;
$mvblog_AutoLoader
->
registerPath
(
$pathInfo
[
"dirname"
]
,
"%s.php"
,
mvblog_AutoLoader
::
OPT_LOWERCASE
)
;
// Register the AutoLoader object as the autoloader.
function
__autoload
(
$className
)
{
global
$mvblog_AutoLoader
;
$mvblog_AutoLoader
->
autoload
(
$className
)
;
}
/* Start heavy error reporting if we're on a dev site */
MvBlog_debug
::
start_development
(
False
)
;
/* Read the configuration file */
$configfile
=
dirname
(
dirname
(
__FILE__
)
.
"../"
)
.
"/conf/mvblog.ini"
;
$availSettings
=
array
(
"general"
=>
array
(
"debug"
=>
array
(
"type"
=>
mvblog_IniFileReader
::
TYPE_BOOL
,
"default"
=>
"no"
)
,
)
,
"database"
=>
array
(
"database"
=>
array
(
"type"
=>
mvblog_IniFileReader
::
TYPE_STRING
,
"default"
=>
"mvblog"
)
,
"hostname"
=>
array
(
"type"
=>
mvblog_IniFileReader
::
TYPE_STRING
,
"default"
=>
"localhost"
)
,
"username"
=>
array
(
"type"
=>
mvblog_IniFileReader
::
TYPE_STRING
,
"default"
=>
"mvblog"
)
,
"password"
=>
array
(
"type"
=>
mvblog_IniFileReader
::
TYPE_STRING
,
"default"
=>
"mvblog"
)
,
"type"
=>
array
(
"type"
=>
mvblog_IniFileReader
::
TYPE_STRING
,
"default"
=>
"mysql"
)
,
)
,
)
;
$config
=
new
mvblog_IniFileReader
(
$availSettings
,
$configfile
)
;
/**
* Class that holds methods to create public site.
*
@package
MvBlog
*/
Class
MvBlog
extends
MvBlog_common
{
/* contstants */
/* variables */
/**
*
@var
string
$lang
Language of blog. Can be en_US or nl_NL for now
*/
public
$lang
=
"en_US"
;
/* }}} */
/* methods */
/* __construct {{{ */
/**
* Constructor to set some defaults
*
*
@param
string
$basedir
If set use this directory where MvBlog is located.
*/
public
function
__construct
(
$basedir
=
""
)
{
parent
::
__construct
(
$basedir
.
"plugins/"
)
;
}
/* }}} */
/* check_admin_logged_in() {{{ */
/**
* Check to see if the admin session is set
*/
public
function
check_admin_logged_in
(
)
{
if
(
!
$_SESSION
[
"author_id"
]
)
{
header
(
"Location: login.php"
)
;
}
}
/* }}} */
/* blog_get_title() {{{ */
/**
* get the blog title.
*
*
@return
string
title or "blog" if no title set
*/
public
function
blog_get_title
(
)
{
if
(
$this
->
settings
[
"blogtitle"
]
)
{
$title
=
stripslashes
(
$this
->
settings
[
"blogtitle"
]
)
;
}
else
{
$title
=
"blog"
;
}
return
$title
;
}
/* }}} */
/* blog_get_description() {{{ */
/**
* Get blogsetting description from database
*
*
@return
string
The blogdescription
*/
public
function
blog_get_description
(
)
{
if
(
$this
->
settings
[
"blogdescription"
]
)
{
$pagedescription
=
nl2br
(
stripslashes
(
$this
->
settings
[
"blogdescription"
]
))
;
}
else
{
$pagedescription
=
""
;
}
return
$pagedescription
;
}
/* }}} */
/* blog_content($start, $limit) {{{ */
/**
* main function to give user correct html depending on action etc
*
*
@param
int
$start
(optional) Start entry in recordset
*
@param
int
$limit
(optional) Number of records to show
*/
public
function
blog_content
(
$start
=
0
,
$limit
=
0
)
{
/* get limit from database, when no limit given by function call */
if
(
!
$limit
)
{
if
(
$this
->
settings
[
"postsperpage"
]
)
{
$limit
=
$this
->
settings
[
"postsperpage"
]
;
}
else
{
/* fall back to something sane if none found. fixes issue #25 */
/* FB: 20070304: This shouldn't happen anymore, as there are now
default settings in MvBlog_common::_get_settings().
Keeping it anyway just to be sure/
*/
$limit
=
20
;
}
}
// FB: 20070304: Check if key exists and strstr->strpos()
// if (strstr($_REQUEST["action"], "view/")) {
if
(
array_key_exists
(
"action"
,
$_REQUEST
)
&&
strpos
(
$_REQUEST
[
"action"
]
,
"view/"
)
!==
false
)
{
$_REQUEST
[
"id"
]
= (int)
substr
(
$_REQUEST
[
"action"
]
,
5
)
;
$_REQUEST
[
"action"
]
=
"view"
;
}
// Determine the action.
if
(
array_key_exists
(
"action"
,
$_REQUEST
))
{
$action
=
$_REQUEST
[
"action"
]
;
}
else
{
$action
=
""
;
// Will be handled by the 'default' case in the switch below.
}
switch
(
$action
)
{
/* user related functions */
//this one is here for backward compatibiliy. Can be removed in release 4
case
"register_confirm"
:
$this
->
user_confirm
(
)
;
break
;
//new user actions
case
"user_confirm"
:
$this
->
user_confirm
(
)
;
break
;
case
"user_save"
:
$this
->
user_save
(
)
;
break
;
case
"user_new"
:
$this
->
user_edit
(
1
)
;
break
;
case
"user_login"
:
if
(
array_key_exists
(
"user"
,
$_REQUEST
)
&&
$this
->
user_login
(
$_REQUEST
[
"user"
]
))
{
$this
->
get_articles
(
$start
,
$limit
)
;
}
else
{
echo
gettext
(
"wrong username/pass"
)
;
}
break
;
case
"user_settings"
:
$this
->
user_edit
(
0
)
;
break
;
/* article related functions */
case
"view"
:
$this
->
show_article
(
$_REQUEST
[
"id"
]
)
;
break
;
case
"viewdossier"
:
$this
->
get_articles
(
$start
,
$limit
,
0
,
$_REQUEST
[
"id"
]
)
;
break
;
case
"post_comment"
:
$this
->
post_comment
(
$_POST
)
;
break
;
case
"rss"
:
header
(
"Location: common/rss.php"
)
;
break
;
case
"archive"
:
$this
->
get_articles
(
$start
,
$limit
,
1
)
;
break
;
case
"archive_old"
:
$this
->
get_articles
(
$start
,
$limit
,
4
)
;
break
;
case
"archive_cat"
:
if
(
array_key_exists
(
"c"
,
$_REQUEST
)
&&
$_REQUEST
[
"c"
]
==
"aside"
)
{
$this
->
get_articles
(
$start
,
$limit
,
3
)
;
}
else
{
$this
->
get_articles
(
$start
,
$limit
,
2
)
;
}
break
;
default
:
$this
->
get_articles
(
$start
,
$limit
)
;
break
;
}
}
/* }}} */
/* get_articles($start, $limit, $archive) {{{ */
/**
* show posts in pages or the archive
*
*
@param
int
$start
the starting point in the recordset
*
@param
int
$limit
the ammount of items to show
*
@param
int
$archive
type of archive. 0 = none, 1 = archive by date, 2 = archive by category, 3 = archive of asides, 4 = old old old
*
@param
int
$dossier
if not 0 show info about dossier
*/
public
function
get_articles
(
$start
,
$limit
,
$archive
=
0
,
$dossier
=
0
)
{
// FB: 20070304: Check key and set a default if not set.
if
(
array_key_exists
(
"top"
,
$_REQUEST
))
$top
=
$_REQUEST
[
"top"
]
;
if
(
!
isset
(
$top
)
||
empty
(
$top
))
$top
=
1
;
//article url base
if
(
$this
->
settings
[
"cleanurl"
]
)
$link
=
"post/%d#READMORE"
;
else
$link
=
"index.php?action=view&id=%d#READMORE"
;
//category url base
if
(
$this
->
settings
[
"cleanurl"
]
)
$catlink
=
"category/%d"
;
else
$catlink
=
"index.php?action=archive_cat&c=%d"
;
$start
=
$top
-
1
;
$max_time
=
mktime
(
0
,
0
,
0
,
date
(
"m"
)
,
date
(
"d"
)
+
1
,
date
(
"Y"
))
;
$options
=
array
(
"start"
=>
$start
,
"limit"
=>
$limit
,
"max_time"
=>
$max_time
,
"top"
=>
$top
,
"archive"
=>
$archive
,
"dossier"
=>
$dossier
,
"replace_references"
=>
1
)
;
foreach
(
$_REQUEST
as
$k
=>
$v
)
$options
[
"urlparams"
]
[
$k
]
=
$v
;
$posts
=
$this
->
_get_posts
(
$options
)
;
echo
$posts
[
"title"
]
;
if
(
array_key_exists
(
"desc"
,
$posts
))
echo
$posts
[
"desc"
]
;
if
(
$posts
[
"total_count"
]
)
{
foreach
(
$posts
[
"posts"
]
as
$row
)
{
//get number of comments
$ccquery
=
sprintf
(
"SELECT COUNT(*) FROM comments WHERE articles_id = %d"
,
$row
[
"id"
]
)
;
$ccq
=
&
$this
->
db
->
query
(
$ccquery
)
;
$comments_count
=
$ccq
->
fetchRow
(
)
;
?>
<div class="log_post">
<?php
if
(
$row
[
"aside"
]
!=
1
)
{
?>
<div class="log_post_head">
<h1 class="log_post_h1"><a href="
<?php
echo
sprintf
(
$link
,
$row
[
"id"
]
)
;
?>
">
<?php
echo
htmlspecialchars
(
stripslashes
(
$row
[
"title"
]
))
;
?>
</a></h1>
<h2 class="log_post_h2">
<?php
echo
gettext
(
"category"
)
;
?>
:
<?php
$categories
=
explode
(
","
,
$row
[
"categories_ids"
]
)
;
foreach
(
$categories
as
$v
)
{
if
(
array_key_exists
(
$v
,
$this
->
categories
)
&&
$this
->
categories
[
$v
]
[
"icon"
]
&&
$this
->
settings
[
"show_cat_icons"
]
)
echo
"<a href=\""
.
sprintf
(
$catlink
,
$v
)
.
"\"><img src=\"images/categories/"
.
$this
->
categories
[
$v
]
[
"icon"
]
.
"\" title=\""
.
htmlspecialchars
(
$this
->
categories
[
$v
]
[
"name"
]
)
.
"\" alt=\""
.
htmlspecialchars
(
$this
->
categories
[
$v
]
[
"name"
]
)
.
"\" class=\"category_icon\" /></a> "
;
elseif
(
array_key_exists
(
$v
,
$this
->
categories
))
echo
"<a href=\""
.
sprintf
(
$catlink
,
$v
)
.
"\">"
.
htmlspecialchars
(
$this
->
categories
[
$v
]
[
"name"
]
)
.
"</a> "
;
}
?>
</h2>
</div>
<?php
}
?>
<div class="log_post_body">
<?php
if
(
$row
[
"aside"
]
)
{
?>
<div class="log_post_aside">
<?php
}
else
{
?>
<div class="log_post_normal">
<?php
}
?>
<?php
$text
=
$this
->
strip_invalid_xml
(
stripslashes
(
$row
[
"body"
]
))
;
$text
=
$this
->
plugman
->
run_hooks
(
"text_output"
,
$text
)
;
if
(
$this
->
limit_text
(
$text
))
{
echo
$text
;
?>
<br /><br /><a href="
<?php
echo
sprintf
(
$link
,
$row
[
"id"
]
)
;
?>
" class="link_readmore">
<?php
echo
gettext
(
"read more"
)
;
?>
</a>
<?php
}
else
{
echo
$text
;
}
?>
</div>
</div>
<div class="log_post_foot">
<?php
if
(
$row
[
"aside"
]
!=
1
)
{
?>
<span class="log_post_commentslink"><a href="
<?php
echo
sprintf
(
$link
,
$row
[
"id"
]
)
;
?>
#comments">
<?php
echo
sprintf
(
ngettext
(
"%d Comment"
,
"%d Comments"
,
$comments_count
[
0
]
)
,
$comments_count
[
0
]
)
;
?>
</a><br /></span>
<span class="log_post_author">
<?php
echo
gettext
(
"By"
)
.
": <i>"
.
htmlspecialchars
(
$this
->
authors
[
$row
[
"authors_id"
]]
[
"fullname"
]
)
;
?>
</i></span>
<span class="log_post_date">|
<?php
echo
gettext
(
"On"
)
.
": <i>"
.
date
(
"d-m-Y H:i"
,
$row
[
"date"
]
)
;
?>
</i></span>
<?php
if
(
$row
[
"last_modified"
]
)
{
?>
<span class="log_post_author"><br />
<?php
echo
gettext
(
"Last modified by"
)
.
": <i>"
.
htmlspecialchars
(
$this
->
authors
[
$row
[
"modified_by"
]]
[
"fullname"
]
)
;
?>
</i></span>
<span class="log_post_date">|
<?php
echo
gettext
(
"Last modified on"
)
.
": <i>"
.
date
(
"d-m-Y H:i"
,
$row
[
"last_modified"
]
)
;
?>
</i></span>
<?php
}
?>
<?php
}
?>
</div>
</div>
<?php
}
if
(
$limit
)
{
echo
"<div class=\"log_nextprev_container\">"
;
if
(
$top
>
1
)
echo
"<a href=\""
.
$posts
[
"url_prev"
]
.
"\" class=\"link_prev\">"
.
gettext
(
"previous"
)
.
"</a> "
;
if
((
$start
+
$limit
)
>
$posts
[
"total_count"
]
)
$end
=
$posts
[
"total_count"
]
;
else
$end
=
(
$start
+
$limit
)
;
echo
" "
.
(
$start
+
1
)
.
"-"
.
$end
.
" ("
.
$posts
[
"total_count"
]
.
" "
.
gettext
(
"total"
)
.
") "
;
if
((
$start
+
$limit
)
<
$posts
[
"total_count"
]
)
echo
"<a href=\""
.
$posts
[
"url_next"
]
.
"\" class=\"link_next\">"
.
gettext
(
"next"
)
.
"</a>"
;
echo
"</div>"
;
}
}
else
{
$adminpage
=
"http://"
.
$_SERVER
[
"SERVER_NAME"
]
.
substr
(
$_SERVER
[
"REQUEST_URI"
]
,
0
,
strrpos
(
$_SERVER
[
"REQUEST_URI"
]
,
"/"
))
.
"/admin/"
;
?>
<div class="log_post">
<div class="log_post_head">
<h1 class="log_post_h1">Welcome to MvBlog.</h1>
</div>
<div class="log_post_body">
<div class="log_post_normal">
<?php
if
(
array_key_exists
(
"action"
,
$_REQUEST
)
&&
!
empty
(
$_REQUEST
[
"action"
]
))
{
?>
There are no posts to show here.
<?php
}
else
{
?>
If you see this your install of MvBlog has been succesfull. Congratulations!<br />
You can now add your posts etc via de Admin Interface.<br /> Login at <a href="
<?php
echo
$adminpage
;
?>
">
<?php
echo
$adminpage
;
?>
</a>
<?php
}
?>
</div>
</div>
<div class="log_post_foot">
<span class="log_post_author"><i>MvBlog system</i></span>
</div>
</div>
<?php
}
}
/* }}} */
/* strip_invalid_xml() {{{ */
/**
* strip some stuff leftover from old editor
*
*
@param
string
$data
the text to process
*
@return
string
the text with invalid xml stripped
*/
public
function
strip_invalid_xml
(
$data
)
{
$data
=
preg_replace
(
"/ ref=\"[^\"].*\"/si"
,
""
,
$data
)
;
return
$data
;
}
/* }}} */
/* limit_text($text) {{{ */
/**
* limit the length of a message
*
*
@param
string
&$text
the text to limit in length
*
@return
int
1 if truncated, 0 if original string is within limits
*/
public
function
limit_text
(
&
$text
)
{
if
(
strstr
(
$text
,
"##BREAKPOINT##"
))
{
$text
=
substr
(
$text
,
0
,
strpos
(
$text
,
"##BREAKPOINT##"
))
;
return
1
;
}
else
{
return
0
;
}
}
/* }}} */
/* show_article($id) {{{ */
/**
* Show an article with all comments etc
*
*
@param
int
$id
The article id to show
*
@param
int
$captcha_error
if 1 it shows an error
*/
public
function
show_article
(
$id
,
$captcha_error
=
0
)
{
//category url base
if
(
$this
->
settings
[
"cleanurl"
]
)
$catlink
=
"category/%d"
;
else
$catlink
=
"index.php?action=archive_cat&c=%d"
;
if
(
$id
==
"httperror"
)
{
$row
=
array
(
"title"
=>
$_REQUEST
[
"error"
]
,
"body"
=>
"The requested URL ("
.
$_SERVER
[
"REQUEST_URI"
]
.
") was not found.
<br /><br />If you got here from a link on another page please contact that webmaster.<br />
If it was in your bookmarks, please update them.<br /><br />
My best guess is you came from: "
.
$_SERVER
[
'HTTP_REFERER'
]
.
"<br /><br />
In the meantime you might be interested in:<br />
<a href=\"index.php\" title=\"homepage\">Homepage</a><br />
<a href=\"common/rss.php\" title=\"rss feed\">RSS feed</a><br />"
,
"date"
=>
time
(
)
)
;
$errormode
=
1
;
}
else
{
$res
=
&
$this
->
db
->
query
(
sprintf
(
"SELECT * FROM articles WHERE id = %d"
,
$id
))
;
if
(
PEAR
::
isError
(
$res
))
die
(
$res
->
getMessage
(
))
;
$row
=
$res
->
fetchRow
(
MDB2_FETCHMODE_ASSOC
)
;
$anoncomments
=
$row
[
"allowanoncomments"
]
;
/* we can have a global overwrite for the anon comments */
if
(
$this
->
settings
[
"allowanoncomments"
]
!=
1
)
$anoncomments
=
0
;
$errormode
=
0
;
}
?>
<div class="log_post">
<div class="log_post_head">
<?php
if
(
$errormode
)
{
?>
<h1 class="log_post_h1_error">
<?php
}
else
{
?>
<h1 class="log_post_h1">
<?php
}
?>
<?php
echo
htmlspecialchars
(
stripslashes
(
$row
[
"title"
]
))
;
?>
</h1>
&n