123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- //
- // Utils.swift
- // Runner
- //
- // Created by Rifqy Fakhrul Rijal on 13/08/20.
- // Copyright © 2020 The Chromium Authors. All rights reserved.
- //
- import Foundation
- import UIKit
- //import var CommonCrypto.CC_MD5_DIGEST_LENGTH
- //import func CommonCrypto.CC_MD5
- //import typealias CommonCrypto.CC_LONG
- public final class Utils {
-
- public static func getCurrentTime()->Int64 {
- return Int64(Date().timeIntervalSince1970)
- }
-
- public static func getCurrentTimeMillis()->Int64 {
- return Int64(Date().timeIntervalSince1970 * 1000)
- }
-
- public static func getCurrentTimeNanos()->Int64 {
- return Int64(Date().timeIntervalSince1970 * 1000_000_000)
- }
-
- public static func getElapsedRealtime() -> Int64 {
- return Int64((ProcessInfo().systemUptime).rounded()) // SystemClock.elapsedRealtime();
- }
-
- public static func getElapsedRealtimeMillis() -> Int64 {
- return Int64((ProcessInfo().systemUptime * 1000).rounded()) // SystemClock.elapsedRealtime();
- }
-
- public static func getElapsedRealtimeNanos() -> Int64 {
- return Int64((ProcessInfo().systemUptime * 1000_000_000).rounded()) // SystemClock.elapsedRealtimeNano();
- }
-
- public static func getForceAnonymous() -> Bool {
- return UserDefaults.standard.bool(forKey: "force_anonymous")
- }
-
- public static func setForceAnonymous(value: Bool){
- UserDefaults.standard.set(value, forKey: "force_anonymous")
- }
-
- public static func getSetProfile() -> Bool {
- return UserDefaults.standard.bool(forKey: "is_change_profile")
- }
-
- public static func setProfile(value: Bool){
- UserDefaults.standard.set(value, forKey: "is_change_profile")
- }
-
- static func setIconCenterAnim2(value: String){
- UserDefaults.standard.set(value, forKey: "pb_fb_icon_center_self_mode2")
- }
-
- static func getIconCenterAnim2() -> String? {
- UserDefaults.standard.string(forKey: "pb_fb_icon_center_self_mode2") ?? Utils.getURLBase() + "get_file_from_path?img=525_411217560576.gif"
- }
-
- static func setIconCenterAnim4(value: String){
- UserDefaults.standard.set(value, forKey: "pb_fb_icon_center_self_mode4")
- }
-
- static func getIconCenterAnim4() -> String? {
- UserDefaults.standard.string(forKey: "pb_fb_icon_center_self_mode4") ?? Utils.getURLBase() + "get_file_from_path?img=semut1.gif"
- }
-
- static func setURLFirstTab(value: String) {
- UserDefaults.standard.set(value, forKey: "app_builder_url_first_tab")
- }
-
- static func setURLThirdTab(value: String) {
- UserDefaults.standard.set(value, forKey: "app_builder_url_third_tab")
- }
-
- static func setURLBase(value: String) {
- UserDefaults.standard.set(value, forKey: "app_builder_url_base")
- }
-
- static func setURLQMS(value: String) {
- UserDefaults.standard.set(value, forKey: "app_builder_url_qms")
- }
-
- static func setIconDock(value: String) {
- UserDefaults.standard.set(value, forKey: "app_builder_icon_dock")
- }
-
- static func setIconSS(value: String) {
- UserDefaults.standard.set(value, forKey: "app_builder_icon_ss")
- }
-
- static func setBackground(value: String) {
- UserDefaults.standard.set(value, forKey: "app_builder_background")
- }
-
- static func setURLPrivacyPolicy(value: String){
- UserDefaults.standard.set(value, forKey: "app_builder_url_privacy_policy")
- }
-
- static func setEnablePrivacyPolicy(value: Bool){
- UserDefaults.standard.set(value, forKey: "app_builder_enable_privacy_policy")
- }
-
- static func setCustomTab(cust: String){
- UserDefaults.standard.set(cust, forKey: "custom_tab")
- }
-
- static func setACTheme(value: String){
- UserDefaults.standard.set(value, forKey: "app_builder_ac_theme")
- }
-
- static func setButtonURL(value: String){
- UserDefaults.standard.set(value, forKey: "app_builder_button_url")
- }
-
- static func setCustomButtons(value: String){
- UserDefaults.standard.set(value, forKey: "app_builder_custom_buttons")
- }
-
- static func getCustomButtons() -> String {
- UserDefaults.standard.string(forKey: "app_builder_custom_buttons") ?? ""
- }
-
- static func setCustomFBIcon(value: String){
- UserDefaults.standard.set(value, forKey: "app_builder_button_icon")
- }
-
- static func getCustomFBIcon() -> String {
- UserDefaults.standard.string(forKey: "app_builder_button_icon") ?? ""
- }
-
- static func setEnableMobileBuilder(value: String){
- UserDefaults.standard.set(value, forKey: "app_builder_enable_mobile_builder")
- }
-
- static func setFinishInitPrefs(value: Bool){
- UserDefaults.standard.set(value, forKey: "finish_init_prefs")
- }
-
- public static func getFinishInitPrefsr() -> Bool {
- UserDefaults.standard.bool(forKey: "finish_init_prefs")
- }
-
- static func setConfigModeFB(value: String) {
- UserDefaults.standard.set(value, forKey: "fb_config_mode")
- }
-
- static func getConfigModeFB() -> String {
- UserDefaults.standard.string(forKey: "fb_config_mode") ?? "1"
- }
-
-
- public static func sGetCurrentDateTime(sFormat: String!) -> String! {
- let todaysDate = NSDate()
- let dateFormatter = DateFormatter()
- dateFormatter.dateFormat = sFormat
- return dateFormatter.string(from: todaysDate as Date)
- }
-
- // public static func getMD5(string: String) -> Data {
- // let length = Int(CC_MD5_DIGEST_LENGTH)
- // let messageData = string.data(using:.utf8)!
- // var digestData = Data(count: length)
- //
- // _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
- // messageData.withUnsafeBytes { messageBytes -> UInt8 in
- // if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
- // let messageLength = CC_LONG(messageData.count)
- // CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
- // }
- // return 0
- // }
- // }
- // return digestData
- // }
-
- public static let callDurationFormatter: DateComponentsFormatter = {
- let dateFormatter: DateComponentsFormatter
- dateFormatter = DateComponentsFormatter()
- dateFormatter.unitsStyle = .positional
- dateFormatter.allowedUnits = [.minute, .second]
- dateFormatter.zeroFormattingBehavior = .pad
-
- return dateFormatter
- }()
-
- public static func previewMessageText(chat: Chat) -> Any {
- if chat.credential == "1" && chat.lock == "2" {
- return ("🚫 _"+"Message has expired".localized()+"_").richText(group_id: chat.pin)
- } else if chat.credential == "1" {
- return "Confidential Message".localized().richText(group_id: chat.pin)
- } else if chat.attachmentFlag == "27" {
- return ("📄 " + "Live Streaming".localized()).richText(group_id: chat.pin)
- } else if chat.attachmentFlag == "26" {
- return ("📄 " + "Seminar".localized()).richText(group_id: chat.pin)
- } else if !chat.image.isEmpty {
- if !chat.messageText.isEmpty {
- return "📷 \(chat.messageText)".richText(group_id: chat.pin)
- } else {
- return ("📷 " + "Photo".localized()).richText(group_id: chat.pin)
- }
- }
- else if !chat.video.isEmpty {
- if !chat.messageText.isEmpty {
- return "📹 \(chat.messageText)".richText(group_id: chat.pin)
- } else {
- return ("📹 " + "Video".localized()).richText(group_id: chat.pin)
- }
- }
- else if !chat.file.isEmpty {
- if chat.messageScope == "18" {
- return ("📄 Form").richText(group_id: chat.pin)
- }
- return ("📄 " + chat.messageText.components(separatedBy: "|")[0]).richText(group_id: chat.pin)
- } else if chat.attachmentFlag == "11" {
- return ("❤️ " + "Sticker".localized()).richText(group_id: chat.pin)
- }
- else {
- return chat.messageText.richText(group_id: chat.pin)
- }
- }
-
- static func getURLBase() -> String {
- return UserDefaults.standard.string(forKey: "app_builder_url_base") ?? "https://nexilis.io/"
- }
-
- static func getIconDock() -> String {
- return UserDefaults.standard.string(forKey: "app_builder_icon_dock") ?? ""
- }
-
- static func getUrlDock() -> String? {
- return Utils.getURLBase() + "get_file_from_path?img=" + Utils.getIconDock()
- }
-
- static func setDefaultCC(value: String){
- UserDefaults.standard.set(value, forKey: "default_cc")
- }
-
- static func getDefaultCC() -> String? {
- return UserDefaults.standard.string(forKey: "default_cc")
- }
-
- static func setFloatingAnim(value: String){
- UserDefaults.standard.set(value, forKey: "fb_floating_anim")
- }
-
- static func getFloatingAnim() -> String {
- return UserDefaults.standard.string(forKey: "fb_floating_anim") ?? "1~1"
- }
-
- static func setFBIconBg(value: String){
- UserDefaults.standard.set(value, forKey: "fb_icon_with_bg")
- }
-
- static func getFBIconBg() -> String {
- return UserDefaults.standard.string(forKey: "fb_icon_with_bg") ?? "1"
- }
-
- static func setFBItemBg(value: String){
- UserDefaults.standard.set(value, forKey: "fb_item_with_bg")
- }
-
- static func getFBItemBg() -> String {
- return UserDefaults.standard.string(forKey: "fb_item_with_bg") ?? "1"
- }
-
- static func setDomainOpr(value: String){
- UserDefaults.standard.set(value, forKey: "domain_opr")
- }
-
- static func getDomainOpr() -> String {
- return UserDefaults.standard.string(forKey: "domain_opr") ?? "https://nexilis.io/"
- }
-
- static func setIpPortOpr(value: String){
- UserDefaults.standard.set(value, forKey: "ip_opr")
- }
-
- static func getIpOpr() -> String {
- return UserDefaults.standard.string(forKey: "ip_opr") ?? "34.101.172.194:42823"
- }
-
- static func setHarcodedIp(value: String){
- UserDefaults.standard.set(value, forKey: "harcoded_ip")
- }
-
- static func getHarcodedIp() -> String {
- return UserDefaults.standard.string(forKey: "harcoded_ip") ?? ""
- }
-
- static func setUserAgent(value: String){
- UserDefaults.standard.set(value, forKey: "user_agent")
- }
-
- public static func getUserAgent() -> String {
- return UserDefaults.standard.string(forKey: "user_agent") ?? "easySoftIndonesia"
- }
-
- public static func fetchDataWithCookiesAndUserAgent(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
- var request = URLRequest(url: url)
- request.httpMethod = "GET"
- request.setValue(Utils.getUserAgent(), forHTTPHeaderField: "User-Agent")
- let cookies = HTTPCookieStorage.shared.cookies ?? []
- if let cookieHeader = HTTPCookie.requestHeaderFields(with: cookies)["Cookie"] {
- // print("listCookiee: \(cookieHeader)")
- request.setValue(cookieHeader, forHTTPHeaderField: "Cookie")
- }
- let task = URLSession.shared.dataTask(with: request, completionHandler: completion)
- task.resume()
- }
-
- public static var inTabChats = false
-
- private static let I_BB = 48 // 0
- private static let I_BBT_1 = 57 // 9
- private static let I_BAT_1 = 65 // A
- private static let I_BBT_2 = 90 // Z
- private static let I_BAT_2 = 97 // a
- private static let I_BA = 122 // z
- private static let IC_BB = 33 // !
- private static let IC_BBT_1 = 47 // /
- private static let IC_BAT_1 = 58 // :
- private static let IC_BBT_2 = 64 // @
- private static let IC_BAT_2 = 91 // [
- private static let IC_BBT_3 = 96 // @
- private static let IC_BAT_3 = 123 // [
- private static let IC_BA = 126 // `
- private static var icIGNORE = Set<Int>()
- private static func initIcIgnore() {
- icIGNORE.insert(10)// \r
- icIGNORE.insert(13)// \n
- icIGNORE.insert(32)// <space>
- }
-
- public static func decrypt(str: String) -> String {
- var arr: [Character]
- var iRandom = 0
- var sDecrypt: String
- iRandom = Int(str.substring(from: 0, to: 0)) ?? 0
- sDecrypt = getPalindrom(str: str.substring(from: 1, to: nil))
- arr = Array(sDecrypt)
- for i in 0..<arr.count {
- if (isSpecialChar(ch: arr[i])) {
- arr[i] = getBeforecChar(ch: arr[i], inc: iRandom)
- } else {
- arr[i] = getBeforeChar(ch: arr[i], inc: iRandom)
- }
- }
- return String(arr)
- }
-
- private static func isSpecialChar(ch: Character) -> Bool {
- let ch = Int(ch.asciiValue ?? 0)
- 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)
- }
-
- private static func getPalindrom(str: String) -> String {
- let arr: [Character] = Array(str)
- var arr2: [Character] = Array(arr)
- for i in 0..<arr.count {
- arr2[i] = arr[arr.count - (i + 1)]
- }
- return String(arr2)
- }
-
- private static func getBeforeChar(ch: Character, inc: Int) -> Character {
- if icIGNORE.isEmpty {
- initIcIgnore()
- }
- var iAscii = ch
- let iAsciiBefore = iAscii
- if (icIGNORE.contains(Int(iAscii.asciiValue ?? 0))) {
- return iAscii;
- }
- if Int(iAscii.asciiValue ?? 0) > I_BA || Int(iAscii.asciiValue ?? 0) < I_BB {
- } else {
- if !icIGNORE.contains(Int(iAscii.asciiValue ?? 0)) {
- iAscii = Character(UnicodeScalar(Int(iAscii.asciiValue ?? 0) - inc)!)
- if (I_BAT_1 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= I_BAT_1) {
- iAscii = Character(UnicodeScalar((I_BBT_1 + 1) - (I_BAT_1 - Int(iAscii.asciiValue ?? 0)))!)
- }
- if (I_BAT_2 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= I_BAT_2) {
- iAscii = Character(UnicodeScalar((I_BBT_2 + 1) - (I_BAT_2 - Int(iAscii.asciiValue ?? 0)))!)
- }
- if (Int(iAscii.asciiValue ?? 0) < I_BB) {
- iAscii = Character(UnicodeScalar((I_BA + 1) + (Int(iAscii.asciiValue ?? 0) - I_BB))!)
- }
- }
- }
- return iAscii
- }
-
- private static func getBeforecChar(ch: Character, inc: Int) -> Character {
- var iAscii = ch
- let iAsciiBefore = iAscii
- if (Int(iAscii.asciiValue ?? 0) > IC_BA || Int(iAscii.asciiValue ?? 0) < IC_BB) {
- } else {
- iAscii = Character(UnicodeScalar(Int(iAscii.asciiValue ?? 0) - inc)!)
- if (Int(iAscii.asciiValue ?? 0) < IC_BB) {
- iAscii = Character(UnicodeScalar((IC_BA + 1) + (Int(iAscii.asciiValue ?? 0) - IC_BB))!)
- if (Int(iAscii.asciiValue ?? 0) < IC_BAT_3 && Int(iAscii.asciiValue ?? 0) > IC_BBT_3) {
- iAscii = Character(UnicodeScalar((IC_BBT_3 + 1) - (IC_BAT_3 - Int(iAscii.asciiValue ?? 0)))!)
- }
- }
- if (IC_BAT_3 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= IC_BAT_3) {
- iAscii = Character(UnicodeScalar((IC_BBT_3 + 1) - (IC_BAT_3 - Int(iAscii.asciiValue ?? 0)))!)
- }
- if (IC_BAT_2 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= IC_BAT_2) {
- iAscii = Character(UnicodeScalar((IC_BBT_2 + 1) - (IC_BAT_2 - Int(iAscii.asciiValue ?? 0)))!)
- }
- if (IC_BAT_1 > Int(iAscii.asciiValue ?? 0) && Int(iAsciiBefore.asciiValue ?? 0) >= IC_BAT_1) {
- iAscii = Character(UnicodeScalar((IC_BBT_1 + 1) - (IC_BAT_1 - Int(iAscii.asciiValue ?? 0)))!)
- }
- }
- return iAscii
- }
- }
- public extension UIImage {
- var jpeg: Data? { jpegData(compressionQuality: 1) } // QUALITY min = 0 / max = 1
- var png: Data? { pngData() }
- }
- public extension Data {
- var uiImage: UIImage? { UIImage(data: self) }
- }
- public extension UIDevice {
-
- var modelName: String {
- var systemInfo = utsname()
- uname(&systemInfo)
- let machineMirror = Mirror(reflecting: systemInfo.machine)
- let identifier = machineMirror.children.reduce("") { identifier, element in
- guard let value = element.value as? Int8, value != 0 else { return identifier }
- return identifier + String(UnicodeScalar(UInt8(value)))
- }
-
- switch identifier {
- case "iPod5,1": return "iPod Touch 5"
- case "iPod7,1": return "iPod Touch 6"
- case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4"
- case "iPhone4,1": return "iPhone 4s"
- case "iPhone5,1", "iPhone5,2": return "iPhone 5"
- case "iPhone5,3", "iPhone5,4": return "iPhone 5c"
- case "iPhone6,1", "iPhone6,2": return "iPhone 5s"
- case "iPhone7,2": return "iPhone 6"
- case "iPhone7,1": return "iPhone 6 Plus"
- case "iPhone8,1": return "iPhone 6s"
- case "iPhone8,2": return "iPhone 6s Plus"
- case "iPhone9,1", "iPhone9,3": return "iPhone 7"
- case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
- case "iPhone8,4": return "iPhone SE"
- case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
- case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad 3"
- case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad 4"
- case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"
- case "iPad5,3", "iPad5,4": return "iPad Air 2"
- case "iPad2,5", "iPad2,6", "iPad2,7": return "iPad Mini"
- case "iPad4,4", "iPad4,5", "iPad4,6": return "iPad Mini 2"
- case "iPad4,7", "iPad4,8", "iPad4,9": return "iPad Mini 3"
- case "iPad5,1", "iPad5,2": return "iPad Mini 4"
- case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
- case "AppleTV5,3": return "Apple TV"
- case "i386", "x86_64": return "Simulator"
- default: return identifier
- }
- }
- }
|