Utils.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //
  2. // Utils.swift
  3. // Runner
  4. //
  5. // Created by Rifqy Fakhrul Rijal on 13/08/20.
  6. // Copyright © 2020 The Chromium Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. //import var CommonCrypto.CC_MD5_DIGEST_LENGTH
  11. //import func CommonCrypto.CC_MD5
  12. //import typealias CommonCrypto.CC_LONG
  13. public final class Utils {
  14. public static func getCurrentTime()->Int64 {
  15. return Int64(Date().timeIntervalSince1970)
  16. }
  17. public static func getCurrentTimeMillis()->Int64 {
  18. return Int64(Date().timeIntervalSince1970 * 1000)
  19. }
  20. public static func getCurrentTimeNanos()->Int64 {
  21. return Int64(Date().timeIntervalSince1970 * 1000_000_000)
  22. }
  23. public static func getElapsedRealtime() -> Int64 {
  24. return Int64((ProcessInfo().systemUptime).rounded()) // SystemClock.elapsedRealtime();
  25. }
  26. public static func getElapsedRealtimeMillis() -> Int64 {
  27. return Int64((ProcessInfo().systemUptime * 1000).rounded()) // SystemClock.elapsedRealtime();
  28. }
  29. public static func getElapsedRealtimeNanos() -> Int64 {
  30. return Int64((ProcessInfo().systemUptime * 1000_000_000).rounded()) // SystemClock.elapsedRealtimeNano();
  31. }
  32. public static func getForceAnonymous() -> Bool {
  33. return UserDefaults.standard.bool(forKey: "force_anonymous")
  34. }
  35. public static func setForceAnonymous(value: Bool){
  36. UserDefaults.standard.set(value, forKey: "force_anonymous")
  37. }
  38. public static func getSetProfile() -> Bool {
  39. return UserDefaults.standard.bool(forKey: "is_change_profile")
  40. }
  41. public static func setProfile(value: Bool){
  42. UserDefaults.standard.set(value, forKey: "is_change_profile")
  43. }
  44. static func setIconCenterAnim2(value: String){
  45. UserDefaults.standard.set(value, forKey: "pb_fb_icon_center_self_mode2")
  46. }
  47. static func getIconCenterAnim2() -> String? {
  48. UserDefaults.standard.string(forKey: "pb_fb_icon_center_self_mode2") ?? Utils.getURLBase() + "get_file_from_path?img=525_411217560576.gif"
  49. }
  50. static func setIconCenterAnim4(value: String){
  51. UserDefaults.standard.set(value, forKey: "pb_fb_icon_center_self_mode4")
  52. }
  53. static func getIconCenterAnim4() -> String? {
  54. UserDefaults.standard.string(forKey: "pb_fb_icon_center_self_mode4") ?? Utils.getURLBase() + "get_file_from_path?img=semut1.gif"
  55. }
  56. static func setURLFirstTab(value: String) {
  57. UserDefaults.standard.set(value, forKey: "app_builder_url_first_tab")
  58. }
  59. static func setURLThirdTab(value: String) {
  60. UserDefaults.standard.set(value, forKey: "app_builder_url_third_tab")
  61. }
  62. static func setURLBase(value: String) {
  63. UserDefaults.standard.set(value, forKey: "app_builder_url_base")
  64. }
  65. static func setURLQMS(value: String) {
  66. UserDefaults.standard.set(value, forKey: "app_builder_url_qms")
  67. }
  68. static func setIconDock(value: String) {
  69. UserDefaults.standard.set(value, forKey: "app_builder_icon_dock")
  70. }
  71. static func setIconSS(value: String) {
  72. UserDefaults.standard.set(value, forKey: "app_builder_icon_ss")
  73. }
  74. static func setBackground(value: String) {
  75. UserDefaults.standard.set(value, forKey: "app_builder_background")
  76. }
  77. static func setURLPrivacyPolicy(value: String){
  78. UserDefaults.standard.set(value, forKey: "app_builder_url_privacy_policy")
  79. }
  80. static func setEnablePrivacyPolicy(value: Bool){
  81. UserDefaults.standard.set(value, forKey: "app_builder_enable_privacy_policy")
  82. }
  83. static func setCustomTab(cust: String){
  84. UserDefaults.standard.set(cust, forKey: "custom_tab")
  85. }
  86. static func setACTheme(value: String){
  87. UserDefaults.standard.set(value, forKey: "app_builder_ac_theme")
  88. }
  89. static func setButtonURL(value: String){
  90. UserDefaults.standard.set(value, forKey: "app_builder_button_url")
  91. }
  92. static func setCustomButtons(value: String){
  93. UserDefaults.standard.set(value, forKey: "app_builder_custom_buttons")
  94. }
  95. static func getCustomButtons() -> String {
  96. UserDefaults.standard.string(forKey: "app_builder_custom_buttons") ?? ""
  97. }
  98. static func setCustomFBIcon(value: String){
  99. UserDefaults.standard.set(value, forKey: "app_builder_button_icon")
  100. }
  101. static func getCustomFBIcon() -> String {
  102. UserDefaults.standard.string(forKey: "app_builder_button_icon") ?? ""
  103. }
  104. static func setEnableMobileBuilder(value: String){
  105. UserDefaults.standard.set(value, forKey: "app_builder_enable_mobile_builder")
  106. }
  107. static func setFinishInitPrefs(value: Bool){
  108. UserDefaults.standard.set(value, forKey: "finish_init_prefs")
  109. }
  110. public static func getFinishInitPrefsr() -> Bool {
  111. UserDefaults.standard.bool(forKey: "finish_init_prefs")
  112. }
  113. static func setConfigModeFB(value: String) {
  114. UserDefaults.standard.set(value, forKey: "fb_config_mode")
  115. }
  116. static func getConfigModeFB() -> String {
  117. UserDefaults.standard.string(forKey: "fb_config_mode") ?? "1"
  118. }
  119. public static func sGetCurrentDateTime(sFormat: String!) -> String! {
  120. let todaysDate = NSDate()
  121. let dateFormatter = DateFormatter()
  122. dateFormatter.dateFormat = sFormat
  123. return dateFormatter.string(from: todaysDate as Date)
  124. }
  125. // public static func getMD5(string: String) -> Data {
  126. // let length = Int(CC_MD5_DIGEST_LENGTH)
  127. // let messageData = string.data(using:.utf8)!
  128. // var digestData = Data(count: length)
  129. //
  130. // _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
  131. // messageData.withUnsafeBytes { messageBytes -> UInt8 in
  132. // if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
  133. // let messageLength = CC_LONG(messageData.count)
  134. // CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
  135. // }
  136. // return 0
  137. // }
  138. // }
  139. // return digestData
  140. // }
  141. public static let callDurationFormatter: DateComponentsFormatter = {
  142. let dateFormatter: DateComponentsFormatter
  143. dateFormatter = DateComponentsFormatter()
  144. dateFormatter.unitsStyle = .positional
  145. dateFormatter.allowedUnits = [.minute, .second]
  146. dateFormatter.zeroFormattingBehavior = .pad
  147. return dateFormatter
  148. }()
  149. public static func previewMessageText(chat: Chat) -> Any {
  150. if chat.credential == "1" && chat.lock == "2" {
  151. return ("🚫 _"+"Message has expired".localized()+"_").richText(group_id: chat.pin)
  152. } else if chat.credential == "1" {
  153. return "Confidential Message".localized().richText(group_id: chat.pin)
  154. } else if chat.attachmentFlag == "27" {
  155. return ("📄 " + "Live Streaming".localized()).richText(group_id: chat.pin)
  156. } else if chat.attachmentFlag == "26" {
  157. return ("📄 " + "Seminar".localized()).richText(group_id: chat.pin)
  158. } else if !chat.image.isEmpty {
  159. if !chat.messageText.isEmpty {
  160. return "📷 \(chat.messageText)".richText(group_id: chat.pin)
  161. } else {
  162. return ("📷 " + "Photo".localized()).richText(group_id: chat.pin)
  163. }
  164. }
  165. else if !chat.video.isEmpty {
  166. if !chat.messageText.isEmpty {
  167. return "📹 \(chat.messageText)".richText(group_id: chat.pin)
  168. } else {
  169. return ("📹 " + "Video".localized()).richText(group_id: chat.pin)
  170. }
  171. }
  172. else if !chat.file.isEmpty {
  173. if chat.messageScope == "18" {
  174. return ("📄 Form").richText(group_id: chat.pin)
  175. }
  176. return ("📄 " + chat.messageText.components(separatedBy: "|")[0]).richText(group_id: chat.pin)
  177. } else if chat.attachmentFlag == "11" {
  178. return ("❤️ " + "Sticker".localized()).richText(group_id: chat.pin)
  179. }
  180. else {
  181. return chat.messageText.richText(group_id: chat.pin)
  182. }
  183. }
  184. static func getURLBase() -> String {
  185. return UserDefaults.standard.string(forKey: "app_builder_url_base") ?? "https://nexilis.io/"
  186. }
  187. static func getIconDock() -> String {
  188. return UserDefaults.standard.string(forKey: "app_builder_icon_dock") ?? ""
  189. }
  190. static func getUrlDock() -> String? {
  191. return Utils.getURLBase() + "get_file_from_path?img=" + Utils.getIconDock()
  192. }
  193. static func setDefaultCC(value: String){
  194. UserDefaults.standard.set(value, forKey: "default_cc")
  195. }
  196. static func getDefaultCC() -> String? {
  197. return UserDefaults.standard.string(forKey: "default_cc")
  198. }
  199. static func setFloatingAnim(value: String){
  200. UserDefaults.standard.set(value, forKey: "fb_floating_anim")
  201. }
  202. static func getFloatingAnim() -> String {
  203. return UserDefaults.standard.string(forKey: "fb_floating_anim") ?? "1~1"
  204. }
  205. static func setFBIconBg(value: String){
  206. UserDefaults.standard.set(value, forKey: "fb_icon_with_bg")
  207. }
  208. static func getFBIconBg() -> String {
  209. return UserDefaults.standard.string(forKey: "fb_icon_with_bg") ?? "1"
  210. }
  211. static func setFBItemBg(value: String){
  212. UserDefaults.standard.set(value, forKey: "fb_item_with_bg")
  213. }
  214. static func getFBItemBg() -> String {
  215. return UserDefaults.standard.string(forKey: "fb_item_with_bg") ?? "1"
  216. }
  217. static func setDomainOpr(value: String){
  218. UserDefaults.standard.set(value, forKey: "domain_opr")
  219. }
  220. static func getDomainOpr() -> String {
  221. return UserDefaults.standard.string(forKey: "domain_opr") ?? "https://nexilis.io/"
  222. }
  223. static func setIpPortOpr(value: String){
  224. UserDefaults.standard.set(value, forKey: "ip_opr")
  225. }
  226. static func getIpOpr() -> String {
  227. return UserDefaults.standard.string(forKey: "ip_opr") ?? "34.101.172.194:42823"
  228. }
  229. static func setHarcodedIp(value: String){
  230. UserDefaults.standard.set(value, forKey: "harcoded_ip")
  231. }
  232. static func getHarcodedIp() -> String {
  233. return UserDefaults.standard.string(forKey: "harcoded_ip") ?? ""
  234. }
  235. static func setUserAgent(value: String){
  236. UserDefaults.standard.set(value, forKey: "user_agent")
  237. }
  238. public static func getUserAgent() -> String {
  239. return UserDefaults.standard.string(forKey: "user_agent") ?? "easySoftIndonesia"
  240. }
  241. public static func fetchDataWithCookiesAndUserAgent(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
  242. var request = URLRequest(url: url)
  243. request.httpMethod = "GET"
  244. request.setValue(Utils.getUserAgent(), forHTTPHeaderField: "User-Agent")
  245. let cookies = HTTPCookieStorage.shared.cookies ?? []
  246. if let cookieHeader = HTTPCookie.requestHeaderFields(with: cookies)["Cookie"] {
  247. // print("listCookiee: \(cookieHeader)")
  248. request.setValue(cookieHeader, forHTTPHeaderField: "Cookie")
  249. }
  250. let task = URLSession.shared.dataTask(with: request, completionHandler: completion)
  251. task.resume()
  252. }
  253. public static var inTabChats = false
  254. private static let I_BB = 48 // 0
  255. private static let I_BBT_1 = 57 // 9
  256. private static let I_BAT_1 = 65 // A
  257. private static let I_BBT_2 = 90 // Z
  258. private static let I_BAT_2 = 97 // a
  259. private static let I_BA = 122 // z
  260. private static let IC_BB = 33 // !
  261. private static let IC_BBT_1 = 47 // /
  262. private static let IC_BAT_1 = 58 // :
  263. private static let IC_BBT_2 = 64 // @
  264. private static let IC_BAT_2 = 91 // [
  265. private static let IC_BBT_3 = 96 // @
  266. private static let IC_BAT_3 = 123 // [
  267. private static let IC_BA = 126 // `
  268. private static var icIGNORE = Set<Int>()
  269. private static func initIcIgnore() {
  270. icIGNORE.insert(10)// \r
  271. icIGNORE.insert(13)// \n
  272. icIGNORE.insert(32)// <space>
  273. }
  274. public static func decrypt(str: String) -> String {
  275. var arr: [Character]
  276. var iRandom = 0
  277. var sDecrypt: String
  278. iRandom = Int(str.substring(from: 0, to: 0)) ?? 0
  279. sDecrypt = getPalindrom(str: str.substring(from: 1, to: nil))
  280. arr = Array(sDecrypt)
  281. for i in 0..<arr.count {
  282. if (isSpecialChar(ch: arr[i])) {
  283. arr[i] = getBeforecChar(ch: arr[i], inc: iRandom)
  284. } else {
  285. arr[i] = getBeforeChar(ch: arr[i], inc: iRandom)
  286. }
  287. }
  288. return String(arr)
  289. }
  290. private static func isSpecialChar(ch: Character) -> Bool {
  291. let ch = Int(ch.asciiValue ?? 0)
  292. return (ch >= IC_BB && ch <= IC_BBT_1) || (ch >= IC_BAT_1 && ch <= IC_BBT_2) || (ch >= IC_BAT_2 && ch <= IC_BBT_3) || (ch >= IC_BAT_3 && ch <= IC_BA)
  293. }
  294. private static func getPalindrom(str: String) -> String {
  295. let arr: [Character] = Array(str)
  296. var arr2: [Character] = Array(arr)
  297. for i in 0..<arr.count {
  298. arr2[i] = arr[arr.count - (i + 1)]
  299. }
  300. return String(arr2)
  301. }
  302. private static func getBeforeChar(ch: Character, inc: Int) -> Character {
  303. if icIGNORE.isEmpty {
  304. initIcIgnore()
  305. }
  306. var iAscii = ch
  307. let iAsciiBefore = iAscii
  308. if (icIGNORE.contains(Int(iAscii.asciiValue ?? 0))) {
  309. return iAscii;
  310. }
  311. if Int(iAscii.asciiValue ?? 0) > I_BA || Int(iAscii.asciiValue ?? 0) < I_BB {
  312. } else {
  313. if !icIGNORE.contains(Int(iAscii.asciiValue ?? 0)) {
  314. iAscii = Character(UnicodeScalar(Int(iAscii.asciiValue ?? 0) - inc)!)
  315. if (I_BAT_1 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= I_BAT_1) {
  316. iAscii = Character(UnicodeScalar((I_BBT_1 + 1) - (I_BAT_1 - Int(iAscii.asciiValue ?? 0)))!)
  317. }
  318. if (I_BAT_2 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= I_BAT_2) {
  319. iAscii = Character(UnicodeScalar((I_BBT_2 + 1) - (I_BAT_2 - Int(iAscii.asciiValue ?? 0)))!)
  320. }
  321. if (Int(iAscii.asciiValue ?? 0) < I_BB) {
  322. iAscii = Character(UnicodeScalar((I_BA + 1) + (Int(iAscii.asciiValue ?? 0) - I_BB))!)
  323. }
  324. }
  325. }
  326. return iAscii
  327. }
  328. private static func getBeforecChar(ch: Character, inc: Int) -> Character {
  329. var iAscii = ch
  330. let iAsciiBefore = iAscii
  331. if (Int(iAscii.asciiValue ?? 0) > IC_BA || Int(iAscii.asciiValue ?? 0) < IC_BB) {
  332. } else {
  333. iAscii = Character(UnicodeScalar(Int(iAscii.asciiValue ?? 0) - inc)!)
  334. if (Int(iAscii.asciiValue ?? 0) < IC_BB) {
  335. iAscii = Character(UnicodeScalar((IC_BA + 1) + (Int(iAscii.asciiValue ?? 0) - IC_BB))!)
  336. if (Int(iAscii.asciiValue ?? 0) < IC_BAT_3 && Int(iAscii.asciiValue ?? 0) > IC_BBT_3) {
  337. iAscii = Character(UnicodeScalar((IC_BBT_3 + 1) - (IC_BAT_3 - Int(iAscii.asciiValue ?? 0)))!)
  338. }
  339. }
  340. if (IC_BAT_3 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= IC_BAT_3) {
  341. iAscii = Character(UnicodeScalar((IC_BBT_3 + 1) - (IC_BAT_3 - Int(iAscii.asciiValue ?? 0)))!)
  342. }
  343. if (IC_BAT_2 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= IC_BAT_2) {
  344. iAscii = Character(UnicodeScalar((IC_BBT_2 + 1) - (IC_BAT_2 - Int(iAscii.asciiValue ?? 0)))!)
  345. }
  346. if (IC_BAT_1 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= IC_BAT_1) {
  347. iAscii = Character(UnicodeScalar((IC_BBT_1 + 1) - (IC_BAT_1 - Int(iAscii.asciiValue ?? 0)))!)
  348. }
  349. }
  350. return iAscii
  351. }
  352. }
  353. public extension UIImage {
  354. var jpeg: Data? { jpegData(compressionQuality: 1) } // QUALITY min = 0 / max = 1
  355. var png: Data? { pngData() }
  356. }
  357. public extension Data {
  358. var uiImage: UIImage? { UIImage(data: self) }
  359. }
  360. public extension UIDevice {
  361. var modelName: String {
  362. var systemInfo = utsname()
  363. uname(&systemInfo)
  364. let machineMirror = Mirror(reflecting: systemInfo.machine)
  365. let identifier = machineMirror.children.reduce("") { identifier, element in
  366. guard let value = element.value as? Int8, value != 0 else { return identifier }
  367. return identifier + String(UnicodeScalar(UInt8(value)))
  368. }
  369. switch identifier {
  370. case "iPod5,1": return "iPod Touch 5"
  371. case "iPod7,1": return "iPod Touch 6"
  372. case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4"
  373. case "iPhone4,1": return "iPhone 4s"
  374. case "iPhone5,1", "iPhone5,2": return "iPhone 5"
  375. case "iPhone5,3", "iPhone5,4": return "iPhone 5c"
  376. case "iPhone6,1", "iPhone6,2": return "iPhone 5s"
  377. case "iPhone7,2": return "iPhone 6"
  378. case "iPhone7,1": return "iPhone 6 Plus"
  379. case "iPhone8,1": return "iPhone 6s"
  380. case "iPhone8,2": return "iPhone 6s Plus"
  381. case "iPhone9,1", "iPhone9,3": return "iPhone 7"
  382. case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
  383. case "iPhone8,4": return "iPhone SE"
  384. case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
  385. case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad 3"
  386. case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad 4"
  387. case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"
  388. case "iPad5,3", "iPad5,4": return "iPad Air 2"
  389. case "iPad2,5", "iPad2,6", "iPad2,7": return "iPad Mini"
  390. case "iPad4,4", "iPad4,5", "iPad4,6": return "iPad Mini 2"
  391. case "iPad4,7", "iPad4,8", "iPad4,9": return "iPad Mini 3"
  392. case "iPad5,1", "iPad5,2": return "iPad Mini 4"
  393. case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
  394. case "AppleTV5,3": return "Apple TV"
  395. case "i386", "x86_64": return "Simulator"
  396. default: return identifier
  397. }
  398. }
  399. }