require_once $SYSTEM_ROOT . '/core/include/init.inc';
if ($report_only) {
    echo "Following Bodycopy Div asset(s) contains unsafe keywords in the content file:\n\n";
} else {
    echo "Fixing the unsafe keyword(s) in content file for following Bodycopy Div asset(s):\n\n";
}
$root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
    echo "ERROR: Failed logging in as root user\n";
    exit;
}
// Get all the bodycopy divs
$assetids = array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren(1, 'bodycopy_div'));
$count = 0;
foreach ($assetids as $assetid) {
    $data_dir = $SYSTEM_ROOT . '/data/private/' . asset_data_path_suffix('bodycopy_div', $assetid) . '/content_file.php';
    if (!is_file($data_dir)) {
        continue;
    }
    $file_content = file_get_contents($data_dir);
    // Extract the keyword replacements in the content file
    preg_match_all('|echo \\(isset\\(\\$keyword_replacements\\["(.*?)\\]\\)\\) \\?|mis', $file_content, $matches);
    if (empty($matches[1])) {
        // No keywords in the content
        continue;
    }
    $keywords_str = '';
    foreach ($matches[1] as $keyword) {
        $keywords_str .= trim($keyword, '"');
    }
    if ($keywords_str != htmlentities(html_entity_decode($keywords_str))) {
$sql = 'DELETE FROM sq_ast_wflow WHERE ' . $in_assetid;
MatrixDAL::executeSql($sql);
echo "\tUpdating permissions table...\n";
$sql = 'DELETE FROM sq_ast_perm WHERE ' . $in_assetid;
MatrixDAL::executeSql($sql);
echo "\tUpdating roles table...\n";
$sql = 'DELETE FROM sq_ast_role WHERE ' . $in_assetid;
MatrixDAL::executeSql($sql);
echo "\tUpdating shadow link table...\n";
$sql = 'DELETE FROM sq_shdw_ast_lnk WHERE ' . $in_majorid;
MatrixDAL::executeSql($sql);
$GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
$GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
echo "\tDeleting asset data directories...\n";
require_once SQ_FUDGE_PATH . '/general/file_system.inc';
foreach ($unquoted_assetids as $sub_assetid) {
    $data_path_suffix = asset_data_path_suffix('form_submission', $sub_assetid);
    $data_path = SQ_DATA_PATH . '/private/' . $data_path_suffix;
    $data_path_public = SQ_DATA_PATH . '/public/' . $data_path_suffix;
    if (is_dir($data_path)) {
        if (!delete_directory($data_path)) {
            trigger_error("Could not delete private data directory for Form Submission (Id: #{$assetid})", E_USER_WARNING);
        }
    }
    if (is_dir($data_path_public)) {
        if (!delete_directory($data_path_public)) {
            trigger_error("Could not delete public data directory for Form Submission (Id: #{$assetid})", E_USER_WARNING);
        }
    }
}
echo "Done\n";