]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/UpgradeHistory.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Administration / UpgradeHistory.php
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-2012 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39
40
41
42 // The history of upgrades on the system
43 class UpgradeHistory extends SugarBean
44 {
45     var $new_schema = true;
46     var $module_dir = 'Administration';
47
48     // Stored fields
49     var $id;
50     var $filename;
51     var $md5sum;
52     var $type;
53     var $version;
54     var $status;
55     var $date_entered;
56     var $name;
57     var $description;
58     var $id_name;
59     var $manifest;
60     var $enabled;
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;
66
67     function delete()
68     {
69         $this->db->query( "delete from " . $this->table_name . " where id = " . $this->db->quoted($this->id));
70     }
71
72     function UpgradeHistory()
73     {
74         parent::SugarBean();
75         $this->disable_row_level_security = true;
76     }
77
78     function getAllOrderBy($orderBy){
79         $query = "SELECT id FROM " . $this->table_name . " ORDER BY ".$orderBy;
80         return $this->getList($query);
81     }
82     /**
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
87      */
88     function checkForExisting($patch_to_check){
89         $uh = new UpgradeHistory();
90         if($patch_to_check != null){
91
92             if(empty($patch_to_check->id_name)){
93                 $where = " WHERE name = '$patch_to_check->name' ";
94             }else{
95                 $where = " WHERE id_name = '$patch_to_check->id_name' ";
96             }
97
98             if(!empty($patch_to_check->id)){
99                 $where .= "  AND id != '$patch_to_check->id'  ";
100             }else{
101                 $where .= "  AND id is not null  ";
102             }
103
104             $query = "SELECT id FROM " . $this->table_name . " ". $where;
105
106             $result = $uh->db->query($query);
107             if(empty($result)){
108                 return null;
109             }
110             $row = $uh->db->fetchByAssoc($result);
111             if(empty($row)){
112                 return null;
113             }
114             if(!empty($row['id'])){
115                 return $uh->retrieve($row['id']);
116             }
117         }
118         return null;
119     }
120
121     /**
122      * Check if this is an upgrade, if it is then return the latest version before this installation
123      */
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);
127          if(empty($result)){
128             return null;
129          }else{
130             $temp_version = 0;
131             $id = '';
132             while($row = $this->db->fetchByAssoc($result))
133             {
134                 if(!$this->is_right_version_greater(explode('.', $row['version']), explode('.', $temp_version))){
135                     $temp_version = $row['version'];
136                     $id = $row['id'];
137                 }
138             }//end while
139             if($this->is_right_version_greater(explode('.', $temp_version), explode('.', $version), false))
140                 return array('id' => $id, 'version' => $temp_version);
141             else
142                 return null;
143          }
144     }
145
146     function getAll()
147     {
148         $query = "SELECT id FROM " . $this->table_name . " ORDER BY date_entered desc";
149         return $this->getList($query);
150     }
151
152     function getList($query){
153         return( parent::build_related_list( $query, $this ) );
154     }
155
156     function findByMd5( $var_md5 )
157     {
158         $query = "SELECT id FROM " . $this->table_name . " where md5sum = '$var_md5'";
159         return( parent::build_related_list( $query, $this ) );
160     }
161
162     function UninstallAvailable($patch_list, $patch_to_check)
163     {
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)));
171             }else{
172                 return true;
173             }
174         }
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)
178         {
179             if($more_recent_patch->id == $patch_to_check->id)
180                 break;
181
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             $patch_to_check_timestamp = TimeDate::getInstance()->fromUser($patch_to_check->date_entered)->getTimestamp();
187             $more_resent_patch_timestamp = TimeDate::getInstance()->fromUser($more_recent_patch->date_entered)->getTimestamp();
188             if (
189                 $this->foundConflict($patch_to_check_backup_path, $more_recent_patch_backup_path) &&
190                 ($more_resent_patch_timestamp >= $patch_to_check_timestamp)
191             )
192             {
193                 return false;
194             }
195         }
196
197         return true;
198     }
199
200     function foundConflict($check_path, $recent_path)
201     {
202         if(is_file($check_path))
203         {
204             if(file_exists($recent_path))
205                 return true;
206             else
207                 return false;
208         }
209         elseif(is_dir($check_path))
210         {
211             $status = false;
212
213             $d = dir( $check_path );
214             while( $f = $d->read() )
215             {
216                 if( $f == "." || $f == ".." )
217                     continue;
218
219                 $status = $this->foundConflict("$check_path/$f", "$recent_path/$f");
220
221                 if($status)
222                     break;
223             }
224
225             $d->close();
226             return( $status );
227         }
228
229         return false;
230     }
231
232     /**
233      * Given a left version and a right version, determine if the right hand side is greater
234      *
235      * @param left           the client sugar version
236      * @param right          the server version
237      *
238      * return               true if the right version is greater or they are equal
239      *                      false if the left version is greater
240      */
241     function is_right_version_greater($left, $right, $equals_is_greater = true){
242         if(count($left) == 0 && count($right) == 0){
243             return $equals_is_greater;
244         }
245         else if(count($left) == 0 || count($right) == 0){
246             return true;
247         }
248         else if($left[0] == $right[0]){
249             array_shift($left);
250             array_shift($right);
251             return $this->is_right_version_greater($left, $right, $equals_is_greater);
252         }
253         else if($left[0] < $right[0]){
254            return true;
255         }
256         else
257             return false;
258     }
259
260     /**
261      * Given an array of id_names and versions, check if the dependencies are installed
262      *
263      * @param dependencies      an array of id_name, version to check if these dependencies are installed
264      *                                          on the system
265      *
266      * @return not_found        an array of id_names that were not found to be installed on the system
267      */
268     function checkDependencies($dependencies = array()){
269         $not_found = array();
270         foreach($dependencies as $dependent){
271             $found = false;
272             $query = "SELECT id FROM $this->table_name WHERE id_name = '".$dependent['id_name']."'";
273             $matches = $this->getList($query);
274             if(0 != sizeof($matches)){
275                 foreach($matches as $match){
276                     if($this->is_right_version_greater(explode('.', $match->version), explode('.', $dependent['version']))){
277                         $found = true;
278                         break;
279                     }//fi
280                 }//rof
281             }//fi
282             if(!$found){
283                 $not_found[] = $dependent['id_name'];
284             }//fi
285         }//rof
286         return $not_found;
287     }
288     function retrieve($id = -1, $encode=true,$deleted=true) {
289         return parent::retrieve($id,$encode,false);  //ignore the deleted filter. the table does not have the deleted column in it.
290     }
291
292 }
293 ?>