1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Services_Connect_Server
9{
10	private $connectlib;
11
12	function setUp()
13	{
14		global $prefs;
15
16		if ($prefs['connect_feature'] !== 'y') {
17			throw new Services_Exception(tr('Connect Feature disabled'), 403);
18		}
19		if ($prefs['connect_server_mode'] !== 'y') {
20			throw new Services_Exception(tr('Connect server mode disabled'), 403);
21		}
22		$this->connectlib = TikiLib::lib('connect_server');
23	}
24
25	function action_new($input)
26	{
27		$rdata = [];
28
29		$caplib = $this->getCaptcha();
30		$capkey = $caplib->generate();
31
32		$status = 'pending';
33		$guid = uniqid(mt_rand(), true);
34
35		$captcha = strip_tags($caplib->render());
36
37		$this->connectlib->recordConnection($status, $guid, $caplib->captcha->getWord(), true);	// save the catcha id as the data
38		// temporary fix for now, save the captcha word in there - validate doesn't seem to keep the session in this context
39
40		// send back confirm message
41		$rdata['status'] = $status;
42		$rdata['message'] = tr('Please confirm that you want to participate in Tiki Connect') . "\n" . $captcha;
43		$rdata['guid'] = $guid;
44
45		//$rdata['debug']['capkey'] = $capkey;
46		//$rdata['debug']['caplib'] = serialize($caplib);
47		return $rdata;
48	}
49
50	function action_confirm($input)
51	{
52		$rdata = [];
53
54
55		$connectData = $input->connect_data->xss();
56
57		if (! empty($connectData)) {
58			$caplib = $this->getCaptcha();
59
60			$capword = $this->connectlib->isPendingGuid($connectData['guid']);
61			//$valid = $caplib->validate(array('captcha' => array('input' => $connectData['captcha'], 'id' => $capkey)));
62			// $caplib->validate never seems to validate here
63
64			$valid = ! empty($capword) && $connectData['captcha'] === $capword;
65			if ($valid) {
66				if (! empty($capword)) {
67					$guid = $connectData['guid'];
68					$this->connectlib->removeGuid($guid, true);
69					$status = 'confirmed';
70					$this->connectlib->recordConnection($status, $guid, '', true);
71
72					// send back welcome message
73					$rdata['status'] = $status;
74					$rdata['message'] = tra('Welcome to Tiki Connect, please click "Send Info" when you want to make a connection.');
75					$rdata['guid'] = $guid;
76				} else {
77					$rdata['status'] = 'error';
78					$rdata['message'] = tra('There was a problem at the server (Tiki Connect is still experimental).');
79				}
80			} else {
81				$this->connectlib->removeGuid($connectData['guid'], true);
82				$status = 'error';
83				$message = tra('CAPTCHA code problem.') . "\n" . $caplib->getErrors();
84				$this->connectlib->recordConnection($status, $connectData['guid'], $message, true);
85				$rdata['status'] = $status;
86				$rdata['message'] = $message;
87				//$rdata['debug']['capkey'] = $capkey;
88				//$rdata['debug']['caplib'] = serialize($caplib);
89			}
90		}
91		return $rdata;
92	}
93
94	function action_receive($input)
95	{
96		$rdata = [];
97
98		$connectData = $input->connect_data->xss();
99		if (! empty($connectData)) {
100			$guid = $connectData['guid'];
101
102			if ($this->connectlib->isConfirmedGuid($guid)) {
103				$status = 'received';
104
105				$this->connectlib->recordConnection($status, $guid, $connectData, true);
106
107				$rdata = [
108					'status' => $status,
109					'message' => tra('Connect data received, thanks'),
110				];
111			} else {	// guid not recorded here
112				$status = 'error';
113				$message = tra('Your Tiki site is not registered here yet. Please try again.');
114				$this->connectlib->recordConnection($status, $guid, $message, true);
115				$rdata = [
116					'status' => $status,
117					'newguid' => uniqid(mt_rand(), true),
118					'message' => $message,
119				];
120			}
121		}
122		return $rdata;
123	}
124
125	function action_cancel($input)
126	{
127
128		$connectData = $input->connect_data->xss();
129		$guid = $connectData['guid'];
130		$isPending = $this->connectlib->isPendingGuid($guid);
131
132		if ($guid && ! empty($isPending)) {
133			$this->connectlib->removeGuid($guid, true);
134		}
135		return $guid . ' "' . $isPending . '"';
136	}
137
138	private function getCaptcha()
139	{
140		$captchalib = TikiLib::lib('captcha');
141		$caplib = new Captcha('dumb');
142		$caplib->captcha->setKeepSession(true)->setUseNumbers(false)->setWordlen(5);
143		return $caplib;
144	}
145}
146