Skip to content

Commit

Permalink
Helpers and Classes fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkZoft committed Dec 11, 2012
1 parent fa6ac6c commit 9061619
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 10 deletions.
12 changes: 12 additions & 0 deletions zan/classes/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public function proccess($data = NULL, $validations = FALSE) {
if(!isEmail(POST($field))) {
return array("error" => getAlert("$field is not a valid email"));
}
} elseif($validation === "captcha?") {
if(!POST("captcha_token") or !POST("captcha_type")) {
return array("error" => getAlert(POST("captcha_type") === "aritmethic" ? "Please enter your answer again" : "Please type the characters you see in the picture"));
} elseif(POST("captcha_type") === "aritmethic") {
if(SESSION("ZanCaptcha". POST("captcha_token")) !== (int)POST($field)) {
return array("error" => getAlert("Your answer was incorrect"));
}
} else {
if(SESSION("ZanCaptcha". POST("captcha_token")) !== POST($field)) {
return array("error" => getAlert("The characters did not match the picture"));
}
}
} elseif($validation === "injection?") {
if(isInjection(POST($field))) {
return array("error" => getAlert("SQL/HTML injection attempt blocked"));
Expand Down
2 changes: 1 addition & 1 deletion zan/classes/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private function getScript($ext) {
}

if(count($scripts) > 0) {
if(_get("environment") < 3) {
if(_get("environment") < 3 or !_get("optimization")) {
array_walk($scripts, create_function('&$val', '$val = "'. _get("webURL") .'/$val";'));
if($ext === "css") {
return '<link rel="stylesheet" href="'. implode('" type="text/css" /><link rel="stylesheet" href="', $scripts) .'" type="text/css" />';
Expand Down
123 changes: 114 additions & 9 deletions zan/helpers/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function formField($a = NULL, $text, $raquo = TRUE) {

if(!is_null($a)) {
$HTML = '<p class="field">' . "\n";
$HTML .= "\t" . '<a href="'. $a .'">'. $raquo . $text .'</a>' . "\n";
$HTML .= "\t" . '<a '. $a .'>'. $raquo . $text .'</a>' . "\n";
$HTML .= '</p>' . "\n";
} else {
$HTML = '<p class="field">' . "\n";
Expand Down Expand Up @@ -259,20 +259,32 @@ function formRadio($attributes, $options = FALSE) {
if(is_array($options)) {
$HTML = NULL;

foreach($options as $attribute) {
$check = (isset($attribute["checked"]) and $attribute["checked"]) ? ' checked="checked"' : NULL;

$HTML .= ' <input '. $attrs .' value="'. $attribute["value"] .'" type="radio"'. $check .' />'. $attribute["value"];
foreach($options as $option) {
if(is_array($option)) {
foreach($option as $attribute) {
if($attribute["default"]) {
$check = ' checked="checked"';
} else {
$check = NULL;
}

$HTML .= ' <input '. $attrs .' value="'. $attribute["name"] .'" type="radio"'. $check .' />'. $attribute["value"];
}
}
}
} else {
$check = (isset($checked) and $checked) ? ' checked="checked"' : NULL;

if(isset($checked) and $checked) {
$check = ' checked="checked"';
} else {
$check = NULL;
}

if(isset($position) and $position === "left" and isset($text)) {
$HTML = $text .' <input'. $attrs .' type="radio"'. $check .' />';
$HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
} elseif(isset($position) and $position === "right" and isset($text)) {
$HTML = '<input'. $attrs .' type="radio"'. $check .' /> '. $text;
} elseif(isset($text)) {
$HTML = $text .' <input'. $attrs .' type="radio"'. $check .' />';
$HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
} else {
$HTML = '<input'. $attrs .' type="radio"'. $check .' />';
}
Expand Down Expand Up @@ -416,3 +428,96 @@ function formSave($action = NULL) {

return $HTML;
}

/**
* formCaptcha
*
* Generate a captcha to validate forms
*
* @param array $attributes = [$p, $field, $name, $class, ...]
* @param boolean $alphanumeric = "No"
* @returns string $HTML
*/
function formCaptcha($attributes = FALSE, $alphanumeric = FALSE) {
$hash = md5(getURL());
$HTML = '<input type="hidden" name="captcha_token" value="'. $hash .'" />';
$HTML .= '<input type="hidden" name="captcha_type" value="'. ($alphanumeric ? 'alphanumeric' : 'aritmethic') .'" />';

if(!$alphanumeric) {
$attributes["style"] = (isset($attributes["style"]) ? $attributes["style"] : '') . "max-width: 50px; text-align: center;";
$attributes["type"] = "number";

$num1 = rand(1, 9);
$num2 = rand(1, 9);

switch(rand(1, 3)) {
case 1:
$operation = '-';
$answer = $num1 - $num2;
break;

default:
$operation = '+';
$answer = $num1 + $num2;
}

$HTML .= __("How much is ") . (rand(0, 1) === 0 ? $num1 : num2str($num1, TRUE)) .' '. $operation .' '. (rand(0, 1) === 0 ? $num2 : num2str($num2, TRUE)) .'? ';

} else {
$attributes["style"] = (isset($attributes["style"]) ? $attributes["style"] : '') . "max-width: 200px; text-align: center;";

$answer = "";
$characters = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0");

for($i = 0; $i < 5; $i++) {
$answer .= $characters[rand(0, count($characters) - 1)];
}

$HTML .= '<img src="'. path("captcha/$hash") .'" /><br />';
}

SESSION("ZanCaptcha$hash", $answer);

if(isset($attributes) and is_array($attributes)) {
$attrs = NULL;

foreach($attributes as $attribute => $value) {
if($attribute === "required") {
$attrs .= ' required ';
} elseif($attribute === "events") {
$attrs .= ' '. $value .' ';
} elseif($attribute !== "p" and $attribute !== "field") {
if(!preg_match('/"/', $value)) {
$attrs .= ' '. strtolower($attribute) .'="'. $value .'"';
} else {
$attrs .= ' '. strtolower($attribute) ."='". $value ."'";
}
} else {
$$attribute = $value;
}
}

$HTML .= '<input'. $attrs .' type="text" /> ' . "\n";

if(isset($p) and $p and isset($field)) {
$HTML = ' <p>
<span class="field">&raquo; '. $field .'</span><br />
'. $HTML .'
</p>';
} elseif(isset($p) and $p) {
$HTML = ' <p>
'. $HTML .'
</p>';
} elseif(isset($field)) {
$HTML = '<span class="field">&raquo; '. $field .'</span><br />'. $HTML .'';
}

return $HTML;
} elseif($attributes) {
return $HTML .'<input name="'. $attributes .'" type="text" />' . "\n";
} else {
return NULL;
}
}
22 changes: 22 additions & 0 deletions zan/helpers/i18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ function __($text, $encode = TRUE) {
if(isset($phrase[$position])) {
return ($encode) ? encode($phrase[$position]) : $phrase[$position];
} else {
if($language !== "English" and !_get("production")) {
$content = "";
$logfile = "www/lib/languages/". strtolower($language) . ".txt";
$today = date("d/m/Y");

if(file_exists($logfile)) {
$content = file_get_contents($logfile);
}

$file = fopen($logfile, "a+");
$pos = strrpos($content, "$today");

if($pos !== FALSE) {
if(!@preg_match("/\\b" . addslashes($position) . "\\b/i", substr($content, $pos + 14))) {
fwrite($file, "$position\r\n");
}
} else {
fwrite($file, "--- $today ---\r\n");
fwrite($file, "$position\r\n");
}
}

return $text;
}
}
Expand Down
53 changes: 53 additions & 0 deletions zan/helpers/string.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,4 +832,57 @@ function social($URL, $content, $facebook = TRUE, $twitter = TRUE, $gPlus = TRUE

function showLinks($content) {
return preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1" target="_blank">$1</a>', $content);
}

function num2str($number, $translate = FALSE, $function = FALSE) {
switch($number) {
case 1:
$string = "one";
break;

case 2:
$string = "two";
break;

case 3:
$string = "three";
break;

case 4:
$string = "four";
break;

case 5:
$string = "five";
break;

case 6:
$string = "six";
break;

case 7:
$string = "seven";
break;

case 8:
$string = "eight";
break;

case 9:
$string = "nine";
break;

default:
$string = "zero";
}

if($translate) {
$string = __($string);
}

if($function) {
$string = $funciont($string);
}

return $string;
}

0 comments on commit 9061619

Please sign in to comment.