FirstTabViewController.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. //
  2. // FirstTabViewController.swift
  3. // AppBuilder
  4. //
  5. // Created by Kevin Maulana on 29/03/22.
  6. //
  7. import UIKit
  8. import WebKit
  9. import NexilisLite
  10. import Speech
  11. import CommonCrypto
  12. class FirstTabViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate, WKScriptMessageHandler, ImageVideoPickerDelegate {
  13. var webView: WKWebView!
  14. var address = ""
  15. private var lastContentOffset: CGFloat = 0
  16. var isAllowSpeech = false
  17. let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "id"))
  18. var recognitionRequest : SFSpeechAudioBufferRecognitionRequest?
  19. var recognitionTask : SFSpeechRecognitionTask?
  20. let audioEngine = AVAudioEngine()
  21. var alertController = LibAlertController()
  22. public static var forceRefresh = true
  23. public static var atFirstPage = true
  24. public static var showModal = false
  25. var indexImageVideoWv = 0
  26. var imageVideoPicker: ImageVideoPicker!
  27. var blockedCertificate = ""
  28. var allowedURLs = Set<String>()
  29. var loadingURL = false
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. self.view.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .black : .white
  33. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(collapseDocked))
  34. tapGesture.cancelsTouchesInView = false
  35. tapGesture.delegate = self
  36. let configuration = WKWebViewConfiguration()
  37. configuration.allowsInlineMediaPlayback = true
  38. let customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1 \(Utils.getUserAgent())"
  39. let finalUserAgent = "\(customUserAgent)"
  40. configuration.applicationNameForUserAgent = finalUserAgent
  41. webView = WKWebView(frame: .zero, configuration: configuration)
  42. view.addSubview(webView)
  43. webView.translatesAutoresizingMaskIntoConstraints = false
  44. NSLayoutConstraint.activate([
  45. webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  46. webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  47. webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  48. webView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
  49. ])
  50. webView.scrollView.addGestureRecognizer(tapGesture)
  51. let refreshControl = UIRefreshControl()
  52. refreshControl.addTarget(self, action: #selector(reloadWebView(_:)), for: .valueChanged)
  53. webView.scrollView.addSubview(refreshControl)
  54. webView.scrollView.delegate = self
  55. webView.navigationDelegate = self
  56. webView.allowsBackForwardNavigationGestures = true
  57. let contentController = self.webView.configuration.userContentController
  58. contentController.add(self, name: "checkProfile")
  59. contentController.add(self, name: "setIsProductModalOpen")
  60. contentController.add(self, name: "toggleVoiceSearch")
  61. contentController.add(self, name: "blockUser")
  62. contentController.add(self, name: "showAlert")
  63. contentController.add(self, name: "closeProfile")
  64. contentController.add(self, name: "tabShowHide")
  65. contentController.add(self, name: "shareText")
  66. contentController.add(self, name: "openGalleryiOS")
  67. let source: String = "var meta = document.createElement('meta');" +
  68. "meta.name = 'viewport';" +
  69. "meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
  70. "var head = document.getElementsByTagName('head')[0];" +
  71. "head.appendChild(meta);" +
  72. "$('#header-layout').find('.col-8').removeClass('col-8').addClass('col');" +
  73. "$('#header-layout').find('.col-4').removeClass('col-4').addClass('col');"
  74. let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
  75. contentController.addUserScript(script)
  76. let cookieScript1 = "document.cookie = '\(Utils.getCookiesMobile().components(separatedBy: ";")[0])';"
  77. let cookieScriptInjection1 = WKUserScript(source: cookieScript1, injectionTime: .atDocumentStart, forMainFrameOnly: false)
  78. configuration.userContentController.addUserScript(cookieScriptInjection1)
  79. let cookieScript2 = "document.cookie = '\(Utils.getCookiesMobile().components(separatedBy: ";")[1])';"
  80. let cookieScriptInjection2 = WKUserScript(source: cookieScript2, injectionTime: .atDocumentStart, forMainFrameOnly: false)
  81. configuration.userContentController.addUserScript(cookieScriptInjection2)
  82. NotificationCenter.default.addObserver(self, selector: #selector(onShowAC(notification:)), name: NSNotification.Name(rawValue: "onShowAC"), object: nil)
  83. NotificationCenter.default.addObserver(self, selector: #selector(onRefreshWebView(notification:)), name: NSNotification.Name(rawValue: "onRefreshWebView"), object: nil)
  84. }
  85. func loadURLWithCookie(url: URL) {
  86. var urlRequest = URLRequest(url: url)
  87. let customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1 \(Utils.getUserAgent())"
  88. urlRequest.setValue(customUserAgent, forHTTPHeaderField: "User-Agent")
  89. if let cookies = HTTPCookieStorage.shared.cookies(for: url) {
  90. for cookie in cookies {
  91. webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)
  92. }
  93. webView.load(urlRequest)
  94. }
  95. }
  96. override func viewWillAppear(_ animated: Bool) {
  97. let me = User.getMyPin()
  98. var myURL : URL?
  99. let lang: String = SecureUserDefaults.shared.value(forKey: "i18n_language") ?? "en"
  100. var intLang = 0
  101. if lang == "id" {
  102. intLang = 1
  103. }
  104. if PrefsUtil.getURLFirstTab() != nil {
  105. ViewController.sURL = PrefsUtil.getURLFirstTab()!
  106. }
  107. switch(ViewController.sURL){
  108. case "0":
  109. address = "\(PrefsUtil.getURLBase())nexilis/pages/tab1-main-only?f_pin=\(me ?? "")&lang=\(intLang)&theme=\(self.traitCollection.userInterfaceStyle == .dark ? "0" : "1")"
  110. myURL = URL(string: address)
  111. case "1":
  112. address = "\(PrefsUtil.getURLBase())nexilis/pages/tab3-main-only?f_pin=\(me ?? "")&lang=\(intLang)&theme=\(self.traitCollection.userInterfaceStyle == .dark ? "0" : "1")"
  113. myURL = URL(string: address)
  114. case "2":
  115. address = "\(PrefsUtil.getURLBase())nexilis/pages/tab1-main?f_pin=\(me ?? "")&lang=\(intLang)&theme=\(self.traitCollection.userInterfaceStyle == .dark ? "0" : "1")"
  116. myURL = URL(string: address)
  117. case "3":
  118. address = "\(PrefsUtil.getURLBase())nexilis/pages/tab3-commerce?f_pin=\(me ?? "")&lang=\(intLang)&theme=\(self.traitCollection.userInterfaceStyle == .dark ? "0" : "1")"
  119. myURL = URL(string: address)
  120. case "4":
  121. address = "\(PrefsUtil.getURLBase())nexilis/pages/tab1-video?f_pin=\(me ?? "")&lang=\(intLang)&theme=\(self.traitCollection.userInterfaceStyle == .dark ? "0" : "1")"
  122. myURL = URL(string: address)
  123. default:
  124. if(!ViewController.sURL.isEmpty){
  125. if(ViewController.sURL.lowercased().contains("https://") || ViewController.sURL.lowercased().contains("http://")){
  126. address = ViewController.sURL
  127. myURL = URL(string: address)
  128. }
  129. else {
  130. if ViewController.sURL.contains("nexilis/pages"){
  131. address = "\(PrefsUtil.getURLBase())\(ViewController.sURL)?f_pin=\(me ?? "")&lang=\(intLang)&theme=\(self.traitCollection.userInterfaceStyle == .dark ? "0" : "1")"
  132. } else {
  133. address = "https://\(ViewController.sURL)"
  134. }
  135. myURL = URL(string: address)
  136. }
  137. }
  138. }
  139. if let u = myURL {
  140. self.webView.evaluateJavaScript("{window.localStorage.setItem('currentTab','\(ViewController.sURL)')}")
  141. if FirstTabViewController.forceRefresh {
  142. loadURLWithCookie(url: u)
  143. } else {
  144. self.webView.evaluateJavaScript("if(resumeAll){resumeAll();}")
  145. }
  146. FirstTabViewController.forceRefresh = false
  147. }
  148. navigationController?.setNavigationBarHidden(true, animated: false)
  149. }
  150. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  151. if Utils.getIsLoadThemeFromOther() {
  152. self.webView.evaluateJavaScript("{window.localStorage.setItem('mobileConfiguration','"+Utils.getMyTheme()+"')}")
  153. }
  154. }
  155. override func viewDidAppear(_ animated: Bool) {
  156. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
  157. var viewController = UIApplication.shared.windows.first!.rootViewController
  158. if !(viewController is ViewController) {
  159. viewController = self.parent
  160. }
  161. if ViewController.middleButton.isHidden {
  162. ViewController.isExpandButton = false
  163. if let viewController = viewController as? ViewController {
  164. if viewController.tabBar.isHidden {
  165. viewController.tabBar.isHidden = false
  166. ViewController.middleButton.isHidden = false
  167. ViewController.alwaysHideButton = false
  168. }
  169. }
  170. } else if PrefsUtil.getCpaasMode() != PrefsUtil.CPAAS_MODE_DOCKED {
  171. DispatchQueue.main.async {
  172. if let viewController = viewController as? ViewController {
  173. if viewController.tabBar.isHidden {
  174. viewController.tabBar.isHidden = false
  175. ViewController.alwaysHideButton = false
  176. }
  177. }
  178. }
  179. }
  180. })
  181. }
  182. @objc func onShowAC(notification: NSNotification) {
  183. self.webView.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  184. view.endEditing(true)
  185. resignFirstResponder()
  186. }
  187. @objc func onRefreshWebView(notification: NSNotification) {
  188. FirstTabViewController.forceRefresh = true
  189. }
  190. override func viewWillDisappear(_ animated: Bool) {
  191. if self.webView.scrollView.contentOffset.y < 0 { // Move tableView to top
  192. self.webView.scrollView.setContentOffset(CGPoint.zero, animated: true)
  193. }
  194. self.webView.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  195. self.webView.evaluateJavaScript("hideAddToCart();")
  196. }
  197. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  198. if (self.lastContentOffset > scrollView.contentOffset.y && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)) {
  199. showTabBar();
  200. }
  201. else if (self.lastContentOffset != 0 && self.lastContentOffset < scrollView.contentOffset.y && self.lastContentOffset >= 0) {
  202. hideTabBar();
  203. }
  204. self.lastContentOffset = scrollView.contentOffset.y
  205. self.collapseDocked()
  206. }
  207. @objc func collapseDocked() {
  208. if ViewController.isExpandButton {
  209. ViewController.expandButton()
  210. }
  211. }
  212. @objc func reloadWebView(_ sender: UIRefreshControl) {
  213. FirstTabViewController.forceRefresh = true
  214. viewWillAppear(false)
  215. ViewController.alwaysHideButton = false
  216. showTabBar()
  217. sender.endRefreshing()
  218. }
  219. func hideTabBar() {
  220. var viewController = UIApplication.shared.windows.first!.rootViewController
  221. if !(viewController is ViewController) {
  222. viewController = self.parent
  223. }
  224. if ViewController.middleButton.isDescendant(of: viewController!.view) {
  225. DispatchQueue.main.async {
  226. if ViewController.isExpandButton {
  227. ViewController.expandButton()
  228. }
  229. ViewController.hideDockedButton()
  230. if let viewController = viewController as? ViewController {
  231. viewController.tabBar.isHidden = true
  232. }
  233. ViewController.removeMiddleButton()
  234. }
  235. } else if PrefsUtil.getCpaasMode() != PrefsUtil.CPAAS_MODE_DOCKED {
  236. DispatchQueue.main.async {
  237. if let viewController = viewController as? ViewController {
  238. if !viewController.tabBar.isHidden {
  239. viewController.tabBar.isHidden = true
  240. }
  241. }
  242. }
  243. }
  244. }
  245. func showTabBar() {
  246. if(ViewController.alwaysHideButton){
  247. return
  248. }
  249. var viewController = UIApplication.shared.windows.first!.rootViewController
  250. if !(viewController is ViewController) {
  251. viewController = self.parent
  252. }
  253. if ViewController.middleButton.isHidden {
  254. if let viewController = viewController as? ViewController {
  255. viewController.tabBar.isHidden = false
  256. ViewController.middleButton.isHidden = false
  257. }
  258. } else if PrefsUtil.getCpaasMode() != PrefsUtil.CPAAS_MODE_DOCKED {
  259. DispatchQueue.main.async {
  260. if let viewController = viewController as? ViewController {
  261. if viewController.tabBar.isHidden {
  262. viewController.tabBar.isHidden = false
  263. }
  264. }
  265. }
  266. }
  267. }
  268. func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
  269. scrollView.pinchGestureRecognizer?.isEnabled = false
  270. }
  271. func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  272. if message.name == "checkProfile" {
  273. guard let dict = message.body as? [String: AnyObject],
  274. let param1 = dict["param1"] as? String,
  275. let param2 = dict["param2"] as? String else {
  276. return
  277. }
  278. if ViewController.checkIsChangePerson() {
  279. if param2 == "like" {
  280. self.webView.evaluateJavaScript("likeProduct('\(param1)',1,true);")
  281. } else if param2 == "comment" {
  282. self.webView.evaluateJavaScript("openComment('\(param1.split(separator: "|")[0])',\(param1.split(separator: "|")[1]),true);")
  283. } else if param2 == "report_user" {
  284. self.webView.evaluateJavaScript("reportUser('\(param1)',true);")
  285. } else if param2 == "report_content" {
  286. self.webView.evaluateJavaScript("reportContent('\(param1.split(separator: "|")[0])','\(param1.split(separator: "|")[1])',true);")
  287. } else if param2 == "block_user" {
  288. self.webView.evaluateJavaScript("blockUser('\(param1)',true);")
  289. } else if param2 == "follow_user" {
  290. self.webView.evaluateJavaScript("followUser('\(param1.split(separator: "|")[0])',\(param1.split(separator: "|")[1]),true);")
  291. } else if param2 == "homepage" || param2 == "gif" {
  292. self.webView.evaluateJavaScript("window.location.href = '\(param1)';")
  293. } else if param2 == "block_content" {
  294. self.webView.evaluateJavaScript("blockContent('\(param1)',true);")
  295. } else {
  296. self.webView.evaluateJavaScript("openNewPost(true);")
  297. }
  298. } else {
  299. self.webView.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
  300. }
  301. } else if message.name == "setIsProductModalOpen" {
  302. guard let dict = message.body as? [String: AnyObject],
  303. let param1 = dict["param1"] as? Bool else {
  304. return
  305. }
  306. if param1 {
  307. if self.webView.scrollView.contentOffset.y < 0 { // Move tableView to top
  308. self.webView.scrollView.setContentOffset(CGPoint.zero, animated: true)
  309. }
  310. }
  311. FirstTabViewController.showModal = param1
  312. } else if message.name == "toggleVoiceSearch" {
  313. if !isAllowSpeech {
  314. setupSpeech()
  315. } else {
  316. runVoice()
  317. }
  318. } else if message.name == "blockUser" {
  319. guard let dict = message.body as? [String: AnyObject],
  320. let param1 = dict["param1"] as? String,
  321. let param2 = dict["param2"] as? Bool else {
  322. return
  323. }
  324. if param2 {
  325. DispatchQueue.global().async {
  326. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getBlock(l_pin: param1)) {
  327. if response.isOk() {
  328. DispatchQueue.main.async {
  329. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  330. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: [
  331. "ex_block" : "1"
  332. ], _where: "f_pin = '\(param1)'")
  333. })
  334. }
  335. }
  336. }
  337. }
  338. } else {
  339. DispatchQueue.global().async {
  340. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getUnBlock(l_pin: param1)) {
  341. if response.isOk() {
  342. DispatchQueue.main.async {
  343. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  344. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: [
  345. "ex_block" : "0"
  346. ], _where: "f_pin = '\(param1)'")
  347. })
  348. }
  349. }
  350. }
  351. }
  352. }
  353. } else if message.name == "showAlert" {
  354. guard let dict = message.body as? [String: AnyObject],
  355. let param1 = dict["param1"] as? String else {
  356. return
  357. }
  358. self.view.makeToast(param1, duration: 3)
  359. } else if message.name == "blockUser" {
  360. guard let dict = message.body as? [String: AnyObject],
  361. let param1 = dict["param1"] as? String,
  362. let param2 = dict["param2"] as? Bool else {
  363. return
  364. }
  365. if param2 {
  366. DispatchQueue.global().async {
  367. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getBlock(l_pin: param1)) {
  368. if response.isOk() {
  369. DispatchQueue.main.async {
  370. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  371. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: [
  372. "ex_block" : "1"
  373. ], _where: "f_pin = '\(param1)'")
  374. })
  375. }
  376. }
  377. }
  378. }
  379. } else {
  380. DispatchQueue.global().async {
  381. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getUnBlock(l_pin: param1)) {
  382. if response.isOk() {
  383. DispatchQueue.main.async {
  384. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  385. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: [
  386. "ex_block" : "0"
  387. ], _where: "f_pin = '\(param1)'")
  388. })
  389. }
  390. }
  391. }
  392. }
  393. }
  394. } else if message.name == "tabShowHide" {
  395. guard let dict = message.body as? [String: AnyObject],
  396. let param1 = dict["param1"] as? Bool else {
  397. return
  398. }
  399. if param1 {
  400. ViewController.alwaysHideButton = false
  401. showTabBar()
  402. } else {
  403. if self.viewIfLoaded?.window != nil {
  404. ViewController.alwaysHideButton = true
  405. hideTabBar()
  406. }
  407. }
  408. } else if message.name == "shareText" {
  409. guard let dict = message.body as? [String: AnyObject],
  410. let param1 = dict["param1"] as? String else {
  411. return
  412. }
  413. if loadingURL {
  414. return
  415. }
  416. let activityViewController = UIActivityViewController(activityItems: [param1], applicationActivities: nil)
  417. self.present(activityViewController, animated: true, completion: nil)
  418. } else if message.name == "openGalleryiOS" {
  419. guard let dict = message.body as? [String: AnyObject],
  420. let param1 = dict["param1"] as? Int else {
  421. return
  422. }
  423. indexImageVideoWv = param1
  424. let alertController = LibAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  425. if let action = self.actionImageVideo(for: "image", title: "Choose Photo".localized()) {
  426. alertController.addAction(action)
  427. }
  428. if let action = self.actionImageVideo(for: "video", title: "Choose Video".localized()) {
  429. alertController.addAction(action)
  430. }
  431. alertController.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
  432. self.present(alertController, animated: true)
  433. }
  434. }
  435. private func actionImageVideo(for type: String, title: String) -> UIAlertAction? {
  436. return UIAlertAction(title: title, style: .default) { [unowned self] _ in
  437. switch type {
  438. case "image":
  439. imageVideoPicker.present(source: .imageAlbum)
  440. case "video":
  441. imageVideoPicker.present(source: .videoAlbum)
  442. default:
  443. imageVideoPicker.present(source: .imageAlbum)
  444. }
  445. }
  446. }
  447. func didSelect(imagevideo: Any?) {
  448. if imagevideo != nil {
  449. let imageData = imagevideo! as! [UIImagePickerController.InfoKey : Any]
  450. if (imageData[.mediaType] as! String == "public.image") {
  451. let compressedImage = (imageData[.originalImage] as! UIImage).pngData()!
  452. let base64String = compressedImage.base64EncodedString()
  453. let base64ToWeb = "data:image/jpeg;base64,\(base64String)"
  454. webView.evaluateJavaScript("loadFromMobile('\(base64ToWeb)',\(indexImageVideoWv))") { (result, error) in
  455. if let error = error {
  456. print("Error executing JavaScript: \(error)")
  457. }
  458. }
  459. } else {
  460. guard var dataVideo = try? Data(contentsOf: imageData[.mediaURL] as! URL) else {
  461. return
  462. }
  463. let sizeOfVideo = Double(dataVideo.count / 1048576)
  464. if (sizeOfVideo > 10.0) {
  465. let compressedURL = NSURL.fileURL(withPath: NSTemporaryDirectory() + UUID().uuidString + ".mp4")
  466. compressVideo(inputURL: imageData[.mediaURL] as! URL,
  467. outputURL: compressedURL) { exportSession in
  468. guard let session = exportSession else {
  469. return
  470. }
  471. switch session.status {
  472. case .unknown:
  473. break
  474. case .waiting:
  475. break
  476. case .exporting:
  477. break
  478. case .completed:
  479. guard let compressedData = try? Data(contentsOf: compressedURL) else {
  480. return
  481. }
  482. dataVideo = compressedData
  483. case .failed:
  484. break
  485. case .cancelled:
  486. break
  487. @unknown default:
  488. break
  489. }
  490. }
  491. }
  492. let base64String = dataVideo.base64EncodedString()
  493. let base64ToWeb = "data:video/mp4;base64,\(base64String)"
  494. webView.evaluateJavaScript("loadFromMobile('\(base64ToWeb)',\(indexImageVideoWv))") { (result, error) in
  495. if let error = error {
  496. print("Error executing JavaScript: \(error)")
  497. }
  498. }
  499. }
  500. }
  501. }
  502. func compressVideo(inputURL: URL,
  503. outputURL: URL,
  504. handler:@escaping (_ exportSession: AVAssetExportSession?) -> Void) {
  505. let urlAsset = AVURLAsset(url: inputURL, options: nil)
  506. guard let exportSession = AVAssetExportSession(asset: urlAsset,
  507. presetName: AVAssetExportPresetMediumQuality) else {
  508. handler(nil)
  509. return
  510. }
  511. exportSession.outputURL = outputURL
  512. exportSession.outputFileType = .mp4
  513. exportSession.exportAsynchronously {
  514. handler(exportSession)
  515. }
  516. }
  517. func setupSpeech() {
  518. self.speechRecognizer?.delegate = self
  519. SFSpeechRecognizer.requestAuthorization { (authStatus) in
  520. var isButtonEnabled = false
  521. switch authStatus {
  522. case .authorized:
  523. isButtonEnabled = true
  524. case .denied:
  525. isButtonEnabled = false
  526. //print("User denied access to speech recognition")
  527. case .restricted:
  528. isButtonEnabled = false
  529. //print("Speech recognition restricted on this device")
  530. case .notDetermined:
  531. isButtonEnabled = false
  532. //print("Speech recognition not yet authorized")
  533. @unknown default:
  534. isButtonEnabled = false
  535. }
  536. OperationQueue.main.addOperation() {
  537. self.isAllowSpeech = isButtonEnabled
  538. if isButtonEnabled {
  539. SecureUserDefaults.shared.set(isButtonEnabled, forKey: "allowSpeech")
  540. self.runVoice()
  541. }
  542. }
  543. }
  544. }
  545. func runVoice() {
  546. if !audioEngine.isRunning {
  547. alertController = LibAlertController(title: "Start Recording".localized(), message: "Say something, I'm listening!".localized(), preferredStyle: .alert)
  548. self.present(alertController, animated: true)
  549. self.webView.evaluateJavaScript("toggleVoiceButton(true)")
  550. self.startRecording()
  551. }
  552. }
  553. func startRecording() {
  554. // Clear all previous session data and cancel task
  555. if recognitionTask != nil {
  556. recognitionTask?.cancel()
  557. recognitionTask = nil
  558. }
  559. // Create instance of audio session to record voice
  560. let audioSession = AVAudioSession.sharedInstance()
  561. do {
  562. try audioSession.setCategory(AVAudioSession.Category.record, mode: .default, options: [])
  563. try audioSession.setMode(AVAudioSession.Mode.measurement)
  564. try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
  565. } catch {
  566. //print("audioSession properties weren't set because of an error.")
  567. }
  568. self.recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
  569. let inputNode = audioEngine.inputNode
  570. guard let recognitionRequest = recognitionRequest else {
  571. fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
  572. }
  573. recognitionRequest.shouldReportPartialResults = true
  574. self.recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
  575. var isFinal = false
  576. var text = ""
  577. if result != nil {
  578. text = result?.bestTranscription.formattedString ?? ""
  579. isFinal = (result?.isFinal)!
  580. self.alertController.dismiss(animated: true)
  581. self.audioEngine.stop()
  582. self.recognitionRequest?.endAudio()
  583. } else {
  584. self.alertController.dismiss(animated: true)
  585. }
  586. if error != nil || isFinal {
  587. if error == nil {
  588. self.webView.evaluateJavaScript("toggleVoiceButton(false)")
  589. self.webView.evaluateJavaScript("submitVoiceSearch('\(text)')")
  590. } else {
  591. self.audioEngine.stop()
  592. self.recognitionRequest?.endAudio()
  593. }
  594. inputNode.removeTap(onBus: 0)
  595. self.recognitionRequest = nil
  596. self.recognitionTask = nil
  597. self.isAllowSpeech = true
  598. }
  599. })
  600. let recordingFormat = inputNode.outputFormat(forBus: 0)
  601. inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
  602. self.recognitionRequest?.append(buffer)
  603. }
  604. self.audioEngine.prepare()
  605. do {
  606. try self.audioEngine.start()
  607. } catch {
  608. //print("audioEngine couldn't start because of an error.")
  609. }
  610. }
  611. func isUsingMyWebview() -> Bool{
  612. return PrefsUtil.getURLFirstTab() == "0" || PrefsUtil.getURLFirstTab() == "1" || PrefsUtil.getURLFirstTab() == "2" || PrefsUtil.getURLFirstTab() == "3" || PrefsUtil.getURLFirstTab() == "4"
  613. }
  614. }
  615. extension FirstTabViewController: SFSpeechRecognizerDelegate {
  616. func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) {
  617. if available {
  618. self.isAllowSpeech = true
  619. } else {
  620. self.isAllowSpeech = false
  621. }
  622. }
  623. }
  624. extension FirstTabViewController: WKUIDelegate, WKNavigationDelegate {
  625. func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping @MainActor (WKNavigationActionPolicy) -> Void) {
  626. guard let url = navigationAction.request.url else {
  627. decisionHandler(.cancel)
  628. return
  629. }
  630. if loadingURL {
  631. decisionHandler(.cancel)
  632. return
  633. }
  634. loadingURL = true
  635. if allowedURLs.contains(url.absoluteString) {
  636. if ViewController.alwaysHideButton {
  637. ViewController.alwaysHideButton = false
  638. showTabBar()
  639. }
  640. loadingURL = false
  641. decisionHandler(.allow)
  642. return
  643. }
  644. validateSSLCertificate(url: url) { isValid in
  645. if isValid {
  646. self.allowedURLs.insert(url.absoluteString)
  647. self.loadingURL = false
  648. decisionHandler(.allow)
  649. } else {
  650. let host = url.host ?? ""
  651. DispatchQueue.main.async {
  652. var messageText = "You're about to access a website that is not currently trusted by your Nexilis Browser. This website's security certificate is not recognized.\n\nDo you wish to proceed to <<domain>> and trust the website's security certificate?\n\nNote: Adding a website to the trusted list may increase your risk of security vulnerability".localized()
  653. messageText = messageText.replacingOccurrences(of: "<<domain>>", with: host)
  654. let alert = UIAlertController(title: "Warning Unknown Url!".localized(),
  655. message: messageText,
  656. preferredStyle: .alert)
  657. let yesAction = UIAlertAction(title: "Yes", style: .default) { _ in
  658. let storedCertificate = Utils.getCertificatePinningWebview()
  659. if let jsonData = storedCertificate.data(using: .utf8),
  660. let certJson = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: String] {
  661. var certJson = certJson
  662. certJson[host] = self.blockedCertificate
  663. if let jsonData = try? JSONSerialization.data(withJSONObject: certJson, options: []),
  664. let jsonString = String(data: jsonData, encoding: .utf8) {
  665. Utils.setCertificatePinningWebview(value: jsonString)
  666. }
  667. }
  668. self.allowedURLs.insert(url.absoluteString)
  669. self.loadingURL = false
  670. decisionHandler(.allow)
  671. }
  672. let noAction = UIAlertAction(title: "No", style: .cancel) { _ in
  673. self.loadingURL = false
  674. decisionHandler(.cancel)
  675. }
  676. alert.addAction(yesAction)
  677. alert.addAction(noAction)
  678. self.present(alert, animated: true, completion: nil)
  679. }
  680. }
  681. }
  682. }
  683. private func validateSSLCertificate(url: URL, completion: @escaping (Bool) -> Void) {
  684. let session = URLSession(configuration: .ephemeral, delegate: self, delegateQueue: nil)
  685. let request = URLRequest(url: url)
  686. let task = session.dataTask(with: request) { _, response, error in
  687. if let error = error {
  688. completion(false)
  689. return
  690. }
  691. completion(true)
  692. }
  693. task.resume()
  694. }
  695. }
  696. extension FirstTabViewController: URLSessionDelegate {
  697. func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  698. guard let serverTrust = challenge.protectionSpace.serverTrust else {
  699. completionHandler(.cancelAuthenticationChallenge, nil)
  700. return
  701. }
  702. if let publicKeyHash = extractPublicKeyHash(from: serverTrust) {
  703. let domain = challenge.protectionSpace.host
  704. let storedCertificate = Utils.getCertificatePinningWebview()
  705. if let jsonData = storedCertificate.data(using: .utf8),
  706. let certJson = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: String] {
  707. if publicKeyHash == certJson[domain] {
  708. completionHandler(.useCredential, URLCredential(trust: serverTrust))
  709. } else {
  710. blockedCertificate = publicKeyHash
  711. completionHandler(.cancelAuthenticationChallenge, nil)
  712. }
  713. }
  714. } else {
  715. completionHandler(.cancelAuthenticationChallenge, nil)
  716. }
  717. }
  718. func extractPublicKeyHash(from serverTrust: SecTrust) -> String? {
  719. guard let certificate = SecTrustGetCertificateAtIndex(serverTrust, 0) else { return nil }
  720. guard let publicKey = SecCertificateCopyKey(certificate) else { return nil }
  721. var error: Unmanaged<CFError>?
  722. guard let publicKeyData = SecKeyCopyExternalRepresentation(publicKey, &error) as Data? else {
  723. return nil
  724. }
  725. // Compute SHA-256 hash
  726. var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
  727. publicKeyData.withUnsafeBytes {
  728. _ = CC_SHA256($0.baseAddress, CC_LONG(publicKeyData.count), &hash)
  729. }
  730. let hashData = Data(hash)
  731. let base64Hash = hashData.base64EncodedString()
  732. return base64Hash
  733. }
  734. }