<?php
$module_css_path = ($line_domain['theme'] != "" && $line_domain['theme'] != "default") ? ($path . ('assets/themes/' . $line_domain['theme'] . '/')) : $path;
$optres['css'][] = $path . "assets/css/bootstrap-dialog.min.css";
$optres['js'][] = $path . "assets/js/bootstrap-dialog.min.js";
$optres['js'][] = $path . "$modules_dir_name/main_article/js/main_article.js";

/**
 * This function tries to fix the problem where a string with stripped html tags
 * contains glued words from adjacent sentences not properly ending with a dot.
 */
if(!function_exists("fix_stripped_words")) {
	function fix_stripped_words($str) {
		if(!class_exists("html2text")) {
			require_once( dirname(__FILE__) .  "/../classes/extras/html2text.class.php");
		}

		$h2t = new html2text($str);
	
		$text = $h2t->get_text();
		$text = preg_replace("/[\r\n\t]{1,}/", " ", $text);
		$text = preg_replace('/\s+/u', ' ', $text);
		return $text;
		
	}
}


// Check if it is a middle_content bound article
$middle_bound = false;
$dcategs = [$domain_id];
$qd = "SELECT catdom_id FROM domaincategories WHERE parent_id='$domain_id'";
$rd = @mysql_query($qd);
if($rd && mysql_num_rows($rd) > 0) {
    while($rowd = mysql_fetch_assoc($rd)) {
        $dcategs [] = intval($rowd['catdom_id']);
    }
}

$dcategs_str = implode(",", $dcategs);
$q = "SELECT * FROM left_content WHERE catdom_id IN ($dcategs_str) AND module_id='$middle_article_id' AND `type`='main_article' AND location='middle';";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    if(isset($_REQUEST['debug_404'])) {
        while ($row = mysql_fetch_assoc($r)) {
            echo '<pre>', var_export($row, true), '</pre>';
        }
    }
    $middle_bound = true;
}


$breads = false;

if(!isset($_GET['page'])) {
	if(!function_exists("show_article_breadcrumbs")) {
		function show_article_breadcrumbs($parent_id){
			$display_bread = false;
			$ret = array();
			$newparent = $parent_id;
			$root = $newparent;
			$i=0;
			$go_up = true;
			$query = "SELECT * FROM main_article_categ WHERE cat_id = '$parent_id'";
			$res = mysql_query ($query);
			$line = mysql_fetch_array($res);
			${"root$i"}=$line["title_en"];
			${"friendly$i"}=$line["friendly_url"];
			${"link$i"}=$line["cat_id"];
			if($line['is_root'] == 1) {
				$go_up = false;
				$display_bread = true;
			}
			while($go_up && $root>0){
				$newparent=$line["parent_id"];
				$query = "SELECT * FROM  main_article_categ WHERE cat_id = '$newparent'";
				$result = mysql_query ($query);
				$line = mysql_fetch_array($result);
				$i++;
				${"root$i"}=$line["title_en"];
				${"friendly$i"}=$line["friendly_url"];
				${"link$i"}=$line["cat_id"];
				$root=$newparent;
				
				if($line['is_root'] == 1) {
					$display_bread = true;
					${"root$i"} = "[home]" . stripslashes(trim($line["title_en"]));
					$go_up = false;
				}
			}
			for($j=($i);$j>=0; $j = $j-1){
				if($j>=0){
					//if($j != $i) $ret .= '<strong> &raquo; </strong>';
					$class= "blue";
					if(strpos(${"root$j"},"[home]") !== false) {
						${"root$j"} = str_replace("[home]", "", ${"root$j"});
						$class = "blue_home";
					}
					//$ret [] = '<a href="articles/'.${"friendly$j"} . ".html" .'" class="' . $class . '" itemprop="breadcrumb">' . ${"root$j"} . '</a>';
                    $ret[] = array(
                        "link"  => 'articles/' . ${"friendly$j"} . '.html',
                        "title" => ${"root$j"}
                    );
				}
				
			}
			
			if($display_bread) return $ret;
			else return false;
		}
	}
	
	$query = "SELECT * FROM  main_article WHERE article_id = '$middle_article_id' AND active='yes'";
	//echo $query;
	$result = @mysql_query ($query);
	if($result && mysql_num_rows($result)>0) {
		$line = mysql_fetch_assoc($result);
		$parent_id = $line['parent_id'];

        $article_friendly_url = stripslashes($line['friendly_url']);
        $article_canonical_link = $base_url . "/article/$article_friendly_url.html";
        if( strpos($_SESSION['current_url'], $article_canonical_link) !== false  && $middle_bound == true) {

            if(isset($_REQUEST['debug_404'])) {
                echo $article_canonical_link;
                exit;
            }

            $send404 = true;
        }

        $breads = show_article_breadcrumbs($parent_id);
        if($breads && count($breads) > 0) {
            foreach($breads as $bread) {
                $breadcrumbs [] = $bread;
            }
        }
	}
	else {
        $send404 = true;
	}
	


    //echo '<pre>', var_export($breads, true), '</pre>';
	
}

if(!isset($send404) || $send404 == false) {

// DEFS
    $_art_content = '';
    $_art_show_schema = true;




    // Check article content type
    $content_type = "article";
    $q = "select * from main_article where article_id='$middle_article_id';";
    $r = @mysql_query($q);
    if ($r && mysql_num_rows($r) > 0) {
        $row = mysql_fetch_assoc($r);
        $parent_id = $row['parent_id'];
        $created_timestamp = strtotime($row['created']);
        $content_type = ($row['a_type'] != "") ? $row['a_type'] : 'article';
        $article_friendly_url = stripslashes($row['friendly_url']);

        $article_canonical_link = $base_url . "/article/$article_friendly_url.html";

        if($_SESSION['current_url'] == $article_canonical_link) {
            $canonical_link = $article_canonical_link;
            if($row['meta_robots'] != "") {
                $meta_robots = trim($row['meta_robots']);
            }
        }

        $orig_timezone = date_default_timezone_get();

        if (isset($_REQUEST['debug1'])) {
            echo "<br/>Orig Timezone: <strong>$orig_timezone</strong>";
            echo "<br/>Orig Datetime: <strong>" . date("l, j F Y H:iA", $created_timestamp) . "</strong>";
        }

        //date_default_timezone_set("UTC");
        $created = date("Y-m-d\TH:i:s", $created_timestamp);
        $created_str = date("l, j F Y h:i A", $created_timestamp);

        if (isset($_REQUEST['debug1'])) {
            echo "<br/>Set Timezone: <strong>" . date_default_timezone_get() . "</strong>";
            echo "<br/>Set Datetime: <strong>" . date("l, j F Y h:i A", $created_timestamp) . "</strong>";
            echo "<br/>Set GMDATE: <strong>" . date("Y-m-d\TH:i T", $created_timestamp) . "</strong>";
        }

        date_default_timezone_set($orig_timezone);

        if (isset($_REQUEST['debug1'])) {
            echo "<br/>Orig Timezone: <strong>$orig_timezone</strong>";
        }


        $q = "select * from main_article_categ where cat_id='$parent_id';";
        $r = @mysql_query($q);
        if ($r && mysql_num_rows($r) > 0) {
            $row = mysql_fetch_assoc($r);
            $categ_content_type = $row['content_type'];
            if ($categ_content_type != "article") {
                $content_type = $categ_content_type;
            }
        }

    }

    ob_start();
    ?>

    <?php

    if (isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
        echo '<div class="user-action-container"><a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article&amp;article_id=' . $middle_article_id . '" target="_blank">Edit</a></div>';
    }

    if (!isset($content_type) || $content_type == "") $content_type = "article";
    if (isset($_REQUEST['debugart'])) {
        echo dirname(__FILE__) . "/type_" . $content_type . ".php";
    }
    require(dirname(__FILE__) . "/type_" . $content_type . ".php");


    $_art_content = ob_get_contents();
    ob_end_clean();


    $_art_full_url = $protocol . str_replace('//', '/', ($_SERVER['HTTP_HOST'] . "/" . $_SERVER['REQUEST_URI']));
    $_art_content = str_replace('#{FULL_URL}', $_art_full_url . '#', $_art_content);

    $schemas = array(
        "article" => "Article",
        "news" => "NewsArticle",
        "poi" => "TouristAttraction"
    );

    if ($_art_show_schema) {
        ?>
        <article class="article cyprus inline_content_display_article" itemscope="itemscope"
                 itemtype="http://schema.org/<?php echo $schemas[$content_type]; ?>">
            <?php
            /*if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
                echo '<div class="user-action-container"><a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article&amp;article_id='.$middle_article_id.'" target="_blank">Edit</a></div>';
            }*/
            echo $_art_content;
            ?>
        </article>
        <?php
    } else {

        echo $_art_content;

    }
    if( (isset($has_map) && $has_map) || (isset($art_photos) && count($art_photos) > 0)) {
        ?>
        <script type="text/javascript">

            <?php
            if(isset($has_map) && $has_map) {
            ?>
            var have_article_map = true;
            <?php
            }

            if(isset($art_photos) && count($art_photos) > 0) {
            ?>
            var have_article_photos = true;
            <?php
            }
            ?>
        </script>
        <?php
    }
}
else {
    include(dirname(__FILE__) . "/../404/404.php");
}