$email], ['id_user' => $config['id_user']] ); } // Update the alert action Mail to XXX/Administrator // if it is set to default. $mail_check = 'yourmail@domain.es'; $mail_alert = alerts_get_alert_action_field1(1); if ($mail_check === $mail_alert && $email !== false) { alerts_update_alert_action( 1, [ 'field1' => $email, 'field1_recovery' => $email, ] ); } config_update_value('initial_wizard', 1); } /** * Generates base code to print main configuration modal. * * Asks for timezone, mail. * * @param boolean $return Print output or not. * @param boolean $launch Process JS modal. * @param string $callback Call to JS function at end. * * @return string HTML. */ function config_wiz_modal( $return=false, $launch=true, $callback=false ) { global $config; $email = db_get_value('email', 'tusuario', 'id_user', $config['id_user']); // Avoid to show default email. if ($email == 'admin@example.com') { $email = ''; } $output = ''; // Prints first step pandora registration. $output .= ''; $output .= ''; // Verification modal. $output .= ''; ob_start(); ?> true, 'message' => '', ]; // Pandora register update. $um_message = update_manager_register_instance(); $ui_feedback['message'] .= $um_message['message'].'

'; $ui_feedback['status'] = $um_message['success'] && $ui_feedback['status']; if ($ui_feedback['status']) { // Store next identification reminder. config_update_value( 'identification_reminder_timestamp', $next_check ); } return $ui_feedback; } /** * Shows a modal to register current console in UpdateManager. * * @param boolean $return Return or show html. * @param boolean $launch Execute wizard. * @param string $callback Call function when done. * * @return string HTML code. */ function registration_wiz_modal( $return=false, $launch=true, $callback=false ) { global $config; $output = ''; $product_name = get_product_name(); $output .= ''; // Verification modal. $output .= ''; // Results modal. $output .= ''; ob_start(); ?> 1], ['id_user' => $config['id_user']] ); $ui_feedback['status'] = $um_message['success']; } else { $ui_feedback['status'] = false; } return $ui_feedback; } /** * Show a modal allowing the user register into newsletter. * * @param boolean $return Print content o return it. * @param boolean $launch Launch directly on load or not. * @param string $callback Call function when done. * * @return string HTML code. */ function newsletter_wiz_modal( $return=false, $launch=true, $callback=false ) { global $config; $output = ''; $product_name = get_product_name(); $email = db_get_value( 'email', 'tusuario', 'id_user', $config['id_user'] ); // Avoid to show default email. if ($email == 'admin@example.com') { $email = ''; } $output .= ''; // Verification modal. $output .= ''; // Results modal. $output .= ''; ob_start(); ?> $license, 'current_update' => update_manager_get_current_package(), 'limit_count' => $limit_count, 'build' => $build_version, 'version' => $pandora_version, 'puid' => $config['pandora_uid'], ]; } /** * Function to remove dir and files inside. * * @param string $dir Path to dir. * * @return void */ function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != '.' && $object != '..') { if (filetype($dir.'/'.$object) == 'dir') { rrmdir($dir.'/'.$object); } else { unlink($dir.'/'.$object); } } } reset($objects); rmdir($dir); } else { unlink($dir); } } /** * Install updates step2. * * @return void */ function update_manager_install_package_step2() { global $config; ob_clean(); $package = (string) get_parameter('package'); $package = trim($package); $version = (string) get_parameter('version', 0); $path = sys_get_temp_dir().'/pandora_oum/'.$package; // All files extracted. $files_total = $path.'/files.txt'; // Files copied. $files_copied = $path.'/files.copied.txt'; $return = []; if (file_exists($files_copied)) { unlink($files_copied); } if (file_exists($path)) { $files_h = fopen($files_total, 'r'); if ($files_h === false) { $return['status'] = 'error'; $return['message'] = __('An error ocurred while reading a file.'); echo json_encode($return); return; } while ($line = stream_get_line($files_h, 65535, "\n")) { $line = trim($line); // Tries to move the old file to the directory backup // inside the extracted package. if (file_exists($config['homedir'].'/'.$line)) { rename($config['homedir'].'/'.$line, $path.'/backup/'.$line); } // Tries to move the new file to the Integria directory. $dirname = dirname($line); if (!file_exists($config['homedir'].'/'.$dirname)) { $dir_array = explode('/', $dirname); $temp_dir = ''; foreach ($dir_array as $dir) { $temp_dir .= '/'.$dir; if (!file_exists($config['homedir'].$temp_dir)) { mkdir($config['homedir'].$temp_dir); } } } if (is_dir($path.'/'.$line)) { if (!file_exists($config['homedir'].'/'.$line)) { mkdir($config['homedir'].'/'.$line); file_put_contents($files_copied, $line."\n", (FILE_APPEND | LOCK_EX)); } } else { // Copy the new file. if (rename($path.'/'.$line, $config['homedir'].'/'.$line)) { // Append the moved file to the copied files txt. if (!file_put_contents($files_copied, $line."\n", (FILE_APPEND | LOCK_EX))) { // If the copy process fail, this code tries to // restore the files backed up before. $files_copied_h = fopen($files_copied, 'r'); if ($files_copied_h === false) { $backup_status = __('Some of your old files might not be recovered.'); } else { while ($line_c = stream_get_line($files_copied_h, 65535, "\n")) { $line_c = trim($line_c); if (!rename($path.'/backup/'.$line, $config['homedir'].'/'.$line_c)) { $backup_status = __('Some of your files might not be recovered.'); } } if (!rename($path.'/backup/'.$line, $config['homedir'].'/'.$line)) { $backup_status = __('Some of your files might not be recovered.'); } fclose($files_copied_h); } fclose($files_h); $return['status'] = 'error'; $return['message'] = __( 'Line "%s" not copied to the progress file.', $line ).' '.$backup_status; echo json_encode($return); return; } } else { // If the copy process fail, this code tries to restore // the files backed up before. $files_copied_h = fopen($files_copied, 'r'); if ($files_copied_h === false) { $backup_status = __('Some of your files might not be recovered.'); } else { while ($line_c = stream_get_line($files_copied_h, 65535, "\n")) { $line_c = trim($line_c); if (!rename( $path.'/backup/'.$line, $config['homedir'].'/'.$line ) ) { $backup_status = __('Some of your old files might not be recovered.'); } } fclose($files_copied_h); } fclose($files_h); $return['status'] = 'error'; $return['message'] = __( 'Line "%s" not copied to the progress file.', $line ).' '.$backup_status; echo json_encode($return); return; } } } fclose($files_h); } else { $return['status'] = 'error'; $return['message'] = __('The package does not exist'); echo json_encode($return); return; } update_manager_enterprise_set_version($version); $product_name = get_product_name(); // Generate audit entry. db_pandora_audit( 'Update '.$product_name, 'Update version: '.$version.' of '.$product_name.' by '.$config['id_user'] ); $return['status'] = 'success'; $return['message'] = __('The package is installed.'); echo json_encode($return); } /** * Launch update manager client. * * @return void */ function update_manager_main() { global $config; ?> 'newest_package', 'license' => $um_config_values['license'], 'limit_count' => $um_config_values['limit_count'], 'current_package' => $um_config_values['current_update'], 'version' => $um_config_values['version'], 'build' => $um_config_values['build'], 'puid' => $um_config_values['puid'], ]; $curlObj = curl_init(); curl_setopt($curlObj, CURLOPT_URL, get_um_url().'server.php'); curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlObj, CURLOPT_POST, true); curl_setopt($curlObj, CURLOPT_POSTFIELDS, $params); curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlObj, CURLOPT_CONNECTTIMEOUT, 4); if (isset($config['update_manager_proxy_server'])) { curl_setopt($curlObj, CURLOPT_PROXY, $config['update_manager_proxy_server']); } if (isset($config['update_manager_proxy_port'])) { curl_setopt($curlObj, CURLOPT_PROXYPORT, $config['update_manager_proxy_port']); } if (isset($config['update_manager_proxy_user'])) { curl_setopt($curlObj, CURLOPT_PROXYUSERPWD, $config['update_manager_proxy_user'].':'.$config['update_manager_proxy_password']); } $result = curl_exec($curlObj); $http_status = curl_getinfo($curlObj, CURLINFO_HTTP_CODE); curl_close($curlObj); if ($result === false) { return false; } else if ($http_status >= 400 && $http_status < 500) { return false; } else if ($http_status >= 500) { return false; } else { $result = json_decode($result, true); if (empty($result)) { return false; } else { return true; } } } /** * Update process, online packages. * * @param boolean $is_ajax Is ajax call o direct call. * * @return string HTML update message. */ function update_manager_check_online_free_packages($is_ajax=true) { global $config; $update_message = ''; $um_config_values = update_manager_get_config_values(); $params = [ 'action' => 'newest_package', 'license' => $um_config_values['license'], 'limit_count' => $um_config_values['limit_count'], 'current_package' => $um_config_values['current_update'], 'version' => $um_config_values['version'], 'build' => $um_config_values['build'], 'puid' => $um_config_values['puid'], ]; /* * To test using shell execute: * wget https://artica.es/pandoraupdate7/server.php -O- \ * --no-check-certificate --post-data \ * "action=newest_package&license=PANDORA_FREE&limit_count=1¤t_package=1&version=v5.1RC1&build=PC140625" */ $curlObj = curl_init(); curl_setopt($curlObj, CURLOPT_URL, get_um_url().'server.php'); curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlObj, CURLOPT_POST, true); curl_setopt($curlObj, CURLOPT_POSTFIELDS, $params); curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false); if (isset($config['update_manager_proxy_server'])) { curl_setopt( $curlObj, CURLOPT_PROXY, $config['update_manager_proxy_server'] ); } if (isset($config['update_manager_proxy_port'])) { curl_setopt( $curlObj, CURLOPT_PROXYPORT, $config['update_manager_proxy_port'] ); } if (isset($config['update_manager_proxy_user'])) { curl_setopt( $curlObj, CURLOPT_PROXYUSERPWD, $config['update_manager_proxy_user'].':'.$config['update_manager_proxy_password'] ); } $result = curl_exec($curlObj); $http_status = curl_getinfo($curlObj, CURLINFO_HTTP_CODE); curl_close($curlObj); if ($result === false) { if ($is_ajax) { echo __('Could not connect to internet'); } else { $update_message = __('Could not connect to internet'); } } else if ($http_status >= 400 && $http_status < 500) { if ($is_ajax) { echo __('Server not found.'); } else { $update_message = __('Server not found.'); } } else if ($http_status >= 500) { if ($is_ajax) { echo $result; } else { $update_message = $result; } } else { if ($is_ajax) { $result = json_decode($result, true); if (!empty($result)) { ?> There is a new version: '.$result[0]['version'].'

'; echo "".__('Update').''; } else { echo __('There is no update available.'); } return $update_message; } else { if (!empty($result)) { $result = json_decode($result, true); $update_message = 'There is a new version: '.$result[0]['version']; } return $update_message; } } } /** * Executes an action against UpdateManager. * * @param string $action Action to perform. * @param boolean $additional_params Extra parameters (optional). * * @return array With UM response. */ function update_manager_curl_request($action, $additional_params=false) { global $config; $error_array = [ 'success' => true, 'update_message' => '', ]; $update_message = ''; $um_config_values = update_manager_get_config_values(); $params = [ 'license' => $um_config_values['license'], 'limit_count' => $um_config_values['limit_count'], 'current_package' => $um_config_values['current_update'], 'version' => $um_config_values['version'], 'build' => $um_config_values['build'], 'puid' => $um_config_values['puid'], ]; if ($additional_params !== false) { $params = array_merge($params, $additional_params); } $params['action'] = $action; $curlObj = curl_init(); curl_setopt($curlObj, CURLOPT_URL, get_um_url().'server.php'); curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlObj, CURLOPT_POST, true); curl_setopt($curlObj, CURLOPT_POSTFIELDS, $params); curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false); if (isset($config['update_manager_proxy_server'])) { curl_setopt($curlObj, CURLOPT_PROXY, $config['update_manager_proxy_server']); } if (isset($config['update_manager_proxy_port'])) { curl_setopt($curlObj, CURLOPT_PROXYPORT, $config['update_manager_proxy_port']); } if (isset($config['update_manager_proxy_user'])) { curl_setopt($curlObj, CURLOPT_PROXYUSERPWD, $config['update_manager_proxy_user'].':'.$config['update_manager_proxy_password']); } $result = curl_exec($curlObj); $http_status = curl_getinfo($curlObj, CURLINFO_HTTP_CODE); curl_close($curlObj); $error_array['http_status'] = $http_status; if ($result === false) { $error_array['success'] = false; if ($is_ajax) { echo __('Could not connect to internet'); return $error_array; } else { $error_array['update_message'] = __('Could not connect to internet'); return $error_array; } } else if ($http_status >= 400 && $http_status < 500) { $error_array['success'] = false; if ($is_ajax) { echo __('Server not found.'); return $error_array; } else { $error_array['update_message'] = __('Server not found.'); return $error_array; } } else if ($http_status >= 500) { $error_array['success'] = false; if ($is_ajax) { echo $result; return $error_array; } else { $error_array['update_message'] = $result; return $error_array; } } $error_array['update_message'] = $result; return $error_array; } /** * Subscribes an email account into newsletter. * * @param string $email E-mail. * * @return array With success [true/false], message [string]. */ function update_manager_insert_newsletter($email) { global $config; if ($email === '') { return false; } $params = [ 'email' => $email, 'language' => $config['language'], 'license' => db_get_value_filter( 'value', 'tupdate_settings', ['key' => 'customer_key'] ), ]; $result = update_manager_curl_request('new_newsletter', $params); if (!$result['success']) { return [ 'success' => false, 'message' => __('Remote server error on newsletter request'), ]; } switch ($result['http_status']) { case 200: $message = json_decode($result['update_message'], true); if ($message['success'] == 1) { return [ 'success' => true, 'message' => __('E-mail successfully subscribed to newsletter.'), ]; } else { return [ 'success' => true, 'message' => __('E-mail has already subscribed to newsletter.'), ]; } default: return [ 'success' => false, 'message' => __('Update manager returns error code: ').$result['http_status'].'.', ]; } } /** * Registers this console into UpdateManager. * * @return array With success [true/false], message [string]. */ function update_manager_register_instance() { global $config; $email = db_get_value('email', 'tusuario', 'id_user', $config['id_user']); $um_config_values = update_manager_get_config_values(); $params = [ 'action' => 'newest_package', 'license' => $um_config_values['license'], 'limit_count' => $um_config_values['limit_count'], 'current_package' => $um_config_values['current_update'], 'version' => $um_config_values['version'], 'build' => $um_config_values['build'], 'puid' => $um_config_values['puid'], 'email' => $email, 'language' => $config['language'], 'timezone' => $config['timezone'], ]; $result = update_manager_curl_request('new_register', $params); if (!$result['success']) { return [ 'success' => false, 'message' => __('Error while registering console.').'
'.$result['update_message'], ]; } switch ($result['http_status']) { case 200: // Retrieve the PUID. $message = json_decode($result['update_message'], true); if ($message['success'] == 1) { $puid = $message['pandora_uid']; config_update_value('pandora_uid', $puid); // The tupdate table is reused to display messages. // A specific entry to tupdate_package is required. // Then, this tupdate_package id is saved in tconfig. db_process_sql_insert( 'tupdate_package', ['description' => '__UMMESSAGES__'] ); $id_um_package_messages = db_get_value( 'id', 'tupdate_package', 'description', '__UMMESSAGES__' ); config_update_value( 'id_um_package_messages', $id_um_package_messages ); return [ 'success' => true, 'message' => __('Pandora successfully subscribed with UID: ').$puid.'.', ]; } else { return [ 'success' => false, 'message' => __('Unsuccessful subscription.'), ]; } default: return [ 'success' => false, 'message' => __('Update manager returns error code: ').$result['http_status'].'.', ]; } } /** * Extracts OUM package. * * @return boolean Success or not. */ function update_manager_extract_package() { global $config; $path_package = $config['attachment_store'].'/downloads/last_package.tgz'; ob_start(); if (!defined('PHP_VERSION_ID')) { $version = explode('.', PHP_VERSION); define( 'PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]) ); } $extracted = false; // Phar and exception working fine in 5.5.0 or higher. if (PHP_VERSION_ID >= 50505) { $phar = new PharData($path_package); try { $result = $phar->extractTo( $config['attachment_store'].'/downloads/', null, true ); $extracted = true; } catch (Exception $e) { echo ' There\'s a problem ... -> '.$e->getMessage(); $extracted = false; } } $return = true; if ($extracted === false) { // Phar extraction failed. Fallback to OS extraction. $return = false; if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') { // Unsupported OS. echo 'This OS ['.PHP_OS.'] does not support direct extraction of tgz files. Upgrade PHP version to be > 5.5.0'; } else { $return = true; system('tar xzf "'.$path_package.'" -C '.$config['attachment_store'].'/downloads/'); } } ob_end_clean(); rrmdir($path_package); if ($result == 0) { db_process_sql_update( 'tconfig', [ 'value' => json_encode( [ 'status' => 'fail', 'message' => __('Failed extracting the package to temp directory.'), ] ), ], ['token' => 'progress_update_status'] ); return false; } db_process_sql_update( 'tconfig', ['value' => 50], ['token' => 'progress_update'] ); return $return; } /** * The update copy entire tgz or fail (leaving some parts copied * and others not). * This does not make changes on DB. * * @return boolean Success or not. */ function update_manager_starting_update() { global $config; $full_path = $config['attachment_store'].'/downloads'; $homedir = $config['homedir']; $result = update_manager_recurse_copy( $full_path, $homedir, ['install.php'] ); rrmdir($full_path.'/pandora_console'); if (!$result) { db_process_sql_update( 'tconfig', [ 'value' => json_encode( [ 'status' => 'fail', 'message' => __('Failed the copying of the files.'), ] ), ], ['token' => 'progress_update_status'] ); return false; } else { db_process_sql_update( 'tconfig', ['value' => 100], ['token' => 'progress_update'] ); db_process_sql_update( 'tconfig', [ 'value' => json_encode( [ 'status' => 'end', 'message' => __('Package extracted successfully.'), ] ), ], ['token' => 'progress_update_status'] ); return true; } } /** * Copies recursively extracted package updates to target path. * * @param string $src Path. * @param string $dst Path. * @param string $black_list Path. * * @return boolean Success or not. */ function update_manager_recurse_copy($src, $dst, $black_list) { $dir = opendir($src); @mkdir($dst); @trigger_error('NONE'); while (false !== ( $file = readdir($dir))) { if (( $file != '.' ) && ( $file != '..' ) && (!in_array($file, $black_list))) { if (is_dir($src.'/'.$file)) { if (!update_manager_recurse_copy( $src.'/'.$file, $dst.'/'.$file, $black_list ) ) { return false; } } else { $result = copy($src.'/'.$file, $dst.'/'.$file); $error = error_get_last(); if (strstr($error['message'], 'copy(')) { return false; } } } } closedir($dir); return true; } /** * Updates current update (DB). * * @param string $current_package Current package. * * @return void */ function update_manager_set_current_package($current_package) { if (enterprise_installed()) { $token = 'current_package_enterprise'; } else { $token = 'current_package'; } $col_value = db_escape_key_identifier('value'); $col_key = db_escape_key_identifier('key'); $value = db_get_value( $col_value, 'tupdate_settings', $col_key, $token ); if ($value === false) { db_process_sql_insert( 'tupdate_settings', [ $col_value => $current_package, $col_key => $token, ] ); } else { db_process_sql_update( 'tupdate_settings', [$col_value => $current_package], [$col_key => $token] ); } } /** * Retrieves current update from DB. * * @return string Current update. */ function update_manager_get_current_package() { global $config; if (enterprise_installed()) { $token = 'current_package_enterprise'; } else { $token = 'current_package'; } $current_update = db_get_value( db_escape_key_identifier('value'), 'tupdate_settings', db_escape_key_identifier('key'), $token ); if ($current_update === false) { $current_update = 0; if (isset($config[$token])) { $current_update = $config[$token]; } } return $current_update; } /** * Function recursive delete directory. * * @param string $dir Directory to delete. * @param array $result Array result state and message. * * @return array Return result array with status 0 valid or 1 false and * type 'f' file and 'd' dir and route path file or directory. */ function rmdir_recursive(string $dir, array &$result) { foreach (scandir($dir) as $file) { if ('.' === $file || '..' === $file) { continue; } if (is_dir($dir.'/'.$file) === true) { rmdir_recursive($dir.'/'.$file, $result); } else { $unlink = unlink($dir.'/'.$file); $res = []; $res['status'] = ($unlink === true) ? 0 : 1; $res['type'] = 'f'; $res['path'] = $dir.'/'.$file; array_push($result, $res); } } $rmdir = rmdir($dir); $res = []; $res['status'] = ($rmdir === true) ? 0 : 1; $res['type'] = 'd'; $res['path'] = $dir; array_push($result, $res); return $result; }