<?PHP

if($member_db[1] > 3){ msg("error", "Access Denied", "You don't have permission to edit news"); }




    $orig_cat_lines = file("./data/category.db.php");
    //only show allowed categories
        $allowed_cats = array();
        $cat_lines = array();
        foreach($orig_cat_lines as $single_line){
                  $ocat_arr = explode("|", $single_line);
                 $cat[$ocat_arr[0]] = $ocat_arr[1];
              if($member_db[1] <= $ocat_arr[3] or ($ocat_arr[3] == '0' || $ocat_arr[3] == '')){
                 $cat_lines[] = $single_line;
                 $allowed_cats[] = $ocat_arr[0];
              }

        }




// ********************************************************************************
// List all news available for editing
// ********************************************************************************
if($action == "list")
{
        echoheader("editnews","Edit News");







    // How Many News to show on one page
    if($news_per_page == ""){ $news_per_page = 21; }

    $all_db = array();
    if($source == ""){ $all_db = file("./data/news.txt"); }
    elseif($source == "postponed"){ $all_db = file("./data/postponed_news.txt"); ResynchronizePostponed(); }
    elseif($source == "unapproved"){ $all_db = file("./data/unapproved_news.txt");}
    else{ $all_db = file("./data/archives/${source}.news.arch"); }



// choose only needed news items
if ($category != '' or $author != "" or $member_db[1] == 3){
        foreach($all_db as $raw_line){
                $raw_arr = explode("|", $raw_line);

                 ;

        if (
                 ($category == '' or in_array($category, explode(',',$raw_arr[6])))
             and ($author == "" or $raw_arr[1] == $author)
             and ($member_db[1] != 3 or $raw_arr[1] == $member_db[2] )

           ){
                         $all_db_tmp[] = $raw_line;
        }
    }
$all_db = $all_db_tmp;
}


// Prelist Entries
    $flag = 1;
        if($start_from == "0"){ $start_from = ""; }
    $i = $start_from;
    $entries_showed = 0;

if(!empty($all_db)){

    foreach ($all_db as $line){
        if($j < $start_from){ $j++; continue; }
        $i++;

        $item_db = explode("|",$line);
             $itemdate = date("d/m/y",$item_db[0]);

                     if($flag == 1){ $bg="#F7F6F4"; $flag = 0; }
                     else {$bg = "#FFFFFF"; $flag = 1;}

                     if(strlen($item_db[2]) > 74){ $title = substr($item_db[2],0,70)." ..."; }
            $title = stripslashes( preg_replace(array("'\|'", "'\"'", "'\''"), array("I", "&quot;", "&#039;"), $item_db[2]) );
            $title = ereg_replace("<[^>]*>","",$title);
                     $entries .= "<tr>

                <td height=18  bgcolor=$bg>
                 <!-- ID: $item_db[0]--><a title='EDIT: $item_db[2]' href=\"$PHP_SELF?mod=editnews&action=editnews&id=$item_db[0]&source=$source\">$title</a>
                 <td height=18 bgcolor=$bg align=right>";
             $count_comments = countComments($item_db[0], $source);
             if($count_comments == 0){$entries .= "<font color=gray>$count_comments</font>";}
             else{$entries .= "$count_comments";}

             $entries .= "&nbsp;&nbsp;&nbsp;&nbsp;<td height=18 bgcolor=$bg nowrap>&nbsp;&nbsp;&nbsp;";


             if($item_db[6] == ""){ $my_cat = "<font color=gray>---</font>"; }

//             elseif($cat[$item_db[6]] == ""){ $my_cat = "<font color=red title='The news article is posted in category which does not exist anymore'>(ID: <b>$item_db[6]</b>)</font>"; }

             elseif(strstr($item_db[6],',')){
                     $all_this_cats_arr = explode(',',$item_db[6]);
                     $my_multy_cat_labels = '';
                     foreach($all_this_cats_arr as $this_single_cat){
                             $my_multy_cat_labels .= "$cat[$this_single_cat], ";
                     }
                     $my_cat = "<span onmouseover=\" window.status='categories: $my_multy_cat_labels'; return true\" onmouseout=\"window.status=''; return true\"><font color=#7979FF title='$my_multy_cat_labels'>(multiple)</font></span>";
             }

             else{ $my_cat = $cat[$item_db[6]]; }

             $entries .= "$my_cat&nbsp;<td height=18 bgcolor=$bg>
                 $itemdate
                 <td height=18 bgcolor=$bg>
                       $item_db[1]

                       <td align=center bgcolor=$bg><input name=\"selected_news[]\" value=\"{$item_db[0]}\" style=\"border:0; background-color:$bg\" type='checkbox'>

             </tr>
            ";
                $entries_showed ++;

            if($i >= $news_per_page + $start_from){ break; }
    }//foreach news line
}
// End prelisting



    $all_count_news = count($all_db);
    if($category != ""){ $cat_msg = "Category: <b>$cat[$category]</b>;"; }

    if($source == "postponed"){ $source_msg = "<span style='background-color:yellow;'>Postponed News, <a title='Refresh the postponed news file' href=\"$PHP_SELF?mod=editnews&action=list&source=postponed\">[Resynchronize]</a></span>"; $postponed_selected = " selected ";}
    elseif($source == "unapproved"){ $source_msg = "<span style='background-color:yellow;'>Unapproved News</span>"; $unapproved_selected = " selected ";}
    elseif($source != "" ){

                $news_lines = file("./data/archives/$source.news.arch");
                $count = count($news_lines);
                $last = $count-1;
                $first_news_arr = explode("|", $news_lines[$last]);
                $last_news_arr        = explode("|", $news_lines[0]);
                $first_timestamp = $first_news_arr[0];
                $last_timestamp         = $last_news_arr[0];
                $source_msg = "Archive: <b>". date("d M Y",$first_timestamp) ." - ". date("d M Y",$last_timestamp) ."</b>;";
        }



///////////////////////////////////////////
// Options Bar
echo"
        <table class=panel  border=0 cellpading=0 cellspacing=0 width=99% >
        <tr>
          <td title='Show More Options' style=\"cursor:pointer;\" onClick=\"getElementById('options').style.display='';\">
         Showing <b>$entries_showed</b> articles from total <b>$all_count_news</b>; $cat_msg $source_msg
          </td>

          <td >
        <p align=\"right\"><a href=\"javascript:ShowOrHide('options','')\">show options&nbsp;</a>
          </td>
        </tr>
    <tr>
          <td colspan=\"2\" >


<div id='options' style='display:none;z-index:1;' >
<form action=\"$PHP_SELF?mod=editnews&action=list\" method=POST name=options_bar>
<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
        <td height=\"1\" style=\"border-top: 1px dashed silver\" width=\"100%\" align=\"right\" colspan=\"3\">
    <p align=\"center\">&nbsp;
        </td>
</tr>
<tr >
        <td height=\"1\" width=\"286\" align=\"right\">
    Source&nbsp;
        </td>
        <td height=\"1\" width=\"180\">
<select name=\"source\" size=\"1\"><option value=\"\">- Active News -</option> \n <option $postponed_selected value=\"postponed\">- Postponed News -</option> \n <option $unapproved_selected value=\"unapproved\">- Unapproved News -</option>";

        if(!$handle = opendir("./data/archives")){ die("<center>Can not open directory ./data/archives "); }
                   while (false !== ($file = readdir($handle)))
           {
                           if($file != "." and $file != ".." and !is_dir("./data/archives/$file") and eregi("news.arch", $file))
            {
                    $file_arr = explode(".", $file);
                $id                  = $file_arr[0];

                $news_lines = file("./data/archives/$file");
                $count = count($news_lines);
                $last = $count-1;
                $first_news_arr = explode("|", $news_lines[$last]);
                $last_news_arr        = explode("|", $news_lines[0]);

                $first_timestamp = $first_news_arr[0];
                $last_timestamp         = $last_news_arr[0];

                                $arch_date = date("d M Y",$first_timestamp) ." - ". date("d M Y",$last_timestamp);
                $ifselected = "";
                if($source == $file_arr[0]){ $ifselected = "selected"; }
                echo "<option $ifselected value=\"$file_arr[0]\">Archive: $arch_date ($count)</option>";
                           }
                   }
        closedir($handle);

echo"</select>

        </td>
        <td height=\"1\" width=\"182\">
        </td>
</tr>
<tr>
        <td height=\"1\" width=\"284\" align=\"right\" >
    Category&nbsp;
        </td>
        <td height=\"1\" width=\"178\" >
    <select name=\"category\" ><option selected value=\"\">- All -</option>";

    //$cat_lines = file("./data/category.db.php");  commented-out because can lines are already opened before, and filtered with propper restrictions for user

        foreach($cat_lines as $single_line){
                $cat_arr = explode("|", $single_line);
                $ifselected = "";
                if($category == $cat_arr[0]){ $ifselected = "selected"; }
        echo"<option $ifselected value=\"$cat_arr[0]\">$cat_arr[1]</option>\n";
           }

    echo"</select>

        </td>
        <td height=\"1\" width=\"180\" >
        </td>
</tr>
";

if($member_db[1] != 3){
echo"
<tr>
        <td height=\"1\" width=\"284\" align=\"right\" >
    Author&nbsp;
        </td>
        <td height=\"1\" width=\"178\" >
    <select name=author size=\"1\"><option value=\"\">- Any -</option>";
    $user_lines = file("./data/users.db.php");
        foreach($user_lines as $single_line){
        if(!eregi("<\?", $single_line)){
                $user_arr = explode("|", $single_line);
                    $ifselected = "";
            if($user_arr[1] != 4){
                    if($author == $user_arr[2]){ $ifselected = "selected"; }
                        echo"<option $ifselected value=\"$user_arr[2]\">$user_arr[2]</option>\n";
                        }
                }
           }
    echo"</select>
        </td>
        <td height=\"1\" width=\"180\" >
        </td>
</tr>
";
}

echo"
<tr>
<td height=\"1\" width=\"284\" align=\"right\" >
News per page&nbsp;
</td>
<td height=\"1\" width=\"178\" >
<input style=\"text-align: Center\" name=\"news_per_page\" value=\"$news_per_page\" type=text size=3>
</td>
<td height=\"1\" width=\"180\" >
</td>
</tr>


<tr>
        <td height=\"1\" width=\"284\" align=\"right\" >
        </td>
        <td height=\"1\" width=\"178\" >
    <br><input type=submit value=\"Show\">
        </td>
        <td height=\"1\" width=\"180\" >
        </td>
</tr>
</table>
</form>
</div>
          </td>
    </tr>
        </table>&nbsp;";

// End Options Bar
////////////////////////////////////////////////////////////////////////////////    Showing List of News
if($entries_showed == 0){
    echo"<table border=0 cellpading=0 cellspacing=0 width=100% >
        <form method=post name=editnews>
        <td colspan=6 ><p style=\"border: solid black 1px;  margin: 22px 22px 22px 22px; padding: 4px 4px 4px 4px;\" align=center>- No news were found matching your criteria -<br><a href=\"#\"  onClick=\"getElementById('options').style.display='';\">[options]</a></p>";
}
else{
echo<<<JSCRIPT
<script language='JavaScript' type="text/javascript">
<!--
function ckeck_uncheck_all() {
        var frm = document.editnews;
        for (var i=0;i<frm.elements.length;i++) {
                var elmnt = frm.elements[i];
                if (elmnt.type=='checkbox') {
                        if(frm.master_box.checked == true){ elmnt.checked=false; }
            else{ elmnt.checked=true; }
                }
        }
        if(frm.master_box.checked == true){ frm.master_box.checked = false; }
    else{ frm.master_box.checked = true; }
}

-->
</script>
JSCRIPT;

    echo"<table border=0 cellpading=0 cellspacing=0 width=99% >
        <form method=post name=editnews>
        <td width=347>
        Title
        <td width=65>
        Comments

        <td width=65>
        &nbsp;Category

        <td width=58>
        &nbsp;Date

        <td width=78>
        Author

        <td width=21 align=center> <input style=\"border: 0px; background:transparent;\" type=checkbox name=master_box title=\"Check All\" onclick=\"javascript:ckeck_uncheck_all()\"> </a> ";
}
#####################################################################################################################
echo $entries;
#####################################################################################################################

if($start_from > 0)
{

        $previous = $start_from - $news_per_page;
        $npp_nav .= "<a href=\"$PHP_SELF?mod=editnews&action=list&start_from=$previous&category=$category&author=$author&source=$source&news_per_page=$news_per_page\"><< Previous</a>";
        $tmp = 1;
}

if(count($all_db) > $i)
{
        if($tmp){ $npp_nav .= "&nbsp;&nbsp;||&nbsp;&nbsp;";}
        $how_next = count($all_db) - $i;
        if($how_next > $news_per_page){ $how_next = $news_per_page; }
        $npp_nav .= "<a href=\"$PHP_SELF?mod=editnews&action=list&start_from=$i&category=$category&author=$author&source=$source&news_per_page=$news_per_page\">Next $how_next >></a>";
}

if($entries_showed != 0){
echo<<<HTML
<tr>
<td colspan=7 align=right>&nbsp;
</tr>

<tr>
<td>
$npp_nav
<td colspan=7 align=right>

With selected:
<select name=action>
<option value="">-- Choose Action --</option>
<option title="delete all selected news" value="mass_delete">Delete</option>
HTML;

if($member_db[1] == 1){ echo"<option title=\"make new archive with all selected news\" value=\"mass_archive\">Send to Archive</option>"; }


if( $source == "unapproved" ){ $if_mass_appr = "SELECTED"; } else {$if_mass_appr = ''; }
if( $source == "unapproved" and ($member_db[1] == 1 or $member_db[1] == 2)){ echo"<option $if_mass_appr title=\"approve selected news\" value=\"mass_approve\">Approve News</option>"; }

if($member_db[1] == 1){ echo"<option title=\"move all selected news to one category\" value=\"mass_move_to_cat\">Change Category</option>"; }
echo<<<HTML

</select>
<input type=hidden name=source value="$source">
<input type=hidden name=mod value="massactions">
<input type=submit value=Go>
</tr>
HTML;
}


echo<<<HTML
</tr>
<tr>
<td  colspan=1>
</tr>
<tr>
<td colspan=6>
</tr>
</form></table>
HTML;

    echofooter();
}
// ********************************************************************************
// Edit News Article
// ********************************************************************************
elseif($action == "editnews")
{
// Show The Article for Editing


    if($source == ""){ $all_db = file("./data/news.txt"); }
    elseif($source == "postponed"){ $all_db = file("./data/postponed_news.txt"); }
    elseif($source == "unapproved"){ $all_db = file("./data/unapproved_news.txt"); }
    else{ $all_db = file("./data/archives/$source.news.arch"); }
        $found = FALSE;
        foreach ($all_db as $line)
    {
                $item_db=explode("|",$line);
                if ($id == $item_db[0]){ $found = TRUE; break;}
        }//foreach news line

        $have_perm = 0;
           if(($member_db[1] == 1) or ($member_db[1] == 2)){$have_perm = 1;}
           elseif($member_db[1] == 3 and $item_db[1] == $member_db[2]) {$have_perm = 1;}
    if(!$have_perm){ msg("error", "NO Access", "You dont have access for this action", "$PHP_SELF?mod=editnews&action=list"); }







    if(strstr($item_db[6], ',')){
       $all_these_cats = explode(',',$item_db[6]);
       foreach($all_these_cats as $all_this_cat){
               if($member_db[1] != 1 and !in_array($all_this_cat,$allowed_cats) ){ msg("error", "Access Denied", "This article is posted under category which you are not allowed to access."); }
       }

    }
    else{
         if($member_db[1] != 1 and !in_array($item_db[6],$allowed_cats) ){ msg("error", "Access Denied", "This article is posted under category which you are not allowed to access."); }
    }






    if (!$found){ msg("error","Error !!!","The selected news item can <b>not</b> be found."); }
    $newstime   = date("D, d F Y h:i:s", $item_db[0]);
    $item_db[2] = stripslashes( preg_replace(array("'\|'", "'\"'", "'\''"), array("I", "&quot;", "&#039;"), $item_db[2]) );




       $short_story_id = 'short_story';
       $full_story_id = 'full_story';

     //
    // Are we using the WYSIWYG ?
    //
    if($config_use_wysiwyg == "yes"){
       $use_wysiwyg = true;
       $article_format_options = "
    <label for='convert'>
     <input id='convert' style=\"border:0; background-color:transparent\" type=checkbox value=\"yes\" name=\"if_convert_new_lines\" disabled > Convert new lines to &lt;br /&gt;</label>
    <br/>
    <label for='html'>
     <input id='html' style=\"border:0; background-color:transparent\" type=checkbox value=\"yes\" name=\"dummi\" checked disabled> Use HTML in this article</label>
     <input type=hidden name=\"if_use_html\" value=\"yes\">
    <br/>";
    }else{
       $use_wysiwyg = false;
       $article_format_options = "
    <label for='convert'>
     <input id='convert' style=\"border:0; background-color:transparent\" type=checkbox value=\"yes\" name=\"if_convert_new_lines\" checked > Convert new lines to &lt;br /&gt;</label>
    <br/>
    <label for='html'>
     <input id='html' style=\"border:0; background-color:transparent\" type=checkbox value=\"yes\" name=\"if_use_html\" checked> Use HTML in this article</label>
    <br/>";
    }



    $item_db[3] = replace_news("admin", $item_db[3], $use_wysiwyg);
    $item_db[4] = replace_news("admin", $item_db[4], $use_wysiwyg);


        echoheader("editnews","Edit News");

    echo"
    <SCRIPT LANGUAGE=\"JavaScript\">
        function preview(){
        dd=window.open('','prv','height=400,width=750,resizable=1,scrollbars=1')
        document.addnews.mod.value='preview';document.addnews.target='prv'
        document.addnews.submit();dd.focus()
        setTimeout(\"document.addnews.mod.value='editnews';document.addnews.target='_self'\",500)
        }
    function confirmDelete(url){
        var agree=confirm(\"Do you really want to permanently delete this article ?\");
        if (agree)
        document.location=url;
        }

    function increaseTextarea(area){
             if (document.getElementById(area).style.height == '') {
                 document.getElementById(area).style.height = '300px';
             }
             else{
                  var pattern = new RegExp(\"\\\\d+\",'ig');
                  var currHeight = document.getElementById(area).style.height.match(pattern);
                  var newHeight = +currHeight + 100;
                  document.getElementById(area).style.height = newHeight + 'px';
             }


    }

function submitForm() {
        //make sure hidden and iframe values are in sync before submitting form
        //to sync only 1 rte, use updateRTE(rte)
        //to sync all rtes, use updateRTEs
        updateRTE('short_story');
        updateRTE('full_story');
        //updateRTEs();

        //change the following line to true to submit form
        return true;
}

        </SCRIPT>

    <form onSubmit = \"return submitForm();\" method=POST name=addnews action=\"$PHP_SELF\">
        <table border=0 cellpading=0 cellspacing=0 width=\"654\" height=\"100%\" >
        <td valign=middle width=\"75\">
        Info.
        <td width=\"571\" colspan=\"6\">
        Posted on $newstime by $item_db[1]

        <tr>
        <td valign=middle width=\"75\" valign=\"top\">
        Title
        <td width=\"464\" colspan=\"3\">
        <input type=text name=title value=\"$item_db[2]\" size=55 tabindex=1>
    <td width=\"103\" valign=\"top\">
        </tr>";

        if($config_use_avatar == "yes"){
            echo"
        <tr>
                <td valign=middle width=\"75\" valign=\"top\">
                Avatar URL
                <td width=\"464\" colspan=\"3\">
                <input type=text name=editavatar value=\"$item_db[5]\" size=42 tabindex=2>&nbsp;&nbsp;&nbsp;<font style=\"font-size:7pt\">(optional)</font>
                <td width=\"103\" valign=\"top\">
                </tr>";
        }



/* Old Single Category Code

       echo"<tr>
        <td valign=middle width=\"75\" valign=\"top\">
        Category
        <td width=\"464\" colspan=\"3\">
        <select name=\"category\" >";

    $cat_lines = file("./data/category.db.php");
        foreach($cat_lines as $single_line){
                $cat_arr = explode("|", $single_line);
        if($item_db[6] == $cat_arr[0]){
                        echo"<option selected=\"selected\" value=\"$cat_arr[0]\">$cat_arr[1]</option>\n";
            $selected = TRUE;
                }else{ echo"<option value=\"$cat_arr[0]\">$cat_arr[1]</option>\n"; }
        }
    if(!$selected){ echo"<option selected value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>"; }
    else{ echo"<option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>"; }

    echo"</select>
    <td width=\"105\" valign=\"top\">

        </tr>";
*/



       echo"<tr>
        <td valign=middle width=\"75\" valign=\"top\">
        Category
        <td width=\"464\" colspan=\"3\">";
        echo'<table width="100%" border="0" cellspacing="0" cellpadding="0" class="panel">';


//        $cat_lines = file("./data/category.db.php"); commented-out because can lines are already opened before, and filtered with propper restrictions for user
        foreach($cat_lines as $single_line){
                $cat_arr = explode("|", $single_line);

        echo"
        <td style='font-size:10px;' valign=top>
        <label for='cat{$cat_arr[0]}'>";

        if( in_array($cat_arr[0], explode(',',$item_db[6])) ){
            echo"<input checked style='background-color:transparent;border:0px;' type=checkbox name='category[]' id='cat{$cat_arr[0]}' value='{$cat_arr[0]}'>$cat_arr[1]</label>";
        }else{

            echo"<input style='background-color:transparent;border:0px;' type=checkbox name='category[]' id='cat{$cat_arr[0]}' value='{$cat_arr[0]}'>$cat_arr[1]</label>";
        }

        $i++;
        if ($i%4 == 0){ echo'<tr>'; }


        }
        echo"</tr>";

    echo" </table> <td> </td> </tr>";








   echo" <tr>
        <td  valign=\"top\">
        <br />Short Story
        <td width=\"464\" colspan=\"3\">
";

if ($use_wysiwyg) {
        echo"
        <script language=\"JavaScript\" type=\"text/javascript\" src=\"rte/richtext.js\"></script>
<script language=\"JavaScript\" type=\"text/javascript\">
        initRTE(\"rte/images/\", \"rte/\", \"rte/\", false); </script>

<script language=\"JavaScript\" type=\"text/javascript\">
        writeRichText('short_story', '".rteSafe($item_db[3])."', 460, 250, true, false);
</script>
        ";
}else{
     echo"
     <textarea rows=\"12\" cols=\"74\" id=\"short_story\" name=\"short_story\" tabindex=3>$item_db[3]</textarea>
     ";
}

     echo"

        <td width=\"165\" valign=\"top\" align=center style='background: url(skins/images/baloon.gif) no-repeat top left'>
        <p align=\"center\"><a href=# onclick=\"window.open('$PHP_SELF?&mod=images&action=quick&area=$short_story_id&wysiwyg=$use_wysiwyg', '_Addimage', 'HEIGHT=500,resizable=yes,scrollbars=yes,WIDTH=360');return false;\" target=\"_Addimage\"><br />
        [insert image]</a><br />
<!--        <a href=# onclick=\"window.open('$PHP_SELF?&mod=about&action=cutecode&target=short_story', '_CuteCode', 'HEIGHT=280,resizable=yes,scrollbars=yes,WIDTH=360');return false;\" target=\"_Addimage\">[quick tags]</a><br /> -->
        <a href=# onclick=\"increaseTextarea('$short_story_id'); return false;\">[expand]</a>
        <br /><br />

";

    echo insertSmilies("$short_story_id", 4, true, "$use_wysiwyg");

        echo"</tr>

        <tr>
        <td width=\"75\" valign=\"top\">
        <br />Full Story<br /><font style=\"font-size:7pt\">(optional)</font>
        <td width=\"464\" colspan=\"3\">
";

if ($use_wysiwyg) {
        echo"
<script language=\"JavaScript\" type=\"text/javascript\">
        writeRichText('full_story', '".rteSafe($item_db[4])."', 460, 250, true, false);
</script>
        ";
}else{
     echo"
     <textarea rows=\"12\" cols=\"74\" id=\"full_story\" name=\"full_story\" tabindex=4>$item_db[4]</textarea>
     ";
}

     echo"

        <td width=\"108\" valign=\"top\" style='background: url(skins/images/baloon.gif) no-repeat top left'>
        <p align=\"center\"><br />
        <a href=# onclick=\"window.open('$PHP_SELF?mod=images&action=quick&area=$full_story_id&wysiwyg=$use_wysiwyg', '_Addimage', 'HEIGHT=500,resizable=yes,scrollbars=yes,WIDTH=360');return false;\" target=\"_Addimage\">[insert image]</a><br />
<!--        <a href=# onclick=\"window.open('$PHP_SELF?&mod=about&action=cutecode&target=full_story', '_Addimage', 'HEIGHT=280,resizable=yes,scrollbars=yes,WIDTH=360');return false;\" target=\"_CuteCode\">[quick tags]</a><br />-->

        <a href=# onclick=\"increaseTextarea('$full_story_id'); return false;\">[expand]</a>
        <br /><br />";

        echo insertSmilies("$full_story_id", 4, true, "$use_wysiwyg");

        echo"</tr>
        <tr>
        <td>
        <td  colspan=\"3\">
        <input type=hidden name=id value=$id>
        <input type=hidden name=action value=doeditnews>
        <input type=hidden name=mod value=editnews>
        <input type=hidden name=source value=$source>






     <table border=0 cellspacing=0 cellpadding=0 width=100%>
      <tr>
       <td width=50%>
        <input type=submit style='font-weight:bold' value=\"Save Changes\" accesskey=\"s\">&nbsp;
        <!--<input type=button value=\"Preview\" onClick=\"preview()\" accesskey=\"p\">-->
       </td>
       <td width=50% align=right>
       ";

  if($source == 'unapproved'){
     echo"<input type=button value=\"Approve\" onClick=\"javascript:document.location=('$PHP_SELF?mod=massactions&selected_news[]=$id&action=mass_approve&source=unapproved');\"> &nbsp;";
  }

  echo"<input type=button value=\"Delete\" onClick=\"confirmDelete('$PHP_SELF?mod=editnews&action=doeditnews&source=$source&ifdelete=yes&id=$id')\"> &nbsp;
        <input style='width:90px;' type=button onClick=\"ShowOrHide('options','')\" value=\"Article Options\">
       </td>
      </tr>
    </table>


        </tr>

        <tr id='options' style='display:none;'>
        <td width=\"75\">
    <br>Options
        <td width=\"565\" colspan=\"4\">
    &nbsp;<br>
$article_format_options
        </tr>

        </form>



        <tr>
        <td width=\"75\"><br>
        <td width=\"561\" colspan=\"4\"><br>
        </tr>



        <tr>
        <td width=\"75\">
        Comments";

// Show the Comments for Editing

    if($source == "" or $source == "postponed" or $source == "unapproved"){ $all_comments_db = file("./data/comments.txt"); }
    else{ $all_comments_db = file("./data/archives/${source}.comments.arch"); }

    $found_newsid = FALSE;
        foreach($all_comments_db as $comment_line)
    {
                $comment_line = trim($comment_line);
                $comments_arr = explode("|>|",$comment_line);
        if($comments_arr[0] == $id)
        {//if these are comments for our story
            $found_newsid = TRUE;
                        if ($comments_arr[1] != "")
            {
                echo"         <td width=290>
                                <b>&nbsp;&nbsp;Poster</b>, Comment preview

                             <td width=120>
                                <b>Date</b>

                              <td width=1>
                              <td width=100>
                          </tr>
                                <form method=post name=comments action=\"$PHP_SELF\">";

                $flag = 1;
                          $different_posters = explode("||",$comments_arr[1]);
                          foreach($different_posters as $individual_comment)
                          {
                            if($flag == 1){ $bg = "bgcolor=#F7F6F4"; $flag = 0;}
                        else{$bg = ""; $flag = 1;}

                            $comment_arr = explode("|",$individual_comment);
                            $comtime = date("d/m/y h:i:s",$comment_arr[0]);
                            $comm_value = stripslashes(strip_tags($comment_arr[4]));
                            $comm_excerpt_lenght = 43 - strlen($comment_arr[1]);

                            if($comm_excerpt_lenght < strlen($comm_value)){
                               $comm_excerpt = substr($comm_value,0, $comm_excerpt_lenght).'...';
                            }else{
                               $comm_excerpt = $comm_value;
                            }

                            if($comment_arr[1])
                            {
                                           if(strlen($comment_arr[1]) > 25){ $comment_arr[1] = substr($comment_arr[1],0,22)."..."; }
                                            echo"<tr>
                                           <td >
                                           <td $bg>
                                            &nbsp; <a title=\"edit this comment\nip:$comment_arr[3]\" href=\"$PHP_SELF?mod=editcomments&action=editcomment&newsid=$id&comid=$comment_arr[0]&source=$source\" onclick=\"window.open('$PHP_SELF?mod=editcomments&action=editcomment&newsid=$id&comid=$comment_arr[0]&source=$source', 'Comments', 'HEIGHT=270,resizable=yes,scrollbars=yes,WIDTH=400');return false;\">$comment_arr[1]</a>,
                                             $comm_excerpt

                                           <td $bg>
                                            <a title=\"edit this comment\nip:$comment_arr[3]\" href=\"$PHP_SELF?mod=editcomments&action=editcomment&newsid=$id&comid=$comment_arr[0]&source=$source\" onclick=\"window.open('$PHP_SELF?mod=editcomments&action=editcomment&newsid=$id&comid=$comment_arr[0]&source=$source', 'Comments', 'HEIGHT=270,resizable=yes,scrollbars=yes,WIDTH=400');return false;\">$comtime</a>
                                           <td width=\"1\" $bg>
                                            <input type=checkbox name=\"delcomid[$comment_arr[0]]\" value=1>
                                           <td $bg>
                                           </tr>";
                    }//if not blank
                          }//foreach comment

                    echo"<tr>
                    <td >
                    <td >

                    <td >
                    <p align=\"right\">delete all?
                    <td >
                    <input type=checkbox name=delcomid[all] value=1>
                    <td >
                    </tr>

                    <tr>
                    <td >
                    <td colspan=\"3\">
                    <p align=\"right\"><input type=submit value=\"Delete Selected\">
                    <td >
                    </tr>

                    <input type=hidden name=newsid value=$id>
                    <input type=hidden name=deletecomment value=yes>
                    <input type=hidden name=action value=doeditcomment>
                    <input type=hidden name=mod value=editcomments>
                <input type=hidden name=source value=$source>
                    </form>
                    </table>";

                    break;//foreach comment line
           }//if there are any comments
           else
           {
                            echo"<td width=\"210\">
                           No Comments
                           <td width=\"219\">
                           <td width=\"1\">
                           <td width=\"105\">
                           </tr>
                           </tr>
                           </table>";
           }
        }//if these are comments for our story
    }//foreach comments line
    if($found_newsid == FALSE){
            echo"<td width=\"210\">
           No Comments
           <td width=\"219\">
           <td width=\"1\">
           <td width=\"105\">
           </tr>
           </tr>
           </table>";
    }
    echofooter();
}
// ********************************************************************************
// Do Edit News
// ********************************************************************************
elseif($action == "doeditnews")
{

/////
///Format our categories variable
/////

if( is_array($category) ){  //User has selected multiple categories

    $nice_category = '';
    $ccount = 0;

    foreach($category as $ckey=>$cvalue){
            if( !in_array($cvalue,$allowed_cats) ){ die('not allowed category'); }
            if($ccount==0){ $nice_category = $cvalue; }//first cat
            else{ $nice_category = $nice_category.','.$cvalue; }

            $ccount++;
    }



}else{ //Not in a category
   if( $category !="" and isset($category) and !in_array($category,$allowed_cats) ){ die('not allowed category'); }
   //don't format $nice_cats because we have not selected any.
}

        if(trim($title) == "" and $ifdelete != "yes"){ msg("error","Error !!!","The title can not be blank.", "javascript:history.go(-1)"); }
        if($short_story == "" and $ifdelete != "yes"){ msg("error","Error !!!","The story can not be blank.", "javascript:history.go(-1)"); }

        if($if_convert_new_lines        == "yes"){ $n_to_br                = TRUE; }
    if($if_use_html                                == "yes"){ $use_html        = TRUE; }

        $short_story =         replace_news("add", rtrim($short_story), $n_to_br, $use_html);
        $full_story =         replace_news("add", rtrim($full_story), $n_to_br, $use_html);
        $title =                 stripslashes( preg_replace(array("'\|'", "'\n'", "''"), array("I", "<br />", ""), $title) );
    $avatar =                stripslashes( preg_replace(array("'\|'", "'\n'", "''"), array("I", "<br />", ""), $avatar) );

    if($source == ""){ $news_file = "./data/news.txt"; $com_file = "./data/comments.txt"; }
    elseif($source == "postponed"){ $news_file = "./data/postponed_news.txt"; $com_file = "./data/comments.txt"; }
    elseif($source == "unapproved"){ $news_file = "./data/unapproved_news.txt"; $com_file = "./data/comments.txt"; }
    else{ $news_file = "./data/archives/$source.news.arch"; $com_file = "./data/archives/$source.comments.arch";}

        $old_db = file("$news_file");
        $new_db = fopen("$news_file", w);
        foreach($old_db as $old_db_line){
                $old_db_arr = explode("|", $old_db_line);
                if($id != $old_db_arr[0]){
                        fwrite($new_db,"$old_db_line");
        }
        else
        {

                $have_perm = 0;
                   if(($member_db[1] == 1) or ($member_db[1] == 2)){$have_perm = 1;}
                   elseif($member_db[1] == 3 and $old_db_arr[1] == $member_db[2]) {$have_perm = 1;}
            if($have_perm){
                if($ifdelete != "yes")
            {
                    $okchanges = TRUE;
                fwrite($new_db,"$old_db_arr[0]|$old_db_arr[1]|$title|$short_story|$full_story|$editavatar|$nice_category||\n");
            }
                        else
            {
                    $okdeleted = TRUE;
                                $all_file = file("$com_file");
                $new_com=fopen("$com_file","w");
                foreach($all_file as $line)
                {
                                        $line_arr = explode("|>|",$line);
                                        if($line_arr[0] == $id){ $okdelcom = TRUE; }
                    else{ fwrite($new_com,"$line"); }
                }
                fclose($new_com);
            }
                }else{ fwrite($new_db,"$old_db_line"); $no_permission = TRUE; }
        }
    }
    fclose($new_db);

    if($no_permission){ msg("error", "NO Access", "You dont have access for this action", "$PHP_SELF?mod=editnews&action=list"); }
        if($okdeleted and $okdelcom){ msg("info","News Deleted","The news item successfully was deleted.<br />If there were comments for this article they are also deleted."); }
    if($okdeleted and !$okdelcom){ msg("info","News Deleted","The news item successfully was deleted.<br />If there were comments for this article they are also deleted.<br /><font color=red>But can not delete comments of this article !!!</font>"); }
    elseif($okchanges){ msg("info","Changes Saved","The changes were successfully saved", "$PHP_SELF?mod=editnews&action=editnews&id=$id&source=$source"); }
        else{ msg("error","Error !!!","The news item can not be found or there is an error with the news database file."); }

}


?>