#!/usr/bin/python

import cgi
import cgitb
from spider import *

cgitb.enable()

def validate_input(uri):
    """ The HTML parser apparently accepts any URI without the http part, so if the user inputs such a URI we have to strip it off. Also we need to check if this particular URI exists """
    if uri.find('http://') != -1:
        trail = uri[7:].find('/') 
        if trail != -1:
            site = uri[7:trail+7]
        else:
            site = uri[7:]
    elif uri.find('https://') != -1:
        trail = uri[8:].find('/') 
        if trail != -1:
            site = uri[8:trail+8]
        else:
            site = uri[8:]
    else:
        """Find the site URI without the protocol or the trailing path """
        trail = uri.find('/') 
        if trail != -1:
            site = uri[:trail]
        else:
            site = uri
    return site
    

def print_table(list):
    print "<table style='border = 0'>"
    for val in list:

        print """<tr><td><hr/></td><td><hr/></td></tr>"""
 
        print """<tr>"""
        print """<td><img src='%s' """% (val[0])
        print """height='100'/></td>""" 
        print """<td>By <a href='%s'>""" % (val[3])
        print """%s <a/><br>""" % (val[2])
        if int(val[1]) == 0:
            print "All Rights Reserved"
        else :
            """ The following snippet of code adds the license icons one-by-one as required to indicate the license the Flickr photos come with. Note that the image icons are stored in dig space"""
            #Print the generic CC icons
            print """Has License <img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/cc.jpg' height='20'/> """
            if int(val[1]) == 1:
                print """ <a href= 'http://creativecommons.org/licenses/by-nc-sa/2.5/'><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/attrib.gif' height='20'/><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/nc.gif' height='20'/> <img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/sa.gif' height='20'/></a>"""
                attribution_xhtml = """<div xmlns:cc="http://creativecommons.org/ns#" about="%s"><a rel="cc:attributionURL" href="%s">%s</a> / <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/2.5/">CC BY NC SA 2.5</a></div>""" % (val[0],val[3],val[2])
            if int(val[1]) == 2:
                print """ <a href= 'http://creativecommons.org/licenses/by-nc/2.5/'><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/attrib.gif' height='20'/><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/nc.gif' height='20'/> </a>"""
                attribution_xhtml = """<div xmlns:cc="http://creativecommons.org/ns#" about="%s"><a rel="cc:attributionURL" href="%s">%s</a> / <a rel="license" href="http://creativecommons.org/licenses/by-nc/2.5/">CC BY NC 2.5</a></div>""" % (val[0],val[3],val[2])
            if int(val[1]) == 3:
                print """ <a href= 'http://creativecommons.org/licenses/by-nc-nd/2.5/'><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/attrib.gif' height='20'/><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/nc.gif' height='20'/><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/deriv.gif' height='20'/> </a>"""
                attribution_xhtml = """<div xmlns:cc="http://creativecommons.org/ns#" about="%s"><a rel="cc:attributionURL" href="%s">%s</a> / <a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.5/">CC BY NC ND 2.5</a></div>""" % (val[0],val[3],val[2])
            if int(val[1]) == 4:
                print """ <a href= 'http://creativecommons.org/licenses/by/2.5/'><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/attrib.gif' height='20'/></a>"""
                attribution_xhtml = """<div xmlns:cc="http://creativecommons.org/ns#" about="%s"><a rel="cc:attributionURL" href="%s">%s</a> / <a rel="license" href="http://creativecommons.org/licenses/by-nc/2.5/">CC BY 2.5</a></div>""" % (val[0],val[3],val[2])
            if int(val[1]) == 5:
                print """ <a href= 'http://creativecommons.org/licenses/by-sa/2.5/'><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/attrib.gif' height='20'/><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/sa.gif' height='20'/></a>""" 
                attribution_xhtml = """<div xmlns:cc="http://creativecommons.org/ns#" about="%s"><a rel="cc:attributionURL" href="%s">%s</a> / <a rel="license" href="http://creativecommons.org/licenses/by-sa/2.5/">CC BY SA 2.5</a></div>""" % (val[0],val[3],val[2])
            if int(val[1]) == 6:
                print """ <a href= 'http://creativecommons.org/licenses/by-nd/2.5/'><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/attrib.gif' height='20'/><img src='http://dig.csail.mit.edu/2008/WSRI-Exchange/images/deriv.gif' height='20'/></a>"""            
                attribution_xhtml = """<div xmlns:cc="http://creativecommons.org/ns#" about="%s"><a rel="cc:attributionURL" href="%s">%s</a> / <a rel="license" href="http://creativecommons.org/licenses/by-nd/2.5/">CC BY ND 2.5</a></div>""" % (val[0],val[3],val[2])

            print """</td></tr>"""
            #The attribution XHTML
            print """<tr><td></td><td><i>Attribution XHTML:</i><br/><textarea rows='4' cols='60'>%s</textarea>""" % attribution_xhtml
            print """</td></tr>"""
            print """<tr><td><hr/></td><td><hr/></td></tr>"""
            
    print """</table>"""

def main():
    try:
        print "Content-type: text/html"
        print
        print """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xml:lang="en">
  <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
      <link rel="stylesheet" type="text/css" href="http://www.w3.org/StyleSheets/TR/base.css" />
      <title>Creative Commons Attribution License Violations Validator for Flickr Images</title>
   </head>
   
   <body>
      <h1>Creative Commons  Attribution License Violations Validator for Flickr Images</h1>
"""

        form = cgi.FieldStorage()
        uri = form.getvalue("uri" , "")

        print """
  <p> Enter the URI of a page which has embedded Flickr images. The validator will indicate if you have not properly attributed the owner of those images (if the images are under a Creative Commons Attribution license). You can use the Attribution XHTML provided on the page to correct your errors.
</p>
<p>
      <form method="post" action="validator.cgi">
         <p>URI: 
            <input type="text" size="50" name="uri"/>
            <input type="submit" value="Validate!"/>
         </p>
      </form>
<hr/>
</p>
"""

        if uri != "": #somebody has put in a value for the blog URI
            #TODO: Check whether it's a valid URI
            result = crawl(form.getvalue("uri"))
            attr_given = result[1]
            attr_not_given = result[2]
            nameless = result[3]
            
            n_attr_given = len(attr_given)
            n_attr_not_given = len(attr_not_given)
            n_nameless = len(nameless)

            print """
      <form method="post" action="validator.cgi">
         <p><input type="submit" value="Clear Results"/></p>
      </form>
"""
            print """<b>Results for the page <a href='%s'>""" % (uri) 
            print """%s</a>.</b><br/>""" % (uri)
            if int(n_attr_not_given) == 1:
                print """<br/><p><font color='red'>Found an attribution license violation for the following image.</font></p>"""
                print_table(attr_not_given)
            elif int(n_attr_not_given) > 1:
                print """<br/><p><font color='red'>Found %s attribution license violations for the following images.</font></p>""" % (n_attr_not_given)
                print_table(attr_not_given)
            else:
                print "<p>No license violations detected.<p>"

            if int(n_attr_given) == 1:
                print """<br/><p><font color='green'>The owner/source of the following image was attributed. </font></p>"""
                print_table(attr_given)
            elif int(n_attr_given) > 0:
                print """<br/><p><font color='green'>The owners/sources of the following %s images were attributed.</font></p>""" % (n_attr_given)
                print_table(attr_given)
            
            if int(n_nameless) > 0:
                print """<br/><p>The following images were found in the site, but Flickr does not seem to have a record of the owners of the images.</p>"""
                print_table(nameless)
            
            print """</body></html>"""
            
    except:
        print "<hr>Oops. An error occurred!</hr>"
        cgi.print_exception()
        
if __name__ == "__main__":
  main()
  
