Posts Tagged Programming

SuzanneHow to separate the trackbacks and pingbacks from comments?

Thursday, February 18th, 2010

Comments on blogs are often criticized as lacking authority, since anyone can post anything using any name they like: there’s no verification process to ensure that the person is who they claim to be. Trackbacks and Pingbacks both aim to provide some verification to blog commenting.

But having trackbacks, pingbacks and comments all mixed up in one place can get really messy and confusing. Let’s separate the trackbacks and the pingbacks from comments-
<strong><?php if ($comments) : ?>
<ol>
<?php foreach ($comments as $comment) : ?>
<li id=”comment-<?php comment_ID() ?>” class=’commentItem’>
<!– THE COMMENT LAYOUT –>
</li>
<?php endforeach; /* end for each comment */ ?>
</ol>
<?php endif; ?></strong>
Above we can see a stripped version of an ordinary comment loop which will show comments, trackbacks and pingbacks all in one place. Let’s change that.
<strong><?php if ($comments) : ?>
<ol>
<?php foreach ($comments as $comment) : ?>
<?php
$commentType = get_comment_type();
if($commentType == ‘comment’) :
?>
<li id=”comment-<?php comment_ID() ?>” class=’commentItem’>
<!– THE COMMENT LAYOUT –>
</li>
<?php endif;/* end if comment check */ ?>
<?php endforeach; /* end for each comment */ ?>
</ol>
<?php endif; ?></strong>
get_comment_type is a function that returns “comment”, “trackback” or “pingback” depending what type the current comment is.
Now just copy paste the same code again and just change “if($commentType == ‘comment’)” toif($commentType != ‘comment’) which is the opposite of “==” and change the class from“commentItem” to “trackbackItem” so you can easily make different styles.
<strong><?php if ($comments) : ?>
<ol>
<?php foreach ($comments as $comment) : ?>
<?php
$commentType = get_comment_type();
if($commentType != ‘comment’) :
?>
<li id=”comment-<?php comment_ID() ?>” class=’trackbackItem’>
<!– THE PINGS LAYOUT –>
</li>
<?php endif;/* end if NOT comment check */ ?>
<?php endforeach; /* end for each comment */ ?>
</ol>
<?php endif; ?></strong>
Making the layout for pingbacks/trackbacks is very simple, there is only 1 function we are going to use, comment_author_link(). It just echoes the trackback/pingbacks link and that’s all we need.
<strong><li id=”comment-<?php comment_ID() ?>” class=’trackbackItem’>
<?php comment_author_link(); ?>
</li></strong>

Having trackbacks, pingbacks and comments all mixed up in one place can get really messy and confusing. Let’s separate the trackbacks and the pingbacks from comments-

<?php if ($comments) : ?>

<ol>

<?php foreach ($comments as $comment) : ?>

<li id=”comment-<?php comment_ID() ?>” class=’commentItem’>

<!– THE COMMENT LAYOUT –>

</li>

<?php endforeach; /* end for each comment */ ?>

</ol>

<?php endif; ?>

Above we can see a stripped version of an ordinary comment loop which will show comments, trackbacks and pingbacks all in one place. Let’s change that.

<?php if ($comments) : ?>

<ol>

<?php foreach ($comments as $comment) : ?>

<?php

$commentType = get_comment_type();

if($commentType == ‘comment’) :

?>

<li id=”comment-<?php comment_ID() ?>” class=’commentItem’>

<!– THE COMMENT LAYOUT –>

</li>

<?php endif;/* end if comment check */ ?>

<?php endforeach; /* end for each comment */ ?>

</ol>

<?php endif; ?>

get_comment_type is a function that returns “comment”, “trackback” or “pingback” depending what type the current comment is.

Now just copy paste the same code again and just change “if($commentType == ‘comment’)” toif($commentType != ‘comment’) which is the opposite of “==” and change the class from“commentItem” to “trackbackItem” so you can easily make different styles.

<?php if ($comments) : ?>

<ol>

<?php foreach ($comments as $comment) : ?>

<?php

$commentType = get_comment_type();

if($commentType != ‘comment’) :

?>

<li id=”comment-<?php comment_ID() ?>” class=’trackbackItem’>

<!– THE PINGS LAYOUT –>

</li>

<?php endif;/* end if NOT comment check */ ?>

<?php endforeach; /* end for each comment */ ?>

</ol>

<?php endif; ?>

Making the layout for pingbacks/trackbacks is very simple, there is only 1 function we are going to use, comment_author_link(). It just echoes the trackback/pingbacks link and that’s all we need.

<li id=”comment-<?php comment_ID() ?>” class=’trackbackItem’>

<?php comment_author_link(); ?>

</li>

http://www.all1Press.com

http://www.all1martpro.com

Tags: , , , ,
Posted in Opensource, Purely Technical | 2 Comments »

NaggieHow can you reduce writing foreach() cycles?

Thursday, February 18th, 2010

Php 5.3 – helps you to reduce writing foreach() cycles

At least in php. Php 5.3 has a solution that will reduce the average number of iteration structures you need to write: closures applied by plain old array functions.

There are some array functions which have already been supported at least from Php 4, and that take as an argument a callback whose formal parameters have to be one or two elements of the array. Let’s talk about array_map(), array_reduce(), array_filter() and uasort() (or similar custom sorting function.) These functions abstract away a foreach() cycle by applying a particular computation to all the elements of an array.

Back in Php 4 and Php 5.2, specifying a callback was cumbersome: you had to define an external function and then passing its name as a string; or passing an array containing an object or the class name plus the method name in case of a public (possibly static) method.

In Php 5.3, callbacks may also be specified as anonymous functions, defined in the middle of other code. These closures are first class citizens, and are treated as you would treat a variable, by passing it around as a method parameter. While I am not a fan of mixing up object-oriented and functional programming, closures can be a time saver which capture very well the intent of low-level processing code, avoiding the foreach() noise.

// obviously only the prime numbers less than 20
$primeNumbers = array(2, 3, 5, 7, 11, 13, 17, 19);

// array_map() applies a function to every element of an array,
// returning the result
$square = function($number) {
return $number * $number;
};
$squared = array_map($square, $primeNumbers);
echo “The squares of those prime numbers are: “,
implode(’, ‘, $squared), “\n”;

// array_reduce() applies a function recursively to pair
// of elements, reducing the array to a single value.
// there is the native array_sum(), but the application of
// a custom function is the interesting part
$sum = function($a, $b) {
return $a + $b;
};
$total = array_reduce($primeNumbers, $sum);
echo “The sum of those prime numbers is “, $total, “.\n”;

// array_filter() produces an array containing
// the elements that satisfy the given predicate
$even = function($number) {
return $number % 2 == 0;
};
$evenPrimes = array_filter($primeNumbers, $even);
echo “The even prime numbers are: “,
implode(’, ‘, $evenPrimes), “.\n”;

// uasort() customize the sorting by value,
// maintaining the association with keys
// there is the native asort(), but again the customization
// of the function is more interesting
$compare = function($a, $b) {
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
};
uasort($primeNumbers, $compare);
echo “The given numbers in descending order are: “,
implode(’, ‘, $primeNumbers), “.\n”;

http://www.all1Press.com

http://www.all1tunes.com

http://www.all1sourcetech.com

Tags: , , , , ,
Posted in Editorial, Purely Technical | No Comments »

SuzanneWhat is a fork in computer programming?

Thursday, February 18th, 2010

fork() is a concept that originated in the UNIX world. Running programs are called processes, and the only way to execute a new program is for a current program to fork() itself, thereby creating a seperate process. The process that called fork() is known as the parent process, and the newly created process is known as the child process.

The child process, which begins its life as a copy of the parent process, can be “replaced”.
In UNIX, the first program that is executed after booting finishes is the init process. This process will fork() and load the getty program over the child process, thereby creating a process which will prompt the user for a password.

Since a call to fork() creates a child process which is in essence the same as the parent process, it is necesarry to determine (from within the process) which process is the child, and which is the parent.

Upon succesfull execution, the fork() call returns the child’s Process ID in the parent process, and a 0 in the child process.

You can then use the child to execute a system call, such as running another program (or whatever you needed a second process for).

If you are using pipes (for communication between the child and parent process), make sure you have the parent wait for it.

http://www.all1martpro.com

http://www.all1sourcetech.com

Tags: , , , , , ,
Posted in Opensource, Unix | No Comments »

NaggieZero-day hack of Oracle 11g database revealed

Tuesday, February 9th, 2010

On Tuesday, a well-known security researcher showed how to subvert security in the Oracle 11g database by exploding zero-day vulnerabilities that would let a savvy user gain full and complete control.

David Litchfield, a researcher at NGS Consulting, demonstrated how a user can subvert security to elevate his privileges to take complete control over Oracle 11g. Altogether Litchfield announced this was his final day at NGS, saying he was considering changing his focus to computer forensics.

Researcher claims hack of processor used to secure Xbox 360, other products

Currently reported, Litchfield’s discovery shows that due to the way Java has been implemented in Oracle 11g Release 2, there’s an overly permissive default grant that makes it possible for a low privileged user to grant him arbitrary permissions. In a demo of Oracle 11g Enterprise Edition, he showed how to execute commands that led to the user granting himself system privileges to have “complete control over the database.” Litchfield also showed how it’s possible to bypass Oracle Label Security used for managing mandatory access to information at different security levels.

Until Oracle remedies the zero-day flaws he exposed, Litchfield advised Oracle 11g administrators to revoke public execute access to certain Java-based functions. He said he expects Oracle to soon release patches for the problems he identified and he intends to publish a white paper on the topic.

According to Litchfield, he thinks Oracle probably deserves a “B+” for security in the current version of its database, which he characterized as an improvement over the previous version, but criticized Oracle for not finding these problems in the requirements and design phases of the product. He added Oracle appears to be relying too much on security tools to catch problems after its product is shipped.

Tags: , , , , , , ,
Posted in New Product Release, Technical News | No Comments »