Chris@76: $v)
Chris@76: {
Chris@76: if (in_array($k, $octdec))
Chris@76: $current[$k] = octdec(trim($v));
Chris@76: else
Chris@76: $current[$k] = trim($v);
Chris@76: }
Chris@76:
Chris@76: $checksum = 256;
Chris@76: for ($i = 0; $i < 148; $i++)
Chris@76: $checksum += ord($header{$i});
Chris@76: for ($i = 156; $i < 512; $i++)
Chris@76: $checksum += ord($header{$i});
Chris@76:
Chris@76: if ($current['checksum'] != $checksum)
Chris@76: break;
Chris@76:
Chris@76: $size = ceil($current['size'] / 512);
Chris@76: $current['data'] = substr($data, ++$offset << 9, $current['size']);
Chris@76: $offset += $size;
Chris@76:
Chris@76: // Not a directory and doesn't exist already...
Chris@76: if (substr($current['filename'], -1, 1) != '/' && !file_exists($destination . '/' . $current['filename']))
Chris@76: $write_this = true;
Chris@76: // File exists... check if it is newer.
Chris@76: elseif (substr($current['filename'], -1, 1) != '/')
Chris@76: $write_this = $overwrite || filemtime($destination . '/' . $current['filename']) < $current['mtime'];
Chris@76: // Folder... create.
Chris@76: elseif ($destination !== null && !$single_file)
Chris@76: {
Chris@76: // Protect from accidental parent directory writing...
Chris@76: $current['filename'] = strtr($current['filename'], array('../' => '', '/..' => ''));
Chris@76:
Chris@76: if (!file_exists($destination . '/' . $current['filename']))
Chris@76: mktree($destination . '/' . $current['filename'], 0777);
Chris@76: $write_this = false;
Chris@76: }
Chris@76: else
Chris@76: $write_this = false;
Chris@76:
Chris@76: if ($write_this && $destination !== null)
Chris@76: {
Chris@76: if (strpos($current['filename'], '/') !== false && !$single_file)
Chris@76: mktree($destination . '/' . dirname($current['filename']), 0777);
Chris@76:
Chris@76: // Is this the file we're looking for?
Chris@76: if ($single_file && ($destination == $current['filename'] || $destination == '*/' . basename($current['filename'])))
Chris@76: return $current['data'];
Chris@76: // If we're looking for another file, keep going.
Chris@76: elseif ($single_file)
Chris@76: continue;
Chris@76: // Looking for restricted files?
Chris@76: elseif ($files_to_extract !== null && !in_array($current['filename'], $files_to_extract))
Chris@76: continue;
Chris@76:
Chris@76: package_put_contents($destination . '/' . $current['filename'], $current['data']);
Chris@76: }
Chris@76:
Chris@76: if (substr($current['filename'], -1, 1) != '/')
Chris@76: $return[] = array(
Chris@76: 'filename' => $current['filename'],
Chris@76: 'md5' => md5($current['data']),
Chris@76: 'preview' => substr($current['data'], 0, 100),
Chris@76: 'size' => $current['size'],
Chris@76: 'skipped' => false
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: if ($destination !== null && !$single_file)
Chris@76: package_flush_cache();
Chris@76:
Chris@76: if ($single_file)
Chris@76: return false;
Chris@76: else
Chris@76: return $return;
Chris@76: }
Chris@76:
Chris@76: // Extract zip data. If destination is null, return a listing.
Chris@76: function read_zip_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
Chris@76: {
Chris@76: umask(0);
Chris@76: if ($destination !== null && !file_exists($destination) && !$single_file)
Chris@76: mktree($destination, 0777);
Chris@76:
Chris@76: // Look for the PK header...
Chris@76: if (substr($data, 0, 2) != 'PK')
Chris@76: return false;
Chris@76:
Chris@76: // Find the central whosamawhatsit at the end; if there's a comment it's a pain.
Chris@76: if (substr($data, -22, 4) == 'PK' . chr(5) . chr(6))
Chris@76: $p = -22;
Chris@76: else
Chris@76: {
Chris@76: // Have to find where the comment begins, ugh.
Chris@76: for ($p = -22; $p > -strlen($data); $p--)
Chris@76: {
Chris@76: if (substr($data, $p, 4) == 'PK' . chr(5) . chr(6))
Chris@76: break;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: $return = array();
Chris@76:
Chris@76: // Get the basic zip file info.
Chris@76: $zip_info = unpack('vfiles/Vsize/Voffset', substr($data, $p + 10, 10));
Chris@76:
Chris@76: $p = $zip_info['offset'];
Chris@76: for ($i = 0; $i < $zip_info['files']; $i++)
Chris@76: {
Chris@76: // Make sure this is a file entry...
Chris@76: if (substr($data, $p, 4) != 'PK' . chr(1) . chr(2))
Chris@76: return false;
Chris@76:
Chris@76: // Get all the important file information.
Chris@76: $file_info = unpack('Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', substr($data, $p + 16, 30));
Chris@76: $file_info['filename'] = substr($data, $p + 46, $file_info['filename_len']);
Chris@76:
Chris@76: // Skip all the information we don't care about anyway.
Chris@76: $p += 46 + $file_info['filename_len'] + $file_info['extra_len'] + $file_info['comment_len'];
Chris@76:
Chris@76: // If this is a file, and it doesn't exist.... happy days!
Chris@76: if (substr($file_info['filename'], -1, 1) != '/' && !file_exists($destination . '/' . $file_info['filename']))
Chris@76: $write_this = true;
Chris@76: // If the file exists, we may not want to overwrite it.
Chris@76: elseif (substr($file_info['filename'], -1, 1) != '/')
Chris@76: $write_this = $overwrite;
Chris@76: // This is a directory, so we're gonna want to create it. (probably...)
Chris@76: elseif ($destination !== null && !$single_file)
Chris@76: {
Chris@76: // Just a little accident prevention, don't mind me.
Chris@76: $file_info['filename'] = strtr($file_info['filename'], array('../' => '', '/..' => ''));
Chris@76:
Chris@76: if (!file_exists($destination . '/' . $file_info['filename']))
Chris@76: mktree($destination . '/' . $file_info['filename'], 0777);
Chris@76: $write_this = false;
Chris@76: }
Chris@76: else
Chris@76: $write_this = false;
Chris@76:
Chris@76: // Check that the data is there and does exist.
Chris@76: if (substr($data, $file_info['offset'], 4) != 'PK' . chr(3) . chr(4))
Chris@76: return false;
Chris@76:
Chris@76: // Get the actual compressed data.
Chris@76: $file_info['data'] = substr($data, $file_info['offset'] + 30 + $file_info['filename_len'], $file_info['compressed_size']);
Chris@76:
Chris@76: // Only inflate it if we need to ;).
Chris@76: if ($file_info['compressed_size'] != $file_info['size'])
Chris@76: $file_info['data'] = @gzinflate($file_info['data']);
Chris@76:
Chris@76: // Okay! We can write this file, looks good from here...
Chris@76: if ($write_this && $destination !== null)
Chris@76: {
Chris@76: if (strpos($file_info['filename'], '/') !== false && !$single_file)
Chris@76: mktree($destination . '/' . dirname($file_info['filename']), 0777);
Chris@76:
Chris@76: // If we're looking for a specific file, and this is it... ka-bam, baby.
Chris@76: if ($single_file && ($destination == $file_info['filename'] || $destination == '*/' . basename($file_info['filename'])))
Chris@76: return $file_info['data'];
Chris@76: // Oh? Another file. Fine. You don't like this file, do you? I know how it is. Yeah... just go away. No, don't apologize. I know this file's just not *good enough* for you.
Chris@76: elseif ($single_file)
Chris@76: continue;
Chris@76: // Don't really want this?
Chris@76: elseif ($files_to_extract !== null && !in_array($file_info['filename'], $files_to_extract))
Chris@76: continue;
Chris@76:
Chris@76: package_put_contents($destination . '/' . $file_info['filename'], $file_info['data']);
Chris@76: }
Chris@76:
Chris@76: if (substr($file_info['filename'], -1, 1) != '/')
Chris@76: $return[] = array(
Chris@76: 'filename' => $file_info['filename'],
Chris@76: 'md5' => md5($file_info['data']),
Chris@76: 'preview' => substr($file_info['data'], 0, 100),
Chris@76: 'size' => $file_info['size'],
Chris@76: 'skipped' => false
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: if ($destination !== null && !$single_file)
Chris@76: package_flush_cache();
Chris@76:
Chris@76: if ($single_file)
Chris@76: return false;
Chris@76: else
Chris@76: return $return;
Chris@76: }
Chris@76:
Chris@76: // Checks the existence of a remote file since file_exists() does not do remote.
Chris@76: function url_exists($url)
Chris@76: {
Chris@76: $a_url = parse_url($url);
Chris@76:
Chris@76: if (!isset($a_url['scheme']))
Chris@76: return false;
Chris@76:
Chris@76: // Attempt to connect...
Chris@76: $temp = '';
Chris@76: $fid = fsockopen($a_url['host'], !isset($a_url['port']) ? 80 : $a_url['port'], $temp, $temp, 8);
Chris@76: if (!$fid)
Chris@76: return false;
Chris@76:
Chris@76: fputs($fid, 'HEAD ' . $a_url['path'] . ' HTTP/1.0' . "\r\n" . 'Host: ' . $a_url['host'] . "\r\n\r\n");
Chris@76: $head = fread($fid, 1024);
Chris@76: fclose($fid);
Chris@76:
Chris@76: return preg_match('~^HTTP/.+\s+(20[01]|30[127])~i', $head) == 1;
Chris@76: }
Chris@76:
Chris@76: // Load the installed packages.
Chris@76: function loadInstalledPackages()
Chris@76: {
Chris@76: global $boarddir, $smcFunc;
Chris@76:
Chris@76: // First, check that the database is valid, installed.list is still king.
Chris@76: $install_file = implode('', file($boarddir . '/Packages/installed.list'));
Chris@76: if (trim($install_file) == '')
Chris@76: {
Chris@76: $smcFunc['db_query']('', '
Chris@76: UPDATE {db_prefix}log_packages
Chris@76: SET install_state = {int:not_installed}',
Chris@76: array(
Chris@76: 'not_installed' => 0,
Chris@76: )
Chris@76: );
Chris@76:
Chris@76: // Don't have anything left, so send an empty array.
Chris@76: return array();
Chris@76: }
Chris@76:
Chris@76: // Load the packages from the database - note this is ordered by install time to ensure latest package uninstalled first.
Chris@76: $request = $smcFunc['db_query']('', '
Chris@76: SELECT id_install, package_id, filename, name, version
Chris@76: FROM {db_prefix}log_packages
Chris@76: WHERE install_state != {int:not_installed}
Chris@76: ORDER BY time_installed DESC',
Chris@76: array(
Chris@76: 'not_installed' => 0,
Chris@76: )
Chris@76: );
Chris@76: $installed = array();
Chris@76: $found = array();
Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76: {
Chris@76: // Already found this? If so don't add it twice!
Chris@76: if (in_array($row['package_id'], $found))
Chris@76: continue;
Chris@76:
Chris@76: $found[] = $row['package_id'];
Chris@76:
Chris@76: $installed[] = array(
Chris@76: 'id' => $row['id_install'],
Chris@76: 'name' => $row['name'],
Chris@76: 'filename' => $row['filename'],
Chris@76: 'package_id' => $row['package_id'],
Chris@76: 'version' => $row['version'],
Chris@76: );
Chris@76: }
Chris@76: $smcFunc['db_free_result']($request);
Chris@76:
Chris@76: return $installed;
Chris@76: }
Chris@76:
Chris@76: function getPackageInfo($gzfilename)
Chris@76: {
Chris@76: global $boarddir;
Chris@76:
Chris@76: // Extract package-info.xml from downloaded file. (*/ is used because it could be in any directory.)
Chris@76: if (strpos($gzfilename, 'http://') !== false)
Chris@76: $packageInfo = read_tgz_data(fetch_web_data($gzfilename, '', true), '*/package-info.xml', true);
Chris@76: else
Chris@76: {
Chris@76: if (!file_exists($boarddir . '/Packages/' . $gzfilename))
Chris@76: return 'package_get_error_not_found';
Chris@76:
Chris@76: if (is_file($boarddir . '/Packages/' . $gzfilename))
Chris@76: $packageInfo = read_tgz_file($boarddir . '/Packages/' . $gzfilename, '*/package-info.xml', true);
Chris@76: elseif (file_exists($boarddir . '/Packages/' . $gzfilename . '/package-info.xml'))
Chris@76: $packageInfo = file_get_contents($boarddir . '/Packages/' . $gzfilename . '/package-info.xml');
Chris@76: else
Chris@76: return 'package_get_error_missing_xml';
Chris@76: }
Chris@76:
Chris@76: // Nothing?
Chris@76: if (empty($packageInfo))
Chris@76: return 'package_get_error_is_zero';
Chris@76:
Chris@76: // Parse package-info.xml into an xmlArray.
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $packageInfo = new xmlArray($packageInfo);
Chris@76:
Chris@76: // !!! Error message of some sort?
Chris@76: if (!$packageInfo->exists('package-info[0]'))
Chris@76: return 'package_get_error_packageinfo_corrupt';
Chris@76:
Chris@76: $packageInfo = $packageInfo->path('package-info[0]');
Chris@76:
Chris@76: $package = $packageInfo->to_array();
Chris@76: $package['xml'] = $packageInfo;
Chris@76: $package['filename'] = $gzfilename;
Chris@76:
Chris@76: if (!isset($package['type']))
Chris@76: $package['type'] = 'modification';
Chris@76:
Chris@76: return $package;
Chris@76: }
Chris@76:
Chris@76: // Create a chmod control for chmoding files.
Chris@76: function create_chmod_control($chmodFiles = array(), $chmodOptions = array(), $restore_write_status = false)
Chris@76: {
Chris@76: global $context, $modSettings, $package_ftp, $boarddir, $txt, $sourcedir, $scripturl;
Chris@76:
Chris@76: // If we're restoring the status of existing files prepare the data.
Chris@76: if ($restore_write_status && isset($_SESSION['pack_ftp']) && !empty($_SESSION['pack_ftp']['original_perms']))
Chris@76: {
Chris@76: function list_restoreFiles($dummy1, $dummy2, $dummy3, $do_change)
Chris@76: {
Chris@76: global $txt;
Chris@76:
Chris@76: $restore_files = array();
Chris@76: foreach ($_SESSION['pack_ftp']['original_perms'] as $file => $perms)
Chris@76: {
Chris@76: // Check the file still exists, and the permissions were indeed different than now.
Chris@76: $file_permissions = @fileperms($file);
Chris@76: if (!file_exists($file) || $file_permissions == $perms)
Chris@76: {
Chris@76: unset($_SESSION['pack_ftp']['original_perms'][$file]);
Chris@76: continue;
Chris@76: }
Chris@76:
Chris@76: // Are we wanting to change the permission?
Chris@76: if ($do_change && isset($_POST['restore_files']) && in_array($file, $_POST['restore_files']))
Chris@76: {
Chris@76: // Use FTP if we have it.
Chris@76: if (!empty($package_ftp))
Chris@76: {
Chris@76: $ftp_file = strtr($file, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76: $package_ftp->chmod($ftp_file, $perms);
Chris@76: }
Chris@76: else
Chris@76: @chmod($file, $perms);
Chris@76:
Chris@76: $new_permissions = @fileperms($file);
Chris@76: $result = $new_permissions == $perms ? 'success' : 'failure';
Chris@76: unset($_SESSION['pack_ftp']['original_perms'][$file]);
Chris@76: }
Chris@76: elseif ($do_change)
Chris@76: {
Chris@76: $new_permissions = '';
Chris@76: $result = 'skipped';
Chris@76: unset($_SESSION['pack_ftp']['original_perms'][$file]);
Chris@76: }
Chris@76:
Chris@76: // Record the results!
Chris@76: $restore_files[] = array(
Chris@76: 'path' => $file,
Chris@76: 'old_perms_raw' => $perms,
Chris@76: 'old_perms' => substr(sprintf('%o', $perms), -4),
Chris@76: 'cur_perms' => substr(sprintf('%o', $file_permissions), -4),
Chris@76: 'new_perms' => isset($new_permissions) ? substr(sprintf('%o', $new_permissions), -4) : '',
Chris@76: 'result' => isset($result) ? $result : '',
Chris@76: 'writable_message' => '' . (@is_writable($file) ? $txt['package_file_perms_writable'] : $txt['package_file_perms_not_writable']) . '',
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: return $restore_files;
Chris@76: }
Chris@76:
Chris@76: $listOptions = array(
Chris@76: 'id' => 'restore_file_permissions',
Chris@76: 'title' => $txt['package_restore_permissions'],
Chris@76: 'get_items' => array(
Chris@76: 'function' => 'list_restoreFiles',
Chris@76: 'params' => array(
Chris@76: !empty($_POST['restore_perms']),
Chris@76: ),
Chris@76: ),
Chris@76: 'columns' => array(
Chris@76: 'path' => array(
Chris@76: 'header' => array(
Chris@76: 'value' => $txt['package_restore_permissions_filename'],
Chris@76: ),
Chris@76: 'data' => array(
Chris@76: 'db' => 'path',
Chris@76: 'class' => 'smalltext',
Chris@76: ),
Chris@76: ),
Chris@76: 'old_perms' => array(
Chris@76: 'header' => array(
Chris@76: 'value' => $txt['package_restore_permissions_orig_status'],
Chris@76: ),
Chris@76: 'data' => array(
Chris@76: 'db' => 'old_perms',
Chris@76: 'class' => 'smalltext',
Chris@76: ),
Chris@76: ),
Chris@76: 'cur_perms' => array(
Chris@76: 'header' => array(
Chris@76: 'value' => $txt['package_restore_permissions_cur_status'],
Chris@76: ),
Chris@76: 'data' => array(
Chris@76: 'function' => create_function('$rowData', '
Chris@76: global $txt;
Chris@76:
Chris@76: $formatTxt = $rowData[\'result\'] == \'\' || $rowData[\'result\'] == \'skipped\' ? $txt[\'package_restore_permissions_pre_change\'] : $txt[\'package_restore_permissions_post_change\'];
Chris@76: return sprintf($formatTxt, $rowData[\'cur_perms\'], $rowData[\'new_perms\'], $rowData[\'writable_message\']);
Chris@76: '),
Chris@76: 'class' => 'smalltext',
Chris@76: ),
Chris@76: ),
Chris@76: 'check' => array(
Chris@76: 'header' => array(
Chris@76: 'value' => '',
Chris@76: ),
Chris@76: 'data' => array(
Chris@76: 'sprintf' => array(
Chris@76: 'format' => '',
Chris@76: 'params' => array(
Chris@76: 'path' => false,
Chris@76: ),
Chris@76: ),
Chris@76: 'style' => 'text-align: center',
Chris@76: ),
Chris@76: ),
Chris@76: 'result' => array(
Chris@76: 'header' => array(
Chris@76: 'value' => $txt['package_restore_permissions_result'],
Chris@76: ),
Chris@76: 'data' => array(
Chris@76: 'function' => create_function('$rowData', '
Chris@76: global $txt;
Chris@76:
Chris@76: return $txt[\'package_restore_permissions_action_\' . $rowData[\'result\']];
Chris@76: '),
Chris@76: 'class' => 'smalltext',
Chris@76: ),
Chris@76: ),
Chris@76: ),
Chris@76: 'form' => array(
Chris@76: 'href' => !empty($chmodOptions['destination_url']) ? $chmodOptions['destination_url'] : $scripturl . '?action=admin;area=packages;sa=perms;restore;' . $context['session_var'] . '=' . $context['session_id'],
Chris@76: ),
Chris@76: 'additional_rows' => array(
Chris@76: array(
Chris@76: 'position' => 'below_table_data',
Chris@76: 'value' => '',
Chris@76: 'class' => 'titlebg',
Chris@76: 'style' => 'text-align: right;',
Chris@76: ),
Chris@76: array(
Chris@76: 'position' => 'after_title',
Chris@76: 'value' => '' . $txt['package_restore_permissions_desc'] . '',
Chris@76: 'class' => 'windowbg2',
Chris@76: ),
Chris@76: ),
Chris@76: );
Chris@76:
Chris@76: // Work out what columns and the like to show.
Chris@76: if (!empty($_POST['restore_perms']))
Chris@76: {
Chris@76: $listOptions['additional_rows'][1]['value'] = sprintf($txt['package_restore_permissions_action_done'], $scripturl . '?action=admin;area=packages;sa=perms;' . $context['session_var'] . '=' . $context['session_id']);
Chris@76: unset($listOptions['columns']['check'], $listOptions['form'], $listOptions['additional_rows'][0]);
Chris@76:
Chris@76: $context['sub_template'] = 'show_list';
Chris@76: $context['default_list'] = 'restore_file_permissions';
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: unset($listOptions['columns']['result']);
Chris@76: }
Chris@76:
Chris@76: // Create the list for display.
Chris@76: require_once($sourcedir . '/Subs-List.php');
Chris@76: createList($listOptions);
Chris@76:
Chris@76: // If we just restored permissions then whereever we are, we are now done and dusted.
Chris@76: if (!empty($_POST['restore_perms']))
Chris@76: obExit();
Chris@76: }
Chris@76: // Otherwise, it's entirely irrelevant?
Chris@76: elseif ($restore_write_status)
Chris@76: return true;
Chris@76:
Chris@76: // This is where we report what we got up to.
Chris@76: $return_data = array(
Chris@76: 'files' => array(
Chris@76: 'writable' => array(),
Chris@76: 'notwritable' => array(),
Chris@76: ),
Chris@76: );
Chris@76:
Chris@76: // If we have some FTP information already, then let's assume it was required and try to get ourselves connected.
Chris@76: if (!empty($_SESSION['pack_ftp']['connected']))
Chris@76: {
Chris@76: // Load the file containing the ftp_connection class.
Chris@76: loadClassFile('Class-Package.php');
Chris@76:
Chris@76: $package_ftp = new ftp_connection($_SESSION['pack_ftp']['server'], $_SESSION['pack_ftp']['port'], $_SESSION['pack_ftp']['username'], package_crypt($_SESSION['pack_ftp']['password']));
Chris@76: }
Chris@76:
Chris@76: // Just got a submission did we?
Chris@76: if (empty($package_ftp) && isset($_POST['ftp_username']))
Chris@76: {
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
Chris@76:
Chris@76: // We're connected, jolly good!
Chris@76: if ($ftp->error === false)
Chris@76: {
Chris@76: // Common mistake, so let's try to remedy it...
Chris@76: if (!$ftp->chdir($_POST['ftp_path']))
Chris@76: {
Chris@76: $ftp_error = $ftp->last_message;
Chris@76: $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
Chris@76: }
Chris@76:
Chris@76: if (!in_array($_POST['ftp_path'], array('', '/')))
Chris@76: {
Chris@76: $ftp_root = strtr($boarddir, array($_POST['ftp_path'] => ''));
Chris@76: if (substr($ftp_root, -1) == '/' && ($_POST['ftp_path'] == '' || substr($_POST['ftp_path'], 0, 1) == '/'))
Chris@76: $ftp_root = substr($ftp_root, 0, -1);
Chris@76: }
Chris@76: else
Chris@76: $ftp_root = $boarddir;
Chris@76:
Chris@76: $_SESSION['pack_ftp'] = array(
Chris@76: 'server' => $_POST['ftp_server'],
Chris@76: 'port' => $_POST['ftp_port'],
Chris@76: 'username' => $_POST['ftp_username'],
Chris@76: 'password' => package_crypt($_POST['ftp_password']),
Chris@76: 'path' => $_POST['ftp_path'],
Chris@76: 'root' => $ftp_root,
Chris@76: 'connected' => true,
Chris@76: );
Chris@76:
Chris@76: if (!isset($modSettings['package_path']) || $modSettings['package_path'] != $_POST['ftp_path'])
Chris@76: updateSettings(array('package_path' => $_POST['ftp_path']));
Chris@76:
Chris@76: // This is now the primary connection.
Chris@76: $package_ftp = $ftp;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Now try to simply make the files writable, with whatever we might have.
Chris@76: if (!empty($chmodFiles))
Chris@76: {
Chris@76: foreach ($chmodFiles as $k => $file)
Chris@76: {
Chris@76: // Sometimes this can somehow happen maybe?
Chris@76: if (empty($file))
Chris@76: unset($chmodFiles[$k]);
Chris@76: // Already writable?
Chris@76: elseif (@is_writable($file))
Chris@76: $return_data['files']['writable'][] = $file;
Chris@76: else
Chris@76: {
Chris@76: // Now try to change that.
Chris@76: $return_data['files'][package_chmod($file, 'writable', true) ? 'writable' : 'notwritable'][] = $file;
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Have we still got nasty files which ain't writable? Dear me we need more FTP good sir.
Chris@76: if (empty($package_ftp) && (!empty($return_data['files']['notwritable']) || !empty($chmodOptions['force_find_error'])))
Chris@76: {
Chris@76: if (!isset($ftp) || $ftp->error !== false)
Chris@76: {
Chris@76: if (!isset($ftp))
Chris@76: {
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $ftp = new ftp_connection(null);
Chris@76: }
Chris@76: elseif ($ftp->error !== false && !isset($ftp_error))
Chris@76: $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
Chris@76:
Chris@76: list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
Chris@76:
Chris@76: if ($found_path)
Chris@76: $_POST['ftp_path'] = $detect_path;
Chris@76: elseif (!isset($_POST['ftp_path']))
Chris@76: $_POST['ftp_path'] = isset($modSettings['package_path']) ? $modSettings['package_path'] : $detect_path;
Chris@76:
Chris@76: if (!isset($_POST['ftp_username']))
Chris@76: $_POST['ftp_username'] = $username;
Chris@76: }
Chris@76:
Chris@76: $context['package_ftp'] = array(
Chris@76: 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
Chris@76: 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
Chris@76: 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
Chris@76: 'path' => $_POST['ftp_path'],
Chris@76: 'error' => empty($ftp_error) ? null : $ftp_error,
Chris@76: 'destination' => !empty($chmodOptions['destination_url']) ? $chmodOptions['destination_url'] : '',
Chris@76: );
Chris@76:
Chris@76: // Which files failed?
Chris@76: if (!isset($context['notwritable_files']))
Chris@76: $context['notwritable_files'] = array();
Chris@76: $context['notwritable_files'] = array_merge($context['notwritable_files'], $return_data['files']['notwritable']);
Chris@76:
Chris@76: // Sent here to die?
Chris@76: if (!empty($chmodOptions['crash_on_error']))
Chris@76: {
Chris@76: $context['page_title'] = $txt['package_ftp_necessary'];
Chris@76: $context['sub_template'] = 'ftp_required';
Chris@76: obExit();
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: return $return_data;
Chris@76: }
Chris@76:
Chris@76: function packageRequireFTP($destination_url, $files = null, $return = false)
Chris@76: {
Chris@76: global $context, $modSettings, $package_ftp, $boarddir, $txt;
Chris@76:
Chris@76: // Try to make them writable the manual way.
Chris@76: if ($files !== null)
Chris@76: {
Chris@76: foreach ($files as $k => $file)
Chris@76: {
Chris@76: // If this file doesn't exist, then we actually want to look at the directory, no?
Chris@76: if (!file_exists($file))
Chris@76: $file = dirname($file);
Chris@76:
Chris@76: // This looks odd, but it's an attempt to work around PHP suExec.
Chris@76: if (!@is_writable($file))
Chris@76: @chmod($file, 0755);
Chris@76: if (!@is_writable($file))
Chris@76: @chmod($file, 0777);
Chris@76: if (!@is_writable(dirname($file)))
Chris@76: @chmod($file, 0755);
Chris@76: if (!@is_writable(dirname($file)))
Chris@76: @chmod($file, 0777);
Chris@76:
Chris@76: $fp = is_dir($file) ? @opendir($file) : @fopen($file, 'rb');
Chris@76: if (@is_writable($file) && $fp)
Chris@76: {
Chris@76: unset($files[$k]);
Chris@76: if (!is_dir($file))
Chris@76: fclose($fp);
Chris@76: else
Chris@76: closedir($fp);
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // No FTP required!
Chris@76: if (empty($files))
Chris@76: return array();
Chris@76: }
Chris@76:
Chris@76: // They've opted to not use FTP, and try anyway.
Chris@76: if (isset($_SESSION['pack_ftp']) && $_SESSION['pack_ftp'] == false)
Chris@76: {
Chris@76: if ($files === null)
Chris@76: return array();
Chris@76:
Chris@76: foreach ($files as $k => $file)
Chris@76: {
Chris@76: // This looks odd, but it's an attempt to work around PHP suExec.
Chris@76: if (!file_exists($file))
Chris@76: {
Chris@76: mktree(dirname($file), 0755);
Chris@76: @touch($file);
Chris@76: @chmod($file, 0755);
Chris@76: }
Chris@76:
Chris@76: if (!@is_writable($file))
Chris@76: @chmod($file, 0777);
Chris@76: if (!@is_writable(dirname($file)))
Chris@76: @chmod(dirname($file), 0777);
Chris@76:
Chris@76: if (@is_writable($file))
Chris@76: unset($files[$k]);
Chris@76: }
Chris@76:
Chris@76: return $files;
Chris@76: }
Chris@76: elseif (isset($_SESSION['pack_ftp']))
Chris@76: {
Chris@76: // Load the file containing the ftp_connection class.
Chris@76: loadClassFile('Class-Package.php');
Chris@76:
Chris@76: $package_ftp = new ftp_connection($_SESSION['pack_ftp']['server'], $_SESSION['pack_ftp']['port'], $_SESSION['pack_ftp']['username'], package_crypt($_SESSION['pack_ftp']['password']));
Chris@76:
Chris@76: if ($files === null)
Chris@76: return array();
Chris@76:
Chris@76: foreach ($files as $k => $file)
Chris@76: {
Chris@76: $ftp_file = strtr($file, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76:
Chris@76: // This looks odd, but it's an attempt to work around PHP suExec.
Chris@76: if (!file_exists($file))
Chris@76: {
Chris@76: mktree(dirname($file), 0755);
Chris@76: $package_ftp->create_file($ftp_file);
Chris@76: $package_ftp->chmod($ftp_file, 0755);
Chris@76: }
Chris@76:
Chris@76: if (!@is_writable($file))
Chris@76: $package_ftp->chmod($ftp_file, 0777);
Chris@76: if (!@is_writable(dirname($file)))
Chris@76: $package_ftp->chmod(dirname($ftp_file), 0777);
Chris@76:
Chris@76: if (@is_writable($file))
Chris@76: unset($files[$k]);
Chris@76: }
Chris@76:
Chris@76: return $files;
Chris@76: }
Chris@76:
Chris@76: if (isset($_POST['ftp_none']))
Chris@76: {
Chris@76: $_SESSION['pack_ftp'] = false;
Chris@76:
Chris@76: $files = packageRequireFTP($destination_url, $files, $return);
Chris@76: return $files;
Chris@76: }
Chris@76: elseif (isset($_POST['ftp_username']))
Chris@76: {
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $ftp = new ftp_connection($_POST['ftp_server'], $_POST['ftp_port'], $_POST['ftp_username'], $_POST['ftp_password']);
Chris@76:
Chris@76: if ($ftp->error === false)
Chris@76: {
Chris@76: // Common mistake, so let's try to remedy it...
Chris@76: if (!$ftp->chdir($_POST['ftp_path']))
Chris@76: {
Chris@76: $ftp_error = $ftp->last_message;
Chris@76: $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $_POST['ftp_path']));
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: if (!isset($ftp) || $ftp->error !== false)
Chris@76: {
Chris@76: if (!isset($ftp))
Chris@76: {
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $ftp = new ftp_connection(null);
Chris@76: }
Chris@76: elseif ($ftp->error !== false && !isset($ftp_error))
Chris@76: $ftp_error = $ftp->last_message === null ? '' : $ftp->last_message;
Chris@76:
Chris@76: list ($username, $detect_path, $found_path) = $ftp->detect_path($boarddir);
Chris@76:
Chris@76: if ($found_path)
Chris@76: $_POST['ftp_path'] = $detect_path;
Chris@76: elseif (!isset($_POST['ftp_path']))
Chris@76: $_POST['ftp_path'] = isset($modSettings['package_path']) ? $modSettings['package_path'] : $detect_path;
Chris@76:
Chris@76: if (!isset($_POST['ftp_username']))
Chris@76: $_POST['ftp_username'] = $username;
Chris@76:
Chris@76: $context['package_ftp'] = array(
Chris@76: 'server' => isset($_POST['ftp_server']) ? $_POST['ftp_server'] : (isset($modSettings['package_server']) ? $modSettings['package_server'] : 'localhost'),
Chris@76: 'port' => isset($_POST['ftp_port']) ? $_POST['ftp_port'] : (isset($modSettings['package_port']) ? $modSettings['package_port'] : '21'),
Chris@76: 'username' => isset($_POST['ftp_username']) ? $_POST['ftp_username'] : (isset($modSettings['package_username']) ? $modSettings['package_username'] : ''),
Chris@76: 'path' => $_POST['ftp_path'],
Chris@76: 'error' => empty($ftp_error) ? null : $ftp_error,
Chris@76: 'destination' => $destination_url,
Chris@76: );
Chris@76:
Chris@76: // If we're returning dump out here.
Chris@76: if ($return)
Chris@76: return $files;
Chris@76:
Chris@76: $context['page_title'] = $txt['package_ftp_necessary'];
Chris@76: $context['sub_template'] = 'ftp_required';
Chris@76: obExit();
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: if (!in_array($_POST['ftp_path'], array('', '/')))
Chris@76: {
Chris@76: $ftp_root = strtr($boarddir, array($_POST['ftp_path'] => ''));
Chris@76: if (substr($ftp_root, -1) == '/' && ($_POST['ftp_path'] == '' || substr($_POST['ftp_path'], 0, 1) == '/'))
Chris@76: $ftp_root = substr($ftp_root, 0, -1);
Chris@76: }
Chris@76: else
Chris@76: $ftp_root = $boarddir;
Chris@76:
Chris@76: $_SESSION['pack_ftp'] = array(
Chris@76: 'server' => $_POST['ftp_server'],
Chris@76: 'port' => $_POST['ftp_port'],
Chris@76: 'username' => $_POST['ftp_username'],
Chris@76: 'password' => package_crypt($_POST['ftp_password']),
Chris@76: 'path' => $_POST['ftp_path'],
Chris@76: 'root' => $ftp_root,
Chris@76: );
Chris@76:
Chris@76: if (!isset($modSettings['package_path']) || $modSettings['package_path'] != $_POST['ftp_path'])
Chris@76: updateSettings(array('package_path' => $_POST['ftp_path']));
Chris@76:
Chris@76: $files = packageRequireFTP($destination_url, $files, $return);
Chris@76: }
Chris@76:
Chris@76: return $files;
Chris@76: }
Chris@76:
Chris@76: // Parses a package-info.xml file - method can be 'install', 'upgrade', or 'uninstall'.
Chris@76: function parsePackageInfo(&$packageXML, $testing_only = true, $method = 'install', $previous_version = '')
Chris@76: {
Chris@76: global $boarddir, $forum_version, $context, $temp_path, $language;
Chris@76:
Chris@76: // Mayday! That action doesn't exist!!
Chris@76: if (empty($packageXML) || !$packageXML->exists($method))
Chris@76: return array();
Chris@76:
Chris@76: // We haven't found the package script yet...
Chris@76: $script = false;
Chris@76: $the_version = strtr($forum_version, array('SMF ' => ''));
Chris@76:
Chris@76: // Emulation support...
Chris@76: if (!empty($_SESSION['version_emulate']))
Chris@76: $the_version = $_SESSION['version_emulate'];
Chris@76:
Chris@76: // Get all the versions of this method and find the right one.
Chris@76: $these_methods = $packageXML->set($method);
Chris@76: foreach ($these_methods as $this_method)
Chris@76: {
Chris@76: // They specified certain versions this part is for.
Chris@76: if ($this_method->exists('@for'))
Chris@76: {
Chris@76: // Don't keep going if this won't work for this version of SMF.
Chris@76: if (!matchPackageVersion($the_version, $this_method->fetch('@for')))
Chris@76: continue;
Chris@76: }
Chris@76:
Chris@76: // Upgrades may go from a certain old version of the mod.
Chris@76: if ($method == 'upgrade' && $this_method->exists('@from'))
Chris@76: {
Chris@76: // Well, this is for the wrong old version...
Chris@76: if (!matchPackageVersion($previous_version, $this_method->fetch('@from')))
Chris@76: continue;
Chris@76: }
Chris@76:
Chris@76: // We've found it!
Chris@76: $script = $this_method;
Chris@76: break;
Chris@76: }
Chris@76:
Chris@76: // Bad news, a matching script wasn't found!
Chris@76: if ($script === false)
Chris@76: return array();
Chris@76:
Chris@76: // Find all the actions in this method - in theory, these should only be allowed actions. (* means all.)
Chris@76: $actions = $script->set('*');
Chris@76: $return = array();
Chris@76:
Chris@76: $temp_auto = 0;
Chris@76: $temp_path = $boarddir . '/Packages/temp/' . (isset($context['base_path']) ? $context['base_path'] : '');
Chris@76:
Chris@76: $context['readmes'] = array();
Chris@76: // This is the testing phase... nothing shall be done yet.
Chris@76: foreach ($actions as $action)
Chris@76: {
Chris@76: $actionType = $action->name();
Chris@76:
Chris@76: if ($actionType == 'readme' || $actionType == 'code' || $actionType == 'database' || $actionType == 'modification' || $actionType == 'redirect')
Chris@76: {
Chris@76: // Allow for translated readme files.
Chris@76: if ($actionType == 'readme')
Chris@76: {
Chris@76: if ($action->exists('@lang'))
Chris@76: {
Chris@76: // Auto-select a readme language based on either request variable or current language.
Chris@76: if ((isset($_REQUEST['readme']) && $action->fetch('@lang') == $_REQUEST['readme']) || (!isset($_REQUEST['readme']) && $action->fetch('@lang') == $language))
Chris@76: {
Chris@76: // In case the user put the readme blocks in the wrong order.
Chris@76: if (isset($context['readmes']['selected']) && $context['readmes']['selected'] == 'default')
Chris@76: $context['readmes'][] = 'default';
Chris@76:
Chris@76: $context['readmes']['selected'] = htmlspecialchars($action->fetch('@lang'));
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: // We don't want this readme now, but we'll allow the user to select to read it.
Chris@76: $context['readmes'][] = htmlspecialchars($action->fetch('@lang'));
Chris@76: continue;
Chris@76: }
Chris@76: }
Chris@76: // Fallback readme. Without lang parameter.
Chris@76: else
Chris@76: {
Chris@76:
Chris@76: // Already selected a readme.
Chris@76: if (isset($context['readmes']['selected']))
Chris@76: {
Chris@76: $context['readmes'][] = 'default';
Chris@76: continue;
Chris@76: }
Chris@76: else
Chris@76: $context['readmes']['selected'] = 'default';
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // !!! TODO: Make sure the file actually exists? Might not work when testing?
Chris@76: if ($action->exists('@type') && $action->fetch('@type') == 'inline')
Chris@76: {
Chris@76: $filename = $temp_path . '$auto_' . $temp_auto++ . ($actionType == 'readme' || $actionType == 'redirect' ? '.txt' : ($actionType == 'code' || $actionType == 'database' ? '.php' : '.mod'));
Chris@76: package_put_contents($filename, $action->fetch('.'));
Chris@76: $filename = strtr($filename, array($temp_path => ''));
Chris@76: }
Chris@76: else
Chris@76: $filename = $action->fetch('.');
Chris@76:
Chris@76: $return[] = array(
Chris@76: 'type' => $actionType,
Chris@76: 'filename' => $filename,
Chris@76: 'description' => '',
Chris@76: 'reverse' => $action->exists('@reverse') && $action->fetch('@reverse') == 'true',
Chris@76: 'boardmod' => $action->exists('@format') && $action->fetch('@format') == 'boardmod',
Chris@76: 'redirect_url' => $action->exists('@url') ? $action->fetch('@url') : '',
Chris@76: 'redirect_timeout' => $action->exists('@timeout') ? (int) $action->fetch('@timeout') : '',
Chris@76: 'parse_bbc' => $action->exists('@parsebbc') && $action->fetch('@parsebbc') == 'true',
Chris@76: 'language' => ($actionType == 'readme' && $action->exists('@lang') && $action->fetch('@lang') == $language) ? $language : '',
Chris@76: );
Chris@76:
Chris@76: continue;
Chris@76: }
Chris@76: elseif ($actionType == 'error')
Chris@76: {
Chris@76: $return[] = array(
Chris@76: 'type' => 'error',
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: $this_action = &$return[];
Chris@76: $this_action = array(
Chris@76: 'type' => $actionType,
Chris@76: 'filename' => $action->fetch('@name'),
Chris@76: 'description' => $action->fetch('.')
Chris@76: );
Chris@76:
Chris@76: // If there is a destination, make sure it makes sense.
Chris@76: if (substr($actionType, 0, 6) != 'remove')
Chris@76: {
Chris@76: $this_action['unparsed_destination'] = $action->fetch('@destination');
Chris@76: $this_action['destination'] = parse_path($action->fetch('@destination')) . '/' . basename($this_action['filename']);
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: $this_action['unparsed_filename'] = $this_action['filename'];
Chris@76: $this_action['filename'] = parse_path($this_action['filename']);
Chris@76: }
Chris@76:
Chris@76: // If we're moving or requiring (copying) a file.
Chris@76: if (substr($actionType, 0, 4) == 'move' || substr($actionType, 0, 7) == 'require')
Chris@76: {
Chris@76: if ($action->exists('@from'))
Chris@76: $this_action['source'] = parse_path($action->fetch('@from'));
Chris@76: else
Chris@76: $this_action['source'] = $temp_path . $this_action['filename'];
Chris@76: }
Chris@76:
Chris@76: // Check if these things can be done. (chmod's etc.)
Chris@76: if ($actionType == 'create-dir')
Chris@76: {
Chris@76: if (!mktree($this_action['destination'], false))
Chris@76: {
Chris@76: $temp = $this_action['destination'];
Chris@76: while (!file_exists($temp) && strlen($temp) > 1)
Chris@76: $temp = dirname($temp);
Chris@76:
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $temp
Chris@76: );
Chris@76: }
Chris@76: }
Chris@76: elseif ($actionType == 'create-file')
Chris@76: {
Chris@76: if (!mktree(dirname($this_action['destination']), false))
Chris@76: {
Chris@76: $temp = dirname($this_action['destination']);
Chris@76: while (!file_exists($temp) && strlen($temp) > 1)
Chris@76: $temp = dirname($temp);
Chris@76:
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $temp
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $this_action['destination']
Chris@76: );
Chris@76: }
Chris@76: elseif ($actionType == 'require-dir')
Chris@76: {
Chris@76: if (!mktree($this_action['destination'], false))
Chris@76: {
Chris@76: $temp = $this_action['destination'];
Chris@76: while (!file_exists($temp) && strlen($temp) > 1)
Chris@76: $temp = dirname($temp);
Chris@76:
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $temp
Chris@76: );
Chris@76: }
Chris@76: }
Chris@76: elseif ($actionType == 'require-file')
Chris@76: {
Chris@76: if ($action->exists('@theme'))
Chris@76: $this_action['theme_action'] = $action->fetch('@theme');
Chris@76:
Chris@76: if (!mktree(dirname($this_action['destination']), false))
Chris@76: {
Chris@76: $temp = dirname($this_action['destination']);
Chris@76: while (!file_exists($temp) && strlen($temp) > 1)
Chris@76: $temp = dirname($temp);
Chris@76:
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $temp
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $this_action['destination']
Chris@76: );
Chris@76: }
Chris@76: elseif ($actionType == 'move-dir' || $actionType == 'move-file')
Chris@76: {
Chris@76: if (!mktree(dirname($this_action['destination']), false))
Chris@76: {
Chris@76: $temp = dirname($this_action['destination']);
Chris@76: while (!file_exists($temp) && strlen($temp) > 1)
Chris@76: $temp = dirname($temp);
Chris@76:
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $temp
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: if (!is_writable($this_action['destination']) && (file_exists($this_action['destination']) || !is_writable(dirname($this_action['destination']))))
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $this_action['destination']
Chris@76: );
Chris@76: }
Chris@76: elseif ($actionType == 'remove-dir')
Chris@76: {
Chris@76: if (!is_writable($this_action['filename']) && file_exists($this_action['destination']))
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $this_action['filename']
Chris@76: );
Chris@76: }
Chris@76: elseif ($actionType == 'remove-file')
Chris@76: {
Chris@76: if (!is_writable($this_action['filename']) && file_exists($this_action['filename']))
Chris@76: $return[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $this_action['filename']
Chris@76: );
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Only testing - just return a list of things to be done.
Chris@76: if ($testing_only)
Chris@76: return $return;
Chris@76:
Chris@76: umask(0);
Chris@76:
Chris@76: $failure = false;
Chris@76: $not_done = array(array('type' => '!'));
Chris@76: foreach ($return as $action)
Chris@76: {
Chris@76: if ($action['type'] == 'modification' || $action['type'] == 'code' || $action['type'] == 'database' || $action['type'] == 'redirect')
Chris@76: $not_done[] = $action;
Chris@76:
Chris@76: if ($action['type'] == 'create-dir')
Chris@76: {
Chris@76: if (!mktree($action['destination'], 0755) || !is_writable($action['destination']))
Chris@76: $failure |= !mktree($action['destination'], 0777);
Chris@76: }
Chris@76: elseif ($action['type'] == 'create-file')
Chris@76: {
Chris@76: if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
Chris@76: $failure |= !mktree(dirname($action['destination']), 0777);
Chris@76:
Chris@76: // Create an empty file.
Chris@76: package_put_contents($action['destination'], package_get_contents($action['source']), $testing_only);
Chris@76:
Chris@76: if (!file_exists($action['destination']))
Chris@76: $failure = true;
Chris@76: }
Chris@76: elseif ($action['type'] == 'require-dir')
Chris@76: {
Chris@76: copytree($action['source'], $action['destination']);
Chris@76: // Any other theme folders?
Chris@76: if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['destination']]))
Chris@76: foreach ($context['theme_copies'][$action['type']][$action['destination']] as $theme_destination)
Chris@76: copytree($action['source'], $theme_destination);
Chris@76: }
Chris@76: elseif ($action['type'] == 'require-file')
Chris@76: {
Chris@76: if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
Chris@76: $failure |= !mktree(dirname($action['destination']), 0777);
Chris@76:
Chris@76: package_put_contents($action['destination'], package_get_contents($action['source']), $testing_only);
Chris@76:
Chris@76: $failure |= !copy($action['source'], $action['destination']);
Chris@76:
Chris@76: // Any other theme files?
Chris@76: if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['destination']]))
Chris@76: foreach ($context['theme_copies'][$action['type']][$action['destination']] as $theme_destination)
Chris@76: {
Chris@76: if (!mktree(dirname($theme_destination), 0755) || !is_writable(dirname($theme_destination)))
Chris@76: $failure |= !mktree(dirname($theme_destination), 0777);
Chris@76:
Chris@76: package_put_contents($theme_destination, package_get_contents($action['source']), $testing_only);
Chris@76:
Chris@76: $failure |= !copy($action['source'], $theme_destination);
Chris@76: }
Chris@76: }
Chris@76: elseif ($action['type'] == 'move-file')
Chris@76: {
Chris@76: if (!mktree(dirname($action['destination']), 0755) || !is_writable(dirname($action['destination'])))
Chris@76: $failure |= !mktree(dirname($action['destination']), 0777);
Chris@76:
Chris@76: $failure |= !rename($action['source'], $action['destination']);
Chris@76: }
Chris@76: elseif ($action['type'] == 'move-dir')
Chris@76: {
Chris@76: if (!mktree($action['destination'], 0755) || !is_writable($action['destination']))
Chris@76: $failure |= !mktree($action['destination'], 0777);
Chris@76:
Chris@76: $failure |= !rename($action['source'], $action['destination']);
Chris@76: }
Chris@76: elseif ($action['type'] == 'remove-dir')
Chris@76: {
Chris@76: deltree($action['filename']);
Chris@76:
Chris@76: // Any other theme folders?
Chris@76: if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['filename']]))
Chris@76: foreach ($context['theme_copies'][$action['type']][$action['filename']] as $theme_destination)
Chris@76: deltree($theme_destination);
Chris@76: }
Chris@76: elseif ($action['type'] == 'remove-file')
Chris@76: {
Chris@76: // Make sure the file exists before deleting it.
Chris@76: if (file_exists($action['filename']))
Chris@76: {
Chris@76: package_chmod($action['filename']);
Chris@76: $failure |= !unlink($action['filename']);
Chris@76: }
Chris@76: // The file that was supposed to be deleted couldn't be found.
Chris@76: else
Chris@76: $failure = true;
Chris@76:
Chris@76: // Any other theme folders?
Chris@76: if (!empty($context['theme_copies']) && !empty($context['theme_copies'][$action['type']][$action['filename']]))
Chris@76: foreach ($context['theme_copies'][$action['type']][$action['filename']] as $theme_destination)
Chris@76: if (file_exists($theme_destination))
Chris@76: $failure |= !unlink($theme_destination);
Chris@76: else
Chris@76: $failure = true;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: return $not_done;
Chris@76: }
Chris@76:
Chris@76: // This function tries to match $version into any of the ranges given in $versions
Chris@76: function matchPackageVersion($version, $versions)
Chris@76: {
Chris@76: // Make sure everything is lowercase and clean of spaces and unpleasant history.
Chris@76: $version = str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($version));
Chris@76: $versions = explode(',', str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($versions)));
Chris@76:
Chris@76: // Perhaps we do accept anything?
Chris@76: if (in_array('all', $versions))
Chris@76: return true;
Chris@76:
Chris@76: // Loop through each version.
Chris@76: foreach ($versions as $for)
Chris@76: {
Chris@76: // Wild card spotted?
Chris@76: if (strpos($for, '*') !== false)
Chris@76: $for = str_replace('*', '0dev0', $for) . '-' . str_replace('*', '999', $for);
Chris@76:
Chris@76: // Do we have a range?
Chris@76: if (strpos($for, '-') !== false)
Chris@76: {
Chris@76: list ($lower, $upper) = explode('-', $for);
Chris@76:
Chris@76: // Compare the version against lower and upper bounds.
Chris@76: if (compareVersions($version, $lower) > -1 && compareVersions($version, $upper) < 1)
Chris@76: return true;
Chris@76: }
Chris@76: // Otherwise check if they are equal...
Chris@76: elseif (compareVersions($version, $for) === 0)
Chris@76: return true;
Chris@76: }
Chris@76:
Chris@76: return false;
Chris@76: }
Chris@76:
Chris@76: // The geek version of versioning checks for dummies, which basically compares two versions.
Chris@76: function compareVersions($version1, $version2)
Chris@76: {
Chris@76: static $categories;
Chris@76:
Chris@76: $versions = array();
Chris@76: foreach (array(1 => $version1, $version2) as $id => $version)
Chris@76: {
Chris@76: // Clean the version and extract the version parts.
Chris@76: $clean = str_replace(array(' ', '2.0rc1-1'), array('', '2.0rc1.1'), strtolower($version));
Chris@76: preg_match('~(\d+)(?:\.(\d+|))?(?:\.)?(\d+|)(?:(alpha|beta|rc)(\d+|)(?:\.)?(\d+|))?(?:(dev))?(\d+|)~', $clean, $parts);
Chris@76:
Chris@76: // Build an array of parts.
Chris@76: $versions[$id] = array(
Chris@76: 'major' => (int) $parts[1],
Chris@76: 'minor' => !empty($parts[2]) ? (int) $parts[2] : 0,
Chris@76: 'patch' => !empty($parts[3]) ? (int) $parts[3] : 0,
Chris@76: 'type' => empty($parts[4]) ? 'stable' : $parts[4],
Chris@76: 'type_major' => !empty($parts[6]) ? (int) $parts[5] : 0,
Chris@76: 'type_minor' => !empty($parts[6]) ? (int) $parts[6] : 0,
Chris@76: 'dev' => !empty($parts[7]),
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: // Are they the same, perhaps?
Chris@76: if ($versions[1] === $versions[2])
Chris@76: return 0;
Chris@76:
Chris@76: // Get version numbering categories...
Chris@76: if (!isset($categories))
Chris@76: $categories = array_keys($versions[1]);
Chris@76:
Chris@76: // Loop through each category.
Chris@76: foreach ($categories as $category)
Chris@76: {
Chris@76: // Is there something for us to calculate?
Chris@76: if ($versions[1][$category] !== $versions[2][$category])
Chris@76: {
Chris@76: // Dev builds are a problematic exception.
Chris@76: // (stable) dev < (stable) but (unstable) dev = (unstable)
Chris@76: if ($category == 'type')
Chris@76: return $versions[1][$category] > $versions[2][$category] ? ($versions[1]['dev'] ? -1 : 1) : ($versions[2]['dev'] ? 1 : -1);
Chris@76: elseif ($category == 'dev')
Chris@76: return $versions[1]['dev'] ? ($versions[2]['type'] == 'stable' ? -1 : 0) : ($versions[1]['type'] == 'stable' ? 1 : 0);
Chris@76: // Otherwise a simple comparison.
Chris@76: else
Chris@76: return $versions[1][$category] > $versions[2][$category] ? 1 : -1;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // They are the same!
Chris@76: return 0;
Chris@76: }
Chris@76:
Chris@76: function parse_path($path)
Chris@76: {
Chris@76: global $modSettings, $boarddir, $sourcedir, $settings, $temp_path;
Chris@76:
Chris@76: $dirs = array(
Chris@76: '\\' => '/',
Chris@76: '$boarddir' => $boarddir,
Chris@76: '$sourcedir' => $sourcedir,
Chris@76: '$avatardir' => $modSettings['avatar_directory'],
Chris@76: '$avatars_dir' => $modSettings['avatar_directory'],
Chris@76: '$themedir' => $settings['default_theme_dir'],
Chris@76: '$imagesdir' => $settings['default_theme_dir'] . '/' . basename($settings['default_images_url']),
Chris@76: '$themes_dir' => $boarddir . '/Themes',
Chris@76: '$languagedir' => $settings['default_theme_dir'] . '/languages',
Chris@76: '$languages_dir' => $settings['default_theme_dir'] . '/languages',
Chris@76: '$smileysdir' => $modSettings['smileys_dir'],
Chris@76: '$smileys_dir' => $modSettings['smileys_dir'],
Chris@76: );
Chris@76:
Chris@76: // do we parse in a package directory?
Chris@76: if (!empty($temp_path))
Chris@76: $dirs['$package'] = $temp_path;
Chris@76:
Chris@76: if (strlen($path) == 0)
Chris@76: trigger_error('parse_path(): There should never be an empty filename', E_USER_ERROR);
Chris@76:
Chris@76: return strtr($path, $dirs);
Chris@76: }
Chris@76:
Chris@76: function deltree($dir, $delete_dir = true)
Chris@76: {
Chris@76: global $package_ftp;
Chris@76:
Chris@76: if (!file_exists($dir))
Chris@76: return;
Chris@76:
Chris@76: $current_dir = @opendir($dir);
Chris@76: if ($current_dir == false)
Chris@76: {
Chris@76: if ($delete_dir && isset($package_ftp))
Chris@76: {
Chris@76: $ftp_file = strtr($dir, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76: if (!is_writable($dir . '/' . $entryname))
Chris@76: $package_ftp->chmod($ftp_file, 0777);
Chris@76: $package_ftp->unlink($ftp_file);
Chris@76: }
Chris@76:
Chris@76: return;
Chris@76: }
Chris@76:
Chris@76: while ($entryname = readdir($current_dir))
Chris@76: {
Chris@76: if (in_array($entryname, array('.', '..')))
Chris@76: continue;
Chris@76:
Chris@76: if (is_dir($dir . '/' . $entryname))
Chris@76: deltree($dir . '/' . $entryname);
Chris@76: else
Chris@76: {
Chris@76: // Here, 755 doesn't really matter since we're deleting it anyway.
Chris@76: if (isset($package_ftp))
Chris@76: {
Chris@76: $ftp_file = strtr($dir . '/' . $entryname, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76:
Chris@76: if (!is_writable($dir . '/' . $entryname))
Chris@76: $package_ftp->chmod($ftp_file, 0777);
Chris@76: $package_ftp->unlink($ftp_file);
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: if (!is_writable($dir . '/' . $entryname))
Chris@76: @chmod($dir . '/' . $entryname, 0777);
Chris@76: unlink($dir . '/' . $entryname);
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: closedir($current_dir);
Chris@76:
Chris@76: if ($delete_dir)
Chris@76: {
Chris@76: if (isset($package_ftp))
Chris@76: {
Chris@76: $ftp_file = strtr($dir, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76: if (!is_writable($dir . '/' . $entryname))
Chris@76: $package_ftp->chmod($ftp_file, 0777);
Chris@76: $package_ftp->unlink($ftp_file);
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: if (!is_writable($dir))
Chris@76: @chmod($dir, 0777);
Chris@76: @rmdir($dir);
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: function mktree($strPath, $mode)
Chris@76: {
Chris@76: global $package_ftp;
Chris@76:
Chris@76: if (is_dir($strPath))
Chris@76: {
Chris@76: if (!is_writable($strPath) && $mode !== false)
Chris@76: {
Chris@76: if (isset($package_ftp))
Chris@76: $package_ftp->chmod(strtr($strPath, array($_SESSION['pack_ftp']['root'] => '')), $mode);
Chris@76: else
Chris@76: @chmod($strPath, $mode);
Chris@76: }
Chris@76:
Chris@76: $test = @opendir($strPath);
Chris@76: if ($test)
Chris@76: {
Chris@76: closedir($test);
Chris@76: return is_writable($strPath);
Chris@76: }
Chris@76: else
Chris@76: return false;
Chris@76: }
Chris@76: // Is this an invalid path and/or we can't make the directory?
Chris@76: if ($strPath == dirname($strPath) || !mktree(dirname($strPath), $mode))
Chris@76: return false;
Chris@76:
Chris@76: if (!is_writable(dirname($strPath)) && $mode !== false)
Chris@76: {
Chris@76: if (isset($package_ftp))
Chris@76: $package_ftp->chmod(dirname(strtr($strPath, array($_SESSION['pack_ftp']['root'] => ''))), $mode);
Chris@76: else
Chris@76: @chmod(dirname($strPath), $mode);
Chris@76: }
Chris@76:
Chris@76: if ($mode !== false && isset($package_ftp))
Chris@76: return $package_ftp->create_dir(strtr($strPath, array($_SESSION['pack_ftp']['root'] => '')));
Chris@76: elseif ($mode === false)
Chris@76: {
Chris@76: $test = @opendir(dirname($strPath));
Chris@76: if ($test)
Chris@76: {
Chris@76: closedir($test);
Chris@76: return true;
Chris@76: }
Chris@76: else
Chris@76: return false;
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: @mkdir($strPath, $mode);
Chris@76: $test = @opendir($strPath);
Chris@76: if ($test)
Chris@76: {
Chris@76: closedir($test);
Chris@76: return true;
Chris@76: }
Chris@76: else
Chris@76: return false;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: function copytree($source, $destination)
Chris@76: {
Chris@76: global $package_ftp;
Chris@76:
Chris@76: if (!file_exists($destination) || !is_writable($destination))
Chris@76: mktree($destination, 0755);
Chris@76: if (!is_writable($destination))
Chris@76: mktree($destination, 0777);
Chris@76:
Chris@76: $current_dir = opendir($source);
Chris@76: if ($current_dir == false)
Chris@76: return;
Chris@76:
Chris@76: while ($entryname = readdir($current_dir))
Chris@76: {
Chris@76: if (in_array($entryname, array('.', '..')))
Chris@76: continue;
Chris@76:
Chris@76: if (isset($package_ftp))
Chris@76: $ftp_file = strtr($destination . '/' . $entryname, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76:
Chris@76: if (is_file($source . '/' . $entryname))
Chris@76: {
Chris@76: if (isset($package_ftp) && !file_exists($destination . '/' . $entryname))
Chris@76: $package_ftp->create_file($ftp_file);
Chris@76: elseif (!file_exists($destination . '/' . $entryname))
Chris@76: @touch($destination . '/' . $entryname);
Chris@76: }
Chris@76:
Chris@76: package_chmod($destination . '/' . $entryname);
Chris@76:
Chris@76: if (is_dir($source . '/' . $entryname))
Chris@76: copytree($source . '/' . $entryname, $destination . '/' . $entryname);
Chris@76: elseif (file_exists($destination . '/' . $entryname))
Chris@76: package_put_contents($destination . '/' . $entryname, package_get_contents($source . '/' . $entryname));
Chris@76: else
Chris@76: copy($source . '/' . $entryname, $destination . '/' . $entryname);
Chris@76: }
Chris@76:
Chris@76: closedir($current_dir);
Chris@76: }
Chris@76:
Chris@76: function listtree($path, $sub_path = '')
Chris@76: {
Chris@76: $data = array();
Chris@76:
Chris@76: $dir = @dir($path . $sub_path);
Chris@76: if (!$dir)
Chris@76: return array();
Chris@76: while ($entry = $dir->read())
Chris@76: {
Chris@76: if ($entry == '.' || $entry == '..')
Chris@76: continue;
Chris@76:
Chris@76: if (is_dir($path . $sub_path . '/' . $entry))
Chris@76: $data = array_merge($data, listtree($path, $sub_path . '/' . $entry));
Chris@76: else
Chris@76: $data[] = array(
Chris@76: 'filename' => $sub_path == '' ? $entry : $sub_path . '/' . $entry,
Chris@76: 'size' => filesize($path . $sub_path . '/' . $entry),
Chris@76: 'skipped' => false,
Chris@76: );
Chris@76: }
Chris@76: $dir->close();
Chris@76:
Chris@76: return $data;
Chris@76: }
Chris@76:
Chris@76: // Parse an xml based modification file.
Chris@76: function parseModification($file, $testing = true, $undo = false, $theme_paths = array())
Chris@76: {
Chris@76: global $boarddir, $sourcedir, $settings, $txt, $modSettings, $package_ftp;
Chris@76:
Chris@76: @set_time_limit(600);
Chris@76: loadClassFile('Class-Package.php');
Chris@76: $xml = new xmlArray(strtr($file, array("\r" => '')));
Chris@76: $actions = array();
Chris@76: $everything_found = true;
Chris@76:
Chris@76: if (!$xml->exists('modification') || !$xml->exists('modification/file'))
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'error',
Chris@76: 'filename' => '-',
Chris@76: 'debug' => $txt['package_modification_malformed']
Chris@76: );
Chris@76: return $actions;
Chris@76: }
Chris@76:
Chris@76: // Get the XML data.
Chris@76: $files = $xml->set('modification/file');
Chris@76:
Chris@76: // Use this for holding all the template changes in this mod.
Chris@76: $template_changes = array();
Chris@76: // This is needed to hold the long paths, as they can vary...
Chris@76: $long_changes = array();
Chris@76:
Chris@76: // First, we need to build the list of all the files likely to get changed.
Chris@76: foreach ($files as $file)
Chris@76: {
Chris@76: // What is the filename we're currently on?
Chris@76: $filename = parse_path(trim($file->fetch('@name')));
Chris@76:
Chris@76: // Now, we need to work out whether this is even a template file...
Chris@76: foreach ($theme_paths as $id => $theme)
Chris@76: {
Chris@76: // If this filename is relative, if so take a guess at what it should be.
Chris@76: $real_filename = $filename;
Chris@76: if (strpos($filename, 'Themes') === 0)
Chris@76: $real_filename = $boarddir . '/' . $filename;
Chris@76:
Chris@76: if (strpos($real_filename, $theme['theme_dir']) === 0)
Chris@76: {
Chris@76: $template_changes[$id][] = substr($real_filename, strlen($theme['theme_dir']) + 1);
Chris@76: $long_changes[$id][] = $filename;
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Custom themes to add.
Chris@76: $custom_themes_add = array();
Chris@76:
Chris@76: // If we have some template changes, we need to build a master link of what new ones are required for the custom themes.
Chris@76: if (!empty($template_changes[1]))
Chris@76: {
Chris@76: foreach ($theme_paths as $id => $theme)
Chris@76: {
Chris@76: // Default is getting done anyway, so no need for involvement here.
Chris@76: if ($id == 1)
Chris@76: continue;
Chris@76:
Chris@76: // For every template, do we want it? Yea, no, maybe?
Chris@76: foreach ($template_changes[1] as $index => $template_file)
Chris@76: {
Chris@76: // What, it exists and we haven't already got it?! Lordy, get it in!
Chris@76: if (file_exists($theme['theme_dir'] . '/' . $template_file) && (!isset($template_changes[$id]) || !in_array($template_file, $template_changes[$id])))
Chris@76: {
Chris@76: // Now let's add it to the "todo" list.
Chris@76: $custom_themes_add[$long_changes[1][$index]][$id] = $theme['theme_dir'] . '/' . $template_file;
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: foreach ($files as $file)
Chris@76: {
Chris@76: // This is the actual file referred to in the XML document...
Chris@76: $files_to_change = array(
Chris@76: 1 => parse_path(trim($file->fetch('@name'))),
Chris@76: );
Chris@76:
Chris@76: // Sometimes though, we have some additional files for other themes, if we have add them to the mix.
Chris@76: if (isset($custom_themes_add[$files_to_change[1]]))
Chris@76: $files_to_change += $custom_themes_add[$files_to_change[1]];
Chris@76:
Chris@76: // Now, loop through all the files we're changing, and, well, change them ;)
Chris@76: foreach ($files_to_change as $theme => $working_file)
Chris@76: {
Chris@76: if ($working_file[0] != '/' && $working_file[1] != ':')
Chris@76: {
Chris@76: trigger_error('parseModification(): The filename \'' . $working_file . '\' is not a full path!', E_USER_WARNING);
Chris@76:
Chris@76: $working_file = $boarddir . '/' . $working_file;
Chris@76: }
Chris@76:
Chris@76: // Doesn't exist - give an error or what?
Chris@76: if (!file_exists($working_file) && (!$file->exists('@error') || !in_array(trim($file->fetch('@error')), array('ignore', 'skip'))))
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'missing',
Chris@76: 'filename' => $working_file,
Chris@76: 'debug' => $txt['package_modification_missing']
Chris@76: );
Chris@76:
Chris@76: $everything_found = false;
Chris@76: continue;
Chris@76: }
Chris@76: // Skip the file if it doesn't exist.
Chris@76: elseif (!file_exists($working_file) && $file->exists('@error') && trim($file->fetch('@error')) == 'skip')
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'skipping',
Chris@76: 'filename' => $working_file,
Chris@76: );
Chris@76: continue;
Chris@76: }
Chris@76: // Okay, we're creating this file then...?
Chris@76: elseif (!file_exists($working_file))
Chris@76: $working_data = '';
Chris@76: // Phew, it exists! Load 'er up!
Chris@76: else
Chris@76: $working_data = str_replace("\r", '', package_get_contents($working_file));
Chris@76:
Chris@76: $actions[] = array(
Chris@76: 'type' => 'opened',
Chris@76: 'filename' => $working_file
Chris@76: );
Chris@76:
Chris@76: $operations = $file->exists('operation') ? $file->set('operation') : array();
Chris@76: foreach ($operations as $operation)
Chris@76: {
Chris@76: // Convert operation to an array.
Chris@76: $actual_operation = array(
Chris@76: 'searches' => array(),
Chris@76: 'error' => $operation->exists('@error') && in_array(trim($operation->fetch('@error')), array('ignore', 'fatal', 'required')) ? trim($operation->fetch('@error')) : 'fatal',
Chris@76: );
Chris@76:
Chris@76: // The 'add' parameter is used for all searches in this operation.
Chris@76: $add = $operation->exists('add') ? $operation->fetch('add') : '';
Chris@76:
Chris@76: // Grab all search items of this operation (in most cases just 1).
Chris@76: $searches = $operation->set('search');
Chris@76: foreach ($searches as $i => $search)
Chris@76: $actual_operation['searches'][] = array(
Chris@76: 'position' => $search->exists('@position') && in_array(trim($search->fetch('@position')), array('before', 'after', 'replace', 'end')) ? trim($search->fetch('@position')) : 'replace',
Chris@76: 'is_reg_exp' => $search->exists('@regexp') && trim($search->fetch('@regexp')) === 'true',
Chris@76: 'loose_whitespace' => $search->exists('@whitespace') && trim($search->fetch('@whitespace')) === 'loose',
Chris@76: 'search' => $search->fetch('.'),
Chris@76: 'add' => $add,
Chris@76: 'preg_search' => '',
Chris@76: 'preg_replace' => '',
Chris@76: );
Chris@76:
Chris@76: // At least one search should be defined.
Chris@76: if (empty($actual_operation['searches']))
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'failure',
Chris@76: 'filename' => $working_file,
Chris@76: 'search' => $search['search'],
Chris@76: 'is_custom' => $theme > 1 ? $theme : 0,
Chris@76: );
Chris@76:
Chris@76: // Skip to the next operation.
Chris@76: continue;
Chris@76: }
Chris@76:
Chris@76: // Reverse the operations in case of undoing stuff.
Chris@76: if ($undo)
Chris@76: {
Chris@76: foreach ($actual_operation['searches'] as $i => $search)
Chris@76: {
Chris@76:
Chris@76: // Reverse modification of regular expressions are not allowed.
Chris@76: if ($search['is_reg_exp'])
Chris@76: {
Chris@76: if ($actual_operation['error'] === 'fatal')
Chris@76: $actions[] = array(
Chris@76: 'type' => 'failure',
Chris@76: 'filename' => $working_file,
Chris@76: 'search' => $search['search'],
Chris@76: 'is_custom' => $theme > 1 ? $theme : 0,
Chris@76: );
Chris@76:
Chris@76: // Continue to the next operation.
Chris@76: continue 2;
Chris@76: }
Chris@76:
Chris@76: // The replacement is now the search subject...
Chris@76: if ($search['position'] === 'replace' || $search['position'] === 'end')
Chris@76: $actual_operation['searches'][$i]['search'] = $search['add'];
Chris@76: else
Chris@76: {
Chris@76: // Reversing a before/after modification becomes a replacement.
Chris@76: $actual_operation['searches'][$i]['position'] = 'replace';
Chris@76:
Chris@76: if ($search['position'] === 'before')
Chris@76: $actual_operation['searches'][$i]['search'] .= $search['add'];
Chris@76: elseif ($search['position'] === 'after')
Chris@76: $actual_operation['searches'][$i]['search'] = $search['add'] . $search['search'];
Chris@76: }
Chris@76:
Chris@76: // ...and the search subject is now the replacement.
Chris@76: $actual_operation['searches'][$i]['add'] = $search['search'];
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Sort the search list so the replaces come before the add before/after's.
Chris@76: if (count($actual_operation['searches']) !== 1)
Chris@76: {
Chris@76: $replacements = array();
Chris@76:
Chris@76: foreach ($actual_operation['searches'] as $i => $search)
Chris@76: {
Chris@76: if ($search['position'] === 'replace')
Chris@76: {
Chris@76: $replacements[] = $search;
Chris@76: unset($actual_operation['searches'][$i]);
Chris@76: }
Chris@76: }
Chris@76: $actual_operation['searches'] = array_merge($replacements, $actual_operation['searches']);
Chris@76: }
Chris@76:
Chris@76: // Create regular expression replacements from each search.
Chris@76: foreach ($actual_operation['searches'] as $i => $search)
Chris@76: {
Chris@76: // Not much needed if the search subject is already a regexp.
Chris@76: if ($search['is_reg_exp'])
Chris@76: $actual_operation['searches'][$i]['preg_search'] = $search['search'];
Chris@76: else
Chris@76: {
Chris@76: // Make the search subject fit into a regular expression.
Chris@76: $actual_operation['searches'][$i]['preg_search'] = preg_quote($search['search'], '~');
Chris@76:
Chris@76: // Using 'loose', a random amount of tabs and spaces may be used.
Chris@76: if ($search['loose_whitespace'])
Chris@76: $actual_operation['searches'][$i]['preg_search'] = preg_replace('~[ \t]+~', '[ \t]+', $actual_operation['searches'][$i]['preg_search']);
Chris@76: }
Chris@76:
Chris@76: // Shuzzup. This is done so we can safely use a regular expression. ($0 is bad!!)
Chris@76: $actual_operation['searches'][$i]['preg_replace'] = strtr($search['add'], array('$' => '[$PACK' . 'AGE1$]', '\\' => '[$PACK' . 'AGE2$]'));
Chris@76:
Chris@76: // Before, so the replacement comes after the search subject :P
Chris@76: if ($search['position'] === 'before')
Chris@76: {
Chris@76: $actual_operation['searches'][$i]['preg_search'] = '(' . $actual_operation['searches'][$i]['preg_search'] . ')';
Chris@76: $actual_operation['searches'][$i]['preg_replace'] = '$1' . $actual_operation['searches'][$i]['preg_replace'];
Chris@76: }
Chris@76:
Chris@76: // After, after what?
Chris@76: elseif ($search['position'] === 'after')
Chris@76: {
Chris@76: $actual_operation['searches'][$i]['preg_search'] = '(' . $actual_operation['searches'][$i]['preg_search'] . ')';
Chris@76: $actual_operation['searches'][$i]['preg_replace'] .= '$1';
Chris@76: }
Chris@76:
Chris@76: // Position the replacement at the end of the file (or just before the closing PHP tags).
Chris@76: elseif ($search['position'] === 'end')
Chris@76: {
Chris@76: if ($undo)
Chris@76: {
Chris@76: $actual_operation['searches'][$i]['preg_replace'] = '';
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: $actual_operation['searches'][$i]['preg_search'] = '(\\n\\?\\>)?$';
Chris@76: $actual_operation['searches'][$i]['preg_replace'] .= '$1';
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Testing 1, 2, 3...
Chris@76: $failed = preg_match('~' . $actual_operation['searches'][$i]['preg_search'] . '~s', $working_data) === 0;
Chris@76:
Chris@76: // Nope, search pattern not found.
Chris@76: if ($failed && $actual_operation['error'] === 'fatal')
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'failure',
Chris@76: 'filename' => $working_file,
Chris@76: 'search' => $actual_operation['searches'][$i]['preg_search'],
Chris@76: 'search_original' => $actual_operation['searches'][$i]['search'],
Chris@76: 'replace_original' => $actual_operation['searches'][$i]['add'],
Chris@76: 'position' => $search['position'],
Chris@76: 'is_custom' => $theme > 1 ? $theme : 0,
Chris@76: 'failed' => $failed,
Chris@76: );
Chris@76:
Chris@76: $everything_found = false;
Chris@76: continue;
Chris@76: }
Chris@76:
Chris@76: // Found, but in this case, that means failure!
Chris@76: elseif (!$failed && $actual_operation['error'] === 'required')
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'failure',
Chris@76: 'filename' => $working_file,
Chris@76: 'search' => $actual_operation['searches'][$i]['preg_search'],
Chris@76: 'search_original' => $actual_operation['searches'][$i]['search'],
Chris@76: 'replace_original' => $actual_operation['searches'][$i]['add'],
Chris@76: 'position' => $search['position'],
Chris@76: 'is_custom' => $theme > 1 ? $theme : 0,
Chris@76: 'failed' => $failed,
Chris@76: );
Chris@76:
Chris@76: $everything_found = false;
Chris@76: continue;
Chris@76: }
Chris@76:
Chris@76: // Replace it into nothing? That's not an option...unless it's an undoing end.
Chris@76: if ($search['add'] === '' && ($search['position'] !== 'end' || !$undo))
Chris@76: continue;
Chris@76:
Chris@76: // Finally, we're doing some replacements.
Chris@76: $working_data = preg_replace('~' . $actual_operation['searches'][$i]['preg_search'] . '~s', $actual_operation['searches'][$i]['preg_replace'], $working_data, 1);
Chris@76:
Chris@76: $actions[] = array(
Chris@76: 'type' => 'replace',
Chris@76: 'filename' => $working_file,
Chris@76: 'search' => $actual_operation['searches'][$i]['preg_search'],
Chris@76: 'replace' => $actual_operation['searches'][$i]['preg_replace'],
Chris@76: 'search_original' => $actual_operation['searches'][$i]['search'],
Chris@76: 'replace_original' => $actual_operation['searches'][$i]['add'],
Chris@76: 'position' => $search['position'],
Chris@76: 'failed' => $failed,
Chris@76: 'ignore_failure' => $failed && $actual_operation['error'] === 'ignore',
Chris@76: 'is_custom' => $theme > 1 ? $theme : 0,
Chris@76: );
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Fix any little helper symbols ;).
Chris@76: $working_data = strtr($working_data, array('[$PACK' . 'AGE1$]' => '$', '[$PACK' . 'AGE2$]' => '\\'));
Chris@76:
Chris@76: package_chmod($working_file);
Chris@76:
Chris@76: if ((file_exists($working_file) && !is_writable($working_file)) || (!file_exists($working_file) && !is_writable(dirname($working_file))))
Chris@76: $actions[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $working_file
Chris@76: );
Chris@76:
Chris@76: if (basename($working_file) == 'Settings_bak.php')
Chris@76: continue;
Chris@76:
Chris@76: if (!$testing && !empty($modSettings['package_make_backups']) && file_exists($working_file))
Chris@76: {
Chris@76: // No, no, not Settings.php!
Chris@76: if (basename($working_file) == 'Settings.php')
Chris@76: @copy($working_file, dirname($working_file) . '/Settings_bak.php');
Chris@76: else
Chris@76: @copy($working_file, $working_file . '~');
Chris@76: }
Chris@76:
Chris@76: // Always call this, even if in testing, because it won't really be written in testing mode.
Chris@76: package_put_contents($working_file, $working_data, $testing);
Chris@76:
Chris@76: $actions[] = array(
Chris@76: 'type' => 'saved',
Chris@76: 'filename' => $working_file,
Chris@76: 'is_custom' => $theme > 1 ? $theme : 0,
Chris@76: );
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: $actions[] = array(
Chris@76: 'type' => 'result',
Chris@76: 'status' => $everything_found
Chris@76: );
Chris@76:
Chris@76: return $actions;
Chris@76: }
Chris@76:
Chris@76: // Parses a BoardMod format mod file...
Chris@76: function parseBoardMod($file, $testing = true, $undo = false, $theme_paths = array())
Chris@76: {
Chris@76: global $boarddir, $sourcedir, $settings, $txt, $modSettings;
Chris@76:
Chris@76: @set_time_limit(600);
Chris@76: $file = strtr($file, array("\r" => ''));
Chris@76:
Chris@76: $working_file = null;
Chris@76: $working_search = null;
Chris@76: $working_data = '';
Chris@76: $replace_with = null;
Chris@76:
Chris@76: $actions = array();
Chris@76: $everything_found = true;
Chris@76:
Chris@76: // This holds all the template changes in the standard mod file.
Chris@76: $template_changes = array();
Chris@76: // This is just the temporary file.
Chris@76: $temp_file = $file;
Chris@76: // This holds the actual changes on a step counter basis.
Chris@76: $temp_changes = array();
Chris@76: $counter = 0;
Chris@76: $step_counter = 0;
Chris@76:
Chris@76: // Before we do *anything*, let's build a list of what we're editing, as it's going to be used for other theme edits.
Chris@76: while (preg_match('~<(edit file|file|search|search for|add|add after|replace|add before|add above|above|before)>\n(.*?)\n\\1>~is', $temp_file, $code_match) != 0)
Chris@76: {
Chris@76: $counter++;
Chris@76:
Chris@76: // Get rid of the old stuff.
Chris@76: $temp_file = substr_replace($temp_file, '', strpos($temp_file, $code_match[0]), strlen($code_match[0]));
Chris@76:
Chris@76: // No interest to us?
Chris@76: if ($code_match[1] != 'edit file' && $code_match[1] != 'file')
Chris@76: {
Chris@76: // It's a step, let's add that to the current steps.
Chris@76: if (isset($temp_changes[$step_counter]))
Chris@76: $temp_changes[$step_counter]['changes'][] = $code_match[0];
Chris@76: continue;
Chris@76: }
Chris@76:
Chris@76: // We've found a new edit - let's make ourself heard, kind of.
Chris@76: $step_counter = $counter;
Chris@76: $temp_changes[$step_counter] = array(
Chris@76: 'title' => $code_match[0],
Chris@76: 'changes' => array(),
Chris@76: );
Chris@76:
Chris@76: $filename = parse_path($code_match[2]);
Chris@76:
Chris@76: // Now, is this a template file, and if so, which?
Chris@76: foreach ($theme_paths as $id => $theme)
Chris@76: {
Chris@76: // If this filename is relative, if so take a guess at what it should be.
Chris@76: if (strpos($filename, 'Themes') === 0)
Chris@76: $filename = $boarddir . '/' . $filename;
Chris@76:
Chris@76: if (strpos($filename, $theme['theme_dir']) === 0)
Chris@76: $template_changes[$id][$counter] = substr($filename, strlen($theme['theme_dir']) + 1);
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Anything above $counter must be for custom themes.
Chris@76: $custom_template_begin = $counter;
Chris@76: // Reference for what theme ID this action belongs to.
Chris@76: $theme_id_ref = array();
Chris@76:
Chris@76: // Now we know what templates we need to touch, cycle through each theme and work out what we need to edit.
Chris@76: if (!empty($template_changes[1]))
Chris@76: {
Chris@76: foreach ($theme_paths as $id => $theme)
Chris@76: {
Chris@76: // Don't do default, it means nothing to me.
Chris@76: if ($id == 1)
Chris@76: continue;
Chris@76:
Chris@76: // Now, for each file do we need to edit it?
Chris@76: foreach ($template_changes[1] as $pos => $template_file)
Chris@76: {
Chris@76: // It does? Add it to the list darlin'.
Chris@76: if (file_exists($theme['theme_dir'] . '/' . $template_file) && (!isset($template_changes[$id][$pos]) || !in_array($template_file, $template_changes[$id][$pos])))
Chris@76: {
Chris@76: // Actually add it to the mod file too, so we can see that it will work ;)
Chris@76: if (!empty($temp_changes[$pos]['changes']))
Chris@76: {
Chris@76: $file .= "\n\n" . '' . "\n" . $theme['theme_dir'] . '/' . $template_file . "\n" . '' . "\n\n" . implode("\n\n", $temp_changes[$pos]['changes']);
Chris@76: $theme_id_ref[$counter] = $id;
Chris@76: $counter += 1 + count($temp_changes[$pos]['changes']);
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: $counter = 0;
Chris@76: $is_custom = 0;
Chris@76: while (preg_match('~<(edit file|file|search|search for|add|add after|replace|add before|add above|above|before)>\n(.*?)\n\\1>~is', $file, $code_match) != 0)
Chris@76: {
Chris@76: // This is for working out what we should be editing.
Chris@76: $counter++;
Chris@76:
Chris@76: // Edit a specific file.
Chris@76: if ($code_match[1] == 'file' || $code_match[1] == 'edit file')
Chris@76: {
Chris@76: // Backup the old file.
Chris@76: if ($working_file !== null)
Chris@76: {
Chris@76: package_chmod($working_file);
Chris@76:
Chris@76: // Don't even dare.
Chris@76: if (basename($working_file) == 'Settings_bak.php')
Chris@76: continue;
Chris@76:
Chris@76: if (!is_writable($working_file))
Chris@76: $actions[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $working_file
Chris@76: );
Chris@76:
Chris@76: if (!$testing && !empty($modSettings['package_make_backups']) && file_exists($working_file))
Chris@76: {
Chris@76: if (basename($working_file) == 'Settings.php')
Chris@76: @copy($working_file, dirname($working_file) . '/Settings_bak.php');
Chris@76: else
Chris@76: @copy($working_file, $working_file . '~');
Chris@76: }
Chris@76:
Chris@76: package_put_contents($working_file, $working_data, $testing);
Chris@76: }
Chris@76:
Chris@76: if ($working_file !== null)
Chris@76: $actions[] = array(
Chris@76: 'type' => 'saved',
Chris@76: 'filename' => $working_file,
Chris@76: 'is_custom' => $is_custom,
Chris@76: );
Chris@76:
Chris@76: // Is this "now working on" file a theme specific one?
Chris@76: $is_custom = isset($theme_id_ref[$counter - 1]) ? $theme_id_ref[$counter - 1] : 0;
Chris@76:
Chris@76: // Make sure the file exists!
Chris@76: $working_file = parse_path($code_match[2]);
Chris@76:
Chris@76: if ($working_file[0] != '/' && $working_file[1] != ':')
Chris@76: {
Chris@76: trigger_error('parseBoardMod(): The filename \'' . $working_file . '\' is not a full path!', E_USER_WARNING);
Chris@76:
Chris@76: $working_file = $boarddir . '/' . $working_file;
Chris@76: }
Chris@76:
Chris@76: if (!file_exists($working_file))
Chris@76: {
Chris@76: $places_to_check = array($boarddir, $sourcedir, $settings['default_theme_dir'], $settings['default_theme_dir'] . '/languages');
Chris@76:
Chris@76: foreach ($places_to_check as $place)
Chris@76: if (file_exists($place . '/' . $working_file))
Chris@76: {
Chris@76: $working_file = $place . '/' . $working_file;
Chris@76: break;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: if (file_exists($working_file))
Chris@76: {
Chris@76: // Load the new file.
Chris@76: $working_data = str_replace("\r", '', package_get_contents($working_file));
Chris@76:
Chris@76: $actions[] = array(
Chris@76: 'type' => 'opened',
Chris@76: 'filename' => $working_file
Chris@76: );
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'missing',
Chris@76: 'filename' => $working_file
Chris@76: );
Chris@76:
Chris@76: $working_file = null;
Chris@76: $everything_found = false;
Chris@76: }
Chris@76:
Chris@76: // Can't be searching for something...
Chris@76: $working_search = null;
Chris@76: }
Chris@76: // Search for a specific string.
Chris@76: elseif (($code_match[1] == 'search' || $code_match[1] == 'search for') && $working_file !== null)
Chris@76: {
Chris@76: if ($working_search !== null)
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'error',
Chris@76: 'filename' => $working_file
Chris@76: );
Chris@76:
Chris@76: $everything_found = false;
Chris@76: }
Chris@76:
Chris@76: $working_search = $code_match[2];
Chris@76: }
Chris@76: // Must've already loaded a search string.
Chris@76: elseif ($working_search !== null)
Chris@76: {
Chris@76: // This is the base string....
Chris@76: $replace_with = $code_match[2];
Chris@76:
Chris@76: // Add this afterward...
Chris@76: if ($code_match[1] == 'add' || $code_match[1] == 'add after')
Chris@76: $replace_with = $working_search . "\n" . $replace_with;
Chris@76: // Add this beforehand.
Chris@76: elseif ($code_match[1] == 'before' || $code_match[1] == 'add before' || $code_match[1] == 'above' || $code_match[1] == 'add above')
Chris@76: $replace_with .= "\n" . $working_search;
Chris@76: // Otherwise.. replace with $replace_with ;).
Chris@76: }
Chris@76:
Chris@76: // If we have a search string, replace string, and open file..
Chris@76: if ($working_search !== null && $replace_with !== null && $working_file !== null)
Chris@76: {
Chris@76: // Make sure it's somewhere in the string.
Chris@76: if ($undo)
Chris@76: {
Chris@76: $temp = $replace_with;
Chris@76: $replace_with = $working_search;
Chris@76: $working_search = $temp;
Chris@76: }
Chris@76:
Chris@76: if (strpos($working_data, $working_search) !== false)
Chris@76: {
Chris@76: $working_data = str_replace($working_search, $replace_with, $working_data);
Chris@76:
Chris@76: $actions[] = array(
Chris@76: 'type' => 'replace',
Chris@76: 'filename' => $working_file,
Chris@76: 'search' => $working_search,
Chris@76: 'replace' => $replace_with,
Chris@76: 'search_original' => $working_search,
Chris@76: 'replace_original' => $replace_with,
Chris@76: 'position' => $code_match[1] == 'replace' ? 'replace' : ($code_match[1] == 'add' || $code_match[1] == 'add after' ? 'before' : 'after'),
Chris@76: 'is_custom' => $is_custom,
Chris@76: 'failed' => false,
Chris@76: );
Chris@76: }
Chris@76: // It wasn't found!
Chris@76: else
Chris@76: {
Chris@76: $actions[] = array(
Chris@76: 'type' => 'failure',
Chris@76: 'filename' => $working_file,
Chris@76: 'search' => $working_search,
Chris@76: 'is_custom' => $is_custom,
Chris@76: 'search_original' => $working_search,
Chris@76: 'replace_original' => $replace_with,
Chris@76: 'position' => $code_match[1] == 'replace' ? 'replace' : ($code_match[1] == 'add' || $code_match[1] == 'add after' ? 'before' : 'after'),
Chris@76: 'is_custom' => $is_custom,
Chris@76: 'failed' => true,
Chris@76: );
Chris@76:
Chris@76: $everything_found = false;
Chris@76: }
Chris@76:
Chris@76: // These don't hold any meaning now.
Chris@76: $working_search = null;
Chris@76: $replace_with = null;
Chris@76: }
Chris@76:
Chris@76: // Get rid of the old tag.
Chris@76: $file = substr_replace($file, '', strpos($file, $code_match[0]), strlen($code_match[0]));
Chris@76: }
Chris@76:
Chris@76: // Backup the old file.
Chris@76: if ($working_file !== null)
Chris@76: {
Chris@76: package_chmod($working_file);
Chris@76:
Chris@76: if (!is_writable($working_file))
Chris@76: $actions[] = array(
Chris@76: 'type' => 'chmod',
Chris@76: 'filename' => $working_file
Chris@76: );
Chris@76:
Chris@76: if (!$testing && !empty($modSettings['package_make_backups']) && file_exists($working_file))
Chris@76: {
Chris@76: if (basename($working_file) == 'Settings.php')
Chris@76: @copy($working_file, dirname($working_file) . '/Settings_bak.php');
Chris@76: else
Chris@76: @copy($working_file, $working_file . '~');
Chris@76: }
Chris@76:
Chris@76: package_put_contents($working_file, $working_data, $testing);
Chris@76: }
Chris@76:
Chris@76: if ($working_file !== null)
Chris@76: $actions[] = array(
Chris@76: 'type' => 'saved',
Chris@76: 'filename' => $working_file,
Chris@76: 'is_custom' => $is_custom,
Chris@76: );
Chris@76:
Chris@76: $actions[] = array(
Chris@76: 'type' => 'result',
Chris@76: 'status' => $everything_found
Chris@76: );
Chris@76:
Chris@76: return $actions;
Chris@76: }
Chris@76:
Chris@76: function package_get_contents($filename)
Chris@76: {
Chris@76: global $package_cache, $modSettings;
Chris@76:
Chris@76: if (!isset($package_cache))
Chris@76: {
Chris@76: // Windows doesn't seem to care about the memory_limit.
Chris@76: if (!empty($modSettings['package_disable_cache']) || ini_set('memory_limit', '128M') !== false || strpos(strtolower(PHP_OS), 'win') !== false)
Chris@76: $package_cache = array();
Chris@76: else
Chris@76: $package_cache = false;
Chris@76: }
Chris@76:
Chris@76: if (strpos($filename, 'Packages/') !== false || $package_cache === false || !isset($package_cache[$filename]))
Chris@76: return file_get_contents($filename);
Chris@76: else
Chris@76: return $package_cache[$filename];
Chris@76: }
Chris@76:
Chris@76: function package_put_contents($filename, $data, $testing = false)
Chris@76: {
Chris@76: global $package_ftp, $package_cache, $modSettings;
Chris@76: static $text_filetypes = array('php', 'txt', '.js', 'css', 'vbs', 'tml', 'htm');
Chris@76:
Chris@76: if (!isset($package_cache))
Chris@76: {
Chris@76: // Try to increase the memory limit - we don't want to run out of ram!
Chris@76: if (!empty($modSettings['package_disable_cache']) || ini_set('memory_limit', '128M') !== false || strpos(strtolower(PHP_OS), 'win') !== false)
Chris@76: $package_cache = array();
Chris@76: else
Chris@76: $package_cache = false;
Chris@76: }
Chris@76:
Chris@76: if (isset($package_ftp))
Chris@76: $ftp_file = strtr($filename, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76:
Chris@76: if (!file_exists($filename) && isset($package_ftp))
Chris@76: $package_ftp->create_file($ftp_file);
Chris@76: elseif (!file_exists($filename))
Chris@76: @touch($filename);
Chris@76:
Chris@76: package_chmod($filename);
Chris@76:
Chris@76: if (!$testing && (strpos($filename, 'Packages/') !== false || $package_cache === false))
Chris@76: {
Chris@76: $fp = @fopen($filename, in_array(substr($filename, -3), $text_filetypes) ? 'w' : 'wb');
Chris@76:
Chris@76: // We should show an error message or attempt a rollback, no?
Chris@76: if (!$fp)
Chris@76: return false;
Chris@76:
Chris@76: fwrite($fp, $data);
Chris@76: fclose($fp);
Chris@76: }
Chris@76: elseif (strpos($filename, 'Packages/') !== false || $package_cache === false)
Chris@76: return strlen($data);
Chris@76: else
Chris@76: {
Chris@76: $package_cache[$filename] = $data;
Chris@76:
Chris@76: // Permission denied, eh?
Chris@76: $fp = @fopen($filename, 'r+');
Chris@76: if (!$fp)
Chris@76: return false;
Chris@76: fclose($fp);
Chris@76: }
Chris@76:
Chris@76: return strlen($data);
Chris@76: }
Chris@76:
Chris@76: function package_flush_cache($trash = false)
Chris@76: {
Chris@76: global $package_ftp, $package_cache;
Chris@76: static $text_filetypes = array('php', 'txt', '.js', 'css', 'vbs', 'tml', 'htm');
Chris@76:
Chris@76: if (empty($package_cache))
Chris@76: return;
Chris@76:
Chris@76: // First, let's check permissions!
Chris@76: foreach ($package_cache as $filename => $data)
Chris@76: {
Chris@76: if (isset($package_ftp))
Chris@76: $ftp_file = strtr($filename, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76:
Chris@76: if (!file_exists($filename) && isset($package_ftp))
Chris@76: $package_ftp->create_file($ftp_file);
Chris@76: elseif (!file_exists($filename))
Chris@76: @touch($filename);
Chris@76:
Chris@76: package_chmod($filename);
Chris@76:
Chris@76: $fp = fopen($filename, 'r+');
Chris@76: if (!$fp && !$trash)
Chris@76: {
Chris@76: // We should have package_chmod()'d them before, no?!
Chris@76: trigger_error('package_flush_cache(): some files are still not writable', E_USER_WARNING);
Chris@76: return;
Chris@76: }
Chris@76: fclose($fp);
Chris@76: }
Chris@76:
Chris@76: if ($trash)
Chris@76: {
Chris@76: $package_cache = array();
Chris@76: return;
Chris@76: }
Chris@76:
Chris@76: foreach ($package_cache as $filename => $data)
Chris@76: {
Chris@76: $fp = fopen($filename, in_array(substr($filename, -3), $text_filetypes) ? 'w' : 'wb');
Chris@76: fwrite($fp, $data);
Chris@76: fclose($fp);
Chris@76: }
Chris@76:
Chris@76: $package_cache = array();
Chris@76: }
Chris@76:
Chris@76: // Try to make a file writable. Return true if it worked, false if it didn't.
Chris@76: function package_chmod($filename, $perm_state = 'writable', $track_change = false)
Chris@76: {
Chris@76: global $package_ftp;
Chris@76:
Chris@76: if (file_exists($filename) && is_writable($filename) && $perm_state == 'writable')
Chris@76: return true;
Chris@76:
Chris@76: // Start off checking without FTP.
Chris@76: if (!isset($package_ftp) || $package_ftp === false)
Chris@76: {
Chris@76: for ($i = 0; $i < 2; $i++)
Chris@76: {
Chris@76: $chmod_file = $filename;
Chris@76:
Chris@76: // Start off with a less agressive test.
Chris@76: if ($i == 0)
Chris@76: {
Chris@76: // If this file doesn't exist, then we actually want to look at whatever parent directory does.
Chris@76: $subTraverseLimit = 2;
Chris@76: while (!file_exists($chmod_file) && $subTraverseLimit)
Chris@76: {
Chris@76: $chmod_file = dirname($chmod_file);
Chris@76: $subTraverseLimit--;
Chris@76: }
Chris@76:
Chris@76: // Keep track of the writable status here.
Chris@76: $file_permissions = @fileperms($chmod_file);
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: // This looks odd, but it's an attempt to work around PHP suExec.
Chris@76: if (!file_exists($chmod_file) && $perm_state == 'writable')
Chris@76: {
Chris@76: $file_permissions = @fileperms(dirname($chmod_file));
Chris@76:
Chris@76: mktree(dirname($chmod_file), 0755);
Chris@76: @touch($chmod_file);
Chris@76: @chmod($chmod_file, 0755);
Chris@76: }
Chris@76: else
Chris@76: $file_permissions = @fileperms($chmod_file);
Chris@76: }
Chris@76:
Chris@76: // This looks odd, but it's another attempt to work around PHP suExec.
Chris@76: if ($perm_state != 'writable')
Chris@76: @chmod($chmod_file, $perm_state == 'execute' ? 0755 : 0644);
Chris@76: else
Chris@76: {
Chris@76: if (!@is_writable($chmod_file))
Chris@76: @chmod($chmod_file, 0755);
Chris@76: if (!@is_writable($chmod_file))
Chris@76: @chmod($chmod_file, 0777);
Chris@76: if (!@is_writable(dirname($chmod_file)))
Chris@76: @chmod($chmod_file, 0755);
Chris@76: if (!@is_writable(dirname($chmod_file)))
Chris@76: @chmod($chmod_file, 0777);
Chris@76: }
Chris@76:
Chris@76: // The ultimate writable test.
Chris@76: if ($perm_state == 'writable')
Chris@76: {
Chris@76: $fp = is_dir($chmod_file) ? @opendir($chmod_file) : @fopen($chmod_file, 'rb');
Chris@76: if (@is_writable($chmod_file) && $fp)
Chris@76: {
Chris@76: if (!is_dir($chmod_file))
Chris@76: fclose($fp);
Chris@76: else
Chris@76: closedir($fp);
Chris@76:
Chris@76: // It worked!
Chris@76: if ($track_change)
Chris@76: $_SESSION['pack_ftp']['original_perms'][$chmod_file] = $file_permissions;
Chris@76:
Chris@76: return true;
Chris@76: }
Chris@76: }
Chris@76: elseif ($perm_state != 'writable' && isset($_SESSION['pack_ftp']['original_perms'][$chmod_file]))
Chris@76: unset($_SESSION['pack_ftp']['original_perms'][$chmod_file]);
Chris@76: }
Chris@76:
Chris@76: // If we're here we're a failure.
Chris@76: return false;
Chris@76: }
Chris@76: // Otherwise we do have FTP?
Chris@76: elseif ($package_ftp !== false && !empty($_SESSION['pack_ftp']))
Chris@76: {
Chris@76: $ftp_file = strtr($filename, array($_SESSION['pack_ftp']['root'] => ''));
Chris@76:
Chris@76: // This looks odd, but it's an attempt to work around PHP suExec.
Chris@76: if (!file_exists($filename) && $perm_state == 'writable')
Chris@76: {
Chris@76: $file_permissions = @fileperms(dirname($filename));
Chris@76:
Chris@76: mktree(dirname($filename), 0755);
Chris@76: $package_ftp->create_file($ftp_file);
Chris@76: $package_ftp->chmod($ftp_file, 0755);
Chris@76: }
Chris@76: else
Chris@76: $file_permissions = @fileperms($filename);
Chris@76:
Chris@76: if ($perm_state != 'writable')
Chris@76: {
Chris@76: $package_ftp->chmod($ftp_file, $perm_state == 'execute' ? 0755 : 0644);
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: if (!@is_writable($filename))
Chris@76: $package_ftp->chmod($ftp_file, 0777);
Chris@76: if (!@is_writable(dirname($filename)))
Chris@76: $package_ftp->chmod(dirname($ftp_file), 0777);
Chris@76: }
Chris@76:
Chris@76: if (@is_writable($filename))
Chris@76: {
Chris@76: if ($track_change)
Chris@76: $_SESSION['pack_ftp']['original_perms'][$filename] = $file_permissions;
Chris@76:
Chris@76: return true;
Chris@76: }
Chris@76: elseif ($perm_state != 'writable' && isset($_SESSION['pack_ftp']['original_perms'][$filename]))
Chris@76: unset($_SESSION['pack_ftp']['original_perms'][$filename]);
Chris@76: }
Chris@76:
Chris@76: // Oh dear, we failed if we get here.
Chris@76: return false;
Chris@76: }
Chris@76:
Chris@76: function package_crypt($pass)
Chris@76: {
Chris@76: $n = strlen($pass);
Chris@76:
Chris@76: $salt = session_id();
Chris@76: while (strlen($salt) < $n)
Chris@76: $salt .= session_id();
Chris@76:
Chris@76: for ($i = 0; $i < $n; $i++)
Chris@76: $pass{$i} = chr(ord($pass{$i}) ^ (ord($salt{$i}) - 32));
Chris@76:
Chris@76: return $pass;
Chris@76: }
Chris@76:
Chris@76: function package_create_backup($id = 'backup')
Chris@76: {
Chris@76: global $sourcedir, $boarddir, $smcFunc;
Chris@76:
Chris@76: $files = array();
Chris@76:
Chris@76: $base_files = array('index.php', 'SSI.php', 'agreement.txt', 'ssi_examples.php', 'ssi_examples.shtml');
Chris@76: foreach ($base_files as $file)
Chris@76: {
Chris@76: if (file_exists($boarddir . '/' . $file))
Chris@76: $files[realpath($boarddir . '/' . $file)] = array(
Chris@76: empty($_REQUEST['use_full_paths']) ? $file : $boarddir . '/' . $file,
Chris@76: stat($boarddir . '/' . $file)
Chris@76: );
Chris@76: }
Chris@76:
Chris@76: $dirs = array(
Chris@76: $sourcedir => empty($_REQUEST['use_full_paths']) ? 'Sources/' : strtr($sourcedir . '/', '\\', '/')
Chris@76: );
Chris@76:
Chris@76: $request = $smcFunc['db_query']('', '
Chris@76: SELECT value
Chris@76: FROM {db_prefix}themes
Chris@76: WHERE id_member = {int:no_member}
Chris@76: AND variable = {string:theme_dir}',
Chris@76: array(
Chris@76: 'no_member' => 0,
Chris@76: 'theme_dir' => 'theme_dir',
Chris@76: )
Chris@76: );
Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76: $dirs[$row['value']] = empty($_REQUEST['use_full_paths']) ? 'Themes/' . basename($row['value']) . '/' : strtr($row['value'] . '/', '\\', '/');
Chris@76: $smcFunc['db_free_result']($request);
Chris@76:
Chris@76: while (!empty($dirs))
Chris@76: {
Chris@76: list ($dir, $dest) = each($dirs);
Chris@76: unset($dirs[$dir]);
Chris@76:
Chris@76: $listing = @dir($dir);
Chris@76: if (!$listing)
Chris@76: continue;
Chris@76: while ($entry = $listing->read())
Chris@76: {
Chris@76: if (preg_match('~^(\.{1,2}|CVS|backup.*|help|images|.*\~)$~', $entry) != 0)
Chris@76: continue;
Chris@76:
Chris@76: $filepath = realpath($dir . '/' . $entry);
Chris@76: if (isset($files[$filepath]))
Chris@76: continue;
Chris@76:
Chris@76: $stat = stat($dir . '/' . $entry);
Chris@76: if ($stat['mode'] & 040000)
Chris@76: {
Chris@76: $files[$filepath] = array($dest . $entry . '/', $stat);
Chris@76: $dirs[$dir . '/' . $entry] = $dest . $entry . '/';
Chris@76: }
Chris@76: else
Chris@76: $files[$filepath] = array($dest . $entry, $stat);
Chris@76: }
Chris@76: $listing->close();
Chris@76: }
Chris@76:
Chris@76: if (!file_exists($boarddir . '/Packages/backups'))
Chris@76: mktree($boarddir . '/Packages/backups', 0777);
Chris@76: if (!is_writable($boarddir . '/Packages/backups'))
Chris@76: package_chmod($boarddir . '/Packages/backups');
Chris@76: $output_file = $boarddir . '/Packages/backups/' . strftime('%Y-%m-%d_') . preg_replace('~[$\\\\/:<>|?*"\']~', '', $id);
Chris@76: $output_ext = '.tar' . (function_exists('gzopen') ? '.gz' : '');
Chris@76:
Chris@76: if (file_exists($output_file . $output_ext))
Chris@76: {
Chris@76: $i = 2;
Chris@76: while (file_exists($output_file . '_' . $i . $output_ext))
Chris@76: $i++;
Chris@76: $output_file = $output_file . '_' . $i . $output_ext;
Chris@76: }
Chris@76: else
Chris@76: $output_file .= $output_ext;
Chris@76:
Chris@76: @set_time_limit(300);
Chris@76: if (function_exists('apache_reset_timeout'))
Chris@76: @apache_reset_timeout();
Chris@76:
Chris@76: if (function_exists('gzopen'))
Chris@76: {
Chris@76: $fwrite = 'gzwrite';
Chris@76: $fclose = 'gzclose';
Chris@76: $output = gzopen($output_file, 'wb');
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: $fwrite = 'fwrite';
Chris@76: $fclose = 'fclose';
Chris@76: $output = fopen($output_file, 'wb');
Chris@76: }
Chris@76:
Chris@76: foreach ($files as $real_file => $file)
Chris@76: {
Chris@76: if (!file_exists($real_file))
Chris@76: continue;
Chris@76:
Chris@76: $stat = $file[1];
Chris@76: if (substr($file[0], -1) == '/')
Chris@76: $stat['size'] = 0;
Chris@76:
Chris@76: $current = pack('a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12', $file[0], decoct($stat['mode']), sprintf('%06d', decoct($stat['uid'])), sprintf('%06d', decoct($stat['gid'])), decoct($stat['size']), decoct($stat['mtime']), '', 0, '', '', '', '', '', '', '', '', '');
Chris@76:
Chris@76: $checksum = 256;
Chris@76: for ($i = 0; $i < 512; $i++)
Chris@76: $checksum += ord($current{$i});
Chris@76:
Chris@76: $fwrite($output, substr($current, 0, 148) . pack('a8', decoct($checksum)) . substr($current, 156, 511));
Chris@76:
Chris@76: if ($stat['size'] == 0)
Chris@76: continue;
Chris@76:
Chris@76: $fp = fopen($real_file, 'rb');
Chris@76: while (!feof($fp))
Chris@76: $fwrite($output, fread($fp, 16384));
Chris@76: fclose($fp);
Chris@76:
Chris@76: $fwrite($output, pack('a' . (512 - $stat['size'] % 512), ''));
Chris@76: }
Chris@76:
Chris@76: $fwrite($output, pack('a1024', ''));
Chris@76: $fclose($output);
Chris@76: }
Chris@76:
Chris@76: // Get the contents of a URL, irrespective of allow_url_fopen.
Chris@76: function fetch_web_data($url, $post_data = '', $keep_alive = false, $redirection_level = 0)
Chris@76: {
Chris@76: global $webmaster_email;
Chris@76: static $keep_alive_dom = null, $keep_alive_fp = null;
Chris@76:
Chris@76: preg_match('~^(http|ftp)(s)?://([^/:]+)(:(\d+))?(.+)$~', $url, $match);
Chris@76:
Chris@76: // An FTP url. We should try connecting and RETRieving it...
Chris@76: if (empty($match[1]))
Chris@76: return false;
Chris@76: elseif ($match[1] == 'ftp')
Chris@76: {
Chris@76: // Include the file containing the ftp_connection class.
Chris@76: loadClassFile('Class-Package.php');
Chris@76:
Chris@76: // Establish a connection and attempt to enable passive mode.
Chris@76: $ftp = new ftp_connection(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? 21 : $match[5], 'anonymous', $webmaster_email);
Chris@76: if ($ftp->error !== false || !$ftp->passive())
Chris@76: return false;
Chris@76:
Chris@76: // I want that one *points*!
Chris@76: fwrite($ftp->connection, 'RETR ' . $match[6] . "\r\n");
Chris@76:
Chris@76: // Since passive mode worked (or we would have returned already!) open the connection.
Chris@76: $fp = @fsockopen($ftp->pasv['ip'], $ftp->pasv['port'], $err, $err, 5);
Chris@76: if (!$fp)
Chris@76: return false;
Chris@76:
Chris@76: // The server should now say something in acknowledgement.
Chris@76: $ftp->check_response(150);
Chris@76:
Chris@76: $data = '';
Chris@76: while (!feof($fp))
Chris@76: $data .= fread($fp, 4096);
Chris@76: fclose($fp);
Chris@76:
Chris@76: // All done, right? Good.
Chris@76: $ftp->check_response(226);
Chris@76: $ftp->close();
Chris@76: }
Chris@76: // This is more likely; a standard HTTP URL.
Chris@76: elseif (isset($match[1]) && $match[1] == 'http')
Chris@76: {
Chris@76: if ($keep_alive && $match[3] == $keep_alive_dom)
Chris@76: $fp = $keep_alive_fp;
Chris@76: if (empty($fp))
Chris@76: {
Chris@76: // Open the socket on the port we want...
Chris@76: $fp = @fsockopen(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? ($match[2] ? 443 : 80) : $match[5], $err, $err, 5);
Chris@76: if (!$fp)
Chris@76: return false;
Chris@76: }
Chris@76:
Chris@76: if ($keep_alive)
Chris@76: {
Chris@76: $keep_alive_dom = $match[3];
Chris@76: $keep_alive_fp = $fp;
Chris@76: }
Chris@76:
Chris@76: // I want this, from there, and I'm not going to be bothering you for more (probably.)
Chris@76: if (empty($post_data))
Chris@76: {
Chris@76: fwrite($fp, 'GET ' . $match[6] . ' HTTP/1.0' . "\r\n");
Chris@76: fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? ($match[2] ? ':443' : '') : ':' . $match[5]) . "\r\n");
Chris@76: fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n");
Chris@76: if ($keep_alive)
Chris@76: fwrite($fp, 'Connection: Keep-Alive' . "\r\n\r\n");
Chris@76: else
Chris@76: fwrite($fp, 'Connection: close' . "\r\n\r\n");
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: fwrite($fp, 'POST ' . $match[6] . ' HTTP/1.0' . "\r\n");
Chris@76: fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? ($match[2] ? ':443' : '') : ':' . $match[5]) . "\r\n");
Chris@76: fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n");
Chris@76: if ($keep_alive)
Chris@76: fwrite($fp, 'Connection: Keep-Alive' . "\r\n");
Chris@76: else
Chris@76: fwrite($fp, 'Connection: close' . "\r\n");
Chris@76: fwrite($fp, 'Content-Type: application/x-www-form-urlencoded' . "\r\n");
Chris@76: fwrite($fp, 'Content-Length: ' . strlen($post_data) . "\r\n\r\n");
Chris@76: fwrite($fp, $post_data);
Chris@76: }
Chris@76:
Chris@76: $response = fgets($fp, 768);
Chris@76:
Chris@76: // Redirect in case this location is permanently or temporarily moved.
Chris@76: if ($redirection_level < 3 && preg_match('~^HTTP/\S+\s+30[127]~i', $response) === 1)
Chris@76: {
Chris@76: $header = '';
Chris@76: $location = '';
Chris@76: while (!feof($fp) && trim($header = fgets($fp, 4096)) != '')
Chris@76: if (strpos($header, 'Location:') !== false)
Chris@76: $location = trim(substr($header, strpos($header, ':') + 1));
Chris@76:
Chris@76: if (empty($location))
Chris@76: return false;
Chris@76: else
Chris@76: {
Chris@76: if (!$keep_alive)
Chris@76: fclose($fp);
Chris@76: return fetch_web_data($location, $post_data, $keep_alive, $redirection_level + 1);
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: // Make sure we get a 200 OK.
Chris@76: elseif (preg_match('~^HTTP/\S+\s+20[01]~i', $response) === 0)
Chris@76: return false;
Chris@76:
Chris@76: // Skip the headers...
Chris@76: while (!feof($fp) && trim($header = fgets($fp, 4096)) != '')
Chris@76: {
Chris@76: if (preg_match('~content-length:\s*(\d+)~i', $header, $match) != 0)
Chris@76: $content_length = $match[1];
Chris@76: elseif (preg_match('~connection:\s*close~i', $header) != 0)
Chris@76: {
Chris@76: $keep_alive_dom = null;
Chris@76: $keep_alive = false;
Chris@76: }
Chris@76:
Chris@76: continue;
Chris@76: }
Chris@76:
Chris@76: $data = '';
Chris@76: if (isset($content_length))
Chris@76: {
Chris@76: while (!feof($fp) && strlen($data) < $content_length)
Chris@76: $data .= fread($fp, $content_length - strlen($data));
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: while (!feof($fp))
Chris@76: $data .= fread($fp, 4096);
Chris@76: }
Chris@76:
Chris@76: if (!$keep_alive)
Chris@76: fclose($fp);
Chris@76: }
Chris@76: else
Chris@76: {
Chris@76: // Umm, this shouldn't happen?
Chris@76: trigger_error('fetch_web_data(): Bad URL', E_USER_NOTICE);
Chris@76: $data = false;
Chris@76: }
Chris@76:
Chris@76: return $data;
Chris@76: }
Chris@76:
Chris@76: // crc32 doesn't work as expected on 64-bit functions - make our own.
Chris@76: // http://www.php.net/crc32#79567
Chris@76: if (!function_exists('smf_crc32'))
Chris@76: {
Chris@76: function smf_crc32($number)
Chris@76: {
Chris@76: $crc = crc32($number);
Chris@76:
Chris@76: if ($crc & 0x80000000)
Chris@76: {
Chris@76: $crc ^= 0xffffffff;
Chris@76: $crc += 1;
Chris@76: $crc = -$crc;
Chris@76: }
Chris@76:
Chris@76: return $crc;
Chris@76: }
Chris@76: }
Chris@76:
Chris@76: ?>