1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 jimport('joomla.filesystem.folder');
13
14 15 16 17 18
19 class JInstallerAdapterTemplate extends JInstallerAdapter
20 {
21 22 23 24 25 26
27 protected $clientId;
28
29 30 31 32 33 34 35 36
37 protected function checkExistingExtension()
38 {
39 try
40 {
41 $this->currentExtensionId = $this->extension->find(
42 array(
43 'element' => $this->element,
44 'type' => $this->type,
45 'client_id' => $this->clientId,
46 )
47 );
48 }
49 catch (RuntimeException $e)
50 {
51
52 throw new RuntimeException(
53 JText::sprintf(
54 'JLIB_INSTALLER_ABORT_ROLLBACK',
55 JText::_('JLIB_INSTALLER_' . $this->route),
56 $e->getMessage()
57 ),
58 $e->getCode(),
59 $e
60 );
61 }
62 }
63
64 65 66 67 68 69 70 71
72 protected function copyBaseFiles()
73 {
74
75 if ($this->parent->parseFiles($this->getManifest()->files, -1) === false)
76 {
77 throw new RuntimeException(
78 JText::sprintf(
79 'JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_FILES',
80 'files'
81 )
82 );
83 }
84
85 if ($this->parent->parseFiles($this->getManifest()->images, -1) === false)
86 {
87 throw new RuntimeException(
88 JText::sprintf(
89 'JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_FILES',
90 'images'
91 )
92 );
93 }
94
95 if ($this->parent->parseFiles($this->getManifest()->css, -1) === false)
96 {
97 throw new RuntimeException(
98 JText::sprintf(
99 'JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_FILES',
100 'css'
101 )
102 );
103 }
104
105
106 if ($this->manifest_script)
107 {
108 $path['src'] = $this->parent->getPath('source') . '/' . $this->manifest_script;
109 $path['dest'] = $this->parent->getPath('extension_root') . '/' . $this->manifest_script;
110
111 if ($this->parent->isOverwrite() || !file_exists($path['dest']))
112 {
113 if (!$this->parent->copyFiles(array($path)))
114 {
115 throw new RuntimeException(
116 JText::sprintf(
117 'JLIB_INSTALLER_ABORT_MANIFEST',
118 JText::_('JLIB_INSTALLER_' . strtoupper($this->getRoute()))
119 )
120 );
121 }
122 }
123 }
124 }
125
126 127 128 129 130 131 132 133
134 protected function finaliseInstall()
135 {
136
137
138 $update = JTable::getInstance('update');
139
140 $uid = $update->find(
141 array(
142 'element' => $this->element,
143 'type' => $this->type,
144 'client_id' => $this->clientId,
145 )
146 );
147
148 if ($uid)
149 {
150 $update->delete($uid);
151 }
152
153
154 if ($this->route !== 'discover_install')
155 {
156 if (!$this->parent->copyManifest(-1))
157 {
158
159 throw new RuntimeException(JText::_('JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_SETUP'));
160 }
161 }
162 }
163
164 165 166 167 168 169 170 171 172
173 public function loadLanguage($path = null)
174 {
175 $source = $this->parent->getPath('source');
176 $basePath = $this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE;
177
178 if (!$source)
179 {
180 $this->parent->setPath('source', $basePath . '/templates/' . $this->parent->extension->element);
181 }
182
183 $this->setManifest($this->parent->getManifest());
184
185 $client = (string) $this->getManifest()->attributes()->client;
186
187
188 if (!$client)
189 {
190 $client = 'ADMINISTRATOR';
191 }
192
193 $base = constant('JPATH_' . strtoupper($client));
194 $extension = 'tpl_' . $this->getName();
195 $source = $path ?: $base . '/templates/' . $this->getName();
196
197 $this->doLoadLanguage($extension, $source, $base);
198 }
199
200 201 202 203 204 205 206
207 protected function parseOptionalTags()
208 {
209 $this->parent->parseMedia($this->getManifest()->media);
210 $this->parent->parseLanguages($this->getManifest()->languages, $this->clientId);
211 }
212
213 214 215 216 217 218 219 220
221 protected function parseQueries()
222 {
223 if (in_array($this->route, array('install', 'discover_install')))
224 {
225 $db = $this->db;
226 $lang = JFactory::getLanguage();
227 $debug = $lang->setDebug(false);
228
229 $columns = array(
230 $db->quoteName('template'),
231 $db->quoteName('client_id'),
232 $db->quoteName('home'),
233 $db->quoteName('title'),
234 $db->quoteName('params'),
235 );
236
237 $values = array(
238 $db->quote($this->extension->element), $this->extension->client_id, $db->quote(0),
239 $db->quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', JText::_($this->extension->name))),
240 $db->quote($this->extension->params),
241 );
242
243 $lang->setDebug($debug);
244
245
246 $query = $db->getQuery(true);
247 $query->insert($db->quoteName('#__template_styles'))
248 ->columns($columns)
249 ->values(implode(',', $values));
250
251
252 $db->setQuery($query)->execute();
253 }
254 }
255
256 257 258 259 260 261 262
263 public function prepareDiscoverInstall()
264 {
265 $client = JApplicationHelper::getClientInfo($this->extension->client_id);
266 $manifestPath = $client->path . '/templates/' . $this->extension->element . '/templateDetails.xml';
267 $this->parent->manifest = $this->parent->isManifest($manifestPath);
268 $this->parent->setPath('manifest', $manifestPath);
269 $this->setManifest($this->parent->getManifest());
270 }
271
272 273 274 275 276 277 278 279
280 protected function setupInstallPaths()
281 {
282
283 $cname = (string) $this->getManifest()->attributes()->client;
284
285 if ($cname)
286 {
287
288 $client = JApplicationHelper::getClientInfo($cname, true);
289
290 if ($client === false)
291 {
292 throw new RuntimeException(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_UNKNOWN_CLIENT', $cname));
293 }
294
295 $basePath = $client->path;
296 $this->clientId = $client->id;
297 }
298 else
299 {
300
301 $basePath = JPATH_SITE;
302 $this->clientId = 0;
303 }
304
305
306 if (empty($this->element))
307 {
308 throw new RuntimeException(
309 JText::sprintf(
310 'JLIB_INSTALLER_ABORT_MOD_INSTALL_NOFILE',
311 JText::_('JLIB_INSTALLER_' . strtoupper($this->route))
312 )
313 );
314 }
315
316 $this->parent->setPath('extension_root', $basePath . '/templates/' . $this->element);
317 }
318
319 320 321 322 323 324 325 326
327 protected function storeExtension()
328 {
329
330 if ($this->route === 'discover_install')
331 {
332 $manifest_details = JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
333
334 $this->extension->manifest_cache = json_encode($manifest_details);
335 $this->extension->state = 0;
336 $this->extension->name = $manifest_details['name'];
337 $this->extension->enabled = 1;
338 $this->extension->params = $this->parent->getParams();
339
340 if (!$this->extension->store())
341 {
342
343 throw new RuntimeException(JText::_('JLIB_INSTALLER_ERROR_TPL_DISCOVER_STORE_DETAILS'));
344 }
345
346 return;
347 }
348
349
350 if ($this->currentExtensionId)
351 {
352 if (!$this->parent->isOverwrite())
353 {
354
355 throw new RuntimeException(
356 JText::_('JLIB_INSTALLER_ABORT_TPL_INSTALL_ALREADY_INSTALLED')
357 );
358 }
359
360
361 $this->extension->load($this->currentExtensionId);
362 }
363 else
364 {
365 $this->extension->type = 'template';
366 $this->extension->element = $this->element;
367
368
369 $this->extension->folder = '';
370 $this->extension->enabled = 1;
371 $this->extension->protected = 0;
372 $this->extension->access = 1;
373 $this->extension->client_id = $this->clientId;
374 $this->extension->params = $this->parent->getParams();
375
376
377 $this->extension->custom_data = '';
378 }
379
380
381 $this->extension->name = $this->name;
382 $this->extension->manifest_cache = $this->parent->generateManifestCache();
383
384 if (!$this->extension->store())
385 {
386
387 throw new RuntimeException(
388 JText::sprintf(
389 'JLIB_INSTALLER_ABORT_ROLLBACK',
390 JText::_('JLIB_INSTALLER_' . strtoupper($this->route)),
391 $this->extension->getError()
392 )
393 );
394 }
395 }
396
397 398 399 400 401 402 403 404 405
406 public function uninstall($id)
407 {
408
409
410 $row = JTable::getInstance('extension');
411
412 if (!$row->load((int) $id) || $row->element === '')
413 {
414 JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_ERRORUNKOWNEXTENSION'), JLog::WARNING, 'jerror');
415
416 return false;
417 }
418
419
420
421 if ($row->protected)
422 {
423 JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_WARNCORETEMPLATE', $row->name), JLog::WARNING, 'jerror');
424
425 return false;
426 }
427
428 429 430 431
432 if ($row->package_id && !$this->parent->isPackageUninstall() && !$this->canUninstallPackageChild($row->package_id))
433 {
434 JLog::add(JText::sprintf('JLIB_INSTALLER_ERROR_CANNOT_UNINSTALL_CHILD_OF_PACKAGE', $row->name), JLog::WARNING, 'jerror');
435
436 return false;
437 }
438
439 $name = $row->element;
440 $clientId = $row->client_id;
441
442
443 if (!$name)
444 {
445 JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_ID_EMPTY'), JLog::WARNING, 'jerror');
446
447 return false;
448 }
449
450
451 $db = $this->parent->getDbo();
452 $query = "SELECT COUNT(*) FROM #__template_styles WHERE home = '1' AND template = " . $db->quote($name);
453 $db->setQuery($query);
454
455 if ($db->loadResult() != 0)
456 {
457 JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DEFAULT'), JLog::WARNING, 'jerror');
458
459 return false;
460 }
461
462
463 $client = JApplicationHelper::getClientInfo($clientId);
464
465 if (!$client)
466 {
467 JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_CLIENT'), JLog::WARNING, 'jerror');
468
469 return false;
470 }
471
472 $this->parent->setPath('extension_root', $client->path . '/templates/' . strtolower($name));
473 $this->parent->setPath('source', $this->parent->getPath('extension_root'));
474
475
476 $this->parent->findManifest();
477 $manifest = $this->parent->getManifest();
478
479 if (!($manifest instanceof SimpleXMLElement))
480 {
481
482 $row->delete($row->extension_id);
483 unset($row);
484
485
486 JFolder::delete($this->parent->getPath('extension_root'));
487 JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_NOTFOUND_MANIFEST'), JLog::WARNING, 'jerror');
488
489 return false;
490 }
491
492
493 $this->parent->removeFiles($manifest->media);
494 $this->parent->removeFiles($manifest->languages, $clientId);
495
496
497 if (JFolder::exists($this->parent->getPath('extension_root')))
498 {
499 $retval = JFolder::delete($this->parent->getPath('extension_root'));
500 }
501 else
502 {
503 JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DIRECTORY'), JLog::WARNING, 'jerror');
504 $retval = false;
505 }
506
507
508 $query = 'UPDATE #__menu'
509 . ' SET template_style_id = 0'
510 . ' WHERE template_style_id in ('
511 . ' SELECT s.id FROM #__template_styles s'
512 . ' WHERE s.template = ' . $db->quote(strtolower($name)) . ' AND s.client_id = ' . $clientId . ')';
513
514 $db->setQuery($query);
515 $db->execute();
516
517 $query = $db->getQuery(true)
518 ->delete($db->quoteName('#__template_styles'))
519 ->where($db->quoteName('template') . ' = ' . $db->quote($name))
520 ->where($db->quoteName('client_id') . ' = ' . $clientId);
521 $db->setQuery($query);
522 $db->execute();
523
524 $row->delete($row->extension_id);
525 unset($row);
526
527 return $retval;
528 }
529
530 531 532 533 534
535 public function discover()
536 {
537 $results = array();
538 $site_list = JFolder::folders(JPATH_SITE . '/templates');
539 $admin_list = JFolder::folders(JPATH_ADMINISTRATOR . '/templates');
540 $site_info = JApplicationHelper::getClientInfo('site', true);
541 $admin_info = JApplicationHelper::getClientInfo('administrator', true);
542
543 foreach ($site_list as $template)
544 {
545 if (file_exists(JPATH_SITE . "/templates/$template/templateDetails.xml"))
546 {
547 if ($template === 'system')
548 {
549
550 continue;
551 }
552
553 $manifest_details = JInstaller::parseXMLInstallFile(JPATH_SITE . "/templates/$template/templateDetails.xml");
554 $extension = JTable::getInstance('extension');
555 $extension->set('type', 'template');
556 $extension->set('client_id', $site_info->id);
557 $extension->set('element', $template);
558 $extension->set('folder', '');
559 $extension->set('name', $template);
560 $extension->set('state', -1);
561 $extension->set('manifest_cache', json_encode($manifest_details));
562 $extension->set('params', '{}');
563 $results[] = $extension;
564 }
565 }
566
567 foreach ($admin_list as $template)
568 {
569 if (file_exists(JPATH_ADMINISTRATOR . "/templates/$template/templateDetails.xml"))
570 {
571 if ($template === 'system')
572 {
573
574 continue;
575 }
576
577 $manifest_details = JInstaller::parseXMLInstallFile(JPATH_ADMINISTRATOR . "/templates/$template/templateDetails.xml");
578 $extension = JTable::getInstance('extension');
579 $extension->set('type', 'template');
580 $extension->set('client_id', $admin_info->id);
581 $extension->set('element', $template);
582 $extension->set('folder', '');
583 $extension->set('name', $template);
584 $extension->set('state', -1);
585 $extension->set('manifest_cache', json_encode($manifest_details));
586 $extension->set('params', '{}');
587 $results[] = $extension;
588 }
589 }
590
591 return $results;
592 }
593
594 595 596 597 598 599 600
601 public function refreshManifestCache()
602 {
603
604 $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
605 $manifestPath = $client->path . '/templates/' . $this->parent->extension->element . '/templateDetails.xml';
606 $this->parent->manifest = $this->parent->isManifest($manifestPath);
607 $this->parent->setPath('manifest', $manifestPath);
608
609 $manifest_details = JInstaller::parseXMLInstallFile($this->parent->getPath('manifest'));
610 $this->parent->extension->manifest_cache = json_encode($manifest_details);
611 $this->parent->extension->name = $manifest_details['name'];
612
613 try
614 {
615 return $this->parent->extension->store();
616 }
617 catch (RuntimeException $e)
618 {
619 JLog::add(JText::_('JLIB_INSTALLER_ERROR_TPL_REFRESH_MANIFEST_CACHE'), JLog::WARNING, 'jerror');
620
621 return false;
622 }
623 }
624 }
625
626 627 628 629 630 631 632
633 class JInstallerTemplate extends JInstallerAdapterTemplate
634 {
635 }
636