link.lib.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Function library for the links tool.
  5. *
  6. * This is a complete remake of the original link tool.
  7. * New features:
  8. * - Organize links into categories;
  9. * - favorites/bookmarks interface;
  10. * - move links up/down within a category;
  11. * - move categories up/down;
  12. * - expand/collapse all categories;
  13. * - add link to 'root' category => category-less link is always visible.
  14. *
  15. * @author Patrick Cool, complete remake (December 2003 - January 2004)
  16. * @author René Haentjens, CSV file import (October 2004)
  17. * @package chamilo.link
  18. */
  19. /* FUNCTIONS */
  20. class Link extends Model {
  21. var $table;
  22. var $is_course_model = true;
  23. var $columns = array('id', 'c_id','url','title','description','category_id', 'display_order', 'on_homepage', 'target', 'session_id');
  24. var $required = array('url', 'title');
  25. public function __construct() {
  26. $this->table = Database::get_course_table(TABLE_LINK);
  27. }
  28. public function save($params) {
  29. $course_info = api_get_course_info();
  30. $params['session_id'] = api_get_session_id();
  31. $params['category_id'] = isset($params['category_id']) ? $params['category_id'] : 0;
  32. $id = parent::save($params);
  33. if (!empty($id)) {
  34. api_item_property_update($course_info, TOOL_LINK, $id, 'LinkAdded', api_get_user_id());
  35. }
  36. return $id;
  37. }
  38. }
  39. /**
  40. * Used to add a link or a category
  41. * @param string $type, "link" or "category"
  42. * @todo replace strings by constants
  43. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  44. */
  45. function addlinkcategory($type) {
  46. global $catlinkstatus;
  47. global $msgErr;
  48. $ok = true;
  49. $course_id = api_get_course_int_id();
  50. if ($type == 'link') {
  51. $tbl_link = Database :: get_course_table(TABLE_LINK);
  52. $title = Security :: remove_XSS(stripslashes($_POST['title']));
  53. $urllink = Security :: remove_XSS($_POST['urllink']);
  54. $description = Security :: remove_XSS($_POST['description']);
  55. $selectcategory = Security :: remove_XSS($_POST['selectcategory']);
  56. if ($_POST['onhomepage'] == '') {
  57. $onhomepage = 0;
  58. } else {
  59. $onhomepage = Security :: remove_XSS($_POST['onhomepage']);
  60. }
  61. if (empty($_POST['target_link'])) {
  62. $target = '_self'; // Default target.
  63. } else {
  64. $target = Security :: remove_XSS($_POST['target_link']);
  65. }
  66. $urllink = trim($urllink);
  67. $title = trim($title);
  68. $description = trim($description);
  69. // We ensure URL to be absolute.
  70. if (strpos($urllink, '://') === false) {
  71. $urllink = 'http://' . $urllink;
  72. }
  73. // If the title is empty, we use the URL as title.
  74. if ($title == '') {
  75. $title = $urllink;
  76. }
  77. // If the URL is invalid, an error occurs.
  78. // Ivan, 13-OCT-2010, Chamilo 1.8.8: Let us still tolerate PHP 5.1.x and avoid a specific bug in filter_var(), see http://bugs.php.net/51192
  79. //if (!filter_var($urllink, FILTER_VALIDATE_URL)) {
  80. if (!api_valid_url($urllink, true)) { // A check against an absolute URL
  81. $msgErr = get_lang('GiveURL');
  82. Display :: display_error_message(get_lang('GiveURL'));
  83. $ok = false;
  84. } else {
  85. // Looking for the largest order number for this category.
  86. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_link . " WHERE c_id = $course_id AND category_id = '" . intval($_POST['selectcategory']) . "'");
  87. list ($orderMax) = Database :: fetch_row($result);
  88. $order = $orderMax +1;
  89. $session_id = api_get_session_id();
  90. $sql = "INSERT INTO " . $tbl_link . " (c_id, url, title, description, category_id, display_order, on_homepage, target, session_id)
  91. VALUES (".$course_id.", '".Database :: escape_string($urllink) . "','" . Database :: escape_string($title) . "','" . Database :: escape_string($description) . "','" .
  92. Database :: escape_string($selectcategory) . "','" . Database :: escape_string($order) . "', '" . Database :: escape_string($onhomepage) . "','" .
  93. Database :: escape_string($target) . "','" . Database :: escape_string($session_id) . "')";
  94. $catlinkstatus = get_lang('LinkAdded');
  95. Database :: query($sql);
  96. $link_id = Database :: insert_id();
  97. if ((api_get_setting('search_enabled') == 'true') && $link_id && extension_loaded('xapian')) {
  98. require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
  99. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  100. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  101. $courseid = api_get_course_id();
  102. $specific_fields = get_specific_field_list();
  103. $ic_slide = new IndexableChunk();
  104. // Add all terms to db.
  105. $all_specific_terms = '';
  106. foreach ($specific_fields as $specific_field) {
  107. if (isset ($_REQUEST[$specific_field['code']])) {
  108. $sterms = trim($_REQUEST[$specific_field['code']]);
  109. if (!empty ($sterms)) {
  110. $all_specific_terms .= ' ' . $sterms;
  111. $sterms = explode(',', $sterms);
  112. foreach ($sterms as $sterm) {
  113. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  114. add_specific_field_value($specific_field['id'], $courseid, TOOL_LINK, $link_id, $sterm);
  115. }
  116. }
  117. }
  118. }
  119. // Build the chunk to index.
  120. $ic_slide->addValue('title', $title);
  121. $ic_slide->addCourseId($courseid);
  122. $ic_slide->addToolId(TOOL_LINK);
  123. $xapian_data = array (
  124. SE_COURSE_ID => $courseid,
  125. SE_TOOL_ID => TOOL_LINK,
  126. SE_DATA => array (
  127. 'link_id' => (int) $link_id
  128. ),
  129. SE_USER => (int) api_get_user_id(),
  130. );
  131. $ic_slide->xapian_data = serialize($xapian_data);
  132. $description = $all_specific_terms . ' ' . $description;
  133. $ic_slide->addValue('content', $description);
  134. // Add category name if set.
  135. if (isset ($_POST['selectcategory']) && $selectcategory > 0) {
  136. $table_link_category = Database :: get_course_table(TABLE_LINK_CATEGORY);
  137. $sql_cat = 'SELECT * FROM %s WHERE id=%d LIMIT 1';
  138. $sql_cat = sprintf($sql_cat, $table_link_category, (int) $selectcategory);
  139. $result = Database :: query($sql_cat);
  140. if (Database :: num_rows($result) == 1) {
  141. $row = Database :: fetch_array($result);
  142. $ic_slide->addValue('category', $row['category_title']);
  143. }
  144. }
  145. $di = new DokeosIndexer();
  146. isset ($_POST['language']) ? $lang = Database :: escape_string($_POST['language']) : $lang = 'english';
  147. $di->connectDb(NULL, NULL, $lang);
  148. $di->addChunk($ic_slide);
  149. // Index and return search engine document id.
  150. $did = $di->index();
  151. if ($did) {
  152. // Save it to db.
  153. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  154. $sql = 'INSERT INTO %s (c_id, id, course_code, tool_id, ref_id_high_level, search_did)
  155. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  156. $sql = sprintf($sql, $tbl_se_ref, api_get_course_int_id(), $courseid, TOOL_LINK, $link_id, $did);
  157. Database :: query($sql);
  158. }
  159. }
  160. unset ($urllink, $title, $description, $selectcategory);
  161. Display :: display_confirmation_message(get_lang('LinkAdded'));
  162. }
  163. } elseif ($type == 'category') {
  164. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  165. $category_title = trim($_POST['category_title']);
  166. $description = trim($_POST['description']);
  167. if (empty($category_title)) {
  168. $msgErr = get_lang('GiveCategoryName');
  169. Display :: display_error_message(get_lang('GiveCategoryName'));
  170. $ok = false;
  171. } else {
  172. // Looking for the largest order number for this category.
  173. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_categories." WHERE c_id = $course_id ");
  174. list ($orderMax) = Database :: fetch_row($result);
  175. $order = $orderMax +1;
  176. $order = intval($order);
  177. $session_id = api_get_session_id();
  178. $sql = "INSERT INTO ".$tbl_categories." (c_id, category_title, description, display_order, session_id)
  179. VALUES (".$course_id.", '" .Database::escape_string($category_title) . "', '" . Database::escape_string($description) . "', '$order', '$session_id')";
  180. Database :: query($sql);
  181. $catlinkstatus = get_lang('CategoryAdded');
  182. unset ($category_title, $description);
  183. Display :: display_confirmation_message(get_lang('CategoryAdded'));
  184. }
  185. }
  186. // "WHAT'S NEW" notification : update last tool Edit.
  187. if ($type == 'link') {
  188. global $_user;
  189. global $_course;
  190. global $nameTools;
  191. api_item_property_update($_course, TOOL_LINK, $link_id, 'LinkAdded', $_user['user_id']);
  192. }
  193. return $ok;
  194. }
  195. /**
  196. * Used to delete a link or a category
  197. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  198. */
  199. function deletelinkcategory($type) {
  200. global $catlinkstatus;
  201. global $_course;
  202. $tbl_link = Database :: get_course_table(TABLE_LINK);
  203. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  204. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  205. $course_id = api_get_course_int_id();
  206. if ($type == 'link') {
  207. global $id;
  208. // -> Items are no longer fysically deleted, but the visibility is set to 2 (in item_property).
  209. // This will make a restore function possible for the platform administrator.
  210. if (isset ($_GET['id']) && $_GET['id'] == strval(intval($_GET['id']))) {
  211. $sql = "UPDATE $tbl_link SET on_homepage='0' WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  212. Database :: query($sql);
  213. }
  214. api_item_property_update($_course, TOOL_LINK, $id, 'delete', api_get_user_id());
  215. delete_link_from_search_engine(api_get_course_id(), $id);
  216. $catlinkstatus = get_lang('LinkDeleted');
  217. unset ($id);
  218. Display :: display_confirmation_message(get_lang('LinkDeleted'));
  219. }
  220. if ($type == 'category') {
  221. global $id;
  222. if (isset ($_GET['id']) && !empty ($_GET['id'])) {
  223. // First we delete the category itself and afterwards all the links of this category.
  224. $sql = "DELETE FROM " . $tbl_categories . " WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  225. Database :: query($sql);
  226. $sql = "DELETE FROM " . $tbl_link . " WHERE c_id = $course_id AND category_id='" . intval($_GET['id']) . "'";
  227. $catlinkstatus = get_lang('CategoryDeleted');
  228. unset ($id);
  229. Database :: query($sql);
  230. Display :: display_confirmation_message(get_lang('CategoryDeleted'));
  231. }
  232. }
  233. }
  234. /**
  235. * Removes a link from search engine database
  236. *
  237. * @param string $course_id Course code
  238. * @param int $document_id Document id to delete
  239. */
  240. function delete_link_from_search_engine($course_id, $link_id) {
  241. // Remove from search engine if enabled.
  242. if (api_get_setting('search_enabled') == 'true') {
  243. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  244. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  245. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  246. $res = Database :: query($sql);
  247. if (Database :: num_rows($res) > 0) {
  248. $row = Database :: fetch_array($res);
  249. require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
  250. $di = new DokeosIndexer();
  251. $di->remove_document((int) $row['search_did']);
  252. }
  253. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  254. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  255. Database :: query($sql);
  256. // Remove terms from db.
  257. require_once (api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  258. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
  259. }
  260. }
  261. /**
  262. *
  263. * Get link info
  264. * @param int link id
  265. * @return array link info
  266. *
  267. * */
  268. function get_link_info($id) {
  269. $tbl_link = Database :: get_course_table(TABLE_LINK);
  270. $course_id = api_get_course_int_id();
  271. $sql = "SELECT * FROM " . $tbl_link . " WHERE c_id = $course_id AND id='" . intval($id) . "' ";
  272. $result = Database::query($sql);
  273. if (Database::num_rows($result)) {
  274. $data = Database::fetch_array($result);
  275. }
  276. return $data;
  277. }
  278. /**
  279. * Used to edit a link or a category
  280. * @todo Rewrite the whole links tool because it is becoming completely cluttered,
  281. * code does not follow the coding conventions, does not use html_quickform, ...
  282. * Some features were patched in.
  283. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  284. * @todo replace the globals with the appropriate $_POST or $_GET values
  285. */
  286. function editlinkcategory($type) {
  287. global $catlinkstatus;
  288. global $id;
  289. global $submit_link;
  290. global $submit_category;
  291. global $_user;
  292. global $_course;
  293. global $nameTools;
  294. global $urllink;
  295. global $title;
  296. global $description;
  297. global $category;
  298. global $selectcategory;
  299. global $description;
  300. global $category_title;
  301. global $onhomepage;
  302. global $target_link;
  303. $tbl_link = Database :: get_course_table(TABLE_LINK);
  304. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  305. $course_id = api_get_course_int_id();
  306. if ($type == 'link') {
  307. // This is used to populate the link-form with the info found in the database.
  308. if (!empty ($_GET['id'])) {
  309. $sql = "SELECT * FROM " . $tbl_link . " WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  310. $result = Database :: query($sql);
  311. if ($myrow = Database :: fetch_array($result)) {
  312. $urllink = $myrow['url'];
  313. $title = $myrow['title'];
  314. $description = $myrow['description'];
  315. $category = $myrow['category_id'];
  316. if ($myrow['on_homepage'] != 0) {
  317. $onhomepage = 'checked';
  318. }
  319. $target_link = $myrow['target'];
  320. }
  321. }
  322. // This is used to put the modified info of the link-form into the database.
  323. if ($_POST['submitLink']) {
  324. // Ivan, 13-OCT-2010: It is a litle bit messy code below, just in case I added some extra-security checks here.
  325. $_POST['urllink'] = trim($_POST['urllink']);
  326. $_POST['title'] = trim(Security :: remove_XSS($_POST['title']));
  327. $_POST['description'] = trim(Security :: remove_XSS($_POST['description']));
  328. $_POST['selectcategory'] = intval($_POST['selectcategory']);
  329. $_POST['id'] = intval($_POST['id']);
  330. // We ensure URL to be absolute.
  331. if (strpos($_POST['urllink'], '://') === false) {
  332. $_POST['urllink'] = 'http://' . $_POST['urllink'];
  333. }
  334. // If the title is empty, we use the URL as title.
  335. if ($_POST['title'] == '') {
  336. $_POST['title'] = $_POST['urllink'];
  337. }
  338. // If the URL is invalid, an error occurs.
  339. // Ivan, 13-OCT-2010, Chamilo 1.8.8: Let us still tolerate PHP 5.1.x and avoid a specific bug in filter_var(), see http://bugs.php.net/51192
  340. //if (!filter_var($urllink, FILTER_VALIDATE_URL)) {
  341. if (!api_valid_url($urllink, true)) { // A check against an absolute URL.
  342. $msgErr = get_lang('GiveURL');
  343. Display :: display_error_message(get_lang('GiveURL'));
  344. return false;
  345. }
  346. $onhomepage = Security :: remove_XSS($_POST['onhomepage']);
  347. $target = Database::escape_string($_POST['target_link']);
  348. if (empty ($mytarget)) {
  349. $mytarget = '_self';
  350. }
  351. $mytarget = ",target='" . $target . "'";
  352. // Finding the old category_id.
  353. $sql = "SELECT * FROM " . $tbl_link . " WHERE c_id = $course_id AND id='" . intval($_POST['id']) . "'";
  354. $result = Database :: query($sql);
  355. $row = Database :: fetch_array($result);
  356. $category_id = $row['category_id'];
  357. if ($category_id != $_POST['selectcategory']) {
  358. $sql = "SELECT MAX(display_order) FROM " . $tbl_link . " WHERE c_id = $course_id AND category_id='" . intval($_POST['selectcategory']) . "'";
  359. $result = Database :: query($sql);
  360. list ($max_display_order) = Database :: fetch_row($result);
  361. $max_display_order++;
  362. } else {
  363. $max_display_order = $row['display_order'];
  364. }
  365. $sql = "UPDATE " . $tbl_link . " SET " .
  366. "url='" . Database :: escape_string($_POST['urllink']) . "', " .
  367. "title='" . Database :: escape_string($_POST['title']) . "', " .
  368. "description='" . Database :: escape_string($_POST['description']) . "', " .
  369. "category_id='" . Database :: escape_string($_POST['selectcategory']) . "', " .
  370. "display_order='" . $max_display_order . "', " .
  371. "on_homepage='" . Database :: escape_string($onhomepage) . " ' $mytarget " .
  372. " WHERE c_id = $course_id AND id='" . intval($_POST['id']) . "'";
  373. Database :: query($sql);
  374. // Update search enchine and its values table if enabled.
  375. if (api_get_setting('search_enabled') == 'true') {
  376. $link_id = intval($_POST['id']);
  377. $course_id = api_get_course_id();
  378. $link_url = Database :: escape_string($_POST['urllink']);
  379. $link_title = Database :: escape_string($_POST['title']);
  380. $link_description = Database :: escape_string($_POST['description']);
  381. // Actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one.
  382. // Get search_did.
  383. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  384. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  385. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  386. $res = Database :: query($sql);
  387. if (Database :: num_rows($res) > 0) {
  388. require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
  389. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  390. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  391. $se_ref = Database :: fetch_array($res);
  392. $specific_fields = get_specific_field_list();
  393. $ic_slide = new IndexableChunk();
  394. $all_specific_terms = '';
  395. foreach ($specific_fields as $specific_field) {
  396. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_LINK, $link_id);
  397. if (isset ($_REQUEST[$specific_field['code']])) {
  398. $sterms = trim($_REQUEST[$specific_field['code']]);
  399. if (!empty ($sterms)) {
  400. $all_specific_terms .= ' ' . $sterms;
  401. $sterms = explode(',', $sterms);
  402. foreach ($sterms as $sterm) {
  403. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  404. add_specific_field_value($specific_field['id'], $course_id, TOOL_LINK, $link_id, $sterm);
  405. }
  406. }
  407. }
  408. }
  409. // Build the chunk to index.
  410. $ic_slide->addValue("title", $link_title);
  411. $ic_slide->addCourseId($course_id);
  412. $ic_slide->addToolId(TOOL_LINK);
  413. $xapian_data = array (
  414. SE_COURSE_ID => $course_id,
  415. SE_TOOL_ID => TOOL_LINK,
  416. SE_DATA => array (
  417. 'link_id' => (int) $link_id
  418. ),
  419. SE_USER => (int) api_get_user_id(),
  420. );
  421. $ic_slide->xapian_data = serialize($xapian_data);
  422. $link_description = $all_specific_terms . ' ' . $link_description;
  423. $ic_slide->addValue('content', $link_description);
  424. // Add category name if set.
  425. if (isset ($_POST['selectcategory']) && $selectcategory > 0) {
  426. $table_link_category = Database :: get_course_table(TABLE_LINK_CATEGORY);
  427. $sql_cat = 'SELECT * FROM %s WHERE id=%d LIMIT 1';
  428. $sql_cat = sprintf($sql_cat, $table_link_category, (int) $selectcategory);
  429. $result = Database :: query($sql_cat);
  430. if (Database :: num_rows($result) == 1) {
  431. $row = Database :: fetch_array($result);
  432. $ic_slide->addValue('category', $row['category_title']);
  433. }
  434. }
  435. $di = new DokeosIndexer();
  436. isset ($_POST['language']) ? $lang = Database :: escape_string($_POST['language']) : $lang = 'english';
  437. $di->connectDb(NULL, NULL, $lang);
  438. $di->remove_document((int) $se_ref['search_did']);
  439. $di->addChunk($ic_slide);
  440. // Index and return search engine document id.
  441. $did = $di->index();
  442. if ($did) {
  443. // Save it to db.
  444. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  445. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  446. Database :: query($sql);
  447. $sql = 'INSERT INTO %s (c_id, id, course_code, tool_id, ref_id_high_level, search_did)
  448. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  449. $sql = sprintf($sql, $tbl_se_ref, api_get_course_int_id(), $course_id, TOOL_LINK, $link_id, $did);
  450. Database :: query($sql);
  451. }
  452. }
  453. }
  454. // "WHAT'S NEW" notification: update table last_toolEdit.
  455. api_item_property_update($_course, TOOL_LINK, $_POST['id'], 'LinkUpdated', $_user['user_id']);
  456. Display :: display_confirmation_message(get_lang('LinkModded'));
  457. }
  458. }
  459. if ($type == 'category') {
  460. // This is used to populate the category-form with the info found in the database.
  461. if (!$submit_category) {
  462. $sql = "SELECT * FROM " . $tbl_categories . " WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  463. $result = Database :: query($sql);
  464. if ($myrow = Database :: fetch_array($result)) {
  465. $category_title = $myrow['category_title'];
  466. $description = $myrow['description'];
  467. }
  468. }
  469. // This is used to put the modified info of the category-form into the database.
  470. if ($submit_category) {
  471. $sql = "UPDATE " . $tbl_categories . " SET category_title='" . Database :: escape_string($_POST['category_title']) . "', description='" . Database :: escape_string($_POST['description']) . "'
  472. WHERE c_id = $course_id AND id='" . Database :: escape_string($_POST['id']) . "'";
  473. Database :: query($sql);
  474. Display :: display_confirmation_message(get_lang('CategoryModded'));
  475. }
  476. }
  477. return true; // On errors before this statement, exit from this function by returning false value.
  478. }
  479. /**
  480. * Creates a correct $view for in the URL
  481. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  482. */
  483. function makedefaultviewcode($locatie) {
  484. global $aantalcategories, $view;
  485. for ($j = 0; $j <= $aantalcategories -1; $j++) {
  486. $view[$j] = 0;
  487. }
  488. $view[intval($locatie)] = '1';
  489. }
  490. /**
  491. * Changes the visibility of a link
  492. * @todo add the changing of the visibility of a course
  493. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  494. */
  495. function change_visibility($id, $scope) {
  496. global $_course, $_user;
  497. if ($scope == 'link') {
  498. api_item_property_update($_course, TOOL_LINK, $id, $_GET['action'], $_user['user_id']);
  499. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  500. }
  501. }
  502. /**
  503. * Displays all the links of a given category.
  504. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  505. */
  506. function showlinksofcategory($catid) {
  507. global $is_allowed, $charset, $urlview, $up, $down, $_user, $token;
  508. $tbl_link = Database :: get_course_table(TABLE_LINK);
  509. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  510. // Condition for the session.
  511. $session_id = api_get_session_id();
  512. $condition_session = api_get_session_condition($session_id, true, true);
  513. $catid = intval($catid);
  514. $course_id = api_get_course_int_id();
  515. $sqlLinks = "SELECT *, link.id FROM " . $tbl_link . " link, " . $TABLE_ITEM_PROPERTY . " itemproperties
  516. WHERE itemproperties.tool='" . TOOL_LINK . "' AND
  517. link.id=itemproperties.ref AND
  518. link.category_id='" . $catid . "' AND
  519. (itemproperties.visibility='0' OR itemproperties.visibility='1')
  520. $condition_session AND
  521. link.c_id = ".$course_id." AND
  522. itemproperties.c_id = ".$course_id."
  523. ORDER BY link.display_order DESC";
  524. $result = Database :: query($sqlLinks);
  525. $numberoflinks = Database :: num_rows($result);
  526. if ($numberoflinks > 0) {
  527. echo '<table class="data_table" width="100%">';
  528. $i = 1;
  529. while ($myrow = Database :: fetch_array($result)) {
  530. // Validacion when belongs to a session.
  531. $session_img = api_get_session_image($myrow['session_id'], $_user['status']);
  532. $css_class = $i % 2 == 0 ? $css_class = 'row_odd' : $css_class = 'row_even';
  533. $link_validator = '';
  534. if (api_is_allowed_to_edit(null, true)) {
  535. $link_validator = ''.Display::url(Display::return_icon('preview_view.png', get_lang('CheckURL'), array(), 16), '#', array('onclick'=>"check_url('".$myrow['id']."', '".addslashes($myrow['url'])."');"));
  536. $link_validator .= Display::span('', array('id'=>'url_id_'.$myrow['id']));
  537. }
  538. if ($myrow['visibility'] == '1') {
  539. echo '<tr class="'.$css_class.'">';
  540. echo '<td align="center" valign="middle" width="15">';
  541. echo '<a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow['id'], '&amp;link_url=', urlencode($myrow['url']), '" target="_blank">
  542. <img src="../../main/img/link.gif" border="0" alt="', get_lang('Link'), '"/></a></td>
  543. <td width="80%" valign="top"><a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow['id'], '&amp;link_url=', urlencode($myrow['url']), '" target="', $myrow['target'], '">';
  544. echo Security :: remove_XSS($myrow['url']);
  545. echo '</a>';
  546. echo $link_validator;
  547. echo $session_img;
  548. echo '<br />', $myrow['title'];
  549. } else {
  550. if (api_is_allowed_to_edit(null, true)) {
  551. echo '<tr class="'.$css_class.'">';
  552. echo '<td align="center" valign="middle" width="15"><a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow['id'], "&amp;link_url=", urlencode($myrow['url']), '" target="_blank" class="invisible">';
  553. echo Display :: return_icon('link_na.gif', get_lang('Link')), '</a>';
  554. echo '</td><td width="80%" valign="top"><a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow['id'], '&amp;link_url=', urlencode($myrow['url']),'" target="', $myrow['target'], '" class="invisible">';
  555. echo Security :: remove_XSS($myrow['url']);
  556. echo "</a>";
  557. echo $link_validator;
  558. echo $session_img, '<br />', $myrow['title'];
  559. }
  560. }
  561. echo '<td style="text-align:center;">';
  562. if (api_is_allowed_to_edit(null, true)) {
  563. if ($session_id == $myrow['session_id']) {
  564. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=editlink&amp;category=' . (!empty ($category) ? $category : '') . '&amp;id=' . $myrow['id'] . '&amp;urlview=' . $urlview . '" title="' . get_lang('Modify') . '">' .
  565. Display :: return_icon('edit.png', get_lang('Modify'), array (), ICON_SIZE_SMALL) . '</a>';
  566. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  567. /*
  568. if ($i != 1) {
  569. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;urlview=' . $urlview . '&amp;up=', $myrow[0], '" title="' . get_lang('Up') . '">' . Display :: return_icon('up.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  570. } else {
  571. echo Display :: return_icon('up_na.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '</a>';
  572. }
  573. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  574. if ($i < $numberoflinks) {
  575. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;urlview=' . $urlview . '&amp;down=' . $myrow[0] . '" title="' . get_lang('Down') . '">' . Display :: return_icon('down.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  576. } else {
  577. echo Display :: return_icon('down_na.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  578. }*/
  579. if ($myrow['visibility'] == '1') {
  580. echo '<a href="link.php?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=invisible&amp;id=' . $myrow['id'] . '&amp;scope=link&amp;urlview=' . $urlview . '" title="' . get_lang('Hide') . '">' .
  581. Display :: return_icon('visible.png', get_lang('Hide'), array (), ICON_SIZE_SMALL) . '</a>';
  582. }
  583. if ($myrow['visibility'] == '0') {
  584. echo ' <a href="link.php?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=visible&amp;id=' . $myrow['id'] . '&amp;scope=link&amp;urlview=' . $urlview . '" title="' . get_lang('Show') . '">' .
  585. Display :: return_icon('invisible.png', get_lang('Show'), array (), ICON_SIZE_SMALL) . '</a>';
  586. }
  587. echo ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=deletelink&amp;id=', $myrow['id'], '&amp;urlview=', $urlview, "\" onclick=\"javascript: if(!confirm('" . get_lang('LinkDelconfirm') . "')) return false;\" title=\"" . get_lang('Delete') . '">' .
  588. Display :: return_icon('delete.png', get_lang('Delete'), array (), ICON_SIZE_SMALL) . '</a>';
  589. } else {
  590. echo Display :: return_icon('edit_na.png', get_lang('EditionNotAvailableFromSession'), array (), ICON_SIZE_SMALL); //get_lang('EditionNotAvailableFromSession');
  591. }
  592. }
  593. echo '</td></tr>';
  594. $i++;
  595. }
  596. echo '</table>';
  597. }
  598. }
  599. /**
  600. * Displays the edit, delete and move icons
  601. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  602. */
  603. function showcategoryadmintools($categoryid) {
  604. global $urlview, $aantalcategories, $catcounter, $token;
  605. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=editcategory&amp;id=' . $categoryid . '&amp;urlview=' . $urlview . '" title=' . get_lang('Modify') . '">' . Display :: return_icon('edit.png', get_lang('Modify'), array (), ICON_SIZE_SMALL) . '</a>';
  606. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  607. if ($catcounter != 1) {
  608. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;catmove=true&amp;up=', $categoryid, '&amp;urlview=' . $urlview . '" title="' . get_lang('Up') . '">' . Display :: return_icon('up.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '</a>';
  609. } else {
  610. echo Display :: return_icon('up_na.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '</a>';
  611. }
  612. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  613. if ($catcounter < $aantalcategories) {
  614. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() .'&amp;sec_token='.$token.'&amp;catmove=true&amp;down=' . $categoryid . '&amp;urlview=' . $urlview . '">
  615. ' . Display :: return_icon('down.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '</a>';
  616. } else {
  617. echo Display :: return_icon('down_na.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '</a>';
  618. }
  619. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=deletecategory&amp;id=', $categoryid, "&amp;urlview=$urlview\" onclick=\"javascript: if(!confirm('" . get_lang('CategoryDelconfirm') . "')) return false;\">", Display :: return_icon('delete.png', get_lang('Delete'), array (), ICON_SIZE_SMALL) . '</a>';
  620. $catcounter++;
  621. }
  622. /**
  623. * move a link or a linkcategory up or down
  624. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  625. */
  626. function movecatlink($catlinkid) {
  627. global $catmove;
  628. global $up;
  629. global $down;
  630. $tbl_link = Database :: get_course_table(TABLE_LINK);
  631. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  632. $course_id = api_get_course_int_id();
  633. if (!empty ($down)) {
  634. $thiscatlinkId = intval($down);
  635. $sortDirection = 'DESC';
  636. }
  637. if (!empty ($up)) {
  638. $thiscatlinkId = intval($up);
  639. $sortDirection = 'ASC';
  640. }
  641. // We check if it is a category we are moving or a link. If it is a category, a querystring catmove = true is present in the url.
  642. if ($catmove == 'true') {
  643. $movetable = $tbl_categories;
  644. $catid = $catlinkid;
  645. } else {
  646. $movetable = $tbl_link;
  647. // Getting the category of the link.
  648. if (!empty ($thiscatlinkId)) {
  649. $sql = "SELECT category_id FROM " . $movetable . " WHERE c_id = $course_id AND id='$thiscatlinkId'";
  650. $result = Database :: query($sql);
  651. $catid = Database :: fetch_array($result);
  652. }
  653. }
  654. // This code is copied and modified from announcements.php.
  655. if (!empty($sortDirection)) {
  656. if (!in_array(trim(strtoupper($sortDirection)), array ('ASC', 'DESC'))){
  657. $sortDirection = 'ASC';
  658. }
  659. if ($catmove == 'true') {
  660. $sqlcatlinks = "SELECT id, display_order FROM " . $movetable . " WHERE c_id = $course_id ORDER BY display_order $sortDirection";
  661. } else {
  662. $sqlcatlinks = "SELECT id, display_order FROM " . $movetable . "
  663. WHERE c_id = $course_id AND category_id='" . $catid[0] . "'
  664. ORDER BY display_order $sortDirection";
  665. }
  666. $linkresult = Database :: query($sqlcatlinks);
  667. while ($sortrow = Database :: fetch_array($linkresult)) {
  668. // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER, COMMIT SWAP
  669. // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  670. // and we know the next iteration of the while loop is the next one. These should be swapped.
  671. if (isset ($thislinkFound) && $thislinkFound) {
  672. $nextlinkId = $sortrow['id'];
  673. $nextlinkOrdre = $sortrow['display_order'];
  674. Database :: query("UPDATE " . $movetable . "
  675. SET display_order = '$nextlinkOrdre'
  676. WHERE c_id = $course_id AND id = '$thiscatlinkId'");
  677. Database :: query("UPDATE " . $movetable . "
  678. SET display_order = '$thislinkOrdre'
  679. WHERE c_id = $course_id AND id = '$nextlinkId'");
  680. break;
  681. }
  682. if ($sortrow['id'] == $thiscatlinkId) {
  683. $thislinkOrdre = $sortrow['display_order'];
  684. $thislinkFound = true;
  685. }
  686. }
  687. }
  688. Display :: display_confirmation_message(get_lang('LinkMoved'));
  689. }
  690. /**
  691. * CSV file import functions
  692. * @author René Haentjens , Ghent University
  693. */
  694. function get_cat($catname) {
  695. // Get category id (existing or make new).
  696. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  697. $course_id = api_get_course_int_id();
  698. $result = Database :: query("SELECT id FROM " . $tbl_categories . " WHERE c_id = $course_id AND category_title='" . Database::escape_string($catname) . "'");
  699. if (Database :: num_rows($result) >= 1 && ($row = Database :: fetch_array($result))) {
  700. return $row['id']; // Several categories with same name: take the first.
  701. }
  702. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_categories);
  703. list ($max_order) = Database :: fetch_row($result);
  704. Database :: query("INSERT INTO " . $tbl_categories . " (c_id, category_title, description, display_order)
  705. VALUES (".$course_id.", '" . Database::escape_string($catname) . "','','" . ($max_order +1) . "')");
  706. return Database :: insert_id();
  707. }
  708. /**
  709. * CSV file import functions
  710. * @author René Haentjens , Ghent University
  711. */
  712. function put_link($url, $cat, $title, $description, $on_homepage, $hidden) {
  713. $tbl_link = Database :: get_course_table(TABLE_LINK);
  714. $course_id = api_get_course_int_id();
  715. $urleq = "url='" . Database :: escape_string($url) . "'";
  716. $cateq = "category_id=" . intval($cat);
  717. $result = Database :: query("SELECT id FROM $tbl_link WHERE c_id = $course_id AND " . $urleq . ' AND ' . $cateq);
  718. if (Database :: num_rows($result) >= 1 && ($row = Database :: fetch_array($result))) {
  719. Database :: query("UPDATE $tbl_link set title='" . Database :: escape_string($title) . "', description='" . Database :: escape_string($description) . "'
  720. WHERE c_id = $course_id AND id='" . Database :: escape_string($row['id']) . "'");
  721. $ipu = 'LinkUpdated';
  722. $rv = 1; // 1 = upd
  723. } else {
  724. // Add new link
  725. $result = Database :: query("SELECT MAX(display_order) FROM $tbl_link WHERE c_id = $course_id AND category_id='" . intval($cat) . "'");
  726. list ($max_order) = Database :: fetch_row($result);
  727. Database :: query("INSERT INTO $tbl_link (c_id, url, title, description, category_id, display_order, on_homepage)
  728. VALUES (".api_get_course_int_id().", '" . Database :: escape_string($url) . "','" . Database :: escape_string($title) . "','" . Database :: escape_string($description) . "','" . intval($cat) . "','" . (intval($max_order) + 1) . "','" . intval($on_homepage) . "')");
  729. $id = Database :: insert_id();
  730. $ipu = 'LinkAdded';
  731. $rv = 2; // 2 = new
  732. }
  733. global $_course, $nameTools, $_user;
  734. api_item_property_update($_course, TOOL_LINK, $id, $ipu, $_user['user_id']);
  735. if ($hidden && $ipu == 'LinkAdded') {
  736. api_item_property_update($_course, TOOL_LINK, $id, 'invisible', $_user['user_id']);
  737. }
  738. return $rv;
  739. }
  740. /**
  741. * CSV file import functions
  742. * @author René Haentjens , Ghent University
  743. */
  744. function import_link($linkdata) {
  745. // url, category_id, title, description, ...
  746. // Field names used in the uploaded file
  747. $known_fields = array (
  748. 'url',
  749. 'category',
  750. 'title',
  751. 'description',
  752. 'on_homepage',
  753. 'hidden'
  754. );
  755. $hide_fields = array (
  756. 'kw',
  757. 'kwd',
  758. 'kwds',
  759. 'keyword',
  760. 'keywords'
  761. );
  762. // All other fields are added to description, as "name:value".
  763. // Only one hide_field is assumed to be present, <> is removed from value.
  764. if (!($url = trim($linkdata['url'])) || !($title = trim($linkdata['title']))) {
  765. return 0; // 0 = fail
  766. }
  767. $cat = ($catname = trim($linkdata['category'])) ? get_cat($catname) : 0;
  768. $regs = array (); // Will be passed to ereg()
  769. foreach ($linkdata as $key => $value)
  770. if (!in_array($key, $known_fields))
  771. if (in_array($key, $hide_fields) && ereg('^<?([^>]*)>?$', $value, $regs)) // possibly in <...>
  772. if (($kwlist = trim($regs[1])) != '')
  773. $kw = '<i kw="' . htmlspecialchars($kwlist) . '">';
  774. else
  775. $kw = '';
  776. // i.e. assume only one of the $hide_fields will be present
  777. // and if found, hide the value as expando property of an <i> tag
  778. elseif (trim($value)) {
  779. $d .= ', ' . $key . ':' . $value;
  780. }
  781. if ($d) {
  782. $d = substr($d, 2) . ' - ';
  783. }
  784. return put_link($url, $cat, $title, $kw . ereg_replace('\[((/?(b|big|i|small|sub|sup|u))|br/)\]', '<\\1>', htmlspecialchars($d . $linkdata['description'])) . ($kw ? '</i>' : ''), $linkdata['on_homepage'] ? '1' : '0', $linkdata['hidden'] ? '1' : '0');
  785. // i.e. allow some BBcode tags, e.g. [b]...[/b]
  786. }
  787. /**
  788. * CSV file import functions
  789. * @author René Haentjens , Ghent University
  790. */
  791. function import_csvfile() {
  792. global $catlinkstatus; // Feedback message to user.
  793. if (is_uploaded_file($filespec = $_FILES['import_file']['tmp_name']) && filesize($filespec) && ($myFile = @ fopen($filespec, 'r'))) {
  794. // read first line of file (column names) and find ',' or ';'
  795. $listsep = strpos($colnames = trim(fgets($myFile)), ',') !== false ? ',' : (strpos($colnames, ';') !== false ? ';' : '');
  796. if ($listsep) {
  797. $columns = array_map('strtolower', explode($listsep, $colnames));
  798. if (in_array('url', $columns) && in_array('title', $columns)) {
  799. $stats = array (
  800. 0,
  801. 0,
  802. 0
  803. ); // fails, updates, inserts
  804. // Modified by Ivan Tcholakov, 01-FEB-2010.
  805. //while (($data = fgetcsv($myFile, 32768, $listsep))) {
  806. while (($data = api_fgetcsv($myFile, null, $listsep))) {
  807. //
  808. foreach ($data as $i => & $text) {
  809. $linkdata[$columns[$i]] = $text;
  810. }
  811. $stats[import_link($linkdata)]++;
  812. unset ($linkdata);
  813. }
  814. $catlinkstatus = '';
  815. if ($stats[0]) {
  816. $catlinkstatus .= $stats[0] . ' ' . get_lang('CsvLinesFailed');
  817. }
  818. if ($stats[1]) {
  819. $catlinkstatus .= $stats[1] . ' ' . get_lang('CsvLinesOld');
  820. }
  821. if ($stats[2]) {
  822. $catlinkstatus .= $stats[2] . ' ' . get_lang('CsvLinesNew');
  823. }
  824. } else {
  825. $catlinkstatus = get_lang('CsvFileNoURL') . ($colnames ? get_lang('CsvFileLine1') . htmlspecialchars(substr($colnames, 0, 200)) . '...' : '');
  826. }
  827. } else {
  828. $catlinkstatus = get_lang('CsvFileNoSeps') . ($colnames ? get_lang('CsvFileLine1') . htmlspecialchars(substr($colnames, 0, 200)) . '...' : '');
  829. }
  830. fclose($myFile);
  831. } else {
  832. $catlinkstatus = get_lang('CsvFileNotFound');
  833. }
  834. }
  835. /**
  836. * This function checks if the url is a youtube link
  837. * @author Jorge Frisancho
  838. * @author Julio Montoya - Fixing code
  839. * @version 1.0
  840. */
  841. function is_youtube_link($url) {
  842. $is_youtube_link = strrpos($url, "youtube") || strrpos($url, "youtu.be");
  843. return $is_youtube_link;
  844. }
  845. function get_youtube_video_id($url) {
  846. // This is the length of YouTube's video IDs
  847. $len = 11;
  848. // The ID string starts after "v=", which is usually right after
  849. // "youtube.com/watch?" in the URL
  850. $pos = strpos($url, "v=");
  851. $id = '';
  852. //If false try other options
  853. if ($pos === false) {
  854. $url_parsed = parse_url($url);
  855. //Youtube shortener
  856. //http://youtu.be/ID
  857. $pos = strpos($url, "youtu.be");
  858. if ($pos == false) {
  859. $id = '';
  860. } else {
  861. return substr($url_parsed['path'], 1);
  862. }
  863. //if empty try the youtube.com/embed/ID
  864. if (empty($id)) {
  865. $pos = strpos($url, "embed");
  866. if ($pos === false) {
  867. return '';
  868. } else {
  869. return substr($url_parsed['path'], 7);
  870. }
  871. }
  872. } else {
  873. // Offset the start location to match the beginning of the ID string
  874. $pos += 2;
  875. // Get the ID string and return it
  876. $id = substr($url, $pos, $len);
  877. return $id;
  878. }
  879. }