<commit_before>
import urlparse
import base64
import zipfile
import io
import hashlib
import os
import urllib

from PIL import Image

from django.contrib.sites.models import Site
from django.template import RequestContext
from django.shortcuts import render_to_response, redirect
from django.template.loader import render_to_string
from django.core.urlresolvers import reverse
from django.http import HttpResponse, QueryDict
from django.conf import settings
from django.utils.html import escape
from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache
from django.utils.encoding import force_str


from codefisher_apps.favicon_getter.views import get_sized_icons
from toolbar_buttons.builder.app_versions import get_app_versions

def index(request, template_name="tbutton_maker/link-button.html"):
    data = {
        "icon_range": range(1,11),
    }
    return render_to_response(template_name, data, context_instance=RequestContext(request))

@csrf_exempt
def create(request):
    if request.method == 'POST':
        url = request.POST.get("url")
        parsed_url = urlparse.urlparse(url)
        if parsed_url[0] == "":
            url = "http://" + url
        elif parsed_url[0] not in ["http", "https", "ftp", "ftps", "javascript", "file"]:
            redirect(reverse('lbutton-custom'))
        button_id = "lbutton-%s" % hashlib.md5(url).hexdigest()
        icon_type = request.POST.get("icon-type")
        icon_data = {}
        if icon_type == "default":
            icon_name = request.POST.get("default-icon")
            for size in [16, 24, 32]:
                icon_path = os.path.join(settings.BASE_DIR, settings.DEFAULT_LINK_ICONS, '%s-%s.png' % (icon_name, size))
                if os.path.exists(icon_path):
                    with open(icon_path) as fp:
                        icon_data["icon-%s" % size] = base64.encodestring(fp.read())
        elif icon_type == "favicon":
            icons = get_sized_icons(url, [16, 24, 32])
            if icons is None:
                return redirect(reverse('lbutton-custom'))
            for size in [16, 24, 32]:
                value = io.BytesIO()
                icons[size].save(value, "png")
                icon_data["icon-%s" % size] = base64.b64encode(value.getvalue())
                value.close()
        elif icon_type == "custom":
            have = []
            for size in [16, 24, 32]:
                if "icon-%s" % size in request.FILES:
                    have.append("icon-%s" % size)
                    icon_data["icon-%s" % size] = base64.encodestring("".join(c for c in request.FILES["icon-%s" % size].chunks()))
            if len(have) == 0:
                return redirect(reverse('lbutton-custom'))
            elif len(have) != 3:<commit_msg>read-mail now opens SeaMonkey mail, in SeaMonkey if not configuired, and closed security hole in link_button<commit_after>
import urlparse
import base64
import zipfile
import io
import hashlib
import os
import urllib

from PIL import Image

from django.contrib.sites.models import Site
from django.template import RequestContext
from django.shortcuts import render_to_response, redirect
from django.template.loader import render_to_string
from django.core.urlresolvers import reverse
from django.http import HttpResponse, QueryDict
from django.conf import settings
from django.utils.html import escape
from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache
from django.utils.encoding import force_str


from codefisher_apps.favicon_getter.views import get_sized_icons
from toolbar_buttons.builder.app_versions import get_app_versions

def index(request, template_name="tbutton_maker/link-button.html"):
    data = {
        "icon_range": range(1,11),
    }
    return render_to_response(template_name, data, context_instance=RequestContext(request))

@csrf_exempt
def create(request):
    if request.method == 'POST':
        url = request.POST.get("url")
        parsed_url = urlparse.urlparse(url)
        if parsed_url[0] == "":
            url = "http://" + url
        elif parsed_url[0] not in ["http", "https", "ftp", "ftps", "javascript", "file"]:
            redirect(reverse('lbutton-custom'))
        button_id = "lbutton-%s" % hashlib.md5(url).hexdigest()
        icon_type = request.POST.get("icon-type")
        icon_data = {}
        if icon_type == "default":
            icon_name = request.POST.get("default-icon")
            if not icon_name in ["www-%s" % i for i in range(1,11)]:
                icon_name = "www-1"
            for size in [16, 24, 32]:
                icon_path = os.path.join(settings.BASE_DIR, settings.DEFAULT_LINK_ICONS, '%s-%s.png' % (icon_name, size))
                if os.path.exists(icon_path):
                    with open(icon_path) as fp:
                        icon_data["icon-%s" % size] = base64.encodestring(fp.read())
        elif icon_type == "favicon":
            icons = get_sized_icons(url, [16, 24, 32])
            if icons is None:
                return redirect(reverse('lbutton-custom'))
            for size in [16, 24, 32]:
                value = io.BytesIO()
                icons[size].save(value, "png")
                icon_data["icon-%s" % size] = base64.b64encode(value.getvalue())
                value.close()
        elif icon_type == "custom":
            have = []
            for size in [16, 24, 32]:
                if "icon-%s" % size in request.FILES:
                    have.append("icon-%s" % size)
                    icon_data["icon-%s" % size] = base64.encodestring("".join(c for c in request.FILES["icon-%s" % size].chunks()))
            if len(have) == 0:
                return redirect(reverse('lbutton-custom'))
            elif len(have) != 3:
{'fraction_non_alphanumeric': 0.1174018756512678, 'fraction_numerical': 0.01528308440430705, 'mean_word_length': 2.5571340333539223, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5422410', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1685, 'n_words': 430}