1 | <?php |
---|
2 | defined('PHPWG_ROOT_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | class UserCollections_maintain extends PluginMaintain |
---|
5 | { |
---|
6 | private $collections; |
---|
7 | private $collection_images; |
---|
8 | private $collection_shares; |
---|
9 | |
---|
10 | private $default_conf = array( |
---|
11 | 'allow_send_admin' => true, |
---|
12 | 'allow_mails' => true, |
---|
13 | 'allow_public' => true, |
---|
14 | ); |
---|
15 | |
---|
16 | function __construct($id) |
---|
17 | { |
---|
18 | global $prefixeTable; |
---|
19 | |
---|
20 | parent::__construct($id); |
---|
21 | $this->collections = $prefixeTable . 'collections'; |
---|
22 | $this->collection_images = $prefixeTable . 'collection_images'; |
---|
23 | $this->collection_shares = $prefixeTable . 'collection_shares'; |
---|
24 | } |
---|
25 | |
---|
26 | function install($plugin_version, &$errors=array()) |
---|
27 | { |
---|
28 | global $conf, $prefixeTable; |
---|
29 | |
---|
30 | if (empty($conf['user_collections'])) |
---|
31 | { |
---|
32 | conf_update_param('user_collections', $this->default_conf, true); |
---|
33 | } |
---|
34 | else |
---|
35 | { |
---|
36 | $old_conf = safe_unserialize($conf['user_collections']); |
---|
37 | |
---|
38 | if (!isset($old_conf['allow_send_admin'])) |
---|
39 | { |
---|
40 | $old_conf['allow_send_admin'] = true; |
---|
41 | conf_update_param('user_collections', $old_conf, true); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | // create tables |
---|
46 | $query = ' |
---|
47 | CREATE TABLE IF NOT EXISTS `' . $this->collections . '` ( |
---|
48 | `id` mediumint(8) NOT NULL AUTO_INCREMENT, |
---|
49 | `user_id` smallint(5) DEFAULT NULL, |
---|
50 | `name` varchar(255) NOT NULL, |
---|
51 | `date_creation` datetime NOT NULL, |
---|
52 | `comment` text NULL, |
---|
53 | `nb_images` mediumint(8) NOT NULL DEFAULT 0, |
---|
54 | PRIMARY KEY (`id`) |
---|
55 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 |
---|
56 | ;'; |
---|
57 | pwg_query($query); |
---|
58 | |
---|
59 | $query = ' |
---|
60 | CREATE TABLE IF NOT EXISTS `' . $this->collection_images . '` ( |
---|
61 | `col_id` mediumint(8) NOT NULL, |
---|
62 | `image_id` mediumint(8) NOT NULL, |
---|
63 | `add_date` datetime NULL, |
---|
64 | UNIQUE KEY `UNIQUE` (`col_id`,`image_id`) |
---|
65 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
---|
66 | ;'; |
---|
67 | pwg_query($query); |
---|
68 | |
---|
69 | $query = ' |
---|
70 | CREATE TABLE IF NOT EXISTS `' . $this->collection_shares . '` ( |
---|
71 | `id` mediumint(8) NOT NULL AUTO_INCREMENT, |
---|
72 | `col_id` mediumint(8) NOT NULL, |
---|
73 | `share_key` varchar(64) NOT NULL, |
---|
74 | `params` text NULL, |
---|
75 | `add_date` datetime NOT NULL, |
---|
76 | PRIMARY KEY (`id`), |
---|
77 | UNIQUE KEY `share_key` (`share_key`) |
---|
78 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 |
---|
79 | ;'; |
---|
80 | pwg_query($query); |
---|
81 | |
---|
82 | |
---|
83 | // version 2.0.0 |
---|
84 | $result = pwg_query('SHOW COLUMNS FROM `' . $this->collection_images . '` LIKE "add_date";'); |
---|
85 | if (!pwg_db_num_rows($result)) |
---|
86 | { |
---|
87 | pwg_query('ALTER TABLE `' . $this->collection_images . '` ADD `add_date` datetime NULL;'); |
---|
88 | } |
---|
89 | |
---|
90 | $result = pwg_query('SHOW COLUMNS FROM `' . $this->collections . '` LIKE "comment";'); |
---|
91 | if (!pwg_db_num_rows($result)) |
---|
92 | { |
---|
93 | pwg_query('ALTER TABLE `' . $this->collections . '` ADD `comment` text NULL;'); |
---|
94 | pwg_query('ALTER TABLE `' . $this->collections . '` DROP `active`;'); |
---|
95 | } |
---|
96 | |
---|
97 | // version 2.1.0 |
---|
98 | $result = pwg_query('SHOW COLUMNS FROM `' . $this->collections . '` LIKE "public";'); |
---|
99 | if (pwg_db_num_rows($result)) |
---|
100 | { |
---|
101 | $now = date('Y-m-d H:i:s'); |
---|
102 | |
---|
103 | $query = ' |
---|
104 | SELECT id, public_id |
---|
105 | FROM `' . $this->collections . '` |
---|
106 | WHERE public = 1 |
---|
107 | ;'; |
---|
108 | $result = pwg_query($query); |
---|
109 | |
---|
110 | $inserts = array(); |
---|
111 | while ($row = pwg_db_fetch_assoc($result)) |
---|
112 | { |
---|
113 | $inserts[] = array( |
---|
114 | 'col_id' => $row['id'], |
---|
115 | 'share_key' => $row['public_id'], |
---|
116 | 'params' => serialize(array('password'=>'','deadline'=>'')), |
---|
117 | 'add_date' => $now, |
---|
118 | ); |
---|
119 | } |
---|
120 | |
---|
121 | mass_inserts($this->collection_shares, |
---|
122 | array('col_id','share_key','params','add_date'), |
---|
123 | $inserts |
---|
124 | ); |
---|
125 | |
---|
126 | pwg_query('ALTER TABLE `' . $this->collections . '` DROP `public`, DROP `public_id`;'); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | function update($old_version, $new_version, &$errors=array()) |
---|
131 | { |
---|
132 | $this->install($new_version, $errors); |
---|
133 | } |
---|
134 | |
---|
135 | function uninstall() |
---|
136 | { |
---|
137 | conf_delete_param('user_collections'); |
---|
138 | |
---|
139 | pwg_query('DROP TABLE IF EXISTS `' . $this->collections . '`;'); |
---|
140 | pwg_query('DROP TABLE IF EXISTS `' . $this->collection_images . '`;'); |
---|
141 | pwg_query('DROP TABLE IF EXISTS `' . $this->collection_shares . '`;'); |
---|
142 | } |
---|
143 | } |
---|