Guest User

Untitled

a guest
Nov 6th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 79.27 KB | None | 0 0
  1. function LibFictofile_Parse_Archiveofourown($content, $fic)
  2. {
  3. // Проверяю на 404
  4. if (mb_strpos($content, 'Error 404') !== false)
  5. {
  6. return array('error'=>'404');
  7. }
  8.  
  9. // Достаю название и шапку
  10. if(!preg_match('/<dl class="work meta group" role="complementary">(.*?)<\/dl>.*?<h2 class="title heading">(.*?)<\/h2>.*?<h3 class="byline heading">(.*?)<\/h3>.*?<blockquote class="userstuff">(.*?)<\/p>[^<]*?<\/blockquote>/ius', $content, $matches))
  11. {
  12. if(!preg_match('/<dl class="work meta group" role="complementary">(.*?)<\/dl>.*?<h2 class="title heading">(.*?)<\/h2>.*?<h3 class="byline heading">(.*?)<\/h3>/ius', $content, $matches))
  13. {
  14. return array('error'=>'1');
  15. }
  16. else
  17. {
  18. $matches[4] = 'Нет описания';
  19. }
  20. }
  21.  
  22. $fic['head'] = $matches[1];
  23.  
  24. $fic['title'] = trim($matches[2]);
  25.  
  26. // автор
  27. $fic['authors'] = '';
  28. preg_match_all('/\/users\/([^\/]*?)\/[^>]*?>([^<]*?)</ius', $matches[3], $matches2, PREG_SET_ORDER);
  29. if(count($matches2) == 0)
  30. {
  31. return array('error'=>'1');
  32. }
  33. else
  34. {
  35. foreach($matches2 AS $tmp)
  36. {
  37. $fic['authors'] .= $tmp[2].', ';
  38. $fic['authors_info'][] = array('id' => $tmp[1], 'name' => $tmp[2]);
  39. }
  40. $fic['authors'] = mb_substr($fic['authors'], 0, -2);
  41. }
  42.  
  43. // саммари
  44. $fic['summary'] = strip_tags(str_replace('</p>', '<br/>', trim($matches[4])), '<br/>');
  45.  
  46. // фандомы
  47. $fic['fandoms'] = '';
  48. $fic['fandoms_arr'] = array();
  49. if(preg_match('/<dd class="fandom tags">[^<]*<ul class="commas">(.*?)<\/ul>/ius', $fic['head'], $matches))
  50. {
  51. preg_match_all('/href="([^"]*?)".*?>([^<]*?)<\/a>/ius', $matches[1], $matches3, PREG_SET_ORDER);
  52. foreach($matches3 AS $tmp)
  53. {
  54. $fic['fandoms'] .= trim($tmp[2]).', ';
  55. $fic['fandoms_arr'][$tmp[1]] = $tmp[2];
  56. }
  57. if($fic['fandoms'] != '')
  58. {
  59. $fic['fandoms'] = mb_substr($fic['fandoms'], 0, -2);
  60. }
  61. }
  62.  
  63. // пейринг и Персонажи
  64. $fic['paring'] = '';
  65. if(preg_match('/<dd class="relationship tags">[^<]*<ul class="commas">(.*?)<\/li>[^<]*?<\/ul>/ius', $fic['head'], $matches))
  66. {
  67. $fic['paring'] = trim(strip_tags(str_replace('</li>', ', ', trim($matches[1]))));
  68. }
  69. if(preg_match('/<dd class="character tags">[^<]*<ul class="commas">(.*?)<\/li>[^<]*?<\/ul>/ius', $fic['head'], $matches))
  70. {
  71. if($fic['paring'] == '')
  72. {
  73. $fic['paring'] = trim(strip_tags(str_replace('</li>', ', ', trim($matches[1]))));
  74. }
  75. else
  76. {
  77. $characters = '';
  78. preg_match_all('/href="[^"]*?".*?>([^<]*?)<\/a>/ius', $matches[1], $matches4, PREG_SET_ORDER);
  79. foreach ($matches4 AS $tmp)
  80. {
  81. if(strpos($fic['paring'], $tmp[1]) === false)
  82. {
  83. $characters .= ', '.$tmp[1];
  84. }
  85. }
  86.  
  87. $fic['paring'] .= $characters;
  88. }
  89. }
  90. if($fic['paring'] != '')
  91. {
  92. $paring = '<b>Персонажи:</b> '.$fic['paring'].'<br/><br/>';
  93. }
  94.  
  95. // рейтинг
  96. $fic['rating'] = '';
  97. if(preg_match('/<dd class="rating tags">[^<]*<ul class="commas">[^<]*<li><a.*?href="[^"]*".*?>(.*?)<\/a><\/li>/ius', $fic['head'], $matches))
  98. {
  99. $tmp = trim(strip_tags($matches[1]));
  100. switch($tmp)
  101. {
  102. case 'Not Rated':
  103. $fic['rating'] = '';
  104. break;
  105. case 'General Audiences':
  106. $fic['rating'] = 'General';
  107. break;
  108. case 'Teen And Up Audiences':
  109. $fic['rating'] = 'PG-13';
  110. break;
  111. case 'Mature':
  112. $fic['rating'] = 'R';
  113. break;
  114. case 'Explicit':
  115. $fic['rating'] = 'NC-17';
  116. break;
  117. }
  118. }
  119. if($fic['rating'] != '')
  120. {
  121. $rating = '<b>Рейтинг:</b> '.$fic['rating'].'<br/><br/>';
  122. }
  123.  
  124. //категория
  125. $fic['het_slash'] = 0;
  126. if(preg_match('/<dd class="category tags">[^<]*<ul class="commas">(.*?)<\/ul>/ius', $fic['head'], $matches))
  127. {
  128. if(strpos($matches[1], 'Other') !== false || strpos($matches[1], 'Multi') !== false || strpos($matches[1], '</li><li>') !== false)
  129. {
  130. $fic['het_slash'] = 5;
  131. $het_slash = 'Смешанная';
  132. }
  133. else
  134. {
  135. $tmp = trim(strip_tags($matches[1]));
  136.  
  137. switch ($tmp)
  138. {
  139. case 'F/M':
  140. $fic['het_slash'] = 1;
  141. $het_slash = 'Гет';
  142. break;
  143. case 'M/M':
  144. $fic['het_slash'] = 2;
  145. $het_slash = 'Слэш';
  146. break;
  147. case 'Gen':
  148. $fic['het_slash'] = 3;
  149. $het_slash = 'Джен';
  150. break;
  151. case 'F/F':
  152. $fic['het_slash'] = 4;
  153. $het_slash = 'Фемслэш';
  154. break;
  155. }
  156. }
  157. }
  158. if($fic['het_slash'] != 0)
  159. {
  160. $het_slash = '<b>Категория:</b> '.$het_slash.'<br/><br/>';
  161. }
  162.  
  163. // метки
  164. $fic['keyword'] = '';
  165. if(preg_match('/<dd class="freeform tags">[^<]*<ul class="commas">(.*?)<\/li>[^<]*?<\/ul>/ius', $fic['head'], $matches))
  166. {
  167. $fic['keyword'] = trim(strip_tags(str_replace('</li>', ', ', trim($matches[1]))));
  168. }
  169. if($fic['keyword'] != '')
  170. {
  171. $keywords = '<b>Метки:</b> '.$fic['keyword'].'<br/><br/>';
  172. }
  173.  
  174. $fic['publ_permission'] = 0;
  175. if(mb_strpos($fic['keyword'], 'Not copy to another site') !== false || mb_strpos($fic['keyword'], 'Don&#39;t copy to another site') !== false || mb_strpos($fic['keyword'], 'Don’t copy to another site') !== false)
  176. {
  177. $fic['publ_permission'] = 2;
  178. }
  179.  
  180. // язык
  181. $fic['language'] = '';
  182. $fic['not_russian'] = '0';
  183. if(preg_match('/<dd class="language">(.*?)<\/dd>/ius', $fic['head'], $matches))
  184. {
  185. $fic['language'] = trim($matches[1]);
  186.  
  187. if($fic['language'] != 'Русский')
  188. {
  189. $fic['not_russian'] = '1';
  190. }
  191. }
  192. if($fic['language'] != '')
  193. {
  194. $language = '<b>Язык:</b> '.$fic['language'].'<br/><br/>';
  195. }
  196.  
  197. // даты
  198. if(preg_match('/<dd class="published">(\d{4}-\d{2}-\d{2})<\/dd>/ius', $fic['head'], $matches))
  199. {
  200. $fic['date_first'] = strtotime($matches[1]);
  201. }
  202. if(preg_match('/<dd class="status">(\d{4}-\d{2}-\d{2})<\/dd>/ius', $fic['head'], $matches))
  203. {
  204. $fic['date_last'] = strtotime($matches[1]);
  205. }
  206. else
  207. {
  208. $fic['date_last'] = $fic['date_first'];
  209. }
  210.  
  211. // статус
  212. $fic['status'] = 2;
  213. if(preg_match('/<dd class="chapters">(\d*)\/(.*?)<\/dd>/ius', $fic['head'], $matches))
  214. {
  215. if($matches[2] == '?')
  216. {
  217. $status = 'В процессе написания';
  218. $fic['status'] = 2;
  219. }
  220. elseif($matches[1] == $matches[2])
  221. {
  222. $status = 'Закончен';
  223. $fic['status'] = 1;
  224. }
  225. else
  226. {
  227. $status = 'В процессе написания';
  228. $fic['status'] = 2;
  229. }
  230. $status = '<b>Статус:</b> '.$status.'<br/><br/>';
  231. }
  232.  
  233. // перевод
  234. $fic['is_translation'] = 0;
  235. $fic['original_author'] = '';
  236. $fic['original_url'] = '';
  237. if(preg_match('/<li>.*?A translation of.*?<a href="([^"]*?)">[^<]*?<\/a> by <a.*?>([^<]*?)<\/a>/ius', $content, $matches))
  238. {
  239. $fic['is_translation'] = 1;
  240. $fic['original_author'] = $matches[2];
  241. $fic['original_url'] = $fic['domen'].$matches[1];
  242. }
  243.  
  244. // для обнуления старых предупреждений, куда писалась категория
  245. $fic['warning'] = '';
  246.  
  247. // шапка
  248. $fic['head'] = '<b>Ссылка:</b> '.$fic['pagetosave'].$fic['default_id'].'<br/><br/><b>Автор:</b> '.$fic['authors'].'<br/><br/><b>Фандом:</b> '.$fic['fandoms'].'<br/><br/>'.$paring.$rating.$het_slash.$keywords.$language.$status.'<b>Аннотация:</b> '.$fic['summary'];
  249.  
  250. // примечание
  251. if(preg_match('/<h3 class="heading">Summary:<\/h3>\s*<blockquote class="userstuff">.*?<\/blockquote>\s*<\/div>\s*<div class="notes module" role="complementary">\s*<h3 class="heading">Notes:<\/h3>\s*<blockquote class="userstuff">(.*?)<\/blockquote>/ius', $content, $matches))
  252. {
  253. $fic['author_comment'] = strip_tags(str_replace('</p>', '<br/>', trim($matches[1])), '<br/>');
  254. }
  255.  
  256. // удаляем выравненные по центру изображения
  257. $content = preg_replace('/<div[^>]*>[^<]*<p>[^<]*<img[^>]*>[^<]*<\/p>[^<]*<\/div>/ius', '', $content);
  258. $content = str_replace('<h3 class="heading">Notes:</h3>', '<br/>Notes:<br/>', $content);
  259. $content = str_replace('<h3 class="landmark heading" id="work">Chapter Text</h3>', '', $content);
  260. $content = preg_replace('/<div align="center">[^<]*<p>\*\*\*<\/p>[^<]*<\/div>/ius', '* * *', $content);
  261. // замена h2 и списков
  262. $content = preg_replace('/<[ou]l>(.*?)<\/[ou]l>/ius', '$1', $content);
  263. $content = preg_replace('/<h2>(.*?)<\/h2>/ius', '<p><b>$1</b></p>', $content);
  264. $content = preg_replace('/<li>(.*?)<\/li>/ius', '<p>$1</p>', $content);
  265.  
  266. // достаю главы
  267. if (preg_match_all('/<h3 class="title">(.*?)<\/h3>/ius', $content, $tmp3, PREG_SET_ORDER))
  268. {
  269. $tmp = explode('<!--main content-->', $content);
  270. $count = count($tmp);
  271. $j = 0;
  272. for($i=1; $i < $count; $i++)
  273. {
  274. $tmp2 = explode('<!--/main-->', $tmp[$i]);
  275. $chapters[$j][1] = $tmp3[$j][1];
  276. $chapters[$j][2] = $tmp2[0];
  277. $j++;
  278. }
  279. }
  280. else
  281. {
  282. if (!preg_match_all('/<!-- chapter management -->.*?<h3 class="title">(.*?)<\/h3>.*?<!--main content-->(.*?)<!--\/main-->/ius', $content, $chapters, PREG_SET_ORDER))
  283. {
  284. //echo "1 no\r\n";
  285. if (!preg_match_all('/<h2 class="title heading">(.*?)<\/h2>.*?<!--chapter content-->(.*?)<!--\/chapter-->/ius', $content, $chapters, PREG_SET_ORDER))
  286. {
  287. //echo "2 no\r\n";
  288. // убираю тэг выравнивания по центру, который сбивает следующий шаблон
  289. $content = preg_replace('/<div align="\w+">(.*?)<\/div>/ius', '$1', $content);
  290. if (!preg_match_all('/<h2 class="title heading">(.*?)<\/h2>.*?<div id="chapters" role="article">(.*?)<\/div>/ius', $content, $chapters, PREG_SET_ORDER))
  291. {
  292. //echo "3 no\r\n";
  293. $tmp = explode('<div id="chapters" role="article">', $content);
  294. $tmp = explode('</div>', $tmp[1]);
  295. $chapters[0][2] = $tmp[0];
  296. $chapters[0][1] = $fic['title'];
  297. }
  298. }
  299. }
  300. }
  301.  
  302. $fic['chapters_count'] = count($chapters);
  303. $fic['length_kb'] = 0;
  304. $fic['length_words'] = 0;
  305.  
  306. for($i=0; $i < $fic['chapters_count']; $i++)
  307. {
  308. $fic['chapter_'.($i+1)]['title'] = trim(strip_tags($chapters[$i][1]));
  309. $fic['chapter_'.($i+1)]['text'] = trim($chapters[$i][2]);
  310. $fic['chapter_'.($i+1)]['text'] = str_replace(array('<blockquote>', '</blockquote>'), '', $fic['chapter_'.($i+1)]['text']);
  311. $fic['chapter_'.($i+1)]['text'] = preg_replace('/[\s]*?<\/p>[^<]*<p[^>]*>[\s]*?/ius', '<br/>', $fic['chapter_'.($i+1)]['text']);
  312. $fic['chapter_'.($i+1)]['text'] = str_replace('<em>', '<i>', $fic['chapter_'.($i+1)]['text']);
  313. $fic['chapter_'.($i+1)]['text'] = str_replace('</em>', '</i>', $fic['chapter_'.($i+1)]['text']);
  314. $fic['chapter_'.($i+1)]['text'] = str_replace('<strong>', '<b>', $fic['chapter_'.($i+1)]['text']);
  315. $fic['chapter_'.($i+1)]['text'] = str_replace('</strong>', '</b>', $fic['chapter_'.($i+1)]['text']);
  316. $fic['chapter_'.($i+1)]['text'] = str_replace("\r", '', $fic['chapter_'.($i+1)]['text']);
  317. $fic['chapter_'.($i+1)]['text'] = str_replace("\n", ' ', $fic['chapter_'.($i+1)]['text']);
  318. $fic['chapter_'.($i+1)]['text'] = str_replace('&ndash;', '-', $fic['chapter_'.($i+1)]['text']);
  319. $fic['chapter_'.($i+1)]['text'] = strip_tags($fic['chapter_'.($i+1)]['text'], '<br/><b><i><u><center><s><hr/>');
  320. $fic['chapter_'.($i+1)]['text'] = trim(preg_replace('/^Chapter Text/ius', '', $fic['chapter_'.($i+1)]['text']));
  321. $fic['chapter_'.($i+1)]['text'] = trim(preg_replace('/^Work Text:/ius', '', $fic['chapter_'.($i+1)]['text']));
  322.  
  323. $fic['length_kb'] += mb_strlen($fic['chapter_'.($i+1)]['text']);
  324. $fic['length_words'] += LibStr_CountWords($fic['chapter_'.($i+1)]['text']);
  325. }
  326. $fic['chapters_count'] += 1;
  327.  
  328. $fic['length_kb'] = ceil($fic['length_kb']/1000);
  329.  
  330. return $fic;
  331. }
  332.  
  333. // Парсинг страницы с fanfiction.net
  334. function LibFictofile_Parse_Fanfiction($content, $fic)
  335. {
  336. // Проверяю на 404
  337. if(mb_strpos($content, 'Unable to locate story. Code 1.') !== false)
  338. {
  339. return array('error'=>'404');
  340. }
  341.  
  342. // Достаю название и шапку
  343. if(preg_match('/<a class=xcontrast_txt[\s]+href=\"(.*?)\">(.*?)<\/a>.*?<b class=\'xcontrast_txt\'>([^<]*?)<\/b>.*?<a class=\'xcontrast_txt\' href=\'\/u\/([\d]{1,11})\/.*?\'>(.*?)<\/a>.*?<div style=\'margin-top:2px\' class=\'xcontrast_txt\'>(.*?)<\/div>.*?Rated:[^>]*?>(.*?)<\/a>\s+-(.+?)-(.*?)<\/div>/ius', $content, $matches))
  344. {
  345. $fic['authors'] = trim($matches[5]);
  346. $fic['authors_info'][] = array('id' => $matches[4], 'name' => $fic['authors']);
  347.  
  348. $fic['title'] = trim($matches[3]);
  349. $fic['summary'] = trim($matches[6]);
  350.  
  351. $fic['fandoms'] = trim($matches[2]);
  352. if(mb_strpos($matches[2], 'Crossover') !== false)
  353. {
  354. preg_match('/\/(.*?)_and_(.*?)_crossovers/ius', $matches[1], $fandoms_urls);
  355. preg_match('/(.*?) \+ (.*?) Crossover/ius', $matches[2], $fandoms_titles);
  356.  
  357. if(count($fandoms_urls) == 3 && count($fandoms_titles) == 3)
  358. {
  359. $fic['fandoms_arr'][$fandoms_urls[1]] = $fandoms_titles[1];
  360. $fic['fandoms_arr'][$fandoms_urls[2]] = $fandoms_titles[2];
  361. }
  362. }
  363. else
  364. {
  365. $fic['fandoms_arr'][$matches[1]] = trim($matches[2]);
  366. }
  367.  
  368. $fic['rating'] = '';
  369. switch($matches[7])
  370. {
  371. case 'Fiction K':
  372. case 'Fiction K+':
  373. $fic['rating'] = 'General';
  374. break;
  375. case 'Fiction T':
  376. $fic['rating'] = 'PG-13';
  377. break;
  378. case 'Fiction M':
  379. $fic['rating'] = 'R';
  380. break;
  381. }
  382. if($fic['rating'] !== '')
  383. {
  384. $rating = '<b>Рейтинг:</b> '.$fic['rating'].'<br/><br/>';
  385. }
  386.  
  387. $fic['genre'] = '';
  388. $fic['paring'] = '';
  389. $tmp = explode('- Chapters:', $matches[9]);
  390. $tmp = explode('- Words:', $tmp[0]);
  391. $tmp = explode(' - ', $tmp[0]);
  392. $maybe_count = count($tmp);
  393. if($maybe_count == 2)
  394. {
  395. $fic['genre'] = trim($tmp[0]);
  396. $fic['paring'] = trim($tmp[1]);
  397. }
  398. elseif($maybe_count == 1)
  399. {
  400. if(mb_strpos($tmp[0], '/') !== false)
  401. {
  402. $fic['genre'] = trim($tmp[0]);
  403. }
  404. elseif(mb_strpos($tmp[0], '.') !== false || mb_strpos($tmp[0], ',') !== false)
  405. {
  406. $fic['paring'] = trim($tmp[0]);
  407. }
  408. else
  409. {
  410. $genres = LibFictofile_GenreListArray_Fanfiction();
  411. foreach($genres AS $genre)
  412. {
  413. if(mb_strpos($tmp[0], $genre) !== false)
  414. {
  415. $fic['genre'] = trim($tmp[0]);
  416. continue;
  417. }
  418. }
  419.  
  420. if($fic['genre'] == '')
  421. {
  422. $fic['paring'] = trim($tmp[0]);
  423. }
  424. }
  425. }
  426. if($fic['paring'] !== '')
  427. {
  428. $paring = '<b>Персонажи:</b> '.$fic['paring'].'<br/><br/>';
  429. }
  430. if($fic['genre'] !== '')
  431. {
  432. $genre = '<b>Жанр:</b> '.$fic['genre'].'<br/><br/>';
  433. }
  434.  
  435. $size = '';
  436. if(preg_match('/Words:\s+([0-9,]*?)\s+-/ius', $matches[9], $matches2))
  437. {
  438. $size = '<b>Размер:</b> '.declension(str_replace(',', '', $matches2[1]), 'слово,слова,слов').'<br/><br/>';
  439. }
  440.  
  441. $status = '<b>Статус:</b> В процессе написания<br/><br/>';
  442. $fic['status'] = '2';
  443. if(mb_strpos($matches[9], 'Complete'))
  444. {
  445. $status = '<b>Статус:</b> Закончен<br/><br/>';
  446. $fic['status'] = '1';
  447. }
  448.  
  449. $fic['language'] = trim($matches[8]);
  450. $fic['not_russian'] = '0';
  451. if($fic['language'] != 'Russian')
  452. {
  453. $fic['not_russian'] = '1';
  454. }
  455. $language = '<b>Язык:</b> '.$fic['language'].'<br/><br/>';
  456.  
  457. if(preg_match('/Published: <span data-xutime=\'(\d{9,10})\'>/ius', $matches[9], $matches2))
  458. {
  459. $fic['date_first'] = $matches2[1];
  460. }
  461. if(preg_match('/Updated: <span data-xutime=\'(\d{9,10})\'>/ius', $matches[9], $matches2))
  462. {
  463. $fic['date_last'] = $matches2[1];
  464. }
  465. else
  466. {
  467. $fic['date_last'] = $fic['date_first'];
  468. }
  469.  
  470. $fic['head'] = '<b>Ссылка:</b> '.$fic['pagetosave'].$fic['default_id'].'<br/><br/><b>Автор:</b> '.$fic['authors'].'<br/><br/><b>Фандом:</b> '.$matches[2].'<br/><br/>'.$paring.$rating.$genre.$language.$status.$size.'<b>Аннотация:</b> '.$fic['summary'];
  471. $fic['head'] = str_replace("\r", '', $fic['head']);
  472. $fic['head'] = preg_replace("/[\n]+/u", "\n", $fic['head']);
  473. }
  474. else
  475. {
  476. return array('error'=>'1');
  477. }
  478.  
  479. // Достаю главы
  480. $fic['length_kb'] = 0;
  481. $fic['length_words'] = 0;
  482. $i = 1;
  483. $dom = new DOMDocument();
  484. @$dom->loadHTML($content);
  485. $xpath = new DOMXPath($dom);
  486. $hasChapters = $xpath->evaluate("boolean(//select[@id='chap_select'])");
  487. $story_chapters = $xpath->query("//select[@id='chap_select']/option");
  488. if ($hasChapters)
  489. {
  490. foreach ($story_chapters as $chapter)
  491. {
  492. $title = trim($chapter->nodeValue);
  493. if (mb_strpos($title, $i.'.') === 0)
  494. {
  495. $title = str_replace($i.'. ', '', $title);
  496. $fic['chapter_'.$i]['title'] = $title;
  497.  
  498. $content1 = LibFictofile_GetContent($fic['pagetosave'].$fic['default_id'].'/'.$i.'/', true);
  499. $content1 = LibFictofile_GetContent_Fanfiction_Unzip($content1);
  500. $tmp = explode("id='storytext'>", $content1);
  501. $tmp = explode('</div>', $tmp[1]);
  502. $innerHTML = $tmp[0];
  503.  
  504. $innerHTML = str_replace('<hr size=1 noshade>', '', $innerHTML);
  505. $innerHTML = preg_replace('/<p style=[\'"]text-align:center;[\'"]>(.*?)<\/p>/ius', '<br/><center>$1</center><br/>', $innerHTML);
  506. $innerHTML = str_replace('<br>', '<br/>', $innerHTML);
  507. $innerHTML = str_replace('<p>', '<br/>', $innerHTML);
  508. $innerHTML = str_replace('<P>', '<br/>', $innerHTML);
  509. $innerHTML = str_replace('<em>', '<i>', $innerHTML);
  510. $innerHTML = str_replace('</em>', '</i>', $innerHTML);
  511. $innerHTML = str_replace('<strong>', '<b>', $innerHTML);
  512. $innerHTML = str_replace('</strong>', '</b>', $innerHTML);
  513. $innerHTML = str_replace("\r", '', $innerHTML);
  514. $innerHTML = str_replace("\n", ' ', $innerHTML);
  515. $innerHTML = str_replace('&ndash;', '-', $innerHTML);
  516. $innerHTML = strip_tags($innerHTML, '<br/><b><i><u><center><s>');
  517.  
  518. $fic['chapter_'.$i]['text'] = $innerHTML;
  519.  
  520. $fic['length_kb'] += mb_strlen($fic['chapter_'.$i]['text']);
  521. $fic['length_words'] += LibStr_CountWords($fic['chapter_'.$i]['text']);
  522. }
  523. else
  524. {
  525. break;
  526. }
  527. $i++;
  528. }
  529. $fic['chapters_count'] = $i;
  530. }
  531. // Если одна глава
  532. else
  533. {
  534. $fic['chapter_1']['title'] = $fic['title'];
  535.  
  536. $tmp = explode("id='storytext'>", $content);
  537. $tmp = explode('</div>', $tmp[1]);
  538. $innerHTML = $tmp[0];
  539.  
  540. $innerHTML = str_replace('<hr size=1 noshade>', '', $innerHTML);
  541. $innerHTML = preg_replace('/<p style=[\'"]text-align:center;[\'"]>(.*?)<\/p>/ius', '<br/><center>$1</center><br/>', $innerHTML);
  542. $innerHTML = str_replace('<br>', '<br/>', $innerHTML);
  543. $innerHTML = str_replace('<p>', '<br/>', $innerHTML);
  544. $innerHTML = str_replace('<P>', '<br/>', $innerHTML);
  545. $innerHTML = str_replace('<em>', '<i>', $innerHTML);
  546. $innerHTML = str_replace('</em>', '</i>', $innerHTML);
  547. $innerHTML = str_replace('<strong>', '<b>', $innerHTML);
  548. $innerHTML = str_replace('</strong>', '</b>', $innerHTML);
  549. $innerHTML = str_replace("\r", '', $innerHTML);
  550. $innerHTML = str_replace("\n", ' ', $innerHTML);
  551. $innerHTML = str_replace('&ndash;', '-', $innerHTML);
  552. $innerHTML = strip_tags($innerHTML, '<br/><b><i><u><center><s>');
  553.  
  554. $fic['chapter_1']['text'] = $innerHTML;
  555.  
  556. $fic['chapters_count'] = 2;
  557. $fic['length_kb'] = mb_strlen($fic['chapter_1']['text']);
  558. $fic['length_words'] = LibStr_CountWords($fic['chapter_1']['text']);
  559. }
  560.  
  561. $fic['length_kb'] = ceil($fic['length_kb']/1024);
  562.  
  563. return $fic;
  564. }
  565.  
  566. // Парсинг страницы с fanfiktion.de
  567. function LibFictofile_Parse_Fanfiktionde($content, $fic)
  568. {
  569. // Проверяю на 404
  570. if(mb_strpos($content, '404 - Not Found') !== false)
  571. {
  572. return array('error'=>'404');
  573. }
  574.  
  575. // Достаю название и шапку
  576. if(preg_match('/Geschichte:(.*?)<\/span>.*?von <a href="\/u\/([^"]*?)".*?<\/span>(.*?)<\/a>.*?small-font center block">(.*?)<span.*?<\/span>(.*?)\/(.*?)<\/div>.*?<div class="block">(.*?)<\/div>.*?title="erstellt"><\/span>(.*?)<\/div>.*?title="aktualisiert"><\/span>(.*?)<\/div>.*?title="(in Arbeit|Fertiggestellt|Pausiert|Abgebrochen)".*?title="Kapitel"><\/span><span class="semibold">(\d+?)<\/span>/ius', $content, $matches))
  577. {
  578. if(preg_match_all('/<a\s+href="(.*?)"[^>]?>(.*?)<\/a>/ius', $matches[1], $matches2, PREG_SET_ORDER))
  579. {
  580. $count = count($matches2);
  581.  
  582. $fic['title'] = $matches2[$count-1][2];
  583. $full_default_url = $matches2[$count-1][1];
  584.  
  585. if(count($matches2) == 5 && strpos($matches2[$count-2][2], $matches2[$count-3][2]) === false)
  586. {
  587. $fic['fandoms'] = $matches2[$count-3][2].' - '.$matches2[$count-2][2];
  588. }
  589. else
  590. {
  591. $fic['fandoms'] = $matches2[$count - 2][2];
  592. }
  593. $fic['fandoms_arr'][$matches2[$count-2][1]] = $fic['fandoms'];
  594. }
  595. else
  596. {
  597. return array('error'=>'1');
  598. }
  599.  
  600. $fic['authors'] = trim($matches[3]);
  601. $fic['authors_info'][] = array('id' => $matches[2], 'name' => $fic['authors']);
  602.  
  603. $fic['rating'] = '';
  604. $fic['het_slash'] = 1;
  605. switch(trim($matches[6]))
  606. {
  607. case 'P6':
  608. $fic['rating'] = 'General';
  609. break;
  610. case 'P12':
  611. case 'P16':
  612. $fic['rating'] = 'PG-13';
  613. break;
  614. case 'P12 Slash':
  615. $fic['rating'] = 'PG-13';
  616. $fic['het_slash'] = 2;
  617. break;
  618. case 'P16 Slash':
  619. $fic['rating'] = 'R';
  620. $fic['het_slash'] = 2;
  621. break;
  622. case 'P18':
  623. case 'P18-AVL':
  624. $fic['rating'] = 'NC-17';
  625. break;
  626. case 'P18 Slash':
  627. case 'P18-AVL Slash':
  628. $fic['rating'] = 'NC-17';
  629. $fic['het_slash'] = 2;
  630. break;
  631. }
  632. if($fic['rating'] !== '')
  633. {
  634. $rating = '<b>Рейтинг:</b> '.$fic['rating'].'<br/><br/>';
  635. }
  636. $het_slash = ($fic['het_slash'] == 1)?'Гет':'Слэш';
  637. $het_slash = '<b>Категория:</b> '.$het_slash.'<br/><br/>';
  638.  
  639. $fic['genre'] = trim($matches[4]).', '.$matches[5];
  640. $genre = '<b>Жанр:</b> '.$fic['genre'].'<br/><br/>';
  641.  
  642. $fic['paring'] = str_replace("\n", ', ', str_replace("\r", '', trim(strip_tags($matches[7]))));
  643. if($fic['paring'] != '')
  644. {
  645. $paring = '<b>Персонажи:</b> '.$fic['paring'].'<br/><br/>';
  646. }
  647.  
  648. $fic['date_first'] = strtotime(preg_replace('/(\d{2})\.(\d{2})\.(\d{4})/', "$3-$2-$1", $matches[8]));
  649. $fic['date_last'] = strtotime(preg_replace('/(\d{2})\.(\d{2})\.(\d{4})/', "$3-$2-$1", $matches[9]));
  650.  
  651. $fic['status'] = '2';
  652. switch($matches[10])
  653. {
  654. case 'in Arbeit':
  655. $fic['status'] = '2';
  656. $status = 'В процессе';
  657. break;
  658. case 'Fertiggestellt':
  659. $fic['status'] = '1';
  660. $status = 'Закончен';
  661. break;
  662. case 'Pausiert':
  663. case 'Abgebrochen':
  664. $fic['status'] = '3';
  665. $status = 'Заморожен';
  666. break;
  667. }
  668. $status = '<b>Статус:</b> '.$status.'<br/><br/>';
  669.  
  670. $fic['chapters_count'] = $matches[11];
  671.  
  672. $content_summary = LibFictofile_GetContent($fic['domen'].'/?a=v&storyid='.$fic['default_id'].'&s=1', true);
  673. if(strpos($content_summary, '<div class="story-summary-ajax">') === false)
  674. {
  675. return array('error'=>'1');
  676. }
  677. $fic['summary'] = trim(strip_tags($content_summary));
  678.  
  679. $fic['head'] = '<b>Ссылка:</b> '.$fic['domen'].$full_default_url.'<br/><br/><b>Автор:</b> '.$fic['authors'].'<br/><br/><b>Фандом:</b> '.$fic['fandoms'].'<br/><br/>'.$paring.$het_slash.$rating.$genre.$status.'<b>Аннотация:</b> '.$fic['summary'];
  680. }
  681. else
  682. {
  683. return array('error'=>'1');
  684. }
  685.  
  686. // Достаю главы
  687. $fic['length_kb'] = 0;
  688. $fic['length_words'] = 0;
  689.  
  690. preg_match('/<option value="\d+" selected="selected">(.*?)<\/option>/ius', $content, $matches3);
  691. $fic['chapter_1']['title'] = trim($matches3[1]);
  692.  
  693. $tmp = explode('<div id="storytext">', $content);
  694. $tmp = explode('<div class="clearfloat">', $tmp[1]);
  695.  
  696. $text = $tmp[0];
  697. $text = preg_replace('/<!-- T\[style type="bold"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<b>$1</b>', $text);
  698. $text = preg_replace('/<!-- T\[style type="italic"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<i>$1</i>', $text);
  699. $text = preg_replace('/<!-- T\[style type="underlined"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<u>$1</u>', $text);
  700. $text = preg_replace('/<!-- T\[style type="(underlined,bold|bold,underlined)"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<u><b>$1</b></u>', $text);
  701. $text = preg_replace('/<!-- T\[style type="(underlined,italic|italic,underlined)"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<u><i>$1</i></u>', $text);
  702. $text = preg_replace('/<!-- T\[style type="(bold,italic|italic,bold)"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<b><i>$1</i></b>', $text);
  703. $text = preg_replace('/<!-- T\[style type="(bold,italic,underlined|italic,bold,underlined|underlined,italic,bold|underlined,bold,italic|bold,underlined,italic|italic,underlined,bold)"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<u><b><i>$1</i></b></u>', $text);
  704. $text = preg_replace('/<!-- T\[align type="center"\]T -->(.*?)<!-- T\[\/align\]T -->/ius', '<center>$1</center>', $text);
  705. $text = preg_replace('/<!-- T\[align type="right"\]T -->(.*?)<!-- T\[\/align\]T -->/ius', '<right>$1</right>', $text);
  706. $text = strip_tags($text, '<br/><br><br /><b><i><u><center><right>');
  707. $fic['chapter_1']['text'] = trim($text);
  708.  
  709. $fic['length_kb'] += mb_strlen($fic['chapter_1']['text']);
  710. $fic['length_words'] += LibStr_CountWords($fic['chapter_1']['text']);
  711.  
  712. if($fic['chapters_count'] > 1)
  713. {
  714. for($i = 2; $i <= $fic['chapters_count']; $i++)
  715. {
  716. $content = LibFictofile_GetContent($fic['domen'].str_replace('/1/', '/'.$i.'/', $full_default_url), true);
  717.  
  718. preg_match('/<option value="\d+" selected="selected">(.*?)<\/option>/ius', $content, $matches3);
  719. $fic['chapter_'.$i]['title'] = trim($matches3[1]);
  720.  
  721. $tmp = explode('<div id="storytext">', $content);
  722. $tmp = explode('<div class="clearfloat">', $tmp[1]);
  723.  
  724. $text = $tmp[0];
  725. $text = preg_replace('/<!-- T\[style type="bold"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<b>$1</b>', $text);
  726. $text = preg_replace('/<!-- T\[style type="italic"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<i>$1</i>', $text);
  727. $text = preg_replace('/<!-- T\[style type="underlined"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<u>$1</u>', $text);
  728. $text = preg_replace('/<!-- T\[style type="(underlined,bold|bold,underlined)"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<u><b>$1</b></u>', $text);
  729. $text = preg_replace('/<!-- T\[style type="(underlined,italic|italic,underlined)"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<u><i>$1</i></u>', $text);
  730. $text = preg_replace('/<!-- T\[style type="(bold,italic|italic,bold)"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<b><i>$1</i></b>', $text);
  731. $text = preg_replace('/<!-- T\[style type="(bold,italic,underlined|italic,bold,underlined|underlined,italic,bold|underlined,bold,italic|bold,underlined,italic|italic,underlined,bold)"\]T -->(.*?)<!-- T\[\/style\]T -->/ius', '<u><b><i>$1</i></b></u>', $text);
  732. $text = preg_replace('/<!-- T\[align type="center"\]T -->(.*?)<!-- T\[\/align\]T -->/ius', '<center>$1</center>', $text);
  733. $text = preg_replace('/<!-- T\[align type="right"\]T -->(.*?)<!-- T\[\/align\]T -->/ius', '<right>$1</right>', $text);
  734. $text = strip_tags($text, '<br/><br><br /><b><i><u><center><right>');
  735. $fic['chapter_'.$i]['text'] = trim($text);
  736.  
  737. $fic['length_kb'] += mb_strlen($fic['chapter_'.$i]['text']);
  738. $fic['length_words'] += LibStr_CountWords($fic['chapter_'.$i]['text']);
  739. }
  740. }
  741. $fic['chapters_count'] += 1;
  742. $fic['length_kb'] = ceil($fic['length_kb']/1024);
  743.  
  744. $fic['not_russian'] = '1';
  745.  
  746. return $fic;
  747. }
  748.  
  749. // Парсинг страницы с ficbook.net
  750. function LibFictofile_Parse_Ficbook($content, $fic)
  751. {
  752. // Проверяю на 404
  753. if(mb_strpos($content, '404 &mdash; Страница не найдена') !== false)
  754. {
  755. return array('error'=>'404');
  756. }
  757. if(mb_strpos($content, 'Работа удалена') !== false)
  758. {
  759. return array('error'=>'404');
  760. }
  761. if(mb_strpos($content, 'Access denied') !== false || $fic['length'] == 0)
  762. {
  763. return array('error'=>'2');
  764. }
  765.  
  766. // Достаю название и шапку
  767. preg_match('/<h1[^>]*>(.*?)<\/h1>(.*?)<hr\s*\/>/ius', $content, $matches);
  768. $fic['title'] = $matches[1];
  769. $fic['head'] = '<b>Ссылка:</b> '.strip_tags(trim(str_replace('<br />', '<br/>',str_replace('<br>', '<br/>',$matches[2]))), '<br/><b><i>');
  770. $fic['head'] = str_replace('&quot;', '"', $fic['head']);
  771. $fic['head'] = str_replace("\r", '', $fic['head']);
  772. $fic['head'] = preg_replace("/[\n]+/u", "\n", $fic['head']);
  773.  
  774. // Достаю направленность
  775. $fic['het_slash'] = 0;
  776. if(preg_match('/<b>Направленность:<\/b>([^<]*?)<br/ius', $fic['head'], $matches12))
  777. {
  778. switch(trim($matches12[1]))
  779. {
  780. case 'Гет': $fic['het_slash'] = 1; break;
  781. case 'Слэш': $fic['het_slash'] = 2; break;
  782. case 'Джен': $fic['het_slash'] = 3; break;
  783. case 'Фемслэш': $fic['het_slash'] = 4; break;
  784. case 'Смешанная': $fic['het_slash'] = 5; break;
  785. case 'Статья': $fic['het_slash'] = 6; break;
  786. }
  787. }
  788.  
  789. // Достаю саммари
  790. if(preg_match('/<b>Описание:<\/b>[^<]*<br\/>(.*?)<br\/><br\/>/ius', $fic['head'], $matches1))
  791. {
  792. $fic['summary'] = trim($matches1[1]);
  793. }
  794. elseif(preg_match('/<b>Описание:<\/b>[^<]*<br\/>(.*?)$/ius', $fic['head'], $matches1))
  795. {
  796. $fic['summary'] = preg_replace('/[\s?<br\/>]+$/u', '', trim($matches1[1]));
  797. }
  798.  
  799. // Достаю "от автора"
  800. if(preg_match('/<b>Примечания автора:<\/b>[^<]*<br\/>(.*?)(<b>|$)/ius', $fic['head'], $matches10))
  801. {
  802. $fic['author_comment'] = preg_replace('/[\s?<br\/>]+$/u', '', trim($matches10[1]));
  803. }
  804.  
  805. // Достаю автора или переводчика
  806. $fic['authors_info'] = array();
  807. $fic['is_translation'] = 0;
  808. $fic['original_author'] = '';
  809. $fic['original_url'] = '';
  810. $matches2 = false;
  811. if(preg_match('/<b>Переводчик:<\/b>(.*?)\(https:\/\/ficbook\.net\/authors\/(\d+)\)/ius', $fic['head'], $matches2))
  812. {
  813. $fic['is_translation'] = 1;
  814.  
  815. // автор оригинала
  816. if(preg_match('/<b>Автор:<\/b>(.*?)</ius', $fic['head'], $matches3))
  817. {
  818. $fic['original_author'] = trim($matches3[1]);
  819. }
  820. // ссылка на оригинал
  821. if(preg_match('/<b>Оригинальный текст:<\/b>(.*?)</ius', $fic['head'], $matches3))
  822. {
  823. $fic['original_url'] = trim($matches3[1]);
  824. }
  825. }
  826. else
  827. {
  828. preg_match('/<b>Автор:<\/b>(.*?)\(https:\/\/ficbook\.net\/authors\/(\d+)\)/ius', $fic['head'], $matches2);
  829. }
  830. if($matches2)
  831. {
  832. $fic['authors'] = trim(strip_tags($matches2[1]));
  833.  
  834. $fic['authors_info'][] = array('id' => $matches2[2], 'name' => $fic['authors']);
  835. }
  836.  
  837. // Достаю соавторов
  838. if(preg_match('/<b>Соавторы:<\/b>(.*?)</ius', $fic['head'], $matches2))
  839. {
  840. preg_match_all('/([^(]*?)\(https:\/\/ficbook\.net\/authors\/(\d+)\)/ius', $matches2[1], $matches2, PREG_SET_ORDER);
  841. foreach($matches2 AS $matches_tmp)
  842. {
  843. $author_name = trim(strip_tags($matches_tmp[1]));
  844. if (strpos($author_name, ',') === 0)
  845. {
  846. $author_name = trim(mb_substr($author_name, 1));
  847. }
  848.  
  849. $fic['authors'] .= ', ' . $author_name;
  850.  
  851. $fic['authors_info'][] = array('id' => $matches_tmp[2], 'name' => $author_name);
  852. }
  853. }
  854.  
  855. // Достаю фандомы
  856. if(preg_match('/<b>Фэндом:<\/b>(.*?)<br\/>/ius', $fic['head'], $matches3))
  857. {
  858. $fic['fandoms'] = preg_replace('/\s+/u', ' ', trim($matches3[1]));
  859. }
  860.  
  861. // Достаю Персонажей
  862. if(preg_match('/<b>Пейринг или персонажи:<\/b>(.*?)<br\/>/ius', $fic['head'], $matches4))
  863. {
  864. $fic['paring'] = trim($matches4[1]);
  865. }
  866.  
  867. // Достаю рейтинг
  868. if(preg_match('/<b>Рейтинг:<\/b>(.*?)<br\/>/ius', $fic['head'], $matches5))
  869. {
  870. $fic['rating'] = trim($matches5[1]);
  871. }
  872. /*
  873. // Достаю жанр
  874. if(preg_match('/<b>Жанры:<\/b>(.*?)<br\/>/ius', $fic['head'], $matches6))
  875. {
  876. $fic['genre'] = preg_replace('/\s+/u', ' ', trim($matches6[1]));
  877. }
  878.  
  879. // Достаю предупреждения
  880. if(preg_match('/<b>Предупреждения:<\/b>(.*?)<br\/>/ius', $fic['head'], $matches7))
  881. {
  882. $fic['warning'] = preg_replace('/\s+/u', ' ', trim($matches7[1]));
  883. }
  884. */
  885.  
  886. // Достаю метки
  887. if(preg_match('/<b>Метки:<\/b>(.*?)<br\/>/ius', $fic['head'], $matches12))
  888. {
  889. $fic['keyword'] = preg_replace('/\s+/u', ' ', trim($matches12[1]));
  890. }
  891.  
  892. // выделяю жанры и предупреждения
  893. $genres = LibFictofile_GenreListArray_Ficbook();
  894. $warnings = LibFictofile_WarningListArray_Ficbook();
  895. $this_keywords = explode(',', $fic['keyword']);
  896. $fic['keyword'] = array();
  897. $fic['genre'] = array();
  898. $fic['warning'] = array();
  899. foreach($this_keywords AS $tmp)
  900. {
  901. if(in_array(trim($tmp), $genres))
  902. {
  903. $fic['genre'][] = $tmp;
  904. }
  905. elseif(in_array(trim($tmp), $warnings))
  906. {
  907. $fic['warning'][] = $tmp;
  908. }
  909. else
  910. {
  911. $fic['keyword'][] = $tmp;
  912. }
  913. }
  914. $fic['genre'] = implode(', ', $fic['genre']);
  915. $fic['warning'] = implode(', ', $fic['warning']);
  916. $fic['keyword'] = implode(', ', $fic['keyword']);
  917.  
  918. // Достаю размер
  919. if(preg_match('/<b>Размер:<\/b>(.*?),(.*?)<br\/>/ius', $fic['head'], $matches8))
  920. {
  921. $fic['size'] = trim($matches8[1]);
  922. switch($fic['size'])
  923. {
  924. case 'Мини': $fic['size'] = '1'; break;
  925. case 'Драббл': $fic['size'] = '1'; break;
  926. case 'Миди': $fic['size'] = '2'; break;
  927. case 'Макси': $fic['size'] = '3'; break;
  928. case 'планируется Мини': $fic['size'] = '1'; break;
  929. case 'планируется Драббл': $fic['size'] = '1'; break;
  930. case 'планируется Миди': $fic['size'] = '2'; break;
  931. case 'планируется Макси': $fic['size'] = '3'; break;
  932. }
  933. }
  934.  
  935. // Достаю статус
  936. if(preg_match('/<b>Статус:<\/b>(.*?)<br\/>/ius', $fic['head'], $matches9))
  937. {
  938. $fic['status'] = trim($matches9[1]);
  939. switch($fic['status'])
  940. {
  941. case 'закончен': $fic['status'] = '1'; break;
  942. case 'в процессе': $fic['status'] = '2'; break;
  943. case 'заморожен': $fic['status'] = '3'; break;
  944. }
  945. }
  946.  
  947. // Достаю количество частей
  948. preg_match('/<b>Кол-во частей:<\/b>.*?(\d+)/ius', $fic['head'], $matches2);
  949. $count_chapters = intval($matches2[1]);
  950.  
  951. // Достаю разрешение на публикацию
  952. $fic['publ_permission'] = 0;
  953. if(preg_match('/<b>Публикация на других ресурсах:<\/b>[^<]*<br\/>(.*?)(<br\/>|$)/ius', $fic['head'], $matches11))
  954. {
  955. $fic['publ_permission'] = trim($matches11[1]);
  956. switch($fic['publ_permission'])
  957. {
  958. case 'Уточнять у автора/переводчика': $fic['publ_permission'] = '1'; break;
  959. case 'Запрещено в любом виде': $fic['publ_permission'] = '2'; break;
  960. case 'Разрешено в любом виде': $fic['publ_permission'] = '3'; break;
  961. case 'Разрешено только в виде ссылки': $fic['publ_permission'] = '4'; break;
  962. case 'Разрешено копирование текста с указанием автора/переводчика и ссылки на исходную публикацию': $fic['publ_permission'] = '5'; break;
  963. }
  964. }
  965.  
  966. // Достаю главы
  967. // Если одна глава
  968. if($count_chapters == 1)
  969. {
  970. // пытаюсь достать единственную главу
  971. if(preg_match('/<div style="white-space: pre-wrap">(.*?)<hr>/ius', $content, $matches))
  972. {
  973. $fic['chapter_1']['title'] = $fic['title'];
  974. $fic['chapter_1']['text'] = LibFictofile_Parse_Ficbook_TextEdit($matches[1]);
  975. $fic['chapters_count'] = 2; // глава одна, но для цикла в ChaptersToBd надо ставить двойку
  976. $fic['length_kb'] = mb_strlen($fic['chapter_1']['text']);
  977. $fic['length_words'] = LibStr_CountWords($fic['chapter_1']['text']);
  978. }
  979. else
  980. {
  981. $tmp = explode('<div style="white-space: pre-wrap">', $content);
  982. $tmp = explode('<hr>', $tmp[2]);
  983. $fic['chapter_1']['title'] = $fic['title'];
  984. $fic['chapter_1']['text'] = LibFictofile_Parse_Ficbook_TextEdit($tmp[0]);
  985. $fic['chapters_count'] = 2; // глава одна, но для цикла в ChaptersToBd надо ставить двойку
  986. $fic['length_kb'] = mb_strlen($fic['chapter_1']['text']);
  987. $fic['length_words'] = LibStr_CountWords($fic['chapter_1']['text']);
  988. }
  989.  
  990. // Достаю даты
  991. $fic['date_last'] = 0;
  992. $fic['date_first'] = 0;
  993. $fic['chapter_1']['date'] = 0;
  994.  
  995. $content = LibFictofile_GetContent('https://ficbook.net/readfic/'.$fic['default_id'], true, true);
  996.  
  997. if(preg_match('/<span title="(\d{1,2} [а-я]{3,9} \d{4}, \d{2}:\d{2}?)">/ius', $content, $matches))
  998. {
  999. $month_ru = array(' января ', ' февраля ', ' марта ', ' апреля ', ' мая ', ' июня ', ' июля ', ' августа ', ' сентября ', ' октября ', ' ноября ', ' декабря ');
  1000. $month_en = array('-01-', '-02-', '-03-', '-04-', '-05-', '-06-', '-07-', '-08-', '-09-', '-10-', '-11-', '-12-');
  1001. $fic['date_first'] = strtotime(str_replace($month_ru, $month_en, str_replace(',', '', $matches[1])));
  1002. $fic['date_last'] = $fic['date_first'];
  1003. $fic['chapter_1']['date'] = $fic['date_first'];
  1004. }
  1005.  
  1006. // достаю фандомы
  1007. if(preg_match('/<dd>[\s]+<i class="icon-book"><\/i>[\s]+(.*?)<\/dd>/ius', $content, $matches))
  1008. {
  1009. if(preg_match_all('/<a href="\/fanfiction\/(.*?)">(.*?)<\/a>/ius', $matches[1], $matches1, PREG_SET_ORDER))
  1010. {
  1011. $count = count($matches1);
  1012. for($i=0;$i<$count;$i++)
  1013. {
  1014. if($matches1[$i][2] != '') $fic['fandoms_arr'][$matches1[$i][1]] = $matches1[$i][2];
  1015. }
  1016. }
  1017. }
  1018. }
  1019. else
  1020. {
  1021. $fic['length_kb'] = 0;
  1022. $fic['length_words'] = 0;
  1023.  
  1024. $content = str_replace('<hr>', '<hr/>', $content);
  1025. $tmp = explode('<hr/>', $content);
  1026. $tmp = explode('<h2>', $tmp[1]);
  1027. $i = 0;
  1028. foreach ($tmp as $tmp3)
  1029. {
  1030. $tmp2 = explode('</h2>', $tmp3);
  1031.  
  1032. if (count($tmp2) == 1)
  1033. {
  1034. continue;
  1035. }
  1036.  
  1037. if ($tmp2[0] == 'Примечание к части')
  1038. {
  1039. if ($i == 0)
  1040. {
  1041. $fic['chapter_1']['text'] .= '<b>Примечание к части</b><br/>'.LibFictofile_Parse_Ficbook_TextEdit($tmp2[1]).'<br/><br/>';
  1042. }
  1043. else
  1044. {
  1045. $fic['chapter_'.$i]['text'] .= '<br/><br/><b>Примечание к части</b><br/>'.LibFictofile_Parse_Ficbook_TextEdit($tmp2[1]);
  1046. }
  1047. }
  1048. else
  1049. {
  1050. $i++;
  1051. $fic['chapter_'.$i]['title'] = trim(strip_tags($tmp2[0]));
  1052. $fic['chapter_'.$i]['text'] .= LibFictofile_Parse_Ficbook_TextEdit($tmp2[1]);
  1053.  
  1054. $fic['length_kb'] += mb_strlen($fic['chapter_'.$i]['text']);
  1055. $fic['length_words'] += LibStr_CountWords($fic['chapter_'.$i]['text']);
  1056. }
  1057. }
  1058. $fic['chapters_count'] = $i+1;
  1059.  
  1060. // Достаю даты
  1061. $fic['date_last'] = 0;
  1062. $fic['date_first'] = 0;
  1063.  
  1064. $content = LibFictofile_GetContent('https://ficbook.net/readfic/'.$fic['default_id'], true, true);
  1065. $tmp = explode('<a name="part_content"></a>', $content);
  1066. $tmp = explode('</ul>', $tmp[1]);
  1067.  
  1068. if(preg_match_all('/<span title="(\d{1,2} [а-я]{3,9} \d{4}, \d{2}:\d{2}?)">/ius', $tmp[0], $matches))
  1069. {
  1070. $month_ru = array(' января ', ' февраля ', ' марта ', ' апреля ', ' мая ', ' июня ', ' июля ', ' августа ', ' сентября ', ' октября ', ' ноября ', ' декабря ');
  1071. $month_en = array('-01-', '-02-', '-03-', '-04-', '-05-', '-06-', '-07-', '-08-', '-09-', '-10-', '-11-', '-12-');
  1072. $fic['date_first'] = strtotime(str_replace($month_ru, $month_en, str_replace(',', '', $matches[1][0])));
  1073. $fic['date_last'] = strtotime(str_replace($month_ru, $month_en, str_replace(',', '', $matches[1][count($matches[1])-1])));
  1074. for($i = 1; $i < $fic['chapters_count']; $i++)
  1075. {
  1076. $fic['chapter_'.$i]['date'] = strtotime(str_replace($month_ru, $month_en, str_replace(',', '', $matches[1][$i-1])));
  1077. }
  1078. }
  1079.  
  1080. // достаю фандомы
  1081. if(preg_match('/<dd>[\s]+<i class="icon-book"><\/i>[\s]+(.*?)<\/dd>/ius', $content, $matches))
  1082. {
  1083. if(preg_match_all('/<a href="\/fanfiction\/(.*?)">(.*?)<\/a>/ius', $matches[1], $matches1, PREG_SET_ORDER))
  1084. {
  1085. $count = count($matches1);
  1086. for($i=0;$i<$count;$i++)
  1087. {
  1088. if($matches1[$i][2] != '') $fic['fandoms_arr'][$matches1[$i][1]] = $matches1[$i][2];
  1089. }
  1090. }
  1091. }
  1092. }
  1093.  
  1094. // Если нет длины фанфика или она слишком мала
  1095. if(!isset($fic['length_kb']) || $fic['length_kb'] < 10)
  1096. {
  1097. return array('error'=>'1');
  1098. }
  1099.  
  1100. $fic['length_kb'] = ceil($fic['length_kb']/1024);
  1101.  
  1102. return $fic;
  1103. }
  1104.  
  1105. // Парсинг страницы с fictionpress.com
  1106. function LibFictofile_Parse_Fictionpress($content, $fic)
  1107. {
  1108. // Проверяю на 404
  1109. if(mb_strpos($content, 'Unable to locate story. Code 1.') !== false)
  1110. {
  1111. return array('error'=>'404');
  1112. }
  1113.  
  1114. // Достаю название и шапку
  1115. if(preg_match('/<a class=xcontrast_txt[\s]+href=\"(.*?)\">(.*?)<\/a>.*?<b class=\'xcontrast_txt\'>([^<]*?)<\/b>.*?<a class=\'xcontrast_txt\' href=\'\/u\/([\d]{1,11})\/.*?\'>(.*?)<\/a>.*?<div style=\'margin-top:2px\' class=\'xcontrast_txt\'>(.*?)<\/div>.*?Rated:[^>]*?>(.*?)<\/a>\s+-(.+?)-(.*?)<\/div>/ius', $content, $matches))
  1116. {
  1117. $fic['authors'] = trim($matches[5]);
  1118. $fic['authors_info'][] = array('id' => $matches[4], 'name' => $fic['authors']);
  1119.  
  1120. $fic['title'] = trim($matches[3]);
  1121. $fic['summary'] = trim($matches[6]);
  1122.  
  1123. $fic['rating'] = '';
  1124. switch($matches[7])
  1125. {
  1126. case 'Fiction K':
  1127. case 'Fiction K+':
  1128. $fic['rating'] = 'General';
  1129. break;
  1130. case 'Fiction T':
  1131. $fic['rating'] = 'PG-13';
  1132. break;
  1133. case 'Fiction M':
  1134. $fic['rating'] = 'R';
  1135. break;
  1136. }
  1137. if($fic['rating'] !== '')
  1138. {
  1139. $rating = '<b>Рейтинг:</b> '.$fic['rating'].'<br/><br/>';
  1140. }
  1141.  
  1142. $fic['genre'] = '';
  1143. $tmp = explode('- Chapters:', $matches[9]);
  1144. $tmp = explode('- Words:', $tmp[0]);
  1145. $genres = LibFictofile_GenreListArray_Fanfiction();
  1146. foreach($genres AS $genre)
  1147. {
  1148. if (mb_strpos($tmp[0], $genre) !== false)
  1149. {
  1150. $fic['genre'] = trim($tmp[0]);
  1151. continue;
  1152. }
  1153. }
  1154. if($fic['genre'] !== '')
  1155. {
  1156. $genre = '<b>Жанр:</b> '.$fic['genre'].'<br/><br/>';
  1157. }
  1158.  
  1159. $size = '';
  1160. if(preg_match('/Words:\s+([0-9,]*?)\s+-/ius', $matches[9], $matches2))
  1161. {
  1162. $size = '<b>Размер:</b> '.declension(str_replace(',', '', $matches2[1]), 'слово,слова,слов').'<br/><br/>';
  1163. }
  1164.  
  1165. $status = '<b>Статус:</b> В процессе написания<br/><br/>';
  1166. $fic['status'] = '2';
  1167. if(mb_strpos($matches[9], 'Complete'))
  1168. {
  1169. $status = '<b>Статус:</b> Закончен<br/><br/>';
  1170. $fic['status'] = '1';
  1171. }
  1172.  
  1173. $fic['language'] = trim($matches[8]);
  1174. $fic['not_russian'] = '0';
  1175. if($fic['language'] != 'Russian')
  1176. {
  1177. $fic['not_russian'] = '1';
  1178. }
  1179. $language = '<b>Язык:</b> '.$fic['language'].'<br/><br/>';
  1180.  
  1181. if(preg_match('/Published: <span data-xutime=\'(\d{9,10})\'>/ius', $matches[9], $matches2))
  1182. {
  1183. $fic['date_first'] = $matches2[1];
  1184. }
  1185. if(preg_match('/Updated: <span data-xutime=\'(\d{9,10})\'>/ius', $matches[9], $matches2))
  1186. {
  1187. $fic['date_last'] = $matches2[1];
  1188. }
  1189. else
  1190. {
  1191. $fic['date_last'] = $fic['date_first'];
  1192. }
  1193.  
  1194. $fic['head'] = '<b>Ссылка:</b> '.$fic['pagetosave'].$fic['default_id'].'<br/><br/><b>Автор:</b> '.$fic['authors'].'<br/><br/>'.$rating.$genre.$language.$status.$size.'<b>Аннотация:</b> '.$fic['summary'];
  1195. $fic['head'] = str_replace("\r", '', $fic['head']);
  1196. $fic['head'] = preg_replace("/[\n]+/u", "\n", $fic['head']);
  1197. }
  1198. else
  1199. {
  1200. return array('error'=>'1');
  1201. }
  1202.  
  1203. // Достаю главы
  1204. $fic['length_kb'] = 0;
  1205. $fic['length_words'] = 0;
  1206. $i = 1;
  1207. $dom = new DOMDocument();
  1208. @$dom->loadHTML($content);
  1209. $xpath = new DOMXPath($dom);
  1210. $hasChapters = $xpath->evaluate("boolean(//select[@id='chap_select'])");
  1211. $story_chapters = $xpath->query("//select[@id='chap_select']/option");
  1212. if ($hasChapters)
  1213. {
  1214. foreach ($story_chapters as $chapter)
  1215. {
  1216. $title = trim($chapter->nodeValue);
  1217. if (mb_strpos($title, $i.'.') === 0)
  1218. {
  1219. $title = str_replace($i.'. ', '', $title);
  1220. $fic['chapter_'.$i]['title'] = $title;
  1221.  
  1222. $content1 = LibFictofile_GetContent($fic['pagetosave'].$fic['default_id'].'/'.$i.'/', true);
  1223. $content1 = LibFictofile_GetContent_Fanfiction_Unzip($content1);
  1224. $tmp = explode("id='storytext'>", $content1);
  1225. $tmp = explode('</div>', $tmp[1]);
  1226. $innerHTML = $tmp[0];
  1227.  
  1228. $innerHTML = str_replace('<hr size=1 noshade>', '', $innerHTML);
  1229. $innerHTML = preg_replace('/<p style=[\'"]text-align:center;[\'"]>(.*?)<\/p>/ius', '<br/><center>$1</center><br/>', $innerHTML);
  1230. $innerHTML = str_replace('<br>', '<br/>', $innerHTML);
  1231. $innerHTML = str_replace('<p>', '<br/>', $innerHTML);
  1232. $innerHTML = str_replace('<P>', '<br/>', $innerHTML);
  1233. $innerHTML = str_replace('<em>', '<i>', $innerHTML);
  1234. $innerHTML = str_replace('</em>', '</i>', $innerHTML);
  1235. $innerHTML = str_replace('<strong>', '<b>', $innerHTML);
  1236. $innerHTML = str_replace('</strong>', '</b>', $innerHTML);
  1237. $innerHTML = str_replace("\r", '', $innerHTML);
  1238. $innerHTML = str_replace("\n", ' ', $innerHTML);
  1239. $innerHTML = str_replace('&ndash;', '-', $innerHTML);
  1240. $innerHTML = strip_tags($innerHTML, '<br/><b><i><u><center><s>');
  1241.  
  1242. $fic['chapter_'.$i]['text'] = $innerHTML;
  1243.  
  1244. $fic['length_kb'] += mb_strlen($fic['chapter_'.$i]['text']);
  1245. $fic['length_words'] += LibStr_CountWords($fic['chapter_'.$i]['text']);
  1246. }
  1247. else
  1248. {
  1249. break;
  1250. }
  1251. $i++;
  1252. }
  1253. $fic['chapters_count'] = $i;
  1254. }
  1255. // Если одна глава
  1256. else
  1257. {
  1258. $fic['chapter_1']['title'] = $fic['title'];
  1259.  
  1260. $tmp = explode("id='storytext'>", $content);
  1261. $tmp = explode('</div>', $tmp[1]);
  1262. $innerHTML = $tmp[0];
  1263.  
  1264. $innerHTML = str_replace('<hr size=1 noshade>', '', $innerHTML);
  1265. $innerHTML = preg_replace('/<p style=[\'"]text-align:center;[\'"]>(.*?)<\/p>/ius', '<br/><center>$1</center><br/>', $innerHTML);
  1266. $innerHTML = str_replace('<br>', '<br/>', $innerHTML);
  1267. $innerHTML = str_replace('<p>', '<br/>', $innerHTML);
  1268. $innerHTML = str_replace('<P>', '<br/>', $innerHTML);
  1269. $innerHTML = str_replace('<em>', '<i>', $innerHTML);
  1270. $innerHTML = str_replace('</em>', '</i>', $innerHTML);
  1271. $innerHTML = str_replace('<strong>', '<b>', $innerHTML);
  1272. $innerHTML = str_replace('</strong>', '</b>', $innerHTML);
  1273. $innerHTML = str_replace("\r", '', $innerHTML);
  1274. $innerHTML = str_replace("\n", ' ', $innerHTML);
  1275. $innerHTML = str_replace('&ndash;', '-', $innerHTML);
  1276. $innerHTML = strip_tags($innerHTML, '<br/><b><i><u><center><s>');
  1277.  
  1278. $fic['chapter_1']['text'] = $innerHTML;
  1279.  
  1280. $fic['chapters_count'] = 2;
  1281. $fic['length_kb'] = mb_strlen($fic['chapter_1']['text']);
  1282. $fic['length_words'] = LibStr_CountWords($fic['chapter_1']['text']);
  1283. }
  1284.  
  1285. $fic['length_kb'] = ceil($fic['length_kb']/1024);
  1286.  
  1287. return $fic;
  1288. }
  1289.  
  1290. // Парсинг страницы с Hogwartsnet
  1291. function LibFictofile_Parse_Hogwartsnet($content, $fic)
  1292. {
  1293. // Проверяю на 404
  1294. if(mb_strpos($content, 'Такого фанфика не существует.') !== false)
  1295. {
  1296. return array('error'=>'404');
  1297. }
  1298.  
  1299. // Достаю название и шапку
  1300. if(!preg_match ('/<td valign=top><font width=70% border=1 cellpadding=2 cellspacing=0 bordercolor=#0000ff style="font-size:12px;color:#555555;font-family:Arial,sans-serif;line-height:13px;"><a href="member\.php\?l=0&id=([\d]+)" class="link1">(.*?)<\/a>[ \(.*?\)]*[&nbsp;]*<i>([^<]*)<\/i>[&nbsp;]*[<img src="\/mfanf\/images\/w00t.gif" alt="стёбный фанфик">]*[<img src="\/mfanf\/see_img\.php\?id=\d" alt="Оценка фанфика">]*[&nbsp;]*[SMS рейтинг: <font color="red"><b>\d+\.?\d?<\/b> <small>\(голосов: \d+\)<\/small><\/font>]*<br><br>[&nbsp;]*(.*?)<br>(.*?)<hr style="color:#dddddd;"><H2 align=center>(.*?)<\/H2>/ius', $content, $matches))
  1301. {
  1302. return array('error'=>'1');
  1303. }
  1304.  
  1305. $fic['title'] = trim($matches[6]);
  1306. $fic['head'] = trim($matches[4]).'<br><br>'.strip_tags(trim(str_replace('<br>', '<br/>',$matches[5])), '<br/>');
  1307. $fic['summary'] = trim($matches[4]);
  1308.  
  1309. $fic['authors'] = strip_tags(trim($matches[2]));
  1310. if(mb_strpos($fic['authors'], '(') !== false)
  1311. {
  1312. $tmp = explode('(', $fic['authors']);
  1313. $fic['authors'] = $tmp[0];
  1314. }
  1315. $fic['authors_info'][] = array('id' => $matches[1], 'name' => $fic['authors']);
  1316.  
  1317. $fic['head'] = str_replace('&nbsp;', '', $fic['head']);
  1318. $fic['head'] = '<b>Ссылка:</b> http://www.hogwartsnet.ru/mfanf/ffshowfic.php?fid='.$fic['default_id'].'<br/><br/><b>Автор:</b> '.$fic['authors'].'<br/><br/>'.$fic['head'];
  1319. $head_tmp = $fic['head'];
  1320.  
  1321. // Достаю поля шапки
  1322. // Форматируем по образу вывода
  1323. $fic['head'] = preg_replace('/(<b>Автор:<\/b>[^<]*<br\/><br\/>)([^<]*<br><br>)([^<]*<br\/>)([^<]*<br\/>)/u', '$1<b>Саммари:</b>$2<b>Фандом:</b>$3<b>Персонажи:</b>$4{%}', $fic['head']);
  1324. $flag = mb_strpos($fic['head'], '{%}');
  1325. $head1 = mb_substr($fic['head'], 0, $flag);
  1326. $head2 = mb_substr($fic['head'], ($flag+3));
  1327.  
  1328. $head2 = str_replace('||', '<br/>', $head2);
  1329. $head2 = preg_replace('/(Размер:|Глав:|Предупреждения:|Начало:|Последнее обновление:)/u', '<b>$1</b>', $head2);
  1330. $tmp = explode('<br/>', $head2);
  1331. $head2 = '<b>Жанр:</b>'.$tmp[0].'<b>Категория:</b>'.$tmp[1].'<b>Рейтинг:</b>'.$tmp[2];
  1332.  
  1333. $count = count($tmp);
  1334. for($i=3; $i<$count; $i++)
  1335. {
  1336. if(!preg_match('/(Прочитано:|Подписано:|Отзывов:)/u',$tmp[$i])) $head2 .= $tmp[$i];
  1337. }
  1338. $fic['head'] = $head1.$head2;
  1339. $fic['head'] = str_replace(array('<br/>', '<br>'), '', $fic['head']);
  1340. $fic['head'] = str_replace(
  1341. array('Ссылка:', 'Примечания автора:', 'Беты (редакторы):'),
  1342. array('Ссылка:', 'От автора:', 'Беты:'),
  1343. $fic['head']);
  1344. $fic['head'] = preg_replace('/<b>(.*?)<\/b>/u', '</div><div class="clear"></div></div><div class="tr"><div class="title">$1</div><div class="content">', $fic['head']).'</div><div class="clear"></div></div>';
  1345. $fic['head'] = mb_substr($fic['head'], 37);
  1346.  
  1347. // Достаю фандомы
  1348. $fic['fandoms'] = '';
  1349. $fic['fandoms_arr'] = array();
  1350. if(preg_match('/Фандом:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches3))
  1351. {
  1352. $fic['fandoms'] = trim($matches3[1]);
  1353. $fic['fandoms_arr'][$fic['fandoms']] = $fic['fandoms'];
  1354. }
  1355.  
  1356. // Достаю Персонажей
  1357. $fic['paring'] = '';
  1358. if(preg_match('/<div class="title">Персонажи:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches4))
  1359. {
  1360. $fic['paring'] = trim($matches4[1]);
  1361. }
  1362.  
  1363. // статус
  1364. $fic['status'] = 0;
  1365. switch($matches[3])
  1366. {
  1367. case 'закончен':
  1368. $fic['status'] = 1;
  1369. break;
  1370. case 'в работе':
  1371. $fic['status'] = 2;
  1372. break;
  1373. case 'заброшен':
  1374. case 'приостановлен':
  1375. $fic['status'] = 3;
  1376. break;
  1377. }
  1378.  
  1379. // Достаю рейтинг
  1380. $fic['rating'] = '';
  1381. if(preg_match('/<div class="title">Рейтинг:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches5))
  1382. {
  1383. $fic['rating'] = trim($matches5[1]);
  1384. if($fic['rating'] == '')
  1385. {
  1386. preg_match('/<div class="title">Категория:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches5);
  1387. $fic['rating'] = trim($matches5[1]);
  1388. }
  1389. }
  1390.  
  1391. // Достаю жанр
  1392. $fic['genre'] = '';
  1393. if(preg_match('/<div class="title">Жанр:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches6))
  1394. {
  1395. $fic['genre'] = preg_replace('/\/$/u', '', preg_replace('/\/+/u', '/', preg_replace('/\/\s+/u', '/', trim($matches6[1]))));
  1396. }
  1397.  
  1398. // Достаю категорию
  1399. $fic['het_slash'] = 0;
  1400. if(preg_match('/<div class="title">Категория:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches10))
  1401. {
  1402. $tmp = trim($matches10[1]);
  1403. switch($tmp)
  1404. {
  1405. case 'гет':
  1406. $fic['het_slash'] = '1';
  1407. break;
  1408. case 'слэш':
  1409. $fic['het_slash'] = '2';
  1410. break;
  1411. case 'джен':
  1412. $fic['het_slash'] = '3';
  1413. break;
  1414. case 'фемслэш':
  1415. $fic['het_slash'] = '4';
  1416. break;
  1417. }
  1418. }
  1419.  
  1420. // Достаю предупреждения
  1421. $fic['warning'] = '';
  1422. if(preg_match('/<div class="title">Предупреждения:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches7))
  1423. {
  1424. $fic['warning'] = trim($matches7[1]);
  1425. }
  1426.  
  1427. // Достаю размер
  1428. $fic['size'] = 0;
  1429. if(preg_match('/<div class="title">Размер:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches8))
  1430. {
  1431. $fic['size'] = trim($matches8[1]);
  1432. switch($fic['size'])
  1433. {
  1434. case 'мини': $fic['size'] = '1'; break;
  1435. case 'миди': $fic['size'] = '2'; break;
  1436. case 'макси': $fic['size'] = '3'; break;
  1437. }
  1438. }
  1439.  
  1440. // Достаю даты
  1441. $fic['date_first'] = 0;
  1442. if(preg_match('/<div class="title">Начало:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches7))
  1443. {
  1444. $fic['date_first'] = trim($matches7[1]);
  1445. $fic['date_first'] = strtotime(preg_replace('/(\d{2})\.(\d{2})\.(\d{2})/u', '20$3-$2-$1', $fic['date_first']));
  1446. }
  1447. if(preg_match('/<div class="title">Последнее обновление:<\/div><div class="content">(.*?)<\/div>/ius', $fic['head'], $matches7))
  1448. {
  1449. $fic['date_last'] = trim($matches7[1]);
  1450. $fic['date_last'] = strtotime(preg_replace('/(\d{2})\.(\d{2})\.(\d{2})/u', '20$3-$2-$1', $fic['date_last']));
  1451. }
  1452. if ($fic['date_last'] == '')
  1453. {
  1454. $fic['date_last'] = $fic['date_first'];
  1455. }
  1456. /*
  1457. if(IsAdmin())
  1458. {
  1459. print_r($fic);
  1460. exit;
  1461. }
  1462. */
  1463. $fic['head'] = $head_tmp;
  1464.  
  1465. // иноформация о переводе
  1466. $fic['is_translation'] = 0;
  1467. $fic['original_author'] = '';
  1468. $fic['original_url'] = '';
  1469. if(preg_match('/Автор фанфика: ([^<]*)<br\/>.*?Ссылка на фанфик: ([^<]*)<br\/>/ius', $fic['head'], $matches9))
  1470. {
  1471. $fic['is_translation'] = 1;
  1472. $fic['original_author'] = $matches9[1];
  1473. $fic['original_url'] = $matches9[2];
  1474. }
  1475.  
  1476. // Достаю главы
  1477. $fic['length_kb'] = 0;
  1478. $fic['length_words'] = 0;
  1479.  
  1480. $tmp = explode('<center><b>', $content);
  1481. $count = count($tmp);
  1482. if($count > 1)
  1483. {
  1484. for ($i = 1; $i < $count; $i++)
  1485. {
  1486. // Отрезаю конец последней главы
  1487. if($i == $count-1)
  1488. {
  1489. $tmp2 = explode('</div>',$tmp[$i]);
  1490. $tmp[$i] = $tmp2[0];
  1491. }
  1492. $tmp1 = explode('</b></center>',$tmp[$i]);
  1493. $fic['chapter_'.$i]['title'] = trim($tmp1[0]);
  1494. $tmp1[1] = preg_replace('/<script.*?<\/script>/ius', '', $tmp1[1]);
  1495. $fic['chapter_'.$i]['text'] = strip_tags(str_replace('<br />', '<br/>', str_replace('<br>', '<br/>', trim($tmp1[1]))), '<br/><b><i><sup><s><center><sub>');
  1496. $fic['length_kb'] += mb_strlen($fic['chapter_'.$i]['text']);
  1497. $fic['length_words'] += LibStr_CountWords($fic['chapter_'.$i]['text']);
  1498. }
  1499. $fic['chapters_count'] = $i;
  1500. }
  1501. // Если главы не распарсились
  1502. if($i == 1)
  1503. {
  1504. return array('error'=>'1');
  1505. }
  1506.  
  1507. $fic['length_kb'] = ceil($fic['length_kb']/1024);
  1508.  
  1509. return $fic;
  1510. }
  1511.  
  1512. // Парсинг страницы с proza.ru
  1513. function LibFictofile_Parse_Proza($content, $fic)
  1514. {
  1515. // Проверяю на 404
  1516. if (mb_strpos($content, '<h1 align="center"><font color="#FF0000">') !== false)
  1517. {
  1518. return array('error'=>'404');
  1519. }
  1520.  
  1521. $content = mb_convert_encoding($content,"UTF-8","CP1251");
  1522.  
  1523. // Достаю название
  1524. $fic['title'] = 'Без названия';
  1525. if(preg_match('/<h1>([^<]*?)<\/h1>/ius', $content, $matches1))
  1526. {
  1527. $fic['title'] = trim($matches1[1]);
  1528. }
  1529.  
  1530. // Достаю автора
  1531. if(!preg_match('/<div class="titleauthor"><em><a href="\/avtor\/([a-zA-Z0-9_-]+?)">([^<]+?)<\/a><\/em><\/div>/ius', $content, $matches2))
  1532. {
  1533. return array('error'=>'1');
  1534. }
  1535. $fic['authors'] = trim($matches2[2]);
  1536. $fic['authors_info'][] = array('id' => $matches2[1], 'name' => $fic['authors']);
  1537.  
  1538. // даты из uri произведения
  1539. preg_match('/^(\d{4})\/(\d{2})\/(\d{2})/', $fic['default_id'], $matches3);
  1540. $fic['date_first'] = strtotime($matches3[1].'-'.$matches3[2].'-'.$matches3[3]);
  1541. $fic['date_last'] = $fic['date_first'];
  1542.  
  1543. $fic['summary'] = 'Нет описания';
  1544.  
  1545. // Достаю главы
  1546. $tmp = explode('<div class="text">', $content);
  1547. $tmp = explode('</div>', $tmp[1]);
  1548.  
  1549. $fic['chapter_1']['title'] = $fic['title'];
  1550. $fic['chapter_1']['text'] = str_replace('<br>', '<br/>', $tmp[0]);
  1551. $fic['chapter_1']['text'] = strip_tags_smart(str_replace("\r", '', str_replace("\n", ' ', $fic['chapter_1']['text'])), array("<br>", "<b>", "<i>"));
  1552.  
  1553. $fic['chapters_count'] = 2;
  1554. $fic['length_kb'] = ceil(mb_strlen($fic['chapter_1']['text'])/1000);
  1555. $fic['length_words'] = LibStr_CountWords($fic['chapter_1']['text']);
  1556.  
  1557. return $fic;
  1558. }
  1559.  
  1560. // Парсинг страницы с СИ
  1561. function LibFictofile_Parse_Samlib($content, $fic)
  1562. {
  1563. // Проверяю на 404
  1564. if (mb_strpos($content, '404 Not Found') !== false)
  1565. {
  1566. return array('error'=>'404');
  1567. }
  1568.  
  1569. $content = mb_convert_encoding($content,"UTF-8","CP1251");
  1570.  
  1571. // Удаляю некоторую херню
  1572. $content = str_replace('>_<', '', $content);
  1573.  
  1574. // Достаю название и шапку
  1575. if(preg_match('/<h3>(.*?):.*?<h2>(.*?)<\/h2>.*?Размещен: (\d\d\/\d\d\/\d\d\d\d?), изменен: (\d\d\/\d\d\/\d\d\d\d?).*?<li>(.*?)<li>.*?<b>Аннотация:<\/b><br><font color="#555555"><i>(.*?)<\/i><\/font><\/ul>/ius', $content, $matches))
  1576. {
  1577. $fic['authors'] = trim($matches[1]);
  1578. $fic['title'] = trim($matches[2]);
  1579. $fic['summary'] = strip_tags(trim($matches[6]), '<br/><br>');
  1580. $fic['summary'] = str_replace(array('<br /><br />', '<br /><br /><br />', '<br /><br /><br /><br />'), '<br />', $fic['summary']);
  1581. $fic['summary'] = preg_replace('/(^<br \/>\s*|\s*<br \/>$)/ius', '', trim($fic['summary']));
  1582. preg_match_all('/<a href="\/janr\/.*?">(.*?)<\/a>/iu', $matches[5], $genres);
  1583. $fic['genre'] = implode('/', $genres[1]);
  1584. if(count($genres[1]) > 0) $genres = '<br/><br/><b>Жанры:</b> '.implode(', ', $genres[1]);
  1585. else $genres = '';
  1586. $fic['head'] = '<b>Ссылка:</b> '.$fic['pagetosave'].$fic['default_id'].'<br/><br/><b>Автор:</b> '.$fic['authors'].$genres.'<br/><br/><b>Аннотация:</b><br/>'.$fic['summary'].'<br/><br/><b>Размещен:</b> '.$matches[3].'<br/><br/><b>Изменен:</b> '.$matches[4];
  1587. $fic['date_first'] = strtotime(preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/u', '$3-$2-$1', $matches[3]));
  1588. $fic['date_last'] = strtotime(preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/u', '$3-$2-$1', $matches[4]));
  1589. }
  1590. elseif(preg_match('/<h3>(.*?):.*?<h2>(.*?)<\/h2>.*?Размещен: (\d\d\/\d\d\/\d\d\d\d?), изменен: (\d\d\/\d\d\/\d\d\d\d?).*?<li>(.*?)<\/ul>/ius', $content, $matches))
  1591. {
  1592. $fic['authors'] = trim($matches[1]);
  1593. $fic['title'] = trim($matches[2]);
  1594. $fic['summary'] = 'Нет описания';
  1595. preg_match_all('/<a href="\/janr\/.*?">(.*?)<\/a>/iu', $matches[5], $genres);
  1596. $fic['genre'] = implode('/', $genres[1]);
  1597. if(count($genres[1]) > 0) $genres = '<br/><br/><b>Жанры:</b> '.implode(', ', $genres[1]);
  1598. else $genres = '';
  1599. $fic['head'] = '<b>Ссылка:</b> '.$fic['pagetosave'].$fic['default_id'].'<br/><br/><b>Автор:</b> '.$fic['authors'].$genres.'<br/><br/><b>Аннотация:</b> нет<br/><br/><b>Размещен:</b> '.$matches[3].'<br/><br/><b>Изменен:</b> '.$matches[4];
  1600. $fic['date_first'] = strtotime(preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/u', '$3-$2-$1', $matches[3]));
  1601. $fic['date_last'] = strtotime(preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/u', '$3-$2-$1', $matches[4]));
  1602. }
  1603. else
  1604. {
  1605. return array('error'=>'1');
  1606. }
  1607.  
  1608. $tmp = explode('/', $fic['default_id']);
  1609. $fic['authors_info'][] = array('id' => $tmp[1], 'name' => $fic['authors']);
  1610.  
  1611. $fic['fandoms'] = '';
  1612.  
  1613. $fic['head'] = str_replace("\r", '', $fic['head']);
  1614. $fic['head'] = preg_replace("/[\n\s]+/u", "\n", $fic['head']);
  1615.  
  1616. // Достаю главы
  1617. $tmp = explode('<!----------- Собственно произведение --------------->', $content);
  1618. $tmp2 = explode('<!--------------------------------------------------->', $tmp[1]);
  1619. /*
  1620. * добавили рекламу, количество hr стало разным, а ещё их могут использовать сами авторы
  1621. $tmp = explode('<hr size=2 noshade>', $content);
  1622. if(count($tmp) != 5)
  1623. {
  1624. return array('error'=>'1');
  1625. }
  1626. */
  1627. $fic['chapter_1']['title'] = $fic['title'];
  1628. $fic['chapter_1']['text'] = trim($tmp2[0]);
  1629. // Обработка тэга pre
  1630. $fic['chapter_1']['text'] = preg_replace_callback('/<pre>(.*?)<\/pre>/ius', create_function('$line','return str_replace("\n", "<br/>", $line[0]);'), $fic['chapter_1']['text']);
  1631.  
  1632. $paragraph = array('</li>', '<br>', '<BR>', '<dd>', '</div>', '<p>', '<P>', '<p >');
  1633. $fic['chapter_1']['text'] = str_replace($paragraph, '<br/>', $fic['chapter_1']['text']);
  1634. $fic['chapter_1']['text'] = strip_tags_smart(str_replace("\r", '', str_replace("\n", ' ', $fic['chapter_1']['text'])), array("<br>", "<b>", "<i>", "<u>", "<center>", "<s>"));
  1635.  
  1636. $fic['chapter_1']['text'] = str_replace('&#8211;', '—', $fic['chapter_1']['text']);
  1637. $fic['chapter_1']['text'] = str_replace('&#0151;&nbsp;', '— ', $fic['chapter_1']['text']);
  1638. $fic['chapter_1']['text'] = str_replace('&#0150;&nbsp;', '— ', $fic['chapter_1']['text']);
  1639. $fic['chapter_1']['text'] = str_replace('&#0151;', '—', $fic['chapter_1']['text']);
  1640. $fic['chapter_1']['text'] = str_replace('&#0150;', '—', $fic['chapter_1']['text']);
  1641. $fic['chapter_1']['text'] = str_replace('&#0171;', '«', $fic['chapter_1']['text']);
  1642. $fic['chapter_1']['text'] = str_replace('&#0187;', '»', $fic['chapter_1']['text']);
  1643. $fic['chapter_1']['text'] = str_replace('&#0133;', '...', $fic['chapter_1']['text']);
  1644.  
  1645. $fic['chapters_count'] = 2;
  1646. $fic['length_kb'] = ceil(mb_strlen($fic['chapter_1']['text'])/1024);
  1647. $fic['length_words'] = LibStr_CountWords($fic['chapter_1']['text']);
  1648.  
  1649. return $fic;
  1650. }
  1651.  
  1652. // Парсинг страницы с Snapetales
  1653. function LibFictofile_Parse_Snapetales($content, $fic)
  1654. {
  1655. $content = mb_convert_encoding($content, "UTF-8", "CP1251");
  1656.  
  1657. // Проверяю на 404
  1658. if (mb_strpos($content, 'Фик с таким идентификатором не существует.') !== false)
  1659. {
  1660. return array('error' => '404');
  1661. }
  1662.  
  1663. // Проверяю на ошибку на сайте, не стоит обновлять тексты на пустоту
  1664. if (mb_strpos($content, 'Случилось невероятное! На сайте завелись бякоклешни и что-то своими клешнями забячили.') !== false)
  1665. {
  1666. return array('error' => '404');
  1667. }
  1668.  
  1669. // Достаю название и шапку
  1670. if (!preg_match('/<h3>(.*?)<\/h3>(.*?)<a href=http:\/\/www.snapetales\.com\/index\.php\?resp_id/ius', $content, $matches))
  1671. {
  1672. return array('error' => '1');
  1673. }
  1674. $fic['title'] = trim($matches[1]);
  1675. $fic['head'] = $matches[2];
  1676. $head_tmp = '<b>Ссылка:</b> http://www.snapetales.com/index.php?fic_id='.$fic['default_id'].'<br/><br/>'.strip_tags(trim(preg_replace('/<td style=\'font-variant:small-caps;\'[^>]*>(.*?):<\/td><td>(.*?)<\/td>/u', '<b>$1:</b> $2<br/><br/>', str_replace('<br />', '<br/>', str_replace('<br>', '<br/>', $matches[2])))), '<br/><b><i>');
  1677.  
  1678. // Достаю саммари
  1679. if (preg_match('/<td style=\'font-variant:small-caps;\' valign=top>Аннотация:<\/td><td>(.*?)<\/td>/ius', $fic['head'], $matches1))
  1680. {
  1681. $fic['summary'] = trim($matches1[1]);
  1682. }
  1683.  
  1684. // Достаю от автора
  1685. if (preg_match('/<td style=\'font-variant:small-caps;\' valign=top>Комментарии:<\/td><td>(.*?)<\/td>/ius', $fic['head'], $matches10))
  1686. {
  1687. $fic['author_comment'] = trim($matches10[1]);
  1688. }
  1689.  
  1690. // Достаю автора
  1691. if (preg_match('/<td style=\'font-variant:small-caps;\'>Автор:<\/td><td>(.*?)<\/td>/ius', $fic['head'], $matches2))
  1692. {
  1693. $fic['authors'] = trim($matches2[1]);
  1694. }
  1695.  
  1696. // Достаю Персонажей
  1697. if (preg_match('/<td style=\'font-variant:small-caps;\' valign=top>Персонажи:<\/td><td>(.*?)<\/td>/ius', $fic['head'], $matches4))
  1698. {
  1699. $fic['paring'] = trim($matches4[1]);
  1700. }
  1701.  
  1702. // Достаю рейтинг
  1703. if (preg_match('/<td style=\'font-variant:small-caps;\'>Рейтинг:<\/td><td>(.*?)<\/td>/ius', $fic['head'], $matches5))
  1704. {
  1705. $fic['rating'] = trim($matches5[1]);
  1706. }
  1707.  
  1708. // Достаю жанр
  1709. if (preg_match('/<td style=\'font-variant:small-caps;\'>Жанр:<\/td><td>(.*?)<\/td>/ius', $fic['head'], $matches6))
  1710. {
  1711. $fic['genre'] = preg_replace('/,\s*/u', '/', trim($matches6[1]));
  1712. }
  1713.  
  1714. // Достаю предупреждения
  1715. if (preg_match('/<td style=\'font-variant:small-caps;\'>Предупреждения:<\/td><td>(.*?)<\/td>/ius', $fic['head'], $matches7))
  1716. {
  1717. $fic['warning'] = strip_tags(trim($matches7[1]), '<br><br />');
  1718. if ($fic['warning'] == 'Tекст не требует предупреждений')
  1719. {
  1720. $fic['warning'] = '';
  1721. }
  1722. }
  1723.  
  1724. // Достаю статус
  1725. if (preg_match('/<td style=\'font-variant:small-caps;\'>Статус:<\/td><td>(.*?)<\/td>/ius', $fic['head'], $matches9))
  1726. {
  1727. $fic['status'] = trim($matches9[1]);
  1728. switch ($fic['status'])
  1729. {
  1730. case 'Закончен':
  1731. $fic['status'] = '1';
  1732. break;
  1733. case 'Не закончен':
  1734. $fic['status'] = '2';
  1735. break;
  1736. case 'Замерз':
  1737. $fic['status'] = '3';
  1738. break;
  1739. }
  1740. }
  1741.  
  1742. // Достаю даты
  1743. if (preg_match('/<td style=\'font-variant:small-caps;\'>Выложен:<\/td><td>(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})(.*?)<\/td>/ius', $fic['head'], $matches11))
  1744. {
  1745. $fic['date_first'] = strtotime($matches11[1]);
  1746.  
  1747. if (preg_match('/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/u', $matches11[2], $matches12))
  1748. {
  1749. $fic['date_last'] = strtotime($matches12[1]);
  1750. }
  1751. elseif (preg_match('/(\d{4}\.\d{2}\.\d{2} \d{2}:\d{2}:\d{2})/u', $matches11[2], $matches12))
  1752. {
  1753. $fic['date_last'] = strtotime(str_replace('.', '-', $matches12[1]));
  1754. }
  1755. elseif (preg_match('/(\d{4}\.\d{2}\.\d{2})/u', $matches11[2], $matches12))
  1756. {
  1757. $fic['date_last'] = strtotime(str_replace('.', '-', $matches12[1]));
  1758. }
  1759. else
  1760. {
  1761. $fic['date_last'] = $fic['date_first'];
  1762. }
  1763. }
  1764. if ($fic['date_last'] == '')
  1765. {
  1766. $fic['date_last'] = '0';
  1767. }
  1768. if ($fic['date_first'] == '')
  1769. {
  1770. $fic['date_first'] = '0';
  1771. }
  1772.  
  1773. $fic['head'] = $head_tmp;
  1774. $fic['head'] = str_replace("\r", '', $fic['head']);
  1775. $fic['head'] = preg_replace("/[\n]+/u", "\n", $fic['head']);
  1776.  
  1777. // Запускаем CURL
  1778. $content2 = LibFictofile_GetContent('http://www.snapetales.com/index.php?fic_id='.$fic['default_id']);
  1779. $content2 = mb_convert_encoding($content2, "UTF-8", "CP1251");
  1780.  
  1781. $fic['authors_info'] = array();
  1782. if (preg_match("/<td style='font-variant:small-caps;'>Автор\/-ы, переводчик\/-и:<\/td>(.*?)<\/td>/ius", $content2, $matches))
  1783. {
  1784. if (preg_match_all('/<a href=index\.php\?auth\_id=(\d+)>(.*?)<\/a>/ius', $matches[1], $matches_author, PREG_SET_ORDER))
  1785. {
  1786. foreach ($matches_author AS $tmp)
  1787. {
  1788. $fic['authors_info'][] = array('id' => $tmp[1], 'name' => $tmp[2]);
  1789. }
  1790. }
  1791. }
  1792.  
  1793. $fic['fandoms_arr'] = array();
  1794. if (preg_match("/<td style='font-variant:small-caps;'>Фандом:<\/td>(.*?)<\/td>/ius", $content2, $matches))
  1795. {
  1796. if (preg_match_all('/<a href=index\.php\?id=3&x\_id=(\d+)[^>]*?>(.*?)<\/a>/ius', $matches[1], $matches_fandom, PREG_SET_ORDER))
  1797. {
  1798. foreach ($matches_fandom AS $tmp)
  1799. {
  1800. $fic['fandoms_arr'][$tmp[1]] = $tmp[2];
  1801. }
  1802. }
  1803. }
  1804.  
  1805. // иноформация о переводе
  1806. $fic['is_translation'] = 0;
  1807. $fic['original_author'] = '';
  1808. $fic['original_url'] = '';
  1809. if (preg_match('/>Ссылка на оригинал:<\/td><td>.*?<a href="([^"]*?)"/ius', $content2, $matches_trl))
  1810. {
  1811.  
  1812. $fic['is_translation'] = 1;
  1813. $fic['original_author'] = $fic['authors_info'][0]['name'];
  1814. unset($fic['authors_info'][0]);
  1815. $fic['original_url'] = $matches_trl[1];
  1816.  
  1817. $fic['original_url'] = str_replace('?view_adult=true', '', $fic['original_url']);
  1818. }
  1819.  
  1820. $fic['length_kb'] = 0;
  1821. $fic['length_words'] = 0;
  1822.  
  1823. // Достаю главы
  1824. $content = preg_replace('/<strong>Данный материал может содержать сцены насилия, описание однополых связей и других НЕДЕТСКИХ отношений\.[\s]*<br>Я предупрежден\(-а\) и осознаю, что делаю, читая нижеизложенный текст\/просматривая видео\.<\/strong>/ius', '', $content);
  1825.  
  1826. $tmp = explode('<strong>', $content);
  1827. $count = count($tmp);
  1828. if ($count > 1)
  1829. {
  1830. for ($i = 1; $i < $count; $i++)
  1831. {
  1832. $tmp1 = explode('</strong>', $tmp[$i]);
  1833. $fic['chapter_'.$i]['title'] = trim($tmp1[0]);
  1834. $fic['chapter_'.$i]['text'] = strip_tags(str_replace('<br />', '<br/>', str_replace('<br>', '<br/>', trim($tmp1[1]))), '<br/><b><i><sup><s><center><sub>');
  1835. $fic['length_kb'] += mb_strlen($fic['chapter_'.$i]['text']);
  1836. $fic['length_words'] += LibStr_CountWords($fic['chapter_'.$i]['text']);
  1837. }
  1838. $fic['chapters_count'] = $i;
  1839. }
  1840. else
  1841. {
  1842. // пытаюсь достать единственную главу
  1843. $tmp = explode('<tr bgcolor="#FFFBE5" align=justify><td>', $content);
  1844. $tmp = explode('</td>', $tmp[1]);
  1845.  
  1846. if (mb_strlen($tmp[0]) > 100)
  1847. {
  1848. $fic['chapter_1']['title'] = $fic['title'];
  1849. $fic['chapter_1']['text'] = trim($tmp[0]);
  1850. $fic['chapter_1']['text'] = strip_tags(str_replace('<br />', '<br/>', str_replace('<br>', '<br/>', $fic['chapter_1']['text'])), '<br/><b><i><sup><s><center><sub>');
  1851. $fic['chapters_count'] = 2; // глава одна, но для цикла, который будет позже, надо ставить двойку
  1852. $fic['length_kb'] = mb_strlen($fic['chapter_1']['text']);
  1853. $fic['length_words'] = LibStr_CountWords($fic['chapter_1']['text']);
  1854. }
  1855. else
  1856. {
  1857. return array('error' => '1');
  1858. }
  1859. }
  1860.  
  1861. $fic['length_kb'] = ceil($fic['length_kb'] / 1024);
  1862.  
  1863. return $fic;
  1864. }
Advertisement
Add Comment
Please, Sign In to add comment