Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
/**
4
 * Cocoon Form Builder integration for Moodle
5
 *
6
 * @package    cocoon_form_builder
7
 * @copyright  ©2021 Cocoon, XTRA Enterprises Ltd. createdbycocoon.com
8
 * @author     Cocoon
9
 */
10
 
11
  require(__DIR__.'/../../config.php');
12
  require(__DIR__.'/../../blocks/cocoon_form/phpmailer/class.custommailer.php');
13
 
14
  defined('MOODLE_INTERNAL') || die();
15
  global $DB;
16
 
17
  $_RESTREQUEST = file_get_contents("php://input");
18
  $_POST = json_decode($_RESTREQUEST, true);
19
 
20
  if (isset($_POST["id"])) {
21
    $sql = "SELECT * FROM mdl_cocoon_form_builder_forms WHERE id = " . intval($_POST["id"]) ;
22
    $form = $DB->get_records_sql($sql);
23
 
24
    $json = json_decode($form[$_POST["id"]]->json);
25
    $emails = explode(";",$form[$_POST["id"]]->recipients);
26
    $confirmMessage = ($form[$_POST["id"]]->confirm_message) ? $form[$_POST["id"]]->confirm_message : "Thank you! Your message has been sent!";
27
    $autoRepEmails = [];
28
    $autoRepData = json_decode($form[$_POST["id"]]->data);
29
 
30
    $data = explode('&', $_POST["data"]);
31
    $obj = new stdClass();
32
    foreach ($data as $key => $value) {
33
      $item = explode('=', $value);
34
      if(property_exists($obj, $item[0])) {
35
        $obj->{$item[0]} = $obj->{$item[0]} . ", " . $item[1];
36
      } else {
37
        $obj->{$item[0]} = $item[1];
38
      }
39
    }
40
 
41
    // echo '<pre>';
42
    // var_dump($obj);
43
    // echo '</pre>';
44
 
45
    $htmlmessage = "Content: " . "<br />";
46
    $fileUpload = $fileExtension = [];
47
    foreach ($json as $key => $value) {
48
      if($value->type == "text" ||
49
         $value->type == "textarea" ||
50
         $value->type == "autocomplete" ||
51
         $value->type == "date" ||
52
         $value->type == "number" ||
53
         $value->type == "radio-group" ||
54
         $value->type == "select") {
55
        if (isset($obj->{$value->name})) {
56
          $htmlmessage .= $value->label . ": " . processData($obj->{$value->name}) . "<br />";
57
 
58
          if(checkemail(urldecode($obj->{$value->name}))) {
59
            array_push($autoRepEmails, urldecode($obj->{$value->name}));
60
          }
61
        }
62
      }
63
      if($value->type == "checkbox-group" || $value->type == "checkbox") {
64
        $machine_name = $value->name . '%5B%5D';
65
        if (isset($obj->{$machine_name})) {
66
          $htmlmessage .= $value->label . ": ";
67
          $htmlmessage .= $obj->{$machine_name};
68
          $htmlmessage .= "<br />";
69
        }
70
      }
71
 
72
      $allowedfileExtensions = array("image/png", "image/jpeg", "image/bmp", "image/vnd.microsoft.icon", "application/pdf", "application/msword",
73
                                     "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.ms-excel",
74
                                     "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
75
                                     "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation",
76
                                     "text/csv", "application/zip");
77
 
78
      if($value->type == "file") {
79
        if (isset($_POST["file"]) && count($_POST["file"]) > 0) {
80
 
81
          foreach ($_POST["file"] as $key => $item) {
82
            $image_parts = explode(";base64,", $item[$value->name]);
83
        		if(count($image_parts) > 1) {
84
        			$file_type = trim(explode(":", $image_parts[0])[1]);
85
 
86
              if (in_array($file_type, $allowedfileExtensions)) {
87
                $base = $item[$value->name];
88
                array_push($fileUpload, base64_decode(str_replace(" ", "+", substr($base, strpos($base, ",")))));
89
 
90
                if($file_type == "image/png") {
91
                  array_push($fileExtension, "png");
92
                } elseif ($file_type == "image/jpeg") {
93
                  array_push($fileExtension, "jpg");
94
                } elseif ($file_type == "image/bmp") {
95
                  array_push($fileExtension,  "bmp");
96
                } elseif ($file_type == "image/vnd.microsoft.icon") {
97
                  array_push($fileExtension,  "ico");
98
                } elseif ($file_type == "application/pdf") {
99
                  array_push($fileExtension, "pdf");
100
                } elseif ($file_type == "application/msword") {
101
                  array_push($fileExtension, "doc");
102
                } elseif ($file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
103
                  array_push($fileExtension, "docx");
104
                } elseif ($file_type == "application/vnd.ms-excel") {
105
                  array_push($fileExtension, "xls");
106
                } elseif ($file_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
107
                  array_push($fileExtension, "xlsx");
108
                } elseif ($file_type == "application/vnd.ms-powerpoint") {
109
                  array_push($fileExtension, "ppt");
110
                } elseif ($file_type == "application/vnd.openxmlformats-officedocument.presentationml.presentation") {
111
                  array_push($fileExtension, "pptx");
112
                } elseif ($file_type == "text/csv") {
113
                  array_push($fileExtension, "csv");
114
                } elseif ($file_type == "application/zip") {
115
                  array_push($fileExtension, "zip");
116
                }
117
              }
118
        		}
119
          }
120
        }
121
      }
122
    }
123
 
124
    $htmlmessage = urldecode($htmlmessage);
125
 
126
    $from = makeemailuser('email@domain.com', 'Moodle Admin', 2);
127
    $subject = "Email Notification";
128
    foreach ($emails as $key => $value) {
129
      if(checkemail($value)) {
130
        //$mail->AddAddress($value); // set recipient email address
131
        $to = makeemailuser($value, $value);
132
 
133
        $mail = new CustomMailer();
134
        $status = $mail->email_to_user_custom(true, $to, $from, $subject, html_to_text($htmlmessage), $htmlmessage, $fileUpload, $fileName, $fileExtension, true);
135
      }
136
    }
137
 
138
    //Send autoreply emails
139
    $fileUpload = $fileExtension = [];
140
 
141
    foreach ($autoRepEmails as $key => $value) {
142
      $value = makeemailuser($value);
143
      foreach ($autoRepData->attachments as $k => $file) {
144
        $image_parts = explode(";base64,", $file);
145
        if(count($image_parts) > 1) {
146
          $file_type = trim(explode(":", $image_parts[0])[1]);
147
 
148
          if (in_array($file_type, $allowedfileExtensions)) {
149
            $base = $file;
150
            array_push($fileUpload, base64_decode(str_replace(" ", "+", substr($base, strpos($base, ",")))));
151
 
152
            if($file_type == "image/png") {
153
              array_push($fileExtension, "png");
154
            } elseif ($file_type == "image/jpeg") {
155
              array_push($fileExtension, "jpg");
156
            } elseif ($file_type == "image/bmp") {
157
              array_push($fileExtension,  "bmp");
158
            } elseif ($file_type == "image/vnd.microsoft.icon") {
159
              array_push($fileExtension,  "ico");
160
            } elseif ($file_type == "application/pdf") {
161
              array_push($fileExtension, "pdf");
162
            } elseif ($file_type == "application/msword") {
163
              array_push($fileExtension, "doc");
164
            } elseif ($file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
165
              array_push($fileExtension, "docx");
166
            } elseif ($file_type == "application/vnd.ms-excel") {
167
              array_push($fileExtension, "xls");
168
            } elseif ($file_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
169
              array_push($fileExtension, "xlsx");
170
            } elseif ($file_type == "application/vnd.ms-powerpoint") {
171
              array_push($fileExtension, "ppt");
172
            } elseif ($file_type == "application/vnd.openxmlformats-officedocument.presentationml.presentation") {
173
              array_push($fileExtension, "pptx");
174
            } elseif ($file_type == "text/csv") {
175
              array_push($fileExtension, "csv");
176
            } elseif ($file_type == "application/zip") {
177
              array_push($fileExtension, "zip");
178
            }
179
          }
180
        }
181
      }
182
      $replyMail = new CustomMailer();
183
      $replyMail->email_to_user_custom(true, $value, $from, $subject, html_to_text($autoRepData->message), $autoRepData->message, $fileUpload, '', $fileExtension,  true);
184
    }
185
 
186
    if($status) {
187
      echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
188
              '. $confirmMessage .'
189
            </div>';
190
    } else {
191
      echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
192
              Sorry! Your message cannot been sent. Please contact administrator!
193
            </div>';
194
    }
195
  }
196
 
197
  function processData($data) {
198
    $data = trim($data);
199
    $data = stripslashes($data);
200
    $data = htmlspecialchars($data);
201
    return $data;
202
  }
203
 
204
  function makeemailuser($email, $name = '', $id = -99) {
205
    $emailuser = new stdClass();
206
    $emailuser->email = $email;
207
    $emailuser->firstname = format_text($name, FORMAT_PLAIN, array('trusted' => false));
208
    $emailuser->lastname = '';
209
    $emailuser->maildisplay = true;
210
    $emailuser->mailformat = 1; // 0 (zero) text-only emails, 1 (one) for HTML emails.
211
    $emailuser->id = $id;
212
    $emailuser->firstnamephonetic = '';
213
    $emailuser->lastnamephonetic = '';
214
    $emailuser->middlename = '';
215
    $emailuser->alternatename = '';
216
    return $emailuser;
217
  }
218
 
219
  function checkemail($str) {
220
    return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
221
  }