Tuesday, September 13, 2016

Compile PHP modules on MacOS

On MacOSX, in order to compile modules such as mcrypt one may need to run the following before starting the configuration and compilation:

MACOSX_DEPLOYMENT_TARGET=10.6
CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe"
LDFLAGS="-arch i386 -arch x86_64 -bind_at_load"
export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET

Then the compilation should continue as below:
$ cd php-5.4.24/ext/mcrypt
$ phpize
$ ./configure # this is the step which fails without the above dependencies
$ make
$ make test
$ sudo make install

Thanks to http://stackoverflow.com/a/8432855

Tuesday, May 03, 2016

Symfony - Navigation with "paginator"


{% if pagination.items|length %}
     {% set queryParams = pagination.params %}
     {% set pg = pagination.paginationData %}
     {% set itemStart = (pg.current - 1)*pg.numItemsPerPage + 1 %}

Records {{ itemStart }} - {{ itemStart + pagination.items|length - 1 }} of {{ pg.totalCount }}

{# array (size=16)
  'last' => int 2235
  'current' => int 1
  'numItemsPerPage' => int 9
  'first' => int 1
  'pageCount' => int 2235
  'totalCount' => int 20112
  'pageRange' => int 5
  'startPage' => int 1
  'endPage' => float 5
  'next' => int 2
  'pagesInRange' =>
    array (size=5)
      0 => int 1
      1 => int 2
      2 => int 3
      3 => int 4
      4 => int 5
  'firstPageInRange' => int 1
  'lastPageInRange' => int 5
  'currentItemCount' => int 9
  'firstItemNumber' => int 1
  'lastItemNumber' => int 9#}



   {% for entity in pagination %}
      ..
   {% endfor %}
{% else %}
   Your search returned no results
{% endif %}

{{ knp_pagination_render(pagination) }}

Wednesday, April 27, 2016

Shell script to scan folders and delete all but the most recent ones

root@server1 [~]# cat rm-releases.sh

#!/bin/bash

DIRECTORY="releases"

for i in $(ls -l /home | grep - | awk '{print $8}'| sed 's|[/]||g'); do
#  echo ${i%%/};

  if [ -d "/home/${i%%/}/$DIRECTORY" ]; then
    TARGETDIR=$(ls -t /home/${i%%/}/$DIRECTORY | head -1)
#    echo $TARGETDIR;
    for j in $(ls -l "/home/$i/$DIRECTORY" | grep - | awk '{print $8}' | sed 's|[/]||g'); do
      if [ "$j" != "$TARGETDIR" ]; then
        echo "rm /home/$i/$DIRECTORY/${j}"
        rm -rf "/home/$i/$DIRECTORY/${j}"
      fi
    done
#    break;
  fi
done

Friday, December 04, 2015

Downgrade PHP Version from 5.5 to 5.4 on Ubuntu 14.04

First one have to install the PPA containing PHP54:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php5-oldstable
sudo apt-get update
sudo apt-get install -y php5

Then install Apache+fcgid+php5.4:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get remove --purge `dpkg -l | grep php | grep -w 5.5 | awk '{print $2}' | xargs`
sudo apt-get purge apache2 php5 libapache2-mod-php5
sudo sed -i.bak "s/trusty/precise/g" /etc/apt/sources.list
sudo apt-get update
sudo apt-get install apache2 apache2-suexec libapache2-mod-fcgid php5-cgi
sudo apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-xdebug
sudo sed -i "s/precise/trusty/g" /etc/apt/sources.list
dpkg --get-selections | egrep '^(apache|php)' | sed 's/install/hold/g' | sudo dpkg --set-selections
sudo apt-get update
sudo apt-get install  mysql-client mysql-server phpmyadmin

Note that the "switch" between trusty and precise was needed because PPA Ondrej does not have packages for trusty

Finally, continue setting up the environment as here: http://www.howtoforge.com/how-to-set-up-apache2-with-mod_fcgid-and-php5-on-ubuntu-12.04


Thursday, November 19, 2015

On-the-fly Symfony form for searches

Version 1: Typeless form

Inside the controller:
$q = $request->query->get('q');
$data = array('q' => $q); 
$searchForm = $this->get('form.factory')
    ->createNamedBuilder('', 'form', $data, array(
        'csrf_protection' => false,
        'method' => 'GET',
    ))
    ->add('q', 'text', array(
        'required' => false,
    ))
    ->getForm();

if($q) {
    $data = $searchForm->handleRequest($request)->getData();
    $qb
        ->andWhere($qb->expr()->like('name',  ':q'))
        ->setParameter('q', $data['q']);
} 
return $this->render('@Bundle:Entity:index.html.twig', array(
            'searchForm'    => $searchForm->createView()
        ));

Version 2:  using FormType

Inside controller:
$filterForm = $this->createForm(new SomeFilterType(), null, array(
    'method' => 'GET',
));

$filterForm->handleRequest($request);
if ($filterForm->isValid()) {
    $name = $filterForm->get('name')->getNormData();
//
}

Wednesday, November 18, 2015

Failed to download zendframework/zend-stdlib from dist

Problem

# php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
  - Removing zendframework/zend-stdlib (2.5.1)
  - Installing zendframework/zend-stdlib (2.2.3)
    Downloading: Connecting...    Failed to download zendframework/zend-stdlib from dist: The "https://api.github.com/repos/zendframework/Component_ZendStdlib/zipball/7c87ce4e840957596bf3401fa4ae4fb0355682e2" file could not be downloaded (HTTP/1.1 404 Not Found)
    Now trying to download from source
  - Installing zendframework/zend-stdlib (2.2.3)
    Cloning 7c87ce4e840957596bf3401fa4ae4fb0355682e2


                                                                                                                   
  [RuntimeException]                                                                                               
  Failed to clone git@github.com:zendframework/Component_ZendStdlib.git via git, https, ssh protocols, aborting.   
  - git://github.com/zendframework/Component_ZendStdlib.git                                                        
    error: The requested URL returned error: 403 Forbidden while accessing https://github.com/zendframework/Compo  
  nent_ZendStdlib.git/info/refs  

Steps to solve:

1.  Verify the connection to github

# ssh -T git@github.com  Permission denied (publickey).
2. Verify that ssh agent is running

# eval "$(ssh-agent -s)"  
Agent pid 100 
3. Verify that the key is loaded into the ssh agent

# ssh-add -l
The agent has no identities.
# ssh-add   
Identity added: /home/{user}/.ssh/id_rsa (/home/{user}/.ssh/id_rsa) 
4. Set-up github's credentials for local computer
# git config --global user.name "{githubuser}"
then copy the local key from  /home/{user}/.ssh/id_rssa.pub into {githubuser}'s account (Settings->SSH keys->Add SSH key)

then verify it:
# ssh -T git@github.com
Hi {githubuser}! You've successfully authenticated, but GitHub does not provide shell access.
5. Run composer update for the specific version you want
# php composer.phar require zendframework/zend-stdlib 2.2.3

Thursday, March 04, 2010

Extract display name from email address


/*
$tomatch = array('Name Surname ','name.surname@blah.com','"b. blah"@blah.co.nz','Some guy <"b. blah"@blah.co.nz>');

foreach ($tomatch as $email) {
print_r(extract_display_name_email($email));
}

*/

function extract_display_name_email($str){
$str = trim($str);
$pos = strrpos($str, '@');
if(false === $pos) return false;

//spaces inside the display name !
if('"' == $str[$pos-1]){
$newpos = strrpos(substr($str, 0, $pos - 1), '"');
if(0 === $newpos) {
$newpos = false; //no display name
}else{
$newpos = strrpos(substr($str, 0, $newpos - 1), ' '); //find last space before "
}
}else{
$newpos = strrpos($str, ' '); //if false then there is no display name
}

if($newpos !== false){
$displayname = trim(substr($str, 0, $newpos));
$email = trim(substr($str, $newpos));
}else{
$displayname = '';
$email = $str;
}
if($email[0] == '<') $email = substr($email, 1, -1); //strip <>
if(strlen($displayname) && $displayname[0] == '"') $displayname = substr($displayname, 1, -1); //strip "

return array('email'=>$email, 'displayname'=>$displayname);
}