<?php

/**
 * ownCloud - Documents App
 *
 * @author Victor Dubiniuk
 * @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 */

namespace OCA\Documents;

class UserController extends Controller{
	
	public static function disconnectUser($args){
		self::preDispatch();
		self::disconnect($args);
	}
	
	public static function disconnectGuest($args){
		 self::preDispatchGuest();
		 self::disconnect($args);
	}
	
	protected static function disconnect($args){
		$esId = @$_POST['esId'];
		$member = new Db\Member();
		$member->loadBy('member_id', $args['member_id']);
		if ($esId && $member->hasData()){
			if ($member->getEsId() === $esId && $member->getStatus() == Db\Member::MEMBER_STATUS_ACTIVE){
				$member->deactivate(array($args['member_id']));
				$op = new Db\Op();
				$op->removeMember($esId, $args['member_id']);
			}
		}
		\OCP\JSON::success();
		exit();
	}
	
	public static function rename($args){
		self::preDispatchGuest();
		
		$memberId = Helper::getArrayValueByKey($args, 'member_id');
		$name = Helper::getArrayValueByKey($_POST, 'name');
		$member = new Db\Member();
		$member->load($memberId);

		if ($member->getEsId()
				&& $member->getStatus() == Db\Member::MEMBER_STATUS_ACTIVE 
				&& $member->getIsGuest()
		){
			$guestMark = Db\Member::getGuestPostfix();
			if (substr($name, -strlen($guestMark)) !== $guestMark){
				$name = $name . ' ' . $guestMark;
			}
			
			$op = new Db\Op();
			$op->changeNick($member->getEsId(), $memberId, $name);
		}
		\OCP\JSON::success();
	}
	
	
	/**
	 * Stub - sends a generic avatar
	 */
	public static function sendAvatar(){
		self::preDispatch(false);
		$image = new \OC_Image('[encoded data, 7756 characters]');
		\OC_Util::obEnd();

		echo $image->show();
	}
}
