Symfony and Ioncube

This article has moved to this location.


In symfony, encoded classes excluded from config_autoload_yml.php. Because when symfony found the classes and read the contents, symfony can not find the words that contain abstract class|interface, final class|interface, class (preg_match_all(‘~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi’, $content, $classes);).

There is an easy way that can be used, although this method has some shortcomings. I’m using Symfony version 1.3beta, but in symfony 1.2 or 1.3alpha it can be applied.
Let’s get started…

  1. Create config_handles.yml file, and put it in project config folder (sf_root_dir/config)
    config/autoload.yml:
      class: mysfAutoloadConfigHandler
    
  2. Create mysfAutoloadConfigHandler class that extends sfAutoloadConfigHandler, override parseFile() function, and put it in the same folder with config_handlers.yml (sf_root_dir/config)
    <?php
    class mysfAutoloadConfigHandler extends sfAutoloadConfigHandler
    {
      static public function parseFile($path, $file, $prefix)
      {
        $mapping = array();
        $content = file_get_contents($file);
        preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi',
                             $content, $classes);
        if (count($classes[1]) == 0)
        {
          if (stristr($content, 'ioncube_loader') !== FALSE)
          {
            $explodes = explode('/', $file);
            $classphp = $explodes[count($explodes)-1];
            $classnames = explode('.', $classphp);
            $classname = $classnames[0];
            $classes = array('', array($classname));
          }
        }
        foreach ($classes[1] as $class)
        {
          $localPrefix = '';
          if ($prefix)
          {
             // FIXME: does not work for plugins installed with a symlink
             preg_match('~^'.str_replace('\*', '(.+?)', preg_quote(str_replace('/',
                             DIRECTORY_SEPARATOR, $path), '~')).'~', str_replace('/',
                             DIRECTORY_SEPARATOR, $file), $match);
             if (isset($match[$prefix]))
             {
               $localPrefix = $match[$prefix].'/';
             }
          }
          $mapping[$localPrefix.strtolower($class)] = $file;
        }
        return $mapping;
      }
    }
    
  3. Edit setup() function in ProjectConfiguration.class.php file

    public function setup()
    {
       include_once(sfConfig::get('sf_root_dir').'/config/mysfAutoloadConfigHandler.class.php');
    
       //........
       $this->enablePlugins(.....);
      //
    }
    

But, the solution i proposed above still have some disadvantages:

  1. Not just Class files, Helper files are also included in config_autoload_yml.php
  2. Class name must have the same name with file name, for example: if you have a class named A (Class A), then your file should be named A (A.class.php)

PS:
Symfony libraries, log, cache and all the files in the config folder (project config folder, application config folder and modules config folder) is left unencrypted.

Advertisements

9 responses to “Symfony and Ioncube

  1. Hector Perez Hernandez April 27, 2010 at 2:11 am

    hola amigo, estoy tratando de ofuscar un proyecto realizado con symfony, he tratado de hacer todo lo que antes usted menciona pero no me funciona, si tiene alguna guia que me pueda dar se lo agradecería ….
    gracias …

  2. Hector Perez Hernandez April 29, 2010 at 7:51 pm

    I’m working with smfony 1.2.12, and I need to obfuscate the lib directory but when you are loading classes not found, because it is encoded, I tried to experiment with the code above but still can not find the classes, do you can suggest me?
    sorry for my English

  3. Hector Perez Hernandez April 29, 2010 at 7:51 pm

    I’m working with smfony 1.2.12, and I need to obfuscate the lib directory but when you are loading classes not found, because it is encoded, I tried to experiment with the code above but still can not find the classes, do you can suggest me?
    sorry for my English, thanks friend

    • nibsirahsieu April 29, 2010 at 9:20 pm

      I assume you are using ioncube encoder. If you are using another encoder, replace the word ‘ioncube_loder’.
      The version of encoder that i use is ‘ioncube_loader_lin_5.2.so’ (linux version). You can get the loader from here.
      Make sure, your class name has the same name with the file name. for example, if you have a class ‘myClass’ (class myClass) then your file name should be myClass.class.php
      If you follow step by step above, i think there’s no problem..
      I’ve been using this in my production for a few months ago, and i’m using the latest symfony 1.4.4 version now.. so far so god, and i still found no problem

      If you still found some errors.. may i see your ProjectConfiguration.class.php? and which files you encoded?. You can post here or send me by email to nibsirahsieu@gmail.com.

      Good luck my friend.. and i’m sorry too for my english.. 😉

      Below is an example of my encoded class (it’s an actions class).

      <?php //003ab
      if(!extension_loaded('ionCube Loader')){$__oc=strtolower(substr(php_uname(),0,3));$__ln='ioncube_loader_'.$__oc.'_'.substr(phpversion(),0,3).(($__oc=='win')?'.dll':'.so');@dl($__ln);if(function_exists('_il_exec')){return _il_exec();}$__ln='/ioncube/'.$__ln;$__oid=$__id=realpath(ini_get('extension_dir'));$__here=dirname(__FILE__);if(strlen($__id)>1&&$__id[1]==':'){$__id=str_replace('\\','/',substr($__id,2));$__here=str_replace('\\','/',substr($__here,2));}$__rd=str_repeat('/..',substr_count($__id,'/')).$__here.'/';$__i=strlen($__rd);while($__i--){if($__rd[$__i]=='/'){$__lp=substr($__rd,0,$__i).$__ln;if(file_exists($__oid.$__lp)){$__ln=$__lp;break;}}}@dl($__ln);}else{die('The file '.__FILE__." is corrupted.\n");}if(function_exists('_il_exec')){return _il_exec();}echo('Site error: the file <b>'.__FILE__.'</b> requires the ionCube PHP Loader '.basename($__ln).' to be installed by the site administrator.');exit(199);
      ?>
      4+oV55Zs9gETGPkKJ4opWAuB3z8UMUzNCWIGxzGjyJJlEJditDzAP8PmycReVcIF8Z7ZlBw+rnh7
      9V/PFcqErLE0EId08LncVnm5fNuvR24oPGRlrDzYvAgWizYFvpB0q3xwbiFPgv2vVJTuwrln86ZT
      iNzWek8MK++NNVM72o6j5pU5Ia6lP7yGuEQUNYuRWVp7wXl5uNVFycX4n3R2buIT0DzkP1YZ1irl
      Hqp5XaoTb17vyyCfcVddiXS/gA3sVm4HqqqcjQnYgxKBPtRWVtdrzDU8cmUhI032E82YgpW6cKe9
      srv0CgxHXmuT0OEUNjkfyvnBaaw9eNXIFNslKVq9BRPG2BKqd0IAReTOViZO4azyErMbMHU2n+el
      pyZpJFY9PYTZSkxCFeWpRaXLgfqNXZT2HZWPuHlNINgYgi/hwAps9i3HU1G7i/hR24TGfj6ND0Ro
      /s27E9n3CfZNLdwO6JfGtC2kVRKH0IEAzzBQlwqiHhJ3pEgSP+jVWL8RzZcH4S5mC0yGohSiElOH
      IOG7hPhot8cF48CS8pb1aY10jD/uKNGb/up9iFxnqWLwSo+tZBpj1iNP1yZUISkEu0NlQ2oI2o1U
      bmUGwynWq+Uosc4MmS4kRE76XUdopUOxS5Cdu5UdEXKtPkJazMioNZ0JxBdLs46ag6ooogjxX2D4
      Bc5VSPcTKJ7bQBsudh+BkwKRxRVPyTB1n+tDHdoh24CbItu7arkiWgj+X2pW86704jyqqXmGHAne
      LQYoE5qdjQMk3/v1xSCqK5A3WQ9W/a6Rw6AE4Hc0GUhrn6nikO3Ye7Q4ahsMkFA7DDq3WpXH7djP
      GJkuIaw2f5rVvIFsKi3WfXCkRfTQ60s2fgH4As72Na7gkKZxqTgPNjoEkuaA/z/h5O6jngy4ECTt
      K6sTpeMudxvVQLT16mS3VZF9feXP+6f+EHP9hGQAr1k1qjRSkNMrQWhtqca2TXVTQ4WEJZzQdYCG
      ZEUIu14ZncpARg8klXvOrhr339uJ
      
  4. Hector Perez Hernandez May 3, 2010 at 10:13 pm

    thanks friend, your solution is brilliant, and it works, my email is bambinoelkilo@gmail.com, if you need help someday you tell me, thanks

  5. Rodrigo Traleski October 20, 2012 at 2:38 am

    Hi, good evening! I’m using symfony 1.4. Is the same instructions? I tried that, but it’s not working. I did how you made. Do you have an idea?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: