2 //YANMAH2 - Yet ANother Mobile Accelerated HatenaHaiku
3 //(c) 2012 YANMAH2 project
4 //This program is freesoftware. you can redistribute it and/ormodify it under the terms of the [GNU Affero GPL v3](http://www.gnu.org/licenses/agpl.html).
6 <?php defined("HELLO") or exit(); ?>
9 function checkNull($array){
10 if (is_array($array)){
11 return array_map('checkNull', $array);
13 if (preg_match('/\0/', $array)){
18 //正しいはてなID=アルファベットで始まり、アルファベットか数字で終わる3文字以上、32文字以内の半角英数字
19 //2012-01-09: DSi の id にも対応。
20 function checkId($element){
21 if (preg_match("/^[a-zA-Z1234567890]{1}[-_a-zA-Z1234567890@]{1,30}[a-zA-Z1234567890]{1} *$/u" ,$element) == 0){
22 echo "なにそれこわい\n(YANMAH2: はてなIDの指定に関するエラー)";
26 function checkHttpUrl($element){
27 if (preg_match("/^s?https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:@&=+$,%#]+$/u", $element) == 0){
28 echo ':return パラメータの値が不正の様な気がします。';
32 function checkKeyword($element){
33 if (strlen(bin2hex($element)) > 511){
34 echo "キーワードが長すぎて入らないです…\n(YANMAH2: キーワードの指定に関するエラー)";
38 //page=n は100までの数値であること。
39 function checkPagenum($element){
40 if (preg_match("/^[1234567890]{1,3}$/u",$element) == 0){
41 echo "数字以外入れちゃだめ\n(YANMAH2: ページ数に関するエラー)";
45 echo "ページ数が大きすぎです…\n(YANMAH2: ページ数に関するエラー)";
49 function checkImagemode($element){
50 if (!preg_match('/^(on|off)$/u', $element)){
51 echo 'んっ!? って思うパラメータは処理しないし。(YANMAH2: 画像モードに関するエラー)';
55 function checkAlbum($element){
56 if (strlen(bin2hex($element)) > 514){
61 function checkEntryid($element){
62 if (preg_match("/^[1234567890]+$/u",$element) === FALSE){
63 echo "エントリさーん、どこですかー\n(YANMAH2: Entry ID に関するエラー)";
67 function checkWord($element){
68 if (strlen(bin2hex($element)) > 511){
69 echo "キーワードが長すぎて入らないです…\n(YANMAH2: キーワードの指定に関するエラー)";
73 function echoHateBxml($object){
75 $ret = $ret . '<dt><a href="';
76 foreach ($object->link as $val){
77 if ($val->attributes()->rel == 'related') $href = $val->attributes()->href;
80 $ret = $ret . '" class="external-link">'.$object->title.'</a></dt>';
81 $ret = $ret . '<dd>'. $object->author->name .'</dd>';
82 $ret = $ret . '<dd>'. $object->summary .'</dd>';
83 $ret = $ret . '<dd>'. $object->issued .'</dd>';
84 $ret = $ret . '</dl>';
87 function checkEntryData($data){
88 $contents = simplexml_load_string($data);
89 $error = libxml_get_last_error();
90 if ($error !== FALSE){
91 $ret['ercode'] = $error->code;
92 $ret['ermes'] = $error->message;
93 $ret['contents'] = $contents;
95 } elseif (isset($contents->error)){
97 $ret['ermes'] = $contents->error;
98 $ret['contents'] = $contents;
101 $ret['contents'] = $contents;
106 function echoCommonHeader($bottom = '#navigation-menu', $return = ''){
107 if (isset($_SESSION['auth']) && $_SESSION['auth'] === TRUE){
108 $common_header = '<div id="com-hdr"><span class="header-log"> <a href="./login.php?log=out" class="navigation-link">ログアウト</a></span> <span class="to-bottom"><a href="#navigation-menu" class="navigation-link" accesskey="#">[#]下へ</a></span></div>';
110 $common_header = '<div id="com-hdr"><span class="header-log"><a href="./login.php?return='.rawurlencode($return).'" class="navigation-link">ログイン</a></span> <span class="to-bottom"><a href="'.$bottom.'" class="navigation-link" accesskey="#">[#]下へ</a></span></div>';
112 return $common_header;
114 function echoCommonFooter($top = '#page'){
116 $common_footer = '<div id="com-ftr"><span class="ftr-cnf"><a href="./config.php?image='.$get_image.'" class="navigation-link">設定</a></span> <span class="to-top"><a href="'.$top.'" class="navigation-link" accesskey="*">[*]上へ</a></span></div><hr />';
117 return $common_footer;
119 function pullLot($ary = array(0, 1)){
123 function echoPhotozou($photo_id, $mgw){
124 $api = 'http://api.photozou.jp/rest/photo_info?photo_id='.$photo_id;
125 $get_xml = getJson($api, FALSE);
126 if ($get_xml['stat'] !== TRUE){
130 $xml = simplexml_load_string($get_xml['body']);
131 $error = libxml_get_last_error();
132 if ($error !== FALSE){
134 } elseif ($xml->attributes()->stat != 'ok'){
137 $thumb = $xml->info->photo->thumbnail_image_url;
138 $title = $xml->info->photo->photo_title;
139 $desc = $xml->info->photo->description;
140 $ret = "<img src=\"{$mgw}".rawurlencode($thumb)."\" alt=\"{$desc}\" />";
142 libxml_clear_errors();
147 function getRequest($init_url, $callback_url, $scope, $conkey, $conkey_sec){
150 require_once 'HTTP/OAuth/Consumer.php';
151 $oauth_con = new HTTP_OAuth_Consumer($conkey, $conkey_sec);
152 $req = new HTTP_Request2();
153 $req->setConfig('ssl_verify_peer', false);
154 $req->setHeader('User-Agent', $user_agent);
155 $con_req = new HTTP_OAuth_Consumer_Request;
156 $con_req->accept($req);
157 $oauth_con->accept($con_req);
158 $oauth_con->getRequestToken($init_url, $callback_url, array('scope' => $scope));
160 $ret['token'] = $oauth_con->getToken();
161 $ret['secret'] = $oauth_con->getTokenSecret();
163 } catch(Exception $e){
164 $ret['error'] = $e->getMessage();
165 $ret['stat'] = $ret['error'];
169 function getAccess($token_url, $reqtoken, $req_sec, $verifier, $conkey, $conkey_sec){
172 require_once 'HTTP/OAuth/Consumer.php';
173 $oauth_con = new HTTP_OAuth_Consumer($conkey, $conkey_sec, $reqtoken, $req_sec);
174 $req = new HTTP_Request2();
175 $req->setConfig('ssl_verify_peer', false);
176 $req->setHeader('User-Agent', $user_agent);
177 $con_req = new HTTP_OAuth_Consumer_Request;
178 $con_req->accept($req);
179 $oauth_con->accept($con_req);
180 $oauth_con->getAccessToken($token_url, $verifier);
181 $lastreq = $req->getLastEvent();
182 $res = $lastreq['data'];
183 // $ret['ob'] = $oauth_con;
184 $ret['token'] = $oauth_con->getToken();
185 $ret['secret'] = $oauth_con->getTokenSecret();
186 $ret['body'] = $res->getBody();
188 } catch(Exception $e){
189 $ret['trace'] = $e->getTrace();
190 $ret['error'] = $e->getMessage();
191 $ret['stat'] = $ret['error'];
195 function getJson($get_url, $use_oauth, $conkey = '', $conkey_sec = '', $acskey = '', $acskey_sec = ''){
197 if ($use_oauth === TRUE){
199 require_once 'HTTP/OAuth/Consumer.php';
200 $oauth_con = new HTTP_OAuth_Consumer($conkey, $conkey_sec);
201 $req = new HTTP_Request2();
202 $req->setConfig('ssl_verify_peer', false);
203 $req->setHeader('User-Agent', $user_agent);
204 $con_req = new HTTP_OAuth_Consumer_Request;
205 $con_req->accept($req);
206 $oauth_con->accept($con_req);
207 $oauth_con->setToken($acskey);
208 $oauth_con->setTokenSecret($acskey_sec);
209 $res = $oauth_con->sendRequest($get_url);
210 $ret['http'] = $res->getStatus();
211 $ret['body'] = preg_replace('/[\x00-\x1f]/u', "", $res->getBody());
213 } catch (Exception $e){
214 $ret['stat'] = $e->getMessage();
219 require_once "HTTP/Request2.php";
220 $req = new HTTP_Request2($get_url);
221 $req->setHeader('User-Agent', $user_agent);
223 $ret['http'] = $res->getStatus();
224 $ret['body'] = preg_replace('/[\x00-\x1f]/u', "", $res->getBody());
226 } catch (Exception $e){
227 $ret['stat'] = $e->getMessage();
232 function postGeneric($api, $postdata = array(), $conkey, $conkey_sec, $acskey, $acskey_sec, $body = ''){
235 require_once 'HTTP/OAuth/Consumer.php';
236 $oauth_con = new HTTP_OAuth_Consumer($conkey, $conkey_sec);
237 $req = new HTTP_Request2();
238 $req->setConfig('ssl_verify_peer', false);
239 $req->setHeader('User-Agent', $user_agent);
240 if ($body != '') $req->setBody($body);
241 // $req->addUpload('file', './apple-touch-icon.png');
242 $con_req = new HTTP_OAuth_Consumer_Request;
243 $con_req->accept($req);
244 $oauth_con->accept($con_req);
245 // $oauth_con->setSignatureMethod("HMAC-SHA1");
246 $oauth_con->setToken($acskey);
247 $oauth_con->setTokenSecret($acskey_sec);
248 $res = $oauth_con->sendRequest($api, $postdata, "POST");
249 $ret['http'] = $res->getStatus();
251 $ret['body'] = $res->getBody();
252 // $ret['session'] = $_SESSION;
254 } catch (Exception $e){
255 $ret['error'] = $e->getMessage();
256 // $ret['session'] = $_SESSION;
257 $ret['stat'] = FALSE;
262 function whatDevice($useragent){
263 if (preg_match("/willcom|mobilephone|ddipocket|pdxgw|astel|docomo|up\.browser|j-phone|vodafone|softbank/i", $useragent)){
265 } elseif (preg_match("/Opera Mobi/i", $useragent)){
267 } elseif (preg_match("/Android (1\.6|1\.5)/i", $useragent)){
268 return 'android-old';
269 } elseif (preg_match("/Android/i", $useragent)){
271 } elseif (preg_match("/Mobile.*Safari/i", $useragent)){