Permalink
Browse files

Added basic suppoert to reward comments

  • Loading branch information...
1 parent 80c99db commit e6a54b9f08e658e8b12d83267130c6b66683d2c8 @pricco pricco committed Apr 4, 2012
Showing with 326 additions and 0 deletions.
  1. +6 −0 incrwd.info
  2. +115 −0 incrwd.module
  3. +52 −0 incrwd_api.inc
  4. +153 −0 incrwd_utils.inc
View
@@ -0,0 +1,6 @@
+name = Incrwd
+description = Add the incrwd snippet for the loyalty program and/or the achievement program
+core = 7.x
+package = Incrwd
+configure = admin/config/system/incrwd
+version = 1.0
View
@@ -0,0 +1,115 @@
+<?php
+/**
+ * Implements hook_help.
+ *
+ * Displays help and module information.
+ *
+ * @param path
+ * Which path of the site we're using to display help
+ * @param arg
+ * Array that holds the current path as returned from arg() function
+ */
+module_load_include('inc', 'incrwd', 'incrwd_utils');
+module_load_include('inc', 'incrwd', 'incrwd_api');
+
+define('INCRWD_DEBUG', True);
+
+function incrwd_help($path, $arg) {
+ switch ($path) {
+ case "admin/help#incrwd":
+ return '<p>'. t("Add the incrwd snippets and reward/badge enables the comments") .'</p>';
+ break;
+ }
+}
+
+function incrwd_page_alter(&$page) {
+ $mode = 'parent';
+ if (module_exists('overlay')) {
+ $mode = overlay_get_mode();
+ }
+
+ if ($mode != 'child') {
+ if (variable_get('incrwd_enable_rewards')) {
+ $script = 'var _icq = _icq || [];';
+ $script .= 'var incrwd_config = {';
+ $script .= ' site_id: ' . variable_get('incrwd_id', 0) . ',';
+ $script .= ' remote_auth: "' . incrwd_sso() . '"';
+ $script .= '};';
+ if (!INCRWD_DEBUG) {
+ $script .= '(function() {';
+ $script .= ' var ic = document.createElement("script"); ic.type = "text/javascript"; ic.async = true;';
+ $script .= " ic.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'static.widget.myincrwd.com/incrwd.js';";
+ $script .= ' var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ic, s);';
+ $script .= '})();';
+ }
+ drupal_add_js($script, array('scope' => 'footer', 'type' => 'inline'));
+ if (INCRWD_DEBUG) {
+ drupal_add_js('http://incrwd.example.com:8000/w/bootloader.js', array('scope' => 'footer'));
+ }
+ }
+ }
+}
+
+function incrwd_menu() {
+ $items = array();
+
+ $items['admin/config/system/incrwd'] = array(
+ 'title' => 'incrwd',
+ 'description' => 'Configuration of incrwd rewards/achievement settings',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('incrwd_form'),
+ 'access arguments' => array('access administration incrwd'),
+ 'type' => MENU_NORMAL_ITEM,
+ );
+
+ return $items;
+}
+
+function incrwd_form($form, &$form_state) {
+ $form['incrwd_id'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Your Id'),
+ '#default_value' => variable_get('incrwd_id', ''),
+ '#size' => 127,
+ '#maxlength' => 127,
+ '#description' => t('The id you got from http://www.incrwd.com.'),
+ '#required' => TRUE,
+ );
+
+ $form['incrwd_key'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Your Secret Key'),
+ '#default_value' => variable_get('incrwd_key', ''),
+ '#size' => 127,
+ '#maxlength' => 127,
+ '#description' => t('The key you got from http://www.incrwd.com.'),
+ '#required' => TRUE,
+ );
+
+ $form['incrwd_enable_rewards'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Enable rewards'),
+ '#default_value' => variable_get('incrwd_enable_rewards', true),
+ '#description' => t('Enables the loyalty program.'),
+ );
+
+ return system_settings_form($form);
+}
+
+function incrwd_get_api() {
+ return new IncrwdApi(
+ variable_get('incrwd_id', ''),
+ variable_get('incrwd_key', ''),
+ INCRWD_DEBUG ? 'http://incrwd.example.com/w/api/' : 'http://widget.myincrwd.com/w/api/');
+}
+
+function incrwd_comment_insert($comment) {
+ $api = incrwd_get_api();
+ $api->approved_comment_left($comment->cid);
+ //$api->insert_comment_left($comment->cid);
+}
+
+function incrwd_comment_publish($comment) {
+ //$api = incrwd_get_api();
+ //$api->approved_comment_left($comment->cid);
+}
View
@@ -0,0 +1,52 @@
+<?php
+
+module_load_include('inc', 'incrwd', 'incrwd_utils');
+
+class IncrwdApi {
+
+ // This always gets incremented by 1.
+ var $api_version = '2';
+
+ function IncrwdApi($site_id, $secret_key, $api_url) {
+ $this->site_id = $site_id;
+ $this->secret_key = $secret_key;
+ $this->api_url = $api_url;
+ }
+
+ function approved_comment_left($comment_id) {
+ $this->call("approved_comment",
+ array("comment_id" => $comment_id,
+ "remote_cookie" => $_COOKIE["__ic"]));
+ }
+
+ /**
+ * Makes a call to an Incrwd API method.
+ *
+ * @param $method
+ * The Incrwd API method to call.
+ * @param $args
+ * An associative array of arguments to be passed.
+ */
+ function call($method, $args=array()) {
+ if (!($this->site_id)) {
+ return -1;
+ }
+ $url = $this->api_url . $method . '/';
+ $args['api_version'] = $this->api_version;
+ $real_args = array(
+ "site_id" => $this->site_id,
+ "args" => incrwd_encode_arr($args));
+ $response = drupal_http_request(
+ $url,
+ array(
+ 'method' => 'POST',
+ 'data' => http_build_query($real_args)));
+ if ($response->code != 200) {
+ return -1;
+ } else {
+ return $response->data;
+ }
+ }
+}
+
+?>
View
@@ -0,0 +1,153 @@
+<?php
+
+function incrwd_encode_arr($arr) {
+ if (count($arr) == 0) {
+ $json = "{}";
+ } else {
+ $json = cf_json_encode($arr);
+ }
+ $data = base64_encode($json);
+ $key = variable_get('incrwd_key', '');
+ $time = time();
+ $hmac = incrwd_hmacsha1($data . ' ' . $time, $key);
+ $payload = $data . ' ' . $hmac . ' '. $time;
+ return $payload;
+}
+
+// Single sign-on integration
+function incrwd_sso() {
+ global $user;
+ /*if ($user->uid) {
+ //$avatar = theme('user_picture', array('account' => user_load($user->uid)));
+ $user_data = array(
+ 'username' => $user->name,
+ 'id' => intval($user->uid),
+ //'avatar' => $avatar,
+ 'email' => $user->mail,
+ );
+ }
+ else {*/
+ $user_data = array();
+ //}
+ return incrwd_encode_arr($user_data);
+}
+
+
+/**
+ * JSON ENCODE for PHP < 5.2.0
+ * Checks if json_encode is not available and defines json_encode
+ * to use php_json_encode in its stead
+ * Works on iteratable objects as well - stdClass is iteratable, so all WP objects are gonna be iteratable
+ */
+if (!function_exists('cf_json_encode')) {
+
+ function cf_json_encode($data) {
+// json_encode is sending an application/x-javascript header on Joyent servers
+// for some unknown reason.
+// if(function_exists('json_encode')) { return json_encode($data); }
+// else { return cfjson_encode($data); }
+ return cfjson_encode($data);
+ }
+
+ function cfjson_encode_string($str) {
+ if(is_bool($str)) {
+ return $str ? 'true' : 'false';
+ }
+
+ return str_replace(
+ array(
+ '"'
+ , '/'
+ , "\n"
+ , "\r"
+ )
+ , array(
+ '\"'
+ , '\/'
+ , '\n'
+ , '\r'
+ )
+ , $str
+ );
+ }
+
+ function cfjson_encode($arr) {
+ $json_str = '';
+ if (is_array($arr)) {
+ $pure_array = true;
+ $array_length = count($arr);
+ for ( $i = 0; $i < $array_length ; $i++) {
+ if (!isset($arr[$i])) {
+ $pure_array = false;
+ break;
+ }
+ }
+ if ($pure_array) {
+ $json_str = '[';
+ $temp = array();
+ for ($i=0; $i < $array_length; $i++) {
+ $temp[] = sprintf("%s", cfjson_encode($arr[$i]));
+ }
+ $json_str .= implode(',', $temp);
+ $json_str .="]";
+ }
+ else {
+ $json_str = '{';
+ $temp = array();
+ foreach ($arr as $key => $value) {
+ $temp[] = sprintf("\"%s\":%s", $key, cfjson_encode($value));
+ }
+ $json_str .= implode(',', $temp);
+ $json_str .= '}';
+ }
+ }
+ else if (is_object($arr)) {
+ $json_str = '{';
+ $temp = array();
+ foreach ($arr as $k => $v) {
+ $temp[] = '"'.$k.'":'.cfjson_encode($v);
+ }
+ $json_str .= implode(',', $temp);
+ $json_str .= '}';
+ }
+ else if (is_string($arr)) {
+ $json_str = '"'. cfjson_encode_string($arr) . '"';
+ }
+ else if (is_numeric($arr)) {
+ $json_str = $arr;
+ }
+ else if (is_bool($arr)) {
+ $json_str = $arr ? 'true' : 'false';
+ }
+ else {
+ $json_str = '"'. cfjson_encode_string($arr) . '"';
+ }
+ return $json_str;
+ }
+}
+
+// from: http://www.php.net/manual/en/function.sha1.php#39492
+// Calculate HMAC-SHA1 according to RFC2104
+// http://www.ietf.org/rfc/rfc2104.txt
+function incrwd_hmacsha1($data, $key) {
+ $blocksize=64;
+ $hashfunc='sha1';
+ if (strlen($key)>$blocksize)
+ $key=pack('H*', $hashfunc($key));
+ $key=str_pad($key,$blocksize,chr(0x00));
+ $ipad=str_repeat(chr(0x36),$blocksize);
+ $opad=str_repeat(chr(0x5c),$blocksize);
+ $hmac = pack(
+ 'H*',$hashfunc(
+ ($key^$opad).pack(
+ 'H*',$hashfunc(
+ ($key^$ipad).$data
+ )
+ )
+ )
+ );
+ return bin2hex($hmac);
+}
+
+
+?>

0 comments on commit e6a54b9

Please sign in to comment.