PageRenderTime 191ms CodeModel.GetById 126ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_jce/install.php

https://github.com/srgg6701/auction-ruseasons
PHP | 1247 lines | 836 code | 251 blank | 160 comment | 167 complexity | 712b34a2d307d2d180febd310771df7b MD5 | raw file
   1<?php
   2
   3/**
   4 * @package   	JCE
   5 * @copyright 	Copyright (c) 2009-2012 Ryan Demmer. All rights reserved.
   6 * @license   	GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
   7 * JCE is free software. This version may have been modified pursuant
   8 * to the GNU General Public License, and as distributed it includes or
   9 * is derivative of works licensed under the GNU General Public License or
  10 * other free or open source software licenses.
  11 */
  12defined('_JEXEC') or die('RESTRICTED');
  13
  14abstract class WFInstall {
  15
  16    private static function cleanupInstall() {
  17        $path = JPATH_ADMINISTRATOR . '/components/com_jce';
  18
  19        if (!is_file($path . '/jce.php')) {
  20            self::removePackages();
  21
  22            $db = JFactory::getDBO();
  23
  24            // cleanup menus
  25            if (defined('JPATH_PLATFORM')) {
  26                $query = $db->getQuery(true);
  27                $query->select('id')->from('#__menu')->where(array('alias = ' . $db->Quote('jce'), 'menutype = ' . $db->Quote('main')));
  28
  29                $db->setQuery($query);
  30                $id = $db->loadResult();
  31
  32                $query->clear();
  33
  34                if ($id) {
  35                    $table = JTable::getInstance('menu');
  36
  37                    // delete main item
  38                    $table->delete((int) $id);
  39
  40                    // delete children
  41                    $query->select('id')->from('#__menu')->where('parent_id = ' . $db->Quote($id));
  42
  43                    $db->setQuery($query);
  44                    $ids = $db->loadColumn();
  45
  46                    $query->clear();
  47
  48                    if (!empty($ids)) {
  49                        // Iterate the items to delete each one.
  50                        foreach ($ids as $menuid) {
  51                            $table->delete((int) $menuid);
  52                        }
  53                    }
  54
  55                    // Rebuild the whole tree
  56                    $table->rebuild();
  57                }
  58            } else {
  59                $db->setQuery('DELETE FROM #__components WHERE `option` = ' . $db->Quote('com_jce'));
  60                $db->query();
  61            }
  62        }
  63
  64        if (is_file($path . '/install.script.php')) {
  65            jimport('joomla.filesystem.folder');
  66            jimport('joomla.filesystem.file');
  67
  68            JFile::delete($path . '/install.script.php');
  69            JFolder::delete($path);
  70        }
  71    }
  72
  73    public static function install($installer) {
  74        error_reporting(E_ERROR | E_WARNING);
  75
  76        // load languages
  77        $language = JFactory::getLanguage();
  78        $language->load('com_jce', JPATH_ADMINISTRATOR, null, true);
  79        $language->load('com_jce.sys', JPATH_ADMINISTRATOR, null, true);
  80
  81        $requirements = array();
  82
  83        // check PHP version
  84        if (version_compare(PHP_VERSION, '5.2.4', '<')) {
  85            $requirements[] = array(
  86                'name' => 'PHP Version',
  87                'info' => 'JCE Requires PHP version 5.2.4 or later. Your version is : ' . PHP_VERSION
  88            );
  89        }
  90
  91        // check JSON is installed
  92        if (function_exists('json_encode') === false || function_exists('json_decode') === false) {
  93            $requirements[] = array(
  94                'name' => 'JSON',
  95                'info' => 'JCE requires the <a href="http://php.net/manual/en/book.json.php" target="_blank">PHP JSON</a> extension which is not available on this server.'
  96            );
  97        }
  98
  99        // check SimpleXML
 100        if (function_exists('simplexml_load_string') === false || function_exists('simplexml_load_file') === false || class_exists('SimpleXMLElement') === false) {
 101            $requirements[] = array(
 102                'name' => 'SimpleXML',
 103                'info' => 'JCE requires the <a href="http://php.net/manual/en/book.simplexml.php" target="_blank">PHP SimpleXML</a> library which is not available on this server.'
 104            );
 105        }
 106
 107        if (!empty($requirements)) {
 108            $message = '<div id="jce"><style type="text/css" scoped="scoped">' . file_get_contents(dirname(__FILE__) . '/media/css/install.css') . '</style>';
 109
 110            $message .= '<h2>' . JText::_('WF_ADMIN_TITLE') . ' - Install Failed</h2>';
 111            $message .= '<h3>JCE could not be installed as this site does not meet <a href="http://www.joomlacontenteditor.net/support/documentation/56-editor/106-requirements" target="_blank">technical requirements</a> (see below)</h3>';
 112            $message .= '<ul class="install">';
 113
 114            foreach ($requirements as $requirement) {
 115                $message .= '<li class="error">' . $requirement['name'] . ' : ' . $requirement['info'] . '<li>';
 116            }
 117
 118            $message .= '</ul>';
 119            $message .= '</div>';
 120
 121            $installer->set('message', $message);
 122
 123            $installer->abort();
 124
 125            self::cleanupInstall();
 126
 127            return false;
 128        }
 129
 130        // get manifest
 131        $manifest = $installer->getManifest();
 132        $new_version = (string) $manifest->version;
 133
 134        // Joomla! 1.5
 135        if (!defined('JPATH_PLATFORM') && !$new_version) {
 136            $new_version = (string) $manifest->document->getElementByPath('version')->data();
 137        }
 138
 139        // get version from xml file
 140        if (!$manifest) {
 141            $manifest = JApplicationHelper::parseXMLInstallFile($installer->getPath('manifest'));
 142            if (is_array($manifest)) {
 143                $new_version = $manifest['version'];
 144            }
 145        }
 146
 147        $state = false;
 148
 149        // the current version
 150        $current_version = $new_version;
 151
 152        if (defined('JPATH_PLATFORM')) {
 153            $xml_file = $installer->getPath('extension_administrator') . '/jce.xml';
 154            // check for an xml file
 155            if (is_file($xml_file)) {
 156                if ($xml = JApplicationHelper::parseXMLInstallFile($xml_file)) {
 157                    $current_version = $xml['version'];
 158                }
 159            }
 160        } else {
 161            if (basename($installer->getPath('manifest')) == 'legacy.xml') {
 162                $xml_file = JPATH_PLUGINS . '/editors/jce.xml';
 163
 164                // check for an xml file
 165                if ($xml = JApplicationHelper::parseXMLInstallFile($xml_file)) {
 166                    $current_version = $xml['version'];
 167                } else {
 168                    // check for old tables
 169                    if (self::checkTable('#__jce_groups')) {
 170                        $current_version = '1.5.0';
 171                    }
 172
 173                    // check for old tables
 174                    if (self::checkTable('#__jce_profiles')) {
 175                        $current_version = '2.0.0beta1';
 176                    }
 177                }
 178            }
 179        }
 180
 181        // perform upgrade
 182        if (version_compare($current_version, $new_version, '<')) {
 183            $state = self::upgrade($current_version);
 184        } else {
 185            // install plugins first
 186            $state = self::installProfiles();
 187        }
 188
 189        if (self::checkTableColumn('#__wf_profiles', 'device') === false) {
 190            $db = JFactory::getDBO();
 191
 192            $query = 'ALTER TABLE #__wf_profiles CHANGE `description` `description` TEXT';
 193            $db->setQuery($query);
 194            $db->query();
 195
 196            // Change types field to TEXT
 197            $query = 'ALTER TABLE #__wf_profiles CHANGE `types` `types` TEXT';
 198            $db->setQuery($query);
 199            $db->query();
 200
 201            // Add device field
 202            $query = 'ALTER TABLE #__wf_profiles ADD `device` VARCHAR(255) AFTER `area`';
 203
 204            if (strtolower($db->name) == 'sqlsrv' || strtolower($db->name) == 'sqlazure') {
 205                $query = 'ALTER TABLE #__wf_profiles ADD `device` NVARCHAR(250)';
 206            }
 207
 208            $db->setQuery($query);
 209            $db->query();
 210        }
 211
 212        if ($state) {
 213            // legacy (JCE 1.5) cleanup
 214            if (!defined('JPATH_PLATFORM')) {
 215                self::legacyCleanup();
 216            }
 217
 218            $message = '<div id="jce"><style type="text/css" scoped="scoped">' . file_get_contents(dirname(__FILE__) . '/media/css/install.css') . '</style>';
 219
 220            $message .= '<h2>' . JText::_('WF_ADMIN_TITLE') . ' ' . $new_version . '</h2>';
 221            $message .= '<ul class="install">';
 222            $message .= '<li class="success">' . JText::_('WF_ADMIN_DESC') . '<li>';
 223
 224            // install packages (editor plugin, quickicon etc)
 225            $packages = dirname(__FILE__) . '/packages';
 226
 227            // install additional packages
 228            if (is_dir($packages)) {
 229                $message .= self::installPackages($packages);
 230            }
 231
 232            $message .= '</ul>';
 233            $message .= '</div>';
 234
 235            $installer->set('message', $message);
 236
 237            // post-install
 238            self::addIndexfiles(array(dirname(__FILE__), JPATH_SITE . '/components/com_jce', JPATH_PLUGINS . '/jce'));
 239        } else {
 240            $installer->abort();
 241
 242            return false;
 243        }
 244    }
 245
 246    public static function uninstall() {
 247        $db = JFactory::getDBO();
 248
 249        // remove Profiles table if its empty
 250        if ((int) self::checkTableContents('#__wf_profiles') == 0) {
 251            if (method_exists($db, 'dropTable')) {
 252                $db->dropTable('#__wf_profiles', true);
 253            } else {
 254                $query = 'DROP TABLE IF EXISTS #__wf_profiles';
 255                $db->setQuery($query);
 256            }
 257
 258            $db->query();
 259        }
 260        // remove packages
 261        self::removePackages();
 262    }
 263
 264    private static function paramsToObject($data) {
 265        $registry = new JRegistry();
 266        $registry->loadIni($data);
 267        return $registry->toObject();
 268    }
 269
 270    private static function loadXMLFile($file) {
 271        $xml = null;
 272
 273        // Disable libxml errors and allow to fetch error information as needed
 274        libxml_use_internal_errors(true);
 275
 276        if (is_file($file)) {
 277            // Try to load the xml file
 278            $xml = simplexml_load_file($file);
 279        }
 280
 281        return $xml;
 282    }
 283
 284    // Upgrade from JCE 1.5.x
 285    private static function upgradeLegacy() {
 286        $app = JFactory::getApplication();
 287        $db = JFactory::getDBO();
 288
 289        $admin = JPATH_ADMINISTRATOR . '/components/com_jce';
 290        $site = JPATH_SITE . '/components/com_jce';
 291
 292        //require_once($admin . '/helpers/parameter.php');
 293        // check for groups table / data
 294        if (self::checkTable('#__jce_groups') && self::checkTableContents('#__jce_groups')) {
 295            jimport('joomla.plugin.helper');
 296
 297            // get plugin
 298            $plugin = JPluginHelper::getPlugin('editors', 'jce');
 299            // get JCE component
 300            $table = JTable::getInstance('component');
 301            $table->loadByOption('com_jce');
 302            // process params to JSON string
 303            $params = self::paramsToObject($table->params);
 304            // set params
 305            $table->params = json_encode(array('editor' => $params));
 306            // store
 307            $table->store();
 308            // get all groups data
 309            $query = 'SELECT * FROM #__jce_groups';
 310            $db->setQuery($query);
 311            $groups = $db->loadObjectList();
 312
 313            // get all plugin data
 314            $query = 'SELECT id, name, icon FROM #__jce_plugins';
 315            $db->setQuery($query);
 316            $plugins = $db->loadAssocList('id');
 317
 318            $map = array(
 319                'advlink' => 'link',
 320                'advcode' => 'source',
 321                'tablecontrols' => 'table',
 322                'cut,copy,paste' => 'clipboard',
 323                'paste' => 'clipboard',
 324                'search,replace' => 'searchreplace',
 325                'cite,abbr,acronym,del,ins,attribs' => 'xhtmlxtras',
 326                'styleprops' => 'style',
 327                'readmore,pagebreak' => 'article',
 328                'ltr,rtl' => 'directionality',
 329                'insertlayer,moveforward,movebackward,absolute' => 'layer'
 330            );
 331
 332            if (self::createProfilesTable()) {
 333                foreach ($groups as $group) {
 334                    $row = JTable::getInstance('profiles', 'WFTable');
 335
 336                    $rows = array();
 337
 338                    // transfer row ids to names
 339                    foreach (explode(';', $group->rows) as $item) {
 340                        $icons = array();
 341                        foreach (explode(',', $item) as $id) {
 342                            // spacer
 343                            if ($id == '00') {
 344                                $icon = 'spacer';
 345                            } else {
 346                                if (isset($plugins[$id])) {
 347                                    $icon = $plugins[$id]['icon'];
 348
 349                                    // map old icon names to new
 350                                    if (array_key_exists($icon, $map)) {
 351                                        $icon = $map[$icon];
 352                                    }
 353                                }
 354                            }
 355                            $icons[] = $icon;
 356                        }
 357                        $rows[] = implode(',', $icons);
 358                    }
 359                    // re-assign rows
 360                    $row->rows = implode(';', $rows);
 361
 362                    $names = array('anchor');
 363                    
 364                    // add lists
 365                    if (preg_match('#(numlist|bullist)#', $row->rows)) {
 366                        $names[] = 'lists';
 367                    }
 368
 369                    // transfer plugin ids to names
 370                    foreach (explode(',', $group->plugins) as $id) {
 371                        if (isset($plugins[$id])) {
 372                            $name = $plugins[$id]['name'];
 373
 374                            // map old icon names to new
 375                            if (array_key_exists($name, $map)) {
 376                                $name = $map[$name];
 377                            }
 378
 379                            $names[] = $name;
 380                        }
 381                    }
 382                    // re-assign plugins
 383                    $row->plugins = implode(',', $names);
 384
 385                    // convert params to JSON
 386                    $params = self::paramsToObject($group->params);
 387                    $data = new StdClass();
 388                    
 389                    // Add lists plugin
 390                    $buttons = array();
 391                    
 392                    if (strpos($row->rows, 'numlist') !== false) {
 393                        $buttons[] = 'numlist';
 394                        // replace "numlist" with "lists"
 395                        $row->rows = str_replace('numlist', 'lists', $row->rows);
 396                    }
 397                    
 398                    if (strpos($row->rows, 'bullist') !== false) {
 399                        $buttons[] = 'bullist';
 400                        // replace "bullist" with "lists"
 401                        if (strpos($row->rows, 'lists') === false) {
 402                            $row->rows = str_replace('bullist', 'lists', $row->rows);
 403                        }
 404                    }
 405                    // remove bullist and numlist
 406                    $row->rows = str_replace(array('bullist', 'numlist'), '', $row->rows);
 407
 408                    // add lists buttons parameter
 409                    if (!empty($buttons)) {
 410                        $params->lists_buttons = $buttons;
 411                    }
 412                    
 413                    // convert parameters
 414                    foreach ($params as $key => $value) {
 415                        $parts = explode('_', $key);
 416
 417                        $node = array_shift($parts);
 418
 419                        // special consideration for imgmanager_ext!!
 420                        if (strpos($key, 'imgmanager_ext_') !== false) {
 421                            $node = $node . '_' . array_shift($parts);
 422                        }
 423
 424                        // convert some nodes
 425                        if (isset($map[$node])) {
 426                            $node = $map[$node];
 427                        }
 428
 429                        $key = implode('_', $parts);
 430
 431                        if ($value !== '') {
 432                            if (!isset($data->$node) || !is_object($data->$node)) {
 433                                $data->$node = new StdClass();
 434                            }
 435                            // convert Link parameters
 436                            if ($node == 'link' && $key != 'target') {
 437                                $sub = $key;
 438                                $key = 'links';
 439
 440                                if (!isset($data->$node->$key)) {
 441                                    $data->$node->$key = new StdClass();
 442                                }
 443
 444                                if (preg_match('#^(content|contacts|static|weblinks|menu)$#', $sub)) {
 445                                    if (!isset($data->$node->$key->joomlalinks)) {
 446                                        $data->$node->$key->joomlalinks = new StdClass();
 447                                        $data->$node->$key->joomlalinks->enable = 1;
 448                                    }
 449                                    $data->$node->$key->joomlalinks->$sub = $value;
 450                                } else {
 451                                    $data->$node->$key->$sub = new StdClass();
 452                                    $data->$node->$key->$sub->enable = 1;
 453                                }
 454                            } else {
 455                                $data->$node->$key = $value;
 456                            }
 457                        }
 458                    }
 459                    // re-assign params
 460                    $row->params = json_encode($data);
 461
 462                    // re-assign other values
 463                    $row->name = $group->name;
 464                    $row->description = $group->description;
 465                    $row->users = $group->users;
 466                    $row->types = $group->types;
 467                    $row->components = $group->components;
 468                    $row->published = $group->published;
 469                    $row->ordering = $group->ordering;
 470
 471                    // add area data
 472                    if ($row->name == 'Default') {
 473                        $row->area = 0;
 474                    }
 475
 476                    if ($row->name == 'Front End') {
 477                        $row->area = 1;
 478                    }
 479
 480                    if (self::checkTable('#__wf_profiles')) {
 481                        $name = $row->name;
 482
 483                        // check for existing profile
 484                        $query = 'SELECT id FROM #__wf_profiles' . ' WHERE name = ' . $db->Quote($name);
 485                        $db->setQuery($query);
 486                        // create name copy if exists
 487                        while ($db->loadResult()) {
 488                            $name = JText::sprintf('WF_PROFILES_COPY_OF', $name);
 489
 490                            $query = 'SELECT id FROM #__wf_profiles' . ' WHERE name = ' . $db->Quote($name);
 491
 492                            $db->setQuery($query);
 493                        }
 494                        // set name
 495                        $row->name = $name;
 496                    }
 497
 498                    if (!$row->store()) {
 499                        $app->enqueueMessage('Conversion of group data failed : ' . $row->name, 'error');
 500                    } else {
 501                        $app->enqueueMessage('Conversion of group data successful : ' . $row->name);
 502                    }
 503
 504                    unset($row);
 505                }
 506
 507                // If profiles table empty due to error, install profiles data
 508                if (!self::checkTableContents('#__wf_profiles')) {
 509                    self::installProfiles();
 510                } else {
 511                    // add Blogger profile
 512                    self::installProfile('Blogger');
 513                    // add Mobile profile
 514                    self::installProfile('Mobile');
 515                }
 516            } else {
 517                return false;
 518            }
 519            // Install profiles
 520        } else {
 521            self::installProfiles();
 522        }
 523
 524        // Remove Plugins menu item
 525        $query = 'DELETE FROM #__components' . ' WHERE admin_menu_link = ' . $db->Quote('option=com_jce&type=plugins');
 526
 527        $db->setQuery($query);
 528        $db->query();
 529
 530        // Update Component Name
 531        $query = 'UPDATE #__components' . ' SET name = ' . $db->Quote('COM_JCE') . ' WHERE ' . $db->Quote('option') . '=' . $db->Quote('com_jce') . ' AND parent = 0';
 532
 533        $db->setQuery($query);
 534        $db->query();
 535
 536        // Fix links for other views and edit names
 537        $menus = array('install' => 'installer', 'group' => 'profiles', 'groups' => 'profiles', 'config' => 'config');
 538
 539        $row = JTable::getInstance('component');
 540
 541        foreach ($menus as $k => $v) {
 542            $query = 'SELECT id FROM #__components' . ' WHERE admin_menu_link = ' . $db->Quote('option=com_jce&type=' . $k);
 543            $db->setQuery($query);
 544            $id = $db->loadObject();
 545
 546            if ($id) {
 547                $row->load($id);
 548                $row->name = $v;
 549                $row->admin_menu_link = 'option=com_jce&view=' . $v;
 550
 551                if (!$row->store()) {
 552                    $mainframe->enqueueMessage('Unable to update Component Links for view : ' . strtoupper($v), 'error');
 553                }
 554            }
 555        }
 556
 557        // remove old admin language files
 558        $folders = JFolder::folders(JPATH_ADMINISTRATOR . '/language', '.', false, true, array('.svn', 'CVS', 'en-GB'));
 559
 560        foreach ($folders as $folder) {
 561            $name = basename($folder);
 562            $files = array($name . '.com_jce.ini', $name . '.com_jce.menu.ini', $name . '.com_jce.xml');
 563            foreach ($files as $file) {
 564                if (is_file($folder . '/' . $file)) {
 565                    @JFile::delete($folder . '/' . $file);
 566                }
 567            }
 568        }
 569
 570        // remove old site language files
 571        $folders = JFolder::folders(JPATH_SITE . '/language', '.', false, true, array('.svn', 'CVS', 'en-GB'));
 572
 573        foreach ($folders as $folder) {
 574            $files = JFolder::files($folder, '^' . basename($folder) . '\.com_jce([_a-z0-9]+)?\.(ini|xml)$', false, true);
 575            @JFile::delete($files);
 576        }
 577
 578        // remove legacy admin folders
 579        $folders = array('cpanel', 'config', 'css', 'groups', 'plugins', 'img', 'installer', 'js');
 580        foreach ($folders as $folder) {
 581            if (is_dir($admin . '/' . $folder)) {
 582                @JFolder::delete($admin . '/' . $folder);
 583            }
 584        }
 585
 586        // remove legacy admin files
 587        $files = array('editor.php', 'helper.php', 'updater.php');
 588
 589        foreach ($files as $file) {
 590            if (is_file($admin . '/' . $file)) {
 591                @JFile::delete($admin . '/' . $file);
 592            }
 593        }
 594
 595        // remove legacy admin folders
 596        $folders = array('controller', 'css', 'js');
 597        foreach ($folders as $folder) {
 598            if (is_dir($site . '/' . $folder)) {
 599                @JFolder::delete($site . '/' . $folder);
 600            }
 601        }
 602
 603        // remove legacy admin files
 604        $files = array('popup.php');
 605
 606        foreach ($files as $file) {
 607            if (is_file($site . '/' . $file)) {
 608                @JFile::delete($site . '/' . $file);
 609            }
 610        }
 611
 612
 613        if (!defined('JPATH_PLATFORM')) {
 614            // remove old plugin folder
 615            $path = JPATH_PLUGINS . '/editors';
 616
 617            if (is_dir($path . '/jce')) {
 618                @JFolder::delete($path . '/jce');
 619            }
 620        }
 621
 622        return true;
 623    }
 624
 625    private static function installProfile($name) {
 626        $db = JFactory::getDBO();
 627
 628        $query = $db->getQuery(true);
 629
 630        if (is_object($query)) {
 631            $query->select('COUNT(id)')->from('#__wf_profiles')->where('name = ' . $db->Quote($name));
 632        } else {
 633            $query = 'SELECT COUNT(id) FROM #__wf_profiles WHERE name = ' . $db->Quote($name);
 634        }
 635
 636        $db->setQuery($query);
 637        $id = $db->loadResult();
 638
 639        if (!$id) {
 640            // Blogger
 641            $file = JPATH_ADMINISTRATOR . '/components/com_jce/models/profiles.xml';
 642
 643            $xml = self::loadXMLFile($file);
 644
 645            if ($xml) {
 646                foreach ($xml->profiles->children() as $profile) {
 647                    if ((string) $profile->attributes()->name == $name) {
 648                        $row = JTable::getInstance('profiles', 'WFTable');
 649
 650                        require_once(JPATH_ADMINISTRATOR . '/components/com_jce/models/profiles.php');
 651                        $groups = WFModelProfiles::getUserGroups((int) $profile->children('area'));
 652
 653                        foreach ($profile->children() as $item) {
 654                            switch ((string) $item->getName()) {
 655                                case 'types':
 656                                    $row->types = implode(',', $groups);
 657                                    break;
 658                                case 'area':
 659                                    $row->area = (int) $item;
 660                                    break;
 661                                case 'rows':
 662                                    $row->rows = (string) $item;
 663                                    break;
 664                                case 'plugins':
 665                                    $row->plugins = (string) $item;
 666                                    break;
 667                                default:
 668                                    $key = $item->getName();
 669                                    $row->$key = (string) $item;
 670
 671                                    break;
 672                            }
 673                        }
 674                        $row->store();
 675                    }
 676                }
 677            }
 678        }
 679    }
 680
 681    /**
 682     * Upgrade database tables and remove legacy folders
 683     * @return Boolean
 684     */
 685    private static function upgrade($version) {
 686        $app = JFactory::getApplication();
 687        $db = JFactory::getDBO();
 688
 689        jimport('joomla.filesystem.folder');
 690        jimport('joomla.filesystem.file');
 691
 692        $admin = JPATH_ADMINISTRATOR . '/components/com_jce';
 693        $site = JPATH_SITE . '/components/com_jce';
 694
 695        // add tables path
 696        JTable::addIncludePath($admin . '/tables');
 697
 698        // upgrade from 1.5.x to 2.0.0 (only in Joomla! 1.5)
 699        if (version_compare($version, '2.0.0', '<') && !defined('JPATH_PLATFORM')) {
 700            return self::upgradeLegacy();
 701        }// end JCE 1.5 upgrade
 702        // Remove folders
 703        $folders = array(
 704            // Remove JQuery folders from admin
 705            $admin . '/media/css/jquery',
 706            $admin . '/media/js/jquery',
 707            // remove plugin package folder
 708            $admin . '/plugin',
 709            // remove legend view
 710            $admin . '/views/legend',
 711            // remove controller from site
 712            $site . '/controller',
 713            // Remove plugin language files (incorporated into main language file)
 714            $site . '/editor/tiny_mce/plugins/article/langs',
 715            $site . '/editor/tiny_mce/plugins/imgmanager/langs',
 716            $site . '/editor/tiny_mce/plugins/link/langs',
 717            $site . '/editor/tiny_mce/plugins/searchreplace/langs',
 718            $site . '/editor/tiny_mce/plugins/style/langs',
 719            $site . '/editor/tiny_mce/plugins/table/langs',
 720            $site . '/editor/tiny_mce/plugins/xhtmlxtras/langs',
 721            // remove paste folder
 722            $site . '/editor/tiny_mce/plugins/paste',
 723            // remove jquery
 724            $site . '/editor/libraries/js/jquery',
 725            // remove browser extension
 726            $site . '/editor/extensions/browser'
 727        );
 728
 729        foreach ($folders as $folder) {
 730            if (JFolder::exists($folder)) {
 731                @JFolder::delete($folder);
 732            }
 733        }
 734
 735        // Remove files
 736        $files = array(
 737            // remove javascript files from admin (moved to site)
 738            $admin . '/media/js/colorpicker.js',
 739            $admin . '/media/js/help.js',
 740            $admin . '/media/js/html5.js',
 741            $admin . '/media/js/select.js',
 742            $admin . '/media/js/tips.js',
 743            // remove legend.js
 744            $admin . '/media/js/legend.js',
 745            // remove css files from admin (moved to site)
 746            $admin . '/media/css/help.css',
 747            $admin . '/media/css/select.css',
 748            $admin . '/media/css/tips.css',
 749            // remove legend model
 750            $admin . '/models/legend.php',
 751            // remove extension adapter
 752            $admin . '/adapters/extension.php',
 753            // remove error class from site (moved to admin)
 754            $site . '/editor/libraries/classes/error.php',
 755            // remove popup file
 756            $site . '/popup.php',
 757            // remove anchor from theme (moved to plugins)
 758            $site . '/editor/tiny_mce/themes/advanced/css/anchor.css',
 759            $site . '/editor/tiny_mce/themes/advanced/css/js/anchor.js',
 760            $site . '/editor/tiny_mce/themes/advanced/css/tmpl/anchor.php',
 761            // remove redundant file
 762            $site . '/editor/tiny_mce/themes/advanced/css/skins/default/img/items.gif',
 763            // remove search files from file browser (renamed to filter)
 764            $site . '/editor/extensions/browser/css/search.css',
 765            $site . '/editor/extensions/browser/js/search.js',
 766            $site . '/editor/extensions/browser/search.php',
 767            // remove dilg language file from theme (incorporated into main dlg file)
 768            $site . '/editor/tiny_mce/themes/advanced/langs/en_dlg.js',
 769            // remove old jquery UI
 770            $site . '/editor/libraries/jquery/js/jquery-ui-1.9.0.custom.min.js'
 771        );
 772
 773        foreach ($files as $file) {
 774            if (JFile::exists($file)) {
 775                @JFile::delete($file);
 776            }
 777        }
 778
 779        // 2.1 - Add visualblocks plugin
 780        if (version_compare($version, '2.1', '<')) {
 781            $profiles = self::getProfiles();
 782            $profile = JTable::getInstance('Profiles', 'WFTable');
 783
 784            if (!empty($profiles)) {
 785                foreach ($profiles as $item) {
 786                    $profile->load($item->id);
 787
 788                    if (strpos($profile->rows, 'visualblocks') === false) {
 789                        $profile->rows = str_replace('visualchars', 'visualchars,visualblocks', $profile->rows);
 790                    }
 791                    if (strpos($profile->plugins, 'visualblocks') === false) {
 792                        $profile->plugins = str_replace('visualchars', 'visualchars,visualblocks', $profile->plugins);
 793                    }
 794
 795                    $profile->store();
 796                }
 797            }
 798        }
 799
 800        // 2.1.1 - Add anchor plugin
 801        if (version_compare($version, '2.1.1', '<')) {
 802            $profiles = self::getProfiles();
 803            $profile = JTable::getInstance('Profiles', 'WFTable');
 804
 805            if (!empty($profiles)) {
 806                foreach ($profiles as $item) {
 807                    $profile->load($item->id);
 808
 809                    // add anchor to end of plugins list
 810                    if (strpos($profile->rows, 'anchor') !== false) {
 811                        $profile->plugins .= ',anchor';
 812                    }
 813
 814                    $profile->store();
 815                }
 816            }
 817        }
 818
 819        // 2.2.1 - Add "Blogger" profile
 820        if (version_compare($version, '2.2.1', '<')) {
 821            self::installProfile('Blogger');
 822        }
 823
 824        // 2.2.1 to 2.2.5 - Remove K2Links partial install
 825        if (version_compare($version, '2.2.1', '>') && version_compare($version, '2.2.5', '<')) {
 826            $path = $site . '/editor/extensions/links';
 827
 828            if (is_file($path . '/k2links.php') && is_file($path . '/k2links.xml') && !is_dir($path . '/k2links')) {
 829                @JFile::delete($path . '/k2links.php');
 830                @JFile::delete($path . '/k2links.xml');
 831            }
 832        }
 833
 834        // replace some profile row items
 835        if (version_compare($version, '2.2.8', '<')) {
 836            $profiles = self::getProfiles();
 837            $profile = JTable::getInstance('Profiles', 'WFTable');
 838
 839            if (!empty($profiles)) {
 840                foreach ($profiles as $item) {
 841                    $profile->load($item->id);
 842
 843                    $profile->rows = str_replace('paste', 'clipboard', $profile->rows);
 844                    $profile->plugins = str_replace('paste', 'clipboard', $profile->plugins);
 845
 846                    $data = json_decode($profile->params, true);
 847
 848                    // swap paste data to 'clipboard'
 849                    if ($data && array_key_exists('paste', $data)) {
 850                        $params = array();
 851
 852                        // add 'paste_' prefix
 853                        foreach ($data['paste'] as $k => $v) {
 854                            $params['paste_' . $k] = $v;
 855                        }
 856
 857                        // remove paste parameters
 858                        unset($data['paste']);
 859
 860                        // assign new params to clipboard
 861                        $data['clipboard'] = $params;
 862                    }
 863
 864                    $profile->params = json_encode($data);
 865
 866                    $profile->store();
 867                }
 868            }
 869        }
 870
 871        if (version_compare($version, '2.3.0beta', '<')) {
 872            // add Mobile profile
 873            self::installProfile('Mobile');
 874        }
 875        
 876        if (version_compare($version, '2.2.9', '<') || version_compare($version, '2.3.0beta3', '<')) {
 877            $profiles = self::getProfiles();
 878            $profile = JTable::getInstance('Profiles', 'WFTable');
 879
 880            if (!empty($profiles)) {
 881                foreach ($profiles as $item) {
 882                    $profile->load($item->id);
 883                    
 884                    $buttons = array('buttons' => array());
 885                    
 886                    if (strpos($profile->rows, 'numlist') !== false) {
 887                        $buttons['buttons'][] = 'numlist';
 888                        
 889                        $profile->rows = str_replace('numlist', 'lists', $profile->rows);
 890                    }
 891                    
 892                    if (strpos($profile->rows, 'bullist') !== false) {
 893                        $buttons['buttons'][] = 'bullist';
 894                        
 895                        if (strpos($profile->rows, 'lists') === false) {
 896                            $profile->rows = str_replace('bullist', 'lists', $profile->rows);
 897                        }
 898                    }
 899                    // remove bullist and numlist
 900                    $profile->rows = str_replace(array('bullist', 'numlist'), '', $profile->rows);
 901                    // replace multiple commas with a single one
 902                    $profile->rows = preg_replace('#,+#', ',', $profile->rows);
 903                    // fix rows
 904                    $profile->rows = str_replace(',;', ';', $profile->rows);
 905                    
 906                    if (!empty($buttons['buttons'])) {
 907                        $profile->plugins .= ',lists';
 908                        
 909                        $data = json_decode($profile->params, true);
 910                        $data['lists'] = $buttons;
 911                        
 912                        $profile->params = json_encode($data);
 913                        
 914                        $profile->store();
 915                    }
 916                }
 917            }
 918        }
 919
 920        return true;
 921    }
 922
 923    private static function getProfiles() {
 924        $db = JFactory::getDBO();
 925
 926        if (is_object($query)) {
 927            $query->select('id')->from('#__wf_profiles');
 928        } else {
 929            $query = 'SELECT id FROM #__wf_profiles';
 930        }
 931
 932        $db->setQuery($query);
 933        return $db->loadObjectList();
 934    }
 935
 936    private static function createProfilesTable() {
 937        include_once (dirname(__FILE__) . '/includes/base.php');
 938        include_once (dirname(__FILE__) . '/models/profiles.php');
 939
 940        $profiles = new WFModelProfiles();
 941
 942        if (method_exists($profiles, 'createProfilesTable')) {
 943            return $profiles->createProfilesTable();
 944        }
 945
 946        return false;
 947    }
 948
 949    private static function installProfiles() {
 950        include_once (dirname(__FILE__) . '/includes/base.php');
 951        include_once (dirname(__FILE__) . '/models/profiles.php');
 952
 953        $profiles = new WFModelProfiles();
 954
 955        if (method_exists($profiles, 'installProfiles')) {
 956            return $profiles->installProfiles();
 957        }
 958
 959        return false;
 960    }
 961
 962    /**
 963     * Install additional packages
 964     * @return Array or false
 965     * @param object $path[optional] Path to package folder
 966     */
 967    private static function installPackages($source) {
 968        jimport('joomla.installer.installer');
 969
 970        $mainframe = JFactory::getApplication();
 971
 972        $db = JFactory::getDBO();
 973
 974        $result = '';
 975
 976        JTable::addIncludePath(JPATH_LIBRARIES . '/joomla/database/table');
 977
 978        $packages = array(
 979            'editors' => array('jce'),
 980            'quickicon' => array('jcefilebrowser'),
 981            'modules' => array('mod_jcefilebrowser')
 982        );
 983
 984        foreach ($packages as $folder => $element) {
 985            // Joomla! 2.5
 986            if (defined('JPATH_PLATFORM')) {
 987                if ($folder == 'modules') {
 988                    continue;
 989                }
 990                // Joomla! 1.5  
 991            } else {
 992                if ($folder == 'quickicon') {
 993                    continue;
 994                }
 995            }
 996
 997            $installer = new JInstaller();
 998            $installer->setOverwrite(true);
 999
1000            if ($installer->install($source . '/' . $folder)) {
1001
1002                if (method_exists($installer, 'loadLanguage')) {
1003                    $installer->loadLanguage();
1004                }
1005
1006                if ($installer->message) {
1007                    $result .= '<li class="success">' . JText::_($installer->message, $installer->message) . '</li>';
1008                }
1009
1010                // enable quickicon
1011                if ($folder == 'quickicon') {
1012                    $plugin = JTable::getInstance('extension');
1013
1014                    foreach ($element as $item) {
1015                        $id = $plugin->find(array('type' => 'plugin', 'folder' => $folder, 'element' => $item));
1016
1017                        $plugin->load($id);
1018                        $plugin->publish();
1019                    }
1020                }
1021                // enable module
1022                if ($folder == 'modules') {
1023                    $module = JTable::getInstance('module');
1024
1025                    foreach ($element as $item) {
1026                        $id = self::getModule($item);
1027
1028                        $module->load($id);
1029                        $module->position = 'icon';
1030                        $module->ordering = 100;
1031                        $module->published = 1;
1032                        $module->store();
1033                    }
1034                }
1035
1036                if ($folder == 'editors') {
1037                    $manifest = $installer->getPath('manifest');
1038
1039                    if (basename($manifest) == 'legacy.xml') {
1040                        // rename legacy.xml to jce.xml
1041                        JFile::move($installer->getPath('extension_root') . '/' . basename($manifest), $installer->getPath('extension_root') . '/jce.xml');
1042                    }
1043                }
1044
1045                // add index files
1046                self::addIndexfiles(array($installer->getPath('extension_root')));
1047            } else {
1048                $result .= '<li class="error">' . JText::_($installer->message, $installer->message) . '</li>';
1049            }
1050        }
1051
1052        return $result;
1053    }
1054
1055    private static function getModule($name) {
1056        // Joomla! 2.5
1057        if (defined('JPATH_PLATFORM')) {
1058            $module = JTable::getInstance('extension');
1059            return $module->find(array('type' => 'module', 'element' => $name));
1060
1061            // Joomla! 1.5    
1062        } else {
1063            $db = JFactory::getDBO();
1064            $query = 'SELECT id FROM #__modules' . ' WHERE module = ' . $db->Quote($name);
1065
1066            $db->setQuery($query);
1067            return $db->loadResult();
1068        }
1069    }
1070
1071    private static function getPlugin($folder, $element) {
1072        // Joomla! 2.5
1073        if (defined('JPATH_PLATFORM')) {
1074            $plugin = JTable::getInstance('extension');
1075            return $plugin->find(array('type' => 'plugin', 'folder' => $folder, 'element' => $element));
1076            // Joomla! 1.5    
1077        } else {
1078            $plugin = JTable::getInstance('plugin');
1079
1080            $db = JFactory::getDBO();
1081            $query = 'SELECT id FROM #__plugins' . ' WHERE folder = ' . $db->Quote($folder) . ' AND element = ' . $db->Quote($element);
1082
1083            $db->setQuery($query);
1084            return $db->loadResult();
1085        }
1086    }
1087
1088    /**
1089     * Uninstall the editor
1090     * @return boolean
1091     */
1092    private static function removePackages() {
1093        $app = JFactory::getApplication();
1094        $db = JFactory::getDBO();
1095
1096        jimport('joomla.module.helper');
1097        jimport('joomla.installer.installer');
1098
1099        $plugins = array(
1100            'editors' => array('jce'),
1101            'quickicon' => array('jcefilebrowser')
1102        );
1103
1104        $modules = array('mod_jcefilebrowser');
1105
1106        // items to remove
1107        $items = array(
1108            'plugin' => array(),
1109            'module' => array()
1110        );
1111
1112        foreach ($plugins as $folder => $elements) {
1113            foreach ($elements as $element) {
1114                $item = self::getPlugin($folder, $element);
1115
1116                if ($item) {
1117                    $items['plugin'][] = $item;
1118                }
1119            }
1120        }
1121
1122        foreach ($modules as $module) {
1123            $item = self::getModule($module);
1124
1125            if ($item) {
1126                $items['module'][] = $item;
1127            }
1128        }
1129
1130        foreach ($items as $type => $extensions) {
1131            if ($extensions) {
1132                foreach ($extensions as $id) {
1133                    $installer = new JInstaller();
1134                    $installer->uninstall($type, $id);
1135                    $app->enqueueMessage($installer->message);
1136                }
1137            }
1138        }
1139    }
1140
1141    private static function addIndexfiles($paths) {
1142        jimport('joomla.filesystem.folder');
1143        jimport('joomla.filesystem.file');
1144
1145        // get the base file
1146        $file = JPATH_ADMINISTRATOR . '/components' . 'com_jce/index.html';
1147
1148        if (is_file($file)) {
1149
1150            foreach ((array) $paths as $path) {
1151                if (is_dir($path)) {
1152                    // admin component
1153                    $folders = JFolder::folders($path, '.', true, true);
1154
1155                    foreach ($folders as $folder) {
1156                        JFile::copy($file, $folder . '/' . basename($file));
1157                    }
1158                }
1159            }
1160        }
1161    }
1162
1163    private static function legacyCleanup() {
1164        $db = JFactory::getDBO();
1165
1166        $query = 'DROP TABLE IF EXISTS #__jce_groups';
1167        $db->setQuery($query);
1168        $db->query();
1169
1170        $query = 'DROP TABLE IF EXISTS #__jce_plugins';
1171        $db->setQuery($query);
1172        $db->query();
1173
1174        $query = 'DROP TABLE IF EXISTS #__jce_extensions';
1175        $db->setQuery($query);
1176        $db->query();
1177    }
1178
1179    private static function checkTable($table) {
1180        $db = JFactory::getDBO();
1181
1182        $tables = $db->getTableList();
1183
1184        if (!empty($tables)) {
1185            // swap array values with keys, convert to lowercase and return array keys as values
1186            $tables = array_keys(array_change_key_case(array_flip($tables)));
1187            $app = JFactory::getApplication();
1188            $match = str_replace('#__', strtolower($app->getCfg('dbprefix', '')), $table);
1189
1190            return in_array($match, $tables);
1191        }
1192
1193        // try with query
1194        $query = $db->getQuery(true);
1195
1196        if (is_object($query)) {
1197            $query->select('COUNT(id)')->from($table);
1198        } else {
1199            $query = 'SELECT COUNT(id) FROM ' . $table;
1200        }
1201
1202        $db->setQuery($query);
1203
1204        return $db->query();
1205    }
1206
1207    /**
1208     * Check table contents
1209     * @return integer
1210     * @param string $table Table name
1211     */
1212    private static function checkTableContents($table) {
1213        $db = JFactory::getDBO();
1214
1215        $query = $db->getQuery(true);
1216
1217        if (is_object($query)) {
1218            $query->select('COUNT(id)')->from($table);
1219        } else {
1220            $query = 'SELECT COUNT(id) FROM ' . $table;
1221        }
1222
1223        $db->setQuery($query);
1224
1225        return $db->loadResult();
1226    }
1227
1228    private static function checkTableColumn($table, $column) {
1229        $db = JFactory::getDBO();
1230
1231        // use built in function
1232        if (method_exists($db, 'getTableColumns')) {
1233            $fields = $db->getTableColumns($table);
1234        } else {
1235            $db->setQuery('DESCRIBE ' . $table);
1236            $fields = $db->loadResultArray();
1237
1238            // we need to check keys not values
1239            $fields = array_flip($fields);
1240        }
1241
1242        return array_key_exists($column, $fields);
1243    }
1244
1245}
1246
1247?>