Tag Archives | php

Doxygen 1.8.16 duyuruldu

C++, C, Java, Objective-C, Python, IDL, PHP, C#, Fortran, VHDL, Tcl ve bir dereceye kadar D dilleri için online/offline dokümantasyon hazırlamayı sağlayan bir dokümantasyon sistemi olan Doxygen‘in 1.8.16 sürümü duyuruldu. Kaynak kodlarının içinde herhangi ek bir IDE’ye yada programa ihtiyaç duymaksızın gezinmeyi sağlayan Doxygen; HTML, RTF (MS-Word), PostScript, hyperlinked PDF, compressed HTML, Unix man şeklinde çıktıları destekliyor. Projelere ait dokümantasyon hazırlarken zaman bakımından büyük bir kazanç sağlayan yazılım, Mac OS X ve Linux altında geliştirilmiş, ancak oldukça taşınabilir bir platform olarak ayarlanmıştır. Doxygen ayrıca hepsi otomatik olarak üretilen bağımlılık grafiklerini, kalıtım şemalarını ve işbirliği şemalarını kullanarak çeşitli elemanlar arasındaki ilişkileri görselleştirebilir. Doxygen’i normal belgeler oluşturmak için de kullanabilirsiniz. Doxygen 1.8.16 hakkında ayrıntılı bilgi edinmek için değişiklikler sayfasını inceleyebilirsiniz.

Continue Reading →

Doxygen 1.8.16 edinmek için aşağıdaki linkten yararlanabilirsiniz.

0

Cast or Convert an Array to Object Using PHP (Hydrator Pattern)

In this post, we will convert an array to object using PHP hydrator pattern. This method is so simple, it’s about transferring data from one place to another. We will define a class that will take an array and an object as inputs and search for all set() methods in the object and fills it with values from the array. First, we will determine the object class using get_class() function then we will use get_class_methods() to get the class methods. Keep in mind that we use PHP 7 coalescing operator (??), in case you are not using PHP 7, you can use ternary operator instead. We use substr then we concatenate because if we have lowerCamelCase member variables like $firstName.

Continue Reading →

class Converter
{
public static function toObject(array $array, $object)
{
$class = get_class($object);

$methods = get_class_methods($class);

foreach ($methods as $method) {

preg_match(' /^(set)(.*?)$/i', $method, $results);

$pre = $results[1] ?? '';

$k = $results[2] ?? '';

$k = strtolower(substr($k, 0, 1)) . substr($k, 1);

If ($pre == 'set' && !empty($array[$k])) {

$object->$method($array[$k]);
}
}
return $object;
}
}

To test this converter class, we need to create a class with properties and methods (getters & setters) and see how to convert an array to object using PHP in action.

Let’s assume that we have an employee class like this:

class Employee
{
protected $name;

protected $phone;

protected $email;

protected $address;

public function getName()
{
return $this->name;
}

public function getPhone()
{
return $this->phone;
}

public function getEmail()
{
return $this->email;
}

public function getAddress()
{
return $this->address;
}

public function setName($name)
{
$this->name = $name;
}

public function setPhone($phone)
{
$this->phone = $phone;
}

public function setEmail($email)
{
$this->email = $email;
}

public function setAddress($address)
{
$this->address = $address;
}
}

Convert Array To Object

Now let’s create an array that will hold the data that will be transferred to the class.

$arr['name'] = "Adam";

$arr['phone'] = "123456";

$arr['email'] = "[email protected]";

$arr['address'] = "U.S";

Great, let’s convert the array data to the class.

$obj = Converter::toObject($arr, new Employee());

var_dump($obj);

Look at the result:

Cool!!

You can convert an array to object using PHP hydrator pattern.

Convert Object to Associative Array

What about converting the object to an associative array, it’s the same concept, we are going to create a new function that does the opposite.

Our function will search for all get() functions the same way as the previous function like this:

public static function toArray($object)
{
$array = array();

$class = get_class($object);

$methods = get_class_methods($class);

foreach ($methods as $method) {

preg_match(' /^(get)(.*?)$/i', $method, $results);

$pre = $results[1] ?? '';

$k = $results[2] ?? '';

$k = strtolower(substr($k, 0, 1)) . substr($k, 1);

If ($pre == 'get') {

$array[$k] = $object->$method();
}
}
return $array;
}

Add this function to our converter class and call it with a passed object like this:

var_dump(Converter::toArray($obj));

Note that the passed $obj here is the generated object from the array to object conversion process.

The output shows the associative array as expected.

I hope you find the post useful. Keep coming back.

Thank you.

0

Process Large Files Using PHP

If you want to process large files using PHP, you may use some of the ordinary PHP functions like file_get_contents() or file() which has a limitation when working with very large files. These functions rely on the memory_limit setting in php.ini file, you may increase the value but these functions still are not suitable for very large files because these functions will put the entire file content into memory at one point. Any file that has a size larger than memory_limit setting will not be loaded into memory, so what if you have 20 GB file and you want to process it using PHP? Another limitation is the speed of producing output. Let’s assume that you will accumulate the output in an array then output it at once which gives a bad user experience. For this limitation, we can use the yield keyword to generate an immediate result.

Continue Reading →

SplFileObject Class

In this post, we will use the SplFileObject class which is a part of Standard PHP Library.

For our demonstration, I will create a class to process large files using PHP.

The class will take the file name as input to the constructor:

class BigFile
{
protected $file;
public function __construct($filename, $mode = "r")
{
if (!file_exists($filename)) {
throw new Exception("File not found");
}
$this->file = new SplFileObject($filename, $mode);
}
}

Now we will define a method for iterating through the file, this method will use fgets() function to read one line at a time.

You can create another method that uses fread() function.

Read Text Files

The fgets() is suitable for parsing text files that include line feeds while fread() is suitable for parsing binary files.

protected function iterateText()
{
$count = 0;
while (!$this->file->eof()) {
yield $this->file->fgets();
$count++;
}
return $count;
}

This function will be used to iterate through lines of text files.

Read Binary Files

Another function which will be used for parsing binary files:

protected function iterateBinary($bytes)
{
$count = 0;
while (!$this->file->eof()) {
yield $this->file->fread($bytes);
$count++;
}
}

Read in One Direction

Now we will define a method that will take the iteration type and return NoRewindIterator instance.

We use the NoRewindIterator to enforce reading in one direction.

public function iterate($type = "Text", $bytes = NULL)
{
if ($type == "Text") {
return new NoRewindIterator($this->iterateText());
} else {
return new NoRewindIterator($this->iterateBinary($bytes));
}
}

Now the entire class will look like this:

class BigFile
{
protected $file;
public function __construct($filename, $mode = "r")
{
if (!file_exists($filename)) {
throw new Exception("File not found");
}
$this->file = new SplFileObject($filename, $mode);
}
protected function iterateText()
{
$count = 0;
while (!$this->file->eof()) {
yield $this->file->fgets();
$count++;
}
return $count;
}
protected function iterateBinary($bytes){
$count = 0;
while (!$this->file->eof()) {
yield $this->file->fread($bytes);
$count++;
}
}
public function iterate($type = "Text", $bytes = NULL)
{
if ($type == "Text") {
return new NoRewindIterator($this->iterateText());
} else {
return new NoRewindIterator($this->iterateBinary($bytes));
}
}
}

Parse large Files

Let’s test our class:

$largefile = new BigFile("file.csv");
$iterator = $largefile->iterate("Text"); // Text or Binary based on your file type
foreach ($iterator as $line) {
echo $line;
}

This class should read any large file without limitations Great!!

You can use this class in your Laravel projects by autoloading your class and add it to composer.json file.

Now you can parse and process large files using PHP easily.

Keep coming back.

Thank you.

0

How to prevent SQL injection attacks?

Before we talk about how to prevent SQL injection, we have to know the impact of SQL Injection attack which is one of the most dangerous attacks on the web. The attacker can steal your data or even worse, the whole web server can be stolen from one SQL injection vulnerability. I wrote this post to show you how to prevent SQL injection. If you need to know more about SQL injection itself and its types and all other stuff, you can do a simple search on google if you want. The solution is to clean the request parameters coming from the user . Keep in mind that the solution that I’ll share with you is not like the most solutions on the web that go to every SQL statement and clean the request variables one by one.

Continue Reading →

My solution is preventing SQL injection without messing with your CMS files. Well, maybe someone who is using PHP would say that is easy, it is just using a function like mysql_real_escape_string or mysqli_real_escape_string. But that works only on a single dimensional array, what about a multi-dimensional array? Well, we need to iterate over array items recursively. So what we will do is preventing SQL injection against a multidimensional array.

Solution

This code does the magic for both single and multidimensional arrays using PHP:

if (!function_exists("clean")) {
//Gets the current configuration setting of magic_quotes_gpc if on or off
if (get_magic_quotes_gpc()) {
function magicquotes_Stripslashes(&$value, $key) {
$value = stripslashes($value);
}
$gpc = array(&$_COOKIE, &$_REQUEST);
array_walk_recursive($gpc, 'magicquotes_Stripslashes');
}
function clean(&$value, $key) {
//here the clean process for every array item
// use mysqli_real_escape_string instead if you use php 7
$value = mysql_real_escape_string($value);
}
}
$req = array(&$_REQUEST);
array_walk_recursive($req, 'clean');

The PHP function used to walk through the request multidimensional array is array_walk_recursive.

Just put this code on the top of your site or header file right after connecting to the database, your file could be up.php or header.php or something similar.

Because if you use this code before the connection occurs, it will show an error because you are using mysql_real_escape_string function which needs a SQL connection.

If you are using PHP 7, you’ll notice that MySQL extension is removed, so in order to make the above code working, you need to replace the function mysql_real_escape_string with mysqli_real_escape_string.

One final thing I have to mention regarding your code, you should keep your SQL parameters on all of your pages between quotes like this:

mysql_query("select * from users where email='$email' order by id");

Notice how the variable $email is quoted.

Without quoting your input like the above statement, SQL injection prevention won’t work.

0

Apache NetBeans IDE 11.1 duyuruldu

Apache NetBeans IDE’nin 11 döngüsünün LTS sürümünün Apache NetBeans 11.0 olduğu belirtilirken, 11.1 sürümünün, LTS sürümü kadar ağır olarak test edilmediği belirtiliyor. Bu nedenle sürümün daha az kararlı olabileceği ifade ediliyor. Nisan 2020’de yayınlanması planlanan bir sonraki LTS sürüm için geri bildirim sağlamak üzere 11.1 sürümünün kullanılmasının önemli olduğu belirtilirken, ilgili sürümü indirmek isteyen kullanıcıların buradan yararlanabilecekleri söyleniyor. The Apache Software Foundation,  Apache NetBeans IDE 11.1 hakkında ayrıntılı bilgi edinmek için sürüm duyurusunu inceleyebilirsiniz.

Continue Reading →

Apache NetBeans IDE 11.1 edinmek için aşağıdaki linkten yararlanabilirsiniz.

0

cURL 7.65.3 duyuruldu

cURL ve liburl adlı iki kısımdan oluşan, özgür ve açık kaynak kodlu bir yazılım olan cURL‘un 7.65.3 sürümü duyuruldu. cURL’un yeni sürümünün arabalar, televizyonlar, yönlendiriciler, yazıcılar, ses ekipmanları, cep telefonları, tabletler, set üstü kutular, medya oynatıcıları gibi platformlarda kullanılan ve milyarlarca insanı her gün etkileyen binlerce yazılım uygulaması için internet üzerinden aktarma omurgası işlevi gören bir yazılım olduğu hatırlatıldı. cURL kütüphanesi; curl_init(), curl_setopt(), curl_exec(), curl_close(), curl_getinfo() gibi fonksiyonlara sahiptir. cURL; FTP , FTPS, HTTP, HTTPS, TELNET, DICT, FILE ve LDAP gibi protokolleri destekler. Pek çok platformda çalışmak üzere tasarlanmış olan libcurl ise ücretsiz , hızlı, IPv6 uyumlu , ve daha pek çok zengin özelliğe sahip olan bir kütüphanedir. cURL; RedHat , Debian , FreeBSD gibi pek çok sistemle birlikte ön tanımlı olarak gelmektedir. cURL 7.65.3 hakkında ayrıntılı bilgi edinmek için değişiklikler sayfasını inceleyebilirsiniz.

Continue Reading →

cURL 7.65.3 edinmek için aşağıdaki linkten yararlanabilirsiniz.

0

cURL 7.65.2 duyuruldu

cURL ve liburl adlı iki kısımdan oluşan, özgür ve açık kaynak kodlu bir yazılım olan cURL‘un 7.65.2 sürümü duyuruldu. cURL’un yeni sürümünün arabalar, televizyonlar, yönlendiriciler, yazıcılar, ses ekipmanları, cep telefonları, tabletler, set üstü kutular, medya oynatıcıları gibi platformlarda kullanılan ve milyarlarca insanı her gün etkileyen binlerce yazılım uygulaması için internet üzerinden aktarma omurgası işlevi gören bir yazılım olduğu hatırlatıldı. cURL kütüphanesi; curl_init(), curl_setopt(), curl_exec(), curl_close(), curl_getinfo() gibi fonksiyonlara sahiptir. cURL; FTP , FTPS, HTTP, HTTPS, TELNET, DICT, FILE ve LDAP gibi protokolleri destekler. Pek çok platformda çalışmak üzere tasarlanmış olan libcurl ise ücretsiz , hızlı, IPv6 uyumlu , ve daha pek çok zengin özelliğe sahip olan bir kütüphanedir. cURL; RedHat , Debian , FreeBSD gibi pek çok sistemle birlikte ön tanımlı olarak gelmektedir. cURL 7.65.2 hakkında ayrıntılı bilgi edinmek için değişiklikler sayfasını inceleyebilirsiniz.

Continue Reading →

cURL 7.65.2 edinmek için aşağıdaki linkten yararlanabilirsiniz.

0