PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/movil/libs/link.php

https://bitbucket.org/jonarano/joneame
PHP | 1182 lines | 958 code | 168 blank | 56 comment | 289 complexity | 0d4014c4627b1865062835a7f6425be5 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?
  2. // The source code packaged with this file is Free Software, Copyright (C) 2005 by
  3. // Ricardo Galli <gallir at uib dot es>.
  4. // It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
  5. // You can get copies of the licenses here:
  6. // http://www.affero.org/oagpl.html
  7. // AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
  8. require_once(mnminclude.'log.php');
  9. require_once(mnminclude.'favorites.php');
  10. class Link {
  11. var $id = 0;
  12. var $author = -1;
  13. var $blog = 0;
  14. var $username = false;
  15. var $randkey = 0;
  16. var $karma = 0;
  17. var $valid = false;
  18. var $date = false;
  19. var $sent_date = 0;
  20. var $sent = 0;
  21. var $modified = 0;
  22. var $url = false;
  23. var $url_title = '';
  24. var $encoding = false;
  25. var $status = 'discard';
  26. var $type = '';
  27. var $category = 0;
  28. var $votes = 0;
  29. var $anonymous = 0;
  30. var $votes_avg = 0;
  31. var $negatives = 0;
  32. var $title = '';
  33. var $tags = '';
  34. var $uri = '';
  35. var $content = '';
  36. var $content_type = '';
  37. var $ip = '';
  38. var $html = false;
  39. var $read = false;
  40. var $voted = false;
  41. var $banned = false;
  42. var $thumb_status = 'unknown';
  43. var $aleatorios_positivos = 0;
  44. var $aleatorios_negativos = 0;
  45. var $clicks = 0;
  46. var $visits = 0;
  47. // sql fields to build an object from mysql
  48. //const SQL = " link_id as id, link_author as author, link_blog as blog, link_status as status, link_votes as votes, link_negatives as negatives, link_anonymous as anonymous, link_votes_avg as votes_avg, link_aleatorios_positivos as aleatorios_positivos, link_aleatorios_negativos as aleatorios_negativos, link_comments as comments, link_karma as karma, link_randkey as randkey, link_category as category, link_url as url, link_uri as uri, link_url_title as title, link_title as title, link_tags as tags, link_content as content, UNIX_TIMESTAMP(link_date) as date, UNIX_TIMESTAMP(link_sent_date) as sent_date, link_sent as sent, UNIX_TIMESTAMP(link_modified) as modified, link_content_type as content_type, link_ip as ip, link_thumb_status as thumb_status, link_thumb_x as thumb_x, link_thumb_y as thumb_y, link_thumb as thumb, link_comentarios_permitidos as comentarios_permitidos, link_votos_permitidos as votos_permitidos, user_login as username, user_email as email, user_avatar as avatar, user_karma as user_karma, user_level as user_level, user_adcode, cat.category_name as category_name, cat.category_uri as category_uri, meta.category_id as meta_id, meta.category_name as meta_name, favorite_link_id as favorite, clicks.counter as clicks, visits.counter as visits FROM links LEFT JOIN favorites ON (@user_id > 0 and favorite_user_id = @user_id and favorite_type = 'link' and favorite_link_id = links.link_id) LEFT JOIN categories as cat on (cat.category_id = links.link_category) LEFT JOIN categories as meta on (meta.category_id = cat.category_parent) LEFT JOIN link_clicks as clicks on (clicks.id = links.link_id) LEFT JOIN link_visits as visits on (visits.id = links.link_id), users ";
  49. // sql fields to build an object from mysql
  50. const SQL = " link_id as id, link_author as author, link_blog as blog, link_status as status, link_votes as votes, link_negatives as negatives, link_anonymous as anonymous, link_votes_avg as votes_avg, link_aleatorios_positivos as aleatorios_positivos, link_aleatorios_negativos as aleatorios_negativos, link_comments as comments, link_karma as karma, link_randkey as randkey, link_category as category, link_url as url, link_uri as uri, link_url_title as title, link_title as title, link_tags as tags, link_content as content, UNIX_TIMESTAMP(link_date) as date, UNIX_TIMESTAMP(link_sent_date) as sent_date, link_sent as sent, UNIX_TIMESTAMP(link_modified) as modified, link_content_type as content_type, link_ip as ip, link_thumb_status as thumb_status, link_thumb_x as thumb_x, link_thumb_y as thumb_y, link_thumb as thumb, link_comentarios_permitidos as comentarios_permitidos, link_votos_permitidos as votos_permitidos, user_login as username, user_email as email, user_avatar as avatar, user_karma as user_karma, user_level , cat.category_name as category_name, cat.category_uri as category_uri, meta.category_id as meta_id, meta.category_name as meta_name, favorite_link_id as favorite, clicks.counter as clicks, visits.counter as visits, votes.vote_value as voted FROM links
  51. INNER JOIN users on (user_id = link_author)
  52. LEFT JOIN (categories as cat, categories as meta) on (cat.category_id = links.link_category AND meta.category_id = cat.category_parent)
  53. LEFT JOIN votes ON (link_date > @enabled_votes and vote_type='links' and vote_link_id = links.link_id and vote_user_id = @user_id and ( @user_id > 0 OR vote_ip_int = @ip_int ) )
  54. LEFT JOIN favorites ON (@user_id > 0 and favorite_user_id = @user_id and favorite_type = 'link' and favorite_link_id = links.link_id)
  55. LEFT JOIN link_clicks as clicks on (clicks.id = links.link_id)
  56. LEFT JOIN link_visits as visits on (visits.id = links.link_id) ";
  57. static function from_db($id) {
  58. global $db;
  59. if (is_numeric($id) && $id > 0) $selector = " link_id = $id ";
  60. else $selector = " link_uri = '$id' ";
  61. if(($object = $db->get_object("SELECT".Link::SQL." WHERE $selector", 'Link'))) {
  62. $object->read = true;
  63. return $object;
  64. }
  65. return false;
  66. }
  67. function json_votes_info($value=false) {
  68. $dict = array();
  69. $dict['id'] = $this->id;
  70. if ($value)
  71. $dict['value'] = $value;
  72. $dict['votes'] = $this->votes;
  73. $dict['aleatorios_positivos'] = $this->aleatorios_positivos;
  74. $dict['anonymous'] = $this->anonymous;
  75. $dict['negatives'] = $this->negatives;
  76. $dict['karma'] = intval($this->karma);
  77. $dict['aleatorio_valor'] = 'no';
  78. return json_encode($dict);
  79. }
  80. function json_votes_info_aleatorio($value=false) {
  81. $voto_aleatorio = $this->aleatorio_info();
  82. $dict = array();
  83. $dict['id'] = $this->id;
  84. if ($value)
  85. $dict['value'] = $value;
  86. $dict['votes'] = $this->votes;
  87. $dict['anonymous'] = $this->anonymous;
  88. $dict['aleatorios_positivos'] = $this->aleatorios_positivos;
  89. $dict['aleatorios_negativos'] = $this->aleatorios_negativos;
  90. $dict['negatives'] = $this->negatives;
  91. $dict['karma'] = intval($this->karma);
  92. $dict['aleatorio_valor'] = $voto_aleatorio->valor;
  93. return json_encode($dict);
  94. }
  95. function print_html() {
  96. echo "Valid: " . $this->valid . "<br>\n";
  97. echo "Url: " . $this->url . "<br>\n";
  98. echo "Title: " . $this->url_title . "<br>\n";
  99. echo "encoding: " . $this->encoding . "<br>\n";
  100. }
  101. function check_url($url, $check_local = true, $first_level = false) {
  102. global $globals, $current_user;
  103. if(!preg_match('/^http[s]*:/', $url))
  104. return false;
  105. $url_components = @parse_url($url);
  106. if (!$url_components)
  107. return false;
  108. if (!preg_match('/[a-z]+/', $url_components['host']))
  109. return false;
  110. $quoted_domain = preg_quote(get_server_name());
  111. if($check_local && preg_match("/^$quoted_domain$/", $url_components['host'])) {
  112. $this->ban = array();
  113. $this->ban['comment'] = _('el servidor es local');
  114. syslog(LOG_NOTICE, "Joneame, server name is local name ($current_user->user_login): $url");
  115. return false;
  116. }
  117. require_once(mnminclude.'ban.php');
  118. if(($this->ban = check_ban($url, 'hostname', false, $first_level))) {
  119. syslog(LOG_NOTICE, "Joneame, server name is banned ($current_user->user_login): $url");
  120. $this->banned = true;
  121. return false;
  122. }
  123. return true;
  124. }
  125. function get($url, $maxlen = 150000, $check_local = true) {
  126. global $globals;
  127. $url=trim($url);
  128. $url_components = @parse_url($url);
  129. if(version_compare(phpversion(), '5.0.0') >= 0) {
  130. $opts = array(
  131. 'http' => array('user_agent' => 'Bot de noticias de Joneame (http://joneame.net/)', 'max_redirects' => 7, 'timeout' => 10, 'header' => 'Referer: http://'.get_server_name().$globals['base_url']."\r\n" ),
  132. 'https' => array('user_agent' => 'Bot de noticias de Joneame (http://joneame.net/)', 'max_redirects' => 7, 'timeout' => 10, 'header' => 'Referer: http://'.get_server_name().$globals['base_url']."\r\n" ),
  133. );
  134. $context = stream_context_create($opts);
  135. if(($stream = @fopen($url, 'r', false, $context))) {
  136. $meta_data = stream_get_meta_data($stream);
  137. foreach($meta_data['wrapper_data'] as $response) {
  138. // Check if it has pingbacks
  139. if (preg_match('/^X-Pingback: /i', $response)) {
  140. $answer = split(' ', $response);
  141. if (!empty($answer[1])) {
  142. $this->pingback = 'ping:'.trim($answer[1]);
  143. }
  144. }
  145. /* Were we redirected? */
  146. if (preg_match('/^location: /i', $response)) {
  147. /* update $url with where we were redirected to */
  148. $answer = split(' ', $response);
  149. $new_url = clean_input_url($answer[1]);
  150. }
  151. if (preg_match('/^content-type: /i', $response)) {
  152. $answer = split(' ', $response);
  153. $this->content_type = preg_replace('/\/.*$/', '', $answer[1]);
  154. }
  155. }
  156. if (!empty($new_url) && $new_url != $url) {
  157. syslog(LOG_NOTICE, "Joneame, redirected ($current_user->user_login): $url -> $new_url");
  158. /* Check again the url */
  159. // Warn: relative path can come in "Location:" headers, manage them
  160. if(!preg_match('/^http[s]*:/', $new_url)) {
  161. // It's relative
  162. $new_url = $url . $new_url;
  163. }
  164. if (!$this->check_url($new_url, $check_local, true)) {
  165. $this->url = $new_url;
  166. return false;
  167. }
  168. // Change the url if we were directed to another host
  169. if (strlen($new_url) < 250 && ($new_url_components = @parse_url($new_url))) {
  170. if ($url_components['host'] != $new_url_components['host']) {
  171. syslog(LOG_NOTICE, "Joneame, changed source URL ($current_user->user_login): $url -> $new_url");
  172. $url = $new_url;
  173. $url_components = $new_url_components;
  174. }
  175. }
  176. }
  177. $url_ok = $this->html = @stream_get_contents($stream, $maxlen);
  178. fclose($stream);
  179. } else {
  180. syslog(LOG_NOTICE, "Joneame, error getting ($current_user->user_login): $url");
  181. $url_ok = false;
  182. }
  183. //$url_ok = $this->html = @file_get_contents($url, false, $context, 0, 200000);
  184. } else {
  185. $url_ok = $this->html = @file_get_contents($url);
  186. }
  187. $this->url=$url;
  188. // Fill content type if empty
  189. // Right now only check for typical image extensions
  190. if (empty($this->content_type)) {
  191. if (preg_match('/(jpg|jpeg|gif|png)(\?|#|$)/i', $this->url)) {
  192. $this->content_type='image';
  193. }
  194. }
  195. // NO more to do
  196. if (!$url_ok)
  197. return true;
  198. if(preg_match('/charset=([a-zA-Z0-9-_]+)/i', $this->html, $matches)) {
  199. $this->encoding=trim($matches[1]);
  200. if(strcasecmp($this->encoding, 'utf-8') != 0) {
  201. $this->html=iconv($this->encoding, 'UTF-8//IGNORE', $this->html);
  202. }
  203. }
  204. // Check if the author doesn't want to share
  205. if (preg_match('/<!-- *jnm_no_share *-->/', $this->html)) {
  206. $this->ban = array();
  207. $this->ban['comment'] = _('el autor no desea que se envíe el artículo, respeta sus deseos');
  208. syslog(LOG_NOTICE, "Joneame, noshare ($current_user->user_login): $url");
  209. return false;
  210. }
  211. // Now we analyse the html to find links to banned domains
  212. // It avoids the trick of using google or technorati
  213. // Ignore it if the link has a rel="nofollow" to ignore comments in blogs
  214. if (!preg_match('/content="[^"]*(vBulletin|phpBB)/i', $this->html)) {
  215. preg_match_all('/(< *meta +http-equiv|< *script|< *iframe|< *frame[^<]*>|< *h[0-9][^<]*>[^<]*<a|window\.|document.\|parent\.|location\.|top\.|self\.)[^>]*(href|url|action|src|location|replace) *[=\(] *[\'"]{0,1}https*:\/\/[^\s "\'>]+[\'"\;\)]{0,1}[^>]*>/i', $this->html, $matches);
  216. } else {
  217. preg_match_all('/(< *a|<* meta +http-equiv|<* script|<* iframe|<* frame[^<]*>|window\.|document.\|parent\.|location\.|top\.|self\.)[^>]*(href|url|action|src|location|replace) *[=\(] *[\'"]{0,1}https*:\/\/[^\s "\'>]+[\'"\;\)]{0,1}[^>]*>/i', $this->html, $matches);
  218. }
  219. $check_counter = 0;
  220. $second_level = preg_quote(preg_replace('/^(.+\.)*([^\.]+)\.[^\.]+$/', "$2", $url_components['host']));
  221. foreach ($matches[0] as $match) {
  222. if (!preg_match('/<a.+rel=.*nofollow.*>/', $match)) {
  223. preg_match('/(href|url|action|src|location|replace) *[=\(] *[\'"]{0,1}(https*:\/\/[^\s "\'>]+)[\'"\;\)]{0,1}/i', $match, $url_a);
  224. $embeded_link = $url_a[2];
  225. $new_url_components = @parse_url($embeded_link);
  226. if (! empty($embeded_link) && $check_counter < 5 && ! $checked_links[$new_url_components['host']]) {
  227. if (! preg_match("/$second_level\.[^\.]+$/", $new_url_components['host']) ) {
  228. $check_counter++;
  229. }
  230. $checked_links[$new_url_components['host']] = true;
  231. if (!$this->check_url($embeded_link, false) && $this->banned) return false;
  232. }
  233. }
  234. }
  235. // The URL has been checked
  236. $this->valid = true;
  237. if(preg_match('/<title[^<>]*>([^<>]*)<\/title>/si', $this->html, $matches)) {
  238. $url_title=clean_text($matches[1]);
  239. if (mb_strlen($url_title) > 3) {
  240. $this->url_title=$url_title;
  241. }
  242. }
  243. return true;
  244. }
  245. function trackback() {
  246. // Now detect trackbacks
  247. if (preg_match('/trackback:ping="([^"]+)"/i', $this->html, $matches) ||
  248. preg_match('/trackback:ping +rdf:resource="([^>]+)"/i', $this->html, $matches) ||
  249. preg_match('/<trackback:ping>([^<>]+)/i', $this->html, $matches)) {
  250. $trackback=trim($matches[1]);
  251. } elseif (preg_match('/<a[^>]+rel="trackback"[^>]*>/i', $this->html, $matches)) {
  252. if (preg_match('/href="([^"]+)"/i', $matches[0], $matches2)) {
  253. $trackback=trim($matches2[1]);
  254. }
  255. } elseif (preg_match('/<a[^>]+href=[^>#]+>[^>]*trackback[^>]*<\/a>/i', $this->html, $matches)) {
  256. if (preg_match('/href="([^"]+)"/i', $matches[0], $matches2)) {
  257. $trackback=trim($matches2[1]);
  258. }
  259. } elseif (preg_match('/(http:\/\/[^\s#]+\/trackback\/*)/i', $this->html, $matches)) {
  260. $trackback=trim($matches[0]);
  261. }
  262. if (!empty($trackback)) {
  263. $this->trackback = clean_input_url($trackback);
  264. return true;
  265. }
  266. return false;
  267. }
  268. function pingback() {
  269. $url_components = @parse_url($this->url);
  270. // Now we use previous pingback or detect it
  271. if ((!empty($url_components['query']) || preg_match('|^/.*[\.-/]+|', $url_components['path']))) {
  272. if (!empty($this->pingback)) {
  273. $trackback = $this->pingback;
  274. } elseif (preg_match('/<link[^>]+rel="pingback"[^>]*>/i', $this->html, $matches)) {
  275. if (preg_match('/href="([^"]+)"/i', $matches[0], $matches2)) {
  276. $trackback='ping:'.trim($matches2[1]);
  277. }
  278. }
  279. }
  280. if (!empty($trackback)) {
  281. $this->trackback = clean_input_url($trackback);
  282. return true;
  283. }
  284. return false;
  285. }
  286. function has_rss() {
  287. return preg_match('/<link[^>]+(text\/xml|application\/rss\+xml|application\/atom\+xml)[^>]+>/i', $this->html);
  288. }
  289. function create_blog_entry() {
  290. require_once(mnminclude.'blog.php');
  291. $blog = new Blog();
  292. $blog->analyze_html($this->url, $this->html);
  293. if(!$blog->read('key')) {
  294. $blog->store();
  295. }
  296. $this->blog=$blog->id;
  297. $this->type=$blog->type;
  298. }
  299. function type() {
  300. if (empty($this->type)) {
  301. if ($this->blog > 0) {
  302. require_once(mnminclude.'blog.php');
  303. $blog = new Blog();
  304. $blog->id = $this->blog;
  305. if($blog->read()) {
  306. $this->type=$blog->type;
  307. return $this->type;
  308. }
  309. }
  310. return 'normal';
  311. }
  312. return $this->type;
  313. }
  314. function store() {
  315. global $db, $current_user;
  316. $link_url = $db->escape($this->url);
  317. $link_uri = $db->escape($this->uri);
  318. $link_url_title = $db->escape($this->url_title);
  319. $link_title = $db->escape($this->title);
  320. $link_tags = $db->escape($this->tags);
  321. $link_content = $db->escape(normalize_smileys($this->content));
  322. $link_thumb = $db->escape($this->thumb);
  323. $link_thumb_x = intval($this->thumb_x);
  324. $link_thumb_y = intval($this->thumb_y);
  325. $link_thumb_status = $db->escape($this->thumb_status);
  326. $db->query("LOCK TABLES links WRITE");
  327. $this->store_basic();
  328. $db->query("UPDATE links set link_url='$link_url', link_uri='$link_uri', link_url_title='$link_url_title', link_title='$link_title', link_content='$link_content', link_tags='$link_tags', link_thumb='$link_thumb', link_thumb_x=$link_thumb_x, link_thumb_y=$link_thumb_y, link_thumb_status='$link_thumb_status' WHERE link_id=$this->id");
  329. $db->query("UNLOCK TABLES");
  330. }
  331. function store_basic() {
  332. global $db, $current_user, $globals;
  333. if(!$this->date)
  334. $this->date=$globals['now'];
  335. $link_author = $this->author;
  336. $link_blog = $this->blog;
  337. $link_status = $db->escape($this->status);
  338. $link_votes = $this->votes;
  339. $link_negatives = $this->negatives;
  340. $link_anonymous = $this->anonymous;
  341. $link_aleatorios_positivos = $this->aleatorios_positivos;
  342. $link_aleatorios_negativos = $this->aleatorios_negativos;
  343. $link_comments = $this->comments;
  344. $link_karma = $this->karma;
  345. $link_votes_avg = $this->votes_avg;
  346. $link_randkey = $this->randkey;
  347. $link_category = $this->category;
  348. $link_votos_permitidos = intval($this->votos_permitidos);
  349. $link_comentarios_permitidos = intval($this->comentarios_permitidos);
  350. $link_date = $this->date;
  351. $link_sent_date = $this->sent_date;
  352. $link_sent = $this->sent;
  353. $link_content_type = $db->escape($this->content_type);
  354. $link_ip = $db->escape($this->ip);
  355. if($this->id===0) {
  356. $db->query("INSERT INTO links (link_author, link_blog, link_status, link_randkey, link_category, link_date, link_sent_date, link_sent, link_votes, link_negatives, link_karma, link_anonymous, link_votes_avg, link_content_type, link_ip) VALUES ($link_author, $link_blog, '$link_status', $link_randkey, $link_category, FROM_UNIXTIME($link_date), FROM_UNIXTIME($link_sent_date), $link_sent, $link_votes, $link_negatives, $link_karma, $link_anonymous, $link_votes_avg, '$link_content_type', '$link_ip')");
  357. $this->id = $db->insert_id;
  358. } else {
  359. // update
  360. $db->query("UPDATE links set link_author=$link_author, link_blog=$link_blog, link_status='$link_status', link_randkey=$link_randkey, link_category=$link_category, link_date=FROM_UNIXTIME($link_date), link_sent_date=FROM_UNIXTIME($link_sent_date), link_votes=$link_votes,link_aleatorios_positivos=$link_aleatorios_positivos,link_aleatorios_negativos=$link_aleatorios_negativos, link_votos_permitidos=$link_votos_permitidos, link_comentarios_permitidos=$link_comentarios_permitidos, link_sent=$link_sent, link_negatives=$link_negatives, link_comments=$link_comments, link_karma=$link_karma, link_anonymous=$link_anonymous, link_votes_avg=$link_votes_avg, link_content_type='$link_content_type', link_ip='$link_ip' WHERE link_id=$this->id");
  361. }
  362. if ($this->votes == 1 && $this->negatives == 0 && $this->status == 'queued') {
  363. // This is a new link, add it to the events, it an additional control
  364. // just in case the user dind't do the last submit phase and voted later
  365. log_conditional_insert('link_new', $this->id, $this->author);
  366. }
  367. }
  368. function read_basic($key='id') {
  369. global $db;
  370. switch ($key) {
  371. case 'id':
  372. $cond = "link_id = $this->id";
  373. break;
  374. case 'uri':
  375. $cond = "link_uri = '$this->uri'";
  376. break;
  377. case 'url':
  378. $cond = "link_url = '$this->url'";
  379. break;
  380. default:
  381. $cond = "link_id = $this->id";
  382. }
  383. if(($link = $db->get_row("SELECT link_id, link_karma as karma, link_author, link_status, link_votes, link_negatives, link_anonymous, link_randkey, link_votos_permitidos, link_aleatorios_positivos, link_sent as sent, UNIX_TIMESTAMP(link_date) as link_ts, link_title FROM links WHERE $cond"))) {
  384. $this->id=$link->link_id;
  385. $this->author=$link->link_author;
  386. $this->status=$link->link_status;
  387. $this->votes=$link->link_votes;
  388. $this->negatives=$link->link_negatives;
  389. $this->anonymous=$link->link_anonymous;
  390. $this->randkey=$link->link_randkey;
  391. $this->votos_permitidos=$link->link_votos_permitidos;
  392. $this->sent = $link->sent;
  393. $this->date=$link->link_ts;
  394. $this->title=$link->link_title;
  395. $this->karma=$link->karma;
  396. $this->aleatorios_positivos=$link->link_aleatorios_positivos;
  397. $this->read = true;
  398. return true;
  399. }
  400. return false;
  401. }
  402. function read($key='id') {
  403. global $db;
  404. switch ($key) {
  405. case 'id':
  406. $cond = "link_id = $this->id";
  407. break;
  408. case 'uri':
  409. $cond = "link_uri = '$this->uri'";
  410. break;
  411. case 'url':
  412. $cond = "link_url = '$this->url'";
  413. break;
  414. default:
  415. $cond = "link_id = $this->id";
  416. }
  417. if(($result = $db->get_row("SELECT".Link::SQL."WHERE $cond AND user_id=link_author"))) {
  418. foreach(get_object_vars($result) as $var => $value) $this->$var = $value;
  419. $this->read = true;
  420. return true;
  421. }
  422. $this->read = false;
  423. return false;
  424. }
  425. function duplicates($url) {
  426. global $db;
  427. $trimmed = $db->escape(preg_replace('/\/$/', '', $url));
  428. $list = "'$trimmed', '$trimmed/'";
  429. if (preg_match('/^http:\/\/www\./', $trimmed)) {
  430. $link_alternative = preg_replace('/^http:\/\/www\./', 'http://', $trimmed);
  431. } else {
  432. $link_alternative = preg_replace('/^http:\/\//', 'http://www.', $trimmed);
  433. }
  434. $list .= ", '$link_alternative', '$link_alternative/'";
  435. $found = $db->get_var("SELECT link_id FROM links WHERE link_url in ($list) AND (link_status not in ('discard', 'abuse') OR link_votes>0) limit 1");
  436. return $found;
  437. }
  438. function get_shake_box_class() {
  439. switch ($this->status) {
  440. case 'queued':
  441. $this->box_class = 'mnm-queued';
  442. break;
  443. case 'abuse':
  444. $this->box_class = 'jnm-abuse';
  445. break;
  446. case 'autodiscard':
  447. case 'discard':
  448. case 'duplicated':
  449. $this->box_class = 'mnm-discarded';
  450. break;
  451. case 'published':
  452. default:
  453. $this->box_class = 'mnm-published';
  454. break;
  455. }
  456. }
  457. // nos dice si el voto es aleatorio o normal
  458. function aleatorio_info() {
  459. global $db, $current_user;
  460. if ($current_user->user_id == 0) return false;
  461. return $db->get_row("SELECT vote_aleatorio as aleatorio, vote_value as valor FROM votes WHERE vote_type = 'links' AND vote_link_id = $this->id AND vote_user_id = $current_user->user_id LIMIT 1");
  462. }
  463. function vote_exists($user) {
  464. require_once(mnminclude.'votes.php');
  465. $vote = new Vote;
  466. $vote->user=$user;
  467. $vote->link=$this->id;
  468. $vote->type = 'links';
  469. return $vote->exists(); //devuelve el valor del voto si existe, y false si no existe
  470. }
  471. function votes($user) {
  472. require_once(mnminclude.'votes.php');
  473. $vote = new Vote;
  474. $vote->user=$user;
  475. $vote->link=$this->id;
  476. return $vote->count();
  477. }
  478. function aleatorios_count(){
  479. return $this->aleatorios_positivos + $this->aleatorios_negativos;
  480. }
  481. function print_summary($type='full', $karma_best_comment = 0, $show_tags = true) {
  482. global $current_user, $current_user, $globals, $db;
  483. if(!$this->read) return;
  484. if($this->is_votable()) {
  485. $this->voted = $this->vote_exists($current_user->user_id);
  486. }
  487. echo '<div class="news-summary">';
  488. echo '<div class="news-body">';
  489. if ($globals['link']) $this->print_warn();
  490. /*
  491. if (! $globals['link']) {
  492. $url = $this->get_permalink();
  493. $nofollow = '';
  494. */
  495. if ($this->status != 'published') $nofollow = ' rel="nofollow"';
  496. else $nofollow = '';
  497. $url = htmlspecialchars($this->url);
  498. if (!empty($this->title)) {
  499. $this->print_shake_box();
  500. }
  501. echo '<h1>';
  502. echo '<a href="'.$url.'"'.$nofollow.'>'. $this->title. '</a>';
  503. echo '</h1>';
  504. if ($globals['link']) {
  505. echo '<div class="news-submitted">';
  506. echo _('por').' <a href="'.get_user_uri($this->username, 'history').'">'.$this->username.'</a> ';
  507. // Print dates
  508. if ($globals['now'] - $this->date > 604800) { // 7 days
  509. if($this->status == 'published')
  510. echo _('publicado el').get_date_time($this->date);
  511. else
  512. echo _('el').get_date_time($this->sent_date);
  513. } else {
  514. if($this->status == 'published')
  515. echo _('publicado hace').txt_time_diff($this->date);
  516. else
  517. echo _('hace').txt_time_diff($this->sent_date);
  518. }
  519. echo "</div>\n";
  520. }
  521. $text = text_to_html($this->content);
  522. // Change links to mydomain.net to m.mydomain.net (used in "related")
  523. $my_domain = get_server_name();
  524. $parent_domain = preg_replace('/movil\./', '', $my_domain);
  525. if ($parent_domain != $my_domain && preg_match('#[^\.]'.preg_quote($parent_domain).'/#', $text)) {
  526. $text = preg_replace('#([^\.])'.preg_quote($parent_domain).'/#', "$1$my_domain/", $text);
  527. }
  528. echo $text;
  529. echo '<div class="news-details">';
  530. if($this->comments > 0) {
  531. $comments_mess = $this->comments . ' ' . _('comentarios');
  532. } else {
  533. $comments_mess = _('sin chorradas');
  534. }
  535. echo '<span class="comments"><a href="'.$this->get_relative_permalink().'">'.$comments_mess. '</a></span>';
  536. if ($globals['link']) {
  537. // Print meta and category
  538. echo ' <span class="tool">'._('en').': ';
  539. echo $this->meta_name.', ';
  540. echo $this->category_name;
  541. echo '</span>';
  542. echo ' <span class="tool">carisma: <span id="a-karma-'.$this->id.'">'.intval($this->karma).'</span></span>';
  543. }
  544. echo '</div>'."\n";
  545. // End news details
  546. if ($globals['link']) {
  547. echo '<div class="news-details">';
  548. echo '<strong>'._('sensuras').'</strong>: '.($this->negatives+$this->aleatorios_negativos).'&nbsp;&nbsp;';
  549. echo '<strong>'._('mafiosos').'</strong>: '.($this->votes+$this->aleatorios_positivos).'&nbsp;&nbsp;';
  550. echo '<strong>'._('anónimos').'</strong>: '.$this->anonymous.'&nbsp;&nbsp;';
  551. echo '</div>' . "\n";
  552. }
  553. echo '</div>'."\n";
  554. echo '</div>'."\n";
  555. }
  556. function print_shake_box() {
  557. global $current_user, $anonnymous_vote, $site_key, $globals;
  558. switch ($this->status) {
  559. case 'queued': // another color box for not-published
  560. $box_class = 'mnm-queued';
  561. break;
  562. case 'abuse': // another color box for discarded
  563. case 'autodiscard': // another color box for discarded
  564. case 'discard': // another color box for discarded
  565. $box_class = 'mnm-discarded';
  566. break;
  567. case 'published': // default for published
  568. default:
  569. $box_class = 'mnm-published';
  570. break;
  571. }
  572. echo '<div class="news-shakeit">';
  573. echo '<div class="'.$box_class.'">';
  574. echo '<a id="a-votes-'.$this->id.'" href="'.$this->get_relative_permalink().'">'.($this->votes+$this->anonymous+$this->aleatorios_positivos).'</a></div>';
  575. echo '<div class="menealo" id="a-va-'.$this->id.'">';
  576. $md5=md5($current_user->user_id.$this->id.$this->randkey.$globals['user_ip']);
  577. $md5 = "'".$md5."'";
  578. if (!$this->votes_enabled) {
  579. echo '<span>'._('cerrado').'</span>';
  580. } elseif( $this->voted == null) {
  581. echo '<a href="javascript:menealo('."$current_user->user_id,$this->id,$this->id".','.$md5.')" id="a-shake-'.$this->id.'">'._('jonéalo').'</a>';
  582. } else {
  583. if ($this->voted >= 0) $mess = _('&#161;&#161;Biba!!');
  584. else $mess = ':-(';
  585. echo '<span id="a-shake-'.$this->id.'">'.$mess.'</span>';
  586. }
  587. echo '</div>'."\n";
  588. echo '</div>'."\n";
  589. }
  590. function print_warn() {
  591. global $db, $globals;
  592. //si el estado es abuso muestra un aviso
  593. if ($this->status == 'abuse') {
  594. echo '<div class="warn"><strong>'._('Aviso').'</strong>: ';
  595. echo _('historia descartada por violar las').' <a href="'.$globals['legal'].'#tos">'._('normas de uso').'</a>';
  596. echo "</div>\n";
  597. }
  598. else {
  599. $this->warned = false;
  600. }
  601. if ($this->status == 'abuse' && stripos($this -> title , '[NSFW]') && stripos($this -> title , '[+18]') != FALSE) {
  602. echo '<div class="warn"><strong>'._('Aviso').'</strong>: ';
  603. echo _('historia descartada por violar las').' <a href="'.$globals['legal'].'#tos">'._('normas de uso').'</a>' ;
  604. echo "</div>\n";
  605. }
  606. else {
  607. $this->warned = false;
  608. }
  609. //fin del aviso
  610. //si detecta NSFW o +18 en el titulo muestra un aviso
  611. if ((stripos($this -> title , '[NSFW]') || stripos($this -> title , '[+18]') != FALSE ) && $this->status != 'abuse') {
  612. echo '<div class="porn"><strong>'._('Oiga!!').'</strong> ';
  613. echo _('el enlace de esta historia podr&iacute;a contener material solo apto para adultos.');
  614. echo "</div>\n";
  615. //fin del aviso
  616. } elseif ( $this->votes_enabled && !$this->is_discarded() && $this->negatives > 1 && $this->negatives > $this->votes/6 ) {
  617. $this->warned = true;
  618. echo '<div class="warn"><strong>'._('Aviso automático de la mafia').'</strong>: ';
  619. if ($this->status == 'published') {
  620. echo _('historia controvertida, por favor lee las chorradas, a saber qué han liado estos.');
  621. } elseif ($this->author == $current_user->user_id && $this->is_editable()) {
  622. echo _('Esta noticia tiene varios votos sensuradores. Si la descartas manualmente tu carisma no será afectado');
  623. } else {
  624. // Only says "what" if most votes are "wrong" or "duplicated"
  625. $negatives = $db->get_row("select vote_value, count(vote_value) as count from votes where vote_type='links' and vote_link_id=$this->id and vote_value < 0 group by vote_value order by count desc limit 1");
  626. if ($negatives->count > 2 && $negatives->count >= $this->negatives/2 && ($negatives->vote_value == -1 || $negatives->vote_value == -3)) {
  627. echo _('Esta noticia podría ser <strong>'). get_negative_vote($negatives->vote_value) . '</strong>. ';
  628. } else {
  629. echo _('Esta noticia tiene varios votos sensuradores.');
  630. }
  631. if(!$this->voted ) {
  632. echo ' <a href="'.$this->get_relative_permalink().'/votos">' ._('Asegúrate').'</a> ' . _('antes de jonear') . '.';
  633. }
  634. }
  635. echo "</div>\n";
  636. } else {
  637. $this->warned = false;
  638. }
  639. }
  640. function print_problem_form() {
  641. global $current_user, $db, $anon_karma, $anonnymous_vote, $globals, $site_key;
  642. echo '<form class="tool" action="" id="problem-'.$this->id.'">';
  643. echo '<select '.$status.' name="ratings" onchange="';
  644. echo 'report_problem(this.form,'."$current_user->user_id, $this->id, "."'".$this->md5."'".')';
  645. echo '">';
  646. echo '<option value="0" selected="selected">'._('sensura').'</option>';
  647. foreach (array_keys($globals['negative_votes_values']) as $pvalue) {
  648. echo '<option value="'.$pvalue.'">'.$globals['negative_votes_values'][$pvalue].'</option>';
  649. }
  650. echo '</select>';
  651. echo '</form>';
  652. }
  653. function insert_vote($value) {
  654. global $db, $current_user;
  655. require_once(mnminclude.'votes.php');
  656. $vote = new Vote;
  657. $vote->user=$current_user->user_id;
  658. $vote->link=$this->id;
  659. if ($vote->exists()) return false;
  660. if (!$this->insert_aleatorio){
  661. // For karma calculation
  662. if ($value < 0)
  663. $karma_value = $value;
  664. else if ($this->status != 'published')
  665. $karma_value = $value;
  666. else
  667. $karma_value = 0;
  668. $vote->aleatorio = false;
  669. if ($karma_value < 0) $user_karma = round($current_user->user_karma);
  670. } else {
  671. $karma_value = $vote->get_aleatorio_value();
  672. $vote->aleatorio = true;
  673. }
  674. $vote->value=$karma_value;
  675. if($vote->insert()) {
  676. /* Aumentar contador aleatorios*/
  677. if ($this->insert_aleatorio && $vote->value >= 0) $db->query("update links set link_aleatorios_positivos=link_aleatorios_positivos+1 where link_id = $this->id");
  678. else if ($this->insert_aleatorio && $vote->value < 0) $db->query("update links set link_aleatorios_negativos=link_aleatorios_negativos+1 where link_id = $this->id");
  679. if ($vote->value < 0) {
  680. if (!$this->insert_aleatorio) /* Aumenta contador y carisma negativos */
  681. $db->query("update links set link_negatives=link_negatives+1, link_karma=link_karma-$user_karma $link_aleatorios where link_id = $this->id"); /* Reduce karma negativo */
  682. else $db->query("update links set link_karma=link_karma-$user_karma where link_id = $this->id");
  683. } else { /* Aumenta contador y carisma positivos */
  684. if ($current_user->user_id > 0 && !$this->insert_aleatorio) $db->query("update links set link_votes = link_votes+1, link_karma=link_karma+$karma_value where link_id = $this->id"); /*Carisma +*/
  685. else if ($current_user->user_id > 0 && $this->insert_aleatorio) $db->query("update links set link_karma=link_karma+$karma_value where link_id = $this->id"); /* Anonimos */
  686. else if ($current_user->user_id == 0) $db->query("update links set link_anonymous = link_anonymous+1, link_karma=link_karma+$karma_value where link_id = $this->id");
  687. }
  688. $new = $db->get_row("select link_votes, link_anonymous, link_negatives, link_karma, link_aleatorios_positivos, link_aleatorios_negativos from links where link_id = $this->id");
  689. $this->votes = $new->link_votes;
  690. $this->anonymous = $new->link_anonymous;
  691. $this->negatives = $new->link_negatives;
  692. $this->karma = $new->link_karma;
  693. $this->aleatorios_positivos = $new->link_aleatorios_positivos;
  694. $this->aleatorios_negativos = $new->link_aleatorios_negativos;
  695. return true;
  696. }
  697. return false;
  698. }
  699. function publish() {
  700. global $globals;
  701. if(!$this->read) $this->read_basic();
  702. $this->published_date = $globals['now'];
  703. $this->date = $globals['now'];
  704. $this->status = 'published';
  705. $this->store_basic();
  706. }
  707. function update_comments() {
  708. global $db;
  709. $this->comments = $db->get_var("SELECT count(*) FROM comments WHERE comment_link_id = $this->id");
  710. $db->query("update links set link_comments = $this->comments where link_id = $this->id");
  711. }
  712. function is_discarded() {
  713. return $this->status == 'discard' || $this->status == 'abuse' || $this->status == 'autodiscard' || $this->sent == 0 || $this->status == 'duplicated' ;
  714. }
  715. function is_editable() {
  716. global $current_user, $globals;
  717. if($current_user->user_id) {
  718. // es el usuario, dispone de los primeros 15 minutos
  719. if(($this->author == $current_user->user_id
  720. && $this->status != 'published'
  721. && $this->status != 'abuse'
  722. && $this->status != 'autodiscard'
  723. && $globals['now'] - $this->sent_date < $globals['edicion_historias_usuario'])
  724. // es "special" , dispone a partir de los 15 minutos, hasta pasados 10
  725. || ($this->author != $current_user->user_id
  726. && $current_user->especial
  727. && $this->status != 'abuse'
  728. && $this->status != 'autodiscard'
  729. // && $this->status != 'published'
  730. && $globals['now'] - $this->sent_date > $globals['edicion_historias_usuario'] + 300
  731. && $globals['now'] - $this->sent_date < $globals['edicion_historias_usuario'] + 600)
  732. // es administrador
  733. || $current_user->admin) {
  734. return true;
  735. }
  736. }
  737. return false;
  738. }
  739. function is_map_editable() {
  740. global $current_user, $globals;
  741. if($current_user->user_id == 0) return false;
  742. if( ($this->author == $current_user->user_id && $current_user->user_level == 'normal' && $globals['now'] - $this->sent_date < 9800)
  743. || ($current_user->especial && $globals['now'] - $this->sent_date < 14400)
  744. || $current_user->admin) {
  745. return true;
  746. }
  747. return false;
  748. }
  749. function get_editable_teaser() {
  750. global $current_user, $globals;
  751. if ($current_user->admin)
  752. $iddqd = ' iddqd';
  753. $editable_teaser = '<span class="n-edit'.$iddqd.'">';
  754. if ($this->author == $current_user->user_id)
  755. $editable_teaser .= calc_remaining_edit_time($this->sent_date, $globals['edicion_historias_usuario']);
  756. elseif ($this->author != $current_user->user_id && $current_user->especial)
  757. $editable_teaser .= 'special';
  758. elseif ($current_user->admin)
  759. $editable_teaser .= 'admin';
  760. $editable_teaser .= '</span>';
  761. return $editable_teaser;
  762. }
  763. function get_map_editable_teaser() {
  764. global $current_user, $globals;
  765. if ($current_user->admin)
  766. $iddqd = ' iddqd';
  767. $editable_teaser = '<span class="geo-edit'.$iddqd.'">';
  768. if ($this->author == $current_user->user_id)
  769. $editable_teaser .= calc_remaining_edit_time($this->sent_date, 9800, true); // !!FIXME hardcoded!
  770. elseif ($this->author != $current_user->user_id && $current_user->especial)
  771. $editable_teaser .= 'special';
  772. elseif ($current_user->admin)
  773. $editable_teaser .= 'admin';
  774. $editable_teaser .= '</span>';
  775. return $editable_teaser;
  776. }
  777. function is_votable() {
  778. global $globals;
  779. if(isset($globals['bot']) && $globals['bot'] || $this->status == 'abuse' || $this->status == 'duplicated' || $this->status == 'autodiscard' ||
  780. ($globals['time_enabled_votes'] > 0 && $this->date < $globals['now'] - $globals['time_enabled_votes']) || !$this->votos_permitidos) {
  781. $this->votes_enabled = false;
  782. } else {
  783. $this->votes_enabled = true;
  784. }
  785. return $this->votes_enabled;
  786. }
  787. function negatives_allowed() {
  788. global $globals, $current_user;
  789. return $current_user->user_id > 0
  790. && $this->author != $current_user->user_id
  791. && $this->status != 'abuse'
  792. && $this->status != 'autodiscard'
  793. && $current_user->user_karma >= $globals['min_karma_for_negatives']
  794. && ($this->status != 'published'
  795. || $this->status == 'published'
  796. && ($this->date > $globals['now'] - 7200
  797. || $this->warned)
  798. );
  799. }
  800. function get_uri() {
  801. global $db, $globals;
  802. $seq = 0;
  803. require_once(mnminclude.'uri.php');
  804. $new_uri = $base_uri = get_uri($this->title);
  805. while ($db->get_var("select count(*) from links where link_uri='$new_uri' and link_id != $this->id") && $seq < 20) {
  806. $seq++;
  807. $new_uri = $base_uri . "-$seq";
  808. }
  809. // In case we tried 20 times, we just add the id of the article
  810. if ($seq >= 20) {
  811. $new_uri = $base_uri . "-$this->id";
  812. }
  813. $this->uri = $new_uri;
  814. }
  815. function get_short_permalink() {
  816. global $globals;
  817. if ($globals['base_story_url']) {
  818. return 'http://'.get_server_name().$globals['base_url'].$globals['base_story_url'].'0'.$this->id;
  819. } else {
  820. return 'http://'.get_server_name().$this->get_relative_permalink();
  821. }
  822. }
  823. function get_relative_permalink() {
  824. global $globals;
  825. if (!empty($this->uri) && !empty($globals['base_story_url']) ) {
  826. return $globals['base_url'] . $globals['base_story_url'] . $this->uri;
  827. } else {
  828. return $globals['base_url'] . 'historia.php?id=' . $this->id;
  829. }
  830. }
  831. function get_permalink() {
  832. return 'http://'.get_server_name().$this->get_relative_permalink();
  833. }
  834. function get_trackback() {
  835. global $globals;
  836. return "http://".get_server_name().$globals['base_url'].'trackback.php?id='.$this->id;
  837. }
  838. function get_status_text($status = false) {
  839. global $current_user, $linkres;
  840. if (!$status) $status = $this->status;
  841. switch ($status) {
  842. case ('abuse'):
  843. return _('abuso');
  844. case ('discard'):
  845. return _('descartada');
  846. case ('queued'):
  847. return _('pendiente');
  848. case ('published'):
  849. return _('publicada');
  850. case ('duplicated'):
  851. return _('duplicada');
  852. case ('autodiscard'):
  853. if ($current_user->user_id == $linkres->author)
  854. return _('autodescartada');
  855. if ((($current_user->admin)) && ($current_user->user_id != $linkres->author))
  856. return _('descartada');
  857. }
  858. return $status;
  859. }
  860. function get_latlng() {
  861. require_once(mnminclude.'geo.php');
  862. return geo_latlng('link', $this->id);
  863. }
  864. function print_content_type_buttons($link_title = false) {
  865. global $globals;
  866. // Is it an image or video?
  867. switch ($this->content_type) {
  868. case 'image':
  869. case 'video':
  870. case 'text':
  871. $type[$this->content_type] = 'checked="checked"';
  872. break;
  873. default:
  874. $type['text'] = 'checked="checked"';
  875. }
  876. // Not Safe For Work (NSFW) y Sólo para adultos (+18)
  877. echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  878. if (stripos($link_title , '[NSFW]')) {
  879. echo '<input type="checkbox" checked="checked" name="sec[0]" value="text" id="nsfw"/>';
  880. } else {
  881. echo '<input type="checkbox" name="sec[0]" value="text" id="nsfw"/>';
  882. }
  883. echo '&nbsp;<label for="nsfw">'._('NSFW').'</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  884. echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  885. if (stripos($link_title , '[+18]')) {
  886. echo '<input type="checkbox" checked="checked" name="sec[1]" value="text" id="mas18"/>';
  887. } else {
  888. echo '<input type="checkbox" name="sec[1]" value="text" id="mas18"/>';
  889. }
  890. echo '&nbsp;<label for="mas18">'._('+18').'</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  891. echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  892. echo '<input type="radio" '.$type['text'].' name="type" value="text" id="text"/>';
  893. echo '&nbsp;<label for="text">'._('texto').'</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  894. echo '<input type="radio" '.$type['image'].' name="type" value="image" id="image"/>';
  895. echo '&nbsp;<label for="image"><img src="'.get_cover_pixel().'" class="icon image media-icon" alt="'._('¿es una imagen?').'" title="'._('¿es una imagen?').'" /></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  896. echo '<input type="radio" '.$type['video'].' name="type" value="video" id="video"/>';
  897. echo '&nbsp;<label for="video"><img src="'.get_cover_pixel().'" class="icon video media-icon" alt="'._('¿es un vídeo?').'" title="'._('¿es un vídeo?').'" /></label>';
  898. }
  899. function read_content_type_buttons($type) {
  900. switch ($type) {
  901. case 'image':
  902. $this->content_type = 'image';
  903. break;
  904. case 'video':
  905. $this->content_type = 'video';
  906. break;
  907. case 'text':
  908. default:
  909. $this->content_type = 'text';
  910. }
  911. }
  912. // Thumbnails management
  913. function get_thumb($debug = false) {
  914. global $globals;
  915. require_once(mnminclude.'webimages.php');
  916. require_once(mnminclude.'blog.php');
  917. $site = false;
  918. if (empty($this->url))
  919. if (!$this->read()) return false;
  920. $blog = new Blog();
  921. $blog->id = $this->blog;
  922. if ($blog->read())
  923. $site = $blog->url;
  924. $this->image_parser = new HtmlImages($this->url, $site);
  925. $this->image_parser->debug = $debug;
  926. $this->image_parser->referer = $this->get_permalink();
  927. $img = $this->image_parser->get();
  928. $this->thumb_status = 'checked';
  929. $this->thumb = '';
  930. if ($img) {
  931. $filepath = '/datos/web/static/thumbs';
  932. @mkdir($filepath);
  933. $l1 = intval($this->id / 100000);
  934. $l2 = intval(($this->id % 100000) / 1000);
  935. $filepath .= "/$l1";
  936. @mkdir($filepath);
  937. @chmod($filepath, 0777);
  938. $filepath .= "/$l2";
  939. @mkdir($filepath);
  940. @chmod($filepath, 0777);
  941. $filepath .= "/$this->id.jpg";
  942. if ($img->type == 'local') {
  943. $img->scale($globals['thumb_size']);
  944. if ($img->save($filepath)) {
  945. @chmod($filepath, 0777);
  946. $this->thumb = 'http://static.joneame.net/thumbs';
  947. $this->thumb .= "/$l1/$l2/$this->id.jpg";
  948. $this->thumb_x = $img->x;
  949. $this->thumb_y = $img->y;
  950. $this->thumb_status='local';
  951. syslog(LOG_NOTICE, "Joneame, new thumbnail $img->url to " . $this->get_permalink());
  952. if ($debug)
  953. echo "<!-- Joneame, new thumbnail $img->url -->\n";
  954. } else {
  955. $this->thumb_status = 'error';
  956. if ($debug)
  957. echo "<!-- Joneame, error saving thumbnail ".$this->get_permalink()." -->\n";
  958. syslog(LOG_NOTICE, "Joneame, error saving thumbnail $img->url for " . $this->get_permalink());
  959. }
  960. }
  961. if ($img->video)
  962. $this->content_type = 'video';
  963. }
  964. $this->store_thumb();
  965. return $this->has_thumb();
  966. }
  967. function store_thumb() {
  968. global $db;
  969. $this->thumb = $db->escape($this->thumb);
  970. $db->query("update links set link_content_type = '$this->content_type', link_thumb = '$this->thumb', link_thumb_x = $this->thumb_x, link_thumb_y = $this->thumb_y, link_thumb_status = '$this->thumb_status' where link_id = $this->id");
  971. }
  972. function has_thumb() {
  973. return $this->thumb && $this->thumb_x > 0 && $this->thumb_y > 0;
  974. }
  975. function is_nsfw () {
  976. if (stripos($this->title,'[NSFW]') == TRUE || stripos($this->title,'[+18]') == TRUE) $this->nsfw = true;
  977. else $this->nsfw = false;
  978. return $this->nsfw;
  979. }
  980. function insert_user_click(){
  981. global $db;
  982. $db->query("INSERT LOW_PRIORITY INTO link_clicks (id, counter) VALUES ($this->id,1) ON DUPLICATE KEY UPDATE counter=counter+1");
  983. setcookie('v', implode('x', $visited));
  984. }
  985. function id_visited() {
  986. if (! isset($_COOKIE['l_v']) || ! ($visited = preg_split('/x/', $_COOKIE['l_v'], 0, PREG_SPLIT_NO_EMPTY)) ) {
  987. $visited = array();
  988. $found = false;
  989. } else {
  990. $found = array_search($this->id, $visited);
  991. if (count($visited) > 10) {
  992. array_shift($visited);
  993. }
  994. if ($found !== false) {
  995. unset($visited[$found]);
  996. }
  997. }
  998. $visited[] = $this->id;
  999. $valor = implode('x', $visited);
  1000. setcookie('l_v', $valor);
  1001. return $found !== false;
  1002. }
  1003. function update_visitors(){
  1004. global $db, $globals;
  1005. if ( isset($globals['bot']) && !$globals['bot']
  1006. // && $globals['click_counter']
  1007. && $this->ip != $globals['user_ip']
  1008. && $this->id_visited() === false) {
  1009. $db->query("INSERT LOW_PRIORITY INTO link_visits (id, counter) VALUES ($this->id,1) ON DUPLICATE KEY UPDATE counter=counter+1");
  1010. $this->visits = $db->get_var("SELECT counter FROM link_visits WHERE id=$this->id");
  1011. }
  1012. }
  1013. }
  1014. ?>