template_dir = getcwd().'/smarty/templates/';
$s->compile_dir = getcwd().'/smarty/templates_c/';
$s->config_dir = getcwd().'/smarty/configs/';
$s->cache_dir = getcwd().'/smarty/cache/';
/**
* Envelopes are defined as a size, address location, return address location.
* Everything seems to be off by .875.
*
* ie. #10 envelope, 9.5" x 4.125".
* you'd assume that to center it on the page, you'd take 8.5" (paper width), subtract 4.125" (envelope height),
* subtract 1.0 from that for auto-borders in browser printing, then divide the remainder by 2 (1.6875 in this case).
* But it seems that 1.6 is more correct.
*
* You'd also assume that to get it right, you'd take 9.5" (envelope width), add .5" for the leading border,
* then add the remainder (1.0"). And it's about right. That's fucked up.
*
* Size is Width x Height,
* addr is The Address: Left Margin, Top Margin, Width of area, Height of area
* rtrn is The Return Address: Left Margin, Top Margin, Width of area, Height of area
* advr is The Advertisement: Left Margin, Top Margin
*
*/
$envelopes = array(
array(
name => '#10',
offs => array(1.0, 1.6),
size => array(9.5, 4.125),
addr => array(3.0, 2.0, 4.0, 1.0),
code => array(3.0, 1.7),
rtrn => array(0.1, 0.25, 2.0, 1.0),
advr => array(0.1, 4.00),
),
array(
name => '7" x 7" Flat Mailer',
offs => array(3.25, 0),
size => array(7.0, 7.0),
addr => array(1.2, 4.55, 3.4, 1.4),
code => array(3.0, 6.6),
rtrn => array(0.25, 0.25, 2.0, 1.0),
advr => array(1.2, 6.1),
),
);
$self = dirname($_SERVER['PHP_SELF']) . '/';
$s->assign("self", $self);
$r = $_REQUEST;
if (isset($r['f']) and is_callable($r['f'])) {
$r['f']($r['x']);
} else {
$s->display('mainpage.tmpl');
}
function faq($x) {
global $s;
$s->display('faq.tmpl');
}
function envelope($x) {
global $s, $envelopes;
$s->assign('envelopes', $envelopes);
if (isset($x['step'])) {
switch($x['step']) {
case 1:
$lines = split("\n", $x['address']);
foreach($lines as $v) {
$v = preg_replace("/^(To:|Mailing Address:)\s*/", '', trim($v));
if (trim($v) == '') continue;
$addr[] = trim($v);
}
if (count($addr) < 3 or count($addr) > 4) {
$x['error'] = "Oops. That address doesn't seem right.
Hint: Put the name on one line, the address
on the second, and at least the zip on the third.";
$s->assign('x', $x);
$s->display('envelope1.tmpl');
break;
}
$name = $addr[0];
$address = $addr[1];
$city = FALSE;
$state = FALSE;
$zip = FALSE;
for( $i=1;$i<=count($addr);$i++) {
preg_match("/(\d{5})/", $addr[$i], $m);
if (isset($m[1])) {
$zip = $m[1];
}
}
if ($zip === FALSE) {
for( $i=1;$i<=count($addr);$i++) {
preg_match("/(.+)\s*,\s*(.+)/", $addr[$i], $m);
if (isset($m[1])) {
$city = $m[1];
$state = $m[2];
}
}
if ($city === FALSE) {
$x['error'] = "Hmm, I couldn't figure out the City and State.
Try putting it on a separate line,
or use the 5 digit zip.";
$s->assign('x', $x);
$s->display('envelope1.tmpl');
break;
}
}
$script = getcwd()."/address-lookup.pl";
$return = `$script "$name|$address|$city|$state|$zip"`;
if (trim($return) == '0') {
$x['error'] = "The USPS says that isn't a valid address.
Check for typos, try again, or contact
support@angryox.com to try and fix.";
$s->assign('x', $x);
$s->display('envelope1.tmpl');
break;
} elseif (strlen(trim($return)) == 1 and intval(trim($return)) >= 2) {
$x['error'] = "The USPS found multiple matches for that address.
Go to the USPS Zip+4 Website to get the
right address.";
$s->assign('x', $x);
$s->display('envelope1.tmpl');
break;
}
$lines = split("\n", $return);
$x['newaddr'][] = strtoupper($name);
$x['newaddr'][] = $lines[0];
$x['newaddr'][] = sprintf("%s %s", $lines[1], $lines[2]);
$x['newaddr'][] = $lines[3];
$s->assign('x', $x);
$s->display('envelope2.tmpl');
break;
case 2:
if (substr($_REQUEST['go'],0,1) != 'Y') {
$x['error'] = "Well, if it isn't right, let's try again.";
$s->assign('x', $x);
$s->display('envelope1.tmpl');
} else {
preg_match("/^(\d+)/", $x['addr2'], $m);
$x['bcurl'] = $GLOBALS['self'] . 'do-barcode.php?bcs=' .
urlencode(dobarcode($m[1], $x['addr4']));
$x['printcssurl'] = getEnvelopeCss($x['envid']);
$x['screencssurl'] = str_replace('print-', 'screen-', $x['printcssurl']);
$s->assign('x', $x);
$s->display('envelope3.tmpl');
}
break;
default:
$s->display('envelope1.tmpl');
break;
}
} else {
$s->display('envelope1.tmpl');
}
}
function about($x) {
global $s;
$s->display('about.tmpl');
}
function privacy($x) {
global $s;
$s->display('privacy.tmpl');
}
/**
* returns string to encode for the barcode
* Logic stolen from: http://pe.usps.com/mpdesign/misc_docs/mqc_html/mqc_5.htm
*/
function dobarcode($housenumber, $zip4) {
$zip4 = str_replace("-", "", $zip4);
if (strlen($zip4) != 9) return FALSE;
$sum = array_sum(preg_split("//", $zip4));
$sum += array_sum(preg_split("//", substr($housenumber,-2,2)));
$correction_char = ((intval($sum/10)+1)*10) - $sum;
if ($correction_char == 10) $correction_char = 0;
$bcs = sprintf('/%09d%02d%01d/', $zip4, substr($housenumber,-2,2), $correction_char);
return $bcs;
}
function getEnvelopeCss($envid) {
global $s, $envelopes;
// checks for the existence of the css already, if it is there, return the filename;
// if not create and return.
$cssfilename = 'envelopes-css/print-'.md5($envelopes[$envid]['name']).'.css';
$cssscreenfn = str_replace('print-', 'screen-', $cssfilename);
$csslocation = getcwd() . '/' . $cssfilename;
$cssscreenloc= getcwd() . '/' . $cssscreenfn;
/* you could add something here to check the XML file you are using... */
if (!is_file($csslocation . '1')) {
$s->left_delimiter = '{{';
$s->right_delimiter = '}}';
$s->assign("e", $envelopes[$envid]);
$output = $s->fetch('print-css.tmpl');
$fp = fopen($csslocation, "w");
fwrite($fp, $output);
fclose($fp);
$output = $s->fetch('screen-css.tmpl');
$fp = fopen($cssscreenloc, "w");
fwrite($fp, $output);
fclose($fp);
$s->left_delimiter = '{';
$s->right_delimiter = '}';
}
return $cssfilename;
}