<%
# $Id: action.php,v 1.21 2005/11/20 15:06:50 hirokai Exp $
include_once( "modules/utils.inc.php" );
$tok = $authenticated = $_COOKIE["c_pass_token"];
$u = $sessionUserId = $_COOKIE["c_uid"];
if( $tok && $tok == md5( getPasswdForUser( $u ))) {
$folder = $folderID ? $folderID : $folder;
$tag = $newFolderID;
if( $debug ) {
e( "DEBUG: operation: $op" );
e( "DEBUG: current folder is: $folder" );
}
if( $op == "delete_unused_tags" ) {
if( $debug )
e( "Deleting unused tags" );
$where_clause = "";
foreach( $_POST as $paramName => $paramValue ) {
if( $paramValue == "1" && substr( $paramName, 0, 2 ) == "ut" )
$where_clause = "$where_clause OR ID = " . substr( $paramName, 2 );
}
if( ! $where_clause ) {
e( "No tags to delete" );
$errors = "No tags to delete";
} else {
$where_clause = preg_replace( "/^ OR /", "", $where_clause);
$qs = "DELETE FROM linksGroups WHERE userid = '$u' AND ($where_clause);";
if( $debug ) echo $qs;
$q = mysql_query( $qs );
if( !$q )
$errors = "Deleting your unused tags failed";
if( !$errors && !$debug ) {
echo bounceWithDelay( "/index", 1 );
include( "$modules/header.inc.php" );
%>
<h1>
Success!
</h1>
<h2>Updating your tags...</h2>
<%
}
}
} else if( $op == "changeCategories" && $id ) {
if( $debug )
e( "DEBUG: recategorizing" );
$errors = 0;
# first, we get the new categories
$catNums = Array();
foreach( $_POST as $catName=>$catValue ) {
if( $catValue == "on" && substr( $catName, 0, 2 ) == "cb" ) {
array_push( $catNums, substr( $catName, 2 ) );
if( $debug )
e( "DEBUG: category " . substr( $catName, 2 ));
}
}
if( $debug )
e( "DEBUG: link ID is $id" );
# then we delete the existing categorization for this link
if( deleteCategorizationForLink( $id ) ) {
$errors = 1;
%>
<div class="attention warning">
de-categorizing link <%= $id %> failed.
</div>
<%
}
# finally, we add the new categorizations
if( count( $catNums )) {
if( $debug )
dumpParams();
$q = add_tags_from_request( $u, $id, Array(), $con_action );
if( !$q )
$errors = "couldn't finish tagging your link.";
if( $debug )
e( "DEBUG: finally, we add the new categorizations" );
$retval = categorizeLink( $id, $catNums );
if( ! $retval )
$errors = 1;
if( $debug )
e( "DEBUG: categorizeLink() retval: $retval" );
} else {
if( $debug )
e( "DEBUG: no categories to add; we're done" );
}
if( ! $errors && !$debug ) {
%>
<script language="javascript"> location.replace( "/<%= $id %>" ); </script>
<%
}
} else {
$nolinks = true;
$linkList = Array();
foreach( $_POST as $name=>$value ) {
if( $value == "on" && $name != "selectAll" ) {
$nolinks = false;
array_push( $linkList, $name );
}
}
if( ! $nolinks ) {
if( $debug )
echo "links: ";
$c = 0;
$whereClause = "";
foreach( $linkList as $link ) {
if( $c != 0 ) {
if( $debug ) echo ", ";
$whereClause = "OR $whereClause";
}
if( $debug ) echo $link;
$whereClause = "ID=$link $whereClause";
$c++;
}
if( $debug )
echo " in " . getCatName() . " " . $folder;
$whereClause = " ($whereClause) AND submitter = '$u'";
if( $tok && $tok == md5( getPasswdForUser( $u ))) {
$con_action = mysql_connect( );
mysql_selectdb( getDBName() );
if( $op == "makePrivate" ) {
$qs = "UPDATE links SET isPrivate = 1 WHERE $whereClause";
%>
being made private.
<%
} else if( $op == "makePublic" ) {
$qs = "UPDATE links SET isPrivate = NULL WHERE $whereClause";
%>
being made public.
<%
} else if( $op == "deleteSelected" ) {
$qs = "DELETE FROM links WHERE $whereClause";
%>
deleted.
<%
} else if( $op == "tag" ) {
if( $tag ) {
if( $debug )
echo "<br>tag (aka Tag ID) is $tag<br><hr>";
$c = 0;
# build and run the queries for EACH link (otherwise, if mysql sees a dupe, it will barf)
foreach( $linkList as $link ) {
if( $c == 0 )
$valuesClause = "VALUES $valuesClause";
else
$valuesClause = "$valuesClause, ";
$valuesClause = "$valuesClause($link, $tag) ";
$c++;
$qs = "INSERT INTO linksCategoriesXRef VALUES($link, $tag);";
$q = mysql_query( $qs );
if( ! $q ) {
$e = mysql_error($con_action);
$tok = get_mysql_dupe_error();
}
if( $q )
echo "tagged link $link with tag $tag";
else if( strstr( $e, $tok)) {
echo "link $link is already tagged w/tag #$tag -- no need to re-tag it.<br>";
} else {
echo "holy shit, unknown error: $e<br>";
$errors = "There were strange, unknown errors while attempting to re-tag $c links with " . getCatName() . " # " . $tag;
}
}
if( !$errors && !$debug ) {
%>
no errors -- sending you home.
<script language="javascript"> location.replace( "/f/<%= $folder %>" ); </script>
<%
}
} else {
$errors = "Select a new tag, first";
}
} else if( $op == "untag" ) {
$debug = 0;
$c = 0;
$whereClause = "";
foreach( $linkList as $link ) {
if( $c == 0 )
$whereClause = "WHERE $whereClause";
else
$whereClause = "$whereClause OR";
$whereClause = "$whereClause (linkID = $link AND catID = $folderID) ";
$c++;
}
$qs = "DELETE FROM linksCategoriesXRef $whereClause;";
} else {
$errors = "Unknown operation parameter: \$op: $op";
}
if( ! $errors ) {
$q = mysql_query( $qs );
if( $q ) {
if( $op == "deleteSelected" ) {
$whereClause = "";
$c = 0;
foreach( $linkList as $link ) {
if( $c == 0 )
$whereClause = "WHERE $whereClause";
else
$whereClause = "$whereClause OR";
$whereClause = "$whereClause (linkID = $link) ";
$c++;
}
$qs = "DELETE FROM linksCategoriesXRef $whereClause;";
$q = mysql_query( $qs );
if( !$q )
$errors = "Delete may not have entirely succeeded...";
}
%>
<div class="attention">
worked!
</div>
<%
if( !$debug ) {
%>
<script language="javascript"> location.replace( "/f/<%= $folder %>" ); </script>
<%
}
}
} else
warn( $errors );
conClose( $con_action );
}
%>
<% } else { %>
<link rel="stylesheet" type="text/css" href="/new-portal.css" title="default stylings">
<div class='welcome'>
no links selected;<br/>
sending you back to the garden.
</div>
<%
if( $folderID || $tag )
echo bounceWithDelay( "/f/$folderID", 1 );
else
echo bounceWithDelay( "/index", 1 );
}
}
}
%>