ossn.lib.upgrade.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @package (Informatikon.com).ossn
6  * @author OSSN Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
11 
12 /**
13  * Disable Errors during upgrade
14  *
15  * @return void
16  * @access private
17  */
18 function ossn_upgrade_init() {
19  ossn_add_hook('database', 'execution:message', 'ossn_disable_database_exception');
20 }
21 /**
22  * Get upgrade files
23  *
24  * @return array
25  * @access private
26  */
28  $path = ossn_validate_filepath(ossn_route()->upgrade . 'upgrades/');
29  $handle = opendir($path);
30  if(!$handle) {
31  return false;
32  }
33 
34  $files = array();
35 
36  while($file = readdir($handle)) {
37  if($file != "." && $file != "..") {
38  $files[] = $file;
39  }
40  }
41 
42  sort($files);
43  return $files;
44 }
45 
46 /**
47  * Get the list of files that already being upraded
48  *
49  * @return array
50  * @access private
51  */
53  $settings = new OssnSite;
54  $upgrades = $settings->getSettings('upgrades');
55  $upgrades = json_decode($upgrades);
56  if(!is_array($upgrades) || empty($upgrades)) {
57  $upgrades = array();
58  }
59  return $upgrades;
60 }
61 
62 /**
63  * Get the files that need to be run for upgrade
64  *
65  * @return array
66  * @access private
67  */
69  $upgrades = ossn_get_upgraded_files();
70  $available_upgrades = ossn_get_upgrade_files();
71  return array_diff($available_upgrades, $upgrades);
72 }
73 
74 /**
75  * Trigger upgrade / Run upgrade
76  *
77  * @return void;
78  * @access private
79  */
81  if(!ossn_isAdminLoggedin()) {
84  }
85  $upgrades = ossn_get_process_upgrade_files();
86  if(!is_array($upgrades) || empty($upgrades)) {
87  ossn_trigger_message(ossn_print('upgrade:not:available'), 'error');
89  redirect('administrator');
90  }
91  foreach($upgrades as $upgrade) {
92  $file = ossn_route()->upgrade . "upgrades/{$upgrade}";
93  if(!include_once($file)) {
94  throw new exception(ossn_print('upgrade:file:load:error'));
95  }
96  }
97  //need to reset cache files
98  if(ossn_site_settings('cache') !== 0) {
101  }
102  return true;
103 }
104 
105 /**
106  * Generate site screat key
107  *
108  * @return str;
109  */
111  return substr(md5('ossn' . rand()), 3, 8);
112 }
113 /**
114  * Get update status
115  *
116  * @return boolean
117  */
119  $upgrading = ossn_route()->www . '_upgrading_process';
120  if(is_file($upgrading)) {
121  return true;
122  }
123  return false;
124 }
125 /**
126  * Disable exception during upgrade
127  *
128  * @return void|false
129  */
132  return false;
133  }
134 }
135 /**
136  * Kill upgrading
137  *
138  * @return boolean
139  */
142  $upgrading = ossn_route()->www . '_upgrading_process';
143  unlink($upgrading);
144  }
145 }
146 /**
147  * Update site version
148  *
149  * @param string $version new Version
150  *
151  * @return boolean
152  */
153 function ossn_update_db_version($version = '') {
154  if(!empty($version)) {
155  $db = new OssnDatabase;
156  $vars = array();
157  $vars['table'] = 'ossn_site_settings';
158  $vars['names'] = array(
159  'value'
160  );
161  $vars['values'] = array(
162  $version
163  );
164  $vars['wheres'] = array(
165  "name='site_version'"
166  );
167  return $db->update($vars);
168  }
169 }
170 /**
171  * Update processed upgrades
172  *
173  * @param integer $upgrade New release
174  *
175  * @return boolean
176  */
177 function ossn_update_upgraded_files($upgrade) {
178  if(empty($upgrade)) {
179  return false;
180  }
181  $database = new OssnDatabase;
182  $upgrade_json = array_merge(ossn_get_upgraded_files(), array(
183  $upgrade
184  ));
185  $upgrade_json = json_encode($upgrade_json);
186 
187  $update = array();
188  $update['table'] = 'ossn_site_settings';
189  $update['names'] = array(
190  'value'
191  );
192  $update['values'] = array(
193  $upgrade_json
194  );
195  $update['wheres'] = array(
196  "name='upgrades'"
197  );
198 
199  if($database->update($update)) {
200  return true;
201  } else {
202  return false;
203  }
204 }
205 //initilize upgrades
206 ossn_register_callback('ossn', 'init', 'ossn_upgrade_init', 1);