1 | <?php |
---|
2 | defined('PHPWG_ROOT_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | class oAuth_maintain extends PluginMaintain |
---|
5 | { |
---|
6 | private $installed = false; |
---|
7 | |
---|
8 | private $default_conf = array( |
---|
9 | 'display_menubar' => true, |
---|
10 | 'display_register' => true, |
---|
11 | 'identification_icon' => '38px', |
---|
12 | 'menubar_icon' => '26px', |
---|
13 | 'allow_merge_accounts' => true, |
---|
14 | ); |
---|
15 | |
---|
16 | private $file; |
---|
17 | |
---|
18 | function __construct($plugin_id) |
---|
19 | { |
---|
20 | parent::__construct($plugin_id); |
---|
21 | |
---|
22 | $this->file = PWG_LOCAL_DIR . 'config/hybridauth.inc.php'; |
---|
23 | } |
---|
24 | |
---|
25 | function install($plugin_version, &$errors=array()) |
---|
26 | { |
---|
27 | global $conf; |
---|
28 | |
---|
29 | if (empty($conf['oauth'])) |
---|
30 | { |
---|
31 | $conf['oauth'] = serialize($this->default_conf); |
---|
32 | conf_update_param('oauth', $conf['oauth']); |
---|
33 | } |
---|
34 | else |
---|
35 | { |
---|
36 | $old_conf = is_string($conf['oauth']) ? unserialize($conf['oauth']) : $conf['oauth']; |
---|
37 | |
---|
38 | if (!isset($old_conf['allow_merge_accounts'])) |
---|
39 | { |
---|
40 | $old_conf['allow_merge_accounts'] = true; |
---|
41 | |
---|
42 | $conf['oauth'] = serialize($old_conf); |
---|
43 | conf_update_param('oauth', $conf['oauth']); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | $result = pwg_query('SHOW COLUMNS FROM `' . USER_INFOS_TABLE . '` LIKE "oauth_id";'); |
---|
48 | if (!pwg_db_num_rows($result)) |
---|
49 | { |
---|
50 | pwg_query('ALTER TABLE `' . USER_INFOS_TABLE . '` ADD `oauth_id` VARCHAR(255) DEFAULT NULL;'); |
---|
51 | } |
---|
52 | |
---|
53 | // move field from users table to user_infos |
---|
54 | $result = pwg_query('SHOW COLUMNS FROM `' . USERS_TABLE . '` LIKE "oauth_id";'); |
---|
55 | if (pwg_db_num_rows($result)) |
---|
56 | { |
---|
57 | $query = ' |
---|
58 | UPDATE `' . USER_INFOS_TABLE . '` AS i |
---|
59 | SET oauth_id = ( |
---|
60 | SELECT oauth_id |
---|
61 | FROM `' . USERS_TABLE . '` AS u |
---|
62 | WHERE u.'.$conf['user_fields']['id'].' = i.user_id |
---|
63 | ) |
---|
64 | ;'; |
---|
65 | pwg_query($query); |
---|
66 | |
---|
67 | pwg_query('ALTER TABLE `' . USERS_TABLE . '` DROP `oauth_id`;'); |
---|
68 | } |
---|
69 | |
---|
70 | // add 'total' and 'enabled' fields in hybridauth conf file |
---|
71 | if (file_exists($this->file)) |
---|
72 | { |
---|
73 | $hybridauth_conf = include($this->file); |
---|
74 | if (!isset($hybridauth_conf['total'])) |
---|
75 | { |
---|
76 | $enabled = array_filter($hybridauth_conf['providers'], create_function('$p', 'return $p["enabled"];')); |
---|
77 | |
---|
78 | $hybridauth_conf['total'] = count($hybridauth_conf['providers']); |
---|
79 | $hybridauth_conf['enabled'] = count($enabled); |
---|
80 | |
---|
81 | $content = "<?php\ndefined('PHPWG_ROOT_PATH') or die('Hacking attempt!');\n\nreturn "; |
---|
82 | $content.= var_export($hybridauth_conf, true); |
---|
83 | $content.= ";\n?>"; |
---|
84 | |
---|
85 | file_put_contents($this->file, $content); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | $this->installed = true; |
---|
90 | } |
---|
91 | |
---|
92 | function activate($plugin_version, &$errors=array()) |
---|
93 | { |
---|
94 | if (!$this->installed) |
---|
95 | { |
---|
96 | $this->install($plugin_version, $errors); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | function deactivate() |
---|
101 | { |
---|
102 | } |
---|
103 | |
---|
104 | function uninstall() |
---|
105 | { |
---|
106 | conf_delete_param('oauth'); |
---|
107 | |
---|
108 | pwg_query('ALTER TABLE `'. USER_INFOS_TABLE .'` DROP `oauth_id`;'); |
---|
109 | |
---|
110 | @unlink($this->file); |
---|
111 | } |
---|
112 | } |
---|