link.lib.php 41 KB

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