1<?php
2/*
3 * e107 website system
4 *
5 * Copyright (C) 2008-2013 e107 Inc (e107.org)
6 * Released under the terms and conditions of the
7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
8 *
9 */
10
11if (!defined('e107_INIT')) { exit; }
12
13$loginPrefs = e107::getConfig('menu')->getPref('login_menu');
14
15if(defined('FPW_ACTIVE'))
16{
17	return;      // prevent failed login attempts when fpw.php is loaded before this menu.
18}
19
20global $eMenuActive, $pref, $e107, $use_imagecode, $ADMIN_DIRECTORY, $LOGIN_MENU_MESSAGE, $LOGIN_MENU_STATITEM, $LM_STATITEM_SEPARATOR,
21       $login_menu_shortcodes, $LOGIN_MENU_FORM, $LOGIN_MENU_LOGGED, $LOGIN_MENU_STATS, $LOGIN_MENU_EXTERNAL_LINK; //FIXME
22
23$sql = e107::getDb();
24$tp = e107::getParser();
25$ns = e107::getRender();
26$ip = e107::getIPHandler()->getIP(FALSE);
27
28//shortcodes
29$login_menu_shortcodes = e107::getScBatch('login_menu',TRUE);
30
31//Bullet
32if(defined("BULLET"))
33{
34	$bullet = "<img src='".THEME_ABS."images/".BULLET."' alt='' class='icon' />";
35	$bullet_src = THEME_ABS."images/".BULLET;
36}
37elseif(file_exists(THEME."images/bullet2.gif"))
38{
39	$bullet = "<img src='".THEME_ABS."images/bullet2.gif' alt='bullet' class='icon' />";
40	$bullet_src = THEME_ABS."images/bullet2.gif";
41}
42else
43{
44	$bullet = "";
45	$bullet_src = "";
46}
47
48//Corrup cookie - template? - TODO
49if (defined('CORRUPT_COOKIE') && CORRUPT_COOKIE == TRUE)
50{
51	$text = "<div class='core-sysmsg loginbox'>".LAN_LOGINMENU_7."<br /><br />
52	{$bullet} <a href='".SITEURL."index.php?logout'>".LAN_LOGOUT."</a></div>";
53	$ns->tablerender(LAN_LOGINMENU_9, $text, 'login_error');
54}
55
56//Image code
57$use_imagecode = ($pref['logcode'] && extension_loaded('gd'));
58
59if ($use_imagecode)
60{
61	global $sec_img;
62	include_once(e_HANDLER.'secure_img_handler.php');
63	$sec_img = new secure_image;
64}
65
66$text = '';
67
68// START LOGGED CODE
69if (USER == TRUE || ADMIN == TRUE)
70{
71    require_once(e_PLUGIN."login_menu/login_menu_class.php");
72	$loginClass = new login_menu_class();
73
74    //login class ??? - TODO
75	if ($sql->select('online', 'online_ip', "`online_ip` = '{$ip}' AND `online_user_id` = '0' "))
76	{	// User now logged in - delete 'guest' record (tough if several users on same IP)
77		$sql->delete('online', "`online_ip` = '{$ip}' AND `online_user_id` = '0' ");
78	}
79
80	//get templates
81    if (!isset($LOGIN_MENU_LOGGED))
82	{
83		if (file_exists(THEME.'templates/login_menu/login_menu_template.php')) // Preferred v2.x location.
84		{
85	   		require(THEME.'templates/login_menu/login_menu_template.php');
86		}
87		elseif(file_exists(THEME.'login_menu_template.php'))
88		{
89	   		require(THEME.'login_menu_template.php');
90		}
91		{
92			require(e_PLUGIN.'login_menu/login_menu_template.php');
93		}
94	}
95	if(!$LOGIN_MENU_LOGGED)
96	{
97    	require(e_PLUGIN.'login_menu/login_menu_template.php');
98	}
99
100
101    //prepare
102	$new_total = 0;
103	$time = USERLV;
104	$menu_data = array();
105
106		// ------------ News Stats -----------
107
108		if (vartrue($loginPrefs['new_news']))
109		{
110			$nobody_regexp = "'(^|,)(".str_replace(",", "|", e_UC_NOBODY).")(,|$)'";
111            $menu_data['new_news'] = $sql->count("news", "(*)", "WHERE `news_datestamp` > {$time} AND news_class REGEXP '".e_CLASS_REGEXP."' AND NOT (news_class REGEXP ".$nobody_regexp.")");
112			$new_total += $menu_data['new_news'];
113		}
114
115		// ------------ Comments Stats -----------
116
117		if (vartrue($loginPrefs['new_comments']))
118		{
119			$menu_data['new_comments'] = $sql->count('comments', '(*)', 'WHERE `comment_datestamp` > '.$time);
120			$new_total += $menu_data['new_comments'];
121		}
122
123		// ------------ Member Stats -----------
124
125		if (vartrue($loginPrefs['new_members']))
126        {
127			$menu_data['new_users'] = $sql->count('user', '(user_join)', 'WHERE user_join > '.$time);
128			$new_total += $menu_data['new_users'];
129		}
130
131		// ------------ Enable stats / other ---------------
132
133		$menu_data['enable_stats'] = $menu_data || vartrue($loginPrefs['external_stats']) ? true : false;
134		$menu_data['new_total'] = $new_total + $loginClass->get_stats_total();
135		$menu_data['link_bullet'] = $bullet;
136		$menu_data['link_bullet_src'] = $bullet_src;
137
138		// ------------ List New Link ---------------
139
140		$menu_data['listnew_link'] = '';
141		if ($menu_data['new_total'] && array_key_exists('list_new', $pref['plug_installed']))
142        {
143            $menu_data['listnew_link'] = e_PLUGIN_ABS.'list_new/list.php?new';
144		}
145
146		// ------------ Pass the data & parse ------------
147		cachevars('login_menu_data', $menu_data);
148		$text = $tp->parseTemplate($LOGIN_MENU_LOGGED, true, $login_menu_shortcodes);
149
150    //menu caption
151	if (file_exists(THEME.'images/login_menu.png')) {
152		$caption = '<img src="'.THEME_ABS.'images/login_menu.png" alt="" />'.LAN_LOGINMENU_5.' '.USERNAME;
153	} else {
154		$caption = LAN_LOGINMENU_5.' '.USERNAME;
155	}
156
157
158	$ns->tablerender($caption, $text, 'login');
159
160// END LOGGED CODE
161}
162// START NOT LOGGED CODE
163else
164{
165	if (intval($pref['user_reg']) === 0)
166	{
167		return;
168	}
169
170
171    //get templates
172	if (!$LOGIN_MENU_FORM || !$LOGIN_MENU_MESSAGE) {
173		if (file_exists(THEME.'templates/login_menu/login_menu_template.php')) // Preferred v2.x location.
174		{
175	   		require(THEME.'templates/login_menu/login_menu_template.php');
176		}
177		elseif (file_exists(THEME."login_menu_template.php")){
178	   		require_once(THEME."login_menu_template.php");
179		}else{
180			require_once(e_PLUGIN."login_menu/login_menu_template.php");
181		}
182	}
183	if(!$LOGIN_MENU_FORM || !$LOGIN_MENU_MESSAGE){
184    	require(e_PLUGIN."login_menu/login_menu_template.php");
185	}
186
187	$text = '<form method="post" action="'.e_SELF.(e_QUERY ? '?'.e_QUERY : '');
188	if (vartrue($pref['password_CHAP'],0))
189	{
190	  $text .= '" onsubmit="hashLoginPassword(this)';
191	}
192	$text .= '">'.$tp->parseTemplate($LOGIN_MENU_FORM, true, $login_menu_shortcodes);
193	$text .= '</form>';
194
195	if (file_exists(THEME.'images/login_menu.png')) {
196		$caption = '<img src="'.THEME_ABS.'images/login_menu.png" alt="" />'.LAN_LOGINMENU_5;
197	} else {
198		$caption = LAN_LOGINMENU_5;
199	}
200
201	$ns->tablerender($caption, $text, 'login');
202}
203// END NOT LOGGED CODE
204
205