OSDN Git Service

[new feature] Generation Gap master v1.1.0
authorUser <taka0125.biz@gmail.com>
Sat, 20 Feb 2010 19:41:38 +0000 (04:41 +0900)
committerUser <taka0125.biz@gmail.com>
Sat, 20 Feb 2010 19:41:38 +0000 (04:41 +0900)
lib/Core/Exception/ValidationException.class.php [new file with mode: 0644]
lib/Core/Http/AgentTemplate.class.php [new file with mode: 0644]
lib/Core/Http/CurlAgent.class.php [new file with mode: 0644]
lib/Core/Http/Error.class.php [new file with mode: 0644]
lib/Core/Http/Request.class.php [new file with mode: 0644]
lib/Core/Http/Response.class.php [new file with mode: 0644]
lib/Core/require.php [new file with mode: 0644]
lib/require.php [new file with mode: 0644]

diff --git a/lib/Core/Exception/ValidationException.class.php b/lib/Core/Exception/ValidationException.class.php
new file mode 100644 (file)
index 0000000..606b142
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP Version 5                                                        |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2010 Takahiro Ooishi                                   |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license,       |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available through the world-wide-web at the following url:           |
+// | http://www.php.net/license/3_0.txt.                                  |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+abstract class Core_ValidationException extends Exception
+{
+    function __construct($error_value, $message = null, $code = 0)
+    {
+        if (is_string($message)) {
+            $message .= '['.@var_export($error_value, true).']';
+        }
+        parent::__construct($message, $code);
+    }
+}
diff --git a/lib/Core/Http/AgentTemplate.class.php b/lib/Core/Http/AgentTemplate.class.php
new file mode 100644 (file)
index 0000000..0e77c20
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP Version 5                                                        |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2010 Takahiro Ooishi                                   |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license,       |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available through the world-wide-web at the following url:           |
+// | http://www.php.net/license/3_0.txt.                                  |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once 'Http/Error.class.php';
+
+abstract class Core_Http_AgentTemplate
+{
+    protected $error;
+
+    protected $options;
+    protected $timeout;
+
+    const DefaultTimeout = 300;
+
+    abstract function send($url, $request);
+    abstract protected function open();
+    abstract protected function close();
+    abstract protected function setOptions($options);
+    abstract function setOption($name, $option);
+    abstract function setBasicAuthenticationAccount($user, $password);    
+    function __construct($options = array(), $timeout = self::DefaultTimeout)
+    {
+        $this->setOptions($options);
+        $this->setTimeout($timeout);
+        $this->open();
+        $this->error = new Http_Error();
+    }
+
+    function __destruct()
+    {
+        $this->close();
+    }
+
+    function options()
+    {
+        return $this->options;
+    }
+
+    function setTimeout($timeout)
+    {
+        if (!is_numeric($timeout)) throw new ValidationException($timeout, 'Invalid Timeout.');
+        $timeout = intval($timeout);
+        if ($timeout < 1) throw new ValidationException($timeout, 'Invalid Timeout.');
+        $this->timeout = $timeout;
+    }
+
+    function isError()
+    {
+        return $this->error->isError();
+    }
+
+    function getError()
+    {
+        return $this->error->get();
+    }
+
+    protected function clearError()
+    {
+        return $this->error->clear();
+    }
+}
diff --git a/lib/Core/Http/CurlAgent.class.php b/lib/Core/Http/CurlAgent.class.php
new file mode 100644 (file)
index 0000000..8690cdf
--- /dev/null
@@ -0,0 +1,135 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP Version 5                                                        |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2010 Takahiro Ooishi                                   |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license,       |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available through the world-wide-web at the following url:           |
+// | http://www.php.net/license/3_0.txt.                                  |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once 'Http/AgentTemplate.class.php';
+require_once 'Http/Response.class.php';
+
+abstract class Core_Http_CurlAgent extends Http_AgentTemplate
+{
+    protected $connection = null;
+
+    protected function open()
+    {
+        $this->connection = curl_init();
+    }
+
+    protected function close()
+    {
+        if ($this->isNotConnected()) return;
+        curl_close($this->connection);
+    }
+
+    function setBasicAuthenticationAccount($user, $password)
+    {
+        $this->setOption(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
+        $this->setOption(CURLOPT_USERPWD, "{$user}:{$password}");
+        return true;
+    }
+
+    function setOptions($options)
+    {
+        $this->options = array(
+            CURLOPT_DNS_USE_GLOBAL_CACHE => false, // グローバル DNS キャッシュを利用しない
+            CURLOPT_NOPROGRESS => true,            // 進捗確認なし
+            CURLOPT_TIMEOUT => $this->timeout,     // タイムアウト(秒)
+            CURLOPT_VERBOSE => false,              // 詳細表示なし
+            CURLOPT_HEADER => false,               // ヘッダ出力なし
+            CURLOPT_FAILONERROR => true,           // 400 以上のコードが返ってきた際に処理失敗
+            CURLOPT_RETURNTRANSFER => true,        // 結果を文字列とし格納
+            CURLOPT_FILETIME => true,              // ファイルの更新日時取得
+            CURLOPT_TIMECONDITION => true,
+            CURLOPT_FOLLOWLOCATION => true,        // 301に対応
+            CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)', // ユーザエージェント
+        );
+
+        if ($this->isInvalidOptions($options)) return true;
+
+        foreach ($options as $name => $option) {
+            $this->options[$name] = $option;
+        }
+        return true;
+    }
+
+    function setOption($name, $option)
+    {
+        $this->options[$name] = $option;
+    }
+
+    function send($url, $request)
+    {
+        if ($this->isInvalidRequest($request)) throw new ValidationException($request, 'Invalid Request.');
+
+        $this->prepare($url, $request);
+    
+        $contents = curl_exec($this->connection);
+        $infomations = curl_getinfo($this->connection);
+        return new Http_Response($contents, $infomations);
+    }
+
+    protected function prepare($url, $request)
+    {
+        $url = $request->buildUrl($url);
+
+        if ($request->isUseCookie()) {
+            $this->setCookie($request);
+        }
+
+        $this->setOption(CURLOPT_URL, $url);
+
+        if ($request->existsPostParams()) {
+            $this->setPostOptions($request);
+        } else {
+            $this->setOption(CURLOPT_POST, false);
+        }
+
+        curl_setopt_array($this->connection, $this->options);
+    }
+
+    protected function setCookie($request)
+    {
+        $this->setOption(CURLOPT_COOKIEJAR, $request->cookieFile());
+        $this->setOption(CURLOPT_COOKIEFILE, $request->cookieFile());
+    }
+
+    protected function setPostOptions($request)
+    {
+        $queries = http_build_query($request->postParams());
+        $this->setOption(CURLOPT_POSTFIELDS, $queries);
+        $this->setOption(CURLOPT_POST, true);
+    }
+
+    protected function isInvalidOptions($params)
+    {
+        if (!is_array($params)) return true;
+        return false;
+    }
+
+    protected function isInvalidRequest($request)
+    {
+        if (!is_a($request, 'Http_Request')) return true;
+        return false;
+    }
+
+    protected function isNotConnected()
+    {
+        if (is_null($this->connection)) return true;
+        return false;
+    }
+}
diff --git a/lib/Core/Http/Error.class.php b/lib/Core/Http/Error.class.php
new file mode 100644 (file)
index 0000000..98a0b37
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP Version 5                                                        |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2010 Takahiro Ooishi                                   |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license,       |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available through the world-wide-web at the following url:           |
+// | http://www.php.net/license/3_0.txt.                                  |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+abstract class Core_Http_Error
+{
+    protected $errorFlg;
+    protected $faultString;
+
+    function __construct()
+    {
+        $this->errorFlg = false;
+        $this->faultString = '';
+    }
+
+    function isError()
+    {
+        return $this->errorFlg;
+    }
+
+    function get()
+    {
+        if (!$this->errorFlg) return '';
+        return "faultString={$this->faultString}";
+    }
+
+    function set($fault_string)
+    {
+        $this->errorFlg = true;
+        $this->faultString = $fault_string;
+    }
+
+    function clear()
+    {
+        $this->errorFlg = false;
+        $this->faultString = '';
+    }
+}
diff --git a/lib/Core/Http/Request.class.php b/lib/Core/Http/Request.class.php
new file mode 100644 (file)
index 0000000..19e177e
--- /dev/null
@@ -0,0 +1,132 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP Version 5                                                        |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2010 Takahiro Ooishi                                   |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license,       |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available through the world-wide-web at the following url:           |
+// | http://www.php.net/license/3_0.txt.                                  |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+abstract class Core_Http_Request
+{
+    protected $getParams;
+    protected $postParams;
+    protected $cookieFile;
+
+    function __construct($get_params = array(), $post_params = array(), $cookie_file = null)
+    {
+        $this->setGetParams($get_params);
+        $this->setPostParams($post_params);
+        $this->setCookieFile($cookie_file);
+    }
+
+    function __destruct()
+    {
+        @unlink($this->cookieFile);
+    }
+
+    function getParams()
+    {
+        return $this->getParams;
+    }
+
+    function postParams()
+    {
+        return $this->postParams;
+    }
+
+    function cookieFile()
+    {
+        return $this->cookieFile;
+    }
+
+    function buildUrl($url)
+    {
+        if (!$this->existsGetParams()) return $url;
+
+        $query_string = http_build_query($this->getParams);
+        $items = parse_url($url);
+
+        if ($this->existsQueryStrings($items)) {
+            $query_string = "?{$items['query']}&{$query_string}";
+        } else {
+            $query_string = "?{$query_string}";
+        }
+
+        return "{$items['scheme']}://{$items['host']}{$items['path']}{$query_string}";
+    }
+
+    function isUseCookie()
+    {
+        if (!is_string($this->cookieFile)) return false;
+        if (strlen($this->cookieFile) < 1) return false;
+        return true;
+    }
+
+    function existsGetParams()
+    {
+        return $this->existsParams($this->getParams);
+    }
+
+    function existsPostParams()
+    {
+        return $this->existsParams($this->postParams);
+    }
+
+    protected function existsParams($params)
+    {
+        if (!is_array($params)) return false;
+        if (count($params) < 1) return false;
+        return true;
+    }
+
+    protected function existsQueryStrings($items)
+    {
+        if (!isset($items['query'])) return false;
+        if (strlen($items['query']) < 1) return false;
+        return true;
+    }
+
+    protected function setGetParams($params)
+    {
+        if ($this->isInvalidParams($params)) throw new ValidationException($params, 'Invalid GetParams.');
+        $this->getParams = $params;
+    }
+
+    protected function setPostParams($params)
+    {
+        if ($this->isInvalidParams($params)) throw new ValidationException($params, 'Invalid PostParams.');
+        $this->postParams = $params;
+    }
+
+    protected function setCookieFile($cookie_file)
+    {
+        if ($this->isInvalidCookieFile($cookie_file)) throw new ValidationException($cookie_file, 'Invalid CookieFile.');
+        $this->cookieFile = $cookie_file;
+    }
+
+    protected function isInvalidParams($params)
+    {
+        if (!is_array($params)) return true;
+        return false;
+    }
+
+    protected function isInvalidCookieFile($cookie_file)
+    {
+        if (is_null($cookie_file)) return false;
+        if (!is_string($cookie_file)) return true;
+        if (strlen($cookie_file) < 1) return true;
+        return false;
+    }
+}
diff --git a/lib/Core/Http/Response.class.php b/lib/Core/Http/Response.class.php
new file mode 100644 (file)
index 0000000..04d0931
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP Version 5                                                        |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2010 Takahiro Ooishi                                   |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license,       |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available through the world-wide-web at the following url:           |
+// | http://www.php.net/license/3_0.txt.                                  |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+abstract class Core_Http_Response
+{
+    protected $contents;
+    protected $informations;
+
+    function __construct($contents, $informations = array())
+    {
+        $this->setContents($contents);
+        $this->setInformations($informations);
+    }
+
+    function contents()
+    {
+        return $this->contents;
+    }
+
+    function informations()
+    {
+        return $this->informations;
+    }
+
+    protected function setContents($contents)
+    {
+        if ($this->isInvalidContents($contents)) throw new ValidationException($contents, 'Invalid Contents.');
+        $this->contents = $contents;
+    }
+
+    protected function setInformations($informations)
+    {
+        if ($this->isInvalidInformations($informations)) throw new ValidationException($informations, 'Invalid Informations.');
+        $this->informations = $informations;
+    }
+
+    protected function isInvalidContents($contents)
+    {
+        return false;
+    }
+
+    protected function isInvalidInformations($informations)
+    {
+        if (!is_array($informations)) return true;
+        return false;
+    }
+}
diff --git a/lib/Core/require.php b/lib/Core/require.php
new file mode 100644 (file)
index 0000000..0848510
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP Version 5                                                        |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2010 Takahiro Ooishi                                   |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license,       |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available through the world-wide-web at the following url:           |
+// | http://www.php.net/license/3_0.txt.                                  |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__).'/../');
+
+require_once 'Core/Exception/ValidationException.class.php';
+require_once 'Core/Http/AgentTemplate.class.php';
+require_once 'Core/Http/Request.class.php';
+require_once 'Core/Http/Error.class.php';
+require_once 'Core/Http/Response.class.php';
+require_once 'Core/Http/CurlAgent.class.php';
diff --git a/lib/require.php b/lib/require.php
new file mode 100644 (file)
index 0000000..d2bcb73
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP Version 5                                                        |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2010 Takahiro Ooishi                                   |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 3.0 of the PHP license,       |
+// | that is bundled with this package in the file LICENSE, and is        |
+// | available through the world-wide-web at the following url:           |
+// | http://www.php.net/license/3_0.txt.                                  |
+// | If you did not receive a copy of the PHP license and are unable to   |
+// | obtain it through the world-wide-web, please send a note to          |
+// | license@php.net so we can mail you a copy immediately.               |
+// +----------------------------------------------------------------------+
+// | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once dirname(__FILE__).'/Core/require.php';
+
+require_once 'Exception/ValidationException.class.php';
+require_once 'Http/AgentTemplate.class.php';
+require_once 'Http/Request.class.php';
+require_once 'Http/Error.class.php';
+require_once 'Http/Response.class.php';
+require_once 'Http/CurlAgent.class.php';