Monday, May 21, 2012

ssl on symfony

copy bellow file in symfony apps/lib forder

filename: sslFilter.class.php

<?php
/**
 * Description of sslFilter
 */
class sslFilter extends sfFilter
{
  /**
   * Execute filter
   *
   * @param FilterChain $filterChain The symfony filter chain
   */
  public function execute ($filterChain)
  {
    // Only execute this filter once
    if ($this->isFirstCall()) {
      // Array of modules/actions that require move to SSL
      $ssl_actions = sfConfig::get('app_ssl_secure_actions');

      if (empty($_SERVER['HTTPS']) && count($_POST) < 1) {

        // We're not using SSL and not POSTing data - check if we should be using SSL
        foreach ($ssl_actions as $action) {
          if ($this->getContext()->getModuleName() == $action['module'] && $this->getContext()->getActionName() == $action['action']) {
            $new_url = sprintf('https://%s%s', sfConfig::get('app_ssl_secure_host'), $_SERVER['REQUEST_URI']);
            header('Location: ' . $new_url);
            exit;
          }
        }

      } elseif (!empty($_SERVER['HTTPS']) && count($_POST) < 1) {

        // We're using SSL and not posting data
        $dont_redirect = false;
        foreach ($ssl_actions as $action) {
          if ($this->getContext()->getModuleName() == $action['module'] && $this->getContext()->getActionName() == $action['action']) {
            $dont_redirect = true;
          }
        }
        if ($dont_redirect == false) {
          // Redirect
          $new_url = sprintf('http://%s%s', sfConfig::get('app_ssl_insecure_host'), $_SERVER['REQUEST_URI']);
          header('Location: ' . $new_url);
          exit;
        }
      }
    }
    // Next filter
    $filterChain->execute();
  }
}
?>

in filter.yml file

rendering: ~
web_debug: ~
security:  ~

# generally, you will want to insert your own filters here
sslFilter:
  class:  sslFilter

cache:     ~
common:    ~
flash:     ~
execution: ~

app.yml file
# default values
all:
  ssl:
    insecure_host:    www.myhostname.com
    secure_host:      www.myhostname.com
    secure_actions:
      - { module: securemodule, action: secureaction }
      - { module: sslmodulename, action: sslactionname }

No comments:

Post a Comment

how to see xml preview in browser php

xml preview view in browser $xml = new DOMDocument('1.0', 'UTF-8');                 $xml->preserveWhiteSpace = false; ...