技術者派遣の技術日誌ブログ

September 29, 2009

【How To】 C#でRFC3339の日付フォーマットを出力するには

Filed under: .NET Framework, ALL, C# — Tags: , , — citiszo @ 12:21 am

Atomなどで使われる日付形式RFC3339 を.Net Framework(C#)で出力するには下記のように行う。

string rfc3339 = System.Xml.XmlConvert.ToString(new DateTime(2009, 1, 1, 12, 3, 4, DateTimeKind.Local), System.Xml.XmlDateTimeSerializationMode.Utc);

 変換結果は下記。

2009-01-01T03:03:04Z

September 28, 2009

【Tips】LINQ to SQLでIDENTITY列のある表にInsertした場合

Filed under: .NET Framework, ADO.NET, LINQ — Tags: , , — Sayuri @ 9:24 pm

LINQ to SQLでIDENTITY列のある表にInsertした場合、SubmitChanges()後に挿入したオブジェクトに自動的に発番したIDを入れてくれるようである。

NorthwindデータベースでCategoryIDがIdentity列であるCategoryテーブルに対して、下記LINQのコードを実行したところ

using (DataClasses1DataContext db = new DataClasses1DataContext())
{
    Categories cat = new Categories(){CategoryName = “test”};
    db.Categories.InsertOnSubmit(cat);
    db.SubmitChanges();
    var id = cat.CategoryID;
}

最後のcat.CategoryIDには自動発番されたIDが取得できていた。

[Tips]ASP.Net MVC Web Applicationテンプレートの使い方

Filed under: .NET Framework, Asp.Net — Tags: , — strmozo @ 6:22 pm

Visual StudioでASP.Net MVC Web Applicationプロジェクトを作成する。

Modelクラス、Repositoryクラスを作成する。RepositoryクラスはModelクラスの取得、保存の操作を実装するクラス。Modelクラスにはビジネスロジックだけを実装する。

Controlクラスを作成するにはControllerフォルダで右クリックし、追加->Controllerをクリックする。
Controllerクラスが作成されるので、例えば下記のようなリストデータ出力用メソッドを作成する。 (more…)

September 26, 2009

【How To 】.Net Frameworkでbase64形式ASCIIテキストのエンコード、デコードを行うには

Filed under: .NET Framework, C# — Tags: , , — doku @ 3:26 pm

エンコーディングは一旦ASCIIのバイト列に変換し、System.ConvertクラスのToBase64Stringメソッドを使う。

string base64Str = Convert.ToBase64String(Encoding.ASCII.GetBytes(textData)));

デコーディングはSystem.ConvertクラスのFromBase64Stringメソッドを使ってASCIIテキストのバイト列を取り出し、stringに変換する。

string textData = Encoding.ASCII.GetString(Convert.FromBase64String(”dGVzdA==”));

September 25, 2009

【How To】CakePHP(+PHPMailer) から Gmail (または Google Apps)経由でメールを送る

Filed under: LAMP, MYSQL, PHP — Tags: , , , — citiszo @ 12:55 pm

使用手順

phpmailer をダウンロードして、phpMailer という名前で vendors 以下にコピーします。

Codeworx Technologies

http://phpmailer.codeworxtech.com/

 

僕が PHP をあまり理解していないからかもしれませんが、class.smtp.php 内で未定義の変数を使っているためエラーとなるので修正します。

class.smtp.php 165行目から

(省略)
public function StartTLS() {
  $this->error = null; # to avoid confusion

  if(!$this->connected()) {
    $this->error = array(“error” => “Called StartTLS() without being connected”);
    return false;
  }
+  $extra = “”;
  fputs($this->smtp_conn,”STARTTLS” . $extra . $this->CRLF);
(省略)

 

次に、app/controllers/components/email.php を作ります。*1

<?php
class EmailComponent extends Object {
  var $from = FROM_EMAIL;
  var $fromName = FROM_NAME;
  var $smtpAuth = true;
  var $smtpUserName = SMTP_USERNAME// Gmail mail address
  var $smtpPassword = SMTP_PASSWORD;// Gmail password
  var $smtpHostNames= SMTP_HOST// smtp.gmail.com
  var $smtpPort = SMTP_PORT; // 587
  var $smtpSecureType = SMTP_SECURE_TYPE; // tls
  var $smtpError = null;
  var $to = null;
  var $toName = null;
  var $subject = null;
  var $cc = null;
  var $bcc = null;
  var $layout = null;
  var $elementDir = ‘email’;
  var $template = ‘default’;
  var $attachments = null;
  var $controller = null;

  function startup(&$controller) {
    $this->controller = &$controller;
  }

  function _element($template) {
    $temp_layout = $this->controller->layout;
    $this->controller->layout = ;

    ob_start();
    $this->controller->autoRender = false;

    App::import(‘Core’, array(‘View’));
    $viewClass = new View($this->controller);

    $content = $viewClass->element($template , $this->controller->viewVars, true);
    echo $viewClass->renderLayout($content, $this->layout) ;

    $this->controller->autoRender = ‘auto’;
    $body = ob_get_clean();

    $this->controller->layout = $temp_layout;

    return $body;
  }

  function bodyText() {
    if ($this->layout == null) {
      $this->layout = ‘email/text/default’;
    }
    return $this->_element($this->elementDir . ‘/text/’ . $this->template);
  }

  function bodyHtml() {
    if ($this->layout == null) {
      $this->layout = ‘email/html/default’;
    }
    return $this->_element($this->elementDir . ‘/html/’ . $this->template);
  }

  function attach($filename, $asfile = ) {
    if (empty($this->attachments)) {
      $this->attachments = array();
      $this->attachments[0]['filename'] = $filename;
      $this->attachments[0]['asfile'] = $asfile;
    } else {
      $count = count($this->attachments);
      $this->attachments[$count+1]['filename'] = $filename;
      $this->attachments[$count+1]['asfile'] = $asfile;
    }
  }

  function base64($str, $charset=“JIS”, $transfer_encoding=“B”, $linefeed=\n) {
    return mb_encode_mimeheader($str, $charset, $transfer_encoding, $linefeed);
  }

  function send($html = false) {
    App::import(‘Vendor’, ‘PHPMailer’, array(‘file’ => ‘phpMailer’ . DS . ‘class.phpmailer.php’));
    $mail = new PHPMailer();

    $mail->IsSMTP(); // set mailer to use SMTP
    if ($this->smtpAuth) {
      $mail->SMTPAuth = true;
      $mail->Host = $this->smtpHostNames;
      $mail->Username = $this->smtpUserName;
      $mail->Password = $this->smtpPassword;
    }
    $mail->From = $this->from;
    $mail->FromName = $this->base64(mb_convert_encoding($this->fromName, “JIS”, “UTF-8″));
    $mail->AddReplyTo($this->from);

    // to
    if (is_array($this->to)) {
      foreach ($this->to as $to) {
        $mail->AddAddress($to);
      }
    } else {
      $mail->AddAddress($this->to, $this->base64(mb_convert_encoding($this->toName, “JIS”, “UTF-8″)));
    }
    // cc
    if(!empty($this->cc)) {
      if (is_array($this->cc)) {
        foreach ($this->cc as $cc) {
          $mail->AddCC($cc);
        }
      } else {
        $mail->AddCC($this->cc);
      }
    }
    // bcc
    if(!empty($this->bcc)) {
      if (is_array($this->bcc)) {
        foreach ($this->bcc as $bcc) {
          $mail->AddBcc($bcc);
        }
      } else {
        $mail->AddBcc($this->bcc);
      }
    }

    $mail->Port = $this->smtpPort;
    $mail->SMTPSecure = $this->smtpSecureType;
    $mail->CharSet  = “iso-2022-jp”;
    $mail->Encoding  = “7bit”;
    $mail->WordWrap = 70// set word wrap to 70 characters

    if (!empty($this->attachments)) {
      foreach ($this->attachments as $attachment) {
        if (empty($attachment['asfile'])) {
          $mail->AddAttachment($attachment['filename']);
        } else {
          $mail->AddAttachment($attachment['filename'], $attachment['asfile']);
        }
      }
    }

    $mail->IsHTML($html);  // set email format to HTML
    $mail->Subject = mb_convert_encoding($this->subject, “JIS”, “UTF-8″);
    if ($html) {
      $mail->Body = mb_convert_encoding($this->bodyHtml(), “JIS”, “UTF-8″);
      $mail->AltBody = mb_convert_encoding($this->bodyText(), “JIS”, “UTF-8″);
    } else {
      $mail->Body = mb_convert_encoding($this->bodyText(), “JIS”, “UTF-8″);
    }
    $result = $mail->Send();
    if($result == false) {
      $this->smtpError = $mail->ErrorInfo;
    }
    return $result;
  }
}
?>

 
コントローラはまず、EmailComponent を読み込みます。

class UsersController extends AppController {
(省略)
+  var $components = array(‘Email’);

 

使い方
CakePHP の EmailComponent に似せています。

メールを送るには以下のようにします。

$this->set(‘user’, $user);
$this->Email->template = ’signup’; // views/elements/email/text/signup
$this->Email->to = $user['User']['pc_email'];
$this->Email->subject = ‘登録が完了しました’;
$this->Email->send();
if (!empty($this->Email->smtpError)) {
  $this->log($this->Email->smtpError, LOG_DEBUG);
}

このとき、実際に使われるテンプレートは以下のようになります。

layout views/layout/email/text/default
view views/elements/email/text/signup

 

ビューは例えば以下のようにします。

<?php echo $user['User']['name'] ?>さん ユーザー登録が完了しました。

複数アドレスに送信

複数アドレスに送信したい場合、$this->Email->to, $this->Email->cc, $this->Email->bcc配列を指定します。

$this->Email->to = array($user['User']['pc_email'], $user['User']['mobile_email']);

layout 変更

// layouts/email $this->Email->layout = ‘email’;

html メール送信

$result = $this->Email->send(true);

html メールの場合、実際に使われるテンプレートは以下のようになります。

layout views/layout/email/html/default
element views/elements/email/html/signup

John Graham-Cumming: JavaScript must die

Filed under: JAVA, Javascript/Xhtml/Css — Tags: , , , , , , , — AspAnn @ 10:10 am

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript, major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

Read more here:
John Graham-Cumming: JavaScript must die

John Graham-Cumming: JavaScript must die

Filed under: Ajax, JAVA, Javascript/Xhtml/Css — Tags: , , , , , , , — AspAnn @ 10:10 am

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript, major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

Read more:
John Graham-Cumming: JavaScript must die

John Graham-Cumming: JavaScript must die

Filed under: JAVA, Javascript/Xhtml/Css — Tags: , , , , , , — citiszo @ 10:10 am

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript, major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

View original post here:
John Graham-Cumming: JavaScript must die

John Graham-Cumming: JavaScript must die

Filed under: Ajax, JAVA, Javascript/Xhtml/Css — Tags: , , , , , , — citiszo @ 10:10 am

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript, major web sites suffer from XSS and CSRF flaws, …

My thesis is that the security situation with JavaScript is so poor that the only solution is to kill it. End users have very little in the way of protection against malicious JavaScript , major web sites suffer from XSS and CSRF flaws, …

Read this article:
John Graham-Cumming: JavaScript must die

【How to】C#でオブジェクト<->JSONの変換を行うには

Filed under: Ajax, C# — Tags: , — Sayuri @ 3:01 am

#でオブジェクトをJSON形式でシリアライズするにはまずシリアライズするクラスにDataContract属性とそのプロパティにDataMember属性を設定しておく。 (more…)

Older Posts »

このページの上部に戻る

Powered by WordPress