bookmarksHelper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // Source: http://www.php.net/manual/de/function.curl-setopt.php#102121
  3. // This works around a safe_mode/open_basedir restriction
  4. function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
  5. $mr = $maxredirect === null ? 5 : intval($maxredirect);
  6. if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
  7. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
  8. curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
  9. } else {
  10. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  11. if ($mr > 0) {
  12. $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  13. $rch = curl_copy_handle($ch);
  14. curl_setopt($rch, CURLOPT_HEADER, true);
  15. curl_setopt($rch, CURLOPT_NOBODY, true);
  16. curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
  17. curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
  18. do {
  19. curl_setopt($rch, CURLOPT_URL, $newurl);
  20. $header = curl_exec($rch);
  21. if (curl_errno($rch)) {
  22. $code = 0;
  23. } else {
  24. $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
  25. if ($code == 301 || $code == 302) {
  26. preg_match('/Location:(.*?)\n/', $header, $matches);
  27. $newurl = trim(array_pop($matches));
  28. } else {
  29. $code = 0;
  30. }
  31. }
  32. } while ($code && --$mr);
  33. curl_close($rch);
  34. if (!$mr) {
  35. if ($maxredirect === null) {
  36. trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
  37. } else {
  38. $maxredirect = 0;
  39. }
  40. return false;
  41. }
  42. curl_setopt($ch, CURLOPT_URL, $newurl);
  43. }
  44. }
  45. return curl_exec($ch);
  46. }
  47. function getURLMetadata($url) {
  48. //allow only http(s) and (s)ftp
  49. $protocols = '/^[hs]{0,1}[tf]{0,1}tp[s]{0,1}\:\/\//i';
  50. //if not (allowed) protocol is given, assume http
  51. if(preg_match($protocols, $url) == 0) {
  52. $url = 'http://' . $url;
  53. }
  54. $metadata['url'] = $url;
  55. if (!function_exists('curl_init')){
  56. return $metadata;
  57. }
  58. $ch = curl_init();
  59. curl_setopt($ch, CURLOPT_URL, $url);
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  61. $page = curl_exec_follow($ch);
  62. curl_close($ch);
  63. @preg_match( "/<title>(.*)<\/title>/si", $page, $match );
  64. $metadata['title'] = htmlspecialchars_decode(@$match[1]);
  65. return $metadata;
  66. }
  67. function addBookmark($url, $title, $tags='') {
  68. $CONFIG_DBTYPE = OCP\Config::getSystemValue( "dbtype", "sqlite" );
  69. if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
  70. $_ut = "strftime('%s','now')";
  71. } elseif($CONFIG_DBTYPE == 'pgsql') {
  72. $_ut = 'date_part(\'epoch\',now())::integer';
  73. } else {
  74. $_ut = "UNIX_TIMESTAMP()";
  75. }
  76. //FIXME: Detect when user adds a known URL
  77. $query = OCP\DB::prepare("
  78. INSERT INTO *PREFIX*bookmarks
  79. (url, title, user_id, public, added, lastmodified)
  80. VALUES (?, ?, ?, 0, $_ut, $_ut)
  81. ");
  82. if(empty($title)) {
  83. $metadata = getURLMetadata($url);
  84. if(isset($metadata['title'])) // Check for problems fetching the title
  85. $title = $metadata['title'];
  86. }
  87. if(empty($title)) {
  88. $l = OC_L10N::get('bookmarks');
  89. $title = $l->t('unnamed');
  90. }
  91. $params=array(
  92. htmlspecialchars_decode($url),
  93. htmlspecialchars_decode($title),
  94. OCP\USER::getUser()
  95. );
  96. $query->execute($params);
  97. $b_id = OCP\DB::insertid('*PREFIX*bookmarks');
  98. if($b_id !== false) {
  99. $query = OCP\DB::prepare("
  100. INSERT INTO *PREFIX*bookmarks_tags
  101. (bookmark_id, tag)
  102. VALUES (?, ?)
  103. ");
  104. $tags = explode(' ', urldecode($tags));
  105. foreach ($tags as $tag) {
  106. if(empty($tag)) {
  107. //avoid saving blankspaces
  108. continue;
  109. }
  110. $params = array($b_id, trim($tag));
  111. $query->execute($params);
  112. }
  113. return $b_id;
  114. }
  115. }