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) 2023
  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. //add multi-lingual support
  33. $language = new text;
  34. $text = $language->get();
  35. //action add or update
  36. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  37. $action = "update";
  38. $message_uuid = $_REQUEST["id"];
  39. }
  40. else {
  41. $action = "add";
  42. }
  43. //get http post variables and set them to php variables
  44. if (!empty($_POST)) {
  45. $message_uuid = $_POST["message_uuid"];
  46. $user_uuid = $_POST["user_uuid"];
  47. $message_type = $_POST["message_type"];
  48. $message_direction = $_POST["message_direction"];
  49. $message_date = $_POST["message_date"];
  50. $message_from = $_POST["message_from"];
  51. $message_to = $_POST["message_to"];
  52. $message_text = $_POST["message_text"];
  53. $message_media_type = $_POST["message_media_type"];
  54. $message_media_url = $_POST["message_media_url"];
  55. $message_media_content = $_POST["message_media_content"];
  56. $message_json = $_POST["message_json"];
  57. }
  58. //process the user data and save it to the database
  59. if (!empty($_POST) && empty($_POST["persistformvar"])) {
  60. //get the uuid from the POST
  61. if ($action == "update") {
  62. $message_uuid = $_POST["message_uuid"];
  63. }
  64. //process the http post data by submitted action
  65. if (!empty($_POST['action']) && isset($message_uuid) && is_uuid($message_uuid)) {
  66. $array[0]['checked'] = 'true';
  67. $array[0]['uuid'] = $message_uuid;
  68. switch ($_POST['action']) {
  69. case 'delete':
  70. if (permission_exists('message_delete')) {
  71. $obj = new messages;
  72. $obj->delete($array);
  73. }
  74. break;
  75. }
  76. header('Location: message_logs.php');
  77. exit;
  78. }
  79. //validate the token
  80. $token = new token;
  81. if (!$token->validate($_SERVER['PHP_SELF'])) {
  82. message::add($text['message-invalid_token'],'negative');
  83. header('Location: messages_log.php');
  84. exit;
  85. }
  86. //check for all required data
  87. $msg = '';
  88. if (empty($message_type)) { $msg .= $text['message-required']." ".$text['label-message_type']."<br>\n"; }
  89. if (empty($message_direction)) { $msg .= $text['message-required']." ".$text['label-message_direction']."<br>\n"; }
  90. if (empty($message_date)) { $msg .= $text['message-required']." ".$text['label-message_date']."<br>\n"; }
  91. if (empty($message_from)) { $msg .= $text['message-required']." ".$text['label-message_from']."<br>\n"; }
  92. if (empty($message_to)) { $msg .= $text['message-required']." ".$text['label-message_to']."<br>\n"; }
  93. //if (empty($message_text)) { $msg .= $text['message-required']." ".$text['label-message_text']."<br>\n"; }
  94. //if (empty($message_media_type)) { $msg .= $text['message-required']." ".$text['label-message_media_type']."<br>\n"; }
  95. //if (empty($message_media_url)) { $msg .= $text['message-required']." ".$text['label-message_media_url']."<br>\n"; }
  96. //if (empty($message_media_content)) { $msg .= $text['message-required']." ".$text['label-message_media_content']."<br>\n"; }
  97. //if (empty($message_json)) { $msg .= $text['message-required']." ".$text['label-message_json']."<br>\n"; }
  98. if (!empty($msg) && empty($_POST["persistformvar"])) {
  99. require_once "resources/header.php";
  100. require_once "resources/persist_form_var.php";
  101. echo "<div align='center'>\n";
  102. echo "<table><tr><td>\n";
  103. echo $msg."<br />";
  104. echo "</td></tr></table>\n";
  105. persistformvar($_POST);
  106. echo "</div>\n";
  107. require_once "resources/footer.php";
  108. return;
  109. }
  110. //add the message_uuid
  111. if (empty($_POST["message_uuid"]) || !is_uuid($_POST["message_uuid"])) {
  112. $message_uuid = uuid();
  113. }
  114. //prepare the array
  115. $array['messages'][0]['domain_uuid'] = $_SESSION["domain_uuid"];;
  116. $array['messages'][0]['user_uuid'] = $user_uuid;
  117. $array['messages'][0]['message_uuid'] = $message_uuid;
  118. $array['messages'][0]['message_type'] = $message_type;
  119. $array['messages'][0]['message_direction'] = $message_direction;
  120. $array['messages'][0]['message_date'] = $message_date;
  121. $array['messages'][0]['message_from'] = $message_from;
  122. $array['messages'][0]['message_to'] = $message_to;
  123. $array['messages'][0]['message_text'] = $message_text;
  124. $array['messages'][0]['message_uuid'] = $message_uuid;
  125. //save to the data
  126. $database = new database;
  127. $database->app_name = 'messages';
  128. $database->app_uuid = '4a20815d-042c-47c8-85df-085333e79b87';
  129. $database->save($array);
  130. //redirect the user
  131. if (isset($action)) {
  132. if ($action == "add") {
  133. message::add($text['message-add']);
  134. }
  135. if ($action == "update") {
  136. message::add($text['message-update']);
  137. }
  138. header('Location: message_edit.php?id='.$message_uuid);
  139. exit;
  140. }
  141. }
  142. //pre-populate the form
  143. if (!empty($_GET["id"]) && empty($_POST["persistformvar"])) {
  144. $message_uuid = $_GET["id"];
  145. $sql = "select * from v_messages ";
  146. $sql .= "where message_uuid = :message_uuid ";
  147. $parameters['message_uuid'] = $message_uuid;
  148. $database = new database;
  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. $database = new database;
  172. $users = $database->select($sql, $parameters, 'all');
  173. unset($sql, $parameters);
  174. //create token
  175. $object = new token;
  176. $token = $object->create($_SERVER['PHP_SELF']);
  177. //include the header
  178. $document['title'] = $text['title-message'];
  179. require_once "resources/header.php";
  180. //show the content
  181. echo "<form name='frm' id='frm' method='post'>\n";
  182. echo "<div class='action_bar' id='action_bar'>\n";
  183. echo " <div class='heading'><b>".$text['title-message']."</b></div>\n";
  184. echo " <div class='actions'>\n";
  185. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','link'=>'message_logs.php']);
  186. if ($action == 'update' && permission_exists('message_delete')) {
  187. 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');"]);
  188. }
  189. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','style'=>'margin-left: 15px;']);
  190. echo " </div>\n";
  191. echo " <div style='clear: both;'></div>\n";
  192. echo "</div>\n";
  193. if ($action == 'update' && permission_exists('message_delete')) {
  194. 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();"])]);
  195. }
  196. echo "<div class='card'>\n";
  197. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  198. echo "<tr>\n";
  199. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  200. echo " ".$text['label-username']."\n";
  201. echo "</td>\n";
  202. echo "<td width='70%' class='vtable' style='position: relative;' align='left'>\n";
  203. echo " <select class='formfld' name='user_uuid'>\n";
  204. echo " <option value=''></option>\n";
  205. if (!empty($users)) {
  206. foreach($users as $row) {
  207. echo " <option value='".escape($row['user_uuid'])."' ".($row['user_uuid'] == $user_uuid ? "selected='selected'" : null).">".escape($row['username'])."</option>\n";
  208. }
  209. }
  210. echo " </select>\n";
  211. echo "<br />\n";
  212. echo $text['description-username']."\n";
  213. echo "</td>\n";
  214. echo "</tr>\n";
  215. echo "<tr>\n";
  216. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  217. echo " ".$text['label-message_type']."\n";
  218. echo "</td>\n";
  219. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  220. echo " <select class='formfld' name='message_type'>\n";
  221. echo " <option value='sms' ".($message_type == 'sms' ? "selected='selected'" : null).">".$text['label-sms']."</option>\n";
  222. echo " <option value='mms' ".($message_type == 'mms' ? "selected='selected'" : null).">".$text['label-mms']."</option>\n";
  223. echo " <option value='chat' ".($message_type == 'chat' ? "selected='selected'" : null).">".$text['label-chat']."</option>\n";
  224. echo " </select>\n";
  225. echo "<br />\n";
  226. echo $text['description-message_type']."\n";
  227. echo "</td>\n";
  228. echo "</tr>\n";
  229. echo "<tr>\n";
  230. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  231. echo " ".$text['label-message_direction']."\n";
  232. echo "</td>\n";
  233. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  234. echo " <select class='formfld' name='message_direction'>\n";
  235. echo " <option value='inbound' ".($message_direction == 'inbound' ? "selected='selected'" : null).">".$text['label-inbound']."</option>\n";
  236. echo " <option value='outbound' ".($message_direction == 'outbound' ? "selected='selected'" : null).">".$text['label-outbound']."</option>\n";
  237. echo " </select>\n";
  238. echo "<br />\n";
  239. echo $text['description-message_direction']."\n";
  240. echo "</td>\n";
  241. echo "</tr>\n";
  242. echo "<tr>\n";
  243. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  244. echo " ".$text['label-message_date']."\n";
  245. echo "</td>\n";
  246. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  247. echo " <input class='formfld' type='text' name='message_date' maxlength='255' value=\"".escape($message_date)."\">\n";
  248. echo "<br />\n";
  249. echo $text['description-message_date']."\n";
  250. echo "</td>\n";
  251. echo "</tr>\n";
  252. echo "<tr>\n";
  253. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  254. echo " ".$text['label-message_from']."\n";
  255. echo "</td>\n";
  256. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  257. echo " <input class='formfld' type='text' name='message_from' maxlength='255' value=\"".escape($message_from)."\">\n";
  258. echo "<br />\n";
  259. echo $text['description-message_from']."\n";
  260. echo "</td>\n";
  261. echo "</tr>\n";
  262. echo "<tr>\n";
  263. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  264. echo " ".$text['label-message_to']."\n";
  265. echo "</td>\n";
  266. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  267. echo " <input class='formfld' type='text' name='message_to' maxlength='255' value=\"".escape($message_to)."\">\n";
  268. echo "<br />\n";
  269. echo $text['description-message_to']."\n";
  270. echo "</td>\n";
  271. echo "</tr>\n";
  272. echo "<tr>\n";
  273. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  274. echo " ".$text['label-message_text']."\n";
  275. echo "</td>\n";
  276. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  277. echo " <textarea class='formfld' style='min-width: 40%; height: 100px;' name='message_text'>".escape($message_text)."</textarea>\n";
  278. echo "<br />\n";
  279. echo $text['description-message_text']."\n";
  280. echo "</td>\n";
  281. echo "</tr>\n";
  282. if (!empty($message_media_type)) {
  283. echo "<tr>\n";
  284. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  285. echo " &nbsp;\n";
  286. echo "</td>\n";
  287. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  288. $image_source = 'data: '.mime_content_type($message_media_type).';base64,'.$message_media_content;
  289. echo "<img src='".$image_source."' width='100%'>";
  290. echo "<br />\n";
  291. echo "</td>\n";
  292. echo "</tr>\n";
  293. }
  294. if (!empty($_GET['debug']) && $_GET['debug'] == 'true') {
  295. echo "<tr>\n";
  296. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  297. echo " ".$text['label-message_media_type']."\n";
  298. echo "</td>\n";
  299. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  300. echo " <input class='formfld' type='text' name='message_media_type' maxlength='255' value=\"".escape($message_media_type)."\">\n";
  301. echo "<br />\n";
  302. echo $text['description-message_media_type']."\n";
  303. echo "</td>\n";
  304. echo "</tr>\n";
  305. echo "<tr>\n";
  306. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  307. echo " ".$text['label-message_media_url']."\n";
  308. echo "</td>\n";
  309. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  310. echo " <input class='formfld' type='text' name='message_media_url' maxlength='255' value=\"".escape($message_media_url)."\">\n";
  311. echo "<br />\n";
  312. echo $text['description-message_media_url']."\n";
  313. echo "</td>\n";
  314. echo "</tr>\n";
  315. echo "<tr>\n";
  316. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  317. echo " ".$text['label-message_media_content']."\n";
  318. echo "</td>\n";
  319. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  320. echo " <input class='formfld' type='text' name='message_media_content' maxlength='255' value=\"".escape($message_media_content)."\">\n";
  321. echo "<br />\n";
  322. echo $text['description-message_media_content']."\n";
  323. echo "</td>\n";
  324. echo "</tr>\n";
  325. echo "<tr>\n";
  326. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  327. echo " ".$text['label-message_json']."\n";
  328. echo "</td>\n";
  329. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  330. echo " <textarea class='formfld' style='min-width: 40%; height: 100px;' name='message_json'>".escape($message_json)."</textarea>\n";
  331. echo "<br />\n";
  332. echo $text['description-message_json']."\n";
  333. echo "</td>\n";
  334. echo "</tr>\n";
  335. }
  336. echo "</table>\n";
  337. echo "</div>\n";
  338. echo "<br /><br />\n";
  339. echo "<input type='hidden' name='message_uuid' value='".escape($message_uuid)."'>\n";
  340. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  341. echo "</form>";
  342. //include the footer
  343. require_once "resources/footer.php";
  344. ?>