<?php
// Master Autoindex plugin
// 8 oct 2012
// ionutvmi


$plugins->add_hook("index_search","updates_edit");

function updates_info(){
    return    array(    
    "name" => "Updates Plugin",
    "author" => "ionutvmi",
    "author_site" => "http://master-land.net",
    "description" => "This will allow you to edit the format of the updates text",
    );
}
function updates_install(){
	global $db;
	// settings 
	$settings_data = array(
	"name" => "updates_tpl", 
	"value" => $db->escape("<a href='\$link'>\$file.name</a> was added on \$date[\"d-M-Y H:i:s\"] <br/>"), 
	"title" => "Template", 
	"description" => "The template text for updates on main page. <br/> Vars: <b>\$link , \$tsince , \$size 
	<br/> \$file.name/id/path/indir/views/dcount/time/size/icon <br/>\$dir.name/id/path/indir/views/dcount/time/size/icon </b>", 
	"type" => "textarea",
	"plugin" => "updates", 
	);
	$db->insert_array(MAI_PREFIX."plugins_settings",$settings_data);
}
function updates_is_installed(){
	global $db;
	if($db->count("SELECT `name` FROM `".MAI_PREFIX."plugins_settings` WHERE `plugin`='updates'") > 0)
		return true;
	
	return false;
}
function updates_uninstall(){
	global $db;
	$db->query("DELETE FROM `".MAI_PREFIX."plugins_settings` WHERE `plugin`='updates'");
}

function updates_edit(){
	global $up_data,$set,$lang,$updates,$db;
	if($up_data){
	$updates = '';
		foreach($up_data as $udata){
			$tpl_text = $set->plugins["updates_tpl"];
			$link = "$set->url/data/file/$udata->id/".mai_converturl($udata->name).".html";
			$tsince = tsince($udata->time,$lang->time_v);
			$size = convert($udata->size);

			foreach($udata as $k=>$v)
				$tpl_text = str_replace("\$file.$k",$v,$tpl_text);
			
			if($dir = $db->get_row("SELECT * FROM `".MAI_PREFIX."files` WHERE `id`='$udata->indir'"))
				foreach($dir as $k=>$v)
					$tpl_text = str_replace("\$dir.$k",$v,$tpl_text);
			
			$tpl_text = preg_replace("~\\\$([a-z]+)\.([a-z]{1,10}+)~iUs","",$tpl_text);			
			$tpl_text = preg_replace("~\\\$date\[\"([^\"]+)\"\]~iUse",'date("$1",'.$udata->time.')',$tpl_text);
			$tpl_text = str_replace(array("\$link","\$tsince","\$size"),array($link,$tsince,$size),$tpl_text);
			$updates .= $tpl_text;
			
		}
	}
}