Uploaded image for project: 'eZ Publish / Platform'
  1. eZ Publish / Platform
  2. EZP-18423

eZSys::hostname() and eZSys::serverPort() should work in shell mode

    XMLWordPrintable

Details

    Description

      In a php script launched in a shell bash (php my-test-script.php for example), I'd like to print an absolute url of an image.

      So I do something like:

      $tpl = eZTemplate::factory();
      $uri = eZURLOperator::eZImage($tpl, "icons/foo.png", "ezimage");
      eZURI::transformURI($uri, false, 'full');
      echo $uri . "\n";
      

      If I use this code in web env, no problem, but if I launch this piece of code from a shell, I can't get the absolute path, I get the 2 erorrs:
      Server variable 'HTTP_HOST' does not exist
      Server variable 'SERVER_PORT' does not exist

      I've looked at cjw-newsletter that needs to print absolute url in the email (the job is done in bin/php/createoutput.php) and they have also no other way than coding several crappy regex, a bunch of preg_replace like:

      $htmlPage = preg_replace("/src=\"\/design/",    'src="'.$hostUrlRoot.'/design',     $htmlPage ); 
      

      (so imagine if the content of a message in the newsletter contains "hello everyone, try this test code: src="/design/foo" in the <my-super-tag> tag", it will be replaced...)

      => The perfect solution would be for those 2 functions eZSys::serverPort() and eZSys::hostname() to work on shell env, no ?

      Here are current functions:

           static function hostname()
           {
               $forwardedHostsString = self::serverVariable( 'HTTP_X_FORWARDED_HOST', true );
               if ( $forwardedHostsString !== null )
               {
                   $forwardedHosts = explode( ',', $forwardedHostsString );
                   return $forwardedHosts[0];
               }
       
               return self::serverVariable( 'HTTP_HOST' );
           }
      
           static function serverPort()
           {
               if ( empty( $GLOBALS['eZSysServerPort'] ) )
               {
                   $hostname = self::hostname();
                   if ( preg_match( "/.*:([0-9]+)/", $hostname, $regs ) )
                   {
                       $port = $regs[1];
                   }
                   else
                   {
                       $port = self::serverVariable( 'SERVER_PORT' );
                   }
                   $GLOBALS['eZSysServerPort'] = $port;
               }
               return $GLOBALS['eZSysServerPort'];
           }
      

      Why not changing a little to have this (something like, to show the idea):

           static function hostname()
           {
              // new chance is given to try to find the hostname, as the rest of code won't work in shell env
              if (eZSys::isShellExecution() && !eZSys::serverVariable('HTTP_HOST', true))
              {
                  $ini = eZINI::instance();
                  // should be better to have a ServerName in site.ini to define by default, as SiteUrl may contains more than a hostname (my case in my site.ini for example)
                  return $ini->variable('SiteSettings', 'SiteURL');
              }
      
               $forwardedHostsString = self::serverVariable( 'HTTP_X_FORWARDED_HOST', true );
               if ( $forwardedHostsString !== null )
               {
                   $forwardedHosts = explode( ',', $forwardedHostsString );
                   return $forwardedHosts[0];
               }
       
               return self::serverVariable( 'HTTP_HOST' );
           }
      
           static function serverPort()
           {
               if ( empty( $GLOBALS['eZSysServerPort'] ) )
               {
                   $hostname = self::hostname();
                   if ( preg_match( "/.*:([0-9]+)/", $hostname, $regs ) )
                   {
                       $port = $regs[1];
                   }
                   // new chance is given to try to find the port
                   elseif (eZSys::isShellExecution() && !eZSys::serverVariable('SERVER_PORT', true))
                   {
                       $port = 80;
                       // hardcoded as I don't see the definition of the port somewhere in the ini file
                       // Maybe some ServerPort should be defined in site.ini (besides ServerName), ServerPort is defined in shop.ini for example already => why not putting it in site.ini to make it available everywhere ? shop is being deprecated...
                      // and then use
                      //$ini = eZINI::instance();
                      //$port = $ini->variable('SiteSettings', 'ServerPort');
                   }
                   else
                   {
                       $port = self::serverVariable( 'SERVER_PORT' );
                   }
                   $GLOBALS['eZSysServerPort'] = $port;
               }
               return $GLOBALS['eZSysServerPort'];
           }
      

      Attachments

        Activity

          People

            dp@ez.no dp@ez.no
            enzo enzo
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: