Monday 10 February 2014

How to use PHP Excel without php built in zip archive

"Fatal error: Class 'ZIPARCHIVE' not found"

I found myself in in the situation that I had to use PHPExcel to generate some graphs. Sounds easy enough, and it kind of is, but... after I develop locally on my machine running php 5.3 I face myself with the challenge that I have to install the code on a machine running php 5.1.2 (not suported by PHPExcel). I soon found out that the main problem is the fact that php 5.1 dose not have ZipArchive or PHPExcel_Shared_ZipArchive, you can install them but with a lot of headache and a lot of trying (more than 1 solutions available for more than 1 case) and to be honest, that solution was not the proper way to do it as this was a production server with a lot of stuff on it (you don't want to mess it up).
Maybe a guru server admin could find a proper solution, but I am not that, I am a dev, so... I started researching to find a way to change to another lib that works on 5.1. And so I found... nothing!!! (at least after 2-3h of research and reading a lot of useless forum posts).
As my search was not fruitful and time consuming and annoying I moved my atention to something I love but not like doing: hacking the code ( hacking the code of a maintained lib is not encouraged as this may lead to rough upgrades with lots of diffs etc.  ).
After a little more research and documentation I found pclzip as a php standalone lib here and I found that PHPExcel has a very simple way of changing the ziping class in the settings.
So this is what I have done:

  1. imported the pclzip lib
  2. created a wrapper class for compatibility reasons (pclzip dose not have the function names implemented in the other to libs)
  3. added my wrapper class as a ziping method in settings.php
  4. forced PHPExcel to use my class for ziping
NOTICE: This was tested only for writing, so I have no ideea what will happen when reading, but that is the subject of another post.

Here is a short description of the code I created:

################################################
// pclzip.extended.php
################################################

include('pclzip.lib.php');  // PhpConcept Library - Zip Module 2.8.2 -  http://www.phpconcept.net
class PCLZipExtended{
 // just a place holder
 function PCLZipExtended(){
  
 }
 
 private $zip;
 private $temp_dir;
 // implementation of function open
 // this function needs to create the zip file
 // pclzip dose not create the file untill any action is taken (create/append)
 function open($pFilename,$zipCreate){
  $this->zip = new PclZip($pFilename);
  $this->zip->create(array());
  
  return file_exists($pFilename);
 }
 // implementation of addFromString function
 // needs to save the string to a local file and then append it to the archive
 // keeping the paths is a ***ch
 function addFromString($_location,$data){
  $location=$this->tempdir().$_location;
  if(!file_exists(dirname($location)))
   mkdir(dirname($location), 0777, true);
  file_put_contents($location,$data);
  $loc_to_add = explode('/',$location);
  array_shift ($loc_to_add);
  array_pop ($loc_to_add);
  $loc_to_add = implode('/',$loc_to_add);
  $this->zip->add($location,$loc_to_add,dirname($location));
 }
 // we are done and we can clean up after our selfs 
 function close(){
  
  // do cleanup
  $this->rrmdir($this->tempdir());
 }
 // generate a temp dir to store the files needed for the zip
 // or if we already done this return it
 // and yes php 5.1 dose not have sys_get_temp_dir
 private function tempdir($dir='') {
  if(isset($temp_dir)) return $temp_dir;
  $tempfile=tempnam($dir,'');
  if (file_exists($tempfile)) { unlink($tempfile); }
  mkdir($tempfile);
  if (is_dir($tempfile)) { return $temp_dir=$tempfile.'/'; }
  return false;
 }
 // recursive delete for php
 private function rrmdir($dir='') {
  if (is_dir($dir)) {
   $objects = scandir($dir);
   foreach ($objects as $object) {
    if ($object != "." && $object != "..") {
     if (filetype($dir."/".$object) == "dir") rmdir($dir."/".$object); else unlink($dir."/".$object);
    }
   }
   reset($objects);
   rmdir($dir);
  }
 }
 
}

################################################
// PHPExcel: PHPExcel/PHPExcel/Settings.php
################################################

################

    const PCLZIP        = 'PHPExcel_Shared_ZipArchive';
// line: 42 added just this
    const PCLZIP_SOURCE = 'PCLZipExtended';
// line: 42 added just this
    const ZIPARCHIVE    = 'ZipArchive';

################


        if (($zipClass === self::PCLZIP) ||
// line: 120 added just this
            ($zipClass === self::PCLZIP_SOURCE) ||
// line: 120 added just this
            ($zipClass === self::ZIPARCHIVE)) {

################

################################################
// Main php file that dose all the work
################################################

        include('modules/ER_Reports/pclzip.extended.php');
        include('modules/ER_Reports/PHPExcel/PHPExcel.php');
        PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP_SOURCE);
        $er = new ER_Reports();


And that's all folks!!!!

Tuesday 29 October 2013

WIndows: HOW TO move files in sequent folders

I was found not long ago in a situation where I had a large number of files in one folder that had to be moved in sequent folders (500 files in each folder). Here is the script I managed to make:


echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: set this number eq to num
set movedFiles=500
:: the number of files in each folder
set num=500
:: location of files 
set location=D:\files
:: destination of files
set destinbation=D:\files\
:: set the starting numbering for the subfolders
set /a sn=5
for %%G in ("!location!"\*) do (
 if !movedFiles! EQU %num% (
  set /a sn=sn+1
  echo Creating !destinbation!!sn!
  mkdir !destinbation!!sn!
  set /a movedFiles=0
 )
 echo "%%G" "!destinbation!!sn!"
 move /Y "%%G" "!destinbation!!sn!"
 set /a movedFiles+=1
)
  
ENDLOCAL

Friday 26 July 2013

Jquery is(':focus') is not working in IE8

I recently encountered a problem when using the is(':focus') with ie8. The only error I got is

SCRIPT5022: Exception thrown and not caught 
jquery.js, line 85 character 139

Not a lot of help.

After further research I found that there is actually a problem with how jQuery is handling this and I had to rewrite it.

jQuery.expr[':'].focus = function( elem ) {
    return elem === document.activeElement && ( elem.type || elem.href );
};

Quoted from Mathias Bynens here:
Note that the (elem.type || elem.href) test was added to filter out false positives like body. This way, we make sure to filter out all elements except form controls and hyperlinks.

Wednesday 27 February 2013

Windows 7 Error "Explorer. exe No such interface supported"

I got this error after I did a mass update of my windows, reaching from a clean installation to SP1. I messed around a lot on the internet but could not find a good solution, most of the final suggestions were "Back up your data and reinstall". But as I didn't wanted to do that (just thinking about all the softwear I have to reinstall gave me a headache ) I keept going until I found exactly what I needed.

The process of fixing this is really easy if you know what you are doing, but I wasn't, so I will have to give credits to Jae Hyuk Lee on his post.

I will rewrite the solution here to keep it for posterity and make it easy to search.

1. open comand prompt as admin
2. run regsvr32 c:\windows\system32\actxprxy.dll
3. restart if needed

Monday 29 October 2012

Update Samsung GT-I5500 from CyanogenMod 7.2 to 9 (android 4.0)

In a previous post I explained how to update your phone to to 2.3. Now you can update it to 4.0 (CyanogenMod 9) !!!!
Supposing you completed the prev steps in this post and you have Cyanogen 7.2 (or later) you will need this file: Cyanogen9 (link on megaupload)
After downloading it redo step 7 from the previous post and instead "update-cm-7.2.0-RC0-galaxy5-v1.9-MADTEAM-signed" use the zip downloaded. Just to keep it simple I will re-write the instructions:

Now we move on to installing packages. Go to "Mount and storage", then "Mount USB Storage". Copy files "cm-10-20121024-MADTEAM-galaxy5" (the file downloaded earlyer) to the removable disk (SD card of the phone). We expect to complete the process successfully and give "Unmount USB Storage" in the CWM phone. Back to main menu using the key back. Navigate to the "Install zip from SD card" -> "Chose zip from SD card". Select "cm-10-20121024-MADTEAM-galaxy5", followed by Yes.

Give "Reboot System"


If you have problems with gMarket redo step 9.In order to copy files to the phone you need to do step 5.If this is to trivial please leave a message and I will post the complete guid on how to do this without references.


If this post helped you in any way don't be shay and treat me to a bear, what do you say?



Sunday 28 October 2012

Class not registered Windows 7 error

Some time ago I had a lot of problems with my Windows 7 that I could not find a solution to. Some of them were:

- opening a folder from uTorrent ('Class not registered')
- opening Network and Shering (dosen't open)
- opening System window (same)
- opening most of my Control Panel windows

I found a lot of solution that didn't work for me, but the only that did was on google chrome's bug tracker in this post, response #9.

The solution just repairs all the dll's on c: drive, if you have Windows installed on a difrent partition just change C: to whatever letter is your windows partition.

I recommend reading the post before running the command just know the risks and what to expect.

FOR /R C:\ %G IN (*.dll) DO "%systemroot%\system32\regsvr32.exe" /s "%G" 

Saturday 27 October 2012

How to root Samsung Galaxy i9100 S2


Galaxy S2 is one of the real success of Samsung's leading devices based on the Android platform. And how it is a very powerful phone that has sold so fast, the hackers did not spent much thought and have root-ed it. There are 2 tutorials on rooting , I will write the easiest for beginners. So let's start.


  1. Download Odin and extract the files from the archive, saving them on your computer
  2. Download XWKDD but not extract any files from it
  3. Download SuperOneClick
  4. Download Samsung Kies and install you phone drivers
  5. After you install the Samsung Kies restart PC
  6. Put your phone in USB debugging mode. Go to Settings, Applications, Development and start USB Debugging
  7. Turn off the phone and turn it back on holding the power, home and volume down buttons
  8. Start Odin and connect your phone to the PC with a USB cable
  9. Wait until Odin will recognize your phone and shows it is connected. On Odin window, except "F.Reset Time" and "Auto Rebot", NO other option should be checked 
  10. Press PDA and select the file that you downloaded at step 2 and press Start
  11. Odin will start flash-ing the kernel. When you are finished, your phone will automatically restart
  12. After the phone will reset start SuperOneClick, click root, the phone will root,  restart the phone and you're done.