kannelsms.lib.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. <?php
  2. /* For licensing terms, see /vendor/license.txt */
  3. /**
  4. * Class Kannelsms
  5. * This script handles incoming SMS information, process it and sends an SMS if everything is right.
  6. *
  7. * @package chamilo.plugin.kannelsms.lib
  8. *
  9. * @author Imanol Losada <imanol.losada@beeznest.com>
  10. *
  11. * Kannelsms-Chamilo connector class
  12. */
  13. class Kannelsms implements SmsPluginLibraryInterface
  14. {
  15. public $api;
  16. public $hostAddress;
  17. public $username;
  18. public $password;
  19. public $from;
  20. public $plugin_enabled = false;
  21. /**
  22. * Constructor (generates a connection to the API).
  23. *
  24. * @param string Kannelsms API key required to use the plugin
  25. */
  26. public function __construct($apiKey = null)
  27. {
  28. $plugin = KannelsmsPlugin::create();
  29. $kannelSMSPlugin = $plugin->get('tool_enable');
  30. $this->table = Database::get_main_table('user_field_values');
  31. if ($kannelSMSPlugin == true) {
  32. $this->api = new Kannel(' ');
  33. $this->hostAddress = $plugin->get('hostAddress');
  34. $this->username = $plugin->get('username');
  35. $this->password = $plugin->get('password');
  36. $this->from = $plugin->get('from');
  37. $this->plugin_enabled = true;
  38. }
  39. }
  40. /**
  41. * getMobilePhoneNumberById (retrieves a user mobile phone number by user id).
  42. *
  43. * @param int $userId User id
  44. *
  45. * @return int User's mobile phone number
  46. */
  47. public function getMobilePhoneNumberById($userId)
  48. {
  49. $mobilePhoneNumberExtraField = new ExtraField('user');
  50. $mobilePhoneNumberExtraField = $mobilePhoneNumberExtraField->get_handler_field_info_by_field_variable(
  51. 'mobile_phone_number'
  52. );
  53. $mobilePhoneNumberExtraFieldValue = new ExtraFieldValue('user');
  54. $mobilePhoneNumberExtraFieldValue = $mobilePhoneNumberExtraFieldValue->get_values_by_handler_and_field_id(
  55. $userId,
  56. $mobilePhoneNumberExtraField['id']
  57. );
  58. return $mobilePhoneNumberExtraFieldValue['value'];
  59. }
  60. /**
  61. * send (sends an SMS to the user).
  62. *
  63. * @param array Data needed to send the SMS. It is mandatory to include the
  64. * 'smsType' and 'userId' (or 'mobilePhoneNumber') fields at least.
  65. * More data may be neccesary depending on the message type
  66. * Example: $additional_parameters = array(
  67. * 'smsType' => EXAMPLE_SMS_TYPE,
  68. * 'userId' => $userId,
  69. * 'moreData' => $moreData
  70. * );
  71. */
  72. public function send($additionalParameters)
  73. {
  74. $trimmedKey = trim(CONFIG_SECURITY_API_KEY);
  75. if (!empty($trimmedKey)) {
  76. $message = [
  77. "to" => array_key_exists("mobilePhoneNumber", $additionalParameters) ?
  78. $additionalParameters['mobilePhoneNumber'] : $this->getMobilePhoneNumberById($additionalParameters['userId']),
  79. "message" => $this->getSms($additionalParameters),
  80. ];
  81. if (!empty($message['message'])) {
  82. if (extension_loaded('curl')) {
  83. $url = $this->hostAddress.'?username='.
  84. $this->username.'&password='.$this->password.'&from='.
  85. $this->from.'&to='.$message['to'].'&msg='.urlencode($message['message']);
  86. $ch = curl_init($url);
  87. curl_exec($ch);
  88. curl_close($ch);
  89. }
  90. // Commented for future message logging / tracking purposes
  91. /*if( $result["success"] ) {
  92. echo "Message sent - ID: " . $result["id"];
  93. } else {
  94. echo "Message failed - Error: " . $result["error_message"];
  95. }*/
  96. }
  97. }
  98. }
  99. /**
  100. * buildSms (builds an SMS from a template and data).
  101. *
  102. * @param object KannelsmsPlugin object
  103. * @param object Template object
  104. * @param string Template file name
  105. * @param string Text key from lang file
  106. * @param array Data to fill message variables (if any)
  107. *
  108. * @return object Template object with message property updated
  109. */
  110. public function buildSms($plugin, $tpl, $templateName, $messageKey, $parameters = null)
  111. {
  112. $result = Database::select(
  113. 'selected_value',
  114. 'settings_current',
  115. [
  116. 'where' => ['variable = ?' => ['kannelsms_message'.$messageKey]],
  117. ]
  118. );
  119. if (empty($result)) {
  120. $tpl->assign('message', '');
  121. } else {
  122. $templatePath = 'kannelsms/sms_templates/';
  123. $content = $tpl->fetch($templatePath.$templateName);
  124. $message = $plugin->get_lang($messageKey);
  125. if ($parameters !== null) {
  126. $message = vsprintf($message, $parameters);
  127. }
  128. $tpl->assign('message', $message);
  129. }
  130. return $tpl->params['message'];
  131. }
  132. /**
  133. * getSms (returns an SMS message depending of its type).
  134. *
  135. * @param array Data needed to send the SMS. It is mandatory to include the
  136. * 'smsType' and 'userId' (or 'mobilePhoneNumber') fields at least.
  137. * More data may be neccesary depending on the message type
  138. * Example: $additional_parameters = array(
  139. * 'smsType' => EXAMPLE_SMS_TYPE,
  140. * 'userId' => $userId,
  141. * 'moreData' => $moreData
  142. * );
  143. *
  144. * @return string A ready to be sent SMS
  145. */
  146. public function getSms($additionalParameters)
  147. {
  148. $plugin = KannelsmsPlugin::create();
  149. $tool_name = $plugin->get_lang('plugin_title');
  150. $tpl = new Template($tool_name);
  151. switch ($additionalParameters['smsType']) {
  152. case SmsPlugin::WELCOME_LOGIN_PASSWORD:
  153. $userInfo = api_get_user_info($additionalParameters['userId']);
  154. return $this->buildSms(
  155. $plugin,
  156. $tpl,
  157. 'welcome_login_password.tpl',
  158. 'WelcomeXLoginXPasswordX',
  159. [
  160. api_get_setting('siteName'),
  161. $userInfo['username'],
  162. $additionalParameters['password'],
  163. ]
  164. );
  165. break;
  166. case SmsPlugin::NEW_FILE_SHARED_COURSE_BY:
  167. return $this->buildSms(
  168. $plugin,
  169. $tpl,
  170. 'new_file_shared_course_by.tpl',
  171. 'XNewFileSharedCourseXByX',
  172. [
  173. api_get_setting('siteName'),
  174. $additionalParameters['courseTitle'],
  175. $additionalParameters['userUsername'],
  176. ]
  177. );
  178. break;
  179. case SmsPlugin::ACCOUNT_APPROVED_CONNECT:
  180. return $this->buildSms(
  181. $plugin,
  182. $tpl,
  183. 'account_approved_connect.tpl',
  184. 'XAccountApprovedConnectX',
  185. [
  186. api_get_setting('siteName'),
  187. $tpl->params['_p']['web'],
  188. ]
  189. );
  190. break;
  191. case SmsPlugin::NEW_COURSE_BEEN_CREATED:
  192. return $this->buildSms(
  193. $plugin,
  194. $tpl,
  195. 'new_course_been_created.tpl',
  196. 'XNewCourseXBeenCreatedX',
  197. [
  198. api_get_setting('siteName'),
  199. $additionalParameters['courseName'],
  200. $additionalParameters['creatorUsername'],
  201. ]
  202. );
  203. break;
  204. case SmsPlugin::NEW_USER_SUBSCRIBED_COURSE:
  205. return $this->buildSms(
  206. $plugin,
  207. $tpl,
  208. 'new_user_subscribed_course.tpl',
  209. 'XNewUserXSubscribedCourseX',
  210. [
  211. api_get_setting('siteName'),
  212. $additionalParameters['userUsername'],
  213. $additionalParameters['courseCode'],
  214. ]
  215. );
  216. break;
  217. case SmsPlugin::NEW_COURSE_SUGGESTED_TEACHER:
  218. return $this->buildSms(
  219. $plugin,
  220. $tpl,
  221. 'new_course_suggested_teacher.tpl',
  222. 'XNewCourseSuggestedTeacherX',
  223. [
  224. api_get_setting('siteName'),
  225. $additionalParameters['userUsername'],
  226. ]
  227. );
  228. break;
  229. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_REGISTERED:
  230. return $this->buildSms(
  231. $plugin,
  232. $tpl,
  233. 'course_opening_request_code_registered.tpl',
  234. 'XCourseOpeningRequestCodeXRegistered',
  235. [
  236. api_get_setting('siteName'),
  237. $additionalParameters['courseCode'],
  238. ]
  239. );
  240. break;
  241. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_APPROVED:
  242. return $this->buildSms(
  243. $plugin,
  244. $tpl,
  245. 'course_opening_request_course_code_approved.tpl',
  246. 'XCourseOpeningRequestCourseCodeXApproved',
  247. [
  248. api_get_setting('siteName'),
  249. $additionalParameters['courseCode'],
  250. ]
  251. );
  252. break;
  253. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_REJECTED:
  254. return $this->buildSms(
  255. $plugin,
  256. $tpl,
  257. 'request_open_course_code_rejected.tpl',
  258. 'XRequestOpenCourseCodeXReject',
  259. [
  260. api_get_setting('siteName'),
  261. $additionalParameters['courseCode'],
  262. ]
  263. );
  264. break;
  265. case SmsPlugin::COURSE_OPENING_REQUEST_CODE:
  266. return $this->buildSms(
  267. $plugin,
  268. $tpl,
  269. 'course_opening_request_course_code.tpl',
  270. 'XCourseOpeningRequestCourseCodeX',
  271. [
  272. api_get_setting('siteName'),
  273. $additionalParameters['courseCode'],
  274. ]
  275. );
  276. break;
  277. case SmsPlugin::BEEN_SUBSCRIBED_COURSE:
  278. return $this->buildSms(
  279. $plugin,
  280. $tpl,
  281. 'been_subscribed_course.tpl',
  282. 'XBeenSubscribedCourseX',
  283. [
  284. api_get_setting('siteName'),
  285. $additionalParameters['courseTitle'],
  286. ]
  287. );
  288. break;
  289. case SmsPlugin::ASSIGNMENT_BEEN_CREATED_COURSE:
  290. return $this->buildSms(
  291. $plugin,
  292. $tpl,
  293. 'assignment_been_created_course.tpl',
  294. 'XAssignmentBeenCreatedCourseX',
  295. [
  296. api_get_setting('siteName'),
  297. $additionalParameters['courseTitle'],
  298. ]
  299. );
  300. break;
  301. // Message types to be implemented. Fill the array parameter with arguments.
  302. /*case SmsPlugin::ACCOUNT_CREATED_UPDATED_LOGIN_PASSWORD:
  303. return $this->buildSms(
  304. $plugin,
  305. $tpl,
  306. 'account_created_updated_login_password.tpl',
  307. 'XAccountCreatedUpdatedLoginXPasswordX',
  308. array(
  309. api_get_setting('siteName')
  310. )
  311. );
  312. break;*/
  313. /*case SmsPlugin::PASSWORD_UPDATED_LOGIN_PASSWORD:
  314. return $this->buildSms(
  315. $plugin,
  316. $tpl,
  317. 'password_updated_login_password.tpl',
  318. 'XPasswordUpdatedLoginXPasswordX',
  319. array(
  320. api_get_setting('siteName')
  321. )
  322. );
  323. break;*/
  324. /*case SmsPlugin::REQUESTED_PASSWORD_CHANGE:
  325. return $this->buildSms(
  326. $plugin,
  327. $tpl,
  328. 'requested_password_change.tpl',
  329. 'XPasswordUpdatedLoginXPasswordX',
  330. array(
  331. api_get_setting('siteName')
  332. )
  333. );
  334. break;*/
  335. /*case SmsPlugin::RECEIVED_NEW_PERSONAL_MESSAGES:
  336. return $this->buildSms(
  337. $plugin,
  338. $tpl,
  339. 'received_new_personal_messages.tpl',
  340. 'XReceivedNewPersonalMessages',
  341. array(
  342. api_get_setting('siteName')
  343. )
  344. );
  345. break;*/
  346. /*case SmsPlugin::NEW_USER_PENDING_APPROVAL:
  347. return $this->buildSms(
  348. $plugin,
  349. $tpl,
  350. 'new_user_pending_approval.tpl',
  351. 'XNewUserXPendingApproval',
  352. array(
  353. api_get_setting('siteName')
  354. )
  355. );
  356. break;*/
  357. /*case SmsPlugin::POSTED_FORUM_COURSE:
  358. return $this->buildSms(
  359. $plugin,
  360. $tpl,
  361. 'posted_forum_course.tpl',
  362. 'XXPostedForumXCourseX',
  363. array(
  364. api_get_setting('siteName')
  365. )
  366. );
  367. break;*/
  368. /*case SmsPlugin::CHECK_EMAIL_CONNECT_MORE_INFO:
  369. return $this->buildSms(
  370. $plugin,
  371. $tpl,
  372. 'check_email_connect_more_info.tpl',
  373. 'XXXCheckEmailConnectMoreInfo',
  374. array(
  375. api_get_setting('siteName')
  376. )
  377. );
  378. break;*/
  379. /*case SmsPlugin::STUDENT_ANSWERED_TEST:
  380. return $this->buildSms(
  381. $plugin,
  382. $tpl,
  383. 'student_answered_test.tpl',
  384. 'XXStudentXAnsweredTestX',
  385. array(
  386. api_get_setting('siteName')
  387. )
  388. );
  389. break;*/
  390. /*case SmsPlugin::STUDENT_ANSWERED_TEST_OPEN_QUESTION:
  391. return $this->buildSms(
  392. $plugin,
  393. $tpl,
  394. 'student_answered_test_open_question.tpl',
  395. 'XXStudentXAnsweredTestXOpenQuestion',
  396. array(
  397. api_get_setting('siteName')
  398. )
  399. );
  400. break;*/
  401. /*case SmsPlugin::STUDENT_ANSWERED_TEST_VOICE_QUESTION:
  402. return $this->buildSms(
  403. $plugin,
  404. $tpl,
  405. 'student_answered_test_voice_question.tpl',
  406. 'XXStudentXAnsweredTestXVoiceQuestion',
  407. array(
  408. api_get_setting('siteName')
  409. )
  410. );
  411. break;*/
  412. /*case SmsPlugin::ANSWER_OPEN_QUESTION_TEST_REVIEWED:
  413. return $this->buildSms(
  414. $plugin,
  415. $tpl,
  416. 'answer_open_question_test_reviewed.tpl',
  417. 'XXAnswerOpenQuestionTestXReviewed',
  418. array(
  419. api_get_setting('siteName')
  420. )
  421. );
  422. break;*/
  423. /*case SmsPlugin::NEW_THREAD_STARTED_FORUM:
  424. return $this->buildSms(
  425. $plugin,
  426. $tpl,
  427. 'new_thread_started_forum.tpl',
  428. 'XXNewThreadXStartedForumX',
  429. array(
  430. api_get_setting('siteName')
  431. )
  432. );
  433. break;*/
  434. /*case SmsPlugin::NEW_ANSWER_POSTED_FORUM:
  435. return $this->buildSms(
  436. $plugin,
  437. $tpl,
  438. 'new_answer_posted_forum.tpl',
  439. 'XXNewAnswerPostedXForumX',
  440. array(
  441. api_get_setting('siteName')
  442. )
  443. );
  444. break;*/
  445. /*case SmsPlugin::NEW_SYSTEM_ANNOUNCEMENT_ADDED:
  446. return $this->buildSms(
  447. $plugin,
  448. $tpl,
  449. 'new_system_announcement_added.tpl',
  450. 'XXNewSystemAnnouncementAdded',
  451. array(
  452. api_get_setting('siteName')
  453. )
  454. );
  455. break;*/
  456. /*case SmsPlugin::TEST_NEW_SYSTEM_ANNOUNCEMENT_ADDED:
  457. return $this->buildSms(
  458. $plugin,
  459. $tpl,
  460. 'test_new_system_announcement_added.tpl',
  461. 'XTestXNewSystemAnnouncementAdded',
  462. array(
  463. api_get_setting('siteName')
  464. )
  465. );
  466. break;*/
  467. /*case SmsPlugin::SYSTEM_ANNOUNCEMENT_UPDATE:
  468. return $this->buildSms(
  469. $plugin,
  470. $tpl,
  471. 'system_announcement_update.tpl',
  472. 'XXSystemAnnouncementUpdate',
  473. array(
  474. api_get_setting('siteName')
  475. )
  476. );
  477. break;*/
  478. /*case SmsPlugin::TEST_SYSTEM_ANNOUNCEMENT_UPDATE:
  479. return $this->buildSms(
  480. $plugin,
  481. $tpl,
  482. 'test_system_announcement_update.tpl',
  483. 'XXSystemAnnouncementUpdate',
  484. array(
  485. api_get_setting('siteName')
  486. )
  487. );
  488. break;*/
  489. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_COURSE_STUDENT_SUBMITS_PAPER:
  490. return $this->buildSms(
  491. $plugin,
  492. $tpl,
  493. 'user_uploaded_assignment_course_student_submits_paper.tpl',
  494. 'XUserXUploadedAssignmentXCourseXStudentSubmitsPaper',
  495. array(
  496. api_get_setting('siteName')
  497. )
  498. );
  499. break;*/
  500. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_CHECK_STUDENT_SUBMITS_PAPER:
  501. return $this->buildSms(
  502. $plugin,
  503. $tpl,
  504. 'user_uploaded_assignment_check_student_submits_paper.tpl',
  505. 'XUserXUploadedAssignmentXCheckXStudentSubmitsPaper',
  506. array(
  507. api_get_setting('siteName')
  508. )
  509. );
  510. break;*/
  511. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_COURSE:
  512. return $this->buildSms(
  513. $plugin,
  514. $tpl,
  515. 'user_uploaded_assignment_course.tpl',
  516. 'XUserXUploadedAssignmentXCourseX',
  517. array(
  518. api_get_setting('siteName')
  519. )
  520. );
  521. break;*/
  522. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_CHECK:
  523. return $this->buildSms(
  524. $plugin,
  525. $tpl,
  526. 'user_uploaded_assignment_check.tpl',
  527. 'XUserXUploadedAssignmentXCheckX',
  528. array(
  529. api_get_setting('siteName')
  530. )
  531. );
  532. break;*/
  533. /*case SmsPlugin::SUBSCRIBED_SESSION:
  534. return $this->buildSms(
  535. $plugin,
  536. $tpl,
  537. 'subscribed_session.tpl',
  538. 'XSubscribedSessionX',
  539. array(
  540. api_get_setting('siteName')
  541. )
  542. );
  543. break;*/
  544. /*case SmsPlugin::SUBSCRIBED_SESSION_CSV:
  545. return $this->buildSms(
  546. $plugin,
  547. $tpl,
  548. 'subscribed_session_csv.tpl',
  549. 'XSubscribedSessionXCSV',
  550. array(
  551. api_get_setting('siteName')
  552. )
  553. );
  554. break;*/
  555. /*case SmsPlugin::USER_SUGGESTED_BE_FRIENDS:
  556. return $this->buildSms(
  557. $plugin,
  558. $tpl,
  559. 'user_suggested_be_friends.tpl',
  560. 'XUserXSuggestedBeFriends',
  561. array(
  562. api_get_setting('siteName')
  563. )
  564. );
  565. break;*/
  566. /*case SmsPlugin::USER_ANSWERED_INBOX_MESSAGE:
  567. return $this->buildSms(
  568. $plugin,
  569. $tpl,
  570. 'user_answered_inbox_message.tpl',
  571. 'XUserXAnsweredInboxMessage',
  572. array(
  573. api_get_setting('siteName')
  574. )
  575. );
  576. break;*/
  577. /*case SmsPlugin::BEEN_INVITED_JOIN_GROUP:
  578. return $this->buildSms(
  579. $plugin,
  580. $tpl,
  581. 'been_invited_join_group.tpl',
  582. 'XBeenInvitedJoinGroupX',
  583. array(
  584. api_get_setting('siteName')
  585. )
  586. );
  587. break;*/
  588. /*case SmsPlugin::MESSAGES_SENT_EDITED_GROUP_EDITED:
  589. return $this->buildSms(
  590. $plugin,
  591. $tpl,
  592. 'messages_sent_edited_group_edited.tpl',
  593. 'XMessagesSentEditedGroupXEdited',
  594. array(
  595. api_get_setting('siteName')
  596. )
  597. );
  598. break;*/
  599. /*case SmsPlugin::MESSAGES_SENT_EDITED_GROUP_ADDED:
  600. return $this->buildSms(
  601. $plugin,
  602. $tpl,
  603. 'messages_sent_edited_group_added.tpl',
  604. 'XMessagesSentEditedGroupXAdded',
  605. array(
  606. api_get_setting('siteName')
  607. )
  608. );
  609. break;*/
  610. /*case SmsPlugin::BEEN_INVITED_COMPLETE_SURVEY_COURSE:
  611. return $this->buildSms(
  612. $plugin,
  613. $tpl,
  614. 'been_invited_complete_survey_course.tpl',
  615. 'XBeenInvitedCompleteSurveyXCourseX',
  616. array(
  617. api_get_setting('siteName')
  618. )
  619. );
  620. break;*/
  621. /*case SmsPlugin::REMINDER_ASSIGNMENT_COURSE_DUE:
  622. return $this->buildSms(
  623. $plugin,
  624. $tpl,
  625. 'reminder_assignment_course_due.tpl',
  626. 'XReminderAssignmentXCourseXDue',
  627. array(
  628. api_get_setting('siteName')
  629. )
  630. );
  631. break;*/
  632. /*case SmsPlugin::USER_DETAILS_MODIFIED:
  633. return $this->buildSms(
  634. $plugin,
  635. $tpl,
  636. 'user_details_modified.tpl',
  637. 'XUserDetailsModified',
  638. array(
  639. api_get_setting('siteName')
  640. )
  641. );
  642. break;*/
  643. default:
  644. return '';
  645. }
  646. }
  647. }