| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
if (__FILE__ == basename($_SERVER['SCRIPT_NAME'])) { die(); } |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
class phptagengine { |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
var $db; |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
var $db_col_escape_char; |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
var $table_tags; |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
var $table_tag_names; |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
var $table_users; |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
var $table_users_name; |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
var $base_url; |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
var $ajax_handler; |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
var $language; |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
var $charset; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
var $strings; |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
var $default_type; |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
var $default_user; |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
var $tag_browse_url; |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
var $show_remove_links; |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
var $edit_button_display; |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
var $edit_button_image_url; |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
var $delete_button_display; |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
var $delete_button_image_url; |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
var $item_tags_cache; |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
var $yac; |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
var $yac_files; |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
var $version; |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
function phptagengine() { |
|---|
| 185 |
$this->base_url = 'http://example.com/'; |
|---|
| 186 |
$this->ajax_handler = 'http://example.com/ajax.php'; |
|---|
| 187 |
$this->language = 'english'; |
|---|
| 188 |
$this->charset = 'UTF-8'; |
|---|
| 189 |
$this->db_type = 'mysql'; |
|---|
| 190 |
$this->strings = array(); |
|---|
| 191 |
$this->tag_browse_url = 'http://example.com/index.php?view=tags&tag=<tag>&type=<type>'; |
|---|
| 192 |
$this->default_type = ''; |
|---|
| 193 |
$this->default_user = 1; |
|---|
| 194 |
$this->edit_button_display = 'text'; |
|---|
| 195 |
$this->delete_button_display = 'text'; |
|---|
| 196 |
$this->yac = true; |
|---|
| 197 |
$this->yac_files = array( |
|---|
| 198 |
'yahoo.js' |
|---|
| 199 |
,'dom.js' |
|---|
| 200 |
,'event.js' |
|---|
| 201 |
,'autocomplete.js' |
|---|
| 202 |
); |
|---|
| 203 |
$this->version = '1.0'; |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
function set_db_col_escape_char() { |
|---|
| 210 |
switch ($this->db->databaseType) { |
|---|
| 211 |
case 'mysql': |
|---|
| 212 |
$this->db_col_escape_char = '`'; |
|---|
| 213 |
break; |
|---|
| 214 |
case 'postgres7': |
|---|
| 215 |
case 'mssql': |
|---|
| 216 |
$this->db_col_escape_char = '"'; |
|---|
| 217 |
break; |
|---|
| 218 |
} |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
function default_value($prop, $value) { |
|---|
| 228 |
if ($value == null && in_array($prop, array('type', 'user'))) { |
|---|
| 229 |
eval('$value = $this->default_'.$prop.';'); |
|---|
| 230 |
} |
|---|
| 231 |
return $value; |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
function normalize($value, $type = 'tag') { |
|---|
| 241 |
switch ($type) { |
|---|
| 242 |
case 'tag': |
|---|
| 243 |
$value = preg_replace('|[^a-z0-9_.\-@#$%*!&]|i', '', strtolower($value)); |
|---|
| 244 |
break; |
|---|
| 245 |
} |
|---|
| 246 |
return $value; |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
function tag_exists($tag) { |
|---|
| 258 |
if ($this->get_tag_id($tag) != false) { |
|---|
| 259 |
return true; |
|---|
| 260 |
} |
|---|
| 261 |
else { |
|---|
| 262 |
return false; |
|---|
| 263 |
} |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
function create_tag($tag = null) { |
|---|
| 277 |
if ($tag == null) { |
|---|
| 278 |
return false; |
|---|
| 279 |
} |
|---|
| 280 |
$tag = $this->normalize($tag); |
|---|
| 281 |
if (strlen($tag) < 1) { |
|---|
| 282 |
return false; |
|---|
| 283 |
} |
|---|
| 284 |
$test = $this->get_tag_id($tag); |
|---|
| 285 |
if ($test != false) { |
|---|
| 286 |
return $test; |
|---|
| 287 |
} |
|---|
| 288 |
$result = $this->db->Execute(" |
|---|
| 289 |
INSERT |
|---|
| 290 |
INTO $this->table_tag_names |
|---|
| 291 |
( ".$this->db_col_escape_char."name".$this->db_col_escape_char." |
|---|
| 292 |
) |
|---|
| 293 |
VALUES |
|---|
| 294 |
( ".$this->db->qstr($tag)." |
|---|
| 295 |
) |
|---|
| 296 |
") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 297 |
|
|---|
| 298 |
if ($result) { |
|---|
| 299 |
$id = false; |
|---|
| 300 |
if (strstr($this->db->databaseType, 'postgres')) { |
|---|
| 301 |
$id = $this->db->GetOne(" |
|---|
| 302 |
SELECT CURRVAL('".$this->table_tag_names."_id_seq') as id |
|---|
| 303 |
") or die($this->db->ErrorMsg()); |
|---|
| 304 |
} |
|---|
| 305 |
else { |
|---|
| 306 |
$id = $this->db->Insert_ID(); |
|---|
| 307 |
} |
|---|
| 308 |
return $id; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
return false; |
|---|
| 312 |
} |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
function add_tag($user = null, $item = null, $tag = null, $type = null) { |
|---|
| 327 |
if ($item == null || $tag == null) { |
|---|
| 328 |
return false; |
|---|
| 329 |
} |
|---|
| 330 |
$type = $this->default_value('type', $type); |
|---|
| 331 |
$user = $this->default_value('user', $user); |
|---|
| 332 |
$tag_id = $this->create_tag($tag); |
|---|
| 333 |
if ($tag_id == false) { |
|---|
| 334 |
return false; |
|---|
| 335 |
} |
|---|
| 336 |
if ($this->item_tag_exists($user, $item, $tag, $type)) { |
|---|
| 337 |
return true; |
|---|
| 338 |
} |
|---|
| 339 |
else { |
|---|
| 340 |
$this->set_db_col_escape_char(); |
|---|
| 341 |
$result = $this->db->Execute(" |
|---|
| 342 |
INSERT |
|---|
| 343 |
INTO $this->table_tags |
|---|
| 344 |
( ".$this->db_col_escape_char."user".$this->db_col_escape_char." |
|---|
| 345 |
, ".$this->db_col_escape_char."item".$this->db_col_escape_char." |
|---|
| 346 |
, ".$this->db_col_escape_char."type".$this->db_col_escape_char." |
|---|
| 347 |
, ".$this->db_col_escape_char."tag".$this->db_col_escape_char." |
|---|
| 348 |
, ".$this->db_col_escape_char."date".$this->db_col_escape_char." |
|---|
| 349 |
) |
|---|
| 350 |
VALUES |
|---|
| 351 |
( ".$this->db->qstr($user)." |
|---|
| 352 |
, ".$this->db->qstr($item)." |
|---|
| 353 |
, ".$this->db->qstr($type)." |
|---|
| 354 |
, ".$this->db->qstr($tag_id)." |
|---|
| 355 |
, ".$this->db->DBDate(date("Y-m-d H:i:s"))." |
|---|
| 356 |
) |
|---|
| 357 |
") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 358 |
if ($result) { |
|---|
| 359 |
return true; |
|---|
| 360 |
} |
|---|
| 361 |
} |
|---|
| 362 |
return false; |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
function save_tags($item, $tags, $type = null, $user = null) { |
|---|
| 376 |
$type = $this->default_value('type', $type); |
|---|
| 377 |
$user = $this->default_value('user', $user); |
|---|
| 378 |
$old_tags = $this->get_tags($item, $type, $user); |
|---|
| 379 |
$new_tags = array(); |
|---|
| 380 |
$success = 'y'; |
|---|
| 381 |
if ($tags != null && $tags != '') { |
|---|
| 382 |
$tags = explode(' ', $tags); |
|---|
| 383 |
$tags = array_unique($tags); |
|---|
| 384 |
natcasesort($tags); |
|---|
| 385 |
reset($tags); |
|---|
| 386 |
if (count($tags) > 0) { |
|---|
| 387 |
foreach ($tags as $tag) { |
|---|
| 388 |
if ($tag != '') { |
|---|
| 389 |
if ($this->add_tag($user, $item, $tag, $type)) { |
|---|
| 390 |
$new_tags[] = $this->normalize($tag); |
|---|
| 391 |
} |
|---|
| 392 |
else { |
|---|
| 393 |
$success = 'n'; |
|---|
| 394 |
} |
|---|
| 395 |
} |
|---|
| 396 |
} |
|---|
| 397 |
if (count($new_tags) > 0) { |
|---|
| 398 |
$tags = implode(' ', $new_tags); |
|---|
| 399 |
} |
|---|
| 400 |
} |
|---|
| 401 |
} |
|---|
| 402 |
if (count($old_tags) > 0) { |
|---|
| 403 |
foreach ($old_tags as $id => $tag) { |
|---|
| 404 |
if (!in_array($tag, $new_tags)) { |
|---|
| 405 |
if (!$this->remove_tag_by_id(str_replace('id_', '', $id))) { |
|---|
| 406 |
$success = 'n'; |
|---|
| 407 |
} |
|---|
| 408 |
} |
|---|
| 409 |
} |
|---|
| 410 |
} |
|---|
| 411 |
$result = array( |
|---|
| 412 |
'success' => $success |
|---|
| 413 |
, 'tags' => $new_tags |
|---|
| 414 |
); |
|---|
| 415 |
return $result; |
|---|
| 416 |
} |
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
function remove_tag($item = null, $tag = null, $user = null, $type = null) { |
|---|
| 432 |
$id = $this->get_item_tag_id($user, $item, $tag, $type); |
|---|
| 433 |
return $this->remove_tag_by_id($id); |
|---|
| 434 |
} |
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
function remove_tag_by_id($id) { |
|---|
| 443 |
if ($id == null) { |
|---|
| 444 |
return false; |
|---|
| 445 |
} |
|---|
| 446 |
$result = $this->db->Execute(" |
|---|
| 447 |
DELETE |
|---|
| 448 |
FROM $this->table_tags |
|---|
| 449 |
WHERE id = ".$this->db->qstr($id)." |
|---|
| 450 |
") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 451 |
if ($result) { |
|---|
| 452 |
return true; |
|---|
| 453 |
} |
|---|
| 454 |
return false; |
|---|
| 455 |
} |
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
function get_tags($item = null, $type = null, $user = null, $use_cache = false) { |
|---|
| 467 |
if ($item == null) { |
|---|
| 468 |
return false; |
|---|
| 469 |
} |
|---|
| 470 |
$tags = array(); |
|---|
| 471 |
if ($use_cache) { |
|---|
| 472 |
if (isset($this->item_tags_cache['item_'.$item])) { |
|---|
| 473 |
$tags = $this->item_tags_cache['item_'.$item]; |
|---|
| 474 |
} |
|---|
| 475 |
return $tags; |
|---|
| 476 |
} |
|---|
| 477 |
$where = ''; |
|---|
| 478 |
if ($user != null) { |
|---|
| 479 |
$where .= ' AND t.user = '.$this->db->qstr($user); |
|---|
| 480 |
} |
|---|
| 481 |
if ($item != null) { |
|---|
| 482 |
$where .= ' AND t.item = '.$this->db->qstr($item); |
|---|
| 483 |
} |
|---|
| 484 |
if ($type != null) { |
|---|
| 485 |
$where .= ' AND t.type = '.$this->db->qstr($type); |
|---|
| 486 |
} |
|---|
| 487 |
if ($where == '') { |
|---|
| 488 |
return false; |
|---|
| 489 |
} |
|---|
| 490 |
$result = $this->db->Execute(" |
|---|
| 491 |
SELECT t.id AS ID |
|---|
| 492 |
, tn.name AS NAME |
|---|
| 493 |
FROM $this->table_tags t |
|---|
| 494 |
JOIN $this->table_tag_names tn |
|---|
| 495 |
ON t.tag = tn.id |
|---|
| 496 |
WHERE 1 = 1 |
|---|
| 497 |
$where |
|---|
| 498 |
ORDER BY tn.name |
|---|
| 499 |
") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 500 |
if ($result && $result->RowCount() > 0) { |
|---|
| 501 |
while ($data = $result->FetchNextObject()) { |
|---|
| 502 |
$tags['id_'.$data->ID] = $data->NAME; |
|---|
| 503 |
} |
|---|
| 504 |
} |
|---|
| 505 |
return $tags; |
|---|
| 506 |
} |
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
function delete_tag($tag = null) { |
|---|
| 515 |
if (is_null($tag) || strstr($tag, ' ')) { |
|---|
| 516 |
return false; |
|---|
| 517 |
} |
|---|
| 518 |
$tag_id = $this->get_tag_id($tag); |
|---|
| 519 |
if ($tag_id == false) { |
|---|
| 520 |
return false; |
|---|
| 521 |
} |
|---|
| 522 |
$result = $this->db->Execute(" |
|---|
| 523 |
DELETE |
|---|
| 524 |
FROM $this->table_tags |
|---|
| 525 |
WHERE tag = '$tag_id' |
|---|
| 526 |
") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 527 |
if (!$result) { |
|---|
| 528 |
return false; |
|---|
| 529 |
} |
|---|
| 530 |
$result = $this->db->Execute(" |
|---|
| 531 |
DELETE |
|---|
| 532 |
FROM $this->table_tag_names |
|---|
| 533 |
WHERE id = '$tag_id' |
|---|
| 534 |
") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 535 |
if (!$result) { |
|---|
| 536 |
return false; |
|---|
| 537 |
} |
|---|
| 538 |
return true; |
|---|
| 539 |
} |
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
function edit_tag($old_tag = null, $new_tag = null) { |
|---|
| 549 |
if (is_null($old_tag) || is_null($new_tag) || strstr($new_tag, ' ')) { |
|---|
| 550 |
return false; |
|---|
| 551 |
} |
|---|
| 552 |
if ($old_tag == $new_tag |
|---|