<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

function get_catnamefromid($id) 
{
	$id = (int) $id;
	if ($id == 0)
		return '- Root -';

	$categories = load_categories();
	return $categories[$id]['name'];
}

function vnamefromvid($vid) 
{
	$type = 'video';

	if (strpos($vid, 'article') !== false)
	{
		$pieces = explode('-', $vid);
		$id = (int) $pieces[1];
		$q = mysql_query("SELECT title FROM art_articles WHERE id = '". $id ."'");
		$type = 'article';
	}
	else
	{
		$q = mysql_query("SELECT artist, video_title FROM pm_videos WHERE uniq_id = '".$vid."'");
	}
	
	$r = mysql_fetch_assoc($q);

	if ($type == 'video')
	{
		$title = $r['artist']." - ".$r['video_title'];
	}
	else if ($type == 'article')
	{
		$title = $r['title'];
	}
	
	mysql_free_result($q);
	
	return (strlen($title) > 0) ? $title : 'Missing title';
}


function a_list_videos($search_term, $search_type = 'video_title', $from = 0, $to = 20, $page = 1, $filter = "", $filter_value = "") 
{
	global $userdata;
	
	if( ! $page)	$page = 1;
	
	if(!empty($search_term) && $search_type == 'video_title' ) 
	{
		$query = mysql_query("SELECT * FROM pm_videos WHERE video_title LIKE '%".$search_term."%' ORDER BY id DESC");
	} 
	else if(!empty($search_term) && $search_type == 'artist' ) 
	{
		$query = mysql_query("SELECT * FROM pm_videos WHERE artist LIKE '%".$search_term."%' ORDER BY id DESC");
	} 
	else if(!empty($search_term) && $search_type == 'yt_id' )
	{
		$query = mysql_query("SELECT * FROM pm_videos WHERE yt_id LIKE '".$search_term."' ORDER BY id DESC");
	} 
	else if(!empty($search_term) && $search_type == 'uniq_id' )
	{
		$query = mysql_query("SELECT * FROM pm_videos WHERE uniq_id LIKE '".$search_term."' ORDER BY id DESC");
	}
	else 
	{
		$sql = '';
		if($filter != '')
		{
			$sql = "SELECT * FROM pm_videos ";
			switch($filter)
			{
				case 'broken':
				
					$sql .= " WHERE status='".VS_BROKEN."' ";
					
				break;
				
				case 'restricted':
				
					$sql .= " WHERE status='".VS_RESTRICTED."' ";
					
				break;
				
				case 'unchecked':
				
					$sql .= " WHERE status='".VS_UNCHECKED."' AND source_id = '3' ";
					
				break;
				
				case 'localhost':
				
					$sql .= " WHERE source_id='1' ";
					
				break;
				
				case 'featured':
				
					$sql .= " WHERE featured='1' ";
					
				break;
				
				case 'category':
					
					$sql .= " WHERE category LIKE '". $filter_value ."' 
						   OR category LIKE '". $filter_value .",%' 
						   OR category LIKE '%,". $filter_value ."' 
						   OR category LIKE '%,". $filter_value .",%' ";
						   
				break;
				
				case 'source':
				
					$sql .= " WHERE source_id='". $filter_value ."' ";
					
				break;
				
				case 'access':
					
					$sql .= " WHERE restricted = '". $filter_value ."' ";
					
				break;
			}
			
			$sql .= " ORDER BY ";
			
			if ($filter == 'mostviewed')
			{
				$sql .= " site_views DESC ";
			} 
			else
			{
				$sql .= " id DESC ";
			}
			$sql .= " LIMIT ".$from.", ".$to;
		}
		else
		{
			$sql = "SELECT * FROM pm_videos ORDER BY id DESC LIMIT ".$from.", ".$to;
		}
		$query = mysql_query($sql);
	}

	$count = mysql_num_rows($query);	
	$categories = a_list_cats_simple();

	// LIST VIDEOS
	if($count >= 1) 
	{
		$videos = '';
		$sources = a_fetch_video_sources();
		
		$alt = 1;
		while($r = mysql_fetch_array($query)) 
		{
			$alt++;
			if(!empty($r['last_check']) && $r['last_check'] != '0') {
				$last_check = date("M d, Y h:i a", $r['last_check']);
			} else {
				$last_check = 'n/a';		
			}
			$status = '';
			$status_img = '';
			switch($r['status'])
			{
				default:
				case VS_UNCHECKED: 	$status = "Video Status: Unchecked";		$status_img = VS_UNCHECKED_IMG;		break;
				case VS_OK: 		$status = "Video Status: OK";				$status_img = VS_OK_IMG; 			break;
				case VS_BROKEN: 	$status = "Video Not Found";				$status_img = VS_BROKEN_IMG; 		break;
				case VS_RESTRICTED:	$status = "Video Status: Geo-restricted";	$status_img = VS_RESTRICTED_IMG;	break;
			}
			//$status_img .= ".png";

			//	Video row
			$videos .= '<tr>';
			//	checkbox
			$videos .= '<td align="center" width="3%">';
			
			if(in_array($r['source_id'], array(3, /*22,*/ 1, 2)))	//	Youtube, MTV(deprecated), "localhost" and "other"
			{
				$videos .= "<input name=\"video_ids[]\" type=\"checkbox\" value=\"".$r['uniq_id']."\" id=\"".$r['id']."\" />";
			}
			else
			{
				$videos .= "<input name=\"video_ids[]\" type=\"checkbox\" value=\"".$r['uniq_id']."\" />";
				$status_img = VS_NOTAVAILABLE_IMG;
				$status = "Not Available";
			}
			
			//	Video Source Icon
			$source_icon = strtolower($sources[$r['source_id']]['source_name']);
			if ($r['source_id'] == 0)
			{
				$source_icon = 'embed_flash';
			}
			if ($r['source_id'] == 1 || $r['source_id'] == 2)
			{
				$source_icon = strtolower(array_pop(explode('.', $r['url_flv'])));
			}
			
			$source_icon_title = ucfirst($sources[$r['source_id']]['source_name']);
			if ($sources[$r['source_id']]['source_name'] == '')
			{
				$source_icon_title = 'Embedded';
			}

			$videos .= '</td>';
			$videos .= '<td align="center" width="3%">';
			//	video source icon
			$videos .= ' <a href="videos.php?page=1&filter=source&fv='. $sources[$r['source_id']]['source_id'] .'">';
			$videos .= ' <img src="src/icons/'. $source_icon .'.gif" width="16" height="16" border="0" ';
			$videos .= 'align="absmiddle" class="hintanchor" alt="List '. $source_icon_title .' videos only" ';
			$videos .= 'title="" onMouseover="showhint(\'Show only this type of media content (<strong>'. $source_icon_title .'</strong>)\', this, event, \'220px\')" />';
			$videos .= ' </a>';
			$videos .= '</td>';
			//	unique id
			$videos .= '<td align="center"><small>'. $r['uniq_id'] .'</small></td>';
			//	video status icon
			$videos .= '<td align="center">';
			$videos .= ' <img src="'. $status_img .'" border="0" align="absmiddle" width="16" height="16" id="status_'. $r['id'] .'"';
			$videos .= '  alt="" title="" class="hintanchor" onMouseover="showhint(\''.$status.' <br> Last checked: '.$last_check.'\', this, event, \'250px\')" />';
			$videos .= '</td>';

			//  Video Restriction icon
			if ($r['restricted'] == '1')
			{
				$videos .= '<td align="center" width="16">';
				$videos .= '<img src="images/locked.png" width="14" height="16" border="0" ';
				$videos .= ' class="hintanchor" alt="Restricted" align="absbottom" '; 
				$videos .= ' onMouseover="showhint(\'Only registered users can watch this video\', this, event, \'250px\')" /> ';
				$videos .= '</td>';
			} else {
				$videos .= '<td align="center" width="16"></td>';
			}
			//	Video title
			$videos .= '<td><span style="visibility:hidden; display:none;">'.stripslashes($r['artist']).' - '.stripslashes($r['video_title']).'</span>';
			if ($r['featured'] == '1')
			{
				$videos .= '<a href="videos.php?filter=featured" title="See all featured videos" /><span class="b_featured">Featured</span></a> ';
			}
			$videos .= ' <a href="'. _URL.'/musicvideo.php?vid='. $r['uniq_id'] .'" target="blank">'; 
			$videos .= stripslashes($r['artist']).' - '.stripslashes($r['video_title']);
			$videos .= ' </a>';
			$videos .= '</td>';
			//	date
			$videos .= '<td align="center">';
			$videos .= ' <span style="font-size:0.1pt; position:absolute; color:#fff; display:none;">'. $r['added'] .'</span>';
			$videos .= '<small>'. date('M d Y', $r['added']) .'</small>';
			$videos .= '</td>';
			//	views
			$videos .= '<td align="center">';
			$videos .= pm_number_format($r['site_views']);
			$videos .= '</td>';
			
			//	category
			$videos .= '<td>';
			//$videos .= make_cats($r['category']);
			$video_cats = explode(',', $r['category']);
			foreach ($video_cats as $k => $cid)
			{
				$cid = (int) $cid;
				$videos .= '<a href="videos.php?page=1&filter=category&fv='. $cid .'" ';
				$videos .= ' title="List videos from '. $categories[ $cid ]['name'] .' only">';
				$videos .= $categories[ $cid ]['name'];
				$videos .= '</a> ';
			}
			$videos .= '</td>';
			//	comments control
			$temp 	 = count_entries('pm_comments', 'uniq_id', $r['uniq_id']);
			$videos .= '<td align="center">';
			$videos .= ' <a href="comments.php?vid='. $r['uniq_id'] .'" class="b_view">';
			$videos .= 'View';
			$videos .= ' </a>';
						
			if (is_admin() || (is_moderator() && mod_can('manage_comments')))
			{
				$videos .= ' | ';
				$videos .= ' <a href="#" ';
				$videos .= ' onClick=\'del_video_comments("'. $r['uniq_id'] .'", "'. $page .'", "'. $filter .'")\'';
				$videos .= ' class="b_delete">';
				$videos .= 'Delete All ('. $temp .')';
			}
			
			$videos .= '</td>';
			//	actions
			$videos .= '<td align="center">';
			$videos .= ' <a href="modify.php?vid='. $r['uniq_id'] .'" class="b_edit">Edit</a>';
			$videos .= ' | ';
			$videos .= ' <a href="#" onClick=\'del_video_id("'. $r['uniq_id'] .'", "'. $page .'", "'. $filter .'")\' class="b_delete">Delete</a>';
			$videos .= '</td>';
			
			$videos .= '</tr>';
		}
	} 
	elseif($count == 0) 
	{
		$videos .= '<tr>';
		$videos .= ' <td colspan="10" align="center">';
		$videos .= 'There aren\'t any videos matching your criteria.';
		$videos .= ' </td>';	
		$videos .= '</tr>';
	}
	return $videos;
}

function a_list_temp($search_term, $search_type = 'video_title', $from = 0, $to = 20, $page = 1) {
	
	$mimetype = array(	'flv' => 'video/x-flv', 
						'mov' => 'video/quicktime', 
						//'avi' => 'video/x-msvideo', 
						'divx' => 'video/x-divx', 
						'mp4' => 'video/mp4', 
						'wmv' => 'video/x-ms-wmv', 
						'bin' => 'application/octet-stream', 
						'avi' => 'video/avi',
						'mkv' => 'video/x-matroska',
						'asf' => 'video/x-ms-asf', 
						'wma' => 'audio/x-ms-wma', 
						'mp3' => 'audio/mpeg', 
						'm4v' => 'video/mp4', 
						'm4a' => 'audio/mp4', 
						'3gp' => 'video/3gpp', 
						'3g2' => 'video/3gpp2' 
						);

	if(!$page)	$page = 1;
	
	$query = mysql_query("SELECT * FROM pm_temp ORDER BY added DESC LIMIT ".$from.", ".$to);
	$count = mysql_num_rows($query);

	// LIST VIDEOS
	if($count >= 1) 
	{
		$videos = '';
		$alt	= 1;
		$sources = a_fetch_video_sources();
		while($r = mysql_fetch_assoc($query)) 
		{
			$col = ($alt % 2) ? 'table_row1' : 'table_row2';
			$alt++;
			
			$status = '';
			$status_img = '';
			switch($r['status'])
			{
				default:
				case VS_UNCHECKED: 	$status = "Video Status: Unchecked";		$status_img = VS_UNCHECKED_IMG;		break;
				case VS_OK: 		$status = "Video Status: OK";				$status_img = VS_OK_IMG; 			break;
				case VS_BROKEN: 	$status = "Video Not Found";				$status_img = VS_BROKEN_IMG; 		break;
				case VS_RESTRICTED:	$status = "Video Status: Geo-restricted";	$status_img = VS_RESTRICTED_IMG;	break;
			}
			//$status_img .= ".png";
			
			//	video row
			$videos .= '<tr class="'. $col .'">';
			//	checkbox
			$videos .= '<td>';
			$videos .= ' <input name="video_ids[]" type="checkbox" value="'. $r['id'] .'" />';
			$videos .= '</td>';
			//	video source icon
			$videos .= '<td>';
			$videos .= ' <img src="src/icons/'. strtolower($sources[$r['source_id']]['source_name']) .'.gif" border="0" ';
			$videos .= 'align="absmiddle" alt="'. ucfirst($sources[$r['source_id']]['source_name']) .'" ';
			$videos .= 'title="'. ucfirst($sources[$r['source_id']]['source_name']).'" />';
			$videos .= '</td>';
			
			//	video title
			$videos .= '<td width="22%">';
			//$videos .= '  <a href="'. $r['url'] .'" title="'. htmlentities($r['video_title']) .'" target="_blank">';
			$videos .= '<b>';
			$videos .= stripslashes($r['artist']).' - '.stripslashes($r['video_title']);
			//$videos .= ' </a>';
			$videos .= ' </b>';
			$videos .= ' <br /><br />';
			
			$videos .= ' <small>';
			if ($r['source_id'] == $sources['localhost']['source_id'])
			{
				$filesize = readable_filesize( @filesize(_VIDEOS_DIR_PATH . $r['url']));
				$buff_ext = strtolower(array_pop(explode('.', $r['url'])));
				$videos .= '  <a href="'. _VIDEOS_DIR . $r['url'] .'" rel="prettyPop[flash]" title="'. htmlentities($r['video_title']) .'" target="_blank">';
				$videos .= '   <img src="images/ico_preview.gif" width="72" height="18" alt="Preview This Video" border="0" align="right" />';
				$videos .= '  </a>';
				$videos .= ' <strong>Size</strong>: '. $filesize .' / ';
				$videos .= ' <strong>Type</strong>: '. $mimetype[$buff_ext];
			}
			else if ($r['source_id'] == $sources['youtube']['source_id'])
			{
				preg_match("/v=([^(\&|$)]*)/", $r['url'], $matches);
				$yt_id = $matches[1];
				
				$videos .= '  <a href="http://www.youtube.com/v/'. $yt_id .'&autoplay=1&v='. $yt_id .'" rel="prettyPop[flash]" title="'. htmlentities($r['video_title']) .'" target="_blank">';
				$videos .= '   <img src="images/ico_preview.gif" width="72" height="18" alt="Preview This Video" border="0" align="right" />';
				$videos .= '  </a>';
				$videos .= $r['url'];
			}
			else
			{
				//$videos .= '  <a href="'. $r['url'] .'" target="_blank">';
				//$videos .= '  <img src="images/ico_preview.gif" width="72" height="18" alt="Preview This Video" border="0" align="right" />';
				//$videos .= '  </a>';
				$videos .= $r['url'];
			}
			
			$videos .= ' </small>';
			$videos .= '</td>';
			//	description
			$videos .= '<td width="20%">';
			if (strlen($r['description']) > 1)
			{
				$videos .= ' <small>';
				$videos .= '<span id="excerpt-'. $r['id'] .'">'. substr($r['description'], 0, 255) .' ... </span>';
				$videos .= '<a href="#" id="show-more-'. $r['id'] .'" title="Show more">show more</a>';
				$videos .= '<span id="full-text-'. $r['id'] .'" style="display:none;">'. $r['description'] .'</span>';
				$videos .= '<br /><a href="#" id="show-less-'. $r['id'] .'" style="display:none;" title="Show less">show less</a>';
				$videos .= '</small>';
			}
			else
			{
				$videos .= ' <small>'. $r['description'] .'</small>';
			}
			$videos .= '</td>';
			//	tags
			$videos .= '<td>';
			$videos .= ' <small>'. $r['tags'] .'</small>';
			$videos .= '</td>';
			//	date
			$videos .= '<td align="center">';
			$videos .= ' <small>'. date('M d Y', $r['added']) .'</small>';
			$videos .= '</td>';
			//	submitted by 
			$videos .= '<td align="center" width="10%">';
			$videos .= ' <a href="'. _URL . '/profile.php?u='. $r['username'] .'">'. $r['username'] .'</a>';
			$videos .= '</td>';
			//	category
			$videos .= '<td>';
			$videos .= make_cats($r['category']); 
			$videos .= '</td>';
			//	actions
			$videos .= '<td align="center" width="10%">';
			$videos .= ' <a href="approve.php?a=approve&vid='. $r['id'] .'&page='. $page .'" class="b_edit">Approve</a>';
			$videos .= ' | ';
			$videos .= ' <a href="#" onClick=\'del_temp_video_id("'. $r['id'] .'", "'. $page .'")\' class="b_delete">Delete</a>';
			$videos .= '</td>';
			
			$videos .= '</tr>';
		}
	} 
	else if($count == 0) 
	{
		$videos .= '<tr>';
		$videos .= ' <td colspan="9" align="center">';
		$videos .= 'No videos pending admin approval.';
		$videos .= ' </td>';	
		$videos .= '</tr>';
	}
	return $videos;
}

function a_list_comments($search_term = '', $search_type = 'comment', $from = 0, $limit = 20, $page = 1, $filter = '') 
{
	if(!$from)	$from = 0;
	if(!$limit)	$limit = 20;
	if(!page)	$page = 1;
	
	if($search_term != '') 
	{
		$sql = 'SELECT * FROM pm_comments WHERE ';
		switch($search_type)
		{
			default:
			case 'comment' : $sql .= 'comment';  break;
			case 'username' : $sql .= 'username'; break;
			case 'ip' : $sql .= 'user_ip'; break;
			case 'uniq_id' : $sql .= 'uniq_id'; break;
		}
			$sql .= " LIKE '%".secure_sql($search_term)."%' ORDER BY added DESC";
		$query = mysql_query($sql);
		$total = mysql_num_rows($query);
	}
	else 
	{
		$sql = '';
		if($filter != '')
		{
			$sql = "SELECT * FROM pm_comments ";
			$sql_count = "SELECT COUNT(*) as total_found FROM pm_comments ";
			
			switch($filter)
			{
				case 'articles':
				
					$sql .= " WHERE uniq_id LIKE 'article-%' ";
					$sql_count .= " WHERE uniq_id LIKE 'article-%' ";
					
				break;
				
				case 'videos':
				
					$sql .= " WHERE uniq_id NOT LIKE 'article-%' ";
					$sql_count .=  " WHERE uniq_id NOT LIKE 'article-%' ";
					
				break;
			}
			$sql .= " ORDER BY added DESC LIMIT ".$from.", ".$limit;
		}
		else
		{
			$sql = "SELECT * FROM pm_comments ORDER BY added DESC LIMIT ".$from.", ".$limit;
			$sql_count = "SELECT COUNT(*) as total_found FROM pm_comments";
		}
		
		//	First, count all entries
		if (strlen($sql_count) > 0)
		{
			$result_count = @mysql_query($sql_count);
			if ( ! $result_count)
			{
				$total = $limit;
			}
			else
			{
				$row_count = mysql_fetch_assoc($result_count);
				mysql_free_result($result_count);
				
				$total = $row_count['total_found'];
				
				unset($sql_count, $result_count, $row_count);
			}
		}
		else
		{
			$total = $limit;
		}

		$query = mysql_query($sql);
	}
	$count = mysql_num_rows($query);

	// LIST COMMENTS
	if($count > 0) 
	{
		$res_arr = array();
		while($r = @mysql_fetch_array($query)) 
		{
			$res_arr[] = $r;
		}
		$res_arr_len = count($res_arr);
		
		if($from == 0)
			$start = 0;
		elseif($from >= $res_arr_len)
			//$start = $from - $limit;
			$start = 0;
		else
			$start = $from;
		
		if( ($start + $limit) >= $res_arr_len)
			$to = $res_arr_len;
		else $to = ($limit + $start);
		
		$comments = '';
		$alt = 1;
		for($i = $start; $i < $to; $i++)
		{
			$col = ($alt % 2) ? 'even' : 'odd';//'odd' : 'even';
			$alt++;

			$comments .= "
			  <tr class=\"".$col."\" id=\"category_update\">
				<td><input name=\"video_ids[]\" type=\"checkbox\" class=\"checkbox\" value=\"".$res_arr[$i]['id']."\" /></td>
				<td align=\"center\" width=\"20\">";
				
			if (strpos($res_arr[$i]['uniq_id'], 'article') !== false)
			{
				$comments .= '<img src="images/ico_filter_articles.png" width="16" height="16" align="absmiddle" /> ';

			} else {

				$comments .= '<img src="images/ico_filter_videos.png" width="16" height="16" align="absmiddle" /> ';

			}
				$comments .= "</td>	
				<td>";
			
			if (strpos($res_arr[$i]['uniq_id'], 'article') !== false)
			{
				$article_id = str_replace('article-', '', $res_arr[$i]['uniq_id']);
				$comments .= '<a href="'. _URL .'/article_read.php?a='.$article_id.'&mode=preview#comments">';
			}
			else
			{
				$comments .= '<a href="'. _URL .'/musicvideo.php?vid='.$res_arr[$i]['uniq_id'].'#comments">';
			}
			
			$comments .= vnamefromvid($res_arr[$i]['uniq_id'])."</a></td>
				<td align=\"center\"><small>".date('M d Y', $res_arr[$i]['added'])."</small></td>";
			
			
			$comments .= '<td class="hintanchor" onMouseover="showhint(\'Click to edit\', this, event, \'100px\')"> ';
			$comments .= '<div id="comment_update_'. $i .'" class="comment_update_hover" name="'. $i .'">';
			$comments .= '<span>'. $res_arr[$i]['comment'] .'</span>'; 
			$comments .= '<div class="comment_update_form">'; 
			$comments .= '<form name="comment_update_'. $i .'" action="comments.php?page='. $page .'&filter='. $filter .'" method="post">';
			$comments .= '<textarea name="comment_txt" rows="3" style="width: 95%;" >'. str_replace('<br />', '', $res_arr[$i]['comment']) .'</textarea>';
			$comments .= '<input type="hidden" name="comment_id" value="'. $res_arr[$i]['id'] .'" />';
			$comments .= '<input name="update" type="submit" value="Update" class="approvebutton" />';
			$comments .= ' | <a href="#" class="comment_update_'. $i .'">Cancel</a>';
			$comments .= '</form>';
			$comments .= '</div>';
			$comments .= '</div>'; // id
			$comments .= "</td>";
			
				
			if($res_arr[$i]['user_id'] == 0 || $res_arr[$i]['user_id'] == 1)
				$comments .= "<td align=\"center\"><strong>".($res_arr[$i]['username'])."</strong></td>";
			else
				$comments .= "<td align=\"center\"><a href=\""._URL."/profile.php?u=".$res_arr[$i]['username']."\">".($res_arr[$i]['username'])."</a></td>";
			$comments .= "
				<td align=\"center\"><small>".$res_arr[$i]['user_ip']."</small></td>";
			$comments .= "<td align=\"center\">";
			if($res_arr[$i]['approved'] == 0)
				$comments .= "<a href=\"comments.php?a=2&cid=".$res_arr[$i]['id']."&page=".$page."&filter=".$filter."\" class=\"b_edit\">Approve</a> | ";
			else
				$comments .= ""; // Approved
			$comments .= "<a href=\"#\" onClick=\"del_comment_id('".$res_arr[$i]['id']."', '".$page."', '". $filter ."')\" class=\"b_delete\">Delete</a></td>";
			
			$comments .= '</tr>';
		}
	} 
	elseif($count == 0) 
	{
		$comments .= "
		  <tr>
			<td colspan=\"8\" align=\"center\">No comments have been posted yet.</td>
		  </tr>";
	}
	return array('comments' => $comments, 'total' => $total);
}

function a_category_table_row($item, &$all_children, $all_categories, $level = 0, $options, &$alternate)
{
	$output = '';	
	
	if ($level > 1)
	{
		$padding = str_repeat($options['spacer'], $level-1);
	}
	
	// build output here.
	$col = ($alternate++ % 2) ? 'table_row1' : 'table_row2'; 
	
	$move_up_href = '<a href="'. $options['page'] .'?move=up&id='. $item['id']. '" title="Move up"><img src="images/ico_v_uploaded.png" width="16" height="16" border="0" /></a>'; 
	$move_down_href = '<a href="'. $options['page'] .'?move=down&id='. $item['id']. '" title="Move down"><img src="images/move_down.png" width="16" height="16" border="0" /></a>';

	$output .= '<form name="'. $item['id'] .'" action="'. $options['form_action'] .'" method="post">';
	$output .= "\n";
	$output .= ' <tr class="'. $col .'" id="category_update" title="category-'. $item['id'] .'">';
	$output .= '  <td align="center">'. $item['id'] .'</td>';$output .= "\n";
	$output .= '  <td>';$output .= "\n";
	$output .= '   <div class="category_update_name">';$output .= "\n";
	$output .= ($level > 0) ? $padding .' <img src="images/ico_subcat.gif" /> ' : '';$output .= "\n";
	$output .= '    <strong>'. htmlentities($item['name'],ENT_COMPAT,'UTF-8') .'</strong>';$output .= "\n";
	$output .= '   </div>';$output .= "\n";
	$output .= '   <div class="category_update_form">';$output .= "\n";
	$output .= '    <input name="name" type="text" size="22" value="'. $item['name'] .'" />';$output .= "\n";
	$output .= '   </div>';$output .= "\n";
	$output .= '   <input name="cid" type="hidden" value="'. $item['id'] .'" />';$output .= "\n";
	$output .= '   <input name="parent_id" type="hidden" value="'. $item['parent_id'] .'" />';
	$output .= '   <input name="old_tag" type="hidden" value="'. $item['tag'] .'" />';
	$output .= '  </td>';$output .= "\n";
	$output .= '  <td>';$output .= "\n";
	$output .= '   <div class="category_update_name">'. $item['tag'] .'</div>';$output .= "\n";
	$output .= '   <div class="category_update_form"><input name="tag" size="15" type="text" value="'. $item['tag'] .'" />';$output .= "\n";
	$output .= '    <input name="update" type="submit" value="Update" class="approvebutton" />';$output .= "\n";
	$output .= '   </div>';$output .= "\n";
	$output .= '  </td>';$output .= "\n";
	$output .= '  <td>'. $all_categories[$item['parent_id']]['name'] .'</td>';
	$output .= "\n";
	$output .= '  <td align="center">';
	$output .= ($options['page'] == 'cat_manager.php') ? $item['total_videos'] : $item['total_articles'];
	$output .= '  </td>';
	$output .= "\n"; 
	$output .= '  <td align="center">'. $move_up_href .' '. $move_down_href .'</td>';$output .= "\n";
	$output .= '  <td align="center">';$output .= "\n";
	if ($options['page'] == 'cat_manager.php')
	{
		$output .= '   <a href="#" onClick="del_cat(\''. $item['id'] .'\')" class="b_delete">Delete</a>'; 
	}
	else
	{
		$output .= '   <a href="#" onClick="onpage_delete_category(\''. $item['id'] .'\', \'#display_result\', \'tr[title=category-'. $item['id'] .']\')" class="b_delete">Delete</a>';
	}
	$output .= "\n"; 
	$output .= '  </td>';
	$output .= "\n";
	$output .= ' </tr>';
	$output .= "\n";
	$output .= '</form>';
	$output .= "\n";$output .= "\n";
	
	
	if (isset($all_children[$item['id']]))
	{
		foreach ($all_children[$item['id']] as $k => $child)
		{
			$output .= a_category_table_row($child, $all_children, $all_categories, $level+1, $options, $alternate);
		}
		unset($all_children[$item['id']]);
	}
	
	return $output;
}

function a_category_table_body($categories = false, $args = array())
{
	$output = '';
	$empty = array();
	$defaults = array(
		'page' => 'cat_manager.php',
		'col_span' => 7,
		'form_action' => 'cat_manager.php',
		'spacer' => '&nbsp;&nbsp;&nbsp;&nbsp;',		
	);
	
	if ( ! is_array($categories))
		return;
	
	$options = array_merge($defaults, $args);
	
	extract($options);
	
	$parents = $parent_ids = $children = array();
	
	if (count($categories) == 0)
	{
		return '<tr><td colspan="'. $col_span .'" align="center">No categories have been defined.</td></tr>';
	}
	
	foreach ($categories as $k => $row)
	{
		if ($row['parent_id'] == 0)
		{
			$parents[] = $row;
			$parent_ids[] = $row['id'];
		}
		else
		{
			$children[$row['parent_id']][] = $row;
		}
	}
	
	$alt = 1;
	
	foreach ($parents as $k => $p)
	{
		$output .= a_category_table_row($p, $children, $categories, 0, $options, $alt);
	}
	
	foreach ($children as $parent_id => $orphans)
	{
		foreach ($orphans as $k => $orphan)
		{
			$orphan['parent_id'] = 0;
			$output .= a_category_table_row($orphan, $empty, $categories, 0, $options, $alt);
		}
	}
	
	
	return $output; 
}

function a_list_cats()
{	
	$categories = load_categories();
	return a_category_table_body($categories, array());
}

// LISTING USERS 
function a_list_users($search_term, $search_type = 'username', $from = 0, $limit = 20, $page = 1, $filter = '', $filter_value = '') 
{

	if(!$page)	$page = 1;
	
	if($search_term != '') 
	{
		$sql = 'SELECT * FROM pm_users WHERE ';
		switch($search_type)
		{
			default:
			case 'username' : $sql .= 'username'; break;
			case 'fullname' : $sql .= 'name';  break;
			case 'email' : $sql .= 'email'; break;
			case 'ip' : $sql .= 'reg_ip'; break;
		}
		$sql .= " LIKE '%".secure_sql($search_term)."%' ORDER BY id DESC";
		$query = mysql_query($sql);
		$total = mysql_num_rows($query);
	}
	else 
	{
		$where = '';
		$orderby = ' ORDER BY id DESC '; // default
		
		if ($filter == 'power')
		{
			$where = " WHERE power = '". $filter_value ."' ";
			//$orderby = '';
		}
		
		$sql = 'SELECT * FROM pm_users ';
		$sql .= $where;
		$sql .= $orderby;
		$sql .= ' LIMIT '. $from .', '. $limit;
		
		$query = mysql_query($sql);
	}

	$count = mysql_num_rows($query);
	// LIST USERS
	if($count > 0) 
	{
		$banlist = get_banlist();
		
		$res_arr = array();
		while($r = @mysql_fetch_array($query)) 
		{
			$res_arr[] = $r;
		}
		$res_arr_len = count($res_arr);
		
		if($from == 0)
			$start = 0;
		elseif($from >= $res_arr_len)
			//$start = $from - $limit;
			$start = 0;
		else
			$start = $from;
		
		if( ($start + $limit) >= $res_arr_len)
			$to = $res_arr_len;
		else $to = ($limit + $start);
		
		$col = '';
		$alt = 1;
		for($i = $start; $i < $to; $i++)
		{
			$username = (array_key_exists($res_arr[$i]['id'], $banlist)) ? '<s>'.$res_arr[$i]['username'].'</s>' : $res_arr[$i]['username'];
			$alt++;
			
			// checkbox
			$checkbox = "<td align=\"center\">";
			if ($res_arr[$i]['power'] != U_ADMIN)
			{
				$checkbox .= "<input name=\"user_ids[]\" type=\"checkbox\" value=\"".$res_arr[$i]['id']."\" />";
			}
			$checkbox .= '</td>';
			
			$users .= "
			  <tr>
			    ". $checkbox ."
				<td align=\"center\">".$res_arr[$i]['id']."</td>
				<td><a href=\""._URL."/profile.php?u=".$res_arr[$i]['username']."\" target=\"_blank\">".stripslashes($username)." <img src=\"images/go_open.gif\" alt=\"New window\" width=\"12\" height=\"12\" border=\"0\" /></a></td>
				<td>".stripslashes($res_arr[$i]['name'])."</td>
				<td align=\"center\"><small>".date('M d Y', $res_arr[$i]['reg_date'])."</small></td>
				<td align=\"center\"><span style='font-size:0.1pt; position:absolute; color:#fff; display:none;'>".$res_arr[$i]['reg_date']."</span> <small>".time_since($res_arr[$i]['last_signin'])." ago</small></td>
				<td><img src=\"images/ico_news.gif\" alt=\"Email this user\" width=\"13\" height=\"11\" border=\"0\" /> <a href=\"mailto:".$res_arr[$i]['email']."\">".$res_arr[$i]['email']."</a></td>
				<td><small>".($res_arr[$i]['reg_ip'])."</small></td>
				<td align=\"center\">";
			if($res_arr[$i]['power'] == U_INACTIVE)
			{
				$users .= "<a href=\"edit_user_profile.php?uid=".$res_arr[$i]['id']."&action=1\" class=\"b_edit\">Activate</a> | ";
			}
			else if ($res_arr[$i]['power'] == U_ADMIN)
			{
				$users .= "<strong>Admin</strong> | ";
			}
			else if ($res_arr[$i]['power'] == U_MODERATOR)
			{
				$users .= "<strong>Moderator</strong> | ";
			}
			else if ($res_arr[$i]['power'] == U_EDITOR)
			{
				$users .= "Editor | ";
			}
			else
			{	
				if (array_key_exists($res_arr[$i]['id'], $banlist))
				{
					$users .= "Banned | ";
				}
				else
				{
					$users .= "Active | ";
				}
			}
			$users .= "<a href=\"edit_user_profile.php?uid=".$res_arr[$i]['id']."\" class=\"b_edit\">Edit</a> | <a href=\"#\" class=\"b_delete\" onClick=\"del_member_id('".$res_arr[$i]['id']."', '".$page."')\">Delete</a></td>
			  </tr>";
			}
	} 
	elseif($count == 0) 
	{
		$users .= "
		  <tr>
			<td colspan=\"9\" align=\"center\">Sorry no users have been found.</td>
		  </tr>";
 	    $total = $count;
	}
	return array('users' => $users, 'total' => $total);
}

function a_list_searches($limit = 50) {

$query = mysql_query("SELECT * FROM pm_searches ORDER BY hits DESC LIMIT ".$limit."");

$count = mysql_num_rows($query);	
	// LIST SEARCHES
	if($count >= 1) {
		$searches = '';
		$i = 1;
		$alt = 1;
		while($r = mysql_fetch_array($query)) {
		$col = ($alt % 2) ? 'table_row1' : 'table_row2';
		$alt++;
	
		$searches .= "
		  <tr class=\"".$col."\">
			<td>".$i."</td>
			<td>".stripslashes($r['string'])."</td>
			<td>".($r['hits']+1)."</td>
		  </tr>";
	
		  $i++;
		}
	} elseif($count == 0) {
		$searches .= "
		  <tr>
			<td colspan=\"5\" align=\"center\">Sorry, no searches available.</td>
		  </tr>";
	}
	return $searches;
}

// LIST LOG ENTRIES

function a_list_log($limit = '50') {

$query = mysql_query("SELECT * FROM pm_log ORDER BY added DESC LIMIT ".$limit."");
$count = mysql_num_rows($query);
	// LIST LOGS
	if($count >= 1) {
		$entries = '';
		$alt = 1;
		while($r = mysql_fetch_array($query)) {
		$col = ($alt % 2) ? 'table_row1' : 'table_row2';
		$alt++;
	
		$entries .= "
		  <tr class=\"".$col."\">
			<td>".date('M d Y h:i:s A', $r['added'])."</td>
			<td>".stripslashes($r['log_msg'])."</td>
			<td>".$r['area']."</td>
		  </tr>";
		}
	} elseif($count == 0) {
		$entries .= "
		  <tr>
			<td colspan=\"5\" align=\"center\">Sorry, no log entries.</td>
		  </tr>";
	}
	return $entries;
}

// LIST VIDEO REPORTS

function a_list_vreports($r_type, $from = 0, $to = 50, $page = 1) {

	if(!$page)	$page = 1;
	$sql = "SELECT pm_reports.*, pm_videos.id as vid, pm_videos.status, pm_videos.last_check, pm_videos.source_id, pm_videos.category  
			FROM pm_reports JOIN pm_videos 
							ON (pm_reports.entry_id = pm_videos.uniq_id) 
			WHERE r_type = '".$r_type."' 
			ORDER BY pm_reports.id DESC 
			LIMIT ".$from.", ".$to;
			
	$query = mysql_query($sql) or die(mysql_error());
	$count = mysql_num_rows($query);	

	// LIST REPORTS
	if($count >= 1) {
		$reports = '';
		$sources = a_fetch_video_sources();
		
		$i = 1;
		$alt = 1;
		while($r = mysql_fetch_assoc($query)) {
		$col = ($alt % 2) ? 'table_row1' : 'table_row2';
		$alt++;
		
		if(!empty($r['last_check']) && $r['last_check'] != '0') {
			$last_check = date("M d, Y h:i a", $r['last_check']);
		} else {
			$last_check = 'n/a';		
		}
		
		$status = '';
		$status_img = '';
		switch($r['status'])
		{
			default:
			case VS_UNCHECKED: 	$status = "Video Status: Unchecked";		$status_img = VS_UNCHECKED_IMG;		break;
			case VS_OK: 		$status = "Video Status: OK";				$status_img = VS_OK_IMG; 			break;
			case VS_BROKEN: 	$status = "Video Missing";					$status_img = VS_BROKEN_IMG; 		break;
			case VS_RESTRICTED:	$status = "Video Status: Geo-restricted";	$status_img = VS_RESTRICTED_IMG;	break;
		}
		//$status_img .= ".png";

		$reports .= "<tr class=\"".$col."\"><td>";
		  
			 if($r['source_id'] == 3 || $r['source_id'] == 22)
			 {
				$reports .= "<input name=\"video_ids[]\" type=\"checkbox\" class=\"checkbox\" value=\"".$r['entry_id']."\" id=\"".$r['vid']."\" />";
			 }
			 else
			 {
				$reports .= "<input name=\"video_ids[]\" type=\"checkbox\" class=\"checkbox\" value=\"".$r['entry_id']."\" />";
				$status_img = VS_NOTAVAILABLE_IMG;
				$status = "Not Available";
			 }
		$reports .= '<input name="video_cat_ids[]" type="hidden" value="'. $r['category'] .'" />';
		$reports .= "
			</td>
			<td><img src=\"src/icons/".strtolower($sources[$r['source_id']]['source_name']).".gif\" border=\"0\" align=\"absmiddle\" alt=\"".ucfirst($sources[$r['source_id']]['source_name'])."\" title=\"".ucfirst($sources[$r['source_id']]['source_name'])."\" /></td>
			<td align=\"center\">".$r['entry_id']."</td>
			<td><img src=\"".$status_img."\" border=\"0\" align=\"absmiddle\" width=\"16\" height=\"16\" id=\"status_".$r['vid']."\" alt=\"\" class=\"hintanchor\" onMouseover=\"showhint('".$status." <br> Last check: ".$last_check."', this, event, '250px')\" /></td>
			<td><a href=\""._URL."/musicvideo.php?vid=".$r['entry_id']."\" target=\"_blank\">".vnamefromvid($r['entry_id'])."</a></td>
			<td>".$r['reason']."</td>
			<td>".$r['submitted']."</td>
			<td align=\"center\"><a href=\"modify.php?vid=".$r['entry_id']."\" class=\"b_edit\">Edit</a> | <a href=\"#\" class=\"b_delete\" onClick=\"del_video_id('".$r['entry_id']."')\">Delete Video</a> | <a href=\"#\" class=\"b_delete\" onClick=\"del_report('".$r['id']."', '".$page."')\">Delete Report</a></td>
		  </tr>";
		  	
		  $i++;
		}
	} elseif($count == 0) {

		$reports .= "
		  <tr>
			<td colspan=\"8\" align=\"center\">No videos have been reported.</td>
		  </tr>";
	}
	return $reports;
}
function unhtmlspecialchars( $string ) {
        $string = str_replace ( '&amp;', '&', $string );
        $string = str_replace ( '&#039;', '\'', $string );
        $string = str_replace ( '&quot;', '\"', $string );
        $string = str_replace ( '&lt;', '<', $string );
        $string = str_replace ( '&gt;', '>', $string );
       
        return $string;
}
function get_rss_news($limit = 5) {
	$rss = new lastRSS; 
	$rssurl = "http://feeds.feedburner.com/pmFeed";
	
	if ($rs = $rss->get($rssurl)) { 
		for( $i = 0; $i < $limit; $i++){
			if($i == 0)
			$ret .= "<p class=\"msg_head msg_head_newest\"> ".$rs['items'][$i]['title']."</p>";
			else
			$ret .= "<p class=\"msg_head\">".$rs['items'][$i]['title']."</p>";
			$ret .= "<div class=\"msg_body\"><p>";
			$ret .= unhtmlspecialchars($rs['items'][$i]['description'])."</p>";
			$ret .= "<a href=\"".$rs['items'][$i]['link']."\" target=\"_blank\" >Read more</a>";
			$ret .= "</div>\n";
		}
	} 
	else { 
		$ret = "Sorry! Couldn't fetch the news. Please join our newsletter at www.phpsugar.com to stay up-to-date."; 
	} 
	return $ret;
}

function download_thumbs($yt_id) {
	$uniq_id = md5($yt_id);
	$uniq_id = substr($uniq_id, 0, 9);
	
	$image_1 = "http://img.youtube.com/vi/".$yt_id."/default.jpg";
	$image_4 = "http://img.youtube.com/vi/".$yt_id."/3.jpg";
	if (is_file( ABSPATH . _UPFOLDER ."/thumbs/".$yt_id."-1.jpg")) {
	}
	else {
	$newimage = $uniq_id."-1.jpg";
	$url4 = ABSPATH . _UPFOLDER ."/thumbs";
  	if ( !copy("$image_1", "$url4/".$uniq_id."-1.jpg") )
		{
	return FALSE;
		}
	$image = imagecreatefromjpeg("$url4/$newimage"); 
	@Imagejpeg($image, 80); 
	//
	$newimage = $uniq_id."-2.jpg";
  	if ( !copy("$image_4", "$url4/".$uniq_id."-2.jpg") )
		{
	return FALSE;
		}
	$image = imagecreatefromjpeg("$url4/$newimage"); 
	@Imagejpeg($image, 80); 
	//
	}
}


function show_pm_notes() {

	global $config, $userdata;
	
	if ($userdata['power'] != U_ADMIN)
		return '';

	$txt_notes  = array();
	$i = 0;
	
	if ( ! is_array($config))
	{
		$config = get_config();
	}
	// check for new versions
	$official_version = cache_this('read_version', 'pm_version'); 

	if (version_compare($official_version, $config['version']) == 1) 
	{
		$txt_notes[$i]['title'] = 'New Release!';
		$txt_notes[$i]['desc']  = '<strong>PHP Melody '.$official_version.'</strong> is available for download.<br /> You are currently running PHP Melody '.$this_version.'.<br />Please visit <a href="http://www.phpsugar.com/" target="_blank">www.phpsugar.com</a> or the <a href="http://www.phpsugar.com/customer/" target="_blank">Customer Area</a> to download PHP Melody '.$official_version;
		$txt_notes[$i]['ico']  = 'gritter-ico_download.png';
		$i++;
	}
	// check for default password and empty Customer ID
	$admin_pass = md5('admin');
	
	if ($userdata['password'] == $admin_pass) 
	{
		$txt_notes[$i]['title'] = 'System Message';
		$txt_notes[$i]['desc']  = 'Please change the default admin password. It\'s not recommended to use <strong>admin</strong> as your password. <br /><a href="password.php">Click here to proceed</a>.';
		$txt_notes[$i]['ico']  = 'gritter-ico_pass.png';
		$i++;
	}
	if (_CUSTOMER_ID == 'YOUR_CUSTOMER_ID') 
	{
		$txt_notes[$i]['title'] = 'System Message';
		$txt_notes[$i]['desc']  = 'Your Customer ID is <strong>invalid</strong>. Please update your <strong>config.php</strong> and add your real Customer ID. In case you don\'t have it anymore please <a href="http://www.phpsugar.com/customer_care.html">contact the support team</a> or log into the <a href="http://www.phpsugar.com/customer/">customer area</a>.';
		$txt_notes[$i]['ico']  = 'gritter-ico_info.png';
		$i++;
	}
	if(file_exists("db_update.php"))
	{
		$txt_notes[$i]['title'] = 'Update Database';
		$txt_notes[$i]['desc']  = 'It appears that you may need to upgrade your database. Please click here to <a href="db_update.php">update the MySQL database</a>.';
		$txt_notes[$i]['ico']  = 'gritter-ico_warn.png';
		$i++;
	}	
	// check for any errors in the log file
	$log_last_48h = $userdata['last_signin'] - (3600 * 48);
	$log_entries_query = mysql_query("SELECT COUNT(*) as total_entries FROM pm_log WHERE added > '".$log_last_48h."'") or die(mysql_error());
	$log_entries_total = mysql_fetch_assoc($log_entries_query);

	if ($log_entries_total['total_entries']  > 0)
	{
		$txt_notes[$i]['title'] = 'System Message';
		$txt_notes[$i]['desc']  = 'The system has logged '.$log_entries_total['total_entries'].' error(s) in the last 48 hours. Please see the <a href="readlog.php"><strong>internal log</strong></a>.';
		$txt_notes[$i]['ico']  = 'gritter-ico_warn.png';
		$i++;
	}
	if ($config['mail_server'] == 'mail.domain.com')
	{
		$txt_notes[$i]['title'] = 'Email Settings';
		$txt_notes[$i]['desc']  = 'PHP Melody couldn\'t find a valid email account. Please setup an email account PHP Melody can use to send notifications and password reset emails. Go to <a href="settings.php"><strong>Settings > E-mail Settings</strong></a> to enter the email details.';
		$txt_notes[$i]['ico']  = 'gritter-ico_warn.png';
		$i++;
	}
	if (count($txt_notes) == 0) 
	{
		$txt_notes = false;
	}
	
	$result = '';
	if (is_array($txt_notes))
	{
		foreach ($txt_notes as $k => $arr) 
		{
			$result .= "show_pm_note('".$arr['title']."', '".secure_sql($arr['desc'])."', 'images/".$arr['ico']."');\n\r"; 
		}
	}
	
	echo $result;
}


function read_version() {

	// You like?
	$ww = 'ht';
	$ww .= 'tp:/';
	$ww .= '/w';
	$ww .= 'ww';	
	$ww .= '.php';
	$ww .= 'sug';
	$ww .= 'ar.co';
	$ww .= 'm/updates/';
	
	$url = $ww.'pm_version.txt';
	if (ini_get('allow_url_fopen') == 1) 
	{
		$content = @file_get_contents($url);
	} else 
	{
	   $ch = @curl_init();
	   @curl_setopt($ch, CURLOPT_URL, $url);
	   @curl_setopt($ch, CURLOPT_HEADER, 0);
	   @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	   @curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
	   $content = @curl_exec($ch);
	   @curl_close($ch);
	}

	return $content;
}

function a_generate_smart_pagination($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "&page=")
{		
	if(!$adjacents) $adjacents = 1;
	if(!$limit) $limit = 15;
	if(!$page) $page = 1;
	if(!$targetpage) $targetpage = "/";
	
		
	$prev = $page - 1;
	$next = $page + 1;
	$lastpage = ceil($totalitems / $limit);
	$lpm1 = $lastpage - 1;
	
	if(strpos($pagestring, 'page=', 0) === FALSE)
		$pagestring .= "&page=";
	
	$pagestring1 = preg_replace('/page=([0-9]*)/', 'page=1', $pagestring);
	$pagestring2 = preg_replace('/page=([0-9]*)/', 'page=2', $pagestring);
	$pagestringlpm1 = preg_replace('/page=([0-9]*)/', 'page='.$lpm1, $pagestring);
	$pagestringlast = preg_replace('/page=([0-9]*)/', 'page='.$lastpage, $pagestring);

	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<ul class=\"pagination\"";
		if($margin || $padding)
		{
			$pagination .= " style=\"";
			if($margin)
				$pagination .= "margin: $margin;";
			if($padding)
				$pagination .= "padding: $padding;";
			$pagination .= "\"";
		}
		$pagination .= ">";

		//previous button
		if ($page > 1)
		{
			$url_query = preg_replace('/page=([0-9]*)/', 'page='.$prev, $pagestring); 
			$pagination .= "<li><a href=\"$targetpage?$url_query\">&laquo; prev</a><li>";
		}
		else
			$pagination .= "<li>&laquo; prev</li>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination .= "<li class=\"current\"><a href=\"#\">$counter</a></li>";
				else
				{
					$url_query = preg_replace('/page=([0-9]*)/', 'page='.$counter, $pagestring);
					$pagination .= "<li><a href=\"$targetpage?$url_query\">$counter</a></li>";
				}					
			}
		}
		elseif($lastpage >= 7 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 2 + ($adjacents * 2))
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{					
					if ($counter == $page)
						$pagination .= "<li class=\"current\"><a href=\"#\">$counter</a></li>";
					else
					{
						$url_query = preg_replace('/page=([0-9]*)/', 'page='.$counter, $pagestring);
						$pagination .= "<li><a href=\"$targetpage?$url_query\">$counter</a></li>";	
					}				
				}
				$pagination .= "...";
				$pagination .= "<li><a href=\"$targetpage?$pagestringlpm1\">$lpm1</a></li>";
				$pagination .= "<li><a href=\"$targetpage?$pagestringlast\">$lastpage</a></li>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{	
				$pagination .= "<li><a href=\"$targetpage?$pagestring1\">1</a></li>";
				$pagination .= "<li><a href=\"$targetpage?$pagestring2\">2</a></li>";
				$pagination .= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination .= "<li class=\"current\"><a href=\"#\">$counter</a></li>";
					else
					{
						$url_query = preg_replace('/page=([0-9]*)/', 'page='.$counter, $pagestring);
						$pagination .= "<li><a href=\"$targetpage?$url_query\">$counter</a></li>";
					}
				}
				$pagination .= "...";
				$pagination .= "<li><a href=\"$targetpage?$pagestringlpm1\">$lpm1</a></li>";
				$pagination .= "<li><a href=\"$targetpage?$pagestringlast\">$lastpage</a></li>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination .= "<li><a href=\"$targetpage?$pagestring1\">1</a></li>";
				$pagination .= "<li><a href=\"$targetpage?$pagestring2\">2</a></li>";
				$pagination .= "...";
				for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination .= "<li class=\"current\"><a href=\"#\">$counter</a></li>";
					else
					{
						$url_query = preg_replace('/page=([0-9]*)/', 'page='.$counter, $pagestring);
						$pagination .= "<li><a href=\"$targetpage?$url_query\">$counter</a></li>";
					}
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
		{
			$url_query = preg_replace('/page=([0-9]*)/', 'page='.$next, $pagestring);
			$pagination .= "<li><a href=\"$targetpage?$url_query\">next &raquo;</a></li>";
		}
		else
			$pagination .= "<li class=\"disabled\">next &raquo;</li>";
		$pagination .= "</ul>\n";
	}
	
	return $pagination;

}

function a_get_video_tags($uniq_id = '')
{
	$sql = "SELECT * FROM pm_tags WHERE uniq_id = '".$uniq_id."' ORDER BY tag_id ASC";
	$result = mysql_query($sql);
	$tags = array();
	while($row = mysql_fetch_assoc($result))
	{
		$tags[] = $row;
	}
	return $tags;
}

function insert_tags($uniq_id = '', $arr_tags = array())
{
	if($uniq_id != '' && count($arr_tags) > 0)
	{
		for($i = 0; $i < count($arr_tags); $i++)
		{
			$safe_tag = safe_tag($arr_tags[$i]);
			$tag = str_replace('"', '&quot;', $arr_tags[$i]);
			
			$sql = "INSERT INTO pm_tags (uniq_id, tag, safe_tag) VALUES('".$uniq_id."','".secure_sql($tag)."', '".$safe_tag."')";
			$result = mysql_query($sql);
			if(!$result)
				return false;
		}
	}
	return true;
}

function a_fetch_video_sources($sort = '')
{
	if ($sort != '')
	{
		$sql = "SELECT * FROM pm_sources ORDER BY ". $sort ." DESC";
	}
	else
	{
		$sql = "SELECT * FROM pm_sources";
	}
	
	$result = mysql_query($sql);
	if(!$result)
		return false;
	$src = array();
	$id = 0;

	while($row = mysql_fetch_assoc($result))
	{
		if ($row['source_name'] == 'mp3')
		{
			$row['source_rule'] = '/(.*?)\.mp3/i';	
		}
		
		$src[ $row['source_id'] ] = $row;
	}
	foreach($src as $id => $source)
	{
		$src[$source['source_name']] = $source;
	}
	
	return array_reverse($src, true);
}


function is_url($url)
{
	$url_regex = "/^(((ht|f)tp(s?))\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6})(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+:&%\$#\=~_\-]+))*$/";
	if(preg_match($url_regex, $url))
		return true;

	return false;
}

function fetch_languages()
{
	$languages = array();
	$sql = "SELECT * FROM pm_langs";
	$result = mysql_query($sql);
	while($row = mysql_fetch_assoc($result))
	{
		$languages[ $row['id'] ] = $row['language'];
	}
	mysql_free_result($result);
	return $languages;
}

function a_list_cats_simple()
{
	$categories = load_categories();
	if (is_array($categories))
		return $categories;
	
	$query = mysql_query("SELECT * FROM pm_categories ORDER BY position ASC");
	$count = mysql_num_rows($query);
	if($count > 0)
	{
		while($r = mysql_fetch_assoc($query))
		{
			$categories[ $r['id'] ] = $r;
		}
	}
	mysql_free_result($query);
	return $categories;
}

function insert_new_video($video_details)
{
	global $config;
	
	if($video_details['description'] != '')
	{
		if((strlen($video_details['description']) == 4) && ($video_details['description'] == "<br>"))
		{
			$video_details['description'] = '';
		}
	}
	
	if($video_details['featured'] == '')
	{
		$video_details['featured'] = 0;
	}
	
	$sql = "INSERT INTO pm_videos (uniq_id, artist, video_title, description, yt_id, yt_length, yt_thumb, category, submitted, added, url_flv, source_id, language, age_verification, yt_views, site_views, featured, restricted)
			VALUES ('".$video_details['uniq_id']."', '". secure_sql($video_details['artist']) ."', '". secure_sql($video_details['video_title']) ."', '". secure_sql($video_details['description']) ."', '".$video_details['yt_id']."', '".$video_details['yt_length']."', '".$video_details['yt_thumb']."', '".$video_details['category']."', '".$video_details['submitted']."', '".time()."', '".$video_details['url_flv']."', '".$video_details['source_id']."', '".$video_details['language']."', '".$video_details['age_verification']."', '0', '0', '".$video_details['featured']."', '".$video_details['restricted']."')";
	
	
	if(is_array($video_details))
	{
		$result = mysql_query($sql);
	}
	if(!$result)
	{
		return array(mysql_error(), mysql_errno());
	}
	
	
	$sql = "UPDATE pm_categories SET total_videos=total_videos+1 WHERE id IN(". $video_details['category'] .")";
	mysql_query($sql);
	
	update_config('total_videos', $config['total_videos'] + 1);
	
	$sql = "INSERT INTO pm_videos_urls (uniq_id, mp4, direct) VALUES 
			('".$video_details['uniq_id']."', '".$video_details['mp4']."', '".$video_details['direct']."')";
	$result = mysql_query($sql);
	
	if(strlen($video_details['embed_code']) > 0)
	{
		$sql = "INSERT INTO pm_embed_code (uniq_id, embed_code) VALUES ('".$video_details['uniq_id']."', '".$video_details['embed_code']."')";
		$result = mysql_query($sql);
	}
	return true; 
}

function a_list_banned($from = 0, $limit = 20)
{
	$sql = "SELECT * FROM pm_banlist ORDER BY id DESC LIMIT ".$from.", ".$limit;
	$result = mysql_query($sql);
	if(!$result)
		return "There was a error while processing data. <br /><strong>MySQL returned:</strong> ".mysql_error();
	$banlist = array();
	while($row = mysql_fetch_assoc($result))
	{
		$banlist[ $row['user_id'] ] = $row;
	}
	mysql_free_result($result);
	
	$total = count($banlist);
	if($total > 0)
	{
		$entries = '';
		$alt = 1;
		foreach($banlist as $user_id => $info)
		{
			$col = ($alt % 2) ? 'table_row1' : 'table_row2';
			$alt++;
			
			$sql = "SELECT username FROM pm_users WHERE id = '".$user_id."'";
			$result = mysql_query($sql);
			$username = mysql_fetch_assoc($result);
			mysql_free_result($result);
			
			if($info['reason'] == '')
				$info['reason'] = "None";
			$entries .= "
			  <tr class=\"".$col."\">
			    <td align=\"center\">".$user_id."</td>
				<td><a href=\""._URL."/admin/edit_user_profile.php?uid=".$user_id."\">".$username['username']." <img src=\"images/go_open.gif\" alt=\"New window\" width=\"12\" height=\"12\" border=\"0\" /></a></td>
				<td>".$info['reason']."</td>
				<td align=\"center\"><a href=\"banlist.php?a=delete&uid=".$user_id."\" class=\"b_delete\">Remove ban</a></td>
			  </tr>";
		}
	} 
	elseif($count == 0) 
	{
		$entries .= "
		  <tr>
			<td colspan=\"4\" align=\"center\">No records found.</td>
		  </tr>";
	}
	return $entries;
}

function is_user_banned($user_id)
{
	$sql = "SELECT COUNT(*) as total_found 
			FROM pm_banlist 
			WHERE user_id = '". $user_id ."'";
	$result = @mysql_query($sql);
	if ( ! $result)
	{
		return false;
	}
	$row = mysql_fetch_assoc($result);
	mysql_free_result($result);
	
	if ($row['total_found'] > 0)
	{
		return true;
	}
	
	return false;
}

function get_banlist()
{
	$banlist = array();
	
	$sql = "SELECT * 
			FROM pm_banlist";
	$result = @mysql_query($sql);
	if ( ! $result)
	{
		return array();
	}
	
	while ($row = mysql_fetch_assoc($result))
	{
		$banlist[ $row['user_id'] ] = $row;
	}
	mysql_free_result($result);
	
	return $banlist;
}

if (!function_exists('json_encode'))
{
  function json_encode($a = false)
  {
    if (is_null($a)) return 'null';
    if ($a === false) return 'false';
    if ($a === true) return 'true';
    if (is_scalar($a))
    {
      if (is_float($a))
      {
        // Always use "." for floats.
        return floatval(str_replace(",", ".", strval($a)));
      }

      if (is_string($a))
      {
        static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
        return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
      }
      else
        return $a;
    }
    $isList = true;
    for ($i = 0, reset($a); $i < count($a); $i++, next($a))
    {
      if (key($a) !== $i)
      {
        $isList = false;
        break;
      }
    }
    $result = array();
    if ($isList)
    {
      foreach ($a as $v) $result[] = json_encode($v);
      return '[' . join(',', $result) . ']';
    }
    else
    {
      foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
      return '{' . join(',', $result) . '}';
    }
  }
}

function cache_this($type, $signature) {

	$cacheFile = './temp/'. md5($signature) .'-'. date('Ym');
	$cacheTime = 48 * 3600;
	$now = time();
	$last_update = 0;
	if ($file_exists = file_exists($cacheFile))
	{
		$last_update = filemtime($cacheFile);
	}
	
	// Serve the cached content if present
	if ($file_exists &&  ($now - $cacheTime) < $last_update) 
	{
		return file_get_contents($cacheFile);
	}

	$date = getdate();
	$last_mo = mktime(0, 0, 0, $date['mon']-1, 1, $date['year']);
	
	$prev_cache = './temp/'. md5($signature) .'-'. date('Ym', $last_mo);
	if (file_exists($prev_cache))
	{
		unlink($prev_cache);
	}

	// Cache the contents to a file
	$cached = @fopen($cacheFile, 'w');
	if ($type == 'read_version')
	{
		$content = read_version();
	}
	else if ($type == 'get_rss_news')
	{
		$content = get_rss_news(6);
	}
	@fwrite($cached, $content, strlen($content));
	@fclose($cached);
	return $content;
}

function get_true_max_filesize()
{
	$upload_max_filesize = return_bytes(ini_get('upload_max_filesize'));
	$post_max_size = return_bytes(ini_get('post_max_size'));
	// uploads shouldn't exceed the size limit of the total post
	$post_max_size = (70 * $post_max_size) / 100;
	
	$max_size = 0;
	$max_size = ($upload_max_filesize < $post_max_size) ? $upload_max_filesize : $post_max_size;
	
	return $max_size;
}

function mass_delete_videos($uniq_ids = array())
{
	$delete_ids_str = '';
	$total_videos = count($uniq_ids);
	
	if ($total_videos > 0)
	{
		if ($total_videos > 20)
		{
			$start  = 0;
			$inc	= 15;

			while ($start <= $total_videos)
			{	
				$delete_ids_str = '';
				$i = 0;
				
				for ($i = $start; $i < $start + $inc; $i++)
				{
					$delete_ids_str .= "'". $uniq_ids[$i] ."', ";
				}

				$delete_ids_str = substr($delete_ids_str, 0, -2);
			
				if (strlen($delete_ids_str) > 2)
				{
					@mysql_query("DELETE FROM pm_videos		 WHERE uniq_id  IN (". $delete_ids_str .")");
					@mysql_query("DELETE FROM pm_comments 	 WHERE uniq_id  IN (". $delete_ids_str .")");
					@mysql_query("DELETE FROM pm_ratings 	 WHERE id 		IN (". $delete_ids_str .")");
					@mysql_query("DELETE FROM pm_reports 	 WHERE entry_id IN (". $delete_ids_str .")");
					@mysql_query("DELETE FROM pm_videos_urls WHERE uniq_id  IN (". $delete_ids_str .")");
					@mysql_query("DELETE FROM pm_favorites 	 WHERE uniq_id  IN (". $delete_ids_str .")");
					@mysql_query("DELETE FROM pm_chart 		 WHERE uniq_id  IN (". $delete_ids_str .")");
					@mysql_query("DELETE FROM pm_tags 		 WHERE uniq_id  IN (". $delete_ids_str .")");
					@mysql_query("DELETE FROM pm_embed_code  WHERE uniq_id  IN (". $delete_ids_str .")");
				}
				$start = $start + $inc;
			}
		}
		else
		{
			$delete_ids_str = '';
			foreach ($uniq_ids as $k => $uniq_id)
			{
				$delete_ids_str .= "'". $uniq_id ."', ";
			}
			$delete_ids_str = substr($delete_ids_str, 0, -2);
			
			@mysql_query("DELETE FROM pm_videos		 WHERE uniq_id  IN (". $delete_ids_str .")");
			@mysql_query("DELETE FROM pm_comments 	 WHERE uniq_id  IN (". $delete_ids_str .")");
			@mysql_query("DELETE FROM pm_ratings 	 WHERE id 		IN (". $delete_ids_str .")");
			@mysql_query("DELETE FROM pm_reports 	 WHERE entry_id IN (". $delete_ids_str .")");
			@mysql_query("DELETE FROM pm_videos_urls WHERE uniq_id  IN (". $delete_ids_str .")");
			@mysql_query("DELETE FROM pm_favorites 	 WHERE uniq_id  IN (". $delete_ids_str .")");
			@mysql_query("DELETE FROM pm_chart 		 WHERE uniq_id  IN (". $delete_ids_str .")");
			@mysql_query("DELETE FROM pm_tags 		 WHERE uniq_id  IN (". $delete_ids_str .")");
			@mysql_query("DELETE FROM pm_embed_code  WHERE uniq_id  IN (". $delete_ids_str .")");
		}
		return true;
	}
	
	return false;
}

function add_config($name, $value)
{
	global $config;
	
	if (array_key_exists($name, $config))
	{
		update_config($name, $value, true);
		return true;
	}
	$value = trim($value);
	$value = secure_sql($value);
	$name = secure_sql($name);
	
	$sql = "INSERT INTO pm_config (name, value) 
			VALUES ('". $name ."', '". $value ."')";
	$result = mysql_query($sql);
	if ( ! $result)
	{
		return array(mysql_error(), mysql_errno());
	}
	
	$config[$name] = $value;
	
	return true;
}

function autosync($force = false)
{
	global $config;
	$now = time();
	
	$config['last_autosync'] = (int) $config['last_autosync'];
	
	if (($config['last_autosync'] < ($now - 2592000)) || $force === true) 
	{
		// Total videos
		$total = 0;
		$query = "SELECT COUNT(*) as total 
				  FROM pm_videos";
		$result =  mysql_query($query);
		$total = mysql_fetch_assoc($result);
		mysql_free_result($result);
		
		$sql[] = "UPDATE pm_config SET value='". $total['total'] ."' WHERE name = 'total_videos'";
		
		$categories = a_list_cats_simple();
		
		//if (($total['total'] < 5000 && count($categories) < 10) || $force)
		if ($total['total'] < 5000 && count($categories) < 10)
		{
			// Count total videos for each category	
			$k = 1;
			foreach ($categories as $cid => $arr)
			{
				$total = 0;
				$query = "SELECT COUNT(*) as total 
							FROM pm_videos 
							WHERE category LIKE '". $cid ."' 
							   OR category LIKE '". $cid .",%' 
							   OR category LIKE '%,". $cid ."' 
							   OR category LIKE '%,". $cid .",%'";
	
				$result =  mysql_query($query);
				$total = mysql_fetch_assoc($result);
				mysql_free_result($result);
	
				$sql[] = "UPDATE pm_categories SET total_videos = '". $total['total'] ."' WHERE id = '". $cid ."'";
				
				if ($k % 3 == 0)
				{
					sleep(1);
				}
				
				$k++;
			}
		}
		
		// Total articles
		$total = 0;
		$query = "SELECT COUNT(*) as total 
				  FROM art_articles";
		$result =  mysql_query($query);
		$total = mysql_fetch_assoc($result);
		mysql_free_result($result);
		
		$sql[] = "UPDATE pm_config SET value='". $total['total'] ."' WHERE name = 'total_articles'";
		
		// Count total articles for each category
		$categories = art_get_categories();
		
		$k = 0;
		foreach ($categories as $cid => $arr)
		{
			$total = 0;
			$query = "SELECT COUNT(*) as total 
						FROM art_articles 
						WHERE category LIKE '". $cid ."' 
						   OR category LIKE '". $cid .",%' 
						   OR category LIKE '%,". $cid ."' 
						   OR category LIKE '%,". $cid .",%'";
	
			$result =  mysql_query($query);
			$total = mysql_fetch_assoc($result);
			mysql_free_result($result);
	
			$sql[] = "UPDATE art_categories SET total_articles = '". $total['total'] ."' WHERE id = '". $cid ."'";
			
			if ($k % 3 == 0)
			{
				sleep(1);
			}
			$k++;
		}
		
		$sql[] = "UPDATE pm_config SET value='". $now ."' WHERE name='last_autosync'";
		
		// Total pages
		$total = 0;
		$query = "SELECT COUNT(*) as total 
				  FROM pm_pages";
		$result =  @mysql_query($query);
		$total = @mysql_fetch_assoc($result);
		@mysql_free_result($result);
		
		$sql[] = "UPDATE pm_config SET value='". $total['total'] ."' WHERE name = 'total_pages'";
		
		$total = count($sql);
		$errors = array();
		
		for($i = 0; $i < $total; $i++)
		{
			$result = @mysql_query($sql[ $i ]);
			if(!$result)
			{
				$errors[] = mysql_error();
			}
		}
		
		if (count($errors) > 0)
		{
			return false;
		}
	}
	
	return true;
}

function restricted_access($exit = true)
{
	echo '
	 <h2>Restricted access</h2>
	 <br />
	<div class="alert-warning">
 	 Sorry, you do not have access to this area.
	</div>
	<input name="continue" type="button" value="&laquo; Home" onClick="location.href=\'index.php\'" class="inputbutton" />';
	
	include('footer.php');
	if ($exit) exit();
	
	return true;
}
function dropdown_jwskins() {

$path = ABSPATH ."/skins";
$dh = opendir($path);
$form_file = '';
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != ".."  && $file != "..") {
		if (strpos(strtolower($file), ".zip"))
			$form_file .= "<option value=\"".$file."\">".ucfirst(trim($file, ".zip"))."</option> \n";
    }
}
closedir($dh);
return $form_file;
}
?>