[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Airnotifier manager class 19 * 20 * @package message_airnotifier 21 * @category external 22 * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 2.7 25 */ 26 27 defined('MOODLE_INTERNAL') || die; 28 29 /** 30 * Airnotifier helper manager class 31 * 32 * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class message_airnotifier_manager { 36 37 /** 38 * Include the relevant javascript and language strings for the device 39 * toolbox YUI module 40 * 41 * @return bool 42 */ 43 public function include_device_ajax() { 44 global $PAGE, $CFG; 45 46 $config = new stdClass(); 47 $config->resturl = '/message/output/airnotifier/rest.php'; 48 $config->pageparams = array(); 49 50 // Include toolboxes. 51 $PAGE->requires->yui_module('moodle-message_airnotifier-toolboxes', 'M.message.init_device_toolbox', array(array( 52 'ajaxurl' => $config->resturl, 53 'config' => $config, 54 )) 55 ); 56 57 // Required strings for the javascript. 58 $PAGE->requires->strings_for_js(array('deletecheckdevicename'), 'message_airnotifier'); 59 $PAGE->requires->strings_for_js(array('show', 'hide'), 'moodle'); 60 61 return true; 62 } 63 64 /** 65 * Return the user devices for a specific app. 66 * 67 * @param string $appname the app name . 68 * @param int $userid if empty take the current user. 69 * @return array all the devices 70 */ 71 public function get_user_devices($appname, $userid = null) { 72 global $USER, $DB; 73 74 if (empty($userid)) { 75 $userid = $USER->id; 76 } 77 78 $devices = array(); 79 80 $params = array('appid' => $appname, 'userid' => $userid); 81 82 // First, we look all the devices registered for this user in the Moodle core. 83 // We are going to allow only ios devices (since these are the ones that supports PUSH notifications). 84 $userdevices = $DB->get_records('user_devices', $params); 85 foreach ($userdevices as $device) { 86 if (core_text::strtolower($device->platform)) { 87 // Check if the device is known by airnotifier. 88 if (!$airnotifierdev = $DB->get_record('message_airnotifier_devices', 89 array('userdeviceid' => $device->id))) { 90 91 // We have to create the device token in airnotifier. 92 if (! $this->create_token($device->pushid)) { 93 continue; 94 } 95 96 $airnotifierdev = new stdClass; 97 $airnotifierdev->userdeviceid = $device->id; 98 $airnotifierdev->enable = 1; 99 $airnotifierdev->id = $DB->insert_record('message_airnotifier_devices', $airnotifierdev); 100 } 101 $device->id = $airnotifierdev->id; 102 $device->enable = $airnotifierdev->enable; 103 $devices[] = $device; 104 } 105 } 106 107 return $devices; 108 } 109 110 /** 111 * Request and access key to Airnotifier 112 * 113 * @return mixed The access key or false in case of error 114 */ 115 public function request_accesskey() { 116 global $CFG, $USER; 117 118 require_once($CFG->libdir . '/filelib.php'); 119 120 // Sending the request access key request to Airnotifier. 121 $serverurl = $CFG->airnotifierurl . ':' . $CFG->airnotifierport . '/accesskeys/'; 122 // We use an APP Key "none", it can be anything. 123 $header = array('Accept: application/json', 'X-AN-APP-NAME: ' . $CFG->airnotifierappname, 124 'X-AN-APP-KEY: none'); 125 $curl = new curl(); 126 $curl->setHeader($header); 127 128 // Site ids are stored as secrets in md5 in the Moodle public hub. 129 $params = array( 130 'url' => $CFG->wwwroot, 131 'siteid' => md5($CFG->siteidentifier), 132 'contact' => $USER->email, 133 'description' => $CFG->wwwroot 134 ); 135 $resp = $curl->post($serverurl, $params); 136 137 if ($key = json_decode($resp, true)) { 138 if (!empty($key['accesskey'])) { 139 return $key['accesskey']; 140 } 141 } 142 debugging("Unexpected response from the Airnotifier server: $resp"); 143 return false; 144 } 145 146 /** 147 * Create a device token in the Airnotifier instance 148 * @param string $token The token to be created 149 * @return bool True if all was right 150 */ 151 private function create_token($token) { 152 global $CFG; 153 154 if (!$this->is_system_configured()) { 155 return false; 156 } 157 158 require_once($CFG->libdir . '/filelib.php'); 159 160 $serverurl = $CFG->airnotifierurl . ':' . $CFG->airnotifierport . '/tokens/' . $token; 161 $header = array('Accept: application/json', 'X-AN-APP-NAME: ' . $CFG->airnotifierappname, 162 'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey); 163 $curl = new curl; 164 $curl->setHeader($header); 165 $params = array(); 166 $resp = $curl->post($serverurl, $params); 167 168 if ($token = json_decode($resp, true)) { 169 if (!empty($token['status'])) { 170 return $token['status'] == 'ok' || $token['status'] == 'token exists'; 171 } 172 } 173 debugging("Unexpected response from the Airnotifier server: $resp"); 174 return false; 175 } 176 177 /** 178 * Tests whether the airnotifier settings have been configured 179 * @return boolean true if airnotifier is configured 180 */ 181 public function is_system_configured() { 182 global $CFG; 183 184 return (!empty($CFG->airnotifierurl) && !empty($CFG->airnotifierport) && 185 !empty($CFG->airnotifieraccesskey) && !empty($CFG->airnotifierappname) && 186 !empty($CFG->airnotifiermobileappname)); 187 } 188 189 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |