user63898
user63898

Reputation: 30963

embed php in c++ application or any way can it be done?

i was looking for way to embed php script into c++ windows application . i found old facebook project that i dont know how much good it is or how to use it in windows app if any . is there any way to embed php in a Windows application?

Upvotes: 4

Views: 5676

Answers (1)

JSON
JSON

Reputation: 1835

PHP isn't an executable it is a lib. php-cli, php-cgi, mod_php etc are just interfaces for the same library. You can embed the lib into your own application as well (desktop apps, network tools, etc) but its a pain because of PHP's threading issues. This isn't just an issue when trying to thread PHP, but the lib will crash or can cause undefined behavior when other parts of your app try to thread around it. However, it can be done. I've done a few projects embedding PHP, and the only one that couldn't overcome the threading related issues was when trying to mate the PHP core within a JNI project.

Here's a blurb from a project where I embedded the PHP core into an SMTP server. It was able to access all of wordpress's codebase without any modifications (such as includes within wp-includes) allowing the AUTH command to authorize using wordpress. It would also deliver emails directly as blog entries based on email subject (if the user was logged on as admin via SMTP), and responses were added as comments to the post. This wasn't a hack or workaround, the SMTP service used the same codebase, configuration, and database as the HTTP service.

case 11: // AUTH
    zval **z_auth_ret_code;
    zend_hash_index_find(Z_ARRVAL_P(ret_array), 2, (void**)&z_auth_ret_code);

    if(Z_TYPE_PP(z_auth_ret_code) == IS_LONG) {
        if(Z_LVAL_PP(z_auth_ret_code) == 334) { // is OK
            // GET AUTH TYPE
            zval **z_auth_type_code;
            zend_hash_index_find(Z_ARRVAL_P(ret_array), 3, (void**)&z_auth_type_code);

            if(Z_LVAL_PP(z_auth_type_code) == 3) { // AUTH LOGIN
                send_to_socket(client_socket, "334 AUTH LOGIN ready. Please send UID\r\n");

                char *uid_input = NULL;
                int uid_input_len = 0;
                if(read_from_socket(client_socket, &uid_input, &uid_input_len, (char*)"\r\n") > 0) {
                    ZVAL_STRING(user_name, uid_input, 1);
                    send_to_socket(client_socket, "334 Please send password for UID\r\n");
                }

                char *pwd_input = NULL;
                int pwd_input_len = 0;
                if(read_from_socket(client_socket, &pwd_input, &pwd_input_len, (char*)"\r\n") > 0) {
                    ZVAL_STRING(user_pass, pwd_input, 1);
                }

                // setup session
                zval *function, *retval;
                zval *params[2];

                MAKE_STD_ZVAL(function);
                MAKE_STD_ZVAL(retval);

                ZVAL_STRING(function, "open_session", 1);

                params[0] = user_name;

                call_user_function(EG(function_table), NULL, function, retval, 1, params);

                // do auth
                ZVAL_STRING(function, "smtp_auth", 1);

                params[0] = user_name;
                params[1] = user_pass;

                call_user_function(EG(function_table), NULL, function, retval, 2, params TSRMLS_CC);

                if(Z_TYPE_P(retval) == IS_LONG && Z_LVAL_P(retval) == 1) {
                    send_to_socket(client_socket, "235 AUTH LOGIN SUCCESSFUL\r\n");
                    is_authorized = 1;
                }else {
                    send_to_socket(client_socket, "535 AUTH LOGIN FAILED\r\n");
                }
            }
        }
    }
break;

Upvotes: 6

Related Questions