| 1 | <?php |
|---|
| 2 | /* ----------------------------------------------------------------------------- |
|---|
| 3 | Plugin : Advanced Menu Manager |
|---|
| 4 | Author : Grum |
|---|
| 5 | email : grum@piwigo.org |
|---|
| 6 | website : http://photos.grum.fr |
|---|
| 7 | PWG user : http://forum.phpwebgallery.net/profile.php?id=3706 |
|---|
| 8 | |
|---|
| 9 | << May the Little SpaceFrog be with you ! >> |
|---|
| 10 | ------------------------------------------------------------------------------ |
|---|
| 11 | See main.inc.php for release information |
|---|
| 12 | |
|---|
| 13 | MyPolls_Install : classe to manage plugin install |
|---|
| 14 | |
|---|
| 15 | --------------------------------------------------------------------------- */ |
|---|
| 16 | include_once('amm_version.inc.php'); |
|---|
| 17 | include_once('amm_root.class.inc.php'); |
|---|
| 18 | include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCTables.class.inc.php'); |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | class AMM_install extends AMM_root |
|---|
| 22 | { |
|---|
| 23 | private $tablef; |
|---|
| 24 | |
|---|
| 25 | public function __construct($prefixeTable, $filelocation) |
|---|
| 26 | { |
|---|
| 27 | parent::__construct($prefixeTable, $filelocation); |
|---|
| 28 | $this->tablef= new GPCTables($this->tables); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | public function __destruct() |
|---|
| 32 | { |
|---|
| 33 | unset($this->tablesManager); |
|---|
| 34 | unset($this->tablef); |
|---|
| 35 | parent::__destruct(); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * function for installation process |
|---|
| 41 | * |
|---|
| 42 | * @return Bool : true if install process is ok, otherwise false |
|---|
| 43 | */ |
|---|
| 44 | public function install() |
|---|
| 45 | { |
|---|
| 46 | $this->initConfig(); |
|---|
| 47 | $this->loadConfig(); |
|---|
| 48 | $this->config['installed']=AMM_VERSION2; |
|---|
| 49 | $this->config['newInstall']='y'; |
|---|
| 50 | $this->saveConfig(); |
|---|
| 51 | |
|---|
| 52 | $tables_def=array( |
|---|
| 53 | "CREATE TABLE `".$this->tables['urls']."` ( |
|---|
| 54 | `id` int(11) NOT NULL auto_increment, |
|---|
| 55 | `label` varchar(50) NOT NULL default '', |
|---|
| 56 | `url` varchar(255) NOT NULL default '', |
|---|
| 57 | `mode` int(11) NOT NULL default '0', |
|---|
| 58 | `icon` varchar(50) NOT NULL default '', |
|---|
| 59 | `position` int(11) NOT NULL default '0', |
|---|
| 60 | `visible` char(1) NOT NULL default 'y', |
|---|
| 61 | `accessUsers` varchar(1024) NOT NULL, |
|---|
| 62 | `accessGroups` varchar(1024) NOT NULL, |
|---|
| 63 | PRIMARY KEY (`id`), |
|---|
| 64 | KEY `order_key` (`position`) |
|---|
| 65 | )", |
|---|
| 66 | |
|---|
| 67 | "CREATE TABLE `".$this->tables['personalised']."` ( |
|---|
| 68 | `id` int(11) NOT NULL auto_increment, |
|---|
| 69 | `visible` char(1) NOT NULL default 'y', |
|---|
| 70 | `nfo` varchar(255) NOT NULL default '', |
|---|
| 71 | PRIMARY KEY (`id`) |
|---|
| 72 | )", |
|---|
| 73 | |
|---|
| 74 | "CREATE TABLE `".$this->tables['personalised_langs']."` ( |
|---|
| 75 | `id` INTEGER UNSIGNED NOT NULL DEFAULT 0, |
|---|
| 76 | `lang` CHAR(5) NOT NULL default '*', |
|---|
| 77 | `title` VARCHAR(255) NOT NULL default '', |
|---|
| 78 | `content` TEXT NOT NULL, |
|---|
| 79 | PRIMARY KEY (`id`, `lang`) |
|---|
| 80 | )", |
|---|
| 81 | |
|---|
| 82 | "CREATE TABLE `".$this->tables['blocks']."` ( |
|---|
| 83 | `id` VARCHAR(40) NOT NULL, |
|---|
| 84 | `order` INTEGER UNSIGNED NOT NULL, |
|---|
| 85 | `users` VARCHAR(1024) NOT NULL, |
|---|
| 86 | `groups` VARCHAR(1024) NOT NULL, |
|---|
| 87 | PRIMARY KEY (`id`), |
|---|
| 88 | INDEX `byOrder`(`order`) |
|---|
| 89 | )" |
|---|
| 90 | |
|---|
| 91 | ); |
|---|
| 92 | //$table_def array |
|---|
| 93 | $tables_def = create_table_add_character_set($tables_def); |
|---|
| 94 | $result=$this->tablef->create($tables_def); |
|---|
| 95 | unset($tables_def); |
|---|
| 96 | |
|---|
| 97 | GPCCore::register($this->getPluginName(), AMM_VERSION, AMM_GPC_NEEDED); |
|---|
| 98 | |
|---|
| 99 | return($result); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * function for uninstall process |
|---|
| 105 | */ |
|---|
| 106 | public function uninstall() |
|---|
| 107 | { |
|---|
| 108 | $this->deleteConfig(); |
|---|
| 109 | $this->tablef->drop(); |
|---|
| 110 | GPCCore::unregister($this->getPluginName()); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | public function activate() |
|---|
| 114 | { |
|---|
| 115 | global $template; |
|---|
| 116 | |
|---|
| 117 | $this->initConfig(); |
|---|
| 118 | $this->loadConfig(); |
|---|
| 119 | |
|---|
| 120 | $this->udpateTablesDef(); |
|---|
| 121 | |
|---|
| 122 | $this->config['newInstall']='n'; |
|---|
| 123 | $this->config['installed']=AMM_VERSION2; //update the installed release number |
|---|
| 124 | $this->saveConfig(); |
|---|
| 125 | |
|---|
| 126 | GPCCore::register($this->getPluginName(), AMM_VERSION, AMM_GPC_NEEDED); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public function deactivate() |
|---|
| 130 | { |
|---|
| 131 | $this->initConfig(); |
|---|
| 132 | $this->loadConfig(); |
|---|
| 133 | $this->restoreMenuConfig(); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * update tables & config between releases |
|---|
| 139 | * |
|---|
| 140 | */ |
|---|
| 141 | protected function udpateTablesDef() |
|---|
| 142 | { |
|---|
| 143 | global $conf; |
|---|
| 144 | |
|---|
| 145 | /* AMM release earlier than the 2.1.3 uses two parameters to manage the display |
|---|
| 146 | * of the menu items ("amm_sections_modspecials" and "amm_sections_modmenu") |
|---|
| 147 | * |
|---|
| 148 | * These two parameters are replaced by a single parameter "amm_blocks_items" |
|---|
| 149 | * |
|---|
| 150 | * This function aim to import the old conf into the new conf property |
|---|
| 151 | */ |
|---|
| 152 | if(isset($this->config['amm_sections_modspecials'])) |
|---|
| 153 | { |
|---|
| 154 | foreach($this->config['amm_sections_modspecials'] as $key=>$val) |
|---|
| 155 | { |
|---|
| 156 | $this->config['amm_blocks_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/"; |
|---|
| 157 | } |
|---|
| 158 | unset($this->config['amm_sections_modspecials']); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | if(isset($this->config['amm_sections_modmenu'])) |
|---|
| 162 | { |
|---|
| 163 | foreach($this->config['amm_sections_modmenu'] as $key=>$val) |
|---|
| 164 | { |
|---|
| 165 | $this->config['amm_blocks_items'][$key]['visibility']=($val=="y")?"guest,generic,normal,admin/":"admin/"; |
|---|
| 166 | } |
|---|
| 167 | unset($this->config['amm_sections_modmenu']); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | if(!array_key_exists('installed', $this->config)) |
|---|
| 171 | { |
|---|
| 172 | /* |
|---|
| 173 | * if key does not exist, probably try to update a plugin older than the |
|---|
| 174 | * 2.2.0 release |
|---|
| 175 | */ |
|---|
| 176 | $this->config['installed']="02.01.06"; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | switch($this->config['installed']) |
|---|
| 180 | { |
|---|
| 181 | case '02.01.06': |
|---|
| 182 | $this->config['newInstall']='n'; |
|---|
| 183 | $this->updateFrom_020106(); |
|---|
| 184 | case '02.02.00': |
|---|
| 185 | case '02.02.01': |
|---|
| 186 | case '02.02.02': |
|---|
| 187 | case '02.02.03': |
|---|
| 188 | $this->config['newInstall']='n'; |
|---|
| 189 | $this->updateFrom_020200(); |
|---|
| 190 | default: |
|---|
| 191 | /* |
|---|
| 192 | * default is applied for fresh install |
|---|
| 193 | */ |
|---|
| 194 | |
|---|
| 195 | if($this->config['installed']<='02.02.03' or |
|---|
| 196 | $this->config['newInstall']=='y') |
|---|
| 197 | { |
|---|
| 198 | /* |
|---|
| 199 | * if new install |
|---|
| 200 | * or plugin updated from a release <= 2.2.3 |
|---|
| 201 | * or plugin |
|---|
| 202 | * |
|---|
| 203 | * update AMM menu from piwigo's menu |
|---|
| 204 | */ |
|---|
| 205 | $this->backupMenuConfig(true); |
|---|
| 206 | } |
|---|
| 207 | else |
|---|
| 208 | { |
|---|
| 209 | /* |
|---|
| 210 | * plugin actived without being installed or updated, only backup |
|---|
| 211 | * the piwigo's menu |
|---|
| 212 | */ |
|---|
| 213 | $this->backupMenuConfig(false); |
|---|
| 214 | } |
|---|
| 215 | break; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | /** |
|---|
| 221 | * update the database from the release 2.1.6 |
|---|
| 222 | * |
|---|
| 223 | * - update config for menu translation |
|---|
| 224 | * - update fields length for table 'personalised' |
|---|
| 225 | */ |
|---|
| 226 | private function updateFrom_020106() |
|---|
| 227 | { |
|---|
| 228 | $sql="ALTER TABLE `".$this->tables['personalised']."` |
|---|
| 229 | MODIFY COLUMN `id` INTEGER NOT NULL AUTO_INCREMENT, |
|---|
| 230 | MODIFY COLUMN `title` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, |
|---|
| 231 | MODIFY COLUMN `nfo` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;"; |
|---|
| 232 | pwg_query($sql); |
|---|
| 233 | |
|---|
| 234 | foreach($this->config['amm_blocks_items'] as $key => $val) |
|---|
| 235 | { |
|---|
| 236 | $this->config['amm_blocks_items'][$key]['translation'] = $this->defaultMenus[$key]['translation']; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | /** |
|---|
| 243 | * update the database from the release 2.2.0 |
|---|
| 244 | * |
|---|
| 245 | * - create 'personalised_lang' table ; filled from the 'personalised' table |
|---|
| 246 | * values |
|---|
| 247 | * - modify 'personalised' table structure (remove lang attributes) |
|---|
| 248 | * - modify 'urls' table structure (add users&group access) |
|---|
| 249 | * - update 'urls' table values (default values for users access) |
|---|
| 250 | * - update config (parameter 'amm_sections_items' is renamed into 'amm_blocks_items') |
|---|
| 251 | */ |
|---|
| 252 | private function updateFrom_020200() |
|---|
| 253 | { |
|---|
| 254 | $tables_def=array( |
|---|
| 255 | "CREATE TABLE `".$this->tables['personalised_langs']."` ( |
|---|
| 256 | `id` INTEGER UNSIGNED NOT NULL DEFAULT 0, |
|---|
| 257 | `lang` CHAR(5) NOT NULL default '*', |
|---|
| 258 | `title` VARCHAR(255) NOT NULL default '', |
|---|
| 259 | `content` TEXT NOT NULL, |
|---|
| 260 | PRIMARY KEY (`id`, `lang`) |
|---|
| 261 | )", |
|---|
| 262 | |
|---|
| 263 | "CREATE TABLE `".$this->tables['blocks']."` ( |
|---|
| 264 | `id` VARCHAR(40) NOT NULL, |
|---|
| 265 | `order` INTEGER UNSIGNED NOT NULL, |
|---|
| 266 | `users` VARCHAR(1024) NOT NULL, |
|---|
| 267 | `groups` VARCHAR(1024) NOT NULL, |
|---|
| 268 | PRIMARY KEY (`id`), |
|---|
| 269 | INDEX `byOrder`(`order`) |
|---|
| 270 | )" |
|---|
| 271 | ); |
|---|
| 272 | |
|---|
| 273 | $tables_def = create_table_add_character_set($tables_def); |
|---|
| 274 | $this->tablef->setTables(array( |
|---|
| 275 | $this->tables['personalised_langs'], |
|---|
| 276 | $this->tables['blocks']) |
|---|
| 277 | ); |
|---|
| 278 | $result=$this->tablef->create($tables_def); |
|---|
| 279 | |
|---|
| 280 | $sql="INSERT INTO `".$this->tables['personalised_langs']."` |
|---|
| 281 | SELECT pap.id, pap.lang, pap.title, pap.content |
|---|
| 282 | FROM `".$this->tables['personalised']."` pap |
|---|
| 283 | WHERE pap.title!='' OR pap.content!='';"; |
|---|
| 284 | pwg_query($sql); |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | $sql="DELETE FROM `".$this->tables['personalised']."` WHERE lang!='".$user['lang']."';"; |
|---|
| 288 | pwg_query($sql); |
|---|
| 289 | |
|---|
| 290 | $sql="ALTER TABLE `".$this->tables['personalised']."` DROP COLUMN `lang`, |
|---|
| 291 | DROP COLUMN `title`, |
|---|
| 292 | DROP COLUMN `content`;"; |
|---|
| 293 | pwg_query($sql); |
|---|
| 294 | |
|---|
| 295 | $sql="ALTER TABLE `".$this->tables['urls']."` |
|---|
| 296 | ADD COLUMN `accessUsers` VARCHAR(1024) NOT NULL AFTER `visible`, |
|---|
| 297 | ADD COLUMN `accessGroups` VARCHAR(1024) NOT NULL AFTER `accessUsers`;"; |
|---|
| 298 | pwg_query($sql); |
|---|
| 299 | |
|---|
| 300 | /* |
|---|
| 301 | $users=new GPCUsers(); |
|---|
| 302 | $uList=array(); |
|---|
| 303 | foreach($users->getList() as $user) |
|---|
| 304 | { |
|---|
| 305 | $uList[]=$user['id']; |
|---|
| 306 | } |
|---|
| 307 | $sql="UPDATE `".$this->tables['urls']."` |
|---|
| 308 | SET accessUsers='".pwg_db_real_escape_string(implode(',', $uList))."';"; |
|---|
| 309 | pwg_query($sql); |
|---|
| 310 | */ |
|---|
| 311 | if(isset($this->config['amm_sections_items'])) |
|---|
| 312 | { |
|---|
| 313 | $this->config['amm_blocks_items']=$this->config['amm_sections_items']; |
|---|
| 314 | unset($this->config['amm_sections_items']); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | $usersList=array('guest', 'generic', 'normal', 'webmaster', 'admin'); |
|---|
| 319 | foreach($this->config['amm_blocks_items'] as $key => $item) |
|---|
| 320 | { |
|---|
| 321 | $tmp0=explode('/', $item['visibility']); |
|---|
| 322 | $this->config['amm_blocks_items'][$key]['visibility']=implode(',', array_diff($usersList, explode(',', $tmp0[0]))).'/'.$tmp0[1]; |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | /** |
|---|
| 327 | * report hidden menu from piwigo's config to AMM config |
|---|
| 328 | */ |
|---|
| 329 | private function backupMenuConfig($updateMenu=false) |
|---|
| 330 | { |
|---|
| 331 | global $conf; |
|---|
| 332 | |
|---|
| 333 | $this->config['amm_old_blk_menubar']=$conf['blk_menubar']; |
|---|
| 334 | pwg_query("UPDATE ".CONFIG_TABLE." SET value = '' WHERE param='blk_menubar';"); |
|---|
| 335 | |
|---|
| 336 | if($updateMenu and $conf['blk_menubar']!='') |
|---|
| 337 | { |
|---|
| 338 | $tmp=unserialize($conf['blk_menubar']); |
|---|
| 339 | foreach($tmp as $key => $val) |
|---|
| 340 | { |
|---|
| 341 | pwg_query("REPLACE INTO ".$this->tables['blocks']." VALUES ('$key', '".abs($val)."', '".($val<0?'guest,generic,normal,webmaster,admin':'')."', '');"); |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | /** |
|---|
| 347 | * restore piwigo's menu |
|---|
| 348 | */ |
|---|
| 349 | private function restoreMenuConfig() |
|---|
| 350 | { |
|---|
| 351 | if($this->config['amm_old_blk_menubar']!='') |
|---|
| 352 | pwg_query("UPDATE ".CONFIG_TABLE." SET value = '".pwg_db_real_escape_string($this->config['amm_old_blk_menubar'])."' WHERE param='blk_menubar';"); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | } //class |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | if(!function_exists('create_table_add_character_set') and !defined('IN_ADMIN')) |
|---|
| 361 | { |
|---|
| 362 | /** |
|---|
| 363 | * from admin/include/functions.php |
|---|
| 364 | * |
|---|
| 365 | * adds the caracter set to a create table sql query. |
|---|
| 366 | * all CREATE TABLE queries must call this function |
|---|
| 367 | * @param string query - the sql query |
|---|
| 368 | */ |
|---|
| 369 | function create_table_add_character_set($query) |
|---|
| 370 | { |
|---|
| 371 | defined('DB_CHARSET') or fatal_error('create_table_add_character_set DB_CHARSET undefined'); |
|---|
| 372 | if ('DB_CHARSET'!='') |
|---|
| 373 | { |
|---|
| 374 | if ( version_compare(mysql_get_server_info(), '4.1.0', '<') ) |
|---|
| 375 | { |
|---|
| 376 | return $query; |
|---|
| 377 | } |
|---|
| 378 | $charset_collate = " DEFAULT CHARACTER SET ".DB_CHARSET; |
|---|
| 379 | if (DB_COLLATE!='') |
|---|
| 380 | { |
|---|
| 381 | $charset_collate .= " COLLATE ".DB_COLLATE; |
|---|
| 382 | } |
|---|
| 383 | if ( is_array($query) ) |
|---|
| 384 | { |
|---|
| 385 | foreach( $query as $id=>$q) |
|---|
| 386 | { |
|---|
| 387 | $q=trim($q); |
|---|
| 388 | $q=trim($q, ';'); |
|---|
| 389 | if (preg_match('/^CREATE\s+TABLE/i',$q)) |
|---|
| 390 | { |
|---|
| 391 | $q.=$charset_collate; |
|---|
| 392 | } |
|---|
| 393 | $q .= ';'; |
|---|
| 394 | $query[$id] = $q; |
|---|
| 395 | } |
|---|
| 396 | } |
|---|
| 397 | else |
|---|
| 398 | { |
|---|
| 399 | $query=trim($query); |
|---|
| 400 | $query=trim($query, ';'); |
|---|
| 401 | if (preg_match('/^CREATE\s+TABLE/i',$query)) |
|---|
| 402 | { |
|---|
| 403 | $query.=$charset_collate; |
|---|
| 404 | } |
|---|
| 405 | $query .= ';'; |
|---|
| 406 | } |
|---|
| 407 | } |
|---|
| 408 | return $query; |
|---|
| 409 | } |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | ?> |
|---|