| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | if (basename($_SERVER['PHP_SELF']) == basename( __FILE__ )) { |
|---|
| 4 | |
|---|
| 5 | exit(); |
|---|
| 6 | } |
|---|
| 7 | |
|---|
| 8 | function parse_tags($str) { |
|---|
| 9 | |
|---|
| 10 | $str = preg_replace('/\Ts{2,}/',' ',$str); |
|---|
| 11 | |
|---|
| 12 | preg_match_all('/(\'|")(.*?)(\1)/',$str,$phrases); |
|---|
| 13 | |
|---|
| 14 | $str = preg_replace('/(\'|")(.*?)(\1)/','',$str); |
|---|
| 15 | |
|---|
| 16 | $str = trim($str); |
|---|
| 17 | |
|---|
| 18 | $words = preg_split('/\s+/',$str); |
|---|
| 19 | |
|---|
| 20 | $tags = array_merge($phrases[2],$words); |
|---|
| 21 | |
|---|
| 22 | return $tags; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | function urlify_tag($tag) { |
|---|
| 26 | |
|---|
| 27 | return rawurlencode($tag); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | function get_picture_tags($picture_id) { |
|---|
| 31 | global $config; |
|---|
| 32 | global $TABLE_PREFIX; |
|---|
| 33 | $picture_id = intval($picture_id); |
|---|
| 34 | $picture_tags = array(); |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | $query = 'SELECT `t2p`.`tag_id`, `t`.`urlified`, `t`.`tag` FROM `'.$TABLE_PREFIX.'tag2picture` as `t2p`, `'.$TABLE_PREFIX.'tags` as `t` WHERE `picture_id` = '.$picture_id.' AND `t2p`.`tag_id` = `t`.`id`;'; |
|---|
| 39 | $result = mysql_query($query); |
|---|
| 40 | while($tag_row = mysql_fetch_assoc($result)) { |
|---|
| 41 | $picture_tags[$tag_row['urlified']] = $tag_row['tag_id']; |
|---|
| 42 | }; |
|---|
| 43 | return $picture_tags; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | function delete_picture_tags($picture_id) { |
|---|
| 47 | global $TABLE_PREFIX; |
|---|
| 48 | $picture_id = intval($picture_id); |
|---|
| 49 | $sql = 'DELETE FROM '.$TABLE_PREFIX.'tag2picture WHERE picture_id = '.$picture_id; |
|---|
| 50 | mysql_query($sql); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | function get_tag_by_name($tag) { |
|---|
| 54 | global $TABLE_PREFIX; |
|---|
| 55 | $existing_tag = array(); |
|---|
| 56 | |
|---|
| 57 | $query = 'SELECT `id`, `tag`, `urlified` FROM `'.$TABLE_PREFIX.'tags` WHERE `tag`="'.$tag.'"'; |
|---|
| 58 | $result = run_query($query); |
|---|
| 59 | $row = mysql_fetch_assoc($result); |
|---|
| 60 | |
|---|
| 61 | if (!is_array($row)) { |
|---|
| 62 | return NULL; |
|---|
| 63 | }; |
|---|
| 64 | return array('id' => $row['id'], 'tag' => $row['tag'], 'urlified' => $row['urlified']); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | function get_tag_by_id($tag_id) { |
|---|
| 68 | global $TABLE_PREFIX; |
|---|
| 69 | $existing_tag = array(); |
|---|
| 70 | $tag_id = intval($tag_id); |
|---|
| 71 | |
|---|
| 72 | $query = 'SELECT `id`, `tag`, `urlified` FROM `'.$TABLE_PREFIX.'tags` WHERE `id`='.$tag_id; |
|---|
| 73 | $result = run_query($query); |
|---|
| 74 | $row = mysql_fetch_assoc($result); |
|---|
| 75 | |
|---|
| 76 | if (!is_array($row)) { |
|---|
| 77 | return NULL; |
|---|
| 78 | }; |
|---|
| 79 | return array('id' => $row['tag_id'], 'tag' => $row['tag'], 'urlified' => $row['urlified']); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | function get_popular_tags($limit=NULL) { |
|---|
| 83 | global $TABLE_PREFIX; |
|---|
| 84 | |
|---|
| 85 | $query = 'SELECT `t2p`.`tag_id`, COUNT(`t2p`.`tag_id`) AS `popularity`, `t`.`tag`, `t`.`urlified` FROM `'.$TABLE_PREFIX.'tag2picture` AS `t2p`, `'.$TABLE_PREFIX.'tags` AS `t` WHERE `t`.`id`=`t2p`.`tag_id` GROUP BY `t2p`.`tag_id` ORDER BY `popularity` DESC'; |
|---|
| 86 | if( isset($limit) ) { |
|---|
| 87 | $limit = intval($limit); |
|---|
| 88 | $query .= ' LIMIT '.$limit; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | function insert_tag($tag) { |
|---|
| 94 | global $TABLE_PREFIX; |
|---|
| 95 | $urlified = mysql_real_escape_string(urlify_tag($tag)); |
|---|
| 96 | $sql = 'INSERT INTO '.$TABLE_PREFIX.'tags (`tag`,`tagdate`,`urlified`) |
|---|
| 97 | VALUES ("'.mysql_real_escape_string($tag).'",NOW(),"'.$urlified.'")'; |
|---|
| 98 | if( mysql_query($sql) ) { |
|---|
| 99 | return mysql_insert_id(); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | function add_picture_tags($picture_id,$tags){ |
|---|
| 104 | global $TABLE_PREFIX; |
|---|
| 105 | $tags = parse_tags($tags); |
|---|
| 106 | $picture_id = intval($picture_id); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | $existing_tags = $existing_rels = array(); |
|---|
| 110 | if (sizeof($tags) > 0) { |
|---|
| 111 | $tagsql = join('","',$tags); |
|---|
| 112 | $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tags WHERE `tag` IN ("'.$tagsql.'")'; |
|---|
| 113 | $result = mysql_query($sql); |
|---|
| 114 | while($tag_row = mysql_fetch_assoc($result)) { |
|---|
| 115 | $existing_tags[$tag_row['tag']] = $tag_row['id']; |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tag2picture WHERE `picture_id` ="'.$picture_id.'"'; |
|---|
| 119 | $result = mysql_query($sql); |
|---|
| 120 | while($tag_row = mysql_fetch_assoc($result)) { |
|---|
| 121 | $existing_rels[$tag_row['tag_id']] = $tag_row['picture_id']; |
|---|
| 122 | }; |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | $added_tag_ids = array(); |
|---|
| 126 | foreach($tags as $tag) { |
|---|
| 127 | if (!isset($existing_tags[$tag])) { |
|---|
| 128 | |
|---|
| 129 | $existing_tags[$tag] = insert_tag($tag); |
|---|
| 130 | $added_tag_ids[] = $existing_tags[$tag]; |
|---|
| 131 | }; |
|---|
| 132 | |
|---|
| 133 | if (!isset($existing_rels[$existing_tags[$tag]])) { |
|---|
| 134 | |
|---|
| 135 | $sql = 'INSERT INTO '.$TABLE_PREFIX.'tag2picture (`picture_id`,`tag_id`,`tagdate`) |
|---|
| 136 | VALUES ("'.$picture_id.'","'.$existing_tags[$tag].'",NOW())'; |
|---|
| 137 | mysql_query($sql); |
|---|
| 138 | }; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | return $added_tag_ids; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | function delete_tags($tag_ids) { |
|---|
| 145 | global $TABLE_PREFIX; |
|---|
| 146 | $tagsql = join(',',$tag_ids); |
|---|
| 147 | $sql = 'DELETE FROM '.$TABLE_PREFIX.'tag2picture WHERE tag_id IN ('.$tagsql.')'; |
|---|
| 148 | mysql_query($sql); |
|---|
| 149 | $sql = 'DELETE FROM '.$TABLE_PREFIX.'tags WHERE id IN ('.$tagsql.')'; |
|---|
| 150 | mysql_query($sql); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | function remove_picture_tags($picture_id,$tag_ids){ |
|---|
| 154 | |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | function rename_picture_tag($tag_id, $new_name, $change_urlified=true) { |
|---|
| 158 | |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | function purge_unused_tags(){ |
|---|
| 162 | |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | function update_picture_tags($picture_id,$tags) { |
|---|
| 166 | global $config; |
|---|
| 167 | global $TABLE_PREFIX; |
|---|
| 168 | $tags = parse_tags($tags); |
|---|
| 169 | $picture_id = intval($picture_id); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | $existing_tags = $existing_rels = array(); |
|---|
| 173 | if (sizeof($tags) > 0) { |
|---|
| 174 | $tagsql = join('","',$tags); |
|---|
| 175 | $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tags WHERE `tag` IN ("'.$tagsql.'")'; |
|---|
| 176 | $result = mysql_query($sql); |
|---|
| 177 | while($tag_row = mysql_fetch_assoc($result)) { |
|---|
| 178 | $existing_tags[$tag_row['tag']] = $tag_row['id']; |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | $sql = 'SELECT * FROM '.$TABLE_PREFIX.'tag2picture WHERE `picture_id` ="'.$picture_id.'"'; |
|---|
| 182 | $result = mysql_query($sql); |
|---|
| 183 | while($tag_row = mysql_fetch_assoc($result)) { |
|---|
| 184 | $existing_rels[$tag_row['tag_id']] = $tag_row['picture_id']; |
|---|
| 185 | }; |
|---|
| 186 | }; |
|---|
| 187 | |
|---|
| 188 | foreach($tags as $tag) { |
|---|
| 189 | if (!isset($existing_tags[$tag])) { |
|---|
| 190 | |
|---|
| 191 | $path = mysql_real_escape_string(preg_replace("/[^\w|\.|'|\-|\[|\]]/","_",$tag)); |
|---|
| 192 | $sql = 'INSERT INTO '.$TABLE_PREFIX.'tags (`tag`,`tagdate`,`path`) |
|---|
| 193 | VALUES ("'.mysql_real_escape_string($tag).'","'.$path.'",NOW())'; |
|---|
| 194 | print $sql; |
|---|
| 195 | $result = mysql_query($sql); |
|---|
| 196 | $existing_tags[$tag] = mysql_insert_id(); |
|---|
| 197 | }; |
|---|
| 198 | |
|---|
| 199 | if (!isset($existing_rels[$existing_tags[$tag]])) { |
|---|
| 200 | |
|---|
| 201 | $sql = 'INSERT INTO '.$TABLE_PREFIX.'tag2picture (`picture_id`,`tag_id`,`tagdate`) |
|---|
| 202 | VALUES ("'.$picture_id.'","'.$existing_tags[$tag].'",NOW())'; |
|---|
| 203 | mysql_query($sql); |
|---|
| 204 | }; |
|---|
| 205 | }; |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | foreach($existing_rels as $tag_id => $pic_id) { |
|---|
| 209 | if (!in_array($tag_id,$existing_tags)) { |
|---|
| 210 | $sql = "DELETE FROM `".$TABLE_PREFIX."tag2picture` |
|---|
| 211 | WHERE `picture_id` = '$picture_id' AND `tag_id` = '$tag_id'"; |
|---|
| 212 | mysql_query($sql); |
|---|
| 213 | }; |
|---|
| 214 | |
|---|
| 215 | }; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | ?> |
|---|