Tag Archive for easy

jsCookies – my simple easy pure js javascript cookies function

//  create my jsCookies function
var jsCookies = {

	// this gets a cookie and returns the cookies value, if no cookies it returns blank ""
	get: function(c_name) {
		if (document.cookie.length > 0) {
			var c_start = document.cookie.indexOf(c_name + "=");
			if (c_start != -1) {
				c_start = c_start + c_name.length + 1;
				var c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) {
					c_end = document.cookie.length;
				}
				return unescape(document.cookie.substring(c_start, c_end));
			}
		}
		return "";
	},

	// this sets a cookie with your given ("cookie name", "cookie value", "good for x days")
	set: function(c_name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toUTCString());
	},

	// this checks to see if a cookie exists, then returns true or false
	check: function(c_name) {
		c_name = jsCookies.get(c_name);
		if (c_name != null && c_name != "") {
			return true;
		} else {
			return false;
		}
	}

};
// end my jsCookies function

// USAGE - get    ::   jsCookies.get("cookie_name_here");  [returns the value of the cookie]
// USAGE - set    ::   jsCookies.set("cookie_name", "cookie_value", 5 );  [give name, val and # of days til expiration]
// USAGE - check  ::   jsCookies.check("cookie_name_here");  [returns only true or false if the cookie exists or not]

source

SimpleXML Set Attributes

function set_simplexml_attributes($element, $attributes) {
foreach($attributes as $key => $value) {
$element->addAttribute($key, $value);
}
}

// ex:
set_simplexml_attributes($categoryElement, array(
"id" => "1",
"enabled" => "true",
"iconUrl" => $this->thinger,
"tooltip" => "popups/study.swf"
));

source

Simple SubSonic Collection Query

ProductsCollection coll = new
ProductsCollection().Where("VendorId", vendorId).Load();

source

Letter back encryption

import sys

alphabet = 'abcdefghijklmnopqrstuvwxyz'

class LetterBack:
'''Class for Encrypting a string of letters'''

def __init__(self, text='mnm'):
self.text = text

def encrypt(self, aSentance):
'''Encrypts a given string by replacing each letter
with the letter before it in the alphabet.'''
output = ''
for element in aSentance:
for i in range(26):
if element == alphabet[i]:
output += alphabet[i-1]
print output

def decrypt(self, aSentance):
'''Decrypts a given string encoded by the encrypt
function.'''
output = ''
for element in aSentance:
#print element + ' this is 1'
for i in range(26):
if element == alphabet[i-1]:
output += alphabet[i]
print output

def main():
lb = LetterBack()
if sys.argv[1] == '-e':
lb.encrypt(sys.argv[2])
elif sys.argv[1] == '-d':
lb.decrypt(sys.argv[2])

if __name__ == '__main__':
main()

source

Beginner’s instructions for compiling from source on OS X

# cd into the source directory and run the following command.  That should do it.
sudo ./configure; sudo make; sudo make install

source

Myspace Add Me Link

<a href="http://friends.myspace.com/index.cfm?fuseaction=invite.addfriend_verify&friendID=XXXXXX">EASY ADD! ADD ME! EASY ADD! ADD ME! EASY ADD! ADD ME!</a>

source

Call a function with arguments directly form a EventListener

my_btn.addEventListener(MouseEvent.MOUSE_DOWN,
function(e:MouseEvent)
{
new OpenWebsite("http://www.flashjunior.ch","_blank");
}
)

/* test class */
package
{
import flash.display.MovieClip
import flash.events.MouseEvent
import flash.display.SimpleButton
import flash.net.navigateToURL
import flash.net.URLRequest;

public class OpenWebsite extends MovieClip
{
public function OpenWebsite(url:String,target:String):void
{
navigateToURL(new URLRequest(url),target);
}
}
}

source

Alias a command and save it in .profile

echo "alias name='command string'" >> ~/.profile

source

tar basics

# create
tar cvf docs.tar docs2

# extract
tar xvf docs.tar

source

SimpleMail

<?php
class SimpleMail {
public $to;
public $from;
public $fromName;
public $subject;
public $cc;
public $bcc;
public $message;

private $uid;
private $mailer;
private $attachments;

public function __construct($to = null, $from = null, $subject = null, $message = null) {
if(!empty($to) && !empty($from)) {
$this->to = $to;
$this->from = $from;

if(!empty($subject)) {
$this->subject = $subject;
}
if(!empty($message)) {
$this->message = $message;
}
}

$this->mailer = "PHP " . phpversion();
$this->attachments = array();
}

public function SetMailer($mailer) {
if(!empty($mailer) && is_string($mailer)) {
$this->mailer = $mailer;
}
}

public function SetTo($to) {
if(!empty($to) && is_string($to)) {
$this->to = $to;
}
}

public function SetFrom($from) {
if(!empty($from) && is_string($from)) {
$this->from = $from;
}
}

public function SetSubject($subject) {
if(!empty($subject) && is_string($subject)) {
$this->subject = $subject;
}
}

public function AddCC($email) {
if(!empty($email) && is_string($email)) {
if(strlen($this->cc) > 0) {
$this->cc .= ",".$email;
}
else {
$this->cc = $email;
}
}
}

public function AddBCC($email) {
if(!empty($email) && is_string($email)) {
if(strlen($this->bcc) > 0) {
$this->bcc .= ",".$email;
}
else {
$this->bcc = $email;
}
}
}

public function AddMessage($message) {
if(!empty($message) && is_string($message)) {
$this->message = $message;
}
}

public function AddAttachment($attachment) {
if(file_exists($attachment)) {
$attachmentInfo = array();

$this->uid = "==Multipart_Boundary_x{" . md5(uniqid(time())) . "}x";
$attachmentInfo['Attachment'] = $attachment;
$attachmentInfo['Size'] = filesize($attachment);

$fp = fopen($attachment, "rb");
flock($fp, LOCK_SH);
$data = fread($fp, filesize($attachment) + 1);
flock($fp, LOCK_UN);
fclose($fp);

$attachmentInfo['Content'] = chunk_split(base64_encode($data), 64);

$extStart = strrpos($this->attachment, ".");
$attachmentInfo['Extension'] = substr($attachmentInfo['Attachment'], $extStart, (strlen($attachmentInfo['Attachment']) - $extStart));
$attachmentInfo['Name'] = self::GenerateRandomFilename($attachmentInfo['Extension']);
$attachmentInfo['Mime'] = "application/octet-stream; name="{$attachmentInfo['Name']}"

";

$this->attachments[] = $attachmentInfo;
}
}

public function SendMessage($to = null, $from = null, $subject = null) {
if(empty($to) && !empty($this->to)) { $to = $this->to; }
if(empty($from) && !empty($this->from)) { $from = $this->from; }
if(empty($subject) && !empty($this->subject)) { $subject = $this->subject; }
if(empty($to) || empty($from) || empty($subject)) { return false; }

$headers = "";
$headers.= "From: {$this->fromName} <{$from}>
";
$headers.= "Reply-To: {$to}
";
$headers.= "Subject: {$subject}
";
$headers.= "X-Mailer: {$this->mailer}
";
$headers.= "MIME-Version: 1.0
";
$headers.= "Content-Type: multipart/mixed; boundary="{$this->uid}"
";
$headers.= "--{$this->uid}
";
$headers.= "Content-Type: text/plain; charset="iso-8859-1"
";
$headers.= "Content-Trasfer-Encoding: 7bit

";
$headers.= $this->message . "

";

if(!empty($this->attachments) && is_array($this->attachments)) {
foreach($this->attachments as $attactment) {
$headers.= "--{$this->uid}
";
$headers.= "Content-Type: application/octet-stream; name="{$attactment['Name']}"
";
$headers.= "Content-Transfer-Encoding: base64
";
$headers.= "Content-Disposition: attachment; filename="{$attactment['Name']}"

";
$headers.= $attactment['Content'] . "

";
}
}

$headers.= "--{$this->uid}--";

if(mail($to, $from, "", $headers)) {
return true;
}
return false;
}

private function GenerateRandomFilename($extension) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$salt = "";

for($i = 0; $i < mt_rand(5, 25); $i++) {
$salt.= $chars{$i};
}

$fname = md5($salt . time());
return $fname . $extension;
}
}
?>

source