]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/utils/db_utils.php
Release 6.2.3
[Github/sugarcrm.git] / include / utils / db_utils.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38
39
40 /**
41  * @deprecated use DBManager::convert() instead.
42  */
43 function db_convert($string, $type, $additional_parameters=array(),$additional_parameters_oracle_only=array())
44         {
45     return $GLOBALS['db']->convert($string, $type, $additional_parameters, $additional_parameters_oracle_only);
46             }
47
48 /**
49  * @deprecated use DBManager::concat() instead.
50  */
51 function db_concat($table, $fields)
52         {
53     return $GLOBALS['db']->concat($table, $fields);
54 }
55
56 /**
57  * @deprecated use DBManager::fromConvert() instead.
58  */
59 function from_db_convert($string, $type)
60         {
61     return $GLOBALS['db']->fromConvert($string, $type);
62         }
63
64 $toHTML = array(
65         '"' => '&quot;',
66         '<' => '&lt;',
67         '>' => '&gt;',
68         "'" => '&#039;',
69 );
70 $GLOBALS['toHTML_keys'] = array_keys($toHTML);
71 $GLOBALS['toHTML_values'] = array_values($toHTML);
72
73 /**
74  * Replaces specific characters with their HTML entity values
75  * @param string $string String to check/replace
76  * @param bool $encode Default true
77  * @return string
78  *
79  * @todo Make this utilize the external caching mechanism after re-testing (see
80  *       log on r25320).
81  */
82 function to_html($string, $encode=true){
83         if (empty($string)) {
84                 return $string;
85         }
86         static $cache = array();
87         global $toHTML;
88         if (isset($cache['c'.$string])) {
89             return $cache['c'.$string];
90         }
91
92         $cache_key = 'c'.$string;
93
94         if($encode && is_string($string)){//$string = htmlentities($string, ENT_QUOTES);
95                 /*
96                  * cn: bug 13376 - handle ampersands separately
97                  * credit: ashimamura via bug portal
98                  */
99                 //$string = str_replace("&", "&amp;", $string);
100
101                 if(is_array($toHTML)) { // cn: causing errors in i18n test suite ($toHTML is non-array)
102                         $string = str_replace(
103                                 $GLOBALS['toHTML_keys'],
104                                 $GLOBALS['toHTML_values'],
105                                 $string
106                         );
107                 }
108         }
109         $cache[$cache_key] = $string;
110         return $cache[$cache_key];
111 }
112
113 /**
114  * Replaces specific HTML entity values with the true characters
115  * @param string $string String to check/replace
116  * @param bool $encode Default true
117  * @return string
118  */
119 function from_html($string, $encode=true) {
120     if (!is_string($string) || !$encode) {
121         return $string;
122     }
123
124         global $toHTML;
125     static $toHTML_values = null;
126     static $toHTML_keys = null;
127     static $cache = array();
128     if (!isset($toHTML_values) || !empty($GLOBALS['from_html_cache_clear'])) {
129         $toHTML_values = array_values($toHTML);
130         $toHTML_keys = array_keys($toHTML);
131     }
132
133     // Bug 36261 - Decode &amp; so we can handle double encoded entities
134         $string = str_replace("&amp;", "&", $string);
135
136     if (!isset($cache[$string])) {
137         $cache[$string] = str_replace($toHTML_values, $toHTML_keys, $string);
138     }
139     return $cache[$string];
140 }
141
142 /**
143  * @deprecated
144  * @todo this function is only used by one function ( run_upgrade_wizard_sql() ), which isn't
145  *       used either; trying kill this off
146  */
147 function run_sql_file( $filename )
148 {
149     if( !is_file( $filename ) ){
150         print( "Could not find file: $filename <br>" );
151         return( false );
152     }
153
154
155     $contents = sugar_file_get_contents($filename);
156
157     $lastsemi   = strrpos( $contents, ';') ;
158     $contents   = substr( $contents, 0, $lastsemi );
159     $queries    = explode( ';', $contents );
160     $db         = DBManagerFactory::getInstance();
161
162     foreach( $queries as $query ){
163         if( !empty($query) ){
164                         if($db->dbType == 'oci8')
165                         {
166                         }
167                         else
168                         {
169                                 $db->query( $query.';', true, "An error has occured while running.<br>" );
170                         }
171         }
172     }
173     return( true );
174 }
175
176 /*
177  * Return a version of $proposed that can be used as a column name in any of our supported databases
178  * Practically this means no longer than 25 characters as the smallest identifier length for our supported DBs is 30 chars for Oracle plus we add on at least four characters in some places (for indicies for example)
179  * @param string $name Proposed name for the column
180  * @param string $ensureUnique
181  * @return string Valid column name trimmed to right length and with invalid characters removed
182  */
183  function getValidDBName ($name, $ensureUnique = false, $maxLen = 30)
184 {
185     // first strip any invalid characters - all but alphanumerics and -
186     $name = preg_replace ( '/[^\w-]+/i', '', $name ) ;
187     $len = strlen ( $name ) ;
188     $result = $name;
189     if ($ensureUnique)
190     {
191         $md5str = md5($name);
192         $tail = substr ( $name, -8) ;
193         $temp = substr($md5str , strlen($md5str)-4 );
194         $result = substr ( $name, 0, 7) . $temp . $tail ;
195     }else if ($len > ($maxLen - 5))
196     {
197         $result = substr ( $name, 0, 8) . substr ( $name, 8 - $maxLen + 5);
198     }
199     return strtolower ( $result ) ;
200 }
201
202 ?>