@@ -39,243 +39,16 @@ public function onInstall() {
3939 global $ conf ;
4040
4141 return true ;
42-
4342 }
4443
45-
4644 /*
4745 This function is called when the plugin is loaded
4846 */
4947
5048 public function onLoad () {
5149 global $ app ;
52-
53- /*
54- Register for the events
55- */
56-
57- $ app ->plugins ->registerEvent ('software_update_inst_insert ' , $ this ->plugin_name , 'process ' );
58- //$app->plugins->registerEvent('software_update_inst_update',$this->plugin_name,'process');
59- //$app->plugins->registerEvent('software_update_inst_delete',$this->plugin_name,'process');
60-
6150 //* Register for actions
6251 $ app ->plugins ->registerAction ('os_update ' , $ this ->plugin_name , 'os_update ' );
63-
64-
65- }
66-
67- private function set_install_status ($ inst_id , $ status ) {
68- global $ app ;
69-
70- $ app ->db ->query ("UPDATE software_update_inst SET status = ? WHERE software_update_inst_id = ? " , $ status , $ inst_id );
71- $ app ->dbmaster ->query ("UPDATE software_update_inst SET status = ? WHERE software_update_inst_id = ? " , $ status , $ inst_id );
72- }
73-
74- public function process ($ event_name , $ data ) {
75- global $ app , $ conf ;
76-
77- //* Get the info of the package:
78- $ software_update_id = intval ($ data ["new " ]["software_update_id " ]);
79- $ software_update = $ app ->db ->queryOneRecord ("SELECT * FROM software_update WHERE software_update_id = ? " , $ software_update_id );
80- $ software_package = $ app ->db ->queryOneRecord ("SELECT * FROM software_package WHERE package_name = ? " , $ software_update ['package_name ' ]);
81-
82- if ($ software_package ['package_type ' ] == 'ispconfig ' && !$ conf ['software_updates_enabled ' ] == true ) {
83- $ app ->log ('Software Updates not enabled on this server. To enable updates, set $conf["software_updates_enabled"] = true; in config.inc.php ' , LOGLEVEL_WARN );
84- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "failed " );
85- return false ;
86- }
87-
88- $ installuser = '' ;
89- if ($ software_package ['package_type ' ] == 'ispconfig ' ) {
90- $ installuser = '' ;
91- } elseif ($ software_package ['package_type ' ] == 'app ' ) {
92- $ installuser = 'ispapps ' ;
93- } else {
94- $ app ->log ('package_type not supported ' , LOGLEVEL_WARN );
95- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "failed " );
96- return false ;
97- }
98-
99- $ temp_dir = '/tmp/ ' .md5 (uniqid (rand ()));
100- $ app ->log ("The temp dir is $ temp_dir " , LOGLEVEL_DEBUG );
101- mkdir ($ temp_dir );
102- if ($ installuser != '' ) chown ($ temp_dir , $ installuser );
103-
104- if (!is_dir ($ temp_dir )) {
105- $ app ->log ("Unable to create temp directory. " , LOGLEVEL_WARN );
106- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "failed " );
107- return false ;
108- }
109-
110- //* Replace placeholders in download URL
111- $ software_update ["update_url " ] = str_replace ('{key} ' , $ software_package ['package_key ' ], $ software_update ["update_url " ]);
112-
113- //* Download the update package
114- if ($ installuser == '' ) {
115- $ cmd = "cd ? && wget ? " ;
116- $ app ->system ->exec_safe ($ cmd , $ temp_dir , $ software_update ["update_url " ]);
117- } else {
118- $ cmd = "cd $ temp_dir && wget " .$ software_update ["update_url " ];
119- $ app ->system ->exec_safe ("su -c ? ? " , $ cmd , $ installuser );
120- }
121- $ app ->log ("Downloading the update file from: " .$ software_update ["update_url " ], LOGLEVEL_DEBUG );
122-
123- //$url_parts = parse_url($software_update["update_url"]);
124- //$update_filename = basename($url_parts["path"]);
125- //* Find the name of the zip file which contains the app.
126- $ tmp_dir_handle = dir ($ temp_dir );
127- $ update_filename = '' ;
128- while (false !== ($ t = $ tmp_dir_handle ->read ())) {
129- if ($ t != '. ' && $ t != '.. ' && is_file ($ temp_dir .'/ ' .$ t ) && substr ($ t , -4 ) == '.zip ' ) {
130- $ update_filename = $ t ;
131- }
132- }
133- $ tmp_dir_handle ->close ();
134- unset($ tmp_dir_handle );
135- unset($ t );
136-
137- if ($ update_filename == '' ) {
138- $ app ->log ("No package file found. Download failed? Installation aborted. " , LOGLEVEL_WARN );
139- $ app ->system ->exec_safe ("rm -rf ? " , $ temp_dir );
140- $ app ->log ("Deleting the temp directory $ temp_dir " , LOGLEVEL_DEBUG );
141- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "failed " );
142- return false ;
143- }
144-
145- $ app ->log ("The update filename is $ update_filename " , LOGLEVEL_DEBUG );
146-
147- if (is_file ($ temp_dir .'/ ' .$ update_filename )) {
148-
149- //* Checking the md5sum
150- if (md5_file ($ temp_dir .'/ ' .$ update_filename ) != $ software_update ["update_md5 " ]) {
151- $ app ->log ("The md5 sum of the downloaded file is incorrect. Update aborted. " , LOGLEVEL_WARN );
152- $ app ->system ->exec_safe ("rm -rf " , $ temp_dir );
153- $ app ->log ("Deleting the temp directory $ temp_dir " , LOGLEVEL_DEBUG );
154- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "failed " );
155- return false ;
156- } else {
157- $ app ->log ("MD5 checksum of the downloaded file verified. " , LOGLEVEL_DEBUG );
158- }
159-
160-
161- //* unpacking the update
162-
163- if ($ installuser == '' ) {
164- $ cmd = "cd ? && unzip ? " ;
165- $ app ->system ->exec_safe ($ cmd , $ temp_dir , $ update_filename );
166- } else {
167- $ cmd = "cd $ temp_dir && unzip $ update_filename " ;
168- $ app ->system ->exec_safe ("su -c ? ? " , $ cmd , $ installuser );
169- }
170-
171- //* Create a database, if the package requires one
172- if ($ software_package ['package_type ' ] == 'app ' && $ software_package ['package_requires_db ' ] == 'mysql ' ) {
173-
174- $ app ->uses ('ini_parser ' );
175- $ package_config = $ app ->ini_parser ->parse_ini_string (stripslashes ($ software_package ['package_config ' ]));
176-
177- $ this ->create_app_db ($ package_config ['mysql ' ]);
178- $ app ->log ("Creating the app DB. " , LOGLEVEL_DEBUG );
179-
180- //* Load the sql dump into the database
181- if (is_file ($ temp_dir .'/setup.sql ' )) {
182- $ db_config = $ package_config ['mysql ' ];
183- if ( $ db_config ['database_user ' ] != '' &&
184- $ db_config ['database_password ' ] != '' &&
185- $ db_config ['database_name ' ] != '' &&
186- $ db_config ['database_host ' ] != '' ) {
187- $ app ->system ->exec_safe ("mysql --default-character-set=utf8 --force -h ? -u ? ? < ? " , $ db_config ['database_host ' ], $ db_config ['database_user ' ], $ db_config ['database_name ' ], $ temp_dir .'/setup.sql ' );
188- $ app ->log ("Loading setup.sql dump into the app db. " , LOGLEVEL_DEBUG );
189- }
190- }
191-
192- }
193-
194- //* Save the package config file as app.ini
195- if ($ software_package ['package_config ' ] != '' ) {
196- file_put_contents ($ temp_dir .'/app.ini ' , $ software_package ['package_config ' ]);
197- $ app ->log ("Writing " .$ temp_dir .'/app.ini ' , LOGLEVEL_DEBUG );
198- }
199-
200- if (is_file ($ temp_dir .'/setup.sh ' )) {
201- // Execute the setup script
202- $ app ->system ->exec_safe ('chmod +x ? ' , $ temp_dir .'/setup.sh ' );
203- $ app ->log ("Executing setup.sh file in directory $ temp_dir " , LOGLEVEL_DEBUG );
204-
205- if ($ installuser == '' ) {
206- $ cmd = 'cd ? && ./setup.sh > package_install.log ' ;
207- $ app ->system ->exec_safe ($ cmd , $ temp_dir );
208- } else {
209- $ cmd = 'cd ' .$ temp_dir .' && ./setup.sh > package_install.log ' ;
210- $ app ->system ->exec_safe ("su -c ? ? " , $ cmd , $ installuser );
211- }
212-
213- $ log_data = @file_get_contents ("{$ temp_dir }/package_install.log " );
214- if (preg_match ("'.*\[OK\]\s*$'is " , $ log_data )) {
215- $ app ->log ("Installation successful " , LOGLEVEL_DEBUG );
216- $ app ->log ($ log_data , LOGLEVEL_DEBUG );
217- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "installed " );
218- } else {
219- $ app ->log ("Installation failed: \n\n" . $ log_data , LOGLEVEL_WARN );
220- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "failed " );
221- }
222- } else {
223- $ app ->log ("setup.sh file not found " , LOGLEVEL_ERROR );
224- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "failed " );
225- }
226- } else {
227- $ app ->log ("Download of the update file failed " , LOGLEVEL_WARN );
228- $ this ->set_install_status ($ data ["new " ]["software_update_inst_id " ], "failed " );
229- }
230-
231- if ($ temp_dir != '' && $ temp_dir != '/ ' ) $ app ->system ->exec_safe ("rm -rf ? " , $ temp_dir );
232- $ app ->log ("Deleting the temp directory $ temp_dir " , LOGLEVEL_DEBUG );
233- }
234-
235- private function create_app_db ($ db_config ) {
236- global $ app , $ conf ;
237-
238- if ( $ db_config ['database_user ' ] != '' &&
239- $ db_config ['database_password ' ] != '' &&
240- $ db_config ['database_name ' ] != '' &&
241- $ db_config ['database_host ' ] != '' ) {
242-
243- if (!include ISPC_LIB_PATH .'/mysql_clientdb.conf ' ) {
244- $ app ->log ('Unable to open ' .ISPC_LIB_PATH .'/mysql_clientdb.conf ' , LOGLEVEL_ERROR );
245- return ;
246- }
247-
248- if ($ db_config ['database_user ' ] == 'root ' ) {
249- $ app ->log ('User root not allowed for App databases ' , LOGLEVEL_WARNING );
250- return ;
251- }
252-
253- //* Connect to the database
254- $ link = mysqli_connect ($ clientdb_host , $ clientdb_user , $ clientdb_password );
255- if (!$ link ) {
256- $ app ->log ('Unable to connect to the database ' .mysqli_connect_error (), LOGLEVEL_ERROR );
257- return ;
258- }
259-
260- $ query_charset_table = '' ;
261-
262- //* Create the new database
263- if (mysqli_query ($ link ,'CREATE DATABASE ' .mysqli_real_escape_string ($ link , $ db_config ['database_name ' ]).$ query_charset_table , $ link )) {
264- $ app ->log ('Created MySQL database: ' .$ db_config ['database_name ' ], LOGLEVEL_DEBUG );
265- } else {
266- $ app ->log ('Unable to connect to the database ' .mysqli_error ($ link ), LOGLEVEL_ERROR );
267- }
268-
269- if (mysqli_query ("GRANT ALL ON " .mysqli_real_escape_string ($ link , $ db_config ['database_name ' ]).".* TO ' " .mysqli_real_escape_string ($ link , $ db_config ['database_user ' ])."'@' " .$ db_config ['database_host ' ]."' IDENTIFIED BY ' " .mysqli_real_escape_string ($ link , $ db_config ['database_password ' ])."'; " , $ link )) {
270- $ app ->log ('Created MySQL user: ' .$ db_config ['database_user ' ], LOGLEVEL_DEBUG );
271- } else {
272- $ app ->log ('Unable to create database user ' .$ db_config ['database_user ' ].' ' .mysqli_error ($ link ), LOGLEVEL_ERROR );
273- }
274-
275- mysqli_close ($ link );
276-
277- }
278-
27952 }
28053
28154 //* Operating system update
0 commit comments