<?php include('header.php'); ?>
<?php 
if (!empty($_GET['case'])) {
$case = make_safe($_GET['case']);	
} else {
$case = '';	
}
switch ($case) {
case 'optimize_database';
if (isset($_POST['save'])) {
	$result = $mysqli->query("SHOW TABLES FROM $db_config[name]");
	while ($row = $result->fetch_row()) {
	$optimize = $mysqli->query("OPTIMIZE TABLE $row[0]");
	}
	if ($optimize) {
	$message = notification('success','Database Optimized Successfuly.');
	} else {
	$message = notification('danger','Error Happened.');
	}
}   
?>

                <div class="page-header page-heading">
                    <h1><i class="fa fa-database"></i> Optimize Database</h1>
                </div>
	<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label for="facebook_account">Are you sure that you want to Optimize the Database ?</label>
		  </div>
		  <button type="submit" name="save" class="btn btn-warning">Optimize Database</button>
		</form>
	
<?php 
break;
case 'clear_cache';
if (isset($_POST['save'])) {
	$folder = '../cache';
	$delete = empty_templates_cache($folder);
	if ($delete) {
	$message = notification('success','All Cache Files Are Cleared.');
	} else {
	$message = notification('danger','Error Happened.');
	}
}   
?>

                <div class="page-header page-heading">
                    <h1><i class="fa fa-eraser"></i> Clear Cache</h1>
                </div>
	<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label for="facebook_account">Are you sure that you want to clear all cached files ?</label>
		  </div>
		  <button type="submit" name="save" class="btn btn-danger">Clear Cache</button>
		</form>
	
<?php 
break;
case 'bulk_remove';
if (isset($_POST['save'])) {
	$period = intval($_POST['period']);
	if (isset($_POST['category'])) {
	$category = intval($_POST['category']);
	} else {
	$category = 0;	
	}
	if (isset($_POST['site'])) {
	$site = make_safe(xss_clean($_POST['site']));
	} else {
	$site = '';
	}
	$now = time();
	$delete_time = $now-$period;
	$where = "datetime < $delete_time";
	if (!empty($site)) {
	$where .= " AND site='$site'";	
	}
	
	if ($category != 0) {
	$where .= " AND category_id='$category'";	
	}
	
	$sql = "SELECT * FROM media WHERE $where";
	$query = $mysqli->query($sql);
	$isthere = $query->num_rows;
	if ($isthere > 0) {
	while ($row = $query->fetch_assoc()) {
		if (!empty($row['thumbnail']) AND file_exists('../upload/media/'.$row['thumbnail'])) {
			@unlink('../upload/media/'.$row['thumbnail']);
		}
	}
		$mysqli->query("DELETE FROM lists_media WHERE media_id='$row[id]'");
	}
	$delete = $mysqli->query("DELETE FROM media WHERE $where");
	$affected = $mysqli->affected_rows;
	if ($delete) {
	if ($affected == 0) {
	$message = notification('warning','There Are no Media to Delete.');	
	} else {	
	$message = notification('success','All Selected Media Are Cleared.');
	}
	} else {
	$message = notification('danger','Error Happened.');
	}
}   
?>

        <div class="page-header page-heading">
            <h1><i class="fa fa-trash"></i> Bulk Remove</h1>
        </div>
	<?php if (isset($message)) {echo $message;} else {echo notification('warning','Be careful, This Action can\'t be undo.');} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
		  <div class="row">
		  <div class="col-md-4">
			<label for="period">Select The Period</label>
			<select class="form-control" name="period" id="period">
				<option value="31104000">Older Than a Year</option>
				<option value="15552000">Older Than 6 months</option>
				<option value="7776000">Older Than 3 months</option>
				<option value="2592000">Older Than a month</option>
				<option value="1209600">Older Than 2 Weeks</option>
				<option value="604800">Older Than a Week</option>
				<option value="259200">Older Than 3 Days</option>
				<option value="1">All Time</option>				
			</select>
		  </div>
		  <div class="col-md-4">
			<label for="category">Select Category</label>
			<select class="form-control" name="category" id="category">
				<option value="0">All Categories</option>
				<?php 
				$categories = $general->categories('category_order ASC');
				foreach ($categories AS $category) {
				?>
				<option value="<?php echo $category['id']; ?>"><?php echo $category['category']; ?></option>
				<?php	
				}
				?>
			</select>
		  </div>
		  <div class="col-md-4">
			<label for="source">Select Site</label>
			<select class="form-control" name="source" id="source">
				<option value="">All Sites</option>
				<option value="youtube">YouTube</option>
				<option value="vimeo">Vimeo</option>
				<option value="dailymotion">DailyMotion</option>
			</select>
		  </div>
		  <div class="clear"></div>
		  </div>
		  </div>
		  <button type="submit" name="save" class="btn btn-danger">Remove</button>
		</form>
	
<?php 
break;
case 'social';
if (isset($_POST['save'])) {
$message = $general->set_options($_POST,'Social');
}
$options = $general->get_options('Social'); 
?>

                <div class="page-header page-heading">
                    <h1><i class="fa fa-twitter"></i> Social Setting</h1>
                </div>
	<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label for="facebook_account">Facebook Page</label>
			<div class="input-group">
				<span class="input-group-addon"><i class="fa fa-facebook"></i></span>
				<input type="text" class="form-control" name="facebook_account" id="facebook_account" placeholder="https://facebook.com/yourpage" value="<?php echo $options['facebook_account']; ?>" />
			</div>
		  </div>
		  <div class="form-group">
			<label for="twitter_account">Twitter Account</label>
			<div class="input-group">
				<span class="input-group-addon"><i class="fa fa-twitter"></i></span>
				<input type="text" class="form-control" name="twitter_account" id="twitter_account" placeholder="http://twitter.com/your_id" value="<?php echo $options['twitter_account']; ?>" />
			</div>
		  </div>
		  <div class="form-group">
			<label for="google_plus_account">Google+ Account</label>
			<div class="input-group">
				<span class="input-group-addon"><i class="fa fa-google-plus"></i></span>
				<input type="text" class="form-control" name="google_plus_account" id="google_plus_account" placeholder="http://plus.google.com/account" value="<?php echo $options['google_plus_account']; ?>" />
			</div>
		  </div>
		  <div class="form-group">
			<label for="youtube_account">Youtube Channel</label>
			<div class="input-group">
				<span class="input-group-addon"><i class="fa fa-youtube"></i></span>
				<input type="text" class="form-control" name="youtube_account" id="youtube_account" placeholder="http://youtube.com/user/channel_name" value="<?php echo $options['youtube_account']; ?>" />
		    </div>
		  </div>
		  <div class="form-group">
			<label for="vimeo_account">Vimeo Channel</label>
			<div class="input-group">
				<span class="input-group-addon"><i class="fa fa-vimeo"></i></span>
				<input type="text" class="form-control" name="vimeo_account" id="vimeo_account" placeholder="http://vimeo.com/user/channel_name" value="<?php echo $options['vimeo_account']; ?>" />
		    </div>
		  </div>
		  <div class="form-group">
			<label for="dailymotion_account">DailyMotion Channel</label>
			<div class="input-group">
				<span class="input-group-addon"><i class="fa fa-youtube-play"></i></span>
				<input type="text" class="form-control" name="dailymotion_account" id="dailymotion_account" placeholder="http://youtube.com/user/channel_name" value="<?php echo $options['dailymotion_account']; ?>" />
		    </div>
		  </div>
		  <button type="submit" name="save" class="btn btn-primary">Save</button>
		</form>
	
<?php 
break;
case 'apis';
if (isset($_POST['save'])) {
$message = $general->set_options($_POST,'API');
}
$options = $general->get_options('API'); 
?>

                <div class="page-header page-heading">
                    <h1><i class="fa fa-key"></i> Api's Setting</h1>
                </div>
	<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label for="recaptcha_public">ReCaptcha Public Key</label>
			<input type="text" class="form-control" name="recaptcha_public" id="recaptcha_public" value="<?php echo $options['recaptcha_public']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="recaptcha_private">ReCaptcha Private Key</label>
			<input type="text" class="form-control" name="recaptcha_private" id="recaptcha_private" value="<?php echo $options['recaptcha_private']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="youtube_apikey">YouTube API Key</label>
			<input type="text" class="form-control" name="youtube_apikey" id="youtube_apikey" value="<?php echo $options['youtube_apikey']; ?>" />
		  </div>
		  
		  <button type="submit" name="save" class="btn btn-primary">Save</button>
		</form>
	
<?php 
break;

case 'mail';
if (isset($_POST['save'])) {
$message = $general->set_options($_POST,'Mail');
}
$options = $general->get_options('Mail'); 
?>

        <div class="page-header page-heading">
            <h1><i class="fa fa-envelope"></i> Mail Setting</h1>
        </div>
		<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  
		  <div class="form-group">
			<label for="mail_method">Send Mail Method</label>
			<div><input type="radio" name="mail_method" onclick="javascript:ShowDiv('mail_div');" value="mail" <?php if ($options['mail_method'] == 'mail') {echo 'CHECKED';} ?> /> <b>PHP Mail Function</b></div>
			<div><input type="radio" name="mail_method" onclick="javascript:ShowDiv('smtp_div');" value="smtp" <?php if ($options['mail_method'] == 'smtp') {echo 'CHECKED';} ?> /> <b>SMTP</b> (Recommended)</div>
		  </div>
		  <div id="mail_div">
		  <div class="form-group">
			<label for="send_email">E-Mail</label>
			<input type="text" class="form-control" name="send_email" id="send_email" value="<?php echo $options['send_email']; ?>" />
		  </div>
		  </div>
		  <div id="smtp_div">
		  <div class="form-group">
			<label for="smtp_host">SMTP Host</label>
			<input type="text" class="form-control" name="smtp_host" id="smtp_host" value="<?php echo $options['smtp_host']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="smtp_port">SMTP Port</label>
			<input type="text" class="form-control" name="smtp_port" id="smtp_port" value="<?php echo $options['smtp_port']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="smtp_username">SMTP Username</label>
			<input type="text" class="form-control" name="smtp_username" id="smtp_username" value="<?php echo $options['smtp_username']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="smtp_password">SMTP Password</label>
			<input type="text" class="form-control" name="smtp_password" id="smtp_password" value="<?php echo $options['smtp_password']; ?>" />
		  </div>
		  </div>
		  <button type="submit" name="save" class="btn btn-primary">Save</button>
		</form>
<?php 
break;
case 'theme';
$theme = $general->get_options('General');
if (isset($_POST['save'])) {
	print_r($_POST);
$message = $general->set_options($_POST,'Theme');
}
$options = $general->get_options('Theme'); 
include('../themes/'.$theme['site_theme'].'/options.php');
break;
case 'comments';
if (isset($_POST['save'])) {
$message = $general->set_options($_POST,'Comments');
}
$options = $general->get_options('Comments'); 
?>

                <div class="page-header page-heading">
                    <h1><i class="fa fa-comment"></i> Comments Setting</h1>
                </div>
	<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<input type="hidden" name="display_comments" value="0" />
			<input type="checkbox" name="display_comments" id="display_comments" value="1" <?php if ($options['display_comments'] == 1) {echo 'CHECKED';} ?> /> <span class="checkbox-label">Display Comments ?</span>
		  </div>
		  <div class="form-group">
			<label for="disqus_shortname">Disqus Shortname</label>
			<input type="text" class="form-control" name="disqus_shortname" id="disqus_shortname" value="<?php echo $options['disqus_shortname']; ?>" />
		  </div>
		  <button type="submit" name="save" class="btn btn-primary">Save</button>
		</form>
	
<?php 
break;
case 'users';
if (isset($_POST['save'])) {
$message = $general->set_options($_POST,'Users');
}
$options = $general->get_options('Users'); 
?>

                <div class="page-header page-heading">
                    <h1><i class="fa fa-users"></i> Users Setting</h1>
                </div>
	<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label>Activate New Users</label>
			<div><input type="radio" name="activate_new_users" value="by_email" <?php if (isset($options['activate_new_users']) AND $options['activate_new_users'] == 'by_email') {echo 'CHECKED';} ?> /> By E-Mail</div>
			<div><input type="radio" name="activate_new_users" value="direct" <?php if (isset($options['activate_new_users']) AND $options['activate_new_users'] == 'direct') {echo 'CHECKED';} ?> /> Direct Activation</div>
		  </div>
		  <div class="form-group">
			<label for="cookie_name">Cookie Name</label>
			<input type="text" class="form-control" name="cookie_name" id="cookie_name" value="<?php if (isset($options['cookie_name'])) { echo $options['cookie_name']; } ?>" />
		  </div>
		  <div class="form-group">
			<label for="cookie_expiration_time">Cookie Expiration Time</label>
			<select class="form-control" name="cookie_expiration_time" id="cookie_expiration_time">
				<?php 
				for ($c=1;$c<11;$c++) {
				?>
				<option value="<?php echo $c; ?>" <?php if ($options['users_cookie_expiration_time'] == $c) {echo 'SELECTED';} ?>><?php echo $c; ?> Day(s)</option>
				<?php	
				}
				?>
			</select>
		  </div>
		  <div class="form-group">
			<input type="hidden" name="allow_facebook_login" value="0" />
			<input type="checkbox" name="allow_facebook_login" id="allow_facebook_login" value="1" <?php if (isset($options['allow_facebook_login']) AND $options['allow_facebook_login'] == 1) {echo 'CHECKED';} ?> /> <span class="checkbox-label">Allow Users Login and Register Using Facebook Account ?</span>
		  </div>
		  <div id="facebook-login-div">
		  <div class="form-group">
			<label for="facebook_app_id">Facebook App ID</label>
			<input type="text" class="form-control" name="facebook_app_id" id="facebook_app_id" value="<?php echo $options['facebook_app_id']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="facebook_app_secret">Facebook App Secret</label>
			<input type="text" class="form-control" name="facebook_app_secret" id="facebook_app_secret" value="<?php echo $options['facebook_app_secret']; ?>" />
		  </div>
		  </div>
		  <div class="form-group">
			<input type="hidden" name="allow_twitter_login" value="0" />
			<input type="checkbox" name="allow_twitter_login" id="allow_twitter_login" value="1" <?php if (isset($options['allow_twitter_login']) AND $options['allow_twitter_login'] == 1) {echo 'CHECKED';} ?> /> <span class="checkbox-label">Allow Users Login and Register Using Twitter Account ?</span>
		  </div>
		   <div id="twitter-login-div">
		  <div class="form-group">
			<label for="twitter_consumer_key">Twitter Consumer Key</label>
			<input type="text" class="form-control" name="twitter_consumer_key" id="twitter_consumer_key" value="<?php echo $options['twitter_consumer_key']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="twitter_consumer_secret">Twitter Consumer Secret</label>
			<input type="text" class="form-control" name="twitter_consumer_secret" id="twitter_consumer_secret" value="<?php echo $options['twitter_consumer_secret']; ?>" />
		  </div>
		  </div>
		  <div class="form-group">
			<label>User's Media Publishing</label>
			<div><input type="radio" name="media_publishing" value="1" <?php if (isset($options['media_publishing']) AND $options['media_publishing'] == 1) {echo 'CHECKED';} ?> /> <b>Published</b></div>
			<div><input type="radio" name="media_publishing" value="0" <?php if (isset($options['media_publishing']) AND $options['media_publishing'] == 0) {echo 'CHECKED';} ?> /> <b>Draft</b> (Published After Admin Approve)</div>
		  </div>
		  <div class="form-group">
			<label for="max_lists_number">Number Of Lists That User Can Create</label>
			<input type="number" class="form-control" name="max_lists_number" id="max_lists_number" value="<?php if (isset($options['max_lists_number'])) { echo $options['max_lists_number']; } ?>" />
			<div class="help">0 = Unlimited Lists</div>	
		  </div>
		  <div class="form-group">
			<label for="max_media_list_number">Maximum Number Of Media Items in each List</label>
			<input type="number" class="form-control" name="max_media_list_number" id="max_media_list_number" value="<?php if (isset($options['max_media_list_number'])) { echo $options['max_media_list_number']; } ?>" />
		    <div class="help">0 = Unlimited Media Items</div>	
		  </div>
		  <button type="submit" name="save" class="btn btn-primary">Save</button>
		</form>
	
<?php 
break;
default;
if (isset($_POST['save'])) {
$message = $general->set_options($_POST,'General');
}
$options = $general->get_options('General'); 
?>
        <div class="page-header page-heading">
            <h1><i class="fa fa-cog"></i> General Setting</h1>
        </div>
		<?php if (isset($message)) {echo $message;} ?>
		<form role="form" method="POST" action="">
		  <div class="form-group">
			<label for="siteurl">Site Url</label>
			<input type="text" class="form-control" name="siteurl" id="siteurl" placeholder="http://www.domain.com" value="<?php echo $options['siteurl']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="seo_title">Site Name</label>
			<input type="text" class="form-control" name="seo_title" id="seo_title" placeholder="your site title" value="<?php echo $options['seo_title']; ?>" />
		  </div>
		  
		  <div class="form-group">
			<label for="seo_keywords">SEO Keywords</label>
			<input type="text" class="form-control" name="seo_keywords" id="seo_keywords" placeholder="news,rss,feeds" value="<?php echo $options['seo_keywords']; ?>" />
		  </div>
		  <div class="form-group">
			<label for="seo_description">SEO Description</label>
			<textarea class="form-control" name="seo_description" id="seo_description" rows="3" placeholder="some words about the site .. don't exceed 255 character."><?php echo $options['seo_description']; ?></textarea>
		  </div>
		  <div class="form-group">
			<label for="site_language">Site Language</label>
			<select name="site_language" id="site_language" class="form-control">
				<?php
				$lpath = '../languages/';
				$lresults = glob($lpath . "*");
					foreach ($lresults as $lresult) {
						if ($lresult === '.' or $lresult === '..') continue;
						if(is_dir($lresult)) {
						
						echo "
						<option value='".str_replace($lpath,'',$lresult)."' ";
						if (isset($options['site_language']) AND $options['site_language'] == str_replace($lpath,'',$lresult)) {
						echo 'SELECTED';
						}
						echo ">".ucfirst(str_replace($lpath,'',$lresult))."</option>";		
						}
						}
						?>						
			</select>
		   </div>
		   <div class="form-group">
			<label for="site_theme">Site Theme</label>
			<select name="site_theme" id="site_theme" class="form-control">
				<?php
				$path = '../themes/';
				$results = glob($path . "*");
					foreach ($results as $result) {
						if ($result === '.' or $result === '..') continue;
						if(is_dir($result)) {
						
						echo "
						<option value='".str_replace($path,'',$result)."'";
						if (isset($options['site_theme']) AND $options['site_theme'] == str_replace($path,'',$result)) {
						echo 'SELECTED';
						}
						echo ">".ucfirst(str_replace($path,'',$result))."</option>";		
						}
						}
						?>						
			</select>
		   </div>
		  <div class="form-group">
			<input type="hidden" name="category_sitemap" value="0" />
			<input type="checkbox" name="category_sitemap" id="category_sitemap" value="1" <?php if (isset($options['category_sitemap']) AND $options['category_sitemap'] == 1) {echo 'CHECKED';} ?> /> <span class="checkbox-label">Include Categories in Sitemap ?</span>
		  </div>
		  <div class="form-group">
			<input type="hidden" name="lists_sitemap" value="0" />
			<input type="checkbox" name="lists_sitemap" id="lists_sitemap" value="1" <?php if (isset($options['lists_sitemap']) AND $options['lists_sitemap'] == 1) {echo 'CHECKED';} ?> /> <span class="checkbox-label">Include Public Lists in Sitemap ?</span>
		  </div>
		  <div class="form-group">
			<label for="sitemap_items">Number Of Items Per Sitemap</label>
			<input type="text" class="form-control" name="sitemap_items" id="sitemap_items" placeholder="10000" value="<?php if (isset($options['sitemap_items'])) { echo $options['sitemap_items'];} ?>" />
			<p class="help">If you leave it blank, the default is : 10000 per sitemap.</p>
		  </div>
		  <div class="form-group">
			<label for="rss_media_number">Number Of Items In RSS</label>
			<input type="text" class="form-control" name="rss_media_number" id="rss_media_number" placeholder="10" value="<?php if (isset($options['rss_media_number'])) { echo $options['rss_media_number'];} ?>" />
		  </div>
		  <button type="submit" name="save" class="btn btn-primary">Save</button>
		</form>
<?php } ?>
<?php include('footer.php'); ?>
 