| 1 |
<?php |
<?php |
| 2 |
// $Id: hidden.module,v 1.6 2008/12/12 19:23:35 ekes Exp $ |
// $Id: hidden.module,v 1.7 2008/12/13 18:42:43 ekes Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 632 |
$links['hidden-unhide'] = array( |
$links['hidden-unhide'] = array( |
| 633 |
'title' => t('unhide'), |
'title' => t('unhide'), |
| 634 |
'href' => "hidden/$type/$target/unhide", |
'href' => "hidden/$type/$target/unhide", |
|
// 'query' => drupal_get_destination(), |
|
| 635 |
); |
); |
| 636 |
} |
} |
| 637 |
} |
} |
| 638 |
|
|
|
// return theme('hidden_link', $links); |
|
|
return $links; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Formats hide unhide links for hidden_link(). |
|
|
* |
|
|
* make hide/unhide links for nodes and comments if permissions allow. |
|
|
* |
|
|
* @param $links |
|
|
* array of links. |
|
|
* @return |
|
|
* array of links to add with any classes etc. |
|
|
* @ingroup themeable. |
|
|
*/ |
|
|
function theme_hidden_link($links) { |
|
| 639 |
return $links; |
return $links; |
| 640 |
} |
} |
| 641 |
|
|
| 844 |
/** |
/** |
| 845 |
* Checks if item is hidden. |
* Checks if item is hidden. |
| 846 |
* |
* |
|
* @todo this isn't an internal function. |
|
|
* |
|
| 847 |
* @param $type |
* @param $type |
| 848 |
* string content 'comment' or 'node'. |
* string content 'comment' or 'node'. |
| 849 |
* @param $id |
* @param $id |
| 884 |
/** |
/** |
| 885 |
* Gets a hidden item. |
* Gets a hidden item. |
| 886 |
* |
* |
|
* TODO It is making a lot of queries with the LEFT JOIN which is often not needed in results |
|
|
* |
|
| 887 |
* @param $type |
* @param $type |
| 888 |
* string content type 'comment' or 'node' |
* string content type 'comment' or 'node' |
| 889 |
* @param $id |
* @param $id |
| 890 |
* int cid or nid |
* int cid or nid |
| 891 |
* @return |
* @return |
| 892 |
* array(hid, rid, title, description, publicnote, privatenote) or bool FALSE not hidden |
* object -> hid, rid, title, description, publicnote, privatenote, or FALSE not hidden |
| 893 |
*/ |
*/ |
| 894 |
function _hidden_hidden_get($type, $id) { |
function _hidden_hidden_get($type, $id) { |
| 895 |
if ($type == 'node') { |
if ($type == 'node') { |
| 896 |
$query = 'SELECT h.hid, h.nid, h.rid, h.publicnote, h.privatenote, h.delay,'. |
$query = 'SELECT h.hid, h.nid, h.rid, h.uid, h.publicnote, h.privatenote, h.delay,'. |
| 897 |
' r.title, r.description'. |
' r.title, r.description'. |
| 898 |
" FROM {hidden_node} AS h". |
" FROM {hidden_node} AS h". |
| 899 |
' LEFT JOIN {hidden_reasons} AS r ON h.rid=r.rid'. |
' LEFT JOIN {hidden_reasons} AS r ON h.rid=r.rid'. |
| 900 |
" WHERE nid=%d"; |
" WHERE nid=%d"; |
| 901 |
} |
} |
| 902 |
elseif ($type == 'comment') { |
elseif ($type == 'comment') { |
| 903 |
$query = 'SELECT h.hid, h.cid, h.rid, h.publicnote, h.privatenote, h.delay,'. |
$query = 'SELECT h.hid, h.cid, h.rid, h.uid, h.publicnote, h.privatenote, h.delay,'. |
| 904 |
' r.title, r.description'. |
' r.title, r.description'. |
| 905 |
" FROM {hidden_comment} AS h". |
" FROM {hidden_comment} AS h". |
| 906 |
' LEFT JOIN {hidden_reasons} AS r ON h.rid=r.rid'. |
' LEFT JOIN {hidden_reasons} AS r ON h.rid=r.rid'. |
| 1231 |
* @return |
* @return |
| 1232 |
* bool TRUE on reason id being valid. |
* bool TRUE on reason id being valid. |
| 1233 |
*/ |
*/ |
| 1234 |
function _hidden_reason_check($rid) { |
function hidden_reason_check($rid) { |
| 1235 |
if (! $result = db_query('SELECT COUNT(*) FROM {hidden_reasons} WHERE rid=%d', $rid)) { |
if (! $result = db_query('SELECT COUNT(*) FROM {hidden_reasons} WHERE rid=%d', $rid)) { |
| 1236 |
_hidden_log(HIDDEN_LOG_DEBUG, '_hidden_reason_check() SELECT failed.', 'reason', $rid); |
_hidden_log(HIDDEN_LOG_DEBUG, 'hidden_reason_check() SELECT failed.', 'reason', $rid); |
| 1237 |
} |
} |
| 1238 |
if (db_result($result) != 0) { |
if (db_result($result) != 0) { |
| 1239 |
return TRUE; |
return TRUE; |
| 1246 |
/** |
/** |
| 1247 |
* Returns a hidden reason. |
* Returns a hidden reason. |
| 1248 |
* |
* |
| 1249 |
* Internally cached as view integration uses this rather than a join |
* Internally cached as view integration uses this rather than a join. |
| 1250 |
|
* |
| 1251 |
|
* @param $rid |
| 1252 |
|
* int reason id. |
| 1253 |
|
* @param $only_enabled |
| 1254 |
|
* bool return only if enabled, default TRUE. |
| 1255 |
|
* @param $cache |
| 1256 |
|
* bool use cached results, default TRUE. |
| 1257 |
|
* @return |
| 1258 |
|
* object individual reason or FALSE. |
| 1259 |
|
*/ |
| 1260 |
|
function hidden_reason_get($rid, $only_enabled = TRUE, $cache = TRUE) { |
| 1261 |
|
return _hidden_reason_get($rid, $only_enabled, $cache); |
| 1262 |
|
} |
| 1263 |
|
|
| 1264 |
|
/** |
| 1265 |
|
* Returns array of all hidden reasons. |
| 1266 |
|
* |
| 1267 |
|
* @param $only_enabled |
| 1268 |
|
* bool return only enabled reasons, default TRUE. |
| 1269 |
|
* @param $cache |
| 1270 |
|
* bool use cached results default TRUE. |
| 1271 |
|
* @return |
| 1272 |
|
* object array of reason objects. |
| 1273 |
|
*/ |
| 1274 |
|
function hidden_reason_get_all($only_enabled = TRUE, $cache = TRUE) { |
| 1275 |
|
return _hidden_reason_get(-1, $only_enabled, $cache); |
| 1276 |
|
} |
| 1277 |
|
|
| 1278 |
|
/** |
| 1279 |
|
* Internal function to handle storing and returning reasons. |
| 1280 |
|
* |
| 1281 |
|
* Cached as used as a non-join table used by views. |
| 1282 |
* |
* |
| 1283 |
* @param $rid |
* @param $rid |
| 1284 |
* int reason id (optional) |
* int reason id (or -1 for all reasons). |
| 1285 |
|
* @param $only_enabled |
| 1286 |
|
* bool only return enabled reasons. |
| 1287 |
|
* @param $cache |
| 1288 |
|
* bool use cache. |
| 1289 |
* @return |
* @return |
| 1290 |
* object individual reason or array of reason objects or FALSE. |
* array of reason object, an inidividual hidden object, FALSE |
| 1291 |
*/ |
*/ |
| 1292 |
function hidden_reason_get($rid = FALSE) { |
function _hidden_reason_get($rid, $only_enabled, $cache) { |
| 1293 |
static $reasons; |
static $reasons; |
| 1294 |
|
|
| 1295 |
if (! is_array($reasons)) { |
if (! is_array($reasons) || ! $cache) { |
| 1296 |
if (! $result = db_query('SELECT * FROM {hidden_reasons} WHERE enabled=1')) { |
$query = 'SELECT * FROM {hidden_reasons}'; |
| 1297 |
|
if ($only_enabled) { |
| 1298 |
|
$query .= ' WHERE enabled=1'; |
| 1299 |
|
} |
| 1300 |
|
if (! $result = db_query($query)) { |
| 1301 |
_hidden_log(HIDDEN_LOG_DEBUG, 'hidden_reason_get() SELECT failed', 'reason', $rid); |
_hidden_log(HIDDEN_LOG_DEBUG, 'hidden_reason_get() SELECT failed', 'reason', $rid); |
| 1302 |
} |
} |
| 1303 |
$reasons = array(); |
$reasons = array(); |
| 1306 |
} |
} |
| 1307 |
} |
} |
| 1308 |
|
|
| 1309 |
if ($rid) { |
if ($rid > 0) { |
| 1310 |
return $reasons[$rid]; |
return isset($reasons[$rid]) ? $reasons[$rid] : FALSE; |
| 1311 |
} |
} |
| 1312 |
else { |
else { |
| 1313 |
return $reasons; |
return $reasons; |
| 1314 |
} |
} |
| 1315 |
} |
} |
| 1316 |
|
|
| 1317 |
|
// @todo all of these can go into hidden_reason_write() :- |
| 1318 |
|
|
| 1319 |
/** |
/** |
| 1320 |
* Creates an options array of active hidden reasons. |
* Update hidden reason. |
|
* |
|
|
* For hide form an options list of reason titles mapped to reason id. |
|
| 1321 |
* |
* |
| 1322 |
|
* @param $title |
| 1323 |
|
* string plain text(255) title for hidden_reason. |
| 1324 |
|
* @param $description |
| 1325 |
|
* string html text describing reason |
| 1326 |
|
* @param $enabled |
| 1327 |
|
* bool TRUE for an enabled reason |
| 1328 |
|
* @param $rid |
| 1329 |
|
* rid - reason id |
| 1330 |
* @return |
* @return |
| 1331 |
* array options array |
* FALSE fail, SAVED_* const for type of saved |
| 1332 |
*/ |
*/ |
| 1333 |
function _hidden_reasons_get_options() { |
function hidden_reason_update($title, $description, $enabled, $rid) { |
| 1334 |
$result = db_query('SELECT rid, title FROM {hidden_reasons} WHERE enabled=1'); |
if (hidden_reason_check($rid)) { |
| 1335 |
if (! $result) { |
if (! db_query("UPDATE {hidden_reasons} SET title='%s', description='%s', enabled=%d WHERE rid=%d", $title, $description, $enabled, $rid)) { |
| 1336 |
_hidden_log(HIDDEN_LOG_DEBUG, '_hidden_reasons_get_options() SELECT failed.'); |
_hidden_log(HIDDEN_LOG_DEBUG, '_hidden_reasons_admin_form_submit() failed UPDATE.', 'reason', $rid); |
| 1337 |
|
return FALSE; |
| 1338 |
|
} |
| 1339 |
|
return $rid; |
| 1340 |
} |
} |
| 1341 |
$options = array(0 => t('No standard reason')); |
else { |
| 1342 |
while ($reason = db_fetch_object($result)) { |
_hidden_log(HIDDEN_LOG_DEBUG, '_hidden_reasons_admin_form_submit() passed invalid $rid.', 'reason', $rid); |
| 1343 |
$options[$reason->rid] = $reason->title; |
return FALSE; |
| 1344 |
} |
} |
|
|
|
|
return $options; |
|
| 1345 |
} |
} |
| 1346 |
|
|
| 1347 |
/******************************************************************** |
/** |
| 1348 |
* Filters |
* Create hidden reason |
| 1349 |
*/ |
* |
| 1350 |
|
* @param $title |
| 1351 |
|
* string plain text(255) title for reason. |
| 1352 |
|
* @param $description |
| 1353 |
|
* string html text description of reason. |
| 1354 |
|
* @param $enabled |
| 1355 |
|
* bool TRUE for an enabled reason. |
| 1356 |
|
*/ |
| 1357 |
|
function hidden_reason_create($title, $description, $enabled = TRUE) { |
| 1358 |
|
if (! db_query("INSERT INTO {hidden_reasons} (title, description, enabled) VALUES ('%s', '%s', %d)", $title, $description, $enabled)) { |
| 1359 |
|
_hidden_log(HIDDEN_LOG_DEBUG, '_hidden_reasons_form_submit() failed INSERT.'); |
| 1360 |
|
return FALSE; |
| 1361 |
|
} |
| 1362 |
|
$rid = db_last_insert_id("{hidden_reasons}_rid", "rid"); |
| 1363 |
|
return $rid; |
| 1364 |
|
} |
| 1365 |
|
|
| 1366 |
/** |
/** |
| 1367 |
* form; list enabled filters, submission changes weight order |
* Enable a reason. |
| 1368 |
|
* |
| 1369 |
|
* @param $rid |
| 1370 |
|
* rid |
| 1371 |
|
* @return |
| 1372 |
|
* bool TRUE on success |
| 1373 |
*/ |
*/ |
| 1374 |
function hidden_filters_admin_enabled_form() { |
function hidden_reason_enable($rid) { |
| 1375 |
$form['header'] = array( |
if (! db_query('UPDATE {hidden_reasons} SET enabled=1 WHERE rid=%d', $rid)) { |
| 1376 |
'#type' => 'value', |
_hidden_log(HIDDEN_LOG_DEBUG, t('hidden_reason_enable failed enable reason'), $rid); |
| 1377 |
'#value' => array( |
return FALSE; |
|
array('data' => t('filter name')), |
|
|
array('data' => t('hits')), |
|
|
array('data' => t('date')), |
|
|
array('data' => t('weight')), |
|
|
array('data' => t('operations'), 'colspan' => 2), |
|
|
) |
|
|
); |
|
|
$query = 'SELECT f.hfid as hfid, f.hits as hits, f.lasthit as date, f.weight as weight,' |
|
|
.' r.title' |
|
|
.' FROM {hidden_filters} AS f' |
|
|
.' INNER JOIN {hidden_filter_reasons} AS r ON f.hfid = r.hfid' |
|
|
.' WHERE f.enabled=1' |
|
|
.' ORDER BY f.weight DESC'; |
|
|
$result = db_query($query); |
|
|
$form['weight'] = array('#tree' => TRUE); |
|
|
while ($filter = db_fetch_object($result)) { |
|
|
$form['title'][$filter->hfid] = array('#value' => check_plain($filter->title)); |
|
|
$form['hits'][$filter->hfid] = array('#value' => $filter->hits); |
|
|
$form['date'][$filter->hfid] = array('#value' => ($filter->date==0) ? t('Never') : format_date($filter->date, 'small')); |
|
|
$form['weight'][$filter->hfid] = array('#type' => 'weight', '#default_value' => $filter->weight, '#tree' => TRUE); |
|
|
$form['edit'][$filter->hfid] = array('#value' => l(t('edit'), 'admin/content/hidden/filters/edit/'. $filter->hfid)); |
|
|
$form['enable'][$filter->hfid] = array( '#value' => l(t('disable'), 'admin/content/hidden/filters/disable/'. $filter->hfid)); |
|
| 1378 |
} |
} |
| 1379 |
|
|
| 1380 |
$form['submit'] = array('#value' => t('Update weights'), '#type' => 'submit'); |
_hidden_log(HIDDEN_LOG_REASONS, t('Reason enabled.'), 'reason', $rid); |
| 1381 |
return $form; |
return TRUE; |
| 1382 |
} |
} |
| 1383 |
|
|
| 1384 |
/** |
/** |
| 1385 |
* theme hidde_filters_admin_enabled_form(). |
* Disable a reason |
| 1386 |
* |
* |
| 1387 |
* @ingroup themeable |
* @param $rid |
| 1388 |
*/ |
* rid |
| 1389 |
function theme_hidden_filters_admin_enabled_form($form) { |
* @return |
| 1390 |
$rows = array(); |
* bool TRUE on success |
| 1391 |
if (isset($form['title']) && is_array($form['title'])) { |
*/ |
| 1392 |
foreach (element_children($form['title']) as $key) { |
function hidden_reason_disable($rid) { |
| 1393 |
$row = array(); |
if (! db_query('UPDATE {hidden_reasons} SET enabled=0 WHERE rid=%d', $rid)) { |
| 1394 |
$row[] = drupal_render($form['title'][$key]); |
_hidden_log(HIDDEN_LOG_DEBUG, t('hidden_reason_disable failed'), $rid); |
| 1395 |
$row[] = drupal_render($form['hits'][$key]); |
return FALSE; |
|
$row[] = drupal_render($form['date'][$key]); |
|
|
$row[] = drupal_render($form['weight'][$key]); |
|
|
$row[] = drupal_render($form['edit'][$key]); |
|
|
$row[] = drupal_render($form['enable'][$key]); |
|
|
$rows[] = $row; |
|
|
} |
|
|
} |
|
|
else { |
|
|
$rows[] = array(array('data' => t('No filters enabled'), 'colspan' => '6')); |
|
| 1396 |
} |
} |
| 1397 |
|
|
| 1398 |
$output = theme('table', $form['header']['#value'], $rows); |
_hidden_log(HIDDEN_LOG_REASONS, t('Reason disabled.'), 'reason', $rid); |
| 1399 |
$output .= drupal_render($form); |
return TRUE; |
|
return $output; |
|
| 1400 |
} |
} |
| 1401 |
|
|
| 1402 |
/** |
/** |
| 1403 |
* Implementation of form API hook; submit hidden_filters_admin_form(). |
* Creates an (options) array of active hidden reasons. |
| 1404 |
|
* |
| 1405 |
|
* For hide form an options list of reason titles mapped to reason id. |
| 1406 |
|
* |
| 1407 |
|
* @return |
| 1408 |
|
* array options array |
| 1409 |
*/ |
*/ |
| 1410 |
function hidden_filters_admin_enabled_form_submit($form, &$form_state) { |
function _hidden_reasons_get_options() { |
| 1411 |
foreach ($form_state['values']['weight'] as $hfid => $weight) { |
$reasons = hidden_reason_get_all(); |
| 1412 |
if (! db_query('UPDATE {hidden_filters} SET weight=%d WHERE hfid=%d', $weight, $hfid)) { |
$options = array(0 => t('No standard reason')); |
| 1413 |
$error = TRUE; |
foreach ($reasons as $rid => $reason ) { |
| 1414 |
} |
$options[$rid] = $reason->title; |
| 1415 |
} |
} |
| 1416 |
|
|
| 1417 |
if ($error) { |
return $options; |
|
drupal_set_message(t('Problem updating weights'), 'error'); |
|
|
} |
|
|
else { |
|
|
drupal_set_message(t('Weights updated')); |
|
|
} |
|
| 1418 |
} |
} |
| 1419 |
|
|
| 1420 |
|
/******************************************************************** |
| 1421 |
|
* Filters |
| 1422 |
|
*/ |
| 1423 |
|
|
| 1424 |
/** |
/** |
| 1425 |
* Send content to be checked against filters. |
* Send content to be checked against filters. |
| 1426 |
* |
* |
| 1437 |
* bool TRUE on matching a filter |
* bool TRUE on matching a filter |
| 1438 |
*/ |
*/ |
| 1439 |
function hidden_filter_content($content, $type, $id) { |
function hidden_filter_content($content, $type, $id) { |
| 1440 |
// TODO hidden filter switch... |
// @todo hidden content filter off-switch... |
| 1441 |
|
|
| 1442 |
if (($filter = _hidden_filter_content_test($content)) == FALSE) { |
if (($filter = _hidden_filter_content_test($content)) == FALSE) { |
| 1443 |
_hidden_log(HIDDEN_LOG_DEBUG_FILTER, "hidden_filter_content() FALSE on $type $id"); |
_hidden_log(HIDDEN_LOG_DEBUG_FILTER, "hidden_filter_content() FALSE on $type $id"); |
| 1444 |
return FALSE; |
return FALSE; |
| 1445 |
} |
} |
| 1446 |
|
|
| 1447 |
_hidden_log(HIDDEN_LOG_DEBUG_FILTER, "hidden_filter_content() true on nid $type $id"); |
_hidden_log(HIDDEN_LOG_DEBUG_FILTER, "hidden_filter_content() TRUE on nid $type $id"); |
| 1448 |
$reason = _hidden_filter_reason_get($filter->hfid); |
if ($filter->delay != 0) { |
| 1449 |
if ($reason->delay != 0) { |
$filter->delay += time(); |
|
$reason->delay += time(); |
|
| 1450 |
} |
} |
| 1451 |
else { |
else { |
| 1452 |
drupal_set_message(t('Your post has been marked as possibly contraviening the editorial policy, and will be hidden until a moderator can review it.')); |
drupal_set_message(t('Your post has been marked as possibly contravening the editorial policy, and will be hidden until a moderator can review it.')); |
| 1453 |
} |
} |
| 1454 |
hidden_hidden_hide($type, $id, $reason->uid, $reason->rid, $reason->publicnote, $reason->privatenote, $reason->delay); |
hidden_hidden_hide($type, $id, $filter->uid, $filter->rid, $filter->publicnote, $filter->privatenote, $filter->delay); |
| 1455 |
return TRUE; |
return TRUE; |
| 1456 |
} |
} |
| 1457 |
|
|
| 1458 |
/** |
/** |
| 1459 |
* Test content against filters. |
* Returns hidden filter |
| 1460 |
* |
* |
| 1461 |
* @param $content |
* @param $hfid |
| 1462 |
* string content to be tested. |
* int hidden filter id. |
| 1463 |
* @return |
* @return |
| 1464 |
* mixed FALSE if it does not match filter object if matches |
* object hidden filter. |
| 1465 |
*/ |
*/ |
| 1466 |
function _hidden_filter_content_test($content) { |
function hidden_filter_load($hfid) { |
| 1467 |
$query = 'SELECT hfid, filter, type, hits FROM {hidden_filters} WHERE enabled=1 ORDER BY weight DESC'; |
$query = 'SELECT f.hfid AS hfid, f.filter AS filter, f.type AS type, f.hits AS hits, f.lasthit AS lasthit, f.enabled AS enabled, f.weight AS weight,' |
| 1468 |
$result = db_query($query); |
.' r.title AS title, r.rid AS rid, r.publicnote AS publicnote, r.privatenote AS privatenote, r.uid AS uid, r.delay AS delay' |
| 1469 |
while ($filter = db_fetch_object($result)) { |
.' FROM {hidden_filters} AS f' |
| 1470 |
if ($filter->type == HIDDEN_FILTER_PLAIN) { |
.' INNER JOIN {hidden_filter_reasons} AS r ON f.hfid = r.hfid' |
| 1471 |
if (strpos($content, $filter->filter)!==FALSE) { |
.' WHERE f.hfid = %d'; |
| 1472 |
$query = 'UPDATE {hidden_filters} SET hits = %d, lasthit = %d WHERE hfid = %d'; |
$result = db_query($query, $hfid); |
| 1473 |
db_query($query, ++$filter->hits, time(), $filter->hfid); |
return db_fetch_object($result); |
| 1474 |
return $filter; |
} |
| 1475 |
} |
|
| 1476 |
|
/** |
| 1477 |
|
* Deletes hidden filter |
| 1478 |
|
* |
| 1479 |
|
* @param $hfid |
| 1480 |
|
* int hidden filter id. |
| 1481 |
|
* @return |
| 1482 |
|
* bool false on fail, true on success. |
| 1483 |
|
*/ |
| 1484 |
|
function hidden_filter_delete($hfid) { |
| 1485 |
|
$filter_result = db_query('DELETE FROM {hidden_filters} WHERE hfid = %d', $hfid); |
| 1486 |
|
$reason_result = db_query('DELETE FROM {hidden_filter_reasons} WHERE hfid = %d', $hfid); |
| 1487 |
|
return ((bool) $filter_result & (bool) $reason_result) ? TRUE : FALSE; |
| 1488 |
|
} |
| 1489 |
|
|
| 1490 |
|
/** |
| 1491 |
|
* Create or update a hidden filter. |
| 1492 |
|
* |
| 1493 |
|
* $filter is passed by reference and will be filled as per drupal_write_record(). |
| 1494 |
|
* |
| 1495 |
|
* @param $filter |
| 1496 |
|
* object filter to write (set hfid = 0 for new record). |
| 1497 |
|
* @return |
| 1498 |
|
* FALSE, SAVED_NEW, SAVED_UPDATED. |
| 1499 |
|
*/ |
| 1500 |
|
function hidden_filter_save(&$filter) { |
| 1501 |
|
$filter_fields = drupal_schema_fields_sql('hidden_filters'); |
| 1502 |
|
$reason_fields = drupal_schema_fields_sql('hidden_filter_reasons'); |
| 1503 |
|
$save_fields = array_keys(get_object_vars($filter)); |
| 1504 |
|
$filter_result = $reason_result = FALSE; |
| 1505 |
|
|
| 1506 |
|
if (isset($filter_fields['hfid'])) { |
| 1507 |
|
unset($filter_fields['hfid']); |
| 1508 |
|
} |
| 1509 |
|
|
| 1510 |
|
if (isset($filter->hfid) && $filter->hfid > 0) { |
| 1511 |
|
// an update |
| 1512 |
|
$update = array('hfid'); |
| 1513 |
|
} |
| 1514 |
|
else { |
| 1515 |
|
// an a new record |
| 1516 |
|
$update = array(); |
| 1517 |
|
if (! count(array_intersect($reason_fields, $save_fields))) { |
| 1518 |
|
// no accompanying reason information, need at least one field |
| 1519 |
|
return FALSE; |
| 1520 |
} |
} |
| 1521 |
else { |
} |
| 1522 |
if (preg_match($filter->filter, $content)==1) { |
|
| 1523 |
$query = 'UPDATE {hidden_filters} SET hits = %d, lasthit = %d WHERE hfid = %d'; |
if (count(array_intersect($filter_fields, $save_fields))) { |
| 1524 |
db_query($query, ++$filter->hits, time(), $filter->hfid); |
$filter_result = drupal_write_record('hidden_filters', $filter, $update); |
| 1525 |
return $filter; |
if (! $filter_result) { |
| 1526 |
|
return FALSE; |
| 1527 |
|
} |
| 1528 |
|
} |
| 1529 |
|
|
| 1530 |
|
if (count(array_intersect($reason_fields, $save_fields))) { |
| 1531 |
|
$reason_result = drupal_write_record('hidden_filter_reasons', $filter, $update); |
| 1532 |
|
if (! $reason_result) { |
| 1533 |
|
if ($filter_result == SAVED_NEW) { |
| 1534 |
|
// prevent a filter without information being stored |
| 1535 |
|
hidden_filter_delete($filter->hfid); |
| 1536 |
} |
} |
| 1537 |
|
return FALSE; |
| 1538 |
} |
} |
| 1539 |
} |
} |
| 1540 |
return FALSE; |
|
| 1541 |
|
return $filter_result ? $filter_result : $reason_result; |
| 1542 |
} |
} |
| 1543 |
|
|
| 1544 |
/** |
/** |
| 1545 |
* Returns filter reasons. |
* Enable or disable a filter. |
|
* |
|
|
* ->title, ->uid, ->rid, ->publicnote, ->privatenote, ->delay. |
|
| 1546 |
* |
* |
| 1547 |
* @param $hfid |
* @param $hfid |
| 1548 |
* int hidden filter id. |
* int hfid. |
| 1549 |
* @return |
* @param $enabled |
| 1550 |
* object hidden filter reasons. |
* bool true to enable, false to disable. |
| 1551 |
|
* @return |
| 1552 |
|
* SAVED_UPDATED or FALSE |
| 1553 |
|
*/ |
| 1554 |
|
function hidden_filter_enable($hfid, $enabled = TRUE) { |
| 1555 |
|
$filter = new StdClass; |
| 1556 |
|
if ($hfid < 1) { |
| 1557 |
|
return FALSE; |
| 1558 |
|
} |
| 1559 |
|
$filter->hfid = $hfid; |
| 1560 |
|
$filter->enabled = $enabled; |
| 1561 |
|
$result = hidden_filter_save($filter); |
| 1562 |
|
if ($result == SAVED_NEW) { |
| 1563 |
|
// we could have avoided this by loading and checking, |
| 1564 |
|
// but shouldn't really ever be called with invalid hfid. |
| 1565 |
|
hidden_filter_delete($filter->hfid); |
| 1566 |
|
return FALSE; |
| 1567 |
|
} |
| 1568 |
|
return $result; |
| 1569 |
|
} |
| 1570 |
|
|
| 1571 |
|
|
| 1572 |
|
/** |
| 1573 |
|
* Returns all hidden filters pager query. |
| 1574 |
|
* |
| 1575 |
|
* Just a start at moving the queries out. |
| 1576 |
|
* |
| 1577 |
|
* @param $pager |
| 1578 |
|
* int pager_query pager id (optional). |
| 1579 |
|
* @param $enabled |
| 1580 |
|
* int 0 only disabled filters, 1 only enabled filters (optional). |
| 1581 |
*/ |
*/ |
| 1582 |
function _hidden_filter_reason_get($hfid) { |
function hidden_filter_all_get_pager($pager = 0, $enabled = -1) { |
| 1583 |
$query = 'SELECT title, uid, rid, publicnote, privatenote, delay FROM {hidden_filter_reasons} WHERE hfid = %d'; |
$query = 'SELECT f.hfid as hfid, f.hits as hits, f.lasthit as date, f.weight as weight,' |
| 1584 |
$result = db_query($query, $hfid); |
.' r.title' |
| 1585 |
return db_fetch_object($result); |
.' FROM {hidden_filters} AS f' |
| 1586 |
|
.' INNER JOIN {hidden_filter_reasons} AS r ON f.hfid = r.hfid'; |
| 1587 |
|
if ($enabled == 0) { |
| 1588 |
|
$query .= ' WHERE f.enabled=0'; |
| 1589 |
|
} |
| 1590 |
|
elseif ($enabled == 1) { |
| 1591 |
|
$query .= ' WHERE f.enabled=1'; |
| 1592 |
|
} |
| 1593 |
|
$query .= ' ORDER BY f.weight DESC'; |
| 1594 |
|
$result = pager_query($query, 50, $pager); |
| 1595 |
|
return $result; |
| 1596 |
|
} |
| 1597 |
|
|
| 1598 |
|
/** |
| 1599 |
|
* Test a preg filter. |
| 1600 |
|
* |
| 1601 |
|
* @param $preg |
| 1602 |
|
* string expression to test. |
| 1603 |
|
* @param $msg |
| 1604 |
|
* string to place error message into (optional). |
| 1605 |
|
* @param |
| 1606 |
|
* bool FALSE on fail |
| 1607 |
|
*/ |
| 1608 |
|
function _hidden_filter_preg_test($preg, &$msg = '') { |
| 1609 |
|
$success = TRUE; |
| 1610 |
|
$msg = ''; |
| 1611 |
|
$result = @preg_match($preg, 'foobar'); |
| 1612 |
|
|
| 1613 |
|
if (version_compare(PHP_VERSION, '5.2.0') === 1) { |
| 1614 |
|
switch (preg_last_error()) { |
| 1615 |
|
case PREG_NO_ERROR: |
| 1616 |
|
break; |
| 1617 |
|
case PREG_INTERNAL_ERROR: |
| 1618 |
|
$error = t('There was an internal PCRE error.'); |
| 1619 |
|
$success = FALSE; |
| 1620 |
|
break; |
| 1621 |
|
case PREG_BACKTRACK_LIMIT_ERROR: |
| 1622 |
|
$error = t('backtrack_limit was exhasted.'); |
| 1623 |
|
$success = FALSE; |
| 1624 |
|
break; |
| 1625 |
|
case PREG_RECURSION_LIMIT_ERROR: |
| 1626 |
|
$error = t('recursion_limit was exhasted.'); |
| 1627 |
|
$success = FALSE; |
| 1628 |
|
break; |
| 1629 |
|
case PREG_BAD_UTF8_ERROR: |
| 1630 |
|
$error = t('malformed UTF-8 data.'); |
| 1631 |
|
$succes = FALSE; |
| 1632 |
|
break; |
| 1633 |
|
} |
| 1634 |
|
if (! $success) { |
| 1635 |
|
$msg = t('There was an error parsing the regular expression: %error', array('%error' => $error)); |
| 1636 |
|
} |
| 1637 |
|
} |
| 1638 |
|
else { |
| 1639 |
|
$msg = t('There was an error parsing the regular expression.'); |
| 1640 |
|
$success = FALSE; |
| 1641 |
|
} |
| 1642 |
|
|
| 1643 |
|
return $success; |
| 1644 |
} |
} |
| 1645 |
|
|
| 1646 |
|
|
| 1647 |
/** |
/** |
| 1648 |
* Returns hidden filter |
* Test content against filters. |
| 1649 |
* |
* |
| 1650 |
* @param $hfid |
* @param $content |
| 1651 |
* int hidden filter id |
* string content to be tested. |
| 1652 |
* @return |
* @return |
| 1653 |
* object hidden filter |
* mixed FALSE if it does not match filter object if matches |
| 1654 |
*/ |
*/ |
| 1655 |
function _hidden_filter_get($hfid) { |
function _hidden_filter_content_test($content) { |
| 1656 |
$query = 'SELECT f.hfid AS hfid, f.filter AS filter, f.type AS type, f.enabled AS enabled, f.weight AS weight,' |
$match = FALSE; |
| 1657 |
.' r.title AS filtertitle, r.rid AS rid, r.publicnote AS publicnote, r.privatenote AS privatenote, r.uid AS uid, r.delay AS delay' |
|
| 1658 |
.' FROM {hidden_filters} AS f' |
$query = 'SELECT hfid, filter, type FROM {hidden_filters} WHERE enabled=1 ORDER BY weight ASC'; |
| 1659 |
.' INNER JOIN {hidden_filter_reasons} AS r ON f.hfid = r.hfid' |
$result = db_query($query); |
| 1660 |
.' WHERE f.hfid = %d'; |
while ($filter = db_fetch_object($result)) { |
| 1661 |
$result = db_query($query, $hfid); |
if ($filter->type == HIDDEN_FILTER_PLAIN && strpos($content, $filter->filter) !== FALSE) { |
| 1662 |
return db_fetch_object($result); |
$match =TRUE; |
| 1663 |
|
} |
| 1664 |
|
elseif ($filter->type == HIDDEN_FILTER_PREG && preg_match($filter->filter, $content) == 1) { |
| 1665 |
|
$match = TRUE; |
| 1666 |
|
} |
| 1667 |
|
|
| 1668 |
|
if ($match) { |
| 1669 |
|
$filter = hidden_filter_load($filter->hfid); |
| 1670 |
|
$filter->hits++; |
| 1671 |
|
$filter->lasthit = time(); |
| 1672 |
|
hidden_filter_save($filter); |
| 1673 |
|
return $filter; |
| 1674 |
|
} |
| 1675 |
|
} |
| 1676 |
|
return FALSE; |
| 1677 |
} |
} |
| 1678 |
|
|
| 1679 |
/******************************************************************** |
/******************************************************************** |