Sunday, May 1, 2011

Receive A File Via PHP Web Service

I'm busy writing a PHP webservice and wish to receive a file as an array of byte streams (you can do something similar using C#) how can I do this in PHP? Using PHP 5 with NuSOAP.

From stackoverflow
  • Perhaps you only need to send an URI and the client grab the file... no?

    You can also encode the file with base64.

  • Hey,

    what about php streams ? I have worked with streams in C# before, not in php, but seems like it should work :)

    check out this presentation ( starting from slide 18 )

    good luck !

Problems using dynamic linked libraries (wxWidgets) from a DLL

We created a plugin; it is a DLL (Run-Time Dynamic Linking) which uses a 3rd party library (wxWidgets) and also links dynamically to that. The host software seems to scan our plugin, but exported functions are not called. We checked all dependencies with DependencyWalker.

We see in the debugger that the plugin is loaded, but the DllMain is not called, and the plugin is unloaded.

We tried loading our plugin from a simple test application using LoadLibrary and GetProcAddress which recognized and called the exported functions.

Having wxWidgets linked statically worked fine, though.

Does anyone have an idea why the exported function, respectively DllMain are not called, or can point out a tool which is capable to monitor the whole DLL loading process?

From stackoverflow
  • If wxWidgets is loaded already into the process address space before your plugin is loaded (the host app could do that, or there might be another plugin linking to wxWidgets which is loaded before yours), then there might be a chance that it is another version, missing some of the entry points that your plugin needs. Running the host app under DependencyWalker or WinDbg should show you which wxWidgets DLL is loaded, and you could try to load your plugin from your test app using exactly the same wxWidgets DLL. That should reveal whether there are missing dependencies.

  • Perhaps the host software does some funky things when loading the plugin and doesn't like wxWindows.

    Anyways, try using the ProcessExplorer from the SysInternals suite to check what the process is doing.

  • there's something usefull.... maybe that can help you....

    http://taringa.net/posts/ebooks-tutoriales/6888465/wxWidgets-y-DLL-de-recursos.html

How might a site like Stack Overflow pass user information around in ASP.NET MVC?

Basically, I log into my website using OpenId, very similar to what I am assuming SO does. When I get the information back, I throw it into a database and create my "Registered User". I set my AuthCookie:

FormsAuthentication.SetAuthCookie(user.Profile.MyProfile.DisplayName, false);

Then I can use this for the User Name. However, I would like to pass in the entire object instead of just the string for display name. So my question is:

How does SO do it?

Do they extend/override the SetAuthCookie(string, bool) method to accept the User object, i.e. SetAuthCookie(User(object), bool).

What is the best way to persist a User object so that it is available to my UserControl on every single page of my Web Application?

Thanks in advance!

From stackoverflow
  • You can achieve this behavior by implementing your custom Membership Provider, or extending an existing one. The provider stores user information based on a key (or just by user name) and provides access to the MembershipUser class, which you can extend however you wish. So when you call FormsAuthentication.SetAuthCookie(...), you basically set the user key, which can be accessed be the provider.

    When you call Membership.GetUser(), the membership infrastructure will invoke the underlying provider and call its GetUser(...) method providing it with a key of the current user. Thus you will receive the current user object.

  • One way is to inject into your controller a class that is responsible for retrieving information for the current logged in user. Here is how I did it. I created a class called WebUserSession which implements an interface called IUserSession. Then I just use dependency injection to inject it into the controller when the controller instance is created. I implemented a method on my interface called, GetCurrentUser which will return a User object that I can then use in my actions if needed, by passing it to the view.

    using System.Security.Principal;
    using System.Web;
    
    public interface IUserSession
    {
        User GetCurrentUser();
    }
    
    public class WebUserSession : IUserSession
    {
        public User GetCurrentUser()
        {
            IIdentity identity = HttpContext.Current.User.Identity;
            if (!identity.IsAuthenticated)
            {
                return null;
            }
    
            User currentUser = // logic to grab user by identity.Name;
            return currentUser;
        }
    }
    
    public class SomeController : Controller
    {
        private readonly IUserSession _userSession;
    
        public SomeController(IUserSession userSession)
        {
            _userSession = userSession;
        }
    
        public ActionResult Index()
        {
            User user = _userSession.GetCurrentUser();
            return View(user);
        }
    }
    

    As you can see, you will now have access to retrieve the user if needed. Of course you can change the GetCurrentUser method to first look into the session or some other means if you want to, so you're not going to the database all the time.

  • Jeff,

    As I said in a comment to your question above, you must use the ClaimedIdentifier for the username -- that is, the first parameter to SetAuthCookie. There is a huge security reason for this. Feel free to start a thread on dotnetopenid@googlegroups.com if you'd like to understand more about the reasons.

    Now regarding your question about an entire user object... if you wanted to send that down as a cookie, you'd have to serialize your user object as a string, then you'd HAVE TO sign it in some way to protect against user tampering. You might also want to encrypt it. Blah blah, it's a lot of work, and you'd end up with a large cookie going back and forth with every web request which you don't want.

    What I do on my apps to solve the problem you state is add a static property to my Global.asax.cs file called CurrentUser. Like this:

    public static User CurrentUser {
        get {
            User user = HttpContext.Current.Items["CurrentUser"] as User;
            if (user == null && HttpContext.Current.User.Identity.IsAuthenticated) {
                user = Database.LookupUserByClaimedIdentifier(HttpContext.Current.User.Identity.Name);
                HttpContext.Current.Items["CurrentUser"] = user;
            }
            return user;
        }
    }
    

    Notice I cache the result in the HttpContext.Current.Items dictionary, which is specific to a single HTTP request, and keeps the user fetch down to a single hit -- and only fetches it the first time if a page actually wants the CurrentUser information.

    So a page can easily get current logged in user information like this:

    User user = Global.CurrentUser;
    if (user != null) { // unnecessary check if this is a page that users must be authenticated to access
        int age = user.Age; // whatever you need here
    }
    
    Jeff Ancel : I am going to look into this. I spent 8 hours yesterday making my own MembershipProvider. In a few hours I will update. This way is much easier than the way I did it yesterday. I also like what I see here.
    Jeff Ancel : I think that this is what I am going to go with. It plugs right in to what I have and only requires the one call like you mention. I very well could do this without the custom provider at all using the method you described. Thanks.
    Andrew Arnott : Glad it will work for you. I find that the asp.net membership provider is a very poor fit for redirect-based, passwordless authentication protocols such as OpenID. Any membership provider that works with OpenID can only be implemented half-way and hokey at best due to the interface that just doesn't fit very well.

Can you run Target Designer for NT 4.0 on an XP box?

I'm trying to run Microsoft Windows NT Embedded 4.0 Target Designer on a Windows XP system, but when I try to build, I get errors. Have you ever done this, and if so, is there a trick to getting it to work?

The best clue I've found so far from Microsoft is that the Development System Hardware Requirements document includes "Windows NT 4.0 Service Pack 4 or later," but I don't know if XP is considered a valid "or later" for NT 4.0.

When I try to build an image by running Target Designer in Windows NT 4.0 or Windows 2000 compatibility mode, I get this error during Building Registry / Adding component parameters:

ERROR: Couldn't get security error = 5

When I run it normally under XP, the error that comes up during Building Registry / Binding Network is:

ERROR: Failed to bind network - Incorrect function
From stackoverflow
  • You have two options (which you may have already tried);

    1. Right click on the link to the Target Designer executable and select properties, then select the Compatibility tab and set 'Run this program in compatibility mode for:' and choose 'Windows NT (SP5)' from the dropdown. You should be able to run TD then.
    2. If that does not work, download Virtual PC from Microsoft (free) and create a new Virtual Machine for Windows NT (you can get the ISO images for NT from MSDN but you need a valid account, otherwise use your CD Roms for NT).

    I hope that helps.

    Ryan

ColdFusion Query to java.sql.ResultSet

I've looked in the "undocumentation", and I can see how to create a coldfusion.sql.QueryTable from a ResultSet, but not the other way around. So, how can I extract the java.sql.ResultSet from a ColdFusion ( coldfusion.sql.QueryTable ) query object?

From stackoverflow
  • Turns out all I had to do was pass in my coldfusion.sql.QueryTable object...don't know why it works, unless ColdFusion is doing some sort of magic casting under the hood.

  • coldfusion.sql.QueryTable implements javax.sql.RowSet, which extends java.sql.ResultSet

    Thus, as you discovered, you don't need to do anything. A ColdFusion query is already a Java ResultSet.

Indented hierarchical table structure

I am trying to create a hierarchical display of nested tables, where each sub level is indented further from the parent. I'm open to using table or div. The closest I've come is below. It looks mostly correct in IE (except that the borders on the right are mashed together). In Chrome the sub item border is extending beyond the parent on the right.

I'm open to using divs as well.

<html> 
<head> 
<style type="text/css"> 
.ItemTable
{
    width: 100%;
    margin-left: 20px;
    border: solid 1px #dbdce3;
}
</style> 
</head> 
<body> 
    <table class="ItemTable">
     <tr>
      <td>Item 1</td>
     </tr>
     <tr>
      <td>
       <table class="ItemTable">
        <tr>
         <td>Item  1A</td>
        </tr>
      </td>
     </tr>
    </table>
</body> 
</html>
From stackoverflow
  • changing

    margin-left: 20px;
    

    to

    padding-left: 20px;
    

    works for me on IE7, firefox and chrome (although with chrome I had to un-maximise the window then remaximise it - looks like a rendering bug to me)

  • Looks like a couple of things. Your sub table was missing it's close tag and i added padding to the TD to help with the indent:

    <style type="text/css">
        .ItemTable
        {
            width: 100%;
    
            border: solid 1px #dbdce3;
        }
        .ItemTable td
        {
            width: auto;
            padding-left: 20px;
            border: solid 1px #dbdce3;
        }
    </style>
    
    
    <table class="ItemTable">
        <tr>
            <td>
                Item 1
            </td>
        </tr>
        <tr>
            <td>
                <table class="ItemTable">
                    <tr>
                        <td>
                            Item 1A
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    

    Tested it in Chrome, FF, IE6, IE7 and Safari and it looks like it works.

  • Do you plan on displaying tabular data? If not you would be better just using div's for this and just applying a margin to the child element like shown below

    <style>
        #container {border:1px solid #999}
        .indent {margin-left:50px; border:1px solid #999;}
        .item {background:#99cc00;}
    </style>
    
    <div id="container">
        <span class="item">This is item 1</span>
    
        <div class="indent">
            <span class="item">This is item 2</span>
    
            <div class="indent">
                <span class="item">This is item 3</span>
            </div>
    
        </div>
    
    </div>
    

    Of course it really depends on what you are trying to display.

Can you Distribute a Ruby on Rails Application without Source?

I'm wondering if it's possible to distribute a RoR app for production use without source code? I've seen this post on SO, but my situation is a little different. This would be an app administered by people with some clue, so I'm cool with still requiring an Apache/Mongrel/MySQL setup on the customer end. All I really want is for the source to be protected. Encoding seems a popular way to go for distributing PHP apps (eg: Helpspot).

I've found these potential solutions:

  • Zenobfuscate - not all types of Ruby code is supported however, so that counts that out
  • Ruby Encoder - may be the best option, as their PHP encoder looks alright (I haven't tried it however) but it's not available yet. I've used IONcube for PHP before and it worked well, but it doesn't seem that IONcube is interested yet.
  • Slingshot - it was mentioned in the other SO post, but it solves a different problem to mine and the source is still visible.
  • RubyScript2Exe - from the doco, it's not production ready, so that counts that out.

I've heard that potentially using JRuby and distributing bytecode might be a way to achieve this, but I've never used JRuby so I'm not sure what's involved.

Can anyone offer any ideas and/or known examples? Ideally I'd love to have some kind of automated build scenario as well.

From stackoverflow
  • You can, but it wouldn't do anything to prevent somebody from reverse-engineering or modifying it. I remember there was an article about similar attempts to obfusticate Perl and how they could be effectively bypassed by a debugger and 5 minutes of effort.

    Unkwntech : I don't think obfusticating perl is needed perl is obfusticated enough by itself...
    Dan Harper - Leopard CRM : Whatever solution it is, it's not as secure as not giving anyone the app. I'm just trying to figure out how to stick a hurdle as large as possible in front of anyone trying to get to the source. There is a small element of trust there as the customers have paid to use the software.
  • If you can't wait for the delivery of RubyEncoder, then I think ZenObfuscate is the most promising. Though it may require some modifications to your source code, they do say this on their site:

    ZenObfuscate costs $2500 for a site license or is individually negotiable for other licensing schemes. Yes, that is expensive. That was on purpose. But don't let that thwart you too much. If your product is really cool and we want to see it succeed, we'll make it work. "Really cool" is not freecell.

    Of course, for $2500 (or more), you'd hope to get a few tweaks to the compiler that'd make your codebase fully supported. It might be worth engaging them in the conversation.

    Dan Harper - Leopard CRM : I didn't notice that, thanks Pete, maybe it is worth talking to them about it.
  • I'd spend less time and money on hiding/protecting your code & more time/money on getting customers.Customers beat code all day long.

    George Jempty : -1 for not answering the question
  • Take a look at JumpBox.

    I've had conversations with them on the topic, and they seem to have a solution that will work soon for Rails apps.

  • If you release the source, obfuscated or otherwise, your app will be pirated. See, for example, Mint. It depends on what you're building, but you may find that you're better off releasing the app as a hybrid of sorts: A hosted app with a well-defined API, and a component that runs on the customer's server. As long as the true value of your product lives on the server side, you don't need to obfuscate your code, and you can just release the source code unmodified. Additionally, this may also give you the opportunity to reach clients running, say, PHP rather than Ruby. See, for example, Google Analytics, HopToad, Scout, etc, etc.

    guns : ++ This is the new open/closed source world of the web application. It actually represents a really great scenario for developers - your application's value is in your service and execution, not necessarily in the code itself. You can enjoy both the freedom of open code and the benefits of limited supply.
    Dan Harper - Leopard CRM : -1 for not answering the question. This is not a question of architecture design, business models, or whether the app will be pirated or not.
    Toby Hede : I think that given there is no actual solution for this problem, and it is generally an inherently flawed approach, offering alternatives is quite reasonable.
  • You can also take a look at Mingle from ThoughtWorks studios as an example of using JRuby for this. It's a Ruby on Rails app, they run it using JRuby. They've customized jruby to load encrypted .rb files.

  • Your best option right now is to use JRuby. A little bit of background: My company (BitRock) works with many proprietary and commercial open source vendors. We help them package their server software, which is typically based on PHP, Java or Ruby together with a web server or application server (Apache, Tomcat), the language runtime and a database (typically Postgres, MySQL) into a self-contained, easy to use installer. We have a large number of PHP-based customers (including HelpSpot, which you mention) but also several Rails-based ones. In the case of the RoR customers the norm is to use JRuby together with Tomcat or Glassfish although in some cases we also bundle a native Ruby interpreter to run specific scripts that rely on libraries not yet ported to JRuby (usually not core to the application). JRuby has matured quickly and in many cases it actually runs their code faster than regular Ruby. You will need to also consider that although porting your code to JRuby is fairly straightforward, you will need to invest some time on that. You may want to check JRuby Stack which is a free installer of everything you need to get started. Good luck!

  • I'm wondering if you could just "compile" the ruby code into an executable using something like RubyScript2Exe ?

    To be honest I haven't used it but it seems like it could be what you want, even if it just packages up the scripts with the interpreter into a single executable.