<?PHP

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Mass Delete
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
if($action == "mass_delete"){

if(!$selected_news){ msg("error", "Error", "You have not specified any articles", "$PHP_SELF?mod=editnews&action=list&source=$source"); }

echoheader("options", "Delete News");
echo "<form method=post action=\"$PHP_SELF\"><table border=0 cellpading=0 cellspacing=0 width=100% height=100%><tr><td >
Are you sure you want to delete all selected news (<b>".count($selected_news)."</b>) ?<br><br>
<input type=button value=\" No \" onclick=\"javascript:document.location='$PHP_SELF?mod=editnews&action=list&source=$source'\"> &nbsp; <input type=submit value=\"   Yes   \">
<input type=hidden name=action value=\"do_mass_delete\">
<input type=hidden name=mod value=\"massactions\">
<input type=hidden name=source value=\"$source\">";
foreach($selected_news as $newsid){
        echo "<input type=hidden name=selected_news[] value=\"$newsid\">\n";
}
echo "</td></tr></table></form>";

echofooter();
exit;

}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Do Mass Delete
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
elseif($action == "do_mass_delete"){
if(!$selected_news){ msg("error", "Error", "You have not specified any articles to be deleted", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
if($source == ""){ $news_file = "data/news.txt"; $comm_file = "data/comments.txt";}
elseif($source == "postponed"){ $news_file = "data/postponed_news.txt"; $comm_file = "data/comments.txt"; }
elseif($source == "unapproved"){ $news_file = "data/unapproved_news.txt"; $comm_file = "data/comments.txt"; }
else{ $news_file = "./data/archives/$source.news.arch"; $comm_file = "./data/archives/$source.comments.arch"; }

$deleted_articles = 0;

// Delete News
        $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(@!in_array($old_db_arr[0], $selected_news)){
                        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){ fwrite($new_db,"$old_db_line"); }
                else{$deleted_articles ++;}
                        }
        }
        fclose($new_db);

// Delete Comments
        $old_db = file("$comm_file");
        $new_db = fopen("$comm_file", w);
        foreach($old_db as $old_db_line){
                $old_db_arr = explode("|", $old_db_line);
                if(@!in_array($old_db_arr[0], $selected_news)){
                        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){ fwrite($new_db,"$old_db_line"); }
                else{ /* Do Nothing => Delete :) */ }
                        }
        }
        fclose($new_db);


if(count($selected_news) == $deleted_articles){ msg("info", "Deleted News", "All articles that you selected (<b>$deleted_articles</b>) were deleted", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
else{ msg("error", "Deleted News (some errors occured !!!)", "$deleted_articles of ".count($selected_news)." articles that you selected were deleted", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Mass Approve
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
elseif($action == "mass_approve"){
if($member_db[1] != 1 and $member_db[1] != 2){ msg("error", "Error", "You do not have permissions for this action.", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
if(!$selected_news){ msg("error", "Error", "You have not specified any articles", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
$news_file = "./data/unapproved_news.txt";

        $approved_articles = 0;
        $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(@!in_array($old_db_arr[0], $selected_news)){
                        fwrite($new_db,"$old_db_line");
                }
                else
                {
                //Move the article to Active News

                         $all_active_db = file("$cutepath/data/news.txt");
                         $active_news_file = fopen("$cutepath/data/news.txt", "w");
                         @flock ($active_news_file,2);

                         fwrite($active_news_file,"$old_db_line");
                         foreach ($all_active_db as $active_line){ fwrite($active_news_file, "$active_line");}
                         @flock ($active_news_file,3);
                         fclose($active_news_file);
                         $approved_articles++;
                }
        }
        fclose($new_db);
if(count($selected_news) == $approved_articles){ msg("info", "News Approved", "All articles that you selected ($approved_articles) were approved and are now active", "$PHP_SELF?mod=editnews&action=list"); }
else{ msg("error", "News Approved (with errors)", "$approved_articles of ".count($selected_news)." articles that you selected were approved", "$PHP_SELF?mod=editnews&action=list"); }

exit;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Mass Move to Cat
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
elseif($action == "mass_move_to_cat"){

if(!$selected_news){ msg("error", "Error", "You have not specified any articles", "$PHP_SELF?mod=editnews&action=list&source=$source"); }

    $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];
              }

        }


echoheader("options", "Move Articles to Category");

echo "<form action=\"$PHP_SELF\" method=post><table border=0 cellpading=0 cellspacing=0 width=100% height=100%><tr><td >Move selected articles (<b>".count($selected_news)."</b>) to category:";


    echo'<table width="80%" border="0" cellspacing="0" cellpadding="0" class="panel">';

    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]}'>
        <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>";


echo "</table>";



foreach($selected_news as $newsid){
        echo "<input type=hidden name=selected_news[] value=\"$newsid\">";
}

echo "<br><input type=hidden name=action value=\"do_mass_move_to_cat\"><input type=hidden name=source value=\"$source\"><input type=hidden name=mod value=\"massactions\">&nbsp;<input type=submit value=\"Move\"></td></tr></table></form>";

echofooter();
exit;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  DO Mass Move to One Category
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
elseif($action == "do_mass_move_to_cat"){

if($member_db[1] != 1){ msg("error", "Error", "You do not have permissions for this action.", "$PHP_SELF?mod=editnews&action=list&source=$source"); }

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


    $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];
              }

        }


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

    $nice_category = '';
    $ccount = 0;

    foreach($category as $ckey=>$cvalue){
            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.
   $nice_category = $category;
}

if(!$selected_news){ msg("error", "Error", "You have not specified any articles", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
if($source == ""){ $news_file = "./data/news.txt";}
elseif($source == "postponed"){ $news_file = "data/postponed_news.txt"; }
elseif($source == "unapproved"){ $news_file = "data/unapproved_news.txt"; }
else{ $news_file = "./data/archives/$source.news.arch"; }
        $moved_articles = 0;
        $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(@!in_array($old_db_arr[0], $selected_news)){
                        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){ fwrite($new_db,"$old_db_line"); }
                else{

                        fwrite($new_db,"$old_db_arr[0]|$old_db_arr[1]|$old_db_arr[2]|$old_db_arr[3]|$old_db_arr[4]|$old_db_arr[5]|$nice_category|||\n");
                        $moved_articles ++;
                }
                        }
        }
        fclose($new_db);
if(count($selected_news) == $moved_articles){ msg("info", "News Moved", "All articles that you selected ($moved_articles) were moved to the specified category", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
else{ msg("error", "News Moved (with errors)", "$moved_articles of ".count($selected_news)." articles that you selected were moved to the specified category", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Mass Archive
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
elseif($action == "mass_archive"){
if(!$selected_news){ msg("error", "Error", "You have not specified any articles", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
if($source != ""){ msg("error", "Error", "These news are already archived or are in postpone queue", "$PHP_SELF?mod=editnews&action=list&source=$source"); }

echoheader("options", "Send News To Archive");

echo "<form method=post action=\"$PHP_SELF\"><table border=0 cellpading=0 cellspacing=0 width=100% height=100%><tr><td >
Are you sure you want to send all selected news (<b>".count($selected_news)."</b>) to the archive ?<br><br>
<input type=button value=\" No \" onclick=\"javascript:document.location='$PHP_SELF?mod=editnews&action=list&source=$source'\"> &nbsp; <input type=submit value=\"   Yes   \">
<input type=hidden name=action value=\"do_mass_archive\">
<input type=hidden name=mod value=\"massactions\">";
foreach($selected_news as $newsid){
        echo"<input type=hidden name=selected_news[] value=\"$newsid\">\n";
}
echo"</td></tr></table></form>";

echofooter();
exit;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  DO Mass Send To Archive
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
elseif($action == "do_mass_archive"){
if($member_db[1] != 1){ msg("error", "Access Denied", "You can not perfor this action if you are not admin"); }
if(!$selected_news){ msg("error", "Error", "You have not specified any articles", "$PHP_SELF?mod=editnews&action=list&source=$source"); }
if(!is_writable("./data/archives/")){ msg("error", "Error", "The ./data/archives/ directory is not writable, CHMOD it to 777"); }
$news_file = "./data/news.txt";
$comm_file = "./data/comments.txt";

    $prepeared_for_archive = array();
    $prepeared_comments_for_archive = array();
        $archived_news = 0;

// Prepear the news for Archiving

        $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(@!in_array($old_db_arr[0], $selected_news)){
                        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  ){  fwrite($new_db,"$old_db_line");  }
                else{
                    $prepeared_news_for_archive[] = $old_db_line;
                    $archived_news++;
                }
                        }
        }
        fclose($new_db);

    if($archived_news == 0){ msg("error", "Error", "No news were found for archiving."); }

// Prepear the comments for Archiving

        $old_db = file("$comm_file");
        $new_db = fopen("$comm_file", w);
        foreach($old_db as $old_db_line){
                $old_db_arr = explode("|", $old_db_line);
                if(@!in_array($old_db_arr[0], $selected_news)){
                        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){ fwrite($new_db,"$old_db_line"); }
                else{
                    $prepeared_comments_for_archive[] = $old_db_line;
                }
                        }
        }
        fclose($new_db);

// Start Archiving

$arch_name = time()+($config_date_adjust*60);

        $arch_news = fopen("./data/archives/$arch_name.news.arch", w);
    foreach($prepeared_news_for_archive as $item){
                fwrite($arch_news, "$item");
    }
    fclose($arch_news);

        $arch_comm = fopen("./data/archives/$arch_name.comments.arch", w);
    foreach($prepeared_comments_for_archive as $item){
                fwrite($arch_comm, "$item");
    }
    fclose($arch_comm);

msg("info", "News Archived", "All articles that you selected ($archived_news) are now archived under ./data/archives/<b>$arch_name</b>.news.arch", "$PHP_SELF?mod=editnews&action=list&source=$source");
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  If No Action Is Choosed
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
else{

msg("info", "Choose Action", "Please choose action from the drop-down menu", "$PHP_SELF?mod=editnews&action=list&source=$source");

}
?>