超絶基礎だけどめも。
$str = 'aaaa,bbbb,cccc,'; $res = substr($str, 0, -1); // 文字列がマルチバイトの場合はmb_substr()を使用 var_dump($res);
文字列のループ連結後、末端を丸め込む際に使用。
超絶基礎だけどめも。
$str = 'aaaa,bbbb,cccc,'; $res = substr($str, 0, -1); // 文字列がマルチバイトの場合はmb_substr()を使用 var_dump($res);
文字列のループ連結後、末端を丸め込む際に使用。
Reply-Toとか細かい設定はないけど、よく使う(と思われる)部分をシンプルに実装。
<?php
/**
* sendmailを用いたシンプルなメール送信用クラス
* 単純に送信したい場合は以下の通り
* ※to, cc, bcc, のいずれか一つの指定は必須
*
* $mail = new simpleMailTransmission();
*
* $res = $mail
* ->to('to@local.host') // 必要に応じて
* ->cc('cc@local.host') // 必要に応じて
* ->Bcc('bcc@local.host') // 必要に応じて
* ->from('from@local.host') // 必須
* ->subject('タイトルを指定')
* ->send('本文を指定');
*
*/
class simpleMailTransmission {
// 送信先
protected $to = array();
// メールタイトル
protected $subject = '';
// メール本文
protected $message = '';
// ヘッダー情報
protected $header = '';
// 送信先一時格納変数(複数可)
protected $_to = array();
// 送信元設定
protected $_from = array();
// Cc格納変数
protected $_cc = array();
// Bcc格納変数
protected $_bcc = array();
// ヘッダー情報一時格納変数
protected $_header = '';
// テンプレートファイルパス
protected $_template = '';
// テンプレートに渡す変数
protected $_viewVars = array();
/**
* 送信先のセット
* @param unknown_type $email
* @param unknown_type $name
* @return multitype:|mailTerminal
*/
public function to($email = null, $name = null) {
if($email === null) {
return $this->_to;
}
return $this->_setEmail('_to', $email, $name);
}
/**
* 送信先の追加
* @param unknown_type $email
* @param unknown_type $name
* @return mailTerminal
*/
public function addTo($email, $name = null) {
return $this->_addEmail('_to', $email, $name);
}
/**
* Ccのセット
* @param unknown_type $email
* @param unknown_type $name
* @return multitype:|mailTerminal
*/
public function cc($email = null, $name = null) {
if($email === null) {
return $this->_cc;
}
return $this->_setEmail('_cc', $email, $name);
}
/**
* Ccの追加
* @param unknown_type $email
* @param unknown_type $name
* @return mailTerminal
*/
public function addCc($email, $name = null) {
return $this->_addEmail('_cc', $email, $name);
}
/**
* Bccのセット
* @param unknown_type $email
* @param unknown_type $name
* @return multitype:|mailTerminal
*/
public function bcc($email = null, $name = null) {
if($email === null) {
return $this->_bcc;
}
return $this->_setEmail('_bcc', $email, $name);
}
/**
* Bccの追加
* @param unknown_type $email
* @param unknown_type $name
* @return mailTerminal
*/
public function addBcc($email, $name = null) {
return $this->_addEmail('_bcc', $email, $name);
}
/**
* セット用メソッド
* @param unknown_type $varName
* @param unknown_type $email
* @param unknown_type $name
* @return mailTerminal
*/
protected function _setEmail($varName, $email, $name) {
if(self::email($email)) {
if($name === null) {
$name = $email;
}
$this->{$varName} = array($email => $name);
}
return $this;
}
/**
* 追加用メソッド
* @param unknown_type $varName
* @param unknown_type $email
* @param unknown_type $name
* @return mailTerminal
*/
protected function _addEmail($varName, $email, $name) {
if(self::email($email)) {
if($name === null) {
$name = $email;
}
$this->{$varName}[$email] = $name;
}
return $this;
}
/**
* 送信元の設定
* @param unknown_type $email
* @param unknown_type $name
* @return multitype:|mailTerminal
*/
public function from($email = null, $name = null) {
if($email === null) {
return $this->_from;
}
return $this->_setEmailSingle('_from', $email, $name);
}
/**
* 単一の値であることを保障するためのセット関数
* @param unknown_type $varName
* @param unknown_type $email
* @param unknown_type $name
* @return mailTerminal
*/
protected function _setEmailSingle($varName, $email, $name) {
$current = $this->{$varName};
$this->_setEmail($varName, $email, $name);
if(count($this->{$varName}) !== 1) {
$this->{$varName} = $current;
}
return $this;
}
/**
* メールタイトルのセット
* @param unknown_type $subject
* @return string|mailTerminal
*/
public function subject($subject = null) {
if($subject === null) {
return $this->subject;
}
$this->subject = (string)$subject;
return $this;
}
/**
* テンプレートファイルパスの指定
* @param unknown_type $template
* @return string|mailTerminal
*/
public function template($template = false) {
if($template === false) {
return $this->_template;
}
$this->_template = $template;
return $this;
}
/**
* テンプレートファイルに渡す変数を設定
* @param unknown_type $viewVars
* @return multitype:|mailTerminal
*/
public function viewVars($viewVars = null) {
if($viewVars === null) {
return $this->_viewVars;
}
$this->_viewVars = array_merge($this->_viewVars, (array)$viewVars);
return $this;
}
/**
* メールアドレスのバリデーション設定
* @param unknown_type $check
* @return boolean
*/
protected function email($check) {
$hostName = '(?:[_a-z0-9][-_a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,})';
$regex = '/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@'.$hostName.'$/i';
return self::_check($check, $regex);
}
/**
* メールの送信開始トリガー
* @param unknown_type $plainMessage
* @return string|Ambigous <boolean, multitype:boolean >
*/
public function send($plainMessage = null) {
if(empty($this->_from)) {
return 'From is not specified.';
}
if(empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) {
return 'You need to specify at least one destination for to, cc or bcc.';
}
$this->header = $this->createHeader();
$this->to = $this->createTo();
if(!file_exists($this->_template)) {
$this->message = self::mce($plainMessage);
} else {
$this->message = self::mce($this->createMessage());
}
return self::_send();
}
/**
* 送信処理
* @return boolean|multitype:boolean
*/
protected function _send() {
mb_language('ja');
mb_internal_encoding('UTF-8');
if(empty($this->to)) {
return mb_send_mail(null, $this->subject, $this->message, $this->header);
} else {
$_errors = array();
foreach($this->to as $to) {
if(!mb_send_mail($to, $this->subject, $this->message, $this->header)) $_errors[$to] = false;
}
if(empty($_errors)) return true;
return $_errors;
}
}
/**
* 追加ヘッダーの生成
* @return string
*/
protected function createHeader() {
foreach($this->_from as $email => $name) $this->_header .= 'From: '.self::mem($name).' <'.$email.'>'."\n";
if(!empty($this->_cc)) {
$this->_header .= 'Cc: ';
foreach($this->_cc as $email => $name) {
$this->_header .= self::mem($name).' <'.$email.'>'."\n,";
}
$this->_header = self::trimLastChar($this->_header);
}
if(!empty($this->_bcc)) {
$this->_header .= 'Bcc: ';
foreach($this->_bcc as $email => $name) {
$this->_header .= self::mem($name).' <'.$email.'>'."\n,";
}
$this->_header = self::trimLastChar($this->_header);
}
return $this->_header;
}
/**
* 送信先ヘッダーの生成
* @return multitype:string
*/
protected function createTo() {
$t = array();
if(!empty($this->_to)) {
foreach($this->_to as $email => $name) {
$t[] = self::mem($name).' <'.$email.'>';
}
}
return $t;
}
/**
* テンプレートより本文の生成
* @return Ambigous <string, mixed>
*/
protected function createMessage() {
$b = '';
$tmp = file_get_contents($this->_template);
if($tmp) {
$varArray = array();
if(!empty($this->_viewVars)) {
foreach($this->_viewVars as $varName => $value) {
$varArray['{%'.$varName.'%}'] = $value;
}
}
$b = str_replace(array_keys($varArray) , array_values($varArray) , $tmp);
}
return $b;
}
/**
* バリデーションの実行
* @param unknown_type $check
* @param unknown_type $regex
* @return boolean
*/
protected function _check($check, $regex) {
if(preg_match($regex, $check)) return true;
return false;
}
protected function mem($var) {
return mb_encode_mimeheader($var);
}
protected function mce($var) {
return mb_convert_encoding($var, 'ISO-2022-JP');
}
protected function trimLastChar($var) {
return substr($var, 0, -1);
}
}
基本的な使い方はクラス上部のコメントアウトを参照。
テンプレートファイルを用いる場合の使用例は以下の通り。
■テンプレートファイル
文字列で置換したい部分を「{%…%}」で囲い定義する。
※.txtファイルだろうが無拡張子だろうが読み込めれば何でもOK
{%name%} 様
テストメッセージテストメッセージテストメッセージ
テストメッセージテストメッセージテストメッセージ
テストメッセージテストメッセージテストメッセージ
担当 {%charge%}
{%url%}
{%footer%}
PHP側から以下のように変数とテンプレートをセットし、メールを送信する。
$params = array(
'name' => 'テスト太郎',
'charge' => 'テスト花子',
'url' => 'http://localhost.com',
'footer' => '株式会社テストテスト'
);
$mail = new simpleMailTransmission();
$res = $mail
->to('to@local.host')
->from('from@local.host')
->subject('テストメールなう。')
->viewVars($params)
->template('/path/to/template.txt')
->send();
イイ感じ。
今更ながら必要だったので作成。
以下のページを参考にさせて頂きました。
http://istb16.wordpress.com/2011/09/11/niconico_download_for_php/
goto構文使ってるのでphp5.3以上じゃないと動作しません。
<?php
/**
* ニコニコ動画保存用クラス
*
* $nico = new niconicoMovieDownloader();
* $nico->email = 'ログインメールアドレス';
* $nico->password = 'ログインパスワード';
* $nico->saveDir = '動画保存用ディレクトリ'; // 末端DS無し
* $nico->tmpDir = '作業用ディレクトリ'; // 末端DS無し
* $res = $nico->download('sm*******');
*
* @author Owner
*
*/
class niconicoMovieDownloader {
// ログイン用メールアドレス
public $email = '';
// ログインパスワード
public $password = '';
// 動画保存ディレクトリ(末端スラッシュ無し)
public $saveDir = '';
// 一時作業ディレクトリ(末端スラッシュ無し)
public $tmpDir = '';
// 動画ID(sm****)
protected $mid = '';
// 送信ヘッダーの入れ物
protected $headers = array();
// エラーメッセージの入れ物
protected $errors = array();
// input要素の名前設定(メールアドレス欄)
protected $formNameEmail = 'mail_tel';
// input要素の名前設定(パスワード欄)
protected $formNamePassword = 'password';
// input要素の名前設定(移動先URL欄)
protected $formNameNextUrl = 'next_url';
// ニコニコ動画ログインポストURL。間違えると動画ページを取得できないので注意。
protected $loginUrl = 'https://secure.nicovideo.jp/secure/login?site=niconico';
// 動画ページのURL設定
protected $watchUrl = 'http://www.nicovideo.jp/watch';
// ダウンロードURLを割り出すAPIの設定
protected $apiUrl = 'http://flapi.nicovideo.jp/api/getflv';
/**
* インスタンス生成時、送信ヘッダーを設定
*/
public function __construct() {
$this->headers[] = 'Connection: keep-alive';
$this->headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$this->headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36';
$this->headers[] = 'Referer: http://www.nicovideo.jp/';
$this->headers[] = 'Accept-Language: ja,en-US;q=0.8,en;q=0.6';
}
/**
* ダウンロードの実行
* @param string $mid
* @return multitype:|boolean
*/
public function download($mid = null) {
$this->mid = $mid;
$this->checkParams();
if(!empty($this->errors)) return $this->errors;
// cookieを持ちまわすためのファイルパスを指定
$tmpCookiePath = $this->tmpDir . DIRECTORY_SEPARATOR . '_cookie';
$tmpWorkPath = $this->tmpDir . DIRECTORY_SEPARATOR . md5(microtime());
$headerFilePath = $this->tmpDir . DIRECTORY_SEPARATOR . 'h_' . $this->mid;
self::tp($tmpCookiePath);
self::tp($tmpWorkPath);
self::tp($headerFilePath);
$params = array(
$this->formNameEmail => $this->email,
$this->formNamePassword => $this->password,
$this->formNameNextUrl => $this->watchUrl . DIRECTORY_SEPARATOR . $this->mid
);
$ch = curl_init();
//curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpCookiePath);
//curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpCookiePath);
curl_setopt($ch, CURLOPT_URL, $this->loginUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$output = curl_exec($ch);
$params = array('v' => $this->mid);
curl_setopt($ch, CURLOPT_URL, $this->apiUrl);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$output = curl_exec($ch);
// DLリンクの解析
preg_match("'url=(.*?)&link'", urldecode($output), $m);
if(empty($m)) goto exception;
curl_setopt($ch, CURLOPT_POSTFIELDS, array());
curl_setopt($ch, CURLOPT_URL, $m[1]);
// 動画DLセクション
$execResult = false;
$hdFp = fopen($headerFilePath, 'w');
if($hdFp) {
//プロセス実行中でも書き込んでくれるWRITEHEADERを利用
curl_setopt($ch, CURLOPT_WRITEHEADER, $hdFp);
$twFp = fopen($tmpWorkPath, 'wb');
if($twFp) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FILE, $twFp);
$execResult = curl_exec($ch);
curl_close($ch);
fclose($twFp);
}
fclose($hdFp);
}
if(!$execResult) goto exception;
// headerFileの解析(ファイルタイプ、拡張子を取得)
$contentType = self::parseHeaderFile($headerFilePath);
$ext = self::getFileExtension($contentType);
// 実ファイルのパスを指定(お名前指定)
$filePath = $this->saveDir . DIRECTORY_SEPARATOR . $this->mid . $ext;
// ファイル移動
rename($tmpWorkPath, $filePath);
// 作業ファイル削除
self::u($tmpCookiePath);
self::u($tmpWorkPath);
self::u($headerFilePath);
return true;
if(false) {
exception:
self::u($tmpCookiePath);
self::u($tmpWorkPath);
self::u($headerFilePath);
curl_close($ch);
return false;
}
}
/**
* ファイルの存在確認と作成、及び権限の付与
* @param unknown $path
*/
protected function tp($path) {
if(!file_exists($path)) touch($path);
chmod($path, 0777);
}
/**
* ファイルの存在確認と削除
* @param unknown $path
*/
protected function u($path) {
if(file_exists($path)) unlink($path);
}
/**
* パラメーターチェック
*/
protected function checkParams() {
if(!self::validateEmail($this->email)) array_push($this->errors, 'please check your email address.');
if(empty($this->password)) array_push($this->errors, 'password is required but not set.');
if(!is_dir($this->saveDir)) array_push($this->errors, 'saveDir must be a directory.');
if(fileperms($this->saveDir) != '16895') array_push($this->errors, 'saveDir required to be permission 777.');
if(!is_dir($this->tmpDir)) array_push($this->errors, 'tmpDir must be a directory.');
if(fileperms($this->tmpDir) != '16895') array_push($this->errors, 'tmpDir required to be permission 777.');
if(empty($this->mid)) array_push($this->errors, 'mid is required but not set.');
if(!self::validateMid($this->mid)) array_push($this->errors, 'ID starting from nm is not allowed (sm****** only).');
}
/**
* メールアドレスのバリデーション
* @param unknown $check
* @return boolean
*/
protected function validateEmail($check) {
$hostname = '(?:[_a-z0-9][-_a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,})';
$regex = '/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@' . $hostname . '$/i';
if (preg_match($regex, $check)) return true;
return false;
}
/**
* 動画IDの正規バリデーション
* @param unknown $check
* @return boolean
*/
protected function validateMid($check) {
$regex = '/^(sm)[0-9]+$/';
if (preg_match($regex, $check)) return true;
return false;
}
/**
* ヘッダーファイルの情報を解析
* @param unknown $path
* @return Ambigous <NULL, string>
*/
protected function parseHeaderFile($path) {
$hd = file_get_contents($path);
$ct = null;
$k = 'Content-Type:';
$st = stripos($hd, $k);
if($st !== false) {
$st += strlen($k);
$ed = strpos($hd, "\n", $st);
if($ed === false) $ed = strlen($hd);
$l = $ed - $st;
$ct = strtolower(trim(substr($hd, $st, $l)));
}
return $ct;
}
/**
* コンテントタイプより動画ファイルの拡張子を分析し返却
* @param unknown $ct
* @return string
*/
protected function getFileExtension($ct) {
$e = '';
switch($ct) {
case 'video/3gpp':
$e = '.3gp';
return $e;
case 'video/mp4':
$e = '.mp4';
return $e;
case 'video/x-flv':
default:
$e = '.flv';
return $e;
}
}
}
コメントアウトにもあるけど、使い方は以下の通り。
$nico = new niconicoMovieDownloader();
$nico->email = 'ログインメールアドレス';
$nico->password = 'ログインパスワード';
$nico->saveDir = '動画保存用ディレクトリ'; // 末端DS無し
$nico->tmpDir = '作業用ディレクトリ'; // 末端DS無し
$res = $nico->download('sm*******');
気が向いたら動画再生ページのパーサー実装しようかな。
調べたのでメモ。
// 存在確認 file_exists() ↓ // ファイル作成 touch() ↓ // 権限変更 chmod() ↓ // ファイルオープン fopen() ↓ // ファイルロック flock() ↓ // 書き込み fwrite() ↓ // ロック解除 flock() ↓ // ファイルクローズ fclose()
いきなりfopen(‘/path’, ‘w’)とかやってもファイルが存在しなければ新規作成してくれちゃうけど、一応上記が厳密?な流れらしい。
ロックとアンロックは仲裁更新が起こりうる場面であればしたほうがいいけど、そうでなければしなくてもいい気はする。(適当
自分用メモ。
// postしたい場合は値を設定 $params = array( 'id' => '', 'password' => '', ); // 初期化 $ch = curl_init(); // curl_exec() の返り値を 文字列で返す(通常はデータを直接出力) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Location: ヘッダの内容を再帰的に辿る curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // サーバーから受け取ったクッキーを保存するファイル名 // 既存ファイルでないとエラーになる可能性があるので注意 curl_setopt($ch, CURLOPT_COOKIEJAR, '/cookie'); // サーバーに投げるクッキーのファイル名 curl_setopt($ch, CURLOPT_COOKIEFILE, '/cookie'); // リクエストを行うURL curl_setopt($ch, CURLOPT_URL, ''); // HTTP POSTを行うか否か curl_setopt($ch, CURLOPT_POST, true); // POSTするパラメーター curl_setopt($ch, CURLOPT_POSTFIELDS, $params); // 実行 $output = curl_exec($ch); // リクエストを閉じて終了 // 返り値は無い curl_close($ch);
COOKIEJAR、COOKIEFILEのパスはどこでもいいが、名前が「cookie」でないと正常に動いてくれない模様
↑そんなこと無かった。「_cookie」で動作確認済み。
基本的なリクエストは上記のoptionで事足りるはず。
その他使用頻度が高そうなoptionは以下。
// HTTPのリクエスト形式をGETに戻す curl_setopt($ch, CURLOPT_HTTPGET, true); // リクエスト結果の詳細情報を出力するか否か curl_setopt($ch, CURLOPT_VERBOSE, true); // 上記VERBOSEの結果を書き込むファイルパス // STDERRはプロセスが完了した後でないとファイル出力しないので注意 curl_setopt($ch, CURLOPT_STDERR, '/path/to/vb'); // WRITEHEADERはプロセス実行中でもファイルに出力してくれる curl_setopt($ch, CURLOPT_WRITEHEADER, '/path/to/hd');
適宜追記予定。
この英単語はPHPの公式リファレンスでよく見かける。
以前からどういう意味なんだろうと思いつつよく分からないまま放置していたが、調べてみると凄くしっくりきたのでメモ。
関数の引数部分にneedleとかhaystackとか書いてあって、いやどっちがどっちやねん。。。とか思ってたけど、非常に納得。
今後はわざわざ別ページに調べに行くこともなくなりそうである。
まず上記二つの英単語は「干し草」と「針」を意味している。
haystack = 干し草
needle = 針
もうこの時点であーなるほどって思ったけど、この単語が慣習的に用いられている理由は上記単語を用いた英語の慣用句にあるらしい。
needle in a haystack.
見つけることが不可能なもの。
look for a needle in a haystack.
望みのない捜し物をする、むだ骨を折る。
例えば文字列を検索する関数で、引数にhaystackとあれば、そこには検索対象文字列を渡し、needleには探したい文字列を渡せばいいわけである。
うん。とても理にかなっている。公式リファレンスに抵抗が無くなった瞬間であった。笑
見易さ度外視孤高のワンライナーには必須の文法「三項目演算子」。
さらにこれを入れ子に出来ないかと調べてみたら簡単に出来たのでメモ。
まずはスタンダードな三項目演算子。
$needle = null; $res = is_null($needle) ? 'null' : 'not null'; var_dump($res); ↓ string(4) "null"
たとえば上記式の評価後に更に条件を分岐させたい場合、参考演算子全体を括弧で括ればひとつの式として正常に評価されるので、入れ子に記述しても全く問題ないのである。
下記例。
$needle = ''; $res = is_null($needle) ? 'null' : (is_string($needle) ? 'string' : 'not string'); var_dump($res); ↓ string(6) "string"
これでワンライナーが更に捗る。
前も調べた気するけど忘れないように一応メモ。
■file_get_contents()
一度変数にぶち込むのでメモリを喰う。
数万?数十万行とかあるファイルを読ませるとmemory_limitにぶち当たる。
■readfile()
読み込むと同時に標準出力するのでメモリを気にしなくていい。
「out of memory」エラーが出る場合は、ob_get_level()で出力バッファリングを無効にすればよい
※公式より