1

I wrote a plugin which fires an apple push notification to IOS devices.

When I fire it manually via the root directory, it works perfectly. The catch, when I move it to plugin folder and run it as a plugin using:

add_action( 'publish_post', 'post_published' ,10,2);

it returns a ssl error from apple servers.

Nothing was changed in the code (except for include ('wp-config.php')) which is not needed as a plugin.

This is the error:

Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /nas/wp/www/staging/geektimecoil/wp-content/plugins/wp-mes-push-to-apple/wp-mes-push-to-apple.php on line 131

Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /nas/wp/www/staging/geektimecoil/wp-content/plugins/wp-mes-push-to-apple/wp-mes-push-to-apple.php on line 131

Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /nas/wp/www/staging/geektimecoil/wp-content/plugins/wp-mes-push-to-apple/wp-mes-push-to-apple.php on line 131 Error: 0

This is the code as it is in the plugin:

function post_published($ID, $post ) {

    // $url = "http://geektimecoil.staging.wpengine.com/googlePush.php"; global $wpdb; 
    // $post = get_post(211051);

    $categories = get_the_category($post->ID );
    if($categories){
        $cond = array();
        $cats = array();
        foreach($categories as $category) {     
            $cats[] = $category->slug;
            switch ($category->slug) {
                case "startup":
                    $cond[] = "startup";
                    break;
                case "development":
                    $cond[] = "dev";
                    break;
                case "gadgets":
                    $cond[] = "mobile";
                    break;              
                case "internet":
                    $cond[] = "internet";
                    break;
                case "hi-tech":
                    $cond[] = "hightech";
                    break;
                case "%d7%a1%d7%a7%d7%99%d7%a8%d7%95%d7%aa": // hebrew skirot
                    $cond[] = "reviews";
                    break;
                case "science":
                    $cond[] = "science";
                    break;
            }
        }
        print_r($cond);

        $condition = "( (".$cond[0]."=1)";
        for ($i=1; $i<=(count($cond)-1) ; $i++) { 
            $condition .= "or(".$cond[$i]."=1)";
        }
        $condition .= " )";
    }

    $sql="SELECT token FROM wp_mobile where device=1 AND ".$condition;
    $users = $wpdb->get_results($sql);

    print_r($sql);
    print_r($users);

        $passphrase = '***';

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
        stream_context_set_option($ctx, 'ssl', 'local_cert','/push/geek_dev.pem');
        // stream_context_set_option($ctx, 'ssl', 'local_cert','/push/geek_prod.pem');
        stream_context_set_option($ctx, 'ssl', 'cafile', '/push/ios_entrust.pem');

        # Open a connection to the APNS server
        $fp = stream_socket_client(//'ssl://gateway.push.apple.com:2195', $err,
                                   'ssl://gateway.sandbox.push.apple.com:2195', $err,
                                   $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp){
            echo "Error: ".$err;
            exit;
        }


        $post_url = str_replace('www','m',get_permalink( $post->ID));
        $body["aps"] = array(
                             "alert" => "message", //title
                             "sound" => "default",
                             "badge" => "0",
                             "url" => $post_url,
                             );

        $payload = json_encode($body);

        $msg = chr(1)                           // command (1 byte)
        . pack('N', 'notification')             // identifier (4 bytes)
        . pack('N', time() + 86400)             // expire after 1 day (4 bytes)
        . pack('n', 32)                         // token length (2 bytes)
        . pack('H*', 'db8***b414004ccb9a***2a54d9de06')   
        . pack('n', strlen($payload))           // payload length (2 bytes)
        . $payload;                             // the JSON payload

        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));
        //echo "\n$result";

        //set blocking
        stream_set_blocking($fp,0);

        //Wait to response
        sleep(1);

        fclose($fp);

        return $print;
    }


add_action( 'publish_post', 'post_published' ,10,2); ?>

As I mentioned before, without the add_action and not as plugin, this exact same code is working.

  • So to be clear the code does work on same server and account, just not in WP context? – Rarst Aug 18 '14 at 16:54
  • @Rarst, correct. – Meschiany Aug 19 '14 at 5:34
1

OMG, finally, as the code runs from the plugin, the current working directory is set to wp-admin whereas when i run it manually it set for /wp-content/plugins...

So, when the push function is called, just add the following line of code:

chdir(ABSPATH . 'wp-content/plugins/*your plugins folder name*/');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.