include('includes/config.php');
include('includes/auth.php');
include('includes/header.php');
$errors=null;
$post=new post();
$artist=new artist();
$track=new track();
$saved=0;
$postlimit=0;
$initialUsed=0;
if(isset($_POST['postid']) && $_POST['postid']>0) $post->fetch($_POST['postid']);
elseif(isset($_GET['postid']) && $_GET['postid']>0) $post->fetch($_GET['postid']);
if(isset($_POST['savePost']) && $_POST['savePost']==1){
//check for required fields
if(!isset($_POST['artist']) || $_POST['artist']=="") $errors['artist']="Who performs this song?";
if(!isset($_POST['songtitle']) || $_POST['songtitle']=="") $errors['songtitle']="What's it called?";
if(!isset($_POST['posttype']) || $_POST['posttype']=="") $errors['posttype']="Song or Album?";
if(!isset($_POST['postdetail']) || $_POST['postdetail']=="" || strlen(trim($_POST['postdetail']))==0) $errors['postdetail']="We need to know what makes this song special - that's kind of the point!";
$post->properties['heading']=$_POST['posttitle'];
$post->properties['post']=$_POST['postdetail'];
$post->properties['posttype']=$_POST['posttype'];
$post->properties['user']=$user->properties['id'];
$post->properties['link']=$_POST['link'];
$post->properties['linktitle']=$_POST['linktitle'];
$post->properties['tags']=$_POST['tags'];
$post->properties['tracktitle']=$_POST['songtitle'];
$post->properties['artistname']=$_POST['artist'];
$post->properties['imagecredit']=$_POST['imagecredit'];
//upload image
$image="";
if(isset($_FILES['postimage']) && $_FILES['postimage']['name']!=""){
$uploadsDirectory=$_SERVER['DOCUMENT_ROOT']."/img/postimages/";
if($_FILES['postimage']['error']==1) $errors['postimage']="There was a problem uploading your image, please try again.";
else{
//check it's an image
@getimagesize($_FILES['postimage']['tmp_name']) or $errors['postimage']="File is not an image.";
//double check extension
$extension = pathinfo($_FILES['postimage']['name'],PATHINFO_EXTENSION);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) $errors['postimage']="The file is not a suitable image format.";
$size=getimagesize($_FILES['postimage']['tmp_name']);
//check it's not too big (4Mb)
if ($size[0] > 4194304) $errors['postimage']="The image is a bit big, try and upload something smaller (the limit is 4Mb)";
}
if($errors['postimage']==""){
$now = 0;
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES['postimage']['name']))
{
$now++;
}
//resize image to max width 600px
if($extension=="jpg" || $extension=="jpeg" ) $src = imagecreatefromjpeg($_FILES['postimage']['tmp_name']);
elseif($extension=="png") $src = imagecreatefrompng($_FILES['postimage']['tmp_name']);
else $src = imagecreatefromgif($_FILES['postimage']['tmp_name']);
list($width,$height)=getimagesize($_FILES['postimage']['tmp_name']);
if($width>600){
$newwidth=600;
$newheight=($height/$width)*$newwidth;
}else{
$newwidth=$width;
$newheight=$height;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
@imagejpeg($tmp,$uploadFilename,100) or $errors['postimage']="There was a problem uploading the file";
imagedestroy($src);
imagedestroy($tmp);
if($errors==null) $post->properties['image']=str_replace($uploadsDirectory,"", $uploadFilename);
}
}//end if(isset($_FILES['postimage']))
if($errors==null){
//lookup artist and if needed save
$artist->findName($_POST['artist']);
if($artist->properties['id']==0){
$artist->properties['name']=$_POST['artist'];
$artist->save();
}
//lookup track and if needed save
$track->findTitleArtist($_POST['songtitle'],$artist->properties['id']);
if($track->properties['id']==0){
$track->properties['title']=$_POST['songtitle'];
$track->properties['artist']=$artist->properties['id'];
$track->save();
}
$post->properties['track']=$track->properties['id'];
$post->properties['artist']=$artist->properties['id'];
//if this post is already live and has a date, don't update the postdate
if($post->properties['live']==1 && $post->properties['postdate']=="") $post->properties['postdate']=time();
elseif($post->properties['live']==0) $post->properties['postdate']=time();
if(isset($_POST['saveDraft']) && $_POST['saveDraft']==1){
$post->properties['live']=0;
$post->properties['postdate']=time();
}elseif($post->properties['live']==0){
$post->properties['live']=1;
//check for post already today
$startofday= mktime(0, 0, 0, date('n'), date('j'));
$endofday= mktime(24, 0, 0, date('n'), date('j'));
$postedToday=mysqli_query($dbh,"SELECT id FROM posts WHERE user='".$user->properties['id']."' AND live=1 AND postdate>'".$startofday."' AND postdate<'".$endofday."'");
if(mysqli_num_rows($postedToday)>0){
//check if there are initial tracks still available
if($user->properties['initialposts']==0){
$post->properties['live']=0;
$post->properties['postdate']=time();
$postlimit=1;
}else{
$user->properties['initialposts']=$user->properties['initialposts']-1;
$user->save();
$initialUsed=1;
}
}
}//end else if(isset($_POST['saveDraft']) && $_POST['saveDraft']==1)
//check for previous posting
$checkPrevQ=mysqli_query($dbh,"SELECT id FROM posts WHERE track='".$post->properties['track']."' AND artist='".$post->properties['artist']."' AND posttype = '".$post->properties['posttype']."' AND user='".$user->properties['id']."' AND id!='".$post->properties['id']."'");
if(mysqli_num_rows($checkPrevQ)>0 && (!isset($_POST['ignore_duplicate']) || $_POST['ignore_duplicate']==0)){
$post->properties['live']=0;
$errors['duplicate']="You've already posted this '".$post->properties['posttype']."' , are you sure you want to post it again?";
}//if(mysqli_num_rows($checkPrevQ)>0 && (!isset($_POST['ignore_duplicate']) || $_POST['ignore_duplicate']==0))
//check for spam
$akismet = new Akismet('http://mumubl.com/', '3a5f1c0c68b4');
$akismet->setCommentAuthor($user->properties['name']);
$akismet->setCommentAuthorEmail($user->properties['email']);
$akismet->setCommentAuthorURL($user->properties['url']);
$akismet->setCommentContent($_POST['postdetail']);
if($akismet->isCommentSpam()){
$post->properties['live']=0;
$errors['general']="This post looks a little bit like spam, we've saved it as a draft just to be safe. You can contact spampostings@mumubl.com with your username and the details of the post if you think there's a problem.";
}
$post->save();
$saved=1;
if($post->properties['live']==1){
//post to social networks
if(isset($_POST['postToTwitter']) && $_POST['postToTwitter']=='1' && $post->properties['tweeted']==0){
$access_token = $user->properties['twittercred'];
$twitterConnection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$tweet="";
if($post->properties['heading']!="") $tweet=stripslashes($post->properties['heading'])." : ";
$tweet.=" ".$_POST['songtitle']." - ".$_POST['artist'];
if($post->properties['bitly']!=null) $tweet.=" ".$post->properties['bitly'];
else{
//get bitly link
$bitly=new Bitly;
$short=$bitly->shorten("http://mumubl.com/posts/".$post->properties['id']);
$post->properties['bitly']=$short['url'];
$post->save();
$tweet.=" ".$post->properties['bitly'];
}
if(strlen($tweet)<140){
if($_POST['tags']!=""){
$tagsArr=split(',',$_POST['tags']);
foreach($tagsArr as $tweetTag){
$tweetTag=trim($tweetTag);
if($tweetTag!="" && strlen($tweet." #".$tweetTag)<140) $tweet.=" #".$tweetTag;
}
}
}
if(strlen($tweet)<132) $tweet.=" /@mumubl";
$parameters = array('status' => $tweet);
$response = $twitterConnection->post('statuses/update', $parameters);
//echo($twitterConnection->http_code);
//print_r($response);
if($twitterConnection->http_code==200) $post->properties['tweeted']=1;
$post->save();
}//end if(isset($_POST['postToTwitter']) && $_POST['postToTwitter']=='1')
if(isset($_POST['postToFacebook']) && $_POST['postToFacebook']=='1' && $post->properties['facebooked']==0){
$access_token = $user->properties['fbcred'];
$facebook->setAccessToken($access_token);
$user_id = $facebook->getUser();
if(!$user_id) $errors['general']='Could not post to facebook. Please reconnect via your profile page.';
else{
$permissions = $facebook->api("/me/permissions");
$exists = 0;
foreach($permissions['data'] as $permissions_arr){
if(!array_key_exists('publish_actions', $permissions_arr)) $exists=1;
}
if($exists==0) $errors['general']='Could not post to facebook. Please reconnect via your profile page.';
else{
$fbpost="I shared a track on Mumubl ";
$fbpost.="http://mumubl.com/posts/".$post->properties['id'];
if($post->properties['heading']!="") $fbpost.=" ".stripslashes($post->properties['heading'])." : ";
$fbpost.=" ".$_POST['songtitle']." - ".$_POST['artist'];
$facebook->api("/me/feed", "post", array(
message => $fbpost
));
$post->properties['facebooked']=1;
$post->save();
}
}
}//end if(isset($_POST['postToFacebook']) && $_POST['postToFacebook']=='1')
//reupdate tags
$tags=explode(",",$_POST['tags']);
foreach($tags as $tag){
$tag=strtolower(trim($tag));
//check for current entry
$tagExists=mysqli_query($dbh,"SELECT id FROM tags WHERE tag='".$tag."'");
//add or update
if(mysqli_num_rows($tagExists)>0){
$tagE=mysqli_fetch_array($tagExists);
$count=mysqli_query($dbh,"SELECT count(*) as count FROM posts WHERE tags LIKE '%".$tag."%'");
$countT=mysqli_fetch_array($count);
mysqli_query($dbh,"UPDATE tags SET countup='".$countT['count']."' WHERE id='".$tagE['id']."'");
}else{
mysqli_query($dbh,"INSERT INTO tags SET countup='1', tag='".$tag."'");
}
}
}//end if post live==1
}//end if errors==null
//refresh post - slashes etc
$post->fetch($post->properties['id']);
}//end if(isset($_POST['savePost']) && $_POST['savePost']==1){
?>
if($saved==1){?>
if($postlimit==1) echo("You can't save anymore tracks today, this has been saved as a draft.
");
if($initialUsed==1) echo("You have used one of your extra tracks you have ".$user->properties['initialposts']." remaining.
");
?>
Saved :
if($post->properties['live']==1) echo("View this post");
else echo("Preview this post");
?>
}elseif($post->properties['live']==0){
$startofday= mktime(0, 0, 0, date('n'), date('j'));
$endofday= mktime(24, 0, 0, date('n'), date('j'));
$postedToday=mysqli_query($dbh,"SELECT id FROM posts WHERE user='".$user->properties['id']."' AND live AND postdate>'".$startofday."' AND postdate<'".$endofday."'");
if(mysqli_num_rows($postedToday)>0 && $user->properties['initialposts']==0){
$nextpost=$endofday-time();
$hours = floor($nextpost / (60 * 60));
// extract minutes
$divisor_for_minutes = $nextpost % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
?>
You have hours and minutes until you can post a new track
This will be saved as a draft.
}
}//if($post->properties['live']==0)
?>
if($user->properties['authenticated']==0){
?>
Unverified account
Your account is currently marked as unverified, you will not be able to post until you have completed verification.
You should have an email from when you signed up with the verification code to enter onto the profile page, otherwise you can authenticate your account by linking to a scoial network.
}elseif($post->properties['id']>0 && $user->properties['id']!=$post->properties['user']){
?>
Wrong user
Oops this isn't your post so you can't edit it.
}elseif($post->properties['inspirationliverpool']==1){
?>
Inspiration Liverpool
Sorry you can't edit an Inspiration Liverpool project link.
Drop an email to dave@mumubl.com if you want to change this post.
}else{
?>
} //end if else authenticated ?>
include('includes/footer.php');
?>