OSDN Git Service

一頁に表示できる最大件数を200に拡大。ユーザーTLとキーワードTLで長い投稿を省略しないように変更。
[yanmah2/YANMAH2-season2-.git] / common_functions.php
1 <?php
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).
5 ?>
6 <?php defined("HELLO") or exit(); ?>
7 <?php
8 //NULL バイトの入力を弾く
9 function checkNull($array){
10         if (is_array($array)){
11                 return array_map('checkNull', $array);
12         }
13         if (preg_match('/\0/', $array)){
14                 echo "NULL ぽ";
15                 exit();
16         }
17 }
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の指定に関するエラー)";
23                 exit();
24         }
25 }
26 function checkHttpUrl($element){
27         if (preg_match("/^s?https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:@&=+$,%#]+$/u", $element) == 0){
28                 echo ':return パラメータの値が不正の様な気がします。';
29                 exit();
30         }
31 }
32 function checkKeyword($element){
33         if (strlen(bin2hex($element)) > 511){
34                 echo "キーワードが長すぎて入らないです…\n(YANMAH2: キーワードの指定に関するエラー)";
35                 exit();
36         }
37 }
38 //page=n は100までの数値であること。
39 function checkPagenum($element){
40         if (preg_match("/^[1234567890]{1,3}$/u",$element) == 0){
41                 echo "数字以外入れちゃだめ\n(YANMAH2: ページ数に関するエラー)";
42                 exit();
43         }
44         if ($element > 100){
45                 echo "ページ数が大きすぎです…\n(YANMAH2: ページ数に関するエラー)";
46                 exit();
47         }
48 }
49 function checkImagemode($element){
50         if (!preg_match('/^(on|off)$/u', $element)){
51                 echo 'んっ!? って思うパラメータは処理しないし。(YANMAH2: 画像モードに関するエラー)';
52                 exit();
53         }
54 }
55 function checkAlbum($element){
56         if (strlen(bin2hex($element)) > 514){
57         echo "はうあ\n";
58         exit();
59 }
60 }
61 function checkEntryid($element){
62         if (preg_match("/^[1234567890]+$/u",$element) === FALSE){
63                 echo "エントリさーん、どこですかー\n(YANMAH2: Entry ID に関するエラー)";
64                 exit();
65         }
66 }
67 function checkWord($element){
68         if (strlen(bin2hex($element)) > 511){
69                 echo "キーワードが長すぎて入らないです…\n(YANMAH2: キーワードの指定に関するエラー)";
70                 exit();
71         }
72 }
73 function echoHateBxml($object){
74   $ret = '<dl>';
75   $ret = $ret . '<dt><a href="';
76   foreach ($object->link as $val){
77         if ($val->attributes()->rel == 'related') $href = $val->attributes()->href;
78   }
79   $ret = $ret . $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>';
85   return $ret;
86 }
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;
94         $ret['stat'] = FALSE;
95   } elseif (isset($contents->error)){
96         $ret['ercode'] = 'x';
97         $ret['ermes'] = $contents->error;
98         $ret['contents'] = $contents;
99         $ret['stat'] = FALSE;
100   } else {
101         $ret['contents'] = $contents;
102         $ret['stat'] = TRUE;
103   }
104   return $ret;
105 }
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>';
109   } else {
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>';
111   }
112   return $common_header;
113 }
114 function echoCommonFooter($top = '#page'){
115   global $get_image;
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;
118 }
119 function pullLot($ary = array(0, 1)){
120   shuffle($ary);
121   return $ary[0];
122 }
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){
127         $ret = '.';
128         return $ret;
129   }
130   $xml = simplexml_load_string($get_xml['body']);
131   $error = libxml_get_last_error();
132   if ($error !== FALSE){
133         $ret = '..';
134   } elseif ($xml->attributes()->stat != 'ok'){
135         $ret = '...';
136   } else {
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}\" />";
141   }
142   libxml_clear_errors();
143   return $ret;
144 }
145 ?>
146 <?php
147 function getRequest($init_url, $callback_url, $scope, $conkey, $conkey_sec){
148   global $user_agent;
149   try{
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));
159   $ret = array();
160   $ret['token'] = $oauth_con->getToken();
161   $ret['secret'] = $oauth_con->getTokenSecret();
162   $ret['stat'] = TRUE;
163   } catch(Exception $e){
164   $ret['error'] = $e->getMessage();
165   $ret['stat'] = $ret['error'];
166   }
167   return $ret;
168 }
169 function getAccess($token_url, $reqtoken, $req_sec, $verifier, $conkey, $conkey_sec){
170   global $user_agent;
171   try{
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();
187   $ret['stat'] = TRUE;
188   } catch(Exception $e){
189   $ret['trace'] = $e->getTrace();
190   $ret['error'] = $e->getMessage();
191   $ret['stat'] = $ret['error'];
192   }
193   return $ret;
194 }
195 function getJson($get_url, $use_oauth, $conkey = '', $conkey_sec = '', $acskey = '', $acskey_sec = ''){
196 global $user_agent;
197 if ($use_oauth === TRUE){
198   try {
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());
212         $ret['stat'] = TRUE;
213   } catch (Exception $e){
214         $ret['stat'] = $e->getMessage();
215   }
216   return $ret;
217 } else {
218   try {
219         require_once "HTTP/Request2.php";
220         $req = new HTTP_Request2($get_url);
221         $req->setHeader('User-Agent', $user_agent);
222         $res = $req->send();
223         $ret['http'] = $res->getStatus();
224         $ret['body'] = preg_replace('/[\x00-\x1f]/u', "", $res->getBody());
225         $ret['stat'] = TRUE;
226   } catch (Exception $e){
227         $ret['stat'] = $e->getMessage();
228   }
229   return $ret;
230 }
231 }
232 function postGeneric($api, $postdata = array(), $conkey, $conkey_sec, $acskey, $acskey_sec, $body = ''){
233   global $user_agent;
234   try {
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();
250 //      var_dump($http);
251         $ret['body'] = $res->getBody();
252 //      $ret['session'] = $_SESSION;
253         $ret['stat'] = TRUE;
254   } catch (Exception $e){
255         $ret['error'] = $e->getMessage();
256 //      $ret['session'] = $_SESSION;
257         $ret['stat'] = FALSE;
258   }
259   return $ret;
260 }
261
262 function whatDevice($useragent){
263   if (preg_match("/willcom|mobilephone|ddipocket|pdxgw|astel|docomo|up\.browser|j-phone|vodafone|softbank/i", $useragent)){
264         return 'ktai';
265   } elseif (preg_match("/Opera Mobi/i", $useragent)){
266         return 'iphone';
267   } elseif (preg_match("/Android (1\.6|1\.5)/i", $useragent)){
268         return 'android-old';
269   } elseif (preg_match("/Android/i", $useragent)){
270         return 'android';
271   } elseif (preg_match("/Mobile.*Safari/i", $useragent)){
272         return 'iphone';
273   } else {
274         return 'generic';
275   }
276 }
277 ?>