<?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){
		$uid = self::preDispatch();
		 self::disconnect($args, $uid);
	}
	
	public static function disconnectGuest($args){
		 self::preDispatchGuest();
		 self::disconnect($args, false);
	}
	
	protected static function disconnect($args, $uid){
		$esId = @$_POST['esId'];
		$member = new Db_Member();
		$member->loadBy('member_id', $args['member_id']);
		if ($esId && $member->hasData()){
			$memberData = $member->getData();
			if ($memberData['es_id']===$esId){
				$member->deactivate(array($args['member_id']));
				$op = new Db_Op();
				$op->removeMember($esId, $args['member_id']);
			}
		}
		\OCP\JSON::success();
	}
	
	/**
	 * Invite users to the editing session
	 */
	public static function invite(){
		self::preDispatch();
		$invitees = @$_POST['users'];
		
		if (is_array($invitees)){
			$invitees = array_unique($invitees);
		
			$esId = @$_POST['esId'];
			foreach ($invitees as $userId){
				try {
					Invite::add($esId, $userId);
				} catch (\Exception $e) {
					
				}
			}
		}
		\OCP\JSON::success();
	}
	
	/**
	 * Stub - sends a generic avatar
	 */
	public static function sendAvatar(){
		$uid = self::preDispatch(false);
		$image = new \OC_Image('[encoded data, 7756 characters]');
		\OC_Util::obEnd();

		echo $image->show();
	}
}
