Category Archives: Development – PHP

WordPress 3.5 pictures display order problem (with Gallery Plus)

I have been using Gallery Plus 1.4.1 and Lightbox Plus 2.4.6 on WordPress, recently after upgrading to WordPress 3.5,  my newly uploaded photos were totally screwed up with the wrong order. There is no effect on the new “ids” tag because of Gallery Plus, the only way is to disable the plug-in  to use the new features in 3.5. Of course, I don’t want to because Gallery Plus adds the “ref” tag to Lightbox automatically.

Usually, I order the photos by file names when exported from Adobe Lightroom.  If not, use FastStone Image Viewer to do a batch rename.

  1. Use FastStone Image Viewer (Free, open source. Google it)
  2. Tools > Batch Convert Selected Images
  3. Check “Rename” >  Type the Prefix, in my example I type “HardWoodFloor_#####”
  4. Now, upload all the photos to your WordPress post but it does not give you the right order because of the HTML5 or Flash uploader
  5. Edit the post,  write down the ID of your post, in my example it is 7042
  6. Run PhpMyAdmin or other SQL manager and run the following script (change the post_parent ID to your ID)
    1
    2
    3
    
    UPDATE  wp_posts SET menu_order = RIGHT(post_name, 4) 
    WHERE post_mime_type='image/jpeg' AND 
    post_parent=7042 AND post_type='attachment'
  7. All the photos are in the same order of the file name. Fixed!

Assumption:

  1. All your photos are JPG
  2. Your file names are in format  [name]_#####.jpg    (##### is integer)
Posted in Development - PHP | Tagged , , | Leave a comment

Getting JPG from Geovision 8.2 HTTP Server with embedded authentication

Recently I upgraded my home security system Geovision from v6.1 to v8.2 but I found that there is one serious problem. In Geovision v6.1, you can get the JPG from your camera directly using http://serveraName/cam1.jpg. However, this no longer works because they have enforced the authentication. I tried to contact their tech support, they said that the only way to do it is to get the SDK. Okay, WTF… so, I do my own hack.

In fact, if you study v8.2 closely, you notice that you only need to authenticate once and you can access the JPG in the format of http://ServerName/[GUID]/Cam1.jpg. However, the GUID keep changing, such as closing the browser.

Since I am running PHP,  I can get around easily. All I have to do is to “POST” the username/password to the Geovision web server and then parse the GUID from the HTML. Then, I can get the image and do my own resizing and whatever I need to do. Here is the source code:

Download the ZIP file for all source files geovision_v8.2_php_scripts.zip

index.php shows all 4 cameras in JPGs
geovision.php the actual JPG image for specific camera. After authentication, you can get the image directly by using geovision.php?id=0&resize=0&guid=xxxxx
config.php configuration file for your server, username and password
geo_guid.php authenticate by logging on to the server and parse the HTML by returning the GUID

File #1: index.php

<html>
<body>
 
<? 
// ---------------------------------------------------------------------
// Authenticate Geovision HTTP web server and show the JPGs of cameras
// with resizing feature in geovision.php, you can customize it for 
// showing it on mobile phone
//
// Written and Copyrighted by: Mythos and Rini
// Created on: 2010-09-12
//
// Required: Geovision version 8.2 
//           PHP with CURL and GD library installed
//
// Notes: Change the Server name, Username, Password in config.php
// ---------------------------------------------------------------------
 
include("config.php");
include("geo_guid.php");
 
?>
 
 
Geovision GUID: <? echo $geo_guid; ?>
 
<br/><br/>Cam 1<br/>
<img border="0" src="geovision.php?id=0&resize=0&guid=<?=$geo_guid?>&time=<?=time()?>">
<br/><br/>Cam 2<br/><br/>
<img border="0" src="geovision.php?id=1&resize=0&guid=<?=$geo_guid?>&time=<?=time()?>">
<br/><br/>Cam 3<br/><br/>
<img border="0" src="geovision.php?id=2&resize=0&guid=<?=$geo_guid?>&time=<?=time()?>">
<br/><br/>Cam 4<br/><br/>
<img border="0" src="geovision.php?id=3&resize=0&guid=<?=$geo_guid?>&time=<?=time()?>">
 
 
</body>
</html>

File #2: config.php

<?
// ---------------------------------------------------------------------
// Configuration of your Geovision Server/port and Username/Password
//
// Written and Copyrighted by: Mythos and Rini
// Created on: 2010-09-12
//
// Required: Geovision version 8.2 
//           PHP with CURL and GD library installed
//
// ---------------------------------------------------------------------
 
define("GN_GEOVISION_CAMERA", "ServerName:Port");
define("GN_GEOVISION_USER", "UserName");
define("GN_GEOVISION_PASSWORD", "Password");
 
 
?>

File #3: geo_guid.php

 
<?
 
// ---------------------------------------------------------------------
// This file uses CURL to logon to Geovision HTTP web server and then
// Parse the HTML using the PHP XML parser, return the GUID
//
// Written and Copyrighted by: Mythos and Rini
// Created on: 2010-09-12
//
// Required: Geovision version 8.2 
//           PHP with CURL and GD library installed
//
// Notes: Change the Server name, Username, Password in config.php
// ---------------------------------------------------------------------
 
 
$url = 'http://' . GN_GEOVISION_CAMERA . '/webcam_login'; 
$ch = curl_init($url);
 
//
// In Geovision HTML form, it takes 2 HTML form elements "id" and "pwd"
// Also, ImageType = 2 - JPG
// 
 
 curl_setopt ($ch, CURLOPT_POST, 1);
 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // return as string instead of showing to screen
 curl_setopt ($ch, CURLOPT_POSTFIELDS, "id=" . GN_GEOVISION_USER ."&pwd=" . GN_GEOVISION_PASSWORD."&ImageType=2");
 
 $output = curl_exec($ch);
 
 
//Create a new DOM document
$dom = new DOMDocument;
 
//Parse the HTML. The @ is used to suppress any parsing errors
//that will be thrown if the $html string isn't valid XHTML.
@$dom->loadHTML($output);
 
//Get all links. You could also use any other tag name here,
//like 'img' or 'table', to extract other tags.
$nodes = $dom->getElementsByTagName('title');
 
 
$nodeListLength = $nodes->length; // this value will also change
 
$node = $nodes->item(0);
 
// Since there should be ONLY one element returns, that's the GUID
$geo_guid = $node->nodeValue;
 
 
// Close handle
curl_close($ch);
 
?>

File #4: geovision.php

 
<? 
// ---------------------------------------------------------------------
// This file show individual image from Geovision HTTP web server
//
// Written and Copyrighted by: Mythos and Rini
// Created on: 2010-09-12
//
// Required: Geovision version 8.2 
//           PHP with CURL and GD library installed
//
// Notes: Change the Server name, Username, Password in config.php
// ---------------------------------------------------------------------
 
include("config.php");
 
 
$id = $_GET["id"];
$resize = $_GET['resize'];
$guid = $_GET['guid'];
 
$my_host = GN_GEOVISION_CAMERA;
 
header("Content-type: image/png"); 
 
 
			// Read  and send it to the client browser
				$my_url = "http://" . $my_host  . "/". $guid . "/cam" . $id . ".jpg";
 
			if ($resize == 1)
			{
 
 
				$src_img = @imagecreatefromjpeg($my_url); 
 
				// get old size of the image
				$old_w = imagesx($src_img);
				$old_h = imagesy($src_img);
 
				// new size for cell phone  // 160 x 120   radio 1.33333 (100x75)
 
				$new_w = 160;
				$new_h = 120;
 
				// resize the image
				$dst_img = imagecreatetruecolor ($new_w,$new_h);
				imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,$old_w,$old_h);
 
				imagepng($dst_img);
				imagedestroy($dst_img);	
 
			}
			else 
			{
 
				$fn=fopen($my_url, "r"); 
				fpassthru($fn); 
				fclose($fn);
 
			}	
 
 
?>
Posted in Development - PHP, Network/Hardware | Tagged , , , | 4 Comments

WordPress Cleanup .thumbnail on database

WordPress introduced Gallery concept from v2.5.x, and it is great for new postings. However if you use Gallery on some old articles prior to v2.5.x, you will run into problems because it doesn’t have the new format of the thumbnails generated. Also, it makes the page load so slowly because it is using the full size images as thumbnails. Even worse, if you delete the images from Media Library, it doesn’t delete the files physically.

The workaround is to re-upload the images, and the following script helps to clean up WP_POSTS and WP_POSTMETA tables for specific article:

Get the post and meta from the specific article

SET @postid = 1234;
 
SELECT * FROM wp_posts
WHERE post_parent = @postid and post_type = 'attachment'
 
select * from wp_postmeta
where post_id in
(SELECT ID FROM wp_posts
WHERE post_parent = @postid and post_type = 'attachment')

Delete the post and meta for the specific article

SET @postid = 1234;
 
DELETE from wp_postmeta
where post_id in
(SELECT ID FROM wp_posts
WHERE post_parent = @postid and post_type = 'attachment')
 
DELETE FROM wp_posts
WHERE post_parent = @postid and post_type = 'attachment'
Posted in Development - PHP | Leave a comment

WassUp 1.7.x never able to log anything – ERROR 1005: Can’t create table

I was using old version WassUp on WordPress and it has been working fine, recently due to WP upgrade, I had to upgrade WassUp to 1.7.x. However, it does not work at all – simply it does not record anything.

So, I traced the code and checked the database, and nailed down to MySQL table. For WassUp to work, there suppose to be 2 tables ‘wp_wassup’ and ‘wp_wassup_tmp”.  The former one was missing from my database.

Root of problemERROR 1005: Can’t create table

When I tried to manually run the CREATE TABLE script (taken it from wassup.php), it gave me  “ERROR 1005: Can’t create table (errno: 121) ” ??  I looked for help on Internet but couldn’t find the answer, some experts said it could be the internal dictionary got same name recorded that was created previously, in other words MySQL was screwed up.  As I am not a DBA, so I don’t waste time to fix it.

My way of workaround

 

1. Deactivate the wasssup Plugin first (very important)

2. Edit the files and Search for “$wpdb->prefix
look for $wpdb->prefix . “wassup” (not “wassup_tmp”)

/wassup.php  (6 of them)
/lib/main.php (2 of them)
/lib/action.php (only 1)
/lib/wassup_class.php (only 1)

3. Replace “wassup” with “wassup_log”

$wpdb->prefix . “wassup_log”;

4. Go to your PhpMyAdmin, and DROP exiting “wp_wassup” and   “wp_wassup” table

5. Go to MySQL, run this query:

delete from wp_options
where option_name = ‘wassup_settings’

6. Active it and now you should get 2 tables created and ready for logging!
Notes: If it only records to _tmp table but not the main table,  De-activate the plug-in and Re-activate again to see if it works.

My Theory

There are always multiple ways of getting from point A to point B, my way may not be the best but it works.

Posted in Development - PHP, Network/Hardware | Tagged , | Leave a comment

Sessionless PHP Captcha

I was looking for PHP Captcha and found one called “PHP Captcha Security Images” written by Simon Jarvis from UK. In general, this is a pretty good Captcha script. However, when I ran it, it didn’t work for me because my project involved Cross-Domain Iframe (i.e. the page that runs the Captcha is running on a remote server and it’s not the same domain of the parent page) and of course the Session got lost.  This happens on IE 7.0.

To overcome this problem, I modified PHP Captcha and broken it down into 2 PHP files and removed Session:

1. captcha_code.php   – to generate the security code in plain text to be encrypted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
/*
* Original Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
*
* Modified by: Mythos and Rini
* Date: 2007-09-10
* Description: Modified the code and remove use of Session 
*
*/
 
class CaptchaCode
{
	function generateCode($characters) 
	{
		/* list all possible characters, similar looking characters and vowels have been removed */
		$possible = '23456789bcdfghjkmnpqrstvwxyz';
		$code = '';
		$i = 0;
		while ($i < $characters) 
		{ 
			$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
			$i++;
		}
		return $code;
	}
 
}

2. captcha_images.php – to generate the image by taking an encrypted code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
 
/*
* Original Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
*
* Modified by: Mythos and Rini
* Date: 2007-09-10
* Description: Modified the code and remove use of Session 
*
*/
 
 
include("shared.php"); 
 
 
class CaptchaSecurityImages {
 
   var $font = 'monofont.ttf';
   function GenerateImage($width='120',$height='40', $code)  {
      ........		
   }
}
 
$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$code = isset($_GET['code']) ? $_GET['code'] : '';
 
if (isset($_GET['code'])) 
{
$captcha = new CaptchaSecurityImages();
$captcha-> GenerateImage($width,$height,str_decrypt($code));
}

By using RC4 encryption, the secured code is passed to the image generating php for showing the image (prevent View Source). On the form, just compare the decrypted code and the user imput to see if they match.  No more use of Session!

DOWNLOAD the Source Code (licensed under GPL)

Note: You must modify the “KEY_FOR_RC4″ in shared.php to any random string, since this file is open for public to download, the captcha can be easily decrypted.

Posted in Development - PHP | 2 Comments