link.lib.php 41 KB

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