/** This function is used to debug variables during page execution
* @param $what , the variable which need to debug,can be array or single variable
* @param $more , to know the method and line number where that variable is used
* @param $die , set TRUE if want to stop the page
* how to use : debug::watch($postarray,1,1);
* @author keshav mohta
* @date Aug 12, 2010
*/
class debug()
{
static function watch($what,$more=FALSE,$die=FALSE)
{
$d = debug_backtrace();
if($more && isset($d[1]))
{
echo "<br/>METHOD:".$d[1]['class']."::".$d[1]['function'];
echo "<br/>LINE:".$d[0]['line'];
}
echo "<br/>";
if( is_array($d[0]['args'][0]) || is_object($d[0]['args'][0]) )
{
echo "DATA:<pre>";
print_r($d[0]['args'][0]);
echo "</pre>";
}
else
{
echo "Data:".$d[0]['args'][0];
}
if($die)
{
echo "<br/>";
die('ends here');
}
}
}
<?php
// ------------------------------------------------------------------
// - Database class'es -
// ------------------------------------------------------------------
/**
* Main database class
* Works as a layer over PDO. Every static call is forwarded to the PDO object, except for those which are defined
*
**/
class DB
{
protected static $DB;
private function __construct() {}
private function __clone() {}
public static function connect($dbname='default', $host='localhost', $user='root', $password='')
{
try {
// connects to the database
self::$DB = new PDO("mysql:dbname=$dbname;host:=$host" , $user , $password);
// set the error reporting attribute
self::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public static function close_connection()
{
self::$DB = NULL;
}
public static function __callStatic($name, $arguments)
{
return forward_static_call_array(array(self::$DB, $name), $arguments);
}
}
/**
* A test class
* Using SQLite with 'sqlite::memory:' so that its no need for username, password, etc
* Perfect for quick testing purpose
*
**/
class DBtest extends DB
{
public static function connect()
{
try {
self::$DB = new PDO("sqlite::memory:"); // connect to database
echo 'database created in memory <br /> <br />'; // only at debug mode - @TODO: remove it
self::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the error reporting attribute
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
}
// ------------------------------------------------------------------
// - Initilize a standard connection -
// ------------------------------------------------------------------
include 'config.php';
$dbh::connect( $config['db']['default']['dbname'],
$config['db']['default']['host'],
$config['db']['default']['username'],
$config['db']['default']['password']
);
?>
<?php if (is_user_logged_in() ) { //only logged in user can see this ?>
<div><p>You are logged in!</p></div>
<?php } ?>
<?php if ( current_user_can( 'delete_others_posts' ) ) { //only admins and editors can see this ?>
<p>You are an admin</p>
<?php } else { ?>
<p>You are not an admin</p>
<?php } ?>
<?php
global $post; $thispage = $post->ID; // grabs the current post id from global and then assigns it to $thispage
$subpages = get_pages("child_of=".$thispage."&sort_column=menu_order"); // gets a list of pages that are sub pages of $thispage and assigns it to $pagekids
?>
<?php if ($subpages) { // if there are any values stored in $pagekids, meaning there are sub-pages of the current page ?>
<ul class="links">
<?php wp_list_pages("depth=1&title_li=&sort_column=menu_order&child_of=".$thispage); // display ONLY the sub pages of the current page ?>
</ul>
<?php } else { // $pagekids is empty ?>
<ul class="links">
<?php wp_list_pages('depth=1&title_li=&child_of='.$post->post_parent.'&sort_column=menu_order'); // display the sub-pages of the current parent page ?>
</ul>
<?php } ?>
# return 4 urls, with and without trailing slash, with and without www
# useful for matching urls passed as params into some function,
# "does this url exist?"... need to make sure you check permutations
def url_permutations(url)
url, params = url.split("?")
# without_trailing_slash, with www
a = url.gsub(/\/$/, "").gsub(/http(s)?:\/\/([^\/]+)/) do |match|
protocol = "http#{$1.to_s}"
domain = $2
domain = "www.#{domain}" if domain.split(".").length < 3 # www.google.com == 3, google.com == 2
"#{protocol}://#{domain}"
end
# with_trailing_slash, with www
b = "#{a}/"
# without_trailing_slash, without www
c = a.gsub(/http(s)?:\/\/www\./, "http#{$1.to_s}://")
# with_trailing_slash, without www
d = "#{c}/"
[a, b, c, d].map { |url| "#{url}?#{params}"}
end
puts url_permutations("http://google.com/search?q=http://google.com").inspect
#=> [
"http://www.google.com/search?q=http://google.com",
"http://www.google.com/search/?q=http://google.com",
"http://google.com/search?q=http://google.com",
"http://google.com/search/?q=http://google.com"
]