<?PHP

if($member_db[1] > 3){ msg("error", "Access Denied", "You don't have permission to add 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);
              if($member_db[1] <= $ocat_arr[3] or ($ocat_arr[3] == '0' || $ocat_arr[3] == '')){
                 $cat_lines[] = $single_line;
                 $allowed_cats[] = $ocat_arr[0];
              }

        }

if ($action == "addnews"){






        echoheader("addnews", "Add News");


    $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/>";
    }

    echo "
    <SCRIPT LANGUAGE=\"JavaScript\">
    function preview(){
             if(document.addnews.short_story.value == '' || document.addnews.title.value == ''){ alert('Your article must have at least Title and Short Story'); }
             else{
                  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='addnews';document.addnews.target='_self'\",500)
             }
    }

    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;
}

    function increaseTextareaBug(){";

     if($use_wysiwyg){
             echo"var area = '$full_story_id';
             if (document.getElementById(area).style.height == '') {
                 document.getElementById(area).style.height = '250px';
             }
             else{
                  var pattern = new RegExp(\"\\\\d+\",'ig');
                  var currHeight = document.getElementById(area).style.height.match(pattern);
                  var newHeight = +currHeight + 1;
                  document.getElementById(area).style.height = newHeight + 'px';
             }";
             }

    echo"}

    onload=focus;function focus(){document.forms[0].title.focus();}

    </SCRIPT>

<form onSubmit = \"return submitForm();\"  method=post name=addnews action=\"$PHP_SELF\">
        <table border=0 cellpading=0 cellspacing=0 width=\"654\" >

    <tr>
        <td width=\"75\">
        Title
        <td width=\"575\" colspan=\"2\">
        <input type=text size=\"55\" name=\"title\" tabindex=1>
        </tr>";

        if($config_use_avatar == "yes"){
            echo"<tr>
                <td width=\"75\">
                Avatar URL
                <td width=\"575\" colspan=\"2\">
                <input tabindex=2 type=text size=\"42\" value=\"$member_db[8]\" name=\"manual_avatar\" >&nbsp;&nbsp;&nbsp;<font style=\"font-size:7pt\">(optional)</font>
                </tr>";
        }
/* Old Single Category Code  */

        if(count($cat_lines) > 0){
                echo"<tr id='singlecat'>
                <td width=\"75\">
                Category
                <td width=\"575\" colspan=\"2\">
                <select id='selecsinglecat' name=category tabindex=3>\n
        <option value=\"\"> </option>\n";
                foreach($cat_lines as $single_line){
                                $cat_arr = explode("|", $single_line);
                $if_is_selected = "";
                if($category == $cat_arr[0]){ $if_is_selected = " selected "; }
                echo"<option $if_is_selected value=\"$cat_arr[0]\">$cat_arr[1]</option>\n";
                    }
            echo"</select> <a href=\"javascript:ShowOrHide('multicat','singlecat');\" onClick=\"javascript:document.getElementById('selecsinglecat').name='';\">(multiple categories)</a></tr>";
        }


/*###
##### New Multy Category Code
*/###



        if(count($cat_lines) > 0){
                echo"
                <tr style=\"display:none;\" id='multicat'>
                <td width=\"75\">
                Category
                <td>";
                echo'<table width="100%" border="0" cellspacing="0" cellpadding="0" class="panel">';

                foreach($cat_lines as $single_line){


                $cat_arr = explode("|", $single_line);
                $if_is_selected = "";
                if($category == $cat_arr[0]){ $if_is_selected = " checked "; }

  //              echo"<input name='category[]' type=checkbox $if_is_selected value=\"$cat_arr[0]\">$cat_arr[1],\n";

        echo"

        <td style='font-size:10px;' valign=top>
        <label for='cat{$cat_arr[0]}'>
        <input $if_is_selected 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></table> <td> </td>  ";
        }









  echo"
    <tr>
        <td width=\"75\" valign=\"top\">
        <br />Short Story
        <td>";


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', '', 460, 250, true, false);
</script>
        ";
}else{
     echo"
     <textarea rows=\"12\" cols=\"74\" id=\"short_story\" name=\"short_story\" tabindex=4></textarea>
     ";
}

     echo"
        <td width=\"108\" valign=\"top\" 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 id='full-story' style='display:none;z-index:1;'>
        <td width=\"75\" valign=\"top\">
        <br />Full Story<br /><font style=\"font-size:7pt\">(optional)</font>
        <td>
        ";


if ($use_wysiwyg) {
    echo"<script language=\"JavaScript\" type=\"text/javascript\">
        writeRichText('full_story', '', 460, 300, true, false);
        </script>";
}else{
      echo"<textarea rows=\"12\" cols=\"74\" id=\"full_story\" name=\"full_story\" tabindex=5></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>



     <table border=0 cellspacing=0 cellpadding=0 width=100%>
      <tr>
       <td width=50%>
        <input type=submit style='font-weight:bold' title=\"Post the New Article\" value=\"     Add News     \" accesskey=\"s\">
       </td>
       <td width=50% align=right>
        <input style='width:110px;'type=button onClick=\"ShowOrHide('full-story',''); setTimeout('increaseTextareaBug()',310);\" value=\"Toggle Full-Story\"> <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=\"575\" colspan=\"4\">
    <br>

    $article_format_options

   <label for='active'><input CHECKED id='active' style=\"border:0; background-color:transparent\" type=radio value=\"active\" name=\"postpone_draft\">
    <b>Normal</b>, add article as active</label>
    <br />

     <label for='draft'><input id='draft' style=\"border:0; background-color:transparent\" type=radio value=\"draft\" name=\"postpone_draft\">
    <b>Draft</b>, add article as unapproved</label>
    <br />

    <label for='postpone'><input id='postpone' style=\"border:0; background-color:transparent\" type=radio value=\"postpone\" name=\"postpone_draft\">
    <b>Postpone</b>, make article active at</label>


       <select name=from_date_day>";


for($i=1;$i<32;$i++){
    if(date("j") == $i){ echo"<option selected value=$i>$i</option> \n"; }
   else{ echo"<option value=$i>$i</option> \n"; }
}

echo"</select> \n <select name=from_date_month> \n";

for($i=1;$i<13;$i++){
    $timestamp = mktime(0,0,0,$i,1,2003);
    if(date("n") == $i){ echo"<option selected value=$i>". date("M", $timestamp) ."</option> \n"; }
    else{ echo"<option value=$i>". date("M", $timestamp) ."</option> \n"; }
}

echo"</select> \n <select name=from_date_year>";

for($i=2005;$i<2011;$i++){
    if(date("Y") == $i){ echo"<option selected value=$i>$i</option> \n"; }
    else{ echo"<option value=$i>$i</option> \n"; }
}

    $date_hour = date("H");
    $date_minutes = date("i");
    echo"
    </select>

    @ <input value='$date_hour' title='24 Hour format [hh]' name=from_date_hour size=2 type=text /> : <input value='$date_minutes' title='Minutes [mm]' name=from_date_minutes size=2 type=text />



    </tr>

    <input type=hidden name=mod value=addnews>
        <input type=hidden name=action value=doaddnews>

        </table></form>";

    echofooter();
}
// ********************************************************************************
// Do add News to news.txt
// ********************************************************************************

elseif($action == "doaddnews")
{

/////
///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{ //Single or Not category
   //don't format $nice_cats because we have not selected any.
   if( $category !="" and isset($category) and !in_array($category,$allowed_cats) ){ die('not allowed category'); }
   $nice_category = $category;

}

// die($nice_category);


    if($member_db[1] == 3 or $postpone_draft == "draft"){
            //if the user is Journalist, add the article as unapproved
             $decide_news_file = "./data/unapproved_news.txt";
             $added_time = time()+ ($config_date_adjust*60);
             $postpone = FALSE;
             $unapproved_status_msg = "The article was marked as Unapproved!";
    }
    elseif($postpone_draft == "postpone"){

             if( !ereg("^[[:digit:]]{1,}$", $from_date_hour) or !ereg("^[[:digit:]]{1,}$", $from_date_minutes)  ){ msg("error","Error !!!","You want to add a postponed article, but the hour format is invalid.", "javascript:history.go(-1)"); }
             $postpone = TRUE;
             $added_time = mktime($from_date_hour,$from_date_minutes,0,$from_date_month,$from_date_day,$from_date_year) + ($config_date_adjust*60);
             $decide_news_file = "./data/postponed_news.txt";

    }
    else{
             $postpone = FALSE;
             $added_time = time()+ ($config_date_adjust*60);
             $decide_news_file = "./data/news.txt";
    }



    if($if_convert_new_lines == "yes"){ $n_to_br = TRUE; }
    if($if_use_html == "yes"){ $use_html = TRUE; }

        $full_story  = replace_news("add", $full_story, $n_to_br, $use_html);
        $short_story = replace_news("add", $short_story, $n_to_br, $use_html);
//        $title       = replace_news("add", $title, TRUE, $use_html);
        $title       = replace_news("add", $title, TRUE, FALSE); // HTML in title is not allowed

        if(trim($title) == "" or !$title){ msg("error","Error !!!","The title can not be blank.", "javascript:history.go(-1)"); }
        if(trim($short_story) == "" or !$short_story){ msg("error","Error !!!","The story can not be blank.", "javascript:history.go(-1)"); }


    if($member_db[7] == 1){ $added_by_email = $member_db[5]; }
    else{ $added_by_email = "none"; }


// Save The News Article In Active_News_File

        $all_db = file("$decide_news_file");

    foreach($all_db as $news_line){
            $news_arr = explode("|", $news_line);
            if($news_arr[0] == $added_time){ $added_time++; }
    }

        $news_file = fopen("$decide_news_file", "w");
        fwrite($news_file, "$added_time|$member_db[2]|$title|$short_story|$full_story|$manual_avatar|$nice_category||\n");
        foreach ($all_db as $line){ fwrite($news_file, "$line");}
        fclose($news_file);

// Add Blank Comment In The Active_Comments_File
        $old_com_db = file("./data/comments.txt");
        $new_com_db = fopen("./data/comments.txt", "w");
        fwrite($new_com_db, "$added_time|>|\n");
        foreach ($old_com_db as $line){ fwrite($new_com_db, "$line");}
        fclose($new_com_db);

// Incrase By 1 The Number of Written News for Current User
    $old_user_db = file("./data/users.db.php");
    $new_user_db = fopen("./data/users.db.php", w);
    foreach($old_user_db as $old_user_db_line){
            $old_user_db_arr = explode("|", $old_user_db_line);
            if($username!=$old_user_db_arr[2]){
                fwrite($new_user_db,"$old_user_db_line");
        }
        else
        {
                $countplus = $old_user_db_arr[6]+1;
                fwrite($new_user_db,"$old_user_db_arr[0]|$old_user_db_arr[1]|$old_user_db_arr[2]|$old_user_db_arr[3]|$old_user_db_arr[4]|$old_user_db_arr[5]|$countplus|$old_user_db_arr[7]|$old_user_db_arr[8]|$old_user_db_arr[9]||\n");
        }
    }
    fclose($new_user_db);

if($member_db[1] == 3){
//user is journalist and the article needs to be approved, Notify !!!

    if($config_notify_unapproved == "yes" and $config_notify_status == "active"){
       send_mail("$config_notify_email", "CuteNews - Unapproved article was Added", "The user $member_db[2] (journalist) posted article '$title' which needs first to be Approved.");
    }
}

    if($postpone){
        msg("info","News added (Postponed)", "The news item was successfully added to the database as postponed.<br/>It will be activated at ".date("r",$added_time));
    }
    else{
        msg("info","News added", "The news item was successfully added.</br>$unapproved_status_msg");
    }


}
?>