# Path: mobsf/MobSF/utils.py
# def is_file_exists(file_path):
#     if os.path.isfile(file_path):
#         return True
#     # This fix situation where a user just typed "adb" or another executable
#     # inside settings.py/config.py
#     if shutil.which(file_path):
#         return True
#     else:
#         return False
# 
# def is_safe_path(safe_root, check_path):
#     """Detect Path Traversal."""
#     safe_root = os.path.realpath(os.path.normpath(safe_root))
#     check_path = os.path.realpath(os.path.normpath(check_path))
#     return os.path.commonprefix([check_path, safe_root]) == safe_root
# 
# def print_n_send_error_response(request,
#                                 msg,
#                                 api=False,
#                                 exp='Description'):
#     """Print and log errors."""
#     logger.error(msg)
#     if api:
#         api_response = {'error': msg}
#         return api_response
#     else:
#         context = {
#             'title': 'Error',
#             'exp': exp,
#             'doc': msg,
#             'version': settings.MOBSF_VER,
#         }
#         template = 'general/error.html'
#         return render(request, template, context, status=500)
# 
# def read_sqlite(sqlite_file):
#     """Sqlite Dump - Readable Text."""
#     logger.info('Reading SQLite db')
#     table_dict = {}
#     try:
#         con = sqlite3.connect(sqlite_file)
#         cur = con.cursor()
#         cur.execute('SELECT name FROM sqlite_master WHERE type=\'table\';')
#         tables = cur.fetchall()
#         for table in tables:
#             table_dict[table[0]] = {'head': [], 'data': []}
#             cur.execute('PRAGMA table_info(\'%s\')' % table)
#             rows = cur.fetchall()
#             for sq_row in rows:
#                 table_dict[table[0]]['head'].append(sq_row[1])
#             cur.execute('SELECT * FROM \'%s\'' % table)
#             rows = cur.fetchall()
#             for sq_row in rows:
#                 tmp_row = []
#                 for each_row in sq_row:
#                     tmp_row.append(str(each_row))
#                 table_dict[table[0]]['data'].append(tmp_row)
#     except Exception:
#         logger.exception('Reading SQLite db')
#     return table_dict


import io
import json
import logging
import ntpath
import os
import biplist
from pathlib import Path
from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.utils.html import escape
from mobsf.MobSF.forms import FormUtil
from mobsf.MobSF.utils import (
    is_file_exists,
    is_safe_path,
    print_n_send_error_response,
    read_sqlite,
)
from mobsf.StaticAnalyzer.forms import (
    ViewSourceIOSApiForm,
    ViewSourceIOSForm,
)
            else:
                dat = 'Class Dump result not Found'
        elif typ == 'txt':
            file_format = 'text'
            with io.open(sfile,
                         mode='r',
                         encoding='utf8',
                         errors='ignore') as flip:
                dat = flip.read()
        else:
            if api:
                return {'error': 'Invalid Parameters'}
            return HttpResponseRedirect('/error/')
        context = {
            'title': escape(ntpath.basename(fil)),
            'file': escape(ntpath.basename(fil)),
            'type': file_format,
            'data': dat,
            'sqlite': sql_dump,
            'version': settings.MOBSF_VER,
        }
        template = 'general/view.html'
        if api:
            return context
        return render(request, template, context)
    except Exception as exp:
        logger.exception('Error Viewing Source')
        msg = str(exp)
        exp = exp.__doc__
        if api:

# Path: mobsf/MobSF/utils.py
# def is_file_exists(file_path):
#     if os.path.isfile(file_path):
#         return True
#     # This fix situation where a user just typed "adb" or another executable
#     # inside settings.py/config.py
#     if shutil.which(file_path):
#         return True
#     else:
#         return False
# 
# def is_safe_path(safe_root, check_path):
#     """Detect Path Traversal."""
#     safe_root = os.path.realpath(os.path.normpath(safe_root))
#     check_path = os.path.realpath(os.path.normpath(check_path))
#     return os.path.commonprefix([check_path, safe_root]) == safe_root
# 
# def print_n_send_error_response(request,
#                                 msg,
#                                 api=False,
#                                 exp='Description'):
#     """Print and log errors."""
#     logger.error(msg)
#     if api:
#         api_response = {'error': msg}
#         return api_response
#     else:
#         context = {
#             'title': 'Error',
#             'exp': exp,
#             'doc': msg,
#             'version': settings.MOBSF_VER,
#         }
#         template = 'general/error.html'
#         return render(request, template, context, status=500)
# 
# def read_sqlite(sqlite_file):
#     """Sqlite Dump - Readable Text."""
#     logger.info('Reading SQLite db')
#     table_dict = {}
#     try:
#         con = sqlite3.connect(sqlite_file)
#         cur = con.cursor()
#         cur.execute('SELECT name FROM sqlite_master WHERE type=\'table\';')
#         tables = cur.fetchall()
#         for table in tables:
#             table_dict[table[0]] = {'head': [], 'data': []}
#             cur.execute('PRAGMA table_info(\'%s\')' % table)
#             rows = cur.fetchall()
#             for sq_row in rows:
#                 table_dict[table[0]]['head'].append(sq_row[1])
#             cur.execute('SELECT * FROM \'%s\'' % table)
#             rows = cur.fetchall()
#             for sq_row in rows:
#                 tmp_row = []
#                 for each_row in sq_row:
#                     tmp_row.append(str(each_row))
#                 table_dict[table[0]]['data'].append(tmp_row)
#     except Exception:
#         logger.exception('Reading SQLite db')
#     return table_dict


# Path: mobsf/StaticAnalyzer/views/ios/view_source.py
import io
import json
import logging
import ntpath
import os
import biplist
from pathlib import Path
from django.conf import settings
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.utils.html import escape
from mobsf.MobSF.forms import FormUtil
from mobsf.MobSF.utils import (
    is_file_exists,
    is_safe_path,
    print_n_send_error_response,
    read_sqlite,
)
from mobsf.StaticAnalyzer.forms import (
    ViewSourceIOSApiForm,
    ViewSourceIOSForm,
)

            else:
                dat = 'Class Dump result not Found'
        elif typ == 'txt':
            file_format = 'text'
            with io.open(sfile,
                         mode='r',
                         encoding='utf8',
                         errors='ignore') as flip:
                dat = flip.read()
        else:
            if api:
                return {'error': 'Invalid Parameters'}
            return HttpResponseRedirect('/error/')
        context = {
            'title': escape(ntpath.basename(fil)),
            'file': escape(ntpath.basename(fil)),
            'type': file_format,
            'data': dat,
            'sqlite': sql_dump,
            'version': settings.MOBSF_VER,
        }
        template = 'general/view.html'
        if api:
            return context
        return render(request, template, context)
    except Exception as exp:
        logger.exception('Error Viewing Source')
        msg = str(exp)
        exp = exp.__doc__
        if api:

            return print_n_send_error_response(request, msg, True, exp)