<?php
@session_start();
@error_reporting ( E_ALL ^ E_WARNING ^ E_NOTICE );
@ini_set ( 'display_errors', true );
@ini_set ( 'html_errors', false );
@ini_set ( 'error_reporting', E_ALL ^ E_WARNING ^ E_NOTICE );

define('DATALIFEENGINE', true);
define( 'ROOT_DIR', substr( dirname(  __FILE__ ), 0, -12 ) );
define( 'SYSTEM_DIR', ROOT_DIR . '/system' );

include SYSTEM_DIR.'/data/config.php';
require_once SYSTEM_DIR.'/classes/mysql.php';
require_once SYSTEM_DIR.'/data/dbconfig.php';
require_once SYSTEM_DIR.'/inc/include/functions.inc.php';
require_once SYSTEM_DIR.'/modules/sitelogin.php';

if(($member_id['user_group'] != 1)) {die ("error");}

$user_group = $cache->get( "usergroup" );
if( ! $user_group ) {
	$user_group = array ();
	$db->query( "SELECT * FROM " . USERPREFIX . "_usergroups ORDER BY id ASC" );
	while ( $row = $db->get_row() ) {
		$user_group[$row['id']] = array ();
		foreach ( $row as $key => $value ) {
			$user_group[$row['id']][$key] = stripslashes($value);
		}
	
	}
	$cache->set( "usergroup", $user_group );
	$db->free();
}

if ($_REQUEST['user_hash'] == "" OR $_REQUEST['user_hash'] != $dle_login_hash) {die ("error");}

require_once ROOT_DIR.'/language/'.$config['langs'].'/adminpanel.lng';
include_once SYSTEM_DIR . '/classes/parse.class.php';

@header("Content-type: text/html; charset=".$config['charset']);

$startfrom = intval($_POST['startfrom']);
$buffer = "";
$step = 0;

if ( intval( $config['tag_img_width'] ) ) $count_per_step = 5; else $count_per_step = 50;

if($_POST['area'] == "forum" ){
    $parse = new ParseFilter(Array(), Array(), 1, 1);
    $result = $db->query("SELECT pid, post_text FROM " . PREFIX . "_forum_posts LIMIT ".$startfrom.", ".$count_per_step);
    while($row = $db->get_row($result)){
        $row['post_text'] = $parse->decodeBBCodes($row['post_text'], false);
        $post_text = $parse->process($row['post_text']);
        $post_text = $db->safesql($parse->BB_Parse($post_text, true));
        $db->query( "UPDATE " . PREFIX . "_forum_posts SET post_text='$post_text' WHERE pid='{$row['pid']}'" );
        $step++;
    }
    $rebuildcount = $startfrom + $step;
    $buffer = "{\"status\": \"ok\",\"rebuildcount\": {$rebuildcount}}";
    echo $buffer;
} elseif ($_POST['area'] == "comments" ) {
    $commparse = new ParseFilter( );
    //$commparse->safe_mode = true;

    $result = $db->query("SELECT id, text FROM " . PREFIX . "_comments LIMIT ".$startfrom.", ".$count_per_step);

    while($row = $db->get_row($result)){
        $row['text'] = $commparse->decodeBBCodes( $row['text'], false );
        $text = $commparse->process( $row['text'] );
        $text = $db->safesql($commparse->BB_Parse( $text, false ));

        $db->query( "UPDATE " . PREFIX . "_comments SET text='$text' WHERE id='{$row['id']}'" );

        $step++;
    }

    $rebuildcount = $startfrom + $step;
    $buffer = "{\"status\": \"ok\",\"rebuildcount\": {$rebuildcount}}";
    echo $buffer;
} elseif ($_POST['area'] == "static" ) {

	$parse = new ParseFilter( Array (), Array (), 1, 1 );
	$result = $db->query("SELECT id, template FROM " . PREFIX . "_static LIMIT ".$startfrom.", ".$count_per_step);

	while($row = $db->get_row($result)){
		$row['template'] = $parse->decodeBBCodes( $row['template'], false );
		$template = $parse->process( $row['template'] );
		$template = $db->safesql($parse->BB_Parse( $template, true ));

		$db->query( "UPDATE " . PREFIX . "_static SET template='$template' WHERE id='{$row['id']}'" );

		$step++;
	}

	$rebuildcount = $startfrom + $step;
	$buffer = "{\"status\": \"ok\",\"rebuildcount\": {$rebuildcount}}";
	echo $buffer;

} else {

	$parse = new ParseFilter( Array (), Array (), 1, 1 );
	$result = $db->query("SELECT id, short_story, full_story, title FROM " . PREFIX . "_post LIMIT ".$startfrom.", ".$count_per_step);
	
	while($row = $db->get_row($result)){
		$row['short_story'] = $parse->decodeBBCodes( $row['short_story'], false );
		$row['full_story'] = $parse->decodeBBCodes( $row['full_story'], false );

		$short_story = $parse->process( $row['short_story'] );
		$full_story = $parse->process( $row['full_story'] );

		$full_story = $db->safesql( $parse->BB_Parse( $row['full_story'], false ) );
		$short_story = $db->safesql( $parse->BB_Parse( $row['short_story'], false ) );

		$db->query( "UPDATE " . PREFIX . "_post SET short_story='$short_story', full_story='$full_story' WHERE id='{$row['id']}'" );

		$step++;
	}
	
	$cache->clear();
	$rebuildcount = $startfrom + $step;
	$buffer = "{\"status\": \"ok\",\"rebuildcount\": {$rebuildcount}}";
	echo $buffer;
}
?>