Advertisement
pthurmond

iPaper (Scribd) Drupal module modifications

Sep 27th, 2011
2,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.16 KB | None | 0 0
  1. <?php
  2. // $Id: ipaper.module,v 1.1.2.43 2009/10/09 14:44:06 rares Exp $
  3. // $Name: DRUPAL-6--1-1 $
  4. /*
  5.  * @file
  6.  * iPaper module hooks and functions
  7.  * Extends iPaper content type, calls funtions in scribd.php to implement Scribd API
  8.  */
  9.  
  10. //definition of constants and constant arrays
  11.  
  12. function ipaper_private_options() {
  13.   return array('private' => t('Only this website'), 'public' => t('World - www.scribd.com'));
  14. }
  15. define ('DEFAULT_PRIVATE', 'private');
  16.  
  17. function ipaper_license_options() {
  18.   return array(
  19.     'ns' => t('Unspecified - no licensing information shown'),
  20.     'by' => t('By attribution'),
  21.     'by-nc' => t('By attribution, non-commercial'),
  22.     'by-nc-nd' => t('By attribution, non-commercial, non-derivative'),
  23.     'by-nc-sa' => t('By attribution, non-commercial, share alike'),
  24.     'by-nd' => t('By attribution, non-derivative'),
  25.     'by-sa' => t('By attribution, share alike'),
  26.     'pd' => t('Public domain'),
  27.     'c' => t('Copyright - all rights reserved'),
  28.   );
  29. }
  30. define ('DEFAULT_LICENSE', 'by-nc');
  31.  
  32. function ipaper_secure_options() {
  33.   return array(0 => t('No - Allow embedding'), 1 => t('Yes - Prevent embedding'));
  34. }
  35. define ('DEFAULT_SECURE', 0);
  36.  
  37. define ('PLATFORM_CURL', 'CURL');
  38. define ('PLATFORM_FOPEN', 'FOPEN');
  39. define ('PLATFORM_EITHER', 'either');
  40.  
  41.  
  42. /**
  43.  * Implementation of hook_help().
  44.  */
  45. function ipaper_help($path, $arg) {
  46.   switch ($path) {
  47.     case 'admin/modules#description':
  48.       return t('Allows you to embed iPaper objects hosted at scribd.com.');
  49.  
  50.     case 'node/add#ipaper':
  51.       return t('Add a paper.');
  52.  
  53.   }
  54. }
  55.  
  56. /**
  57.  * Implementation of hook_perm().
  58.  */
  59. function ipaper_perm() {
  60.   return array("create ipaper", "view ipapers", "edit own ipapers", "edit ipapers", "delete own ipapers", "delete ipapers", "embed ipapers", "embed own ipapers", "download ipapers", "edit ipaper parameters");
  61. }
  62.  
  63. /**
  64.  * Implementation of hook_node_info().
  65.  */
  66. function ipaper_node_info() {
  67.   return array('ipaper' => array(
  68.     'name' => 'iPaper',
  69.     'description' => t('An iPaper is a document hosted on the scribd website.'),
  70.     'module' => 'ipaper'));
  71. }
  72.  
  73. /**
  74.  * Implementation of hook_access().
  75.  */
  76. function ipaper_access($op, $node, $account) {
  77.  
  78.   switch ($op) {
  79.     case 'create':
  80.       return user_access('create ipaper');
  81.  
  82.     case 'view':
  83.       if ($node->uid == $account->uid && $account->uid != 0)
  84.         return TRUE;
  85.       if ($node->status == 0)
  86.         return user_access('administer nodes');
  87.       return user_access('view ipapers');
  88.  
  89.     case 'update':
  90.       if ($account->uid == $node->uid && user_access("edit own ipapers")) {
  91.         return TRUE;
  92.       }
  93.       else {
  94.         return user_access("edit ipapers");
  95.       }
  96.  
  97.     case 'delete':
  98.       if ($account->uid == $node->uid && user_access("delete own ipapers")) {
  99.         return TRUE;
  100.       }
  101.       else {
  102.         return user_access("delete ipapers");
  103.       }
  104.  
  105.     default:
  106.       return FALSE;
  107.   }
  108. }
  109.  
  110. /**
  111.  * Implementation of hook_form().
  112.  * This function is called to retrieve the form that is displayed when one attempts
  113.  * to create/edit an ipaper
  114.  */
  115. function ipaper_form(&$node) {
  116.  
  117.   $type = node_get_types('type', 'ipaper');
  118.  
  119.   $form['title'] = array(
  120.     '#type' => 'textfield',
  121.     '#title' => $type->title_label,
  122.     '#default_value' => $node->title,
  123.     '#required' => TRUE,
  124.     '#weight' => -10,
  125.   );
  126.  
  127.     $form['body'] = array(
  128.       '#type' => $type->has_body ? 'textarea': 'hidden',
  129.       '#title' => $type->body_label,
  130.       '#default_value' => $node->body,
  131.       '#description' => t('This text is displayed with your document.'),
  132.       '#weight' => -9,
  133.     );  
  134.  
  135.     $form['private'] = array(
  136.       '#type' => variable_get('ipaper_choose_private', 1) ? 'select' : 'hidden',
  137.       '#title' => t('Visibility'),
  138.       '#options' => ipaper_private_options(),
  139.       '#disabled' => ($node->secure) ? TRUE : FALSE,
  140.       '#default_value' => ($node->private) ? $node->private : variable_get('ipaper_default_private', DEFAULT_PRIVATE),
  141.       '#description' => t('Public documents are also available on scribd.com.'),
  142.       '#weight' => -8,
  143.     );
  144.  
  145.     $form['license'] = array(
  146.       '#type' => variable_get('ipaper_choose_license', 1) ? 'select' : 'hidden',
  147.       '#title' => t('License'),
  148.       '#options' => ipaper_license_options(),
  149.       '#default_value' => ($node->license) ? $node->license : variable_get('ipaper_default_license', DEFAULT_LICENSE),
  150.       '#description' => t('The license under which the document is published. For more information about licenses, see <a href="http://creativecommons.org/about/licenses/meet-the-licenses" target="_blank">http://creativecommons.org/</a>'),
  151.       '#weight' => -6,
  152.     );
  153.  
  154.   //this cannot be changed once the document has been created
  155.     $form['secure'] = array(
  156.       '#type' => variable_get('ipaper_choose_secure', 1) ? 'select' : 'hidden',
  157.       '#title' => t('Secure iPaper'),
  158.       '#options' => ipaper_secure_options(),
  159.       '#disabled' => ($node->doc_id==NULL) ? FALSE : TRUE,
  160.       '#default_value' => ($node->secure!=NULL) ? $node->secure : variable_get('ipaper_default_secure', DEFAULT_SECURE),
  161.       '#description' => t('This option provides an additional layer of security for your upload, as it prevents users from embedding it into other websites. Note that this setting cannot be changed once the iPaper has been created.'),
  162.       '#weight' => -7,
  163.     );
  164.  
  165. //these parameters are to be shown and edited only by administrators unless you want your users to create ipapers by entering these parameters
  166.  
  167. if (user_access("edit ipaper parameters")) {
  168.  
  169.   $form['doc_id'] = array(
  170.     '#type' => 'textfield',
  171.     '#title' => t('doc_id'),
  172.     '#default_value' => $node->doc_id,
  173.     '#description' => t('Scribd document ID. Required unless you are attaching a file.'),
  174.     '#required' => FALSE,
  175.     '#weight' => -5,
  176.   );
  177.  
  178.   $form['secret_password'] = array(
  179.     '#type' => 'textfield',
  180.     '#title' => t('secret_password'),
  181.     '#default_value' => $node->secret_password,
  182.     '#description' => t('Scribd secret password'),
  183.     '#required' => FALSE,
  184.     '#weight' => -4,
  185.   );
  186.  
  187.   $form['access_key'] = array(
  188.     '#type' => 'textfield',
  189.     '#title' => t('access_key'),
  190.     '#default_value' => $node->access_key,
  191.     '#description' => t('Scribd document access key. Required unless you are attaching a file.'),
  192.     '#required' => FALSE,
  193.     '#weight' => -3,
  194.   );
  195.  
  196.   $form['fid'] = array(
  197.     '#type' => 'hidden',
  198.     //change type to textfield if you want to be able to edit this
  199.     '#title' => t('fid'),
  200.     '#default_value' => $node->fid,
  201.     '#description' => t('Drupal upload file ID. This is updated automatically by the module to track which file was last uploaded to scribd.'),
  202.     '#required' => FALSE,
  203.     '#weight' => -3,
  204.   );
  205.  
  206. } //if (user_access...
  207.  
  208.   return $form;
  209. }
  210.  
  211. /**
  212.  * Implementation of hook_validate().
  213.  */
  214. function ipaper_validate($node) {
  215.  
  216.   //check to make sure that there are attachments
  217.   $files = $node->files;
  218.   if (!$files && !($node->doc_id && $node->access_key)) {
  219.     form_set_error('files[upload]', t('You must upload an attachment to create an iPaper.'));
  220.     return;
  221.   }
  222.  
  223.   //also show a message if there is more than one attachment
  224.   //if (count($files)>1) {
  225.   //  drupal_set_message(t('iPapers are created from a single document. The iPaper that is displayed will always be generated from your most recent upload.'), 'notice');
  226.   //}
  227.  
  228. }
  229.  
  230. /**
  231.  * Database hooks when loading, inserting, updating or deleting an ipaper
  232.  */
  233.  
  234. function ipaper_load($node) {
  235.   $paper = db_fetch_object(db_query('SELECT * FROM {ipaper} WHERE nid = %d', $node->nid));
  236.   return $paper;
  237. }
  238.  
  239. function ipaper_insert($node) {
  240.   db_query("INSERT INTO {ipaper} (nid, fid, doc_id, secret_password, access_key, private, license, secure) VALUES (%d, %d, %d, '%s', '%s', '%s', '%s', %d)", $node->nid, $node->fid, $node->doc_id, $node->secret_password, $node->access_key, $node->private, $node->license, $node->secure);
  241.   $_REQUEST['destination'] = "ipaper_transfer/$node->nid";
  242.   //using drupal_goto would redirect before the node is saved
  243. }
  244.  
  245. function ipaper_update($node) {
  246.   ipaper_dbupdate($node);
  247.   $_REQUEST['destination'] = "ipaper_transfer/$node->nid";
  248. }
  249.  
  250. function ipaper_dbupdate($node) {
  251.   //used when a node is saved/submitted in Drupal as well as when the scribd.com API returns keys.
  252.   if (user_access('edit ipaper parameters')) {
  253.     _ipaper_dbupdate_params($node);
  254.   }
  255.   //$node->secure is included in case this property will become changeable in the future
  256.   return db_query("UPDATE {ipaper} SET private = '%s', license = '%s', secure = %d WHERE nid = %d", $node->private, $node->license, $node->secure, $node->nid);
  257. }
  258.  
  259. function _ipaper_dbupdate_params($node) {
  260.   //used when when the scribd.com API returns keys.
  261.   return db_query("UPDATE {ipaper} SET fid = %d, doc_id = %d, secret_password = '%s', access_key = '%s' WHERE nid = %d", $node->fid, $node->doc_id, $node->secret_password, $node->access_key, $node->nid);
  262. }
  263.  
  264. function _ipaper_dbupdate_full_text($node) {
  265.   //save full text for searching.
  266.   return db_query("UPDATE {ipaper} SET full_text = '%s' WHERE nid = %d", $node->full_text, $node->nid);
  267. }
  268.  
  269. function ipaper_delete($node) {
  270.   db_query('DELETE FROM {ipaper} WHERE nid = %d', $node->nid);
  271.   if ($node->doc_id){
  272.     $scribd = ipaper_scribd_init();
  273.     $result = $scribd->delete($node->doc_id);
  274.     if (!$result) ipaper_report_error();
  275.   }
  276. }
  277.  
  278. /**
  279.  * Implementation of hook_menu().
  280.  */
  281. function ipaper_menu() {
  282.   $items = array();
  283.  
  284.     $items['admin/settings/ipaper'] = array(
  285.       'title' => t('IPaper module configuration'),
  286.       'description' => t('iPaper API keys and configuration.'),
  287.       'page callback' => 'drupal_get_form',
  288.       'page arguments' => array('ipaper_config_form'),
  289.       'access callback' => 'user_access',
  290.       'access arguments' => array('administer site configuration'),
  291.       'type' => MENU_NORMAL_ITEM,
  292.       'file' => 'ipaper.admin.inc',
  293.     );
  294.     $items['ipaper_transfer/%node'] = array(
  295.       'title' => t('Please wait...'),
  296.       'access callback' => 'node_access',
  297.       'access arguments' => array('create', 'ipaper'),
  298.       'page callback' => 'ipaper_transfer',
  299.       'page arguments' => array(1),
  300.       'type' => MENU_CALLBACK,
  301.       'file' => 'ipaper.upload.inc',
  302.     );
  303.     $items['ipaper_upload/%node'] = array(
  304.       'title' => t('Please wait...'),
  305.       'access callback' => 'node_access',
  306.       'access arguments' => array('create', 'ipaper'),
  307.       'page callback' => 'ipaper_upload',
  308.       'page arguments' => array(1),
  309.       'type' => MENU_CALLBACK,
  310.       'file' => 'ipaper.upload.inc',
  311.     );
  312.     $items['ipaper_download/%node'] = array(
  313.       'title' => t('Redirecting...'),
  314.       'access callback' => 'user_access',
  315.       'access arguments' => array('download ipapers'),
  316.       'page callback' => 'ipaper_download',
  317.       'page arguments' => array(1),
  318.       'type' => MENU_CALLBACK,
  319.       'file' => 'ipaper.upload.inc',
  320.     );
  321.  
  322.   return $items;
  323. }
  324.  
  325.  
  326. /*
  327.  * Used by all functions that require Scribd API calls to return the $scribd class from the scribd.php library
  328.  */
  329.  
  330. function ipaper_scribd_init() {
  331.  
  332.   require_once drupal_get_path('module', 'ipaper') .'/ipaper.upload.inc';
  333.   require_once drupal_get_path('module', 'ipaper') .'/scribd.php';
  334.  
  335.   static $scribd_obj;
  336.   if ($scribd_obj) return $scribd_obj;
  337.  
  338.   if (variable_get('scribd_api_key', '') == '') {
  339.     drupal_set_message(t('The iPaper module has not yet been configured. You need to set an API key obtained from scribd.com/platform/start before you can use the module'), 'error');
  340.   }
  341.  
  342.   $scribd_api_key = variable_get('scribd_api_key', '');
  343.   $scribd_secret = variable_get('scribd_secret_key', '');
  344.  
  345.   $scribd_obj = new Scribd($scribd_api_key, $scribd_secret);
  346.  
  347.   //uncomment this if you want to store documents under the virtual accounts for each user
  348.   //note that files created for virtual users are only accessible through the API
  349.   //global $user;
  350.   //$scribd_obj->my_user_id = $user->name; // The user ID of one of your users
  351.  
  352.   return $scribd_obj;
  353. }
  354.  
  355.  
  356. function ipaper_report_error() {
  357.   $scribd = ipaper_scribd_init();
  358.   drupal_set_message("Scribd error #". $scribd->geterror() .": ". $scribd->geterrormsg(), 'error');
  359.   watchdog('ipaper', "Scribd error #%number: %msg", array('%number' => $scribd->geterror(), '%msg' => $scribd->geterrormsg()), WATCHDOG_ERROR);
  360.  
  361. }
  362.  
  363. /**
  364.  * Implementation of hook_view().
  365.  */
  366. function ipaper_view($node, $teaser = FALSE, $page = FALSE) {
  367.  
  368.   global $user;
  369.   $node = node_prepare($node, $teaser);
  370.   drupal_add_css(drupal_get_path('module', 'ipaper') .'/ipaper.css');
  371.  
  372.   if ($teaser == FALSE) {
  373.  
  374.     if (!$node->doc_id) {
  375.       $files = $node->files;
  376.       if ($files) {
  377.         $stat = node_access('create', 'ipaper') ? l(t("The previous attempt to convert this document to an iPaper failed. Click here to retry"), "ipaper_transfer/$node->nid"): "";
  378.       }
  379.       else{                          
  380.         $stat = t("Please upload a document to view it as an iPaper.");
  381.       }
  382.       $node->content['body']['#value'] .= $stat;
  383.       return $node;
  384.     }
  385.    
  386.     $type = node_get_types('type', 'ipaper');
  387.  
  388.     $fs = array(
  389.       '#collapsible' => TRUE,
  390.       '#collapsed' => TRUE,
  391.       '#title' => $type->body_label,
  392.       '#value' => $node->content['body']['#value'],
  393.     );
  394.     if ($type->has_body && $node->content['body']['#value']!='') {
  395.       $node->content['body']['#value'] = theme('fieldset', $fs);
  396.     }
  397.  
  398.     $node->content['viewer'] = array(
  399.       '#value' => theme('ipaper_viewer', $node),
  400.       '#weight' => 2,
  401.     );
  402.  
  403.     if (user_access("download ipapers")) {
  404.       $node->content['download_link'] = array(
  405.         '#value' => theme('ipaper_download_link', $node),
  406.         '#weight' => 3,
  407.       );
  408.     }
  409.  
  410.     if (variable_get('ipaper_show_scribd_link', 0)) {
  411.       $node->content['scribd_link'] = array(
  412.         '#value' => theme('ipaper_scribd_link', $node),
  413.         '#weight' => 4,
  414.       );
  415.     }
  416.  
  417.     if (user_access("embed ipapers") || (user_access("embed own ipapers") && $node->uid == $user->uid)) {
  418.       $node->content['embed_code'] = array(
  419.         '#value' => theme('ipaper_embed_code', $node),
  420.         '#weight' => 5,
  421.       );
  422.     }
  423.       $node->content['license'] = array(
  424.         '#value' => theme('ipaper_license', $node),
  425.         '#weight' => 8,
  426.       );
  427.  
  428.   }//if teaser=false
  429.   else{
  430.  
  431.     $node->content['body']['#value'] = "<div class=ipaper-descr-teaser>" . $node->content['body']['#value'] ."</div>\n";
  432.  
  433.     $node->content['thumbnail'] = array(
  434.       '#value' => theme('ipaper_thumbnail', $node),
  435.       '#weight' => -2,
  436.     );
  437.   }
  438.  
  439.   return $node;
  440. }
  441.  
  442. /**
  443.  * Implementation of hook_theme().
  444.  */
  445.  
  446. function ipaper_theme() {
  447.   return array(
  448.     'ipaper_viewer' => array(
  449.       'arguments' => array('node'),
  450.     ),
  451.     'ipaper_thumbnail' => array(
  452.       'arguments' => array('node'),
  453.     ),
  454.     'ipaper_embed_code' => array(
  455.       'arguments' => array('node'),
  456.     ),
  457.     'ipaper_download_link' => array(
  458.       'arguments' => array('node'),
  459.     ),
  460.     'ipaper_scribd_link' => array(
  461.       'arguments' => array('node'),
  462.     ),
  463.     'ipaper_full_text' => array(
  464.       'arguments' => array('node'),
  465.     ),
  466.     'ipaper_license' => array(
  467.       'arguments' => array('node'),
  468.     ),
  469.   );
  470. }
  471.  
  472. //Detect if the user is using IE or not.
  473. function ipaper_is_ie() {
  474.   if (preg_match('|MSIE ([0-9].[0-9]{1,2})|', $_SERVER['HTTP_USER_AGENT']))
  475.     return true;
  476.  
  477.   return false;
  478. }
  479.  
  480. //If the user is in IE then lets use this alternative method to display
  481. function ipaper_ie_display($node, $is_secure = true) {
  482.   if ($node->files)
  483.     $file = end($node->files);
  484.  
  485.   //This is extra hacky but I noticed that all the powerpoint files have a PDF version so this is the fix for it.
  486.   if ($file->filemime == 'application/vnd.ms-powerpoint')
  487.   {
  488.     $file->filemime = 'application/pdf';
  489.     $filename = str_ireplace('.ppt', '.pdf', $file->filename);
  490.     $file->filepath = str_ireplace($file->filename, $filename, $file->filepath);
  491.     $file->filename = $filename;
  492.   }
  493.  
  494.   if ($is_secure)
  495.     return "<embed src='/{$file->filepath}#toolbar=0&scrollbar=1&navpanes=1' height='550' width='100%'>";
  496.  
  497.   return "<embed src='/{$file->filepath}#toolbar=1&scrollbar=1&navpanes=1' height='550' width='100%'>";
  498. }
  499.  
  500. function theme_ipaper_viewer($node) {
  501.   if (ipaper_is_ie())
  502.     $output = ipaper_ie_display($node);
  503.   else
  504.   {
  505.     $extraJS = variable_get('ipaper_extraJS', '');
  506.    
  507.     if ($node->secure)
  508.       $extraJS .= ipaper_generate_securesig($node);
  509.    
  510.     $output = "
  511.    <script type=text/javascript src='http://www.scribd.com/javascripts/view.js'></script>
  512.    <div id='embedded_flash_$node->doc_id'>
  513.      <a href='http://www.scribd.com'>Embedded Scribd iPaper - Requires Javascript and Flash Player</a><br />
  514.    ". theme('ipaper_full_text', $node) ."
  515.    </div>
  516.    <script type=text/javascript>
  517.      var scribd_doc = scribd.Document.getDoc($node->doc_id, '$node->access_key');
  518.      scribd_doc.addParam( 'jsapi_version', 1 );
  519.      $extraJS
  520.      scribd_doc.write('embedded_flash_$node->doc_id');
  521.    
  522.    </script>";
  523.   }
  524.  
  525.   return $output ."\n";
  526. }
  527.  
  528. function theme_ipaper_thumbnail($node) {
  529.  
  530.   if(!$node->doc_id) return;
  531.  
  532.   $size = variable_get('ipaper_thumbnail_size', 140);
  533.   if ($size == 0) return;
  534.  
  535.   $path = _ipaper_get_thumb_path($node->doc_id);
  536.  
  537.   //if the thumbnail doesn't yet exist, fetch it, but only if a while has passed after the node was last saved. this ensures that we don't get a word doc or PDF icon
  538.   if (!file_exists($path) && time() - $node->changed > 120) {
  539.     $scribd = ipaper_scribd_init();
  540.     $stat = $scribd->getConversionStatus($node->doc_id);
  541.     if ($stat == "DONE")
  542.       _ipaper_save_thumb($node->doc_id, $scribd);
  543.   }
  544.  
  545.   if (file_exists($path)) {
  546.     $hover = t("Thumbnail of !title", array('!title' => $node->title));
  547.     $href = file_create_url($path);
  548.   }
  549.   else{
  550.     $href = base_path() . drupal_get_path('module', 'ipaper') .'/empty.jpg';
  551.     $hover = t("Thumbnail not yet available");
  552.   }
  553.  
  554.   $output = '<img class="ipaper-thumbnail" src="'. $href .'" title="'. $hover .'" height="'. $size .'">';
  555.   if (node_access('view', $node)) {
  556.     $output = '<a href="'. url("node/$node->nid") .'" title="Read more...">'. $output ."</a>";
  557.   }
  558.   return $output ."\n";
  559. }
  560.  
  561. function theme_ipaper_embed_code($node) {
  562.   if ($node->secure)
  563.     return;
  564.  
  565.   if (ipaper_is_ie())
  566.     $output = ipaper_ie_display($node, false);
  567.   else
  568.   {
  569.     $output = '<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="doc_!doc_id" name="doc_!doc_id" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" align="middle" height="500" width="100%"> <param name="movie" value="http://documents.scribd.com/ScribdViewer.swf?document_id=!doc_id&access_key=!access_key&page=&version=1&auto_size=true&viewMode="> <param name="quality" value="high"> <param name="play" value="true"> <param name="loop" value="true"> <param name="scale" value="showall"> <param name="wmode" value="opaque"> <param name="devicefont" value="false"> <param name="bgcolor" value="#ffffff"> <param name="menu" value="true"> <param name="allowFullScreen" value="true"> <param name="allowScriptAccess" value="always"> <param name="salign" value=""> <embed src = "http://documents.scribd.com/ScribdViewer.swf?document_id=!doc_id&access_key=!access_key&page=&version=1&auto_size=true&viewMode=" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" play="true" loop="true" scale="showall" wmode="opaque" devicefont="false" bgcolor="#ffffff" name="doc_!doc_id_object" menu="true" allowfullscreen="true" allowscriptaccess="always" salign="" type="application/x-shockwave-flash" align="middle"  height="500" width="100%"> </embed> </object>';
  570.     $keys = array(
  571.       '!doc_id' => $node->doc_id,
  572.       '!access_key' => $node->access_key,
  573.     );
  574.     $output = t($output, $keys);
  575.     $output = check_plain($output);
  576.     $output = '
  577.    <form action="" name="embedForm" id="embedForm">
  578.      <label for="embed_code">Embed</label>
  579.      <input id="embed_code_'.$node->doc_id.'" name="embed_code" type="text" value='."'$output'".' onClick="javascript:document.embedForm.embed_code.focus();document.embedForm.embed_code.select();" readonly />
  580.    </form>
  581.    ';
  582.   }
  583.  
  584.   return $output;
  585. }
  586.  
  587. function theme_ipaper_download_link($node) {
  588.   $format = variable_get('ipaper_download_format', 'pdf');
  589.   switch ($format) {
  590.     case 'pdf': $text = t("Download document as PDF"); break;
  591.     case 'txt': $text = t("Download document (plain text)"); break;
  592.     case 'original': $text = t("Download document");
  593.   }
  594.  
  595.   $output = "<div class=\"ipaper-download-link ipaper-download-$format \">";
  596.   $output .= l($text, "ipaper_download/$node->nid");
  597.   $output .= '</div>';
  598.   return $output ."\n";
  599. }
  600.  
  601. function theme_ipaper_scribd_link($node) {
  602.   if ($node->secure) return;
  603.   $output = '<div class="ipaper-scribd-link">';
  604.  
  605.   $link = "http://www.scribd.com/doc/$node->doc_id";
  606.   if ($node->private == 'private') {
  607.     $link .= '?secret_password='. $node->secret_password;
  608.   }
  609.  
  610.   $output .= '<a href="'. $link .'" target="_blank">Click here to view this document on www.scribd.com</a>';  
  611.   $output .= '</div>';
  612.   return $output ."\n";
  613. }
  614.  
  615. function theme_ipaper_full_text($node) {
  616.  
  617.   if (variable_get('ipaper_show_full_text', 1) || (variable_get('cron_semaphore', FALSE) && function_exists('search_cron'))) {
  618.     if (!$node->full_text)
  619.       $node->full_text = ipaper_get_full_text($node);
  620.     return $node->full_text ."\n";
  621.   //check_plain is not necessary because the equivalent was already done when the full text was saved.
  622.   }
  623. }
  624.  
  625. function theme_ipaper_license($node) {
  626.   if ($node->license == 'ns' || $node->license == '' || $node->license == NULL)
  627.     return;
  628.   $output = '<div class="ipaper-license">';
  629.   switch ($node->license) {
  630.     case 'c':
  631.       $yr = date("Y", $node->created);
  632.       $output .= t("This document is &copy; !yr by !name - all rights reserved.", array('!yr'=>$yr, '!name'=>$node->name)); break;
  633.     case 'pd':
  634.       $output .= t('This document has been released into the public domain.'); break;
  635.     default:
  636.       $licenses = ipaper_license_options();
  637.       $title = $licenses[$node->license];
  638.       $img = "<img src=http://i.creativecommons.org/l/$node->license/3.0/88x31.png>";
  639.       $info = t("http://creativecommons.org/licenses/!lic/3.0/deed.en_US", array('!lic' => $node->license));
  640.       $output .= "<a href=$info target=blank>$img</a> <br />";
  641.       $output .= t("Published under a Creative Commons License"). " <a href=\"$info\" target=\"_blank\">$title</a> <br/>";
  642.   }
  643.  
  644.   $output .= '</div>';
  645.   return $output ."\n";
  646. }
  647.  
  648.  
  649. /**
  650.  * This is called for nodes without full text by theme_ipaper_full_text and by the cron job
  651.  */
  652.  
  653. function ipaper_get_full_text($node){
  654.  
  655.   require_once drupal_get_path('module', 'ipaper') .'/ipaper.upload.inc';
  656.  
  657.   $scribd = ipaper_scribd_init();
  658.   $stat = $scribd->getConversionStatus($node->doc_id);
  659.   if ($stat == "PROCESSING" || $stat == "DISPLAYABLE")
  660.     return;
  661.  
  662.   if ($stat == "DONE") {
  663.     //get full text for search index and display
  664.     $texturl = $scribd->getDownloadURL($node->doc_id, 'txt');
  665.     //for some reason, the URL comes back with leading and trailing spaces
  666.     $texturl = trim($texturl);
  667.     $full_text = _ipaper_request($texturl);
  668.     $full_text = htmlspecialchars($full_text);
  669.     // add <br /> instead of line breaks
  670.     $full_text = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $full_text);
  671.   }
  672.  
  673.   //if there's an error, or no text, it will still save something so that it doesn't end up here again
  674.   $node->full_text = $full_text ? $full_text : "<!-- no text -->";
  675.   _ipaper_dbupdate_full_text($node);
  676.  
  677.   if ($full_text){
  678.     if (function_exists("calais_nodeapi"))
  679.       calais_nodeapi($node, "insert");
  680.     return $full_text;
  681.   }
  682.  
  683. }
  684.  
  685. /**
  686.  * Implementation of hook_requirements().
  687.  */
  688. function ipaper_requirements($phase) {
  689.   $requirements = array();
  690.  
  691.   if ($phase == 'runtime') {
  692.     // Raise warning if API keys have not been entered.
  693.     if (variable_get('scribd_api_key', '') == '') {
  694.       $requirements['ipaper'] = array(
  695.         'title' => t('iPaper configuration'),
  696.         'description' => t('The iPaper module has not been configured. Please configure it from the <a href="@url">iPaper settings page</a>.', array('@url' => url('admin/settings/ipaper'))),
  697.         'severity' => REQUIREMENT_ERROR,
  698.         'value' => t('Not configured'),
  699.       );
  700.     }
  701.   }
  702.  
  703.   return $requirements;
  704. }
  705.  
  706. function _ipaper_get_thumb_path($doc_id) {
  707.   return file_directory_path() .'/ipaper_thumbs/'. $doc_id .'.jpg';
  708. }
  709.  
  710.  
  711. /**
  712.  * Generate grantAccess instruction to include in Javascript API for documents that are secure
  713.  * Called by theme_ipaper_viewer()
  714.  */
  715.  
  716. function ipaper_generate_securesig($node){
  717.   $APIkey = variable_get('scribd_api_key', '');
  718.   $secretkey = variable_get('scribd_secret_key', '');
  719.   global $user;
  720.   $grantuser = user_is_logged_in()? $user->name : 'guest';
  721.   $grantsid = session_id();
  722.   $securesig = "$secretkey"."document_id".$node->doc_id."session_id".$grantsid."user_identifier".$grantuser;
  723.   $securesig = md5($securesig);
  724.   $output = "
  725.    scribd_doc.addParam('use_ssl', true);
  726.    scribd_doc.grantAccess('$grantuser', '$grantsid', '$securesig');
  727.  ";
  728.   return $output;
  729.  
  730. }
  731.  
  732. /*
  733.  * Implementation of hook_nodeapi().
  734.  */
  735.  
  736. function ipaper_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  737.   switch ($op) {
  738.     case 'view':
  739.    
  740.       break;
  741.   }
  742. }
  743.  
  744. /*
  745.  * Implementation of hook_cron().
  746.  */
  747.  
  748. function ipaper_cron() {
  749.  
  750.   if (!function_exists('search_cron') || !variable_get('ipaper_show_full_text', 1))
  751.     return;
  752.  
  753.   //require_once drupal_get_path('module', 'ipaper') .'/ipaper.upload.inc';
  754.   $result = db_query("SELECT * FROM {ipaper} WHERE full_text is NULL");
  755.   while ($row = db_fetch_object($result)){
  756.     if ($row->doc_id){
  757.       $node = node_load($row->nid);
  758.       $scribd = ipaper_scribd_init();
  759.       //in case full text is saved, also change the update time so that the search module will index it
  760.       if (ipaper_get_full_text($node)){
  761.         $row = db_fetch_object($result);
  762.         db_query("UPDATE {search_dataset} SET reindex = %d WHERE type = 'node' AND sid = %d", time(), $row->nid);
  763.       }
  764.     }
  765.   }
  766.  
  767. }
  768.  
  769. /**
  770.  * Implementation of hook_filter().
  771.  */
  772. function ipaper_filter($op, $delta = 0, $format = -1, $text = "") {
  773.   switch ($op) {
  774.     case 'list':
  775.       return array(0 => t('iPaper embed filter'));
  776.     case 'description':
  777.       return t('Inserts iPaper embed code wherever tags of the following format are found: [scribd id={docid} key={access key}].');
  778.     case "process":
  779.       return ipaper_filter_process($text);
  780.     default:
  781.       return $text;
  782.   }
  783. }
  784.  
  785. function ipaper_filter_process($text) {
  786.  
  787.   $results = array();
  788.   $pattern = "/\[scribd id=([0-9]+) key=([-A-z0-9]+)\]/";
  789.   preg_match_all($pattern, $text, $results);
  790.   $output = $text;
  791.   foreach($results[0] as $rid=>$result){
  792.     $docobj->doc_id = $results[1][$rid];
  793.     $docobj->access_key = $results[2][$rid];
  794.     $JScode = theme("ipaper_viewer", $docobj);
  795.     $output = str_replace($results[0][$rid], $JScode, $output);
  796.   }
  797.   return $output;
  798. }
  799.  
  800.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement