<?php
//MPM This program deletes expired announcements from phpWebsite version 0.9.3-4
//    Author: Mike Majorowicz (http://magicbeandip.com)
//    It's a hack.  

//    Use at your own risk!


//MPM This section is copied from /index.php
$GLOBALS['ALWAYS'] = array("layout""users""language""fatcat""search""menuman""comments");

// Change to TRUE to allow DEBUG mode
define("DEBUG_MODE"FALSE);

/* Show all errors */
//error_reporting (E_ALL);

/* Security against those with register globals = on */
if (isset($_POST)){
  foreach (
$_POST as $postVarName=>$nullIT) {
    unset(
$postVarName);
  }
}

//MPM These 3 lines of code fake the query string so the announcements module is loaded by the core
$_REQUEST=array('module'=>'announce');
$_GET=$_REQUEST;
$_POST=array();

//MPM This section is copied from /index.php
if (!isset($hub_dir)) {
  
$hub_dir NULL;
}

loadConfig($hub_dir);

if (
file_exists($hub_dir "core/Core.php") && file_exists($hub_dir "core/Debug.php") && !preg_match ("/:\/\//i"$hub_dir)) {
  require_once(
$hub_dir "core/Core.php");
} else {
  exit(
"FATAL ERROR! Required file <b>Core.php</b> not found.");
}

if(!isset(
$branchName)) {
  
$branchName NULL;
}

$_SESSION["core"] = new PHPWS_Core($branchName$hub_dir);
$GLOBALS["core"] = &$_SESSION["core"];

$includeList $core->initModules();

if (isset(
$_SESSION["siteHash"]) && $_SESSION["siteHash"] != $core->site_hash){
     
$core->killAllSessions();
     
$_SESSION["siteHash"] = $core->site_hash;
}

// Load module index file
$current_mod_file NULL;

//MPM Copied from mod/announce/index.php
/* Check to see if the manager exists and create it if it doesn't */
if(!isset($_SESSION["SES_ANN_MANAGER"])) {
  
$_SESSION["SES_ANN_MANAGER"] = new PHPWS_AnnouncementManager;
}

//MPM End copied from mod/announce/index.php


deleteExpiredAnnouncements();


// Preventing last mod loaded from being 'current_mod'
$core->current_mod NULL;
$core->db->disconnect();


function 
loadConfig($hub_dir){
  
/* Check for config file and define source directory. */
  
if(file_exists($hub_dir "conf/config.php")){
    if(
filesize($hub_dir "conf/config.php") > 0) {
      include(
$hub_dir "conf/config.php");
      
define("PHPWS_SOURCE_DIR"$source_dir);
    } else {
      
header("location:setup/set_config.php");
      exit();
    }
  }
  else {
    
header("location:setup/set_config.php");
    exit();
  }
}

//MPM This function is a combination of PHPWS_AnnouncementManager::syncWithFatCat() and 
//    PHPWS_Announcement::delete().  The loop structure to determine which announcements 
//    are expired is from syncWithFatCat().  The choice tidbits to actually delete the
//    announcement are from delete().

function deleteExpiredAnnouncements() {
//MPM Copied from syncWithFatCat()
    
$all_announcements $GLOBALS["core"]->sqlSelect("mod_announce");
    
$today time();

    foreach(
$all_announcements as $announce) {
        
$year       substr($announce["expiration"], 04);
        
$month      substr($announce["expiration"], 52);
        
$day        substr($announce["expiration"], 82);
        
$expiration mktime(000$month$day$year);
//MPM Display whats happening.
print "<br />\n".$announce["id"].':';
        if(
$expiration <= $today) {

print 
"<b>deleted</b> ".$announce['_subject'];
#        if(!isset($this->_id)) {
#            $this->_error("delete_failed");
#            return;
#        }
//MPM Copied from delete()
            
$GLOBALS["core"]->sqlDelete("mod_announce""id"$announce["id"]);

            if (
class_exists("PHPWS_Fatcat"))
                
PHPWS_Fatcat::purge($announce["id"], "announce");

            if (
class_exists("PHPWS_Comment")){
                
$where['module'] = "announce";
                
$where['itemId'] = $announce["id"];
                
$GLOBALS['core']->sqlDelete("mod_comments_data"$where);
            }
        }
    }
}
?>