<?php
//class to verify some types of data
// @access public
class Utils {
var $langdir = '../admin/lang';
/* variable to return boolean result*/
/* @access private */
/* @var boolean */
var $isValid;
/* variable to show the error message */
/* @access private */
/* @var text */
var $errorMsg;
/* variable to receive a db connection */
/* @access private */
/* @var MySQL class */
static $db;
/* prefix for the table */
/* @access private */
var $tabprefix;
//construct
function __construct($dbConn, $tabprefix) {
$this->db = $dbConn;
$this->tabprefix = $tabprefix;
}
/**
* convert a data from mysql format yyyy-mm-dd to php format mm-dd-yyyy
* @param date mysql date
* @return php date mm-dd-yyyy
*/
function convertDateToPhp($date) {
if (isset ( $date ) && $date != "") {
$array = explode ( "-", $date );
$dataToPHP = $array [1] . "-" . $array [2] . "-" . $array [0];
}
return $dataToPHP;
}
/**
* return an array contains title and the path of the image about the first project
* @param $db MySQL Connection
* @param $isTitle String true if search the title, false search the image path
* @return an array contains the title and the path to image
*/
function getFirstProject($db, $isTitle) {
if ($isTitle) {
$result = $db->query ( "select title from ".$this->getPrefix()."projects where deleted = 0 limit 1" );
$row = $result->fetch ();
$s = $row ['title'];
} else {
$result = $db->query ( "select path_img from ".$this->getPrefix()."projects where deleted = 0 limit 1" );
$row = $result->fetch ();
$s = $row ['path_img'];
}
return $s;
}
/* validate an email address */
function validateEmail($email) {
if (! isset ( $email ) || $email == "") {
return "The email is a required field and can not be empty<br><br>";
}
if (strlen ( $email ) < 8) {
return "The email is too short. Must be at least 8 characters<br><br>";
}
$version = substr ( phpversion (), 0, 3 );
if (PHP_VERSION_ID >= 50300) {
if (! preg_match ( "/^[a-z0-9][_\.a-z0-9-]+@([a-z0-9][0-9a-z-]+\.)+([a-z]{2,4})/", $email )) {
return "The email you entered is not valid. Enter a valid email address.<br><br>";
}
} else {
if (! ereg ( "^[a-z0-9][_\.a-z0-9-]+@([a-z0-9][0-9a-z-]+\.)+([a-z]{2,4})", $email )) {
return "The email you entered is not valid. Enter a valid email address.<br><br>";
}
}
return "";
}
/**
* get all languages for admin area
* @return an array contains all langs names
*/
function getAllLangs() {
$array = array ();
$extValid = "php";
$dir = opendir ( $this->langdir );
echo "<br>dir is: ".$this->langdir."<br>";
while ( $file = readdir ( $dir ) ) {
if ($file != "." && $file != ".." && ! is_dir ( $file ) && $file != "index.php" && $file != 'index.html') {
$ext = pathinfo ( $file, PATHINFO_EXTENSION );
if ($ext === $extValid) {
array_push ( $array, pathinfo ( $file, PATHINFO_FILENAME ) );
}
}
}
return $array;
}
/**
*
* return if the language is set
* #@return true language is set
*/
function isIssetLang() {
return isset ( $_SESSION ['lang'] ) && $_SESSION ['lang'] != '';
}
/**
*
* set the language
* @param String $language name of the language
*/
function setLang($language) {
$_SESSION ['lang'] = $language.".php";
}
/**
* return the lang
* @return String return the lang
*/
function getLang() {
return $_SESSION ['lang'];
}
function unsetLang() {
$_SESSION['lang'] = '';
unset($_SESSION['lang']);
}
/**
* get a value for specific setting name
* @param String $name
* @return String the setting value
*/
function getSettingValue($name) {
$result = $this->db->query ( "select setting_value from " . $this->getPrefix () . "settings where setting_name='" . $name . "'" );
$row = $result->fetch ();
$value = stripslashes ( $row ['setting_value'] );
if (isset($value) && $value != '') {
$value = str_replace("<p>", '', $value);
$value = str_replace("</p>", '', $value);
}
return $value;
}
/**
* create the contents for file config.php
* @param String $hostDb db host
* @param String $usernameDb db username
* @param String $passwordDb db password
* @param String $dbname db name
* @param String $prefix table prefix
* @return Array
*/
function createFileInstall($hostDb, $usernameDb, $passwordDb, $dbname, $prefix) {
//php open tag
$output [] = '<?php';
//database
$output [] = '/* db connection data */';
$output [] = '$_CONFIG[\'host\'] = "' . $hostDb . '";';
$output [] = '$_CONFIG[\'user\'] = "' . $usernameDb . '";';
$output [] = '$_CONFIG[\'pass\'] = "' . $passwordDb . '";';
$output [] = '$_CONFIG[\'dbname\'] = "' . $dbname . '";';
//$output[] = '$_CONFIG[\'tab_prefix\'] = "'.$prefix.'";';
//tables
$output [] = '/* tables for login */';
$output [] = '$_CONFIG[\'table_sessioni\'] = "' . $prefix . 'session";';
$output [] = '$_CONFIG[\'table_utenti_admin\'] = "' . $prefix . 'useradmin";';
//cookie
$output [] = '$_CONFIG[\'expire\'] = 3600;';
$output [] = '$_CONFIG[\'session_time\'] = 0;';
//time to execution the garbage collection
$output [] = '$_CONFIG[\'session_gc_time\'] = 3600;';
//secret password
$output [] = '$_CONFIG[\'secret\'] = md5("76dhd::@j++ahaa((--^");';
//images
$output [] = '$_CONFIG[\'thumbnail_path\'] = "images/gallery/thumb/";';
$output [] = '$_CONFIG[\'gallery_path\'] = "images/gallery/";';
$output [] = '$_CONFIG[\'category_path\'] = "images/category/";';
$output [] = 'define(\'AUTH_LOGGED\', 99);';
$output [] = 'define(\'AUTH_NOT_LOGGED\', 100);';
$output [] = 'define(\'AUTH_USE_COOKIE\', 101);';
$output [] = 'define(\'AUTH_USE_LINK\', 103);';
$output [] = 'define(\'AUTH_INVALID_PARAMS\', 104);';
$output [] = 'define(\'AUTH_LOGEDD_IN\', 105);';
$output [] = 'define(\'AUTH_FAILED\', 106);';
$output [] = '/* define table prefix const */';
$output [] = 'define(\'TABPREFIX\', "' . $prefix . '");';
/** Absolute path to the ezphotopress directory. */
$output [] = 'define(\'ABSPATH\', dirname(__FILE__) . \'/\');';
//php close tag
$output [] = '?>';
return $output;
}
function endsWith($haystack, $needle) {
$length = strlen ( $needle );
if ($length == 0) {
return true;
}
$start = $length * - 1; //negative
return (substr ( $haystack, $start ) === $needle);
}
function getProjectsForThumbs() {
$sql = "select p.id as id,p.title as title,p.path_thumb as thumb,p.path_img as image,c.name as name from " . $this->getPrefix () . "projects as p ";
$sql .= "left join " . $this->getPrefix () . "categories as c on p.id_category = c.id ";
$sql .= "where p.deleted = 0 order by p.title,p.create_date,c.name";
$query = $this->db->query ( $sql );
return $query;
}
function getProjects() {
//get all projects
$sql = "select p.id as id,p.title as title,p.body as body,c.name as name,c.path_img as cat_image from " . $this->getPrefix () . "projects as p ";
$sql .= "left join " . $this->getPrefix () . "categories as c on p.id_category = c.id ";
$sql .= "where p.deleted = 0 order by p.title,p.create_date,c.name";
$result = $this->db->query ( $sql );
return $result;
}
function getBlogArticles() {
$query = $this->db->query ( "select * from " . $this->getPrefix () . "blog where published = 1 order by create_date" );
return $query;
}
function getPrefix() {
return $this->tabprefix;
}
function getAboutMePage() {
$sql = "select * from " . $this->getPrefix () . "pages where code='about_me'";
$query = $this->db->query ( $sql );
return $query->fetch();
}
}
?>