1<?php
2	class Sites_SO
3	{
4		var $db;
5
6		function Sites_SO()
7		{
8			$this->db = $GLOBALS['phpgw']->db;
9		}
10
11		function list_siteids()
12		{
13			$result = array();
14
15			$this->db->query('SELECT site_id FROM phpgw_sitemgr_sites', __LINE__, __FILE__);
16			while ($this->db->next_record())
17			{
18				$result[] = $this->db->f('site_id');
19			}
20			return $result;
21		}
22
23		function getWebsites($limit,$start,$sort,$order,$query,&$total)
24		{
25			if ($limit)
26			{
27				if (!$sort)
28				{
29					$sort = 'DESC';
30				}
31				if ($query)
32				{
33					$query = $this->db->db_addslashes($query);
34					$whereclause = "WHERE site_name LIKE '%$query%'"
35						. "OR site_url LIKE '%$query%'"
36						. "OR site_dir LIKE '%$query%'";
37				}
38				if ($order)
39				{
40					$orderclause = 'ORDER BY ' . $this->db->db_addslashes($order)
41							. ' ' . $this->db->db_addslashes($sort);
42				}
43				else
44				{
45					$orderclause = 'ORDER BY site_name ASC';
46				}
47				$sql = "SELECT site_id,site_name,site_url from phpgw_sitemgr_sites $whereclause $orderclause";
48				$this->db->query($sql,__LINE__,__FILE__);
49				$total = $this->db->num_rows();
50				$this->db->limit_query($sql,$start,__LINE__,__FILE__);
51			}
52			else
53			{
54				$this->db->query('SELECT site_id,site_name,site_url FROM phpgw_sitemgr_sites', __LINE__, __FILE__);
55			}
56			while ($this->db->next_record())
57			{
58				foreach(array('site_id', 'site_name', 'site_url') as $col)
59				{
60					$site[$col] = $this->db->f($col);
61				}
62				$result[$site['site_id']] = $site;
63			}
64			return $result;
65		}
66
67		function getnumberofsites()
68		{
69			$this->db->query('SELECT COUNT(*) FROM phpgw_sitemgr_sites',__LINE__,__FILE__);
70			$this->db->next_record();
71			return $this->db->f(0);
72		}
73
74		function urltoid($url)
75		{
76			$sql  = 'SELECT site_id FROM phpgw_sitemgr_sites ';
77			$sql .= "WHERE site_url ='" . $this->db->db_addslashes($url) . "'";
78			$this->db->query($sql,__LINE__,__FILE__);
79			$this->db->next_record();
80			return $this->db->f('site_id');
81		}
82
83		function read($id)
84		{
85			$sql =  'SELECT * FROM phpgw_sitemgr_sites ';
86			$sql .= 'WHERE site_id = ' . intval($id);
87			$this->db->query($sql,__LINE__,__FILE__);
88			if ($this->db->next_record())
89			{
90				foreach(
91					array(
92						'site_id', 'site_name', 'site_url', 'site_dir', 'themesel',
93						'site_languages', 'home_page_id', 'anonymous_user','anonymous_passwd'
94					) as $col
95				)
96				{
97					$site[$col] = $this->db->f($col);
98				}
99				return $site;
100			}
101			else
102			{
103				return false;
104			}
105		}
106
107		function read2($id)
108		{
109			$sql  = 'SELECT site_url,site_dir FROM phpgw_sitemgr_sites ';
110			$sql .= 'WHERE site_id = ' . intval($id);
111			$this->db->query($sql,__LINE__,__FILE__);
112			if ($this->db->next_record())
113			{
114				foreach(
115					array(
116						'site_url', 'site_dir'
117					) as $col
118				)
119				{
120					$site[$col] = $this->db->f($col);
121				}
122				return $site;
123			}
124			else
125			{
126				return false;
127			}
128		}
129
130		function add($site)
131		{
132			$cats = CreateObject('phpgwapi.categories',-1,'sitemgr');
133				$data = array
134			(
135				'name'		=> $site['name'],
136				'descr'		=> '',
137				'access'	=> 'public',
138				'parent'	=> 0,
139				'old_parent' => 0
140			);
141			$site_id =  $cats->add($data);
142			$sql = "INSERT INTO phpgw_sitemgr_sites (site_id,site_name,site_url,site_dir,anonymous_user,anonymous_passwd) VALUES ($site_id,'" .
143				$site['name'] . "','" . $site['url'] . "','" . $site['dir'] . "','" . $site['anonuser'] . "','" . $site['anonpasswd'] .
144				"')";
145			$this->db->query($sql,__LINE__,__FILE__);
146			return $site_id;
147		}
148
149		function update($id,$site)
150		{
151			$sql = "UPDATE phpgw_sitemgr_sites SET site_name = '" . $site['name'] . "', site_url = '" . $site['url'] . "', site_dir = '" .
152				$site['dir'] . "', anonymous_user = '" . $site['anonuser'] . "', anonymous_passwd = '" . $site['anonpasswd'] .
153				"' WHERE site_id = $id";
154			 $this->db->query($sql,__LINE__,__FILE__);
155		}
156
157		function delete($id)
158		{
159			$sql = "DELETE FROM phpgw_sitemgr_sites WHERE site_id = $id";
160			$this->db->query($sql,__LINE__,__FILE__);
161		}
162
163		function saveprefs($prefs)
164		{
165			$sql = "UPDATE phpgw_sitemgr_sites SET themesel = '" . $prefs['themesel'] . "', site_languages = '" . $prefs['site_languages'] .
166				"', home_page_id = " . $prefs['home_page_id'] . " WHERE site_id = " . CURRENT_SITE_ID;
167			$this->db->query($sql,__LINE__,__FILE__);
168		}
169	}
170