Administration::get_config_prefix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
1
<?php
2
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
/*********************************************************************************
4
 * SugarCRM Community Edition is a customer relationship management program developed by
5
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
/*********************************************************************************
42
43
 * Description:  TODO: To be written.
44
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
45
 * All Rights Reserved.
46
 * Contributor(s): ______________________________________..
47
 ********************************************************************************/
48
require_once('data/SugarBean.php');
49
require_once('include/OutboundEmail/OutboundEmail.php');
50
51
class Administration extends SugarBean {
52
    var $settings;
53
    var $table_name = "config";
54
    var $object_name = "Administration";
55
    var $new_schema = true;
56
    var $module_dir = 'Administration';
57
    var $config_categories = array(
58
        // 'mail', // cn: moved to include/OutboundEmail
59
        'disclosure', // appended to all outbound emails
60
        'notify',
61
        'system',
62
        'portal',
63
        'proxy',
64
        'massemailer',
65
        'ldap',
66
        'captcha',
67
        'sugarpdf',
68
69
    );
70
    var $disable_custom_fields = true;
71
    var $checkbox_fields = Array("notify_send_by_default", "mail_smtpauth_req", "notify_on", 'portal_on', 'skypeout_on', 'system_mailmerge_on', 'proxy_auth', 'proxy_on', 'system_ldap_enabled','captcha_on');
72
73 71
    function __construct() {
74 71
        parent::__construct();
75
76 71
        $this->setupCustomFields('Administration');
77 71
    }
78
79
    /**
80
     * @deprecated deprecated since version 7.6, PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code, use __construct instead
81
     */
82
    function Administration(){
83
        $deprecatedMessage = 'PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code';
84
        if(isset($GLOBALS['log'])) {
85
            $GLOBALS['log']->deprecated($deprecatedMessage);
86
        }
87
        else {
88
            trigger_error($deprecatedMessage, E_USER_DEPRECATED);
89
        }
90
        self::__construct();
91
    }
92
93
94 69
    function retrieveSettings($category = FALSE, $clean=false) {
95
        // declare a cache for all settings
96 69
        $settings_cache = sugar_cache_retrieve('admin_settings_cache');
97
98 69
        if($clean) {
99 48
            $settings_cache = array();
100
        }
101
102
        // Check for a cache hit
103 69
        if(!empty($settings_cache)) {
104 20
            $this->settings = $settings_cache;
105 20
            if (!empty($this->settings[$category]))
106
            {
107 16
                return $this;
108
            }
109
        }
110
111 56
        if ( ! empty($category) ) {
112 52
            $query = "SELECT category, name, value FROM {$this->table_name} WHERE category = '{$category}'";
113
        } else {
114 4
            $query = "SELECT category, name, value FROM {$this->table_name}";
115
        }
116
117 56
        $result = $this->db->query($query, true, "Unable to retrieve system settings");
118
119 56
        if(empty($result)) {
120
            return NULL;
121
        }
122
123 56
        while($row = $this->db->fetchByAssoc($result)) {
124 25
            if($row['category']."_".$row['name'] == 'ldap_admin_password' || $row['category']."_".$row['name'] == 'proxy_password')
125
                $this->settings[$row['category']."_".$row['name']] = $this->decrypt_after_retrieve($row['value']);
126
            else
127 25
                $this->settings[$row['category']."_".$row['name']] = $row['value'];
128 25
            $this->settings[$row['category']] = true;
129
        }
130 56
        $this->settings[$category] = true;
131
132 56
        if(!isset($this->settings["mail_sendtype"])) {
133
            // outbound email settings
134 50
            $oe = new OutboundEmail();
135 50
            $oe->getSystemMailerSettings();
136
137 50
            foreach ($oe->field_defs as $def) {
138 50
                if (strpos($def, "mail_") !== false)
139 50
                    $this->settings[$def] = $oe->$def;
140
            }
141
        }
142
143
        // At this point, we have built a new array that should be cached.
144 56
        sugar_cache_put('admin_settings_cache',$this->settings);
145 56
        return $this;
146
    }
147
148 1
    function saveConfig() {
149
150
151
        // outbound email settings
152 1
        $oe = new OutboundEmail();
153
154 1
        foreach($_POST as $key => $val) {
155 1
            $prefix = $this->get_config_prefix($key);
156 1
            if(in_array($prefix[0], $this->config_categories)) {
157 1
                if(is_array($val)){
158
                    $val=implode(",",$val);
159
                }
160 1
                $this->saveSetting($prefix[0], $prefix[1], $val);
161
            }
162 1
            if(strpos($key, "mail_") !== false) {
163
                if(in_array($key, $oe->field_defs)) {
164 1
                    $oe->$key = $val;
165
                }
166
            }
167
        }
168
169
        //saving outbound email from here is probably redundant, adding a check to make sure
170
        //smtpserver name is set.
171 1
        if (!empty($oe->mail_smtpserver)) {
172
            $oe->saveSystem();
173
        }
174
175 1
        $this->retrieveSettings(false, true);
176 1
    }
177
178 4
    function saveSetting($category, $key, $value) {
179 4
        $result = $this->db->query("SELECT count(*) AS the_count FROM config WHERE category = '{$category}' AND name = '{$key}'");
180 4
        $row = $this->db->fetchByAssoc($result);
181 4
        $row_count = $row['the_count'];
182
183 4
        if($category."_".$key == 'ldap_admin_password' || $category."_".$key == 'proxy_password')
184
            $value = $this->encrpyt_before_save($value);
185
186 4
        if( $row_count == 0){
187 4
            $result = $this->db->query("INSERT INTO config (value, category, name) VALUES ('$value','$category', '$key')");
188
        }
189
        else{
190 1
            $result = $this->db->query("UPDATE config SET value = '{$value}' WHERE category = '{$category}' AND name = '{$key}'");
191
        }
192 4
        sugar_cache_clear('admin_settings_cache');
193 4
        return $this->db->getAffectedRowCount($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, DBManager::getAffectedRowCount() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
194
    }
195
196 2
    function get_config_prefix($str) {
197 2
        return $str
198 2
            ? Array(substr($str, 0, strpos($str, "_")), substr($str, strpos($str, "_")+1))
199 2
            : Array(false, false);
200
    }
201
}
202
?>
203