The version of vichan running on lainchan.org
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

137 líneas
3.1KB

  1. <?php
  2. /*
  3. * Copyright (c) 2010-2013 Tinyboard Development Group
  4. */
  5. defined('TINYBOARD') or exit;
  6. /**
  7. * Class for generating json API compatible with 4chan API
  8. */
  9. class Api {
  10. function __construct(){
  11. global $config;
  12. /**
  13. * Translation from local fields to fields in 4chan-style API
  14. */
  15. $this->config = $config;
  16. $this->postFields = array(
  17. 'id' => 'no',
  18. 'thread' => 'resto',
  19. 'subject' => 'sub',
  20. 'body' => 'com',
  21. 'email' => 'email',
  22. 'name' => 'name',
  23. 'trip' => 'trip',
  24. 'capcode' => 'capcode',
  25. 'time' => 'time',
  26. 'thumbheight' => 'tn_w',
  27. 'thumbwidth' => 'tn_h',
  28. 'fileheight' => 'w',
  29. 'filewidth' => 'h',
  30. 'filesize' => 'fsize',
  31. 'filename' => 'filename',
  32. 'omitted' => 'omitted_posts',
  33. 'omitted_images' => 'omitted_images',
  34. 'sticky' => 'sticky',
  35. 'locked' => 'locked',
  36. );
  37. if (isset($config['api']['extra_fields']) && gettype($config['api']['extra_fields']) == 'array'){
  38. $this->postFields = array_merge($this->postFields, $config['api']['extra_fields']);
  39. }
  40. }
  41. private static $ints = array(
  42. 'no' => 1,
  43. 'resto' => 1,
  44. 'time' => 1,
  45. 'tn_w' => 1,
  46. 'tn_h' => 1,
  47. 'w' => 1,
  48. 'h' => 1,
  49. 'fsize' => 1,
  50. 'omitted_posts' => 1,
  51. 'omitted_images' => 1,
  52. 'sticky' => 1,
  53. 'locked' => 1,
  54. );
  55. private function translatePost($post) {
  56. $apiPost = array();
  57. foreach ($this->postFields as $local => $translated) {
  58. if (!isset($post->$local))
  59. continue;
  60. $toInt = isset(self::$ints[$translated]);
  61. $val = $post->$local;
  62. if ($val !== null && $val !== '') {
  63. $apiPost[$translated] = $toInt ? (int) $val : $val;
  64. }
  65. }
  66. if (isset($post->filename)) {
  67. $dotPos = strrpos($post->filename, '.');
  68. $apiPost['filename'] = substr($post->filename, 0, $dotPos);
  69. $apiPost['ext'] = substr($post->filename, $dotPos);
  70. }
  71. // Handle country field
  72. if (isset($post->body_nomarkup) && $this->config['country_flags']) {
  73. $modifiers = extract_modifiers($post->body_nomarkup);
  74. if (isset($modifiers['flag']) && isset($modifiers['flag alt']) && preg_match('/^[a-z]{2}$/', $modifiers['flag'])) {
  75. $country = strtoupper($modifiers['flag']);
  76. if ($country) {
  77. $apiPost['country'] = $country;
  78. $apiPost['country_name'] = $modifiers['flag alt'];
  79. }
  80. }
  81. }
  82. return $apiPost;
  83. }
  84. function translateThread(Thread $thread) {
  85. $apiPosts = array();
  86. $op = $this->translatePost($thread);
  87. $op['resto'] = 0;
  88. $apiPosts['posts'][] = $op;
  89. foreach ($thread->posts as $p) {
  90. $apiPosts['posts'][] = $this->translatePost($p);
  91. }
  92. return $apiPosts;
  93. }
  94. function translatePage(array $threads) {
  95. $apiPage = array();
  96. foreach ($threads as $thread) {
  97. $apiPage['threads'][] = $this->translateThread($thread);
  98. }
  99. return $apiPage;
  100. }
  101. function translateCatalogPage(array $threads) {
  102. $apiPage = array();
  103. foreach ($threads as $thread) {
  104. $ts = $this->translateThread($thread);
  105. $apiPage['threads'][] = current($ts['posts']);
  106. }
  107. return $apiPage;
  108. }
  109. function translateCatalog($catalog) {
  110. $apiCatalog = array();
  111. foreach ($catalog as $page => $threads) {
  112. $apiPage = $this->translateCatalogPage($threads);
  113. $apiPage['page'] = $page;
  114. $apiCatalog[] = $apiPage;
  115. }
  116. return $apiCatalog;
  117. }
  118. }