Index: admin/plugin.php
===================================================================
--- admin/plugin.php	(revision 270)
+++ admin/plugin.php	(revision 1070)
@@ -15,77 +15,97 @@
 dcPage::check('usage,contentadmin');
 
+//* TODO: Do it better later, required by some javascripts
+$some_globals = array(
+	'rtl' => l10n::getTextDirection($_lang) == 'rtl',
+	'Nonce' => $core->getNonce(),
+	'sess_id' => session_id(),
+	'sess_uid' => $_SESSION['sess_browser_uid'],
+	'media_manage' => $core->auth->check('media,media_admin',$core->blog->id),
+	'enable_wysiwyg' => isset($core->auth) && $core->auth->getOption('enable_wysiwyg'),
+	'edit_size' => $core->auth->getOption('edit_size')
+);
+foreach($some_globals as $name => $value) {
+	$_ctx->$name = $value;
+};
+//*/
+
+$has_content = false;
 $p_file = '';
 $p = !empty($_REQUEST['p']) ? $_REQUEST['p'] : null;
-$popup = (integer) !empty($_REQUEST['popup']);
-
-if ($popup) {
-	$open_f = array('dcPage','openPopup');
-	$close_f = array('dcPage','closePopup');
-} else {
-	$open_f = array('dcPage','open');
-	$close_f = array('dcPage','close');
-}
+$popup = $_ctx->popup = (integer) !empty($_REQUEST['popup']);
 
 if ($core->plugins->moduleExists($p)) {
 	$p_file = $core->plugins->moduleRoot($p).'/index.php';
 }
+if (file_exists($p_file)) {
 
-if (file_exists($p_file))
-{
-	# Loading plugin
+//* Keep this for old style plugins using dcPage
+	if ($popup) {
+		$open_f = array('dcPage','openPopup');
+		$close_f = array('dcPage','closePopup');
+	} else {
+		$open_f = array('dcPage','open');
+		$close_f = array('dcPage','close');
+	}
+	
 	$p_info = $core->plugins->getModules($p);
-	
 	$p_url = 'plugin.php?p='.$p;
-	
-	$p_title = 'no content - plugin';
-	$p_head = '';
-	$p_content = '<p>'.__('No content found on this plugin.').'</p>';
-	
+	$p_title = $p_head = $p_content = '';
+//*/	
+	# Get page content
 	ob_start();
 	include $p_file;
 	$res = ob_get_contents();
 	ob_end_clean();
-	
-	if (preg_match('|<head>(.*?)</head|ms',$res,$m)) {
-		if (preg_match('|<title>(.*?)</title>|ms',$m[1],$mt)) {
-			$p_title = $mt[1];
-		}
+
+//* Keep this for old style plugins using dcPage
+	if (!$_ctx->hasPageTitle()) {
 		
-		if (preg_match_all('|(<script.*?>.*?</script>)|ms',$m[1],$ms)) {
-			foreach ($ms[1] as $v) {
-				$p_head .= $v."\n";
+		if (preg_match('|<head>(.*?)</head|ms',$res,$m)) {
+			if (preg_match('|<title>(.*?)</title>|ms',$m[1],$mt)) {
+				$p_title = $mt[1];
+			}
+			
+			if (preg_match_all('|(<script.*?>.*?</script>)|ms',$m[1],$ms)) {
+				foreach ($ms[1] as $v) {
+					$p_head .= $v."\n";
+				}
+			}
+			
+			if (preg_match_all('|(<style.*?>.*?</style>)|ms',$m[1],$ms)) {
+				foreach ($ms[1] as $v) {
+					$p_head .= $v."\n";
+				}
+			}
+			
+			if (preg_match_all('|(<link.*?/>)|ms',$m[1],$ms)) {
+				foreach ($ms[1] as $v) {
+					$p_head .= $v."\n";
+				}
 			}
 		}
 		
-		if (preg_match_all('|(<style.*?>.*?</style>)|ms',$m[1],$ms)) {
-			foreach ($ms[1] as $v) {
-				$p_head .= $v."\n";
-			}
-		}
-		
-		if (preg_match_all('|(<link.*?/>)|ms',$m[1],$ms)) {
-			foreach ($ms[1] as $v) {
-				$p_head .= $v."\n";
-			}
+		if (preg_match('|<body.*?>(.+)</body>|ms',$res,$m)) {
+			$p_content = $m[1];
+			
+			call_user_func($open_f,$p_title,$p_head);
+			echo $p_content;
+			call_user_func($close_f);
+			
+			$has_content = true;
 		}
 	}
-	
-	if (preg_match('|<body.*?>(.+)</body>|ms',$res,$m)) {
-		$p_content = $m[1];
+//*/
+	# Check context and display
+	if ($_ctx->hasPageTitle() && !empty($res)) {
+		$has_content = true;
+		echo $res;
 	}
-	
-	call_user_func($open_f,$p_title,$p_head);
-	echo $p_content;
-	call_user_func($close_f);
 }
-else
-{
-	call_user_func($open_f,__('Plugin not found'));
-	
-	echo '<h2>'.__('Plugin not found').'</h2>';
-	
-	echo '<p>'.__('The plugin you reached does not exist or does not have an admin page.').'</p>';
-	
-	call_user_func($close_f);
+# No plugin or content found
+if (!$has_content) {
+	$_ctx->setPageTitle(__('Plugin not found'));
+	$_ctx->addError(__('The plugin you reached does not exist or does not have an admin page.'));
+	$core->tpl->display('plugin.html.twig');
 }
 ?>
