Archive for category php

Testlink 1.9 problem with Charts – Error 500

Today I faced problem with Testlink 1.9 – while clicking on Test Results > Charts I’ve got :

PHP Fatal error: require_once() [function.require]: Failed opening required '/var/www/html/testlink/cfg/.cfg.php' (include_path='.:/usr/share/pear:.:/var/www/html/testlink/lib/functions/:/var/www/html/testlink/third_party/') in /var/www/html/testlink/lib/bugtracking/int_bugtracking.php on line 367, referer: http://xxxxxx/testlink/lib/results/resultsNavigator.php

If you have similar edit /var/www/html/testlink/lib/bugtracking/int_bugtracking.php

Line 363: $btsname = strtolower($bts_type);

Dirty, but works

Line

Tags: ,

Prime numbers in PHP – quick and efficient way

Looking for a way to find prime numbers in php? I wfound one perl solution here, but it wasn’t perl magic – it was power of regular expressions.

Please find code below which will be self-explanatory.


<?php

/**
* Function checks if given number is prime
*
* @param integer $n
* @return boolean
*/
function isPrime($n) {
return (!preg_match(‘/^1?$|^(11+?)\1+$/x’, str_repeat(’1′, intval($n)))) ;
}

for ($n=0;$n<100;$n++) {
echo “Number {$n} is “.(isPrime($n) ? ‘prime’ : ‘not prime’).”<br/>\n”;
}

?>

Tags: , ,

Use PHP and PDO to connect to MS SQL Server (Zend Framework)

Problems connecting to MS SQL server from Zend Framework?

Message: The mssql driver is not currently installed

Solution is simple

Install pdo_mssql driver: (Ubuntu)

#sudo apt-get install php5-sybase
#sudo /etc/init.d/apache2 restart

On Centos you need to install pdo_dblib (any problems check here)

It will not reflect in phpinfo() page

Trick is to setup connection properly:

$dbAdapter = new Zend_Db_Adapter_Pdo_Mssql(array(
 'host'     =&gt; '192.xxx.xxx.xxx', // parklife
 'username' =&gt; 'xxx',
 'password' =&gt; 'xxx',
 'dbname'   =&gt; 'xxx',
'pdoType'  =&gt;  'dblib' )
 );

Tags: , , ,

PHP & MongoDB Sitting in a Tree: Part 1

Always wanted to try it, nice tutorial here
http://www.dealtaker.com/blog/2010/05/12/php-mongodb-sitting-in-a-tree-part-1/

Tags:

What should a developer know before building a public web site?

“The idea here is that most of us should already know most of what is on this list. But there just might be one or two items you haven’t really looked into before, don’t fully understand, or maybe never even heard of.”

Great article here

Tags:

Run PHP (and others) code in a browser – revolutionary pastebin

If you need to test a code in browser – try  http://ideone.com/

Entry found on http://alouche.net/blog/2009/11/24/ideone-compiler-pastebin/

Tags: , , ,

Geotagging script

Geotagging script (http://themeforest.net/user/php4ucouk)

Script converts visitor’s ip adress into :

  • country code (2 and 3 letter ISO code)
  • full english country name
  • region code and name
  • currency code used in this country
  • postal code
  • gps coordinates
  • area code
  • dma code
  • metro code

and BONUS

  • overall statitics about users from different countries
  • number or live visitors on your web page

Data is provided to you, so you can use it (display it, store in db or do whatever you need)

Live demo

Tags: , , ,

PHP Mail problem on Ubuntu

I’ve installed fresh version of Ubuntu 9.04 with Lamp, but I couldn’t send emails outside.

Problem was SMTP auth of outgoing server, spending time found http://dbaron.org/linux/sendmail who made my day. Thanks

Tags: , , ,

Logging page rendering time in Symfony 1.xx

Change your SF_APP/web/index.php file to be:

$timer = sfTimerManager::getTimer('myTimer');
sfContext::getInstance()-&gt;getController()-&gt;dispatch();
$timer-&gt;addTime();
$elapsedTime = $timer-&gt;getElapsedTime();
$fullRealUri = str_replace( sfContext::getInstance()-&gt;getRequest()-&gt;getUriPrefix(), "", sfContext::getInstance()-&gt;getRequest()-&gt;getUri());
file_put_contents('/tmp/time.log',date('Y-m-d H:i:s')."|$fullRealUri|$elapsedTime\n\r",FILE_APPEND);

Tags: , , ,

sFTP for PHP 5.xx

Install SSh2 extension for PHP, then you can use class below:

&lt;?php
/**
 * Class to deal with sFTP connections
 * to use install http://php.oregonstate.edu/manual/en/ssh2.installation.php
 *
 */
class SFTPConnection
{
    private $connection;
    private $sftp;
 
    public function __construct($host, $port=22)
    {
        if (!extension_loaded('ssh2'))
        {
        	throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
        }
    	$this-&gt;connection = ssh2_connect($host, $port);
        if (! $this-&gt;connection)
            throw new Exception("Could not connect to $host on port $port.");
    }
 
    public function login($username, $password)
    {
    	if (!extension_loaded('ssh2'))
        {
        	throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
        }
    	if (! @ssh2_auth_password($this-&gt;connection, $username, $password))
            throw new Exception("Could not authenticate with username $username " . "and password $password.");
        $this-&gt;sftp = @ssh2_sftp($this-&gt;connection);
        if (! $this-&gt;sftp)
            throw new Exception("Could not initialize SFTP subsystem.");
    }
 
    public function uploadFile($local_file, $remote_file)
    {
    	if (!extension_loaded('ssh2'))
        {
        	throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
        }
    	$sftp = $this-&gt;sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');
        if (! $stream)
            throw new Exception("Could not open file: $remote_file");
        $data_to_send = @file_get_contents($local_file);
        if ($data_to_send === false)
            throw new Exception("Could not open local file: $local_file.");
        if (@fwrite($stream, $data_to_send) === false)
            throw new Exception("Could not send data from file: $local_file.");
        @fclose($stream);
    }
 
        function scanFilesystem($remote_file) {
	        if (!extension_loaded('ssh2'))
	        {
	        	throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
	        }
        	$sftp = $this-&gt;sftp;
            $dir = "ssh2.sftp://$sftp$remote_file";
              $tempArray = array();
            $handle = opendir($dir);
          // List all the files
            while (false !== ($file = readdir($handle))) {
            if (substr("$file", 0, 1) != "."){
              if(is_dir($file)){
//                $tempArray[$file] = $this-&gt;scanFilesystem("$dir/$file");
               } else {
                 $tempArray[]=$file;
               }
             }
            }
           closedir($handle);
          return $tempArray;
        }   
 
    public function receiveFile($remote_file, $local_file)
    {
    	if (!extension_loaded('ssh2'))
        {
        	throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
        }
    	$sftp = $this-&gt;sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'r');
        if (! $stream)
            throw new Exception("Could not open file: $remote_file");
        $contents = fread($stream, filesize("ssh2.sftp://$sftp$remote_file"));
        file_put_contents ($local_file, $contents);
        @fclose($stream);
    }
 
    public function deleteFile($remote_file){
      if (!extension_loaded('ssh2'))
      {
      	throw new Exception("Extension ssh2 has to be loaded to use this class. Please enable in php.ini.");
      }
      $sftp = $this-&gt;sftp;
      unlink("ssh2.sftp://$sftp$remote_file");
    }
}
?&gt;

Tags: , ,