Items filtered by date: December 2014

Sunday, 22 February 2015 22:06

Display server time and date

This code simply display time and date when run.

<?php print date("l dS of F Y h:i:s A"); ?>

 

Published in PHP
Sunday, 22 February 2015 21:21

GrabPHP - Dump the system php.ini

To do a lot of php.ini overides you need to get the system php.ini before you can start but this can be a tricky thing. This script will read and then dump the system php.ini

  1. Alter the code below to fit your server and then create grab.php with the altered code and upload to your website public directory.

    <?
    copy('/usr/local/lib/php.ini', '/home/example/public_html/php.ini');
    ?>
    If you do not know your physical paths you can create a file called info.phpwith the following code, upload it to your root public folder and run it. The script will display all of the required information including the physical path.
    <? phpinfo() ?>
  2. Upload grab.php to your root public folder
  3. Browse to the file and run it. Assuming you loaded it to your root public_html folder then you should browse to
    http://www.example.com/grab.php
  4. This file will create a copy of your system php.ini file and dump it into your root public folder.
  5. Download the new php.ini to your PC
  6. Delete the php.ini and grab.php from your webserver

 

Published in PHP

The following snippets are simple methods of adding HTML and text in to the DOM tree with javascript.

//Add some text in to the variable sometext - note it adds it as text not html - also next section is required
var sometext = document.createTextNode("Z<br /><br />");

// This appends the variable some text to cellRightSel
cellRightSel.appendChild(sometext);

//--

// This adds the text straight on to cellRightSel, no variable required
cellRightSel.appendChild(document.createTextNode('Here is some syndicated content.'));

//-

// didnt work
cellRightSel.appendChild(create('<div>Hello!</div><br /><br />'));

//-

// this makes a variable newElement populated with the following html (this does actually paste the html)
var newElement = '<p id="foo">This is some dynamically added HTML. Yay!</p>';
var bodyElement = cellRightSel;
bodyElement.innerHTML = newElement + bodyElement.innerHTML;

// same as above but shorter and quickr, you can probably alter the order appropiately
cellRightSel.innerHTML = '<p id="foo">This is some dynamically added HTML. Yay!</p>' + cellRightSel.innerHTML;

 

Published in Javascript
Sunday, 22 February 2015 20:54

Flatten Arrays

This is my collection of php snippets that flatten arrays. There are scripts that work on traditional arrays and some that work on objects. I have tried to include where i got them from aswell.

1

// possibly from here - http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html

/**
*
* Convert an object to an array
*
* @param    object  $object The object to convert
* @reeturn      array
*
*/
function objectToArray( $object )
{
	if( !is_object( $object ) && !is_array( $object ) )
	{
		return $object;
	}
	if( is_object( $object ) )
	{
		$object = get_object_vars( $object );
	}
	return array_map( 'objectToArray', $object );				
}

/*** convert the array to object ***/
//$array = objectToArray( $obj );

/*** show the array ***/
//print_r( $array );

2

// not all of these flatteners can handle objects or associative arrays, experiment to find one that works
// i think associated arrays are different, and require different looping


// see - http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array
function flatten(array $array)
{
	$return = array();
	array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
	return $return;
}

// does not flatten array completely, same as above

function array_flatten($array)
{

	$return = array();
	
	   foreach ($array as $key => $value) {
		   if (is_array($value)){ $return = array_merge($return, array_flatten($value));}
		   else {$return[$key] = $value;}
				}
				
	return $return;

}

3

// WORKS
// this works but murders all the keys and flattens them all to strings
// see - http://www.cowburn.info/2012/03/17/flattening-a-multidimensional-array-in-php/	
// works on objects
function jon_flatten ($input)
{
$output = iterator_to_array(new RecursiveIteratorIterator(
new RecursiveArrayIterator($input)), FALSE);

return $output;
}

4

// see - http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php

function flatten_array($array, $preserve_keys = 0, &$out = array()) {
	# Flatten a multidimensional array to one dimension, optionally preserving keys.
	#
	# $array - the array to flatten
	# $preserve_keys - 0 (default) to not preserve keys, 1 to preserve string keys only, 2 to preserve all keys
	# $out - internal use argument for recursion
	foreach($array as $key => $child)
		if(is_array($child))
			$out = flatten_array($child, $preserve_keys, $out);
		elseif($preserve_keys + is_string($key) > 1)
			$out[$key] = $child;
		else
			$out[] = $child;
	return $out;
}

5

// flatten multidimensional arrary into single dimension
// see - http://davidwalsh.name/flatten-nested-arrays-php

function array_flatten($array,$return)
{
	for($x = 0; $x < count($array); $x++)
	{
		if(is_array($array[$x]))
		{
			$return = array_flatten($array[$x],$return);
		}
		else
		{
			if($array[$x])
			{
				$return[] = $array[$x];
			}
		}
	}
	return $return;
}

6

// flattenEachArrayToAnyDepth-------------------//
// see - http://davidwalsh.name/flatten-nested-arrays-php in the coments

//$res = array_flatten($myarray,array());
function array_flatten($array,$return){
	foreach($array as $key => $value){
		if(@is_array($value)){
			$return = $this->array_flatten($value,$return);
		}elseif(@$value){
			$return[$key] = $value;
		}
	}
	return $return;
}

7

// doesnt seem to work properely
// see - http://davidwalsh.name/flatten-nested-arrays-php#comment-56256}

/**
 * Flattens a nested array.
 *
 * Based on:
 * {@link http://davidwalsh.name/flatten-nested-arrays-php#comment-56256}
 *
 * @param array $array     - The array to flatten.
 * @param int   $max_depth - How many levels to flatten.  Negative numbers
 *                           mean flatten all levels.  Defaults to -1.
 * @param int   $_depth    - The current depth level.  Should be left alone.
 */
function array_flatten(array $array, $max_depth = -1, $_depth = 0) {
  $result = array();

  foreach ($array as $key => $value) {
	if (is_array($value) && ($max_depth < 0 || $_depth < $max_depth)) {
	  $flat = array_flatten($value, $max_depth, $_depth + 1);
	  if (is_string($key)) {
		$duplicate_keys = array_keys(array_intersect_key($array, $flat));
		foreach ($duplicate_keys as $k) {
		  $flat["$key.$k"] = $flat[$k];
		  unset($flat[$k]);
		}
	  }
	  $result = array_merge($result, $flat);
	}
	else {
	  if (is_string($key)) {
		$result[$key] = $value;
	  }
	  else {
		$result[] = $value;
	  }
	}
  }

  return $result;
}

8

// WORKS	
// this works but kills the keys
// see - http://davidwalsh.name/flatten-nested-arrays-php
function array_flatten($array, $return=array()) {
	foreach ($array AS $key => $value) {
		if(is_array($value))
		{
		$return = array_flatten($value,$return);
		}
		else
		{
			if($value)
			{
			$return[] = $value;
			}
		}
	}
return $return;

}

9

// Ennio Wolsink

// @Manu: if you replace $return[] = $value; with $return[$key] = $value; you get the preserve the index names of the source array(s). Don’t know if that’s everyone preference, but that was what I was looking for. If one wanted to make this optional, he could add a 3rd optional parameter to this function to indicate wether or not to preserve index names, like so:

function array_flatten($array, $return=array(), $preserve_index_names = false)
{
		foreach ($array AS $key => $value) {
				if(is_array($value)) {
				$return = array_flatten($value,$return, $preserve_index_names);
				}
				else {
					if($value) {
					if($preserve_index_names === false) {
					$return[] = $value;
					}
						else {
						$return[$key] = $value;
						}
					}
				}
		}

		return $return;
}

And then run it like so to get a flattened array back with index names preserved:

$result = array_flatten($input, array(), true);

And like this if you don’t want the index names preserved:

$result = array_flatten($input);

10

// recursively reduces deep arrays to single-dimensional arrays
// $preserve_keys: (0=>never, 1=>strings, 2=>always)
// see - http://www.php.net//manual/en/function.array-values.php
function array_flatten($array, $preserve_keys = 1, &$newArray = Array()) {
  foreach ($array as $key => $child) {
    if (is_array($child)) {
      $newArray =& array_flatten($child, $preserve_keys, $newArray);
    } elseif ($preserve_keys + is_string($key) > 1) {
      $newArray[$key] = $child;
    } else {
      $newArray[] = $child;
    }
  }
  return $newArray;
}

11

// same array_flatten function, compressed and preserving keys.
// see - http://www.php.net//manual/en/function.array-values.php
function array_flatten($a,$f=array()){
  if(!$a||!is_array($a))return '';
  foreach($a as $k=>$v){
    if(is_array($v))$f=self::array_flatten($v,$f);
    else $f[$k]=$v;
  }
  return $f;
}	

 

Published in PHP
Sunday, 22 February 2015 20:38

Dynamically stretch background images

This code allows you to stretch background images when you page expands to provide a better user experience.

Vertically Stretch Background Image

HTML

<link href="/global.css" rel="stylesheet" type="text/css" />
<table width="100%" height="100%" border="0"  cellpadding="0" cellspacing="0">
  <tr>
    <td class="header" colspan="2">&nbsp;</td>    
  </tr>
  <tr>
    <td class="top">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="middle"><img src="/middle.jpg" alt="background image" id="bg" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="bottom" colspan="2">&nbsp;</td>
  </tr>
</table>

CSS

.top {
	width: 130px;
	height: 143px;
	background: black url('top.jpg') left top no-repeat;
	}
.middle {
	width: 130px;
	height: 100%;
	/* background: url('middle.jpg') no-repeat; */
	background-size: 100%;
	}
td.middle img#bg {
	width:100%;
	height:100%;
	}
.bottom {
	height: 59px;
	background: #fff url('bottom.jpg') left top no-repeat;
	}

Although this code is rigged using tables the method can easily be changed to use <div> .

Click here for a demo of this code

Links

Published in HTML
Sunday, 22 February 2015 20:07

Disable Magic Quotes via htaccess

This is a method to disable 'Magic Quotes' when you need to turn them off for such things as joomla. The following instructions are to get your system php.ini file, disable Magic Quotes in this file, and then get the system to load the new file and overide the system php.ini using a command in your .htaccess file.

Please follow the instructions below:

  1. download a copy of the system php.ini (/usr/local/lib/php.ini)
  2. modified the following line
    ; Magic quotes for incoming GET/POST/Cookie data.
    magic_quotes_gpc = On
    To
    ; Magic quotes for incoming GET/POST/Cookie data.
    magic_quotes_gpc = Off
  3. upload the file to somewhere on your webserver, preferably outside of your public_html folders but not required
  4. create/edit your route .htaccess and add the following line which references the new php.ini file
    <IfModule mod_suphp.c>
      suPHP_ConfigPath /home/example/public_html
      <Files php.ini>
        order allow,deny
        deny from all
      </Files>
    </IfModule>
Published in htaccess
Sunday, 22 February 2015 19:05

Overriding the php.ini

There are a couple of ways to overide the PHP settings as defined in /usr/local/lib/php.ini (the system php.ini) and i will list them below.

  • if the specified .ini file does not exist default /usr/local/lib/php.ini is loaded
  • you do not need to use php.ini as the .ini filename, the system php.ini file will still get overidden no matter what name you give it as long as it is correctely specified. The only exception is that when using the 'php.ini in a folder' method, it must be called php.ini
  • in all of these method below you make the changes in the target php.ini file and are basically telling the system to use that instead.
  • when i mention php.ini, this can be replaced with 'Loaded Configuration File'

NB: DO NOT PUT php.ini IN THE PUBLIC_HTML, they can be read and downloaded., only put them in there or any subfolder if they are protected.

php.ini in a folder

You can just place a copy of your modified php.ini file, altered to your needs, in to any folder and all scripts run from that folder will run using the php.ini in the folder, instead of the system php.ini.

  • it takes all settings from the root htaccees file /usr/local/lib/php.ini and then overides them with what is in the new php.ini
  • this will be shown as the loaded php.ini file
  • Some settings can not be overidden with this method.
  • this is not recursive
  • php.ini will not work in directories where SetEnv PHPRC has been used because the 'Loaded Configuration File' has already been set via the htaccess file

suPHP

This is set in the .htaccess file and points to a php.ini of your choice. It is preferable to place this php.ini outside the public_html folders.

  • maintains current php.ini file but appears as the loaded php.ini file used.
  • It only overides what is written in it
  • i dont know if it is recusive

Examples

1 - This is the most basic of the command and will work for most people

suPHP_ConfigPath /home/example/php-folder/php.ini

2 - This example shows how you can put the php.ini in a public_html folder and prevent access to it. The suphp command will also not run unless the module is installed and this can prevent errors upon installation.

<IfModule mod_suphp.c>
  suPHP_ConfigPath /home/example/public_html
  <Files php.ini>
    order allow,deny
    deny from all
  </Files>
</IfModule>

SetENV PHPRC

This is set in the .htaccess file and points to a php.ini of your choice. It is preferable to place this php.ini outside the public_html folders.

  • becomes the loaded php.ini file
  • this method replaces the loaded php.ini, it does not use an overide mechanism
  • it is recursive
  • SetEnv PHPRC only works when using PHP as CGI, not when using php as an Apache Module
  • this method can be used to effectively remove 'disable_functions'

Examples

SetEnv PHPRC /home/example/php-folder/php.ini

Links

This assumes that you webhost company has already sideloaded the appropriate version of PHP you want to use. This is the .htaccess code required to tell the server to use the alternative version.

Sideload alternative version of PHP

Sideloading PHP allows you to have mulitple PHP versions running on the same hosting account/server. You have the default PHP version and then the sideloaded ones which you can access by using an alternative php.ini defined by a .htaccess file. Using suPHP will override the default php.ini recursively.

This code below is put in your .htaccess file and configures the host to use a sideloaded version of PHP, in this case PHP 5.5.

# Custom Legacy PHP 5.5 handler placed by host
    AddType application/x-httpd-php55 .php5 .php4 .php .php3 .php2 .phtml
    suPHP_ConfigPath /usr/local/lib/php55.ini
# End Custom Legacy PHP 5.5 handler placed by host

The target version of PHP needs to be installed by your host provider for this to work.

Published in General

These instructions assume your phone has been unlocked/rooted, if not you should use the cyanogenmod windows installer to do the work for you.

The current ROM I am using is cm-11-20141230-NIGHTLY-i9100 on my Samsung Galaxy S2 i9100 (intl) from http://download.cyanogenmod.org/?device=i9100 and I used GApps from http://slimroms.net/index.php/downloads/dlsearch/viewcategory/1150-addons4-4 - I used the Slim_mini_gapps.4.4.4.build.8.x-385 og

When you install a ROM

When install a ROM it just replaces the /system/ partition (and cyanogen it also does the /boot/ partition) thus all system settings are saved which are stored in /data/

Wiping the system Partition

Does the following

  • seems to do nothing in the sense of when you re-install your ROM, all settings are maintained
  • you need to reconnect your google account, possibly android keeps google account info in the /system/ partition

Notes

General Installation Notes

CM 11 ROM Notes

Other Notes

Android 5.1.1 on Samsung S2 Links

Published in Android
Saturday, 21 February 2015 22:00

How I repaired missing drives on my Samsung S2

The problem

Both my internal and external SDCards dissapered on my Samsung s2. The external SD Card when I removed it and put it in my windows PC it read normally and had no errors.

Cause

I do not know what the exact cause is but possibly one of the following, but you should consider that both of my SD Cards stopped working.

  • Virus
  • Corrupt file system
  • 3rd party App
  • Faulty Hardware

So Basically it appears that the partitions are mounted but the filessytem that is mounted is corrupt so it almost appears as if they are not mounted.

Solution

After days of research I did the following to get my SD Cards to work

External SD Card (sdcard1)

  • I copied the stuff of the card to my windows PC
  • Formatted the card in my windows PC as FAT32 (or whatever its format was)

    If this does not work, change its type FAT32/extFAT and see if it works and then change back again. Only CM11+ supports extFat

Internal Card (sdcard / sdcard0)

This is more tricky because it cannot be removed and this solution is currently the best I could come up with

  • Boot in to clockworkmod(CWM)
  • Format the sdcard via CWM by doing the following:
  1. ‘mounts and storage’
  2. ‘format /sdcard’ (if unsure remove your external SD Card)
  3. Select default (unless you know what your file system is)
  • Do file recovery on the newly visible drive to recover your files
  1. DiskDigger – Use on the phone itself. The free version will recover images and .mp4 to a selectable location ie your external SD Card. Seems Quite Good
  2. Windows Based Recovery – Mount SD Cards as USB Mass Storage
    (possibly run this from CWM), then runt he software

Research

These are my notes on all of the googleing and trying of different solutions

Works (or partially works)

Stuff That Does Not Work

  • Upgrading the installed version of CM/Android – The problem persists exactly the same
  • updating the ROM does not fix the missing internal SD Card issue (cyanogen updates all keep the current files apps etc..)
    useful if you cannot boot you can do a repair install.
  • wiping system partition + re-install of CM
  • these apps make no difference to fixing the issue
    • mount /system (rw / r0)
    • sd card auto remount w/ad
    • sd card check

Stuff That Might Have Merit – But I possibly did wrong!

  • possibly delete the mount points before recreating them

Stuff To Try

UseFul Apps

  • DiskDigger for android – This can be useful for a couple of reasons
    • Can be used to show mount points and their mappings to devices
    • Recover files from damaged partitions. The partitions need to be mounted and the free version does images and mp4 videos. It should be noted it does not recover folder structure
  • DiskUsage – shows you where all your space is being used
  • Diskinfo - this apps shows disk info mounted and unmounted. Excellent for disk diagnostics

Other Notes

Misc

Terminal

The terminal is a powerful tool to be able to help diagnose partion and disk issues

Useful Terminal Commands

  • cd ..   (notice space after cd)
  • umount (notice no 'n')
  • cd /mnt
  • cd mnt
  • mount (with not switches shows the active mounts)
  • cat /proc/partitions – shows partitions
  • df – shows file systems
  • fdisk – perfom actions on the disks and partitions
  • fsck - ? possibly does disk repair, not native to android?
  • dev_mount – device mounting command, might not be readily available in terminal
  • df - lists all partitions
  • mount – (without any switches) show all mount points
  • rmdir
  • rm

Mounting

** add websites where appropriate to my links directory

Published in Android
Saturday, 21 February 2015 21:58

How I flashed my Samsung S2

My Samsung was already rooted so if yours is skip to the next section………

Ways to flash a Samsung

  • Use ODIN from windows to flash a rooted kernel or a full ROM if your phone is not rooted at all. Most Full ROMS will have a rooted kernel
  • If your phone is rooted you can install Mobile ODIN and install a new Kernel throught that.
    ODIN lite – I think only flashes kernels
  • If your bootloader has CWM (clock work mods), you can use this to update the ROM
    newer ROMS tend to need newer CWM to flash them

{Picture of odin here}

In the picture you can see the ROM is cut up into sections

  1. Kernel
  2. Restore
  3. OS
  4. Data
  • ROM sections can be flashed independently
  • The stock kernel has a brick bug in it
  • Odin is samsungs direct flasher software
  • You can use CWM to flash once it is installed on a rooted phone
  • Rooted phone is made by modifying the kernel
  • CWM is flashed into the recovery partition
  • So flashing a ROM can either flash all areas or just the particular segments, but usually all
  • Mobile ODIN is an app for running off the phone itself with similar functionality to ODIN
  • You can flash with clockworkmod (CWM)/ODIN/Mobile ODIN

So I Flashed by

Files I used for a successful flash

  1. Siyah-s2-v6.0b5.tar (brick bug free)
  2. MobileODINLite-v4.00.apk
  3. MobileODIN_FlashKernel_I9100-v2.0.apk

My Phone was already Rooted

  1. Installed mobile ODIN on my phone
  2. Installed the extra addon for Mobile ODIN specific to my phone, on my phone
  3. Copied the kernel, Siyah-s2-v6.0b5.tar, this is the ODIN version of the kernel
  4. Ran Mobile ODIN and allowed permanent root access
  5. Selected the Siyah-s2-v6.0b5.tar file
  6. Flashed

Some other notes?

  • CWM is in the kernel ? Can be in the Kernel or the Recovery ?
  • I got a status 7 error when I tried installing the Siyah-s2-v6.0b5-CWM.zip (CWM version) of the kernel via CWM. This is because the version of CWM was not capable of flashing this file because it was to new. Flashing the ODIN version of the file fixed the installation issue (the author notes this on his website)
  • Can I do a factory reset without loosing my custom ROM
  • A modern CWM is require to flash modern ROMs
  • you can apply updates via the revovery mode if you have already updated it.
  • the kernel gets replace usually with new roms
  • once the dodgy kerenel is replace the brick bug should no longer be an issue
  • this is a non brick bug rom - http://forum.xda-developers.com/showthread.php?t=1555259
  • the cynogenmod does not have GAAPS installed, you have to download them seperate at 96mb

  • files for flashing can be 1 or 3 files. in either case these files will be tar files.
  • for flashing put the phone in download mode, make sure kies is not running
    • if one files, which is a tar, place this in the pda slot see picture
    • if 3 files, which are tar put them in like in the picture
    • then if backup all done, hit flash, this is it

  • wipe custom rom couter and yellow triangle with jig
  • you can flash individual sections ie kernel, csc etc..
  • flashing a full stock rom does not seem to wipe data or super root
  • an insecure kernel is only required once to make the rom rooted
  • factory reset (from off, vol up + home + pwr) gets you into hidden menus which icludes wipe all user data, this wipes all stored programs and user data
  • download mode (from off, vol dwn + home + pwr)
  • S2 Root v1.1 - For Samsung Galaxy S II is simple! Flash insecure kernel then run S2 Root and Press Root Device. My Root method only roots the device and installs busybox. Chainfires method also installs Clockworkmod Recovery.
  • to wipe counter and triangle using jig, power phone of and plug it in until it says complete
  • cwm = Clockworkmod Recovery, ClockworkMod – also known as Clockwork and CWM
  • What Is ClockworkMod Recovery And How To Use It On Android [Complete Guide]
  • odin is samsungs own flashing program

  • Factory/Hard Reset:
    • Recovery mode - Turn phone off, press and hold Vol Up + Home + Power. The Volume keys navigate the menus and Home actions your choice
    • Dialer - enter *2767*3855#
    • Accessing through menu - Settings - Privacy - Factory data reset.
Published in Android
Page 48 of 96