' Present Web Browser Favourites in Dropdown on Local Web Server MyFavourites.html  R.Metcalfe  July 2013 RJM Programming 
' When dropdown option picked give a summary of line/word/character count before going to that website.
Imports System
Imports System.IO

Module Module1

    Function anotherlayer(ByVal inurl As String, ByVal phpcheckplace As String) As String
        anotherlayer = inurl
        If System.IO.File.Exists(phpcheckplace + "fgc.php") = False Then
            System.IO.File.WriteAllText(phpcheckplace + "fgc.php", "<?php" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "// Url line/word/character counter R.Metcalfe  RJM Programming July 2013" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$uris = " + Chr(34) + "http://127.0.0.1/" + Chr(34) + ";" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "if (isset($_GET[inurl])) $uris = str_replace(" + Chr(34) + "~!~" + Chr(34) + "," + Chr(34) + "?" + Chr(34) + ",$_GET[inurl]);" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$wordcount = 0;" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$charactercount = 0;" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$data = file_get_contents(($uris)); //read the file" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$charactercount = strlen($data);" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$convert = explode(" + Chr(34) + "\" + "n" + Chr(34) + ", $data); //create array separate by new line" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$linecount = count($convert);" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$blurb = " + Chr(34) + "<s" + Chr(34) + " . " + Chr(34) + "cript> alert('" + Chr(34) + " . $uris . " + Chr(34) + " consists of " + Chr(34) + " . $linecount . " + Chr(34) + " lines and " + Chr(34) + ";" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "$bigblurb = " + Chr(34) + "" + Chr(34) + ";" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "for ($i=0;$i<count($convert);$i++)" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "{" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "  if (strlen($convert[$i]) > 0) $wordcount += 1;" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "  for ($ii=1;$ii<strlen($convert[$i]);$ii++)" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "  {" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "    if (substr($convert[$i],$ii-1,1) == " + Chr(34) + " " + Chr(34) + " && substr($convert[$i],$ii,1) != " + Chr(34) + " " + Chr(34) + ")" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "    {" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "       $wordcount += 1;" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "    }" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "  }" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", " $bigblurb = $blurb . $wordcount . " + Chr(34) + " (html-)words and " + Chr(34) + " . $charactercount;" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "  $bigblurb = $bigblurb . " + Chr(34) + " characters.'); window.location = '" + Chr(34) + " . $uris . " + Chr(34) + "'; </script>" + Chr(34) + ";" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "  $out = str_replace(" + Chr(34) + "</body>" + Chr(34) + ",($bigblurb . " + Chr(34) + "</Body>" + Chr(34) + "),$convert[$i]);" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "  $out = str_replace(" + Chr(34) + "</BODY>" + Chr(34) + ",($bigblurb . " + Chr(34) + "</Body>" + Chr(34) + "),$out);" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "  echo $out;" + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "} " + Chr(13) + Chr(10))
            System.IO.File.AppendAllText(phpcheckplace + "fgc.php", "?>" + Chr(13) + Chr(10))
        End If
        If System.IO.File.Exists(phpcheckplace + "fgc.php") = True Then
            anotherlayer = "http://127.0.0.1/fgc.php?inurl=" + inurl.Replace("?", "~!~")
        End If
    End Function

    Sub Main()
        Dim ourpath As String = Environment.GetEnvironmentVariable("PATH")
        Dim iplace As Integer = ourpath.ToLower().IndexOf("easyphp")
        Dim jplace As Integer
        Dim localrootdir As String = ""
        Dim localrootfile As String = ""
        If iplace > 0 Then
            jplace = iplace
            While (jplace < ourpath.Length And ourpath.Substring(jplace, 1) <> "\" And ourpath.Substring(jplace, 1) <> ";")
                localrootdir = localrootdir + ourpath.Substring(jplace, 1)
                jplace = jplace + 1
            End While
            jplace = iplace - 1
            While (jplace > 0 And ourpath.Substring(jplace, 1) <> ";")
                localrootdir = ourpath.Substring(jplace, 1) + localrootdir
                jplace = jplace - 1
            End While
            localrootdir = localrootdir + "\www\"
            Dim fplace As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
            Dim ourdi As DirectoryInfo = New DirectoryInfo(fplace + "\")
            Dim ourfls() As FileInfo = ourdi.GetFiles("*.url", SearchOption.AllDirectories)
            Dim ourfl As FileInfo
            Dim baseurl As String = ""
            Dim cnt As Integer = 0
            For Each ourfl In ourfls
                If localrootfile.Length = 0 Then
                    localrootfile = "MyFavourites.html"
                    System.IO.File.WriteAllText(localrootdir + localrootfile, "<html><head><title>Your Favourites</title><body><h1>Your Favourites</h1><select id='favourites'>")
                End If
                cnt = cnt + 1
                Try
                    ' Create new StreamReader instance with Using block.
                    Using reader As StreamReader = New StreamReader(ourfl.FullName)
                        ' Read one line from file
                        baseurl = "[DEFAULT]"
                        While baseurl.IndexOf("ASEURL=") <= 0
                            baseurl = reader.ReadLine
                        End While
                    End Using
                Catch ex As Exception
                    baseurl = "BASEURL=http://127.0.0.1/" + localrootfile
                End Try
                System.IO.File.AppendAllText(localrootdir + localrootfile, "<option id='f" + cnt.ToString() + "' onclick='window.open(" + Chr(34) + anotherlayer(baseurl.Replace("BASEURL=", ""), localrootdir) + Chr(34) + "," + Chr(34) + "_blank" + Chr(34) + ");' value='" + ourfl.Name.Replace(".url", "") + "'>" + ourfl.Name.Replace(".url", "") + "</option>")
            Next
            If localrootfile.Length > 0 Then
                System.IO.File.AppendAllText(localrootdir + localrootfile, "</select</body></html>")
                Try
                    Shell(localrootdir + localrootfile, AppWinStyle.NormalFocus, False, -1)
                Catch ex As Exception
                    Process.Start("IExplore.exe", "http://127.0.0.1/" + localrootfile)
                End Try
            End If
        End If
    End Sub

End Module
