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
header("Access-Control-Allow-Origin: *");
12
header('Content-Type: application/json');
13
 
14
require_once('../../config.php');
15
global $USER, $DB, $CFG;
16
 
17
$PAGE->set_url('/local/cocoon_form_builder/updateinsert.php');
18
$PAGE->set_context(context_system::instance());
19
 
20
$_RESTREQUEST = file_get_contents("php://input");
21
$_POST = json_decode($_RESTREQUEST, true);
22
 
23
$attachments = array();
24
if(isset($_POST['attachments']) && $_POST['attachments'] != '') {
25
	$allowedfileExtensions = array("image/png", "image/jpeg", "image/bmp", "image/vnd.microsoft.icon", "application/pdf", "application/msword",
26
																 "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.ms-excel",
27
																 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
28
																 "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation",
29
																 "text/csv", "application/zip");
30
 
31
	foreach ($_POST['attachments'] as $key => $value) {
32
		define('UPLOAD_DIR', $CFG->dataroot);
33
		$image_parts = explode(";base64,", $value);
34
		if(count($image_parts) > 1) {
35
			$file_type_aux = explode(":", $image_parts[0]);
36
			$file_type = $file_type_aux[1];
37
			$image_base64 = base64_decode($image_parts[1]);
38
 
39
			$fileExtension = "";
40
 
41
			if((strpos($file_type, "image/png")) || ($file_type == "image/png")) {
42
				$fileExtension = "png";
43
			} elseif ((strpos($file_type, "image/jpeg")) || ($file_type == "image/jpeg")) {
44
				$fileExtension = "jpg";
45
			} elseif ((strpos($file_type, "image/bmp")) || ($file_type == "image/bmp")) {
46
				$fileExtension =  "bmp";
47
			} elseif ((strpos($file_type, "image/vnd.microsoft.icon")) || ($file_type == "image/vnd.microsoft.icon")) {
48
				$fileExtension =  "ico";
49
			} elseif ((strpos($file_type, "application/pdf")) || ($file_type == "application/pdf")) {
50
				$fileExtension = "pdf";
51
			} elseif ((strpos($file_type, "application/msword")) || ($file_type == "application/msword")) {
52
				$fileExtension = "doc";
53
			} elseif ((strpos($file_type, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) || ($file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
54
				$fileExtension = "docx";
55
			} elseif ((strpos($file_type, "application/vnd.ms-excel")) || ($file_type == "application/vnd.ms-excel")) {
56
				$fileExtension = "xls";
57
			} elseif ((strpos($file_type, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) || ($file_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
58
				$fileExtension = "xlsx";
59
			} elseif ((strpos($file_type, "application/vnd.ms-powerpoint")) || ($file_type == "application/vnd.ms-powerpoint")) {
60
				$fileExtension = "ppt";
61
			} elseif ((strpos($file_type, "application/vnd.openxmlformats-officedocument.presentationml.presentation")) || ($file_type == "application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
62
				$fileExtension = "pptx";
63
			} elseif ((strpos($file_type, "text/csv")) || ($file_type == "text/csv")) {
64
				$fileExtension = "csv";
65
			} elseif ((strpos($file_type, "application/zip")) || ($file_type == "application/zip")) {
66
				$fileExtension = "zip";
67
			}
68
 
69
			$fileName = uniqid() . '.' . $fileExtension;
70
			$file = UPLOAD_DIR . "/" . $fileName;
71
			file_put_contents($file, $image_base64);
72
 
73
			$f = $file;
74
			// Read image path, convert to base64 encoding
75
			$fData = base64_encode(file_get_contents($file));
76
 
77
			// Format the image SRC:  data:{mime};base64,{data};
78
			$src = 'data: '.mime_content_type($f).';base64,'.$fData;
79
 
80
			$attachments[] = $src;
81
		} else {
82
			$attachments[] = $value;
83
		}
84
	}
85
}
86
 
87
$reply = new stdClass();
88
$reply->subject =  $_POST['email_subject'];
89
$reply->message =  $_POST['email_message'];
90
$reply->attachments = $attachments;
91
 
92
 
93
$obj = new stdClass();
94
$obj->title = $_POST['title'];
95
$obj->json = $_POST['json'];
96
$obj->data = json_encode($reply);
97
$obj->url = $_POST['url'];
98
$obj->confirm_message = $_POST['confirm_message'];
99
$obj->recipients = $_POST['recipients'];
100
$obj->status = $_POST['status'];
101
$obj->ajax = $_POST['ajax'];
102
 
103
if(empty(trim($_POST['title']))) {
104
	$sql = "SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '{cocoon_form_builder_forms}'";
105
	$form = $DB->get_records_sql($sql);
106
	$value = intval(array_values($form)[0]->auto_increment);
107
	$obj->title = "Form #" . $value;
108
}
109
 
110
if(isset($_POST['id']) && $_POST['id'] !== '') {
111
	$obj->id = $_POST['id'];
112
	$result = $DB->update_record('cocoon_form_builder_forms', $obj);
113
	echo $result;
114
} else {
115
	$result = $DB->insert_record('cocoon_form_builder_forms', $obj, true, false);
116
	echo $result;
117
}