PHP Code:
function savelisting( $option ) {
global $mt_user_addlisting, $mt_needapproval_addlisting, $mt_needapproval_modifylisting, $database, $_MT_LANG, $Itemid, $my;
global $mt_notifyuser_newlisting, $mt_notifyadmin_newlisting, $mt_notifyuser_modifylisting, $mt_notifyadmin_modifylisting, $mt_allow_html, $mt_user_addlisting;
global $mosConfig_mailfrom, $mosConfig_fromname, $mt_admin_email, $mosConfig_absolute_path, $mosConfig_live_site, $mt_listing_image_dir, $mt_resize_method, $mt_resize_quality, $mt_resize_listing_size;
# Get cat_id / remove_image / link_image
$cat_id = intval( mosGetParam( $_REQUEST, 'cat_id', 0 ) );
$remove_image = mosGetParam( $_REQUEST, 'remove_image', 0 );
$link_image = mosGetParam( $_FILES, 'link_image', null );
$new_cat_id = intval( mosGetParam( $_POST, 'new_cat_id', 0 ) );
# Check if any malicious user is trying to submit link
if ( ($mt_user_addlisting == 1 && $my->id < 1) || $mt_user_addlisting == -1 ) {
echo _NOT_EXIST;
} else {
# Allowed
// Convert all checkbox value to comma delimited
for($i=1; $i<=30; $i++) {
if ( @is_array($_POST["cust_".$i]) ) {
$_POST["cust_".$i] = implode(",",$_POST["cust_".$i]);
}
}
$row = new mtLinks( $database );
if (!@$row->bind( $_POST )) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
exit();
}
if ( $new_cat_id <> $row->cat_id AND $new_cat_id > 0 ) {
$row->cat_id = $new_cat_id;
}
$isNew = ($row->link_id < 1) ? 1 : 0;
# website's validation
if ( $row->website == "http://" ) {
$row->website = '';
} elseif( !empty($row->website) && substr($row->website,0,7) != 'http://' ) {
$row->website = 'http://'.$row->website;
}
# Assignment for new record
if ($isNew) {
$row->link_created = date( "Y-m-d H:i:s" );
$row->ordering = 999;
if ( $my->id > 0) {
$row->user_id = $my->id;
} else {
$database->setQuery( "SELECT id FROM #__users WHERE usertype = 'Super Administrator' LIMIT 1" );
$row->user_id = $database->loadResult();
}
if (!$mt_allow_html) {
$row->link_desc = strip_tags($row->link_desc);
}
// Approval for adding listing
if ( $mt_needapproval_addlisting ) {
$row->link_approved = '0';
} else {
$row->link_approved = 1;
$row->link_published = 1;
mosCache::cleanCache( 'com_mtree' );
}
# Modification to existing record
} else {
//print_r( $_POST );
# Validate that this user is the rightful owner
$database->setQuery( "SELECT user_id FROM #__mt_links WHERE link_id = '".$row->link_id."'" );
$user_id = $database->loadResult();
if ( $user_id <> $my->id ) {
echo _NOT_EXIST;;
} else {
# Get the name of the old photo and last modified date
$sql="SELECT link_id, link_image, link_modified, link_created FROM #__mt_links WHERE link_id='".$row->link_id."'";
$database->setQuery($sql);
$database->loadObject($old);
$old_image = $old->link_image;
# Remove previous old image
/*
if ( $remove_image || ($old_image <> '' && $link_image['tmp_name'] <> '') ) {
$row->link_image = '-1';
if(!unlink($mosConfig_absolute_path.$mt_listing_image_dir.$old_image)) {
echo "<script> alert('".$_MT_LANG->ERROR_DELETING_OLD_IMAGE."'); </script>\n";
}
}
*/
# Retrive last modified date
$old_modified = $old->link_modified;
$link_created = $old->link_created;
$row->link_modified = date( "Y-m-d H:i:s" );
$row->link_published = 1;
$row->user_id = $my->id;
// Get other info from original listing
$database->setQuery( "SELECT link_hits, link_votes, link_rating, link_featured, link_created, link_visited, link_image, ordering, publish_down, publish_up FROM #__mt_links WHERE link_id = '$row->link_id'" );
$database->loadObject( $original );
foreach( $original AS $k => $v ) {
// Set link_image to "-1" to indicate image removal
if ( $k == "link_image" && ( $remove_image || ($old_image <> '' && $link_image['tmp_name'] <> '') ) ) {
if ( $mt_needapproval_modifylisting ) {
$row->link_image = '-1';
} else {
if(!unlink($mosConfig_absolute_path.$mt_listing_image_dir.$old_image)) {
echo "<script> alert('".$_MT_LANG->ERROR_DELETING_OLD_IMAGE."'); </script>\n";
} else {
$row->link_image = '';
}
}
} else {
$row->$k = $v;
}
}
// Remove any listing that is waiting for approval for this listing
$database->setQuery( "DELETE FROM #__mt_links WHERE link_approved = '".(-1*$row->link_id)."'" );
$database->query();
// Approval for modify listing
if ( $mt_needapproval_modifylisting ) {
$row->link_approved = (-1 * $row->link_id);
$row->link_id = null;
} else {
$row->link_approved = 1;
mosCache::cleanCache( 'com_mtree' );
}
}
} // End of $isNew
# Create Thumbnail
if ( $link_image['name'] <> '' ) {
$mtImage = new mtImage( $link_image, $mosConfig_absolute_path.$mt_listing_image_dir );
$mtImage->setMethod( $mt_resize_method );
$mtImage->setQuality( $mt_resize_quality );
$mtImage->setSize( $mt_resize_listing_size );
if ( $row->link_id > 0 ) {
$mtImage->setName( $row->link_id."_".$link_image['name'] );
}
if ( $mtImage->check() ) {
if ( $mtImage->resize() ) {
if ( $row->link_id > 0 ) {
$row->link_image = $row->link_id."_".$link_image['name'];
} elseif( $isNew ) {
// Do nothing yet. The record is not saved, therefore no link_id available.
} else {
$row->link_image = $old->link_id."_".$link_image['name'];
}
}
} else {
echo "<script> alert('".$mtImage->getErrorMsg()."'); window.history.go(-1); </script>\n";
exit();
}
}
# Strip HTML tags if admin does not allow it
if (!$mt_allow_html) {
$row->link_desc = strip_tags($row->link_desc);
}
# OK. Store new listing into database
if (!$row->store()) {
echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
exit();
} else {
# If this is a newly submitted link and has image, rename the photo to listingID_photoName.jpg
if ( $isNew && $link_image['error'] == 0 ) {
// Get last inserted listing ID
$mysql_last_insert_cl_id = $database->insertid();
$database->setQuery( "SELECT link_id FROM #__mt_cl WHERE cl_id = ".$mysql_last_insert_cl_id );
$mysql_last_insert_id = $database->loadResult();
if ( $mysql_last_insert_id > 0 ) {
if ( rename( $mosConfig_absolute_path.$mt_listing_image_dir.$link_image['name'], $mosConfig_absolute_path.$mt_listing_image_dir.$mysql_last_insert_id."_".$link_image['name'] ) ) {
$database->setQuery( "UPDATE #__mt_links SET link_image = '".$mysql_last_insert_id."_".$link_image['name']."' WHERE link_id = '".$mysql_last_insert_id."' LIMIT 1" );
$database->query();
}
}
# This is modification to an existing listing
} elseif( !$isNew && $link_image['error'] == 0 && $row->link_id > 0 ) {
$database->setQuery( "SELECT LAST_INSERT_ID()" );
$mysql_last_insert_id = $database->loadResult();
if ( $mysql_last_insert_id > 0 ) {
if ( rename( $mosConfig_absolute_path.$mt_listing_image_dir.$link_image['name'], $mosConfig_absolute_path.$mt_listing_image_dir.($row->link_approved * -1)."_".$link_image['name'] ) ) {
$database->setQuery( "UPDATE #__mt_links SET link_image = '".($row->link_approved * -1)."_".$link_image['name']."' WHERE link_id = '".$mysql_last_insert_id."' LIMIT 1" );
$database->query();
}
}
}
}
# Send e-mail notification to user/admin upon adding a new listing
// Get owner's email
$database->setQuery( "SELECT email, name FROM #__users WHERE id = '".$my->id."' LIMIT 1" );
$database->loadObject( $author );
if ( $isNew ) {
# To User
if ( $mt_notifyuser_newlisting == 1 && $my->id > 0 ) {
if ( $row->link_approved == 0 ) {
$subject = sprintf($_MT_LANG->NEW_LISTING_EMAIL_SUBJECT_WAITING_APPROVAL, $row->link_name);
$msg = $_MT_LANG->NEW_LISTING_EMAIL_MSG_WAITING_APPROVAL;
} else {
$subject = sprintf($_MT_LANG->NEW_LISTING_EMAIL_SUBJECT_APPROVED, $row->link_name);
$msg = sprintf($_MT_LANG->NEW_LISTING_EMAIL_MSG_APPROVED, $row->link_name, sefRelToAbs($mosConfig_live_site."/index.php?option=com_mtree&task=viewlink&link_id=$row->link_id&Itemid=$Itemid"),$mosConfig_fromname);
}
mosMail( $mosConfig_mailfrom, $mosConfig_fromname, $author->email, $subject, wordwrap($msg) );
}
# To Admin
if ( $mt_notifyadmin_newlisting == 1 ) {
if ( $row->link_approved == 0 ) {
$subject = sprintf($_MT_LANG->NEW_LISTING_EMAIL_SUBJECT_WAITING_APPROVAL, $row->link_name);
$msg = sprintf($_MT_LANG->ADMIN_NEW_LISTING_MSG_WAITING_APPROVAL, $row->link_name, $row->link_name, $row->link_id, $author->name, $my->username, $author->email);
} else {
$subject = sprintf($_MT_LANG->NEW_LISTING_EMAIL_SUBJECT_APPROVED, $row->link_name);
$msg = sprintf($_MT_LANG->ADMIN_NEW_LISTING_MSG_APPROVED, $row->link_name, sefRelToAbs($mosConfig_live_site."/index.php?option=com_mtree&task=viewlink&link_id=$row->link_id&Itemid=$Itemid"), $row->link_name, $row->link_id, $author->name, $my->username, $author->email);
}
//mosMail( $mosConfig_mailfrom, $mosConfig_fromname, $mt_admin_email, $subject, wordwrap($msg) );
mosMailToAdmin( $subject, $msg );
}
}
# Send e-mail notification to user/admin upon modifying an existing listing
else {
# To User
if ( $mt_notifyuser_modifylisting == 1 && $my->id > 0 ) {
if ( $row->link_approved < 0 ) {
$subject = sprintf($_MT_LANG->MODIFY_LISTING_EMAIL_SUBJECT_WAITING_APPROVAL, $row->link_name);
$msg = sprintf($_MT_LANG->MODIFY_LISTING_EMAIL_MSG_WAITING_APPROVAL, $row->link_name, sefRelToAbs($mosConfig_live_site."/index.php?option=com_mtree&task=viewlink&link_id=$old->link_id&Itemid=$Itemid") );
} else {
$subject = sprintf($_MT_LANG->MODIFY_LISTING_EMAIL_SUBJECT_APPROVED, $row->link_name);
$msg = sprintf($_MT_LANG->MODIFY_LISTING_EMAIL_MSG_APPROVED, $row->link_name, sefRelToAbs($mosConfig_live_site."/index.php?option=com_mtree&task=viewlink&link_id=$old->link_id&Itemid=$Itemid"),$mosConfig_fromname);
}
mosMail( $mosConfig_mailfrom, $mosConfig_fromname, $author->email, $subject, wordwrap($msg) );
}
# To Admin
if ( $mt_notifyadmin_modifylisting == 1 ) {
if ( $row->link_approved < 0 ) {
$subject = sprintf($_MT_LANG->MODIFY_LISTING_EMAIL_SUBJECT_WAITING_APPROVAL, $row->link_name);
$msg = sprintf($_MT_LANG->ADMIN_MODIFY_LISTING_MSG_WAITING_APPROVAL, $row->link_name, sefRelToAbs($mosConfig_live_site."/index.php?option=com_mtree&task=viewlink&link_id=$old->link_id&Itemid=$Itemid"), $row->link_name, $row->link_id, $author->name, $my->username, $author->email);
} else {
$subject = sprintf($_MT_LANG->MODIFY_LISTING_EMAIL_SUBJECT_APPROVED, $row->link_name);
$msg = sprintf($_MT_LANG->ADMIN_MODIFY_LISTING_MSG_APPROVED, $row->link_name, sefRelToAbs($mosConfig_live_site."/index.php?option=com_mtree&task=viewlink&link_id=$old->link_id&Itemid=$Itemid"), $row->link_name, $row->link_id, $author->name, $my->username, $author->email);
}
mosMailToAdmin( $subject, $msg );
}
}
mosRedirect( "index.php?option=$option&task=listcats&cat_id=$cat_id&Itemid=$Itemid", ( ($isNew) ? ( ($mt_needapproval_addlisting) ? $_MT_LANG->LISTING_WILL_BE_REVIEWED : $_MT_LANG->LISTING_HAVE_BEEN_ADDED) : ( ($mt_needapproval_modifylisting) ? $_MT_LANG->LISTING_MODIFICATION_WILL_BE_REVIEWED : $_MT_LANG->LISTING_HAVE_BEEN_UPDATED ) ) );
}
}
Bookmarks