<?php
+
/**
* @file src/Model/Mail.php
*/
namespace Friendica\Model;
-use Friendica\App;
+use Friendica\Core\L10n;
+use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Core\Worker;
-use Friendica\Database\DBM;
-use dba;
-
-require_once 'include/dba.php';
+use Friendica\Database\DBA;
+use Friendica\Network\Probe;
+use Friendica\Util\DateTimeFormat;
/**
* Class to handle private messages
*/
class Mail
{
- function send_message($recipient = 0, $body = '', $subject = '', $replyto = '')
+ /**
+ * Send private message
+ *
+ * @param integer $recipient recipient id, default 0
+ * @param string $body message body, default empty
+ * @param string $subject message subject, default empty
+ * @param string $replyto reply to
+ * @return int
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ */
+ public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
{
- $a = get_app();
+ $a = \get_app();
if (!$recipient) {
return -1;
}
if (!strlen($subject)) {
- $subject = t('[no subject]');
+ $subject = L10n::t('[no subject]');
}
- $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
- intval(local_user())
- );
- $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
- intval($recipient),
- intval(local_user())
- );
+ $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
+ $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
if (!(count($me) && (count($contact)))) {
return -2;
}
- $guid = get_guid(32);
+ $guid = System::createUUID();
$uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
$convid = 0;
if (strlen($replyto)) {
$reply = true;
- $r = q("SELECT `convid` FROM `mail` WHERE `uid` = %d AND (`uri` = '%s' OR `parent-uri` = '%s') LIMIT 1",
- intval(local_user()),
- dbesc($replyto),
- dbesc($replyto)
- );
- if (DBM::is_result($r)) {
- $convid = $r[0]['convid'];
+ $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
+ local_user(), $replyto, $replyto];
+ $mail = DBA::selectFirst('mail', ['convid'], $condition);
+ if (DBA::isResult($mail)) {
+ $convid = $mail['convid'];
}
}
+ $convuri = '';
if (!$convid) {
// create a new conversation
- $recip_host = substr($contact[0]['url'], strpos($contact[0]['url'], '://') + 3);
+ $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
$recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
- $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
+ $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
$sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
- $conv_guid = get_guid(32);
+ $conv_guid = System::createUUID();
$convuri = $recip_handle . ':' . $conv_guid;
$handles = $recip_handle . ';' . $sender_handle;
- $fields = array('uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
- 'created' => datetime_convert(), 'updated' => datetime_convert(),
- 'subject' => $subject, 'recips' => $handles);
- if (dba::insert('conv', $fields)) {
- $convid = dba::lastInsertId();
+ $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
+ 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
+ 'subject' => $subject, 'recips' => $handles];
+ if (DBA::insert('conv', $fields)) {
+ $convid = DBA::lastInsertId();
}
}
if (!$convid) {
- logger('send message: conversation not found.');
+ Logger::log('send message: conversation not found.');
return -4;
}
}
$post_id = null;
- $result = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
- `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`)
- VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
- intval(local_user()),
- dbesc($guid),
- intval($convid),
- dbesc($me[0]['name']),
- dbesc($me[0]['thumb']),
- dbesc($me[0]['url']),
- intval($recipient),
- dbesc($subject),
- dbesc($body),
- 1,
- intval($reply),
- 0,
- dbesc($uri),
- dbesc($replyto),
- datetime_convert()
+ $success = DBA::insert(
+ 'mail',
+ [
+ 'uid' => local_user(),
+ 'guid' => $guid,
+ 'convid' => $convid,
+ 'from-name' => $me['name'],
+ 'from-photo' => $me['thumb'],
+ 'from-url' => $me['url'],
+ 'contact-id' => $recipient,
+ 'title' => $subject,
+ 'body' => $body,
+ 'seen' => 1,
+ 'reply' => $reply,
+ 'replied' => 0,
+ 'uri' => $uri,
+ 'parent-uri' => $replyto,
+ 'created' => DateTimeFormat::utcNow()
+ ]
);
- if ($result) {
- $post_id = dba::lastInsertId();
+
+ if ($success) {
+ $post_id = DBA::lastInsertId();
}
/**
}
$image_uri = substr($image, strrpos($image, '/') + 1);
$image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
- q("UPDATE `photo` SET `allow_cid` = '%s'
- WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
- dbesc('<' . $recipient . '>'),
- dbesc($image_uri),
- dbesc( t('Wall Photos')),
- intval(local_user())
- );
+ Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
}
}
}
}
}
- function send_wallmessage($recipient = '', $body = '', $subject = '', $replyto = '')
+ /**
+ * @param array $recipient recipient, default empty
+ * @param string $body message body, default empty
+ * @param string $subject message subject, default empty
+ * @param string $replyto reply to, default empty
+ * @return int
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
{
if (!$recipient) {
return -1;
}
if (!strlen($subject)) {
- $subject = t('[no subject]');
+ $subject = L10n::t('[no subject]');
}
- $guid = get_guid(32);
+ $guid = System::createUUID();
$uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
$me = Probe::uri($replyto);
return -2;
}
- $conv_guid = get_guid(32);
+ $conv_guid = System::createUUID();
$recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
$handles = $recip_handle . ';' . $sender_handle;
$convid = null;
- $fields = array('uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
- 'created' => datetime_convert(), 'updated' => datetime_convert(),
- 'subject' => $subject, 'recips' => $handles);
- if (dba::insert('conv', $fields)) {
- $convid = dba::lastInsertId();
+ $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
+ 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
+ 'subject' => $subject, 'recips' => $handles];
+ if (DBA::insert('conv', $fields)) {
+ $convid = DBA::lastInsertId();
}
-
+
if (!$convid) {
- logger('send message: conversation not found.');
+ Logger::log('send message: conversation not found.');
return -4;
}
- q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
- `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
- VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
- intval($recipient['uid']),
- dbesc($guid),
- intval($convid),
- dbesc($me['name']),
- dbesc($me['photo']),
- dbesc($me['url']),
- 0,
- dbesc($subject),
- dbesc($body),
- 0,
- 0,
- 0,
- dbesc($uri),
- dbesc($replyto),
- datetime_convert(),
- 1
+ DBA::insert(
+ 'mail',
+ [
+ 'uid' => $recipient['uid'],
+ 'guid' => $guid,
+ 'convid' => $convid,
+ 'from-name' => $me['name'],
+ 'from-photo' => $me['photo'],
+ 'from-url' => $me['url'],
+ 'contact-id' => 0,
+ 'title' => $subject,
+ 'body' => $body,
+ 'seen' => 0,
+ 'reply' => 0,
+ 'replied' => 0,
+ 'uri' => $uri,
+ 'parent-uri' => $replyto,
+ 'created' => DateTimeFormat::utcNow(),
+ 'unknown' => 1
+ ]
);
return 0;