$config[$update_id] = new stdClass();
     $config[$update_id]->sitename = '';
     // 新規の場合はパスワード必須
     $config[$update_id]->password = required_param("password{$update_id}");
 } else {
     // 更新の場合はパスワードがフォームに自動入力されないので
     // 入力値が空でなければ更新
     if ($new_password = optional_param("password{$update_id}")) {
         $config[$update_id]->password = $new_password;
     }
 }
 if (!empty($url)) {
     // リポジトリサイト名をHTTP通信で取得
     require_once './lib/FileTransfer.php';
     $tmp_file = tempnam(make_upload_directory('temp/download', FALSE), 'title');
     $response = FileTransfer::downloadFile($tmp_file, "{$url}/course/format/repository/title.php", array('instance' => $instance, 'username' => $config[$update_id]->username, 'password' => $config[$update_id]->password, 'usersite' => $CFG->wwwroot, 'sitename' => $SITE->fullname));
     if (preg_match('@^HTTP/1.. (\\d+) @', $response, $m)) {
         // HTTPステータスコード
         $status_code = intval($m[1]);
         switch ($status_code) {
             case 200:
                 // OK
                 $sitename = file_get_contents($tmp_file);
                 if (strspn($sitename, "\r\n")) {
                     // 改行文字が含まれていたらRepository側で
                     // PHPエラーが発生したと判断
                 } else {
                     $config[$update_id]->sitename = $sitename;
                     SharingCart_Repository::setConfig($USER, $config);
                     $succeeded = TRUE;
                 }
 foreach ($materials as $id => $fields) {
     // フィールドを分解
     // @see /course/format/repository/RepositoryMaterial.php # download()
     list($sha1, $type, $icon, $text) = explode('|', $fields, 4);
     // ダウンロードボタンがクリックされたら、教材ファイルをHTTP通信により取得
     if (!empty($downloads[$id])) {
         $user_dir = make_user_directory($USER->id);
         $temp_dir = make_upload_directory('temp/download', false);
         $zip_name = SharingCart_Repository::getDownloadName($id);
         if (!is_dir($user_dir) || !is_dir($temp_dir)) {
             throw new SharingCart_RepositoryException('Directory creation failure');
         }
         if (is_file($user_dir . '/' . $zip_name)) {
             throw new SharingCart_RepositoryException('File already exists');
         }
         $response_header = FileTransfer::downloadFile($temp_dir . '/' . $zip_name, $repository_wwwroot . '/course/format/repository/material.php', array('mode' => 'transfer', 'material' => $id, 'sessionkey' => $sessionkey));
         // ダウンロードしたファイルのハッシュを比較してダウンロード成功かチェック
         if (sha1_file($temp_dir . '/' . $zip_name) == $sha1) {
             if (!rename($temp_dir . '/' . $zip_name, $user_dir . '/' . $zip_name)) {
                 throw new SharingCart_RepositoryException('File rename failure');
             }
             $sharing_cart = new stdClass();
             $sharing_cart->user = $USER->id;
             $sharing_cart->name = $type;
             $sharing_cart->icon = $icon;
             $sharing_cart->text = $text;
             $sharing_cart->time = time();
             $sharing_cart->file = $zip_name;
             sharing_cart_table::insert_record($sharing_cart);
             $completes[$id] = TRUE;
         }