Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
31f6ce 1 <?php
M 2 /*
3 Copyright (c) 2008, Till Brehm, projektfarm Gmbh
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation
13       and/or other materials provided with the distribution.
14     * Neither the name of ISPConfig nor the names of its contributors
15       may be used to endorse or promote products derived from this software without
16       specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 class tools_sites {
31
b1a6a5 32     function replacePrefix($name, $dataRecord) {
MC 33         // No input -> no possible output -> go out!
34         if ($name=="") return "";
31f6ce 35
b1a6a5 36         // Array containing keys to search
MC 37         $keywordlist=array('CLIENTNAME', 'CLIENTID', 'DOMAINID');
31f6ce 38
b1a6a5 39         // Try to match the key within the string
MC 40         foreach ($keywordlist as $keyword) {
41             if (substr_count($name, '['.$keyword.']') > 0) {
42                 switch ($keyword) {
43                 case 'CLIENTNAME':
44                     $name=str_replace('['.$keyword.']', $this->getClientName($dataRecord), $name);
45                     break;
46                 case 'CLIENTID':
47                     $name=str_replace('['.$keyword.']', $this->getClientID($dataRecord), $name);
48                     break;
49                 case 'DOMAINID':
a8d947 50                     $name=str_replace('['.$keyword.']', $dataRecord['parent_domain_id'] ? $dataRecord['parent_domain_id'] : '[DOMAINID]', $name);
b1a6a5 51                     break;
MC 52                 }
53             }
54         }
55         return $name;
56     }
31f6ce 57
b1a6a5 58     function removePrefix($name, $currentPrefix, $globalPrefix) {
MC 59         if($name == "") return "";
31f6ce 60
b1a6a5 61         if($currentPrefix === '') return $name; // empty prefix, do not change name
MC 62         if($currentPrefix === '#') $currentPrefix = $globalPrefix; // entry has no prefix set, maybe it was created before this function was introduced
63
64         if($currentPrefix === '') return $name; // no current prefix and global prefix is empty -> nothing to remove here.
65
66         return preg_replace('/^' . preg_quote($currentPrefix, '/') . '/', '', $name); // return name without prefix
67     }
68
69     function getPrefix($currentPrefix, $userPrefix, $adminPrefix = false) {
70         global $app;
71
72         if($currentPrefix !== '#') return $currentPrefix; // return the currently set prefix for this entry (# = no prefix set yet)
73
74         if($adminPrefix === false) $adminPrefix = $userPrefix;
75
76         if($_SESSION["s"]["user"]["typ"] == 'admin' || $app->auth->has_clients($_SESSION['s']['user']['userid'])) return $adminPrefix;
77         else return $userPrefix;
78     }
79
80     function getClientName($dataRecord) {
81         global $app, $conf;
82         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
83             // Get the group-id of the user if the logged in user is neither admin nor reseller
84             $client_group_id = $_SESSION["s"]["user"]["default_group"];
85         } else {
86             // Get the group-id from the data itself
87             if(isset($dataRecord['client_group_id'])) {
88                 $client_group_id = $dataRecord['client_group_id'];
89             } elseif (isset($dataRecord['parent_domain_id'])) {
cc7a82 90                 $tmp = $app->db->queryOneRecord("SELECT sys_groupid FROM web_domain WHERE domain_id = ?", $dataRecord['parent_domain_id']);
b1a6a5 91                 $client_group_id = $tmp['sys_groupid'];
MC 92             } elseif(isset($dataRecord['sys_groupid'])) {
93                 $client_group_id = $dataRecord['sys_groupid'];
94             } else {
a8d947 95                 return '[CLIENTNAME]';
b1a6a5 96             }
MC 97         }
98
cc7a82 99         $tmp = $app->db->queryOneRecord("SELECT name FROM sys_group WHERE groupid = ?", $client_group_id);
b1a6a5 100         $clientName = $tmp['name'];
MC 101         if ($clientName == "") $clientName = 'default';
102         $clientName = $this->convertClientName($clientName);
103         return $clientName;
104     }
105
106     function getClientID($dataRecord) {
107         global $app, $conf;
108
109         if($_SESSION["s"]["user"]["typ"] != 'admin' && !$app->auth->has_clients($_SESSION['s']['user']['userid'])) {
110             // Get the group-id of the user
111             $client_group_id = $_SESSION["s"]["user"]["default_group"];
112         } else {
113             // Get the group-id from the data itself
114             if(isset($dataRecord['client_group_id'])) {
115                 $client_group_id = $dataRecord['client_group_id'];
116             } elseif (isset($dataRecord['parent_domain_id']) && $dataRecord['parent_domain_id'] != 0) {
cc7a82 117                 $tmp = $app->db->queryOneRecord("SELECT sys_groupid FROM web_domain WHERE domain_id = ?", $dataRecord['parent_domain_id']);
b1a6a5 118                 $client_group_id = $tmp['sys_groupid'];
MC 119             } elseif(isset($dataRecord['sys_groupid'])) {
120                 $client_group_id = $dataRecord['sys_groupid'];
121             } else {
a8d947 122                 return '[CLIENTID]';
b1a6a5 123             }
MC 124         }
cc7a82 125         $tmp = $app->db->queryOneRecord("SELECT client_id FROM sys_group WHERE groupid = ?", $client_group_id);
b1a6a5 126         $clientID = $tmp['client_id'];
MC 127         if ($clientID == '') $clientID = '0';
128         return $clientID;
129     }
130
131     function convertClientName($name){
132         $allowed = 'abcdefghijklmnopqrstuvwxyz0123456789_';
133         $res = '';
134         $name = strtolower(trim($name));
135         for ($i=0; $i < strlen($name); $i++){
136             if ($name[$i] == ' ') continue;
137             if (strpos($allowed, $name[$i]) !== false){
138                 $res .= $name[$i];
139             }
140             else {
141                 $res .= '_';
142             }
143         }
144         return $res;
145     }
146
2af58c 147     /* TODO: rewrite SQL */
9ec304 148     function getDomainModuleDomains($not_used_in_table = null, $selected_domain = null) {
b1a6a5 149         global $app;
MC 150
151         $sql = "SELECT domain_id, domain FROM domain WHERE";
9ec304 152         if ($not_used_in_table) {
SC 153             if (strpos($not_used_in_table, 'dns') !== false) {
154                 $field = "origin";
155                 $select = "SUBSTRING($field, 1, CHAR_LENGTH($field) - 1)";
156             } else {
157                 $field = "domain";
158                 $select = $field;
159             }
baf5dd 160             $sql .= " domain NOT IN (SELECT $select FROM ?? WHERE $field != ?) AND";
9ec304 161         }
b1a6a5 162         if ($_SESSION["s"]["user"]["typ"] == 'admin') {
MC 163             $sql .= " 1";
164         } else {
165             $groups = ( $_SESSION["s"]["user"]["groups"] ) ? $_SESSION["s"]["user"]["groups"] : 0;
166             $sql .= " sys_groupid IN (".$groups.")";
167         }
168         $sql .= " ORDER BY domain";
baf5dd 169         return $app->db->queryAllRecords($sql, $not_used_in_table, $selected_domain);
b1a6a5 170     }
MC 171
2af58c 172     /* TODO: rewrite SQL */
b1a6a5 173     function checkDomainModuleDomain($domain_id) {
MC 174         global $app;
175
176         $sql = "SELECT domain_id, domain FROM domain WHERE domain_id = " . $app->functions->intval($domain_id);
177         if ($_SESSION["s"]["user"]["typ"] != 'admin') {
178             $groups = ( $_SESSION["s"]["user"]["groups"] ) ? $_SESSION["s"]["user"]["groups"] : 0;
179             $sql .= " AND sys_groupid IN (".$groups.")";
180         }
181         $domain = $app->db->queryOneRecord($sql);
182         if(!$domain || !$domain['domain_id']) return false;
183         return $domain['domain'];
184     }
2af58c 185     
MC 186     /* TODO: rewrite SQL */
3e94c8 187     function getClientIdForDomain($domain_id) {
SC 188         global $app;
189
190         $sql = "SELECT sys_groupid FROM domain WHERE domain_id = " . $app->functions->intval($domain_id);
191         if ($_SESSION["s"]["user"]["typ"] != 'admin') {
192             $groups = ( $_SESSION["s"]["user"]["groups"] ) ? $_SESSION["s"]["user"]["groups"] : 0;
193             $sql .= " AND sys_groupid IN (".$groups.")";
194         }
195         $domain = $app->db->queryOneRecord($sql);
196         if(!$domain || !$domain['sys_groupid']) return false;
197         return $domain['sys_groupid'];
198     }
199
31f6ce 200 }
M 201
202 ?>