egroupware_official/infolog/inc/class.soinfolog.inc.php

287 lines
8.2 KiB
PHP
Raw Normal View History

2001-07-12 01:17:32 +02:00
<?php
/**************************************************************************\
* phpGroupWare - InfoLog *
* http://www.phpgroupware.org *
* Written by Ralf Becker <RalfBecker@outdoor-training.de> *
* originaly based on todo written by Joseph Engo <jengo@phpgroupware.org> *
* -------------------------------------------- *
* This program 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. *
\**************************************************************************/
/* $Id$ */
class soinfolog // DB-Layer
{
var $db,$db2;
var $grants;
var $data = array( );
function soinfolog( $info_id = 0)
{
2001-07-12 01:17:32 +02:00
global $phpgw;
$this->db = $phpgw->db;
$this->grants = $phpgw->acl->get_grants('infolog');
$this->read( $info_id );
2001-07-12 01:17:32 +02:00
}
function readProj($proj_id)
{
2001-07-12 01:17:32 +02:00
if ($proj_id) {
if (!is_object($this->projects)) {
$this->projects = createobject('projects.projects');
}
if (list( $proj ) = $this->projects->read_single_project( $proj_id))
2001-07-12 01:17:32 +02:00
return $proj;
}
return False;
}
function readAddr($addr_id)
{
2001-07-12 01:17:32 +02:00
if ($addr_id) {
if (!is_object($this->contacts)) {
$this->contacts = createobject('phpgwapi.contacts');
}
if (list( $addr ) = $this->contacts->read_single_entry( $addr_id ))
return $addr;
}
return False;
}
function check_access( $info_id,$required_rights )
{
2001-07-12 01:17:32 +02:00
global $phpgw_info;
if ($info_id != $this->data['info_id']) // already loaded?
{
// dont change our own internal data,
// dont use new as it changes $phpgw->db
$private_info = $this;
2001-07-12 01:17:32 +02:00
$info = $private_info->read($info_id);
}
else
{
2001-07-12 01:17:32 +02:00
$info = $this->data;
}
if (!$info || !$info_id)
{
2001-07-12 01:17:32 +02:00
return False;
}
2001-07-12 01:17:32 +02:00
$owner = $info['info_owner'];
$user = $phpgw_info['user']['account_id'];
2001-07-12 01:17:32 +02:00
$access_ok = $owner == $user || // user has all rights
// ACL only on public entrys || $owner granted _PRIVATE
!!($this->grants[$owner] & $required_rights) &&
($info['info_access'] == 'public' ||
!!($this->grants[$owner] & PHPGW_ACL_PRIVATE));
2001-07-12 01:17:32 +02:00
// echo "check_access(info_id=$info_id (owner=$owner, user=$user),required_rights=$required_rights): access".($access_ok?"Ok":"Denied");
return $access_ok;
}
// sql to be AND into a query to ensure ACL is respected (incl. _PRIVATE)
// filter: none - list all entrys user have rights to see
// private - list only his personal entrys
// (incl. those he is responsible for !!!)
function aclFilter($filter = 'none')
{
global $phpgw_info;
2001-07-12 01:17:32 +02:00
if (isset($this->acl_filter[$filter]))
{
return $this->acl_filter[$filter]; // used cached filter if found
}
if (is_array($this->grants))
{
while (list($user,$grant) = each($this->grants))
{
// echo "<p>grants: user=$user, grant=$grant</p>";
2001-07-12 01:17:32 +02:00
if ($grant & (PHPGW_ACL_READ|PHPGW_ACL_EDIT))
{
2001-07-12 01:17:32 +02:00
$public_user_list[] = $user;
}
2001-07-12 01:17:32 +02:00
if ($grant & PHPGW_ACL_PRIVATE)
{
2001-07-12 01:17:32 +02:00
$private_user_list[] = $user;
}
2001-07-12 01:17:32 +02:00
}
if (count($private_user_list))
{
$has_private_access = 'info_owner IN ('.
implode(',',$private_user_list).')';
2001-07-12 01:17:32 +02:00
}
}
$user = $phpgw_info['user']['account_id'];
$filtermethod = " (info_owner=$user"; // user has all rights
2001-07-12 01:17:32 +02:00
// private: own entries plus the one user is responsible for
if ($filter == 'private')
{
2001-07-12 01:17:32 +02:00
$filtermethod .= " OR info_responsible=$user AND (info_access='public'".($has_private_access?" OR $has_private_access":'').')';
}
else // none --> all entrys user has rights to see
{
if ($has_private_access)
{
2001-07-12 01:17:32 +02:00
$filtermethod .= " OR $has_private_access";
}
if (count($public_user_list))
{
2001-07-12 01:17:32 +02:00
$filtermethod .= " OR (info_access='public' AND info_owner IN(" . implode(',',$public_user_list) . '))';
}
}
$filtermethod .= ') ';
// echo "<p>aclFilter('$filter')(user='$user') = '$filtermethod'</p>";
return $this->acl_filter[$filter] = $filtermethod; // cache the filter
2001-07-12 01:17:32 +02:00
}
function init()
{
2001-07-12 01:17:32 +02:00
global $phpgw_info;
$this->data = array( 'info_owner' => $phpgw_info['user']['account_id'],
'info_pri' => 'normal' );
}
function read($info_id) // did _not_ ensure ACL
{
2001-07-12 01:17:32 +02:00
if ($info_id <= 0 || $info_id != $this->data['info_id'] &&
(!$this->db->query("select * FROM phpgw_infolog where info_id='$info_id'") || !$this->db->next_record()))
{
$this->init( );
return False;
}
if ($info_id != $this->data['info_id']) // data yet read in
{
2001-07-12 01:17:32 +02:00
$this->data = $this->db->Record;
}
return $this->data;
}
function delete($info_id) // did _not_ ensure ACL
{
2001-07-12 01:17:32 +02:00
global $phpgw_info;
2001-07-12 01:17:32 +02:00
$this->db->query("delete FROM phpgw_infolog where info_id='$info_id' or info_id_parent='"
. "$info_id' AND ((info_access='public' and info_owner != '"
. $phpgw_info['user']['account_id'] . "') or (info_owner='"
. $phpgw_info['user']['account_id'] . "'))" ,__LINE__,__FILE__);
if ($this->data['info_id'] == $info_id)
{
2001-07-12 01:17:32 +02:00
$this->init( );
}
2001-07-12 01:17:32 +02:00
}
function write($values) // did _not_ ensure ACL
{
while (list($key,$val) = each($values))
{
2001-07-12 01:17:32 +02:00
$this->data['info_'.$key] = $val; // update internal data
switch ($key)
{
2001-07-12 01:17:32 +02:00
case 'info_id':
break;
2001-07-12 01:17:32 +02:00
case 'des': case 'subject': case 'from': case 'addr':
$val = addslashes($val);
default:
if ($query) $query .= ',';
$query .= "info_$key='$val'";
}
}
if ($values['info_id'])
{
$query = 'update phpgw_infolog set '.$query.' where info_id=\'' .
$values['info_id'] .'\'';
}
else
{
2001-07-12 01:17:32 +02:00
$query = 'insert INTO phpgw_infolog set '.$query;
/*
* need to set $this->data['info_id'] with assigned autoincrement id
* now data will be rereaded
2001-07-12 01:17:32 +02:00
*/
}
$this->db->query($query,__LINE__,__FILE__);
}
function anzSubs( $info_id )
{
$this->db->query('select count(*) FROM phpgw_infolog where '.
"info_id_parent=$info_id",__LINE__,__FILE__);
$this->db->next_record();
return $this->db->f(0);
}
function readIdArray($order,$sort,$filter,$cat_id,$query,$action,$addr_id,
$proj_id,$info_id,$ordermethod,&$start,&$total)
{
if ($order)
{
$ordermethod = 'order by ' . $order . ' ' . $sort;
}
else
{
$ordermethod = 'order by info_datecreated desc'; // newest first
}
if (!$filter)
{
$filter = 'none';
}
$filtermethod = $this->aclFilter($filter);
if ($cat_id)
{
$filtermethod .= " AND info_cat='$cat_id' ";
}
switch ($action)
{
case 'addr': $filtermethod .= " AND info_addr_id=$addr_id ";
break;
case 'proj': $filtermethod .= " AND info_proj_id=$proj_id ";
break;
}
if ($query) // we search in _from, _subject and _des for $query
{
$sql_query = "AND (info_from like '%$query%' OR info_subject ".
"like '%$query%' OR info_des like '%$query%') ";
}
$pid = 'AND info_id_parent='.($action == 'sp' ? $info_id : 0);
if (!$phpgw_info['user']['preferences']['infolog']['listNoSubs'] &&
$action != 'sp')
{
$pid = '';
}
$this->db->query("SELECT COUNT(*) FROM phpgw_infolog WHERE $filtermethod $pid $sql_query",__LINE__,__FILE__);
$this->db->next_record();
$total = $this->db->f(0);
if (!$start || $start > $total)
{
$start = 0;
}
$this->db->limit_query("SELECT info_id,info_id_parent FROM phpgw_infolog WHERE $filtermethod $pid $sql_query $ordermethod",$start,__LINE__,__FILE__);
$ids = array( );
while ($this->db->next_record())
{
$ids[$this->db->f('info_id')] = $this->db->f('info_parent_id');
}
return $ids;
}
2001-07-12 01:17:32 +02:00
}