PHP: Quick Un-Zipper

<?php
/**
* Income Pitbull – Quick UnZipper
* Place this in the same directory of the zip file. Then go to the file in your browser, enter the file name in the textbox, and hit “Unzip”.
* For security reasons, this only is allowed to run inside it’s own current directory.
* It is recommended to delete this file after use.
*/
$curdir = dirname(__FILE__) . “/”;
$status = “”;

if ( isset($_POST[‘file’]) )
{

$path = $curdir;
$file = basename($_POST[‘file’]);
$ext = pathinfo($file, PATHINFO_EXTENSION);

if ( (strpos($file, “/”) !== false) || (strpos($file, “..”) !== false) ) {
$status = “<strong>Error:</strong> You must stay within this directory. Slashes and other chars are blocked for security reasons!”;
} elseif ( $ext !== “zip” ) {
$status = “<strong>Error:</strong> The file extension must be a ‘zip’ file!”;
} else {

$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
$status = “<strong>Success:</strong> ‘$file’ extracted to ‘$path’.”;
} else {
$status = “<strong>Error:</strong> Could not extract ‘$file’.”;
}

}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
</head>
<body>

<div style=”border:1px solid black;width:600px; margin: 30px auto; padding:10px 5px 20px 5px; text-align:center;font-family:Arial, Verdana;”>
<h1 style=”font-size:16px;font-weight:900;font-family: Arial, Verdana;text-decoration:underline;”>Income Pitbull Quick Unzipper</h1>
<?php
if ( isset($status) && ! empty($status) ) {
echo ‘<p align=”center”>’ . $status . ‘</p>’;
}
?>
<form name=”unzipform” action=”” method=”post”>
<label for=”file”>Filename:</label>
<input type=”text” name=”file” placeholder=”filename.zip” style=”padding-left:4px;” />
<button>Unzip</button>
</form>

</div>

</body>
</html>

A Simple Server-to-Server File Transfer Script (PHP)

<?php
set_time_limit(0); //Unlimited max execution time
 
$path = 'newfile.zip';
$url = 'http://example.com/oldfile.zip';
$newfname = $path;
echo 'Starting Download!<br>';
$file = fopen ($url, "rb");
if($file) {
	$newf = fopen ($newfname, "wb");
	if($newf)
		while(!feof($file)) {
			fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
			echo '1 MB File Chunk Written!<br>';
		}
}
if($file) {
	fclose($file);
}
if($newf) {
	fclose($newf);
}
echo 'Finished!';
?>

A Simple Server-to-Server File Transfer Script (PHP)

PHP: Recursively Zipping Files & Folders to .zip File

https://gist.github.com/toddsby/f98d82314259ec5483d8

<?php
/*
 * PHP: Recursively Backup Files & Folders to ZIP-File
 * (c) 2012-2014: Marvin Menzerath - http://menzerath.eu
 * contribution: Drew Toddsby
*/

// Make sure the script can handle large folders/files
ini_set('max_execution_time', 600);
ini_set('memory_limit','1024M');

// Start the backup!
zipData('/path/to/folder', '/path/to/backup.zip');
echo 'Finished.';

// Here the magic happens :)
function zipData($source, $destination) {
	if (extension_loaded('zip')) {
		if (file_exists($source)) {
			$zip = new ZipArchive();
			if ($zip->open($destination, ZIPARCHIVE::CREATE)) {
				$source = realpath($source);
				if (is_dir($source)) {
					$iterator = new RecursiveDirectoryIterator($source);
					// skip dot files while iterating 
					$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
					$files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
					foreach ($files as $file) {
						$file = realpath($file);
						if (is_dir($file)) {
							$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
						} else if (is_file($file)) {
							$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
						}
					}
				} else if (is_file($source)) {
					$zip->addFromString(basename($source), file_get_contents($source));
				}
			}
			return $zip->close();
		}
	}
	return false;
}

Creating a WordPress Admin Options Page

I do not like the WordPress Settings API. This tools is very useful for creating WordPress admin settings page with options. You can insert a text, textarea, radio group, checkbox and more fields in your form with this tool.

Plugin/theme name: your_theme_or_plugin_name

: your_prefix (Choose a prefix for the code. E.g. plugin name: WP Settings Ali → wpsa)

: your-textdomain

  • Please change these names with find-replace (CTRL+F)

This is sample code for creating option page for WordPress:

<?php
add_action( ‘admin_menu’, ‘your_prefix_add_admin_menu’ );
add_action( ‘admin_init’, ‘your_prefix_settings_init’ );

function your_prefix_add_admin_menu( ) {

add_options_page( ‘your_theme_or_plugin_name’, ‘your_theme_or_plugin_name’, ‘manage_options’, ‘your_theme_or_plugin_name’, ‘your_prefix_options_page’ );

}

function your_prefix_settings_init( ) {

register_setting( ‘pluginPage’, ‘your_prefix_settings’ );

add_settings_section(
‘your_prefix_pluginPage_section’,
__( ‘Your section description’, ‘your-textdomain’ ),
‘your_prefix_settings_section_callback’,
‘pluginPage’
);

add_settings_field(
‘your_prefix_text_field_0’,
__( ‘Settings field description’, ‘your-textdomain’ ),
‘your_prefix_text_field_0_render’,
‘pluginPage’,
‘your_prefix_pluginPage_section’
);

add_settings_field(
‘your_prefix_text_field_1’,
__( ‘Settings field description’, ‘your-textdomain’ ),
‘your_prefix_text_field_1_render’,
‘pluginPage’,
‘your_prefix_pluginPage_section’
);

add_settings_field(
‘your_prefix_checkbox_field_2’,
__( ‘Settings field description’, ‘your-textdomain’ ),
‘your_prefix_checkbox_field_2_render’,
‘pluginPage’,
‘your_prefix_pluginPage_section’
);

add_settings_field(
‘your_prefix_textarea_field_3’,
__( ‘Settings field description’, ‘your-textdomain’ ),
‘your_prefix_textarea_field_3_render’,
‘pluginPage’,
‘your_prefix_pluginPage_section’
);

}

function your_prefix_text_field_0_render( ) {

$options = get_option( ‘your_prefix_settings’ );
?>
<input type=’text’ name=’your_prefix_settings[your_prefix_text_field_0]’ value='<?php echo $options[‘your_prefix_text_field_0′]; ?>’>
<?php

}

function your_prefix_text_field_1_render( ) {

$options = get_option( ‘your_prefix_settings’ );
?>
<input type=’text’ name=’your_prefix_settings[your_prefix_text_field_1]’ value='<?php echo $options[‘your_prefix_text_field_1′]; ?>’>
<?php

}

function your_prefix_checkbox_field_2_render( ) {

$options = get_option( ‘your_prefix_settings’ );
?>
<input type=’checkbox’ name=’your_prefix_settings[your_prefix_checkbox_field_2]’ <?php checked( $options[‘your_prefix_checkbox_field_2′], 1 ); ?> value=’1’>
<?php

}

function your_prefix_textarea_field_3_render( ) {

$options = get_option( ‘your_prefix_settings’ );
?>
<textarea cols=’40’ rows=’5′ name=’your_prefix_settings[your_prefix_textarea_field_3]’>
<?php echo $options[‘your_prefix_textarea_field_3’]; ?>
</textarea>
<?php

}

function your_prefix_settings_section_callback( ) {

echo __( ‘This section description’, ‘your-textdomain’ );

}

function your_prefix_options_page( ) {

?>
<form action=’options.php’ method=’post’>

<h2>your_theme_or_plugin_name</h2>

<?php
settings_fields( ‘pluginPage’ );
do_settings_sections( ‘pluginPage’ );
submit_button();
?>

</form>
<?php

}

?>

Api Generator Page: http://wpsettingsapi.jeroensormani.com/

[PHP] How to convert array to SimpleXML?

Question: How can I convert an ARRAY to a SimpleXML object in PHP?

This code will convert array of any depth to xml document and works under php 5.2

Array
(
    ['total_stud']=> 500
    [0] => Array
        (
            [student] => Array
                (
                    [id] => 1
                    [name] => abc
                    [address] => Array
                        (
                            [city]=>Pune
                            [zip]=>411006
                        )                       
                )
        )
    [1] => Array
        (
            [student] => Array
                (
                    [id] => 2
                    [name] => xyz
                    [address] => Array
                        (
                            [city]=>Mumbai
                            [zip]=>400906
                        )   
                )

        )
)

Generated XML would be as:

<?xml version="1.0"?>
<student_info>
    <total_stud>500</total_stud>
    <student>
        <id>1</id>
        <name>abc</name>
        <address>
            <city>Pune</city>
            <zip>411006</zip>
        </address>
    </student>
    <student>
        <id>1</id>
        <name>abc</name>
        <address>
            <city>Mumbai</city>
            <zip>400906</zip>
        </address>
    </student>
</student_info>

PHP Code:

<?php
// function defination to convert array to xml
function array_to_xml( $data, &$xml_data ) {
    foreach( $data as $key => $value ) {
        if( is_numeric($key) ){
            $key = 'item'.$key; //dealing with <0/>..<n/> issues
        }
        if( is_array($value) ) {
            $subnode = $xml_data->addChild($key);
            array_to_xml($value, $subnode);
        } else {
            $xml_data->addChild("$key",htmlspecialchars("$value"));
        }
     }
}

// initializing or creating array
$data = array('total_stud' => 500);

// creating object of SimpleXMLElement
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');

// function call to convert array to xml
array_to_xml($data,$xml_data);

//saving generated xml file; 
$result = $xml_data->asXML('/file/path/name.xml');

?>

References:

Documentation on SimpleXMLElement::asXML used in this snippet

Redirect Detective: Yeniden yönlendirme izleyicisi

Redirect Detective sitesi bir yönlendirme olduğunda bunun hangi yolu izlediğini size gösteren basit güzel bir uygulama yapmış.

Redirect Detective is a free redirect checker that allows you to see the complete path a redirected URL goes through.

Ne işe yarar?

  • Affiliate linklerinizi kontrol edebilirsiniz
  • Site taşıma sonrası yönlendirmelerin düzgün çalışmasını kontrol edebilirsinizBir
  • Bir bağlantının zararlı bir adrese yönlenmiş olduğunu o bağlantıyı açmadan görebilirsiniz
  • Kısaltılmış adreslerin gittiği yeri görebilirsiniz. Bit.ly, goog.ly, facebook.com/r.php gibi

Kamp Malzeme Listesi

Kampa gidecekler için bir malzeme listesi faydalı olabilir.

Kamp Malzeme Listesi:
* Kamp Çantası – giysi, çorap, gözlük vs
* Çadır
* Mat + Uyku tulumu
* Kamp Bıçağı + çakı
* Kafa Feneri + pil (+ yedek pil)
* İlk yardım seti: oksijenli su, fucidin krem (antibiyotikli), yara bandı, sargı bezi, ağrı kesici ve diğer ilaçlarımız.
* Muhtelif uzunlukta ipler
* Balta, Kürek, kova
* Tuvalet kağıdı + Sabun + Ayak pudrası. Kalın çorap.
* Güneş kremi + güneş gözlüğü + Şapka
* Matara + Su
* İç çamaşır + yedek tişört + yedek pantolon + çorap
* Kazak + Polar
* Cep telefonu + Powerbank
* Battal çöp poşeti ve market poşetleri
* Yağmurluk, Çadır yağmur bezi.
* El / yüz, plaj havlusu
* Koli Bandı yada ducktape
* Dikiş set
* Mum
* Boyun için polar fular
* Yemek için kap kacak vs
* Çakmak

Yemek için Malzeme Listesi:
* Izgara teli
* Konserve
* Yumuta
* Baharat (tuz – karabiber – kırmızı biber – kimyon)
* Küçük boy patates, soğan, yeşil biber, domatesler
* peynir, zeytin
* Ekmek
* Demlik, Demlik poşet çay + şeker
* Plastik veya ahşap tabak + bardak
* Kamp Ocağı