PageRenderTime 45ms CodeModel.GetById 2ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/code/administrator/components/com_jce/install.php

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