def test_guess_format_from_url_local_gpl_license(self):
        local_path = os.path.join(CFG_TMPDIR, "LICENSE")
        print >>open(
            local_path, "w"
        ), """
            GNU GENERAL PUBLIC LICENSE
               Version 2, June 1991

 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
                       59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

                Preamble

  The licenses for most software are designed to take away your
freedom to share and change it.  By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users.  This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it.  (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.)  You can apply it to
your programs, too.

  When we speak of free software, we are referring to freedom, not
price.  Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.

  To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
[...]
"""
        try:
            if CFG_HAS_MAGIC:
                self.assertEqual(guess_format_from_url(local_path), ".txt")
            else:
                self.assertEqual(guess_format_from_url(local_path), ".bin")
        finally:
            os.remove(local_path)
Exemplo n.º 2
0
    def test_guess_format_from_url_local_gpl_license(self):
        local_path = os.path.join(CFG_TMPDIR, 'LICENSE')
        print >> open(local_path, 'w'), """
            GNU GENERAL PUBLIC LICENSE
               Version 2, June 1991

 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
                       59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

                Preamble

  The licenses for most software are designed to take away your
freedom to share and change it.  By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users.  This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it.  (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.)  You can apply it to
your programs, too.

  When we speak of free software, we are referring to freedom, not
price.  Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.

  To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
[...]
"""
        try:
            if CFG_HAS_MAGIC:
                self.assertEqual(guess_format_from_url(local_path), '.txt')
            else:
                self.assertEqual(guess_format_from_url(local_path), '.bin')
        finally:
            os.remove(local_path)
Exemplo n.º 3
0
 def test_guess_format_from_url_local_known_ext(self):
     """bibdocfile - guess_format_from_url(), local URL, unknown extension"""
     self.assertEqual(
         guess_format_from_url(os.path.join(CFG_WEBDIR, 'img', 'test.gif')),
         '.gif')
Exemplo n.º 4
0
 def test_guess_format_from_url_local_no_ext_with_magic(self):
     """bibdocfile - guess_format_from_url(), local URL, no extension, with magic"""
     self.assertEqual(
         guess_format_from_url(
             os.path.join(CFG_WEBDIR, 'img', 'testgif')), '.gif')
Exemplo n.º 5
0
def forge_email(fromaddr, toaddr, subject, content, html_content='',
                html_images=None, usebcc=False, header=None, footer=None,
                html_header=None, html_footer=None, ln=CFG_SITE_LANG,
                charset=None, replytoaddr="", attachments=None, bccaddr=""):
    """Prepare email. Add header and footer if needed.
    @param fromaddr: [string] sender
    @param toaddr: [string or list-of-strings] list of receivers (if string, then
                   receivers are separated by ',')
    @param usebcc: [bool] True for using Bcc in place of To
    @param subject: [string] subject of the email
    @param content: [string] content of the email
    @param html_content: [string] html version of the email
    @param html_images: [dict] dictionary of image id, image path
    @param header: [string] None for the default header
    @param footer: [string] None for the default footer
    @param ln: language
    @charset: [string] the content charset. By default is None which means
    to try to encode the email as ascii, then latin1 then utf-8.
    @param replytoaddr: [string or list-of-strings] to be used for the
                        reply-to header of the email (if string, then
                        receivers are separated by ',')
    @param attachments: list of paths of files to be attached. Alternatively,
        every element of the list could be a tuple: (filename, mimetype)
    @param bccaddr: [string or list-of-strings] to be used for BCC header of the email
                    (if string, then receivers are separated by ',')
    @return: forged email as a string"""
    if html_images is None:
        html_images = {}

    if header is None:
        content = email_header(ln) + content
    else:
        content = header + content
    if footer is None:
        content += email_footer(ln)
    else:
        content += footer

    if charset is None:
        (content, content_charset) = guess_minimum_encoding(content)
    else:
        content_charset = charset

    subject = get_mail_header(subject)
    fromaddr = get_mail_header(fromaddr)
    toaddr = get_mail_header(toaddr)
    replytoaddr = get_mail_header(replytoaddr)
    bccaddr = get_mail_header(bccaddr)

    toaddr = remove_temporary_emails(toaddr)

    if html_content:
        if html_header is None:
            html_content = email_html_header(ln) + html_content
        else:
            html_content = html_header + html_content
        if html_footer is None:
            html_content += email_html_footer(ln)
        else:
            html_content += html_footer

        if charset is None:
            (html_content, html_content_charset) = guess_minimum_encoding(html_content)
        else:
            html_content_charset = charset

        msg_root = MIMEMultipart('alternative')
        msg_root.preamble = 'This is a multi-part message in MIME format.'

        msg_text = MIMEText(content, _charset=content_charset)
        msg_root.attach(msg_text)

        msg_text = MIMEText(html_content, 'html', _charset=html_content_charset)
        if not html_images:
            # No image? Attach the HTML to the root
            msg_root.attach(msg_text)
        else:
            # Image(s)? Attach the HTML and image(s) as children of a
            # "related" block
            msg_related = MIMEMultipart('related')
            msg_related.attach(msg_text)
            for image_id, image_path in html_images.iteritems():
                msg_image = MIMEImage(open(image_path, 'rb').read())
                msg_image.add_header('Content-ID', '<%s>' % image_id)
                msg_image.add_header('Content-Disposition', 'attachment', filename=os.path.split(image_path)[1])
                msg_related.attach(msg_image)
            msg_root.attach(msg_related)
    else:
        msg_root = MIMEText(content, _charset=content_charset)

    if attachments:
        from invenio.bibdocfile import _mimes, guess_format_from_url
        old_msg_root = msg_root
        msg_root = MIMEMultipart()
        msg_root.attach(old_msg_root)
        for attachment in attachments:
            try:
                if type(attachment) in (list, tuple):
                    attachment, mime = attachment
                if mime is None:
                    ## Automatic guessing of mimetype
                    mime = _mimes.guess_type(attachment)[0]
                if mime is None:
                    ext = guess_format_from_url(attachment)
                    mime = _mimes.guess_type("foo" + ext)[0]
                if not mime:
                    mime = 'application/octet-stream'
                part = MIMEBase(*mime.split('/', 1))
                part.set_payload(open(attachment, 'rb').read())
                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attachment))
                msg_root.attach(part)
            except:
                register_exception(alert_admin=True, prefix="Can't attach %s" % attachment)

    msg_root['From'] = fromaddr
    if replytoaddr:
        msg_root['Reply-To'] = replytoaddr
    if usebcc:
        msg_root['Bcc'] = toaddr
        msg_root['To'] = 'Undisclosed.Recipients:'
        if bccaddr:
            msg_root['Bcc'] += ",%s" % (bccaddr,)
    else:
        msg_root['To'] = toaddr
        if bccaddr:
            msg_root['Bcc'] = bccaddr
    msg_root['Date'] = formatdate(localtime=True)
    msg_root['Subject'] = subject
    msg_root['User-Agent'] = 'Invenio %s at %s' % (CFG_VERSION, CFG_SITE_URL)
    return msg_root.as_string()
Exemplo n.º 6
0
 def test_guess_format_from_url_local_no_ext(self):
     """bibdocfile - guess_format_from_url(), local URL, no extension"""
     self.assertEqual(
         guess_format_from_url(os.path.join(CFG_WEBDIR, 'img', 'test')),
         '.bin')
Exemplo n.º 7
0
 def test_guess_format_from_url_remote_known_ext(self):
     """bibdocfile - guess_format_from_url(), remote URL, known extension"""
     self.assertEqual(guess_format_from_url(CFG_SITE_URL + '/img/test.gif'),
                      '.gif')
Exemplo n.º 8
0
 def test_guess_format_from_url_remote_no_ext_with_magic(self):
     """bibdocfile - guess_format_from_url(), remote URL, no extension, no magic"""
     self.failUnless(
         guess_format_from_url(CFG_SITE_URL +
                               '/img/testgif') in ('.bin', '.gif'))
 def test_guess_format_from_url_remote_no_ext(self):
     """bibdocfile - guess_format_from_url(), remote URL, no extension"""
     self.assertEqual(guess_format_from_url(CFG_SITE_URL + "/img/test"), ".bin")
 def test_guess_format_from_url_local_known_ext(self):
     """bibdocfile - guess_format_from_url(), local URL, unknown extension"""
     self.assertEqual(guess_format_from_url(os.path.join(CFG_WEBDIR, "img", "test.gif")), ".gif")
 def test_guess_format_from_url_local_no_ext_with_magic(self):
     """bibdocfile - guess_format_from_url(), local URL, no extension, no magic"""
     self.assertEqual(guess_format_from_url(os.path.join(CFG_WEBDIR, "img", "testgif")), ".bin")
 def test_guess_format_from_url_local_unknown_ext(self):
     """bibdocfile - guess_format_from_url(), local URL, unknown extension"""
     self.assertEqual(guess_format_from_url(os.path.join(CFG_WEBDIR, 'img', 'test.foo')), '.foo')
 def test_guess_format_from_url_local_no_ext_with_magic(self):
     """bibdocfile - guess_format_from_url(), local URL, no extension, with magic"""
     self.assertEqual(guess_format_from_url(os.path.join(CFG_WEBDIR, 'img', 'testgif')), '.gif')
 def test_guess_format_from_url_local_no_ext(self):
     """bibdocfile - guess_format_from_url(), local URL, no extension"""
     self.assertEqual(guess_format_from_url(os.path.join(CFG_WEBDIR, 'img', 'test')), '.bin')
 def test_guess_format_from_url_remote_unknown_ext(self):
     """bibdocfile - guess_format_from_url(), remote URL, unknown extension, with magic"""
     self.assertEqual(guess_format_from_url(CFG_SITE_URL + '/img/test.foo'), '.gif')
Exemplo n.º 16
0
 def test_guess_format_from_url_remote_no_ext(self):
     """bibdocfile - guess_format_from_url(), remote URL, no extension"""
     self.assertEqual(guess_format_from_url(CFG_SITE_URL + '/img/test'),
                      '.bin')
Exemplo n.º 17
0
 def test_guess_format_from_url_remote_no_ext_with_magic(self):
     """bibdocfile - guess_format_from_url(), remote URL, no extension, with magic"""
     self.assertEqual(
         guess_format_from_url(CFG_SITE_URL + '/img/testgif'), '.gif')
 def test_guess_format_from_url_remote_no_ext_with_magic(self):
     """bibdocfile - guess_format_from_url(), remote URL, no extension, with magic"""
     self.assertEqual(guess_format_from_url(CFG_SITE_URL + "/img/testgif"), ".gif")
Exemplo n.º 19
0
 def test_guess_format_from_url_remote_unknown_ext(self):
     """bibdocfile - guess_format_from_url(), remote URL, unknown extension, no magic"""
     self.failUnless(
         guess_format_from_url(CFG_SITE_URL +
                               '/img/test.foo') in ('.bin', '.gif'))
 def test_guess_format_from_url_remote_no_ext_with_magic(self):
     """bibdocfile - guess_format_from_url(), remote URL, no extension, no magic"""
     self.failUnless(guess_format_from_url(CFG_SITE_URL + "/img/testgif") in (".bin", ".gif"))
 def test_guess_format_from_url_remote_unknown_ext(self):
     """bibdocfile - guess_format_from_url(), remote URL, unknown extension, no magic"""
     self.failUnless(guess_format_from_url(CFG_SITE_URL + "/img/test.foo") in (".bin", ".gif"))
 def test_guess_format_from_url_remote_known_ext(self):
     """bibdocfile - guess_format_from_url(), remote URL, known extension"""
     self.assertEqual(guess_format_from_url(CFG_SITE_URL + "/img/test.gif"), ".gif")
Exemplo n.º 23
0
def forge_email(fromaddr, toaddr, subject, content, html_content='',
                html_images=None, usebcc=False, header=None, footer=None,
                html_header=None, html_footer=None, ln=CFG_SITE_LANG,
                charset=None, replytoaddr="", attachments=None, bccaddr=""):
    """Prepare email. Add header and footer if needed.
    @param fromaddr: [string] sender
    @param toaddr: [string or list-of-strings] list of receivers (if string, then
                   receivers are separated by ',')
    @param usebcc: [bool] True for using Bcc in place of To
    @param subject: [string] subject of the email
    @param content: [string] content of the email
    @param html_content: [string] html version of the email
    @param html_images: [dict] dictionary of image id, image path
    @param header: [string] None for the default header
    @param footer: [string] None for the default footer
    @param ln: language
    @charset: [string] the content charset. By default is None which means
    to try to encode the email as ascii, then latin1 then utf-8.
    @param replytoaddr: [string or list-of-strings] to be used for the
                        reply-to header of the email (if string, then
                        receivers are separated by ',')
    @param attachments: list of paths of files to be attached. Alternatively,
        every element of the list could be a tuple: (filename, mimetype)
    @param bccaddr: [string or list-of-strings] to be used for BCC header of the email
                    (if string, then receivers are separated by ',')
    @return: forged email as a string"""
    if html_images is None:
        html_images = {}

    if header is None:
        content = email_header(ln) + content
    else:
        content = header + content
    if footer is None:
        content += email_footer(ln)
    else:
        content += footer

    if charset is None:
        (content, content_charset) = guess_minimum_encoding(content)
    else:
        content_charset = charset

    subject = get_mail_header(subject)
    fromaddr = get_mail_header(fromaddr)
    toaddr = get_mail_header(toaddr)
    replytoaddr = get_mail_header(replytoaddr)
    bccaddr = get_mail_header(bccaddr)

    toaddr = remove_temporary_emails(toaddr)

    if html_content:
        if html_header is None:
            html_content = email_html_header(ln) + html_content
        else:
            html_content = html_header + html_content
        if html_footer is None:
            html_content += email_html_footer(ln)
        else:
            html_content += html_footer

        if charset is None:
            (html_content, html_content_charset) = guess_minimum_encoding(html_content)
        else:
            html_content_charset = charset

        msg_root = MIMEMultipart('alternative')
        msg_root.preamble = 'This is a multi-part message in MIME format.'

        msg_text = MIMEText(content, _charset=content_charset)
        msg_root.attach(msg_text)

        msg_text = MIMEText(html_content, 'html', _charset=html_content_charset)
        if not html_images:
            # No image? Attach the HTML to the root
            msg_root.attach(msg_text)
        else:
            # Image(s)? Attach the HTML and image(s) as children of a
            # "related" block
            msg_related = MIMEMultipart('related')
            msg_related.attach(msg_text)
            for image_id, image_path in html_images.iteritems():
                msg_image = MIMEImage(open(image_path, 'rb').read())
                msg_image.add_header('Content-ID', '<%s>' % image_id)
                msg_image.add_header('Content-Disposition', 'attachment', filename=os.path.split(image_path)[1])
                msg_related.attach(msg_image)
            msg_root.attach(msg_related)
    else:
        msg_root = MIMEText(content, _charset=content_charset)

    if attachments:
        from invenio.bibdocfile import _mimes, guess_format_from_url
        old_msg_root = msg_root
        msg_root = MIMEMultipart()
        msg_root.attach(old_msg_root)
        for attachment in attachments:
            try:
                if type(attachment) in (list, tuple):
                    attachment, mime = attachment
                if mime is None:
                    ## Automatic guessing of mimetype
                    mime = _mimes.guess_type(attachment)[0]
                if mime is None:
                    ext = guess_format_from_url(attachment)
                    mime = _mimes.guess_type("foo" + ext)[0]
                if not mime:
                    mime = 'application/octet-stream'
                part = MIMEBase(*mime.split('/', 1))
                part.set_payload(open(attachment, 'rb').read())
                Encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attachment))
                msg_root.attach(part)
            except:
                register_exception(alert_admin=True, prefix="Can't attach %s" % attachment)

    msg_root['From'] = fromaddr
    if replytoaddr:
        msg_root['Reply-To'] = replytoaddr
    if usebcc:
        msg_root['Bcc'] = toaddr
        msg_root['To'] = 'Undisclosed.Recipients:'
        if bccaddr:
            msg_root['Bcc'] += ",%s" % (bccaddr,)
    else:
        msg_root['To'] = toaddr
        if bccaddr:
            msg_root['Bcc'] = bccaddr
    msg_root['Date'] = formatdate(localtime=True)
    msg_root['Subject'] = subject
    msg_root['User-Agent'] = 'Invenio %s at %s' % (CFG_VERSION, CFG_SITE_URL)
    return msg_root.as_string()
Exemplo n.º 24
0
def forge_email(fromaddr,
                toaddr,
                subject,
                content,
                html_content='',
                html_images=None,
                usebcc=False,
                header=None,
                footer=None,
                html_header=None,
                html_footer=None,
                ln=CFG_SITE_LANG,
                charset=None,
                replytoaddr="",
                attachments=None):
    """Prepare email. Add header and footer if needed.
    @param fromaddr: [string] sender
    @param toaddr: [string or list-of-strings] list of receivers (if string, then
                   receivers are separated by ',')
    @param usebcc: [bool] True for using Bcc in place of To
    @param subject: [string] subject of the email
    @param content: [string] content of the email
    @param html_content: [string] html version of the email
    @param html_images: [dict] dictionary of image id, image path
    @param header: [string] None for the default header
    @param footer: [string] None for the default footer
    @param ln: language
    @charset: [string] the content charset. By default is None which means
    to try to encode the email as ascii, then latin1 then utf-8.
    @param replytoaddr: [string or list-of-strings] to be used for the
                        reply-to header of the email (if string, then
                        receivers are separated by ',')
    @param attachments: list of paths of files to be attached. Alternatively,
        every element of the list could be a tuple: (filename, mimetype)
    @return: forged email as a string"""
    if html_images is None:
        html_images = {}

    content = render_template_to_string('mail_text.tpl',
                                        content=content,
                                        header=header,
                                        footer=footer)

    if type(toaddr) is not str:
        toaddr = ','.join(toaddr)

    if type(replytoaddr) is not str:
        replytoaddr = ','.join(replytoaddr)

    toaddr = remove_temporary_emails(toaddr)

    headers = {}
    kwargs = {'to': [], 'cc': [], 'bcc': []}

    if replytoaddr:
        headers['Reply-To'] = replytoaddr
    if usebcc:
        headers['Bcc'] = toaddr
        kwargs['bcc'] = toaddr.split(',')
        kwargs['to'] = ['Undisclosed.Recipients:']
    else:
        kwargs['to'] = toaddr.split(',')
    headers['From'] = fromaddr
    headers['Date'] = formatdate(localtime=True)
    headers['User-Agent'] = 'Invenio %s at %s' % (CFG_VERSION, CFG_SITE_URL)

    if html_content:
        html_content = render_template_to_string('mail_html.tpl',
                                                 content=html_content,
                                                 header=html_header,
                                                 footer=html_footer)

        msg_root = EmailMultiAlternatives(subject=subject,
                                          body=content,
                                          from_email=fromaddr,
                                          headers=headers,
                                          **kwargs)
        msg_root.attach_alternative(html_content, "text/html")

        #if not html_images:
        #    # No image? Attach the HTML to the root
        #    msg_root.attach(msg_text)
        #else:
        if html_images:
            # Image(s)? Attach the HTML and image(s) as children of a
            # "related" block
            msg_related = MIMEMultipart('related')
            #msg_related.attach(msg_text)
            for image_id, image_path in html_images.iteritems():
                attach_embed_image(msg_related, image_id, image_path)
            msg_root.attach(msg_related)
    else:
        msg_root = EmailMessage(subject=subject,
                                body=content,
                                from_email=fromaddr,
                                headers=headers,
                                **kwargs)

    if attachments:
        from invenio.bibdocfile import _mimes, guess_format_from_url
        #old_msg_root = msg_root
        #msg_root = MIMEMultipart()
        #msg_root.attach(old_msg_root)
        for attachment in attachments:
            try:
                mime = None
                if type(attachment) in (list, tuple):
                    attachment, mime = attachment
                if mime is None:
                    ## Automatic guessing of mimetype
                    mime = _mimes.guess_type(attachment)[0]
                if mime is None:
                    ext = guess_format_from_url(attachment)
                    mime = _mimes.guess_type("foo" + ext)[0]
                if not mime:
                    mime = 'application/octet-stream'
                part = MIMEBase(*mime.split('/', 1))
                part.set_payload(open(attachment, 'rb').read())
                Encoders.encode_base64(part)
                part.add_header(
                    'Content-Disposition',
                    'attachment; filename="%s"' % os.path.basename(attachment))
                msg_root.attach(part)
            except:
                register_exception(alert_admin=True,
                                   prefix="Can't attach %s" % attachment)

    return msg_root