kannelsms.lib.php 25 KB

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