2019年7月23日火曜日

HTML imgタグに指定したスクリプトへの値の受け渡し

2019 Jul. 23.

HTMLのimgタグに指定したスクリプトへの値の受け渡しはGETリクエスト。
書式: <img src='SCRIPT.php?var1=VALUE1&var2=VALUE2'>
 値は文字列でも引用符で囲まない。
PHPでの受け取りは $_GET['var1'] に VALUE1 が入る。

[呼び出し元]
<?php
$phpFilePath = "/myProject/my_php/display_jpg.php?imgFile=myImage.jpg";
echo "<img src='{$phpFilePath}'>";
?>

[呼ばれる側](/myProject/my_php/display_jpg.php)
<?php
$jpgpath = "/virtual/rokkonet/public_html/www.rokkonet.ga/dispose_list/data/{$_GET['imgFile']}";
?>

XREA WordPress imgタグからphpスクリプトを読み込んで画像を表示する img src="SCRIPT.php"

2019 Jul. 23.

テキストとして読み込まれたHTMLファイルの途中で header("Content-Type: image/jpeg") を発行しても有効にならない。
"Content-Type: image/jpeg" を指定してimgタグに直接画像を指定しても、画像はテキスト扱いされ文字化け表示される。
 「fopen(), fread(), fclose()」「file_get_contents()」「readfile()」「imagejpeg()」はいずれも画像表示に失敗する。
imgタグに画像を表示するphpスクリプトを指定する。

[画像を表示するページとなるfoo.phpの保存場所]

/public_html/www.MyXrea.Domain/myProject/my_php/foo.php



[foo.php内の画像表示部]

// imgタグに指定するphpファイルのパス指定は /myProject から始まる
$phpFilePath = "/myProject/my_php/display_jpg.php";
echo "<img src='{$phpFilePath}'>";


[foo.phpから呼び出されるdisplay_jpg.phpの保存場所] 

/public_html/www.MyXrea.Domain/myProject/my_php/display_jpg.php

[画像ファイルの保存場所]

 /virtual/XREA-NAME/public_html/www.MyXrea.Domain/myProject/data/myImage.jpg

[display_jpg.phpの画像表示部]

<?php
header("Content-Type: image/jpeg");
$jpgpath = "/virtual/XREA-NAME/public_html/www.MyXrea.Domain/myProject/data/myImage.jpg";
  // 画像ファイルパスは /virtual/XREA-NAME/public_html/www.MyXrea.Domain から始める
$img = imagecreatefromjpeg( $jpgpath );
imagejpeg( $img );
imagedestroy( $img );
?>

2019年7月15日月曜日

android AsyncTaskによるJSchを用いたSSH通信とAsyncTask内イベントの呼び出し元クラスでのキャッチ

2019 Jul. 15.

MyJsch.java
  1. package your.package;
  2.  
  3. import android.content.Context;
  4.  
  5. import com.jcraft.jsch.Channel;
  6. import com.jcraft.jsch.ChannelSftp;
  7. import com.jcraft.jsch.JSch;
  8. import com.jcraft.jsch.JSchException;
  9. import com.jcraft.jsch.Session;
  10. import com.jcraft.jsch.SftpException;
  11. import com.jcraft.jsch.UserInfo;
  12.  
  13. import java.io.File;
  14. import java.io.InputStream;
  15. import java.util.Collections;
  16. import java.util.Hashtable;
  17. import java.util.List;
  18.  
  19. class MyJsch {
  20. private final String ServerIp;
  21. private final int Port;
  22. private final String User;
  23. private final String PassPhraseWord;
  24. private final String IdentityKeyPath;
  25. private final Context AppContext;
  26.  
  27. /** Channel接続タイプ */
  28. private static final String CHANNEL_TYPE = "sftp";
  29.  
  30. /*
  31. * コンストラクタ
  32. */
  33. MyJsch( final String ServerIp, final int Port, final String User, final String PassPhraseWord,
  34. final String IdentityKeyPath, final Context AppContext) {
  35. this.ServerIp = ServerIp;
  36. this.Port = Port;
  37. this.User = User;
  38. this.PassPhraseWord = PassPhraseWord;
  39. this.IdentityKeyPath = IdentityKeyPath;
  40. this.AppContext = AppContext;
  41. }
  42.  
  43.  
  44. /*
  45. * Sessionを開始
  46. */
  47. protected Session connectSession() throws JSchException {
  48. Session session = null;
  49.  
  50. // android端末内の外部ストーリッジ確認
  51. List sdCardFilesDirPaths =
  52. SdCardDirPaths.getSdCardFilesDirPathListForLollipop( AppContext );
  53. Collections.sort(sdCardFilesDirPaths, new CompStringLength());
  54. String externalPath = sdCardFilesDirPaths.get(0);
  55. externalPath = externalPath.replaceAll("/Android.*$", "");
  56. final JSch jsch = new JSch();
  57. // クライアント側のknownHostsチェックを行わない
  58. Hashtable config = new Hashtable();
  59. config.put("StrictHostKeyChecking", "no");
  60. jsch.setConfig(config);
  61. // パスフレーズ・秘密鍵方式
  62. String privKeyFilePath = externalPath + "/" + IdentityKeyPath;
  63. File privKeyFile = new File(privKeyFilePath);
  64. if (privKeyFile.exists() ) {
  65. jsch.addIdentity(privKeyFilePath, PassPhraseWord);
  66. }
  67. // Session取得
  68. session = jsch.getSession(User, ServerIp, Port);
  69. // パスワード方式でも可とする
  70. session.setPassword(PassPhraseWord);
  71. final UserInfo userInfo = new SftpUserInfo();
  72. session.setUserInfo(userInfo);
  73. session.connect();
  74. return session;
  75. }
  76. /**
  77. * SFTPのChannelを開始
  78. *
  79. * @param session
  80. * 開始されたSession情報
  81. */
  82. protected ChannelSftp connectChannelSftp(final Session session)
  83. throws JSchException {
  84. final ChannelSftp channel = (ChannelSftp) session.openChannel(CHANNEL_TYPE);
  85. try {
  86. channel.connect();
  87. } catch (JSchException e) {
  88. return null;
  89. }
  90. return channel;
  91. }
  92. /**
  93. * Session・Channelの終了
  94. *
  95. * @param session
  96. * 開始されたSession情報
  97. * @param channels
  98. * 開始されたChannel情報.複数指定可能
  99. */
  100. protected void disconnect(final Session session, final Channel... channels) {
  101. if (channels != null) {
  102. for (Channel c: channels ) {
  103. if (c != null) {
  104. c.disconnect();
  105. }
  106. }
  107. }
  108. if (session != null) {
  109. session.disconnect();
  110. }
  111. }
  112. /**
  113. * SFTPに接続するユーザ情報を保持するクラス
  114. */
  115. private static class SftpUserInfo implements UserInfo {
  116. @Override
  117. public String getPassword() {
  118. return null;
  119. }
  120. @Override
  121. public boolean promptPassword(String arg0) {
  122. return true;
  123. }
  124. @Override
  125. public boolean promptPassphrase(String arg0) {
  126. return true;
  127. }
  128. @Override
  129. public boolean promptYesNo(String arg0) {
  130. return true;
  131. }
  132. @Override
  133. public void showMessage(String arg0) {
  134. }
  135. @Override
  136. public String getPassphrase() {
  137. return null;
  138. }
  139. }
  140. /**
  141. * ファイルアップロード
  142. *
  143. * @throws JSchException
  144. * Session・Channelの設定/接続エラー時に発生
  145. */
  146. public void putFile(ChannelSftp channel, InputStream inStream, String destPath)
  147. throws SftpException {
  148. /*
  149. * sftp通信を実行
  150. */
  151. String absoluteDestPath = channel.getHome() + "/" + destPath;
  152. String destFile = new File(absoluteDestPath).getName();
  153. int numDestPath = absoluteDestPath.length();
  154. int numDestFile = destFile.length();
  155. String destParentPath = absoluteDestPath.substring( 0, numDestPath - numDestFile );
  156. channel.cd(destParentPath);
  157. channel.put( inStream, destFile);
  158. // confirm existance of destFile
  159. channel.lstat(destFile);
  160. }
  161. /*
  162. * サーバー上のファイルの削除
  163. * destFileNameFront文字列から始まるファイルを全て削除する
  164. */
  165. public void deleteFiles(ChannelSftp channel, String destFileNameFront) throws SftpException {
  166. channel.rm(destFileNameFront + "*");
  167. }
  168. }

SdCardDirPaths.java
  1. package your.package;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.content.Context;
  5. import android.os.Build;
  6. import android.os.Environment;
  7.  
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class SdCardDirPaths {
  13.  
  14. /**
  15. * SDカードのfilesディレクトリパスのリストを取得する。
  16. * Android5.0以上対応。
  17. *
  18. * @param context
  19. * @return SDカードのfilesディレクトリパスのリスト
  20. */
  21. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  22. public static List getSdCardFilesDirPathListForLollipop(Context context) {
  23. List sdCardFilesDirPathList = new ArrayList<>();
  24. // getExternalFilesDirsはAndroid4.4から利用できるAPI。
  25. // filesディレクトリのリストを取得できる。
  26. File[] dirArr = context.getExternalFilesDirs(null);
  27. for (File dir : dirArr) {
  28. if (dir != null) {
  29. String path = dir.getAbsolutePath();
  30. // isExternalStorageRemovableはAndroid5.0から利用できるAPI。
  31. // 取り外し可能かどうか(SDカードかどうか)を判定している。
  32. if (Environment.isExternalStorageRemovable(dir)) {
  33. // 取り外し可能であればSDカード。
  34. // このパスをパスリストに加える
  35. if (!sdCardFilesDirPathList.contains(path)) {
  36. sdCardFilesDirPathList.add(path);
  37. }
  38. }
  39. }
  40. }
  41. return sdCardFilesDirPathList;
  42. }
  43. }

(利用例)
Send2ServerTask.java
  1. package your.package;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.os.AsyncTask;
  6. import android.widget.Toast;
  7.  
  8. import com.jcraft.jsch.ChannelSftp;
  9. import com.jcraft.jsch.JSchException;
  10. import com.jcraft.jsch.Session;
  11. import com.jcraft.jsch.SftpException;
  12.  
  13. import java.io.InputStream;
  14. import java.lang.ref.WeakReference;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17.  
  18. // 複数のファイルをSSH送信する
  19. public class Send2ServerTask extends AsyncTask {
  20. final String ServerIp = "SSH.SERVER.IP.ADDRESS";
  21. final int Port = 22;
  22. final String User = "SshLogInUser";
  23. final String IdentityKeyPath = "PRIVATE/KEY/FILE/PATH/IN/CLIENT/DEVICE";
  24. // if IdentityKeyPath is illegal, password authentication is used.
  25. final String DestParentPath = "SERVER/DIR/TO/BE/STORED";
  26. private WeakReference weakRef;
  27. private HashMap inStreamMap;
  28. final private String passPhrase;
  29. final private String fileNameFront;
  30. // SSH失敗時のファイル削除のためにファイル名共通文字列を保持
  31. // 複数の送信ファイルはいずれもfileNameFront文字列で始まるファイル名とすること
  32. private Listener listener;
  33. /*
  34. * constructor
  35. * inStreamMap 送信する複数ファイルのファイル名とインプットストリームをHashMapで渡す
  36. */
  37. Send2ServerTask(Activity parentActivity, HashMap inStreamMap,
  38. String passPhrase, String fileNameFront) {
  39. weakRef = new WeakReference<>(parentActivity);
  40. this.inStreamMap = new HashMap<>(inStreamMap);
  41. this.passPhrase = passPhrase;
  42. this.fileNameFront = fileNameFront;
  43. }
  44. @Override
  45. protected String doInBackground(Void... params) {
  46. MyJsch mJsch;
  47. Session sftpSession = null;
  48. ChannelSftp sftpChannel = null;
  49. Activity refActivity = weakRef.get();
  50. if (refActivity == null || refActivity.isFinishing()) {
  51. return null;
  52. }
  53. Context appContext = weakRef.get().getApplicationContext();
  54. // initialise Jsch
  55. mJsch = new MyJsch(ServerIp, Port, User, passPhrase, IdentityKeyPath, appContext);
  56. // connect session, channel
  57. try {
  58. sftpSession = mJsch.connectSession();
  59. sftpChannel = mJsch.connectChannelSftp( sftpSession );
  60. // upload
  61. int count = 0;
  62. for ( Map.Entry entry : inStreamMap.entrySet()) {
  63. // entry.getKey() entry.getValue()
  64. String destPath = DestParentPath + "/" + entry.getKey();
  65. try {
  66. mJsch.putFile(sftpChannel, entry.getValue(), destPath);
  67. } catch (SftpException e) {
  68. try {
  69. // このセッションでアップロードされたファイルをすべて削除する
  70. mJsch.deleteFiles(sftpChannel, fileNameFront);
  71. mJsch.disconnect( sftpSession, sftpChannel);
  72. return "Failed to upload files. count: " + count + ". " + destPath + " " + e.toString();
  73. } catch (SftpException e1) {
  74. mJsch.disconnect( sftpSession, sftpChannel);
  75. return "Failed to delete remote files. " + e1.toString();
  76. }
  77. }
  78. try {
  79. // take sleep between SFTP#puts
  80. Thread.sleep(500);
  81. } catch (InterruptedException e) {
  82. // このセッションでアップロードされたファイルをすべて削除する
  83. mJsch.deleteFiles(sftpChannel, fileNameFront);
  84. mJsch.disconnect( sftpSession, sftpChannel);
  85. return "Failed! sleep" + e.toString();
  86. }
  87. count++;
  88. }
  89. if ( count != inStreamMap.size()) {
  90. // このセッションでアップロードされたファイルをすべて削除する
  91. mJsch.deleteFiles(sftpChannel, fileNameFront);
  92. mJsch.disconnect( sftpSession, sftpChannel);
  93. return "Could not upload " + inStreamMap.size() + " files";
  94. }
  95. } catch (JSchException | SftpException e) {
  96. mJsch.disconnect( sftpSession, sftpChannel);
  97. return "connection failed. " + e.toString();
  98. } finally {
  99. // disconnect channel, session
  100. mJsch.disconnect( sftpSession, sftpChannel);
  101. }
  102. return "Succeeded. " + inStreamMap.size() + " files upload";
  103. }
  104. @Override
  105. protected void onPostExecute(String result) {
  106. super.onPostExecute(result);
  107. Activity refActivity = weakRef.get();
  108. if (refActivity == null || listener == null) {
  109. Toast.makeText(refActivity, "failed sending to server.", Toast.LENGTH_LONG).show();
  110. } else {
  111. if ( result.contains("Succeeded") ) {
  112. Toast.makeText(refActivity, result, Toast.LENGTH_SHORT).show();
  113. } else {
  114. Toast.makeText(refActivity, result, Toast.LENGTH_LONG).show();
  115. }
  116. listener.onSuccess(result);
  117. }
  118. return;
  119. }
  120. void setListener(Listener listener) {
  121. this.listener = listener;
  122. }
  123. interface Listener {
  124. void onSuccess(String str);
  125. }
  126. }

MyActivity.java
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_confirm_before_send);
  5. buttonOK = findViewById(R.id.buttonOK);
  6. setListeners();
  7. }
  8.  
  9. /*
  10. * Send2ServerTaskへのリスナ
  11. * サーバーへのアップロード終了後に行う処理を記述
  12. */
  13. private Send2ServerTask.Listener createListener() {
  14. return new Send2ServerTask.Listener() {
  15. @Override
  16. public void onSuccess(String result) {
  17. deleteFiles();
  18. }
  19. };
  20. }
  21.  
  22. protected void setListeners(){
  23. buttonOK.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. /*
  27. * HashMap SSHサーバーに送るデータ
  28. * String 送信先ファイルパス
  29. * InputStream 送信ファイルのインプットストリーム
  30. */
  31. HashMap dataMap = new HashMap<>();
  32. setData2dataMap(); // dataMapにSSH通信データをセットする
  33. setPassPhraseWord();
  34. setFileNameFront();
  35. /*
  36. * dataMapを保管サーバーに送る
  37. */
  38. send2serverTask = new Send2ServerTask(thisActivity, dataMap, passPhraseWord, fileNameFront);
  39. // Listenerを設定し、send2serverTaskを実行してdataMapを保管サーバーに送る
  40. send2serverTask.setListener(createListener());
  41. send2serverTask.execute();
  42. }
  43. });
  44. }

httpサーバー リダイレクト設定

2019 Jul. 15.

https://www.jaskun.com/server/xrea-server/xrea301/ より

aaa.com を www.aaa.com にリダイレクトする例


.htaccess に次の3行を記述する
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^aaa\.com
  RewriteRule ^(.*)$ http://www.aaa.com/$1 [R=301,L]

xrea SSH通信許可設定

2019 Jul. 15.

  1. xreaにログイン
  2. 旧コントロールパネルを表示
  3. 管理メニュー -> ホスト情報登録
  4. 「SSH登録」をクリック

2019年7月13日土曜日

xreaサイトのSSL化

2019 Jul. 13.

参考元

https://damema.net/article/17/
https://www.hobofoto.net/xrea-ssl/

  1. xreaのサイト設定変更で「無料SSL」設定する
  2. xrea画面右上のプルダウンメニューから「旧コンパネ切替」する
  3. マイドメイン利用->ドメインウェブ->SSL設定 で「SSLを有効にするドメイン名」にSSL設定するドメインを選択し、「証明書設定」をクリックする
  4. 4つの大きな窓が内、下部の3つが埋まるまで、キャンセルと前記「証明書設定」クリックを繰り返す。あるいは、参考元サイトに書かれているように、コピー&ペーストする。
    3つの窓が埋まると、下部の「インストール」をクリックする。
    (必要3データ )
     プライベートキー[パスフレーズなし]SSLCertificateKeyFile
     発行された証明書(必須) SSLCertificateFile
     発行された中間証明書(必須) SSLCACertificateFile
  5.  マイドメイン利用->ドメインウェブ画面の下部の「ドメイン設定」をクリックする

xreaに独自ドメイン設定

2019 Jul. 13.


参考元 https://www.homepage-tukurikata.com/domain/coreserver-dns.html
  1. freenomで独自ドメイン取得
  2. freenomで"Manage Domain" ->  "Manage Freenom DNS" -> "Add Records"に進みNameにホスト名(wwwにした)、TTLに3600、TargetにxreaのサーバーIPアドレスを指定
  3. バリュードメインサイトの「無料ネームサーバー」->「他社登録しているドメインを登録」でfreenomで取得したドメインを登録
  4. バリュードメインの「無料ネームサーバー」->「ドメインのDNS設定」で「当サービス内サーバーの自動DNS設定」からxreaで取得したサーバーのドメインを選択
  5. xreaの「ドメイン設定」->「ドメイン設定の新規作成」で "ホスト名.freecomで取得のドメイン" を設定


2019年7月8日月曜日

NHKらじるらじるの録音

2019 Jul. 08.

らじるらじるの配信形式


  • HLS(HTTP Live Streaming) HE-AAC形式
  • M3Uプレイリストフォーマット
  • 各チャンネルのURLはconfig_web.xmlファイルに記載




 録画コマンド

$ ffmpeg -i ChannelAspxUrl -t SECONDS -codec copy OUTFILE.m4a

2019年7月6日土曜日

radikoの音声コーデックは48kbps HE-AAC v2 ステレオ

2019 Jul. 06.

radikoの音声コーデックは「48kbps HE-AAC v2 ステレオ」。
「MPEG-4 AAC Plus SBR」「aacPlus v2」「eAAC+」とも言われる。