-
Notifications
You must be signed in to change notification settings - Fork 0
/
sent_news_vk.php
executable file
·149 lines (133 loc) · 7.15 KB
/
sent_news_vk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/php
<?php
// переход в корневую папку сайта
define('DEBUG', !empty($_SERVER['SERVER_NAME']) && preg_match('/.+\.int$/i', $_SERVER['SERVER_NAME']) ? true : false);
$root = DEBUG ? realpath("") : realpath('/home/bsn/sites/bsn.ru/public_html/' );
if(defined("PHP_OS")) $os = PHP_OS; else $os = php_uname();
if(strtolower(substr( $os, 0, 3 ) ) == "win" ) $root = str_replace( "\\", '/', $root );
define( "ROOT_PATH", $root );
chdir($root);
include('cron/robot/robot_functions.php'); // функции из крона
mb_internal_encoding('UTF-8');
setlocale(LC_ALL, 'ru_RU.UTF-8');
mb_regex_encoding('UTF-8');
//запись всех ошибок в лог
$error_log = ROOT_PATH.'/cron/mailers/vk_error.log';
file_put_contents($error_log,'');
ini_set('error_log', $error_log);
ini_set('log_errors', 'On');
if (is_running($_SERVER['PHP_SELF'])) die('Already running');
/**
* Обработка новых объектов
*/
// подключение классов ядра
include('includes/class.config.php'); // Config (конфигурация сайта)
Config::Init();
include('includes/class.convert.php'); // Convert, Validate (конвертирование, проверки валидности)
include('includes/class.storage.php'); // Session, Cookie, Responce, Request
include('includes/functions.php'); // функции из модуля
Session::Init();
Request::Init();
Cookie::Init();
include('includes/class.db.mysqli.php'); // mysqli_db (база данных)
$db = new mysqli_db(Config::$values['mysql']['host'], Config::$values['mysql']['user'], Config::$values['mysql']['pass']);
$db->querys("set names ".Config::$values['mysql']['charset']);
require_once('includes/class.host.php');
require_once('includes/class.template.php');
require_once('includes/class.email.php');
require_once('includes/class.content.php');
require_once('includes/class.opinions.php');
require('includes/class.memcache.php'); // MCache (memcached, кеширование в памяти)
require_once('includes/class.estate.statistics.php');
require_once('includes/class.vk.php');
$memcache = new MCache(Config::$values['memcache']['host'], Config::$values['memcache']['port']);
define ('__SERVER_NAME__',$_SERVER['SERVER_NAME']);
define('IS_DEBUG', preg_match('/.+\.int/i', __SERVER_NAME__) ? true : false);
$bsn_url = IS_DEBUG ? "https://www.bsnnew.int/" : "http://st.bsn.ru/";
$filename = ROOT_PATH.'/cron/mailers/vk_params.txt';
// вспомогательные таблицы модуля
$sys_tables = Config::$sys_tables;
$_secret = '9NWfVGpVALvssJz1cu0W';
$_client_id = 2816223;
$scopes = array('offline','wall','group','photos');
$get_parameters = Request::GetParameters();
if(!empty($get_parameters['get_code'])){
$auth_link ='http://oauth.vk.com/authorize?'.http_build_query(array(
'client_id' => $_client_id,
'scope' => implode(',', $scopes),
'redirect_uri' => 'http://api.vk.com/blank.html',
'display' => 'page',
'response_type' => 'code'
));
Host::Redirect($auth_link);
} else if(!empty($get_parameters['get_token'])){
$post_parameters = Request::GetParameters(METHOD_POST);
if(!empty($post_parameters)){
$token = json_decode(file_get_contents('https://oauth.vk.com/access_token?'.http_build_query(array(
'client_id' => $_client_id,
'client_secret' => $_secret,
'code' => $post_parameters['code'],
'redirect_uri' => 'http://api.vk.com/blank.html'
))));
$fpointer = fopen($filename, 'w');
fwrite($fpointer, 'code:' . $post_parameters['code'] . ';token:' . $token->access_token . ';');
fclose($fpointer);
chmod($filename, 0666);
echo 'Новый токен получен!';
} else {
echo "<form method=\"POST\" action=\"/cron/mailers/sent_news_vk.php?get_token=1\"><input type=\"text\" value=\"\" name=\"code\"/><input type=\"submit\" value=\"Отправить\" /></form>";
}
} else {
$content = file_get_contents($filename);
preg_match_all("|code\:([a-z0-9]{1,})\;|msiU", $content, $code);
preg_match_all("|token\:([a-z0-9]{1,})\;|msiU", $content, $token);
if(!empty($token[1][0])) $_access_token = $token[1][0];
else die('wrong filename');
$vk_posting = new vk($_access_token, $_client_id, 33058562);
$_album_id = '190833663'; //$vk_posting->create_album('Новости BSN.ru','');
$auth_link ='http://oauth.vk.com/authorize?'.http_build_query(array(
'client_id' => $_client_id,
'scope' => implode(',', $scopes),
'redirect_uri' => 'http://api.vk.com/blank.html',
'display' => 'page',
'response_type' => 'code'
));
$news = new NewsContent();
$list = $news->getNewsList(30,0,false,false,false,'vkontakte_feed = 1 AND published = 1');
foreach($list as $k=>$item){
$photo_id = false;
$photo_error = false;
if(!empty($item['photo'])){
echo $file = $root . '/' . Config::$values['img_folders']['news'].'/big/'.$item['subfolder'].'/'.$item['photo'];
if(file_exists($file)) $photo_id = $vk_posting->upload_photo($file,$_album_id,html_entity_decode(strip_tags($item['content_short'])));
else $photo_error = true;
}
if(empty($photo_error))
$status = $vk_posting->post(html_entity_decode($item['title'])."\n".'https://www.bsn.ru/news/'.$item['category_code'].'/'.$item['region_code'].'/'.$item['id'].'/',$photo_id);
if(empty($status)) {
$mailer = new EMailer('mail');
$eml_tpl = new Template('sent.news.vk.html', 'cron/mailers/');
$html = $eml_tpl->Processing();
$html = iconv('UTF-8', $mailer->CharSet.'//IGNORE', $html);
// параметры письма
$mailer->Body = $html;
$mailer->Subject = iconv('UTF-8', $mailer->CharSet, 'Неудачный постинг новостей ВК'.(!empty($photo_error) ? ", нет картинки ".$file : ""));
$mailer->IsHTML(true);
$mailer->AddAddress('[email protected]');
$mailer->AddAddress('[email protected]');
$mailer->AddAddress('[email protected]');
$mailer->From = '[email protected]';
$mailer->FromName = iconv('UTF-8', $mailer->CharSet, "BSN.ru");
// попытка отправить
$mailer->Send();
}
else $db->querys("UPDATE ".$sys_tables['news']." SET `vkontakte_feed` = 3 WHERE vkontakte_feed = 1 AND published = 1 AND datetime <=NOW() AND id = ?", $item['id']);
}
}
//если были ошибки выполнения скрипта
if(filesize($error_log)>10){
$error_log_text = '<br><br>Логи ошибок <br><font size="1">';
$error_log_text .= fread(fopen($error_log, "r"), filesize($error_log));
$error_log_text .= '</font>';
} else $error_log_text = "";
?>