scripts.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. function sanitize_string(str) {
  2. let temp = document.createElement('div');
  3. temp.textContent = str;
  4. return temp.innerHTML;
  5. }
  6. let user_agent;
  7. let session;
  8. let answer_time;
  9. var config = {
  10. uri: '<?php echo $user_extension.'@'.$domain_name; ?>',
  11. ws_servers: 'wss://<?php echo $domain_name; ?>:7443',
  12. authorizationUser: '<?php echo $user_extension; ?>',
  13. password: atob('<?php echo base64_encode($user_password); ?>'),
  14. registerExpires: 120,
  15. displayName: "<?php echo $user_extension; ?>"
  16. };
  17. user_agent = new SIP.UA(config);
  18. //here you determine whether the call has video and audio
  19. var options = {
  20. media: {
  21. constraints: {
  22. audio: true,
  23. video: false
  24. },
  25. render: {
  26. remote: document.getElementById('remote_video'),
  27. local: document.getElementById('local_video')
  28. },
  29. RTCConstraints: {
  30. "optional": [{ 'DtlsSrtpKeyAgreement': 'true'} ]
  31. }
  32. }
  33. };
  34. //answer
  35. user_agent.on('invite', function (s) {
  36. if (typeof session !== "undefined" && session.display_name != s.remoteIdentity.displayName) {
  37. return;
  38. }
  39. //save the session to the global session
  40. session = s;
  41. session.display_name = session.remoteIdentity.displayName;
  42. session.uri_user = session.remoteIdentity.uri.user;
  43. //send the object to the browser console
  44. //console.log(session);
  45. //play the ringtone
  46. document.getElementById('ringtone').play();
  47. //add the caller ID
  48. document.getElementById('ringing_caller_id').innerHTML = sanitize_string(session.display_name) + "<br /><a href='https://<?php echo $_SESSION['domain_name']; ?>/app/contacts/contacts.php?search=" + sanitize_string(session.uri_user) + "' target='_blank'>" + sanitize_string(session.uri_user) + "</a>";
  49. document.getElementById('active_caller_id').innerHTML = sanitize_string(session.display_name) + "<br /><a href='https://<?php echo $_SESSION['domain_name']; ?>/app/contacts/contacts.php?search=" + sanitize_string(session.uri_user) + "' target='_blank'>" + sanitize_string(session.uri_user) + "</a>";
  50. //show or hide the panels
  51. document.getElementById('dialpad').style.display = "none";
  52. document.getElementById('ringing').style.display = "inline";
  53. //show or hide the buttons
  54. document.getElementById('answer').style.display = "inline";
  55. document.getElementById('decline').style.display = "inline";
  56. document.getElementById('hangup').style.display = "none";
  57. document.getElementById('mute_audio').style.display = "inline";
  58. document.getElementById('mute_video').style.display = "none";
  59. session.on('cancel', function (s) {
  60. //play the ringtone
  61. document.getElementById('ringtone').pause();
  62. //show or hide the panels
  63. document.getElementById('dialpad').style.display = "grid";
  64. document.getElementById('ringing').style.display = "none";
  65. document.getElementById('active').style.display = "none";
  66. //show or hide the buttons
  67. document.getElementById('answer').style.display = "none";
  68. document.getElementById('decline').style.display = "none";
  69. document.getElementById('hangup').style.display = "none";
  70. //clear the caller id
  71. document.getElementById('ringing_caller_id').innerHTML = '';
  72. document.getElementById('active_caller_id').innerHTML = '';
  73. //clear the answer time
  74. answer_time = null;
  75. //end the call
  76. hangup();
  77. });
  78. session.on('bye', function (s) {
  79. //play the ringtone
  80. document.getElementById('ringtone').pause();
  81. //show or hide the panels
  82. document.getElementById('dialpad').style.display = "grid";
  83. document.getElementById('ringing').style.display = "none";
  84. document.getElementById('active').style.display = "none";
  85. //show or hide the buttons
  86. document.getElementById('answer').style.display = "none";
  87. document.getElementById('decline').style.display = "none";
  88. document.getElementById('hangup').style.display = "none";
  89. //clear the answer time
  90. answer_time = null;
  91. //end the call
  92. hangup();
  93. });
  94. session.on('failed', function (s) {
  95. //play the ringtone
  96. document.getElementById('ringtone').pause();
  97. //show or hide the panels
  98. document.getElementById('dialpad').style.display = "grid";
  99. document.getElementById('ringing').style.display = "none";
  100. document.getElementById('active').style.display = "none";
  101. //show or hide the buttons
  102. document.getElementById('answer').style.display = "none";
  103. document.getElementById('decline').style.display = "none";
  104. document.getElementById('hangup').style.display = "none";
  105. //clear the answer time
  106. answer_time = null;
  107. //end the call
  108. hangup();
  109. });
  110. session.on('rejected', function (s) {
  111. //play the ringtone
  112. document.getElementById('ringtone').pause();
  113. //show or hide the panels
  114. document.getElementById('dialpad').style.display = "grid";
  115. document.getElementById('ringing').style.display = "none";
  116. document.getElementById('active').style.display = "none";
  117. //show or hide the buttons
  118. document.getElementById('answer').style.display = "none";
  119. document.getElementById('decline').style.display = "none";
  120. document.getElementById('hangup').style.display = "none";
  121. //clear the answer time
  122. answer_time = null;
  123. //end the call
  124. hangup();
  125. });
  126. });
  127. function answer() {
  128. //continue if the session exists
  129. if (!session) {
  130. return false;
  131. }
  132. //start the answer time
  133. answer_time = Date.now();
  134. //pause the ringtone
  135. document.getElementById('ringtone').pause();
  136. //answer the call
  137. session.accept({
  138. media: {
  139. constraints: {
  140. audio: true,
  141. video: false
  142. },
  143. render: {
  144. remote: document.getElementById('remote_video'),
  145. local: document.getElementById('local_video')
  146. },
  147. RTCConstraints: {
  148. "optional": [{ 'DtlsSrtpKeyAgreement': 'true'} ]
  149. }
  150. }
  151. });
  152. //show the or hide the panels
  153. document.getElementById('dialpad').style.display = "none";
  154. document.getElementById('ringing').style.display = "none";
  155. document.getElementById('active').style.display = "grid";
  156. document.getElementById('destination').value = '';
  157. //show or hide the buttons
  158. document.getElementById('answer').style.display = "none";
  159. document.getElementById('decline').style.display = "none";
  160. document.getElementById('unhold').style.display = "none";
  161. document.getElementById('hangup').style.display = "inline";
  162. }
  163. // Function to pad numbers with leading zeros
  164. function pad(number, length) {
  165. return (number < 10 ? '0' : '') + number;
  166. }
  167. //function to get the current time in seconds
  168. function get_session_time() {
  169. if (answer_time) {
  170. // get the elapsed time using the answer time
  171. elapsed_time = Date.now() - answer_time;
  172. // Calculate hours, minutes, and seconds
  173. var hours = Math.floor(elapsed_time / (1000 * 60 * 60));
  174. var minutes = Math.floor((elapsed_time % (1000 * 60 * 60)) / (1000 * 60));
  175. var seconds = Math.floor((elapsed_time % (1000 * 60)) / 1000);
  176. // Format the time with leading zeros if necessary
  177. var formatted_time = pad(hours, 2) + ":" + pad(minutes, 2) + ":" + pad(seconds, 2);
  178. // Update the element with id="elapsed-time" to display the formatted elapsed time
  179. document.getElementById("answer_time").textContent = formatted_time;
  180. }
  181. else {
  182. console.log('Call has not been answered yet');
  183. return null;
  184. }
  185. }
  186. //update elapsed time every second
  187. setInterval(get_session_time, 1000);
  188. //function used to end the session
  189. function hangup() {
  190. //end the session
  191. //session.bye();
  192. session.terminate();
  193. //show or hide the panels
  194. document.getElementById('dialpad').style.display = "grid";
  195. document.getElementById('ringing').style.display = "none";
  196. document.getElementById('active').style.display = "none";
  197. //show or hide the buttons
  198. document.getElementById('answer').style.display = "none";
  199. document.getElementById('decline').style.display = "none";
  200. document.getElementById('hangup').style.display = "none";
  201. document.getElementById('local_video').style.display = "none";
  202. document.getElementById('remote_video').style.display = "none";
  203. document.getElementById('mute_audio').style.display = "none";
  204. //document.getElementById('mute_video').style.display = "none";
  205. document.getElementById('unmute_audio').style.display = "none";
  206. //document.getElementById('unmute_video').style.display = "none";
  207. document.getElementById('unhold').style.display = "none";
  208. document.getElementById('hold').style.display = "inline";
  209. //clear the caller id and timer
  210. document.getElementById('ringing_caller_id').innerHTML = '';
  211. document.getElementById('active_caller_id').innerHTML = '';
  212. document.getElementById('answer_time').innerHTML = '00:00:00';
  213. //mute the audio
  214. session.mute({audio: true});
  215. }
  216. function hold() {
  217. document.getElementById('hold').style.display = "none";
  218. document.getElementById('unhold').style.display = "inline";
  219. session.hold();
  220. }
  221. function unhold() {
  222. document.getElementById('hold').style.display = "inline";
  223. document.getElementById('unhold').style.display = "none";
  224. session.unhold();
  225. }
  226. function send() {
  227. //get the destination number
  228. destination = document.getElementById('destination').value;
  229. //return immediately if there is no destination
  230. if (destination.length == 0) {
  231. return;
  232. }
  233. //show or hide the panels
  234. document.getElementById('dialpad').style.display = "none";
  235. document.getElementById('ringing').style.display = "none";
  236. document.getElementById('active').style.display = "grid";
  237. document.getElementById('answer').style.display = "none";
  238. document.getElementById('decline').style.display = "none";
  239. document.getElementById('hangup').style.display = "inline";
  240. //document.getElementById('local_video').style.display = "inline";
  241. //document.getElementById('remote_video').style.display = "inline";
  242. document.getElementById('mute_audio').style.display = "inline";
  243. //document.getElementById('mute_video').style.display = "inline";
  244. //make a call using a sip invite
  245. session = user_agent.invite('sip:'+destination+'@<?php echo $domain_name; ?>', options);
  246. var remote_video = document.getElementById("remote_video");
  247. remote_video.setAttribute("controls","controls");
  248. //unmute the audio
  249. session.unmute({audio: true});
  250. //start the answer time
  251. answer_time = Date.now();
  252. //set the caller ID to the destination
  253. document.getElementById('ringing_caller_id').innerHTML = destination;
  254. document.getElementById('active_caller_id').innerHTML = destination;
  255. }
  256. function mute_audio(destination) {
  257. session.mute({audio: true});
  258. document.getElementById('mute_audio').style.display = "none";
  259. document.getElementById('unmute_audio').style.display = "inline";
  260. }
  261. function mute_video(destination) {
  262. session.mute({video: true});
  263. document.getElementById('local_video').style.display = "none";
  264. document.getElementById('mute_video').style.display = "none";
  265. document.getElementById('unmute_video').style.display = "inline";
  266. }
  267. function unmute_audio(destination) {
  268. session.unmute({audio: true});
  269. document.getElementById('mute_audio').style.display = "inline";
  270. document.getElementById('unmute_audio').style.display = "none";
  271. }
  272. function unmute_video(destination) {
  273. session.unmute({video: true});
  274. document.getElementById('local_video').style.display = "inline";
  275. document.getElementById('mute_video').style.display = "inline";
  276. document.getElementById('unmute_video').style.display = "none";
  277. }
  278. //function to center entered digits until full, then right-align and change text direction so last entered digits are always visible
  279. function correct_alignment() {
  280. if (document.getElementById('destination').scrollWidth > document.getElementById('destination').clientWidth) {
  281. document.getElementById('destination').style.textAlign = 'right';
  282. document.getElementById('destination').style.direction = 'rtl';
  283. }
  284. else {
  285. document.getElementById('destination').style.textAlign = 'center';
  286. document.getElementById('destination').style.direction = 'ltr';
  287. }
  288. }
  289. function digit_add($digit) {
  290. document.getElementById('destination').value = document.getElementById('destination').value + $digit;
  291. correct_alignment();
  292. }
  293. function digit_delete() {
  294. destination = document.getElementById('destination').value;
  295. document.getElementById('destination').value = destination.substring(0, destination.length -1);
  296. correct_alignment();
  297. }
  298. function digit_clear() {
  299. document.getElementById('destination').value = '';
  300. correct_alignment();
  301. }
  302. //function to check for Enter key press
  303. function send_enter_key(event) {
  304. if (event.key === "Enter") {
  305. send();
  306. }
  307. }
  308. //function to detect numberpad keypresses
  309. document.addEventListener('keyup', function(e) {
  310. if (document.getElementById('destination')) { //destination field is visible
  311. if (
  312. (e.which >= 48 && e.which <= 57) || //numbers
  313. (e.which >= 96 && e.which <= 105) || //number pad
  314. (e.which == 56 || e.which == 106) || //asterisk
  315. (e.which == 51) //pound
  316. ) {
  317. e.preventDefault();
  318. digit_add(e.key);
  319. }
  320. if (e.which == 8 || e.which == 46) { //backspace or delete
  321. e.preventDefault();
  322. digit_delete();
  323. }
  324. if (e.which == 27) { //escape
  325. e.preventDefault();
  326. digit_clear();
  327. }
  328. if (e.which == 13) { //enter
  329. e.preventDefault();
  330. send();
  331. }
  332. }
  333. });
  334. //add event listener for keydown event on input field
  335. document.addEventListener("DOMContentLoaded", function() {
  336. document.getElementById("destination").addEventListener("keydown", send_enter_key);
  337. });