Location: PHPKode > projects > PhpProjectMaster > www/inc/att.lib.php
<?php
/* 
 att related functions
 (c) 2004-2007 by "Oleg Savchuk" <hide@address.com>

 part of phpProjectMaster project
 http://phpprojmaster.sourceforge.net

 The contents of this file are subject to the GNU GENERAL PUBLIC LICENSE
 http://www.gnu.org/copyleft/gpl.html
*/

 require_once "sitelib.php";
 require_once "form_utils.php";
 require_once "upload_utils.php";

//preset module variables

 $MAX_ATTACHMENTS=32; //just for example
 $att_vars=array(
   'table_name'   => 'att',
   'table_key_id' => 'att_id',
   'upload_url'   => "$root_url/php/att.php",
   'upload_path'  => "$site_root_offline/upload/att",
 );


//get item fields from database
function get_att($item_id, $comm_id=0, $t_id=0, $p_id=0){
 global $att_vars;

 $asql="";
 if ($comm_id) $asql.=" and comm_id=$comm_id";
 if ($t_id) $asql.=" and t_id=$t_id";
 if ($p_id) $asql.=" and p_id=$p_id";

 $sql="select * from $att_vars[table_name] where $att_vars[table_key_id]=$item_id $asql";
 return db_row($sql);
}

//mark item as removed
function delete_att($item_id, $comm_id=0, $t_id=0, $p_id=0){
 global $att_vars;

 $asql="";
 if ($comm_id) $asql.=" and comm_id=$comm_id";
 if ($t_id) $asql.=" and t_id=$t_id";
 if ($p_id) $asql.=" and p_id=$p_id";

 db_query("update $att_vars[table_name] set status=127 where $att_vars[table_key_id]=$item_id $asql");
}

//********************* for uploads
// for uploads
function get_upload_url_att($item_id, $is_preview=''){
 global $att_vars;

 $nameadd='';
 if ($is_preview) $nameadd='&preview=1';

 return $att_vars['upload_url']."?id=$item_id$nameadd";
}

/*
function get_upload_url_att($item_id, $is_preview=''){
 global $att_vars;

 $nameadd='';
 if ($is_preview) $nameadd='_s';

 return get_upload_url($item_id, $att_vars['upload_path'], $att_vars['upload_url'], $nameadd);
}
*/

function get_upload_path_att($item_id, $is_preview='', $ext='', $nocheck=''){
 global $att_vars;

 $nameadd='';
 if ($is_preview) $nameadd='_s';

 return get_upload_path($item_id, $att_vars['upload_path'], $nameadd, $ext, $nocheck);
}



//************ bytes 2 normally read value:
// 123 - 123 b
// 1234 - 1.24 Kb
// 12345 - 12.35 Kb
// 1234567 - 1.23 Mb
// 1234567890 - 1.23 Gb
function bytes2str($b){
 $result=$b;

 if ($b<1024){
    $result.=" b";

 }elseif ($b<1048576){
    $result=(ceil($b/1024*100)/100)." Kb";

 }elseif ($b<1073741824){
    $result=(ceil($b/1048576*100)/100)." Mb";
 }else{
    $result=(ceil($b/1073741824*100)/100)." Gb";
 }

 return $result;
}


//************************ add WITH STATUS =1 (under upload)
function add_att($p_id, $t_id, $comm_id, $fname, $iname=''){
 global $att_vars;

 if (!$p_id || !$fname) return 0;

 $path_parts=pathinfo( strtolower($fname) ); #get the extension
 if (!$iname) $iname=$path_parts['basename'];
 $path_parts['extension']=jpeg2jpg( $path_parts['extension'] );

 $sf=array(
  'p_id'  => $p_id,
  't_id'  => $t_id,
  'comm_id'  => $comm_id,

  'fname' => $fname,
  'ext'   => $path_parts['extension'],
  'iname' => $iname,
  'status' => 1,
 );
 $sql="insert into att ".get_sqlinsert_set($sf,', add_time',', now()');
 db_query($sql);
 $item_id=get_identity();

 return $item_id;
}

//***************
function add_ok_att($item_id, $ext=''){
 $ext=jpeg2jpg( $ext );
 $fsize=get_att_size($item_id, $ext);

 db_query("update att set status=0, fsize=$fsize where att_id=$item_id");
}

//***************
function get_att_size($item_id, $ext=''){
 global $att_vars;

 if (!$ext){
    $hATT=get_att($item_id);
    $ext=$hATT['ext'];
 }

 $file_path=get_upload_path($item_id, $att_vars['upload_path'], '', $ext);
 return filesize($file_path)+0;
}

//************
function get_att_list_dr($p_id, $t_id='', $comm_id=''){
 global $IMAGES_EXT;

 if (!$p_id && !$t_id && !$comm_id) return;

 $asql="";
 if (strlen($comm_id)) $asql.=" and comm_id=$comm_id";
 if (strlen($t_id)) $asql.=" and t_id=$t_id";
 if (strlen($p_id)) $asql.=" and p_id=$p_id";

 $rows=db_array("select * from att where status=0 $asql");
 foreach($rows as $key => $row){
   $rows[$key]['fsize_human']=bytes2str( $rows[$key]['fsize'] );
   $rows[$key]['is_img']     = ( $IMAGES_EXT[ $rows[$key]['ext'] ] )?1:0;
 }

 return $rows;
}

//************ check access of logged user to att
// returns:
// 100 - full access: view, create, edit, delete + any other
// 40 - view+create+edit+delete
// 30 - view+create+edit
// 20 - view+create
// 10 - view only
// 0 - no access
function check_access_att($att_id){
 #!!!TODO

 if (!$_SESSION['u_id']){
    return 0;
 }
# if ($_SESSION['access_level']<=0){  #not logged in - no access to attachments at all
#    return 0;
# }

 return 100;  //full access
}

?>
Return current item: PhpProjectMaster