0

I have a weird problem. I made a website where I can publish different kinds of articles. Suddenly when I try to view an article, there is no content text outputted to the page. On my local host everything is working fine, but on the server it stopped working and there are no errors. Take a look at this page for example - https://buildtool.dev/view-article.php?id=63. I have no idea what causes this.

Update: I narrowed down this problem. In my code, I use DomDocument for parsing headings. If I comment out code with DomDocument, the page works.

<!DOCTYPE html>
   <head>
   <title>Viewing article</title>
   <link rel="stylesheet" href="main.css" type="text/css">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<div class="vertical">
<a href="index.php">Front page</a>
<script>
    function deleteArticle(){
       let request = new XMLHttpRequest();
       request.onreadystatechange = function () {
           if (request.readyState === 4) {
               window.location='index.php'
               console.log(request.responseText)
        }
    }
    let id=document.getElementById('article-id').value;
    let confirmation=confirm('Confirm deletion of the article')
    if(confirmation) {
        request.open('GET', 'delete-article.php?id=' + id, true);
        request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        request.send()
    }
}

function deleteComment(button){
    let commentid=button.getAttribute('data-cid')
    let articleid=button.getAttribute('data-aid')
    let request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (request.readyState === 4) {
            console.log(request.responseText)
            window.location.reload()
        }
    }
    let confirmation=confirm('Confirm deletion of the comment')
    if(confirmation) {
        request.open('GET', 'delete-comment.php?c=' + commentid+'&a='+articleid, true);
        request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        request.send()
    }
}
</script>
<?php
session_start();
include_once 'constants.php';
include_once 'functions.php';
$c=defaultMysqlConnection();
$articleId=$_GET['id'];
if(isset($_SESSION[id]))
{
  $articles=executeQuery($c,"SELECT * FROM defaultdb.articles WHERE id='$articleId'",true);
  $group=$_SESSION['group'];
if($group=='administrators'){
    echo "<button style='width: 200px' onclick='deleteArticle()'>Delete this article</button>";
}
$userid=$_SESSION[id];
$title=$articles[0]['title'];
$content=$articles[0]['content'];
$creator=$articles[0]['creator'];
$created=$articles[0]['created'];
$accepted=$articles[0]['permitted'];
$sources=$articles[0]['sources'];
$sources=htmlspecialchars($sources,ENT_QUOTES);
$processedContent='';
$parts=preg_split("/(<br>)+/",$content);
for($i=0;$i<sizeof($parts);$i++){
    $part=preg_replace("/::\d+::/","",$parts[$i]);
    $processedContent.=$part."\n";
}
$processedContent=htmlspecialchars($processedContent,ENT_QUOTES);
$categories=$articles[0]['categories'];
echo "<form method='post' action='edit-article.php'>
        <input hidden value='$articleId' name='id'>
        <input hidden value='$title' name='title'>  
        <textarea hidden name='content'>$processedContent</textarea>  
        <input hidden value='$creator' name='creator'>  
        <input hidden value='$created' name='created'>  
        <input hidden value='$accepted' name='accepted'> 
        <input hidden value='$sources' name='sources'>
        <input hidden value='$userid' name='current-editor'>
        <input hidden value='$categories' name='categories'>
        <input type='submit' value='Edit'> 
      </form>";
}
echo "</div>";
include_once 'constants.php';
include_once 'functions.php';
$id=$_GET['id'];
$r=executeQuery($c,"SELECT * FROM defaultdb.articles WHERE id='$id'",true);
if($r){
  $title2=$r[0]['title'];
  $content2=($r[0]['content']);
  $createdOn=$r[0]['created'];
  $by=$r[0]['creator'];
  $sources=$r[0]['sources'];
  $sources=html_entity_decode($sources,ENT_QUOTES);
  $previousContent=$r[0]['previous_version'];
  $lastEdited=$r[0]['last_modified'];

  $personalExperiences=$r[0]['personal_experiences'];
  $categories=$r[0]['categories'];
  $explCategories=explode(',',$categories);

echo "<h1>$title2</h1>";
echo "<h3>Created by $by on $createdOn</h3>";
echo "<h3>Last edit on $lastEdited</h3>";
echo "<div class='content-text'>";
$headings='';
$content='';
$parts=preg_split("/<br>?/",$content2);
foreach ($parts as $part){
    $part=convertFromNumberedText($part);
    $content.=$part;
}

$brSplit=explode("<br>",$content);
$content='';
foreach ($brSplit as $nextLine)
{

    //this causes the problem
    $domDocument=new DOMDocument();
    $domDocument->loadHTML("<!DOCTYPE html>".$nextLine);
    $heading1=$domDocument->getElementsByTagName("h1");
    $heading2=$domDocument->getElementsByTagName("h2");
    $heading3=$domDocument->getElementsByTagName("h3");
    $heading4=$domDocument->getElementsByTagName("h4");
    $heading5=$domDocument->getElementsByTagName("h5");
    $heading6=$domDocument->getElementsByTagName("h6");
    $hasHeading=false;
    foreach ($heading1 as $h) {
        $value = $h->nodeValue;
        $link = "<a href='#$value'>$value</a>";
        $headings .= $link;
        if(!$hasHeading)
        {
            $content .= "<div id='$value'>$nextLine</div><br>";
            $hasHeading=true;
        }
    }
    foreach ($heading2 as $h)
    {
        $value = $h->nodeValue;
        $link = "<a href='#$value'>$value</a>";
        $headings .= $link;
        if(!$hasHeading)
        {
            $content .= "<div id='$value'>$nextLine</div><br>";
            $hasHeading=true;
        }
    }
    foreach ($heading3 as $h)
    {
        $value = $h->nodeValue;
        $link = "<a href='#$value'>$value</a>";
        $headings .= $link;
        if(!$hasHeading)
        {
            $content .= "<div id='$value'>$nextLine</div><br>";
            $hasHeading=true;
        }
    }
    foreach ($heading4 as $h)
    {
        $value = $h->nodeValue;
        $link = "<a href='#$value'>$value</a>";
        $headings .= $link;
        if(!$hasHeading)
        {
            $content .= "<div id='$value'>$nextLine</div><br>";
            $hasHeading=true;
        }
    }
    foreach ($heading5 as $h)
    {
        $value = $h->nodeValue;
        $link = "<a href='#$value'>$value</a>";
        $headings .= $link;
        if(!$hasHeading)
        {
            $content .= "<div id='$value'>$nextLine</div><br>";
            $hasHeading=true;
        }
    }
    foreach ($heading6 as $h)
    {
        $value = $h->nodeValue;
        $link = "<a href='#$value'>$value</a>";
        $headings .= $link;
        if(!$hasHeading)
        {
            $content .= "<div id='$value'>$nextLine</div><br>";
            $hasHeading=true;
        }
    }
    if(!$hasHeading){
        $content.=$nextLine."<br>";
    }
    $content.=$nextLine."<br>";
}
if(!empty($headings)) {
    echo "<div class='vertical left-margin'>Chapters:";
    echo $headings . "</div><br>";
}
echo $content;
echo "</div>";
echo "<input type='hidden' value='$id' id='article-id'>";

echo "<label>Categories:</label><br>";
if($explCategories) {
    foreach ($explCategories as $category) {
        echo "<a href='browse-category.php?category=$category'>$category</a><br>";
    }
}
echo "<br>";
echo "<label>Sources:</label>";
if($personalExperiences){
    echo "<p>Personal experience/knowledge of $personalExperiences</p>";
}
echo "<div>$sources</div>";
$brokenContent1=htmlspecialchars(preg_replace("/\n/","<br>",$content2),ENT_QUOTES);
$brokenContent2=htmlspecialchars(preg_replace("/\n/","<br>",$previousContent),ENT_QUOTES);
echo "<input hidden value='$brokenContent1' id='current-content'>";
echo "<input hidden value='$brokenContent2' id='previous-content'>";

if ($previousContent && isset($_SESSION[id]))
   echo"<button onclick='compareView()'>Compare with previous version</button>";
}
else  echo $c->error;
$checkComments="SHOW TABLES LIKE 'article$articleId"."comments'";
$ccr=executeQuery($c,$checkComments);
if(sizeof($ccr)==1) {
  $q = "SELECT * FROM article$articleId" . "comments";
  $r2 = executeQuery($c, $q, true);
if ($r2) {
    foreach ($r2 as $comment) {
        $content = $comment['content'];
        $user = $comment['user'];
        $commentid=$comment['comment'];
        echo "<div class='comment'><p>$content</p><p>By $user</p></div>";
        if(isset($_SESSION[id]) && $_SESSION['group']=='administrators')
            echo "<button data-cid='$commentid' data-aid='$articleId' onclick='deleteComment(this)'>Delete</button>";
        echo "<br><br><br>";
    }
} else
    echo $c->error;
}
if(isset($_SESSION[id])){
   $user=$_SESSION[id];
   echo "<form method='post' action='leave-comment.php'>
        <input hidden name='userid' value='$user'>
        <input hidden name='articleid' value='$articleId'>
        <textarea required name='comment' placeholder='Comment'></textarea>
        <input type='submit' value='Post'>
    </form>";
}
?>

<script>
    function compareView(){
        let request = new XMLHttpRequest();
        request.onreadystatechange = function () {
            if (request.readyState === 4) {
                let current=document.getElementById('current-version')
                let previous=document.getElementById('previous-version')
                let res=request.responseText;
                console.log(res)
                let response=res.split('++')
                current.innerHTML=response[0];
                document.getElementById('l1').innerText='Current version:'
                previous.innerHTML=response[1];
                document.getElementById('l2').innerText='Previous version:'
                if(response[0]===response[1])
                    console.log(true)

            }
        }
        request.open('POST', 'prepare-versions.php', true);
        request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        let sendCurrent=document.getElementById('current-content').value
        let sendPrevious=document.getElementById('previous-content').value
        request.send("current="+sendCurrent+"&previous="+sendPrevious)
    }
</script>
<div class="horizontal">
    <div class="vertical expand">
        <label id="l1"></label>
        <div class="expand" id="current-version"></div>
    </div>
    <div class="vertical expand">
        <label id="l2"></label>
        <div class="expand" id="previous-version"></div>
    </div>
</div>
</body>
Alexiy
  • 1,966
  • 16
  • 18

0 Answers0