Skip to content

Commit

Permalink
Avoid PHP warning on preg_split() and unserialize() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
weilai-irl committed May 2, 2023
1 parent 78bd0d5 commit 1d7c964
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
6 changes: 5 additions & 1 deletion classes/adminsetting/usersynccreationrestriction.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public function __construct($name, $visiblename, $description, $defaultsetting)
* @return mixed returns config if successful else null
*/
public function get_setting() {
return unserialize($this->config_read($this->name));
$setting = $this->config_read($this->name);
if (is_null($setting)) {
$setting = '';
}
return unserialize($setting);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/feature/coursesync/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public static function get_group_mail_alias(stdClass $course) : string {
*/
public static function clean_up_group_mail_alias(string $mailalias) {
$notallowedbasicchars = ['@', '(', ')', "\\", '[', ']', '"', ';', ':', '.', '<', '>', ' '];
$chars = preg_split( '//u', $mailalias, null, PREG_SPLIT_NO_EMPTY);
$chars = preg_split( '//u', $mailalias, -1, PREG_SPLIT_NO_EMPTY);
foreach($chars as $key => $char){
$charorder = ord($char);
if ($charorder < 0 || $charorder > 127 || in_array($char, $notallowedbasicchars)) {
Expand Down
8 changes: 8 additions & 0 deletions classes/healthcheck/systemapiuser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class systemapiuser implements \local_o365\healthcheck\healthcheckinterface {
public function run() {
// Check that the system API user has a graph resource.
$tokens = get_config('local_o365', 'systemtokens');
if (empty($tokens)) {
return [
'result' => false,
'severity' => static::SEVERITY_WARNING,
'message' => get_string('healthcheck_systemtoken_result_notokens', 'local_o365'),
'fixlink' => new \moodle_url('/local/o365/acp.php', ['mode' => 'setsystemuser']),
];
}
$tokens = unserialize($tokens);
$graphresource = \local_o365\rest\unified::get_tokenresource();
if (!isset($tokens[$graphresource])) {
Expand Down
18 changes: 16 additions & 2 deletions classes/oauth2/apptoken.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public function refresh() {
*/
protected static function get_stored_token($userid, $tokenresource) {
$tokens = get_config('local_o365', 'apptokens');
if (empty($tokens)) {
return false;
}
$tokens = unserialize($tokens);
if (isset($tokens[$tokenresource])) {
// App tokens do not have a user.
Expand All @@ -205,7 +208,11 @@ protected static function get_stored_token($userid, $tokenresource) {
*/
protected function update_stored_token($existingtoken, $newtoken) {
$tokens = get_config('local_o365', 'apptokens');
$tokens = unserialize($tokens);
if ($tokens) {
$tokens = unserialize($tokens);
} else {
$tokens = [];
}
if (isset($tokens[$existingtoken['tokenresource']])) {
unset($tokens[$existingtoken['tokenresource']]);
}
Expand All @@ -227,6 +234,9 @@ protected function update_stored_token($existingtoken, $newtoken) {
*/
protected function delete_stored_token($existingtoken) {
$tokens = get_config('local_o365', 'apptokens');
if (empty($tokens)) {
return true;
}
$tokens = unserialize($tokens);
if (isset($tokens[$existingtoken['tokenresource']])) {
unset($tokens[$existingtoken['tokenresource']]);
Expand All @@ -253,7 +263,11 @@ public static function store_new_token($userid, $token, $expiry, $refreshtoken,
$tokenresource = 'https://graph.microsoft.com';
}
$tokens = get_config('local_o365', 'apptokens');
$tokens = unserialize($tokens);
if (empty($tokens)) {
$tokens = [];
} else {
$tokens = unserialize($tokens);
}
$newtoken = [
'token' => $token,
'expiry' => $expiry,
Expand Down
18 changes: 16 additions & 2 deletions classes/oauth2/systemapiusertoken.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class systemapiusertoken extends \local_o365\oauth2\token {
*/
protected static function get_stored_token($userid, $tokenresource) {
$tokens = get_config('local_o365', 'systemtokens');
if (empty($tokens)) {
return false;
}
$tokens = unserialize($tokens);
if (isset($tokens[$tokenresource])) {
$tokens[$tokenresource]['user_id'] = null;
Expand All @@ -59,7 +62,11 @@ protected static function get_stored_token($userid, $tokenresource) {
*/
protected function update_stored_token($existingtoken, $newtoken) {
$tokens = get_config('local_o365', 'systemtokens');
$tokens = unserialize($tokens);
if (empty($tokens)) {
$tokens = [];
} else {
$tokens = unserialize($tokens);
}
if (isset($tokens[$existingtoken['tokenresource']])) {
unset($tokens[$existingtoken['tokenresource']]);
}
Expand All @@ -77,6 +84,9 @@ protected function update_stored_token($existingtoken, $newtoken) {
*/
protected function delete_stored_token($existingtoken) {
$tokens = get_config('local_o365', 'systemtokens');
if (empty($tokens)) {
return true;
}
$tokens = unserialize($tokens);
if (isset($tokens[$existingtoken['tokenresource']])) {
unset($tokens[$existingtoken['tokenresource']]);
Expand All @@ -100,7 +110,11 @@ protected function delete_stored_token($existingtoken) {
*/
public static function store_new_token($userid, $token, $expiry, $refreshtoken, $scope, $tokenresource) {
$tokens = get_config('local_o365', 'systemtokens');
$tokens = unserialize($tokens);
if (empty($tokens)) {
$tokens = [];
} else {
$tokens = unserialize($tokens);
}
$newtoken = [
'token' => $token,
'expiry' => $expiry,
Expand Down
18 changes: 16 additions & 2 deletions classes/oauth2/systemtoken.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static function instance($userid, $tokenresource, \local_o365\oauth2\clie
*/
protected static function get_stored_token($userid, $tokenresource) {
$tokens = get_config('local_o365', 'systemtokens');
if (empty($tokens)) {
return false;
}
$tokens = unserialize($tokens);
if (isset($tokens[$tokenresource])) {
$tokens[$tokenresource]['user_id'] = null;
Expand All @@ -73,7 +76,11 @@ protected static function get_stored_token($userid, $tokenresource) {
*/
protected function update_stored_token($existingtoken, $newtoken) {
$tokens = get_config('local_o365', 'systemtokens');
$tokens = unserialize($tokens);
if (empty($tokens)) {
$tokens = [];
} else {
$tokens = unserialize($tokens);
}
if (isset($tokens[$existingtoken['tokenresource']])) {
unset($tokens[$existingtoken['tokenresource']]);
}
Expand All @@ -91,6 +98,9 @@ protected function update_stored_token($existingtoken, $newtoken) {
*/
protected function delete_stored_token($existingtoken) {
$tokens = get_config('local_o365', 'systemtokens');
if (empty($tokens)) {
return true;
}
$tokens = unserialize($tokens);
if (isset($tokens[$existingtoken['tokenresource']])) {
unset($tokens[$existingtoken['tokenresource']]);
Expand All @@ -114,7 +124,11 @@ protected function delete_stored_token($existingtoken) {
*/
public static function store_new_token($userid, $token, $expiry, $refreshtoken, $scope, $tokenresource) {
$tokens = get_config('local_o365', 'systemtokens');
$tokens = unserialize($tokens);
if (empty($tokens)) {
$tokens = [];
} else {
$tokens = unserialize($tokens);
}
$newtoken = [
'token' => $token,
'expiry' => $expiry,
Expand Down

0 comments on commit 1d7c964

Please sign in to comment.