]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/UpgradeHistory.php
Release 6.4.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-2011 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             if($this->foundConflict($patch_to_check_backup_path, $more_recent_patch_backup_path) &&
187             ($more_recent_patch->date_entered >= $patch_to_check->date_entered)){
188                 return false;
189             }
190         }
191
192         return true;
193     }
194
195     function foundConflict($check_path, $recent_path)
196     {
197         if(is_file($check_path))
198         {
199             if(file_exists($recent_path))
200                 return true;
201             else
202                 return false;
203         }
204         elseif(is_dir($check_path))
205         {
206             $status = false;
207
208             $d = dir( $check_path );
209             while( $f = $d->read() )
210             {
211                 if( $f == "." || $f == ".." )
212                     continue;
213
214                 $status = $this->foundConflict("$check_path/$f", "$recent_path/$f");
215
216                 if($status)
217                     break;
218             }
219
220             $d->close();
221             return( $status );
222         }
223
224         return false;
225     }
226
227     /**
228      * Given a left version and a right version, determine if the right hand side is greater
229      *
230      * @param left           the client sugar version
231      * @param right          the server version
232      *
233      * return               true if the right version is greater or they are equal
234      *                      false if the left version is greater
235      */
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;
239         }
240         else if(count($left) == 0 || count($right) == 0){
241             return true;
242         }
243         else if($left[0] == $right[0]){
244             array_shift($left);
245             array_shift($right);
246             return $this->is_right_version_greater($left, $right, $equals_is_greater);
247         }
248         else if($left[0] < $right[0]){
249            return true;
250         }
251         else
252             return false;
253     }
254
255     /**
256      * Given an array of id_names and versions, check if the dependencies are installed
257      *
258      * @param dependencies      an array of id_name, version to check if these dependencies are installed
259      *                                          on the system
260      *
261      * @return not_found        an array of id_names that were not found to be installed on the system
262      */
263     function checkDependencies($dependencies = array()){
264         $not_found = array();
265         foreach($dependencies as $dependent){
266             $found = false;
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']))){
272                         $found = true;
273                         break;
274                     }//fi
275                 }//rof
276             }//fi
277             if(!$found){
278                 $not_found[] = $dependent['id_name'];
279             }//fi
280         }//rof
281         return $not_found;
282     }
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.
285     }
286
287 }
288 ?>