PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cms/content.lib.php

https://github.com/akash6190/pragyan
PHP | 326 lines | 307 code | 2 blank | 17 comment | 12 complexity | d0856529df43c28842275e0fbe5b1f57 MD5 | raw file
  1. <?php
  2. if(!defined('__PRAGYAN_CMS'))
  3. {
  4. header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
  5. echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
  6. echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
  7. exit(1);
  8. }
  9. /**
  10. * @package pragyan
  11. * @copyright (c) 2010 Pragyan Team
  12. * @license http://www.gnu.org/licenses/ GNU Public License
  13. * For more details, see README
  14. */
  15. /**
  16. * Find page type, and module component id. See if the module is consistent with standards.
  17. * Give the action and module component id to module.
  18. */
  19. /**TODO: Make sure a newly created page or renamed page does not have a . in its name. -> clashes with .php, .jpg etc
  20. *
  21. *Actions which are taken care of from here only : login, logout, profile
  22. *Actions in "page" module : login, logout, profile, admin, groupadmin, settings, grant
  23. */
  24. function getContent($pageId, $action, $userId, $permission, $recursed=0) {
  25. if($action=="login") {
  26. if($userId==0) {
  27. ///Commented the requirement of login.lib.php because it is already included in /index.php
  28. //require_once("login.lib.php");
  29. $newUserId = login();
  30. if(is_numeric($newUserId))
  31. return getContent($pageId, "view", $newUserId, getPermissions($newUserId,$pageId,"view"), 0);
  32. else
  33. return $newUserId; ///<The login page
  34. } else
  35. displayinfo("You are logged in as ".getUserName($userId)."! Click <a href=\"./+logout\">here</a> to logout.");
  36. return getContent($pageId, "view", $userId, getPermissions($userId,$pageId,"view"), $recursed=0);
  37. }
  38. if($action=="profile") {
  39. if($userId != 0) {
  40. require_once("profile.lib.php");
  41. return profile($userId);
  42. } else
  43. displayinfo("You need to <a href=\"./+login\">login</a> to view your profile.!");
  44. }
  45. if($action=="logout") {
  46. if($userId!=0) {
  47. $newUserId=resetAuth();
  48. displayinfo("You have been logged out!");
  49. global $openid_enabled;
  50. if($openid_enabled=='true')
  51. displaywarning("If you logged in via Open ID, make sure you also log out from your Open ID service provider's website. Until then your session in this website will remain active !");
  52. return getContent($pageId, "view", $newUserId, getPermissions($newUserId,$pageId,"view"), 0);
  53. } else
  54. displayinfo("You need to <a href=\"./+login\">login</a> first to logout!");
  55. }
  56. if($action=="search") {
  57. require_once("search.lib.php");
  58. $ret = getSearchBox();
  59. if(isset($_POST['query']))
  60. $ret .= getSearchResultString($_POST['query']);
  61. elseif(isset($_GET['query']))
  62. $ret .= getSearchResultString($_GET['query']);
  63. return $ret;
  64. }
  65. if (isset($_GET['subaction']) && $_GET['subaction'] == 'getchildren') {
  66. if (isset($_GET['parentpath'])) {
  67. global $urlRequestRoot;
  68. require_once('menu.lib.php');
  69. $pidarr = Array();
  70. parseUrlReal(escape($_GET['parentpath']), $pidarr);
  71. $pid = $pidarr[count($pidarr) - 1];
  72. $children = getChildren($pid, $userId);
  73. $response = array();
  74. $response['path'] = escape($_GET['parentpath']);
  75. $response['items'] = array();
  76. foreach ($children as $child)
  77. $response['items'][] = array($urlRequestRoot . '/home' . escape($_GET['parentpath']) . $child[1], $child[2]);
  78. //echo json_encode($response);
  79. exit();
  80. }
  81. }
  82. if($permission!=true) {
  83. if($userId==0) $suggestion = "(Try <a href=\"./+login\">logging in?</a>)";
  84. else $suggestion = "";
  85. displayerror("You do not have the permissions to view this page. $suggestion<br /><input type=\"button\" onclick=\"history.go(-1)\" value=\"Go back\" />");
  86. return '';
  87. }
  88. if($action=="admin") {
  89. require_once("admin.lib.php");
  90. return admin($pageId,$userId);
  91. }
  92. ///default actions also to be defined here (and not outside)
  93. /// Coz work to be done after these actions do involve the page
  94. $pagetype_query = "SELECT page_module, page_modulecomponentid FROM ".MYSQL_DATABASE_PREFIX."pages WHERE page_id='".escape($pageId)."'";
  95. $pagetype_result = mysql_query($pagetype_query);
  96. $pagetype_values = mysql_fetch_assoc($pagetype_result);
  97. if(!$pagetype_values) {
  98. displayerror("The requested page does not exist.");
  99. return "";
  100. }
  101. $moduleType = $pagetype_values['page_module'];
  102. $moduleComponentId = $pagetype_values['page_modulecomponentid'];
  103. if($action=="settings") {///<done here because we needed to check if the page exists for sure.
  104. require_once("pagesettings.lib.php");
  105. return pagesettings($pageId,$userId);
  106. }
  107. if($action=="widgets")
  108. {
  109. return handleWidgetPageSettings($pageId);
  110. }
  111. if($recursed==0) {
  112. $pagetypeupdate_query = "UPDATE ".MYSQL_DATABASE_PREFIX."pages SET page_lastaccesstime=NOW() WHERE page_id='".escape($pageId)."'";
  113. $pagetypeupdate_result = mysql_query($pagetypeupdate_query);
  114. if(!$pagetypeupdate_result)
  115. return '<div class="cms-error">Error No. 563 - An error has occured. Contact the site administators.</div>';
  116. }
  117. if($moduleType=="link")
  118. return getContent($moduleComponentId,$action,$userId,true,1);
  119. if($action=="grant") {
  120. return grantPermissions($userId, $pageId);
  121. }
  122. if($moduleType=="menu")
  123. return getContent(getParentPage($pageId),$action,$userId,true,1);
  124. if($moduleType=="external") {
  125. $query = "SELECT `page_extlink` FROM `".MYSQL_DATABASE_PREFIX."external` WHERE `page_modulecomponentid` =
  126. (SELECT `page_modulecomponentid` FROM `".MYSQL_DATABASE_PREFIX."pages` WHERE `page_id`= '".escape($pageId)."')";
  127. $result = mysql_query($query);
  128. $values = mysql_fetch_array($result);
  129. $link=$values[0];
  130. header("Location: $link");
  131. }
  132. global $sourceFolder;
  133. global $moduleFolder;
  134. require_once($sourceFolder."/".$moduleFolder."/".$moduleType.".lib.php");
  135. $page = new $moduleType();
  136. if(!($page instanceof module)){
  137. displayerror("The module \"$moduleType\" does not implement the inteface module</div>");
  138. return "";
  139. }
  140. $createperms_query = " SELECT * FROM ".MYSQL_DATABASE_PREFIX."permissionlist where perm_action = 'create' AND page_module = '".$moduleType."'";
  141. $createperms_result = mysql_query($createperms_query);
  142. if(mysql_num_rows($createperms_result)<1) {
  143. displayerror("The action \"create\" does not exist in the module \"$moduleType\"</div>");
  144. return "";
  145. }
  146. $availableperms_query = "SELECT * FROM ".MYSQL_DATABASE_PREFIX."permissionlist where perm_action != 'create' AND page_module = '".$moduleType."'";
  147. $availableperms_result = mysql_query($availableperms_query);
  148. $permlist = array();
  149. while ($value=mysql_fetch_assoc($availableperms_result)) {
  150. array_push($permlist,$value['perm_action']);
  151. }
  152. array_push($permlist,"view");
  153. $class_methods = get_class_methods($moduleType);
  154. foreach($permlist as $perm) {
  155. if(!in_array("action".ucfirst($perm),$class_methods))
  156. {
  157. displayerror("The action \"$perm\" does not exist in the module \"$moduleType\"</div>");
  158. return "";
  159. }
  160. }
  161. if($action=="pdf")
  162. {
  163. if(isset($_GET['depth']))
  164. $depth=$_GET['depth'];
  165. else $depth=0;
  166. if(!is_numeric($depth))
  167. {
  168. $depth=0;
  169. }
  170. global $TITLE;
  171. global $sourceFolder;
  172. require_once("$sourceFolder/modules/pdf/html2fpdf.php");
  173. $pdf=new HTML2FPDF();
  174. $pdf->setModuleComponentId($moduleComponentId);
  175. $pdf->AddPage();
  176. $pdf->WriteHTML($page->getHtml($userId,$moduleComponentId,"view"));
  177. $cp=array();
  178. $j=0;
  179. if($depth == -1)
  180. {
  181. $cp=child($pageId,$userId,$depth);
  182. if($cp[0][0])
  183. {
  184. for($i=0 ; $cp[$i][0] != NULL ; $i++)
  185. {
  186. require_once($sourceFolder."/".$moduleFolder."/".$cp[$i][2].".lib.php");
  187. $page1 = new $cp[$i][2]();
  188. $modCompId = $cp[$i][5];
  189. $pdf->setModuleComponentId($modCompId);
  190. $pdf->AddPage();
  191. $pdf->WriteHTML($page1->getHtml($userId,$modCompId,"view"));
  192. }
  193. }
  194. }
  195. else if ($depth>0)
  196. {
  197. $cp=child($pageId,$userId,$depth);
  198. --$depth;
  199. while($depth>0)
  200. {
  201. $count = count($cp);
  202. for($j; $j<$count; $j++)
  203. {
  204. $cp=array_merge((array)$cp,(array)child($cp[$j][0],$userId,$depth));
  205. }
  206. --$depth;
  207. }
  208. if($cp[0][0])
  209. {
  210. for($i=0 ; isset($cp[$i]) ; $i++)
  211. {
  212. require_once($sourceFolder."/".$moduleFolder."/".$cp[$i][2].".lib.php");
  213. $page1 = new $cp[$i][2]();
  214. $modCompId = $cp[$i][5];
  215. $pdf->setModuleComponentId($modCompId);
  216. $pdf->AddPage();
  217. $pdf->WriteHTML($page1->getHtml($userId,$modCompId,"view"));
  218. }
  219. }
  220. }
  221. $filePath = $sourceFolder . "/uploads/temp/" . $TITLE . ".pdf";
  222. while(file_exists($filePath))
  223. $filePath = $sourceFolder . "/uploads/temp/" . $TITLE."-".rand() . ".pdf";
  224. $pdf->Output($filePath);
  225. header("Pragma: public");
  226. header("Expires: 0");
  227. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  228. header("Cache-Control: private",false);
  229. header("Content-Type: application/pdf");
  230. header("Content-Disposition: attachment; filename=\"".basename($filePath)."\";" );
  231. header("Content-Transfer-Encoding: binary");
  232. header("Content-Length: ".filesize($filePath));
  233. @readfile("$filePath");
  234. unlink($filePath);
  235. }
  236. return $page->getHtml($userId, $moduleComponentId, $action);
  237. }
  238. /**
  239. * To get title bar text
  240. */
  241. function getTitle($pageId,$action, &$heading) {
  242. if($action=="login" || $action == "logout") {
  243. $heading = ucfirst($action);
  244. return true;
  245. }
  246. $pagetitle_query = "SELECT `page_title`, `page_module`, `page_modulecomponentid`, `page_displaypageheading` FROM `".MYSQL_DATABASE_PREFIX."pages` WHERE `page_id`='".$pageId."'";
  247. $pagetitle_result = mysql_query($pagetitle_query);
  248. if (!$pagetitle_result)
  249. return false;
  250. $pagetitle_values = mysql_fetch_assoc($pagetitle_result);
  251. if ($pagetitle_values['page_displaypageheading'] == 0)
  252. return false;
  253. //if($pagetitle_values['page_module']=="link") return getTitle($pagetitle_values['page_modulecomponentid'],$action);
  254. //A link has its own page title, page menurank, display menubar property
  255. if ($action=="grant") $heading = $pagetitle_values['page_title']." - Grant Permissions";
  256. else if ($action=="settings") $heading = $pagetitle_values['page_title']." - Page Settings";
  257. else if ($action=="admin") $heading = $pagetitle_values['page_title']." - Website Administration";
  258. else if ($action=="widget") $heading = $pagetitle_values['page_title']." - Page Widgets";
  259. else if ($action=="profile") $heading = $pagetitle_values['page_title']." - User Profile";
  260. else $heading = $pagetitle_values['page_title'];
  261. return true;
  262. }
  263. function child($pageId, $userId,$depth) {
  264. $pageId=escape($pageId);
  265. if($depth < 0)
  266. {
  267. $childrenQuery = 'SELECT `page_id`, `page_name`, `page_title`, `page_module`, `page_modulecomponentid`, `page_displayinmenu`, `page_image` , `page_displayicon` FROM `' . MYSQL_DATABASE_PREFIX . 'pages` WHERE `page_id` != \'' . $pageId . '\' AND `page_displayinmenu` = 1 ORDER BY `page_menurank`';
  268. }
  269. else
  270. {
  271. $childrenQuery = 'SELECT `page_id`, `page_name`, `page_title`, `page_module`, `page_modulecomponentid`, `page_displayinmenu`, `page_image` , `page_displayicon` FROM `' . MYSQL_DATABASE_PREFIX . 'pages` WHERE `page_parentid` = \'' . $pageId . '\' AND `page_id` != \'' . $pageId . '\' AND `page_displayinmenu` = 1 ORDER BY `page_menurank`';
  272. }
  273. $childrenResult = mysql_query($childrenQuery);
  274. $children = array();
  275. while ($childrenRow = mysql_fetch_assoc($childrenResult))
  276. if ($childrenRow['page_displayinmenu'] == true && getPermissions($userId, $childrenRow['page_id'], 'view', $childrenRow['page_module']) == true)
  277. $children[] = array($childrenRow['page_id'], $childrenRow['page_name'], $childrenRow['page_module'], $childrenRow['page_image'],$childrenRow['page_displayicon'],$childrenRow['page_modulecomponentid']);
  278. return $children;
  279. }
  280. /**
  281. * The interface to be followed by each module. In addition to this, each module needs to have
  282. * a function with the name actionAction for each action. (eg: actionView, actionEdit named functions)
  283. */
  284. interface module {
  285. public function getHtml($userId, $moduleComponentId, $action);
  286. public function deleteModule($moduleComponentId);
  287. public function copyModule($moduleComponentId,$newModuleComponentId);
  288. public function createModule($moduleComponentId);
  289. }
  290. interface fileuploadable {
  291. /**
  292. * Should return true in case file viewing allowed, false if not allowed
  293. */
  294. public static function getFileAccessPermission($pageId,$moduleComponentId,$userId,$fileName);
  295. }