<?
/*
* This file is part of Webgenerator-X,
* an object oriented website management engine working an top of
* Apache/PHP4/MySQL.
* http://www.webgenerator-x.com
* @2001 REGNI Giorgio
* hide@address.com
*
* Webgenerator-X is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Webgenerator-X is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Webgenerator-X; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*************************************************************************/
/**
* IText class
*
* Text data access interface for the Collection data system.
* IText provides multi language support to any WG-X module.
* @author REGNI Giorgio
* @package Developers
* @subpackage Helper functions
*/
class A_IText {
/**
*Get a text string from the database
*By default, if no record is found for lang, the default language for this website will be used.
*The function will return a string or this if an error occured:
* integer -1 on non existant record at all,
* integer 0 on any other error,
* integer -2 if $exitonabsent is set and no record exists for the language instead instead of returning the default language.
*@param string $lang Lang is a 2 caracters language identifiant. If lang is not set the actual language will be used.
*@param integer $id Id of the IText to get data from.
*@param boolean $exitonabsent When set to anything but "", the function will return -2 if no record exists for the language instead
*of returning the default language data.
*@return string the data string,
*/
function Get($id,$lang='',$exitonabsent="")
{
global $db_prefix,$language,$defaultlanguage;
if ($lang=='')
$lang = $language;
$sql = "select data from $db_prefix"."ctext_data where tid='$id' and lang='$lang'";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
if (mysql_numrows($res)>0)
{
list($out)=mysql_fetch_row($res);
mysql_free_result($res);
}
else
if ($exitonabsent==="")
{
mysql_free_result($res);
$sql = "select data from $db_prefix"."ctext_data where tid='$id' and lang='$defaultlanguage'";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
if (mysql_numrows($res)>0)
{
list($out)=mysql_fetch_row($res);
mysql_free_result($res);
}
else
{
mysql_free_result($res);
$out= -1;
}
}
else
$out= 0;
}
else
$out = -2;
}
else
$out= 0;
return $out;
}
/**
*Set a text string
*If lang is not set the actual language will be used
*return true on success and false else
*It will create the sql record if it's not already created
*@param string $lang Lang is a 2 caracters language identifiant. If lang is not set the actual language will be used.
*@param integer $id Id of the IText to set data.
*@param string data The data to set. It does not need to be addslashed or anything, the functions does any database validation itself.
*@return boolean
*
*/
function Set($id,$data,$lang='')
{
global $db_prefix,$language,$VALIDATOR;
if ($lang=='')
$lang = $language;
$data = $VALIDATOR->make_dbrecord($data);
$sql = "select tid from $db_prefix"."ctext_data where tid='$id' and lang='$lang'";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
if (mysql_numrows($res)==1)
{
mysql_free_result($res);
// already created, just an update to do
$sql = "update $db_prefix"."ctext_data set data='$data' where tid='$id' and lang='$lang'";
$res = mysql_query($sql);
if (mysql_errno()==0)
return true;
else
return false;
}
else
{
mysql_free_result($res);
// must be added
$sql = "insert into $db_prefix"."ctext_data VALUES('$id','$lang','$data')";
$res = mysql_query($sql);
if (mysql_errno()==0)
return true;
else
return false;
}
}
else
return false;
}
/**
*Create a new text string
*Returns the new text id or 0 if there was an error
*Creates an empty default language entry in ctext_data
*@return integer
*/
function Create()
{
global $db_prefix,$defaultlanguage;
$out = 0;
// get the next free itext id
$sql="select max(tid)+1 from $db_prefix"."ctext_data";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
list($tid)= mysql_fetch_row($res);
mysql_free_result($res);
$sql = "insert into $db_prefix"."ctext_data VALUES('$tid','$defaultlanguage','')";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
$out = $tid;
}
}
return $out;
}
/**
*Create N new text strings
*Returns the new ids in an array
*Each element will be the id or 0 if there was an error
*@param integer $nb how many IText to create
*@return array
*/
function CreateNb($nb)
{
$out = array();
for ($i=0;$i<$nb;$i++)
{
$out[] = $this->Create();
}
return $out;
}
/**
*Create new text strings from an array of anything
*keeped because we need compatibility with previous abbrev code
*Returns the new ids in an array
*Each element will be the id or 0 if there was an error
*@param array $in number of element in $in = how many IText to create
*@return array
*/
function CreateFromArray($in)
{
$out = array();
foreach( $in as $abbrev)
{
$out[] = $this->Create();
}
return $out;
}
/**
*Delete a text and all its translations
*return true on ok etc...
*If the id is zero then returns always true because there is no IText where id=0.
*@param integer $id IText to delete
*@return boolean
*/
function Delete($id)
{
global $db_prefix;
if ($id == 0)
return true;
$sql = "delete from $db_prefix"."ctext_data where tid='$id'";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
return true;
}
else
return false;
}
/**
*Delete an array of IText ids and all their translations
*return an array of true/false on ok etc...
*@param array $list IText ids to delete
*@return array
*/
function DeleteFromArray($list)
{
global $db_prefix;
$ret=array();
foreach($list as $id)
{
$ret[] = $this->Delete($id);
}
return $ret;
}
/**
Delete of IText DB entries and all their translations of the required table
$table, required - a table which needed to delete IText entries
$list, required - a num array of field names for which IText entries must be deleted
$keys, not required - an assoc array of pairs field name/field value to select a range of the table records
If $keys is not defined then IText entries in all records will be deleted for all fields listed in $list
return true/false
*/
function DeleteFromTable($table,$list,$keys="")
{
global $db_prefix;
//prepare fields and where phrases
$fields = implode( ',', $list);
$sql = "select $fields from $db_prefix$table";
if ($keys !== "")
{
$where = "";
foreach($keys as $key=>$value)
{
if ($where !== "")
$where .= " and ";
$where .= "$key='$value'";
}
$sql .= " where " . $where;
}
$res = @mysql_query( $sql );
$ret = true;
if ($res)
{
if ( mysql_numrows($res)>0 )
{
while ($ids = mysql_fetch_array( $res, MYSQL_NUM))
{
// delete the texts of all languages for one record of $table
$deleted = $this->DeleteFromArray($ids);
// test the results of deleting
if (in_array(false, $deleted))
{
$ret = false;
break;
}
}
mysql_free_result( $res );
}
}
else
{
$ret = false;
}
return $ret;
}
}
$IText = new A_IText();
/**
* IImage class
*
* Image access interface for the Collection data system.
* IImage provides transparent image support to any WG-X module.
* IImage are stored as files in the $image_dir directory under $root_dir.
* Filenames are somefile__width__heigh.ext
* In db fields, they are stored as integers
* All operations uses the filename expect GetFilenameFromRecord, GetRecordFromFilename
* @author REGNI Giorgio
* @package Developers
* @subpackage Helper functions
*/
class A_IImage {
/**
* The function returns an associative array like this:
* url: url according do $images_dir
* desc: description for the image (for alt tag) (uses IText)
* width,height,align,border,ext,base_file
* This class is used by the Smarty image plugin and the admin panel
*
* On error returns an empty array
*@return array
*@param integer $id Id of IImage concerned
*/
function Get($id)
{
global $db_prefix,$images_dir,$siteurl;
$out = array();
$sql = "select basefile,ext,width,height,idesc,align,border from $db_prefix"."cimage where imid='$id'";
$res= mysql_query($sql);
echo mysql_error();
if (mysql_errno()==0)
{
if (mysql_numrows($res)==1)
{
list( $out[base_file],
$out[ext],
$out[width],
$out[height],
$out[desc],
$out[align],
$out[border]) = mysql_fetch_row($res);
$file = $out[base_file]."__$out[width]"."__$out[height].".$out[ext];
$out[url]= concat_dirs($siteurl,concat_dirs( $images_dir,$file));
}
}
return $out;
}
/**
* Create a new image record.
*
* Return the newly created id or 0 on error.
*@return integer
*@param string $basefile
*@param string $desc
*@param intger $border
*@param integer $width
*@param integer $height
*@param interger $align
*@param string $ext
*/
function Create($basefile,$ext,$width,$height,$desc,$align,$border)
{
global $db_prefix,$VALIDATOR,$IText;
$out = 0;
$basefile = $VALIDATOR->make_dbrecord($basefile);
$ext = $VALIDATOR->make_dbrecord($ext);
$border = $VALIDATOR->make_dbrecord(intval($border));
$width = $VALIDATOR->make_dbrecord(intval($width));
$height = $VALIDATOR->make_dbrecord(intval($height));
$align = $VALIDATOR->make_dbrecord($align);
$idesc = $IText->Create();
$IText->Set($idesc,$desc);
$sql = "insert into $db_prefix"."cimage VALUES('','$basefile','$ext','$width','$height','$idesc','$align','$border')";
$res= mysql_query($sql);
if (mysql_errno()==0)
{
$out = mysql_insert_id();
}
return $out;
}
function Set($dbid,$width,$height,$align,$border)
{
global $VALIDATOR,$db_prefix;
$border = $VALIDATOR->make_dbrecord(intval($border));
$width = $VALIDATOR->make_dbrecord(intval($width));
$height = $VALIDATOR->make_dbrecord(intval($height));
$align = $VALIDATOR->make_dbrecord($align);
$sql = "update $db_prefix"."cimage set width='$width',height='$height',align='$align',border='$border' where imid='$dbid'";
$res= mysql_query($sql);
echo mysql_error();
if (mysql_errno()==0)
{
return true;
}
return false;
}
/**
* Upload a new image
*@param $tmp_file string
*@param integer $imid Image id
*/
function Upload($imid,$tmp_file)
{
global $IFile,$images_dir,$root_dir;
if ($IFile->Upload($tmp_file, $this->GetFilenameFromRecord($imid),concat_dirs( $root_dir,$images_dir)) == $IFile->NO_ERROR)
return true;
else
return false;
}
/**
*GetFilenameFromRecord
* returns an image file name according to its db record or boolean false on error.
* @param integer $dbid
* @return string
*/
function GetFilenameFromRecord($dbid)
{
global $db_prefix;
$filename = false;
$sql = "select basefile,width,height,ext from $db_prefix"."cimage where imid='$dbid'";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
if (mysql_numrows($res)==1)
{
list($basefile,$width,$height,$ext) = mysql_fetch_row($res);
mysql_free_result($res);
$filename = $basefile."__$width"."__$height.".$ext;
}
}
return $filename;
}
/**
* GetRecordFromFilename
* returns an image record id according to its filename or boolean false on error.
* @param integer $dbid
* @return string
*/
function GetRecordFromFilename($filename)
{
global $db_prefix;
$dbid = false;
list($base,$width,$height) = explode('__',$filename);
// here height as the extension, get it off and constuct the base_file
ereg("^(.*)\.([^\.]*$)", $height, $elts);
$height = $elts[1];
$ext = $elts[2];
unset($elts);
$sql = "select imid from $db_prefix"."cimage where base_file='$base' and width='$width' and height='$height' and ext='$ext'";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
if (mysql_numrows()>0)
{
list($dbid) = mysql_fetch_row($res);
mysql_free_result($res);
}
}
return $dbid;
}
/**
* List every uploaded or computed sizes present.
* returns an array of db ids.
* @return array
*/
function List_Images()
{
global $db_prefix;
$out = array();
$sql = "select imid from $db_prefix"."cimage where 1";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
while(list($dbid) = mysql_fetch_row($res))
$out [] = $dbid;
mysql_free_result($res);
}
return $out;
}
/**
* Search images.
* returns an array of db ids.
* @param string $filename Begining of the files to search
* @param integer $width if =0 when width is not important
* @param string $width_op <,> or =
* @param integer $height if =0 when height is not important
* @param string $height_op <,> or =
* @param string $ext file type ="" if not important
* @return array
*/
function Search_Images($filename,$width,$width_op,$height,$height_op,$ext)
{
global $siteurl,$db_prefix;
$out = array();
// construct the sql query
if (intval($width)!=0)
{
// width is important
switch($width_op)
{
case '>':
$where_width = "and width>'$width'";
break;
case '<':
$where_width = "and width<'$width'";
break;
case '=':
$where_width = "and width='$width'";
break;
}
}
if (intval($height)!=0)
{
// height is important
switch($height_op)
{
case '>':
$where_height = "and height>'$height'";
break;
case '<':
$where_height = "and height<'$height'";
break;
case '=':
$where_height = "and height='$height'";
break;
}
}
if ($filename!='')
{
$where_file = "and basefile like '$filename%'";
}
if ($ext!='')
{
$where_ext = "and ext like '$ext%'";
}
$sql = "select imid from $db_prefix"."cimage where 1 $where_file $where_ext $where_width $where_height";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
while(list($dbid) = mysql_fetch_row($res))
$out [] = $dbid;
mysql_free_result($res);
}
return $out;
}
/**
* Given a id delete the sql record and the file.
* return true/false
* Delete the Desc Itext in the process.
* GD is not needed here.
* @param string $file
* @return boolean
*/
function Delete($id)
{
global $images_dir,$root_dir,$IText,$IFile,$db_prefix;
$out = false;
$file = $this->GetFilenameFromRecord($id);
echo $file;
if ($file !== false)
{
$file = concat_dirs( $root_dir, concat_dirs( $images_dir, $file));
$IFile->Delete( $file);
$sql = "select idesc from $db_prefix"."cimage where imid='$id'";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
list ($desc)=mysql_fetch_row($res);
mysql_free_result($res);
if ($IText->Delete($desc))
{
$sql = "delete from $db_prefix"."cimage where imid='$id'";
$res = mysql_query($sql);
if (mysql_errno()==0)
{
$out = true;
}
}
}
}
return $out;
}
/**
*Delete an array of IImage file_names
*return an array of true/false on ok etc...
*@param array $list IImages to delete
*@return array
*/
function DeleteFromArray($list)
{
$ret=array();
foreach($list as $id)
{
$ret[] = $this->Delete($id);
}
return $ret;
}
/**
Delete of IImage records in a table record
$table, required - a table which needed to delete IText entries
$list, required - a num array of field names for which IText entries must be deleted
$keys, not required - an assoc array of pairs field name/field value to select a range of the table records
If $keys is not defined then IText entries in all records will be deleted for all fields listed in $list
return true/false
*/
function DeleteFromTable($table,$list,$keys="")
{
global $db_prefix;
//prepare fields and where phrases
$fields = implode( ',', $list);
$sql = "select $fields from $db_prefix$table";
if ($keys !== "")
{
$where = "";
foreach($keys as $key=>$value)
{
if ($where !== "")
$where .= " and ";
$where .= "$key='$value'";
}
$sql .= " where " . $where;
}
$res = @mysql_query( $sql );
$ret = true;
if ($res)
{
if ( mysql_numrows($res)>0 )
{
while ($ids = mysql_fetch_array( $res, MYSQL_NUM))
{
// delete the texts of all languages for one record of $table
$deleted = $this->DeleteFromArray($ids);
// test the results of deleting
if (in_array(false, $deleted))
{
$ret = false;
break;
}
}
mysql_free_result( $res );
}
}
else
{
$ret = false;
}
return $ret;
}
/**
* Generate_HTML for an image, $params is an array of all parameters that must override those in database.
* The id is the only parameter required
*/
function Generate_HTML($params)
{
global $IText;
extract($params);
$code ="";
if (!isset($id)) {
return "";
}
$img = $this->Get($id);
if (is_array($img))
{
// img contains default values for this image
if (isset($width))
$img[width] = intval($width);
if (isset($height))
$img[height] = intval($height);
if (isset($border))
$img[border] = intval($border);
if (isset($align))
$img[align] = addslashes($align);
if (!isset($alt))
$img[desc] = $IText->Get($img[desc]);
$code = $this->Generate_HTML_From_Get_Array($img);
}
return $code;
}
/**
* Generate_HTML for an image an array returned by Get.
* No database call is done here.
* If img[desc] is an integer, $IText is called to retrieve the data else it is pasted as text for the alt tag.
*/
function Generate_HTML_From_Get_Array($img)
{
global $IText;
if (is_int($img[desc]))
$img[desc] = $IText->Get($img[desc]);
switch(strtolower($img[ext]))
{
case 'jpg':
case 'jpeg':
case 'gif':
case 'png':
$code = "<img src=\"$img[url]\" width=\"$img[width]\" height=\"$img[height]\" border=\"$img[border]\" align=\"$img[align]\" alt=\"$img[desc]\">\n";
break;
case 'swf':
$code = "<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
codebase=\"http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0\"
ID=\"$img[desc]\" WIDTH=\"$img[width]\" HEIGHT=\"$img[height]\">
<PARAM NAME=movie VALUE=\"$img[url]\">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>
<EMBED src=\"$img[url]\" quality=high
WIDTH=\"$img[width]\" HEIGHT=\"$img[height]\" TYPE=\"application/x-shockwave-flash\"
PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">
</EMBED>
</OBJECT>";
break;
}
return $code;
}
}
$IImage = new A_IImage();
/** IFile is a class to help uploading files on the server.
* Files are not stored nor listed in db, just copied to a chmoded 777 path on the server.
* @author REGNI Giorgio
* @package Developers
* @subpackage Helper functions
*/
class A_IFile {
var $max_file_size = 240800; # in bytes = 200k
var $NO_ERROR = 0;
var $ERROR_NOT_FOUND = -1;
var $ERROR_COPY = -2;
/**
* This function will copy the file temporary $tmp_file (given by a browser file form input)
* to the $directory and name the new file $file_name.
* overwrite the file if it already exists
* returns A_IFile->NO_ERROR on success
* returns A_IFile->ERROR_NOT_FOUND when tmp_file can't be found
* returns A_IFile->ERROR_COPY when it can copy to the directory
*/
function upload($tmp_file,$file_name,$directory)
{
if (file_exists($tmp_file))
{
$file = concat_dirs($directory,$file_name);
if (file_exists($file))
$this->Delete($file);
if (@copy($tmp_file, $file)) {
$out = $this->NO_ERROR;
}
else
$out = $this->ERROR_COPY;
}
else
$out = $this->ERROR_NOT_FOUND;
return $out;
}
/**
* This function lists a directory
* returns an array of all files found in the directory except . and ..
*/
function dir($directory,$fileonly=false)
{
$out = array();
$handle = dir($directory);
while ($file = @$handle->read()) {
if (($file != ".") && ($file != "..")) {
if ( (!$fileonly) or ($fileonly and is_file(concat_dirs($directory,$file))))
$out [] = $file;
}
}
return $out;
}
/**
* This function searchs files that starts with $file_start in a given directory
* returns an array of all files found
*/
function search_files($directory,$file_start)
{
$out = array();
$lenght = strlen($file_start);
$handle = dir($directory);
while ($file = @$handle->read()) {
if (($file != ".") && ($file != "..") and is_file(concat_dirs($directory,$file))) {
echo substr ($file,0,$lenght)."<br>";
if (strcmp(substr ($file,0,$lenght),$file_start)==0 )
$out [] = $file;
}
}
return $out;
}
/**
* This function deletes a file
*@param string $file
*@return boolean true/false
*/
function delete($file)
{
if ( @unlink($file))
return true;
else
return false;
}
}
$IFile = new A_IFile();
?>