/** * Uses two-legged OAuth to respond to a Google documents list API request * @param string $base_feed Full URL of the resource to access * @param array $params (optional) parameters to be added to url line * @param string $type The HTTP method (GET, POST, PUT, DELETE) * @param string $postData (optional) POST/PUT request body * @param string $version (optional) if not sent will be set to 3.0 * @param string $content_type (optional) what kind of content is being sent * @param string $slug (optional) used in determining the revision of a document * @param boolean $batch is this a batch transmission? * @return string $response body from the server */ function twolegged($base_feed, $params, $type, $postdata = null, $version = null, $content_type = null, $slug = null, $batch = null) { global $CFG; require_once $CFG->dirroot . '/repository/morsle/lib.php'; // for morsle_decode require_once $CFG->dirroot . '/google/oauth.php'; // Establish an OAuth consumer based on our admin 'credentials' if (!($CONSUMER_KEY = get_config('morsle', 'consumer_key'))) { return NULL; } if (!($CONSUMER_SECRET = get_config('morsle', 'oauthsecretstr'))) { return NULL; } $CONSUMER_SECRET = morsle_decode($CONSUMER_SECRET); $consumer = new OAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET, NULL); // Create an Atom entry $contactAtom = new DOMDocument(); // $contactAtom = null; $request = OAuthRequest::from_consumer_and_token($consumer, NULL, $type, $base_feed, $params); // Sign the constructed OAuth request using HMAC-SHA1 $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL); // scope=https://docs.google.com/feeds/%20http://spreadsheets.google.com/feeds/%20https://docs.googleusercontent.com/ // Make signed OAuth request to the Contacts API server if (!is_null($params)) { $url = $base_feed . '?' . implode_assoc('=', '&', $params); } else { $url = $base_feed; } $header_request = $request->to_header(); $response = send_request($request->get_normalized_http_method(), $url, $header_request, $contactAtom, $postdata, $version, $content_type, $slug, $batch); return $response; }
function calmassdelete() { global $CFG, $DB, $success; // require_once('../../../config.php'); require_once $CFG->dirroot . '/google/lib.php'; require_once $CFG->dirroot . '/google/gauth.php'; //$chewon = $DB->get_records('morsle_active'); // get course record from which events are to be deleted $coursesql = 'SELECT ma.* FROM mdl_morsle_active ma JOIN mdl_course c on c.id = ma.courseid WHERE c.id = 692'; $courseid = $DB->get_record_sql($coursesql); // authenticate $service = 'cl'; $owner = $courseid->shortname . '@' . $this->domain; $calowner = str_replace('@', '%40', $owner); $password = morsle_decode($courseid->password); $this->authstring = "Authorization: GoogleLogin auth=" . clientauth($owner, $password, $service); // $password = rc4decrypt($courseid->password); // set up get of feed $base_feed = $this->cal_feed; $counter = 0; while ($counter < 100) { $response = send_request('GET', $base_feed, $this->authstring, null, null, '2.0'); if ($success) { $feed = simplexml_load_string($response->response); if (!isset($feed->entry)) { $counter = 101; } else { $counter++; foreach ($feed->entry as $entry) { $event->googleid = substr($entry->id, strpos($entry->id, 'events/') + 7, 50); $delete_feed = "https://www.google.com/calendar/feeds/default/private/full/{$event->googleid}"; $response = send_request('DELETE', $delete_feed, $this->authstring, null, null, '2.0'); if ($success) { echo $entry->title . ' DELETED <br />'; } } } } } }
/** * Constructor, allows subauth requests using the response from an initial * AuthSubRequest or with the subauth long-term token. Note that constructing * this object without a valid token will cause an exception to be thrown. * * @param string $sessiontoken A long-term subauth session token * @param string $authtoken A one-time auth token wich is used to upgrade to session token * @param mixed @options Options to pass to the base curl object */ public function __construct($sessiontoken = '', $authtoken = '', $options = array()) { // $returnurl = new moodle_url('/repository/repository_callback.php', // array('callback' => 'yes', 'repo_id' =>$options['repo_id'])); // unset($options['repo_id']); // Establish an OAuth consumer based on our admin 'credentials' if (!($CONSUMER_KEY = get_config('morsle', 'consumer_key'))) { throw new moodle_exception('Consumer key not set up'); } if (!($CONSUMER_SECRET = morsle_decode(get_config('morsle', 'oauthsecretstr')))) { throw new moodle_exception('Consumer secret not set up'); } // parent::__construct($CONSUMER_KEY, $CONSUMER_SECRET, $returnurl->out(false), morsle_docs::REALM); // set up variables and parameters $type = 'GET'; $url = get_morsle_url($options); // $url = str_replace('default',urlencode($options['xoauth_requestor_id']), $url); // if (array_key_exists('path', $options)) { // unset($options['path']); // } foreach ($options as $key => $param) { if ($key === 'q') { $param = urlencode($param); } $params[$key] = $param; } $consumer = new OAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET, NULL); // Create an Atom entry // $contactAtom = new DOMDocument(); // $contactAtom = null; $request = OAuthRequest::from_consumer_and_token($consumer, NULL, $type, $url, $params); // Sign the constructed OAuth request using HMAC-SHA1 $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL); if (!is_null($params)) { $url = $url . '?' . implode_assoc('=', '&', $params); // } else { // $url = $url; } $header_request = $request->to_header(); $this->setHeader($header_request); return $header_request; // at what point do we actually make the get request from Google -- work backward from there and integrate send_rquest // $response = send_request($request->get_normalized_http_method(), $url, $header_request, $contactAtom, $postdata, $version, $content_type, $slug, $batch); // return $response; }