Beispiel #1
0
    def test_get_relative_path(self):
        """ get_relative_path() returns a valid relative path. """

        # Guard against '..'.
        self.assertEqual(
            '',
            utils.get_relative_path('{}/../html'),
            msg='failed to guard against ..!'
        )

        # Valid static path.
        self.assertEqual(
            '/static/css/main.min.css',
            utils.get_relative_path('{}/css/main.min.css'.format(
                settings.STATIC_ROOT
            )),
            msg='failed to get get relative path.'
        )

        # Valid base path.
        self.assertEqual(
            '/wp_main',
            utils.get_relative_path('{}/wp_main'.format(
                settings.BASE_PARENT
            )),
            msg='failed to get relative base path.'
        )
Beispiel #2
0
def get_using_paths(dir_path, absolute_path=None, proj=None):
    """ When given a dir as a path,
        find out which file is preferred to use first.
    """

    if absolute_path is None:
        absolute_path = utilities.get_absolute_path(dir_path)

    if not os.path.isdir(absolute_path):
        return (None, None)

    try:
        files = os.listdir(absolute_path)
    except Exception as ex:
        files = []
        log.debug('unable to listdir: {}\n  {}'.format(
            absolute_path,
            ex,
        ))

    # no files in this directory, or bad dir.
    if not files:
        return (None, None)

    default_file = utilities.get_relative_path(
        utilities.append_path(dir_path, files[0])
    )
    # Dir has files, get most important to show.
    if proj is None:
        # No project, try first file.
        return (
            default_file,
            utilities.get_absolute_path(default_file),
        )

    # Has project, see if source_file is good. if so, use it.
    if proj.source_file:
        absolute_path = utilities.get_absolute_path(proj.source_file)
        return (
            utilities.get_relative_path(absolute_path),
            absolute_path,
        )

    # Just use first file found for project.
    return (
        default_file,
        utilities.get_absolute_path(default_file),
    )
Beispiel #3
0
def inject_sourceview(project, source_string,
                      request=None, link_text=None, desc_text=None,
                      target_replacement="{{ source_view }}"):
    """ injects code for source viewing.
        needs wp_project (project) passed to gather info.
        if target_replacement is not found, returns source_string.

        uses sourceview.html template to display. the template handles
        missing information.
        returns rendered source_string.
    """

    # fail check.
    target = check_replacement(source_string, target_replacement)
    if not target:
        return source_string

    # has project info?
    if project is None:
        return source_string.replace(target, "")

    # use source_file if no source_dir was set.
    relativefile = utilities.get_relative_path(project.source_file)
    relativedir = utilities.get_relative_path(project.source_dir)
    relativepath = relativedir if relativedir else relativefile
    # has good link?
    if relativepath == "":
        _log.debug("missing source file/dir for: " + project.name)

    # get default filename to display in link.
    if project.source_file:
        file_name = utilities.get_filename(project.source_file)
    else:
        file_name = project.name

    # get link text
    if link_text is None:
        link_text = file_name + " (local)"

    sourceview = render_clean("home/sourceview.html",
                              context_dict={'project': project,
                                            'file_path': relativepath,
                                            'link_text': link_text,
                                            'desc_text': desc_text,
                                            },
                              request=request,
                              )
    return source_string.replace(target, sourceview)
Beispiel #4
0
def view_scriptkids(request):
    """ return my script kiddie view 
        (for people trying to access wordpress-login pages and stuff like that.)
    """
    
    # get ip if possible.
    ip_address = request.META.get("HTTP_X_FORWARDED_FOR", None)
    if ip_address is None:
        ip_address = request.META.get("REMOTE_ADDR", None)
    use_ip = (ip_address is not None)

    # get insulting image to display
    scriptkid_img = htools.get_scriptkid_image()
    if scriptkid_img is not None:
        scriptkid_img = utilities.get_relative_path(scriptkid_img)
    use_img = (scriptkid_img is not None)
    
    # return formatted template.
    return responses.clean_response("home/scriptkids.html",
                                    {'request': request,
                                     'extra_style_link_list': [utilities.get_browser_style(request),
                                                               "/static/css/highlighter.min.css"],
                                     'use_img': use_img,
                                     'scriptkid_img': scriptkid_img,
                                     'use_ip': use_ip,
                                     'ip_address': ip_address,
                                     })
Beispiel #5
0
def build_download_file_content(project, surl, desc_text=None):
    """ builds download content box from single file url.
        given the file to download, it outputs the html for the
        download section.
    """
    if not desc_text:
        desc_text = ' - Current package.'
    # intial template for this downloadable file link
    html_ = htmltools.html_content("""
        <div class='download-file'>
            <a class='download-link' href='{{ relative_link }}'>
                <span class='download-link-text'>{{ link_text }}</span>
            </a>
            <span class='download-desc'>&nbsp;{{ desc_text }}</span>
        </div>""")
    
    # make link
    html_.replace('{{ relative_link }}',
                  '/dl{}'.format(utilities.get_relative_path(surl)))
    # make desc text
    html_.replace('{{ desc_text }}', desc_text)
    # make link text
    sver = project.version
    if sver == "":
        slinktext = project.name
    else:
        slinktext = project.name + ' v' + sver
    html_.replace('{{ link_text }}', slinktext)
    
    return html_.tostring()
Beispiel #6
0
def get_screenshots(images_dir, noscript_image=None):
    """ Retrieves html formatted screenshots box for all images in
        a directory.
        Returns None on failure.
        Arguments:
            images_dir          : Relative dir containing all the images.

        Keyword Arguments:
            noscript_image      : Path to image to show for <noscript> tag.

    """
    # accceptable image formats (last 4 chars)
    formats = ['.png', '.jpg', '.gif', '.bmp', '.jpeg']

    if os.path.isdir(images_dir):
        dirpath = images_dir
    else:
        # Make sure we are using the right dir.
        # get absolute path for images dir.
        dirpath = utilities.get_absolute_path(images_dir)
    if not dirpath:
        log.error('No images dir: {!r} -> {!r}'.format(images_dir, dirpath))
        return None

    # Get useable relative dir (user-passed may be wrong format)
    relative_dir = utilities.get_relative_path(dirpath)

    # find acceptable pics
    try:
        all_files = os.listdir(dirpath)
    except Exception as ex:
        log.error('Can\'t list dir: {}\n{}'.format(dirpath, ex))
        return None

    # Help functions for building screenshots.
    def relative_img(filename):
        return os.path.join(relative_dir, filename)

    def good_format(filename):
        return os.path.splitext(filename)[-1] in formats

    # Build acceptable pics list
    good_pics = [relative_img(f) for f in all_files if good_format(f)]

    # auto-pick noscript image if needed
    if (len(good_pics) > 0) and (noscript_image is None):
        noscript_image = good_pics[0]
    else:
        # no good pics.
        noscript_image = None

    # Render from template.
    screenshots = render_clean(
        'home/imageviewer.html',
        context={
            'images': good_pics,
            'noscript_image': noscript_image,
        })
    return screenshots
Beispiel #7
0
def get_screenshots(images_dir, noscript_image=None):
    """ Retrieves html formatted screenshots box for all images in
        a directory.
        Returns None on failure.
        Arguments:
            images_dir          : Relative dir containing all the images.

        Keyword Arguments:
            noscript_image      : Path to image to show for <noscript> tag.

    """
    # accceptable image formats (last 4 chars)
    formats = ['.png', '.jpg', '.gif', '.bmp', '.jpeg']

    if os.path.isdir(images_dir):
        dirpath = images_dir
    else:
        # Make sure we are using the right dir.
        # get absolute path for images dir.
        dirpath = utilities.get_absolute_path(images_dir)
    if not dirpath:
        log.error('No images dir: {!r} -> {!r}'.format(images_dir, dirpath))
        return None

    # Get useable relative dir (user-passed may be wrong format)
    relative_dir = utilities.get_relative_path(dirpath)

    # find acceptable pics
    try:
        all_files = os.listdir(dirpath)
    except Exception as ex:
        log.error('Can\'t list dir: {}\n{}'.format(dirpath, ex))
        return None

    # Help functions for building screenshots.
    def relative_img(filename):
        return os.path.join(relative_dir, filename)

    def good_format(filename):
        return os.path.splitext(filename)[-1] in formats

    # Build acceptable pics list
    good_pics = [relative_img(f) for f in all_files if good_format(f)]

    # auto-pick noscript image if needed
    if (len(good_pics) > 0) and (noscript_image is None):
        noscript_image = good_pics[0]
    else:
        # no good pics.
        noscript_image = None

    # Render from template.
    screenshots = render_clean('home/imageviewer.html',
                               context={
                                   'images': good_pics,
                                   'noscript_image': noscript_image,
                               })
    return screenshots
Beispiel #8
0
 def get_download_file(self):
     """ Return relative download path, only if it's valid.
         Returns empty string on invalid or missing paths.
     """
     absolute = self.get_download_file_abs()
     if absolute:
         return utilities.get_relative_path(absolute)
     log.debug('Bad file name passed: {}'.format(absolute))
     return ''
Beispiel #9
0
    def test_get_relative_path(self):
        """ get_relative_path() returns a valid relative path. """

        # Guard against '..'.
        self.assertEqual('',
                         utils.get_relative_path('{}/../html'),
                         msg='failed to guard against ..!')

        # Valid static path.
        self.assertEqual('/static/css/main.min.css',
                         utils.get_relative_path('{}/css/main.min.css'.format(
                             settings.STATIC_ROOT)),
                         msg='failed to get get relative path.')

        # Valid base path.
        self.assertEqual('/wp_main',
                         utils.get_relative_path('{}/wp_main'.format(
                             settings.BASE_PARENT)),
                         msg='failed to get relative base path.')
Beispiel #10
0
def get_screenshots(images_dir, noscript_image=None):
    """ Retrieves html formatted screenshots box for all images in
        a directory.
        Returns None on failure.
        Arguments:
            images_dir          : Relative dir containing all the images.

        Keyword Arguments:
            noscript_image      : Path to image to show for <noscript> tag.

    """
    # accceptable image formats (last 4 chars)
    formats = [".png", ".jpg", ".gif", ".bmp", "jpeg"]

    # Make sure we are using the right dir.
    # get absolute path for images dir,
    # if none exists then delete the target_replacement.
    images_dir = utilities.get_absolute_path(images_dir)
    if not images_dir:
        return None

    # Get useable relative dir (user-passed may be wrong format)
    relative_dir = utilities.get_relative_path(images_dir)

    # find acceptable pics
    try:
        all_files = os.listdir(images_dir)
    except Exception as ex:
        _log.debug("Can't list dir: " + images_dir + '\n' + str(ex))
        return None

    # Help functions for building screenshots.
    relative_img = lambda filename: os.path.join(relative_dir, filename)
    good_format = lambda filename: (filename[-4:] in formats)

    # Build acceptable pics list
    good_pics = [relative_img(f) for f in all_files if good_format(f)]

    # auto-pick noscript image if needed
    if (len(good_pics) > 0) and (noscript_image is None):
        noscript_image = good_pics[0]
    else:
        # no good pics.
        noscript_image = None

    # Render from template.
    screenshots = render_clean("home/screenshots.html",
                               context_dict={'images': good_pics,
                                             'noscript_image': noscript_image,
                                             })
    return screenshots
Beispiel #11
0
    def get_sourceview(self):
        """ Retrieves code for source viewing.
            Uses sourceview.html template to display.
            The template handles missing information.
            Returns rendered string (html code).
        """
        # has project and request info?
        if (self.project is None) or (self.request is None):
            return None

        # use source_file if no source_dir was set.
        relativefile = utilities.get_relative_path(self.project.source_file)
        relativedir = utilities.get_relative_path(self.project.source_dir)
        relativepath = relativedir if relativedir else relativefile
        # has good link?
        if not relativepath:
            logfmt = 'Missing source file/dir for: {}'
            log.debug(logfmt.format(self.project.name))

        # get default filename to display in link.
        if self.project.source_file:
            file_name = utilities.get_filename(self.project.source_file)
        else:
            file_name = self.project.name

        # get link text
        link_text = '{} (local)'.format(file_name)

        return htmltools.render_clean(
            'projects/sourceview.html',
            context={
                'project': self.project,
                'file_path': relativepath,
                'link_text': link_text,
                'desc_text': None,
            },
            request=self.request)
Beispiel #12
0
    def get_images(self):
        """ Retrieves html formatted images box for all images in
            a directory.
            Returns None on failure.
            Arguments:
                images_dir          : Relative dir containing all the images.
        """
        # accceptable image formats
        formats = ('.png', '.jpg', '.gif', '.bmp', '.jpeg')

        # Make sure we are using the right dir.
        # get absolute path for images dir,
        # if none exists then no screenshots are given.
        images_dir = utilities.get_absolute_path(self.images_dir)
        if not images_dir:
            return None

        # Get useable relative dir (user-passed may be wrong format)
        relative_dir = utilities.get_relative_path(images_dir)

        # find acceptable pics
        try:
            all_files = os.listdir(images_dir)
        except Exception as ex:
            log.debug('Can\'t list dir: {}\n{}'.format(images_dir, ex))
            return None

        # Build acceptable pics list
        good_pics = [
            os.path.join(relative_dir, f)
            for f in all_files
            if os.path.splitext(f)[-1] in formats
        ]

        # auto-pick noscript image if needed
        if len(good_pics) > 0:
            noscript_image = good_pics[0]
        else:
            # no good pics.
            noscript_image = None

        # Render from template.
        c = {
            'images': good_pics,
            'noscript_image': noscript_image,
        }
        return htmltools.render_clean(
            'home/imageviewer.html',
            context=c)
Beispiel #13
0
    def get_images(self):
        """ Retrieves html formatted images box for all images in
            a directory.
            Returns None on failure.
            Arguments:
                images_dir          : Relative dir containing all the images.
        """
        # accceptable image formats
        formats = ('.png', '.jpg', '.gif', '.bmp', '.jpeg')

        # Make sure we are using the right dir.
        # get absolute path for images dir,
        # if none exists then no screenshots are given.
        images_dir = utilities.get_absolute_path(self.images_dir)
        if not images_dir:
            return None

        # Get useable relative dir (user-passed may be wrong format)
        relative_dir = utilities.get_relative_path(images_dir)

        # find acceptable pics
        try:
            all_files = os.listdir(images_dir)
        except Exception as ex:
            log.debug('Can\'t list dir: {}\n{}'.format(images_dir, ex))
            return None

        # Build acceptable pics list
        good_pics = [
            os.path.join(relative_dir, f) for f in all_files
            if os.path.splitext(f)[-1] in formats
        ]

        # auto-pick noscript image if needed
        if len(good_pics) > 0:
            noscript_image = good_pics[0]
        else:
            # no good pics.
            noscript_image = None

        # Render from template.
        c = {
            'images': good_pics,
            'noscript_image': noscript_image,
        }
        return htmltools.render_clean('home/imageviewer.html', context=c)
Beispiel #14
0
def view_scriptkids(request):
    """ return my script kiddie view
        for people trying to access wordpress-login pages and stuff like that.
    """

    # get ip if possible.
    ip_address = utilities.get_remote_ip(request)
    try:
        path = request.path
    except AttributeError:
        path = '<error getting path>'
    log.error('ScriptKid Access from: {} -> {}'.format(ip_address, path))

    # get insulting image to display
    scriptkid_img = hometools.get_scriptkid_image()
    if scriptkid_img is not None:
        scriptkid_img = utilities.get_relative_path(scriptkid_img)
    use_img = (scriptkid_img is not None)
    use_ip = (ip_address is not None)
    context = {
        'use_img': use_img,
        'scriptkid_img': scriptkid_img,
        'use_ip': use_ip,
        'ip_address': ip_address,
    }
    # Try banning the ip.
    ban_ip = use_ip and (ip_address != '127.0.0.1')
    if ban_ip:
        if utilities.ban_add(request):
            log.error('Banned script kid: {}'.format(ip_address))
        else:
            log.error('Could not ban script kid: {}'.format(ip_address))
    else:
        log.debug('Not banning scriptkid: {}'.format(ip_address))

    # return formatted template.
    return responses.clean_response(
        'home/scriptkids.html',
        context=context,
        request=request)
Beispiel #15
0
def get_source_files(project):
    """ returns list of all source files for a project, if any.
        uses relative static path. (/static/files/project/source/file.py)
        returns None on  failure.
    """

    if project.source_dir == "":
        source_files = []
    else:
        sabsolute = utilities.get_absolute_path(project.source_dir)
        if sabsolute == "":
            source_files = []
        else:
            # retrieve all source filenames
            file_names = os.listdir(sabsolute)

            if len(file_names) == 0:
                source_files = []
            else:
                source_files = []
                for sfilename in file_names:
                    srelativepath = utilities.get_relative_path(os.path.join(project.source_dir, sfilename))  # noqa
                    source_files.append(srelativepath)
    return source_files
Beispiel #16
0
def get_source_files(project):
    """ returns list of all source files for a project, if any.
        uses relative static path. (/static/files/project/source/file.py)
        returns None on  failure.
    """
    
    if project.source_dir == "":
        source_files = []
    else:
        sabsolute = utilities.get_absolute_path(project.source_dir)
        if sabsolute == "":
            source_files = []
        else:
            # retrieve all source filenames
            file_names = os.listdir(sabsolute)

            if len(file_names) == 0:
                source_files = []
            else:
                source_files = []
                for sfilename in file_names:
                    srelativepath = utilities.get_relative_path(os.path.join(project.source_dir, sfilename))  # noqa
                    source_files.append(srelativepath)
    return source_files
Beispiel #17
0
def get_file_info(file_path):
    """ retrieves actual file content for viewer. """
    absolute_path = utilities.get_absolute_path(file_path)
    static_path = utilities.get_relative_path(file_path)
    # no file to load.
    if not absolute_path:
        log.error(
            'Invalid file path for viewer.get_file_content(): {}'.format(
                file_path))
        raise Http404('Sorry, that file doesn\'t exist.')

    project = ptools.get_project_from_path(absolute_path)

    # Directory was passed, get files to use. (based on project, dir listing)
    if os.path.isdir(absolute_path):
        if project:
            static_path, absolute_path = get_using_paths(
                static_path,
                proj=project
            )
            if static_path is None or absolute_path is None:
                # Raise error which will display a 404.
                raise Http404('Sorry, there was an error viewing that file.')
        else:
            raise Http404('Sorry, that file doesn\'t exist.')

    # Update project view count tracker
    miscobj = None
    if project:
        project.view_count += 1
        project.save()
    else:
        # Not a project, may be a Misc Object.
        miscobj = misctools.get_by_filename(file_path)
        log.debug('Found Misc Object: {}'.format(repr(miscobj)))

        # Update misc view count tracker
        if miscobj:
            miscobj.view_count += 1
            miscobj.save()
        else:
            log.debug(
                'get_file_content: not a project or misc object: {}'.format(
                    file_path))

    # Update file tracker
    if os.path.isfile(absolute_path):
        filetracker = dltools.get_file_tracker(absolute_path)
        if filetracker is not None:
            if project is not None:
                dltools.update_tracker_projects(
                    filetracker,
                    project,
                    dosave=False
                )
            filetracker.view_count += 1
            filetracker.save()

    # Get file content
    try:
        with open(absolute_path) as fread:
            file_content = fread.read()
    except Exception as ex:
        log.error('Error loading file: {}\n{}'.format(absolute_path, ex))
        raise Http404('Sorry, I can\'t load that file right now.')

    fileinfo = {
        'project': project,
        'miscobj': miscobj,
        'static_path': static_path,
        'absolute_path': absolute_path,
        'file_content': file_content,
    }
    if project:
        fileinfo['name'] = project.name
        fileinfo['related_url'] = '/projects/{}'.format(project.alias)
    elif miscobj:
        fileinfo['name'] = miscobj.name
        fileinfo['related_url'] = '/misc/{}'.format(miscobj.alias)
    else:
        fileinfo['name'] = None
        fileinfo['related_url'] = None
    return fileinfo
Beispiel #18
0
def get_file_info(file_path):
    """ retrieves actual file content for viewer. """
    absolute_path = utilities.get_absolute_path(file_path)
    static_path = utilities.get_relative_path(file_path)
    # no file to load.
    if not absolute_path:
        _log.error('Invalid file path for viewer.get_file_content(): '
                   '{}'.format(file_path))
        raise Http404("Sorry, that file doesn't exist.")
        
    project = ptools.get_project_from_path(absolute_path)
    
    # Directory was passed, get files to use. (based on project, dir listing)
    if os.path.isdir(absolute_path):
        if project:
            static_path, absolute_path = get_using_paths(static_path,
                                                         absolute_path,
                                                         project)
            if static_path is None or absolute_path is None:
                # Raise error which will display a 404.
                raise Http404("Sorry, there was an error viewing that file.")
        else:
            raise Http404("Sorry, that file doesn't exist.")
    
    # Update project view count tracker
    miscobj = None
    if project:
        project.view_count += 1
        project.save()
    else:
        # Not a project, may be a Misc Object.
        miscobj = misctools.get_by_filename(file_path)
        logdebug('Found Misc Object: {}'.format(repr(miscobj)))
        
        # Update misc view count tracker
        if miscobj:
            miscobj.view_count += 1
            miscobj.save()
        else:
            _log.debug('get_file_content: not a project or misc object: '
                       '{}'.format(file_path))
            
    # Update file tracker
    if os.path.isfile(absolute_path):
        filetracker = dltools.get_file_tracker(absolute_path)
        if filetracker is not None:
            if project is not None:
                dltools.update_tracker_projects(filetracker,
                                                project,
                                                dosave=False)
            filetracker.view_count += 1
            filetracker.save()
    
    # Get file content
    file_content = ""
    try:
        with open(absolute_path) as fread:
            file_content = fread.read()
    except Exception as ex:
        _log.error("Error loading file: " + absolute_path + '\n' + str(ex))
        raise Http404("Sorry, I can't load that file right now.")
    
    fileinfo = {'project': project,
                'miscobj': miscobj,
                'static_path': static_path,
                'absolute_path': absolute_path,
                'file_content': file_content, }
    return fileinfo