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-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
42 // The history of upgrades on the system
43 class UpgradeHistory extends SugarBean
45 var $new_schema = true;
46 var $module_dir = 'Administration';
61 var $tracker_visibility = false;
62 var $table_name = "upgrade_history";
63 var $object_name = "UpgradeHistory";
64 var $column_fields = Array( "id", "filename", "md5sum", "type", "version", "status", "date_entered" );
65 var $disable_custom_fields = true;
69 $this->db->query( "delete from " . $this->table_name . " where id = " . $this->db->quoted($this->id));
72 function UpgradeHistory()
75 $this->disable_row_level_security = true;
78 function getAllOrderBy($orderBy){
79 $query = "SELECT id FROM " . $this->table_name . " ORDER BY ".$orderBy;
80 return $this->getList($query);
83 * Given a name check if it exists in the table
84 * @param name the unique key from the manifest
85 * @param id the id of the item you are comparing to
86 * @return upgrade_history object if found, null otherwise
88 function checkForExisting($patch_to_check){
89 $uh = new UpgradeHistory();
90 if($patch_to_check != null){
92 if(empty($patch_to_check->id_name)){
93 $where = " WHERE name = '$patch_to_check->name' ";
95 $where = " WHERE id_name = '$patch_to_check->id_name' ";
98 if(!empty($patch_to_check->id)){
99 $where .= " AND id != '$patch_to_check->id' ";
101 $where .= " AND id is not null ";
104 $query = "SELECT id FROM " . $this->table_name . " ". $where;
106 $result = $uh->db->query($query);
110 $row = $uh->db->fetchByAssoc($result);
114 if(!empty($row['id'])){
115 return $uh->retrieve($row['id']);
122 * Check if this is an upgrade, if it is then return the latest version before this installation
124 function determineIfUpgrade($id_name, $version){
125 $query = "SELECT id, version FROM " . $this->table_name . " WHERE id_name = '$id_name' ORDER BY date_entered DESC";
126 $result = $this->db->query($query);
132 while($row = $this->db->fetchByAssoc($result))
134 if(!$this->is_right_version_greater(explode('.', $row['version']), explode('.', $temp_version))){
135 $temp_version = $row['version'];
139 if($this->is_right_version_greater(explode('.', $temp_version), explode('.', $version), false))
140 return array('id' => $id, 'version' => $temp_version);
148 $query = "SELECT id FROM " . $this->table_name . " ORDER BY date_entered desc";
149 return $this->getList($query);
152 function getList($query){
153 return( parent::build_related_list( $query, $this ) );
156 function findByMd5( $var_md5 )
158 $query = "SELECT id FROM " . $this->table_name . " where md5sum = '$var_md5'";
159 return( parent::build_related_list( $query, $this ) );
162 function UninstallAvailable($patch_list, $patch_to_check)
164 //before we even go through the list, let us try to see if we find a match.
165 $history_object = $this->checkForExisting($patch_to_check);
166 if($history_object != null){
167 if((!empty($history_object->id_name) && !empty($patch_to_check->id_name) && strcmp($history_object->id_name, $patch_to_check->id_name) == 0) || strcmp($history_object->name, $patch_to_check->name) == 0){
168 //we have found a match
169 //if the patch_to_check version is greater than the found version
170 return ($this->is_right_version_greater(explode('.', $history_object->version), explode('.', $patch_to_check->version)));
175 //we will only go through this loop if we have not found another UpgradeHistory object
176 //with a matching unique_key in the database
177 foreach($patch_list as $more_recent_patch)
179 if($more_recent_patch->id == $patch_to_check->id)
182 //we will only resort to checking the files if we cannot find the unique_keys
183 //or the unique_keys do not match
184 $patch_to_check_backup_path = clean_path(remove_file_extension(from_html($patch_to_check->filename))).'-restore';
185 $more_recent_patch_backup_path = clean_path(remove_file_extension(from_html($more_recent_patch->filename))).'-restore';
186 if($this->foundConflict($patch_to_check_backup_path, $more_recent_patch_backup_path) &&
187 ($more_recent_patch->date_entered >= $patch_to_check->date_entered)){
195 function foundConflict($check_path, $recent_path)
197 if(is_file($check_path))
199 if(file_exists($recent_path))
204 elseif(is_dir($check_path))
208 $d = dir( $check_path );
209 while( $f = $d->read() )
211 if( $f == "." || $f == ".." )
214 $status = $this->foundConflict("$check_path/$f", "$recent_path/$f");
228 * Given a left version and a right version, determine if the right hand side is greater
230 * @param left the client sugar version
231 * @param right the server version
233 * return true if the right version is greater or they are equal
234 * false if the left version is greater
236 function is_right_version_greater($left, $right, $equals_is_greater = true){
237 if(count($left) == 0 && count($right) == 0){
238 return $equals_is_greater;
240 else if(count($left) == 0 || count($right) == 0){
243 else if($left[0] == $right[0]){
246 return $this->is_right_version_greater($left, $right, $equals_is_greater);
248 else if($left[0] < $right[0]){
256 * Given an array of id_names and versions, check if the dependencies are installed
258 * @param dependencies an array of id_name, version to check if these dependencies are installed
261 * @return not_found an array of id_names that were not found to be installed on the system
263 function checkDependencies($dependencies = array()){
264 $not_found = array();
265 foreach($dependencies as $dependent){
267 $query = "SELECT id FROM $this->table_name WHERE id_name = '".$dependent['id_name']."'";
268 $matches = $this->getList($query);
269 if(0 != sizeof($matches)){
270 foreach($matches as $match){
271 if($this->is_right_version_greater(explode('.', $match->version), explode('.', $dependent['version']))){
278 $not_found[] = $dependent['id_name'];
283 function retrieve($id = -1, $encode=true,$deleted=true) {
284 return parent::retrieve($id,$encode,false); //ignore the deleted filter. the table does not have the deleted column in it.