1: <?php
2: /**
3: * Private message module
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package pm
15: * @since 2.3.0
16: * @author Jan Pedersen
17: * @author Taiwen Jiang <phppp@users.sourceforge.net>
18: */
19:
20: /**
21: * {description}
22: *
23: * @package pm
24: *
25: * @author Kazumi Ono <onokazu@xoops.org>
26: * @author Taiwen Jiang <phppp@users.sourceforge.net>
27: */
28: class PmMessage extends XoopsObject
29: {
30: //PHP 8.2 Dynamic properties deprecated
31: public $msg_id;
32: public $msg_image;
33: public $subject;
34: public $from_userid;
35: public $to_userid;
36: public $msg_time;
37: public $msg_text;
38: public $read_msg;
39: public $from_delete;
40: public $to_delete;
41: public $from_save;
42: public $to_save;
43:
44: /**
45: *
46: */
47: public function __construct()
48: {
49: parent::__construct();
50: $this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
51: $this->initVar('msg_image', XOBJ_DTYPE_OTHER, 'icon1.gif', false, 100);
52: $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, true, 255);
53: $this->initVar('from_userid', XOBJ_DTYPE_INT, null, true);
54: $this->initVar('to_userid', XOBJ_DTYPE_INT, null, true);
55: $this->initVar('msg_time', XOBJ_DTYPE_INT, time(), false);
56: $this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
57: $this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
58: $this->initVar('from_delete', XOBJ_DTYPE_INT, 1, false);
59: $this->initVar('to_delete', XOBJ_DTYPE_INT, 0, false);
60: $this->initVar('from_save', XOBJ_DTYPE_INT, 0, false);
61: $this->initVar('to_save', XOBJ_DTYPE_INT, 0, false);
62: }
63: }
64:
65: /**
66: * Class PmMessageHandler
67: */
68: class PmMessageHandler extends XoopsPersistableObjectHandler
69: {
70: /**
71: * @param null|XoopsDatabase $db
72: */
73: public function __construct(XoopsDatabase $db)
74: {
75: parent::__construct($db, 'priv_msgs', 'PmMessage', 'msg_id', 'subject');
76: }
77:
78: /**
79: * Mark a message as read
80: *
81: * @param PmMessage $pm {@link PmMessage} object
82: * @param int $val
83: *
84: * @return bool
85: */
86: public function setRead(PmMessage $pm, $val = 1)
87: {
88: return $this->updateAll('read_msg', (int)$val, new Criteria('msg_id', $pm->getVar('msg_id')), true);
89: }
90:
91: /**
92: * Mark a message as from_delete = 1 or removes it if the recipient has also deleted it
93: * @param PmMessage $pm {@link PmMessage} object
94: * @param int $val
95: * @return bool
96: */
97: public function setFromdelete(PmMessage $pm, $val = 1)
98: {
99: if ($pm->getVar('to_delete') == 0) {
100: return $this->updateAll('from_delete', (int)$val, new Criteria('msg_id', $pm->getVar('msg_id')));
101: } else {
102: return parent::delete($pm);
103: }
104: }
105:
106: /**
107: * Mark a message as to_delete = 1 or removes it if the sender has also deleted it or sent by anonymous
108: * @param PmMessage $pm {@link PmMessage} object
109: * @param int $val
110: * @return bool
111: */
112: public function setTodelete(PmMessage $pm, $val = 1)
113: {
114: $val = (int)$val;
115: if ($pm->getVar('from_delete') == 0 || $pm->getVar('from_userid') == 0) {
116: $pm->setVar('to_delete', $val);
117: if ($val) {
118: $pm->setVar('read_msg', 1);
119: }
120: return $this->insert($pm);
121: } else {
122: return parent::delete($pm);
123: }
124: }
125:
126: /**
127: * Mark a message as from_save = 1
128: * @param PmMessage $pm {@link PmMessage} object
129: * @param int $val
130: * @return bool
131: */
132: public function setFromsave(PmMessage $pm, $val = 1)
133: {
134: return $this->updateAll('from_save', (int)$val, new Criteria('msg_id', $pm->getVar('msg_id')));
135: }
136:
137: /**
138: * Mark a message as to_save = 1
139: * @param PmMessage $pm {@link PmMessage} object
140: * @param int $val
141: * @return bool
142: */
143: public function setTosave(PmMessage $pm, $val = 1)
144: {
145: return $this->updateAll('to_save', (int)$val, new Criteria('msg_id', $pm->getVar('msg_id')));
146: }
147:
148: /**
149: * get user's message count in savebox
150: * @param XoopsUser $user
151: * @return int
152: **/
153: public function getSavecount(XoopsUser $user = null)
154: {
155: if (!is_object($user)) {
156: $user =& $GLOBALS['xoopsUser'];
157: }
158: $crit_to = new CriteriaCompo(new Criteria('to_delete', 0));
159: $crit_to->add(new Criteria('to_save', 1));
160: $crit_to->add(new Criteria('to_userid', $user->getVar('uid')));
161: $crit_from = new CriteriaCompo(new Criteria('from_delete', 0));
162: $crit_from->add(new Criteria('from_save', 1));
163: $crit_from->add(new Criteria('from_userid', $user->getVar('uid')));
164: $criteria = new CriteriaCompo($crit_to);
165: $criteria->add($crit_from, 'OR');
166:
167: return $this->getCount($criteria);
168: }
169:
170: /**
171: * Send a message to user's email
172: * @param PmMessage $pm message object
173: * @param XoopsUser $user user object
174: * @return bool
175: **/
176: public function sendEmail(PmMessage $pm, XoopsUser $user)
177: {
178: global $xoopsConfig;
179:
180: if (!is_object($user)) {
181: $user =& $GLOBALS['xoopsUser'];
182: }
183: $msg = sprintf(_PM_EMAIL_DESC, $user->getVar('uname'));
184: $msg .= "\n\n";
185: $msg .= formatTimestamp($pm->getVar('msg_time'));
186: $msg .= "\n";
187: $from = new XoopsUser($pm->getVar('from_userid'));
188: $to = new XoopsUser($pm->getVar('to_userid'));
189: $msg .= sprintf(_PM_EMAIL_FROM, $from->getVar('uname') . ' (' . XOOPS_URL . '/userinfo.php?uid=' . $pm->getVar('from_userid') . ')');
190: $msg .= "\n";
191: $msg .= sprintf(_PM_EMAIL_TO, $to->getVar('uname') . ' (' . XOOPS_URL . '/userinfo.php?uid=' . $pm->getVar('to_userid') . ')');
192: $msg .= "\n";
193: $msg .= _PM_EMAIL_MESSAGE . ":\n";
194: $msg .= "\n" . $pm->getVar('subject') . "\n";
195: $msg .= "\n" . strip_tags(str_replace(array('<p>', '</p>', '<br>', '<br>'), "\n", $pm->getVar('msg_text'))) . "\n\n";
196: $msg .= "--------------\n";
197: $msg .= $xoopsConfig['sitename'] . ': ' . XOOPS_URL . "\n";
198:
199: $xoopsMailer = xoops_getMailer();
200: $xoopsMailer->useMail();
201: $xoopsMailer->setToEmails($user->getVar('email'));
202: $xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
203: $xoopsMailer->setFromName($xoopsConfig['sitename']);
204: $xoopsMailer->setSubject(sprintf(_PM_EMAIL_SUBJECT, $pm->getVar('subject')));
205: $xoopsMailer->setBody($msg);
206:
207: return $xoopsMailer->send();
208: }
209:
210: /**
211: * Get {@link XoopsForm} for setting prune criteria
212: *
213: * @return XoopsForm
214: **/
215: public function getPruneForm()
216: {
217: include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
218: $form = new XoopsThemeForm(_PM_AM_PRUNE, 'form', 'prune.php', 'post', true);
219:
220: $form->addElement(new XoopsFormDateTime(_PM_AM_PRUNEAFTER, 'after'));
221: $form->addElement(new XoopsFormDateTime(_PM_AM_PRUNEBEFORE, 'before'));
222: $form->addElement(new XoopsFormRadioYN(_PM_AM_ONLYREADMESSAGES, 'onlyread', 1));
223: $form->addElement(new XoopsFormRadioYN(_PM_AM_INCLUDESAVE, 'includesave', 0));
224: $form->addElement(new XoopsFormRadioYN(_PM_AM_NOTIFYUSERS, 'notifyusers', 0));
225:
226: $form->addElement(new XoopsFormHidden('op', 'prune'));
227: $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
228:
229: return $form;
230: }
231: }
232: