Archive

Archive for May, 2010

.NET PHP Interop – Evaluate C# from PHP

May 21, 2010 Leave a comment
Download: http://sites.google.com/site/appanalyzecomponent/phptodotnet

This code allows you to evaluate C# code in PHP.

Download the code above, import the DLL into the GAC, and create a PHP file as below:

<?php

//create an instance of the  ADO connection object
 $phpdotnet = new COM ("PhpDotNet.PhpDotNet")
  or die("Cannot start PhpDotNet.PhpDotNet");

 echo $phpdotnet->Info();
 echo ‘<br> ‘;
 echo ‘Windows folder is located at: ‘;
 echo $phpdotnet->Eval(‘Environment.GetEnvironmentVariable("SystemRoot")’);

?>

— See that Environment.GetEnvironmentVariable("SystemRoot") is C# code, not PHP.

using System.CodeDom.Compiler;
using System.Runtime.InteropServices;
using System.Text;

namespace PhpDotNet
{
    /// <summary>
    /// COM Interface
    /// </summary>
    [Guid("D5FA3365-D97F-49a9-A970-CDD5E0A3609F")]
    public interface IPhpDotNet
    {
        [DispId(1)]
        string Info();

        [DispId(2)]
        object Eval(string sCSCode);
    }
   
    /// <summary>
    /// COM Events interface
    /// </summary>
    [Guid("13F93C05-C751-4691-8888-BD72F13DCF04"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IPhpDotNetEvents
    {
    }

    /// <summary>
    /// Implemintation
    /// </summary>
    public class PhpDotNet : IPhpDotNet
    {

        public string Info()
        {
            return "Php.NET is now working";
        }

        /// <summary>
        /// Evaluate custom c# Code.
        /// http://www.codeproject.com/KB/cs/evalcscode.aspx
        /// </summary>
        /// <param name="sCSCode"></param>
        /// <returns></returns>
        public object Eval(string sCSCode)
        {
            var provider = CodeDomProvider.CreateProvider("CSharp");
            var cp = new CompilerParameters();
            cp.ReferencedAssemblies.Add("system.dll");
            cp.ReferencedAssemblies.Add("system.xml.dll");
            cp.ReferencedAssemblies.Add("system.data.dll");
            cp.ReferencedAssemblies.Add("system.windows.forms.dll");
            cp.ReferencedAssemblies.Add("system.drawing.dll");
            cp.CompilerOptions = "/t:library";
            cp.GenerateInMemory = true;
            var sb = new StringBuilder("");
            sb.Append("using System;n");
            sb.Append("using System.Xml;n");
            sb.Append("using System.Data;n");
            sb.Append("using System.Data.SqlClient;n");
            sb.Append("using System.Windows.Forms;n");
            sb.Append("using System.Drawing;n");
            sb.Append("namespace CSCodeEvaler{ n");
            sb.Append("public class CSCodeEvaler{ n");
            sb.Append("public object EvalCode(){n");
            sb.Append("return " + sCSCode + "; n");
            sb.Append("} n");
            sb.Append("} n");
            sb.Append("}n");
            var cr = provider.CompileAssemblyFromSource(cp, sb.ToString());
            if (cr.Errors.Count > 0)
            {
                return cr.Errors[0].ErrorText;
            }
            var a = cr.CompiledAssembly;
            var o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");
            var t = o.GetType();
            var mi = t.GetMethod("EvalCode");
            var s = mi.Invoke(o, null);
            return s;
        }
    }
}

Categories: Uncategorized

Read Web.Config from PHP

May 21, 2010 1 comment
Here is some code to read a setting from an ASP.NET web.config file using PHP

<?php
try
{
    function GetSetting($key)
    {
     $sxe = simplexml_load_file("C:Inetpubwwwrootweb.config");
     foreach($sxe->xpath(‘/configuration/appSettings/add’) as $item) {
        if ($item[‘key’] ==$key) return $item[‘value’];
     }
    }
    echo GetSetting(‘sqlclient’);
}
catch(Exception $e)
{
  echo ‘Message: ‘ .$e->getMessage();
}
?>

Categories: Uncategorized

Site down for a month!

May 15, 2010 Leave a comment
My website, http://www.webtropy.com  was down for a month!, I couldn’t believe it… Accidentally moved the root folder of the website when in over remote desktop.

I spotted a huge drop in traffic -64%, but I thought this was just google changing it’s mind about the quality of the site!

grrr. such an idiot.

Categories: Uncategorized