diff --git a/components/menu.php b/components/menu.php
index f4b1975a3fc3daee7c13ce81a69773b1e2321d24..1677708a67fe707d4f82f070e68bffa9b5ab8414 100644
--- a/components/menu.php
+++ b/components/menu.php
@@ -7,7 +7,7 @@ function getPageID($name) {
 
 function translate($word) {
 	global $language, $DB;
-	
+
 	if ($language['code']!="en") {
 		$query=$DB->GetRow("select title, title".$language['db']." from menu where title='".$word."'");
 		if ($query['title'.$language['db']]!="") return $query['title'.$language['db']];
@@ -60,9 +60,25 @@ $res = $DB->GetAll($query);
 $menu = parseMenu($res);
 
 // this needs to choose the actual current one
-$res = $DB->GetAll("select * from menu where parent=".getPageID($pagename)." order by menuorder");
-if (count($res)>0) {
-	$submenu = parseMenu($res);
+// subpages/submenu items can have permissions attached to them as well!
+
+$query2  = "select * from menu where parent=";
+$query2 .= "'";
+$query2 .= getPageID($pagename);
+$query2 .= "'";
+$query2 .= " and (permission is NULL";
+
+if ($session->loggedin) $query2 .= " or permission='users'";
+
+foreach ($session->groups as $group => $value) {
+        $query2 .= " or permission='$group'";
+        }
+
+$query2 .= ") order by menuorder";
+
+$res2 = $DB->GetAll($query2);
+if (count($res2)>0) {
+	$submenu = parseMenu($res2);
 	$menu[translate($pagename)] = $submenu;
 }