<?php
$mysql_server = 'localhost';
$mysql_username = 'root';
$mysql_password = 'root';
$mysql_database = 'cms_design';
$mysql_table = 'pages';
$cms_no_results = 'No results';
$cms_content = '';
$cms_name = '';
$cms_views = '';
$cms_created_by = '';
$cms_create_date = '';
$cms_last_update_date = '';
$cms_extra_data = '';
$db = mysql_connect($mysql_server, $mysql_username, $mysql_password);
if (!$db)
{
   die('Failed to connect to database server!<br>'.mysql_error());
}
mysql_select_db($mysql_database, $db) or die('Failed to select database<br>'.mysql_error());
$ajax = isset($_REQUEST['ajax']) ? $_REQUEST['ajax'] : 'false';
$label = isset($_REQUEST['label']) ? $_REQUEST['label'] : '';
$id = isset($_REQUEST['page']) ? mysql_real_escape_string($_REQUEST['page']) : '';
$query = isset($_REQUEST['query']) ? $_REQUEST['query'] : '';
if (!empty($query))
{
   if (!get_magic_quotes_gpc())
   {
      $query = addslashes($query);
   }
   $words = preg_split('/\s+/', $query);
   foreach ($words as $word)
   {
      $word = preg_replace('/\W/', '', $word);
      if (strlen($word) > 1)
      {
         $terms[] = $word;
      }
   }
   if ($terms)
   {
      if (count($terms) > 4)
      {
         array_splice($terms, 4);
      }
      $sql = "SELECT p.id, p.name, p.content FROM " . $mysql_table . "PAGES p, " . $mysql_table . "SEARCH_WORDS w, " . $mysql_table . "SEARCH_WORDMATCH m WHERE(";
      $where = "w.word LIKE '%" . array_shift($terms) . "%'";
      while ($term = array_shift($terms))
      {
         $where .= " OR w.word LIKE '%" . $term . "%'";
      }
      $sql .= $where . ") AND m.word_id = w.id AND m.page_id = p.id GROUP BY p.id ORDER BY p.last_update_date DESC";
      $result = mysql_query($sql) or die(mysql_error());
      if ($aantal = mysql_num_rows($result))
      {
         $cms_content .= "  <ol class=\"searchresults\">\n";
         while ($data = mysql_fetch_array($result))
         {
            $cms_content .= "   <li><a href=\"" . basename(__FILE__) . "?page=" . $data['id'] . "\">" . $data['name'] . "</a><br>";
            $contents = substr(strip_tags($data['content']), 0, 200);
            if (strlen($contents) > 199)
            {
               $contents .= "...";
            }
            $cms_content .= $contents;
            $cms_content .= "</li>\n";
         }
         $cms_content .= "  </ol>\n";
      }
      else
      {
         $cms_content = $cms_no_results;
      }
   }
   else
   {
      $cms_content = $cms_no_results;
   }
}
else
{
   if (!empty($id))
   {
      $sql = "SELECT * FROM " . $mysql_table . "PAGES WHERE id = '$id'";
   }
   else
   {
      $sql = "SELECT * FROM " . $mysql_table . "PAGES WHERE home = 1";
   }
   $result = mysql_query($sql, $db);
   if ($result)
   {
      if ($data = mysql_fetch_array($result))
      {
         if (!empty($data['url']))
         {
            $cms_content = "<iframe name=\"cmscontent\" style=\"position:absolute;border-width:0;width:100%;height:100%;\" src=\"" . $data['url'] . "\"></iframe>\n";
         }
         else
         {
            $cms_content = $data['content'];
         }
         $cms_name = $data['name'];
         $cms_views = $data['views'];
         $cms_created_by = $data['created_by'];
         $cms_create_date = $data['create_date'];
         $cms_last_update_date = $data['last_update_date'];
         $cms_extra_data = $data['extra_data'];
         if ($label == 'CmsLabel1')
         {
            $cms_content = "$cms_name";
         }
         else
         if ($label == 'CmsLabel2')
         {
            $cms_content = "Last modified: $cms_last_update_date";
         }
      }
      if (empty($label))
      {
         if (!empty($id))
         {
            $sql = "UPDATE " . $mysql_table . "PAGES SET views=views+1 WHERE id = '$id'";
         }
         else
         {
            $sql = "UPDATE " . $mysql_table . "PAGES SET views=views+1 WHERE home = 1";
         }
         mysql_query($sql, $db);
      }
   }
}
mysql_close($db);
if ($ajax == 'true')
{
   echo $cms_content;
   exit;
}
?>
<!doctype html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Administrator</title>
<meta name="generator" content="Francesco De Stefano">

<link rel="stylesheet" href="./cms_design.css" type="text/css">
<link rel="stylesheet" href="./content.css" type="text/css">

<script type="text/javascript" src="./jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="./wb.canvas.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
   $("#Shape5").DrawRectangle(
   {
      radius: 20,
      width: 798,
      height: 650,
      strokeStyle: '#C0C0C0',
      lineWidth: 2,
      fillStyle: '#FFFFFF'
   });
   $("#Shape2").DrawRectangle(
   {
      radius: 20,
      width: 206,
      height: 566,
      strokeStyle: '#C0C0C0',
      lineWidth: 2,
      fillStyle: 'gradient',
      gradientMode: 0,
      gradientColorStops: [0.00,"#FFFFFF",1.00,"#FFFFE0"]
   });
   $("#Shape3").DrawRectangle(
   {
      radius: 20,
      width: 798,
      height: 111,
      strokeStyle: '#C0C0C0',
      lineWidth: 2,
      fillStyle: '#FFFFE0'
   });
   $("#Shape1").DrawRectangle(
   {
      radius: 20,
      width: 513,
      height: 81,
      strokeStyle: '#C0C0C0',
      lineWidth: 2,
      fillStyle: 'gradient',
      gradientMode: 0,
      gradientColorStops: [0.00,"#FFFFFF",1.00,"#FFFFE0"]
   });
   $("#Shape4").DrawRectangle(
   {
      radius: 20,
      width: 460,
      height: 134,
      strokeStyle: '#C0C0C0',
      lineWidth: 2,
      fillStyle: '#FFFFE0'
   });
   $.ajaxSetup ({cache: false});
   $("#wb_CmsMenu1 a").click(function(e)
   {
      e.preventDefault();
      $('#wb_CmsView1').load('<?php echo basename(__FILE__);?>', 'ajax=true&page=' +  $(this).attr('id'));
      $('#CmsLabel1').load('<?php echo basename(__FILE__);?>', 'ajax=true&label=CmsLabel1&page=' +  $(this).attr('id'));
      $('#CmsLabel2').load('<?php echo basename(__FILE__);?>', 'ajax=true&label=CmsLabel2&page=' +  $(this).attr('id'));
   });
   var $search = $('#CmsSearch1_form');
   var $searchInput = $search.find('input');
   var $searchLabel = $search.find('label');
   if ($searchInput.val())
   {
      $searchLabel.hide();
   }
   $searchInput.focus(function()
   {
      $searchLabel.hide();
   }).blur(function()
   {
      if (this.value == '')
      {
         $searchLabel.show();
      }
   });
   $searchLabel.click(function()
   {
      $searchInput.trigger('focus');
   });

   $("#CmsSearch1_form").submit(function(e)
   {
      e.preventDefault();
      $('#wb_CmsView1').load('<?php echo basename(__FILE__);?>', 'ajax=true&query=' +  $('#CmsSearch1_keyword').val());
   });
   $("#Line1").DrawLine(
   {
      points: [32,156,0,0],
      strokeStyle: "#000000",
      lineWidth: 1.0
   });
   $("#Line2").DrawLine(
   {
      points: [0,0,71,23],
      strokeStyle: "#000000",
      lineWidth: 1.0
   });
   $("#Line3").DrawLine(
   {
      points: [17,0,0,26],
      strokeStyle: "#000000",
      lineWidth: 1.0
   });
   $("#Line4").DrawLine(
   {
      points: [0,0,13,20],
      strokeStyle: "#000000",
      lineWidth: 1.0
   });
});
</script>
</head>
<body>
<div id="container">
<div id="wb_MasterPage1" style="position:absolute;left:0px;top:0px;width:799px;height:834px;z-index:9;padding:0;">
<div id="wb_Shape5" style="position:absolute;left:1px;top:151px;width:798px;height:650px;z-index:1;padding:0;">
<canvas id="Shape5" height="650" width="798"></canvas></div>
<div id="wb_Shape2" style="position:absolute;left:28px;top:202px;width:206px;height:566px;z-index:2;padding:0;">
<canvas id="Shape2" height="566" width="206"></canvas></div>
<div id="wb_Text8" style="position:absolute;left:0px;top:805px;width:793px;height:29px;text-align:center;z-index:3;">
<span style="color:#282828;font-family:Tahoma;font-size:11px;">Copyright © 2012&nbsp; by &quot;Francesco De Stefano&quot;&nbsp;&nbsp; &#183;&nbsp;&nbsp; All Rights reserved&nbsp; &#183;&nbsp; <a href=" http://www.mediamaster.eu" class="linkstyle"> http://www.mediamaster.eu</a></span><span style="color:#000000;font-family:Arial;font-size:13px;"><br></span></div>
<div id="wb_Shape3" style="position:absolute;left:1px;top:25px;width:798px;height:111px;z-index:4;padding:0;">
<canvas id="Shape3" height="111" width="798"></canvas></div>
<div id="wb_Shape1" style="position:absolute;left:258px;top:202px;width:513px;height:81px;z-index:5;padding:0;">
<canvas id="Shape1" height="81" width="513"></canvas></div>
<div id="wb_Shape4" style="position:absolute;left:28px;top:52px;width:460px;height:134px;z-index:6;padding:0;">
<canvas id="Shape4" height="134" width="460"></canvas></div>
<div id="wb_Text4" style="position:absolute;left:46px;top:78px;width:427px;height:73px;z-index:7;">
<span style="color:#7F7F7F;font-family:Arial;font-size:35px;"><strong>CMS Design Tutorial<br></strong></span><span style="color:#7F7F7F;font-family:Arial;font-size:27px;"><strong>www.mediamaster.eu</strong></span></div>
</div>
<div id="CmsLabel1" style="position:absolute;left:269px;top:220px;width:483px;height:33px;z-index:10;"><?php echo "$cms_name"; ?></div>
<div id="CmsLabel2" style="position:absolute;left:514px;top:760px;width:249px;height:26px;z-index:11;"><?php echo "Last modified: $cms_last_update_date"; ?></div>
<div id="wb_CmsView1" style="position:absolute;left:259px;top:297px;width:500px;height:460px;z-index:12;overflow-y:auto;">
<?php
   echo $cms_content;
?>
</div>
<div id="wb_CmsMenu1" style="position:absolute;left:51px;top:225px;width:155px;height:150px;z-index:13;padding:0;">
<?php
$db = mysql_connect($mysql_server, $mysql_username, $mysql_password);
if (!$db)
{
   die('Failed to connect to database server!<br>'.mysql_error());
}
mysql_select_db($mysql_database, $db) or die('Failed to select database<br>'.mysql_error());
$sql = "SELECT id, name FROM " . $mysql_table . "PAGES WHERE visible = 1 ORDER BY menu_index ASC";
$result = mysql_query($sql, $db);
while ($data = mysql_fetch_array($result))
{
   echo "<a href=\"" . basename(__FILE__) . "?page=" . $data['id'] . "\" id=\"" . $data['id'] . "\">" . $data['name'] . "</a>\n";
}
mysql_close($db);
?>
</div>
<div id="wb_CmsSearch1" style="position:absolute;left:554px;top:88px;width:202px;height:23px;z-index:14;padding:0;">
<form method="get" name="CmsSearch1_form" id="CmsSearch1_form" action="<?php echo basename(__FILE__); ?>">
<input type="text" id="CmsSearch1_keyword" style="position:absolute;left:0px;top:0px;width:202px;height:23px;line-height:23px;;" name="query" value="">
<label id="CmsSearch1_label" style="position:absolute;left:7px;top:6px;" for="CmsSearch1_keyword"> Search article</label>
</form>
</div>
<div id="wb_Text1" style="position:absolute;left:55px;top:494px;width:123px;height:56px;z-index:15;">
<span style="color:#000000;font-family:Arial;font-size:11px;">This menu will be filled based on the online page structure of the CMS.</span></div>
<div id="wb_Line1" style="position:absolute;left:73px;top:325px;width:32px;height:156px;z-index:16;padding:0;">
<canvas id="Line1" height="156" width="32"></canvas></div>
<div id="wb_Text2" style="position:absolute;left:307px;top:453px;width:414px;height:14px;text-align:center;z-index:17;">
<span style="color:#000000;font-family:Arial;font-size:11px;">The content of the current page will be displayed inside the CMS View.</span></div>
<div id="wb_Text3" style="position:absolute;left:335px;top:716px;width:247px;height:28px;z-index:18;">
<span style="color:#000000;font-family:Arial;font-size:11px;">This is a CMS label which will display the last updated date for the current page</span></div>
<div id="wb_Line2" style="position:absolute;left:536px;top:734px;width:71px;height:23px;z-index:19;padding:0;">
<canvas id="Line2" height="23" width="71"></canvas></div>
<div id="wb_Text4" style="position:absolute;left:458px;top:179px;width:341px;height:14px;z-index:20;">
<span style="color:#000000;font-family:Arial;font-size:11px;">This is a CMS label which will display the name of the current page.</span></div>
<div id="wb_Line3" style="position:absolute;left:554px;top:193px;width:17px;height:26px;z-index:21;padding:0;">
<canvas id="Line3" height="26" width="17"></canvas></div>
<div id="wb_Text5" style="position:absolute;left:503px;top:46px;width:153px;height:14px;z-index:22;">
<span style="color:#000000;font-family:Arial;font-size:11px;">This is a CMS search field</span></div>
<div id="wb_Line4" style="position:absolute;left:625px;top:58px;width:13px;height:20px;z-index:23;padding:0;">
<canvas id="Line4" height="20" width="13"></canvas></div>
</div>
</body>
</html>