englishextra

update.php

Sep 28th, 2011
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.34 KB | None | 0 0
  1. <?php
  2. /**
  3.  * shimansky.biz, mobile-english.ru
  4.  *
  5.  * Static web site core scripts
  6.  * @package shimansky.biz
  7.  * @author Serguei Shimansky <[email protected]>
  8.  * @copyright Serguei Shimansky 11:28 23.07.2011
  9.  * @access public
  10.  * @version 0.1
  11.  * @link https://bitbucket.org/englishextra/shimansky.biz
  12.  * @link https://bitbucket.org/englishextra/mobile-english.ru
  13.  * @link https://github.com/englishextra/shimansky.biz.git
  14.  * @link https://github.com/englishextra/mobile-english.ru.git
  15.  */
  16.  
  17. $relpa = ($relpa0 = preg_replace("/[\/]+/", "/", $_SERVER['DOCUMENT_ROOT'] . '/')) ? $relpa0 : '';
  18.  
  19. $a_inc = array(
  20.     'lib/lib_global2.inc',
  21.     'lib/lib_mysql2.inc',
  22.     'inc/vars2.inc',
  23.     'inc/regional.inc',
  24.     'inc/adminauth.inc',
  25.     'inc/mysql.inc',
  26.     'inc/update_config_' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . '.inc'
  27. );
  28.  
  29. foreach ($a_inc as $v) {require_once $relpa . $v;}
  30.  
  31. /**
  32.  * functions
  33.  */
  34. function lib_local_write_file ($data, $w, $type) {
  35.     if (!$fo = fopen($data, $type)) {
  36.         die('Cannot open file: ' . $data);
  37.     }
  38.     if (!is_writable($data)) {
  39.         die('Cannot write file: ' . $data);
  40.     }
  41.     flock($fo, LOCK_EX);
  42.     fputs($fo, $w);
  43.     fflush($fo);
  44.     flock($fo, LOCK_UN);
  45.     fclose($fo);
  46. }
  47.  
  48. function lib_write_dir_index_html($title, $description, $filepath, $relpa, $site_root_printable, $p, $uplevel_link, $qrcode_sprite_image, $qrcode, $qrcode_title, $css, $js) {
  49.  
  50.     lib_local_write_file ($relpa . $filepath . 'index.html', '<!--#include virtual="/virtual/head.html" -->
  51. <meta name="description" content="' . lib_global2_ensure_amp(lib_global2_remove_tags($description)) . '" />
  52. <title>' . $site_root_printable . ' - ' . lib_global2_ensure_amp($title) . '</title>
  53. </head>
  54. <body>
  55. <!--#include virtual="/virtual/top.html" -->
  56. <h1><a href="' . $uplevel_link . '">&#160;&#8593;&#160;</a> ' . lib_global2_ensure_amp($title) . '</h1>
  57. <!--<div id="pageqrcode" title="' . $qrcode_title . '" style="background:url(' . $qrcode_sprite_image . ') no-repeat top left;background-position:' . $qrcode . ';"></div>-->
  58. <!--<div id="pageqrcode" title="' . $qrcode_title . '" style="width:137px;height:137px;background:url(/images/QR_CODE/' . $qrcode . ') no-repeat top left;background-color:transparent;"></div>-->
  59. ' . "\n" . '<p>' . lib_global2_ensure_amp($description) . '</p>' . "\n" . $p . '
  60. <!--#include virtual="/virtual/bottom.html" -->
  61. <!--#include virtual="/virtual/counters.html" -->
  62. <!--#include virtual="/virtual/foot.html" -->' . "\n", "w+");
  63.  
  64.     echo 'written dir index in file ' . $filepath . "<br />\n";
  65. }
  66.  
  67. function lib_local_update_dir_info($title, $description, $production_dir, $ext, $relpa, $site_root, $site_root_printable, $table_name, $read_length, $qrcode_sprite_image, $qrcode, $qrcode_title, $css, $js) {
  68.  
  69.     $dir = $relpa . $production_dir;
  70.  
  71.     if ($opendir = opendir($dir)) {
  72.  
  73.         $files = array();while ($files[] = readdir($opendir));sort($files, SORT_STRING);reset($files);closedir($opendir);
  74.  
  75.         $p = '';
  76.  
  77.         foreach($files as $file) {
  78.             if (!is_dir($dir . $file)) {
  79.                 $content = lib_global2_read_local_file($dir . $file, $f = '');
  80.                 if (preg_match("/\." . preg_quote($ext, "/") . "$/i", $file) && $file !='index.html' && $file !='index.php' && preg_match('#<title[^>]*>(.+?)</title>#', $content, $matches0) && preg_match('#<meta name="description" content="(.+?)" />#', $content, $matches1)) {
  81.                     $heading = preg_replace("/[ ]+/", " ", str_replace(array($site_root_printable, 'localhost', 'mobile-english.ru', 'shimansky.biz', ' - ' . $title . ' - ', '\''), array('', '', '', '', '', '&#39;'), $matches0[1]));
  82.                     $about = preg_replace("/[ ]+/", " ", str_replace(array('\''), array('&#39;'), $matches1[1]));
  83.                     $wordhash = lib_global2_html_to_safe_str(str_replace(array($site_root_printable . ' - ', '\'', $heading, $about), array('', '&#39;', '', ''), $content), $read_length);
  84.                     if (!empty($heading) && !empty($about)) {
  85.                         $p .= '<h2><a href="' . htmlentities('/' . $production_dir . $file) . '">' . lib_global2_ensure_amp($heading) . '</a></h2><p>' . lib_global2_ensure_amp($about) . '</p>';//' . "\n";
  86.                         //$p .= '<dl><dt><a href="' . htmlentities('/' . $production_dir . $file) . '">' . lib_global2_ensure_amp($heading) . '</a></dt><dd>' . lib_global2_ensure_amp($about) . '</dd></dl>';//' . "\n";
  87.                         @mysql_query("INSERT INTO `" . $table_name . "` (`page_title`, `page_url`, `description`, `wordhash`) VALUES ('" . mysql_real_escape_string (lib_global2_ensure_amp($title . ' - ' . $heading)) . "', '" . mysql_real_escape_string (lib_global2_ensure_amp(htmlentities('/' . $production_dir . $file))) . "', '" . mysql_real_escape_string (lib_global2_ensure_amp($about)) . "', '" . mysql_real_escape_string (lib_global2_ensure_amp($wordhash)) . "');") or die(@mysql_error());
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.  
  93.         lib_write_dir_index_html($title, $description, $production_dir, $relpa, $site_root_printable, $p, '../', $qrcode_sprite_image, $qrcode, $qrcode_title, $css, $js);
  94.     }
  95. }
  96.  
  97. $table_name = $pt_pages_table_name;
  98.  
  99. /**
  100.  * clear index table
  101.  */
  102. @mysql_query("DROP TABLE IF EXISTS `" . $table_name . "`;") or die(@mysql_error());
  103. @mysql_query("CREATE TABLE `" . $table_name . "` ( `id` mediumint(9) NOT NULL AUTO_INCREMENT, `page_title` mediumtext NOT NULL, `page_url` varchar(255) NOT NULL DEFAULT '', `description` mediumtext NOT NULL, `wordhash` mediumtext NOT NULL, PRIMARY KEY (`id`), FULLTEXT KEY `page_title` (`page_title`), FULLTEXT KEY `description` (`description`), FULLTEXT KEY `wordhash` (`wordhash`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;") or die(@mysql_error());
  104.  
  105. /**
  106.  * update directories
  107.  * $dirs is an array in inc/update_config_' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . '.inc
  108.  */
  109. sort($dir, 0);reset($dir);
  110.  
  111. /**
  112.  * write index.html in each directory
  113.  */
  114. for ($i = 0; $i < count($dir); $i++) {
  115.     if (!empty($dir[$i][0]) && !empty($dir[$i][1]) && !empty($dir[$i][2]) && !empty($dir[$i][3]) && !empty($dir[$i][4])) {
  116.         lib_local_update_dir_info($dir[$i][0], $dir[$i][1], $dir[$i][2], $dir[$i][3], $relpa, $vars2_site_root, $vars2_site_root_printable, $table_name, $update_config_read_length, $update_config_qrcode_sprite_image, $dir[$i][4], $update_config_qrcode_title, $update_config_css, $update_config_js);
  117.     }
  118. }
  119.  
  120. /**
  121.  * generate list for directories contents index.html
  122.  */
  123. $p = '';
  124. $pages_xml_str = '';
  125.  
  126. for ($i = 0; $i < count($dir); $i++) {
  127.     if (!empty($dir[$i][0]) && !empty($dir[$i][1]) && !empty($dir[$i][2]) && !empty($dir[$i][3])) {
  128.         $p .= '<h2><a href="/' . htmlentities($dir[$i][2]). '">' . lib_global2_ensure_amp($dir[$i][0]) . '</a></h2><p>' . lib_global2_ensure_amp($dir[$i][1]) . '</p>';//' . "\n";
  129.         //$p .= '<dl><dt><a href="/' . htmlentities($dir[$i][2]). '">' . lib_global2_ensure_amp($dir[$i][0]) . '</a></dt><dd>' . lib_global2_ensure_amp($dir[$i][1]) . '</dd></dl>';//' . "\n";
  130.         $pages_xml_str .= '<item><title>' . lib_global2_ensure_amp($dir[$i][0]) . '</title><link>' . htmlentities($vars2_site_root . $dir[$i][2]) . '</link><description>' . lib_global2_ensure_amp($dir[$i][1]) . '</description><category>Pages</category><pubDate>' . date("r") . '</pubDate></item>';//' . "\n";
  131.     }
  132. }
  133.  
  134. /**
  135.  * write directories contents index.html
  136.  */
  137. lib_write_dir_index_html($update_config_msg_site_contents, lib_global2_ensure_amp(lib_global2_remove_tags($pt_regional['site_description'])), $update_config_pages_dir, $relpa, $vars2_site_root_printable, $p, '../', $update_config_qrcode_sprite_image, $update_config_contents_qrcode, $update_config_qrcode_title, $update_config_css, $update_config_js);
  138.  
  139. /**
  140.  * update pages.xml
  141.  */
  142. lib_local_write_file($relpa . $update_config_pages_xml, '<?xml version="1.0" encoding="UTF-8"?>
  143. <rss version="2.0"
  144.     xmlns:content="http://purl.org/rss/1.0/modules/content/"
  145.     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  146.     xmlns:dc="http://purl.org/dc/elements/1.1/"
  147.     xmlns:atom="http://www.w3.org/2005/Atom"
  148.     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  149.     xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  150.     >
  151.  
  152. <channel>
  153.     <title>' . ucwords($vars2_site_root_printable) . ' - ' . lib_global2_ensure_amp($pt_regional['root_page_title']) . '</title>
  154.     <atom:link href="' . htmlentities($vars2_site_root . $update_config_pages_xml) . '" rel="self" type="application/rss+xml" />
  155.     <link>' . htmlentities($vars2_site_root . $update_config_pages_xml) . '</link>
  156.     <description>' . lib_global2_ensure_amp(lib_global2_remove_tags($pt_regional['site_description'])) . '</description>
  157.     <lastBuildDate>' . date("r") . '</lastBuildDate>
  158.     <language>en</language>
  159.     <sy:updatePeriod>hourly</sy:updatePeriod>
  160.     <sy:updateFrequency>1</sy:updateFrequency>
  161.     <generator>http://wordpress.org/?v=3.0.1</generator>
  162.     ' . $pages_xml_str . '
  163.     </channel>
  164.     </rss>', "w+");
  165.  
  166. /**
  167.  * update root directory index.html
  168.  */
  169. lib_local_write_file($relpa . 'index.html', '<!--#include virtual="/virtual/head.html" -->
  170. <meta name="description" content="' . lib_global2_ensure_amp(lib_global2_remove_tags($pt_regional['site_description'])) . '" />
  171. <link rel="stylesheet" href="/scripts/minify/?type=css&amp;load=homepage" type="text/css" />
  172. <title>' . $vars2_site_root_printable . ' - ' . lib_global2_ensure_amp($pt_regional['root_page_title']) . '</title>
  173. </head>
  174. <body>
  175. <!--#include virtual="/virtual/top.html" -->
  176. <!--#include virtual="/virtual/homepage.html" -->
  177. <!--#include virtual="/virtual/bottom.html" -->
  178. <!--#include virtual="/virtual/counters.html" -->
  179. <!--#include virtual="/virtual/foot.html" -->', "w+");
  180.  
  181. /**
  182.  * update Cache-Control, Expires, Last-Modified
  183.  */
  184. $p = lib_global2_read_local_file($relpa . 'virtual/head.html');
  185. $p = preg_replace("/\<meta http\-equiv\=\"Cache-Control\" content\=\"(.*?)\" \/\>/i", "<meta http-equiv=\"Cache-Control\" content=\"public, max-age=" . $update_config_expires_offset . "\" />", $p);
  186. $p = preg_replace("/\<meta http\-equiv\=\"Expires\" content\=\"(.*?)\" \/\>/i", "<meta http-equiv=\"Expires\" content=\"" . gmdate('D, d M Y H:i:s', time() + $update_config_expires_offset ) . " GMT\" />", $p);
  187. $p = preg_replace("/\<meta http\-equiv\=\"Last\-Modified\" content\=\"(.*?)\" \/\>/i", "<meta http-equiv=\"Last-Modified\" content=\"" . gmdate('D, d M Y H:i:s \G\M\T') . "\" />", $p);
  188. lib_local_write_file($relpa . 'virtual/head.html', $p, "w+");
  189.  
  190. /**
  191.  * clear logs
  192.  */
  193. lib_local_write_file($relpa . 'logs/sniffer.log', '', "w+");
  194. lib_local_write_file($relpa . 'logs/sniffer_robots.log', '', "w+");
  195. lib_local_write_file($relpa . 'logs/ua.log', '', "w+");
  196.  
  197. /**
  198.  * print out
  199.  */
  200. echo 'done!';
  201.  
  202.  
Advertisement
Add Comment
Please, Sign In to add comment