いずみちゃんどっとこむ プログラミング掲示板

過去ログ一覧

PHP (11)
1 名前:いずみ 投稿日:2003/11/24(月) 19:25:20
最近はこれが私のメイン

2 名前:いずみ 投稿日:2003/11/24(月) 19:26:59
ということで板が死んでるので(笑)自分でスレ立ててみました。

最近、なんでもクラスで書くのがマイブームで、そのため共同開発者には迷惑かけてます(^^;;)
つーかやっぱりスクリプト言語って1人で書くものだよなぁ、と居直ってみるてすと^^;;

3 名前:いずみ 投稿日:2003/11/24(月) 19:37:49
最近はこんな感じのスケルトンで書いてます。

// Hoge1.php

class CHoge1 extends CHoge
{
}

function &Hoge1_factory()
{
  return new CHoge1();
}

// index.php

class CHoge
{
  function CHoge($mode)
  {
    $this->mode = $mode;
  }
  function initialize() {}
  function finalize() {}
  function print_page()
  {
    if (method_exists($this, $this->mode)) eval("\$this->{$this->mode}();");
    else $this->view();
  }
  function view() {}
}

$classname = $_REQUEST['name'];
if (file_exists("$classname.php"))
{
  require_once("$classname.php");
  eval("\$page = &$classname_factory();");
  $page->initialize();
  $page->print_page();
  $page->finalize();
}
else
{
  print_error();
}


4 名前:いずみ 投稿日:2003/12/09(火) 16:38:27
なんか、Apache + mod_php で動かしてる環境で、物理的に同じApacheインスタンスに飛ぶような URL でもって URL wrapper を利用するとブロックしてしまうような気がするのだが気のせいかしらん?

ex:
$lines = @file('http://www.izumichan.com/hoge.php');
こんなコードを含むPHPスクリプトを www.izumichan.com 内に置いて実行するとブロックしてしまう。

5 名前:いずみ 投稿日:2003/12/26(金) 19:40:47 [投稿者削除]

6 名前:いずみ 投稿日:2003/12/26(金) 19:42:12
class CSV
{
  function parse($line)
  {
    $tmpfields = array();
    preg_match_all(
      '/("(?:[^"]|"")*"|[^,]*),/s',
      preg_replace('/(?:\x0d\x0a|[\x0d\x0a])?$/', ',', $line, 1),
      $tmpfields);
    $fields = array();
    foreach ($tmpfields[1] as $field)
    {
      if (preg_match('/^"(.*)"$/s', $field, $body))
      {
        array_push($fields, str_replace('""', '"', $body[1]));
      }
      else
      {
        array_push($fields, $field);
      }
    }
    return $fields;
  }
  function parse_all($line_produce_func)
  {
    $records = array();
    while ($line = call_user_func(&$line_produce_func))
    {
      while (substr_count($line, '"') % 2)
      {
        $nextline = call_user_func(&$line_produce_func);
        if (!$nextline)  break;
        $line .= $nextline;
      }
      array_push($records, CSV::parse($line));
    }
    return $records;
  }
  function construct($fields)
  {
    foreach ($fields as $index => $field)
    {
      if ($changed = strstr($field, '"'))
      {
        $field = str_replace('"', '""', $field);
      }
      elseif (strspn($field, "\x0d\x0a,"))
      {
        $changed = true;
      }
      $changed and $fields[$index] = "\"$field\"";
    }
    return implode(',', $fields);
  }
}

7 名前:いずみ 投稿日:2004/03/05(金) 23:52:19

elseif (strspn($field, "\x0d\x0a,")) は大ウソだ〜(T_T)

正しくは elseif (preg_match('/[\x0d\x0a,]/', $field)) ということで。。。

8 名前:いずみ 投稿日:2004/03/05(金) 23:55:41
define('CRECEIVE_MAILADDR_PREG',
'[\x21\x23-\x27\x2a\x2b\x2d-\x3b\x3d\x3f\x41-\x5a\x5e-\x7e]+\@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)+'
);

class CReceive2
{
  // constructor
  function CReceive2($parse_body = true, $in_mail = "")
  {
    $this->Address = array();
    if ($in_mail) $this->stream = new CLineFromString($in_mail);
    else  $this->stream = new CLineFromFile();
    while ($line = $this->stream->getline())
    {
      if ($line == "\r\n" || $line == "\n") break;
      if (preg_match('/^(\S[^:]+):(.*)$/', rtrim($line), $matched))
      {
        $cur_header = strtoupper($matched[1]);
        $this->header[$cur_header][] = trim($matched[2]);
      }
      elseif ($cur_header)
      {
        $index = count($this->header[$cur_header]) - 1;
        $index >= 0 and $this->header[$cur_header][$index] .= ' ' . trim($line);
      }
    }
    if ($this->header['TO'])
    {
      preg_match('/' . CRECEIVE_MAILADDR_PREG . '/', $this->header['TO'][0], $matched);
      $this->To = $matched[0];
    }
    if ($this->header['FROM'])
    {
      preg_match('/' . CRECEIVE_MAILADDR_PREG . '/', $this->header['FROM'][0], $matched);
      $this->From = $matched[0];
    }
    $this->body = array();
    if ($parse_body)
    {
      while ($line = $this->stream->getline()) array_push($this->body, trim($line));
    }
  }
  function collect_preg_patterns($preg, $modifier = '')
  {
    preg_match_all('/(' . (is_array($preg) ? implode(')|(', $preg) : $preg) . ")/$modifier", implode(' ', $this->body), $matched);
    if (is_array($matched))
    {
      array_shift($matched);
      foreach ($matched as $index => $subjects)
      {
        $matched[$index] = array();
        foreach ($subjects as $sub) $sub and array_push($matched[$index], $sub);
      }
      return $matched;
    }
    else
    {
      return array();
    }
  }
}

9 名前:いずみ 投稿日:2004/11/29(月) 16:06:18
仕事に使うので5分で書いてみた。意味明瞭。

class CSimpleDomElement
{
  private $name;
  private $props;
  private $parent;
  private $children;
  function __construct($name)
  {
    $this->name = $name;
    $this->props = array();
    $this->parent = null;
    $this->children = array();
  }
  function AddChild($child)
  {
    $this->children[] = $child;
  }
  function AddProp($name, $value)
  {
    $this->props[$name] = $value;
  }
  function Dump($indent = 0)
  {
    print str_repeat(' ', $indent * 2);
    print "<{$this->name}";
    if ($this->props)
    {
      foreach ($this->props as $name => $value)  print " $name='$value'";
    }
    print ">\n";
    foreach ($this->children as $child)
    {
      if (is_a($child, 'CSimpleDomTree'))
      {
        $child->Dump($indent + 1);
      }
      else
      {
        print str_repeat(' ', ($indent + 1) * 2);
        print $child;
        print "\n";
      }
    }
    print str_repeat(' ', $indent * 2);
    print "</{$this->name}>\n";
  }
}

10 名前:いずみ 投稿日:2004/12/27(月) 12:06:21
特殊な用途で必要になったので書きました。

class CFileDivider
{
  private $filename;
  private $outfilebase;
  private $maxsize;
  const ERR_SRC_OPEN = -1;
  const ERR_DST_OPEN = -2;
  const ERR_DST_WRITE = -3;
  function __construct($filename, $outfilebase = '')
  {
    $this->filename = $filename;
    $this->outfilebase = $outfilebase ? $outfilebase : $filename;
  }
  protected function create_filename($index)
  {
    return $this->outfilebase . '.' . $index;
  }
  function divide($maxsize, $initial_index = 1)
  {
    $fp = @fopen($this->filename, 'rb');
    if (!$fp)  return array(CFileDivider::ERR_SRC_OPEN);
    $index = 0;
    $lastlen = 0;
    for (;;)
    {
      $buf = @fread($fp, $maxsize);
      if (!strlen($buf))  break;

      $fname = $this->create_filename($index + $initial_index);
      $fwp = @fopen($fname, 'wb');
      if (!$fwp)  return array(CFileDivider::ERR_DST_OPEN);
      if (!@fwrite($fwp, $buf))  return array(CFileDivider::ERR_DST_WRITE);
      @fclose($fwp);
      $index++;
      $lastlen = strlen($buf);
    }
    @fclose($fp);
    return array($index, $lastlen);
  }
  function concat($num_files = 0)
  {
    exit("NOT IMPREMENTED.\n");
  }
}

11 名前:いずみ@職場 投稿日:2005/03/17(木) 17:42:42
↓こんなファイルを置いて $menu = include_once('hogehoge.php'); 。笑

<?php
// 関数名の重複を避けるコード-:p
return call_user_func(create_function('', <<<CONTENTSFUNC
  global \$DB;
  \$m = array(
    '_label' => 'コンテンツ管理',
    '_class' => 'Contents'
  );
  foreach (\$DB->GetList('domains', 'domain', 'order by domain') as \$domain => \$domainprops)
  {
    \$m[\$domain] = \$domainprops['domainname'];
  }
  return \$m;
CONTENTSFUNC
));
?>

Script: mjuz float bbs ver.1.32