« Linux | Main | Security »

Friday, June 03, 2011

Guessing Game v1.0

This brings back memories. It's a python recreation of old guessing game in qbasic. Computer guesses random number from 1 to 10. You have 3 chaces to guess it right. (Download)

#!/usr/bin/env python
import sys
from random import randint
play=1
while (play):
	chances = 3
	computer=randint(1,10)
	print "++Guessing Game v0.1 by EmErgE++"
	print "I've guessed a number between 1 to 10. You have 3 chances to guess."
	while (chances):
		myguess=int(raw_input("Enter your guess:"))
		if myguess < computer:
			print "Your guess is less than mine."
		elif myguess > computer:
			print "Your is greater than mine."
		else:
			print "Bravo! We both guessed",computer," XDD"
			break
		chances = chances - 1
	else:
		print "Oops, your chances are over! I guessed ",computer
	cont=raw_input("Do You want to play again? (y/n)")
	if cont=="n":
		play=0
else:
	print "Game Over."
Posted by EmErgE at 12:37 PM
Categories: Programming

jQuery + PHP Multiple File Upload v1.0 by EmErgE

<?php
## Jquery + PHP Multiple File Upload Script v1.0 by EmErgE  released under GNU General Public License Version 2.0
## This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License 
## as published by the Free Software Foundation; version 2 of the License.

session_start();

if (!isset($_SESSION['imagelist'])) $_SESSION['imagelist'] = array();

$errormsg = "";
define ('MAX_FILE_SIZE', 1024000);
define('UPLOAD_DIR', 'uploads/');
if (!is_dir(UPLOAD_DIR)) {
	mkdir(UPLOAD_DIR, 0755);
}
touch(UPLOAD_DIR."index.html");


$image_extensions_allowed = array('jpg', 'jpeg', 'png', 'gif','bmp');
$mime_allowed = array('image/jpg', 'image/jpeg', 'image/pjpeg','image/png', 'image/gif','image/bmp');

if (isset($_POST['Send'])) {
	while(list($key,$value) = each($_FILES[fileX][name]))
	{
		if(!empty($value)){
			$filename = basename($value);
			$filename=str_replace(" ","_",$filename);
			$ext = strtolower(substr($filename, strrpos($filename, '.') + 1));
			if( ($_FILES[fileX][size][$key] > 0) && ( $_FILES[fileX][size][$key] <= MAX_FILE_SIZE ) && ( in_array($_FILES[fileX][type][$key],$mime_allowed) ) && (in_array($ext, $image_extensions_allowed)) )
			{
				$file_info = getimagesize($_FILES[fileX][tmp_name][$key]);
				if(!empty($file_info)) {
					$add = UPLOAD_DIR."$filename";
					if (!move_uploaded_file($_FILES[fileX][tmp_name][$key], $add)) unlink($_FILES[fileX][tmp_name][$key]);
					chmod("$add",0777);
					$_SESSION['imagelist'][] = "$add";			
				} else $errormsg .="Empty File Information. ";

			} else $errormsg .="Unknown Extension. ";
		} //else $errormsg .="Empty File. ";
	}
}

?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"><head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<title>Jquery + PHP Multiple File Upload Script v1.0 by EmErgE v1.0 released under GPLv2</title>

<style type="text/css">
.remove:hover{
	background: #DDD;
}
.remove{
	color: #F00;
	font-size: 15px;
	font-weight: bold;
}
</style>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>	

<script type="text/javascript">

$(document).ready(function() {

var MaxNumber = 3;
$("input.upload").change(function(){
	validateFile(this, MaxNumber);
});

	 
function validateFile(myelement, maxAllowed) {
	if($('input.upload').size() > maxAllowed) {
		$("#errormsg").html("Files count exceeded Maximum Allowed Number of "+maxAllowed);return true;
	}

	$(myelement).hide();
	$(myelement).parent().prepend('<input type="file" class="upload" name="fileX[]" />').find("input").change(function() {validateFile(this, maxAllowed)});
	var elementval = myelement.value;
	if(elementval != '') {
		$("#queue").append('<div>'+elementval+'&nbsp;&nbsp;<a class="remove">X</a></div>').find("a").click(function(){
			$(this).parent().remove();
			$(myelement).remove();
			return true;
		});
	}
};

});

</script>
</head>
<body>

<?php if(!empty($errormsg)) { echo "<span style='color: f00'>$errormsg</span>"; } ?>
<span id="errormsg" style="color: #f00"></span>
Upload Image - Maximum Number of Files: 3 &amp; Maximum Size: 1Mb
<form method=post action="<?php $_SERVER['PHP_SELF']; ?>" enctype='multipart/form-data' id="myform">
<input type="file" class="upload" name="fileX[]" />
<input type="hidden" name="MAX_FILE_SIZE" value="1048000" />
<input type=submit name="Send" value="Upload">
</form>
<div id="queue" class="queue"></div>

<?php
if (!empty($_SESSION['imagelist'][0])) {
	echo "Thank You For Uploading:<br>";
	foreach ($_SESSION['imagelist'] as &$value) {
		echo "<img src='$value' />";
	}
}
?>

</body>
</html>
Posted by EmErgE at 12:37 PM
Categories: Programming

Thumbnail Generator

<?php
/*
Thumbnail Generator by EmErgE :: irc.securitychat.org

This php code will generate specific number of vertical and horizontal tables
with specifi number of columns and rows per cell. This can be used to generate
image thumbnails by replacing the * with <img> tag.

Sample Output 1:
	Column per cell: 5
	Rows Per cell: 5
	Horizontal Tables: 2
	Vertical Tables: 3

  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|

  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|

  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|
  |*_|*_|*_|*_|*_|  |*_|*_|*_|*_|*_|

Sample Output 2:
	Column per cell: 2
	Rows Per cell: 2
	Horizontal Tables: 8
	Vertical Tables: 4

  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|
  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|

  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|
  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|

  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|
  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|

  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|
  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|  |*_|*_|

 
*/  
$columns_per_cell=2;
$rows_per_cell=2;
$horizontal_tables =3;
$vertical_tables = 3;

$columncount = $columns_per_cell * $horizontal_tables;
$rowcount = $rows_per_cell * $vertical_tables;
$endvalue = $columncount * ($vertical_tables-1);

$colors=array("red","green","blue","green","orange","gray","brown");
for ($i=0; $i<=$endvalue; $i=$i+$columncount) {
	for ($k = 1; $k <= $rows_per_cell; $k++) {
		$mycolorindex = array_rand($colors);
		$mycolor = $colors[$mycolorindex];
		echo "<font color='".$mycolor."'>";
	
		for ($j=$i; $j <= ($i+$columncount-1); $j++){
			if(!($j%$columns_per_cell)) echo "&nbsp;&nbsp;|";
			echo "*_|";
		}
		echo "</font>";
		echo '<br>';	
	}
		echo "<br> ";
}
echo '<br>';

?>
Posted by EmErgE at 12:37 PM
Categories: Programming

Thursday, January 14, 2010

Image Streaming with jQuery


Download This Page
<!-- Most IP based security cameras come equipped with management
software, web interface and rtsp protocol to stream videos. They
also provide image url which is constantly updated as new video
capture is available. Following HTML + jQuery code uses such url
to stream live images from security cameras without management 
software. This is a sample code for D-Link based security cameras.
You would need: IP Address, image url and username, password.
Feel free to reuse the code below.  

Code Reference: http://savitechnologies.com/?p=107
Modified by: EmErgE :: irc.securitychat.org --> 

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
 $(document).ready(function() {
setInterval('reloadImages()',2000);
});

function reloadImages() {
$('#camera1').attr('src','http://user:password@192.168.100.200/cgi-bin/video.jpg?' + Math.random());
$('#camera2').attr('src','http://user:password@192.168.100.201/cgi-bin/video.jpg?' + Math.random());
$('#camera3').attr('src','http://user:password@192.168.100.202/cgi-bin/video.jpg?' + Math.random());
$('#camera4').attr('src','http://user:password@192.168.100.203/cgi-bin/video.jpg?' + Math.random());
}
</script>
</head>
<body>
<!-- Row 1 Column 1 -->
<div style="float: left">
<img id="camera1" src="http://user:password@192.168.100.200/cgi-bin/video.jpg" border=1/>
</div>
<!-- Row 1 Column 2 -->
<div>
<img id="camera2" src="http://user:password@192.168.100.201/cgi-bin/video.jpg" border=1/>
</div>
<!-- Row 2 Column 1 -->
<div style="clear: both; float: left">
<img id="camera3" src="http://user:password@192.168.100.202/cgi-bin/video.jpg" border=1/>
</div>
<!-- Row 2 Column 2 -->
<div>
<img id="camera4" src="http://user:password@192.168.100.203/cgi-bin/video.jpg" border=1/>
</div>
</body>
</html>
Posted by EmErgE at 5:53 PM
Categories: Programming