message_edit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2024
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. //check permissions
  25. if (permission_exists('message_add') || permission_exists('message_edit')) {
  26. //access granted
  27. }
  28. else {
  29. echo "access denied";
  30. exit;
  31. }
  32. //connect to the database
  33. $database = database::new();
  34. //add multi-lingual support
  35. $language = new text;
  36. $text = $language->get();
  37. //action add or update
  38. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  39. $action = "update";
  40. $message_uuid = $_REQUEST["id"];
  41. }
  42. else {
  43. $action = "add";
  44. }
  45. //get http post variables and set them to php variables
  46. if (!empty($_POST)) {
  47. $message_uuid = $_POST["message_uuid"];
  48. $user_uuid = $_POST["user_uuid"];
  49. $message_type = $_POST["message_type"];
  50. $message_direction = $_POST["message_direction"];
  51. $message_date = $_POST["message_date"];
  52. $message_from = $_POST["message_from"];
  53. $message_to = $_POST["message_to"];
  54. $message_text = $_POST["message_text"];
  55. $message_media_type = $_POST["message_media_type"];
  56. $message_media_url = $_POST["message_media_url"];
  57. $message_media_content = $_POST["message_media_content"];
  58. $message_json = $_POST["message_json"];
  59. }
  60. //process the user data and save it to the database
  61. if (!empty($_POST) && empty($_POST["persistformvar"])) {
  62. //get the uuid from the POST
  63. if ($action == "update") {
  64. $message_uuid = $_POST["message_uuid"];
  65. }
  66. //process the http post data by submitted action
  67. if (!empty($_POST['action']) && isset($message_uuid) && is_uuid($message_uuid)) {
  68. $array[0]['checked'] = 'true';
  69. $array[0]['uuid'] = $message_uuid;
  70. switch ($_POST['action']) {
  71. case 'delete':
  72. if (permission_exists('message_delete')) {
  73. $obj = new messages;
  74. $obj->delete($array);
  75. }
  76. break;
  77. }
  78. header('Location: message_logs.php');
  79. exit;
  80. }
  81. //validate the token
  82. $token = new token;
  83. if (!$token->validate($_SERVER['PHP_SELF'])) {
  84. message::add($text['message-invalid_token'],'negative');
  85. header('Location: messages_log.php');
  86. exit;
  87. }
  88. //check for all required data
  89. $msg = '';
  90. if (empty($message_type)) { $msg .= $text['message-required']." ".$text['label-message_type']."<br>\n"; }
  91. if (empty($message_direction)) { $msg .= $text['message-required']." ".$text['label-message_direction']."<br>\n"; }
  92. if (empty($message_date)) { $msg .= $text['message-required']." ".$text['label-message_date']."<br>\n"; }
  93. if (empty($message_from)) { $msg .= $text['message-required']." ".$text['label-message_from']."<br>\n"; }
  94. if (empty($message_to)) { $msg .= $text['message-required']." ".$text['label-message_to']."<br>\n"; }
  95. //if (empty($message_text)) { $msg .= $text['message-required']." ".$text['label-message_text']."<br>\n"; }
  96. //if (empty($message_media_type)) { $msg .= $text['message-required']." ".$text['label-message_media_type']."<br>\n"; }
  97. //if (empty($message_media_url)) { $msg .= $text['message-required']." ".$text['label-message_media_url']."<br>\n"; }
  98. //if (empty($message_media_content)) { $msg .= $text['message-required']." ".$text['label-message_media_content']."<br>\n"; }
  99. //if (empty($message_json)) { $msg .= $text['message-required']." ".$text['label-message_json']."<br>\n"; }
  100. if (!empty($msg) && empty($_POST["persistformvar"])) {
  101. require_once "resources/header.php";
  102. require_once "resources/persist_form_var.php";
  103. echo "<div align='center'>\n";
  104. echo "<table><tr><td>\n";
  105. echo $msg."<br />";
  106. echo "</td></tr></table>\n";
  107. persistformvar($_POST);
  108. echo "</div>\n";
  109. require_once "resources/footer.php";
  110. return;
  111. }
  112. //add the message_uuid
  113. if (empty($_POST["message_uuid"]) || !is_uuid($_POST["message_uuid"])) {
  114. $message_uuid = uuid();
  115. }
  116. //prepare the array
  117. $array['messages'][0]['domain_uuid'] = $_SESSION["domain_uuid"];;
  118. $array['messages'][0]['user_uuid'] = $user_uuid;
  119. $array['messages'][0]['message_uuid'] = $message_uuid;
  120. $array['messages'][0]['message_type'] = $message_type;
  121. $array['messages'][0]['message_direction'] = $message_direction;
  122. $array['messages'][0]['message_date'] = $message_date;
  123. $array['messages'][0]['message_from'] = $message_from;
  124. $array['messages'][0]['message_to'] = $message_to;
  125. $array['messages'][0]['message_text'] = $message_text;
  126. $array['messages'][0]['message_uuid'] = $message_uuid;
  127. //save to the data
  128. $database->app_name = 'messages';
  129. $database->app_uuid = '4a20815d-042c-47c8-85df-085333e79b87';
  130. $database->save($array);
  131. //redirect the user
  132. if (isset($action)) {
  133. if ($action == "add") {
  134. message::add($text['message-add']);
  135. }
  136. if ($action == "update") {
  137. message::add($text['message-update']);
  138. }
  139. header('Location: message_edit.php?id='.$message_uuid);
  140. exit;
  141. }
  142. }
  143. //pre-populate the form
  144. if (!empty($_GET["id"]) && empty($_POST["persistformvar"])) {
  145. $message_uuid = $_GET["id"];
  146. $sql = "select * from v_messages ";
  147. $sql .= "where message_uuid = :message_uuid ";
  148. $parameters['message_uuid'] = $message_uuid;
  149. $row = $database->select($sql, $parameters, 'row');
  150. if (!empty($row) && @sizeof($row) != 0) {
  151. $user_uuid = $row["user_uuid"];
  152. $message_type = $row["message_type"];
  153. $message_direction = $row["message_direction"];
  154. $message_date = $row["message_date"];
  155. $message_from = $row["message_from"];
  156. $message_to = $row["message_to"];
  157. $message_text = $row["message_text"];
  158. $message_media_type = $row["message_media_type"] ?? '';
  159. $message_media_url = $row["message_media_url"] ?? '';
  160. $message_media_content = $row["message_media_content"] ?? '';
  161. $message_json = $row["message_json"];
  162. }
  163. unset($sql, $parameters);
  164. }
  165. //get the users
  166. $sql = "select user_uuid, username from v_users ";
  167. $sql .= "where domain_uuid = :domain_uuid ";
  168. $sql .= "and user_enabled = 'true' ";
  169. $sql .= "order by username asc ";
  170. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  171. $users = $database->select($sql, $parameters, 'all');
  172. unset($sql, $parameters);
  173. //create token
  174. $object = new token;
  175. $token = $object->create($_SERVER['PHP_SELF']);
  176. //include the header
  177. $document['title'] = $text['title-message'];
  178. require_once "resources/header.php";
  179. //show the content
  180. echo "<form name='frm' id='frm' method='post'>\n";
  181. echo "<div class='action_bar' id='action_bar'>\n";
  182. echo " <div class='heading'><b>".$text['title-message']."</b></div>\n";
  183. echo " <div class='actions'>\n";
  184. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','link'=>'message_logs.php']);
  185. if ($action == 'update' && permission_exists('message_delete')) {
  186. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'name'=>'btn_delete','style'=>'margin-left: 15px;','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  187. }
  188. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','style'=>'margin-left: 15px;']);
  189. echo " </div>\n";
  190. echo " <div style='clear: both;'></div>\n";
  191. echo "</div>\n";
  192. if ($action == 'update' && permission_exists('message_delete')) {
  193. echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','name'=>'action','value'=>'delete','onclick'=>"modal_close();"])]);
  194. }
  195. echo "<div class='card'>\n";
  196. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  197. echo "<tr>\n";
  198. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  199. echo " ".$text['label-username']."\n";
  200. echo "</td>\n";
  201. echo "<td width='70%' class='vtable' style='position: relative;' align='left'>\n";
  202. echo " <select class='formfld' name='user_uuid'>\n";
  203. echo " <option value=''></option>\n";
  204. if (!empty($users)) {
  205. foreach($users as $row) {
  206. echo " <option value='".escape($row['user_uuid'])."' ".($row['user_uuid'] == $user_uuid ? "selected='selected'" : null).">".escape($row['username'])."</option>\n";
  207. }
  208. }
  209. echo " </select>\n";
  210. echo "<br />\n";
  211. echo $text['description-username']."\n";
  212. echo "</td>\n";
  213. echo "</tr>\n";
  214. echo "<tr>\n";
  215. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  216. echo " ".$text['label-message_type']."\n";
  217. echo "</td>\n";
  218. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  219. echo " <select class='formfld' name='message_type'>\n";
  220. echo " <option value='sms' ".($message_type == 'sms' ? "selected='selected'" : null).">".$text['label-sms']."</option>\n";
  221. echo " <option value='mms' ".($message_type == 'mms' ? "selected='selected'" : null).">".$text['label-mms']."</option>\n";
  222. echo " <option value='chat' ".($message_type == 'chat' ? "selected='selected'" : null).">".$text['label-chat']."</option>\n";
  223. echo " </select>\n";
  224. echo "<br />\n";
  225. echo $text['description-message_type']."\n";
  226. echo "</td>\n";
  227. echo "</tr>\n";
  228. echo "<tr>\n";
  229. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  230. echo " ".$text['label-message_direction']."\n";
  231. echo "</td>\n";
  232. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  233. echo " <select class='formfld' name='message_direction'>\n";
  234. echo " <option value='inbound' ".($message_direction == 'inbound' ? "selected='selected'" : null).">".$text['label-inbound']."</option>\n";
  235. echo " <option value='outbound' ".($message_direction == 'outbound' ? "selected='selected'" : null).">".$text['label-outbound']."</option>\n";
  236. echo " </select>\n";
  237. echo "<br />\n";
  238. echo $text['description-message_direction']."\n";
  239. echo "</td>\n";
  240. echo "</tr>\n";
  241. echo "<tr>\n";
  242. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  243. echo " ".$text['label-message_date']."\n";
  244. echo "</td>\n";
  245. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  246. echo " <input class='formfld' type='text' name='message_date' maxlength='255' value=\"".escape($message_date)."\">\n";
  247. echo "<br />\n";
  248. echo $text['description-message_date']."\n";
  249. echo "</td>\n";
  250. echo "</tr>\n";
  251. echo "<tr>\n";
  252. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  253. echo " ".$text['label-message_from']."\n";
  254. echo "</td>\n";
  255. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  256. echo " <input class='formfld' type='text' name='message_from' maxlength='255' value=\"".escape($message_from)."\">\n";
  257. echo "<br />\n";
  258. echo $text['description-message_from']."\n";
  259. echo "</td>\n";
  260. echo "</tr>\n";
  261. echo "<tr>\n";
  262. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  263. echo " ".$text['label-message_to']."\n";
  264. echo "</td>\n";
  265. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  266. echo " <input class='formfld' type='text' name='message_to' maxlength='255' value=\"".escape($message_to)."\">\n";
  267. echo "<br />\n";
  268. echo $text['description-message_to']."\n";
  269. echo "</td>\n";
  270. echo "</tr>\n";
  271. echo "<tr>\n";
  272. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  273. echo " ".$text['label-message_text']."\n";
  274. echo "</td>\n";
  275. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  276. echo " <textarea class='formfld' style='min-width: 40%; height: 100px;' name='message_text'>".escape($message_text)."</textarea>\n";
  277. echo "<br />\n";
  278. echo $text['description-message_text']."\n";
  279. echo "</td>\n";
  280. echo "</tr>\n";
  281. if (!empty($message_media_type)) {
  282. echo "<tr>\n";
  283. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  284. echo " &nbsp;\n";
  285. echo "</td>\n";
  286. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  287. $image_source = 'data: '.mime_content_type($message_media_type).';base64,'.$message_media_content;
  288. echo "<img src='".$image_source."' width='100%'>";
  289. echo "<br />\n";
  290. echo "</td>\n";
  291. echo "</tr>\n";
  292. }
  293. if (!empty($_GET['debug']) && $_GET['debug'] == 'true') {
  294. echo "<tr>\n";
  295. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  296. echo " ".$text['label-message_media_type']."\n";
  297. echo "</td>\n";
  298. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  299. echo " <input class='formfld' type='text' name='message_media_type' maxlength='255' value=\"".escape($message_media_type)."\">\n";
  300. echo "<br />\n";
  301. echo $text['description-message_media_type']."\n";
  302. echo "</td>\n";
  303. echo "</tr>\n";
  304. echo "<tr>\n";
  305. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  306. echo " ".$text['label-message_media_url']."\n";
  307. echo "</td>\n";
  308. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  309. echo " <input class='formfld' type='text' name='message_media_url' maxlength='255' value=\"".escape($message_media_url)."\">\n";
  310. echo "<br />\n";
  311. echo $text['description-message_media_url']."\n";
  312. echo "</td>\n";
  313. echo "</tr>\n";
  314. echo "<tr>\n";
  315. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  316. echo " ".$text['label-message_media_content']."\n";
  317. echo "</td>\n";
  318. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  319. echo " <input class='formfld' type='text' name='message_media_content' maxlength='255' value=\"".escape($message_media_content)."\">\n";
  320. echo "<br />\n";
  321. echo $text['description-message_media_content']."\n";
  322. echo "</td>\n";
  323. echo "</tr>\n";
  324. echo "<tr>\n";
  325. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  326. echo " ".$text['label-message_json']."\n";
  327. echo "</td>\n";
  328. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  329. echo " <textarea class='formfld' style='min-width: 40%; height: 100px;' name='message_json'>".escape($message_json)."</textarea>\n";
  330. echo "<br />\n";
  331. echo $text['description-message_json']."\n";
  332. echo "</td>\n";
  333. echo "</tr>\n";
  334. }
  335. echo "</table>\n";
  336. echo "</div>\n";
  337. echo "<br /><br />\n";
  338. echo "<input type='hidden' name='message_uuid' value='".escape($message_uuid)."'>\n";
  339. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  340. echo "</form>";
  341. //include the footer
  342. require_once "resources/footer.php";
  343. ?>