[ Index ] |
PHP Cross Reference of moodle-2.8 |
[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 <[email protected]> 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 <[email protected]> 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 return false; 143 } 144 145 /** 146 * Create a device token in the Airnotifier instance 147 * @param string $token The token to be created 148 * @return bool True if all was right 149 */ 150 private function create_token($token) { 151 global $CFG; 152 153 if (!$this->is_system_configured()) { 154 return false; 155 } 156 157 require_once($CFG->libdir . '/filelib.php'); 158 159 $serverurl = $CFG->airnotifierurl . ':' . $CFG->airnotifierport . '/tokens/' . $token; 160 $header = array('Accept: application/json', 'X-AN-APP-NAME: ' . $CFG->airnotifierappname, 161 'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey); 162 $curl = new curl; 163 $curl->setHeader($header); 164 $params = array(); 165 $resp = $curl->post($serverurl, $params); 166 167 if ($resp = json_decode($resp, true)) { 168 if (!empty($resp['status'])) { 169 return $resp['status'] == 'ok' || $resp['status'] == 'token exists'; 170 } 171 } 172 return false; 173 } 174 175 /** 176 * Tests whether the airnotifier settings have been configured 177 * @return boolean true if airnotifier is configured 178 */ 179 public function is_system_configured() { 180 global $CFG; 181 182 return (!empty($CFG->airnotifierurl) && !empty($CFG->airnotifierport) && 183 !empty($CFG->airnotifieraccesskey) && !empty($CFG->airnotifierappname) && 184 !empty($CFG->airnotifiermobileappname)); 185 } 186 187 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |