Page MenuHomeGRNET

controllo_campi.php
No OneTemporary

File Metadata

Created
Sat, Jan 17, 11:29 AM

controllo_campi.php

<?php
include_once("db.php");
include_once("dbutility.php");
include_once("utility.php");
include_once("form.php");
include_once("dbfield.php");
class ControlloCampi {
public static function check_all(&$post, &$controllo) {
$error = null;
$error = ControlloCampi::completezza($post, $controllo);
if ($error != null) {
return $error;
}
$error = ControlloCampi::coerenza($post, $controllo);
if ($error != null) {
return $error;
}
$error = ControlloCampi::forma($post, $controllo);
if ($error != null) {
return $error;
}
return $error;
}
private static function completezza($post, &$controllo) {
$error = null;
foreach ($controllo as $key => $v) {
if (strpos($key, "_file")) {
if (isset($controllo[$key]) && $controllo[$key]["obbligo"] == 1 && isset($post[$key . "_full"]) && $post[$key . "_full"] != 1)
$error[$key] = $key;
}
else
if (isset($controllo[$key]) && $controllo[$key]["obbligo"] == 1 && (!isset($post[$key]) || $post[$key] == "")) {
$error[$key] = $key;
}
}
if (isset($controllo["bi_piva"]) && empty($post["bi_piva"]) && empty($post["bi_cf"])) {
$error["bi_piva"] = "bi_piva";
$error["bi_cf"] = "bi_cf";
}
$data1 = null;
$data2 = null;
if (isset($controllo["bi_sospini_gg"]) || isset($controllo["bi_sospini_mm"]) || isset($controllo["bi_sospini_aaaa"]))
$data1 = $post["bi_sospini_gg"] . $post["bi_sospini_mm"] . $post["bi_sospini_aaaa"];
if (isset($controllo["bi_sospfine_gg"]) || isset($controllo["bi_sospfine_mm"]) || isset($controllo["bi_sospfine_aaaa"]))
$data2 = $post["bi_sospfine_gg"] . $post["bi_sospfine_mm"] . $post["bi_sospfine_aaaa"];
if ($data1 == null && $data2 != null) {
$error["bi_sospini_gg"] = "bi_sospini_gg";
$error["bi_sospini_mm"] = "bi_sospini_mm";
$error["bi_sospini_aaaa"] = "bi_sospini_aaaa";
}
if ($data2 == null && $data1 != null) {
$error["bi_sospfine_gg"] = "bi_sospfine_gg";
$error["bi_sospfine_mm"] = "bi_sospfine_mm";
$error["bi_sospfine_aaaa"] = "bi_sospfine_aaaa";
}
return $error;
}
private function coerenza(&$post, &$controllo) {
$error = null;
foreach ($controllo as $key => $v) {
if (isset($controllo[$key]["tipo"])) {
switch ($controllo[$key]["tipo"]) {
case "int":
if (!empty($post[$key]) && !ctype_digit($post[$key]))
$error[$key] = $key;
else if (isset($post[$key]))
$post[$key] = (int) $post[$key];
break;
case "decimal":
$post[$key] = str_replace(',', '.', $post[$key]);
if (!empty($post[$key]) && !preg_match("/^[0-9]{1,5}([\.][0-9][0-9]?)?$/i", $post[$key]))
$error[$key] = $key;
if (!empty($post[$key]) && preg_match("/^[0-9]{1,5}[\.][0-9]$/i", $post[$key]))
$post[$key] = $post[$key] . "0";
elseif (!empty($post[$key]) && preg_match("/^[0-9]{1,5}$/i", $post[$key]))
$post[$key] = $post[$key] . ".00";
break;
}
}
}
return $error;
}
private static function forma(&$post, &$controllo) {
$error = null;
foreach ($controllo as $key => $v) {
if (strpos($key, "email") && !empty($post[$key]) && !preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", $post[$key])) {
$error[$key] = $key;
}
if (strpos($key, "url") && !empty($post[$key]) && !preg_match("/^(http:\/\/|https:\/\/)[:\/~a-zA-Z0-9_\-\.\?#=&]+$/i", $post[$key])) {
$post[$key] = "http://" . $post[$key];
}
if (strpos($key, "_file") && !empty($post[$key]) && !preg_match("/^(file:\/\/)[:\/~a-zA-Z0-9_\-\.\?#=&]+$/", $post[$key])) {
$post[$key] = "file://" . $post[$key];
}
if (strpos($key, "issn") && !empty($post[$key])) {
if (preg_match("/[0-9]{7}[0-9X]{1}$/i", $post[$key])) {
$trunk = str_split($post[$key], 4);
$post[$key] = $trunk[0] . "-" . $trunk[1];
}
if (!preg_match("/[0-9]{4}[-]?[0-9]{3}[0-9X]{1}$/i", $post[$key])) {
$error[$key] = $key;
}
}
if ((strpos($key, "_tel") || strpos($key, "_fax") ) && !empty($post[$key]) && !preg_match("/^[+0-9]+$/i", $post[$key])) {
$error[$key] = $key;
}
if ($key == 'ri_anno' && !empty($post[$key]) && $post[$key] > (date('Y') + 1)) {
$error[$key] = $key;
}
}
return $error;
}
public static function check_richiesta(&$post, $tipomat) {
if ($tipomat == getConstVar("TIPO_ARTICOLO")) {
if(
(!empty($post["ri_anno"]) && !empty($post["ri_pgini"])) ||
(!empty($post["ri_anno"]) && !empty($post["ri_au1"])) ||
(!empty($post["ri_vol"]) && !empty($post["ri_pgini"])) ||
(!empty($post["ri_vol"]) && !empty($post["ri_au1"]))
)
return false;
}
elseif ($tipomat == getConstVar("TIPO_LIBRO")) {
if(
(!empty($post["ri_anno"]) && !empty($post["ri_pgini"]) && !empty($post["ri_pgfine"])) ||
(!empty($post["ri_anno"]) && !empty($post["ri_au1"]))
)
return false;
}
return true;
}
public static function check_username(&$post, &$controllo) {
$error = null;
if (isset($controllo["ut_usr"]))
$tab = "ut";
elseif (isset($controllo["bi_usr"]))
$tab = "bi";
else
return NULL;
$res_ute = $_SESSION['db']->execute("SELECT COUNT(ut_id) FROM utente WHERE ut_usr='" . $post[$tab . "_usr"] . "'");
if ($res_ute)
if ($_SESSION['db']->fetch_single($res_ute) >= 1)
$error[$tab . "_usr"] = $tab . "_usr";
else {
$res_bib = $_SESSION['db']->execute("SELECT COUNT(bi_id) FROM biblio WHERE bi_usr='" . $post[$tab . "_usr"] . "'");
if ($res_bib)
if ($_SESSION['db']->fetch_single($res_bib) >= 1)
$error[$tab . "_usr"] = $tab . "_usr";
else if (strlen($post[$tab . "_usr"]) < getConstVar("USR_MINLENGTH"))
$error[$tab . "_usr"] = $tab . "_usr";
}
return $error;
}
public static function check_password(&$post, &$controllo) {
$error = null;
if (isset($controllo["ut_pwd"]))
$tab = "ut";
elseif (isset($controllo["bi_pwd"]))
$tab = "bi";
else
return NULL;
if (!empty($post[$tab . "_pwd"]) && empty($post[$tab . "_pwd_2"]))
$error[$tab . "_pwd_2"] = $tab . "_pwd_2";
elseif (empty($post[$tab . "_pwd"]) && !empty($post[$tab . "_pwd_2"]))
$error[$tab . "_pwd"] = $tab . "_pwd";
elseif (!empty($post[$tab . "_pwd"]) && !empty($post[$tab . "_pwd_2"]) && $post[$tab . "_pwd"] != $post[$tab . "_pwd_2"])
$error[$tab . "_pwd_2"] = $tab . "_pwd_2";
elseif (!empty($post[$tab . "_pwd"]) && !empty($post[$tab . "_pwd_2"]) && $post[$tab . "_pwd"] == $post[$tab . "_pwd_2"] && strlen($post[$tab . "_pwd"]) < getConstVar("PWD_MINLENGTH")) {
$error[$tab . "_pwd"] = $tab . "_pwd";
$error[$tab . "_pwd_2"] = $tab . "_pwd_2";
}
return $error;
}
public static function check_date($post, $data, $yearfrom = null, $yearto = null) {
$error = null;
if (!isset($yearto) || $yearto == null)
$yearto = date('Y') + 1;
if (!isset($yearfrom) || $yearfrom == null)
$yearfrom = date('Y') - 1;
if (($post[$data . "_mm"] != "") && ($post[$data . "_gg"] != "") && ($post[$data . "_aaaa"] != "")) {
if ($post[$data . "_aaaa"] >= $yearfrom && $post[$data . "_aaaa"] <= $yearto) {
if (!checkdate($post[$data . "_mm"], $post[$data . "_gg"], $post[$data . "_aaaa"])) {
$error[$data . "_gg"] = $data . "_gg";
$error[$data . "_mm"] = $data . "_mm";
$error[$data . "_aaaa"] = $data . "_aaaa";
}
} else
$error[$data . "_aaaa"] = $data . "_aaaa";
}
else {
if (($post[$data . "_mm"] == "") && ($post[$data . "_gg"] == "") && ($post[$data . "_aaaa"] == ""))
return null;
else {
if ($post[$data . "_mm"] == "")
$error[$data . "_mm"] = $data . "_mm";
if ($post[$data . "_gg"] == "")
$error[$data . "_gg"] = $data . "_gg";
if ($post[$data . "_aaaa"] == "")
$error[$data . "_aaaa"] = $data . "_aaaa";
}
}
return $error;
}
public static function check_dataeva($dataeva, $dataric) {
$error = null;
$timestamp_dataric = strtotime($dataric);
$timestamp_dataeva = strtotime($dataeva);
$datadif = $timestamp_dataric - $timestamp_dataeva;
if ($datadif > 86400) {
$error["dd_dataeva_gg"] = 'dd_dataeva_gg';
$error["dd_dataeva_mm"] = 'dd_dataeva_mm';
$error["dd_dataeva_aaaa"] = 'dd_dataeva_aaaa';
} elseif ((0 < $datadif) && ($datadif < 86400)) {
$timestamp_dataeva = $timestamp_dataric + 1;
$dataeva = date('Y-m-d H:i:s', $timestamp_dataeva);
}
return $error;
}
public static function check_sospensione_dd(&$post, &$controllo) {
$error = null;
if (isset($controllo["bi_sospini_gg"]) && isset($controllo["bi_sospfine_gg"]) && $post["bi_sospini_gg"] != "" && $post["bi_sospfine_gg"] != "") {
$t_sospini = strtotime($post["bi_sospini_aaaa"] . "-" . $post["bi_sospini_mm"] . "-" . $post["bi_sospini_gg"]);
$t_sospfine = strtotime($post["bi_sospfine_aaaa"] . "-" . $post["bi_sospfine_mm"] . "-" . $post["bi_sospfine_gg"]);
$t_now = strtotime(date("Y-m-d"));
if ($t_sospini > $t_sospfine) {
$error["bi_sospini_gg"] = "bi_sospini_gg";
$error["bi_sospini_mm"] = "bi_sospini_mm";
$error["bi_sospini_aaaa"] = "bi_sospini_aaaa";
$error["bi_sospfine_gg"] = "bi_sospfine_gg";
$error["bi_sospfine_mm"] = "bi_sospfine_mm";
$error["bi_sospfine_aaaa"] = "bi_sospfine_aaaa";
}
if ($t_sospini < $t_now) {
$error["bi_sospini_gg"] = "bi_sospini_gg";
$error["bi_sospini_mm"] = "bi_sospini_mm";
$error["bi_sospini_aaaa"] = "bi_sospini_aaaa";
}
if ($t_sospfine < $t_now) {
$error["bi_sospfine_gg"] = "bi_sospfine_gg";
$error["bi_sospfine_mm"] = "bi_sospfine_mm";
$error["bi_sospfine_aaaa"] = "bi_sospfine_aaaa";
}
}
return $error;
}
}
?>

Event Timeline