Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Code DGAFP
mentor.gouv.fr
Commits
daee30f3
Commit
daee30f3
authored
Jul 01, 2022
by
Adrien Jamot
Browse files
MERGE sprint28 into qualif
parent
f6b25bf9
Changes
118
Expand all
Hide whitespace changes
Inline
Side-by-side
.gitignore
0 → 100755
View file @
daee30f3
.idea
*.min.js
.gitlab-ci.yml
100644 → 100755
View file @
daee30f3
...
...
@@ -6,6 +6,7 @@ staging:
BUILD
:
build
SKIP_TEST
:
$SKIP_TEST
DEV
:
$DEV
maintenance
:
$maintenance
NEXUS_ARTEFACT_VERSION
:
$NEXUS_ARTEFACT_VERSION
NEXUS_REPOSITORY
:
$NEXUS_REPOSITORY
RELEASE_NUBO
:
$RELEASE_NUBO
...
...
@@ -23,4 +24,4 @@ staging:
strategy
:
depend
only
:
variables
:
-
$CI_PIPELINE_SOURCE != "push"
-
$CI_PIPELINE_SOURCE != "push"
\ No newline at end of file
blocks/mysessions/amd/build/.gitkeep
0 → 100644
View file @
daee30f3
blocks/mytrainings/amd/build/.gitkeep
0 → 100644
View file @
daee30f3
course/format/edadmin/amd/build/.gitkeep
0 → 100644
View file @
daee30f3
course/format/edadmin/amd/src/format_edadmin.js
View file @
daee30f3
...
...
@@ -94,15 +94,16 @@ define([
$maincontent
.
append
(
$buttons
);
for
(
var
buttonindex
in
params
.
buttons
)
{
var
button
=
params
.
buttons
[
buttonindex
];
var
$button
=
$
(
'
<button class="edadmin-button">
'
+
button
.
text
+
'
</button>
'
);
if
(
button
.
id
)
{
$button
.
attr
(
'
id
'
,
button
.
id
);
}
$button
s
.
append
(
$
button
)
;
$button
.
on
(
'
click
'
,
button
,
function
(
e
)
{
e
.
preventDefault
();
e
.
data
.
onclick
();
var
text
=
encodeURIComponent
(
button
.
text
);
var
$button
=
document
.
createElement
(
'
button
'
);
$button
.
classList
.
add
(
'
edadmin-
button
'
);
$button
.
textContent
=
decodeURIComponent
(
text
);
$button
.
data
=
button
;
$button
.
addEventListener
(
'
click
'
,
function
(
e
)
{
e
.
preventDefault
();
e
.
currentTarget
.
data
.
onclick
();
});
$buttons
.
append
(
$button
);
}
}
$
(
'
#
'
+
params
.
modalid
+
'
.close
'
).
on
(
'
click
'
,
function
()
{
...
...
enrol/sirh/ajax/ajax.php
View file @
daee30f3
...
...
@@ -44,10 +44,16 @@ $format = optional_param('format', 'html', PARAM_TEXT);
// Call front controller.
$frontcontroller
=
new
\
local_mentor_core\front_controller
(
'sirh'
,
'enrol_sirh\\'
);
// Call the controller method, choose the format and print the result.
if
(
strtolower
(
$format
)
==
'json'
)
{
echo
json_encode
(
$frontcontroller
->
execute
());
}
else
{
echo
$frontcontroller
->
execute
();
try
{
// Call the controller method, choose the format and print the result.
if
(
strtolower
(
$format
)
==
'json'
)
{
echo
json_encode
(
$frontcontroller
->
execute
());
}
else
{
echo
$frontcontroller
->
execute
();
}
}
catch
(
Exception
$e
)
{
echo
json_encode
([
'success'
=>
false
,
'message'
=>
$e
->
getMessage
()]);
}
enrol/sirh/amd/build/.gitkeep
0 → 100644
View file @
daee30f3
enrol/sirh/amd/src/enrol_sirh.js
View file @
daee30f3
...
...
@@ -49,8 +49,18 @@ define([
that
.
table
=
$
(
'
#sessions-table
'
).
DataTable
({
ajax
:
{
// Call edadmin course list.
url
:
M
.
cfg
.
wwwroot
+
'
/enrol/sirh/ajax/ajax.php
'
,
"
dataSrc
"
:
function
(
json
)
{
if
(
typeof
json
.
success
!=
'
undefined
'
)
{
$
(
'
#sessions-table_wrapper
'
).
hide
();
$
(
'
<div class="error">
'
+
json
.
message
+
'
</div>
'
).
insertBefore
(
'
#sessions-table_wrapper
'
);
return
;
}
else
{
$
(
'
#sessions-table_wrapper
'
).
show
();
$
(
'
.error
'
).
remove
();
return
json
.
data
;
}
},
data
:
function
(
d
)
{
// GET HTTP data setting.
d
.
controller
=
'
sirh
'
;
d
.
plugintype
=
'
enrol
'
;
...
...
@@ -60,8 +70,12 @@ define([
d
.
sirh
=
[
$
(
'
#sirh
'
).
val
()];
d
.
sirhtraining
=
$
(
'
#trainingsirh
'
).
val
();
d
.
sirhsession
=
$
(
'
#sessionsirh
'
).
val
();
d
.
sirhsessionname
=
$
(
'
#sessionlabelsirh
'
).
val
();
d
.
sirhtrainingname
=
$
(
'
#traininglabelsirh
'
).
val
();
d
.
datestart
=
$
(
'
#datestart
'
).
val
();
d
.
dateend
=
$
(
'
#dateend
'
).
val
();
},
error
:
function
(
xhr
,
error
,
code
)
{
console
.
log
(
xhr
,
code
);
}
},
oLanguage
:
{
...
...
@@ -113,7 +127,7 @@ define([
return
'
<span class="existing">Déjà existant</span>
'
;
}
return
'
<button class="session-users-button" data-sirh="
'
+
data
.
sirh
+
'
" data-sirhtraining="
'
+
data
.
sirhtraining
+
'
" data-sirhsession="
'
+
data
.
sirhsession
+
'
" title="
'
+
M
.
util
.
get_string
(
'
instancepluginname
'
,
'
enrol_sirh
'
)
+
'
"></button>
'
;
return
'
<button class="session-users-button" data-sirh="
'
+
data
.
sirh
+
'
" data-sirhtraining="
'
+
data
.
sirhtraining
+
'
" data-sirhsession="
'
+
data
.
sirhsession
+
'
" title="
'
+
M
.
util
.
get_string
(
'
instancepluginname
'
,
'
enrol_sirh
'
)
+
'
"></button>
'
;
}
}
]
...
...
@@ -219,14 +233,15 @@ define([
sirh
:
sirh
,
sirhtraining
:
sirhtraining
,
sirhsession
:
sirhsession
},
},
error
:
function
(
xhr
,
error
,
code
)
{
console
.
log
(
xhr
,
code
);
}
}).
done
(
function
(
response
)
{
response
=
JSON
.
parse
(
response
);
if
(
response
.
message
)
{
window
.
location
.
href
=
that
.
managesessionsurl
;
}
else
{
}
else
{
// TODO : manage error.
console
.
log
(
response
);
...
...
enrol/sirh/classes/api/sirh.php
View file @
daee30f3
...
...
@@ -57,14 +57,16 @@ class sirh_api {
self
::
check_enrol_sirh_capability
(
$session
->
get_context
()
->
id
);
// Check if "sirh" key filter exist.
if
(
!
isset
(
$filter
[
'
sirh
'
]))
{
throw
new
\
Exception
(
'Missing filter :
sirh
'
);
if
(
!
isset
(
$filter
[
'
listeIdentifiantSirhOrigine
'
]))
{
throw
new
\
Exception
(
'Missing filter :
listeIdentifiantSirhOrigine
'
);
}
$sirhfilter
=
[];
$sirhlist
=
explode
(
','
,
$filter
[
'listeIdentifiantSirhOrigine'
]);
// If exist, check if sirh is link to entity.
foreach
(
$
filter
[
'sirh'
]
as
$sirhdatafilter
)
{
foreach
(
$
sirhlist
as
$sirhdatafilter
)
{
if
(
in_array
(
$sirhdatafilter
,
$entitysirhlist
))
{
$sirhfilter
[]
=
$sirhdatafilter
;
}
...
...
@@ -76,10 +78,10 @@ class sirh_api {
}
// Re-set sirh data filter.
$filter
[
'
sirh'
]
=
$sirhfilter
;
$filter
[
'
listeIdentifiantSirhOrigine'
]
=
implode
(
','
,
$sirhfilter
)
;
// Get SIRH REST API.
$sirhrest
=
\
enrol_sirh\sirh
::
get_instance
();
$sirhrest
=
static
::
get_sirh_rest_api
();
// Call request.
$sirhsessions
=
$sirhrest
->
get_sirh_sessions
(
$filter
);
...
...
@@ -107,6 +109,65 @@ class sirh_api {
return
$finalsessions
;
}
/**
* Count all SIRH returns to the REST API request with different filter.
* key filter :
* - "sirh" => Originals SIRH link with session Entity
* - "sirhtraining" => Text conditional to training SIRH id
* - "sirhsession" => Text conditional to session SIRH id
* - "datestart" => Conditional when to session SIRH start
* - "dateend" => Conditional when to session SIRH finish
* - "order" => Defines on which element the return list will be ordered
* - "start" => Select page list
* - "length" => Define number of element by page
*
* @param array $filter
* @return int
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
*/
public
static
function
count_sirh_sessions
(
$sessionid
,
$filter
)
{
// Get session and this entity sirh list.
$session
=
\
local_mentor_core\session_api
::
get_session
(
$sessionid
);
$entitysirhlist
=
$session
->
get_entity
()
->
get_main_entity
()
->
get_sirh_list
();
$sirhinstances
=
$session
->
get_sirh_instances
();
// Check user capacbility.
self
::
check_enrol_sirh_capability
(
$session
->
get_context
()
->
id
);
// Check if "sirh" key filter exist.
if
(
!
isset
(
$filter
[
'listeIdentifiantSirhOrigine'
]))
{
throw
new
\
Exception
(
'Missing filter : listeIdentifiantSirhOrigine'
);
}
$sirhfilter
=
[];
$sirhlist
=
explode
(
','
,
$filter
[
'listeIdentifiantSirhOrigine'
]);
// If exist, check if sirh is link to entity.
foreach
(
$sirhlist
as
$sirhdatafilter
)
{
if
(
in_array
(
$sirhdatafilter
,
$entitysirhlist
))
{
$sirhfilter
[]
=
$sirhdatafilter
;
}
}
// Not access if sirh filter is empty.
if
(
empty
(
$sirhfilter
))
{
throw
new
\
Exception
(
'Permission denied'
);
}
// Re-set sirh data filter.
$filter
[
'listeIdentifiantSirhOrigine'
]
=
implode
(
','
,
$sirhfilter
);
// Get SIRH REST API.
$sirhrest
=
static
::
get_sirh_rest_api
();
return
$sirhrest
->
count_sirh_sessions
(
$filter
);
}
/**
* Return result of users list of SIRH session select
* to the REST API request
...
...
@@ -135,7 +196,7 @@ class sirh_api {
}
// Get SIRH REST API.
$sirhrest
=
\
enrol_sirh\sirh
::
get_instance
();
$sirhrest
=
static
::
get_sirh_rest_api
();
// Call request.
return
$sirhrest
->
get_session_users
(
$sirh
,
$sirhtraining
,
$sirhsession
,
$nbuser
);
...
...
@@ -168,7 +229,7 @@ class sirh_api {
}
// Get SIRH REST API.
$sirhrest
=
\
enrol_sirh\sirh
::
get_instance
();
$sirhrest
=
static
::
get_sirh_rest_api
();
// Get all users link with SIRH session.
$users
=
$sirhrest
->
get_session_users
(
$sirh
,
$sirhtraining
,
$sirhsession
);
...
...
@@ -322,4 +383,13 @@ class sirh_api {
throw
new
\
Exception
(
'Permission denied'
);
}
}
/**
* Get SIRH REST API.
*
* @return sirh
*/
public
static
function
get_sirh_rest_api
()
{
return
\
enrol_sirh\sirh
::
get_instance
();
}
}
enrol/sirh/classes/controllers/sirh_controller.php
View file @
daee30f3
...
...
@@ -53,23 +53,49 @@ class sirh_controller extends controller_base {
switch
(
$action
)
{
case
'get_sirh_sessions'
:
$sessionid
=
$this
->
get_param
(
'sessionid'
,
PARAM_INT
);
$data
=
new
\
stdClass
();
$filter
[
'sirh'
]
=
$this
->
get_param
(
'sirh'
,
PARAM_TEXT
);
$data
->
recordsTotal
=
count
(
$this
->
get_sirh_sessions
(
$sessionid
,
$filter
));
$data
=
new
\
stdClass
();
$filter
[
'listeIdentifiantSirhOrigine'
]
=
implode
(
','
,
$this
->
get_param
(
'sirh'
,
PARAM_TEXT
));
$data
->
recordsTotal
=
$this
->
count_sirh_sessions
(
$sessionid
,
$filter
);
$filter
[
'identifiantFormation'
]
=
$this
->
get_param
(
'sirhtraining'
,
PARAM_TEXT
,
null
);
$filter
[
'identifiantSession'
]
=
$this
->
get_param
(
'sirhsession'
,
PARAM_TEXT
,
null
);
$filter
[
'libelleSession'
]
=
$this
->
get_param
(
'sirhsessionname'
,
PARAM_TEXT
,
null
);
$filter
[
'libelleFormation'
]
=
$this
->
get_param
(
'sirhtrainingname'
,
PARAM_TEXT
,
null
);
$filter
[
'dateDebut'
]
=
$this
->
get_param
(
'datestart'
,
PARAM_TEXT
,
null
);
$filter
[
'dateFin'
]
=
$this
->
get_param
(
'dateend'
,
PARAM_TEXT
,
null
);
$data
->
recordsFiltered
=
$this
->
count_sirh_sessions
(
$sessionid
,
$filter
);
$order
=
$this
->
get_param
(
'order'
,
PARAM_RAW
,
null
);
$order
=
is_null
(
$order
)
?
$order
:
$order
[
0
];
// Manage sort order.
if
(
!
is_null
(
$order
))
{
$columnsorder
=
[
'identifiantFormation'
,
'libelleFormation'
,
'identifiantSession'
,
'libelleSession'
,
'dateDebut'
,
'dateFin'
];
$filter
[
'tris'
]
=
$columnsorder
[
$order
[
'column'
]]
.
' '
.
strtoupper
(
$order
[
'dir'
]);
}
$filter
[
'nombreElementPage'
]
=
$this
->
get_param
(
'length'
,
PARAM_INT
,
null
);
$filter
[
'sirhformation'
]
=
$this
->
get_param
(
'sirhtraining'
,
PARAM_TEXT
,
null
);
$filter
[
'sirhsession'
]
=
$this
->
get_param
(
'sirhsession'
,
PARAM_TEXT
,
null
);
$filter
[
'datestart'
]
=
$this
->
get_param
(
'datestart'
,
PARAM_TEXT
,
null
);
$filter
[
'dateend'
]
=
$this
->
get_param
(
'dateend'
,
PARAM_TEXT
,
null
);
$data
->
recordsFiltered
=
count
(
$this
->
get_sirh_sessions
(
$sessionid
,
$filter
));
if
(
!
is_null
(
$filter
[
'nombreElementPage'
]))
{
$filter
[
'order'
]
=
$this
->
get_param
(
'order'
,
PARAM_RAW
,
null
);
$filter
[
'order'
]
=
is_null
(
$filter
[
'order'
])
?
$filter
[
'order'
]
:
$filter
[
'order'
][
0
];
$filter
[
'start'
]
=
$this
->
get_param
(
'start'
,
PARAM_INT
,
null
);
$filter
[
'length'
]
=
$this
->
get_param
(
'length'
,
PARAM_INT
,
null
);
$data
->
data
=
$this
->
get_sirh_sessions
(
$sessionid
,
$filter
);
$filter
[
'numeroPage'
]
=
$this
->
get_param
(
'start'
,
PARAM_INT
,
null
);
if
(
!
is_null
(
$filter
[
'numeroPage'
]))
{
$filter
[
'numeroPage'
]
=
$filter
[
'numeroPage'
]
/
$filter
[
'nombreElementPage'
]
+
1
;
}
}
$data
->
data
=
$this
->
get_sirh_sessions
(
$sessionid
,
$filter
);
$data
->
length
=
$this
->
get_param
(
'length'
,
PARAM_INT
,
null
);
$data
->
start
=
$this
->
get_param
(
'start'
,
PARAM_INT
,
null
);
...
...
@@ -124,6 +150,20 @@ class sirh_controller extends controller_base {
return
sirh_api
::
get_sirh_sessions
(
$sessionid
,
$filter
);
}
/**
* Count SIRH sessions
*
* @param int $sessionid
* @param array $filter
* @return int
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
*/
public
static
function
count_sirh_sessions
(
$sessionid
,
$filter
)
{
return
sirh_api
::
count_sirh_sessions
(
$sessionid
,
$filter
);
}
/**
* Return result of users list of SIRH session select
* to the REST API request
...
...
enrol/sirh/classes/restclient.php
0 → 100644
View file @
daee30f3
<?php
declare
(
strict_types
=
1
);
/**
* PHP REST Client
* https://github.com/tcdent/php-restclient
* (c) 2013-2022 Travis Dent <tcdent@gmail.com>
*/
class
RestClientException
extends
Exception
{}
class
RestClient
implements
Iterator
,
ArrayAccess
{
public
$options
;
public
$handle
;
// cURL resource handle.
// Populated after execution:
public
$response
;
// Response body.
public
$headers
;
// Parsed reponse header object.
public
$info
;
// Response info object.
public
$error
;
// Response error string.
public
$response_status_lines
;
// indexed array of raw HTTP response status lines.
// Populated as-needed.
public
$decoded_response
;
// Decoded response body.
public
function
__construct
(
array
$options
=
[]){
$default_options
=
[
'headers'
=>
[],
'parameters'
=>
[],
'curl_options'
=>
[],
'build_indexed_queries'
=>
FALSE
,
'user_agent'
=>
"PHP RestClient/0.1.8"
,
'base_url'
=>
NULL
,
'format'
=>
NULL
,
'format_regex'
=>
"/(\w+)\/(\w+)(;[.+])?/"
,
'decoders'
=>
[
'json'
=>
'json_decode'
,
'php'
=>
'unserialize'
],
'username'
=>
NULL
,
'password'
=>
NULL
];
$this
->
options
=
array_merge
(
$default_options
,
$options
);
if
(
array_key_exists
(
'decoders'
,
$options
))
$this
->
options
[
'decoders'
]
=
array_merge
(
$default_options
[
'decoders'
],
$options
[
'decoders'
]);
}
public
function
set_option
(
$key
,
$value
)
:
void
{
$this
->
options
[
$key
]
=
$value
;
}
public
function
register_decoder
(
string
$format
,
callable
$method
)
:
void
{
// Decoder callbacks must adhere to the following pattern:
// array my_decoder(string $data)
$this
->
options
[
'decoders'
][
$format
]
=
$method
;
}
// Iterable methods:
public
function
rewind
()
:
void
{
$this
->
decode_response
();
reset
(
$this
->
decoded_response
);
}
public
function
current
()
:
mixed
{
return
current
(
$this
->
decoded_response
);
}
public
function
key
()
:
mixed
{
return
key
(
$this
->
decoded_response
);
}
public
function
next
()
:
void
{
next
(
$this
->
decoded_response
);
}
public
function
valid
()
:
bool
{
return
is_array
(
$this
->
decoded_response
)
&&
(
key
(
$this
->
decoded_response
)
!==
NULL
);
}
// ArrayAccess methods:
public
function
offsetExists
(
$key
)
:
bool
{
$this
->
decode_response
();
return
is_array
(
$this
->
decoded_response
)
?
isset
(
$this
->
decoded_response
[
$key
])
:
isset
(
$this
->
decoded_response
->
{
$key
});
}
public
function
offsetGet
(
$key
)
:
mixed
{
$this
->
decode_response
();
if
(
!
$this
->
offsetExists
(
$key
))
return
NULL
;
return
is_array
(
$this
->
decoded_response
)
?
$this
->
decoded_response
[
$key
]
:
$this
->
decoded_response
->
{
$key
};
}
public
function
offsetSet
(
$key
,
$value
)
:
void
{
throw
new
RestClientException
(
"Decoded response data is immutable."
);
}
public
function
offsetUnset
(
$key
)
:
void
{
throw
new
RestClientException
(
"Decoded response data is immutable."
);
}
// Request methods:
public
function
get
(
string
$url
,
$parameters
=
[],
array
$headers
=
[])
:
RestClient
{
return
$this
->
execute
(
$url
,
'GET'
,
$parameters
,
$headers
);
}
public
function
post
(
string
$url
,
$parameters
=
[],
array
$headers
=
[])
:
RestClient
{
return
$this
->
execute
(
$url
,
'POST'
,
$parameters
,
$headers
);
}
public
function
put
(
string
$url
,
$parameters
=
[],
array
$headers
=
[])
:
RestClient
{
return
$this
->
execute
(
$url
,
'PUT'
,
$parameters
,
$headers
);
}
public
function
patch
(
string
$url
,
$parameters
=
[],
array
$headers
=
[])
:
RestClient
{
return
$this
->
execute
(
$url
,
'PATCH'
,
$parameters
,
$headers
);
}
public
function
delete
(
string
$url
,
$parameters
=
[],
array
$headers
=
[])
:
RestClient
{
return
$this
->
execute
(
$url
,
'DELETE'
,
$parameters
,
$headers
);
}
public
function
head
(
string
$url
,
$parameters
=
[],
array
$headers
=
[])
:
RestClient
{
return
$this
->
execute
(
$url
,
'HEAD'
,
$parameters
,
$headers
);
}
public
function
execute
(
string
$url
,
string
$method
=
'GET'
,
$parameters
=
[],
array
$headers
=
[])
:
RestClient
{
$client
=
clone
$this
;
$client
->
url
=
$url
;
$client
->
handle
=
curl_init
();
$curlopt
=
[
CURLOPT_HEADER
=>
TRUE
,
CURLOPT_RETURNTRANSFER
=>
TRUE
,
CURLOPT_USERAGENT
=>
$client
->
options
[
'user_agent'
]
];
if
(
$client
->
options
[
'username'
]
&&
$client
->
options
[
'password'
])
$curlopt
[
CURLOPT_USERPWD
]
=
sprintf
(
"%s:%s"
,
$client
->
options
[
'username'
],
$client
->
options
[
'password'
]);
if
(
count
(
$client
->
options
[
'headers'
])
||
count
(
$headers
)){
$curlopt
[
CURLOPT_HTTPHEADER
]
=
[];
$headers
=
array_merge
(
$client
->
options
[
'headers'
],
$headers
);
foreach
(
$headers
as
$key
=>
$values
){
foreach
(
is_array
(
$values
)
?
$values
:
[
$values
]
as
$value
){
$curlopt
[
CURLOPT_HTTPHEADER
][]
=
sprintf
(
"%s:%s"
,
$key
,
$value
);
}
}
}
if
(
$client
->
options
[
'format'
])
$client
->
url
.
=
'.'
.
$client
->
options
[
'format'
];
// Allow passing parameters as a pre-encoded string (or something that
// allows casting to a string). Parameters passed as strings will not be
// merged with parameters specified in the default options.
if
(
is_array
(
$parameters
)){
$parameters
=
array_merge
(
$client
->
options
[
'parameters'
],
$parameters
);
$parameters_string
=
http_build_query
(
$parameters
);
// http_build_query automatically adds an array index to repeated
// parameters which is not desirable on most systems. This hack
// reverts "key[0]=foo&key[1]=bar" to "key[]=foo&key[]=bar"
if
(
!
$client
->
options
[
'build_indexed_queries'
])
$parameters_string
=
preg_replace
(
"/%5B[0-9]+%5D=/simU"
,
"%5B%5D="
,
$parameters_string
);
// TODO : supprimer ?
$parameters_string
=
str_replace
(
'%2C'
,
','
,
$parameters_string
);
$parameters_string
=
str_replace
(
'&'
,
'&'
,
$parameters_string
);
$parameters_string
=
str_replace
(
'+'
,
'%20'
,
$parameters_string
);
}
else
$parameters_string
=
(
string
)
$parameters
;
if
(
strtoupper
(
$method
)
===
'POST'
){
$curlopt
[
CURLOPT_POST
]
=
TRUE
;
$curlopt
[
CURLOPT_POSTFIELDS
]
=
$parameters_string
;
}
elseif
(
strtoupper
(
$method
)
!==
'GET'
){
$curlopt
[
CURLOPT_CUSTOMREQUEST
]
=
strtoupper
(
$method
);
$curlopt
[
CURLOPT_POSTFIELDS
]
=
$parameters_string
;
}
elseif
(
$parameters_string
){
$client
->
url
.
=
strpos
(
$client
->
url
,
'?'
)
?
'&'
:
'?'
;
$client
->
url
.
=
$parameters_string
;
}
if
(
$client
->
options
[
'base_url'
]){
if
(
$client
->
url
[
0
]
!==
'/'
&&
substr
(
$client
->
options
[
'base_url'
],
-
1
)
!==
'/'
)
$client
->
url
=
'/'
.
$client
->
url
;
$client
->
url
=
$client
->
options
[
'base_url'
]
.
$client
->
url
;
}
$curlopt
[
CURLOPT_URL
]
=
$client
->
url
;
if
(
$client
->
options
[
'curl_options'
]){
// array_merge would reset our numeric keys.
foreach
(
$client
->
options
[
'curl_options'
]
as
$key
=>
$value
){
$curlopt
[
$key
]
=
$value
;
}
}
curl_setopt_array
(
$client
->
handle
,
$curlopt
);
$client
->
parse_response
(
curl_exec
(
$client
->
handle
));
$client
->
info
=
(
object
)
curl_getinfo
(
$client
->
handle
);
$client
->
error
=
curl_error
(
$client
->
handle
);
curl_close
(
$client
->
handle
);
return
$client
;
}
public
function
parse_response
(
$response
)
:
void
{
$headers
=
[];
$this
->
response_status_lines
=
[];
$line
=
strtok
(
$response
,
"
\n
"
);
do
{
if
(
strlen
(
trim
(
$line
))
==
0
){
// Since we tokenize on \n, use the remaining \r to detect empty lines.
if
(
count
(
$headers
)
>
0
)
break
;
// Must be the newline after headers, move on to response body
}
elseif
(
strpos
(
$line
,
'HTTP'
)
===
0
){
// One or more HTTP status lines
$this
->
response_status_lines
[]
=
trim
(
$line
);
}
else
{
// Has to be a header
[
$key
,
$value
]
=
explode
(
':'
,
$line
,
2
);
$key
=
strtolower
(
trim
(
str_replace
(
'-'
,
'_'
,
$key
)));
$value
=
trim
(
$value
);
if
(
empty
(
$headers
[
$key
]))
$headers
[
$key
]
=
$value
;
elseif
(
is_array
(
$headers
[
$key
]))
$headers
[
$key
][]
=
$value
;
else
$headers
[
$key
]
=
[
$headers
[
$key
],
$value
];
}
}
while
(
$line
=
strtok
(
"
\n
"
));