Newer
Older
<?php
/*
* blogs class - provides functions for blogs
*/
// Some useful validation functions
require_once("validation.lib.php");
// random other functions that aren't validation or db related
require_once("miscfunctions.lib.php");
//stuff from blog.lib will be useful
require_once("blog.lib.php");
//Our Blogs Class
class admin {
//Blog ID
var $id;
//Blogger's Details
var $userName;
var $realName;
//Errors
var $error;
//Date formats
var $shortDateFormat;
var $longDateFormat;
//Paths
var $httpPath;
var $adminPath;
var $blogPath;
var $basePath;
//[temporary] holder for instance of blog class
var $blog;
//Constructor - checks we've been given a valid username, and pulls in generic blog info
function admin()
{
global $session, $BlogDB, $baseurl;
//set the error string first, so we dont wipe out any errors
$this->error = '';
//set the locale
setlocale(LC_ALL, 'en_GB');
//pull in the session stuff
$this->startSession();
//setup our environment
$this->userName = $session->username;
$this->realName = $session->fullname;
$this->id = $BlogDB->GetOne("select id from users where username='".$this->userName."'");
$this->shortDateFormat = "Y-m-d";
$this->longDateFormat = "r";
$this->httpPath = $baseurl."/Blogs/";
$this->adminPath = $this->httpPath."Admin/";
$this->basePath = $baseurl."/Blogs/";
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
if($this->httpPath[strlen($this->httpPath)-1]!="/") {
$this->httpPath .= "/";
}
//if we are logged in start a blog instance, and setup the blog path
if ($this->userName) {
$this->blog = new blogs($this->userName);
$this->blogPath = $this->basePath.$this->userName."/";
}
}
//start / check our session
function startSession()
{
//set the session time out in seconds
$maxSessionAge = 10800; //1 hour
//setup the session stuff
session_name("BlogSession");
session_set_cookie_params($maxSessionAge,dirname($_SERVER['SCRIPT_NAME'])."/");
session_start();
//if we dont have a session, start one
if (!$_SESSION[time]) {
$_SESSION[time] = time();
}
//close the session if its too old
elseif ((time()-$_SESSION[time]) > $maxSessionAge) {
session_unset();
$this->error =_("Session Expired");
$this->startSession();
}
//else we are happy, and we just update the session time
else {
$_SESSION[oldTime] = $_SESSION[time];
$_SESSION[time] = time();
}
}
//logs people in
function login()
{
global $BlogDB, $smarty;
$username = "";
$password = "";
//sanitise username
if (isset($_POST['username']) && trim($_POST['username']) != "" && safeuname(trim($_POST['username']))) {
$username = trim($_POST['username']);
}
else {
$this->error = _("Please check the username field");
}
//sanitise password
if (isset($_POST['password']) && trim($_POST['password']) != "") {
$password = trim($_POST['password']);
}
else {
$this->error = _("Please check the password field");
}
//no errors?
if(!$this->error)
{
//try to pull in the users details
$sqlRow = $BlogDB->GetRow("SELECT id, name, password from users where enabled = true and username = '".$username."' limit 1;");
//check we returned a user
if (!$sqlRow) {
$this->error =_("Invalid Username or Password");
}
else {
//check the password the user gave us agaisnt the one in the database
if ($sqlRow['password']!=crypt($password, $sqlRow['password'])) {
$this->error =_("Invalid Username or Password");
}
else {
//if everything matches dump some persistant details into the session
$_SESSION['id'] = $sqlRow['id'];
$_SESSION['userName'] = $username;
$_SESSION['realName'] = $sqlRow['name'];
$this->id = $_SESSION['id'];
$this->userName = $_SESSION['userName'];
$this->realName = $_SESSION['realName'];
}
}
}
//return a state to indicate wether login was successful
if ($this->error) {
return false;
}
else {
return true;
}
}
//admin menu
function menu() {
global $BlogDB, $smarty, $session;
$submenu = array();
if (blogger($session->username)) {
$submenu[_("My blog")] = $this->blogPath;
$submenu[_("Write new entry")] = $this->adminPath."newentry";
$submenu[_("Edit entries")] = $this->adminPath."showentries";
$submenu[_("Settings")] = $this->adminPath."Settings";
$comments = _("Comments");
$result = $BlogDB->GetOne("SELECT count(comments.id) from comments join entries on comments.post = entries.id where moderated = false and entries.user_id = ".$this->id.";");
if($result){
$comments .= " (".$result.")";
}
$submenu[$comments] = $this->adminPath."moderatecomments";
} else {
$submenu[_("Start a Blog")] = $this->adminPath."signup";
}
$menu = $smarty->get_template_vars("menu");
$menu[Blogs] = $submenu;
$smarty->assign("menu", $menu);
}
//destroys the session
function logout ()
{
session_unset();
header("Location: ".$this->blogPath);
}
//prints a login form
function printLoginForm()
{
echo "The Login Form display function has been called. This should not happen.";
}
// post an entry to the db
function postEntry()
{
global $BlogDB;
$category = '';
$subject = '';
$body = '';
//sanitise category (make sure it IS a number!)
if (isset($_POST['category']) && (int)$_POST['category'] != "" && (int)$_POST['category'] != 0) {
$category = (int)$_POST['category'];
} else {
$this->error = _("Undefined Category!");
}
//sanitise subject
if (isset($_POST['subject']) && trim($_POST['subject']) != "") {
//complain if the subject contains html or html like things rather than dumping it without warning
Loading
Loading full blame...