<?php

	@set_time_limit(0);
	error_reporting(E_ALL & ~E_NOTICE);
    ini_set('magic_quotes_runtime', false);
	ignore_user_abort( true );
	clearstatcache();
	ini_set('output_buffering', 0);
	ini_set('implicit_flush', 1);

	if (!function_exists('file_put_contents')) {
		function file_put_contents($file, $data) {
			if (!$f = fopen($file, 'w')) return false;
			fputs($f, $data);
			fclose($f);
		}
	}

	if (isset($argv)) {
		foreach ($argv as $str) {
			$tmp = explode("=", chop($str));
			$params[chop($tmp[0])] = chop($tmp[1]);
		}
	}


	// new IonCude fix 

	if (!in_array('ionCube Loader', get_loaded_extensions()) or !function_exists('ioncube_loader_version')) die("This script requires ionCube php extension\n");

	$ioncubeVersion = ioncube_loader_version();

	$ioncubeMajorVersion = (int)substr($ioncubeVersion, 0, strpos($ioncubeVersion, '.'));
	$ioncubeMinorVersion = (int)substr($ioncubeVersion, strpos($ioncubeVersion, '.')+1);
	if ($ioncubeMajorVersion < 4 || ($ioncubeMajorVersion == 4 && $ioncubeMinorVersion < 3)) {
		die("ionCube Loader '.$ioncubeVersion.' - old, required is a minimum of ionCube extension version 4.3 \n");
	}


	$install_source_url = 'http://smartcj.com/scj_install/ic/52/';



$permissions = array(
    'scj_pro.lic' =>  '0644', 
    'admin' =>  '0755', 
    'admin/files' =>  '0755', 
    'admin/files/ac_settings.php' =>  '0644', 
    'admin/files/active_ips.php' =>  '0644', 
    'admin/files/add_trade.php' =>  '0644', 
    'admin/files/admin_access_log.php' =>  '0644', 
    'admin/files/admin_colors.php' =>  '0644', 
    'admin/files/admin_layouts.php' =>  '0644', 
    'admin/files/analize.php' =>  '0644', 
    'admin/files/analize2.php' =>  '0644', 
    'admin/files/analize3.php' =>  '0644', 
    'admin/files/autoupdate.php' =>  '0644', 
    'admin/files/backup.php' =>  '0644', 
    'admin/files/backup_mysql.php' =>  '0644', 
    'admin/files/bl_list.php' =>  '0644', 
    'admin/files/blacklist.php' =>  '0644', 
    'admin/files/bottom.php' =>  '0644', 
    'admin/files/cheat.php' =>  '0644', 
    'admin/files/checker_log.php' =>  '0644', 
    'admin/files/checker_settings.php' =>  '0644', 
    'admin/files/checker_view.php' =>  '0644', 
    'admin/files/ga.php' =>  '0644', 
    'admin/files/cj_settings.php' =>  '0644', 
    'admin/files/config_files.php' =>  '0644', 
    'admin/files/db_repair.php' =>  '0644', 
    'admin/files/dynamic_skim.php' =>  '0644', 
    'admin/files/export_trades.php' =>  '0644', 
    'admin/files/fastedit.php' =>  '0644', 
    'admin/files/ga_add_trade.php' =>  '0644', 
    'admin/files/ga_alexa.php' =>  '0644', 
    'admin/files/ga_niches.php' =>  '0644', 
    'admin/files/ga_site_edit.php' =>  '0644', 
    'admin/files/ga_sites.php' =>  '0644', 
    'admin/files/ga_traders.php' =>  '0644', 
    'admin/files/gal_comments.php' =>  '0644', 
    'admin/files/gal_export.php' =>  '0644', 
    'admin/files/gal_galleries.php' =>  '0644', 
    'admin/files/gal_gallery_crop.php' =>  '0644', 
    'admin/files/gal_gallery_edit.php' =>  '0644', 
    'admin/files/gal_grabber.php' =>  '0644', 
    'admin/files/gal_server_edit.php' =>  '0644', 
    'admin/files/gal_servers.php' =>  '0644', 
    'admin/files/gal_settings.php' =>  '0644', 
    'admin/files/gal_specials.php' =>  '0644', 
    'admin/files/gal_sponsor_edit.php' =>  '0644', 
    'admin/files/gal_sponsors.php' =>  '0644', 
    'admin/files/gal_templates.php' =>  '0644', 
    'admin/files/galleries.php' =>  '0644', 
    'admin/files/graph.php' =>  '0644', 
    'admin/files/groups.php' =>  '0644', 
    'admin/files/h_details_domain.php' =>  '0644', 
    'admin/files/header.php' =>  '0644', 
    'admin/files/history.php' =>  '0644', 
    'admin/files/history_details.php' =>  '0644', 
    'admin/files/import_trades.php' =>  '0644', 
    'admin/files/inrules.php' =>  '0644', 
    'admin/files/internal.php' =>  '0644', 
    'admin/files/ip2lng.php' =>  '0644', 
    'admin/files/logs.php' =>  '0644', 
    'admin/files/main.php' =>  '0644', 
    'admin/files/maintainance.php' =>  '0644', 
    'admin/files/pages.php' =>  '0644', 
    'admin/files/mass_edit.php' =>  '0644', 
    'admin/files/mass_mail.php' =>  '0644', 
    'admin/files/new_trade.php' =>  '0644', 
    'admin/files/out_settings.php' =>  '0644', 
    'admin/files/outrules.php' =>  '0644', 
    'admin/files/password.php' =>  '0644', 
    'admin/files/password_edit.php' =>  '0644', 
    'admin/files/publish_top.php' =>  '0644', 
    'admin/files/quality_settings.php' =>  '0644', 
    'admin/files/reports.php' =>  '0644', 
    'admin/files/reset_cj.php' =>  '0644', 
    'admin/files/rot_checker.php' =>  '0644', 
    'admin/files/rot_cms.php' =>  '0644', 
    'admin/files/rot_cms_custom_vars.php' =>  '0644', 
    'admin/files/rot_comments.php' =>  '0644', 
    'admin/files/rot_crop_profiles.php' =>  '0644', 
    'admin/files/rot_easyedit_crop_profile.php' =>  '0644', 
    'admin/files/rot_edit.php' =>  '0644', 
    'admin/files/rot_edit_crop_profile.php' =>  '0644', 
    'admin/files/rot_export.php' =>  '0644', 
    'admin/files/rot_graber.php' =>  '0644', 
    'admin/files/rot_graber_queue.php' =>  '0644', 
    'admin/files/rot_graph.php' =>  '0644', 
    'admin/files/rot_groups.php' =>  '0644', 
    'admin/files/rot_import.php' =>  '0644', 
    'admin/files/rot_import_sets.php' =>  '0644', 
    'admin/files/rot_list.php' =>  '0644', 
    'admin/files/rot_make.php' =>  '0644', 
    'admin/files/rot_manual_crop.php' =>  '0644', 
    'admin/files/rot_massedit.php' =>  '0644', 
    'admin/files/rot_models.php' =>  '0644', 
    'admin/files/rot_pages.php' =>  '0644', 
    'admin/files/rot_preload.php' =>  '0644', 
    'admin/files/rot_preload2.php' =>  '0644', 
    'admin/files/rot_preview.php' =>  '0644', 
    'admin/files/rot_replacements.php' =>  '0644', 
    'admin/files/rot_server_edit.php' =>  '0644', 
    'admin/files/rot_servers.php' =>  '0644', 
    'admin/files/stats.php' =>  '0644', 
    'admin/files/rot_settings.php' =>  '0644', 
    'admin/files/rot_sponsor_edit.php' =>  '0644', 
    'admin/files/rot_sponsors.php' =>  '0644', 
    'admin/files/rot_stats.php' =>  '0644', 
    'admin/files/rot_tags.php' =>  '0644', 
    'admin/files/rot_templates.php' =>  '0644', 
    'admin/files/rot_tube_settings.php' =>  '0644', 
    'admin/files/rot_user_edit.php' =>  '0644', 
    'admin/files/rot_users.php' =>  '0644', 
    'admin/files/stats_page_track.php' =>  '0644', 
    'admin/files/rot_view_thumbs.php' =>  '0644', 
    'admin/files/rotation.php' =>  '0644', 
    'admin/files/rows.inc.php' =>  '0644', 
    'admin/files/script_messages.php' =>  '0644', 
    'admin/files/sel_process.php' =>  '0644', 
    'admin/files/sel_settings.php' =>  '0644', 
    'admin/files/settings.php' =>  '0644', 
    'admin/files/smoothness.php' =>  '0644', 
    'admin/files/stats_bookmark.php' =>  '0644', 
    'admin/files/stats_browser.php' =>  '0644', 
    'admin/files/stats_geoip.php' =>  '0644', 
    'admin/files/stats_graph.php' =>  '0644', 
    'admin/files/stats_ips.php' =>  '0644', 
    'admin/files/stats_links.php' =>  '0644', 
    'admin/files/stats_lng.php' =>  '0644', 
    'admin/files/stats_quality.php' =>  '0644', 
    'admin/files/stats_refs.php' =>  '0644', 
    'admin/files/stats_traffic.php' =>  '0644', 
    'admin/files/templates.php' =>  '0644', 
    'admin/files/timed_forces.php' =>  '0644', 
    'admin/files/top.php' =>  '0644', 
    'admin/files/top_positions.php' =>  '0644', 
    'admin/files/toplist.php' =>  '0644', 
    'admin/files/trade.php' =>  '0644', 
    'admin/files/trade_activation.php' =>  '0644', 
    'admin/files/trade_delete.php' =>  '0644', 
    'admin/files/trade_edit.php' =>  '0644', 
    'admin/files/trade_plugs.php' =>  '0644', 
    'admin/files/trade_stats.php' =>  '0644', 
    'admin/files/traffic_rules.php' =>  '0644', 
    'admin/files/traffic_sell.php' =>  '0644', 
    'admin/files/trash.php' =>  '0644', 

    'admin/files/hostinfo.php' =>  '0644', 
    'admin/files/rot_banner_groups.php' =>  '0644', 
    'admin/files/rot_banners.php' =>  '0644', 
    'admin/files/rot_cms_massedit.php' =>  '0644', 
    'admin/files/rot_search_log.php' =>  '0644', 
    'admin/files/tds.php' =>  '0644', 
    'admin/files/tds_rule_edit.php' =>  '0644', 
    'admin/files/tds_scheme_edit.php' =>  '0644', 
    'admin/styles/calendar.css' =>  '0644', 


    'admin/files/.htaccess' =>  '0644', 
    'admin/graph' =>  '0755', 
    'admin/graph/imgdata_balls.inc' =>  '0644', 
    'admin/graph/imgdata_bevels.inc' =>  '0644', 
    'admin/graph/imgdata_diamonds.inc' =>  '0644', 
    'admin/graph/imgdata_pushpins.inc' =>  '0644', 
    'admin/graph/imgdata_squares.inc' =>  '0644', 
    'admin/graph/imgdata_stars.inc' =>  '0644', 
    'admin/graph/jpg-config.inc' =>  '0644', 
    'admin/graph/jpgraph.php' =>  '0644', 
    'admin/graph/jpgraph_antispam.php' =>  '0644', 
    'admin/graph/jpgraph_antispam-digits.php' =>  '0644', 
    'admin/graph/jpgraph_bar.php' =>  '0644', 
    'admin/graph/jpgraph_canvas.php' =>  '0644', 
    'admin/graph/jpgraph_canvtools.php' =>  '0644', 
    'admin/graph/jpgraph_date.php' =>  '0644', 
    'admin/graph/jpgraph_error.php' =>  '0644', 
    'admin/graph/jpgraph_flags.php' =>  '0644', 
    'admin/graph/jpgraph_gantt.php' =>  '0644', 
    'admin/graph/jpgraph_gb2312.php' =>  '0644', 
    'admin/graph/jpgraph_gradient.php' =>  '0644', 
    'admin/graph/jpgraph_iconplot.php' =>  '0644', 
    'admin/graph/jpgraph_imgtrans.php' =>  '0644', 
    'admin/graph/jpgraph_line.php' =>  '0644', 
    'admin/graph/jpgraph_log.php' =>  '0644', 
    'admin/graph/jpgraph_pie.php' =>  '0644', 
    'admin/graph/jpgraph_pie3d.php' =>  '0644', 
    'admin/graph/jpgraph_plotband.php' =>  '0644', 
    'admin/graph/jpgraph_plotmark.inc' =>  '0644', 
    'admin/graph/jpgraph_polar.php' =>  '0644', 
    'admin/graph/jpgraph_radar.php' =>  '0644', 
    'admin/graph/jpgraph_regstat.php' =>  '0644', 
    'admin/graph/jpgraph_scatter.php' =>  '0644', 
    'admin/graph/jpgraph_stock.php' =>  '0644', 
    'admin/mods' =>  '0755', 
    'admin/mods/cron.priority.php' =>  '0644', 
    'admin/styles' =>  '0755', 
    'admin/styles/images' =>  '0755', 
    'admin/styles/images/blue' =>  '0755', 
    'admin/styles/images/blue/all.png' =>  '0644', 
    'admin/styles/images/blue/new_old.png' =>  '0644', 
    'admin/styles/images/grey' =>  '0755', 
    'admin/styles/images/grey/all.png' =>  '0644', 
    'admin/styles/images/grey/all_2.png' =>  '0644', 
    'admin/styles/images/new_blue' =>  '0755', 
    'admin/styles/images/new_blue/all.png' =>  '0644', 
    'admin/styles/images/new_green' =>  '0755', 
    'admin/styles/images/new_green/all.png' =>  '0644', 
    'admin/styles/blue.css' =>  '0644', 
    'admin/styles/grey.css' =>  '0644', 
    'admin/styles/new_blue.css' =>  '0644', 
    'admin/styles/new_green.css' =>  '0644', 
    'admin/styles/style.css' =>  '0644', 
    'admin/styles/thickbox.css' =>  '0644', 
    'admin/build_version.txt' =>  '0644', 
    'admin/host.php' =>  '0644', 
    'admin/index.php' =>  '0644', 
    'admin/style.css' =>  '0644', 
    'admin/test.php' =>  '0644', 
    'admin/update.php' =>  '0644', 
    'admin/.htaccess' =>  '0644', 
    'admin/.htpasswd' =>  '0666', 
    'backup' =>  '0777', 
    'backup/last.data' =>  '0644', 
    'backup/.htaccess' =>  '0644', 
    'bin' =>  '0755', 
    'bin/content_cron.php' =>  '0644', 
    'bin/cron.php' =>  '0644', 
    'bin/hostinfo.php' =>  '0644', 
    'bin/preload_process.php' =>  '0644', 
    'bin/rot.php' =>  '0644', 
    'bin/rot_check_galleries.php' =>  '0644', 
    'bin/scj_ex.php' =>  '0644', 
    'bin/sel_cron.php' =>  '0644', 
    'cache' =>  '0777', 
    'cache/tmp.file' =>  '0644', 
    'cache/.htaccess' =>  '0644', 
    'cgi' =>  '0755', 
    'cgi/common.php' =>  '0644', 
    'cgi/gout.php' =>  '0644', 
    'cgi/include.php' =>  '0644', 
    'cgi/index.php' =>  '0644', 
    'cgi/out.php' =>  '0644', 
    'cgi/plug.php' =>  '0644', 
    'cgi/rot_in.php' =>  '0644', 
    'cgi/user.php' =>  '0644', 
    'data' =>  '0755', 
    'data/data' =>  '0755', 
    'data/data/test.url' =>  '0644', 
    'data/blacklist' =>  '0666', 
    'data/last_click.data' =>  '0666', 
    'data/members' =>  '0666', 
    'data/members_pages.conf' =>  '0666', 
    'data/query_queue.data' =>  '0666', 
    'data/rot.data' =>  '0666', 
    'data/in.data' =>  '0666', 
    'data/out.data' =>  '0666', 
    'data/rotpages.cfg' =>  '0666', 
    'data/sql' =>  '0666', 
    'data/traders_urls.conf' =>  '0666', 
    'data/user.data' =>  '0666', 
    'data/user_var.conf' =>  '0666', 
    'data/.htaccess' =>  '0644', 
    'includes' =>  '0755', 
    'includes/conf' =>  '0755', 
    'includes/conf/banned_ips.txt' =>  '0666', 
    'includes/conf/banned_words.txt' =>  '0666', 
    'includes/conf/br.txt' =>  '0666', 
    'includes/conf/browsers.txt' =>  '0666', 
    'includes/conf/gal_cloud_skip.txt' =>  '0666', 
    'includes/conf/GeoIP.dat' =>  '0666', 
    'includes/conf/icq_messages.txt' =>  '0666', 
    'includes/conf/languages.txt' =>  '0666', 
    'includes/conf/logo.png' =>  '0666', 
    'includes/conf/rot_deletion_rules.txt' =>  '0666', 
    'includes/conf/se_domains.txt' =>  '0666', 
    'includes/conf/spiders_ua.txt' =>  '0666', 
    'includes/conf/substitution.txt' =>  '0666', 
    'includes/conf/syn.txt' =>  '0666', 
    'includes/conf/tld.txt' =>  '0666', 
    'includes/conf/tags_white_list.txt' =>  '0666', 
    'includes/db_modules' =>  '0755', 
    'includes/db_modules/mysql.php' =>  '0644', 
    'includes/fonts' =>  '0755', 
    'includes/fonts/Vera.ttf' =>  '0644', 
    'includes/fonts/VeraBd.ttf' =>  '0644', 
    'includes/fonts/VeraIt.ttf' =>  '0644', 
    'includes/img' =>  '0755', 
    'includes/img/img_mod' =>  '0755', 
    'includes/img/img_mod/b-.gif' =>  '0644', 
    'includes/img/img_mod/b+.gif' =>  '0644', 
    'includes/img/img_mod/c-.gif' =>  '0644', 
    'includes/img/img_mod/c+.gif' =>  '0644', 
    'includes/img/img_mod/hue_minus.gif' =>  '0644', 
    'includes/img/img_mod/hue+.gif' =>  '0644', 
    'includes/img/img_mod/restore.gif' =>  '0644', 
    'includes/img/img_mod/s-.gif' =>  '0644', 
    'includes/img/img_mod/s+.gif' =>  '0644', 
    'includes/img/img_mod/sat_minus.gif' =>  '0644', 
    'includes/img/img_mod/sat+.gif' =>  '0644', 
    'includes/img/img_mod/snapshot.gif' =>  '0644', 
    'includes/img/img_mod/undo.gif' =>  '0644', 
    'includes/img/b_edit.png' =>  '0644', 
    'includes/img/blue.gif' =>  '0644', 
    'includes/img/border-anim-h.gif' =>  '0644', 
    'includes/img/border-anim-v.gif' =>  '0644', 
    'includes/img/check.png' =>  '0644', 
    'includes/img/close.gif' =>  '0644', 
    'includes/img/delete.png' =>  '0644', 
    'includes/img/divider.gif' =>  '0644', 
    'includes/img/down.gif' =>  '0644', 
    'includes/img/drop1.gif' =>  '0644', 
    'includes/img/drop2.gif' =>  '0644', 
    'includes/img/ed_about.gif' =>  '0644', 
    'includes/img/ed_align_center.gif' =>  '0644', 
    'includes/img/ed_align_left.gif' =>  '0644', 
    'includes/img/ed_align_right.gif' =>  '0644', 
    'includes/img/ed_blank.gif' =>  '0644', 
    'includes/img/ed_charmap.gif' =>  '0644', 
    'includes/img/ed_color_bg.gif' =>  '0644', 
    'includes/img/ed_color_fg.gif' =>  '0644', 
    'includes/img/ed_copy.gif' =>  '0644', 
    'includes/img/ed_custom.gif' =>  '0644', 
    'includes/img/ed_cut.gif' =>  '0644', 
    'includes/img/ed_delete.gif' =>  '0644', 
    'includes/img/ed_format_bold.gif' =>  '0644', 
    'includes/img/ed_format_italic.gif' =>  '0644', 
    'includes/img/ed_format_strike.gif' =>  '0644', 
    'includes/img/ed_format_sub.gif' =>  '0644', 
    'includes/img/ed_format_sup.gif' =>  '0644', 
    'includes/img/ed_format_underline.gif' =>  '0644', 
    'includes/img/ed_help.gif' =>  '0644', 
    'includes/img/ed_hr.gif' =>  '0644', 
    'includes/img/ed_html.gif' =>  '0644', 
    'includes/img/ed_image.gif' =>  '0644', 
    'includes/img/ed_indent_less.gif' =>  '0644', 
    'includes/img/ed_indent_more.gif' =>  '0644', 
    'includes/img/ed_link.gif' =>  '0644', 
    'includes/img/ed_list_bullet.gif' =>  '0644', 
    'includes/img/ed_list_num.gif' =>  '0644', 
    'includes/img/ed_redo.gif' =>  '0644', 
    'includes/img/ed_undo.gif' =>  '0644', 
    'includes/img/edit.gif' =>  '0644', 
    'includes/img/fullscreen_maximize.gif' =>  '0644', 
    'includes/img/fullscreen_minimize.gif' =>  '0644', 
    'includes/img/green.gif' =>  '0644', 
    'includes/img/insert_table.gif' =>  '0644', 
    'includes/img/left1.gif' =>  '0644', 
    'includes/img/left2.gif' =>  '0644', 
    'includes/img/letter.gif' =>  '0644', 
    'includes/img/loadingAnimation.gif' =>  '0644', 
    'includes/img/red.gif' =>  '0644', 
    'includes/img/right1.gif' =>  '0644', 
    'includes/img/right2.gif' =>  '0644', 
    'includes/img/up.gif' =>  '0644', 
    'includes/js' =>  '0755', 
    'includes/js/popups' =>  '0755', 
    'includes/js/popups/about.html' =>  '0644', 
    'includes/js/popups/blank.html' =>  '0644', 
    'includes/js/popups/custom2.html' =>  '0644', 
    'includes/js/popups/editor_help.html' =>  '0644', 
    'includes/js/popups/fullscreen.html' =>  '0644', 
    'includes/js/popups/insert_image.html' =>  '0644', 
    'includes/js/popups/insert_table.html' =>  '0644', 
    'includes/js/popups/select_color.html' =>  '0644', 
    'includes/js/_clipboard.swf' =>  '0644', 
    'includes/js/buttons.js' =>  '0644', 
    'includes/js/easy_sort.js' =>  '0644', 
    'includes/js/editor.js' =>  '0644', 
    'includes/js/fast_trade_import.js' =>  '0644', 
    'includes/js/jquery.areaselect.js' =>  '0644', 
    'includes/js/jquery.colorInput.min.js' =>  '0644', 
    'includes/js/jquery.contextmenu.js' =>  '0644', 
    'includes/js/jquery.cookie.js' =>  '0644', 
    'includes/js/jquery.dimensions.js' =>  '0644', 
    'includes/js/jquery.dragsort-0.3.10.min.js' =>  '0644', 
    'includes/js/jquery.editinplace.js' =>  '0644', 
    'includes/js/jquery.editinplace.packed.js' =>  '0644', 
    'includes/js/jquery.imgareaselect.min.9.3.js' =>  '0644', 
    'includes/js/jquery.jdMenu.js' =>  '0644', 
    'includes/js/jquery.js' =>  '0644', 
    'includes/js/jquery.lazyload.js' =>  '0644', 
    'includes/js/jquery.positionBy.js' =>  '0644', 
    'includes/js/jquery.rating.pack.js' =>  '0644', 
    'includes/js/jquery.tablednd.js' =>  '0644', 
    'includes/js/jquery.tw.js' =>  '0644', 
    'includes/js/pop_menu.js' =>  '0644', 
    'includes/js/popcalendar.js' =>  '0644', 
    'includes/js/sorttable.js' =>  '0644', 
    'includes/js/thickbox.js' =>  '0644', 
    'includes/js/trade.js' =>  '0644', 
    'includes/js/edit_area' =>  '0755', 
    'includes/js/edit_area/images' =>  '0755', 
    'includes/js/edit_area/images/autocompletion.gif' =>  '0644', 
    'includes/js/edit_area/images/close.gif' =>  '0644', 
    'includes/js/edit_area/images/fullscreen.gif' =>  '0644', 
    'includes/js/edit_area/images/go_to_line.gif' =>  '0644', 
    'includes/js/edit_area/images/help.gif' =>  '0644', 
    'includes/js/edit_area/images/highlight.gif' =>  '0644', 
    'includes/js/edit_area/images/load.gif' =>  '0644', 
    'includes/js/edit_area/images/move.gif' =>  '0644', 
    'includes/js/edit_area/images/newdocument.gif' =>  '0644', 
    'includes/js/edit_area/images/opacity.png' =>  '0644', 
    'includes/js/edit_area/images/processing.gif' =>  '0644', 
    'includes/js/edit_area/images/redo.gif' =>  '0644', 
    'includes/js/edit_area/images/reset_highlight.gif' =>  '0644', 
    'includes/js/edit_area/images/save.gif' =>  '0644', 
    'includes/js/edit_area/images/search.gif' =>  '0644', 
    'includes/js/edit_area/images/smooth_selection.gif' =>  '0644', 
    'includes/js/edit_area/images/spacer.gif' =>  '0644', 
    'includes/js/edit_area/images/statusbar_resize.gif' =>  '0644', 
    'includes/js/edit_area/images/undo.gif' =>  '0644', 
    'includes/js/edit_area/images/word_wrap.gif' =>  '0644', 
    'includes/js/edit_area/langs' =>  '0755', 
    'includes/js/edit_area/langs/bg.js' =>  '0644', 
    'includes/js/edit_area/langs/cs.js' =>  '0644', 
    'includes/js/edit_area/langs/de.js' =>  '0644', 
    'includes/js/edit_area/langs/dk.js' =>  '0644', 
    'includes/js/edit_area/langs/en.js' =>  '0644', 
    'includes/js/edit_area/langs/eo.js' =>  '0644', 
    'includes/js/edit_area/langs/es.js' =>  '0644', 
    'includes/js/edit_area/langs/fi.js' =>  '0644', 
    'includes/js/edit_area/langs/fr.js' =>  '0644', 
    'includes/js/edit_area/langs/hr.js' =>  '0644', 
    'includes/js/edit_area/langs/it.js' =>  '0644', 
    'includes/js/edit_area/langs/ja.js' =>  '0644', 
    'includes/js/edit_area/langs/mk.js' =>  '0644', 
    'includes/js/edit_area/langs/nl.js' =>  '0644', 
    'includes/js/edit_area/langs/pl.js' =>  '0644', 
    'includes/js/edit_area/langs/pt.js' =>  '0644', 
    'includes/js/edit_area/langs/ru.js' =>  '0644', 
    'includes/js/edit_area/langs/sk.js' =>  '0644', 
    'includes/js/edit_area/langs/zh.js' =>  '0644', 
    'includes/js/edit_area/plugins' =>  '0755', 
    'includes/js/edit_area/plugins/charmap' =>  '0755', 
    'includes/js/edit_area/plugins/charmap/css' =>  '0755', 
    'includes/js/edit_area/plugins/charmap/css/charmap.css' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/images' =>  '0755', 
    'includes/js/edit_area/plugins/charmap/images/charmap.gif' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/jscripts' =>  '0755', 
    'includes/js/edit_area/plugins/charmap/jscripts/map.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs' =>  '0755', 
    'includes/js/edit_area/plugins/charmap/langs/bg.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/cs.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/de.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/dk.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/en.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/eo.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/es.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/fr.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/hr.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/it.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/ja.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/mk.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/nl.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/pl.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/pt.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/ru.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/sk.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/langs/zh.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/charmap.js' =>  '0644', 
    'includes/js/edit_area/plugins/charmap/popup.html' =>  '0644', 
    'includes/js/edit_area/reg_syntax' =>  '0755', 
    'includes/js/edit_area/reg_syntax/basic.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/brainfuck.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/c.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/coldfusion.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/cpp.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/css.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/html.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/java.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/js.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/pas.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/perl.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/php.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/python.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/robotstxt.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/ruby.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/sql.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/tsql.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/vb.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax/xml.js' =>  '0644', 
    'includes/js/edit_area/autocompletion.js' =>  '0644', 
    'includes/js/edit_area/edit_area.css' =>  '0644', 
    'includes/js/edit_area/edit_area.js' =>  '0644', 
    'includes/js/edit_area/edit_area_compressor.php' =>  '0644', 
    'includes/js/edit_area/edit_area_full.gz' =>  '0644', 
    'includes/js/edit_area/edit_area_full.js' =>  '0644', 
    'includes/js/edit_area/edit_area_functions.js' =>  '0644', 
    'includes/js/edit_area/edit_area_loader.js' =>  '0644', 
    'includes/js/edit_area/elements_functions.js' =>  '0644', 
    'includes/js/edit_area/highlight.js' =>  '0644', 
    'includes/js/edit_area/keyboard.js' =>  '0644', 
    'includes/js/edit_area/license_apache.txt' =>  '0644', 
    'includes/js/edit_area/license_bsd.txt' =>  '0644', 
    'includes/js/edit_area/license_lgpl.txt' =>  '0644', 
    'includes/js/edit_area/manage_area.js' =>  '0644', 
    'includes/js/edit_area/reg_syntax.js' =>  '0644', 
    'includes/js/edit_area/regexp.js' =>  '0644', 
    'includes/js/edit_area/resize_area.js' =>  '0644', 
    'includes/js/edit_area/search_replace.js' =>  '0644', 
    'includes/js/edit_area/template.html' =>  '0644', 
    'includes/libs' =>  '0755', 
    'includes/libs/cache' =>  '0755', 
    'includes/libs/.htaccess' =>  '0644', 
    'includes/libs/cache/file.cache.php' =>  '0644', 
    'includes/libs/cache/memcache.cache.php' =>  '0644', 
    'includes/libs/cache/nocache.cache.php' =>  '0644', 
    'includes/libs/alexa.class.php' =>  '0644', 
    'includes/libs/cache_header.php' =>  '0644', 
    'includes/libs/captcha.class.php' =>  '0644', 
    'includes/libs/cj_functions.php' =>  '0644', 
    'includes/libs/cjlog_feed.php' =>  '0644', 
    'includes/libs/content_functions.php' =>  '0644', 
    'includes/libs/email.php' =>  '0644', 
    'includes/libs/ext_stats.php' =>  '0644', 
    'includes/libs/functions.php' =>  '0644', 
    'includes/libs/geoip.inc' =>  '0644', 
    'includes/libs/geoipcity.inc' =>  '0644', 
    'includes/libs/gzipcomp.php' =>  '0644', 
    'includes/libs/http.php' =>  '0644', 
    'includes/libs/http_functions.php' =>  '0644', 
    'includes/libs/img_functions.php' =>  '0644', 
    'includes/libs/mime.php' =>  '0644', 
    'includes/libs/my_snoopy.php' =>  '0644', 
    'includes/libs/mysql_backup.php' =>  '0644', 
    'includes/libs/sphinxapi.php' =>  '0644', 
    'includes/libs/rot_functions.php' =>  '0644', 
    'includes/libs/trader_thumbs.php' =>  '0644', 
    'includes/libs/tpl_functions.php' =>  '0644', 
    'includes/libs/evalmath.class.php' =>  '0644', 
    'includes/config.php' =>  '0644', 
    'includes/end.php' =>  '0644', 
    'includes/prepare.php' =>  '0644', 
    'includes/vars.php' =>  '0644', 

    'includes/img/calendar-disabled.png' =>  '0644', 
    'includes/img/calendar.png' =>  '0644', 
    'includes/js/jquery.autocomplete.min.js' =>  '0644', 
    'includes/js/jquery.tablesorter.js' =>  '0644', 
    'includes/js/zebra_datepicker.js' =>  '0644', 
    'includes/libs/CurlWrapper.php' =>  '0644', 
    'includes/libs/cache/redis.cache.php' =>  '0644', 
    'includes/libs/cache.php' =>  '0644', 
    'includes/libs/check_scj.lib.php' =>  '0644', 
    'includes/libs/cookie.php' =>  '0644', 
    'includes/libs/ftp.class.php' =>  '0644', 
    'includes/libs/sessions.class.php' =>  '0644', 


    'logs' =>  '0755', 
    'logs/cron.log' =>  '0666', 
    'logs/cj.log' =>  '0666', 
    'logs/error.log' =>  '0666', 
    'logs/rot.log' =>  '0666', 
    'logs/.htaccess' =>  '0644', 
    'logs/content_graber.log' =>  '0666', 
    'logs/preload.log' =>  '0666', 
    'logs/rot_checker.log' =>  '0666',     

    'rot_pages' =>  '0755', 
    'rot_pages/tmp.file' =>  '0644', 
    'rot_pages/.htaccess' =>  '0644', 
    'templates' =>  '0755', 
    'templates/wm_closed.tpl' =>  '0666', 
    'templates/wm_signup.tpl' =>  '0666', 
    'templates/wm_signup_done.tpl' =>  '0666', 
    'templates/wm_stats.tpl' =>  '0666', 

    'templates/1.gif' =>  '0644', 
    'templates/2.gif' =>  '0644', 
    'templates/3.gif' =>  '0644', 
    'templates/4.gif' =>  '0644', 

    'templates/bl.gif' =>  '0644', 
    'templates/blc.gif' =>  '0644', 
    'templates/border.gif' =>  '0644', 
    'templates/br.gif' =>  '0644', 
    'templates/brc.gif' =>  '0644', 
    'templates/lines.gif' =>  '0644', 
    'templates/report.tpl' =>  '0644', 
    'templates/report_done.tpl' =>  '0644', 
    'templates/spacer.gif' =>  '0644', 
    'templates/tl.gif' =>  '0644', 
    'templates/tlc.gif' =>  '0644', 
    'templates/tr.gif' =>  '0644', 
    'templates/trc.gif' =>  '0644', 


    'thumbs' =>  '0777', 
    'thumbs/tmp.file' =>  '0644', 
    'thumbs/.htaccess' =>  '0644', 
    
    'tmp' =>  '0777', 
    'tmp/.htaccess' =>  '0644', 

    'tmp/tmp.file' =>  '0644', 

    'top' =>  '0777', 
    'top/tt' =>  '0777', 
    'top/tt/default_image.jpg' =>  '0644', 
    'top/tt/tmp.file' =>  '0644', 
    'top/tt/.htaccess' =>  '0644', 
    'top/_top.html' =>  '0644', 
    'top/top.html' =>  '0666', 
    'top/.htaccess' =>  '0644', 
    'tube' =>  '0755', 
    'tube/default_templates' =>  '0755', 
    'tube/default_templates/advanced' =>  '0755', 
    'tube/default_templates/advanced/bottom.tpl' =>  '0644', 
    'tube/default_templates/advanced/content_list.tpl' =>  '0644', 
    'tube/default_templates/advanced/content_page.tpl' =>  '0644', 
    'tube/default_templates/advanced/content_search.tpl' =>  '0644', 
    'tube/default_templates/advanced/favorites.tpl' =>  '0644', 
    'tube/default_templates/advanced/header.tpl' =>  '0644', 
    'tube/default_templates/advanced/index.tpl' =>  '0644', 
    'tube/default_templates/advanced/login.tpl' =>  '0644', 
    'tube/default_templates/advanced/profile.tpl' =>  '0644', 
    'tube/default_templates/advanced/register.tpl' =>  '0644', 
    'tube/default_templates/advanced/registration_email.tpl' =>  '0644', 
    'tube/default_templates/easy' =>  '0755', 
    'tube/default_templates/easy/bottom.tpl' =>  '0644', 
    'tube/default_templates/easy/content_list.tpl' =>  '0644', 
    'tube/default_templates/easy/content_page.tpl' =>  '0644', 
    'tube/default_templates/easy/content_search.tpl' =>  '0644', 
    'tube/default_templates/easy/header.tpl' =>  '0644', 
    'tube/default_templates/easy/index.tpl' =>  '0644', 
    'tube/player' =>  '0755', 
    'tube/player/player.swf' =>  '0644', 
    'tube/player/swfobject.js' =>  '0644', 
    'tube/player/yt.swf' =>  '0644', 
    'tube/captcha.php' =>  '0644', 
    'tube/common.php' =>  '0644', 
    'tube/.htaccess' =>  '0644', 
    'tube/index.php' =>  '0644', 
    'tube/readme!!!.txt' =>  '0644', 
    'tube/star.gif' =>  '0644', 
    'tube/htaccess' =>  '0644', 
    'index.html' =>  '0644', 
    'rss.php' =>  '0644', 
    'scj_pro.sql' =>  '0644', 
    'scjwebmaster.php' =>  '0644', 
    'sitemap.php' =>  '0644', 

);


	if ($_SERVER['HTTP_USER_AGENT']) {
		$path = dirname(__FILE__);
		die ("SmartCJ Installation. Run from SSH: <br> <b> cd ".dirname(__FILE__)."; php install_scj.php");
	}

	$cmdline = new prompt();

	$phpversion = phpversion();
	if ($phpversion[0] == 4) {
		$phpversion = 4;
	} elseif ($phpversion[0] == 5) {
		$phpversion = 5;
	} else die("Unknown PHP version, contact support");


	if (!function_exists("imagecreatefromjpeg") or !function_exists("imagecreatetruecolor") or !function_exists( "imagejpeg" ) || !function_exists( "imagedestroy" ) ) die("PHP GD lib is NOT installed. Ask admin to install it.");
	// if (!function_exists("ftp_connect")) die("PHP FTP lib is NOT installed. Ask admin to install it.");
	if (!function_exists("mysql_connect")) die("PHP MySQL lib is NOT installed. Ask admin to install it.");
	if (!function_exists("fsockopen")) die("PHP net functions (fsockopen) is NOT installed. Ask admin to install it.");
	if (!function_exists("session_start")) die("PHP Session support (session_start) is NOT installed. Ask admin to install it.");
	if (!function_exists("exec")) die("PHP exec() function is NOT supported. Ask admin to install it.");

	$scj_folder = ($params['scj_folder']) ? $params['scj_folder'] : $cmdline->get("Folder name for script (default is scj): ");
	if (!$scj_folder) $scj_folder = 'scj';

	$mysql_charset = ($params['mysql_charset']) ? $params['mysql_charset'] : $cmdline->get("Mysql charset (just hit enter if you are going to use latin chars only or type UTF8 in most cases or any other charset): ");
	$mysql_charset = trim($mysql_charset);


    $test_eval = false;
    @eval("\$test_eval=true;");
    if (!$test_eval) die("PHP eval() function is NOT installed. Ask admin to install it.");


	clearstatcache();
	if (is_dir('./' . $scj_folder)) die("Folder {$scj_folder} already exists. Rename or remove it and run install again.");
	@mkdir('./' . $scj_folder);
	if (!is_dir("./" . $scj_folder)) {
		die ("Can not create {$scj_folder} folder, contact support \n");
	}

	$turl = $install_source_url . "/filelist.php?php_version={$phpversion}";
	$tmp = load_url($turl, array('return_info' => true));
	if($tmp['info']['http_code'] != 200) {
		die("Can not get filelist\n");	
	}
	$files = unserialize($tmp['body']);
	if (!is_array($files)) {
		print_r($tmp);
		die("Can not get filelist ({$tmp['info']['http_code']} {$turl}). Please, contact script author. ");
	}

	$scj_path = dirname(__FILE__);
	if (!$scj_path) {
		die ("Can not get path to current folder __FILE__");
	}
	$scj_path .= '/'.$scj_folder.'/';

	$scj_domain = ($params['domain']) ? $params['domain'] : $cmdline->get("Type your domain (only domain ie site.com  - NO WWW, NO HTTP) : ");
	$admin_email = ($params['admin_email']) ? $params['admin_email'] : $cmdline->get("Type your email (will be used as script admin email) : ");

	// locate php
	if (file_exists("/usr/bin/php")) {
		$php_path = "/usr/bin/php";
	} elseif (file_exists("/usr/local/bin/php")) {
		$php_path = "/usr/local/bin/php";
	} else {
		$php_path = ($params['php_path']) ? $params['php_path'] : $cmdline->get("Type PATH to php (ask admin if you don't know) : ");
	}

	// locate mysql 
	if (@file_exists("/usr/bin/mysql")) {
		$mysql_path = '/usr/bin/mysql';
	} elseif (@file_exists("/usr/local/bin/mysql")) {
		$mysql_path = '/usr/local/bin/mysql';
	} else {
		$mysql_path = ($params['mysql_path']) ? $params['mysql_path'] : $cmdline->get("Type PATH to Mysql binany (ask admin if you don't know) (for example /usr/bin/mysql) : ");
	}



	echo "Getting key \n";
	$tmp = load_url('http://smartcj.com/scj_install/get_key.php?type=ic&domain=' . $scj_domain . '&email=' . $admin_email, array('return_info' => true));
	if($tmp['info']['http_code'] != 200) {
		die("Can not get a key");	
	}

	$f = fopen("./{$scj_folder}/scj_pro.lic", 'w');
	fputs($f, $tmp['body']);
	fclose($f);

	$f = fopen("./scj_pro.lic", 'w');
	fputs($f, $tmp['body']);
	fclose($f);


	$tr = array();
	$tr['htaccess'] = '.htaccess';
	$tr['htpasswd'] = '.htpasswd';

	echo "Downloading script files \n";
	foreach ($files as $name => $dest) {
		$count++;

		unset($tmp);
		$url = $install_source_url . $name; //"http://smartcj.com/ioncube/{$name}";
		$try = 0;
		while ($try < 3) {
			$tmp = load_url($url, array('return_info' => true, 'method' => 'get'));
			if ($tmp['info']['http_code'] != 200) {
				$try++;
				continue;
			} else break;
		}
		if ($tmp['info']['http_code'] != 200) {
			print_r($tmp);
			die("Can not download $name, please, contact support {$tmp['info']['http_code']}");
			break;
		}

		$path = './'.$scj_folder.'/' . dirname($dest) . '/';
		$p = 0;
		while ($p = strpos($path, '/', $p+1)) {
			$dir = substr($path, 0, $p);
			if (!is_dir($dir)) {
				if (!mkdir($dir)) die("Can not create $dir");
			}
			
		}

		//echo "Size " . strlen($tmp['body']) . " bytes at ({$tmp['info']['speed_download']}) <br>";
		$percent = round($count/count($files)*100);
		if ($percent % 10 == 0 and $percent > $percent_step) {
			echo $percent . '%';
            $percent_step = $percent;
		} elseif ($percent > $percent_step) {
			echo '.';
            $percent_step = $percent;
		}

		$dest = strtr($dest, $tr);

		if (!$f = fopen($scj_folder . '/' . $dest, 'w')) {
			die("Can not write {$dest}");
		}

		fputs($f, $tmp['body']);
		fclose($f);
	}

	echo "\n Done \n";


	$htaccess = 'DirectoryIndex index.php
AuthType Basic
AuthName "SCJ Admin Area"
AuthUserFile __PATH__/.htpasswd
require valid-user
';

	if (!$params['admin_password']) {
		$passwd = '';
		for ($i=0; $i < 10; $i++) $passwd .= chr(rand(97,122));
	} else $passwd = $params['admin_password'];
	
	$htpasswd = 'admin:' . crypt($passwd);
	
	$ht_deny = 'deny from all';
	
	file_put_contents($scj_folder . '/admin/.htaccess', $htaccess);
	file_put_contents($scj_folder . '/admin/.htpasswd', $htpasswd);

	file_put_contents($scj_folder . '/admin/files/.htaccess', $ht_deny);
	file_put_contents($scj_folder . '/backup/.htaccess', $ht_deny);
	file_put_contents($scj_folder . '/data/.htaccess', $ht_deny);
	file_put_contents($scj_folder . '/logs/.htaccess', $ht_deny);
	file_put_contents($scj_folder . '/cache/.htaccess', $ht_deny);
	file_put_contents($scj_folder . '/includes/libs/.htaccess', $ht_deny);


    $ht_list = array('top', 'top/tt', 'tmp', 'thumbs', 'rot_pages');
    foreach ($ht_list as $z => $folder) {
		if (!$f = fopen('./'.$scj_folder  .'/' . $folder . '/.htaccess', 'w')) {
			die("Can not open " . $folder . '/.htaccess for writing ');
		}
    
		fputs($f, "RemoveHandler .php .phtml .php3 .php4 .php5 .htm .html .shtml\nAddType text/html .php .phtml .php3 .php4 .php5 .htm .html .shtml\n");
		fclose($f);

    }


	echo "Making config.php \n";

	while (true) {

		$mysql_host = ($params['mysql_host']) ? $params['mysql_host'] : $cmdline->get("Mysql host (usually localhost): ");
		$mysql_user = ($params['mysql_user']) ? $params['mysql_user'] : $cmdline->get("Mysql username: ");
		$mysql_pass = ($params['mysql_pass']) ? $params['mysql_pass'] : $cmdline->get("Mysql password: ");
		$mysql_name = ($params['mysql_name']) ? $params['mysql_name'] : $cmdline->get("Mysql DB name: ");

    	if (!$link = mysql_connect($mysql_host, $mysql_user, $mysql_pass)) {
    		echo "Can not connect to mysql using this data, please try again or contact your admin \n";
	    	continue;
    	}

	    if (!mysql_select_db($mysql_name)) {
    		echo "Can not connect to mysql using this data (DB name error), please try again or contact your admin \n";
    		continue;
	    }

    	break;

	}


	// create config

	$tr = array();
	$tr['__DB_HOST__'] = $mysql_host;
	$tr["__DB_USER__"] = $mysql_user;
	$tr["__DB_PASSWORD__"] = $mysql_pass;
	$tr["__DB_NAME__"] = $mysql_name;
	$tr["__PHP_PATH__"] = $php_path;
	$tr["__ADMIN_EMAIL__"] = $admin_email;
	$tr["__PATH_TO_LOG__"] = $scj_path . 'logs/';
	
	

	$config_text = file_get_contents("./{$scj_folder}/includes/config.php");
	$config_text = strtr($config_text, $tr);

	if ($mysql_charset != '') $config_text .= "\n \$config['db_charset'] = '{$mysql_charset}'; \n\n";

	if (!$f = fopen("./{$scj_folder}/includes/config.php", w)) {
		die("Can not open {$scj_folder}/includes/config.php for writing, please contact support \n");
	}
	fputs($f, $config_text);
	fclose($f);


	//create htaccess
	$config_text = file_get_contents("./{$scj_folder}/admin/.htaccess");
	$config_text = str_replace('__PATH__', $scj_path . 'admin', $config_text);

	if (!$f = fopen("./{$scj_folder}/admin/.htaccess", w)) {
		die("Can not open {$scj_folder}/admin/.htaccess for writing, please contact support \n");
	}
	fputs($f, $config_text);
	fclose($f);


	// create common.php

	$tr = array();
	$tr["__path_to_data__"] = $scj_path . 'data/';
	$tr['__local_domain__'] = $scj_domain;


	$config_text = file_get_contents("./{$scj_folder}/cgi/common.php");
	$config_text = str_replace('__SCJ_FOLDER__', $scj_path, $config_text);

	if (!$f = fopen("./{$scj_folder}/cgi/common.php", w)) {
		die("Can not open {$scj_folder}/cgi/common.php for writing, please contact support \n");
	}
	fputs($f, $config_text);
	fclose($f);

	if (!$f = fopen("./{$scj_folder}/tube/common.php", w)) {
		die("Can not open {$scj_folder}/tube/common.php for writing, please contact support \n");
	}
	fputs($f, $config_text);
	fclose($f);


	$text = file_get_contents("{$scj_folder}/scj_pro.sql");
	$text = str_replace('/scj/', "/{$scj_folder}/", $text);
	file_put_contents("{$scj_folder}/scj_pro.sql", $text);

	if ($mysql_charset != '') {
		$text = file_get_contents("{$scj_folder}/scj_pro.sql");
		$text = str_replace(') ENGINE', ") DEFAULT CHARSET={$mysql_charset} ENGINE", $text);
		file_put_contents("{$scj_folder}/scj_pro.sql", $text);

    	$cmd = "{$mysql_path} --default-character-set={$mysql_charset} --user='{$mysql_user}' --password='{$mysql_pass}' --host='{$mysql_host}' {$mysql_name} < {$scj_folder}/scj_pro.sql";
	} else {
    	$cmd = "{$mysql_path} --user='{$mysql_user}' --password='{$mysql_pass}' --host='{$mysql_host}' {$mysql_name} < {$scj_folder}/scj_pro.sql";
	}

	// load mysql DB
    $out = `$cmd`;

	mysql_query("UPDATE rot_linked_db SET rot_linked_id = 0 where linked_db_name = 'main_site' ");

	mysql_query("update settings set value = '".addslashes($admin_email)."' 
	where name = 'system_email'");

	mysql_query("update settings set value = '".addslashes('http://' . $scj_domain .'/')."' 
	where name = 'site_url'");

	if (@file_exists("/usr/bin/convert")) {
		$im_path = "/usr/bin/";
	} elseif (@file_exists("/usr/local/bin/convert")) {
		$im_path = "/usr/local/bin/";
	}

	if ($im_path) {
		mysql_query("UPDATE rot_settings SET value = '".addslashes($im_path)."' WHERE name = 'im_path' ");
		mysql_query("UPDATE rot_settings SET value = 'im' WHERE name = 'img_lib' ");
	}


	mysql_query("UPDATE settings SET value = '".str_replace('/mysql', '/mysqldump', $mysql_path)."' WHERE name = 'mysqldump_path' ");


	$pass = '';
	for ($i=1; $i<=10; $i++) {
		$pass .= chr(rand(97,122));
	}
	mysql_query("update settings set value = '".addslashes($pass)."' where name = 'exchange_password' ");

	// changes paths
	mysql_query("UPDATE settings SET value = REPLACE(value, '/scj/', '/".addslashes($scj_folder)."/') where name IN ('trader_thumbs_url', 'top_default_image', 'top_empty_url') ");

	// chmod
	foreach ($permissions as $file => $perm) {
		if (!file_exists($scj_path . $file)) continue;
		if (!chmod($scj_path . $file, octdec( (int) $perm) )) {
			die("Can not set permission on {$scj_path}{$file}");
		}
	}


	//`cd scj/admin; php update.php`;

	echo "Add following to Crontab (note, if you are not going to use internal rotation you can skip line with rot.php) \n\n";

	echo "#SmartCJ Pro at domain {$scj_domain}\n";
	echo "*/1 * * * * cd {$scj_path}bin; env HTTP_HOST={$scj_domain} {$php_path} -q cron.php \n";
	echo "*/1 * * * * cd {$scj_path}bin; env HTTP_HOST={$scj_domain} {$php_path} -q rot.php \n";


	//if (!$params['domain'])	$mysql_name = $cmdline->get("\n\n Script Installation is done. Please, open http://{$scj_domain}/scj/admin/ in your browser. Your login is 'admin' and password is '{$passwd}'. Press Enter to continue. \n\n");
	echo "\n\n Script Installation is done. Please, open http://{$scj_domain}/{$scj_folder}/admin/ in your browser. Your login is 'admin' and password is '{$passwd}'. \n\n";

	mail($admin_email, 'SmartCJ admin password', "Thank you for installing Smartcj at {$scj_domain}. Your login is admin and password is {$passwd} ");



// =========================================================================================

class prompt {
  var $tty;

  function prompt() {
   if (substr(PHP_OS, 0, 3) == "WIN") {
     $this->tty = fOpen("\con", "rb");
   } else {
     if (!($this->tty = fOpen("/dev/tty", "r"))) {
       $this->tty = fOpen("php://stdin", "r");
     }
   }
  }

  function get($string, $length = 1024) {
   echo $string;
   $result = trim(fGets($this->tty, $length));
   echo "\n";
   return $result;
  }
}



/**
 * Link: http://www.bin-co.com/php/scripts/load/
 * Version : 3.00.A
 */
function load_url($url,$options=array()) {
    $default_options = array(
        'method'        => 'get',
        'post_data'        => false,
        'return_info'    => false,
        'return_body'    => true,
        'cache'            => false,
        'referer'        => '',
        'headers'        => array(),
        'session'        => false,
        'session_close'    => false,
    );

    // Sets the default options.
    foreach($default_options as $opt=>$value) {
        if(!isset($options[$opt])) $options[$opt] = $value;
    }

    $url_parts = parse_url($url);
    $ch = false;
    $info = array(//Currently only supported by curl.
        'http_code'    => 200
    );
    $response = '';
    
    $send_header = array(
        'Accept' => 'text/*',
        'User-Agent' => 'BinGet/1.00.A (http://www.bin-co.com/php/scripts/load/)'
    ) + $options['headers']; // Add custom headers provided by the user.
    
    if($options['cache']) {
        $cache_folder = joinPath(sys_get_temp_dir(), 'php-load-function');
        if(isset($options['cache_folder'])) $cache_folder = $options['cache_folder'];
        if(!file_exists($cache_folder)) {
            $old_umask = umask(0); // Or the folder will not get write permission for everybody.
            mkdir($cache_folder, 0777);
            umask($old_umask);
        }
        
        $cache_file_name = md5($url) . '.cache';
        $cache_file = joinPath($cache_folder, $cache_file_name); //Don't change the variable name - used at the end of the function.
        
        if(file_exists($cache_file)) { // Cached file exists - return that.
            $response = file_get_contents($cache_file);
            
            //Seperate header and content
            $separator_position = strpos($response,"\r\n\r\n");
            $header_text = substr($response,0,$separator_position);
            $body = substr($response,$separator_position+4);
            
            foreach(explode("\n",$header_text) as $line) {
                $parts = explode(": ",$line);
                if(count($parts) == 2) $headers[$parts[0]] = chop($parts[1]);
            }
            $headers['cached'] = true;
            
            if(!$options['return_info']) return $body;
            else return array('headers' => $headers, 'body' => $body, 'info' => array('cached'=>true));
        }
    }

    if(isset($options['post_data']) and $options['post_data']) { //There is an option to specify some data to be posted.
        $options['method'] = 'post';
        
        if(is_array($options['post_data'])) { //The data is in array format.
            $post_data = array();
            foreach($options['post_data'] as $key=>$value) {
                $post_data[] = "$key=" . urlencode($value);
            }
            $url_parts['query'] = implode('&', $post_data);
        } else { //Its a string
            $url_parts['query'] = $options['post_data'];
        }
    } elseif(isset($options['multipart_data'])) { //There is an option to specify some data to be posted.
        $options['method'] = 'post';
        $url_parts['query'] = $options['multipart_data'];
        /*
            This array consists of a name-indexed set of options.
            For example,
            'name' => array('option' => value)
            Available options are:
            filename: the name to report when uploading a file.
            type: the mime type of the file being uploaded (not used with curl).
            binary: a flag to tell the other end that the file is being uploaded in binary mode (not used with curl).
            contents: the file contents. More efficient for fsockopen if you already have the file contents.
            fromfile: the file to upload. More efficient for curl if you don't have the file contents.

            Note the name of the file specified with fromfile overrides filename when using curl.
         */
    }

    ///////////////////////////// Curl /////////////////////////////////////
    //If curl is available, use curl to get the data.
    if(function_exists("curl_init") 
                and (!(isset($options['use']) and $options['use'] == 'fsocketopen'))) { //Don't use curl if it is specifically stated to use fsocketopen in the options
        
        if(isset($options['post_data']) and $options['post_data']) { //There is an option to specify some data to be posted.
            $page = $url;
            $options['method'] = 'post';
            
            if(is_array($options['post_data'])) { //The data is in array format.
                $post_data = array();
                foreach($options['post_data'] as $key=>$value) {
                    $post_data[] = "$key=" . urlencode($value);
                }
                $url_parts['query'] = implode('&', $post_data);
            
            } else { //Its a string
                $url_parts['query'] = $options['post_data'];
            }
        } else {
            if(isset($options['method']) and $options['method'] == 'post') {
                $page = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];
            } else {
                $page = $url;
            }
        }

        if($options['session'] and isset($GLOBALS['_binget_curl_session'])) $ch = $GLOBALS['_binget_curl_session']; //Session is stored in a global variable
        else $ch = curl_init($url_parts['host']);

       
        curl_setopt($ch, CURLOPT_URL, $page) or die("Invalid cURL Handle Resouce");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Just return the data - not print the whole thing.
        curl_setopt($ch, CURLOPT_HEADER, true); //We need the headers
        curl_setopt($ch, CURLOPT_NOBODY, !($options['return_body'])); //The content - if true, will not download the contents. There is a ! operation - don't remove it.
        $tmpdir = NULL; //This acts as a flag for us to clean up temp files
        if(isset($options['method']) and $options['method'] == 'post' and isset($url_parts['query'])) {
            curl_setopt($ch, CURLOPT_POST, true);
            if(is_array($url_parts['query'])) {
                //multipart form data (eg. file upload)
                $postdata = array();
                foreach ($url_parts['query'] as $name => $data) {
                    if (isset($data['contents']) && isset($data['filename'])) {
                        if (!isset($tmpdir)) { //If the temporary folder is not specifed - and we want to upload a file, create a temp folder.
                            //  :TODO:
                            $dir = sys_get_temp_dir();
                            $prefix = 'load';
                            
                            if (substr($dir, -1) != '/') $dir .= '/';
                            do {
                                $path = $dir . $prefix . mt_rand(0, 9999999);
                            } while (!mkdir($path, $mode));
                        
                            $tmpdir = $path;
                        }
                        $tmpfile = $tmpdir.'/'.$data['filename'];
                        file_put_contents($tmpfile, $data['contents']);
                        $data['fromfile'] = $tmpfile;
                    }
                    if (isset($data['fromfile'])) {
                        // Not sure how to pass mime type and/or the 'use binary' flag
                        $postdata[$name] = '@'.$data['fromfile'];
                    } elseif (isset($data['contents'])) {
                        $postdata[$name] = $data['contents'];
                    } else {
                        $postdata[$name] = '';
                    }
                }
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            } else {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $url_parts['query']);
            }
        }

        //Set the headers our spiders sends
        curl_setopt($ch, CURLOPT_USERAGENT, $send_header['User-Agent']); //The Name of the UserAgent we will be using ;)
        $custom_headers = array("Accept: " . $send_header['Accept'] );
        if(isset($options['modified_since']))
            array_push($custom_headers,"If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T',strtotime($options['modified_since'])));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);
        if($options['referer']) curl_setopt($ch, CURLOPT_REFERER, $options['referer']);

        @curl_setopt($ch, CURLOPT_COOKIEJAR, "./binget-cookie.txt"); //If ever needed...
        @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $custom_headers = array();
        unset($send_header['User-Agent']); // Already done (above)
        foreach ($send_header as $name => $value) {
            if (is_array($value)) {
                foreach ($value as $item) {
                    $custom_headers[] = "$name: $item";
                }
            } else {
                $custom_headers[] = "$name: $value";
            }
        }
        if(isset($url_parts['user']) and isset($url_parts['pass'])) {
            $custom_headers[] = "Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']);
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $custom_headers);

        $response = curl_exec($ch);

        if(isset($tmpdir)) {
            //rmdirr($tmpdir); //Cleanup any temporary files :TODO:
        }

        $info = curl_getinfo($ch); //Some information on the fetch
        
        if($options['session'] and !$options['session_close']) $GLOBALS['_binget_curl_session'] = $ch; //Dont close the curl session. We may need it later - save it to a global variable
        else curl_close($ch);  //If the session option is not set, close the session.

    //////////////////////////////////////////// FSockOpen //////////////////////////////
    } else { //If there is no curl, use fsocketopen - but keep in mind that most advanced features will be lost with this approch.

        if(!isset($url_parts['query']) || (isset($options['method']) and $options['method'] == 'post'))
            $page = $url_parts['path'];
        else
            $page = $url_parts['path'] . '?' . $url_parts['query'];
        
        if(!isset($url_parts['port'])) $url_parts['port'] = ($url_parts['scheme'] == 'https' ? 443 : 80);
        $host = ($url_parts['scheme'] == 'https' ? 'ssl://' : '').$url_parts['host'];
        $fp = fsockopen($host, $url_parts['port'], $errno, $errstr, 30);
        if ($fp) {
            $out = '';
            if(isset($options['method']) and $options['method'] == 'post' and isset($url_parts['query'])) {
                $out .= "POST $page HTTP/1.1\r\n";
            } else {
                $out .= "GET $page HTTP/1.0\r\n"; //HTTP/1.0 is much easier to handle than HTTP/1.1
            }
            $out .= "Host: $url_parts[host]\r\n";
        foreach ($send_header as $name => $value) {
        if (is_array($value)) {
            foreach ($value as $item) {
            $out .= "$name: $item\r\n";
            }
        } else {
            $out .= "$name: $value\r\n";
        }
        }
            $out .= "Connection: Close\r\n";
            
            //HTTP Basic Authorization support
            if(isset($url_parts['user']) and isset($url_parts['pass'])) {
                $out .= "Authorization: Basic ".base64_encode($url_parts['user'].':'.$url_parts['pass']) . "\r\n";
            }

            //If the request is post - pass the data in a special way.
            if(isset($options['method']) and $options['method'] == 'post') {
                if(is_array($url_parts['query'])) {
                    //multipart form data (eg. file upload)

                    // Make a random (hopefully unique) identifier for the boundary
                    srand((double)microtime()*1000000);
                    $boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);

                    $postdata = array();
                    $postdata[] = '--'.$boundary;
                    foreach ($url_parts['query'] as $name => $data) {
                        $disposition = 'Content-Disposition: form-data; name="'.$name.'"';
                        if (isset($data['filename'])) {
                            $disposition .= '; filename="'.$data['filename'].'"';
                        }
                        $postdata[] = $disposition;
                        if (isset($data['type'])) {
                            $postdata[] = 'Content-Type: '.$data['type'];
                        }
                        if (isset($data['binary']) && $data['binary']) {
                            $postdata[] = 'Content-Transfer-Encoding: binary';
                        } else {
                            $postdata[] = '';
                        }
                        if (isset($data['fromfile'])) {
                            $data['contents'] = file_get_contents($data['fromfile']);
                        }
                        if (isset($data['contents'])) {
                            $postdata[] = $data['contents'];
                        } else {
                            $postdata[] = '';
                        }
                        $postdata[] = '--'.$boundary;
                    }
                    $postdata = implode("\r\n", $postdata)."\r\n";
                    $length = strlen($postdata);
                    $postdata = 'Content-Type: multipart/form-data; boundary='.$boundary."\r\n".
                                'Content-Length: '.$length."\r\n".
                                "\r\n".
                                $postdata;

                    $out .= $postdata;
                } else {
                    $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
                    $out .= 'Content-Length: ' . strlen($url_parts['query']) . "\r\n";
                    $out .= "\r\n" . $url_parts['query'];
                }
            }
            $out .= "\r\n";

            fwrite($fp, $out);
            while (!feof($fp)) {
            	$currentHeader = fgets($fp, 128);
		        if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status)) {
        			$info['http_code'] = $status[1];
		        }

      			if($currentHeader == "\r\n" or $currentHeader == "\n") break;
      			$header_text .= $currentHeader;
            }
            while (!feof($fp)) {
                $response .= fgets($fp, 128);
            }
            fclose($fp);
        }
    }

    //Get the headers in an associative array
    $headers = array();

    if($info['http_code'] == 404) {
        $body = "";
        $headers['Status'] = 404;
    } else {
        //Seperate header and content
        $header_text = substr($response, 0, $info['header_size']);
        $body = substr($response, $info['header_size']);
        
        foreach(explode("\n",$header_text) as $line) {
            $parts = explode(": ",$line);
            if(count($parts) == 2) {
                if (isset($headers[$parts[0]])) {
                    if (is_array($headers[$parts[0]])) $headers[$parts[0]][] = chop($parts[1]);
                    else $headers[$parts[0]] = array($headers[$parts[0]], chop($parts[1]));
                } else {
                    $headers[$parts[0]] = chop($parts[1]);
                }
            }
        }

    }
    
    if(isset($cache_file)) { //Should we cache the URL?
        file_put_contents($cache_file, $response);
    }

    if($options['return_info']) return array('headers' => $headers, 'body' => $body, 'info' => $info, 'curl_handle'=>$ch);
    return $body;
} 


