Changeset 1251

Show
Ignore:
Timestamp:
11/13/06 23:04:38 (2 years ago)
Author:
jm3
Message:

finally fixed tabs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • feedmelinks/modules/utils.inc.php

    r1226 r1251  
    22 
    33if( ! $cached_page_include ) 
    4        session_register("cache"); 
     4  session_register("cache"); 
    55 
    66include_once( "env.inc.php" ); 
     
    1111%> 
    1212<div class="progress"> 
    13        <h3><%= $mesg %></h3> 
    14        <img src="/img/aqua-progressbar.gif" /> 
     13  <h3><%= $mesg %></h3> 
     14  <img src="/img/aqua-progressbar.gif" /> 
    1515</div> 
    1616<% 
     
    2323 
    2424function get_max_ttl_ratio() { 
    25        return 0.2; 
     25  return 0.2; 
    2626} 
    2727 
    2828function get_tag_to_link_ratio( $user ) { 
    29        if( ! $user )  
    30                return 0; 
    31        return( get_simple_rs( getQuery( "tags_per_user", $user )) /  get_simple_rs( getQuery( "links_per_user", $user ))); 
     29  if( ! $user )  
     30    return 0; 
     31  return( get_simple_rs( getQuery( "tags_per_user", $user )) /  get_simple_rs( getQuery( "links_per_user", $user ))); 
    3232} 
    3333 
     
    4949  $success = mail( get_maintainer_email(), "Potential spammer: $alleged_spammer reported by $reporter", $body, $headers); 
    5050 
    51        return "Filed Successfully!"; 
     51  return "Filed Successfully!"; 
    5252  if( $success ) 
    53                log_mesg_to( "user $alleged_spammer reported as a spammer by $reporter", "global" ); 
     53    log_mesg_to( "user $alleged_spammer reported as a spammer by $reporter", "global" ); 
    5454} 
    5555 
    5656function user_enabled( $user ) { 
    57        $disabled = getFieldForUser( $user, "disabled" ); 
    58        return (!$disabled || $disabled == "NULL") ? true : false; 
     57  $disabled = getFieldForUser( $user, "disabled" ); 
     58  return (!$disabled || $disabled == "NULL") ? true : false; 
    5959} 
    6060 
    6161function enable_user( $user ) { 
    62        $q = run_query( getQuery( "enable_user", $user )); 
    63        return "success"; # FIXME: replace with a real status 
     62  $q = run_query( getQuery( "enable_user", $user )); 
     63  return "success"; # FIXME: replace with a real status 
    6464} 
    6565 
    6666function disable_user( $user ) { 
    67        $q = run_query( getQuery( "disable_user", $user )); 
    68        log_mesg_to( "disabled user $user", "global" ); 
    69        return "disabled user"; # FIXME: replace with a real status 
     67  $q = run_query( getQuery( "disable_user", $user )); 
     68  log_mesg_to( "disabled user $user", "global" ); 
     69  return "disabled user"; # FIXME: replace with a real status 
    7070} 
    7171 
    7272function snuffed( $u ) { 
    73        return getFieldForUser( $u, "snuffed" ); 
     73  return getFieldForUser( $u, "snuffed" ); 
    7474} 
    7575 
    7676function snuff_user( $user ) { 
    77        if( is_privileged_importer( $user ) || isSuperUser( $user )) 
    78                return "you cannot snuff a superuser."; 
    79  
    80        # disable their login  
    81        disable_user( $user ); 
    82  
    83        # reset their password to garbage 
    84        $snuff_pass = get_validation_code( $user . "-SNUFFED" ); 
    85        $q = run_query( getQuery( "snuff_pass", $user, $snuff_pass )); 
    86  
    87        # mark all their tags private 
    88        $q = run_query( getQuery( "privatize_tags", $user )); 
    89  
    90        # mark all their links private 
    91        $q = run_query( getQuery( "privatize_links", $user )); 
    92  
    93        # make their user directory un-readable 
    94        $f = get_user_folder( $user ); 
    95        if( is_dir( $f )) { 
    96                shell_exec( "cp " . get_webserver_root() . "/admin/.htaccess $f/" ); 
    97        
    98  
    99        # mark them as snuffed 
    100        $q = run_query( getQuery( "snuff_user", $user )); 
    101  
    102        log_mesg_to( "snuffed out user $user", "global" ); 
    103        echo get_snuffed_mesg(); 
    104 %> 
    105  
    106        <a href="/u/<%= $user %>">Done. Check your work</a>. 
    107  
    108 <% 
    109        return "success."; 
     77  if( is_privileged_importer( $user ) || isSuperUser( $user )) 
     78    return "you cannot snuff a superuser."; 
     79 
     80  # disable their login  
     81  disable_user( $user ); 
     82 
     83  # reset their password to garbage 
     84  $snuff_pass = get_validation_code( $user . "-SNUFFED" ); 
     85  $q = run_query( getQuery( "snuff_pass", $user, $snuff_pass )); 
     86 
     87  # mark all their tags private 
     88  $q = run_query( getQuery( "privatize_tags", $user )); 
     89 
     90  # mark all their links private 
     91  $q = run_query( getQuery( "privatize_links", $user )); 
     92 
     93  # make their user directory un-readable 
     94  $f = get_user_folder( $user ); 
     95  if( is_dir( $f )) { 
     96    shell_exec( "cp " . get_webserver_root() . "/admin/.htaccess $f/" ); 
     97 
     98 
     99  # mark them as snuffed 
     100  $q = run_query( getQuery( "snuff_user", $user )); 
     101 
     102  log_mesg_to( "snuffed out user $user", "global" ); 
     103  echo get_snuffed_mesg(); 
     104%> 
     105 
     106  <a href="/u/<%= $user %>">Done. Check your work</a>. 
     107 
     108<% 
     109  return "success."; 
    110110} 
    111111 
    112112function get_snuffed_mesg() { 
    113        ob_start(); 
    114 %> 
    115        <ol> 
    116                <li>we changed their password, </li> 
    117                <li>disabled their ability to log in,</li> 
    118                <li>hid their tags,</li> 
    119                <li>hid their links,</li> 
    120                <li>censored their profile page,</li> 
    121                <li>locked their user directory, and</li> 
    122                <li>marked them as a snuffed user</li> 
    123        </ol> 
    124 <% 
    125        return ob_get_clean(); 
     113  ob_start(); 
     114%> 
     115  <ol> 
     116    <li>we changed their password, </li> 
     117    <li>disabled their ability to log in,</li> 
     118    <li>hid their tags,</li> 
     119    <li>hid their links,</li> 
     120    <li>censored their profile page,</li> 
     121    <li>locked their user directory, and</li> 
     122    <li>marked them as a snuffed user</li> 
     123  </ol> 
     124<% 
     125  return ob_get_clean(); 
    126126} 
    127127 
    128128function relight_user( $user ) { 
    129        return "not yet implemented"; 
    130        log_mesg_to( "re-lit user $user", "global" ); 
     129  return "not yet implemented"; 
     130  log_mesg_to( "re-lit user $user", "global" ); 
    131131} 
    132132 
    133133function get_salt( $s ) { 
    134        return "40ape"; # FIXME: hardcoded few now 
     134  return "40ape"; # FIXME: hardcoded few now 
    135135} 
    136136 
    137137function get_validation_code( $user ) { 
    138        return md5( $user . get_salt( $user )); 
     138  return md5( $user . get_salt( $user )); 
    139139} 
    140140 
    141141function log_mesg_to( $mesg, $to ) { 
    142        if( ! $mesg || ! $to ) 
    143                return; 
     142  if( ! $mesg || ! $to ) 
     143    return; 
    144144  $filename = get_root() . "/_logs/$to.log"; 
    145145  if( !$f = fopen( $filename, "a" )) { 
     
    152152 
    153153function get_ip() { 
    154        return getenv(HTTP_X_FORWARDED_FOR) ? getenv(HTTP_X_FORWARDED_FOR) : getenv(REMOTE_ADDR); 
     154  return getenv(HTTP_X_FORWARDED_FOR) ? getenv(HTTP_X_FORWARDED_FOR) : getenv(REMOTE_ADDR); 
    155155} 
    156156 
    157157function notify_added_as_peep( $u, $by ) { 
    158        $body = "Hi, $u. 
     158  $body = "Hi, $u. 
    159159 
    160160$by just added you as a fellow linker on Feed Me Links. 
     
    169169 
    170170"; 
    171        $headers = get_mail_headers(); 
    172        $to = getEmailAddressForUser( $u ); 
    173  
    174        $success = mail( $to, "Someone has added you as a contact", $body, $headers); 
    175        if( $success ) 
    176                log_mesg_to( "sent contact notification mail from $by to $to", "mail" ); 
    177        return $success; 
     171  $headers = get_mail_headers(); 
     172  $to = getEmailAddressForUser( $u ); 
     173 
     174  $success = mail( $to, "Someone has added you as a contact", $body, $headers); 
     175  if( $success ) 
     176    log_mesg_to( "sent contact notification mail from $by to $to", "mail" ); 
     177  return $success; 
    178178} 
    179179 
    180180# returns the PATH to the newly created thumbnail 
    181181function make_thumbnail_for_user( $u ) { 
    182        $i = get_profile_image( $u ); 
    183        $out = "/" . get_user_folder( $u ) . "/thumb.png"; 
    184        imagepng( get_png_thumbnail_from_image( $i ), get_root() . $out ); 
    185        return $out; 
     182  $i = get_profile_image( $u ); 
     183  $out = "/" . get_user_folder( $u ) . "/thumb.png"; 
     184  imagepng( get_png_thumbnail_from_image( $i ), get_root() . $out ); 
     185  return $out; 
    186186} 
    187187 
    188188# returns an image OBJECT of the newly created thumbnail 
    189189function get_png_thumbnail_from_image( $i ) { 
    190        $goal = 64; 
    191        list($width, $height, $type, $attr) = getimagesize( $i ); 
    192  
    193        #calculate which is the shorter side, re-map that to $goal, and calculate the new length of the longer side 
    194        $tall = ( $width < $height ) ? true : false; 
    195        $new_height = $new_width = $goal; 
    196        if( $tall ) { 
    197                $frac = $goal / $width; 
    198                $new_height = $height * $frac; 
    199        } else { 
    200                $frac = $goal / $height; 
    201                $new_width = $width * $frac; 
    202        
    203  
    204        switch( $type ){ 
    205                case 1 : $orig = imagecreatefromgif($i);  break; 
    206                case 2 : $orig = imagecreatefromjpeg($i); break; 
    207                case 3 : $orig = imagecreatefrompng($i);  break; 
    208                default: break; 
    209        
    210  
    211        # resize it: 
    212        $small = imagecreatetruecolor( $new_width, $new_height ); 
    213        imagecopyresampled( $small, $orig, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); 
    214        imagepng( $small, "icons/small.png" ); 
    215  
    216        # new black-bordered palette to copy onto: 
    217        $cropped = imagecreatetruecolor( $goal + 2, $goal + 2 ); 
    218        imagefill( $cropped, 0, 0, imagecolorallocate( $cropped, 0, 0, 0 )); 
    219  
    220        # crop image onto black 
    221        imagecopy($cropped, $small, 1, 1, ceil( ($new_width - $goal) / 2), ceil( ($new_height - $goal) / 2), $goal, $goal); 
    222  
    223        imagedestroy($small); 
    224        imagedestroy($orig); 
    225        return $cropped; 
     190  $goal = 64; 
     191  list($width, $height, $type, $attr) = getimagesize( $i ); 
     192 
     193  #calculate which is the shorter side, re-map that to $goal, and calculate the new length of the longer side 
     194  $tall = ( $width < $height ) ? true : false; 
     195  $new_height = $new_width = $goal; 
     196  if( $tall ) { 
     197    $frac = $goal / $width; 
     198    $new_height = $height * $frac; 
     199  } else { 
     200    $frac = $goal / $height; 
     201    $new_width = $width * $frac; 
     202 
     203 
     204  switch( $type ){ 
     205    case 1 : $orig = imagecreatefromgif($i);  break; 
     206    case 2 : $orig = imagecreatefromjpeg($i); break; 
     207    case 3 : $orig = imagecreatefrompng($i);  break; 
     208    default: break; 
     209 
     210 
     211  # resize it: 
     212  $small = imagecreatetruecolor( $new_width, $new_height ); 
     213  imagecopyresampled( $small, $orig, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); 
     214  imagepng( $small, "icons/small.png" ); 
     215 
     216  # new black-bordered palette to copy onto: 
     217  $cropped = imagecreatetruecolor( $goal + 2, $goal + 2 ); 
     218  imagefill( $cropped, 0, 0, imagecolorallocate( $cropped, 0, 0, 0 )); 
     219 
     220  # crop image onto black 
     221  imagecopy($cropped, $small, 1, 1, ceil( ($new_width - $goal) / 2), ceil( ($new_height - $goal) / 2), $goal, $goal); 
     222 
     223  imagedestroy($small); 
     224  imagedestroy($orig); 
     225  return $cropped; 
    226226} 
    227227 
    228228function get_id_for_existing_link( $url, $u ) { 
    229        $q = run_query( getQuery( "get_id_for_users_existing_link", $url, $u )); 
    230        return mysql_result($q, 0, "id" ); 
     229  $q = run_query( getQuery( "get_id_for_users_existing_link", $url, $u )); 
     230  return mysql_result($q, 0, "id" ); 
    231231} 
    232232 
    233233function diff_in_secs( $db_date_0, $db_date_1 ) { 
    234        $dt0 =  strtotime( $db_date_0 ); 
    235        $dt1 =  strtotime( $db_date_1 ); 
    236        return( $dt0 - $dt1 ); 
     234  $dt0 =  strtotime( $db_date_0 ); 
     235  $dt1 =  strtotime( $db_date_1 ); 
     236  return( $dt0 - $dt1 ); 
    237237} 
    238238 
    239239function get_db_date_diff_in_days( $qs ) { 
    240        $con_fuq_slot = mysql_connect(); 
    241        mysql_selectdb( getDBName() ); 
    242        $q = mysql_query( $qs ); 
    243        if( ! $q ) 
    244                return undefined; 
    245        return diff_driver( mysql_result($q,0), mysql_result($q,1)); 
     240  $con_fuq_slot = mysql_connect(); 
     241  mysql_selectdb( getDBName() ); 
     242  $q = mysql_query( $qs ); 
     243  if( ! $q ) 
     244    return undefined; 
     245  return diff_driver( mysql_result($q,0), mysql_result($q,1)); 
    246246} 
    247247 
    248248function diff_driver( $d0, $d1 ) { 
    249        $diff = round( diff_in_secs( $d0, $d1 ) / 60 / 60 / 24, 4); 
    250        return $diff > 0 ? $diff : 0; 
     249  $diff = round( diff_in_secs( $d0, $d1 ) / 60 / 60 / 24, 4); 
     250  return $diff > 0 ? $diff : 0; 
    251251} 
    252252 
    253253function get_now_db() { 
    254        return get_simple_rs( "SELECT now() AS now" ); 
     254  return get_simple_rs( "SELECT now() AS now" ); 
    255255} 
    256256 
    257257function get_simple_rs( $qs ) { 
    258        $con = mysql_connect(); 
    259        mysql_selectdb( getDBName() ); 
    260        $q = mysql_query( $qs ); 
    261        if( ! $q ) 
    262                return undefined; 
    263          
    264        return mysql_result($q,0); 
     258  $con = mysql_connect(); 
     259  mysql_selectdb( getDBName() ); 
     260  $q = mysql_query( $qs ); 
     261  if( ! $q ) 
     262    return undefined; 
     263   
     264  return mysql_result($q,0); 
    265265} 
    266266 
     
    268268 
    269269function get_fuq( $u ) { 
    270        $con = mysql_connect(); 
    271        mysql_selectdb( getDBName() ); 
    272        $qs = getQuery( "get_fuq", $u ); 
    273  
    274        $q = mysql_query( $qs ); 
    275        $num_rows = mysql_affected_rows($con); 
    276  
    277        if( $num_rows == 1 ) 
    278                return mysql_result($q, 0, "FUQ" ); 
    279        else  
    280                return 2; 
     270  $con = mysql_connect(); 
     271  mysql_selectdb( getDBName() ); 
     272  $qs = getQuery( "get_fuq", $u ); 
     273 
     274  $q = mysql_query( $qs ); 
     275  $num_rows = mysql_affected_rows($con); 
     276 
     277  if( $num_rows == 1 ) 
     278    return mysql_result($q, 0, "FUQ" ); 
     279  else  
     280    return 2; 
    281281} 
    282282 
    283283function delta_time_between_links( $u, $raw = false ) { 
    284        $value = get_db_date_diff_in_days( getQuery( "get_fuq_slot_delta_time_between_links", $u )) * 60; 
    285        if( $raw ) 
    286                return $value; 
    287        else 
    288                if( $value < 0.5 ) 
    289                        return -1; 
    290                else if( $value > 10 ) 
    291                        return 1; 
    292                else 
    293                        return 0; 
     284  $value = get_db_date_diff_in_days( getQuery( "get_fuq_slot_delta_time_between_links", $u )) * 60; 
     285  if( $raw ) 
     286    return $value; 
     287  else 
     288    if( $value < 0.5 ) 
     289      return -1; 
     290    else if( $value > 10 ) 
     291      return 1; 
     292    else 
     293      return 0; 
    294294} 
    295295 
    296296function recency( $u, $raw = false ) { 
    297        $d0 =  get_simple_rs( getQuery( "get_fuq_slot_recency", $u )); 
    298  
    299        $now = getdate(); 
    300        $d1 = $now['year'] ."-". ($now['mon'] < 10 ? "0" : "" ) . $now['mon'] ."-". ($now['mday'] < 10 ? "0" : "" ) . $now['mday'] ." ". $now['hours'] .":". $now['minutes'] .":". $now['seconds']; 
    301        $value = round( diff_in_secs( $d1, $d0 ) / 60 / 60 / 24, 3 ); 
    302        return $value; 
     297  $d0 =  get_simple_rs( getQuery( "get_fuq_slot_recency", $u )); 
     298 
     299  $now = getdate(); 
     300  $d1 = $now['year'] ."-". ($now['mon'] < 10 ? "0" : "" ) . $now['mon'] ."-". ($now['mday'] < 10 ? "0" : "" ) . $now['mday'] ." ". $now['hours'] .":". $now['minutes'] .":". $now['seconds']; 
     301  $value = round( diff_in_secs( $d1, $d0 ) / 60 / 60 / 24, 3 ); 
     302  return $value; 
    303303} 
    304304 
    305305function length_of_usage( $u, $raw = false ) { 
    306        $r0 = get_simple_rs( "SELECT createDate FROM links WHERE submitter = '" . urlencode( $u ) . "' ORDER BY id DESC LIMIT 1;" ); 
    307        $r1 = get_simple_rs( "SELECT createDate FROM links WHERE submitter = '" . urlencode( $u ) . "' ORDER BY id ASC LIMIT 1;" ); 
    308        $value = round( diff_in_secs( $r0, $r1 ) / 60 / 60 / 24, 1 ); 
    309        return $value; 
     306  $r0 = get_simple_rs( "SELECT createDate FROM links WHERE submitter = '" . urlencode( $u ) . "' ORDER BY id DESC LIMIT 1;" ); 
     307  $r1 = get_simple_rs( "SELECT createDate FROM links WHERE submitter = '" . urlencode( $u ) . "' ORDER BY id ASC LIMIT 1;" ); 
     308  $value = round( diff_in_secs( $r0, $r1 ) / 60 / 60 / 24, 1 ); 
     309  return $value; 
    310310} 
    311311 
     
    313313function recalculate_fuq( $u ) { 
    314314 
    315        # calculate new fuq: 
    316        $new_fuq = get_fuq( $u ); 
    317  
    318        $slots = array(  
    319                "delta_time_between_links" => 0,  
    320                "length_of_usage" => 0  
    321        ); 
    322  
    323        foreach( $slots as $key => $value ) { $slots[$key] =  call_user_func( $key, $u ); } 
    324  
    325        # mash together slot values here: 
    326        # TBD 
    327  
    328        #foreach( $slots as $slot ) { echo "\$slot = $slot<br>"; } 
    329  
    330        return; 
    331  
    332        # this method sig changed: run_query( getQuery( "update_fuq", $u, $new_fuq )); # and then set it 
    333        # this will break now: return $new_fuq; 
     315  # calculate new fuq: 
     316  $new_fuq = get_fuq( $u ); 
     317 
     318  $slots = array(  
     319    "delta_time_between_links" => 0,  
     320    "length_of_usage" => 0  
     321  ); 
     322 
     323  foreach( $slots as $key => $value ) { $slots[$key] =  call_user_func( $key, $u ); } 
     324 
     325  # mash together slot values here: 
     326  # TBD 
     327 
     328  #foreach( $slots as $slot ) { echo "\$slot = $slot<br>"; } 
     329 
     330  return; 
     331 
     332  # this method sig changed: run_query( getQuery( "update_fuq", $u, $new_fuq )); # and then set it 
     333  # this will break now: return $new_fuq; 
    334334} 
    335335 
    336336function run_query( $qs ) { 
    337        $con = mysql_connect(); 
    338        mysql_selectdb( getDBName() ); 
    339        $q = mysql_query( $qs ); 
    340        return $q; 
     337  $con = mysql_connect(); 
     338  mysql_selectdb( getDBName() ); 
     339  $q = mysql_query( $qs ); 
     340  return $q; 
    341341} 
    342342 
    343343function run_count( $qs ) { 
    344        $con = mysql_connect(); 
    345        mysql_selectdb( getDBName() ); 
    346        $q = mysql_query( $qs ); 
    347  
    348        if( $q ) 
    349         return mysql_result($q,0); 
    350        else 
    351                return 0; 
     344  $con = mysql_connect(); 
     345  mysql_selectdb( getDBName() ); 
     346  $q = mysql_query( $qs ); 
     347 
     348  if( $q ) 
     349  return mysql_result($q,0); 
     350  else 
     351    return 0; 
    352352} 
    353353 
    354354# usr dirs are of the path /usr/u/username 
    355355function get_user_folder( $u ) { 
    356        if( !$u )  
    357                return "usr/NO_PROFILE/"; 
    358        else 
    359                return "usr/" . substr( $u, 0, 1 ) . "/" . $u; 
     356  if( !$u )  
     357    return "usr/NO_PROFILE/"; 
     358  else 
     359    return "usr/" . substr( $u, 0, 1 ) . "/" . $u; 
    360360} 
    361361 
    362362function ajax_edit( $u, $tok, $type, $subtype, $l, $new_value, $use_xajax = false ) { 
    363        if( ! auth( $u, $tok ) ) 
    364                return; 
    365        $con = mysql_connect(); 
    366        mysql_selectdb( getDBName() ); 
    367  
    368        $new_value = o( $new_value ); 
    369        if( $type == "checkbox" ) 
    370                $new_value = ($new_value == 'false') ? "NULL" : 1; 
    371  
    372        $qs = getQuery( "edit_$subtype", "$new_value", $l, $u ); 
    373  
    374        if( $debug ) echo "DEBUG: $qs<br>"; 
    375        $arg = "$u, $tok, $type, $subtype, $l, $new_value, $use_xajax"; 
    376  
    377        $q = mysql_query( $qs ); 
    378        $num_rows = mysql_affected_rows($con); 
    379        $e = mysql_error($con); 
    380  
    381        if( $debug ) echo "\$q: $q<br>"; 
    382        if( $debug ) echo "\$num_rows: $num_rows<br>"; 
    383  
    384        if( $num_rows > 0 || ! $e ) 
    385                $success = "success"; 
    386  
    387        $safe_value = preg_replace( "/'/", "\'", $new_value ); 
    388          
    389        conClose( $con ); 
    390        if( $use_xajax ) { 
    391                $objResponse = new xajaxResponse(); 
    392                if( $success ) 
    393                        $objResponse->addScript( "update_" . $type . "_after_save( '$safe_value' );" ); 
    394                else 
    395                        $objResponse->addAssign( "content-hook", "innerHTML", "Oops, we messed up: <b>$qs / $arg)</b>!!!" ); 
    396                #$objResponse->addAssign( "content-hook", "innerHTML", "Oops, we messed up: <b>Database $e (edit_$subtype / $qs / $arg)</b>!!!" ); 
    397                // FIXME: in the above else case, also unroll the edit also so the user can try again 
    398                return $objResponse->getXML(); 
    399        } else 
    400                return $success ? true : ""; 
     363  if( ! auth( $u, $tok ) ) 
     364    return; 
     365  $con = mysql_connect(); 
     366  mysql_selectdb( getDBName() ); 
     367 
     368  $new_value = o( $new_value ); 
     369  if( $type == "checkbox" ) 
     370    $new_value = ($new_value == 'false') ? "NULL" : 1; 
     371 
     372  $qs = getQuery( "edit_$subtype", "$new_value", $l, $u ); 
     373 
     374  if( $debug ) echo "DEBUG: $qs<br>"; 
     375  $arg = "$u, $tok, $type, $subtype, $l, $new_value, $use_xajax"; 
     376 
     377  $q = mysql_query( $qs ); 
     378  $num_rows = mysql_affected_rows($con); 
     379  $e = mysql_error($con); 
     380 
     381  if( $debug ) echo "\$q: $q<br>"; 
     382  if( $debug ) echo "\$num_rows: $num_rows<br>"; 
     383 
     384  if( $num_rows > 0 || ! $e ) 
     385    $success = "success"; 
     386 
     387  $safe_value = preg_replace( "/'/", "\'", $new_value ); 
     388   
     389  conClose( $con ); 
     390  if( $use_xajax ) { 
     391    $objResponse = new xajaxResponse(); 
     392    if( $success ) 
     393      $objResponse->addScript( "update_" . $type . "_after_save( '$safe_value' );" ); 
     394    else 
     395      $objResponse->addAssign( "content-hook", "innerHTML", "Oops, we messed up: <b>$qs / $arg)</b>!!!" ); 
     396    #$objResponse->addAssign( "content-hook", "innerHTML", "Oops, we messed up: <b>Database $e (edit_$subtype / $qs / $arg)</b>!!!" ); 
     397    // FIXME: in the above else case, also unroll the edit also so the user can try again 
     398    return $objResponse->getXML(); 
     399  } else 
     400    return $success ? true : ""; 
    401401} 
    402402 
    403403function add_tags_from_request( $u, $newLinkID, $catNums, $con ) { 
    404        if( $debug ) echo "<hr>"; 
    405  
    406        $newCatNames = Array(); 
    407  
    408        foreach( $_POST as $paramName => $newCatName ) { 
    409                if( substr( $paramName, 0, strlen( "new_tag" )) == "new_tag" && $newCatName) { 
    410                        array_push( $newCatNames, $newCatName ); 
    411                
    412        
    413  
    414        foreach( $_GET as $paramName => $newCatName ) { 
    415                if( substr( $paramName, 0, strlen( "new_tag" )) == "new_tag" && $newCatName) { 
    416                        array_push( $newCatNames, $newCatName ); 
    417                
    418        
    419  
    420        if( $debug ) { 
    421                echo "User entered " . count( $newCatNames ) . " 'new' tags: "; 
    422                print_r( $newCatNames ); 
    423                echo "<br>And " . count( $catNums ) . " existing tags: "; 
    424                print_r( $catNums ); 
    425                echo "<br>"; 
    426        
    427  
    428        if( count( $newCatNames )) { 
    429                foreach( $newCatNames as $newCategoryName ) { 
    430                        if( $debug ) 
    431                                echo "First we add any tags that are actually new, then we tag the link w/all the remaining existing tags:<br />"; 
    432                        $categoryAlreadyExists = getIdForFolder( $newCategoryName, $u ); 
    433                        if( ! $categoryAlreadyExists ) { 
    434                                if( $debug ) 
    435                                        echo "User $u correctly claims s/he has no " . getCatName() . " called '$newCategoryName' yet...<br />"; 
    436                                $newCatNum = addFolder( $newCategoryName, $u ); 
    437                                if( $debug ) 
    438                                        echo "added $newCategoryName successfully as: \$newCatNum: $newCatNum<br />"; 
    439                                if( $newCatNum ) 
    440                                        array_push( $catNums, $newCatNum ); 
    441                        } else 
    442                                # user actually already had this tag, so just push it onto the rest of the existing tags to tag this link with 
    443                                array_push( $catNums, $categoryAlreadyExists ); 
    444                
    445        } else { 
    446                if( $debug ) 
    447                        echo "no new " . getCatsName() . " to be added, adding to existing " . getCatsName() . "...<br />"; 
    448        
    449  
    450        #starting sentinal value: 
    451        $q = "bogus query"; 
    452  
    453        if( $debug )  
    454                echo "<br />DEBUG: looping over " . (count($catNums)) . " tags"; 
    455        foreach( $catNums as $catNum ) { 
    456                if( ! $q || ! $catNum ) 
    457                        return; 
    458                if( $debug ) echo "<br />"; 
    459  
    460                $qs = getQuery( "add_new_category_xref", $newLinkID, $catNum ); 
    461                if( $debug ) 
    462                        echo "DEBUG: $qs"; 
    463                $q = mysql_query( $qs ); 
    464                if( ! $q ) { 
    465                        $e = mysql_error($con); 
    466                        $tok = get_mysql_dupe_error(); 
    467                        if( ! strstr( $e, $tok)) { 
     404  if( $debug ) echo "<hr>"; 
     405 
     406  $newCatNames = Array(); 
     407 
     408  foreach( $_POST as $paramName => $newCatName ) { 
     409    if( substr( $paramName, 0, strlen( "new_tag" )) == "new_tag" && $newCatName) { 
     410      array_push( $newCatNames, $newCatName ); 
     411   
     412 
     413 
     414  foreach( $_GET as $paramName => $newCatName ) { 
     415    if( substr( $paramName, 0, strlen( "new_tag" )) == "new_tag" && $newCatName) { 
     416      array_push( $newCatNames, $newCatName ); 
     417   
     418 
     419 
     420  if( $debug ) { 
     421    echo "User entered " . count( $newCatNames ) . " 'new' tags: "; 
     422    print_r( $newCatNames ); 
     423    echo "<br>And " . count( $catNums ) . " existing tags: "; 
     424    print_r( $catNums ); 
     425    echo "<br>"; 
     426 
     427 
     428  if( count( $newCatNames )) { 
     429    foreach( $newCatNames as $newCategoryName ) { 
     430      if( $debug ) 
     431        echo "First we add any tags that are actually new, then we tag the link w/all the remaining existing tags:<br />"; 
     432      $categoryAlreadyExists = getIdForFolder( $newCategoryName, $u ); 
     433      if( ! $categoryAlreadyExists ) { 
     434        if( $debug ) 
     435          echo "User $u correctly claims s/he has no " . getCatName() . " called '$newCategoryName' yet...<br />"; 
     436        $newCatNum = addFolder( $newCategoryName, $u ); 
     437        if( $debug ) 
     438          echo "added $newCategoryName successfully as: \$newCatNum: $newCatNum<br />"; 
     439        if( $newCatNum ) 
     440          array_push( $catNums, $newCatNum ); 
     441      } else 
     442        # user actually already had this tag, so just push it onto the rest of the existing tags to tag this link with 
     443        array_push( $catNums, $categoryAlreadyExists ); 
     444   
     445  } else { 
     446    if( $debug ) 
     447      echo "no new " . getCatsName() . " to be added, adding to existing " . getCatsName() . "...<br />"; 
     448 
     449 
     450  #starting sentinal value: 
     451  $q = "bogus query"; 
     452 
     453  if( $debug )  
     454    echo "<br />DEBUG: looping over " . (count($catNums)) . " tags"; 
     455  foreach( $catNums as $catNum ) { 
     456    if( ! $q || ! $catNum ) 
     457      return; 
     458    if( $debug ) echo "<br />"; 
     459 
     460    $qs = getQuery( "add_new_category_xref", $newLinkID, $catNum ); 
     461    if( $debug ) 
     462      echo "DEBUG: $qs"; 
     463    $q = mysql_query( $qs ); 
     464    if( ! $q ) { 
     465      $e = mysql_error($con); 
     466      $tok = get_mysql_dupe_error(); 
     467      if( ! strstr( $e, $tok)) { 
    468468%> 
    469469<div class="attention warning"> 
     
    472472</div> 
    473473<% 
    474                                break; 
    475                        } else { 
    476                                e( "already tagged w/that tag -- no worries!" ); 
    477                                # fake a real result set so our caller thinks we succeeded, since we did 
    478                                $q = 1; 
    479                        
    480                
    481        
     474        break; 
     475      } else { 
     476        e( "already tagged w/that tag -- no worries!" ); 
     477        # fake a real result set so our caller thinks we succeeded, since we did 
     478        $q = 1; 
     479     
     480   
     481 
    482482return $q; 
    483483}  
     
    485485 
    486486function get_mysql_dupe_error() { 
    487        return "Duplicate entry"; 
    488 } 
    489  
    490        function dumpParams() { 
    491        %> 
    492        <% 
    493                foreach( $_GET as $pname => $pval ) { e( $pname . " = " . $pval ); } 
    494                e( "" ); 
    495                foreach( $_POST as $pname => $pval ) { e( $pname . " = " . $pval ); } 
    496                %> 
    497                <% 
    498        
    499  
    500        # search to see if the URL exists anywhere public 
    501        function link_exists( $url ) { 
    502                $qs = getQuery( "get_id_for_existing_link", $url ); 
    503                $q = mysql_query( $qs ); 
    504                if( $q && mysql_num_rows($q) ) 
    505                        return mysql_result( $q, 0, "id" ); 
    506        
    507  
    508        function users_link_exists( $u, $url ) { 
    509                $url = urlencode( $url ); 
    510                $con_users_link_exists = mysql_connect(); 
    511                mysql_selectdb( getDBName() ); 
    512                $qs = "SELECT COUNT(id) as count FROM links WHERE url = '$url' AND submitter = '$u';";  
    513                $q = mysql_query( $qs ); 
    514                if( $q ) 
    515                        $count = mysql_result($q, 0, "count" ); 
    516                conClose( $con_users_link_exists ); 
    517                return $count; 
    518        
    519  
    520        function users_tag_exists( $u, $tag ) { 
    521                $tag = urlencode( $tag ); 
    522                $con_users_tag_exists = mysql_connect(); 
    523                mysql_selectdb( getDBName() ); 
    524                $qs = "SELECT COUNT(id) as count FROM linksgroups WHERE name = '$tag' AND userid = '$u';";  
    525                $q = mysql_query( $qs ); 
    526                if( $q ) 
    527                        $count = mysql_result($q, 0, "count" ); 
    528                conClose( $con_users_tag_exists ); 
    529                return $count; 
    530        
    531  
    532        # can this user import more than max_links? 
    533        function is_privileged_importer( $userId ) { 
    534                $pi = Array(); 
    535  
    536                $pi["jm3"] = true; 
    537                $pi["pheezy"] = true; 
    538                $pi["gus"] = true; 
    539                $pi["dcnstctr"] = true; 
    540                $pi["formalplay"] = true; 
    541                $pi["testuser"] = true; 
    542                $pi["samanthers"] = true; 
    543  
    544                #$pi["rrhobbs"] = true; 
    545                #$pi["Madrisa"] = true; 
    546                #$pi["stutes"] = true; 
    547                #$pi["gagliaudo"] = true; 
    548                #$pi["auslander"] = true; 
    549  
    550                return $pi[ $userId ]; 
    551        
    552  
    553        class Config { 
    554                function get_import_links_cache_file_name() { return "LINKS.RAW"; } 
    555                function get_deduped_links_cache_file_name() { return "LINKS.READY"; } 
    556                function get_import_tags_cache_file_name() { return "TAGS.RAW"; } 
    557                function get_deduped_tags_cache_file_name() { return "TAGS.READY"; } 
    558                function get_delimeter() { return "___DELIMETER___"; } 
    559                function get_import_tag_name() { return "imported links"; } 
    560        
    561        $config = new Config; 
    562  
    563        function make_user_dir( $u ) { 
    564                chdir( get_root() ); 
    565                $u_dir = get_user_folder( $u ? $u : get_user() ); 
    566                if( ! is_dir( $u_dir )) { 
    567                        $old_umask = umask(0); 
    568                        mkdir( $u_dir ); 
    569                        umask($old_umask); 
    570                
    571                if( !is_dir( $u_dir ))  
    572                        fatal( "Your user folder could not be created." ); 
    573                else if( ! is_writable( $u_dir )) 
    574                        fatal( "Your folder was not writable." ); 
    575                else 
    576                        return $u_dir; 
    577        
    578  
    579        function open_user_file_for_write( $f, $u ) { 
    580                $u_dir = make_user_dir( $u ); 
    581                if( ! is_writable( $u_dir )) 
    582                        fatal( "Your folder was not writable." ); 
    583                else { 
    584                        $f = $u_dir . "/" . escapeshellcmd( $f ); 
    585                        if( is_file( $f )) 
    586                                l( "warning: opening existing user $u's file $f for write and erasing it" ); 
    587                        return fopen( $f, "w" ); 
    588                
    589        
    590          
    591        # NOTE THAT THE RETURN SEMANTICS OF _for_read ARE VERY DIFFERENT THAN _for_write; WE RETURN AN ARRAY, NOT A FILE_POINTER!! 
    592        function open_user_file_for_read( $f ) { 
    593                chdir( get_root() ); 
    594                $u_dir = get_user_folder( get_user() ); 
    595                $f = $u_dir . "/" . escapeshellcmd( $f ); 
    596                if( ! is_dir( $u_dir ))  
    597                        fatal( "Your user folder could not be found." ); 
    598                else if( !is_file( $f ))  
    599                        l( "the file $f could not be found." ); 
    600                else 
    601                        return file( $f ); 
    602        
    603  
    604        function get_bookmarks_info( $u ) { 
    605                $b = Array(); 
    606                $b["file_name"] = "/imported-favorites.html"; 
    607                $b["field_name"] = "bookmarks_file"; 
    608                $b["dir"] = get_root() . "/" . get_user_folder( $u ); 
    609                $b["path"] = $b["dir"] . $b["file_name"]; 
    610  
    611                return $b; 
    612        
    613  
    614        function e( $m ) { 
    615                echo "$m<br>\n"; 
    616        
    617          
    618        function croak( $m ) { 
    619                ob_clean(); 
    620                exit( $m ); 
    621        
    622  
    623        function l( $m ) { 
    624                trigger_error( $m ); 
    625        
    626          
    627        function comment( $m ) { 
    628                echo "\n<!-- $m -->\n"; 
    629        
    630  
    631        function fatal( $m ) { 
    632                l( "FATAL ERROR.", $m ); 
    633                return tell( "Darn! ", $m ); 
    634        
    635  
    636        function warn( $m ) { 
    637                return tell( "Warning.", $m ); 
    638        
    639  
    640        function shout( $t, $m ) { 
    641                tell( $t, $m, $wide = true ); 
    642        
    643  
    644  
    645        function startTell( $t = "", $wide = false ) { 
     487  return "Duplicate entry"; 
     488} 
     489 
     490  function dumpParams() { 
     491  %> 
     492  <% 
     493    foreach( $_GET as $pname => $pval ) { e( $pname . " = " . $pval ); } 
     494    e( "" ); 
     495    foreach( $_POST as $pname => $pval ) { e( $pname . " = " . $pval ); } 
     496    %> 
     497    <% 
     498 
     499 
     500  # search to see if the URL exists anywhere public 
     501  function link_exists( $url ) { 
     502    $qs = getQuery( "get_id_for_existing_link", $url ); 
     503    $q = mysql_query( $qs ); 
     504    if( $q && mysql_num_rows($q) ) 
     505      return mysql_result( $q, 0, "id" ); 
     506 
     507 
     508  function users_link_exists( $u, $url ) { 
     509    $url = urlencode( $url ); 
     510    $con_users_link_exists = mysql_connect(); 
     511    mysql_selectdb( getDBName() ); 
     512    $qs = "SELECT COUNT(id) as count FROM links WHERE url = '$url' AND submitter = '$u';";  
     513    $q = mysql_query( $qs ); 
     514    if( $q ) 
     515      $count = mysql_result($q, 0, "count" ); 
     516    conClose( $con_users_link_exists ); 
     517    return $count; 
     518 
     519 
     520  function users_tag_exists( $u, $tag ) { 
     521    $tag = urlencode( $tag ); 
     522    $con_users_tag_exists = mysql_connect(); 
     523    mysql_selectdb( getDBName() ); 
     524    $qs = "SELECT COUNT(id) as count FROM linksgroups WHERE name = '$tag' AND userid = '$u';";  
     525    $q = mysql_query( $qs ); 
     526    if( $q ) 
     527      $count = mysql_result($q, 0, "count" ); 
     528    conClose( $con_users_tag_exists ); 
     529    return $count; 
     530 
     531 
     532  # can this user import more than max_links? 
     533  function is_privileged_importer( $userId ) { 
     534    $pi = Array(); 
     535 
     536    $pi["jm3"] = true; 
     537    $pi["pheezy"] = true; 
     538    $pi["gus"] = true; 
     539    $pi["dcnstctr"] = true; 
     540    $pi["formalplay"] = true; 
     541    $pi["testuser"] = true; 
     542    $pi["samanthers"] = true; 
     543 
     544    #$pi["rrhobbs"] = true; 
     545    #$pi["Madrisa"] = true; 
     546    #$pi["stutes"] = true; 
     547    #$pi["gagliaudo"] = true; 
     548    #$pi["auslander"] = true; 
     549 
     550    return $pi[ $userId ]; 
     551 
     552 
     553  class Config { 
     554    function get_import_links_cache_file_name() { return "LINKS.RAW"; } 
     555    function get_deduped_links_cache_file_name() { return "LINKS.READY"; } 
     556    function get_import_tags_cache_file_name() { return "TAGS.RAW"; } 
     557    function get_deduped_tags_cache_file_name() { return "TAGS.READY"; } 
     558    function get_delimeter() { return "___DELIMETER___"; } 
     559    function get_import_tag_name() { return "imported links"; } 
     560 
     561  $config = new Config; 
     562 
     563  function make_user_dir( $u ) { 
     564    chdir( get_root() ); 
     565    $u_dir = get_user_folder( $u ? $u : get_user() ); 
     566    if( ! is_dir( $u_dir )) { 
     567      $old_umask = umask(0); 
     568      mkdir( $u_dir ); 
     569      umask($old_umask); 
     570   
     571    if( !is_dir( $u_dir ))  
     572      fatal( "Your user folder could not be created." ); 
     573    else if( ! is_writable( $u_dir )) 
     574      fatal( "Your folder was not writable." ); 
     575    else 
     576      return $u_dir; 
     577 
     578