/** * Start redirecting PHP errors * @param int $level PHP Error level to catch (Default = E_ALL & ~E_DEPRECATED) */ static function Start($level = null) { if ($level == null) { if (defined("E_DEPRECATED")) { $level = E_ALL & ~E_DEPRECATED; } else { // php 5.2 and earlier don't support E_DEPRECATED $level = E_ALL; self::$IGNORE_DEPRECATED = true; } } set_error_handler(array("ExceptionThrower", "HandleError"), $level); }
/** * Send a push notification * @param string $deviceToken the push token for the mobile device * @param string $message the message to display * @param string $alertSound the audio file to play, otherwise use the default sound * @param string $unlockText if the device is locked, show "Slide to XXX" where XXX is the unlockText * @param int $badgeCount the number that should be shown on the springboard badge */ public function Send($deviceToken, $message, $alertSound = 'default', $unlockText = '', $badgeCount = 0) { $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $this->certFilePath); stream_context_set_option($ctx, 'ssl', 'passphrase', $this->certPassphrase); $output = new stdClass(); $output->date = date('Y-m-d H:i:s'); // Open a connection to the APNS server $fp = stream_socket_client($this->gatewayUrl, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); stream_set_blocking($fp, 0); //This allows fread() to return right away when there are no errors. But it can also miss errors during last seconds of sending, as there is a delay before error is returned. Workaround is to pause briefly AFTER sending last notification, and then do one more fread() to see if anything else is there. if (!$fp) { $output->success = false; $output->message = "Connection Failed: {$err} {$errstr}"; } else { $apple_expiry = time() + 10 * 24 * 60 * 60; //Keep push alive (waiting for delivery) for 10 days // format the body based on whether an unlock message was specified $alert = $unlockText ? array('body' => $message, 'action-loc-key' => $unlockText) : $message; // Create the payload body $body['aps'] = array('alert' => $alert, 'sound' => $alertSound, 'badge' => $badgeCount); $apple_identifier = 1; // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification ExceptionThrower::$IGNORE_DEPRECATED = true; ExceptionThrower::Start(); try { //$msg = pack("C", 1) . pack("N", $apple_identifier) . pack("N", $apple_expiry) . pack("n", 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload; //Enhanced Notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); usleep(500000); $apple_error_response = fread($fp, 6); if (!$result) { $output->success = false; $output->message = 'Communication Error: Unable to deliver message'; } else { $output->success = true; $output->message = print_r($result, 1) . ' ::: ' . print_r($apple_error_response, 1); } } catch (Exception $ex) { $output->success = false; $output->message = $ex->getMessage(); } ExceptionThrower::Stop(); } // Close the connection to the server fclose($fp); return $output; }
/** * Commence la redirection des erreurs * @param int $level PHP niveau d'Erreur à attraper (Default = E_ALL & ~E_DEPRECATED) */ static function Start($level = null) { if ($level == null) { if (defined("E_DEPRECATED")) { $level = E_ALL & ~E_DEPRECATED; } else { // php 5.2 et les versions antérieur ne support pas E_DEPRECATED $level = E_ALL; self::$IGNORE_DEPRECATED = true; } } //Modification du handler // il appelera la class lorqu'il attrapera une erreur set_error_handler(array("ExceptionThrower", "HandleError"), $level); }
/** * Send a push notification directly using a socket * @param string $deviceToken the push token for the mobile device * @param string $message the message to display * @param string $alertSound the audio file to play, otherwise use the default sound * @param string $unlockText if the device is locked, show "Slide to XXX" where XXX is the unlockText * @param int $badgeCount the number that should be shown on the springboard badge */ public function SendWithSocket($deviceToken, $message, $alertSound = 'default', $unlockText = '', $badgeCount = 0) { $gatewayUrl = $this->sandboxMode ? "ssl://gateway.sandbox.push.apple.com:2195" : "ssl://gateway.push.apple.com:2195"; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $this->certFilePath); stream_context_set_option($ctx, 'ssl', 'passphrase', $this->certPassphrase); $output = new stdClass(); $output->date = date('Y-m-d H:i:s'); $fh = null; $errorMesssage = NULL; try { // Open a connection to the APNS server $fh = stream_socket_client($gatewayUrl, $err, $errorMesssage, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); } catch (Exception $ex) { $errorMesssage = $ex->getMessage(); } if ($errorMesssage || !$fh) { $output->success = false; $output->message = "Connection Failed: {$errorMesssage}"; } else { $appleExpiry = time() + 10 * 24 * 60 * 60; //Keep push alive (waiting for delivery) for 10 days // format the body based on whether an unlock message was specified $alert = $unlockText ? array('body' => $message, 'action-loc-key' => $unlockText) : $message; // Create the payload body $body['aps'] = array('alert' => $alert, 'sound' => $alertSound, 'badge' => (int) $badgeCount); // Encode the payload as JSON $payload = json_encode($body); // Build the binary notification ExceptionThrower::$IGNORE_DEPRECATED = true; ExceptionThrower::Start(); try { // @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; fwrite($fh, $msg, strlen($msg)); $response = $this->getResponse($fh); if (!$response) { // everything is cool $output->success = true; $output->message = 'Message sent'; } else { $output->success = false; $output->message = 'Push notification failed with response: ' . $response; } } catch (Exception $ex) { $output->success = false; $output->message = $ex->getMessage(); } ExceptionThrower::Stop(); } // Close the connection to the server if ($fh) { @fclose($fh); } return $output; }
/** * Processes user input and executes the specified controller method, ensuring * that the controller dependencies are all injected properly * * @param Phreezer $phreezer Object persistance engine * @param IRenderEngine $renderEngine rendering engine * @param string (optional) $action the user requested action (if not provided will use router->GetRoute()) * @param Context (optional) a context object for persisting state * @param IRouter (optional) router object for reading/writing URLs (if not provided, GenericRouter will be used) */ static function Dispatch($phreezer, $renderEngine, $action = '', $context = null, $router = null) { if ($router == null) { require_once 'GenericRouter.php'; $router = new GenericRouter(); } // get the route and normalize the controller name list($controller_param, $method_param) = $router->GetRoute($action); $controller_class = $controller_param . "Controller"; if (self::$FAST_LOOKUP) { if (!class_exists($controller_class)) { $controller_file = "Controller/{$controller_class}.php"; include_once $controller_file; } $controller = new $controller_class($phreezer, $renderEngine, $context, $router); $controller->{$method_param}(); return true; } // if the controller was in a sub-directory, get rid of the directory path $slashPos = strpos($controller_class, '/'); while ($slashPos !== false) { $controller_class = substr($controller_class, $slashPos + 1); $slashPos = strpos($controller_class, '/'); } if (!class_exists($controller_class)) { // attempt to locate the controller file $controller_file = "Controller/" . $controller_param . "Controller.php"; $controller_filepath = null; // search for the controller file in the default locations, then the include path $paths = array_merge(array('./libs/', './'), explode(PATH_SEPARATOR, get_include_path())); $found = false; foreach ($paths as $path) { $controller_filepath = self::ControllerFileExists($path . "/" . $controller_file); if ($controller_filepath) { $found = true; break; } } if (!$found) { throw new Exception("File ~/libs/" . $controller_file . " was not found in include path"); } // convert any php errors into an exception if (self::$IGNORE_DEPRECATED) { ExceptionThrower::Start(); } else { ExceptionThrower::Start(E_ALL); ExceptionThrower::$IGNORE_DEPRECATED = false; } // we should be fairly certain the file exists at this point include_once $controller_filepath; // we found the file but the expected class doesn't appear to be defined if (!class_exists($controller_class)) { throw new Exception("Controller file was found, but class '" . $controller_class . "' is not defined"); } } // create an instance of the controller class $controller = new $controller_class($phreezer, $renderEngine, $context, $router); // we have a valid instance, just verify there is a matching method if (!is_callable(array($controller, $method_param))) { throw new Exception("'" . $controller_class . "." . $method_param . "' is not a valid action"); } // do not call the requested method/route if the controller request has been cancelled if (!$controller->IsTerminated()) { // file, class and method all are ok, go ahead and call it call_user_func(array(&$controller, $method_param)); } // reset error handling back to whatever it was //restore_exception_handler(); ExceptionThrower::Stop(); return true; }