2002-09-01 20:59:01 +02:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
|
|
|
* phpGroupWare - InfoLog Links *
|
|
|
|
* http://www.phpgroupware.org *
|
|
|
|
* Written by Ralf Becker <RalfBecker@outdoor-training.de> *
|
|
|
|
* -------------------------------------------- *
|
|
|
|
* 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$ */
|
|
|
|
|
2002-10-18 01:34:28 +02:00
|
|
|
$GLOBALS['phpgw_info']['flags']['included_classes']['solink'] = True;
|
2002-09-01 20:59:01 +02:00
|
|
|
/*!
|
|
|
|
@class solink
|
2002-09-02 13:14:30 +02:00
|
|
|
@author ralfbecker
|
2003-06-14 15:51:53 +02:00
|
|
|
@copyright GPL - GNU General Public License
|
2002-09-01 20:59:01 +02:00
|
|
|
@abstract generalized linking between entries of phpGroupware apps - DB layer
|
2003-06-14 15:51:53 +02:00
|
|
|
@discussion This class is to access the links in the DB<br>
|
|
|
|
Links have to ends each pointing two an entry, each entry is a double:<br>
|
|
|
|
app app-name or directory-name of an phpgw application, eg. 'infolog'<br>
|
|
|
|
id this is the id, eg. an integer or a tupple like '0:INBOX:1234'
|
|
|
|
@note All vars passed to this class are run either through addslashes or intval
|
|
|
|
to prevent query insertion and to get pgSql 7.3 compatibility.
|
2002-09-01 20:59:01 +02:00
|
|
|
*/
|
|
|
|
class solink // DB-Layer
|
|
|
|
{
|
|
|
|
var $public_functions = array
|
|
|
|
(
|
|
|
|
'link' => True,
|
|
|
|
'get_links' => True,
|
|
|
|
'unlink' => True,
|
2002-10-18 00:02:44 +02:00
|
|
|
'chown' => True,
|
|
|
|
'get_link' => True
|
2002-09-01 20:59:01 +02:00
|
|
|
);
|
2002-09-04 01:09:21 +02:00
|
|
|
var $db;
|
2002-09-01 20:59:01 +02:00
|
|
|
var $user;
|
|
|
|
var $db_name = 'phpgw_links';
|
2003-02-22 13:04:12 +01:00
|
|
|
var $debug;
|
2002-09-01 20:59:01 +02:00
|
|
|
|
|
|
|
/*!
|
2002-09-02 13:14:30 +02:00
|
|
|
@function solink
|
|
|
|
@syntax solink( )
|
|
|
|
@author ralfbecker
|
2002-09-01 20:59:01 +02:00
|
|
|
@abstract constructor
|
|
|
|
*/
|
|
|
|
function solink( )
|
|
|
|
{
|
|
|
|
$this->db = $GLOBALS['phpgw']->db;
|
|
|
|
$this->user = $GLOBALS['phpgw_info']['user']['account_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2002-09-02 13:14:30 +02:00
|
|
|
@function link
|
2002-09-04 01:09:21 +02:00
|
|
|
@syntax link( $app1,$id1,$app2,$id2,$remark='',$user=0 )
|
2002-09-02 13:14:30 +02:00
|
|
|
@author ralfbecker
|
2002-09-04 01:09:21 +02:00
|
|
|
@abstract creats a link between $app1,$id1 and $app2,$id2
|
2002-09-01 20:59:01 +02:00
|
|
|
@param $remark Remark to be saved with the link (defaults to '')
|
|
|
|
@param $owner Owner of the link (defaults to user)
|
2002-09-02 13:14:30 +02:00
|
|
|
@discussion Does NOT check if link already exists
|
2002-10-18 00:02:44 +02:00
|
|
|
@result False (for db or param-error) or link_id for success
|
2002-09-01 20:59:01 +02:00
|
|
|
*/
|
2002-10-09 02:37:00 +02:00
|
|
|
function link( $app1,$id1,$app2,$id2,$remark='',$owner=0,$lastmod=0 )
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
2002-10-20 17:06:52 +02:00
|
|
|
if ($this->debug)
|
2002-09-04 01:09:21 +02:00
|
|
|
{
|
|
|
|
echo "<p>solink.link('$app1',$id1,'$app2',$id2,'$remark',$owner)</p>\n";
|
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
if ($app1 == $app2 && $id1 == $id2 ||
|
|
|
|
$id1 == '' || $id2 == '' || $app1 == '' || $app2 == '')
|
|
|
|
{
|
2002-10-18 00:02:44 +02:00
|
|
|
return False; // dont link to self or other nosense
|
|
|
|
}
|
2003-06-14 15:51:53 +02:00
|
|
|
if ($link = $this->get_link($app1,$id1,$app2,$id2))
|
2002-10-18 00:02:44 +02:00
|
|
|
{
|
|
|
|
return $link['link_id']; // link alread exist
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
if (!$owner)
|
|
|
|
{
|
|
|
|
$owner = $this->user;
|
|
|
|
}
|
2003-06-14 15:51:53 +02:00
|
|
|
$vars2addslashes = array('app1','id1','app2','id2','remark');
|
|
|
|
foreach ($vars2addslashes as $var)
|
|
|
|
{
|
|
|
|
$$var = $this->db->db_addslashes($$var);
|
|
|
|
}
|
2002-10-09 02:37:00 +02:00
|
|
|
if (!$lastmod)
|
|
|
|
{
|
|
|
|
$lastmod = time();
|
2002-10-18 00:02:44 +02:00
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
$sql = "INSERT INTO $this->db_name (link_app1,link_id1,link_app2,link_id2,link_remark,link_lastmod,link_owner) ".
|
2003-06-14 15:51:53 +02:00
|
|
|
" VALUES ('$app1','$id1','$app2','$id2','$remark',".intval($lastmod).','.intval($owner).')';
|
2002-09-01 20:59:01 +02:00
|
|
|
|
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
echo "<p>solink.link($app1,$id1,$app2,$id2,'$remark',$owner) sql='$sql'</p>\n";
|
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
$this->db->query($sql,__LINE__,__FILE__);
|
2002-09-01 20:59:01 +02:00
|
|
|
|
2002-10-18 00:02:44 +02:00
|
|
|
return $this->db->errno ? False : $this->db->get_last_insert_id($this->db_name,'link_id');
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2002-09-02 13:14:30 +02:00
|
|
|
@function get_links
|
2002-09-04 01:09:21 +02:00
|
|
|
@syntax get_links( $app,$id,$only_app='',$only_name='',$order='link_lastmod DESC' )
|
2002-09-02 13:14:30 +02:00
|
|
|
@author ralfbecker
|
2002-09-04 01:09:21 +02:00
|
|
|
@abstract returns array of links to $app,$id
|
|
|
|
@param $only_app if set return only links from $only_app (eg. only addressbook-entries) or NOT from if $only_app[0]=='!'
|
2002-09-01 20:59:01 +02:00
|
|
|
@param $order defaults to newest links first
|
2002-10-16 02:22:04 +02:00
|
|
|
@result array of links (only_app: ids) or empty array if no matching links found
|
2002-09-01 20:59:01 +02:00
|
|
|
*/
|
|
|
|
function get_links( $app,$id,$only_app='',$order='link_lastmod DESC' )
|
|
|
|
{
|
|
|
|
$links = array();
|
|
|
|
|
2003-06-14 15:51:53 +02:00
|
|
|
$vars2addslashes = array('app','id','only_app','order');
|
|
|
|
foreach ($vars2addslashes as $var)
|
|
|
|
{
|
|
|
|
$$var = $this->db->db_addslashes($$var);
|
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
$sql = "SELECT * FROM $this->db_name".
|
|
|
|
" WHERE (link_app1 = '$app' AND link_id1 = '$id')".
|
|
|
|
" OR (link_app2 = '$app' AND link_id2 = '$id')".
|
|
|
|
($order != '' ? " ORDER BY $order" : '');
|
|
|
|
|
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
echo "<p>solink.get_links($app,$id,$only_app,$order) sql='$sql'</p>\n";
|
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
$this->db->query($sql,__LINE__,__FILE__);
|
2002-09-01 20:59:01 +02:00
|
|
|
|
2002-09-04 01:09:21 +02:00
|
|
|
if ($not_only = $only_app[0] == '!')
|
|
|
|
{
|
|
|
|
$only_app = substr($only_app,1);
|
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
while ($this->db->next_record())
|
|
|
|
{
|
|
|
|
$row = $this->db->Record;
|
|
|
|
|
2002-10-16 02:22:04 +02:00
|
|
|
if ($row['link_app1'] == $app AND $row['link_id1'] == $id)
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
|
|
|
$link = array(
|
|
|
|
'app' => $row['link_app2'],
|
2003-06-14 15:51:53 +02:00
|
|
|
'id' => $row['link_id2']
|
2002-09-01 20:59:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$link = array(
|
|
|
|
'app' => $row['link_app1'],
|
2003-06-14 15:51:53 +02:00
|
|
|
'id' => $row['link_id1']
|
2002-09-01 20:59:01 +02:00
|
|
|
);
|
|
|
|
}
|
2002-10-09 02:37:00 +02:00
|
|
|
if ($only_app && $not_only == ($link['app'] == $only_app) ||
|
|
|
|
!$GLOBALS['phpgw_info']['user']['apps'][$link['app']])
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2003-06-14 15:51:53 +02:00
|
|
|
$link['remark'] = $row['link_remark'];
|
2002-09-01 20:59:01 +02:00
|
|
|
$link['owner'] = $row['link_owner'];
|
|
|
|
$link['lastmod'] = $row['link_lastmod'];
|
|
|
|
$link['link_id'] = $row['link_id'];
|
|
|
|
|
2002-10-16 02:22:04 +02:00
|
|
|
$links[] = $only_app && !$not_only ? $link['id'] : $link;
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
return $links;
|
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@function get_link
|
|
|
|
@syntax get_link( $app_link_id,$id='',$app2='',$id2='' )
|
|
|
|
@author ralfbecker
|
|
|
|
@abstract returns data of a link
|
|
|
|
@param $app_link_id > 0 link_id of link or app-name of link
|
|
|
|
@param $id,$app2,$id2 other param of the link if not link_id given
|
|
|
|
@result array with link-data or False
|
|
|
|
*/
|
|
|
|
function get_link($app_link_id,$id='',$app2='',$id2='')
|
|
|
|
{
|
2002-10-18 22:20:35 +02:00
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
echo "<p>solink.get_link('$app_link_id',$id,'$app2','$id2')</p>\n";
|
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
$sql = "SELECT * FROM $this->db_name WHERE ";
|
|
|
|
if (intval($app_link_id) > 0)
|
|
|
|
{
|
2003-06-14 15:51:53 +02:00
|
|
|
$sql .= 'link_id='.intval($app_link_id);
|
2002-10-18 00:02:44 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($app_link_id == '' || $id == '' || $app2 == '' || $id2 == '')
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
2003-06-14 15:51:53 +02:00
|
|
|
$vars2addslashes = array('app_link_id','id','app2','id2');
|
|
|
|
foreach ($vars2addslashes as $var)
|
|
|
|
{
|
|
|
|
$$var = $this->db->db_addslashes($$var);
|
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
$sql .= "(link_app1='$app_link_id' AND link_id1='$id' AND link_app2='$app2' AND link_id2='$id2') OR".
|
|
|
|
"(link_app2='$app_link_id' AND link_id2='$id' AND link_app1='$app2' AND link_id1='$id2')";
|
|
|
|
}
|
|
|
|
$this->db->query($sql,__LINE__,__FILE__);
|
|
|
|
|
|
|
|
if ($this->db->next_record())
|
|
|
|
{
|
2002-10-18 22:20:35 +02:00
|
|
|
if ($this->debug)
|
|
|
|
{
|
|
|
|
_debug_array($this->db->Record);
|
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
return $this->db->Record;
|
|
|
|
}
|
|
|
|
return False;
|
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
|
|
|
|
/*!
|
2003-06-14 15:51:53 +02:00
|
|
|
@function unlink
|
|
|
|
@syntax unlink( $link_id,$app='',$id='',$owner='',$app2='',$id2='' )
|
|
|
|
@author ralfbecker
|
2002-09-01 20:59:01 +02:00
|
|
|
@abstract Remove link with $link_id or all links matching given params
|
|
|
|
@param $link_id link-id to remove if > 0
|
2002-10-09 02:37:00 +02:00
|
|
|
@param $app,$id,$owner,$app2,$id2 if $link_id <= 0: removes all links matching the non-empty params
|
2002-09-02 13:14:30 +02:00
|
|
|
@result the number of links deleted
|
2002-09-01 20:59:01 +02:00
|
|
|
*/
|
2002-10-09 02:37:00 +02:00
|
|
|
function unlink($link_id,$app='',$id='',$owner='',$app2='',$id2='')
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
|
|
|
$sql = "DELETE FROM $this->db_name WHERE ";
|
2003-06-14 15:51:53 +02:00
|
|
|
if (intval($link_id) > 0)
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
2003-06-14 15:51:53 +02:00
|
|
|
$sql .= 'link_id='.intval($link_id);
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
elseif ($app == '' AND $owner == '')
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-14 15:51:53 +02:00
|
|
|
$vars2addslashes = array('app','id','app2','id2');
|
|
|
|
foreach ($vars2addslashes as $var)
|
|
|
|
{
|
|
|
|
$$var = $this->db->db_addslashes($$var);
|
|
|
|
}
|
2002-10-09 02:37:00 +02:00
|
|
|
if ($app != '' && $app2 == '')
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
|
|
|
$sql .= "((link_app1='$app'";
|
|
|
|
$sql2 = '';
|
|
|
|
if ($id != '')
|
|
|
|
{
|
|
|
|
$sql .= " AND link_id1='$id'";
|
|
|
|
$sql2 .= " AND link_id2='$id'";
|
|
|
|
}
|
|
|
|
$sql .= ") OR (link_app2='$app'$sql2))";
|
|
|
|
}
|
2002-10-09 02:37:00 +02:00
|
|
|
elseif ($app != '' && $app2 != '')
|
|
|
|
{
|
|
|
|
$sql .= "((link_app1='$app' AND link_id1='$id' AND link_app2='$app2' AND link_id2='$id2') OR";
|
|
|
|
$sql .= " (link_app1='$app2' AND link_id1='$id2' AND link_app2='$app' AND link_id2='$id'))";
|
|
|
|
}
|
2002-09-01 20:59:01 +02:00
|
|
|
if ($owner != '')
|
|
|
|
{
|
2003-06-14 15:51:53 +02:00
|
|
|
$sql .= ($app != '' ? ' AND ' : '') . 'link_owner='.intval($owner);
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->debug)
|
|
|
|
{
|
2002-10-09 02:37:00 +02:00
|
|
|
echo "<p>solink.unlink($link_id,$app,$id,$owner,$app2,$id2) sql='$sql'</p>\n";
|
2002-09-01 20:59:01 +02:00
|
|
|
}
|
2002-10-18 00:02:44 +02:00
|
|
|
$this->db->query($sql,__LINE__,__FILE__);
|
2002-09-01 20:59:01 +02:00
|
|
|
|
|
|
|
return $this->db->affected_rows();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2003-06-14 15:51:53 +02:00
|
|
|
@function chown
|
|
|
|
@syntax chown( $owner,$new_owner )
|
|
|
|
@author ralfbecker
|
2002-09-01 20:59:01 +02:00
|
|
|
@abstract Changes ownership of all links from $owner to $new_owner
|
|
|
|
@discussion This is needed when a user/account gets deleted
|
2002-09-02 13:14:30 +02:00
|
|
|
@discussion Does NOT change the modification-time
|
|
|
|
@result the number of links changed
|
2002-09-01 20:59:01 +02:00
|
|
|
*/
|
|
|
|
function chown($owner,$new_owner)
|
|
|
|
{
|
2003-06-14 15:51:53 +02:00
|
|
|
if (intval($owner) <= 0 || intval($new_owner) <= 0)
|
2002-09-01 20:59:01 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2003-06-14 15:51:53 +02:00
|
|
|
$this->db->query("UPDATE $this->db_name SET owner=".intval($new_owner).' WHERE owner='.intval($owner),__LINE__,__FILE__);
|
2002-09-01 20:59:01 +02:00
|
|
|
|
|
|
|
return $this->db->affected_rows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|