<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

if (User::i()->isGuest()) LabCMS::i()->error403();

if (!config::get('msg_at_page')) config::set('msg_at_page', 50);
if (!config::get('max_inbox')) config::set('max_inbox', 250);
if (!config::get('max_outbox')) config::set('max_outbox', 250);

Hook::call('lc_pm_before');

$pm=Factory::create('lc_pm.main');

if (LabCMS::i()->isAjax()) {
	$json_response=array('error'=>'Метод '.$_POST['action'].' не найден');
	LabCMS::i()->setModuleTemplate('');
	if (method_exists($pm, $method = 'pm_'.$_POST['action'])) {
		$json_response=array('error'=>0);
		if ($response=call_user_func(array($pm, $method)))
				$json_response=$response;
	}
	if (!LabCMS::i()->getModuleTemplate()) {
		if (LabCMS::i()->errors) $json_response=array('error'=>LabCMS::i()->errors[0]);
		echo real_json_encode($json_response);
	}
	return;
} else {
	$action=empty($_GET['action'])?'':$_GET['action'];
	if (method_exists($pm, 'pm_'.$action)) $method='pm_'.$action;
	else $method='pm_list';
	
	$pm->$method();
}

Hook::call('lc_pm_after');