Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stanzamapper.php « db « lib - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 28b56f86d3326f6655552f25e1f811261b66c7c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

namespace OCA\OJSXC\Db;

use OCA\OJSXC\StanzaLogger;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\Mapper;
use OCP\IDb;
use OCP\IDBConnection;
use Sabre\Xml\Writer;

/**
 * Class StanzaMapper
 *
 * @package OCA\OJSXC\Db
 */
class StanzaMapper extends Mapper
{
	private $host;

	/**
	 * @var StanzaLogger
	 */
	private $stanzaLogger;

	/**
	 * StanzaMapper constructor.
	 *
	 * @param IDBConnection $db
	 * @param string $host
	 */
	public function __construct(IDBConnection $db, $host, StanzaLogger $stanzaLogger)
	{
		parent::__construct($db, 'ojsxc_stanzas');
		$this->host = $host;
		$this->stanzaLogger = $stanzaLogger;
	}

	/**
	 * @param Entity $entity
	 * @return void
	 */
	public function insert(Entity $entity)
	{
		$writer = new Writer();
		$writer->openMemory();
		$writer->write($entity);
		$xml = $writer->outputMemory();

		$this->stanzaLogger->logRaw($xml, StanzaLogger::STORING);

		$sql = "INSERT INTO `*PREFIX*ojsxc_stanzas` (`to`, `from`, `stanza`) VALUES(?,?,?)";
		$q = $this->db->prepare($sql);
		$q->execute([$entity->getUnSanitizedTo(), $entity->getUnSanitizedFrom(), $xml]);
	}


	/**
	 * @param string $to
	 * @return Stanza[]
	 * @throws DoesNotExistException
	 */
	public function findByTo($to)
	{
		$stmt = $this->execute("SELECT stanza, id FROM *PREFIX*ojsxc_stanzas WHERE `to`=?", [$to]);
		$results = [];
		while ($row = $stmt->fetch()) {
			$row['stanza'] = preg_replace('/to="([^"]*)"/', "to=\"$1@" .$this->host ."/internal\"", $row['stanza']);
			$row['stanza'] = preg_replace('/from="([^"]*)"/', "from=\"$1@" .$this->host ."/internal\"", $row['stanza']);
			$row['stanza'] = preg_replace('/jid="([^"]*)"/', "jid=\"$1@" .$this->host ."\"", $row['stanza']);
			$results[] = $this->mapRowToEntity($row);
		}
		$stmt->closeCursor();

		if (count($results) === 0) {
			throw new DoesNotExistException('Not Found');
		}

		foreach ($results as $result) {
			$this->delete($result);
		}

		return $results;
	}

	/**
	 * @brief Deletes all stanzas addressed to a user.
	 * @param $uid
	 */
	public function deleteByTo($uid)
	{
		$this->execute("DELETE FROM *PREFIX*ojsxc_stanzas WHERE `to`=?", [$uid]);
	}
}