2 /***************************************
3 * http://www.program-o.com
7 * AUTHOR: Elizabeth Perreau and Dave Morton
9 * DETAILS: Gateway to the admin functions for the script
10 ***************************************/
13 if (!file_exists('../config/global_config.php')) header('location: ../install/install_programo.php');
14 require_once('../config/global_config.php');
16 // set up error logging and display
17 ini_set('log_errors', true);
18 ini_set('error_log', _LOG_PATH_
. 'admin.error.log');
19 ini_set('html_errors', false);
20 ini_set('display_errors', false);
21 set_exception_handler("handle_exceptions");
24 require_once(_LIB_PATH_
. 'PDO_functions.php');
25 require_once(_LIB_PATH_
. 'error_functions.php');
26 require_once(_LIB_PATH_
. 'misc_functions.php');
27 require_once(_LIB_PATH_
. 'template.class.php');
28 require_once(_ADMIN_PATH_
. 'allowedPages.php');
30 // Set session parameters
31 $session_name = 'PGO_Admin';
32 session_name($session_name);
37 $pc = print_r($_GET, true) . "\n" . print_r($_POST, true);
38 $page = filter_input(INPUT_GET
, 'page', FILTER_SANITIZE_STRING
);
39 $page = ($page === false ||
$page === null) ?
'main' : $page;
40 if (!array_key_exists($page, $allowed_pages))
42 $msg = 'Invalid argument!';
44 $filters = $allowed_pages[$page];
45 $post_vars = filter_input_array(INPUT_POST
, $filters);
46 $get_vars = filter_input_array(INPUT_GET
, $filters);
47 $input_vars = array_merge((array) $get_vars, (array) $post_vars);
50 $bot_name = '<b class="red">not selected</b>';
54 // Begin script execution
55 $thisPath = dirname(__FILE__
);
56 $template = new Template("$thisPath/default.page.htm");
57 $githubVersion = getCurrentVersion();
58 $version = ($githubVersion == VERSION
) ?
'Program O version ' . VERSION
: 'Program O ' . $githubVersion . ' is now available. <a href="https://github.com/Program-O/Program-O/archive/master.zip">Click here</a> to download it.';
60 if ($get_vars['page'] == 'logout') logout();
61 $logged_in = getLoginStatus();
66 $curPage = (isset($get_vars['page'])) ?
$get_vars['page'] : 'main';
69 $curPage = ($get_vars['page'] == 'login') ?
login() : 'logout';
72 $name = (isset($_SESSION['poadmin']['name'])) ?
$_SESSION['poadmin']['name'] : '';
73 $ip = (isset($_SESSION['poadmin']['ip'])) ?
$_SESSION['poadmin']['ip'] : '';
74 $last = (isset($_SESSION['poadmin']['last_login'])) ?
$_SESSION['poadmin']['last_login'] : '';
75 $lip = (isset($_SESSION['poadmin']['lip'])) ?
$_SESSION['poadmin']['lip'] : '';
76 $llast = (isset($_SESSION['poadmin']['prior_login'])) ?
$_SESSION['poadmin']['prior_login'] : '';
77 $bot_name = (isset($_SESSION['poadmin']['bot_name'])) ?
$_SESSION['poadmin']['bot_name'] : $bot_name;
78 $bot_id = (isset($_SESSION['poadmin']['bot_id'])) ?
$_SESSION['poadmin']['bot_id'] : 1;
79 $hide_logo = (isset($_SESSION['display'])) ?
$_SESSION['display'] : '';
80 $bot_format = (isset($_SESSION['poadmin']['bot_format'])) ?
$_SESSION['poadmin']['bot_format'] : '';
84 # ordered here in the order that the page is constructed
85 $logo = $template->getSection('Logo');
86 $titleSpan = $template->getSection('TitleSpan');
87 $main = $template->getSection('Main');
89 $mainContent = $template->getSection('LoginForm');
90 $noLeftNav = $template->getSection('NoLeftNav');
91 $noRightNav = $template->getSection('NoRightNav');
92 $navHeader = $template->getSection('NavHeader');
93 $footer = $template->getSection('Footer');
98 $lowerScripts = $template->getSection('LogoLinkScript');
100 $topNavLinks = makeLinks('top', makeTopLinks());
101 $leftNavLinks = makeLinks('left', makeLeftLinks());
102 $mediaType = ' media="screen"';
103 $mainTitle = 'Program O Login';
104 $FooterInfo = '<p>© 2011-2014 My Program-O<br /><a href="http://www.program-o.com">www.program-o.com</a></p>';
106 $pageTitle = 'My-Program O - Login';
111 //if we get to the login page and we are still actually logged in
112 //just destroy the session to prevent weirdness
113 if($curPage == 'login' && !empty($_SESSION['poadmin']['logged_in'])){
117 $_SESSION['poadmin']['curPage'] = $curPage;
118 ($curPage != 'logout' ||
$curPage == 'login') ?
include ("$curPage.php") : false;
120 $bot_format_link = (!empty($bot_format)) ?
"&format=$bot_format" : '';
121 $curPage = (isset($curPage)) ?
$curPage : 'main';
122 $upperScripts .= ($hide_logo == 'HideLogoCSS') ?
$template->getSection('HideLogoCSS') : '';
127 # Build page content from the template
129 $content = $template->getSection('Header');
130 #$content .= "hide_logo = $hide_logo";
131 $content .= $template->getSection('PageBody');
133 # Replace template labels with real data
134 $styleSheet = 'style.css';
135 $errMsgClass = (!empty($msg)) ?
"ShowError" : "HideError";
136 $errMsgStyle = $template->getSection($errMsgClass);
137 $bot_id = ($bot_id == 'new') ?
0 : $bot_id;
139 '[charset]' => $charset,
140 '[myPage]' => $curPage,
141 '[pageTitle]' => $pageTitle,
142 '[styleSheet]' => $styleSheet,
143 '[mediaType]' => $mediaType,
144 '[extraCSS]' => $extraCSS,
145 '[upperScripts]' => $upperScripts,
147 '[pageTitleInfo]' => $pageTitleInfo,
148 '[topNav]' => $topNav,
149 '[leftNav]' => $leftNav,
150 '[rightNav]' => $rightNav,
152 '[footer]' => $footer,
153 '[lowerScripts]' => $lowerScripts,
154 '[titleSpan]' => $titleSpan,
155 '[divDecoration]' => $divDecoration,
156 '[topNavLinks]' => $topNavLinks,
157 '[navHeader]' => $navHeader,
158 '[leftNavLinks]' => $leftNavLinks,
159 '[mainTitle]' => $mainTitle,
160 '[mainContent]' => $mainContent,
161 '[rightNavLinks]' => $rightNavLinks,
162 '[FooterInfo]' => $FooterInfo,
163 '[headerTitle]' => $headerTitle,
165 '[bot_id]' => $bot_id,
166 '[bot_name]' => $bot_name,
167 '[errMsgStyle]' => $errMsgStyle,
168 '[noRightNav]' => $noRightNav,
169 '[noLeftNav]' => $noLeftNav,
170 '[version]' => $version,
171 '[bot_format_link]' => $bot_format_link,
173 foreach ($searches as $search => $replace) {
174 $content = str_replace($search, $replace, $content);
176 $content = str_replace('[myPage]', $curPage, $content);
177 $content = str_replace('[divDecoration]', $divDecoration, $content);
178 $content = str_replace('[blank]', '', $content);
190 function makeLinks($section, $linkArray) {
191 $out = "<!-- making links for section $section -->\n";
192 global $template, $curPage;
193 $curPage = (empty($curPage)) ?
'main' : $curPage;
194 $botName = (isset($_SESSION['poadmin']['bot_name'])) ?
$_SESSION['poadmin']['bot_name'] : '<b class="red">not selected</b>';
195 $botId = (isset($_SESSION['poadmin']['bot_id'])) ?
$_SESSION['poadmin']['bot_id'] : 1;
196 $botId = ($botId == 'new') ?
1 : $botId;
197 # [linkClass][linkHref][linkOnclick][linkAlt][linkTitle]>[linkLabel]
198 $linkText = $template->getSection('NavLink');
199 foreach ($linkArray as $needle) {
201 foreach ($needle as $search => $replace) {
202 $tmp = str_replace($search, $replace, $tmp);
204 $linkClass = $needle['[linkHref]'];
205 $linkClass = str_replace(' href="index.php?page=', '', $linkClass);
206 $linkClass = str_replace('"', '', $linkClass);
207 $curClass = ($linkClass == $curPage) ?
'selected' : 'noClass';
208 if ($curPage == 'main') $curClass = (stripos($linkClass,'main') !== false) ?
'selected' : 'noClass';
209 $tmp = str_replace('[curClass]', $curClass, $tmp);
212 #print "<!-- returning links for section $section:\n\n out = $out\n\n -->\n";
213 $strippedBotName = preg_replace('~\<b class="red"\>(.*?)\</b\>~', '$1', $botName);
214 $out = str_replace('[botId]', $botId, $out);
215 $out = str_replace('[curBot]', $botName, $out);
216 $out = str_replace('[curBot2]', $strippedBotName, $out);
223 * Function makeTopLinks
228 function makeTopLinks() {
232 '[linkClass]' => ' class="[curClass]"',
233 '[linkHref]' => ' href="'.DOCS_URL
.'"',
234 '[linkOnclick]' => '',
235 '[linkAlt]' => ' alt="The Program O User\'s Guide"',
236 '[linkTitle]' => ' title="The Program O User\'s Guide"',
237 '[linkLabel]' => 'Documentation'
240 '[linkClass]' => ' class="[curClass]"',
241 '[linkHref]' => ' href="https://github.com/Program-O/Program-O/issues"',
242 '[linkOnclick]' => '',
243 '[linkAlt]' => ' alt="Bug reporting"',
244 '[linkTitle]' => ' title="Bug reporting"',
245 '[linkLabel]' => 'Bug Reporting'
248 '[linkClass]' => ' class="[curClass]"',
249 '[linkHref]' => ' href="index.php?page=stats"',
250 '[linkOnclick]' => '',
251 '[linkAlt]' => ' alt="Get bot statistics"',
252 '[linkTitle]' => ' title="Get bot statistics"',
253 '[linkLabel]' => 'Stats'
257 '[linkHref]' => ' href="index.php?page=logout"',
258 '[linkOnclick]' => '',
259 '[linkAlt]' => ' alt="Log out"',
260 '[linkTitle]' => ' title="Log out"',
261 '[linkLabel]' => 'Log Out'
268 * Function makeLeftLinks
273 function makeLeftLinks() {
276 '[linkClass]' => ' class="[curClass]"',
277 '[linkHref]' => ' href="index.php?page=select_bots"',
278 '[linkOnclick]' => '',
279 '[linkAlt]' => ' alt="Change or edit the current bot"',
280 '[linkTitle]' => ' title="Change or edit the current bot"',
281 '[linkLabel]' => 'Current Bot: ([curBot])'
284 '[linkClass]' => ' class="[curClass]"',
285 '[linkHref]' => ' href="index.php?page=botpersonality"',
286 '[linkOnclick]' => '',
287 '[linkAlt]' => ' alt="Edit your bot\'s personality"',
288 '[linkTitle]' => ' title="Edit your bot\'s personality"',
289 '[linkLabel]' => 'Bot Personality'
292 '[linkClass]' => ' class="[curClass]"',
293 '[linkHref]' => ' href="index.php?page=logs"',
294 '[linkOnclick]' => '',
295 '[linkAlt]' => ' alt="View the log files"',
296 '[linkTitle]' => ' title="View the log files"',
297 '[linkLabel]' => 'Logs'
300 '[linkClass]' => ' class="[curClass]"',
301 '[linkHref]' => ' href="index.php?page=teach"',
302 '[linkOnclick]' => '',
303 '[linkAlt]' => ' alt="Train your bot"',
304 '[linkTitle]' => ' title="Train your bot"',
305 '[linkLabel]' => 'Teach'
308 '[linkClass]' => ' class="[curClass]"',
309 '[linkHref]' => ' href="index.php?page=upload"',
310 '[linkOnclick]' => '',
311 '[linkAlt]' => ' alt="Upload AIML files"',
312 '[linkTitle]' => ' title="Upload AIML files"',
313 '[linkLabel]' => 'Upload AIML'
316 '[linkClass]' => ' class="[curClass]"',
317 '[linkHref]' => ' href="index.php?page=download"',
318 '[linkOnclick]' => '',
319 '[linkAlt]' => ' alt="Download AIML files"',
320 '[linkTitle]' => ' title="Download AIML files"',
321 '[linkLabel]' => 'Download AIML'
324 '[linkClass]' => ' class="[curClass]"',
325 '[linkHref]' => ' href="index.php?page=clear"',
326 '[linkOnclick]' => '',
327 '[linkAlt]' => ' alt="Clear AIML Categories"',
328 '[linkTitle]' => ' title="Clear AIML Categories"',
329 '[linkLabel]' => 'Clear AIML Categories'
332 '[linkClass]' => ' class="[curClass]"',
333 '[linkHref]' => ' href="index.php?page=spellcheck"',
334 '[linkOnclick]' => '',
335 '[linkAlt]' => ' alt="Edit the SpellCheck entries"',
336 '[linkTitle]' => ' title="Edit the SpellCheck entries"',
337 '[linkLabel]' => 'Spell Check'
340 '[linkClass]' => ' class="[curClass]"',
341 '[linkHref]' => ' href="index.php?page=wordcensor"',
342 '[linkOnclick]' => '',
343 '[linkAlt]' => ' alt="Edit the Word Censor entries"',
344 '[linkTitle]' => ' title="Edit the Word Censor entries"',
345 '[linkLabel]' => 'Word Censor'
348 '[linkClass]' => ' class="[curClass]"',
349 '[linkHref]' => ' href="index.php?page=editAiml"',
350 '[linkOnclick]' => '',
351 '[linkAlt]' => ' alt="Search and edit specific AIML categories"',
352 '[linkTitle]' => ' title="Search and edit specific AIML categories"',
353 '[linkLabel]' => 'Search/Edit AIML'
356 '[linkClass]' => ' class="[curClass]"',
357 '[linkHref]' => ' href="index.php?page=srai_lookup"',
358 '[linkOnclick]' => '',
359 '[linkAlt]' => ' alt="Search and edit entries in the srai_lookup table"',
360 '[linkTitle]' => ' title="Search and edit entries in the srai_lookup table"',
361 '[linkLabel]' => 'SRAI Lookup'
364 '[linkClass]' => ' class="[curClass]"',
365 '[linkHref]' => ' href="index.php?page=demochat"',
366 '[linkOnclick]' => '',
367 '[linkAlt]' => ' alt="Run a demo version of your bot"',
368 '[linkTitle]' => ' title="Run a demo version of your bot"',
369 '[linkLabel]' => 'Test Your Bot'
373 '[linkHref]' => ' href="index.php?page=members"',
374 '[linkOnclick]' => '',
375 '[linkAlt]' => ' alt="Edit Admin Accounts"',
376 '[linkTitle]' => ' title="Edit Admin Accounts"',
377 '[linkLabel]' => 'Edit Admin Accounts'
381 '[linkHref]' => ' href="index.php?page=logout"',
382 '[linkOnclick]' => '',
383 '[linkAlt]' => ' alt="Log out"',
384 '[linkTitle]' => ' title="Log out"',
385 '[linkLabel]' => 'Log Out'
389 '[linkHref]' => ' href="#"',
390 '[linkOnclick]' => ' onclick="toggleLogo(); return false;"',
391 '[linkAlt]' => ' alt="Toggle the Logo"',
392 '[linkTitle]' => ' title="Toggle the Logo"',
393 '[linkLabel]' => 'Toggle the Logo'
397 '[linkHref]' => ' href="' . _BASE_URL_
. '?bot_id=[botId][bot_format_link]"',
398 '[linkOnclick]' => ' target="_blank"',
399 '[linkAlt]' => ' alt="open the page for [curBot] in a new tab/window"',
400 '[linkTitle]' => ' title="open the page for [curBot2] in a new tab/window"',
401 '[linkLabel]' => 'Talk to [curBot]'
410 * Function getCurrentVersion
413 * @return bool|mixed|string
415 function getCurrentVersion()
417 if(isset($_SESSION['GitHubVersion'])) return $_SESSION['GitHubVersion'];
418 $url = 'https://api.github.com/repos/Program-O/Program-O/contents/version.txt';
420 if (function_exists('curl_init'))
423 curl_setopt($ch, CURLOPT_URL
, $url);
424 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, false);
425 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
426 curl_setopt($ch, CURLOPT_USERAGENT
, 'Program-O/Program-O');
427 $out = curl_exec($ch);
428 //if (false === $out) trigger_error('Not sure what it is, but there\'s a problem with checking the current version on GitHub. Maybe this will help: "' . curl_error($ch) . '"');
430 if (false === $out) return VERSION
;
431 $repoArray = json_decode($out, true);
432 //save_file(_LOG_PATH_ . 'repoArray.txt', print_r($repoArray, true));
433 if (!isset($repoArray['content'])) return VERSION
;
434 $versionB64 = $repoArray['content'];
435 $version = base64_decode($versionB64);
436 #save_file(_DEBUG_PATH_ . 'version.txt', "out = " . print_r($out, true) . "\r\nVersion = $versionB64 = $version");
439 $_SESSION['GitHubVersion'] = $out;
440 return ($out !== false) ?
$out : VERSION
;
444 * Function handle_exceptions
446 * * @param exception $e
449 function handle_exceptions(exception
$e)
452 $trace = $e->getTrace();
453 file_put_contents(_LOG_PATH_
. 'admin.exception.log', print_r($trace, true), FILE_APPEND
);
454 $msg .= $e->getMessage();
460 global $post_vars, $get_vars, $dbConn, $msg;
461 if((!isset($post_vars['user_name'])) ||
(!isset($post_vars['pw']))) return 'logout';
462 //$_SESSION['poadmin']['display'] = $hide_logo;
463 $user_name = $post_vars['user_name'];
464 $pw_hash = md5($post_vars['pw']);
465 $sql = "SELECT * FROM `myprogramo` WHERE user_name = :user_name AND password = :pw_hash";
466 $params = array(':user_name' => $user_name, ':pw_hash' => $pw_hash);
467 $row = db_fetch($sql, $params, __FILE__
, __FUNCTION__
, __LINE__
);
469 $_SESSION['poadmin']['uid'] = $row['id'];
470 $_SESSION['poadmin']['name'] = $row['user_name'];
471 $_SESSION['poadmin']['lip']=$row['last_ip'];
472 $_SESSION['poadmin']['prior_login']=date('l jS \of F Y h:i:s A', strtotime($row['last_login']));
475 case (!empty($_SERVER['HTTP_CLIENT_IP'])):
476 $ip = $_SERVER['HTTP_CLIENT_IP'];
478 case (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])):
479 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
482 $ip = $_SERVER['REMOTE_ADDR'];
485 $sql = "UPDATE `myprogramo` SET `last_ip` = :ip, `last_login` = CURRENT_TIMESTAMP WHERE user_name = :user_name limit 1";
486 $params = array(':ip' => $ip, ':user_name' => $user_name);
487 $transact = db_write($sql, $params, false, __FILE__
, __FUNCTION__
, __LINE__
);
488 $_SESSION['poadmin']['ip'] = $ip;
489 $_SESSION['poadmin']['last_login'] = date('l jS \of F Y h:i:s A');
491 $sql = "SELECT * FROM `bots` WHERE bot_active = '1' ORDER BY bot_id ASC LIMIT 1";
492 $row = db_fetch($sql, null, __FILE__
, __FUNCTION__
, __LINE__
);
493 $count = count($row);
495 $_SESSION['poadmin']['bot_id'] = $row['bot_id'];
496 $_SESSION['poadmin']['bot_name'] = $row['bot_name'];
499 $_SESSION['poadmin']['bot_id'] = -1;
500 $_SESSION['poadmin']['bot_name'] = "unknown";
504 $msg .= "incorrect username/password<br>\n";
508 $_SESSION['poadmin']['logged_in'] = true;
509 header('Location: index.php');
517 global $session_name, $session_cookie_domain, $session_cookie_path;
520 setcookie($session_name, '', time()-3600, $session_cookie_path, $session_cookie_domain, false, false);
521 header('Location: ./');
525 function getLoginStatus()
527 return (isset($_SESSION['poadmin']['logged_in']) && $_SESSION['poadmin']['logged_in'] === true) ?
true : false;