1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36:
37:
38: Yii::import('application.components.util.*');
39:
40: 41: 42: 43: 44: 45:
46: class UpdateCommand extends CConsoleCommand {
47:
48: public function beforeAction($action, $params){
49: $this->attachBehaviors(array(
50: 'UpdaterBehavior' => array(
51: 'class' => 'application.components.UpdaterBehavior',
52: 'isConsole' => true,
53: 'scenario' => 'update',
54: )
55: ));
56: $this->requireDependencies();
57: return parent::beforeAction($action, $params);
58: }
59:
60: public function actionIndex(){
61: echo $this->help;
62: }
63:
64: 65: 66: 67: 68:
69: public function actionApp($force = 0,$backup = 1, $lock=0) {
70:
71: $this->runOperation('update',(bool) $force, (bool) $backup, (bool) $lock);
72: return 0;
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82: 83:
84: public function actionUpgrade($key,$firstName,$lastName,$email,$force=0,$backup=1) {
85: $this->uniqueId = $key;
86:
87: if(!$this->requirements['requirements']['extensions']['curl'])
88: $this->output(Yii::t('admin','Cannot proceed; cURL extension is required for registration.'),1,1);
89:
90: $ch = curl_init($this->updateServer.'/installs/registry/register');
91: curl_setopt_array($ch, array(
92: CURLOPT_POST => 1,
93: CURLOPT_RETURNTRANSFER => 1,
94: CURLOPT_POSTFIELDS => array(
95: 'firstName' => $firstName,
96: 'lastName' => $lastName,
97: 'email' => $email,
98: 'unique_id' => $key
99: ),
100: ));
101: $cr = json_decode(curl_exec($ch));
102:
103:
104:
105: $this->runOperation('upgrade',(bool) $force, (bool) $backup);
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115: 116:
117: public function runOperation($scenario,$force=false,$backup=true,$lock=false) {
118: $this->scenario = $scenario;
119: $unpacked = $this->checkIf('packageExists',false);
120: if($this->checkIf('packageApplies',false)) {
121:
122:
123: } else if($unpacked) {
124:
125:
126:
127:
128: $this->checkIf('packageApplies');
129: } else {
130:
131:
132:
133:
134: $this->runUpdateUpdater();
135:
136: $latestVersion = $this->checkUpdates(true);
137: if(version_compare($this->configVars['version'], $latestVersion) >= 0) {
138: if($scenario != 'upgrade') {
139: $this->output(Yii::t('admin', 'X2Engine is at the latest version!'));
140: Yii::app()->end();
141: }
142: } else if($scenario == 'upgrade') {
143: $this->output(Yii::t('admin',"Before upgrading, you must update to the latest version ({latestver}). ",array('{latestver}'=>$latestVersion)),1,1);
144: }
145: $data = $this->getUpdateData();
146: if(array_key_exists('errors', $data)){
147:
148: $this->output($data['errors'], 1,1);
149: Yii::app()->end();
150: }
151: $this->manifest = $data;
152: }
153:
154:
155: $this->output($this->renderCompatibilityMessages());
156: if(!$this->compatibilityStatus['allClear'] && !$force) {
157: Yii::app()->end();
158: }
159:
160:
161: if(!$unpacked) {
162: $this->downloadPackage();
163: $this->unpack();
164: $this->checkIf('packageApplies');
165: if(!((bool) $this->files) || $this->filesStatus[UpdaterBehavior::FILE_CORRUPT] > 0 || $this->filesStatus[UpdaterBehavior::FILE_MISSING] > 0) {
166: $this->output(Yii::t('admin','Could not apply package. {n_mis} files are missing, {n_cor} are corrupt', array(
167: '{n_mis}' => $this->filesStatus[UpdaterBehavior::FILE_MISSING],
168: '{n_cor}' => $this->filesStatus[UpdaterBehavior::FILE_CORRUPT]
169: )), 1, 1);
170: }
171: }
172:
173:
174: if($lock) {
175: $this->output(Yii::t('admin','Locking the app to prevent data entry during update.'));
176: Yii::app()->locked = time();
177: }
178:
179: try{
180:
181: if($backup)
182: $this->makeDatabaseBackup();
183:
184:
185: $this->enactChanges($backup);
186:
187: }catch(Exception $e){
188:
189: if($lock){
190: $this->output(Yii::t('admin', 'Unlocking the app.'));
191: Yii::app()->setLocked(false);
192: }
193: throw $e;
194: }
195:
196: if($lock) {
197: $this->output(Yii::t('admin','Unlocking the app.'));
198: Yii::app()->setLocked(false);
199: }
200: $this->finalizeUpdate($scenario, $this->uniqueId, $this->version, $this->edition);
201: $this->output(Yii::t('admin','All done.'));
202: }
203:
204:
205: 206: 207: 208: 209:
210: public function runUpdateUpdater() {
211: $config = $this->configVars;
212: extract($config);
213: $status = 0;
214: $latestUpdaterVersion = $this->getLatestUpdaterVersion();
215: if($latestUpdaterVersion){
216: $backCompat = $this->backCompatHooks($latestUpdaterVersion);
217: $refreshCriteria = version_compare($updaterVersion,$latestUpdaterVersion) < 0
218: || $backCompat;
219: if($refreshCriteria){
220: $classes = $this->updateUpdater($latestUpdaterVersion);
221: if(empty($classes)){
222: if($backCompat) {
223: $this->output(Yii::t('admin', 'Re-run the command to proceed.'));
224: } else {
225: $this->output(Yii::t('admin', 'The updater is now up-to-date and compliant with the updates server. Re-run the command to proceed.'));
226: }
227: } else {
228: $this->output(Yii::t('admin', 'One or more dependencies of AdminController are missing and could not be automatically retrieved. They are {classes}', array('{classes}' => implode(', ', $classes))),1,1);
229: }
230: Yii::app()->end();
231: } else {
232: $this->output(Yii::t('admin','The updater is up-to-date and safe to use.'));
233: return;
234: }
235: }else{
236: if(!$this->requirements['requirements']['environment']['updates_connection']) {
237: $this->output(Yii::t('admin','Could not connect to the updates server, or an error occurred on the updates server.').' '.(
238: $this->requirements['requirements']['extensions']['curl'] || $this->requirements['requirements']['environment']['allow_url_fopen']
239: ? ''
240: : Yii::t('admin','Note, outbound HTTP requests are not permitted in this PHP runtime environment, because all methods of doing so have been disabled.')
241: ),1,1);
242: }
243: }
244: }
245:
246: }
247:
248: ?>
249: