Code snippets for symfony 1.x

Navigation

Snippets by user karoly sz

Use databases.yml in Ruckusing Migrations

Replace (or link) Ruckusing's database.inc.php with this code. You'll be able to use symfony's environment names in Ruckusing commands.

Example: php lib/vendor/ruckusing/main.php db:migrate ENV=dev

<?php
$configFile = dirname(__FILE__)."/../../../../config/databases.yml";
include(dirname(__FILE__)."/../../symfony/lib/yaml/sfYaml.class.php");
$connections = sfYaml::Load($configFile);
 
foreach($connections as $con => $val)
{
  if($con == 'all') continue;
  $connection_name = $con;
  $con = array();
  $params = array_pop($val);
  $params = $params['param'];
 
  preg_match("/^(.*)\:/", $params['dsn'], $match);
  $con['type'] = $match[1];
 
  preg_match("/dbname=(.*);/", $params['dsn'], $match);
  $con['database'] = $match[1];
 
  preg_match("/host=(.*);?/", $params['dsn'], $match);
  $con['host'] = $match[1];
 
  if(preg_match("/port=(.*);?/", $params['dsn'], $match))
  {
    $con['port'] = $match[1];
  }
  elseif($con['type'] == 'mysql')
  {
    $con['port'] = 3306;
  }
 
  $con['user'] = $params['username'];
  $con['password'] = $params['password'];
 
  $ruckusing_db_config[$connection_name] = $con;
}
 
// dev and development means the same thing in the two config files
$ruckusing_db_config['development'] = $ruckusing_db_config['dev'];
?>
 
by karoly sz on 2009-10-18, tagged migration  ruckusing 

Image cache to get any version of an image.

<?php
    /**
     * IMAGECACHE
     *
     * Image-cache script to convert and store images on-the-fly to any size.
     * (c) k at isr.hu
     *
     * THE PROBLEM
     *
     * You upload a lot of image assets to your site, but will need 
     * several sizes of them for various reasons. For example You'll need a big 
     * resolution version for downloading (original), also a smaller for previewing 
     * to the visitors and even another for an image-list page.
     *
     * This script will create any version of the image using imagemagick and store 
     * it next to the original file. Images already generated are served without 
     * PHP, so You'll see no performance loss.
     *
     * HOW IT WORKS
     *
     * You define ImageMagick commands in this script, giving them aliases.
     * When You need an image converted, You sppecify the command in the URL of the 
     * image. If the image (of this size/version) does not exist yet, this script 
     * will generate it and save it next to the original image. The next time this 
     * image is requested, it will be served from the filesystem, without any 
     * overhead, thanks to the mod_rewrite rule You save into the image folder's 
     * .htaccess 
     *
     * Requirement: ImageMagick installed in the server. See http://www.imagemagick.org/ for information.
     * You will also need Apache with mod_rewrite.
     *
     * EXAMPLE
     *
     * Suppose You keep image assets under web/images, and have an image called greenfrog.jpg (size: 1200x1200px)
     * saved under web/images/animals/green/greenfrog.jpg. The image is accessible under
     * http://www.you.com/images/animals/green/greenfrog.jpg
     * One day You realize that You need a 180x180px version of the image on the frontpage.
     * You can upload another variation, but with this script, you can just specify the new size in the URL, like this:
     * http://www.you.com/images/animals/green/greenfrog.180x180.jpg
     *
     * To be able to do this, You will need to save a .htaccess file into the 
     * images folder (see below), plus set the ImageMagick command associated with 180x180.
     * (The reson for the latter is security: if You run any command from the URL 
     * You open the door for DOS attacks.)
     *
     * INSTALL
     *
     * - save this script to the root of the images folder
     * - create a .htaccess file next to it, with this content: 
     *
     * <IfModule mod_rewrite.c>
     *   RewriteEngine on
     *   RewriteCond %{REQUEST_FILENAME} !-f
     *   RewriteCond %{REQUEST_FILENAME} !-d
     *   RewriteRule ^(.*)$ index.php?img=$1 [L,QSA]
     * </IfModule>
     * 
     * - specify the commands You want to accept in the commands array
     * - modify the image URL-s to take advantage of the script.
     * - done :)
     *
     * SECURITY
     *
     * AFAIK all possible DOS attacks are handled (can't generate images forever),
     * plus the script takes care not allow access to files outside the 
     * directory it is saved to.
     *
     * No guarantee or warranty of any kind!
     *
     * TIP ME!
     *
     * If You find this script useful, please consider giving me a tip!
     * Just follow this link to donate: 
     * https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7030104
     **/
 
    // find the convert command
    // you can hard-code it to make the srcipt faster, example:
    // $convert = '/usr/bin/convert';
    $convert = trim(shell_exec('which convert'));
 
    // this directory
    $thisDir = dirname(__FILE__).'/';
 
    // set commands via aliases
    $commands = array(
      '120x120' => '-geometry 120x120', // convert so no dimension is larger than 120px and keep ratio. For more commands see ImageMagick's documentation
    );
 
    // die if no image is set
    if(!$img = $_GET['img']) die('no img');
 
    // make the filename safe to use
    //$img = strip_tags(htmlspecialchars($img));
    $img = trim($img);
    $img = str_replace('..', '', $img); // no going up in directory tree
 
    // get the command specified in the filename
    // example: big_green_frog.thumb150.jpg
    preg_match("/\.([^\.]*)\..{3}$/", $img, $match); // this also means no double commands (possible DOS attack)
    if(!$command = $match[1]) die('no command');
    if(!$commands[$command]) die('command does not exist');
 
    // get original filename
    // example: big_green_frog.jpg
    $imgFile = str_replace('.'.$command, '', $img, $replaceCount);
 
    // stop the possibility of creating unlimited files
    // example (attack): abc.120.jpg, abc.120.120.jpg, abc.120.....120.jpg - this could go on forever
    if($replaceCount > 1) die('command specified more than 1 time');
 
    // die if the original file does not exist
    if(!file_exists($thisDir.$imgFile)) die('original file does not exist');
 
    // prepare the command
    $preparedCommand = $convert .' '. $commands[$command] .' '. $thisDir . $imgFile .' '. $thisDir . $img;
 
    // run the conversion
    shell_exec($preparedCommand);
 
    // redirect the browser to the new image - this is more reliable than fpassthru
    header("Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.$img);
    exit;
    ?>
 
by karoly sz on 2009-07-25, tagged cache  convert  image  imagemagick  resize  thumbnail