run();
class SearchConsole {
private $_selfURI;
private $_outputPath = 'output/';
private $_gc;
private $_gsw;
private $_error = array();
function __construct()
{
// 古いファイルは消す
$this->garbageCollection();
// ログアウト処理。セッションに記録したアクセストークンを破棄することで再度認証画面へ
if (isset($_GET['logout'])) {
$_SESSION = array();
}
$this->_selfURI = 'http://' . $_SERVER['HTTP_HOST'] . preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
$this->_gc = new Google_Client();
}
public function run()
{
// client_id.jsonを使用
if (file_exists('client_id.json')) {
$this->_gc->setAuthConfigFile('client_id.json');
$this->_gc->setRedirectUri($this->_selfURI);
$this->_gc->addScope(Google_Service_Webmasters::WEBMASTERS_READONLY);
// 認証しているか確認
if (!$this->checkCode() && !$this->checkToken()) {
$this->redirectGoogleOAuth();
} else {
$this->_gsw = new Google_Service_Webmasters($this->_gc);
}
} else {
$this->setError('client_id.jsonがありません');
}
// html出力
$this->getHeader();
if (is_object($this->_gsw)) {
// サイトの一覧か結果の一覧
if (empty($_GET['uri'])) {
$this->displaySiteList();
} else {
$this->displayQueryList();
}
}
$this->displayErrors();
$this->getFooter();
}
public function checkToken()
{
if (isset($_SESSION['access_token'])) {
$this->_gc->setAccessToken($_SESSION['access_token']);
if (!$this->_gc->isAccessTokenExpired()) {
return true;
}
}
return false;
}
public function checkCode()
{
// 認証されcodeがついていた時
if (isset($_GET['code'])) {
$ret = $this->_gc->authenticate($_GET['code']);
if (!isset($ret['error'])) {
$_SESSION['access_token'] = $this->_gc->getAccessToken();
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $this->_selfURI);
die;
} else {
// $this->setError($ret['error_description']);
$this->redirectGoogleOAuth();
}
}
return false;
}
public function redirectGoogleOAuth()
{
// Googleの認証画面へ
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $this->_gc->createAuthUrl());
die;
}
public function setError($str)
{
$this->_error[] = $str;
}
private function displayErrors()
{
echo implode('
', $this->_error);
}
public function displaySiteList()
{
$select = null;
$html = '
' . $h . ' | '; } echo '
---|
' . htmlentities($c, ENT_QUOTES) . ' | '; } echo '
結果がありませんでした
'; } } } public function getHeader() { echo ''; } public function getFooter() { echo ''; } public function garbageCollection() { if (file_exists($this->_outputPath)) { $files = glob($this->_outputPath . '*'); foreach ($files as $file) { if (time() - 86400 > filemtime($file)) { unlink($file); } } } else { mkdir($this->_outputPath); } } public function array2csv($array, $outputPath = null, $outputEncoding = 'sjis-win') { $string = null; if (!empty($array) && is_array($array)) { foreach ($array as $val) { $line = null; if (!is_array($val)) { $val = array($val); } if (!count($val)) { $line[] = ''; } foreach ($val as $v) { $v = strtr($v, array('"' => '""')); if (preg_match("/(\r|\n|\"|'|,)/", $v)) { $line[] = '"' . $v . '"'; } else { $line[] = $v; } } $string .= implode(',', $line) . "\r\n"; } } if (isset($string)) { if (isset($outputEncoding)) { $string = mb_convert_encoding($string, $outputEncoding, 'utf-8'); } if (isset($outputPath)) { file_put_contents($outputPath, $string); } else { return $string; } } else { return false; } } }