Wednesday, June 22, 2011

return only normal characters (a-z 0-9) php function using preg_replace

here is a nice simple function that uses preg_replace to clean a string, and make sure it returns only normal characters.

It is another way of cleaning a string, similar to the method i used in Generate Safe File Name in PHP using ereg_replace function.

Not sure really which is better i think one of them may be faster than the other. Please feel free to comment and let me know.

return only normal characters (a-z 0-9) php function using preg_replace

Friday, December 11, 2009

PHP – Automated Creation of Thumbnails

[PHP CODE]

// define the base image dir
$base_img_dir = "./";

// $QUERY_STRING =
// f(3c9b5fa6bc0fa) img_file
// w(123|15%) width of output
// h(123|10%) height of output
// x(123) max width of output
// y(123) max height of output
// t(jpg|png) type of output
// q(100) quality of jpeg

// find tags
preg_match_all("/\+*(([a-z])\(([^\)]+)\))\+*/", $QUERY_STRING,
$matches, PREG_SET_ORDER);

// empty array and set regular expressions for the check
$tags = array();
$check = array( "f" => "[0-9a-zA-Z]{13}",
"w" => "[0-9]+%?",
"h" => "[0-9]+%?",
"x" => "[0-9]+",
"y" => "[0-9]+",
"t" => "jpg|png",
"q" => "1?[0-9]{1,2}" );

// check tags and save correct values in array
for ($i=0; $iif (isset($check[$matches[$i][2]])) {
if (preg_match('/^('.$check[$matches[$i][2]].')$/',
$matches[$i][3])) {
$tags[$matches[$i][2]] = $matches[$i][3];
}
}
}

function notfound() {
header("HTTP/1.0 404 Not Found");
exit;
}

// check that filename is given
if (!isset($tags["f"])) {
notfound();
}

// check if file exists
if (!file_exists($base_img_dir.$tags["f"])) {
notfound();
}

// retrieve file info
$imginfo = getimagesize($base_img_dir.$tags["f"]);

// load image
switch ($imginfo[2]) {
case 2: // jpg
$img_in = imagecreatefromjpeg($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "jpg";
}
break;
case 3: // png
$img_in = imagecreatefrompng($base_img_dir.$tags["f"]) or notfound();
if (!isset($tags["t"])) {
$tags["t"] = "png";
}
break;
default:
notfound();
}

// check for maximum width and height
if (isset($tags["x"])) {
if ($tags["x"] < imagesx($img_in)) {
$tags["w"] = $tags["x"];
}
}
if (isset($tags["y"])) {
if ($tags["y"] < imagesy($img_in)) {
$tags["h"] = $tags["y"];
}
}

// check for need to resize
if (isset($tags["h"]) or isset($tags["w"])) {
// convert relative to absolute
if (isset($tags["w"])) {
if (strstr($tags["w"], "%")) {
$tags["w"] = (intval(substr($tags["w"], 0, -1)) / 100) *
$imginfo[0];
}
}
if (isset($tags["h"])) {
if (strstr($tags["h"], "%")) {
$tags["h"] = (intval(substr($tags["h"], 0, -1)) / 100) *
$imginfo[1];
}
}

// resize
if (isset($tags["w"]) and isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $tags["h"];
} elseif (isset($tags["w"]) and !isset($tags["h"])) {
$out_w = $tags["w"];
$out_h = $imginfo[1] * ($tags["w"] / $imginfo[0]);
} elseif (!isset($tags["w"]) and isset($tags["h"])) {
$out_w = $imginfo[0] * ($tags["h"] / $imginfo[1]);
$out_h = $tags["h"];
} else {
$out_w = $tags["w"];
$out_h = $tags["h"];
}

// new image in $img_out
$img_out = imagecreate($out_w, $out_h);
imagecopyresized($img_out, $img_in, 0, 0, 0, 0, imagesx($img_out),
imagesy($img_out), imagesx($img_in), imagesy($img_in));
} else {
// no resize needed
$img_out = $img_in;
}

// check for a given jpeg-quality, otherwise set to default
if (!isset($tags["q"])) {
$tags["q"] = 75;
}

// returning the image
switch ($tags["t"]) {
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($img_out, "", $tags["q"]);
exit;
case "png":
header("Content-type: image/png");
imagepng($img_out);
exit;
default:
notfound();
}

[PHP CODE]

cant get the dam thing to show properly, grab the source here: Elgene

Monday, November 23, 2009

How to Convert a date from SQL to RFC822 Format

This was quite an annoying issue when trying to manually create a RSS feed from SQL / ASP.

Finally worked out the Statement needed (if your DateTime row name is datetime):



or full example statement:



So just chuck that in your SQL statement, and your good to go.

It will format the date as follows:
Wed, 02 Oct 2009 08:00:00 GMT

Also another handy link i found was here, validates your feed. Very helpful:
feedvalidator.org

Monday, November 9, 2009

This is how you get rid of the rick rolled iphone background, applied by the ikee virus

This one worked for me! Had to also download iphone explorer, so that i didnt have to spend about 30minutes typing into the mobile terminal app, but that one works as well.

Today a friend of mine and my girlfriend's jailbroken iphones were hit with an SSH virus, first the background changes and the phone freezes, upon restart the background is a picture of Rick Astley with the words "ikee is never going to give you up" at the top of the screen, trying to change your background will revert back to this background. I had SSH turned off on my own iPhone and it hasn't hit me.

If you have the "ikee is never going to give you up" virus here is how to get rid of it.

Step 1, change your SSH password, make sure you change the mobile password and root password and don't forget the password lol, do this even if it hasn't happened to you yet as it will stop it ever happening to you.
http://www.redmondpie.com/how-to-secure-yo...h-hack-9140084/

Step 2, to remove the ikee wallpaper you will need to SSH into your phone and delete \var\mobile\Library\LockBackgroung.jpg then reboot. You can probably do this through mobileterminal from step 1 if you know how to use unix, but I don't.

ps. Apparently this has also happened to people who haven't jailbroken, the only way to get rid of it would be to jailbreak and do the above steps, or wipe it back to factory and restore.



via: nonoobs

Sunday, November 8, 2009

Quick Fix for iPhone "ikee is never going to give you up" SSH the ikee virus


Step 1. Change the default mobile and root password for your iphone SSH to become immune to this virus once for all ! The tutorial for this is here: http://www.redmondpie.com/how-to-secure-your-jailbroken-iphone-from-ssh-hack-9140084/

Step 2. Download mobileterminal through Cydia or use Iphone Explorer (www.iphone-explorer.com) for Windows

Step 3. Logon as Root and delete below files:
(1). /var/mobile/Library/LockBackground.jpg
(2). /System/Library/LaunchDaemons/com.ikey.bbot.plist
(3). /bin/poc-bbot
(4). /bin/sshpass
(5). /var/log/youcanbeclosertogod.jpg

Step 4. Set up your favourite wallpaper and reboot your device.

P.S. For those who use mobileterminal to delete files and not familiar with linux command,

a. the command for deleting files is "rm". Example, rm /var/mobile/Library/LockBackground.jpg

b. the linux path is case-sensitive, so make sure you type the path correctly.

Why aren't songs with the same album art grouped together, Was having a problem with syncing music from itunes.

Found this one useful in google cache.
  • Last Modified: August 05, 2008
  • Article: TS1468
  • Old Article: 304389

Symptoms

When viewing the iTunes Library or a playlist, songs from the same album may not be grouped together as expected in Album view when sorted by album.

Album View

Products Affected

iTunes, iTunes 7 or later

Resolution

Solution

There are three reasons why songs from the same album may not be grouped together

  • The album names of the songs may differ slightly
  • The songs from the same album may have different artists
  • Not all songs in the album have the same "Album Artist" assigned to it

Album Names

In the example below the two songs appear to have the same album name, however, if you look closely the first song includes an additional space between the words "Ray" and "of" that the second song does not have.

Slight variations in spacing, spelling, or punctuation of the album name can cause iTunes to consider the two songs to be from separate albums. Be sure that all the songs that are supposed to be from the same album have the exact same album name. One way to easily do this is to select all the songs and choose Get Info from the File menu and retype the album name such that it will apply to all the selected songs.


Albums with various artists

Some albums include songs from multiple artists. Because of this iTunes may display them as separate album as shown below:

You can mark these songs as a compilation in iTunes. Do do this, select all the songs that you want to be included in the compilation and select Get Info from the file menu and select Yes in the Compilation menu as shown below. After clicking OK iTunes group all the selected songs together.


Albums in which all songs don't have the same Album Artist information

iTunes 7 includes a new attribute for song files called Album Artist This allows you to assign a primary artist name for an album that many includes songs by several artists. It is important that all songs in the album have the same Album Artist name. To assign an Album Artist, select all the songs in the album and choose Get Info from the File menu. Next, type a name in the Album Artist field name such that it will apply to all the selected songs. This field can optionally be left blank if all the songs have the same album name. The Album Artist field is shown in the picture above

Additional Information

The solutions above also apply to situations where multiple album covers for the same album are seen in cover flow as shown below

Cover Flow View

Dont get Rick Rolled on you iPhone, Change the Password

If you’re one of the many who are jailbreaking your iPhone to get options such as tethering, make sure you change the root access password once you do.
In addition to your possibly getting Rick-Rolled

Your jailbroken phone could possibly be held for ransom

If you’ve never changed the default device password, now’s the time. Here’s how:

The app to use on the iPhone is called MobileTerminal and it’s available for free in the Cydia store.

Once you have MobileTerminal installed, launch it and you should see a prompt saying this or similar:

iPhoneName: ~ Mobile$

  • At that prompt, type: passwd
  • You’ll be prompted for the ‘old’ (current) password for the mobile user. Enter this as the old password: alpine
  • You’ll then be prompted to enter the new password – so just type in your desired new password. Use good password principles if possible (long and stong). You will not see characters appearing on the screen as you type – that’s normal, not a concern.
  • You’ll then be prompted to re-enter the new password. Do that.
  • You should then be returned to the Mobile$ prompt that you started on when opening the MobileTerminal app. There’s no success message to say the password was changed – but if you’re returned to the prompt and do not get an error, the change was successful. And you’re done with change for the mobile account.
  • The second primary admin account for the iPhone is called root – so now you need to change that as well.
  • Type this to switch to the root user: login root
  • You’ll be prompted for the root user’s current password. Enter this: alpine
  • Type this to start the password change routine again: passwd
  • Enter the old password for root (it is ‘alpine’, same as for the mobile user) and enter your desired new password twice, just as you did for the mobile account
via whatsmypass